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 <[email protected]>
Signed-off-by: Levente Polyak <[email protected]>
  • Loading branch information
tsautereau-anssi authored and anthraxx committed Jan 2, 2020
1 parent 16bdb9c commit 0135feb
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 @@ -683,8 +683,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 @@ -2824,8 +2824,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 @@ -3269,8 +3275,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 0135feb

Please sign in to comment.