Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4521154rdh; Wed, 29 Nov 2023 04:01:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IEuo3djDZBgaZ34x08g4qTRM1SsqbSbBEhik7/Md+ua8SoN3KbV2245j8NX8jfN2+W+8/mQ X-Received: by 2002:a05:6870:568d:b0:1fa:261f:5336 with SMTP id p13-20020a056870568d00b001fa261f5336mr16718723oao.10.1701259263443; Wed, 29 Nov 2023 04:01:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701259263; cv=none; d=google.com; s=arc-20160816; b=OypoioFQGJVNREoBFGoyDj3jW6qA0wEqlM/u1x+tw457d+bqnR3g9sYk1ZPzPWCQRH bAYV4fj/YH9jRkC9DonkQslXD6Zx/bI2FoTvoRtJ+5oD/L9abG1aRsodtBaG3IrLhHtD Fjvs2NEMFGovRzh7Vy/Z3Xw0behkCGEVFMXRzvvtuHmHb71P4Zmmnk+xAyjTJwyaxPqc miIOqRHw9By1PlUhOty7g8Yd3B35KQYyJeRj6eTf+DbVCuVb0HXpLwSTVD0CO1H3t/FW gY0oe9zaT/uHDSi5pZN4rw4h+/7CEPMSYqfHeqm2GIpzRSNDvwZ6WE2AZiBjO0mF1Ijv t8xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=nPXPu1JYkKeWx1IZ2D0WOSznovDcK9NZrhDXsB39l8g=; fh=BCNYuPqol1vLzLo/yJnM/ouRZane89kp7ezRxw3G5Zc=; b=c2Ks7MKNFPGohZ+n1Lhl0hk+oyZzK/rAdQRLd1RoV7dQL6eMfIQ6Oqgt6Fk0LlN+st Q+KclpHimhsfh5YzOGfnLgHnDJU7+/eu4zYA8Vc6/UlVFCqfZ3dJ5jACzEn6cquuGQ3h lqIc5efkB4PqmlgsDvWtfMUg5iB98WI7g8j4s4qdpH2NM9l0YuC2Tpt5m0YLIFwR1iQq HOwffKxN+sLoGTgsnd0KjE6zLLYELH0aY5HDBgR8W+iYWY3MyFShHfEVJ/MQhMYAS+jq 6EVkBJPfPrXa7H5N7JpZS5loRcbzlRBoudX7y9MF8JA+EXljUFrFDClg+S888aTdcp6k YevA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PywMPGeU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id b11-20020a056870470b00b001fa21454a88si3837308oaq.4.2023.11.29.04.01.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 04:01:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PywMPGeU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id C5469808001C; Wed, 29 Nov 2023 04:00:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230320AbjK2MAL (ORCPT + 99 others); Wed, 29 Nov 2023 07:00:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229542AbjK2MAK (ORCPT ); Wed, 29 Nov 2023 07:00:10 -0500 Received: from mail-ua1-x92c.google.com (mail-ua1-x92c.google.com [IPv6:2607:f8b0:4864:20::92c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 286C2D48 for ; Wed, 29 Nov 2023 04:00:16 -0800 (PST) Received: by mail-ua1-x92c.google.com with SMTP id a1e0cc1a2514c-7c46c7bc538so1811968241.3 for ; Wed, 29 Nov 2023 04:00:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701259215; x=1701864015; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=nPXPu1JYkKeWx1IZ2D0WOSznovDcK9NZrhDXsB39l8g=; b=PywMPGeUJG5uFQKfheyvmk0xN2fNIacl8nwQMRoF+P2cgOyX6/Y1A3OjQobHD3v78k AQlmOaDWM4wshk6Q9i6/mYxFqcbj+in/uzg4tiBMMXIm9vIKdP1X8uwxoSmh81xqKzdw wGFF6FQqafHlwA14gzEmyqUY1+ZHZk7SbWmKN6weINehwNr0njjiiyYvlpdvDSmMAiUs WpjssuqcWFzf1n8ZEBp2wjzUQH27XE36khfJULjJCtFlre5e3Cp8lhpF3E9NRNX17/So 6LRUS4BF9RbD6EQ1tYAMfUeRwaYJimuxK3OBdE8ep6H9vgIbIVSAqlv7b/HPveqlWTm4 gGfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701259215; x=1701864015; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nPXPu1JYkKeWx1IZ2D0WOSznovDcK9NZrhDXsB39l8g=; b=go/5+cNTCbjMNUoMvjM1sK2Jt/ofP7mmKCzh7f6M6I9l0+9FQsUL0wmS3OTJfadgg9 8pRMjV5NG5g7C/jHL2Dr6RYU8VfKeMTG0e7Xd2f2obu9YtmF2PUCvzjnR+XYp6t41HbX 528d/0yFwa5j0b5w4XjBSC9fJ9+zBIPTOZZAkqvRnV5dvP8vg6joEslubsLpj7O43ttP KjbgsD1yFoG+ul7l0zfxj7iattHkZ5RPBoEnONuLFrDwwhuKUp7r/Ia03+ewplZGnK3K beQOiRLyzD/a9J+8avHon/LQd6ILi6i7ptA+C3Ersqtl6xuOcrLz598+6GbEIf9huMjH ulXA== X-Gm-Message-State: AOJu0YxHm2TQA36pC7EfdajQOgQtnej9oTJOv7Z7a4WBdXkMIZS5rjwA sPsz3ffJgxquZRI0E9yoV3rle86ZYLTCh2qB2B+MbQ== X-Received: by 2002:a05:6102:5108:b0:460:621c:d14b with SMTP id bm8-20020a056102510800b00460621cd14bmr20684204vsb.20.1701259215088; Wed, 29 Nov 2023 04:00:15 -0800 (PST) MIME-Version: 1.0 References: <20231129-slub-percpu-caches-v3-0-6bcf536772bc@suse.cz> <20231129-slub-percpu-caches-v3-4-6bcf536772bc@suse.cz> In-Reply-To: <20231129-slub-percpu-caches-v3-4-6bcf536772bc@suse.cz> From: Marco Elver Date: Wed, 29 Nov 2023 13:00:00 +0100 Message-ID: Subject: Re: [PATCH RFC v3 4/9] mm/slub: free KFENCE objects in slab_free_hook() To: Vlastimil Babka Cc: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Matthew Wilcox , "Liam R. Howlett" , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Alexander Potapenko , Dmitry Vyukov , linux-mm@kvack.org, linux-kernel@vger.kernel.org, maple-tree@lists.infradead.org, kasan-dev@googlegroups.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 29 Nov 2023 04:00:26 -0800 (PST) On Wed, 29 Nov 2023 at 10:53, Vlastimil Babka wrote: > > When freeing an object that was allocated from KFENCE, we do that in the > slowpath __slab_free(), relying on the fact that KFENCE "slab" cannot be > the cpu slab, so the fastpath has to fallback to the slowpath. > > This optimization doesn't help much though, because is_kfence_address() > is checked earlier anyway during the free hook processing or detached > freelist building. Thus we can simplify the code by making the > slab_free_hook() free the KFENCE object immediately, similarly to KASAN > quarantine. > > In slab_free_hook() we can place kfence_free() above init processing, as > callers have been making sure to set init to false for KFENCE objects. > This simplifies slab_free(). This places it also above kasan_slab_free() > which is ok as that skips KFENCE objects anyway. > > While at it also determine the init value in slab_free_freelist_hook() > outside of the loop. > > This change will also make introducing per cpu array caches easier. > > Signed-off-by: Vlastimil Babka Tested-by: Marco Elver > --- > mm/slub.c | 21 ++++++++++----------- > 1 file changed, 10 insertions(+), 11 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index 7d23f10d42e6..59912a376c6d 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -1772,7 +1772,7 @@ static bool freelist_corrupted(struct kmem_cache *s, struct slab *slab, > * production configuration these hooks all should produce no code at all. > * > * Returns true if freeing of the object can proceed, false if its reuse > - * was delayed by KASAN quarantine. > + * was delayed by KASAN quarantine, or it was returned to KFENCE. > */ > static __always_inline > bool slab_free_hook(struct kmem_cache *s, void *x, bool init) > @@ -1790,6 +1790,9 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init) > __kcsan_check_access(x, s->object_size, > KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT); > > + if (kfence_free(kasan_reset_tag(x))) > + return false; > + > /* > * As memory initialization might be integrated into KASAN, > * kasan_slab_free and initialization memset's must be > @@ -1819,22 +1822,25 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s, > void *object; > void *next = *head; > void *old_tail = *tail; > + bool init; > > if (is_kfence_address(next)) { > slab_free_hook(s, next, false); > - return true; > + return false; > } > > /* Head and tail of the reconstructed freelist */ > *head = NULL; > *tail = NULL; > > + init = slab_want_init_on_free(s); > + > do { > object = next; > 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 (slab_free_hook(s, object, init)) { > /* Move object to the new freelist */ > set_freepointer(s, object, *head); > *head = object; > @@ -3619,9 +3625,6 @@ static void __slab_free(struct kmem_cache *s, struct slab *slab, > > stat(s, FREE_SLOWPATH); > > - if (kfence_free(head)) > - return; > - > if (IS_ENABLED(CONFIG_SLUB_TINY) || kmem_cache_debug(s)) { > free_to_partial_list(s, slab, head, tail, cnt, addr); > return; > @@ -3806,13 +3809,9 @@ static __fastpath_inline > void slab_free(struct kmem_cache *s, struct slab *slab, void *object, > unsigned long addr) > { > - bool init; > - > memcg_slab_free_hook(s, slab, &object, 1); > > - init = !is_kfence_address(object) && slab_want_init_on_free(s); > - > - if (likely(slab_free_hook(s, object, init))) > + if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) > do_slab_free(s, slab, object, object, 1, addr); > } > > > -- > 2.43.0 >