Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2052319rwb; Sun, 15 Jan 2023 08:26:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXsYby2tbgECXo92BW/PDaOme2TJjh2r+uZYqmiVVZBxjCaEyTJD4UhIAB1yXmQXczLhErqS X-Received: by 2002:a17:906:19d5:b0:86e:147c:9bdc with SMTP id h21-20020a17090619d500b0086e147c9bdcmr5133464ejd.40.1673799969515; Sun, 15 Jan 2023 08:26:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673799969; cv=none; d=google.com; s=arc-20160816; b=dnZkaH6bAL7JBh1SWrEshkuVvshSCvXIXGrFKG/jkP1Fz5wTTnR8Ftj7v/oCUOgWEq tAQsQIykj0XGvf3j72IP6NYSk8GaRo4eaj0gr4E87iG9Zxa0RHBv1kuB/VLenSEfq/Yi PZM3EnRKaBH/10p5lSCVxOmNFLqSk7jM8qp1+xFhmyxX2dmatJ6h0aLn3dmIoeyDYznR BDDKNvj2D+B2d0aMNCUR2c/vHIpV0E4bj9kytm5xRHqKqWW8v6eKPu3/XIa7dtCG19Yi JAo1q4tcCZMbkSa066ZsGvkthljwhRHhQxaOeeo7VYrmG0gHxc7EoRe4SWy1pq6tuLvZ yvAw== 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=dNd1WjEBNJtTlazVQTwjFevNqNdAe6AtV5jK4RMe98g=; b=b+DZjYuKVJqwuQQ07OnZ3vAYoA46cOf7z77hYGAcIsyUSCLR0VRPaT8Pbx0VJy1wew ng/TLhQaVA1Lbm5A3bcE1GuDHzD1VWQG00Se6MEf+OTvxq2sXTGoPls2DKqa/wC/NurM bZIZ01dx1DRqgugBhaLK77FmVlfHDSEw7TNlealLElu4P5Bv5Fav8ZaeriuwDCsfkzHf FT3sbp9JyObUwT/vAW4XX1PdNwgHf9/7FFWWpec7lRsPHJUlfCl5pFm44jwEhXdqg2ho e82N8YBDLoeaJmXkrJvI/It1ZSFKOs5qfhwkQlQoIA5pFtILYF6SEByCNDLTvA9feECS PSNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DdrgAsgw; 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 du15-20020a17090772cf00b00870c907ed43si112924ejc.602.2023.01.15.08.25.57; Sun, 15 Jan 2023 08:26:09 -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=DdrgAsgw; 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 S231626AbjAOQCI (ORCPT + 52 others); Sun, 15 Jan 2023 11:02:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231520AbjAOQAw (ORCPT ); Sun, 15 Jan 2023 11:00:52 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D2F3113DF; Sun, 15 Jan 2023 08:00:33 -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 ams.source.kernel.org (Postfix) with ESMTPS id 1CC51B80B8A; Sun, 15 Jan 2023 16:00:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F974C433F0; Sun, 15 Jan 2023 16:00:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1673798430; bh=mINO93PYoIl2Le02IWiuk8J2800U31qq9j/6NT8h4Nc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DdrgAsgwrTvTsV6VHjq5HTcb1PpoZm4hZ2OiXa0qNIfHZpNilj3/svAWkCIIQwZfF sMNXFzpEUcY7KaCZaBVOrsK/SjzGUbHbUWEokDTP1Z+QHPgetuKInyabh5gfOx7N4L LOV6TCGxm8k8bmSfjuW7oFyx6AJtx2Hay9OgA2dBbtmSegYN1LQ+Dd4ZcC/G3aiqkd Tfr/qdVaNM4cC5nzilR3UMo6sGeFDtN5OjFab9I8gOsKsT2WSjZDgyi/gml9aKC0Yr B0k1S961lgZL+X1DvBxw5/yGW22vZC1IWYrsuggZ80WnDAHrg6/653v3f6U2gmLKSq GMdVdPE2bainA== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , Heiko Stuebner , Conor Dooley , Andrew Jones Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Subject: [PATCH v4 06/13] riscv: introduce riscv_has_extension_[un]likely() Date: Sun, 15 Jan 2023 23:49:46 +0800 Message-Id: <20230115154953.831-7-jszhang@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230115154953.831-1-jszhang@kernel.org> References: <20230115154953.831-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, so a hart's features won't change after booting. This chacteristic makes it straightforward to use a static branch to check if a 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. Link: https://lore.kernel.org/linux-riscv/20220922060958.44203-1-samuel@sholland.org/ [1] Link: https://lore.kernel.org/linux-arm-kernel/20220912162210.3626215-8-mark.rutland@arm.com/ [2] 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 09a7767723f6..1767a9ce1a04 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 @@ -97,6 +98,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.38.1