[PATCH v2 4/4] mm/slab: clear stale layout values in calculate_sizes()

From: Vlastimil Babka (SUSE)

Date: Tue Dec 01 2026 - 13:16:27 EST


From: Chris Mason <mason@xxxxxxxxxx>

With O in slab_debug, SLUB can place the slabobj_ext of an object out
of bounds: in another object, or past the end of the slab.

If debug metadata raises the minimum order of a cache,
do_kmem_cache_create() drops the metadata flags and runs
calculate_sizes() again, which has to recompute the whole layout. But
calculate_sizes() only ever sets SLAB_OBJ_EXT_IN_OBJ and s->red_left_pad
when they apply and never resets, so the values of the first run stay,
and alloc_slab_obj_exts_early() uses both. For kmalloc-cg-4k with
slab_debug=FZPUO, 4K pages and no KASAN:

first run second run
========= ==========
s->size 12288 4096
alignment padding yes none
SLAB_OBJ_EXT_IN_OBJ set still set
s->red_left_pad 4096 still 4096

slabobj_ext of object i
= slab + s->red_left_pad + s->inuse + i * s->size
= object i + 2

The fix used here is to reset both values before the second run. The
s->offset is already reset that way.

[vbabka: consolidate the resets in the caller]

Fixes: a77d6d338685 ("mm/slab: place slabobj_ext metadata in unused space within s->size")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Co-developed-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
Reviewed-by: Harry Yoo (Meta) <harry@xxxxxxxxxx>
---
mm/slub.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/mm/slub.c b/mm/slub.c
index 4e29720ee62d..a3d5d3dbf8a5 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -8871,10 +8871,19 @@ int do_kmem_cache_create(struct kmem_cache *s, const char *name,
* order increased.
*/
if (get_order(s->size) > get_order(s->object_size)) {
+
s->flags &= ~DEBUG_METADATA_FLAGS;
+ /*
+ * Reset fields that the previous run may have set and
+ * now might be different.
+ */
+ s->flags &= ~SLAB_OBJ_EXT_IN_OBJ;
s->offset = 0;
+ s->red_left_pad = 0;
+
/* Created as unmergeable, so it has to stay so */
s->flags |= SLAB_NO_MERGE;
+
if (!calculate_sizes(args, s))
goto out;
}

--
2.56.1