Skip to content

Commit

Permalink
slub: Extend init_on_alloc to slab caches with constructors
Browse files Browse the repository at this point in the history
Signed-off-by: Thibaut Sautereau <thibaut.sautereau@ssi.gouv.fr>
Signed-off-by: Levente Polyak <levente@leventepolyak.net>
  • Loading branch information
tsautereau-anssi authored and anthraxx committed Mar 23, 2022
1 parent 776e69a commit 59a2f4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mm/slab.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,10 @@ static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
{
if (static_branch_unlikely(&init_on_alloc)) {
#ifndef CONFIG_SLUB
if (c->ctor)
return false;
#endif
if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
return flags & __GFP_ZERO;
return true;
Expand Down
16 changes: 14 additions & 2 deletions mm/slub.c
Original file line number Diff line number Diff line change
Expand Up @@ -2967,8 +2967,14 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
if (s->ctor)
s->ctor(object);
kasan_poison_object_data(s, object);
} else if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object)
} else if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object) {
memset(object, 0, s->object_size);
if (s->ctor) {
kasan_unpoison_object_data(s, object);
s->ctor(object);
kasan_poison_object_data(s, object);
}
}

if (object) {
check_canary(s, object, s->random_inactive);
Expand Down Expand Up @@ -3430,8 +3436,14 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
} else if (unlikely(slab_want_init_on_alloc(flags, s))) {
int j;

for (j = 0; j < i; j++)
for (j = 0; j < i; j++) {
memset(p[j], 0, s->object_size);
if (s->ctor) {
kasan_unpoison_object_data(s, p[j]);
s->ctor(p[j]);
kasan_poison_object_data(s, p[j]);
}
}
}

for (k = 0; k < i; k++) {
Expand Down

0 comments on commit 59a2f4f

Please sign in to comment.