From nobody Tue Dec 30 18:48:58 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 546A2C072A2 for ; Mon, 13 Nov 2023 19:14:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232183AbjKMTOz (ORCPT ); Mon, 13 Nov 2023 14:14:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231844AbjKMTOT (ORCPT ); Mon, 13 Nov 2023 14:14:19 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35C3C1731; Mon, 13 Nov 2023 11:14:16 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id E153B1F88E; Mon, 13 Nov 2023 19:14:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1699902854; h=from:from:reply-to: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=AvYULxpvTpi0KZyzNL3YX1bvF9SuuOz0+MetYRBUoJQ=; b=oIHlLgRjvw7JPEbJGFmSOpsKrINW0f9XdqQWVeRsISFOABjZ4DK8pmw1yqPCX9sGOBppD7 /2PaS3UjA8toXgHPERa2WAt2ew9Q/P4mq1rIHw9O8CtTaonrLfZW/F/b1AC8WVMaSEimF7 oFMsRG1N0wxBUjIAKDrXoROaTC6D8HQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1699902854; h=from:from:reply-to: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=AvYULxpvTpi0KZyzNL3YX1bvF9SuuOz0+MetYRBUoJQ=; b=yC6gI2OaZlpEgEV5qLkZwNfRnz195znLfF6b/XZpYlw9m0kJIbvR1gOmpqfTy7c7Hdbuie qkHrE5YZ8HMzm4Dw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 9CF6613398; Mon, 13 Nov 2023 19:14:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id sLfCJYZ1UmVFOgAAMHmgww (envelope-from ); Mon, 13 Nov 2023 19:14:14 +0000 From: Vlastimil Babka To: David Rientjes , Christoph Lameter , Pekka Enberg , Joonsoo Kim Cc: Andrew Morton , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Marco Elver , Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Kees Cook , kasan-dev@googlegroups.com, cgroups@vger.kernel.org, Vlastimil Babka Subject: [PATCH 16/20] mm/slab: move kmalloc_slab() to mm/slab.h Date: Mon, 13 Nov 2023 20:13:57 +0100 Message-ID: <20231113191340.17482-38-vbabka@suse.cz> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231113191340.17482-22-vbabka@suse.cz> References: <20231113191340.17482-22-vbabka@suse.cz> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In preparation for the next patch, move the kmalloc_slab() function to the header, as it will have callers from two files, and make it inline. To avoid unnecessary bloat, remove all size checks/warnings from kmalloc_slab() as they just duplicate those in callers, especially after recent changes to kmalloc_size_roundup(). We just need to adjust handling of zero size in __do_kmalloc_node(). Also we can stop handling NULL result from kmalloc_slab() there as that now cannot happen (unless called too early during boot). The size_index array becomes visible so rename it to a more specific kmalloc_size_index. Signed-off-by: Vlastimil Babka Reviewed-by: Kees Cook --- mm/slab.h | 28 ++++++++++++++++++++++++++-- mm/slab_common.c | 43 ++++++++----------------------------------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index 179467e8aacc..744384efa7be 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -388,8 +388,32 @@ extern const struct kmalloc_info_struct { void setup_kmalloc_cache_index_table(void); void create_kmalloc_caches(slab_flags_t); =20 -/* Find the kmalloc slab corresponding for a certain size */ -struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags, unsigned long ca= ller); +extern u8 kmalloc_size_index[24]; + +static inline unsigned int size_index_elem(unsigned int bytes) +{ + return (bytes - 1) / 8; +} + +/* + * Find the kmem_cache structure that serves a given size of + * allocation + * + * This assumes size is larger than zero and not larger than + * KMALLOC_MAX_CACHE_SIZE and the caller must check that. + */ +static inline struct kmem_cache * +kmalloc_slab(size_t size, gfp_t flags, unsigned long caller) +{ + unsigned int index; + + if (size <=3D 192) + index =3D kmalloc_size_index[size_index_elem(size)]; + else + index =3D fls(size - 1); + + return kmalloc_caches[kmalloc_type(flags, caller)][index]; +} =20 void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node, size_t orig_size, diff --git a/mm/slab_common.c b/mm/slab_common.c index f4f275613d2a..31ade17a7ad9 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -665,7 +665,7 @@ EXPORT_SYMBOL(random_kmalloc_seed); * of two cache sizes there. The size of larger slabs can be determined us= ing * fls. */ -static u8 size_index[24] __ro_after_init =3D { +u8 kmalloc_size_index[24] __ro_after_init =3D { 3, /* 8 */ 4, /* 16 */ 5, /* 24 */ @@ -692,33 +692,6 @@ static u8 size_index[24] __ro_after_init =3D { 2 /* 192 */ }; =20 -static inline unsigned int size_index_elem(unsigned int bytes) -{ - return (bytes - 1) / 8; -} - -/* - * Find the kmem_cache structure that serves a given size of - * allocation - */ -struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags, unsigned long ca= ller) -{ - unsigned int index; - - if (size <=3D 192) { - if (!size) - return ZERO_SIZE_PTR; - - index =3D size_index[size_index_elem(size)]; - } else { - if (WARN_ON_ONCE(size > KMALLOC_MAX_CACHE_SIZE)) - return NULL; - index =3D fls(size - 1); - } - - return kmalloc_caches[kmalloc_type(flags, caller)][index]; -} - size_t kmalloc_size_roundup(size_t size) { if (size && size <=3D KMALLOC_MAX_CACHE_SIZE) { @@ -843,9 +816,9 @@ void __init setup_kmalloc_cache_index_table(void) for (i =3D 8; i < KMALLOC_MIN_SIZE; i +=3D 8) { unsigned int elem =3D size_index_elem(i); =20 - if (elem >=3D ARRAY_SIZE(size_index)) + if (elem >=3D ARRAY_SIZE(kmalloc_size_index)) break; - size_index[elem] =3D KMALLOC_SHIFT_LOW; + kmalloc_size_index[elem] =3D KMALLOC_SHIFT_LOW; } =20 if (KMALLOC_MIN_SIZE >=3D 64) { @@ -854,7 +827,7 @@ void __init setup_kmalloc_cache_index_table(void) * is 64 byte. */ for (i =3D 64 + 8; i <=3D 96; i +=3D 8) - size_index[size_index_elem(i)] =3D 7; + kmalloc_size_index[size_index_elem(i)] =3D 7; =20 } =20 @@ -865,7 +838,7 @@ void __init setup_kmalloc_cache_index_table(void) * instead. */ for (i =3D 128 + 8; i <=3D 192; i +=3D 8) - size_index[size_index_elem(i)] =3D 8; + kmalloc_size_index[size_index_elem(i)] =3D 8; } } =20 @@ -977,10 +950,10 @@ void *__do_kmalloc_node(size_t size, gfp_t flags, int= node, unsigned long caller return ret; } =20 - s =3D kmalloc_slab(size, flags, caller); + if (unlikely(!size)) + return ZERO_SIZE_PTR; =20 - if (unlikely(ZERO_OR_NULL_PTR(s))) - return s; + s =3D kmalloc_slab(size, flags, caller); =20 ret =3D __kmem_cache_alloc_node(s, flags, node, size, caller); ret =3D kasan_kmalloc(s, ret, size, flags); --=20 2.42.1