From nobody Tue Feb 10 22:17:52 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 CD347395276 for ; Fri, 16 Jan 2026 14:41:19 +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=1768574481; cv=none; b=Yf2fATyGw73MEZ5ObwmtFvlQV4sC3CvKw5NyVxgmqAk3txEqrL0V/Um69p3govwXQkNEjyEpoPtsBIMGL96tkWnnE9nzVFJLmX3B6dygyID+vwaJNLQf8fSaNqfV0fk4t1krPZ7C09Xc9/EVc0LjWqrqW2FKn7GYiJTmgtHTisI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768574481; c=relaxed/simple; bh=h6h2MSm04t8w93VqRrG3/kiLekV41p56STsPuFCUGyg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iC/cn2hPfizCB3LIH26v/LOy/FCKvTas/tkRDa6yoaQGVVo0NxW+nlbeCLphmzARa6BCwNJyh7XYOwWs9uniW/XJHASWLpTgWpH+R8Ggpp+8jAIRiqX1akwlkok6u+MTFvpXypjz++vaGdER9bsHqKBR8wkAM8NcH6KVfuG8MWo= 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; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=k6hRjn02; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=3NeDCWdf; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=k6hRjn02; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=3NeDCWdf; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="k6hRjn02"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="3NeDCWdf"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="k6hRjn02"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="3NeDCWdf" Received: from imap1.dmz-prg2.suse.org (unknown [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 7CFEA337F4; Fri, 16 Jan 2026 14:40:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1768574437; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=en/PV8L5vRbZ1mp+m0UL7Z3JJuIYQSFGAs1yxFq9ZMM=; b=k6hRjn02kgL6wXHoy4EJb+sOs8vawSDtCZXKYpubQOgmFJW4ifecLsuxeRYDdihbZBdrHA jDQ8RQBfZAOkNIUD+gpO1Dc+GoasvzTBd2t1DE4vAA60xeFKiRtGrxJjx/eYqzRul+/+3i oysVC477tk84xgu0UhFHEcn+RUWNKqA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1768574437; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=en/PV8L5vRbZ1mp+m0UL7Z3JJuIYQSFGAs1yxFq9ZMM=; b=3NeDCWdfbS4Ut4e0yvLQm+Wq3XKjTQqscJKUneY6XLrZSK4XqBUQFduaHFfkwMlZ8fKz4t WyT9KpEjEmNERDCg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1768574437; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=en/PV8L5vRbZ1mp+m0UL7Z3JJuIYQSFGAs1yxFq9ZMM=; b=k6hRjn02kgL6wXHoy4EJb+sOs8vawSDtCZXKYpubQOgmFJW4ifecLsuxeRYDdihbZBdrHA jDQ8RQBfZAOkNIUD+gpO1Dc+GoasvzTBd2t1DE4vAA60xeFKiRtGrxJjx/eYqzRul+/+3i oysVC477tk84xgu0UhFHEcn+RUWNKqA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1768574437; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=en/PV8L5vRbZ1mp+m0UL7Z3JJuIYQSFGAs1yxFq9ZMM=; b=3NeDCWdfbS4Ut4e0yvLQm+Wq3XKjTQqscJKUneY6XLrZSK4XqBUQFduaHFfkwMlZ8fKz4t WyT9KpEjEmNERDCg== 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 5F6B03EA65; Fri, 16 Jan 2026 14:40:37 +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 iCQFF+VNamnydgAAD6G6ig (envelope-from ); Fri, 16 Jan 2026 14:40:37 +0000 From: Vlastimil Babka Date: Fri, 16 Jan 2026 15:40:28 +0100 Subject: [PATCH v3 08/21] 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: <20260116-sheaves-for-all-v3-8-5595cb000772@suse.cz> References: <20260116-sheaves-for-all-v3-0-5595cb000772@suse.cz> In-Reply-To: <20260116-sheaves-for-all-v3-0-5595cb000772@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-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_TWELVE(0.00)[18]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[linux.dev,linux-foundation.org,gmail.com,oracle.com,google.com,linutronix.de,kernel.org,kvack.org,vger.kernel.org,lists.linux.dev,googlegroups.com,suse.cz]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; R_RATELIMIT(0.00)[to_ip_from(RLwn5r54y1cp81no5tmbbew5oc)] X-Spam-Flag: NO X-Spam-Score: -4.30 X-Spam-Level: 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: Suren Baghdasaryan Signed-off-by: Vlastimil Babka Reviewed-by: Hao Li Reviewed-by: Harry Yoo --- mm/slub.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index b385247c219f..9bea8a65e510 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2605,7 +2605,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; @@ -2623,7 +2624,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)) @@ -2636,6 +2637,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); @@ -8119,8 +8126,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 /* @@ -8615,6 +8625,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, @@ -8658,6 +8736,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