From patchwork Mon Nov 13 19:14:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13454375 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 4C176C4332F for ; Mon, 13 Nov 2023 19:15:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDBBD6B0289; Mon, 13 Nov 2023 14:14:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D77B06B028A; Mon, 13 Nov 2023 14:14:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A900B6B028E; Mon, 13 Nov 2023 14:14:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 69FFF6B028A for ; Mon, 13 Nov 2023 14:14:19 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 51637160622 for ; Mon, 13 Nov 2023 19:14:19 +0000 (UTC) X-FDA: 81453881838.13.C25A1BC Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf15.hostedemail.com (Postfix) with ESMTP id 60199A0029 for ; Mon, 13 Nov 2023 19:14:17 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=MCduxZYm; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=V8jBNiQR; spf=pass (imf15.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=1699902857; 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=HPRL/KrzqhhRv//jIzaBEKs7IEvhqlXTEPf11LGvT+E=; b=IOI2vOqJfTbZfLGmE0ISr04tu0kKwx1Tp3esBKG8DAr1AMF/lltFXKlYYZ2Oy4bqcHnkgj RaEqm15njfbP8AGiaI5ez1UAdFIFgMrCicGzBV/MGf4E1Om7Rm12z46TAXxuumbZ4RIMNQ /GmfBV0ljFWq7pp6Nn3znKjAnX1eBHk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699902857; a=rsa-sha256; cv=none; b=4UqFQ6lXulzHU0qi0N39eYOmwLkScIYQRgPeiV32x9ITZ2de2FRXd7ZENDYgfUMied6E4V C/ChShzU/nffy12d7SGiYconAlBLsKJ2ECNjuxDhzjglbA2WigWMR6Zb5o/sUhIqOdgFAz VITdvwfXPLfiZbnwplTWZQU1UOzrVyw= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=MCduxZYm; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=V8jBNiQR; spf=pass (imf15.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 237611F88F; Mon, 13 Nov 2023 19:14:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1699902856; 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=HPRL/KrzqhhRv//jIzaBEKs7IEvhqlXTEPf11LGvT+E=; b=MCduxZYmVuQsqxVK7yN6VRgd6oRH1+OX+bZLl8Vq/FjOKSCiK1k9AbIeXyymmv5FFiChqN BrILdcy4pVskHuPR1IM6wOan8Zd7bZZfAX9X8kZte2Wtwcm+KZ7P4HFKM5WjJe/L4pdaPg C72FBd07XRG9KFPVqy66V6Gvg2vePOk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1699902856; 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=HPRL/KrzqhhRv//jIzaBEKs7IEvhqlXTEPf11LGvT+E=; b=V8jBNiQRZxe6eGxYJtbNWf/MQPc4nz18AGu/sDaheaWDiPfd7VFSosfgTjsJi1S0XqFTQK D7rYRLY+Nl5VtdBg== 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 C7F5313398; Mon, 13 Nov 2023 19:14:15 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id aLpDMId1UmVFOgAAMHmgww (envelope-from ); Mon, 13 Nov 2023 19:14:15 +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 20/20] mm/slub: optimize free fast path code layout Date: Mon, 13 Nov 2023 20:14:01 +0100 Message-ID: <20231113191340.17482-42-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: 60199A0029 X-Rspam-User: X-Stat-Signature: ug8oibbspfkicbh8mhrxopmmx37ardn4 X-Rspamd-Server: rspam03 X-HE-Tag: 1699902857-565658 X-HE-Meta: U2FsdGVkX18clLGkzCFldmtwItW7fZjlft+UcRvvx2VfKhUMtvIXdqkkxGl2WK/tujFKZRfLZDZoEZ6mtlBCnCGRsNQInUr5vpz7cmNXbON+tnkeN5zVIheaFL7X742aSb0ug2y13Tq0dEU1jPFCKag04tBhVEuCppcwHSegr/XRKXfKphpKeO8rrB0SsX0iwhnhGRJqzlwZ6nRduJsgNaC/xLtQ6UmgwMJpn9OD1fJufzZcm6ielFRMoP62Y0poCwroJpTzFKd1W2VzVC02hkbXIIw5HoIQl+XROS90sJAamp9F4jpbgeWqvvxVAZTh+9wGvMKdRGIilXxV3olSD5GjSjtIsARKq7+O4pqDWIF+wNSXZaVjFicny4cAg4wEtKMqm90N/05ySeQu9/51lSTS3oAYqAQm5idvOrc0JgvyHrlpbBRdBRLQS6Jw9yN8hbDt5d9bStkKMqSl6A3BeoRa4OOKRYCRSExKeHhg9ChjpjPd8itFtmiIVpY+a3DbQsiwuY++m6jSpAp+iOMNoMrAWxbtTnXYP1QlxU6RTNsrHYAKNgTNasLwW+lndwNSqQdPmsOHUMlyPhsS9p5o8VH6p0yvIBbkx7TGxirrMk8EUoFJb210lYvAmUbjwl38582dl8Pr/5tIdssJqmQQGr5NzDkrjmd02O1vRj3jgapDGrUld+AMQI8in6jTSK50E6/dhA6gDn0FQOU1u/OEQHbVRXNNk/uxs0RaJOW2kCqqui0IJ2cww7uLf6MhGTq2yK09Z+blUFZsFgJx7Sv1lSMYzGD4kKB9YhoemQkPjhYhY0sX6XZyTT6RE85Fh6zFlQtAvA17VrOk0gAVKRD52UIQOcbOq1fGJRqOnz+ESvctf/OiJd1aW1m+3IPBj4sa1AaJj9WhKmrKM0z1fnoBGiI7r5PwRflA6NLHZq43CuE2t/krgvKrxveNj5SzHLky5QenNiI4W9qVLXKJVnv Vpr/yHIc fAu4ilL/i5d/bI68pAHVvRYy/hNiHbHWvg1cYiBPYQSXH4AC84gr9f8E++pR2SoR4CYcf4eiHDDMC8PfMBTygNlc3HWB0roDlRUV2SwDwhFVrbpkBvkICBk2wckiJx1P9nnfFHaee2BjOIDmfjGG8h8hxFEGWEMzJ0SDSUnU/I0ltIXGtr4wHMWzNj7wZN8UC59w77IFO3cOgV7HQAft4YimibRv22s1tQadbFAbtJ2W+ReOJYHyy5W+bNet3woYhg1dk 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 --- mm/slub.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 7a40132b717a..ae1e6e635253 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; @@ -4270,7 +4278,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); }