Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Harry Yoo (Oracle)" <harry@kernel.org>
To: Vlastimil Babka <vbabka@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Hao Li <hao.li@linux.dev>,  Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	 Roman Gushchin <roman.gushchin@linux.dev>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	 Suren Baghdasaryan <surenb@google.com>,
	 "Liam R. Howlett" <liam@infradead.org>
Subject: [PATCH RFC 4/8] mm/slab: allow bootstrap_cache_sheaves() to fail
Date: Sat, 16 May 2026 01:24:28 +0900	[thread overview]
Message-ID: <20260516-sheaves-tuning-v1-4-221aa3e1d829@kernel.org> (raw)
In-Reply-To: <20260516-sheaves-tuning-v1-0-221aa3e1d829@kernel.org>

Panicking on sheaf allocation failure is acceptable during boot, but
to allow changing the sheaf capacity at runtime, the bootstrap path
must be able to propagate errors instead. Return an error code from
bootstrap_cache_sheaves() so callers can decide how to react.

Change it to return an int (0 on success, negative errno on failure),
accept capacity as a parameter, and drop __init. Callers without a
user-specified capacity pass zero to use the default capacity
calculated by the slab allocator. Failures are now handled by the
caller.

Signed-off-by: Harry Yoo (Oracle) <harry@kernel.org>
---
 mm/slub.c | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 44f36ae32570..fb98d0da5c78 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -8462,18 +8462,18 @@ static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
  * init_kmem_cache_nodes(). For normal kmalloc caches we have to bootstrap it
  * since sheaves and barns are allocated by kmalloc.
  */
-static void __init bootstrap_cache_sheaves(struct kmem_cache *s)
+static int bootstrap_cache_sheaves(struct kmem_cache *s,
+				   unsigned short capacity)
 {
 	struct kmem_cache_args empty_args = {};
-	unsigned short capacity;
-	bool failed = false;
-	int node, cpu;
+	int node, cpu, err = 0;
 
-	capacity = calculate_sheaf_capacity(s, &empty_args);
+	if (!capacity)
+		capacity = calculate_sheaf_capacity(s, &empty_args);
 
 	/* capacity can be 0 due to debugging or SLUB_TINY */
 	if (!capacity)
-		return;
+		return 0;
 
 	for_each_node_mask(node, slab_barn_nodes) {
 		struct node_barn *barn;
@@ -8481,7 +8481,7 @@ static void __init bootstrap_cache_sheaves(struct kmem_cache *s)
 		barn = kmalloc_node(sizeof(*barn), GFP_KERNEL, node);
 
 		if (!barn) {
-			failed = true;
+			err = -ENOMEM;
 			goto out;
 		}
 
@@ -8497,31 +8497,37 @@ static void __init bootstrap_cache_sheaves(struct kmem_cache *s)
 		pcs->main = __alloc_empty_sheaf(s, GFP_KERNEL, capacity);
 
 		if (!pcs->main) {
-			failed = true;
+			err = -ENOMEM;
 			break;
 		}
 	}
 
 out:
-	/*
-	 * It's still early in boot so treat this like same as a failure to
-	 * create the kmalloc cache in the first place
-	 */
-	if (failed)
-		panic("Out of memory when creating kmem_cache %s\n", s->name);
+	if (!err)
+		s->sheaf_capacity = capacity;
 
-	s->sheaf_capacity = capacity;
+	return err;
 }
 
+#define for_each_normal_kmalloc_cache(s, type, idx) \
+	for (type = KMALLOC_NORMAL; type <= KMALLOC_RANDOM_END; type++)	\
+		for (idx = 0; idx < KMALLOC_SHIFT_HIGH + 1; idx++)	\
+			if ((s = kmalloc_caches[type][idx]))
+
 static void __init bootstrap_kmalloc_sheaves(void)
 {
 	enum kmalloc_cache_type type;
+	struct kmem_cache *s;
+	int idx;
 
-	for (type = KMALLOC_NORMAL; type <= KMALLOC_RANDOM_END; type++) {
-		for (int idx = 0; idx < KMALLOC_SHIFT_HIGH + 1; idx++) {
-			if (kmalloc_caches[type][idx])
-				bootstrap_cache_sheaves(kmalloc_caches[type][idx]);
-		}
+	for_each_normal_kmalloc_cache(s, type, idx) {
+		/*
+		 * It's still early in boot so treat this as a failure to
+		 * create the kmalloc cache in the first place.
+		 */
+		if (bootstrap_cache_sheaves(s, 0))
+			panic("Out of memory when creating kmem_cache %s\n",
+			      s->name);
 	}
 }
 

-- 
2.43.0



  parent reply	other threads:[~2026-05-15 16:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15 16:24 [PATCH RFC 0/8] mm/slab: enable runtime sheaves tuning Harry Yoo (Oracle)
2026-05-15 16:24 ` [PATCH RFC 1/8] mm/slab: do not store cache pointer in struct slab_sheaf Harry Yoo (Oracle)
2026-05-15 16:24 ` [PATCH RFC 2/8] mm/slab: change sheaf_capacity type to unsigned short Harry Yoo (Oracle)
2026-05-15 16:24 ` [PATCH RFC 3/8] mm/slab: track capacity per sheaf Harry Yoo (Oracle)
2026-05-15 16:24 ` Harry Yoo (Oracle) [this message]
2026-05-15 16:24 ` [PATCH RFC 5/8] mm/slab: rework cache_has_sheaves() to check immutable properties only Harry Yoo (Oracle)
2026-05-15 16:24 ` [PATCH RFC 6/8] mm/slab: allow changing sheaf_capacity at runtime Harry Yoo (Oracle)
2026-05-15 16:24 ` [PATCH RFC 7/8] mm/slab: add pcs->lock lockdep assert when accessing the barn Harry Yoo (Oracle)
2026-05-15 16:24 ` [PATCH RFC 8/8] mm/slab: allow changing max_{full,empty}_sheaves at runtime Harry Yoo (Oracle)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260516-sheaves-tuning-v1-4-221aa3e1d829@kernel.org \
    --to=harry@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=hao.li@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox