From nobody Mon Jun 8 15:36:54 2026 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) (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 E37893B47C4 for ; Thu, 28 May 2026 09:53:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779962003; cv=none; b=Yr5QjQtx7voFUNnxc29Zg4z9XdYZo5iumDF1TChXfgZIzTY39EKy32Pmw9rFM3rvOyDYfZyhAmfVhG/iYKGWVjypFh2ov3+4OmjL0RHEkkUzUFYrWWfSpRZGm+OqcpKJYq6kIXSV8WnuRw0kQuD8bBoXM/+ntKpHuJM5xiMYmJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779962003; c=relaxed/simple; bh=yY05zrriUrmZdwZ+EEQU1kuZPPXgz2aLwMC1/L+VN0U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XykhwuoKKDY7wNa7cVEHS0ocytYAa8h2M6ouSKitG4UvnrlhKWlpKEHOpm/Prd9F6/oDjdEtNZDrFi9mRAVp7PFgcorMA+OJ38wLHHkqu8tWc9tz/1lyOprBaWzAbJ4m+yvgLHqgnriPDCLzsg9QLHfL593QyH9Fo0RNTzCrI3M= 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=MFvO0i0i; arc=none smtp.client-ip=91.218.175.185 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="MFvO0i0i" 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=1779961999; 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=7Psi/3PdGYQ/8BNVb4Dult2mh9IIeWs6X+I0h1Ys+4s=; b=MFvO0i0i8oD03Zk4F4oBxnrdZN92TFSlALnlmqAM2vtQEzVA/wIphM7iFUXo3AGL8DX/Vi oYoArEu82HTsvhp+EMKbRJruB5IbtLZecAJQLrvYDbLL6gedfg7haDTjsHd2AJRxmaiBQh iNwwye44gXCxTXcYHgku4EhHyCkwOyg= 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 v2 1/2] mm/slub: detach and reattach partial slabs in batch Date: Thu, 28 May 2026 17:52:43 +0800 Message-ID: <20260528095254.43721-2-hao.li@linux.dev> In-Reply-To: <20260528095254.43721-1-hao.li@linux.dev> References: <20260528095254.43721-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 --- mm/slub.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index b9f10b869914..eb60b3da23ff 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3739,6 +3739,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 @@ -3757,8 +3758,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 @@ -3775,15 +3783,21 @@ 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; + slab_clear_node_partial(slab); + n->nr_partial--; =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; } @@ -7194,12 +7208,13 @@ __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_del(&slab->slab_list); - add_partial(n, slab, ADD_TO_TAIL); + list_for_each_entry(slab, &pc.slabs, slab_list) { + slab_set_node_partial(slab); + n->nr_partial++; } =20 + list_splice_tail(&pc.slabs, &n->partial); + spin_unlock_irqrestore(&n->list_lock, flags); } =20 --=20 2.54.0 From nobody Mon Jun 8 15:36:54 2026 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (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 5E37E3B4EAB for ; Thu, 28 May 2026 09:53:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779962008; cv=none; b=kmw8VOVi1VKjhUJEAUh6wW6Tcnif1qJxRR8cThdA8WZChfjJZO1QyaJ2+EKgxXbqKgZhWlpADbiwC7OYE8wKADFmYFgLhCw9JdE8jvxnsSaz0bid7o1fXuwp4ndEw12qGxL5aQ2LnWM47FkTNqkxGzE2Ds6O4j+hEg4+BLE1cww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779962008; c=relaxed/simple; bh=p7R2smM3R79rduZHUJp+jpkBMb33NIUw+a924uaK5L0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CNqvy6s+gaQB/3WyKu2seAvYrQtLUYBXJgRv1TfgBd1MQUJWlLT8tWlheEfQ9W+kcm7EhIv7thD3h8eN4x02Fro2XsoduxY6bl17Skqvoiz0zWmSvn82akVKemY1H0xYvZ1m2WIS6ZS5wjSOCCUeU/ewFNPM+XaLyo1zuz79rPg= 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=sk5xpDBr; arc=none smtp.client-ip=91.218.175.182 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="sk5xpDBr" 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=1779962004; 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=v3vBrP0czaKHTwDPD06avz57QmH+1JNC+yH6Fj/n/GA=; b=sk5xpDBr47nH4YUBnVq2KUK2/+UXlt8usoKUf6Y+8gEF8KPMkHtwSRb8O6Drk6AquU3OX3 TIdfIFYTVVdzSyz5lkB4UuYwzI3wqP1cL3M8Qex/creCZdP56x8yevEHAkkXYekhECnFSi PllMc7zDX3aNAYJ45Yy1gFsF/ftvc9k= 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 v2 2/2] mm/slub: introduce helper to decrement partial slab count and clear flag Date: Thu, 28 May 2026 17:52:44 +0800 Message-ID: <20260528095254.43721-3-hao.li@linux.dev> In-Reply-To: <20260528095254.43721-1-hao.li@linux.dev> References: <20260528095254.43721-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 the partial slab count decrement and slab_clear_node_partial() into a helper function to reduce code duplication. Suggested-by: Harry Yoo Signed-off-by: Hao Li --- mm/slub.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index eb60b3da23ff..71305290c8ca 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3539,13 +3539,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 /* @@ -3786,8 +3792,7 @@ static bool get_partial_node_bulk(struct kmem_cache *= s, if (!first) first =3D slab; last =3D slab; - slab_clear_node_partial(slab); - n->nr_partial--; + clear_node_partial_state(n, slab); =20 total_free +=3D slab_free; if (total_free >=3D pc->max_objects) @@ -8286,8 +8291,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