From nobody Tue Feb 10 11:13:01 2026 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0300633E347 for ; Fri, 23 Jan 2026 06:53:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769151223; cv=none; b=FrKsn4D/eWtG0keA+SpyULucBFljGzxUoZY4Ixm+DzLbARZ8fuU8w1oIrjTsYETtxBQLycPXjvFEcMwU2JD8KaYg6UaaBdLsi0DhHyUy56JkfWd7HfMJzjQIWzAW0PKI2pdEt2z+xK6QluI4tpT4qzDerd6st62JGPBDc4fYESU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769151223; c=relaxed/simple; bh=1lfzi97QRd1cOo69vWPCjn4qeUYI8cS75ctgy91doWY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bJVYxrmI3uTqh8/I59Zg9NyMLG+stB6Vv2FfpqZNk6LcG9aSIFiKix522xVZ9ucw3KOBwNZm9WOFXTBDPKeP4oLqsFdmSsl/3Vg356h8yZud4H6sptx31nu7jQl6yET87zYA7PQXel+nJz6MByTyDEVMaudePxqyQtDQTaPBFcw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D0DD833775; Fri, 23 Jan 2026 06:53:10 +0000 (UTC) Authentication-Results: smtp-out1.suse.de; none Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 0FF1D139F0; Fri, 23 Jan 2026 06:53:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id CKtxA9Yac2k4YgAAD6G6ig (envelope-from ); Fri, 23 Jan 2026 06:53:10 +0000 From: Vlastimil Babka Date: Fri, 23 Jan 2026 07:52:47 +0100 Subject: [PATCH v4 09/22] slab: handle kmalloc sheaves bootstrap 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: <20260123-sheaves-for-all-v4-9-041323d506f7@suse.cz> References: <20260123-sheaves-for-all-v4-0-041323d506f7@suse.cz> In-Reply-To: <20260123-sheaves-for-all-v4-0-041323d506f7@suse.cz> To: Harry Yoo , Petr Tesarik , Christoph Lameter , David Rientjes , Roman Gushchin Cc: Hao Li , Andrew Morton , Uladzislau Rezki , "Liam R. Howlett" , Suren Baghdasaryan , Sebastian Andrzej Siewior , Alexei Starovoitov , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev, bpf@vger.kernel.org, kasan-dev@googlegroups.com, Vlastimil Babka X-Mailer: b4 0.14.3 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spam-Score: -4.00 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Rspamd-Queue-Id: D0DD833775 X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Level: X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[]; R_RATELIMIT(0.00)[to_ip_from(RLfsjnp7neds983g95ihcnuzgq)] X-Spam-Flag: NO Enable sheaves for kmalloc caches. For other types than KMALLOC_NORMAL, we can simply allow them in calculate_sizes() as they are created later than KMALLOC_NORMAL caches and can allocate sheaves and barns from those. For KMALLOC_NORMAL caches we perform additional step after first creating them without sheaves. Then bootstrap_cache_sheaves() simply allocates and initializes barns and sheaves and finally sets s->sheaf_capacity to make them actually used. Afterwards the only caches left without sheaves (unless SLUB_TINY or debugging is enabled) are kmem_cache and kmem_cache_node. These are only used when creating or destroying other kmem_caches. Thus they are not performance critical and we can simply leave it that way. Reviewed-by: Harry Yoo Reviewed-by: Hao Li Signed-off-by: Vlastimil Babka Reviewed-by: Liam R. Howlett --- mm/slub.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 4ca6bd944854..22acc249f9c0 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2593,7 +2593,8 @@ static void *setup_object(struct kmem_cache *s, void = *object) return object; } =20 -static struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s, gfp_t gf= p) +static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t = gfp, + unsigned int capacity) { struct slab_sheaf *sheaf; size_t sheaf_size; @@ -2611,7 +2612,7 @@ static struct slab_sheaf *alloc_empty_sheaf(struct km= em_cache *s, gfp_t gfp) if (s->flags & SLAB_KMALLOC) gfp |=3D __GFP_NO_OBJ_EXT; =20 - sheaf_size =3D struct_size(sheaf, objects, s->sheaf_capacity); + sheaf_size =3D struct_size(sheaf, objects, capacity); sheaf =3D kzalloc(sheaf_size, gfp); =20 if (unlikely(!sheaf)) @@ -2624,6 +2625,12 @@ static struct slab_sheaf *alloc_empty_sheaf(struct k= mem_cache *s, gfp_t gfp) return sheaf; } =20 +static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s, + gfp_t gfp) +{ + return __alloc_empty_sheaf(s, gfp, s->sheaf_capacity); +} + static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *shea= f) { kfree(sheaf); @@ -8144,8 +8151,11 @@ static int calculate_sizes(struct kmem_cache_args *a= rgs, struct kmem_cache *s) if (s->flags & SLAB_RECLAIM_ACCOUNT) s->allocflags |=3D __GFP_RECLAIMABLE; =20 - /* kmalloc caches need extra care to support sheaves */ - if (!is_kmalloc_cache(s)) + /* + * For KMALLOC_NORMAL caches we enable sheaves later by + * bootstrap_kmalloc_sheaves() to avoid recursion + */ + if (!is_kmalloc_normal(s)) s->sheaf_capacity =3D calculate_sheaf_capacity(s, args); =20 /* @@ -8640,6 +8650,74 @@ static struct kmem_cache * __init bootstrap(struct k= mem_cache *static_cache) return s; } =20 +/* + * Finish the sheaves initialization done normally by init_percpu_sheaves(= ) and + * 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) +{ + struct kmem_cache_args empty_args =3D {}; + unsigned int capacity; + bool failed =3D false; + int node, cpu; + + capacity =3D calculate_sheaf_capacity(s, &empty_args); + + /* capacity can be 0 due to debugging or SLUB_TINY */ + if (!capacity) + return; + + for_each_node_mask(node, slab_nodes) { + struct node_barn *barn; + + barn =3D kmalloc_node(sizeof(*barn), GFP_KERNEL, node); + + if (!barn) { + failed =3D true; + goto out; + } + + barn_init(barn); + get_node(s, node)->barn =3D barn; + } + + for_each_possible_cpu(cpu) { + struct slub_percpu_sheaves *pcs; + + pcs =3D per_cpu_ptr(s->cpu_sheaves, cpu); + + pcs->main =3D __alloc_empty_sheaf(s, GFP_KERNEL, capacity); + + if (!pcs->main) { + failed =3D true; + 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); + + s->sheaf_capacity =3D capacity; +} + +static void __init bootstrap_kmalloc_sheaves(void) +{ + enum kmalloc_cache_type type; + + for (type =3D KMALLOC_NORMAL; type <=3D KMALLOC_RANDOM_END; type++) { + for (int idx =3D 0; idx < KMALLOC_SHIFT_HIGH + 1; idx++) { + if (kmalloc_caches[type][idx]) + bootstrap_cache_sheaves(kmalloc_caches[type][idx]); + } + } +} + void __init kmem_cache_init(void) { static __initdata struct kmem_cache boot_kmem_cache, @@ -8683,6 +8761,8 @@ void __init kmem_cache_init(void) setup_kmalloc_cache_index_table(); create_kmalloc_caches(); =20 + bootstrap_kmalloc_sheaves(); + /* Setup random freelists for each cache */ init_freelist_randomization(); =20 --=20 2.52.0