From mboxrd@z Thu Jan 1 00:00:00 1970 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 6F6E1C433F5 for ; Mon, 24 Jan 2022 08:20:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B02A86B0083; Mon, 24 Jan 2022 03:20:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AB20F6B0085; Mon, 24 Jan 2022 03:20:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A1A96B0087; Mon, 24 Jan 2022 03:20:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0225.hostedemail.com [216.40.44.225]) by kanga.kvack.org (Postfix) with ESMTP id 8C0506B0083 for ; Mon, 24 Jan 2022 03:20:30 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4027091E54 for ; Mon, 24 Jan 2022 08:20:30 +0000 (UTC) X-FDA: 79064483820.24.2B37D25 Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) by imf23.hostedemail.com (Postfix) with ESMTP id E0654140036 for ; Mon, 24 Jan 2022 08:20:29 +0000 (UTC) Received: by mail-ot1-f51.google.com with SMTP id y11-20020a0568302a0b00b0059a54d66106so21263312otu.0 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=usCFeVZ0gLTHyfWJpJI7fVfzsswgx/CDMsahC6ql78SwbMSID9rejK1BEfVAX3STjv v4YUk5RayM49vG/fC4Qz3QDqG6ShmMAYQrMt2/GQUhjLs7QkYdDQ6AuJgvLs9JYVgxTb 7P9nlt6tM74rt3fSplfPfVor0on5deg6oUH4fbI/VOwrU98eV83WF5qO7laN+B7867+w 2K8jLIIhtC66qdJc36lbFztq6sZ1cLuAoD0aJJn2+oAQ0DP1lzo81a4Y8A2bGoKkRN+r UMqofDTkkJmi9eS/m7J6MwkbvdQOtudpvWzfCOlds6t2VAszyBgvUkznT21mndFwQTA8 umjg== X-Gm-Message-State: AOAM532jJdvv1oLEM3DkW/+Pd55NC4NSRWhlaiJ+8Uw3N8LOT02k2J3I ywYo7irgpjKR7WwqFW7hDRodg4CHshAXicDnZvEMjg== X-Google-Smtp-Source: ABdhPJwOSQbPzFSjnMIC0k8eXN8NZWLSv40CjBpajvpNz8+4iCNlxNVEC0V+MQKJlFwrUD/QSe/IcXsddnFctH+E070= 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" X-Stat-Signature: 5s3zr87h9cii6cd66qx6za6c7zhdspqu Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=hbC0p1Hg; spf=pass (imf23.hostedemail.com: domain of elver@google.com designates 209.85.210.51 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E0654140036 X-HE-Tag: 1643012429-610800 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: 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 >