Skip to contentSkip to conversation

slab AI fixes

PATCH In review Vlastimil Babka (SUSE) 3 participants 13 messages Nov 27 – Nov 30

Subsystems (3)
lore
Share & tools
lore

Vlastimil Babka (SUSE)

author maintainer
#0 lore raw

cover slab AI fixes

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.

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

author maintainer
#1 lore raw

PATCH 1/4 mm/slab: drop __GFP_NOFAIL when refilling sheaves

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.

Fixes 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves")
---
 mm/slub.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/slub.c +8 -1
index 145a32c334cb..5fd148f11efd 100644
@@ -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
Signature · 1 line
2.56.1
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] mm/slab: drop __GFP_NOFAIL when refilling sheaves

Quoted 6 lines · Vlastimil Babka (SUSE)

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

s/__GFP_MEMALLOC/__GFP_NOMEMALLOC/

Quoted 36 lines

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.

Quoted 5 lines
Fixes 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves")

---

Looks good to me,

Quoted 33 lines
 mm/slub.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/slub.c +8 -1
index 145a32c334cb..5fd148f11efd 100644
@@ -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
Signature · 2 lines
-- 
2.56.1
Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#3 lore raw

PATCH 2/4 mm/slab: handle __GFP_NOFAIL better with prefilled sheaves

In reply to Vlastimil Babka (SUSE)

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.

Fixes 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves")
---
 mm/slub.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/slub.c +10 -2
index 5fd148f11efd..20570e4f1795 100644
@@ -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;
Signature · 1 line
2.56.1
#4 lore raw

Re: [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves

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

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) <[email protected]>

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().

Quoted 5 lines
mm/slub.c +0 -0
index 5fd148f11efd1..20570e4f1795e 100644

mm/slub.c static int __prefill_sheaf_pfmemalloc(struct kmem_cache *s, · line 5212

Quoted 10 lines
mm/slub.c +3 -1
@@ -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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#5 lore raw

Re: [PATCH 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves

In reply to Vlastimil Babka (SUSE)
Quoted 11 lines · Vlastimil Babka (SUSE)

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

Quoted 2 lines

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

Hmm the underlying problem here is that slab can't tell if it might be allocated from memory reserves or not just by looking at the GFP flags?

We should probably set sheaf->pfmemalloc to true in refill_objects() when it ended up allocating a pfmemalloc slab and refilled objects from it.

Quoted 19 lines

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.

Fixes 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves")
---
 mm/slub.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/slub.c +0 -0
index 5fd148f11efd..20570e4f1795 100644

mm/slub.c static int __prefill_sheaf_pfmemalloc(struct kmem_cache *s, · line 5207

Quoted 5 lines
mm/slub.c +1 -1
@@ -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)) {

As pointed out by Sashiko gfp_pfmemalloc_allowed could still return false with __GFP_NOFAIL. __GFP_NOFAIL is not included in __gfp_pfmemalloc_flags() but it _may_ end up setting ALLOC_NO_WATERMARKS.

Quoted 30 lines
mm/slub.c +9 -1
+		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;
Signature · 2 lines
-- 
2.56.1
Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#6 lore raw

PATCH 3/4 mm/slab: keep caches that drop debug flags unmergeable

In reply to Vlastimil Babka (SUSE)

From: Chris Mason <[email protected]>

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")
---
 mm/slub.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/slub.c +2 -4
index 20570e4f1795..9bb031f9a41f 100644
@@ -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.
Signature · 1 line
2.56.1
#7 lore raw

Re: [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable

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() --

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 <[email protected]>

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().

mm/slub.c int do_kmem_cache_create(struct kmem_cache *s, const char *name, · line 8870

Quoted 12 lines
mm/slub.c +2 -0
index 20570e4f1795e..9bb031f9a41fd 100644
@@ -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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] mm/slab: keep caches that drop debug flags unmergeable

In reply to Vlastimil Babka (SUSE)
Quoted 36 lines · Vlastimil Babka (SUSE)

From: Chris Mason <[email protected]>

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]

Quoted 7 lines
Fixes e17f1dfba37b ("mm, slub: extend slub_debug syntax for multiple blocks")

---

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#9 lore raw

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

In reply to Vlastimil Babka (SUSE)

From: Chris Mason <[email protected]>

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")
---
 mm/slub.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/slub.c +8 -0
index 9bb031f9a41f..003023b6f8dc 100644
@@ -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 */
Signature · 1 line
2.56.1
#10 lore raw

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

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

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 <[email protected]>

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.

Quoted 11 lines
mm/slub.c +2 -0
index 9bb031f9a41fd..003023b6f8dc6 100644
@@ -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;
+			/*

mm/slub.c int do_kmem_cache_create(struct kmem_cache *s, const char *name, · line 8875

Quoted 10 lines
mm/slub.c +6 -0
+			 * 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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#11 lore raw

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

Quoted 52 lines · On Fri, Nov 27, 2026 at 09:16:00PM +0000, [email protected] wrote:

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 <[email protected]>

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.

Quoted 11 lines
mm/slub.c +2 -0
index 9bb031f9a41fd..003023b6f8dc6 100644
@@ -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;
+			/*

mm/slub.c int do_kmem_cache_create(struct kmem_cache *s, const char *name, · line 8875

Quoted 10 lines
mm/slub.c +6 -0
+			 * 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;
    ...
}
Quoted 6 lines

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.

True but I don't really care TBH :)
It's fine either way (to me).

Signature · 2 lines
Cheers,
Harry / Hyeonggon
maintainer Reviewed-by on v1 4/4
#12 lore raw

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

In reply to Vlastimil Babka (SUSE)
Quoted 29 lines · Vlastimil Babka (SUSE)

From: Chris Mason <[email protected]>

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]

Quoted 7 lines
Fixes a77d6d338685 ("mm/slab: place slabobj_ext metadata in unused space within s->size")

---

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon