Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3244401lfo; Sun, 22 May 2022 23:41:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzt7z2om9CRDUQTAbJtQBCWv6aD6Q56ABvyPa8rVKlkehgqxHke1rQ6dCTELD97DJNZAU1X X-Received: by 2002:a17:90b:1b41:b0:1e0:17f:d17 with SMTP id nv1-20020a17090b1b4100b001e0017f0d17mr15364428pjb.85.1653288100876; Sun, 22 May 2022 23:41:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653288100; cv=none; d=google.com; s=arc-20160816; b=09yChLPPR61aPQOP6IHXzXRwKOJIGWOYQpDAboP8yVRSrjUQXJe2WV5o+67vvBDD/a BgH23ibAoVoQ/YkCqhJjrBXD2lHVIWV49WAHmI/+tfV3mkCNEVx5EkYo8RGLsRKI9UJX ETWZHftJm96dkZmdCW7VpQ8m24c6wY0KT9oX3WJonETdJBrOTYAgh4pvOctu3Rn327EJ gWpc6Xnn41p4q1ONVPgvWLWchqrXqYlVX7srDfMQEorDCE8bR0fw5xShfQQUxcNiDPQv cIYHyEDd++9r6pkFqN9YGNqrWC1f4y7HB3mseS8VcmPFzgC9xDhjSEq6V6+l5Id+widt JAvA== 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=rukv6zbZ46XrUko/c47Z0GRGNmLsrnaZg/GHBjEigTA=; b=WRJxjl/O2HuOjgX6D3qzZwzMqIdV66u/WirV+Qp354Z+b4gqwFNClvBqeafAIOZebL rVCoboI4gaCykGjGXCu3MDmjP7ycmlTtnv+4q+iQ0yqK7hklG9oBjL/BwG1PscDYnQ4M Ef3E9L/PkXGxuFv60nlH4/Z8BeOZDlVRXFyqFLx/gAZqT8zT02PM3vB2lkzi0eMIrDtS EjNFAD9xtcXq14Y52YJQlGX3wzjFSHyxuiOeJ90hVVautZ7RW6unFEAGj6AZ71n/3N5i AWwpgwpXTMiYf9uIv8PQLfSruwQEzyKZmnlikcmLFDLKQZG8Sk1UuEEvBAvBQxOOaePj RZlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="q9C/FMqI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id f2-20020a17090ace0200b001c7c69ce93esi10689973pju.13.2022.05.22.23.41.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 23:41:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="q9C/FMqI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6A152DF76; Sun, 22 May 2022 23:14:50 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348205AbiEVPoe (ORCPT + 99 others); Sun, 22 May 2022 11:44:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348145AbiEVPob (ORCPT ); Sun, 22 May 2022 11:44:31 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 241723668E for ; Sun, 22 May 2022 08:44:30 -0700 (PDT) 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 B9F89B80AC0 for ; Sun, 22 May 2022 15:44:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83A75C385AA; Sun, 22 May 2022 15:44:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653234267; bh=issCkMCtvgjOFJMYrAwxjLaKfTsyPKtkpaH6yaHo3nk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q9C/FMqIswh924to3Qp7jaIljZnpLaaKeYqsqusO7vhazCSSbw37NUGB8LKP4PPQX ZK8ILwCXjCulprCDNzK151akWkrgxu/EXjiDxR3WbHklzh95mZ9eRilFL4hP5Qi/rf +XUd0XcDaKl851xKXpDXTcY6vOpuJ8+M1YH7+87AgHfrK7TUoBKVp1svjEVBhrdm4B BdSDgg03pB8l/9bKYCj+dhYrYGLtc+uGi8YauAj2hJeiUphJxcqPemqyOSJ43yo1Er hEoUb/gJCT3CPX1hT6R8XwmXClC1M6Ccvz6KpkFtwzN7NfnTQdXVc6tnFdbZpti+FD sJeu+F5e8pPpQ== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Anup Patel Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] riscv: introduce unified static key mechanism for ISA extensions Date: Sun, 22 May 2022 23:35:42 +0800 Message-Id: <20220522153543.2656-2-jszhang@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220522153543.2656-1-jszhang@kernel.org> References: <20220522153543.2656-1-jszhang@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Currently, riscv has several extensions which may not be supported on all riscv platforms, for example, FPU and so on. To support unified kernel Image style, we need to check whether the feature is supported or not. If the check sits at hot code path, then performance will be impacted a lot. static key can be used to solve the issue. In the past, FPU support has been converted to use static key mechanism. I believe we will have similar cases in the future. This patch tries to add an unified mechanism to use static keys for some ISA extensions by implementing an array of default-false static keys and enabling them when detected. Signed-off-by: Jisheng Zhang --- arch/riscv/include/asm/hwcap.h | 25 +++++++++++++++++++++++++ arch/riscv/kernel/cpufeature.c | 7 +++++++ 2 files changed, 32 insertions(+) diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 0734e42f74f2..d3e113fe7366 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -12,6 +12,7 @@ #include #ifndef __ASSEMBLY__ +#include /* * This yields a mask that user programs can use to figure out what * instruction set this cpu supports. @@ -55,6 +56,16 @@ enum riscv_isa_ext_id { RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX, }; +/* + * This enum represents the logical ID for each RISC-V ISA extension static + * keys. We can use static key to optimize code path if some ISA extensions + * are available. + */ +enum riscv_isa_ext_key { + RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */ + RISCV_ISA_EXT_KEY_MAX, +}; + struct riscv_isa_ext_data { /* Name of the extension displayed to userspace via /proc/cpuinfo */ char uprop[RISCV_ISA_EXT_NAME_LEN_MAX]; @@ -62,6 +73,20 @@ struct riscv_isa_ext_data { unsigned int isa_ext_id; }; +extern struct static_key_false riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_MAX]; + +static __always_inline int riscv_isa_ext2key(int num) +{ + switch (num) { + case RISCV_ISA_EXT_f: + return RISCV_ISA_EXT_KEY_FPU; + case RISCV_ISA_EXT_d: + return RISCV_ISA_EXT_KEY_FPU; + default: + return -EINVAL; + } +} + unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap); #define riscv_isa_extension_mask(ext) BIT_MASK(RISCV_ISA_EXT_##ext) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 1b2d42d7f589..89f886b35357 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -24,6 +24,8 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; #ifdef CONFIG_FPU __ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_fpu); #endif +__ro_after_init DEFINE_STATIC_KEY_ARRAY_FALSE(riscv_isa_ext_keys, RISCV_ISA_EXT_KEY_MAX); +EXPORT_SYMBOL(riscv_isa_ext_keys); /** * riscv_isa_extension_base() - Get base extension word @@ -232,6 +234,11 @@ void __init riscv_fill_hwcap(void) print_str[j++] = (char)('a' + i); pr_info("riscv: ELF capabilities %s\n", print_str); + for_each_set_bit(i, riscv_isa, RISCV_ISA_EXT_MAX) { + j = riscv_isa_ext2key(i); + if (j >= 0) + static_branch_enable(&riscv_isa_ext_keys[j]); + } #ifdef CONFIG_FPU if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)) static_branch_enable(&cpu_hwcap_fpu); -- 2.34.1