Message ID | 20230803012608.3540081-1-samuel.holland@sifive.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp871828vqx; Wed, 2 Aug 2023 19:56:29 -0700 (PDT) X-Google-Smtp-Source: APBJJlGALIBER6aan6X7/w3ap9l9RQLvEffDBJEiy17e9ASChcjshkb4Crw0RZpuz0SyW840JABE X-Received: by 2002:a05:6a20:158a:b0:13d:7aa3:aa6c with SMTP id h10-20020a056a20158a00b0013d7aa3aa6cmr13821045pzj.0.1691031389578; Wed, 02 Aug 2023 19:56:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691031389; cv=none; d=google.com; s=arc-20160816; b=N2MYzhVDWo+EzN/nbo+NMmA0BtzVDAf02sktvAgztvMHuSBmoR5MQIhYfKpIcZVAkz ioFymXLhw0u6UG1XhAZAC/vu2ByEkaYtDE24IackSwo/ZHzUo1PovoxXEn7gerStnR9+ BgTVOx3BFWiVRNhxV0anZbitmK7px5LY8k3V6GA5AT9panxv1oaC7ER8+evJ6+urm0CX pqySEPYh2VNguIHE4Hwji4gqo5tIp/K8W71DVwFViE4jLhRRjp/ZLUiXkXncNdyJRwX+ yWLq/b4PEjtlfx1Yt1DMAhLzhYJ3j1BaDo4lbPEW/j0g1nWkj2cNBjW+vwx+jgmvPSwo l0pA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=KnkEmzZYZtLV9bJcf/lKsKflsIaYK5O+EUs/jmBBVS8=; fh=MGHrIQi8/eCfV697qc7ZVAjA60wD0aOsdfq5P7KW9t8=; b=KergC19kcJ4ldskfejokactvHc230kKICyTurePitH0ZV5z58GJxNCPf4OBaoio+Jx tBmuif7l4HVx+1AyzJom9YM3M5LW1OaDB8O8MZ4feqx1Lf3ih4A///pntgEPjZtWPmpj 6v6Jav00S2iFf4Y1xedvT9dcSnuRvCD71Em9Wq6gm38VJQ2Evp9DNAHtU3UrQwaVcFA/ Z35KBdl/i52QGuXhtzF6tmHqpoXcOPSSiKFE+EztwllXIFEeV1oKhNbdq7PWWAebbdef pXDWF5Uq9wbDJgNTVqhLNpbcXyRfQHt4LikQrh7amus5RAhJ/fUZ+8kh+1skNWZ3SPjs tAfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=mW1d1iN5; 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=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q10-20020a056a00084a00b00682b0b7ed43si10701458pfk.368.2023.08.02.19.56.16; Wed, 02 Aug 2023 19:56:29 -0700 (PDT) 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=@sifive.com header.s=google header.b=mW1d1iN5; 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=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231269AbjHCB0h (ORCPT <rfc822;cambridge8321@gmail.com> + 99 others); Wed, 2 Aug 2023 21:26:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230327AbjHCB0e (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 2 Aug 2023 21:26:34 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F92E3590 for <linux-kernel@vger.kernel.org>; Wed, 2 Aug 2023 18:26:11 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-686efa1804eso306334b3a.3 for <linux-kernel@vger.kernel.org>; Wed, 02 Aug 2023 18:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1691025970; x=1691630770; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=KnkEmzZYZtLV9bJcf/lKsKflsIaYK5O+EUs/jmBBVS8=; b=mW1d1iN57SrWe3nCbYedyyIjo5aVpEAFsdcvOI56OLPAO1wdvj8v71+OP/wjZcuJ+n nFtZYyqycgEH5gdLo7BKz2kb6v6/rM6bcQsLEo5JBupZ1kPGvS0pdKc5hN1p4IQSn0yb KPJc+3bTAi2pirNR+tZbOmjmq6abdFIWz7VmZpSAfQ45iS2LbsiDWAYM3OH4QeUFXYDi RqnAsx3J9XGQNOGuDzWCv7O8JK84QXDyoKdrcfUKDUiBdEzO0KKaefi7e41+DNe0Jd22 SGpoWATjtNQru/ZttY9eJjGiluLbHvQz/kPR0hJcS/zvGs8r+8i5hzLMDVYDz2+AryGj BxEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691025970; x=1691630770; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KnkEmzZYZtLV9bJcf/lKsKflsIaYK5O+EUs/jmBBVS8=; b=PJhzvOOveebpZwg3fk9Q6kOFM65ZhMLJCMd5XZ7dL0x+i0tYjyj5/znvLMajme0V+l pabHWOuxuXjRfiI/ORXvu4G+gCt1coG7Lk9axxUyQcipx6yBWI7Ok5VexwC3BKzhdmu1 cr1LhCtQCImpPpM4fTLiBbIojBtoA45JnChGxhjLsLYnoijkuCDLIerp+7TWJo7lxIKZ azKbg2oZoKHu+5EqaHZo8uvc4qOoXKYhPXLST6+tFCOIAWcg1q8RggOgJyMP/mPaBw6O PUwwM/P04o9rmghEgrhGk5jA6a6jg98XkMoKpXMJKnQXEljB+Rz0m/lYE2dDYJ2lfUnP brjw== X-Gm-Message-State: ABy/qLYttIK0hwcG94VYSAv9V03WXWBE/1t83Fq6GiLtyg0BYddfoHNn d0OEZi6MN0SHC648QMVpMbP1EQ== X-Received: by 2002:a05:6a20:4cf:b0:134:a478:6061 with SMTP id 15-20020a056a2004cf00b00134a4786061mr16915077pzd.26.1691025970144; Wed, 02 Aug 2023 18:26:10 -0700 (PDT) Received: from sw06.internal.sifive.com ([64.62.193.194]) by smtp.gmail.com with ESMTPSA id z1-20020a170903018100b001b6a27dff99sm13025734plg.159.2023.08.02.18.26.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 18:26:09 -0700 (PDT) From: Samuel Holland <samuel.holland@sifive.com> To: Palmer Dabbelt <palmer@dabbelt.com>, Paul Walmsley <paul.walmsley@sifive.com>, linux-riscv@lists.infradead.org Cc: linux-kernel@vger.kernel.org, Samuel Holland <samuel.holland@sifive.com>, Palmer Dabbelt <palmer@rivosinc.com> Subject: [PATCH] riscv: Fix CPU feature detection with SMP disabled Date: Wed, 2 Aug 2023 18:26:06 -0700 Message-ID: <20230803012608.3540081-1-samuel.holland@sifive.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773174930208196545 X-GMAIL-MSGID: 1773174930208196545 |
Series |
riscv: Fix CPU feature detection with SMP disabled
|
|
Commit Message
Samuel Holland
Aug. 3, 2023, 1:26 a.m. UTC
commit 914d6f44fc50 ("RISC-V: only iterate over possible CPUs in ISA
string parser") changed riscv_fill_hwcap() from iterating over CPU DT
nodes to iterating over logical CPU IDs. Since this function runs long
before cpu_dev_init() creates CPU devices, it hits the fallback path in
of_cpu_device_node_get(), which itself iterates over the DT nodes,
searching for a node with the requested CPU ID. (Incidentally, this
makes riscv_fill_hwcap() now take quadratic time.)
riscv_fill_hwcap() passes a logical CPU ID to of_cpu_device_node_get(),
which uses the arch_match_cpu_phys_id() hook to translate the logical ID
to a physical ID as found in the DT.
arch_match_cpu_phys_id() has a generic weak definition, and RISC-V
provides a strong definition using cpuid_to_hartid_map(). However, the
RISC-V specific implementation is located in arch/riscv/kernel/smp.c,
and that file is only compiled when SMP is enabled.
As a result, when SMP is disabled, the generic definition is used, and
riscv_isa gets initialized based on the ISA string of hart 0, not the
boot hart. On FU740, this means has_fpu() returns false, and userspace
crashes when trying to use floating-point instructions.
Fix this by moving arch_match_cpu_phys_id() to a file which is always
compiled.
Fixes: 70114560b285 ("RISC-V: Add RISC-V specific arch_match_cpu_phys_id")
Fixes: 914d6f44fc50 ("RISC-V: only iterate over possible CPUs in ISA string parser")
Reported-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
---
arch/riscv/kernel/cpu.c | 5 +++++
arch/riscv/kernel/smp.c | 5 -----
2 files changed, 5 insertions(+), 5 deletions(-)
Comments
On Wed, Aug 02, 2023 at 06:26:06PM -0700, Samuel Holland wrote: > commit 914d6f44fc50 ("RISC-V: only iterate over possible CPUs in ISA > string parser") changed riscv_fill_hwcap() from iterating over CPU DT > nodes to iterating over logical CPU IDs. Since this function runs long > before cpu_dev_init() creates CPU devices, it hits the fallback path in > of_cpu_device_node_get(), which itself iterates over the DT nodes, > searching for a node with the requested CPU ID. > (Incidentally, this > makes riscv_fill_hwcap() now take quadratic time.) Ouch, that I did not realise. Should we revert that portion of the changes? Starting to sound like we should.. > riscv_fill_hwcap() passes a logical CPU ID to of_cpu_device_node_get(), > which uses the arch_match_cpu_phys_id() hook to translate the logical ID > to a physical ID as found in the DT. > > arch_match_cpu_phys_id() has a generic weak definition, and RISC-V > provides a strong definition using cpuid_to_hartid_map(). However, the > RISC-V specific implementation is located in arch/riscv/kernel/smp.c, > and that file is only compiled when SMP is enabled. > > As a result, when SMP is disabled, the generic definition is used, and > riscv_isa gets initialized based on the ISA string of hart 0, not the > boot hart. On FU740, this means has_fpu() returns false, and userspace > crashes when trying to use floating-point instructions. > > Fix this by moving arch_match_cpu_phys_id() to a file which is always > compiled. > > Fixes: 70114560b285 ("RISC-V: Add RISC-V specific arch_match_cpu_phys_id") > Fixes: 914d6f44fc50 ("RISC-V: only iterate over possible CPUs in ISA string parser") > Reported-by: Palmer Dabbelt <palmer@rivosinc.com> > Signed-off-by: Samuel Holland <samuel.holland@sifive.com> Reviewed-by: Conor Dooley <conor.dooley@microchip.com> Thanks for fixing this Samuel.
Hello: This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <palmer@rivosinc.com>: On Wed, 2 Aug 2023 18:26:06 -0700 you wrote: > commit 914d6f44fc50 ("RISC-V: only iterate over possible CPUs in ISA > string parser") changed riscv_fill_hwcap() from iterating over CPU DT > nodes to iterating over logical CPU IDs. Since this function runs long > before cpu_dev_init() creates CPU devices, it hits the fallback path in > of_cpu_device_node_get(), which itself iterates over the DT nodes, > searching for a node with the requested CPU ID. (Incidentally, this > makes riscv_fill_hwcap() now take quadratic time.) > > [...] Here is the summary with links: - riscv: Fix CPU feature detection with SMP disabled https://git.kernel.org/riscv/c/6514f81e1bd5 You are awesome, thank you!
On Wed, 02 Aug 2023 18:26:06 -0700, Samuel Holland wrote: > commit 914d6f44fc50 ("RISC-V: only iterate over possible CPUs in ISA > string parser") changed riscv_fill_hwcap() from iterating over CPU DT > nodes to iterating over logical CPU IDs. Since this function runs long > before cpu_dev_init() creates CPU devices, it hits the fallback path in > of_cpu_device_node_get(), which itself iterates over the DT nodes, > searching for a node with the requested CPU ID. (Incidentally, this > makes riscv_fill_hwcap() now take quadratic time.) > > [...] Applied, thanks! [1/1] riscv: Fix CPU feature detection with SMP disabled https://git.kernel.org/palmer/c/6514f81e1bd5 Best regards,
diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c index a2fc952318e9..35b854cf078e 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv/kernel/cpu.c @@ -17,6 +17,11 @@ #include <asm/smp.h> #include <asm/pgtable.h> +bool arch_match_cpu_phys_id(int cpu, u64 phys_id) +{ + return phys_id == cpuid_to_hartid_map(cpu); +} + /* * Returns the hart ID of the given device tree node, or -ENODEV if the node * isn't an enabled and valid RISC-V hart node. diff --git a/arch/riscv/kernel/smp.c b/arch/riscv/kernel/smp.c index 85bbce0f758c..40420afbb1a0 100644 --- a/arch/riscv/kernel/smp.c +++ b/arch/riscv/kernel/smp.c @@ -61,11 +61,6 @@ int riscv_hartid_to_cpuid(unsigned long hartid) return -ENOENT; } -bool arch_match_cpu_phys_id(int cpu, u64 phys_id) -{ - return phys_id == cpuid_to_hartid_map(cpu); -} - static void ipi_stop(void) { set_cpu_online(smp_processor_id(), false);