Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4897509rwb; Sun, 4 Dec 2022 10:06:35 -0800 (PST) X-Google-Smtp-Source: AA0mqf4j7ETdDlEkcQwYIBLQYNNBuuTcK4XEpfy6+akRY9b/ay1FcII4yYP0Bgt/DfR9jHAe4I6T X-Received: by 2002:a17:906:160a:b0:7c0:688a:6251 with SMTP id m10-20020a170906160a00b007c0688a6251mr26892344ejd.274.1670177195584; Sun, 04 Dec 2022 10:06:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670177195; cv=none; d=google.com; s=arc-20160816; b=wuKIPkWGQF2ENQ07wIQ1TQDGREmIkHhH+Yj75N8JdZ7eWzW8ulS7J3PbEFtSykA6gy wzjdJo9EKqgATUBSKzXXQ9kj2KnT+TR1siY7kdr4ksLir0rBwMhIgOKyr90xXfTY7PV5 3zwq0ZZc9Us43SA9OoACq1viNIhIL86zWVraaxlmWNTWDUJVgIoc176Q7rmU3ov8plSQ OkB8/YetJjpQ0zF3Za0zdutqY+HExiVv83Y80BJSGTyziwwP1fSec7+46qxYPYBgcPdv ulxwsqehWcY8V6StHzklutSfbzfnRT7AP4LPLQrQbZTQL/cMMe9W9cE0NRLY90M3SuQL zWDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6ZQP2+hN2ueV1+SpeIsJoHPFJjepVE3kiQ82a1KkxYM=; b=unbpfQY0tsqocPexLaT8JHpSAPgOxWsl0ARfjCM7zLI+OI9TuGHv2YigU5OwdpSq6e FkiNOeh0h7SwykVLZ7PHf1lvx49Gl3SS3+v9eS/hAn7axlBlaE51pFhqlvMV8snzpvN0 baceYYyBPzjBroApu0UY7lNaZJXgdkTg6bTbo6q9k5HE6MgGyWBqzXbuXyaytOMa7Aej Lqkxr6Fg8l52a98jiv1/STuifySN1FDvQLoJy1S8mqrsF1MQD32WEYrooxXKb03KKeQB LnBtdTNnOT5HhYpenxHdxjLXSYiiR8/zu+dzvbaheXizG2oHeOmul9FY/4RTR6v6w6r8 JSJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DaGvlKjd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m9-20020aa7c2c9000000b0046ad8e026e1si9976537edp.69.2022.12.04.10.06.15; Sun, 04 Dec 2022 10:06:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DaGvlKjd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230377AbiLDR5F (ORCPT + 82 others); Sun, 4 Dec 2022 12:57:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230310AbiLDR4r (ORCPT ); Sun, 4 Dec 2022 12:56:47 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6929B140D2; Sun, 4 Dec 2022 09:56:46 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0571460EE2; Sun, 4 Dec 2022 17:56:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19E69C433D6; Sun, 4 Dec 2022 17:56:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1670176605; bh=tZ397MWMJtxGYEh8FaWqZrQtcINmca1TWFedDoKCtqA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DaGvlKjdPp6aSmzQx42izCS1qJh9dBvP8j42q+NUfuT1VeDJGTOjX8P2mM3ZFUvht 9nPczVFJVgLcHia5Zk0wPhISGZJP5cMOqIHhPGd7DN6Dv45/IJ/T7M3KiPKSQTt80T OKWa02S/6WxVjk4BKcsEYNbQL9nrmK0HKqmTxg3bLFSj6wAgavGBr1HbulvTjdRom3 TcwvFZ+vGtKcBhGtnQRJJ5Ie3MolYiBxfJYriTCa0XJfgGqxCHCswsyXO5CtlSHPYd qL+Y8s/WeCJ0wjtr/jahs8VmMXN1NF8HfwpW+kdFCDZl068/GgWs1kh705nj/6VBKu QTKRVp1VzZF0A== From: Jisheng Zhang To: Palmer Dabbelt , Paul Walmsley , Albert Ou , Anup Patel , Atish Patra , Heiko Stuebner , Andrew Jones Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Subject: [PATCH v2 06/13] riscv: introduce riscv_has_extension_[un]likely() Date: Mon, 5 Dec 2022 01:46:25 +0800 Message-Id: <20221204174632.3677-7-jszhang@kernel.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221204174632.3677-1-jszhang@kernel.org> References: <20221204174632.3677-1-jszhang@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Generally, riscv ISA extensions are fixed for any specific hardware platform, that's to say, the hart features won't change any more after booting, this chacteristic make it straightforward to use static branch to check one specific ISA extension is supported or not to optimize performance. However, some ISA extensions such as SVPBMT and ZICBOM are handled via. the alternative sequences. Basically, for ease of maintenance, we prefer to use static branches in C code, but recently, Samuel found that the static branch usage in cpu_relax() breaks building with CONFIG_CC_OPTIMIZE_FOR_SIZE[1]. As Samuel pointed out, "Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table." Samuel's findings pointed out one of a few downsides of static branches usage in C code to handle ISA extensions detected at boot time: static branch's metadata in the __jump_table section, which is not discarded after ISA extensions are finalized, wastes some space. I want to try to solve the issue for all possible dynamic handling of ISA extensions at boot time. Inspired by Mark[2], this patch introduces riscv_has_extension_*() helpers, which work like static branches but are patched using alternatives, thus the metadata can be freed after patching. [1]https://lore.kernel.org/linux-riscv/20220922060958.44203-1-samuel@sholland.org/ [2]https://lore.kernel.org/linux-arm-kernel/20220912162210.3626215-8-mark.rutland@arm.com/ Signed-off-by: Jisheng Zhang Reviewed-by: Andrew Jones --- arch/riscv/include/asm/hwcap.h | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 996884986fea..e2d3f6df7701 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -8,6 +8,7 @@ #ifndef _ASM_RISCV_HWCAP_H #define _ASM_RISCV_HWCAP_H +#include #include #include #include @@ -96,6 +97,42 @@ static __always_inline int riscv_isa_ext2key(int num) } } +static __always_inline bool +riscv_has_extension_likely(const unsigned long ext) +{ + compiletime_assert(ext < RISCV_ISA_EXT_MAX, + "ext must be < RISCV_ISA_EXT_MAX"); + + asm_volatile_goto( + ALTERNATIVE("j %l[l_no]", "nop", 0, %[ext], 1) + : + : [ext] "i" (ext) + : + : l_no); + + return true; +l_no: + return false; +} + +static __always_inline bool +riscv_has_extension_unlikely(const unsigned long ext) +{ + compiletime_assert(ext < RISCV_ISA_EXT_MAX, + "ext must be < RISCV_ISA_EXT_MAX"); + + asm_volatile_goto( + ALTERNATIVE("nop", "j %l[l_yes]", 0, %[ext], 1) + : + : [ext] "i" (ext) + : + : l_yes); + + return false; +l_yes: + return true; +} + unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap); #define riscv_isa_extension_mask(ext) BIT_MASK(RISCV_ISA_EXT_##ext) -- 2.37.2