From patchwork Mon Nov 20 18:34:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13461890 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 CE197C2BB3F for ; Mon, 20 Nov 2023 18:35:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57E296B03E0; Mon, 20 Nov 2023 13:34:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 472886B042F; 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 ACFD06B03E0; Mon, 20 Nov 2023 13:34:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 841EC6B03AC for ; Mon, 20 Nov 2023 13:34:48 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6D7131CB3EB for ; Mon, 20 Nov 2023 18:34:48 +0000 (UTC) X-FDA: 81479183856.26.2F05FD9 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf03.hostedemail.com (Postfix) with ESMTP id 4893D20011 for ; Mon, 20 Nov 2023 18:34:46 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=dA1DtXnN; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=hPvTmXax; spf=pass (imf03.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none 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=CxVtau361btrxEZ1p8oQK+uCjxmURZ9Wrr8QyUc+PdA=; b=eGN/8DpqjuhRJpKYz48gbl/zMGw6tYkyh8TvjWjVg45pCAGhSFN5HfVHKINrVBtJKy0+H7 XnW+ii4svoT9VSW05EHXa5Y3Y9M3WhTmyj4ggUF68rABX1sWbYIIUEez+R7rERzCT6lJFp 44NzmiMyVq39onkWiqCQ5g6y9qSLMmc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700505286; a=rsa-sha256; cv=none; b=0vbAdcjVDtc1dH6YGFGaBZjYSrD9thBSaYevvoan1JIFB7D8iUB9SIgs55lg78hX2hxyO4 Mov0HmKCS9obvNJrIYPyXobJm5S1zeHiLs1LjSqVzBmB87j5Q0djXQAFnNCwajKLl9DXWH fnmAfEwPg7vJJY7WHlyQcexjMtzXMJM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=dA1DtXnN; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=hPvTmXax; spf=pass (imf03.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none 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 A69CC1F8B4; Mon, 20 Nov 2023 18:34:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1700505284; 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=CxVtau361btrxEZ1p8oQK+uCjxmURZ9Wrr8QyUc+PdA=; b=dA1DtXnNqrzA2tfUWppvLMcFXSoz5YA8kR1l30NMkK9d/reHC3N3+6WLFQbFu+6G/gWj76 JC3Uh1TALn6ggVdxqlFKre2d3QneXkVjlbhMiZCcq3uLU/WAx9iR2vAZpfgBG5X2D4mPUD jM3np8iFo/V/3eDfwFTF2GdSkTpwG4M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1700505284; 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=CxVtau361btrxEZ1p8oQK+uCjxmURZ9Wrr8QyUc+PdA=; b=hPvTmXaxercEKsn6Df6woNxnTvve8p2Um8LN9+kKZnVI6mHvDWKoVXO6fZr38i79VzZZAW lzYTXtH3eDkXvLAA== 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 67EF913912; Mon, 20 Nov 2023 18:34:44 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id UHi1GMSmW2UUMgAAMHmgww (envelope-from ); Mon, 20 Nov 2023 18:34:44 +0000 From: Vlastimil Babka Date: Mon, 20 Nov 2023 19:34:32 +0100 Subject: [PATCH v2 21/21] mm/slub: optimize free fast path code layout MIME-Version: 1.0 Message-Id: <20231120-slab-remove-slab-v2-21-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: 4893D20011 X-Rspam-User: X-Stat-Signature: rqad7upxcknqthipwcbbar7gmaaiwjn9 X-Rspamd-Server: rspam03 X-HE-Tag: 1700505286-250503 X-HE-Meta: U2FsdGVkX18DVhk6Hwt/GecJzVJ5dXk6tHznzSNcVGBp+MWpMwmFFuaarGCNDDEH5q2nIVkhBgrG9ehpX++AZZyJ8CF+FAkwORj7/K7Vhxx9PnUPAro6RyOgXuZiu4zMJHlw2dV/PKVXlXmrKZDX1GF7sIBmBeO57pOfBa3frfQSoq7N/golyFW+27DvkrUIP3Fxcq0C19lO50oHesjhI/shOUztX+yKfKlgtapfoqTn9DFrxaFuWQzQKIxcMSUPxqKsFEjcSBWC4+mvNPMQcqXBI2v1HKUVoy4wf5GJpxT6ZhpTwfEG/1oowtLu/eI4lJ4BfBO4l17LZ+nRqUNVNj9tbvdxw6Sfbr+pnJwXMUb2OFRvD2CjtgXD9FTFCzd78FBt2trmdutoZH8IQZDvuBxC5wL9iwpeidahVY1wCe1t+fBGRhtNa+GJQR5lUzJ6gEZ22bOZTsHvIZaqe4sE4FCEDaIvCgCvaaQl4ZOPd2babSLuvfadsQykzyVlGgyRS3/eCDANc/0JxHjqQCfM/bcGMZYSeOHyUiv0OfcEC7bkYzjkScjuNqLIITxtsdqhMW7SIicBN/BLeLeMvzBwD1jneljbSNBefFubrZ+gMWdUEvXMhOly91yOwBeM0Q3UnwEQ+eTK2a5d+DxjBV4kcaKk3UuvA5M2NRI4s/DQeu4U3/1lHXVhLpofk5n6L7ZbpNgxlS29kes3pn2RfiuqlGm6gudj0refhmhS62jDAU9QXQJNcTW5G6h5oME96w3KX5Qqeji4o+2wjybIjncbSc7owI1+kyXDAUWcDkaiu4/McnHD8H2cCCFpQCww8jYRw3ZIzlFS/0g08A+c1+xwQix/MuC9Rv9WZYghJn7hQyIRvuatzccmYYpUDCpNzgZdvdTBa4SIA3VfOqJn6YXOB/KjTKdP7gwVIQtSdWE7x2Fb6lqnJnBLwKqpv+nSpb/1pkR7YGHGj9HDzs2mkNG +X5Eei6J 10Em70JRgg+h4Zh0sr8yyjOiakRXNDdiQCBSowvRjl0LTnY/KvWqqDq4xtMLeJOyhzEWZR6kgpzPAlahJszsB76lelwlhJoSADoppq5lQZnnabSyPqOLoIE+1Ec9v5qdQDsgbJewMue1BeCK78ovIP4+RRB492jfLyNlPkHokYZBSu4Gqw4CpI+werqXbzh1OgUNmChiCqMmf5KbIcBcu6W+YfJn3ZyvLzeuEbcb0raBkmJLYstn4db1IhjlKUbkdObA441HU+0hzJeQ= 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: Inspection of kmem_cache_free() disassembly showed we could make the fast path smaller by providing few more hints to the compiler, and splitting the memcg_slab_free_hook() into an inline part that only checks if there's work to do, and an out of line part doing the actual uncharge. bloat-o-meter results: add/remove: 2/0 grow/shrink: 0/3 up/down: 286/-554 (-268) Function old new delta __memcg_slab_free_hook - 270 +270 __pfx___memcg_slab_free_hook - 16 +16 kfree 828 665 -163 kmem_cache_free 1116 948 -168 kmem_cache_free_bulk.part 1701 1478 -223 Checking kmem_cache_free() disassembly now shows the non-fastpath cases are handled out of line, which should reduce instruction cache usage. Signed-off-by: Vlastimil Babka Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- mm/slub.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 77d259f3d592..3f8b95757106 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1959,20 +1959,11 @@ void memcg_slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, return __memcg_slab_post_alloc_hook(s, objcg, flags, size, p); } -static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, - void **p, int objects) +static void __memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, + void **p, int objects, + struct obj_cgroup **objcgs) { - struct obj_cgroup **objcgs; - int i; - - if (!memcg_kmem_online()) - return; - - objcgs = slab_objcgs(slab); - if (!objcgs) - return; - - for (i = 0; i < objects; i++) { + for (int i = 0; i < objects; i++) { struct obj_cgroup *objcg; unsigned int off; @@ -1988,6 +1979,22 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, obj_cgroup_put(objcg); } } + +static __fastpath_inline +void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, + int objects) +{ + struct obj_cgroup **objcgs; + + if (!memcg_kmem_online()) + return; + + objcgs = slab_objcgs(slab); + if (likely(!objcgs)) + return; + + __memcg_slab_free_hook(s, slab, p, objects, objcgs); +} #else /* CONFIG_MEMCG_KMEM */ static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr) { @@ -2047,7 +2054,7 @@ static __always_inline bool slab_free_hook(struct kmem_cache *s, * The initialization memset's clear the object and the metadata, * but don't touch the SLAB redzone. */ - if (init) { + if (unlikely(init)) { int rsize; if (!kasan_has_integrated_init()) @@ -2083,7 +2090,8 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s, next = get_freepointer(s, object); /* If object's reuse doesn't have to be delayed */ - if (!slab_free_hook(s, object, slab_want_init_on_free(s))) { + if (likely(!slab_free_hook(s, object, + slab_want_init_on_free(s)))) { /* Move object to the new freelist */ set_freepointer(s, object, *head); *head = object; @@ -4282,7 +4290,7 @@ static __fastpath_inline void slab_free(struct kmem_cache *s, struct slab *slab, * With KASAN enabled slab_free_freelist_hook modifies the freelist * to remove objects, whose reuse must be delayed. */ - if (slab_free_freelist_hook(s, &head, &tail, &cnt)) + if (likely(slab_free_freelist_hook(s, &head, &tail, &cnt))) do_slab_free(s, slab, head, tail, cnt, addr); }