From patchwork Mon Nov 13 19:13:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13454371 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B360C4332F for ; Mon, 13 Nov 2023 19:14:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 141F86B027E; Mon, 13 Nov 2023 14:14:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0502C6B0283; Mon, 13 Nov 2023 14:14:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFE406B0286; Mon, 13 Nov 2023 14:14:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 802A46B027D for ; Mon, 13 Nov 2023 14:14:18 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3ABC0C03E7 for ; Mon, 13 Nov 2023 19:14:18 +0000 (UTC) X-FDA: 81453881796.24.8E1DFE5 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf26.hostedemail.com (Postfix) with ESMTP id 2FF46140024 for ; Mon, 13 Nov 2023 19:14:15 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=oIHlLgRj; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=yC6gI2Oa; dmarc=none; spf=pass (imf26.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699902856; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AvYULxpvTpi0KZyzNL3YX1bvF9SuuOz0+MetYRBUoJQ=; b=z9PW6+ritHN6tOdOqfb7XRXoRwugQ3pxrnoH70umJ+sHjqRs/kR2dgekVpmOy40NgDcWzT QZtRyVqw2ZPiMJZ0s++BrtL2ObZLZYSMzAvg991OrfVPDiOrsVuZmYi4NZgsR6onrA539w 0Y/k4DCY7HKdSpL/bq0yyg8A54rtqNU= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=oIHlLgRj; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=yC6gI2Oa; dmarc=none; spf=pass (imf26.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699902856; a=rsa-sha256; cv=none; b=OTcn5h+Osu/CAgVYSEQq6SAqu0sl9n/dJV5tsDeSxCIX2Uuy2zJQx3Zwm8jnfc3590m70L v5c5Sc7ruymLKln8jb+rveh3c0CwvvgHfaSLpx3YHzcop1iMYkfRrQV0y8mQuVu/5G1T87 plq5bmRAlS7eFPswcTVtUsvyD62Uqug= 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 X-Rspamd-Queue-Id: 2FF46140024 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: frprjsg1oy6ys1kxsnze5s6f1z6hhast X-HE-Tag: 1699902855-911247 X-HE-Meta: U2FsdGVkX19IRMvIH+rSwvTkrm+gImia84ONKqWw8smgK0CC4bL+TNUQHRDZ6q/LykGuq3c9kbXwjy1SQwJxFx4HGSmebVtnyccN1yrIJHbOSgEb0LXyXcXskv1s7VvmvmsPa8u+ZF4bw2JRq8eodKfFDASLbwYyxZw7r+v/f3lJhrJ/8vil3UPW/SpBkh5HCqI/e0+0mIJ3n1qDg5wP407wxMliZvVT9ARw2DUwpevNaAh2B/K76kCUYhao7V0aFNj+V9SAo+pVNWftJMpZ6wDNzrtjgv6bJFIr59WLG18RuxNYulQ1+XwUBr0bM5wiMpxRxXoffUBjtiLdF04GwFmZKHTAXVKgPae4DPR3bro7nVtLgO7UcTAq064flMHzCtLVra8llpSbtDv8SwbGNac1RQlAFbi8bhSroEyyOYvozf52ZDIdYfQU5xXo3AZz+QfQdv5U4wtT4FnsTpFGGeIwRwqv7FwHxLvvmx6E9HO91WghhNffu11qLXVeH9XXYr+otIt6wyNRI8Wr3+kf+j83GFWj/QEOMt5aedCJ+HdPFRgCbg6KO2we7d1ybMuPS6ydkQ2jR/Mtj3ULaQesDTQyvl9NyN5djUvwKmFNlc+Q1lzMYzKrTbntIEkxVsYo4XdUoLSssZOtqjIMgP0VBI6hSNzt73Ih0xiXliJgweYh7xr75AvaoCVxWIXPO0RcyExbvs5UOnJsjlrDvAXHIp3JcRed9U1eYLdE4m5XdS6uOGUc1kp3KiVnp1HJbXwOEI2lPyfQxlaAN0Rd7HT2PHQI8DjzhFFpFr3JkjkPndsgu6sC0GQpNAELCrGVVqCNwIl8yebGKGJMUNMB1GfPTM94iTrdXoXaxynBzIdjj/COrTWU+f0fpVerg4w/5HMSgIeDTnfoVZnMJXwAR4CXObsd8cZ5DUYXOoINp4ZJ9CzAXI1F8GykV5/Gf+Tb17AIqxM9X55lrMUT8jnAjQL w+tIxHAs 2i7Qf1ogilUYv8x2t7cPXiQnq7wXMH1AnaArRnzB0SxDLIaPUNsJTm/pOtzkWn24yh/qsfogjZ6d+v23u42nJy0VaVThiPheQK+UebjeH86MqUp1uffRN6JWm+BhYbzSUu+RZG2TXVvea8gw9ZpnLsSBT7xRZRcb/JlbJ1fVO5a5rKBZo/a3sZbg0szSJSzB8IkzorxHp8e7murbKQcyJqBhHNB7C9vSAY5CDQhH48GPZbUMOSngd+BYCAg6B6qIm8NeRZbFU5lu6/q4SJ9jiGpgPJqAUBgtpa2Q17boe2o3CBLXXUCA5KHM+qw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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); -/* Find the kmalloc slab corresponding for a certain size */ -struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags, unsigned long caller); +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 <= 192) + index = kmalloc_size_index[size_index_elem(size)]; + else + index = fls(size - 1); + + return kmalloc_caches[kmalloc_type(flags, caller)][index]; +} 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 using * fls. */ -static u8 size_index[24] __ro_after_init = { +u8 kmalloc_size_index[24] __ro_after_init = { 3, /* 8 */ 4, /* 16 */ 5, /* 24 */ @@ -692,33 +692,6 @@ static u8 size_index[24] __ro_after_init = { 2 /* 192 */ }; -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 caller) -{ - unsigned int index; - - if (size <= 192) { - if (!size) - return ZERO_SIZE_PTR; - - index = size_index[size_index_elem(size)]; - } else { - if (WARN_ON_ONCE(size > KMALLOC_MAX_CACHE_SIZE)) - return NULL; - index = fls(size - 1); - } - - return kmalloc_caches[kmalloc_type(flags, caller)][index]; -} - size_t kmalloc_size_roundup(size_t size) { if (size && size <= KMALLOC_MAX_CACHE_SIZE) { @@ -843,9 +816,9 @@ void __init setup_kmalloc_cache_index_table(void) for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) { unsigned int elem = size_index_elem(i); - if (elem >= ARRAY_SIZE(size_index)) + if (elem >= ARRAY_SIZE(kmalloc_size_index)) break; - size_index[elem] = KMALLOC_SHIFT_LOW; + kmalloc_size_index[elem] = KMALLOC_SHIFT_LOW; } if (KMALLOC_MIN_SIZE >= 64) { @@ -854,7 +827,7 @@ void __init setup_kmalloc_cache_index_table(void) * is 64 byte. */ for (i = 64 + 8; i <= 96; i += 8) - size_index[size_index_elem(i)] = 7; + kmalloc_size_index[size_index_elem(i)] = 7; } @@ -865,7 +838,7 @@ void __init setup_kmalloc_cache_index_table(void) * instead. */ for (i = 128 + 8; i <= 192; i += 8) - size_index[size_index_elem(i)] = 8; + kmalloc_size_index[size_index_elem(i)] = 8; } } @@ -977,10 +950,10 @@ void *__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller return ret; } - s = kmalloc_slab(size, flags, caller); + if (unlikely(!size)) + return ZERO_SIZE_PTR; - if (unlikely(ZERO_OR_NULL_PTR(s))) - return s; + s = kmalloc_slab(size, flags, caller); ret = __kmem_cache_alloc_node(s, flags, node, size, caller); ret = kasan_kmalloc(s, ret, size, flags);