From nobody Mon Jun 8 13:23:43 2026 Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (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 8903A361640 for ; Fri, 29 May 2026 03:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780026712; cv=none; b=BffQ2GvNm2WQBWkBCAE9VcZIwafWoyacgG0EJJL+EQ9xRlywDUOHELlyC1sl4u0U6+Bwx/qyE3HOgql/IdZ66FDpTLpDCQ4aY6/s0IEFbpffSz8F4q4d9HJWMP5IgUKyhAxn3weTlN5d1W6RRSoDM63UkyP05zGDI9qNNZWzP28= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780026712; c=relaxed/simple; bh=Oe3s1dXd+576lo6urCmnEus6HL6OgTh1kwiMn1xaIWc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=afukbnu8Paq8Zw9m7Hm17OwqkgmbUdpaRb1KBdC6GIgGVwCP0GNygmj3LJSNkcnhkrGNrrG9Y3iQek5ZKvP8UJ8tU68ol2s+yhu80/eEwv8RmGXRyXD/3qYeLBn1nAAoEUrvv1Jyt8nKqU5zFTxmwS3gtdKJprg+kztgiDKQy+I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=deJpLXPn; arc=none smtp.client-ip=91.218.175.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="deJpLXPn" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1780026708; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0B/bCGwErREfiHqYBbNLfFiWmSgms7I7OCV4Ix5QlOg=; b=deJpLXPnqBZX3xG39KVWI1DoXBwP0OQ8KIFV3yIrnfpDUPnOG5EO2JYevOBV7b+AmSrMHI MHD85XCZKUrdzImMMsKoS5+giXII668/nj/LzilMpnAwfy/7rqujGuZxwBvBezg7680b7e Js+pdQWpT9Q23nhOI+DpS1qO1qJRD+s= From: Hao Li To: vbabka@kernel.org, harry@kernel.org, akpm@linux-foundation.org Cc: cl@gentwo.org, rientjes@google.com, roman.gushchin@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hao Li Subject: [PATCH v3 1/2] mm/slub: introduce helpers for node partial slab state Date: Fri, 29 May 2026 11:50:51 +0800 Message-ID: <20260529035120.81304-2-hao.li@linux.dev> In-Reply-To: <20260529035120.81304-1-hao.li@linux.dev> References: <20260529035120.81304-1-hao.li@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Wrap partial slab count inc/dec and flag set/clear into helper functions to reduce code duplication. Note that __add_partial() is called locklessly in early_kmem_cache_node_alloc(), but since there is no such use case for removal, __remove_partial() does not exist. Suggested-by: Harry Yoo Signed-off-by: Hao Li Reviewed-by: Harry Yoo (Oracle) --- mm/slub.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index b9f10b869914..45aa0a834f97 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3521,15 +3521,21 @@ static inline void slab_clear_node_partial(struct s= lab *slab) /* * Management of partially allocated slabs. */ +static inline void set_node_partial_state(struct kmem_cache_node *n, + struct slab *slab) +{ + slab_set_node_partial(slab); + n->nr_partial++; +} + static inline void __add_partial(struct kmem_cache_node *n, struct slab *slab, enum add_mode = mode) { - n->nr_partial++; if (mode =3D=3D ADD_TO_TAIL) list_add_tail(&slab->slab_list, &n->partial); else list_add(&slab->slab_list, &n->partial); - slab_set_node_partial(slab); + set_node_partial_state(n, slab); } =20 static inline void add_partial(struct kmem_cache_node *n, @@ -3539,13 +3545,19 @@ static inline void add_partial(struct kmem_cache_no= de *n, __add_partial(n, slab, mode); } =20 +static inline void clear_node_partial_state(struct kmem_cache_node *n, + struct slab *slab) +{ + slab_clear_node_partial(slab); + n->nr_partial--; +} + static inline void remove_partial(struct kmem_cache_node *n, struct slab *slab) { lockdep_assert_held(&n->list_lock); list_del(&slab->slab_list); - slab_clear_node_partial(slab); - n->nr_partial--; + clear_node_partial_state(n, slab); } =20 /* @@ -8271,8 +8283,7 @@ static int __kmem_cache_do_shrink(struct kmem_cache *= s) =20 if (free =3D=3D slab->objects) { list_move(&slab->slab_list, &discard); - slab_clear_node_partial(slab); - n->nr_partial--; + clear_node_partial_state(n, slab); dec_slabs_node(s, node, slab->objects); } else if (free <=3D SHRINK_PROMOTE_MAX) list_move(&slab->slab_list, promote + free - 1); --=20 2.54.0 From nobody Mon Jun 8 13:23:43 2026 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (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 7323A35F5E5 for ; Fri, 29 May 2026 03:52:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780026721; cv=none; b=goJo4eqGl6BXXUPAS7uj+11DRAEMuDTm1+jj+ET1OXahikleOwOFjacd+x6JIN2hqCC4q8F9sh3WpTGP/7CZ10hxA8rwcIGYJ+hA1H7lR4ICNYttME+ahW9X4urU8YSgmhrFXmkXfl3biEzutj92ngGHkWHwFnnxu256voUMs64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780026721; c=relaxed/simple; bh=uPgcfVsQNnYx2lVyCaiJdaUshwsXBTkdFyiYQqnU3OQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SiYRzpMI5AiX8DdPpa6bRt1cPzKT4lyp5/z2qaaxGg3KoCb0e37gjdz3ZEJ/1JucpBrq9If2dbkEJk3Xo9NCTYDr9UF0agwQJtFViYOLRXMObveqOPqKGcLh+J1INyoUkVkFMbBR1VfVtQoGnK/XxVqc6eTlvQVRQEomnfJIF3Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=A1u8pdDo; arc=none smtp.client-ip=91.218.175.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="A1u8pdDo" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1780026717; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Vm7EtoPN4QmeJ2Y9CSRukPyMKuVmFRo7olYJHuJ3ke8=; b=A1u8pdDoeRJKed4RmTVAxkvXMMDzLTT/u2icwQoTkOfKw/eCkU3sFNbZX7SpzsB87+/cXP nb6dKS9jiBAGo4D+oZa3/Onc+9Jasal9zy9SEOOOWlM0YH/ub3HuI1Ygi8vyB1zXrDhPKu ZdTJrfVlgbXNEjJ97gzG3LXZNK/w9co= From: Hao Li To: vbabka@kernel.org, harry@kernel.org, akpm@linux-foundation.org Cc: cl@gentwo.org, rientjes@google.com, roman.gushchin@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hao Li Subject: [PATCH v3 2/2] mm/slub: detach and reattach partial slabs in batch Date: Fri, 29 May 2026 11:50:52 +0800 Message-ID: <20260529035120.81304-3-hao.li@linux.dev> In-Reply-To: <20260529035120.81304-1-hao.li@linux.dev> References: <20260529035120.81304-1-hao.li@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" get_partial_node_bulk() moves each selected slab from the node's partial list to the local pc->slabs list using a remove_partial() and list_add() pair. In practice, the loop often detaches several adjacent slabs. Doing this individually repeatedly manipulates list pointers while holding n->list_lock, which causes unnecessary churn. To demonstrate this, the counts below show how often single vs. multiple consecutive slabs are retrieved during a will-it-scale mmap stress test: consecutive_slabs_count frequency =3D 1 277345324 =3D 2 335238023 =3D 3 175717884 >=3D 4 88862337 The data confirms that retrieving multiple contiguous slabs is highly frequent. To optimize this, track contiguous runs of matching slabs and move each run in a single operation using list_bulk_move_tail(). This reduces list pointer churn inside the lock critical section. Apply the same optimization to __refill_objects_node() when reattaching leftover partial slabs back to the node's partial list. The will-it-scale mmap benchmark shows a 2% ~ 5% performance improvement after applying this patch. Signed-off-by: Hao Li Reviewed-by: Harry Yoo (Oracle) --- mm/slub.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 45aa0a834f97..4fdf5c36a69b 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3751,6 +3751,7 @@ static bool get_partial_node_bulk(struct kmem_cache *= s, bool allow_spin) { struct slab *slab, *slab2; + struct slab *first =3D NULL, *last =3D NULL; unsigned int total_free =3D 0; unsigned long flags; =20 @@ -3769,8 +3770,15 @@ static bool get_partial_node_bulk(struct kmem_cache = *s, struct freelist_counters flc; unsigned int slab_free; =20 - if (!pfmemalloc_match(slab, pc->flags)) + if (!pfmemalloc_match(slab, pc->flags)) { + if (first) { + list_bulk_move_tail(&pc->slabs, + &first->slab_list, + &last->slab_list); + first =3D NULL; + } continue; + } =20 /* * determine the number of free objects in the slab racily @@ -3787,15 +3795,20 @@ static bool get_partial_node_bulk(struct kmem_cache= *s, && total_free + slab_free > pc->max_objects) break; =20 - remove_partial(n, slab); - - list_add(&slab->slab_list, &pc->slabs); + if (!first) + first =3D slab; + last =3D slab; + clear_node_partial_state(n, slab); =20 total_free +=3D slab_free; if (total_free >=3D pc->max_objects) break; } =20 + if (first) + list_bulk_move_tail(&pc->slabs, &first->slab_list, + &last->slab_list); + spin_unlock_irqrestore(&n->list_lock, flags); return total_free > 0; } @@ -7206,11 +7219,10 @@ __refill_objects_node(struct kmem_cache *s, void **= p, gfp_t gfp, unsigned int mi if (!list_empty(&pc.slabs)) { spin_lock_irqsave(&n->list_lock, flags); =20 - list_for_each_entry_safe(slab, slab2, &pc.slabs, slab_list) { + list_for_each_entry(slab, &pc.slabs, slab_list) + set_node_partial_state(n, slab); =20 - list_del(&slab->slab_list); - add_partial(n, slab, ADD_TO_TAIL); - } + list_splice_tail(&pc.slabs, &n->partial); =20 spin_unlock_irqrestore(&n->list_lock, flags); } --=20 2.54.0