riscv: support CPUs with Zalrsc but not Zaamo
Subsystems (3)
v3 cover riscv: support CPUs with Zalrsc but not Zaamo
RISC-V split the A extension into Zaamo (AMO*.w/AMO*.d) and Zalrsc (LR/SC). Linux has so far required the whole A extension.
MIPS P8700 [1] implements only Zalrsc. AMO instructions are emulated by a trap into SBI. I measured typical userspace atomics on this CPU: an LR/SC atomic takes about 40 cycles, an AMO atomic about 600. The same ratio applies to kernel refcounts, bitops, xchg and futexes, so the kernel needs to emit LR/SC when Zaamo is not implemented.
Mobileye is going to use the "eyeq7h" platform, built on this CPU, for ADAS and autonomous driving. Kernel atomics are on the scheduling, memory-management and driver paths of that product, so the emulated AMO cost is not acceptable there.
This series keeps today's default: both extensions on, -march still contains "a", generated code is unchanged. Turning Zaamo off is a NONPORTABLE choice. The resulting kernel matches one -march, which userspace has to use as well, so a runtime alternative would not produce a portable image.
Zalrsc stays mandatory. cmpxchg, conditional atomics and futex CAS are already LR/SC sequences; Zaamo cannot replace them. BPF atomic ops still emit AMO and remain unsupported on a Zalrsc-only kernel.
[1] https://mips.com/products/hardware/p8700/
Changes in v3:
- Make Zalrsc mandatory. cmpxchg and the conditional atomics already use LR/SC, so RISCV_ISA_ZALRSC can no longer be turned off.
- Check AMO and LR/SC support independently. A kernel that emits both requires "a", or both "zaamo" and "zalrsc".
- Put the LR/SC bitop sequence in its own block so two inlined set_bit or clear_bit calls compile.
- Remove the LR/SC loop that ran after the hart lottery and accessed address 1. Build the lottery increment with add so RV32 assembles.
- Add an exception-table entry for sc as well as lr in the futex LR/SC helper.
Changes in v2:
- Switch from a runtime choice of atomic implementation to a compile-time one, gated by NONPORTABLE.
---
Vladimir Kondratiev (2):
riscv: build kernel atomics from LR/SC when Zaamo is disabled
riscv: accept the atomic extensions this kernel was built with
arch/riscv/Kconfig | 23 +++++++++++++++++ arch/riscv/Makefile | 15 ++++++++--- arch/riscv/include/asm/atomic.h | 56 +++++++++++++++++++++++++++++++++++++++- arch/riscv/include/asm/bitops.h | 51 ++++++++++++++++++++++++++++++++++++ arch/riscv/include/asm/cmpxchg.h | 18 +++++++++++++ arch/riscv/include/asm/futex.h | 53 +++++++++++++++++++++++++++++++++++++ arch/riscv/kernel/cpu.c | 28 +++++++++++++++++--- arch/riscv/kernel/entry.S | 9 +++++++ arch/riscv/kernel/head.S | 14 ++++++++++ 9 files changed, 258 insertions(+), 9 deletions(-)
---
base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
change-id: 20260120-lrsc-only-cc06422f5bb1
Best regards,
Signature · 1 line
Vladimir Kondratiev <[email protected]>
PATCH v3 1/2 riscv: build kernel atomics from LR/SC when Zaamo is disabled
The A extension is Zaamo plus Zalrsc. MIPS P8700 implements only Zalrsc, and its AMO instructions trap to SBI. A typical atomic is about 40 cycles with LR/SC and about 600 cycles with an emulated AMO, so keep the existing AMO sequences when the toolchain defines __riscv_zaamo or __riscv_atomic, and use an LR/SC loop otherwise.
Add RISCV_ISA_ZAAMO and RISCV_ISA_ZALRSC. Both default on, and that selection still passes "a" in -march. Zaamo can be turned off only with CONFIG_NONPORTABLE; Zalrsc remains required because cmpxchg and the conditional atomics are LR/SC sequences. An LR/SC-only build passes _zalrsc instead of "a".
--- arch/riscv/Kconfig | 23 +++++++++++++++++ arch/riscv/Makefile | 15 ++++++++--- arch/riscv/include/asm/atomic.h | 56 +++++++++++++++++++++++++++++++++++++++- arch/riscv/include/asm/bitops.h | 51 ++++++++++++++++++++++++++++++++++++ arch/riscv/include/asm/cmpxchg.h | 18 +++++++++++++ arch/riscv/include/asm/futex.h | 53 +++++++++++++++++++++++++++++++++++++ arch/riscv/kernel/entry.S | 9 +++++++ arch/riscv/kernel/head.S | 14 ++++++++++ 8 files changed, 234 insertions(+), 5 deletions(-)
arch/riscv/Kconfig +23 -0
@@ -730,6 +730,29 @@ config RISCV_ISA_ZACAS If you don't know what to do here, say Y. +config RISCV_ISA_ZAAMO + bool "Zaamo extension support for AMO atomics" if NONPORTABLE + default y + help + Use AMO instructions for kernel atomic memory operations. When + disabled, those operations are built from LR/SC and the kernel + is compiled with zalrsc instead of the A extension. + + Disable this only for a hart that implements Zalrsc but not + Zaamo, such as the MIPS P8700. The resulting kernel cannot run + on a hart without LR/SC. + + If you don't know what to do here, say Y. + +config RISCV_ISA_ZALRSC + def_bool y + help + Zalrsc provides LR/SC. cmpxchg and the conditional atomics + require it, so it is always enabled. + +config RISCV_ISA_A + def_bool RISCV_ISA_ZAAMO && RISCV_ISA_ZALRSC + config TOOLCHAIN_HAS_ZBB bool default y
arch/riscv/Makefile +11 -4
@@ -58,8 +58,9 @@ ifeq ($(CONFIG_SHADOW_CALL_STACK),y) endif # ISA string setting -riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima -riscv-march-$(CONFIG_ARCH_RV64I) := rv64ima +riscv-march-$(CONFIG_ARCH_RV32I) := rv32im +riscv-march-$(CONFIG_ARCH_RV64I) := rv64im +riscv-march-$(CONFIG_RISCV_ISA_A) := $(riscv-march-y)a riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v
@@ -75,9 +76,15 @@ else riscv-march-$(CONFIG_TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI) := $(riscv-march-y)_zicsr_zifencei endif -KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') +KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)fd([^v_]*)v?/\1\2/') export KBUILD_BASE_ISA +# if "A" not supported, toolchain shall support zaamo/zalrsc +ifneq ($(CONFIG_RISCV_ISA_A),y) +riscv-march-$(CONFIG_RISCV_ISA_ZAAMO) := $(riscv-march-y)_zaamo +riscv-march-$(CONFIG_RISCV_ISA_ZALRSC) := $(riscv-march-y)_zalrsc +endif + # Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by # matching non-v and non-multi-letter extensions out with the filter ([^v_]*) KBUILD_CFLAGS += $(KBUILD_BASE_ISA)
@@ -85,7 +92,7 @@ KBUILD_CFLAGS += $(KBUILD_BASE_ISA) KBUILD_AFLAGS += -march=$(riscv-march-y) # For C code built with floating-point support, exclude V but keep F and D. -CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\1\2/') +CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)([^v_]*)v?/\1\2/') KBUILD_CFLAGS += -mno-save-restore
arch/riscv/include/asm/atomic.h +55 -1
80 lines not on this page · raw message
arch/riscv/include/asm/bitops.h +51 -0
81 lines not on this page · raw message
arch/riscv/include/asm/cmpxchg.h +18 -0
@@ -57,6 +57,7 @@ } \ }) +#if defined(__riscv_atomic) || defined(__riscv_zaamo)#define __arch_xchg (sfx, prepend, append, r, p, n) \ ({ \ __asm__ __volatile__ ( \
@@ -67,6 +68,23 @@ : "r" (n) \ : "memory"); \ }) +#elif defined(__riscv_zalrsc) +#define __arch_xchg (sfx, prepend, append, r, p, n) \ +({ \ + __typeof__(*(__ptr)) temp; \ + __asm__ __volatile__ ( \ + prepend \ + "1: lr" sfx " %0, %1\n" \ + " sc" sfx " %2, %3, %1\n" \ + " bnez %2, 1b\n" \ + append \ + : "=&r" (r), "+A" (*(p)), "=&r" (temp) \ + : "r" (n) \ + : "memory"); \ +}) +#else +#error "Need AMO or LR/SC atomics" +#endif #define _arch_xchg (ptr, new, sc_sfx, swap_sfx, prepend, \ sc_append, swap_append) \
arch/riscv/include/asm/futex.h +53 -0
82 lines not on this page · raw message
arch/riscv/kernel/entry.S +9 -0
@@ -73,7 +73,16 @@ beq a2, zero, .Lnew_valid_map_cpus_restore_context /* Atomically reset the current cpu bit in new_valid_map_cpus */ +#if defined(__riscv_atomic) || defined(__riscv_zaamo) amoxor.d a0, a1, (a0) +#elif defined(__riscv_zalrsc) +1: lr.d a2, (a0) + xor a2, a1, a2 + sc.d a2, a2, (a0) + bnez a2, 1b +#else +#error "Need AMO or LR/SC atomics" +#endif /* * A sfence.vma is required here. Even if we had Svvptc, there's no
arch/riscv/kernel/head.S +14 -0
@@ -265,7 +265,21 @@ SYM_CODE_START(_start_kernel) /* Pick one hart to run the main boot sequence */ la a3, hart_lottery li a2, 1 +#if defined(__riscv_atomic) || defined(__riscv_zaamo) amoadd.w a3, a2, (a3) +#elif defined(__riscv_zalrsc) + /* + * hart_lottery is a small 32-bit counter. add is valid on RV32 + * and RV64; sc.w stores the low 32 bits. + */ +1: lr.w t0, (a3) + add t1, t0, a2 + sc.w t1, t1, (a3) + bnez t1, 1b + mv a3, t0 +#else +#error "Need AMO or LR/SC atomics" +#endif bnez a3, .Lsecondary_start #endif /* CONFIG_RISCV_BOOT_SPINWAIT */
Signature · 1 line
2.43.0
kernel test robot
build botRe: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled
kernel test robot noticed the following build errors:
Hi Vladimir,
kernel test robot noticed the following build errors:
[auto build test ERROR on c03c1a14507dfaf8b856c2608f9ac216021fc222]
url: https://github.com/intel-lab-lkp/linux/commits/Vladimir-Kondratiev/riscv-build-kernel-atomics-from-LR-SC-when-Zaamo-is-disabled/20261026-112002 base: c03c1a14507dfaf8b856c2608f9ac216021fc222 patch link: https://lore.kernel.org/r/20261026-lrsc-only-v3-1-ec29c8190fb1%40mobileye.com patch subject: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled config: riscv-randconfig-1001-20261031 (https://download.01.org/0day-ci/archive/20261031/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261031/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> cc1: error: -march=rv64im_zalrsc: unsupported ISA substring '_zalrsc' make[3]: *** [scripts/Makefile.build:290: scripts/mod/empty.o] Error 1 shuffle=1929180073 >> cc1: error: -march=rv64im_zalrsc: unsupported ISA substring '_zalrsc'
make[3]: *** [scripts/Makefile.build:185: scripts/mod/devicetable-offsets.s] Error 1 shuffle=1929180073 make[3]: Target 'scripts/mod/' not remade because of errors.
make[2]: *** [Makefile:1424: prepare0] Error 2 shuffle=1929180073
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2 shuffle=1929180073
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2 shuffle=1929180073
make: Target 'prepare' not remade because of errors.
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled
In reply to Vladimir Kondratievkernel test robot noticed the following build errors:
Hi Vladimir,
kernel test robot noticed the following build errors:
[auto build test ERROR on c03c1a14507dfaf8b856c2608f9ac216021fc222]
url: https://github.com/intel-lab-lkp/linux/commits/Vladimir-Kondratiev/riscv-build-kernel-atomics-from-LR-SC-when-Zaamo-is-disabled/20261026-112002 base: c03c1a14507dfaf8b856c2608f9ac216021fc222 patch link: https://lore.kernel.org/r/20261026-lrsc-only-v3-1-ec29c8190fb1%40mobileye.com patch subject: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled config: riscv-randconfig-1000-20261031 (https://download.01.org/0day-ci/archive/20261031/[email protected]/config) compiler: riscv32-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261031/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> cc1: error: -march=rv32imc_zalrsc: unsupported ISA substring '_zalrsc' make[3]: *** [scripts/Makefile.build:290: scripts/mod/empty.o] Error 1 shuffle=1963593903 >> cc1: error: -march=rv32imc_zalrsc: unsupported ISA substring '_zalrsc'
make[3]: *** [scripts/Makefile.build:185: scripts/mod/devicetable-offsets.s] Error 1 shuffle=1963593903 make[3]: Target 'scripts/mod/' not remade because of errors.
make[2]: *** [Makefile:1424: prepare0] Error 2 shuffle=1963593903
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2 shuffle=1963593903
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2 shuffle=1963593903
make: Target 'prepare' not remade because of errors.
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled
In reply to Vladimir KondratievQuoted 6 lines · Vladimir Kondratiev
The A extension is Zaamo plus Zalrsc. MIPS P8700 implements only Zalrsc, and its AMO instructions trap to SBI. A typical atomic is about 40 cycles with LR/SC and about 600 cycles with an emulated AMO, so keep the existing AMO sequences when the toolchain defines
__riscv_zaamoor__riscv_atomic, and use an LR/SC loop otherwise.
Quoted 8 lines
Add
RISCV_ISA_ZAAMOandRISCV_ISA_ZALRSC. Both default on, and that selection still passes "a" in -march. Zaamo can be turned off only withCONFIG_NONPORTABLE; Zalrsc remains required because cmpxchg and the conditional atomics are LR/SC sequences. An LR/SC-only build passes_zalrscinstead of "a".Suggested-by Chao-ying Fu <[email protected]>Signed-off-by Vladimir Kondratiev <[email protected]>
As you can see by the lkp reports, this is not the right way to do this.
Quoted 24 lines
--- arch/riscv/Kconfig | 23 +++++++++++++++++ arch/riscv/Makefile | 15 ++++++++--- arch/riscv/include/asm/atomic.h | 56 +++++++++++++++++++++++++++++++++++++++- arch/riscv/include/asm/bitops.h | 51 ++++++++++++++++++++++++++++++++++++ arch/riscv/include/asm/cmpxchg.h | 18 +++++++++++++ arch/riscv/include/asm/futex.h | 53 +++++++++++++++++++++++++++++++++++++ arch/riscv/kernel/entry.S | 9 +++++++ arch/riscv/kernel/head.S | 14 ++++++++++ 8 files changed, 234 insertions(+), 5 deletions(-)arch/riscv/Kconfig +1
-0+ config TOOLCHAIN_HAS_ZBB bool default yarch/riscv/Kconfig +1
-0@@ -730,6 +730,29 @@ config RISCV_ISA_ZACAS If you don't know what to do here, say Y. +config RISCV_ISA_ZAAMO
arch/riscv/Kconfig config RISCV_ISA_ZACAS · line 743
Quoted 10 lines
arch/riscv/Kconfig +10
-0+ bool "Zaamo extension support for AMO atomics" if NONPORTABLE + default y + help + Use AMO instructions for kernel atomic memory operations. When + disabled, those operations are built from LR/SC and the kernel + is compiled with zalrsc instead of the A extension. + + Disable this only for a hart that implements Zalrsc but not + Zaamo, such as the MIPS P8700. The resulting kernel cannot run + on a hart without LR/SC.
The text here doesn't really make much sense, given it is default disabled as it depends on NONPORTABLE that is default disabled. If this is retained, it should be default n and the text reworded.
arch/riscv/Kconfig config RISCV_ISA_ZACAS · line 751
Quoted 8 lines
arch/riscv/Kconfig +8
-0+ + If you don't know what to do here, say Y. + +config RISCV_ISA_ZALRSC + def_bool y + help + Zalrsc provides LR/SC. cmpxchg and the conditional atomics + require it, so it is always enabled.
So why have a kconfig option for it then? It's pointless if always enabled.
arch/riscv/Kconfig config RISCV_ISA_ZACAS · line 754
Quoted 3 lines
arch/riscv/Kconfig +3
-0+ +config RISCV_ISA_A + def_bool RISCV_ISA_ZAAMO && RISCV_ISA_ZALRSC
I think this all needs a redo...
Quoted 13 lines
arch/riscv/include/asm/atomic.h +1
-0@@ -50,6 +50,7 @@ static __always_inline void arch_atomic64_set(atomic64_t *v, s64 i)* have the AQ or RL bits set. These don't return anything, so there's only * one version to worry about. */ +#if defined(__riscv_atomic) || defined(__riscv_zaamo)#define ATOMIC_OP (op, asm_op,I , asm_type, c_type, prefix) \ static __always_inline \ void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
arch/riscv/include/asm/atomic.h void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \ · line 64
Quoted 6 lines
arch/riscv/include/asm/atomic.h +2
-1@@ -59,7 +60,25 @@ void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \ : "+A" (v->counter) \ : "r" (I) \ : "memory"); \ -} \ +} +#elif defined(__riscv_zalrsc)
...why are we making this a compiletime decision in the first place? Why can't this be an alternative, starting with the zalrsc stuff and moving to the better implementation if available?
Is there some zaamo code that gets generated automatically that we lose out on by always building the kernel with zalrsc instead of with all of a?
Quoted 37 lines
arch/riscv/include/asm/atomic.h +17
-0+#define ATOMIC_OP (op, asm_op,I , asm_type, c_type, prefix) \ +static __always_inline \ +void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \ +{ \ + register c_type ret, temp; \ + __asm__ __volatile__ ( \ + "1: lr." #asm_type " %1, %0\n" \ + " " #asm_op " %2, %1, %3\n" \ + " sc." #asm_type " %2, %2, %0\n" \ + " bnez %2, 1b\n" \ + : "+A" (v->counter), "=&r" (ret), "=&r" (temp) \ + : "r" (I) \ + : "memory"); \ +} +#else +#error "Need AMO or LR/SC atomics" +#endif #ifdef CONFIG_GENERIC_ATOMIC64 #define ATOMIC_OPS (op, asm_op,I ) \arch/riscv/Makefile +3
-2@@ -58,8 +58,9 @@ ifeq ($(CONFIG_SHADOW_CALL_STACK),y) endif # ISA string setting -riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima -riscv-march-$(CONFIG_ARCH_RV64I) := rv64ima +riscv-march-$(CONFIG_ARCH_RV32I) := rv32im +riscv-march-$(CONFIG_ARCH_RV64I) := rv64im +riscv-march-$(CONFIG_RISCV_ISA_A) := $(riscv-march-y)a riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v
arch/riscv/Makefile else · line 82
Quoted 8 lines
arch/riscv/Makefile +2
-1@@ -75,9 +76,15 @@ else riscv-march-$(CONFIG_TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI) := $(riscv-march-y)_zicsr_zifencei endif -KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') +KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)fd([^v_]*)v?/\1\2/') export KBUILD_BASE_ISA +# if "A" not supported, toolchain shall support zaamo/zalrsc
Obviously this is not true, disabling some Kconfig option has no bearing on what a toolchain is capable of. You cannot put _foo in here without checking if the toolchain actually supports it.
If no zaamo instructions are automatically generated, this dance isn't needed, so my suggestions for how to proceed depend on the answer to that question.
pw-bot: changes-requested
Thanks,
Conor.
Quoted 17 lines
arch/riscv/Makefile +6
-1+ifneq ($(CONFIG_RISCV_ISA_A),y) +riscv-march-$(CONFIG_RISCV_ISA_ZAAMO) := $(riscv-march-y)_zaamo +riscv-march-$(CONFIG_RISCV_ISA_ZALRSC) := $(riscv-march-y)_zalrsc +endif + # Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by # matching non-v and non-multi-letter extensions out with the filter ([^v_]*) KBUILD_CFLAGS += $(KBUILD_BASE_ISA)@@ -85,7 +92,7 @@ KBUILD_CFLAGS += $(KBUILD_BASE_ISA) KBUILD_AFLAGS += -march=$(riscv-march-y) # For C code built with floating-point support, exclude V but keep F and D. -CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\1\2/') +CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)([^v_]*)v?/\1\2/') KBUILD_CFLAGS += -mno-save-restore
Re: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled
Some of issues already fixed on v4.
- Link to v4:
https://patch.msgid.link/[email protected]
Quoted 13 lines · Conor Dooley
Quoted 12 lines · Vladimir Kondratiev
The A extension is Zaamo plus Zalrsc. MIPS P8700 implements only Zalrsc, and its AMO instructions trap to SBI. A typical atomic is about 40 cycles with LR/SC and about 600 cycles with an emulated AMO, so keep the existing AMO sequences when the toolchain defines
__riscv_zaamoor__riscv_atomic, and use an LR/SC loop otherwise.Add
RISCV_ISA_ZAAMOandRISCV_ISA_ZALRSC. Both default on, and that selection still passes "a" in -march. Zaamo can be turned off only withCONFIG_NONPORTABLE; Zalrsc remains required because cmpxchg and the conditional atomics are LR/SC sequences. An LR/SC-only build passes_zalrscinstead of "a".
Quoted 4 lines
Quoted 2 lines
Suggested-by Chao-ying Fu <[email protected]>Signed-off-by Vladimir Kondratiev <[email protected]>As you can see by the lkp reports, this is not the right way to do this.
I fixed these lkp reported bugs in v4 by TOOLCHAIN_HAS_ZALRSC that test compiler support of _zalrsc in -march
Quoted 7 lines
Quoted 7 lines
arch/riscv/Kconfig +7
-0+config RISCV_ISA_ZAAMO + bool "Zaamo extension support for AMO atomics" if NONPORTABLE + default y + help + Use AMO instructions for kernel atomic memory operations. When + disabled, those operations are built from LR/SC and the kernel + is compiled with zalrsc instead of the A extension.
Quoted 8 lines
arch/riscv/Kconfig config RISCV_ISA_ZACAS · line 743
Quoted 4 lines
arch/riscv/Kconfig +4
-0+ + Disable this only for a hart that implements Zalrsc but not + Zaamo, such as the MIPS P8700. The resulting kernel cannot run + on a hart without LR/SC.The text here doesn't really make much sense, given it is default disabled as it depends on NONPORTABLE that is default disabled. If this is retained, it should be default n and the text reworded.
Not exactly. RISCV_ISA_ZAAMO is visible if NONPORTABLE && TOOLCHAIN_HAS_ZALRSC (later was added in v4). Otherwise it is not visible and set to "y". If one disables NONPORTABLE, RISCV_ISA_ZAAMO set to "y" with no option to change it. Same for added in v4 TOOLCHAIN_HAS_ZALRSC
Quoted 11 lines
arch/riscv/Kconfig config RISCV_ISA_ZACAS · line 751
Quoted 8 lines
arch/riscv/Kconfig +8
-0+ + If you don't know what to do here, say Y. + +config RISCV_ISA_ZALRSC + def_bool y + help + Zalrsc provides LR/SC. cmpxchg and the conditional atomics + require it, so it is always enabled.So why have a kconfig option for it then? It's pointless if always enabled.
My logic when adding it:
There are 2 sets of extensions related defines: one is Kconfig ones, CONFIG_RISCV_ISA_<ext>; and another one found in the arch/riscv/include/asm/hwcap.h, RISCV_ISA_EXT_<ext>
In the hwcap.h, there's already
#define RISCV_ISA_EXT_ZAAMO 97
#define RISCV_ISA_EXT_ZALRSC 98
added by commit 35173b666a16 (riscv: add parsing for Zaamo and Zalrsc extensions, 2024-06-19)
These 2 sets are logically coupled, and used in the BPF JUT code like
#define rv_ext_enabled(ext) \ (IS_ENABLED(CONFIG_RISCV_ISA_##ext) &&
riscv_has_extension_likely(RISCV_ISA_EXT_##ext))
So I thought if RISCV_ISA_EXT_ZALRSC exists although it is mandatory, CONFIG_RISCV_ISA_ZALRSC counterpart should exist for the same reason.
Do you suggest this is different case and it is worth to remove CONFIG_RISCV_ISA_ZALRSC anyway?
Quoted 21 lines
arch/riscv/Kconfig config RISCV_ISA_ZACAS · line 754
Quoted 3 lines
arch/riscv/Kconfig +3
-0+ +config RISCV_ISA_A + def_bool RISCV_ISA_ZAAMO && RISCV_ISA_ZALRSCI think this all needs a redo...
Quoted 15 lines
arch/riscv/include/asm/atomic.h +1
-0@@ -50,6 +50,7 @@ static __always_inline void arch_atomic64_set(atomic64_t *v, s64 i)* have the AQ or RL bits set. These don't return anything, so there's only * one version to worry about. */ +#if defined(__riscv_atomic) || defined(__riscv_zaamo)#define ATOMIC_OP (op, asm_op,I , asm_type, c_type, prefix) \ static __always_inline \ void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \@@ -59,7 +60,25 @@ void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \ : "+A" (v->counter) \ : "r" (I) \
Quoted 8 lines
arch/riscv/include/asm/atomic.h void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \ · line 64
Quoted 4 lines
arch/riscv/include/asm/atomic.h +2
-1: "memory"); \ -} \ +} +#elif defined(__riscv_zalrsc)...why are we making this a compiletime decision in the first place? Why can't this be an alternative, starting with the zalrsc stuff and moving to the better implementation if available?
I started with alternatives approach, see
https://lore.kernel.org/all/[email protected]/
Then I converted it to the static compile-time decision following input from Gary Guo that I think is valid, here:
https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Is there some zaamo code that gets generated automatically that we lose out on by always building the kernel with zalrsc instead of with all of a?
There's BPF JIT that emits AMO instructions, patch for this is not ready yet. These AMO instructions will be emulated and work correctly but very slowly.
Quoted 4 lines
Quoted 4 lines
arch/riscv/include/asm/atomic.h +1
-1-KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') +KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)fd([^v_]*)v?/\1\2/') export KBUILD_BASE_ISA
Quoted 8 lines
arch/riscv/include/asm/atomic.h void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \ · line 68
Quoted 1 line
arch/riscv/include/asm/atomic.h +1
-0+# if "A" not supported, toolchain shall support zaamo/zalrscObviously this is not true, disabling some
Kconfigoption has no bearing on what a toolchain is capable of. You cannot put_fooin here without checking if the toolchain actually supports it.
If no zaamo instructions are automatically generated, this dance isn't needed, so my suggestions for how to proceed depend on the answer to that question.
Fixed in v4
Thanks, Vladimir
Re: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled
Quoted 18 lines · Vladimir Kondratiev
Some of issues already fixed on v4.
- Link to v4:
https://patch.msgid.link/[email protected]Quoted 13 lines · Conor Dooley
Quoted 12 lines · Vladimir Kondratiev
The A extension is Zaamo plus Zalrsc. MIPS P8700 implements only Zalrsc, and its AMO instructions trap to SBI. A typical atomic is about 40 cycles with LR/SC and about 600 cycles with an emulated AMO, so keep the existing AMO sequences when the toolchain defines
__riscv_zaamoor__riscv_atomic, and use an LR/SC loop otherwise.Add
RISCV_ISA_ZAAMOandRISCV_ISA_ZALRSC. Both default on, and that selection still passes "a" in -march. Zaamo can be turned off only withCONFIG_NONPORTABLE; Zalrsc remains required because cmpxchg and the conditional atomics are LR/SC sequences. An LR/SC-only build passes_zalrscinstead of "a".
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Suggested-by Chao-ying Fu <[email protected]>Signed-off-by Vladimir Kondratiev <[email protected]>As you can see by the lkp reports, this is not the right way to do this.
I fixed these lkp reported bugs in v4 by
TOOLCHAIN_HAS_ZALRSCthat test compiler support of_zalrscin -march
Right, I'll go conjure up a reply to v4 so.
PATCH v3 2/2 riscv: accept the atomic extensions this kernel was built with
In reply to Vladimir Kondratievriscv_early_of_processor_hartid() rejects a hart unless its riscv,isa-extensions property contains "a". That rejects a Zalrsc-only hart even when the kernel was built to use only LR/SC.
Require "a" or "zaamo" when the kernel emits AMO, and "a" or "zalrsc" when it emits LR/SC. A normal build defines both toolchain macros and still requires the full A extension. The legacy "riscv,isa" fallback keeps its rv{32,64}ima check.
--- arch/riscv/kernel/cpu.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)
arch/riscv/kernel/cpu.c +24 -4
@@ -82,12 +82,32 @@ int __init riscv_early_of_processor_hartid(struct device_node *node, unsigned loreturn -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 ; } - +#if !defined(__riscv_atomic) && !defined(__riscv_zaamo) && !defined(__riscv_zalrsc) +#error "Need AMO or LR/SC atomics" +#endif +/* +* Require each atomic form this kernel emits. The single-letter +* "a" extension satisfies either check. A Zalrsc-only build defines +* __riscv_zalrsc and checks that extension only. +*/ +#if defined(__riscv_atomic) || defined(__riscv_zaamo) +if (of_property_match_string (node,"riscv,isa-extensions" ,"a" ) <0 && +of_property_match_string (node,"riscv,isa-extensions" ,"zaamo" ) <0 ) { +pr_warn ("CPU with hartid=%lu does not support AMO atomics" , *hart); +return -ENODEV ; + } +#endif +#if defined(__riscv_atomic) || defined(__riscv_zalrsc) +if (of_property_match_string (node,"riscv,isa-extensions" ,"a" ) <0 && +of_property_match_string (node,"riscv,isa-extensions" ,"zalrsc" ) <0 ) { +pr_warn ("CPU with hartid=%lu does not support LR/SC atomics" , *hart); +return -ENODEV ; + } +#endif return 0 ; old_interface:
Signature · 1 line
2.43.0
Re: [PATCH v3 2/2] riscv: accept the atomic extensions this kernel was built with
Quoted 11 lines · Vladimir Kondratiev
riscv_early_of_processor_hartid()rejects a hart unless its riscv,isa-extensions property contains "a". That rejects a Zalrsc-only hart even when the kernel was built to use only LR/SC.Require "a" or "zaamo" when the kernel emits AMO, and "a" or "zalrsc" when it emits LR/SC. A normal build defines both toolchain macros and still requires the full A extension. The legacy "riscv,isa" fallback keeps its rv{32,64}ima check.
Signed-off-by Vladimir Kondratiev <[email protected]
IMO, this should not be a separate patch.
Quoted 49 lines
--- arch/riscv/kernel/cpu.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)arch/riscv/kernel/cpu.c +24
-4@@ -82,12 +82,32 @@ int __init riscv_early_of_processor_hartid(struct device_node *node, unsigned loreturn -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 ; } - +#if !defined(__riscv_atomic) && !defined(__riscv_zaamo) && !defined(__riscv_zalrsc) +#error "Need AMO or LR/SC atomics" +#endif +/* +* Require each atomic form this kernel emits. The single-letter +* "a" extension satisfies either check. A Zalrsc-only build defines +* __riscv_zalrsc and checks that extension only. +*/ +#if defined(__riscv_atomic) || defined(__riscv_zaamo) +if (of_property_match_string (node,"riscv,isa-extensions" ,"a" ) <0 && +of_property_match_string (node,"riscv,isa-extensions" ,"zaamo" ) <0 ) { +pr_warn ("CPU with hartid=%lu does not support AMO atomics" , *hart); +return -ENODEV ; + } +#endif +#if defined(__riscv_atomic) || defined(__riscv_zalrsc) +if (of_property_match_string (node,"riscv,isa-extensions" ,"a" ) <0 && +of_property_match_string (node,"riscv,isa-extensions" ,"zalrsc" ) <0 ) { +pr_warn ("CPU with hartid=%lu does not support LR/SC atomics" , *hart); +return -ENODEV ; + } +#endif return 0 ; old_interface:Signature · 2 lines
-- 2.43.0
Re: [PATCH v3 2/2] riscv: accept the atomic extensions this kernel was built with
Quoted 10 lines · Conor Dooley
Quoted 9 lines · Vladimir Kondratiev
riscv_early_of_processor_hartid()rejects a hart unless its riscv,isa-extensions property contains "a". That rejects a Zalrsc-only hart even when the kernel was built to use only LR/SC.Require "a" or "zaamo" when the kernel emits AMO, and "a" or "zalrsc" when it emits LR/SC. A normal build defines both toolchain macros and still requires the full A extension. The legacy "riscv,isa" fallback keeps its rv{32,64}ima check.
Quoted 4 lines
Quoted 2 lines
Signed-off-by Vladimir Kondratiev <[email protected]IMO, this should not be a separate patch.
I can squash it with previous patch.
My thought was: it address different aspect. While previous patch allows compiling code that don't use AMO, this one is about correct manifestation of the extensions supported by CPU. One can use only previous patch and declare "a" support in the riscv,isa-extensions. Not exactly true but works. With this patch, one can say "zalrsc" instead of "a". This have no effect on how kernel get compiled but let user space know exact CPU features.
Thanks, Vladimir
Re: [PATCH v3 2/2] riscv: accept the atomic extensions this kernel was built with
Quoted 16 lines · Vladimir Kondratiev
Quoted 10 lines · Conor Dooley
Quoted 9 lines · Vladimir Kondratiev
riscv_early_of_processor_hartid()rejects a hart unless its riscv,isa-extensions property contains "a". That rejects a Zalrsc-only hart even when the kernel was built to use only LR/SC.Require "a" or "zaamo" when the kernel emits AMO, and "a" or "zalrsc" when it emits LR/SC. A normal build defines both toolchain macros and still requires the full A extension. The legacy "riscv,isa" fallback keeps its rv{32,64}ima check.
Quoted 4 lines
Quoted 2 lines
Signed-off-by Vladimir Kondratiev <[email protected]IMO, this should not be a separate patch.
Quoted 8 lines
I can squash it with previous patch.
My thought was: it address different aspect. While previous patch allows compiling code that don't use AMO, this one is about correct manifestation of the extensions supported by CPU. One can use only previous patch and declare "a" support in the riscv,isa-extensions. Not exactly true but works. With this patch, one can say "zalrsc" instead of "a". This have no effect on how kernel get compiled but let user space know exact CPU features.
This has nothing to do with letting userspace know about cpu features though, this code rejects CPUs during boot that do not meet the minimum featureset of the kernel. Your previous patch relies on this, you can't boot a zalrsc only kernel without making this change so I would say this is intrinsically linked and should be part of the prior patch. Just because it works if you lie about what your hardware can do, doesn't justify splitting it...
Peter Zijlstra
maintainerQuoted 20 lines · Vladimir Kondratiev
RISC-V split the A extension into Zaamo (AMO*.w/AMO*.d) and Zalrsc (LR/SC). Linux has so far required the whole A extension.
MIPS P8700 [1] implements only Zalrsc. AMO instructions are emulated by a trap into SBI. I measured typical userspace atomics on this CPU: an LR/SC atomic takes about 40 cycles, an AMO atomic about 600. The same ratio applies to kernel refcounts, bitops, xchg and futexes, so the kernel needs to emit LR/SC when Zaamo is not implemented.
Mobileye is going to use the "eyeq7h" platform, built on this CPU, for ADAS and autonomous driving. Kernel atomics are on the scheduling, memory-management and driver paths of that product, so the emulated AMO cost is not acceptable there.
This series keeps today's default: both extensions on, -march still contains "a", generated code is unchanged. Turning Zaamo off is a NONPORTABLE choice. The resulting kernel matches one -march, which userspace has to use as well, so a runtime alternative would not produce a portable image.
Quoted 3 lines
Zalrsc stays mandatory. cmpxchg, conditional atomics and futex CAS are already LR/SC sequences; Zaamo cannot replace them. BPF atomic ops still emit AMO and remain unsupported on a Zalrsc-only kernel.
The comment with RISCV_QUEUED_SPINLOCKS state that Zalrsc requires that cmpxchg() loops have forward progress; which is much stronger than the typical LL/SC implementation.
Does this MIPS thing indeed provide this?
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 5 lines · Peter Zijlstra
The comment with
RISCV_QUEUED_SPINLOCKSstate that Zalrsc requires thatcmpxchg()loops have forward progress; which is much stronger than the typical LL/SC implementation.Does this MIPS thing indeed provide this?
You mean Ziccrse extension? Yes, p8700 does provide it. I explicitly confirmed it with hardware designers.
In fact, I use RISCV_QUEUED_SPINLOCKS on this platform
Thanks, Vladimir