Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3499511pxb; Mon, 24 Jan 2022 10:50:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxPnKPGS257F1r/WCFz9dS76kCuPXI28e6K7houpXAxnWWLgm7I9tqqHZJIW7Vcu43U60+O X-Received: by 2002:a17:90a:2bcd:: with SMTP id n13mr3223669pje.155.1643050241411; Mon, 24 Jan 2022 10:50:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643050241; cv=none; d=google.com; s=arc-20160816; b=rTRMRNtdaObc7B0jP6upP/l1rH3rWQAnx1glcligi0sVTHY/FP+L7j5u4iZIDpvi03 51nL2NnLwT7zgtEEVCmOuASs0Y1JJy8hpPINW81ooyq7nu8oGTT+07n/4ths4GLtbLor dscvALEY/cARqDSBX58gdMawbeqqwj4YN4G91hXro5RpKlRz6e2OXoPb16HzGZ3Xudll hLTHoa2NCw3rLm0CrC7ZBDt6aT+oqYVdxoDfqHUPBu8JRKQgZKU1+To9xNOSxgv3LrGa ckoAwmNJnZPxluokaqSXBZvgEtHz0Bc9vvzRi9YFqRJPQH0qwOCn61r5ox638MtijiHe Z1mw== 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=Fe4pubot63aeTTcTlbwPRtQDcLp34onKse+sfKBfh3g=; b=eltjmalxmbhIMBthNVpz098hZ59MM0ZumuThKkC358cded4LGXWla6t6vXy/34u9bH 8yO0rd6W9NInjUCMW4PyzX2eIyFQVixOuaXKLIpTrTVG2YbUHzbhPCo5nVWzcncwIRGT LdDtLIvbR9P1FLWqKra5yRp9qxhLo0T+pH6r2YxDMG+sD9ORTVVBMfd2u9thygGYNiQt KeasjrOCeeCAC+WfWGjry4iv2Uw4AkVBlzarV6mvbaelV3YjpbuG3X6EZ1HkBKKwkws0 PkU9NWcLnhspLXmslIqtH02OK3fqK010Gk18YVam/56ojn79KvoMm/ihk3IZ2NvFBU3n JVmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hbC0p1Hg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y1si146149pjp.66.2022.01.24.10.50.29; Mon, 24 Jan 2022 10:50:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hbC0p1Hg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242116AbiAXIUi (ORCPT + 99 others); Mon, 24 Jan 2022 03:20:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242113AbiAXIUa (ORCPT ); Mon, 24 Jan 2022 03:20:30 -0500 Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2E36C06173D for ; Mon, 24 Jan 2022 00:20:29 -0800 (PST) Received: by mail-ot1-x334.google.com with SMTP id 10-20020a9d030a000000b0059f164f4a86so3756804otv.13 for ; Mon, 24 Jan 2022 00:20:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Fe4pubot63aeTTcTlbwPRtQDcLp34onKse+sfKBfh3g=; b=hbC0p1HgxcZOO8c4bWIlLFPNxwvCGs6Ub4zMCdalps8HkcEOtggdMiekbyEPh6BfgO 4uobjusmvFQj9LeEIgWb+tK8VZzkTQbdJg3hP/g+iEXPHcjv4rIUhbYxt+fJCq/5S7Vi oMuTLRyLsaqtbdEbkL0wBWIpQpgMJpvm+atEfWH+rpt50zQOIYrh/tU2xqe5t1nRbizp og7jrskWPDy65dLCj3eWex6wpg5E3BNSuplfpblwi6mf8pP16hdD8yqbb+833/0D7xew FQvVS34vXLTiIvNxMpFSFwhu5F9+m50cxLEn6saAs4oyrzq4K1VTO0PkpqRktNVzql6p et5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Fe4pubot63aeTTcTlbwPRtQDcLp34onKse+sfKBfh3g=; b=CPTNKeC5Fk4YqfRc3Rk2yFjzpVVKMo58iNaR91GIQOvBWv3gN78Q3YtzrJq/Co4LSA ICv/fisrLcvyjGDQCTBhyHKNhHC+4YO0pESsJxoniiu8ZWfiSkqxdstYwl6+/6nW5rpR 5veNQaM/9PWCI2FtwBYOmpXua/F/RWxL1TMJoqjadTDURWDUHA2OMx9imsUvgG41w2WK 2DCixhwHyyLPXmeCUobj9UT0siE04JRrT9kR1iAtqJFMENR2u+VXlfBHSMQ+koxH1W+f cBOUoRocqK+0GQYPp765ndXcgL3RpIQthE6i54KtrcrrYbXJAWmM+eYDznZedo8An5G7 t5+g== X-Gm-Message-State: AOAM533WCiYyXFakbhIL1AYn2FneI+MieDPfrSU7puw5s+fXDCEpUQVd wUMkzdLdm51tbhdQ4VSuYXNIWo+TXYMkXpzx1X5MCA== X-Received: by 2002:a9d:7053:: with SMTP id x19mr10625205otj.196.1643012428642; Mon, 24 Jan 2022 00:20:28 -0800 (PST) MIME-Version: 1.0 References: <20220124025205.329752-1-liupeng256@huawei.com> <20220124025205.329752-3-liupeng256@huawei.com> In-Reply-To: <20220124025205.329752-3-liupeng256@huawei.com> From: Marco Elver Date: Mon, 24 Jan 2022 09:20:17 +0100 Message-ID: Subject: Re: [PATCH RFC 2/3] kfence: Optimize branches prediction when sample interval is zero To: Peng Liu Cc: glider@google.com, dvyukov@google.com, corbet@lwn.net, sumit.semwal@linaro.org, christian.koenig@amd.com, akpm@linux-foundation.org, kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 24 Jan 2022 at 03:37, Peng Liu wrote: > > In order to release a uniform kernel with KFENCE, it is good to > compile it with CONFIG_KFENCE_SAMPLE_INTERVAL = 0. For a group of > produtions who don't want to use KFENCE, they can use kernel just > as original vesion without KFENCE. For KFENCE users, they can open > it by setting the kernel boot parameter kfence.sample_interval. > Hence, set KFENCE sample interval default to zero is convenient. > > The current KFENCE is supportted to adjust sample interval via the > kernel boot parameter. However, branches prediction in kfence_alloc > is not good for situation with CONFIG_KFENCE_SAMPLE_INTERVAL = 0 > and boot parameter kfence.sample_interval != 0, which is because > the current kfence_alloc is likely to return NULL when > CONFIG_KFENCE_SAMPLE_INTERVAL = 0. To optimize branches prediction > in this situation, kfence_enabled will check firstly. This patch doesn't make any sense. You're adding an unconditional LOAD to the fast path. And the choice of static_branch_unlikely() if CONFIG_KFENCE_SAMPLE_INTERVAL == 0 is very much deliberate, as it generates code that is preferable in the common case (KFENCE is disabled). Please see include/linux/jump_label.h:430. But even then, CPUs are very good at dealing with unconditional branches, so the difference really is a wash. But that new LOAD is not acceptable. Sorry, but Nack. > Signed-off-by: Peng Liu > --- > include/linux/kfence.h | 5 ++++- > mm/kfence/core.c | 2 +- > 2 files changed, 5 insertions(+), 2 deletions(-) > > diff --git a/include/linux/kfence.h b/include/linux/kfence.h > index aec4f6b247b5..bf91b76b87ee 100644 > --- a/include/linux/kfence.h > +++ b/include/linux/kfence.h > @@ -17,6 +17,7 @@ > #include > #include > > +extern bool kfence_enabled; > extern unsigned long kfence_num_objects; > /* > * We allocate an even number of pages, as it simplifies calculations to map > @@ -115,7 +116,9 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags); > */ > static __always_inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) > { > -#if defined(CONFIG_KFENCE_STATIC_KEYS) || CONFIG_KFENCE_SAMPLE_INTERVAL == 0 > + if (!kfence_enabled) > + return NULL; > +#if defined(CONFIG_KFENCE_STATIC_KEYS) > if (!static_branch_unlikely(&kfence_allocation_key)) > return NULL; > #else > diff --git a/mm/kfence/core.c b/mm/kfence/core.c > index 4655bcc0306e..2301923182b8 100644 > --- a/mm/kfence/core.c > +++ b/mm/kfence/core.c > @@ -48,7 +48,7 @@ > > /* === Data ================================================================= */ > > -static bool kfence_enabled __read_mostly; > +bool kfence_enabled __read_mostly; > > static unsigned long kfence_sample_interval __read_mostly = CONFIG_KFENCE_SAMPLE_INTERVAL; > > -- > 2.18.0.huawei.25 >