[PATCH v7 2/6] riscv: accept the atomic extensions this kernel was built with
From: Vladimir Kondratiev
Date: Wed Nov 18 2026 - 11:25:24 EST
riscv_early_of_processor_hartid() rejects a hart unless its
riscv,isa-extensions property contains "a". A hart that lists "zaamo"
and "zalrsc" instead of "a" is rejected, and so is one that lists only
"zalrsc", even when the kernel was built to use only LR/SC.
Accept "a" as before. Without it, require "zalrsc", which every kernel
uses for cmpxchg, and "zaamo" when RISCV_ISA_ZAAMO is enabled. The
default build therefore needs "a", or "zaamo" together with "zalrsc";
a kernel built without Zaamo needs only "zalrsc".
The legacy "riscv,isa" fallback keeps its rv{32,64}ima check.
Once such a hart can boot, hwprobe must stop telling userspace that
the base behaviour is IMA regardless of the hardware: report
RISCV_HWPROBE_BASE_BEHAVIOR_IMA only when every selected hart has A.
A hart with Zalrsc alone still shows what it has through
RISCV_HWPROBE_EXT_ZALRSC in RISCV_HWPROBE_KEY_IMA_EXT_0.
Signed-off-by: Vladimir Kondratiev <vladimir.kondratiev@xxxxxxxxxxxx>
---
Documentation/arch/riscv/hwprobe.rst | 4 +++-
arch/riscv/kernel/cpu.c | 23 ++++++++++++++++++++---
arch/riscv/kernel/sys_hwprobe.c | 23 ++++++++++++++++++-----
3 files changed, 41 insertions(+), 9 deletions(-)
diff --git a/Documentation/arch/riscv/hwprobe.rst b/Documentation/arch/riscv/hwprobe.rst
index bb1e0cbab36f..63d7ef028bf8 100644
--- a/Documentation/arch/riscv/hwprobe.rst
+++ b/Documentation/arch/riscv/hwprobe.rst
@@ -61,7 +61,9 @@ The following keys are defined:
* :c:macro:`RISCV_HWPROBE_BASE_BEHAVIOR_IMA`: Support for rv32ima or
rv64ima, as defined by version 2.2 of the user ISA and version 1.10 of the
privileged ISA, with the following known exceptions (more exceptions may be
- added, but only if it can be demonstrated that the user ABI is not broken):
+ added, but only if it can be demonstrated that the user ABI is not broken).
+ This bit is clear on a hart that implements Zalrsc without Zaamo; such a
+ hart lists its atomic support in :c:macro:`RISCV_HWPROBE_KEY_IMA_EXT_0`.
* The ``fence.i`` instruction cannot be directly executed by userspace
programs (it may still be executed in userspace via a
diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
index 3dbc8cc557dd..9ba0d49c87bc 100644
--- a/arch/riscv/kernel/cpu.c
+++ b/arch/riscv/kernel/cpu.c
@@ -82,12 +82,29 @@ int __init riscv_early_of_processor_hartid(struct device_node *node, unsigned lo
return -ENODEV;
if (of_property_match_string(node, "riscv,isa-extensions", "i") < 0 ||
- of_property_match_string(node, "riscv,isa-extensions", "m") < 0 ||
- of_property_match_string(node, "riscv,isa-extensions", "a") < 0) {
- pr_warn("CPU with hartid=%lu does not support ima", *hart);
+ of_property_match_string(node, "riscv,isa-extensions", "m") < 0) {
+ pr_warn("CPU with hartid=%lu does not support im", *hart);
return -ENODEV;
}
+ /*
+ * Require the atomic extensions this kernel was built with. "a"
+ * covers both; otherwise LR/SC is always needed and AMO is needed
+ * when the kernel uses it.
+ */
+ if (of_property_match_string(node, "riscv,isa-extensions", "a") < 0) {
+ if (of_property_match_string(node, "riscv,isa-extensions", "zalrsc") < 0) {
+ pr_warn("CPU with hartid=%lu does not support zalrsc", *hart);
+ return -ENODEV;
+ }
+
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO) &&
+ of_property_match_string(node, "riscv,isa-extensions", "zaamo") < 0) {
+ pr_warn("CPU with hartid=%lu does not support zaamo", *hart);
+ return -ENODEV;
+ }
+ }
+
return 0;
old_interface:
diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c
index 7818e1d32622..52c4c2085d0a 100644
--- a/arch/riscv/kernel/sys_hwprobe.c
+++ b/arch/riscv/kernel/sys_hwprobe.c
@@ -213,6 +213,18 @@ static void hwprobe_isa_ext1(struct riscv_hwprobe *pair,
pair->value &= ~missing;
}
+static bool hwprobe_all_cpus_have(const struct cpumask *cpus, unsigned long ext)
+{
+ int cpu;
+
+ for_each_cpu(cpu, cpus) {
+ if (!__riscv_isa_extension_available(hart_isa[cpu].isa, ext))
+ return false;
+ }
+
+ return true;
+}
+
static bool hwprobe_ext0_has(const struct cpumask *cpus, u64 ext)
{
struct riscv_hwprobe pair;
@@ -306,13 +318,14 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair,
hwprobe_arch_id(pair, cpus);
break;
/*
- * The kernel already assumes that the base single-letter ISA
- * extensions are supported on all harts, and only supports the
- * IMA base, so just cheat a bit here and tell that to
- * userspace.
+ * The kernel requires I and M on every hart, so IMA holds when
+ * every selected hart has A as well. A hart that lists only
+ * Zalrsc boots on a kernel built without Zaamo, but it has no
+ * AMO instructions and must not be reported as IMA.
*/
case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
- pair->value = RISCV_HWPROBE_BASE_BEHAVIOR_IMA;
+ if (hwprobe_all_cpus_have(cpus, RISCV_ISA_EXT_A))
+ pair->value = RISCV_HWPROBE_BASE_BEHAVIOR_IMA;
break;
case RISCV_HWPROBE_KEY_IMA_EXT_0:
--
2.43.0