From patchwork Mon Nov 20 18:34:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13461892 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 09779C2BB3F for ; Mon, 20 Nov 2023 18:35:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFB316B040B; Mon, 20 Nov 2023 13:34:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A84546B03AC; Mon, 20 Nov 2023 13:34:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA1E06B03AC; Mon, 20 Nov 2023 13:34:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B30B46B040E for ; Mon, 20 Nov 2023 13:34:48 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 83F77140763 for ; Mon, 20 Nov 2023 18:34:48 +0000 (UTC) X-FDA: 81479183856.24.CDDA5C3 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf12.hostedemail.com (Postfix) with ESMTP id 4E82C40026 for ; Mon, 20 Nov 2023 18:34:45 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=LCzJgngk; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=dVVsCdgV; dmarc=none; spf=pass (imf12.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=1700505286; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=l41Jb+BHNYtQ2OIFgOGmX9WMKyvcGjadAJ7/ZFyre/E=; b=5YS14j1uQXpOsicPop57inqI7sI7OUQZfzBBDIU82NkeESF+IiRm/OB4gnnrrGbndLKuX3 uYU+Nix7pQ4bdfEje/ivgYpRYEPxkcrSVlwPfQn+3GnJwpmOGUIBchT5h8Qs57myDHFhol 4zbPE/HpsK1c0ue92Ww7o5/GLuf1SXI= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=LCzJgngk; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=dVVsCdgV; dmarc=none; spf=pass (imf12.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=1700505286; a=rsa-sha256; cv=none; b=VyFcQ8mYEYnqGY4+/xM2WPcrqhs8ZGEOyLmp1HicGDzqAJZgE492WopEETihXUVCUSc5SN Zlj/Pgo7aa0VBwhit1zyziiD4bXQlFUk27PDZ+sHd/atk1igFP6xEXoIrbKdxlHeRlRea4 PcauVS/G3sXv+xyVrAwbbLNRZdgMJH8= 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 C354A1F8B2; Mon, 20 Nov 2023 18:34:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1700505283; 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=l41Jb+BHNYtQ2OIFgOGmX9WMKyvcGjadAJ7/ZFyre/E=; b=LCzJgngkzITGb5Tf0wQOI+ebqHHcqa+GWs7cc+7WK7+jpNWwF/bcTQTNRVPXcGQ01CGICv ncaCAQ/HZm06a64rnhjOXN3b9S9XNga/ibSht5pA2L6A5MWVv5JAM8bOo7CPnLv0WV56BW q6exGNXv09TdutkEwyn0/rMlVEeaaPw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1700505283; 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=l41Jb+BHNYtQ2OIFgOGmX9WMKyvcGjadAJ7/ZFyre/E=; b=dVVsCdgVXxLlGdzVI19fD3bgdu6SF1+kdlZ9nh7NFaxQIN/5XU0uCCQx033gY2PJHi2iYV xCNf7qL+X9Mol2DQ== 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 8455213499; Mon, 20 Nov 2023 18:34:43 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id OCGvH8OmW2UUMgAAMHmgww (envelope-from ); Mon, 20 Nov 2023 18:34:43 +0000 From: Vlastimil Babka Date: Mon, 20 Nov 2023 19:34:29 +0100 Subject: [PATCH v2 18/21] mm/slab: move kmalloc() functions from slab_common.c to slub.c MIME-Version: 1.0 Message-Id: <20231120-slab-remove-slab-v2-18-9c9c70177183@suse.cz> References: <20231120-slab-remove-slab-v2-0-9c9c70177183@suse.cz> In-Reply-To: <20231120-slab-remove-slab-v2-0-9c9c70177183@suse.cz> To: David Rientjes , Christoph Lameter , Pekka Enberg , Joonsoo Kim Cc: Andrew Morton , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Roman Gushchin , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Marco Elver , Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org, linux-hardening@vger.kernel.org, Vlastimil Babka X-Mailer: b4 0.12.4 X-Rspamd-Queue-Id: 4E82C40026 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: amu9grb355kdcu4bmjo91pxdba8hir8c X-HE-Tag: 1700505285-274502 X-HE-Meta: U2FsdGVkX1/X4XqDNODUcz6w0KCgWAVx+KxbN2Zs5ioRqdgaD0sUlwWpE3oUe4o+v/A5aohEwDwkS4Y61eFfLmON+TubyC12prbZSP/DA2BQVOhcI7Hi63iLgDJIovxyKtuA/jO1s9OlvpgEi+PEN1QywQraHjULTI7rh+/iWNeasXdhHDjdMBLum8PWjceXGsxBByVU/0v8wRVnQfqWeOWhwYhxlxJ78HiL+4MChbP4GZGoOHN9cmF7IInnQQvP4H9tlclIqozJs7rllwCkP2+PKTGMwC11IvzyGJz4gJuFx+UYUt42MajhlGPQzlMXdnaJC8Grdj4FmEul6XcbRmBEtG3X8sdNzk2/EpnAIWvVPnKZSSNiVb+6ZqldpNK8THbKSSEPDHcyI9O37QXkdN19+6rX3NHbZ2nq/s0ilTrminMf4mziP+gWuy+nPy623DNLmpoHBcwh+Uew392VIcut59Eg94opT3jPuB0qEMvl405+S2C6Q/h/laz1S8U98t8TotMXAgFgFkyc66qJHJ5VE1rBdffyFFZCCK3+2OyPS8ujXOVHRjsM0rVGAlnRMjQYhTPr1sqLANWO5+nutPamf5B9sOA8DatSS36HBq0JnNmz8PKzR8JBeKiyofXEJGGKrkRVAwChWe32S2PYhKx1gCFnxK4AYHzL4dC0ZKjTka23C0PaAlMgYegjE82xbvhMnvr1HlGKlX4f9iAuU8PGgkkG7UJEbYfN/GZbXZi062dJXjCyYAE0W/pYtLw7p0rB4OHZoZ3Uqaiw5ZWMPOpbSCafL0PkdB57+KxXOert9z2Fzk6Og95E9dREnCfr3LiSml8QIc7nE8CzsRAkoHNDxiEmrGfnRquGEpBO7FTsKODSN/xCfvv5KpPS7tHJmwL2/5vXHAEaH1lJYeyHdVy/utt8ig+0AVw+vIZkoPROP9IhYExTdwGfT+fu8kkS9TWoMw6sUUUm1BLwXIn kRobSp9D fDBkutEBkHDGCueX1aXZtf30yTTqyAe+cuhgb7BU5moxZ/U3z6JHMp63JCLXhcJbOyXXTkkPu51A2jH18w2C1MWQkphukXauieU5ubSG8m31XYxtMpmfi4QSTMKnFndL31YUpwTF/hljFZR6+gkJOLy6TwGVgtY+z5sheUOrPW7OzU0bH/oW1hyOlJCEuepXnd36HJSILifMNzNOHJIMGiKpvDWKiDBfO1w0L2qMLvqLer6/qSIHjwuNpToHu03dQ/h9Vnfz88JS7SK6QlqeLnKeQl3bfJImjTKOUCZ2V7tr71cVys96KagyCVdRu5t9XQuocJ5k+CJlljLfYZv8Mn4elXZ+1tXcmHD89dHAQ6v22J/ujjXcErs6GzclzxjOiHKSx 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: This will eliminate a call between compilation units through __kmem_cache_alloc_node() and allow better inlining of the allocation fast path. Reviewed-by: Kees Cook Signed-off-by: Vlastimil Babka Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- mm/slab.h | 3 -- mm/slab_common.c | 119 ---------------------------------------------------- mm/slub.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 118 insertions(+), 130 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index 7d7cc7af614e..54deeb0428c6 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -416,9 +416,6 @@ kmalloc_slab(size_t size, gfp_t flags, unsigned long caller) 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, - unsigned long caller); gfp_t kmalloc_fix_flags(gfp_t flags); /* Functions provided by the slab allocators */ diff --git a/mm/slab_common.c b/mm/slab_common.c index 31ade17a7ad9..238293b1dbe1 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -936,50 +936,6 @@ void __init create_kmalloc_caches(slab_flags_t flags) slab_state = UP; } -static void *__kmalloc_large_node(size_t size, gfp_t flags, int node); -static __always_inline -void *__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller) -{ - struct kmem_cache *s; - void *ret; - - if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) { - ret = __kmalloc_large_node(size, flags, node); - trace_kmalloc(caller, ret, size, - PAGE_SIZE << get_order(size), flags, node); - return ret; - } - - if (unlikely(!size)) - return ZERO_SIZE_PTR; - - s = kmalloc_slab(size, flags, caller); - - ret = __kmem_cache_alloc_node(s, flags, node, size, caller); - ret = kasan_kmalloc(s, ret, size, flags); - trace_kmalloc(caller, ret, size, s->size, flags, node); - return ret; -} - -void *__kmalloc_node(size_t size, gfp_t flags, int node) -{ - return __do_kmalloc_node(size, flags, node, _RET_IP_); -} -EXPORT_SYMBOL(__kmalloc_node); - -void *__kmalloc(size_t size, gfp_t flags) -{ - return __do_kmalloc_node(size, flags, NUMA_NO_NODE, _RET_IP_); -} -EXPORT_SYMBOL(__kmalloc); - -void *__kmalloc_node_track_caller(size_t size, gfp_t flags, - int node, unsigned long caller) -{ - return __do_kmalloc_node(size, flags, node, caller); -} -EXPORT_SYMBOL(__kmalloc_node_track_caller); - /** * __ksize -- Report full size of underlying allocation * @object: pointer to the object @@ -1016,30 +972,6 @@ size_t __ksize(const void *object) return slab_ksize(folio_slab(folio)->slab_cache); } -void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) -{ - void *ret = __kmem_cache_alloc_node(s, gfpflags, NUMA_NO_NODE, - size, _RET_IP_); - - trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, NUMA_NO_NODE); - - ret = kasan_kmalloc(s, ret, size, gfpflags); - return ret; -} -EXPORT_SYMBOL(kmalloc_trace); - -void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, - int node, size_t size) -{ - void *ret = __kmem_cache_alloc_node(s, gfpflags, node, size, _RET_IP_); - - trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, node); - - ret = kasan_kmalloc(s, ret, size, gfpflags); - return ret; -} -EXPORT_SYMBOL(kmalloc_node_trace); - gfp_t kmalloc_fix_flags(gfp_t flags) { gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK; @@ -1052,57 +984,6 @@ gfp_t kmalloc_fix_flags(gfp_t flags) return flags; } -/* - * To avoid unnecessary overhead, we pass through large allocation requests - * directly to the page allocator. We use __GFP_COMP, because we will need to - * know the allocation order to free the pages properly in kfree. - */ - -static void *__kmalloc_large_node(size_t size, gfp_t flags, int node) -{ - struct page *page; - void *ptr = NULL; - unsigned int order = get_order(size); - - if (unlikely(flags & GFP_SLAB_BUG_MASK)) - flags = kmalloc_fix_flags(flags); - - flags |= __GFP_COMP; - page = alloc_pages_node(node, flags, order); - if (page) { - ptr = page_address(page); - mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B, - PAGE_SIZE << order); - } - - ptr = kasan_kmalloc_large(ptr, size, flags); - /* As ptr might get tagged, call kmemleak hook after KASAN. */ - kmemleak_alloc(ptr, size, 1, flags); - kmsan_kmalloc_large(ptr, size, flags); - - return ptr; -} - -void *kmalloc_large(size_t size, gfp_t flags) -{ - void *ret = __kmalloc_large_node(size, flags, NUMA_NO_NODE); - - trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size), - flags, NUMA_NO_NODE); - return ret; -} -EXPORT_SYMBOL(kmalloc_large); - -void *kmalloc_large_node(size_t size, gfp_t flags, int node) -{ - void *ret = __kmalloc_large_node(size, flags, node); - - trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size), - flags, node); - return ret; -} -EXPORT_SYMBOL(kmalloc_large_node); - #ifdef CONFIG_SLAB_FREELIST_RANDOM /* Randomize a generic freelist */ static void freelist_randomize(unsigned int *list, diff --git a/mm/slub.c b/mm/slub.c index 2baa9e94d9df..d6bc15929d22 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3851,14 +3851,6 @@ void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, } EXPORT_SYMBOL(kmem_cache_alloc_lru); -void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, - int node, size_t orig_size, - unsigned long caller) -{ - return slab_alloc_node(s, NULL, gfpflags, node, - caller, orig_size); -} - /** * kmem_cache_alloc_node - Allocate an object on the specified node * @s: The cache to allocate from. @@ -3882,6 +3874,124 @@ void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node) } EXPORT_SYMBOL(kmem_cache_alloc_node); +/* + * To avoid unnecessary overhead, we pass through large allocation requests + * directly to the page allocator. We use __GFP_COMP, because we will need to + * know the allocation order to free the pages properly in kfree. + */ +static void *__kmalloc_large_node(size_t size, gfp_t flags, int node) +{ + struct page *page; + void *ptr = NULL; + unsigned int order = get_order(size); + + if (unlikely(flags & GFP_SLAB_BUG_MASK)) + flags = kmalloc_fix_flags(flags); + + flags |= __GFP_COMP; + page = alloc_pages_node(node, flags, order); + if (page) { + ptr = page_address(page); + mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B, + PAGE_SIZE << order); + } + + ptr = kasan_kmalloc_large(ptr, size, flags); + /* As ptr might get tagged, call kmemleak hook after KASAN. */ + kmemleak_alloc(ptr, size, 1, flags); + kmsan_kmalloc_large(ptr, size, flags); + + return ptr; +} + +void *kmalloc_large(size_t size, gfp_t flags) +{ + void *ret = __kmalloc_large_node(size, flags, NUMA_NO_NODE); + + trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size), + flags, NUMA_NO_NODE); + return ret; +} +EXPORT_SYMBOL(kmalloc_large); + +void *kmalloc_large_node(size_t size, gfp_t flags, int node) +{ + void *ret = __kmalloc_large_node(size, flags, node); + + trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size), + flags, node); + return ret; +} +EXPORT_SYMBOL(kmalloc_large_node); + +static __always_inline +void *__do_kmalloc_node(size_t size, gfp_t flags, int node, + unsigned long caller) +{ + struct kmem_cache *s; + void *ret; + + if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) { + ret = __kmalloc_large_node(size, flags, node); + trace_kmalloc(caller, ret, size, + PAGE_SIZE << get_order(size), flags, node); + return ret; + } + + if (unlikely(!size)) + return ZERO_SIZE_PTR; + + s = kmalloc_slab(size, flags, caller); + + ret = slab_alloc_node(s, NULL, flags, node, caller, size); + ret = kasan_kmalloc(s, ret, size, flags); + trace_kmalloc(caller, ret, size, s->size, flags, node); + return ret; +} + +void *__kmalloc_node(size_t size, gfp_t flags, int node) +{ + return __do_kmalloc_node(size, flags, node, _RET_IP_); +} +EXPORT_SYMBOL(__kmalloc_node); + +void *__kmalloc(size_t size, gfp_t flags) +{ + return __do_kmalloc_node(size, flags, NUMA_NO_NODE, _RET_IP_); +} +EXPORT_SYMBOL(__kmalloc); + +void *__kmalloc_node_track_caller(size_t size, gfp_t flags, + int node, unsigned long caller) +{ + return __do_kmalloc_node(size, flags, node, caller); +} +EXPORT_SYMBOL(__kmalloc_node_track_caller); + +void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) +{ + void *ret = slab_alloc_node(s, NULL, gfpflags, NUMA_NO_NODE, + _RET_IP_, size); + + trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, NUMA_NO_NODE); + + ret = kasan_kmalloc(s, ret, size, gfpflags); + return ret; +} +EXPORT_SYMBOL(kmalloc_trace); + +void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, + int node, size_t size) +{ + void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, size); + + trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, node); + + ret = kasan_kmalloc(s, ret, size, gfpflags); + return ret; +} +EXPORT_SYMBOL(kmalloc_node_trace); + static noinline void free_to_partial_list( struct kmem_cache *s, struct slab *slab, void *head, void *tail, int bulk_cnt,