Skip to content

Commit a21fe7b

Browse files
committed
slab: validate slab before using it in alloc_single_from_partial()
We touch slab->freelist and slab->inuse before checking the slab pointer is actually sane. Do that validation first, which will be safer. We can thus also remove the check from alloc_debug_processing(). This adds a new "s->flags & SLAB_CONSISTENCY_CHECKS" test but alloc_single_from_partial() is only called for caches with debugging enabled so it's acceptable. In alloc_single_from_new_slab() we just created the struct slab and call alloc_debug_processing() to mainly set up redzones, tracking etc, while not really expecting the consistency checks to fail. Thus don't validate it there. Reviewed-by: Harry Yoo <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent 40522db commit a21fe7b

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

mm/slub.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,8 @@ static inline unsigned int get_orig_size(struct kmem_cache *s, void *object)
821821
return *(unsigned int *)p;
822822
}
823823

824+
#ifdef CONFIG_SLUB_DEBUG
825+
824826
/*
825827
* For debugging context when we want to check if the struct slab pointer
826828
* appears to be valid.
@@ -830,7 +832,6 @@ static inline bool validate_slab_ptr(struct slab *slab)
830832
return PageSlab(slab_page(slab));
831833
}
832834

833-
#ifdef CONFIG_SLUB_DEBUG
834835
static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)];
835836
static DEFINE_SPINLOCK(object_map_lock);
836837

@@ -1651,11 +1652,6 @@ static noinline bool alloc_debug_processing(struct kmem_cache *s,
16511652
struct slab *slab, void *object, int orig_size)
16521653
{
16531654
if (s->flags & SLAB_CONSISTENCY_CHECKS) {
1654-
if (!validate_slab_ptr(slab)) {
1655-
slab_err(s, slab, "Not a valid slab page");
1656-
return false;
1657-
}
1658-
16591655
if (!alloc_consistency_checks(s, slab, object))
16601656
goto bad;
16611657
}
@@ -2825,13 +2821,21 @@ static void *alloc_single_from_partial(struct kmem_cache *s,
28252821

28262822
lockdep_assert_held(&n->list_lock);
28272823

2824+
#ifdef CONFIG_SLUB_DEBUG
2825+
if (s->flags & SLAB_CONSISTENCY_CHECKS) {
2826+
if (!validate_slab_ptr(slab)) {
2827+
slab_err(s, slab, "Not a valid slab page");
2828+
return NULL;
2829+
}
2830+
}
2831+
#endif
2832+
28282833
object = slab->freelist;
28292834
slab->freelist = get_freepointer(s, object);
28302835
slab->inuse++;
28312836

28322837
if (!alloc_debug_processing(s, slab, object, orig_size)) {
2833-
if (validate_slab_ptr(slab))
2834-
remove_partial(n, slab);
2838+
remove_partial(n, slab);
28352839
return NULL;
28362840
}
28372841

0 commit comments

Comments
 (0)