Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] slab AI fixes
@ 2026-11-27 20:12 Vlastimil Babka (SUSE)
  2026-11-27 20:12 ` [PATCH 1/4] mm/slab: drop __GFP_NOFAIL when refilling sheaves Vlastimil Babka (SUSE)
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-11-27 20:12 UTC (permalink / raw)
  To: Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, linux-kernel,
	Vlastimil Babka (SUSE), Chris Mason, stable, stable

A bunch of fixes that are marked for stable but not really urgent, based
on patches from Chris Mason, dispatched to slab maintainers by David.

This batch is based on (and intended for) slab/for-7.5/misc

Two fixes for sheaf refill with __GFP_NOFAIL allocations and sheaf
preallocation. Those I've rewritten completely compared to the original
proposals. May improve slab behavior a bit under low memory conditions.

Two fixes related to the rather obscure slub_debug=O boot option, which
can in theory negatively suprise someone trying to use that mode.

Nothing of this should be an exploitable vulnerability, really.

Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
Chris Mason (2):
      mm/slab: keep caches that drop debug flags unmergeable
      mm/slab: clear stale layout values in calculate_sizes()

Vlastimil Babka (SUSE) (2):
      mm/slab: drop __GFP_NOFAIL when refilling sheaves
      mm/slab: handle __GFP_NOFAIL better with prefilled sheaves

 mm/slub.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)
---
base-commit: 152bb61a6134d1d2b736cead67f653ce57ba559b
change-id: 20261127-slab-ai-fixes-a0b04d767c9b



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/4] mm/slab: drop __GFP_NOFAIL when refilling sheaves
  2026-11-27 20:12 [PATCH 0/4] slab AI fixes Vlastimil Babka (SUSE)
@ 2026-11-27 20:12 ` Vlastimil Babka (SUSE)
  2026-11-27 20:12 ` [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves Vlastimil Babka (SUSE)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-11-27 20:12 UTC (permalink / raw)
  To: Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, linux-kernel,
	Vlastimil Babka (SUSE), Chris Mason, stable

A __GFP_NOFAIL slab allocation in a low memory situation may end up
refilling a whole sheaf from slabs (possibly newly allocated), passing
the __GFP_NOFAIL to all underlying allocations. This can make the low
memory condition worse and it's unnecessary, given there's a fallback.

Given how we use __GFP_MEMALLOC for the refill, there are also some
undesirable corner cases, as reported by Chris Mason. If the allocation
happens in a PF_MEMALLOC task (which it should not in the first place,
but there's no guarantee), the following can occur:

    refill_sheaf(gfp | __GFP_NOMEMALLOC)
      __alloc_pages_slowpath()
	WARN_ON_ONCE(nofail && PF_MEMALLOC)
        retry:
        reserve_flags = 0           /* __GFP_NOMEMALLOC */
        goto nopage                 /* PF_MEMALLOC */
        goto retry                  /* __GFP_NOFAIL */

The allocation will loop without reclaim and no access to memory
reserves.

Additionally the slab page may be marked pfmemalloc, which the sheaf
refill does not expect and handle:

    refill_objects()              __GFP_NOFAIL | __GFP_NOMEMALLOC
      new_slab()
        __alloc_pages_may_oom()
          if (gfp_mask & __GFP_NOFAIL)
            ALLOC_NO_WATERMARKS   the slab is then pfmemalloc
      alloc_from_new_slab()       its objects go into the sheaf

Improve or prevent all of the above by dropping __GFP_NOFAIL for the
sheaf refill. Also perform all adjustments before potentially calling
alloc_empty_sheaf(). While that one already clears __GFP_NOFAIL, it
should also get the __GFP_NOMEMALLOC | __GFP_NOWARN flags, since a
fallback exists.

One consequence is that the WARN_ON_ONCE(nofail && PF_MEMALLOC) in the
page allocator will now only be reachable from the fallback and thus in
case such a caller is introduced, might lurk undetected for a while. If
people think it's an issue, we can duplicate the warning here as well.

Reported-by: Chris Mason <mason@kernel.org>
Fixes: 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves")
Cc: <stable@vger.kernel.org>
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
 mm/slub.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/mm/slub.c b/mm/slub.c
index 145a32c334cb..5fd148f11efd 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -4807,13 +4807,20 @@ __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
 	if (!allow_spin)
 		return NULL;
 
+	/*
+	 * We don't want refills to rely on __GFP_NOFAIL or memalloc reserves to
+	 * succeed, nor to warn on failure. Leave that to the fallback.
+	 */
+	gfp &= ~__GFP_NOFAIL;
+	gfp |= __GFP_NOMEMALLOC | __GFP_NOWARN;
+
 	if (!empty) {
 		empty = alloc_empty_sheaf(s, gfp, alloc_flags);
 		if (!empty)
 			return NULL;
 	}
 
-	if (refill_sheaf(s, empty, gfp | __GFP_NOMEMALLOC | __GFP_NOWARN)) {
+	if (refill_sheaf(s, empty, gfp)) {
 		/*
 		 * we must be very low on memory so don't bother
 		 * with the barn

-- 
2.56.1




^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves
  2026-11-27 20:12 [PATCH 0/4] slab AI fixes Vlastimil Babka (SUSE)
  2026-11-27 20:12 ` [PATCH 1/4] mm/slab: drop __GFP_NOFAIL when refilling sheaves Vlastimil Babka (SUSE)
@ 2026-11-27 20:12 ` Vlastimil Babka (SUSE)
  2026-11-27 21:15   ` sashiko-bot
  2026-11-27 20:12 ` [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable Vlastimil Babka (SUSE)
  2026-11-27 20:12 ` [PATCH 4/4] mm/slab: clear stale layout values in calculate_sizes() Vlastimil Babka (SUSE)
  3 siblings, 1 reply; 8+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-11-27 20:12 UTC (permalink / raw)
  To: Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, linux-kernel,
	Vlastimil Babka (SUSE), Chris Mason, stable

If a prefilled sheaf is requested with __GFP_NOFAIL in a
gfp_pfmemalloc_allowed() context, the following can happen in the
initial __GFP_NOMEMALOC attempt.

    refill_objects()              __GFP_NOFAIL | __GFP_NOMEMALLOC
      new_slab()
        __alloc_pages_may_oom()
          if (gfp_mask & __GFP_NOFAIL)
            ALLOC_NO_WATERMARKS   the slab is pfmemalloc
      alloc_from_new_slab()       its objects go into the sheaf

So the sheaf should be marked pfmemalloc, but isn't. Fix this by
removing __GFP_NOFAIL from the __GFP_NOMEMALLOC attempt.

For consistency, also adjust kmem_cache_return_sheaf() in case somebody
would hypothetically call it with __GFP_NOFAIL. Just drop it
unconditionally and also add __GFP_NORETRY there to make returns
cheaper - in a low memory situation, keeping the sheaf from being
flushed isn't a priority.

Reported-by: Chris Mason <mason@kernel.org>
Fixes: 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves")
Cc: <stable@vger.kernel.orG>
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
 mm/slub.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 5fd148f11efd..20570e4f1795 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5204,8 +5204,10 @@ static int __prefill_sheaf_pfmemalloc(struct kmem_cache *s,
 		return 0;
 
 	gfp_nomemalloc = gfp | __GFP_NOMEMALLOC;
-	if (gfp_pfmemalloc_allowed(gfp))
+	if (gfp_pfmemalloc_allowed(gfp)) {
+		gfp_nomemalloc &= ~__GFP_NOFAIL;
 		gfp_nomemalloc |= __GFP_NOWARN;
+	}
 
 	ret = refill_sheaf(s, sheaf, gfp_nomemalloc);
 
@@ -5345,12 +5347,18 @@ void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
 
 	stat(s, SHEAF_RETURN_SLOW);
 
+	/*
+	 * If we end up refilling the sheaf, don't try too hard.
+	 */
+	gfp &= ~__GFP_NOFAIL;
+	gfp |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
+
 	/*
 	 * If the barn has too many full sheaves or we fail to refill the sheaf,
 	 * simply flush and free it.
 	 */
 	if (!barn || data_race(barn->nr_full) >= MAX_FULL_SHEAVES ||
-	    refill_sheaf(s, sheaf, gfp | __GFP_NOMEMALLOC | __GFP_NOWARN)) {
+	    refill_sheaf(s, sheaf, gfp)) {
 		sheaf_flush_unused(s, sheaf);
 		free_empty_sheaf(s, sheaf);
 		return;

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable
  2026-11-27 20:12 [PATCH 0/4] slab AI fixes Vlastimil Babka (SUSE)
  2026-11-27 20:12 ` [PATCH 1/4] mm/slab: drop __GFP_NOFAIL when refilling sheaves Vlastimil Babka (SUSE)
  2026-11-27 20:12 ` [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves Vlastimil Babka (SUSE)
@ 2026-11-27 20:12 ` Vlastimil Babka (SUSE)
  2026-11-27 21:15   ` sashiko-bot
  2026-11-27 20:12 ` [PATCH 4/4] mm/slab: clear stale layout values in calculate_sizes() Vlastimil Babka (SUSE)
  3 siblings, 1 reply; 8+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-11-27 20:12 UTC (permalink / raw)
  To: Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, linux-kernel,
	Vlastimil Babka (SUSE), Chris Mason, stable

From: Chris Mason <mason@kernel.org>

When slab_debug boot parameter contains the O flag and a list of caches,
two caches can get the same id in /sys/kernel/slab, so sysfs warns about
a duplicate filename and one of them is missing there as a result.

create_unique_id() builds the id of a mergeable cache from its size and
flags, so caches that are mergeable and result in the same unique id
must get merged - otherwise a duplicate id would try to be created.

But find_mergeable() doesn't merge a new cache that has (at that point)
debug flags, and with O do_kmem_cache_create() later drops the flags
that store metadata if they raise the minimum slab order, and the cache
can become mergeable again, but too late. An example with the id:

    slab_debug=OU,sgpool-128

    biovec-max   mergeable                              :0004096
    sgpool-128   has U, so not merged into biovec-max
                 later U dropped, so mergeable          :0004096

sysfs_slab_add() has a check for such caches since commit 11066386efa6
("slub: fix sysfs duplicate filename creation when slub_debug=O") but it
tests only the global slub_debug, and flags given for a list of caches
don't end up there.

The fix used here is to mark the cache unmergeable when its debug flags
are dropped. With that, we can also effectively revert commit
11066386efa6 since the global slub_debug is covered by this fix too.

Ideally we would evaluate mergeability with the O flag precisely
(dropping the U soon enough to merge) but that would be a more involved
change and probably not worth the effort.

[vbabka: revert 11066386efa6, tweak commit message]

Fixes: e17f1dfba37b ("mm, slub: extend slub_debug syntax for multiple blocks")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Co-developed-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
 mm/slub.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 20570e4f1795..9bb031f9a41f 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -8864,6 +8864,8 @@ int do_kmem_cache_create(struct kmem_cache *s, const char *name,
 			s->offset = 0;
 			if (!calculate_sizes(args, s))
 				goto out;
+			/* Created as unmergeable, so it has to stay so */
+			s->flags |= SLAB_NO_MERGE;
 		}
 	}
 
@@ -9825,10 +9827,6 @@ static int sysfs_slab_add(struct kmem_cache *s)
 	struct kset *kset = cache_kset(s);
 	int unmergeable = slab_unmergeable(s);
 
-	if (!unmergeable && disable_higher_order_debug &&
-			(slub_debug & DEBUG_METADATA_FLAGS))
-		unmergeable = 1;
-
 	if (unmergeable) {
 		/*
 		 * Slabcache can never be merged so we can use the name proper.

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/4] mm/slab: clear stale layout values in calculate_sizes()
  2026-11-27 20:12 [PATCH 0/4] slab AI fixes Vlastimil Babka (SUSE)
                   ` (2 preceding siblings ...)
  2026-11-27 20:12 ` [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable Vlastimil Babka (SUSE)
@ 2026-11-27 20:12 ` Vlastimil Babka (SUSE)
  2026-11-27 21:16   ` sashiko-bot
  3 siblings, 1 reply; 8+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-11-27 20:12 UTC (permalink / raw)
  To: Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, linux-kernel,
	Vlastimil Babka (SUSE), Chris Mason, stable

From: Chris Mason <mason@kernel.org>

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@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Co-developed-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
 mm/slub.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mm/slub.c b/mm/slub.c
index 9bb031f9a41f..003023b6f8dc 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -8860,8 +8860,16 @@ 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;
+
 			if (!calculate_sizes(args, s))
 				goto out;
 			/* Created as unmergeable, so it has to stay so */

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves
  2026-11-27 20:12 ` [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves Vlastimil Babka (SUSE)
@ 2026-11-27 21:15   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-27 21:15 UTC (permalink / raw)
  To: Vlastimil Babka (SUSE), Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, Chris Mason

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/slub: __prefill_sheaf_pfmemalloc() misses __GFP_NOFAIL
  without pfmemalloc
--

commit 7b605e7e1980fd6063399368212254f7119ec46c
Author: Vlastimil Babka (SUSE) <vbabka@kernel.org>

mm/slab: handle __GFP_NOFAIL better with prefilled sheaves

This commit strips __GFP_NOFAIL from the initial __GFP_NOMEMALLOC sheaf
refill attempt in __prefill_sheaf_pfmemalloc() when pfmemalloc is allowed,
and also clears __GFP_NOFAIL and adds __GFP_NORETRY in
kmem_cache_return_sheaf().

> diff --git a/mm/slub.c b/mm/slub.c
> index 5fd148f11efd1..20570e4f1795e 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -5204,8 +5204,10 @@ static int __prefill_sheaf_pfmemalloc(struct kmem_cache *s,
>  		return 0;
>  
>  	gfp_nomemalloc = gfp | __GFP_NOMEMALLOC;
> -	if (gfp_pfmemalloc_allowed(gfp))
> +	if (gfp_pfmemalloc_allowed(gfp)) {
> +		gfp_nomemalloc &= ~__GFP_NOFAIL;
>  		gfp_nomemalloc |= __GFP_NOWARN;
> +	}
>  
>  	ret = refill_sheaf(s, sheaf, gfp_nomemalloc);

[Severity: Medium]
Can a __GFP_NOFAIL refill still allocate a pfmemalloc slab without marking
sheaf->pfmemalloc when gfp_pfmemalloc_allowed(gfp) is false?

Callers such as mas_nomem_nofail() -> mas_alloc_nodes() pass
GFP_KERNEL | __GFP_NOFAIL in normal process context, where
gfp_pfmemalloc_allowed(gfp) returns false because __gfp_pfmemalloc_flags()
only checks __GFP_MEMALLOC, PF_MEMALLOC and oom_reserves_allowed(current).

In that case __GFP_NOFAIL remains set in gfp_nomemalloc.  If direct
reclaim fails, __alloc_pages_may_oom() grants ALLOC_NO_WATERMARKS to
__GFP_NOFAIL regardless of __GFP_NOMEMALLOC:

mm/page_alloc.c:__alloc_pages_may_oom() {
	...
		if (gfp_mask & __GFP_NOFAIL)
			page = __alloc_pages_cpuset_fallback(gfp_mask, order,
					ac->alloc_flags|ALLOC_NO_WATERMARKS, ac);
	...
}

The resulting slab is marked pfmemalloc, but refill_sheaf() succeeds on the
first attempt and returns with sheaf->pfmemalloc left false, allowing
kmem_cache_return_sheaf() to place pfmemalloc objects into pcs->spare or
the node barn.  Even if __GFP_NOFAIL is stripped unconditionally from
gfp_nomemalloc, the fallback condition (!ret || !gfp_pfmemalloc_allowed(gfp))
below would still return -ENOMEM without retrying unless it also checks for
gfp & __GFP_NOFAIL.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-slab-ai-fixes-v1-0-cf13b6be6692@kernel.org?part=2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable
  2026-11-27 20:12 ` [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable Vlastimil Babka (SUSE)
@ 2026-11-27 21:15   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-27 21:15 UTC (permalink / raw)
  To: Vlastimil Babka (SUSE), Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, Chris Mason

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm/slub: SLAB_NO_MERGE set after second calculate_sizes() in
  do_kmem_cache_create()
--

commit d25c80897a858b81158f0edaa6a941c6407fc597
Author: Chris Mason <mason@kernel.org>

mm/slab: keep caches that drop debug flags unmergeable

This commit sets SLAB_NO_MERGE on slab caches that drop debug metadata flags
due to disable_higher_order_debug, and removes the global slub_debug check
from sysfs_slab_add().

> diff --git a/mm/slub.c b/mm/slub.c
> index 20570e4f1795e..9bb031f9a41fd 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -8864,6 +8864,8 @@ int do_kmem_cache_create(struct kmem_cache *s, const char *name,
>  			s->offset = 0;
>  			if (!calculate_sizes(args, s))
>  				goto out;
> +			/* Created as unmergeable, so it has to stay so */
> +			s->flags |= SLAB_NO_MERGE;
>  		}
>  	}

[Severity: Low]
Should SLAB_NO_MERGE be set before calling calculate_sizes() a second time?

calculate_sizes() checks slab_args_unmergeable(args, s->flags) when deciding
whether to set SLAB_OBJ_EXT_IN_OBJ:

mm/slub.c:calculate_sizes() {
    ...
	aligned_size = ALIGN(size, s->align);
#if defined(CONFIG_SLAB_OBJ_EXT) && defined(CONFIG_64BIT)
	if (slab_args_unmergeable(args, s->flags) &&
			(aligned_size - size >= cache_obj_ext_size(s)))
		s->flags |= SLAB_OBJ_EXT_IN_OBJ;
#endif
    ...
}

When DEBUG_METADATA_FLAGS are cleared before the second calculate_sizes()
call, a cache that was unmergeable only because of those debug flags will
appear mergeable to slab_args_unmergeable() during that recalculation and
miss setting SLAB_OBJ_EXT_IN_OBJ when alignment padding is large enough.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-slab-ai-fixes-v1-0-cf13b6be6692@kernel.org?part=3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 4/4] mm/slab: clear stale layout values in calculate_sizes()
  2026-11-27 20:12 ` [PATCH 4/4] mm/slab: clear stale layout values in calculate_sizes() Vlastimil Babka (SUSE)
@ 2026-11-27 21:16   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-27 21:16 UTC (permalink / raw)
  To: Vlastimil Babka (SUSE), Harry Yoo
  Cc: Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin,
	David Hildenbrand (Arm), linux-mm, Chris Mason

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm/slab: late SLAB_NO_MERGE in do_kmem_cache_create() skips
  obj_ext in object
--

commit e36d70bf4b2fe1e8cc089823e91c1a5b0b9817ea
Author: Chris Mason <mason@kernel.org>

mm/slab: clear stale layout values in calculate_sizes()

This commit resets SLAB_OBJ_EXT_IN_OBJ and s->red_left_pad in
do_kmem_cache_create() before recalculating cache sizes when debug metadata
flags are dropped due to an increased minimum slab order.

> diff --git a/mm/slub.c b/mm/slub.c
> index 9bb031f9a41fd..003023b6f8dc6 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -8860,8 +8860,16 @@ 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;
> +
>  			if (!calculate_sizes(args, s))
>  				goto out;
>  			/* Created as unmergeable, so it has to stay so */

[Severity: Low]
Should SLAB_NO_MERGE be set on s->flags before the second calculate_sizes()
call rather than after it?

calculate_sizes() only sets SLAB_OBJ_EXT_IN_OBJ when the cache is
unmergeable:

mm/slub.c:calculate_sizes() {
    ...
	if (slab_args_unmergeable(args, s->flags) &&
			(aligned_size - size >= cache_obj_ext_size(s)))
		s->flags |= SLAB_OBJ_EXT_IN_OBJ;
    ...
}

For a cache that was only unmergeable because of DEBUG_METADATA_FLAGS,
clearing DEBUG_METADATA_FLAGS and SLAB_OBJ_EXT_IN_OBJ before setting
SLAB_NO_MERGE makes slab_args_unmergeable(args, s->flags) return false
during the second calculate_sizes() call.  Even when the non-debug object
layout still has enough alignment padding, SLAB_OBJ_EXT_IN_OBJ is not
re-enabled and slab allocations fall back to separate slabobj_ext arrays.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-slab-ai-fixes-v1-0-cf13b6be6692@kernel.org?part=4


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-27 21:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 20:12 [PATCH 0/4] slab AI fixes Vlastimil Babka (SUSE)
2026-11-27 20:12 ` [PATCH 1/4] mm/slab: drop __GFP_NOFAIL when refilling sheaves Vlastimil Babka (SUSE)
2026-11-27 20:12 ` [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves Vlastimil Babka (SUSE)
2026-11-27 21:15   ` sashiko-bot
2026-11-27 20:12 ` [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable Vlastimil Babka (SUSE)
2026-11-27 21:15   ` sashiko-bot
2026-11-27 20:12 ` [PATCH 4/4] mm/slab: clear stale layout values in calculate_sizes() Vlastimil Babka (SUSE)
2026-11-27 21:16   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox