Skip to content

Commit 0135feb

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 16bdb9c commit 0135feb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

mm/slab.h

+2
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

+14-2
Original file line numberDiff line numberDiff line change
@@ -2824,8 +2824,14 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
28242824
if (s->ctor)
28252825
s->ctor(object);
28262826
kasan_poison_object_data(s, object);
2827-
} else if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object)
2827+
} else if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object) {
28282828
memset(object, 0, s->object_size);
2829+
if (s->ctor) {
2830+
kasan_unpoison_object_data(s, object);
2831+
s->ctor(object);
2832+
kasan_poison_object_data(s, object);
2833+
}
2834+
}
28292835

28302836
if (object) {
28312837
check_canary(s, object, s->random_inactive);
@@ -3269,8 +3275,14 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
32693275
} else if (unlikely(slab_want_init_on_alloc(flags, s))) {
32703276
int j;
32713277

3272-
for (j = 0; j < i; j++)
3278+
for (j = 0; j < i; j++) {
32733279
memset(p[j], 0, s->object_size);
3280+
if (s->ctor) {
3281+
kasan_unpoison_object_data(s, p[j]);
3282+
s->ctor(p[j]);
3283+
kasan_poison_object_data(s, p[j]);
3284+
}
3285+
}
32743286
}
32753287

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

0 commit comments

Comments
 (0)