From nobody Tue Feb 10 15:46:10 2026 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 E23B8324B39 for ; Thu, 23 Oct 2025 13:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761227638; cv=none; b=Ry+68YwB29ufVU2LHU6YNXsF7uA9XX9SF4xZPx+dkfUeHd6v4gnHQ3U9AXp9HO7PNfv66j6aM36cMXhDKcUJAi1H50xShx04s1hzvU5T3HVD/gAoH0AKi05p1ZFkaVgFRQVC0NBKEuIQmS1VlopY/YKV3B/PJeVZTR51w9E2FvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761227638; c=relaxed/simple; bh=gIBLRv+7sMG3wWCQ7/G3IufiPATEXv9bQNITdHcjk6o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XMYFM+tH5gsqfCjXDyHDBqtjPjTR1E6bROa4nWJe0G0p4hrqQ1KQkjX09g1bXCyvCHUeVyT32laj/F8AaC+KKRaaY1PxEEEwirpG5c69qj/LkfZWGRBVKyN04Ski6zDSaKZBnlbXnZimWA1+ETBFPeJdMFv1eJmvhFa1if4xxzA= 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=g2RqqKtb; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=2EFdvI6L; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=a5zjqizc; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=sWIyNwSz; arc=none smtp.client-ip=195.135.223.131 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="g2RqqKtb"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="2EFdvI6L"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="a5zjqizc"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="sWIyNwSz" 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-out2.suse.de (Postfix) with ESMTPS id 799841F788; Thu, 23 Oct 2025 13:53:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1761227585; 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=bwEJAB2u9ltAgWYDeLEgZcAvDMG4WKRh64HkDQCHmoQ=; b=g2RqqKtbdgDO5PzJSrGzm/1RUPPCbQTAhSf1EsfLVAyEJQUGqw3q2BKm6dC7Nz87CyuBa2 PXFvqzQSb8Bit8YvQkEFYRTxQfPCl+3iZDb8SmqZi9Zs0tdv20/+bUOS1LBhJv4DBhZBeN yubVX/UoWHnVHsRzgfNdi9S0cgF/ZZo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1761227585; 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=bwEJAB2u9ltAgWYDeLEgZcAvDMG4WKRh64HkDQCHmoQ=; b=2EFdvI6LKPGqMVlUHaZwPgV9WiojBfwO6nCP8O0l6222vnuPhpc8R+eJO6sux7/zhK2znv 9cm9o+FAz9peUuCw== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1761227581; 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=bwEJAB2u9ltAgWYDeLEgZcAvDMG4WKRh64HkDQCHmoQ=; b=a5zjqizcJ+R3FBfrwgMdNMnv3gqv/TcisPLUO3PJDxYXyhZIPfoi3mlk6HMHjco5s7OXY+ h12Wgzo/N3oF/3GSpoOvnBs4MKQs2dHxCAf9XTv3mxgenk3fdytcSYs8HqIMX+wR7sKoId s9//9D7GoVn1ouxIoxO59mDj77cnNsI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1761227581; 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=bwEJAB2u9ltAgWYDeLEgZcAvDMG4WKRh64HkDQCHmoQ=; b=sWIyNwSz3I0urZD07FY5c2Si4JYzhXTW3YuKNjV6bM+hzo1cKhr/e0aDoK2F/54o3PUGFd UTH+vRqfOOxQZWCA== 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 C4B1713B06; Thu, 23 Oct 2025 13:52:53 +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 KE6+LzUz+mjvQQAAD6G6ig (envelope-from ); Thu, 23 Oct 2025 13:52:53 +0000 From: Vlastimil Babka Date: Thu, 23 Oct 2025 15:52:30 +0200 Subject: [PATCH RFC 08/19] 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: <20251023-sheaves-for-all-v1-8-6ffa2c9941c0@suse.cz> References: <20251023-sheaves-for-all-v1-0-6ffa2c9941c0@suse.cz> In-Reply-To: <20251023-sheaves-for-all-v1-0-6ffa2c9941c0@suse.cz> To: Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: 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 [-8.30 / 50.00]; REPLY(-4.00)[]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; RCPT_COUNT_TWELVE(0.00)[16]; ARC_NA(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)[gmail.com,oracle.com,google.com,linutronix.de,kernel.org,kvack.org,vger.kernel.org,lists.linux.dev,googlegroups.com,suse.cz]; R_RATELIMIT(0.00)[to_ip_from(RLwn5r54y1cp81no5tmbbew5oc)]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.cz:mid,suse.cz:email,imap1.dmz-prg2.suse.org:helo] X-Spam-Flag: NO X-Spam-Score: -8.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. Signed-off-by: Vlastimil Babka --- mm/slub.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 5d0b2cf66520..a84027fbca78 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2588,7 +2588,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; @@ -2606,7 +2607,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)) @@ -2619,6 +2620,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); @@ -8064,8 +8071,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 /* @@ -8549,6 +8559,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, @@ -8592,6 +8670,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.51.1