Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1024684rdb; Wed, 6 Dec 2023 06:47:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IE+4MJrOPGnIDKJ8MUjYbEnqWz8cN7EIycskxjEjkdc6bw3gWQ3B60PwEOHCFfbPgZkUDaO X-Received: by 2002:a17:902:dacc:b0:1d0:700b:3f75 with SMTP id q12-20020a170902dacc00b001d0700b3f75mr3781139plx.47.1701874041063; Wed, 06 Dec 2023 06:47:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701874041; cv=none; d=google.com; s=arc-20160816; b=eolJzZu0l6+eFxz/v/Udvl6LQTMoLLrQGZY7xcX+ElPqaJl5C6qMvhfVrPaAjsjcbA oRQs5oxS8/VUUBZIa8cUIFTZhygF1g/2rABKMTaK2qhT7mQWmBREbMVBDV1C5ZwUAnJb 3k7JlH1kS70yMxBt5d754QC1E0Cm8vIDZhwva/2+3nnI/ie0ke7/ve0wEqzGWWBxndIu tdjuU/goim7hEyc51/pORt95G94eFhzbRMqmDX7EBi4iF54GTs8bd9WUB8foJ/CYPNmG ywAnWYrtK38mMzUljxLXUBJkDx36JhOBKM9p1zLk1f6IvDjTdt+HAm9wGm1BTAM6sWOz CotQ== 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=RvuRYbFdpoGcm/YpWzxHjiZOvKmQglmv2U2Hz0xPJx8=; fh=go8Y/Z2zMKxw9GTvs/wrSRn4+U+QAqosGEOkKS4ggMI=; b=ywih3VGChpyCTTapYiXNGnKtEyJ5Tv17EH2tMMTyb32yrrbZ3BF6lYyXrFCtIDXCmZ r+IKNvWE6c/jCmto3ypvPa9VsoZ0EJUqQSOthHrqV6U0f5b0Ig4XGCcJcxce9Ls2GA2R 4TU+6zWHUHISN5N+0S7a3acfq2SOrkHpQM+Cm0CDdUMtUaWBQtZ0mo+SaN0WJHVnlwOO /6Wmebiv6L7RWQYNDV1qkxCDMTFEQEmIbiV6LtoZQOVN3+1EHWkqew2zcK3r9PpOrpAh EhK89KcS/9JRbUZgOfXHQXsH6sQZpwKGwCJa6B59xwm4hnfdvJV9Ut7U2365t2EEpzAt LOnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MdCruzts; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id x7-20020a1709027c0700b001d07c2a582dsi6910448pll.507.2023.12.06.06.47.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 06:47:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MdCruzts; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id 97C0C80AD105; Wed, 6 Dec 2023 06:46:37 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442434AbjLFOqO (ORCPT + 99 others); Wed, 6 Dec 2023 09:46:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379089AbjLFOp5 (ORCPT ); Wed, 6 Dec 2023 09:45:57 -0500 Received: from mail-vs1-xe29.google.com (mail-vs1-xe29.google.com [IPv6:2607:f8b0:4864:20::e29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A59541BC0 for ; Wed, 6 Dec 2023 06:45:39 -0800 (PST) Received: by mail-vs1-xe29.google.com with SMTP id ada2fe7eead31-4649d22b78aso597446137.0 for ; Wed, 06 Dec 2023 06:45:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701873938; x=1702478738; 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=RvuRYbFdpoGcm/YpWzxHjiZOvKmQglmv2U2Hz0xPJx8=; b=MdCruztsGfzor59nRnYv+CxH//8yiFYSNbReYqh+RLkqv7zXJoYfse68eHtdq6gowM MM3oBM5Ruio8ZComLRCZcU0TZ+cNnXcLBLiAG4Rjges1CodFB1iA6ne7TqzjA3QehlrV b+WjKyl32l4KtqzRuuVm/WABuxCZtq0vcHL2nHepXPXOEXNFz2rAp/fc/nPrW9PwgBKi EMYDx8Ox+XAvhSeiRz0kOOoI2OOhnx75HLXp4s3AtiFabOlriwkNNVOTHVBZsqN2e5fK 6sl8zmSbhRw7GMl0MD4zsyJzXOTLhE8QLQqr2v6or5685LM10NrX+Gxyno8CQ3bWacME p+vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701873938; x=1702478738; 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=RvuRYbFdpoGcm/YpWzxHjiZOvKmQglmv2U2Hz0xPJx8=; b=saMhYbVEoISwdzaoF4iIFIq1xHCEA91eAsYB8mk3kbg4Lm/wYKiaa7uykDOqO43tLk RQrsO142uOgOJNbHEMquZN5rBsgjaI72Px1yJ5KhGZlcIq9luEsfvOzjBVJDv/cEOn/C uHZMHw3xrt9cm8oJJOadf5odnm2WEGpBuHu+JYvr/K6nr5hJ9QiuYb7il5VQ20lHA48x psU6Yeg/8VKcnDAaveViHMQjxq0C99ddkDvc9g03ZAhRhYt7pu7bS7bfX4jhhjh6A671 73WTxMdjPBZ5OaAomQjDgEXHAdxwiqkMb+rweTJ3w9zYEZFM6mK7jR8aq0CUy3Yt05fO udrw== X-Gm-Message-State: AOJu0Yx2RugE/0AQ21uw5sYBud2ppk/OOn+eUOcAZ3y0JmG+eE97Ljan KZiW2pQCMlAe145DRhACtTm2fsnDcDelQwj/ivytrA== X-Received: by 2002:a05:6102:1901:b0:464:9f88:8310 with SMTP id jk1-20020a056102190100b004649f888310mr511241vsb.10.1701873938446; Wed, 06 Dec 2023 06:45:38 -0800 (PST) MIME-Version: 1.0 References: <20231204-slub-cleanup-hooks-v1-0-88b65f7cd9d5@suse.cz> <20231204-slub-cleanup-hooks-v1-4-88b65f7cd9d5@suse.cz> <44421a37-4343-46d0-9e5c-17c2cd038cf2@linux.dev> <79e29576-12a2-a423-92f3-d8a7bcd2f0ce@suse.cz> In-Reply-To: From: Marco Elver Date: Wed, 6 Dec 2023 15:44:59 +0100 Message-ID: Subject: Re: [PATCH 4/4] mm/slub: free KFENCE objects in slab_free_hook() To: Chengming Zhou , Andrey Konovalov Cc: Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Alexander Potapenko , Dmitry Vyukov , linux-mm@kvack.org, linux-kernel@vger.kernel.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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 06 Dec 2023 06:46:37 -0800 (PST) On Wed, 6 Dec 2023 at 14:02, Chengming Zhou wrote: > > On 2023/12/6 17:58, Vlastimil Babka wrote: > > On 12/5/23 14:27, Chengming Zhou wrote: > >> On 2023/12/5 03:34, 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. > >>> > >>> Tested-by: Marco Elver > >>> Signed-off-by: Vlastimil Babka > >>> --- > >>> mm/slub.c | 22 ++++++++++------------ > >>> 1 file changed, 10 insertions(+), 12 deletions(-) > >>> > >>> diff --git a/mm/slub.c b/mm/slub.c > >>> index ed2fa92e914c..e38c2b712f6c 100644 > >>> --- a/mm/slub.c > >>> +++ b/mm/slub.c > >>> @@ -2039,7 +2039,7 @@ static inline void memcg_slab_free_hook(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) > >>> @@ -2057,6 +2057,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))) > >> > >> I'm wondering if "kasan_reset_tag()" is needed here? > > > > I think so, because AFAICS the is_kfence_address() check in kfence_free() > > could be a false negative otherwise. In fact now I even question some of the > > Ok. > > > other is_kfence_address() checks in mm/slub.c, mainly > > build_detached_freelist() which starts from pointers coming directly from > > slab users. Insight from KASAN/KFENCE folks appreciated :) > > > I know very little about KASAN/KFENCE, looking forward to their insight. :) > > Just saw a check in __kasan_slab_alloc(): > > if (is_kfence_address(object)) > return (void *)object; > > So thought it seems that a kfence object would be skipped by KASAN. The is_kfence_address() implementation tolerates tagged addresses, i.e. if it receives a tagged non-kfence address, it will never return true. The KASAN_HW_TAGS patches and KFENCE patches were in development concurrently, and at the time there was some conflict resolution that happened when both were merged. The is_kfence_address(kasan_reset_tag(..)) initially came from [1] but was squashed into 2b8305260fb. [1] https://lore.kernel.org/all/9dc196006921b191d25d10f6e611316db7da2efc.1611946152.git.andreyknvl@google.com/ Andrey, do you recall what issue you encountered that needed kasan_reset_tag()?