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

From: Vlastimil Babka (SUSE)

Date: Fri Nov 27 2026 - 15:12:46 EST


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@xxxxxxxxxx>
Fixes: 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves")
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
---
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