Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3358729pxb; Mon, 24 Jan 2022 08:01:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJw/j+E+/Bx1jGKNGglmECyx2erTdq3a/yze6ya80+MDbye8DGq6A/UKnB2cY2VMMuWf31MX X-Received: by 2002:a63:be45:: with SMTP id g5mr12366412pgo.309.1643040096942; Mon, 24 Jan 2022 08:01:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643040096; cv=none; d=google.com; s=arc-20160816; b=JWUv5NXU1PW+H+SX+Yu8F7UWN+cjS7498NINX+n55gsRMYqkHjR7YyIp4urkb5UZtn a7EyuaiJ8fY0sU9l3bcS/ywhJABOgbsKbeZTqJG6TSw8kIVKNjyTfI1CJvRs1QO+NLfa NLIdhE0fYIw2RsJ6WCcXMnOcJI1Ti3m6ucYg/yyWh2AURRj8S3mwaoU+cT0XxZDmhjoH m/njF48/OJhQ+LLu+DSGoUr4l2T4/ni15xcF+hTvZ8zfnMqNDUxZtsre8f8PlQ6j+v9E paAdARflkiS/SwxLvHYCH7tz+lMrH+7bzuJG04GTmwtd05nkLvASxJYqFqJyuCB0V3t6 iVYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=3g+MPf0IwUa/f2agonMpS0G5IPjK9URyioB0BTuRp2k=; b=YxMQE2lNdMC01zejES587kgxCYAvDJ5aRX1Q0ON7fGwmG2BeHSw+4zCOtD8XAz5JkY YnYHVhguoEoGAAfmmLtxC1ns8KzmmxxlM/+EA4gmrMF4rzml3LrmGVyz/lLNWV1XDeQ8 /+qbFdNauztyUMUHzLF6I8M1Qa3eg+iIXS3730xw5SEesjSpYIf8j9+H0K6fTnBqiDCw 2WOUF/NYq/lCRJwDmJUwjWAXfrmfZ7cwIJLdHpJ0E3gEms2CsP+BxNokZ6wE2aboLPLu wK19PffgTU177KBEEaEB7w2ZXzo1Xy0yx8cOJSjHWmQIJ9Ob7HZZZfBk1VWxWp75BieC R3iw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x9si14796224pgu.748.2022.01.24.08.01.22; Mon, 24 Jan 2022 08:01:36 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240900AbiAXChx (ORCPT + 99 others); Sun, 23 Jan 2022 21:37:53 -0500 Received: from szxga03-in.huawei.com ([45.249.212.189]:31179 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240814AbiAXChr (ORCPT ); Sun, 23 Jan 2022 21:37:47 -0500 Received: from kwepemi500013.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4JhvG070gtz8wTb; Mon, 24 Jan 2022 10:34:48 +0800 (CST) Received: from kwepemm600017.china.huawei.com (7.193.23.234) by kwepemi500013.china.huawei.com (7.221.188.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Mon, 24 Jan 2022 10:37:43 +0800 Received: from localhost.localdomain (10.175.112.125) by kwepemm600017.china.huawei.com (7.193.23.234) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Mon, 24 Jan 2022 10:37:42 +0800 From: Peng Liu To: , , , , , , CC: , , , , , Subject: [PATCH RFC 2/3] kfence: Optimize branches prediction when sample interval is zero Date: Mon, 24 Jan 2022 02:52:04 +0000 Message-ID: <20220124025205.329752-3-liupeng256@huawei.com> X-Mailer: git-send-email 2.18.0.huawei.25 In-Reply-To: <20220124025205.329752-1-liupeng256@huawei.com> References: <20220124025205.329752-1-liupeng256@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm600017.china.huawei.com (7.193.23.234) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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