Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1389724rdb; Wed, 6 Dec 2023 18:40:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IHRFw8f6WIE6iASlRk/4jFfdYvePS3aCeU6uBFYb0okPZ6ydLIfgynLBgivgkP1Go+Z5E27 X-Received: by 2002:a05:6358:719:b0:16e:4c3:f8c3 with SMTP id e25-20020a056358071900b0016e04c3f8c3mr2483777rwj.11.1701916842122; Wed, 06 Dec 2023 18:40:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701916842; cv=none; d=google.com; s=arc-20160816; b=hM2SLSKcPQM+AbpKpaUxEMV6qbiMl90JMVyIE/eb+NTxsETK4x6wtwboiQBSpQlHa4 Je96za30zFJbkfqDv51ebx0ziCgJRx1vnvQObKqY9CIs3YbOdDSke2yEwvVgRtef/ya2 uGXH2il8tgsCo4Ph6PBomO7bpY9DrFZIJnoH237veKD9JFNusJHg20Yr+cEgGxdp3skC wZze4kG7et23rKTTQIXbRPQKksylcXYm+8XZUtGYq4Wf3MwpCfWORqMdmaXUm/UG412M pPeM0elDjofP+4u+xRCKZwO3oXLdAEBSGK7IGdzZxLfTN8A7YQ4xQ3NiFesdNRs7d/Ct /8Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=CjGR+gix1XQRxaJ5p7IMY1S7CLbhofOgFFoOucSld8k=; fh=ezGgTJoHdqijbJ6IWHiSjHZUYI0p+VPBCX/sPRmQd+E=; b=IUYEtTc6HeyBk6Q5kh9d+J5sesDAaB9fRZy1RgRc9QfdtfTW0sroQFuAnlF219zcJ2 e8YKwPaEmcpX746rZODGetiULWCb7putAEs1w1NMr2ok2BAy/OYECohuAwwhEnJi5Aoy iQpLMf98AZ+0DQEI70HM8V1OuAKVe2shm2M12KWeoxaOtj65Mtt/NXnjbuArOnNEKCqD umQIgeB9rTN67/xSu2OWKZ1EERgImSciGFRglSFQROigw+DsZWKE4ViwT9d2k20pFnV5 7o4ZqA4aup3vGe4APJH8b207E7lQA0Cwk3UkM9FZW05DO41hMM6EscVPx/mGJyB1glih M9yQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=FgiFiSVI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id cn10-20020a056a020a8a00b005b8f446408bsi300912pgb.422.2023.12.06.18.40.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 18:40:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=FgiFiSVI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id A267C81F3615; Wed, 6 Dec 2023 18:40:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443025AbjLGCkL (ORCPT + 99 others); Wed, 6 Dec 2023 21:40:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235205AbjLGCkK (ORCPT ); Wed, 6 Dec 2023 21:40:10 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06FFAD5E; Wed, 6 Dec 2023 18:40:15 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6ce6caedce6so147486b3a.3; Wed, 06 Dec 2023 18:40:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701916814; x=1702521614; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=CjGR+gix1XQRxaJ5p7IMY1S7CLbhofOgFFoOucSld8k=; b=FgiFiSVIWYuZAXOi9ywgWfJl2OcRi3OVnj4BY4f1INUoulTPxFtwsYLZAtx1oAhTPR CuFVDDHYus/RIP0/nDDMSjxWTVK+REDu5P+6fpicNdwLNMXawtje0+1d+xpztLNvVRjy 58R2O0+9tW6G8SPCTE474skVXMOePlMbYV1LTR3yNQRpJtpbYELZAcycx+hgZNUu6ZoB LlV7PvdY/huazd3H1QUntLaanccCFLLLch9EhR39XBxszty5h7I8Vl00n/9H7q7/fFIY 9ZEzt1WtuLPXdKZEYjXo6eoX6r8xUhh0J0HUYgHKHHmUUN2Rx6BDkFhQizx67xyYzIgK sVIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701916814; x=1702521614; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=CjGR+gix1XQRxaJ5p7IMY1S7CLbhofOgFFoOucSld8k=; b=Zq3pv92HmRtTvmdcmG8sxPDrVNz3CIxz6pUP//0IHWxyNuBvTskvenQAx58F9mYikE fiosgOuoiYGSH4wOUt97IC5/U2zrwZRhMwgJa3Wd2BnIr66GceuN9Epmj1c3IOgbMIzE KAY9agetFj9/2akjYrGPNx+TjTfmSAmUerXdDvKkZ+sEN4PQ94f/YBSYQSOavVIjsrxE L46+EA+LBhMRW4L6XxlvjX6Nh1lCTFIu+yS+vj0UPpXGruop44i7LTaAe2SvUQ+L1yWT vcfy3TIkOXFONq/4StWDQ1QduaOJsWReFyVwYnNTST86X9tWEhVti9mKGL4b3LiKaizf ojFw== X-Gm-Message-State: AOJu0Yzj+/qc9U5M2LA6bArebTOH1uCSMnZqZuMmRHlRM2VrfhSoyYq8 IOu+anD4bQJ6eQVsgMMEaew= X-Received: by 2002:a05:6a20:1593:b0:18f:97c:4f3b with SMTP id h19-20020a056a20159300b0018f097c4f3bmr1673961pzj.71.1701916814237; Wed, 06 Dec 2023 18:40:14 -0800 (PST) Received: from localhost.localdomain ([1.245.180.67]) by smtp.gmail.com with ESMTPSA id pl16-20020a17090b269000b0028652f98978sm121526pjb.8.2023.12.06.18.40.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 18:40:13 -0800 (PST) Date: Thu, 7 Dec 2023 11:40:05 +0900 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Vlastimil Babka Cc: David Rientjes , Christoph Lameter , Pekka Enberg , Joonsoo Kim , Andrew Morton , 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 Subject: Re: [PATCH v2 21/21] mm/slub: optimize free fast path code layout Message-ID: References: <20231120-slab-remove-slab-v2-0-9c9c70177183@suse.cz> <20231120-slab-remove-slab-v2-21-9c9c70177183@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231120-slab-remove-slab-v2-21-9c9c70177183@suse.cz> X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 06 Dec 2023 18:40:35 -0800 (PST) On Mon, Nov 20, 2023 at 07:34:32PM +0100, Vlastimil Babka wrote: > 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 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); > } > > > -- Looks good to me, Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> > 2.42.1 > >