From nobody Wed Jun 17 01:33:32 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 659E43D8128 for ; Tue, 21 Apr 2026 14:50:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776783000; cv=none; b=ldvcKdiAezo5eQHXBwvGLtnWd20CaWrrneTgPwKAUk5J5KRRQGjCXdquakkdrlGv3o4xM/E8XZoac9Uu0Qts5/L4pCl+CJGPh93GEIC2P9choa0obA7scRoOORt/4m1MoPlSa4azRYNFssv7rnInJIlKYWI06vB18gM0NFZgpD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776783000; c=relaxed/simple; bh=4dlPrU8yFUaDBn0M4T8t9tx0LHAIhkl8Hlx1Qxruau8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=D/+TYjEBv4ciz3EIi1L/VemqmOcgng/UaOnhvWezHdHSYd6riXSSIkEUZuTHXDdh9m+VM4yxPztLiC2BQj3gJV0pvBcPxoHyIPWRrgQcN2562F6OopSeodEokkxszRJBcy9xpaaAotpmxG4wMik6kLRe14ii81D2iNJ1eV3XsCU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oZxvH+b8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oZxvH+b8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05C60C2BCB6; Tue, 21 Apr 2026 14:49:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776783000; bh=4dlPrU8yFUaDBn0M4T8t9tx0LHAIhkl8Hlx1Qxruau8=; h=From:Date:Subject:To:Cc:From; b=oZxvH+b8ZlT7ophr0Aw/Gb8XXvVck8gU0A0pjZXtK5cggQZGAJ2Set2stRddi2vcQ awLh0j599WbSKlR6PreGNP20Ejpz0AADLTKifhMTUgUiAGybJVRHNQUjgayzauRQWl 5Of7LPP92tdrQBBNUTIz9zyByhPSF1370LY+isBcOfy92yj7TprGXarUzhJSo76rbl 6LFOQYR0rsweptYyUPtKv7AufcUuFkC1vZEgZfIetqIsIlOTV1AYTwzr5fN2nvbou/ X/SpLmNWVxYS5cWY4W7Npa/Fn9C5lnfJUI1njEHfCSkTpyQKk75tJZgt4oYXWHecld MPIls4QP5WocA== From: "Vlastimil Babka (SUSE)" Date: Tue, 21 Apr 2026 16:49:52 +0200 Subject: [PATCH RFC] mm, slab: add an optimistic __slab_try_return_freelist() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260421-b4-refill-optimistic-return-v1-1-24f0bfc1acff@kernel.org> X-B4-Tracking: v=1; b=H4sIAI+O52kC/yWMwQqCQBBAf0Xm3IBuQ1HXoA/wGh7acawJW2Vnl UD899Y6vsfjLWASVQzOxQJRZjUdQoZqVwA/7+EhqG1mcKU7lOQq9IRROu17HMakb7WknE2aYsC OqN37IzHTCfJh3MrP736D+nqB5i9t8i/htH1hXb+cheDDhAAAAA== X-Change-ID: 20260421-b4-refill-optimistic-return-f44d3b74cc49 To: Harry Yoo Cc: Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, hu.shengming@zte.com.cn, Vinicius Costa Gomes , "Vlastimil Babka (SUSE)" X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6503; i=vbabka@kernel.org; h=from:subject:message-id; bh=4dlPrU8yFUaDBn0M4T8t9tx0LHAIhkl8Hlx1Qxruau8=; b=owEBiQF2/pANAwAIAbvgsHXSRYiaAcsmYgBp546T+OHvSI+A1IGjQ7RTjXxVbAUXST8qJtnBh yLtAXdqAWeJAU8EAAEIADkWIQR7u8hBFZkjSJZITfG74LB10kWImgUCaeeOkxsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwyLDIACgkQu+CwddJFiJoOVgf+PlJMZ5LGHo8YS1NDWCjC3ldC0EzoReR 5wMTonVKUzITZnslQcY3rauafZqIZ8h2d+LOxFltPVHFTilX6hKLRhByZa+4a1uUxtGFNSczDx2 SnWJCMftuDm2vB517IRSeYRvtCW+JWqCl1eEKFTNkyjQ/AR6xLTxip/v+7xmTCcM40flnVMNoyB NOLrsJ1tSeWHP/a89DIlI7ll5Oy5xgPPOpbd9bVDuPr9vLtk226yr9oK5hcM5zJ/NcerYfwJ8nD 7vtRJnL9D7eENqDLOhw+GWn86uFkAagtquL3uXTLToPE9jIVu5OEOpbpewLX5GhjD3qdAPogqZU 9g4sx8ENFrQ== X-Developer-Key: i=vbabka@kernel.org; a=openpgp; fpr=A940D434992C2E8E99103D50224FA7E7CC82A664 When we end up returning extraneous objects during refill to a slab where we just did a get_freelist_nofreeze(), it is likely no other CPU has freed objects to it meanwhile. We can then reattach the remainder of the freelist without having to walk the (potentially cache cold) freelist to to find its tail to connect slab->freelist to it. Add a __slab_try_return_freelist() function that does that. It's a bit like __slab_free() but many situations from there cannot occur in this specific scenario so it's simpler. Signed-off-by: Vlastimil Babka (SUSE) Reviewed-by: Hao Li --- I've got this idea during the discussions on refill spilling, but not sure if I've mentioned it on-list. Anyway it seems like there should be no downsides (in theory...) so please test if it indeed improves things anywhere and then it could be a better baseline before trying anything that comes with tradeoffs? I've included SLUB_STAT items that show to me the optimistic path is indeed successful, but maybe they don't need to make it in final version due to being rather low-level. Git version here: https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/linux.git/log/?h=3Db= 4/refill-optimistic-return It's Linus' upstream few days ago with "mm/slub: defer freelist construction until after bulk allocation from a new slab" which is heading to 7.2 so also should be considered part of baseline. --- mm/slub.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----= ---- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 35b6cd0efc3b..176bc4936d03 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -373,6 +373,8 @@ enum stat_item { SHEAF_PREFILL_OVERSIZE, /* Allocation of oversize sheaf for prefill */ SHEAF_RETURN_FAST, /* Sheaf return reattached spare sheaf */ SHEAF_RETURN_SLOW, /* Sheaf return could not reattach spare */ + REFILL_RETURN_FAST, + REFILL_RETURN_SLOW, NR_SLUB_STAT_ITEMS }; =20 @@ -4323,7 +4325,8 @@ static inline bool pfmemalloc_match(struct slab *slab= , gfp_t gfpflags) * Assumes this is performed only for caches without debugging so we * don't need to worry about adding the slab to the full list. */ -static inline void *get_freelist_nofreeze(struct kmem_cache *s, struct sla= b *slab) +static inline void *get_freelist_nofreeze(struct kmem_cache *s, struct sla= b *slab, + unsigned int *count) { struct freelist_counters old, new; =20 @@ -4339,6 +4342,7 @@ static inline void *get_freelist_nofreeze(struct kmem= _cache *s, struct slab *sla =20 } while (!slab_update_freelist(s, slab, &old, &new, "get_freelist_nofreez= e")); =20 + *count =3D old.objects - old.inuse; return old.freelist; } =20 @@ -5502,6 +5506,50 @@ static noinline void free_to_partial_list( } } =20 +/* + * Try returning a (part of) freelist that we just detached from the slab. + * Optimistically assume the slab is still full so we don't need to know + * the tail of the freelist. Return to the partial list unconditionally ev= en + * if it became empty. + * + * Fail if the slab isn't full anymore due to a cocurrent free. + * + * Can be only used for non-debug caches + */ +static bool __slab_try_return_freelist(struct kmem_cache *s, struct slab *= slab, + void *head, int cnt) +{ + struct freelist_counters old, new; + struct kmem_cache_node *n =3D NULL; + unsigned long flags; + + old.freelist =3D slab->freelist; + old.counters =3D slab->counters; + + if (old.freelist) + return false; + + new.freelist =3D head; + new.counters =3D old.counters; + new.inuse -=3D cnt; + + n =3D get_node(s, slab_nid(slab)); + + spin_lock_irqsave(&n->list_lock, flags); + + if (!slab_update_freelist(s, slab, &old, &new, "__slab_try_return_freelis= t")) { + spin_unlock_irqrestore(&n->list_lock, flags); + return false; + } + + add_partial(n, slab, ADD_TO_HEAD); + stat(s, FREE_ADD_PARTIAL); + + spin_unlock_irqrestore(&n->list_lock, flags); + stat(s, REFILL_RETURN_FAST); + return true; +} + /* * Slow path handling. This may still be called frequently since objects * have a longer lifetime than the cpu slabs in most processing loads. @@ -7113,34 +7161,40 @@ __refill_objects_node(struct kmem_cache *s, void **= p, gfp_t gfp, unsigned int mi =20 list_for_each_entry_safe(slab, slab2, &pc.slabs, slab_list) { =20 + unsigned int count; + list_del(&slab->slab_list); =20 - object =3D get_freelist_nofreeze(s, slab); + object =3D get_freelist_nofreeze(s, slab, &count); =20 - while (object && refilled < max) { + while (count && refilled < max) { p[refilled] =3D object; object =3D get_freepointer(s, object); maybe_wipe_obj_freeptr(s, p[refilled]); =20 refilled++; + count--; } =20 /* * Freelist had more objects than we can accommodate, we need to - * free them back. We can treat it like a detached freelist, just - * need to find the tail object. + * free them back. First we try to be optimistic and assume the + * slab is stil full since we just detached its freelist. + * Otherwise we must need to find the tail object. */ - if (unlikely(object)) { + if (unlikely(count)) { void *head =3D object; void *tail; - int cnt =3D 0; + + if (__slab_try_return_freelist(s, slab, head, count)) + break; =20 do { tail =3D object; - cnt++; object =3D get_freepointer(s, object); } while (object); - __slab_free(s, slab, head, tail, cnt, _RET_IP_); + __slab_free(s, slab, head, tail, count, _RET_IP_); + stat(s, REFILL_RETURN_SLOW); } =20 if (refilled >=3D max) @@ -9366,6 +9420,8 @@ STAT_ATTR(SHEAF_PREFILL_SLOW, sheaf_prefill_slow); STAT_ATTR(SHEAF_PREFILL_OVERSIZE, sheaf_prefill_oversize); STAT_ATTR(SHEAF_RETURN_FAST, sheaf_return_fast); STAT_ATTR(SHEAF_RETURN_SLOW, sheaf_return_slow); +STAT_ATTR(REFILL_RETURN_FAST, refill_return_fast); +STAT_ATTR(REFILL_RETURN_SLOW, refill_return_slow); #endif /* CONFIG_SLUB_STATS */ =20 #ifdef CONFIG_KFENCE @@ -9454,6 +9510,8 @@ static const struct attribute *const slab_attrs[] =3D= { &sheaf_prefill_oversize_attr.attr, &sheaf_return_fast_attr.attr, &sheaf_return_slow_attr.attr, + &refill_return_fast_attr.attr, + &refill_return_slow_attr.attr, #endif #ifdef CONFIG_FAILSLAB &failslab_attr.attr, --- base-commit: da993c58f9bde299a5e1a4e7900125b32dccd2a6 change-id: 20260421-b4-refill-optimistic-return-f44d3b74cc49 Best regards, -- =20 Vlastimil Babka (SUSE)