Skip to content

Commit 932258a

Browse files
tsautereau-anssianthraxx
authored andcommitted
slub: Extend init_on_alloc to slab caches with constructors
Signed-off-by: Thibaut Sautereau <[email protected]> Signed-off-by: Levente Polyak <[email protected]>
1 parent 922fadc commit 932258a

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

mm/slab.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,10 @@ static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
683683
static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
684684
{
685685
if (static_branch_unlikely(&init_on_alloc)) {
686+
#ifndef CONFIG_SLUB
686687
if (c->ctor)
687688
return false;
689+
#endif
688690
if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
689691
return flags & __GFP_ZERO;
690692
return true;

mm/slub.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,8 +2843,14 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
28432843
if (s->ctor)
28442844
s->ctor(object);
28452845
kasan_poison_object_data(s, object);
2846-
} else if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object)
2846+
} else if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object) {
28472847
memset(object, 0, s->object_size);
2848+
if (s->ctor) {
2849+
kasan_unpoison_object_data(s, object);
2850+
s->ctor(object);
2851+
kasan_poison_object_data(s, object);
2852+
}
2853+
}
28482854

28492855
if (object) {
28502856
check_canary(s, object, s->random_inactive);
@@ -3288,8 +3294,14 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
32883294
} else if (unlikely(slab_want_init_on_alloc(flags, s))) {
32893295
int j;
32903296

3291-
for (j = 0; j < i; j++)
3297+
for (j = 0; j < i; j++) {
32923298
memset(p[j], 0, s->object_size);
3299+
if (s->ctor) {
3300+
kasan_unpoison_object_data(s, p[j]);
3301+
s->ctor(p[j]);
3302+
kasan_poison_object_data(s, p[j]);
3303+
}
3304+
}
32933305
}
32943306

32953307
for (k = 0; k < i; k++) {

0 commit comments

Comments
 (0)