Skip to content

Commit

Permalink
slab: __GFP_ZERO is incompatible with a constructor
Browse files Browse the repository at this point in the history
__GFP_ZERO requests that the object be initialised to all-zeroes, while
the purpose of a constructor is to initialise an object to a particular
pattern.  We cannot do both.  Add a warning to catch any users who
mistakenly pass a __GFP_ZERO flag when allocating a slab with a
constructor.

Link: http://lkml.kernel.org/r/[email protected]
Fixes: d07dbea ("Slab allocators: support __GFP_ZERO in all allocators")
Signed-off-by: Matthew Wilcox <[email protected]>
Acked-by: Johannes Weiner <[email protected]>
Acked-by: Vlastimil Babka <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Reviewed-by: Andrew Morton <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Matthew Wilcox authored and torvalds committed Jun 8, 2018
1 parent e56ee57 commit 128227e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mm/slab.c
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@ static struct page *cache_grow_begin(struct kmem_cache *cachep,
invalid_mask, &invalid_mask, flags, &flags);
dump_stack();
}
WARN_ON_ONCE(cachep->ctor && (flags & __GFP_ZERO));
local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);

check_irq_off();
Expand Down Expand Up @@ -3071,6 +3072,7 @@ static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
gfp_t flags, void *objp, unsigned long caller)
{
WARN_ON_ONCE(cachep->ctor && (flags & __GFP_ZERO));
if (!objp)
return objp;
if (cachep->flags & SLAB_POISON) {
Expand Down
4 changes: 3 additions & 1 deletion mm/slob.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,10 @@ static void *slob_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
flags, node);
}

if (b && c->ctor)
if (b && c->ctor) {
WARN_ON_ONCE(flags & __GFP_ZERO);
c->ctor(b);
}

kmemleak_alloc_recursive(b, c->size, 1, c->flags, flags);
return b;
Expand Down
2 changes: 2 additions & 0 deletions mm/slub.c
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,8 @@ static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
struct kmem_cache_cpu *c = *pc;
struct page *page;

WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));

freelist = get_partial(s, flags, node, c);

if (freelist)
Expand Down

0 comments on commit 128227e

Please sign in to comment.