From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796148976; b=EHPRhL2eNZET9AaOandvNUy/aOawq52G/ZmXweTtu9iPS6bLvVaGiB1biAYHyiLiMqQ1 ibU3B1xBS9C4CcEpMAbUZXpPw56LvnYFHYSHwA7Q9t7Q91S+Ie88EGcMJqM+ovo7mKZdM TA9L86XmP510UJTVQFBMnlqUV4PJc+jNooqBtIElPQORrhhqKoaZWENctXGwZOEnNT1cf JQqO9D+ttT7gAAjD+YE7rPpw5SfocuFo1RuDR0yam+qHW5/Dso49ampJ09xSn+mnIrj6G vSucIYHQB0vQVIq+hRSq0dtqMVQwwpn/zhwR3OzuzjkVLMZIlcwQ1XBcC4KUmHu13Cg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796148976; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=8ArSKJbEGH9WiyddCxdhjinZZ+mKG8BuPGYv2k6rZyE=; b=tdOgoVxysjHevDDOJWD5r5fEzoWueHManrfyAl8Jar6yux7hN88Z83FlmC45VQtDe6xm Obkf5zBs39qaQq8pZS4TXap95j2WPepGmpzmq00g7lxoOwkoF+jJFcsuaP3kt5XGDP50t toKQNgnPD4sfn0bea8Scc5tz0vIudWbwUYjUraUh11kHHdm4vblKTkiC01eIizVM931ce CJtCK+3bg8ADPcThW8EdbVLYjLgCQ4dle+cFAISIeI4M5NfiCh4aidKRZqA0DEtNPaVKy v55ScYsZ7W97KffnCKlE48XRW5qcwLQo2MPWNmAmjY6j3lhubxoYhKfDUFZqQQr5+fA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MNbJEThj; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 772E2606A32D; Tue, 1 Dec 2026 18:16:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MNbJEThj Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8A6B1F0089A; Tue, 1 Dec 2026 18:16:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796148976; bh=8ArSKJbEGH9WiyddCxdhjinZZ+mKG8BuPGYv2k6rZyE=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=MNbJEThjeguSDxrnAQt4unMXUMNM6tcrr3dJ6HLhnvEFMXPtD+/zedbhcT8lM8SNq xcW7bppAScTd/gpJgHSOJuLarsFcXm+puLrkXF4BTV9R7JUHptkHtDX228AutOk7FS PXUVRCvVktEUFXUjnr/lPT1RsFrADs8FlLRZOO9lm4Bgfh+NVvubBycppLg4HcI4mz z7GSsI6xb/icXuwDr9DVHozT+RlMhcSkoRW5y/pQHww9NIn24/Tc8LJXoObMF+x4Jj 9ynT5IIGs3DV0H1fctVLqA0ZdlFjEE1ETxoqFXUEWYrF84clPQWvDZbH3Vh9pLoJN8 jW443ZcZ3N/ww== From: "Vlastimil Babka (SUSE)" Date: Tue, 01 Dec 2026 19:16:03 +0100 Subject: [PATCH v2 2/4] mm/slab: handle __GFP_NOFAIL better with prefilled sheaves X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261201-slab-ai-fixes-v2-2-7419dfbd3bac@kernel.org> References: <20261201-slab-ai-fixes-v2-0-7419dfbd3bac@kernel.org> In-Reply-To: <20261201-slab-ai-fixes-v2-0-7419dfbd3bac@kernel.org> To: Harry Yoo Cc: Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , "David Hildenbrand (Arm)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Vlastimil Babka (SUSE)" , Chris Mason , stable@vger.kernel.orG X-Mailer: b4 0.16.0 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 precisely determining in the refill path, whether a pfmemalloc slab was used to refill the sheaf, whether it was on the partial list or newly allocated. Additionally, remove the __GFP_NOFAIL from the initial nomemalloc attempt so it really cannot end up with a pfmemalloc slab, as intended. 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 Fixes: 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves") Cc: Signed-off-by: Vlastimil Babka (SUSE) --- mm/slub.c | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 5fd148f11efd..0927ffbb1b5e 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2909,7 +2909,7 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf) static unsigned int refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, - unsigned int max); + unsigned int max, bool *pfmemalloc); static int refill_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf, gfp_t gfp) @@ -2921,7 +2921,7 @@ static int refill_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf, return 0; filled = refill_objects(s, &sheaf->objects[sheaf->size], gfp, to_fill, - to_fill); + to_fill, &sheaf->pfmemalloc); sheaf->size += filled; @@ -5197,6 +5197,7 @@ static bool refill_sheaf_from_barn(struct kmem_cache *s, static int __prefill_sheaf_pfmemalloc(struct kmem_cache *s, struct slab_sheaf *sheaf, gfp_t gfp) { + bool memalloc_allowed = false; gfp_t gfp_nomemalloc; int ret; @@ -5204,20 +5205,23 @@ 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 & __GFP_NOFAIL) { + memalloc_allowed = true; + gfp_nomemalloc &= ~__GFP_NOFAIL; gfp_nomemalloc |= __GFP_NOWARN; + } ret = refill_sheaf(s, sheaf, gfp_nomemalloc); - if (likely(!ret || !gfp_pfmemalloc_allowed(gfp))) + if (likely(!ret || !memalloc_allowed)) return ret; /* - * if we are allowed to, refill sheaf with pfmemalloc but then remember - * it for when it's returned + * If we are allowed to, refill sheaf with pfmemalloc or __GFP_NOFAIL. + * This will mark the sheaf as pfmemalloc if it gets refilled with at + * least one object from a pfmemalloc slab. */ ret = refill_sheaf(s, sheaf, gfp); - sheaf->pfmemalloc = true; return ret; } @@ -5345,12 +5349,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; @@ -7377,7 +7387,7 @@ EXPORT_SYMBOL(kmem_cache_free_bulk); static unsigned int __refill_objects_node(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, unsigned int max, struct kmem_cache_node *n, - bool allow_spin) + bool allow_spin, bool *pfmemalloc) { struct partial_bulk_context pc; struct slab *slab, *slab2; @@ -7400,6 +7410,9 @@ __refill_objects_node(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int mi object = get_freelist_nofreeze(s, slab, &count); + if (slab_test_pfmemalloc(slab)) + *pfmemalloc = true; + while (count && refilled < max) { p[refilled] = object; object = get_freepointer(s, object); @@ -7450,7 +7463,7 @@ __refill_objects_node(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int mi #ifdef CONFIG_NUMA static unsigned int __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, - unsigned int max) + unsigned int max, bool *pfmemalloc) { struct zonelist *zonelist; struct zoneref *z; @@ -7478,7 +7491,8 @@ __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min continue; r = __refill_objects_node(s, p, gfp, min, max, n, - /* allow_spin = */ false); + /* allow_spin = */ false, + pfmemalloc); refilled += r; if (r >= min) { @@ -7501,7 +7515,7 @@ __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min #else static inline unsigned int __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, - unsigned int max) + unsigned int max, bool *pfmemalloc) { return 0; } @@ -7509,7 +7523,7 @@ __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min static unsigned int refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, - unsigned int max) + unsigned int max, bool *pfmemalloc) { int local_node = numa_mem_id(); unsigned int refilled; @@ -7517,12 +7531,13 @@ refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, refilled = __refill_objects_node(s, p, gfp, min, max, get_node(s, local_node), - /* allow_spin = */ true); + /* allow_spin = */ true, + pfmemalloc); if (refilled >= min) return refilled; refilled += __refill_objects_any(s, p + refilled, gfp, min - refilled, - max - refilled); + max - refilled, pfmemalloc); if (refilled >= min) return refilled; @@ -7537,6 +7552,9 @@ refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, refilled += alloc_from_new_slab(s, slab, p + refilled, max - refilled, /* allow_spin = */ true); + if (slab_test_pfmemalloc(slab)) + *pfmemalloc = true; + if (refilled < min) goto new_slab; @@ -7547,6 +7565,7 @@ refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min, static bool __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, void **p) { + bool pfmemalloc; int i; if (IS_ENABLED(CONFIG_SLUB_TINY) || kmem_cache_debug(s)) { @@ -7564,7 +7583,7 @@ static bool __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, maybe_wipe_obj_freeptr(s, p[i]); } } else { - i = refill_objects(s, p, flags, size, size); + i = refill_objects(s, p, flags, size, size, &pfmemalloc); if (i < size) goto error; stat_add(s, ALLOC_SLOWPATH, i); -- 2.56.1