| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/11/22 21:08 | flow | patch-triage |
1mArgs:null Results: {
"EnableConfigs": null,
"FocusSymbols": [],
"KMSANReasoning": "",
"NeedsKMSAN": false,
"Reasoning": "All changes in this patch series are either documentation updates or code exclusive to the RISC-V architecture (arch/riscv/). Because the target architecture is amd64, none of the modified executable code can be compiled or executed during fuzzing.",
"WorthFuzzing": false
} |
| 1/1 | 2026/11/22 21:08 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 83842795ec1d4b3e01626923b7662826360ed224\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sun Nov 22 21:08:00 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt\nindex e75344f4e0cdef..9bc5baff20ff0f 100644\n--- a/Documentation/admin-guide/kernel-parameters.txt\n+++ b/Documentation/admin-guide/kernel-parameters.txt\n@@ -513,6 +513,13 @@ Kernel parameters\n \t\t\tFormat: \u003ca\u003e,\u003cb\u003e\n \t\t\tSee also Documentation/input/joydev/joystick.rst\n \n+\tamo_speed=\t[RISCV]\n+\t\t\tFormat: {slow | fast}\n+\t\t\tAllow skipping the AMO instruction speed test. This is\n+\t\t\tuseful for testing alternative code paths and to skip\n+\t\t\tthe test in environments where it runs too slowly. All\n+\t\t\tCPUs must have the same AMO speed.\n+\n \tanalog.map=\t[HW,JOY] Analog joystick and gamepad support\n \t\t\tSpecifies type or capabilities of an analog joystick\n \t\t\tconnected to one of 16 gameports\ndiff --git a/Documentation/arch/riscv/hwprobe.rst b/Documentation/arch/riscv/hwprobe.rst\nindex bb1e0cbab36f08..84aef6a0a79c22 100644\n--- a/Documentation/arch/riscv/hwprobe.rst\n+++ b/Documentation/arch/riscv/hwprobe.rst\n@@ -61,7 +61,9 @@ The following keys are defined:\n * :c:macro:`RISCV_HWPROBE_BASE_BEHAVIOR_IMA`: Support for rv32ima or\n rv64ima, as defined by version 2.2 of the user ISA and version 1.10 of the\n privileged ISA, with the following known exceptions (more exceptions may be\n- added, but only if it can be demonstrated that the user ABI is not broken):\n+ added, but only if it can be demonstrated that the user ABI is not broken).\n+ This bit is clear on a hart that implements Zalrsc without Zaamo; such a\n+ hart lists its atomic support in :c:macro:`RISCV_HWPROBE_KEY_IMA_EXT_0`.\n \n * The ``fence.i`` instruction cannot be directly executed by userspace\n programs (it may still be executed in userspace via a\n@@ -423,3 +425,21 @@ The following keys are defined:\n * :c:macro:`RISCV_HWPROBE_EXT_ZA64RS`: The Za64rs extension is supported,\n as defined in the RISC-V Profiles specification starting from commit\n b1d80660 (\"Updated to ratified state.\")\n+\n+* :c:macro:`RISCV_HWPROBE_KEY_AMO_PERF`: An enum value describing the\n+ performance of the AMO instructions of the Zaamo extension, relative to\n+ the equivalent LR/SC sequence, on the selected set of processors.\n+\n+ * :c:macro:`RISCV_HWPROBE_AMO_UNKNOWN`: The performance of AMO instructions\n+ is unknown.\n+\n+ * :c:macro:`RISCV_HWPROBE_AMO_SLOW`: An uncontended AMO instruction takes\n+ longer than the equivalent LR/SC sequence. This is the case when AMO\n+ instructions are trapped and emulated by firmware. Code that can choose\n+ should use LR/SC.\n+\n+ * :c:macro:`RISCV_HWPROBE_AMO_FAST`: An uncontended AMO instruction is at\n+ least as fast as the equivalent LR/SC sequence.\n+\n+ * :c:macro:`RISCV_HWPROBE_AMO_UNSUPPORTED`: AMO instructions are not\n+ supported, the Zaamo extension is absent.\ndiff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig\nindex 5874ca30f4f6d3..dfd6237f879b83 100644\n--- a/arch/riscv/Kconfig\n+++ b/arch/riscv/Kconfig\n@@ -237,6 +237,8 @@ config RISCV\n config RUSTC_SUPPORTS_RISCV\n \tdef_bool y\n \tdepends on 64BIT\n+\t# The riscv64imac target emits AMO instructions\n+\tdepends on RISCV_ISA_ZAAMO\n \n config CLANG_SUPPORTS_DYNAMIC_FTRACE\n \tdef_bool CC_IS_CLANG\n@@ -706,6 +708,7 @@ config RISCV_ISA_ZABHA\n \tbool \"Zabha extension support for atomic byte/halfword operations\"\n \tdepends on TOOLCHAIN_HAS_ZABHA\n \tdepends on RISCV_ALTERNATIVE\n+\tdepends on RISCV_ISA_ZAAMO\n \tdefault y\n \thelp\n \t Enable the use of the Zabha ISA-extension to implement kernel\n@@ -723,6 +726,7 @@ config TOOLCHAIN_HAS_ZACAS\n config RISCV_ISA_ZACAS\n \tbool \"Zacas extension support for atomic CAS\"\n \tdepends on RISCV_ALTERNATIVE\n+\tdepends on RISCV_ISA_ZAAMO\n \tdefault y\n \thelp\n \t Enable the use of the Zacas ISA-extension to implement kernel atomic\n@@ -730,6 +734,49 @@ config RISCV_ISA_ZACAS\n \n \t If you don't know what to do here, say Y.\n \n+config TOOLCHAIN_HAS_ZALRSC_ZAAMO\n+\tbool\n+\tdefault y\n+\tdepends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64im_zaamo_zalrsc)\n+\tdepends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32im_zaamo_zalrsc)\n+\n+config RISCV_ISA_ZAAMO\n+\tbool \"Zaamo extension support for AMO atomics\" if TOOLCHAIN_HAS_ZALRSC_ZAAMO\n+\tdefault y\n+\thelp\n+\t Use AMO instructions for kernel atomic memory operations. When\n+\t disabled, those operations are built from LR/SC and the kernel\n+\t is compiled with zalrsc instead of the A extension.\n+\n+\t Disable this for a CPU where AMO is much slower than LR/SC. That\n+\t is the case when AMO instructions are emulated by a trap into\n+\t M-mode, as on the MIPS P8700: an emulated atomic costs hundreds\n+\t of cycles where LR/SC costs tens.\n+\n+\t A kernel built without Zaamo runs on any CPU that implements the\n+\t A extension. Zabha, Zacas, userspace control flow integrity and\n+\t Rust need Zaamo and are unavailable without it.\n+\n+\t The compiler must accept _zaamo and _zalrsc in -march to make\n+\t this choice. GCC 15.1 is the first release that does. With an\n+\t older compiler the option stays enabled and -march keeps the A\n+\t extension.\n+\n+\t If you don't know what to do here, say Y.\n+\n+config RISCV_PROBE_AMO_SPEED\n+\tbool \"Probe AMO instruction speed at boot\"\n+\tdefault y\n+\thelp\n+\t During boot, time an uncontended AMO instruction against the\n+\t equivalent LR/SC sequence on every CPU, and report which one is\n+\t faster to userspace through the hwprobe syscall, as\n+\t RISCV_HWPROBE_KEY_AMO_PERF. On a CPU that traps AMO instructions\n+\t into firmware and emulates them there, LR/SC is much faster, and\n+\t code that can choose should use it.\n+\n+\t If you don't know what to do here, say Y.\n+\n config TOOLCHAIN_HAS_ZBB\n \tbool\n \tdefault y\n@@ -1206,6 +1253,8 @@ config RISCV_USER_CFI\n \tdepends on 64BIT \u0026\u0026 MMU \u0026\u0026 \\\n \t\t$(cc-option,-mabi=lp64 -march=rv64ima_zicfiss_zicfilp -fcf-protection=full)\n \tdepends on RISCV_ALTERNATIVE\n+\t# ssamoswap is an AMO instruction\n+\tdepends on RISCV_ISA_ZAAMO\n \tselect RISCV_SBI\n \tselect ARCH_HAS_USER_SHADOW_STACK\n \tselect ARCH_USES_HIGH_VMA_FLAGS\ndiff --git a/arch/riscv/Makefile b/arch/riscv/Makefile\nindex 44f5a439741a91..59c5123c5485fa 100644\n--- a/arch/riscv/Makefile\n+++ b/arch/riscv/Makefile\n@@ -58,8 +58,9 @@ ifeq ($(CONFIG_SHADOW_CALL_STACK),y)\n endif\n \n # ISA string setting\n-riscv-march-$(CONFIG_ARCH_RV32I)\t:= rv32ima\n-riscv-march-$(CONFIG_ARCH_RV64I)\t:= rv64ima\n+riscv-march-$(CONFIG_ARCH_RV32I)\t:= rv32im\n+riscv-march-$(CONFIG_ARCH_RV64I)\t:= rv64im\n+riscv-march-$(CONFIG_RISCV_ISA_ZAAMO)\t:= $(riscv-march-y)a\n riscv-march-$(CONFIG_FPU)\t\t:= $(riscv-march-y)fd\n riscv-march-$(CONFIG_RISCV_ISA_C)\t:= $(riscv-march-y)c\n riscv-march-$(CONFIG_RISCV_ISA_V)\t:= $(riscv-march-y)v\n@@ -75,9 +76,15 @@ else\n riscv-march-$(CONFIG_TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI) := $(riscv-march-y)_zicsr_zifencei\n endif\n \n-KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\\1\\2/')\n+KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)fd([^v_]*)v?/\\1\\2/')\n export KBUILD_BASE_ISA\n \n+# Without Zaamo only the LR/SC half of \"a\" is used. The compiler accepts\n+# _zalrsc here, see TOOLCHAIN_HAS_ZALRSC_ZAAMO.\n+ifneq ($(CONFIG_RISCV_ISA_ZAAMO),y)\n+riscv-march-y := $(riscv-march-y)_zalrsc\n+endif\n+\n # Remove F,D,V from isa string for all. Keep extensions between \"fd\" and \"v\" by\n # matching non-v and non-multi-letter extensions out with the filter ([^v_]*)\n KBUILD_CFLAGS += $(KBUILD_BASE_ISA)\n@@ -85,7 +92,7 @@ KBUILD_CFLAGS += $(KBUILD_BASE_ISA)\n KBUILD_AFLAGS += -march=$(riscv-march-y)\n \n # For C code built with floating-point support, exclude V but keep F and D.\n-CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\\1\\2/')\n+CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)([^v_]*)v?/\\1\\2/')\n \n KBUILD_CFLAGS += -mno-save-restore\n \ndiff --git a/arch/riscv/include/asm/atomic.h b/arch/riscv/include/asm/atomic.h\nindex 616b8b332ac56c..cca1fc3a68ffcd 100644\n--- a/arch/riscv/include/asm/atomic.h\n+++ b/arch/riscv/include/asm/atomic.h\n@@ -50,6 +50,7 @@ static __always_inline void arch_atomic64_set(atomic64_t *v, s64 i)\n * have the AQ or RL bits set. These don't return anything, so there's only\n * one version to worry about.\n */\n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n #define ATOMIC_OP(op, asm_op, I, asm_type, c_type, prefix)\t\t\\\n static __always_inline\t\t\t\t\t\t\t\\\n void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v)\t\\\n@@ -59,7 +60,23 @@ void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v)\t\\\n \t\t: \"+A\" (v-\u003ecounter)\t\t\t\t\t\\\n \t\t: \"r\" (I)\t\t\t\t\t\t\\\n \t\t: \"memory\");\t\t\t\t\t\t\\\n-}\t\t\t\t\t\t\t\t\t\\\n+}\n+#else\n+#define ATOMIC_OP(op, asm_op, I, asm_type, c_type, prefix)\t\t\\\n+static __always_inline\t\t\t\t\t\t\t\\\n+void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v)\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+\tregister c_type ret, temp;\t\t\t\t\t\\\n+\t__asm__ __volatile__ (\t\t\t\t\t\t\\\n+\t\t\"1:\tlr.\" #asm_type \" %1, %0\\n\"\t\t\t\\\n+\t\t\"\t\" #asm_op \" %2, %1, %3\\n\"\t\t\t\\\n+\t\t\"\tsc.\" #asm_type \" %2, %2, %0\\n\"\t\t\t\\\n+\t\t\"\tbnez %2, 1b\\n\"\t\t\t\t\t\\\n+\t\t: \"+A\" (v-\u003ecounter), \"=\u0026r\" (ret), \"=\u0026r\" (temp)\t\t\\\n+\t\t: \"r\" (I)\t\t\t\t\t\t\\\n+\t\t: \"memory\");\t\t\t\t\t\t\\\n+}\n+#endif\n \n #ifdef CONFIG_GENERIC_ATOMIC64\n #define ATOMIC_OPS(op, asm_op, I)\t\t\t\t\t\\\n@@ -84,6 +101,7 @@ ATOMIC_OPS(xor, xor, i)\n * There's two flavors of these: the arithmetic ops have both fetch and return\n * versions, while the logical ops only have fetch versions.\n */\n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n #define ATOMIC_FETCH_OP(op, asm_op, I, asm_type, c_type, prefix)\t\\\n static __always_inline\t\t\t\t\t\t\t\\\n c_type arch_atomic##prefix##_fetch_##op##_relaxed(c_type i,\t\t\\\n@@ -108,6 +126,39 @@ c_type arch_atomic##prefix##_fetch_##op(c_type i, atomic##prefix##_t *v)\t\\\n \t\t: \"memory\");\t\t\t\t\t\t\\\n \treturn ret;\t\t\t\t\t\t\t\\\n }\n+#else\n+#define ATOMIC_FETCH_OP(op, asm_op, I, asm_type, c_type, prefix)\t\\\n+static __always_inline\t\t\t\t\t\t\t\\\n+c_type arch_atomic##prefix##_fetch_##op##_relaxed(c_type i,\t\t\\\n+\t\t\t\t\t atomic##prefix##_t *v)\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+\tregister c_type ret, temp;\t\t\t\t\t\\\n+\t__asm__ __volatile__ (\t\t\t\t\t\t\\\n+\t\t\"1:\tlr.\" #asm_type \" %1, %0\\n\"\t\t\t\\\n+\t\t\"\t\" #asm_op \" %2, %1, %3\\n\"\t\t\t\\\n+\t\t\"\tsc.\" #asm_type \" %2, %2, %0\\n\"\t\t\t\\\n+\t\t\"\tbnez %2, 1b\\n\"\t\t\t\t\t\\\n+\t\t: \"+A\" (v-\u003ecounter), \"=\u0026r\" (ret), \"=\u0026r\" (temp)\t\t\\\n+\t\t: \"r\" (I)\t\t\t\t\t\t\\\n+\t\t: \"memory\");\t\t\t\t\t\t\\\n+\treturn ret;\t\t\t\t\t\t\t\\\n+}\t\t\t\t\t\t\t\t\t\\\n+static __always_inline\t\t\t\t\t\t\t\\\n+c_type arch_atomic##prefix##_fetch_##op(c_type i, atomic##prefix##_t *v)\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+\tregister c_type ret, temp;\t\t\t\t\t\\\n+\t__asm__ __volatile__ (\t\t\t\t\t\t\\\n+\t\t\"1:\tlr.\" #asm_type \" %1, %0\\n\"\t\t\t\\\n+\t\t\"\t\" #asm_op \" %2, %1, %3\\n\"\t\t\t\\\n+\t\t\"\tsc.\" #asm_type \".rl %2, %2, %0\\n\"\t\t\\\n+\t\t\"\tbnez %2, 1b\\n\"\t\t\t\t\t\\\n+\t\tRISCV_FULL_BARRIER\t\t\t\t\t\\\n+\t\t: \"+A\" (v-\u003ecounter), \"=\u0026r\" (ret), \"=\u0026r\" (temp)\t\t\\\n+\t\t: \"r\" (I)\t\t\t\t\t\t\\\n+\t\t: \"memory\");\t\t\t\t\t\t\\\n+\treturn ret;\t\t\t\t\t\t\t\\\n+}\n+#endif\n \n #define ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_type, c_type, prefix)\t\\\n static __always_inline\t\t\t\t\t\t\t\\\ndiff --git a/arch/riscv/include/asm/bitops.h b/arch/riscv/include/asm/bitops.h\nindex 3c1a15be54d804..adcd7052577468 100644\n--- a/arch/riscv/include/asm/bitops.h\n+++ b/arch/riscv/include/asm/bitops.h\n@@ -170,12 +170,17 @@ static __always_inline int variable_fls(unsigned int x)\n \n #if (BITS_PER_LONG == 64)\n #define __AMO(op)\t\"amo\" #op \".d\"\n+#define __LR\t\"lr.d\"\n+#define __SC\t\"sc.d\"\n #elif (BITS_PER_LONG == 32)\n #define __AMO(op)\t\"amo\" #op \".w\"\n+#define __LR\t\"lr.w\"\n+#define __SC\t\"sc.w\"\n #else\n #error \"Unexpected BITS_PER_LONG\"\n #endif\n \n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n #define __test_and_op_bit_ord(op, mod, nr, addr, ord)\t\t\\\n ({\t\t\t\t\t\t\t\t\\\n \tunsigned long __res, __mask;\t\t\t\t\\\n@@ -195,10 +200,58 @@ static __always_inline int variable_fls(unsigned int x)\n \t\t: \"r\" (mod(BIT_MASK(nr)))\t\t\t\\\n \t\t: \"memory\");\n \n-#define __test_and_op_bit(op, mod, nr, addr) \t\t\t\\\n+#define __test_and_op_bit(op, mod, nr, addr)\t\t\t\\\n \t__test_and_op_bit_ord(op, mod, nr, addr, .aqrl)\n+#define __test_and_op_bit_acquire(op, mod, nr, addr)\t\t\\\n+\t__test_and_op_bit_ord(op, mod, nr, addr, .aq)\n #define __op_bit(op, mod, nr, addr)\t\t\t\t\\\n \t__op_bit_ord(op, mod, nr, addr, )\n+#define __op_bit_release(op, mod, nr, addr)\t\t\t\\\n+\t__op_bit_ord(op, mod, nr, addr, .rl)\n+#else\n+/*\n+ * Fully ordered LR/SC is lr, sc.rl and a full fence; acquire is lr.aq and\n+ * release is sc.rl, with no ordering on the other instruction. This is how\n+ * the LR/SC loops in asm/atomic.h and asm/cmpxchg.h are built.\n+ */\n+#define __test_and_op_bit_lrsc(op, mod, nr, addr, lr_sfx, sc_sfx, append)\t\\\n+({\t\t\t\t\t\t\t\t\\\n+\tunsigned long __res, __mask, __temp;\t\t\t\\\n+\t__mask = BIT_MASK(nr);\t\t\t\t\t\\\n+\t__asm__ __volatile__ (\t\t\t\t\t\\\n+\t\t\"1: \" __LR lr_sfx \" %0, %1\\n\"\t\t\t\\\n+\t\t#op \" %2, %0, %3\\n\"\t\t\t\t\\\n+\t\t__SC sc_sfx \" %2, %2, %1\\n\"\t\t\t\\\n+\t\t\"bnez %2, 1b\\n\"\t\t\t\t\t\\\n+\t\tappend\t\t\t\t\t\t\\\n+\t\t: \"=\u0026r\" (__res), \"+A\" (addr[BIT_WORD(nr)]), \"=\u0026r\" (__temp)\t\\\n+\t\t: \"r\" (mod(__mask))\t\t\t\t\\\n+\t\t: \"memory\");\t\t\t\t\t\\\n+\t((__res \u0026 __mask) != 0);\t\t\t\t\\\n+})\n+\n+#define __op_bit_lrsc(op, mod, nr, addr, lr_sfx, sc_sfx)\t\\\n+do {\t\t\t\t\t\t\t\t\\\n+\tunsigned long __res, __temp;\t\t\t\t\\\n+\t__asm__ __volatile__ (\t\t\t\t\t\\\n+\t\t\"1: \" __LR lr_sfx \" %0, %1\\n\"\t\t\t\\\n+\t\t#op \" %2, %0, %3\\n\"\t\t\t\t\\\n+\t\t__SC sc_sfx \" %2, %2, %1\\n\"\t\t\t\\\n+\t\t\"bnez %2, 1b\\n\"\t\t\t\t\t\\\n+\t\t: \"=\u0026r\" (__res), \"+A\" (addr[BIT_WORD(nr)]), \"=\u0026r\" (__temp)\t\\\n+\t\t: \"r\" (mod(BIT_MASK(nr)))\t\t\t\\\n+\t\t: \"memory\");\t\t\t\t\t\\\n+} while (0)\n+\n+#define __test_and_op_bit(op, mod, nr, addr)\t\t\t\\\n+\t__test_and_op_bit_lrsc(op, mod, nr, addr, \"\", \".rl\", RISCV_FULL_BARRIER)\n+#define __test_and_op_bit_acquire(op, mod, nr, addr)\t\t\\\n+\t__test_and_op_bit_lrsc(op, mod, nr, addr, \".aq\", \"\", \"\")\n+#define __op_bit(op, mod, nr, addr)\t\t\t\t\\\n+\t__op_bit_lrsc(op, mod, nr, addr, \"\", \"\")\n+#define __op_bit_release(op, mod, nr, addr)\t\t\t\\\n+\t__op_bit_lrsc(op, mod, nr, addr, \"\", \".rl\")\n+#endif\n \n /* Bitmask modifiers */\n #define __NOP(x)\t(x)\n@@ -297,7 +350,7 @@ static __always_inline void arch_change_bit(int nr, volatile unsigned long *addr\n static __always_inline int arch_test_and_set_bit_lock(\n \tunsigned long nr, volatile unsigned long *addr)\n {\n-\treturn __test_and_op_bit_ord(or, __NOP, nr, addr, .aq);\n+\treturn __test_and_op_bit_acquire(or, __NOP, nr, addr);\n }\n \n /**\n@@ -310,7 +363,7 @@ static __always_inline int arch_test_and_set_bit_lock(\n static __always_inline void arch_clear_bit_unlock(\n \tunsigned long nr, volatile unsigned long *addr)\n {\n-\t__op_bit_ord(and, __NOT, nr, addr, .rl);\n+\t__op_bit_release(and, __NOT, nr, addr);\n }\n \n /**\n@@ -337,20 +390,37 @@ static __always_inline void arch___clear_bit_unlock(\n static __always_inline bool arch_xor_unlock_is_negative_byte(unsigned long mask,\n \t\tvolatile unsigned long *addr)\n {\n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n \tunsigned long res;\n \t__asm__ __volatile__ (\n \t\t__AMO(xor) \".rl %0, %2, %1\"\n \t\t: \"=r\" (res), \"+A\" (*addr)\n \t\t: \"r\" (__NOP(mask))\n \t\t: \"memory\");\n+#else\n+\tunsigned long res, temp;\n+\n+\t__asm__ __volatile__ (\n+\t\t\"1: \" __LR \" %0, %1\\n\"\n+\t\t\"xor %2, %0, %3\\n\"\n+\t\t__SC \".rl %2, %2, %1\\n\"\n+\t\t\"bnez %2, 1b\\n\"\n+\t\t: \"=\u0026r\" (res), \"+A\" (*addr), \"=\u0026r\" (temp)\n+\t\t: \"r\" (__NOP(mask))\n+\t\t: \"memory\");\n+#endif\n \treturn (res \u0026 BIT(7)) != 0;\n }\n \n #undef __test_and_op_bit\n+#undef __test_and_op_bit_acquire\n #undef __op_bit\n+#undef __op_bit_release\n #undef __NOP\n #undef __NOT\n #undef __AMO\n+#undef __LR\n+#undef __SC\n \n #include \u003casm-generic/bitops/instrumented-atomic.h\u003e\n #include \u003casm-generic/bitops/instrumented-lock.h\u003e\ndiff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h\nindex 662e160b05229b..f0199432948a0e 100644\n--- a/arch/riscv/include/asm/cmpxchg.h\n+++ b/arch/riscv/include/asm/cmpxchg.h\n@@ -57,16 +57,39 @@\n \t}\t\t\t\t\t\t\t\t\t\\\n })\n \n-#define __arch_xchg(sfx, prepend, append, r, p, n)\t\t\t\\\n+/*\n+ * Word and double-word xchg is one amoswap, or an LR/SC loop without\n+ * Zaamo. Each form uses its own pair of ordering arguments, as in\n+ * __arch_xchg_masked() above.\n+ */\n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n+#define __arch_xchg(w, sc_sfx, swap_sfx, prepend, sc_append,\t\t\\\n+\t\t swap_append, r, p, n)\t\t\t\t\\\n ({\t\t\t\t\t\t\t\t\t\\\n \t__asm__ __volatile__ (\t\t\t\t\t\t\\\n \t\tprepend\t\t\t\t\t\t\t\\\n-\t\t\"\tamoswap\" sfx \" %0, %2, %1\\n\"\t\t\t\\\n-\t\tappend\t\t\t\t\t\t\t\\\n+\t\t\"\tamoswap\" w swap_sfx \" %0, %2, %1\\n\"\t\t\\\n+\t\tswap_append\t\t\t\t\t\t\\\n \t\t: \"=r\" (r), \"+A\" (*(p))\t\t\t\t\t\\\n \t\t: \"r\" (n)\t\t\t\t\t\t\\\n \t\t: \"memory\");\t\t\t\t\t\t\\\n })\n+#else\n+#define __arch_xchg(w, sc_sfx, swap_sfx, prepend, sc_append,\t\t\\\n+\t\t swap_append, r, p, n)\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\t__typeof__(*(p)) __temp;\t\t\t\t\t\\\n+\t__asm__ __volatile__ (\t\t\t\t\t\t\\\n+\t\tprepend\t\t\t\t\t\t\t\\\n+\t\t\"1:\tlr\" w \" %0, %1\\n\"\t\t\t\t\\\n+\t\t\"\tsc\" w sc_sfx \" %2, %3, %1\\n\"\t\t\t\\\n+\t\t\"\tbnez %2, 1b\\n\"\t\t\t\t\t\\\n+\t\tsc_append\t\t\t\t\t\t\\\n+\t\t: \"=\u0026r\" (r), \"+A\" (*(p)), \"=\u0026r\" (__temp)\t\t\\\n+\t\t: \"r\" (n)\t\t\t\t\t\t\\\n+\t\t: \"memory\");\t\t\t\t\t\t\\\n+})\n+#endif\n \n #define _arch_xchg(ptr, new, sc_sfx, swap_sfx, prepend,\t\t\t\\\n \t\t sc_append, swap_append)\t\t\t\t\\\n@@ -87,12 +110,14 @@\n \t\t\t\t __ret, __ptr, __new);\t\t\\\n \t\tbreak;\t\t\t\t\t\t\t\\\n \tcase 4:\t\t\t\t\t\t\t\t\\\n-\t\t__arch_xchg(\".w\" swap_sfx, prepend, swap_append,\t\\\n-\t\t\t __ret, __ptr, __new);\t\t\t\\\n+\t\t__arch_xchg(\".w\", sc_sfx, swap_sfx,\t\t\t\\\n+\t\t\t prepend, sc_append, swap_append,\t\t\\\n+\t\t\t __ret, __ptr, __new);\t\t\t\\\n \t\tbreak;\t\t\t\t\t\t\t\\\n \tcase 8:\t\t\t\t\t\t\t\t\\\n-\t\t__arch_xchg(\".d\" swap_sfx, prepend, swap_append,\t\\\n-\t\t\t __ret, __ptr, __new);\t\t\t\\\n+\t\t__arch_xchg(\".d\", sc_sfx, swap_sfx,\t\t\t\\\n+\t\t\t prepend, sc_append, swap_append,\t\t\\\n+\t\t\t __ret, __ptr, __new);\t\t\t\\\n \t\tbreak;\t\t\t\t\t\t\t\\\n \tdefault:\t\t\t\t\t\t\t\\\n \t\tBUILD_BUG();\t\t\t\t\t\t\\\ndiff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h\nindex 5efa72823475fd..92cab4a7a0f127 100644\n--- a/arch/riscv/include/asm/cpufeature.h\n+++ b/arch/riscv/include/asm/cpufeature.h\n@@ -83,6 +83,10 @@ static inline bool unaligned_ctl_available(void)\n }\n #endif\n \n+#ifdef CONFIG_RISCV_PROBE_AMO_SPEED\n+DECLARE_PER_CPU(long, amo_speed);\n+#endif\n+\n #if defined(CONFIG_RISCV_MISALIGNED)\n DECLARE_PER_CPU(long, misaligned_access_speed);\n bool misaligned_traps_can_delegate(void);\ndiff --git a/arch/riscv/include/asm/futex.h b/arch/riscv/include/asm/futex.h\nindex 6f3bc9bdb85e39..e79d28af55cdfa 100644\n--- a/arch/riscv/include/asm/futex.h\n+++ b/arch/riscv/include/asm/futex.h\n@@ -12,6 +12,7 @@\n #include \u003clinux/errno.h\u003e\n #include \u003casm/asm.h\u003e\n #include \u003casm/asm-extable.h\u003e\n+#include \u003casm/fence.h\u003e\n \n /* We don't even really need the extable code, but for now keep it simple */\n #ifndef CONFIG_MMU\n@@ -19,6 +20,7 @@\n #define __disable_user_access()\t\tdo { } while (0)\n #endif\n \n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\t\\\n {\t\t\t\t\t\t\t\t\\\n \t__enable_user_access();\t\t\t\t\t\\\n@@ -32,6 +34,39 @@\n \t: \"memory\");\t\t\t\t\t\t\\\n \t__disable_user_access();\t\t\t\t\\\n }\n+#else\n+/*\n+ * Bound the number of LR/SC retries, as arm64 does for LL/SC: a user can\n+ * keep the word changing from another thread forever. futex_wake_op()\n+ * retries on -EAGAIN after dropping the hash bucket locks.\n+ */\n+#define FUTEX_MAX_LOOPS\t128\n+\n+#define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\t\\\n+{\t\t\t\t\t\t\t\t\\\n+\tint temp, loops = FUTEX_MAX_LOOPS;\t\t\t\\\n+\t\t\t\t\t\t\t\t\\\n+\t__enable_user_access();\t\t\t\t\t\\\n+\t__asm__ __volatile__ (\t\t\t\t\t\\\n+\t\"1:\tlr.w %[ov], %[u]\t\t\t\\n\"\t\\\n+\t\"\t\" insn \"\t\t\t\t\\n\"\t\\\n+\t\"2:\tsc.w.rl %[t], %[t], %[u]\t\t\\n\"\t\\\n+\t\"\tbeqz %[t], 3f\t\t\t\t\\n\"\t\\\n+\t\"\taddi %[l], %[l], -1\t\t\t\\n\"\t\\\n+\t\"\tbnez %[l], 1b\t\t\t\t\\n\"\t\\\n+\t\"\tli %[r], %[ea]\t\t\t\t\\n\"\t\\\n+\t\"3:\t\t\t\t\t\t\\n\"\t\\\n+\tRISCV_FULL_BARRIER\t\t\t\t\t\\\n+\t_ASM_EXTABLE_UACCESS_ERR(1b, 3b, %[r])\t\t\t\\\n+\t_ASM_EXTABLE_UACCESS_ERR(2b, 3b, %[r])\t\t\t\\\n+\t: [r] \"+r\" (ret), [ov] \"=\u0026r\" (oldval),\t\t\t\\\n+\t [t] \"=\u0026r\" (temp), [l] \"+\u0026r\" (loops),\t\t\t\\\n+\t [u] \"+m\" (*(uaddr))\t\t\t\t\t\\\n+\t: [op] \"Jr\" (oparg), [ea] \"I\" (-EAGAIN)\t\t\t\\\n+\t: \"memory\");\t\t\t\t\t\t\\\n+\t__disable_user_access();\t\t\t\t\\\n+}\n+#endif\n \n static inline int\n arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)\n@@ -43,6 +78,7 @@ arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)\n \tuaddr = untagged_addr(uaddr);\n \n \tswitch (op) {\n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n \tcase FUTEX_OP_SET:\n \t\t__futex_atomic_op(\"amoswap.w.aqrl %[ov],%z[op],%[u]\",\n \t\t\t\t ret, oldval, uaddr, oparg);\n@@ -63,6 +99,28 @@ arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)\n \t\t__futex_atomic_op(\"amoxor.w.aqrl %[ov],%z[op],%[u]\",\n \t\t\t\t ret, oldval, uaddr, oparg);\n \t\tbreak;\n+#else\n+\tcase FUTEX_OP_SET:\n+\t\t__futex_atomic_op(\"mv %[t], %z[op]\",\n+\t\t\t\t ret, oldval, uaddr, oparg);\n+\t\tbreak;\n+\tcase FUTEX_OP_ADD:\n+\t\t__futex_atomic_op(\"add %[t], %[ov], %z[op]\",\n+\t\t\t\t ret, oldval, uaddr, oparg);\n+\t\tbreak;\n+\tcase FUTEX_OP_OR:\n+\t\t__futex_atomic_op(\"or %[t], %[ov], %z[op]\",\n+\t\t\t\t ret, oldval, uaddr, oparg);\n+\t\tbreak;\n+\tcase FUTEX_OP_ANDN:\n+\t\t__futex_atomic_op(\"and %[t], %[ov], %z[op]\",\n+\t\t\t\t ret, oldval, uaddr, ~oparg);\n+\t\tbreak;\n+\tcase FUTEX_OP_XOR:\n+\t\t__futex_atomic_op(\"xor %[t], %[ov], %z[op]\",\n+\t\t\t\t ret, oldval, uaddr, oparg);\n+\t\tbreak;\n+#endif\n \tdefault:\n \t\tret = -ENOSYS;\n \t}\ndiff --git a/arch/riscv/include/asm/hwprobe.h b/arch/riscv/include/asm/hwprobe.h\nindex 9b04377c0f98fa..a7eb17c8180fe4 100644\n--- a/arch/riscv/include/asm/hwprobe.h\n+++ b/arch/riscv/include/asm/hwprobe.h\n@@ -8,7 +8,7 @@\n \n #include \u003cuapi/asm/hwprobe.h\u003e\n \n-#define RISCV_HWPROBE_MAX_KEY\t\t16\n+#define RISCV_HWPROBE_MAX_KEY\t\t17\n \n static inline bool riscv_hwprobe_key_is_valid(__s64 key)\n {\n@@ -43,4 +43,10 @@ static inline bool riscv_hwprobe_pair_cmp(struct riscv_hwprobe *pair,\n \treturn pair-\u003evalue == other_pair-\u003evalue;\n }\n \n+#ifdef CONFIG_MMU\n+void riscv_hwprobe_update_vdso_data(void);\n+#else\n+static inline void riscv_hwprobe_update_vdso_data(void) {}\n+#endif\n+\n #endif\ndiff --git a/arch/riscv/include/asm/insn-def.h b/arch/riscv/include/asm/insn-def.h\nindex 8fcc848c196e00..5ca5c0b8ad66c4 100644\n--- a/arch/riscv/include/asm/insn-def.h\n+++ b/arch/riscv/include/asm/insn-def.h\n@@ -219,6 +219,11 @@ ALTERNATIVE(\"nop\", \"fence rw, rw\", SIFIVE_VENDOR_ID,\t\t\t\\\n \t__ASM_STR(.error \"hlv.d requires 64-bit support\")\n #endif\n \n+/* Spelled out so that it assembles when -march lacks Zaamo */\n+#define AMOADD_W(dest, addr, val)\t\t\t\t\\\n+\tINSN_R(OPCODE_AMO, FUNC3(2), FUNC7(0),\t\t\t\\\n+\t RD(dest), RS1(addr), RS2(val))\n+\n #define LB_AQ(dest, addr)\t\t\t\t\t\\\n \tINSN_R(OPCODE_AMO, FUNC3(0), FUNC7(26),\t\t\t\\\n \t RD(dest), RS1(addr), __RS2(0))\ndiff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h\nindex e6a394fbe7fce9..a5f726f94204cf 100644\n--- a/arch/riscv/include/uapi/asm/hwprobe.h\n+++ b/arch/riscv/include/uapi/asm/hwprobe.h\n@@ -122,6 +122,11 @@ struct riscv_hwprobe {\n #define\t\tRISCV_HWPROBE_EXT_ZICCIF\t_BITULL(3)\n #define\t\tRISCV_HWPROBE_EXT_ZICCRSE\t_BITULL(4)\n #define\t\tRISCV_HWPROBE_EXT_ZA64RS\t_BITULL(5)\n+#define RISCV_HWPROBE_KEY_AMO_PERF\t17\n+#define\t\tRISCV_HWPROBE_AMO_UNKNOWN\t0\n+#define\t\tRISCV_HWPROBE_AMO_SLOW\t\t1\n+#define\t\tRISCV_HWPROBE_AMO_FAST\t\t2\n+#define\t\tRISCV_HWPROBE_AMO_UNSUPPORTED\t3\n \n /* Increase RISCV_HWPROBE_MAX_KEY when adding items. */\n \ndiff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile\nindex ebe1c3588177b4..fc935c877e8283 100644\n--- a/arch/riscv/kernel/Makefile\n+++ b/arch/riscv/kernel/Makefile\n@@ -79,6 +79,7 @@ obj-$(CONFIG_RISCV_MISALIGNED)\t+= traps_misaligned.o\n obj-$(CONFIG_RISCV_MISALIGNED)\t+= unaligned_access_speed.o\n obj-$(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)\t\t+= copy-unaligned.o\n obj-$(CONFIG_RISCV_PROBE_VECTOR_UNALIGNED_ACCESS)\t+= vec-copy-unaligned.o\n+obj-$(CONFIG_RISCV_PROBE_AMO_SPEED)\t+= amo_speed.o\n \n obj-$(CONFIG_FPU)\t\t+= fpu.o\n obj-$(CONFIG_FPU)\t\t+= kernel_mode_fpu.o\ndiff --git a/arch/riscv/kernel/amo_speed.c b/arch/riscv/kernel/amo_speed.c\nnew file mode 100644\nindex 00000000000000..8cd66941c3a61a\n--- /dev/null\n+++ b/arch/riscv/kernel/amo_speed.c\n@@ -0,0 +1,183 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/*\n+ * Measure whether AMO instructions are faster than the equivalent LR/SC\n+ * sequence. They are not on a CPU that traps them into firmware and\n+ * emulates them there, such as MIPS P8700, where an AMO costs hundreds of\n+ * cycles and LR/SC tens. The result goes to userspace through hwprobe,\n+ * so code that can choose between the two knows which one to use.\n+ */\n+\n+#include \u003clinux/cpu.h\u003e\n+#include \u003clinux/smp.h\u003e\n+#include \u003clinux/timekeeping.h\u003e\n+#include \u003casm/cpufeature.h\u003e\n+#include \u003casm/hwprobe.h\u003e\n+#include \u003casm/insn-def.h\u003e\n+#include \u003casm/timex.h\u003e\n+\n+/* Time window for each measurement, and atomics per sample within it */\n+#define AMO_SPEED_NS\t\t8000000\n+#define AMO_SPEED_ITERS\t\t1024\n+\n+DEFINE_PER_CPU(long, amo_speed) = RISCV_HWPROBE_AMO_UNKNOWN;\n+\n+static DEFINE_PER_CPU_ALIGNED(u32, amo_speed_word);\n+static long amo_speed_param = RISCV_HWPROBE_AMO_UNKNOWN;\n+\n+static void amo_speed_amo(u32 *word, unsigned long n)\n+{\n+\tunsigned long tmp;\n+\n+\tasm volatile(\n+\t\t\"1:\\n\"\n+\t\tAMOADD_W(%[tmp], %[word], %[one])\n+\t\t\"\taddi %[n], %[n], -1\\n\"\n+\t\t\"\tbnez %[n], 1b\\n\"\n+\t\t: [tmp] \"=\u0026r\" (tmp), [n] \"+\u0026r\" (n), \"+m\" (*word)\n+\t\t: [word] \"r\" (word), [one] \"r\" (1UL)\n+\t\t: \"memory\");\n+}\n+\n+static void amo_speed_lrsc(u32 *word, unsigned long n)\n+{\n+\tunsigned long tmp;\n+\n+\tasm volatile(\n+\t\t\"1:\tlr.w %[tmp], (%[word])\\n\"\n+\t\t\"\taddi %[tmp], %[tmp], 1\\n\"\n+\t\t\"\tsc.w %[tmp], %[tmp], (%[word])\\n\"\n+\t\t\"\tbnez %[tmp], 1b\\n\"\n+\t\t\"\taddi %[n], %[n], -1\\n\"\n+\t\t\"\tbnez %[n], 1b\\n\"\n+\t\t: [tmp] \"=\u0026r\" (tmp), [n] \"+\u0026r\" (n), \"+m\" (*word)\n+\t\t: [word] \"r\" (word)\n+\t\t: \"memory\");\n+}\n+\n+/*\n+ * For a fixed amount of time, repeatedly run func and take the best time\n+ * in cycles as the measurement.\n+ */\n+static u64 amo_speed_measure(void (*func)(u32 *word, unsigned long n),\n+\t\t\t u32 *word)\n+{\n+\tu64 start_cycles, end_cycles, cycles = -1ULL;\n+\tu64 start_ns;\n+\n+\t/* Do a warmup. */\n+\tfunc(word, AMO_SPEED_ITERS);\n+\n+\tpreempt_disable();\n+\n+\tstart_ns = ktime_get_mono_fast_ns();\n+\twhile (ktime_get_mono_fast_ns() \u003c start_ns + AMO_SPEED_NS) {\n+\t\tstart_cycles = get_cycles64();\n+\t\t/* Ensure the CSR read can't reorder WRT to the atomics. */\n+\t\tmb();\n+\t\tfunc(word, AMO_SPEED_ITERS);\n+\t\t/* Ensure the atomics end before the end time is snapped. */\n+\t\tmb();\n+\t\tend_cycles = get_cycles64();\n+\t\tif ((end_cycles - start_cycles) \u003c cycles)\n+\t\t\tcycles = end_cycles - start_cycles;\n+\t}\n+\n+\tpreempt_enable();\n+\n+\treturn cycles;\n+}\n+\n+/*\n+ * Settle the speed of one CPU: unsupported without Zaamo, the command\n+ * line value if one was given, otherwise measure. Runs on that CPU.\n+ */\n+static void check_amo_speed(void *unused)\n+{\n+\tint cpu = smp_processor_id();\n+\tu32 *word = this_cpu_ptr(\u0026amo_speed_word);\n+\tu64 amo_cycles, lrsc_cycles;\n+\tlong speed;\n+\tint ratio;\n+\n+\tif (per_cpu(amo_speed, cpu) != RISCV_HWPROBE_AMO_UNKNOWN)\n+\t\treturn;\n+\n+\tif (!riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_ZAAMO)) {\n+\t\tper_cpu(amo_speed, cpu) = RISCV_HWPROBE_AMO_UNSUPPORTED;\n+\t\treturn;\n+\t}\n+\n+\tif (amo_speed_param != RISCV_HWPROBE_AMO_UNKNOWN) {\n+\t\tper_cpu(amo_speed, cpu) = amo_speed_param;\n+\t\treturn;\n+\t}\n+\n+\tamo_cycles = amo_speed_measure(amo_speed_amo, word);\n+\tlrsc_cycles = amo_speed_measure(amo_speed_lrsc, word);\n+\n+\t/* Don't divide by zero. */\n+\tif (!amo_cycles || !lrsc_cycles) {\n+\t\tpr_warn(\"cpu%d: rdtime lacks granularity needed to measure AMO speed\\n\",\n+\t\t\tcpu);\n+\t\treturn;\n+\t}\n+\n+\tif (amo_cycles \u003c= lrsc_cycles)\n+\t\tspeed = RISCV_HWPROBE_AMO_FAST;\n+\telse\n+\t\tspeed = RISCV_HWPROBE_AMO_SLOW;\n+\n+\tratio = div64_u64(lrsc_cycles * 100, amo_cycles);\n+\tpr_info(\"cpu%d: AMO speed is %d.%02dx LR/SC speed (%s)\\n\",\n+\t\tcpu, ratio / 100, ratio % 100,\n+\t\tspeed == RISCV_HWPROBE_AMO_FAST ? \"fast\" : \"slow\");\n+\n+\tper_cpu(amo_speed, cpu) = speed;\n+\n+\tif (speed == RISCV_HWPROBE_AMO_SLOW \u0026\u0026 IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO))\n+\t\tpr_warn_once(\"AMO is slow but kernel atomics use it, consider disabling RISCV_ISA_ZAAMO\\n\");\n+}\n+\n+static int amo_speed_online_cpu(unsigned int cpu)\n+{\n+\tcheck_amo_speed(NULL);\n+\treturn 0;\n+}\n+\n+static int __init set_amo_speed_param(char *str)\n+{\n+\tif (!strcmp(str, \"slow\"))\n+\t\tamo_speed_param = RISCV_HWPROBE_AMO_SLOW;\n+\telse if (!strcmp(str, \"fast\"))\n+\t\tamo_speed_param = RISCV_HWPROBE_AMO_FAST;\n+\telse\n+\t\treturn 0;\n+\n+\treturn 1;\n+}\n+__setup(\"amo_speed=\", set_amo_speed_param);\n+\n+static int __init check_amo_speed_all_cpus(void)\n+{\n+\tif (amo_speed_param != RISCV_HWPROBE_AMO_UNKNOWN)\n+\t\tpr_info(\"AMO speed set to '%s' (%lu) by command line\\n\",\n+\t\t\tamo_speed_param == RISCV_HWPROBE_AMO_FAST ? \"fast\" : \"slow\",\n+\t\t\tamo_speed_param);\n+\n+\ton_each_cpu(check_amo_speed, NULL, 1);\n+\n+\tcpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, \"riscv:amo_speed\",\n+\t\t\t\t amo_speed_online_cpu, NULL);\n+\n+\t/* The vDSO was filled before the probe ran; give it the result. */\n+\triscv_hwprobe_update_vdso_data();\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * Same level as the unaligned access probe: after clocksource_done_booting()\n+ * so that amo_speed_measure() uses a stable clocksource, but before\n+ * rootfs_initcall() enables usermode helpers that can reach hwprobe.\n+ */\n+fs_initcall_sync(check_amo_speed_all_cpus);\ndiff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c\nindex 3dbc8cc557dd1d..9ba0d49c87bc3b 100644\n--- a/arch/riscv/kernel/cpu.c\n+++ b/arch/riscv/kernel/cpu.c\n@@ -82,12 +82,29 @@ int __init riscv_early_of_processor_hartid(struct device_node *node, unsigned lo\n \t\treturn -ENODEV;\n \n \tif (of_property_match_string(node, \"riscv,isa-extensions\", \"i\") \u003c 0 ||\n-\t of_property_match_string(node, \"riscv,isa-extensions\", \"m\") \u003c 0 ||\n-\t of_property_match_string(node, \"riscv,isa-extensions\", \"a\") \u003c 0) {\n-\t\tpr_warn(\"CPU with hartid=%lu does not support ima\", *hart);\n+\t of_property_match_string(node, \"riscv,isa-extensions\", \"m\") \u003c 0) {\n+\t\tpr_warn(\"CPU with hartid=%lu does not support im\", *hart);\n \t\treturn -ENODEV;\n \t}\n \n+\t/*\n+\t * Require the atomic extensions this kernel was built with. \"a\"\n+\t * covers both; otherwise LR/SC is always needed and AMO is needed\n+\t * when the kernel uses it.\n+\t */\n+\tif (of_property_match_string(node, \"riscv,isa-extensions\", \"a\") \u003c 0) {\n+\t\tif (of_property_match_string(node, \"riscv,isa-extensions\", \"zalrsc\") \u003c 0) {\n+\t\t\tpr_warn(\"CPU with hartid=%lu does not support zalrsc\", *hart);\n+\t\t\treturn -ENODEV;\n+\t\t}\n+\n+\t\tif (IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO) \u0026\u0026\n+\t\t of_property_match_string(node, \"riscv,isa-extensions\", \"zaamo\") \u003c 0) {\n+\t\t\tpr_warn(\"CPU with hartid=%lu does not support zaamo\", *hart);\n+\t\t\treturn -ENODEV;\n+\t\t}\n+\t}\n+\n \treturn 0;\n \n old_interface:\ndiff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c\nindex 7329792978cc0f..45684e129be191 100644\n--- a/arch/riscv/kernel/cpufeature.c\n+++ b/arch/riscv/kernel/cpufeature.c\n@@ -682,6 +682,15 @@ static void __init riscv_resolve_isa(unsigned long *source_isa,\n \tint max_loop_count = riscv_isa_ext_count, ret;\n \tunsigned int bit;\n \n+\t/*\n+\t * \"a\" is exactly Zaamo plus Zalrsc. A hart that lists both has A,\n+\t * however the firmware spelled it, so report it the same way in\n+\t * ELF hwcap and /proc/cpuinfo.\n+\t */\n+\tif (test_bit(RISCV_ISA_EXT_ZAAMO, source_isa) \u0026\u0026\n+\t test_bit(RISCV_ISA_EXT_ZALRSC, source_isa))\n+\t\tset_bit(RISCV_ISA_EXT_A, source_isa);\n+\n \tdo {\n \t\tloop = false;\n \t\tif (max_loop_count-- \u003c 0) {\ndiff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S\nindex 61b7a77166971d..ba2db6804a6c2c 100644\n--- a/arch/riscv/kernel/entry.S\n+++ b/arch/riscv/kernel/entry.S\n@@ -73,7 +73,14 @@\n \tbeq\ta2, zero, .Lnew_valid_map_cpus_restore_context\n \n \t/* Atomically reset the current cpu bit in new_valid_map_cpus */\n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n \tamoxor.d\ta0, a1, (a0)\n+#else\n+1:\tlr.d\ta2, (a0)\n+\txor\ta2, a1, a2\n+\tsc.d\ta2, a2, (a0)\n+\tbnez\ta2, 1b\n+#endif\n \n \t/*\n \t * A sfence.vma is required here. Even if we had Svvptc, there's no\ndiff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S\nindex f6a8ca49e6277c..dc3862ed29c40e 100644\n--- a/arch/riscv/kernel/head.S\n+++ b/arch/riscv/kernel/head.S\n@@ -265,7 +265,19 @@ SYM_CODE_START(_start_kernel)\n \t/* Pick one hart to run the main boot sequence */\n \tla a3, hart_lottery\n \tli a2, 1\n+#ifdef CONFIG_RISCV_ISA_ZAAMO\n \tamoadd.w a3, a2, (a3)\n+#else\n+\t/*\n+\t * hart_lottery is a small 32-bit counter. add is valid on RV32\n+\t * and RV64; sc.w stores the low 32 bits.\n+\t */\n+1:\tlr.w t0, (a3)\n+\tadd t1, t0, a2\n+\tsc.w t1, t1, (a3)\n+\tbnez t1, 1b\n+\tmv a3, t0\n+#endif\n \tbnez a3, .Lsecondary_start\n #endif /* CONFIG_RISCV_BOOT_SPINWAIT */\n \ndiff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c\nindex 7818e1d32622dd..32ce3ac8bc4a85 100644\n--- a/arch/riscv/kernel/sys_hwprobe.c\n+++ b/arch/riscv/kernel/sys_hwprobe.c\n@@ -213,6 +213,18 @@ static void hwprobe_isa_ext1(struct riscv_hwprobe *pair,\n \tpair-\u003evalue \u0026= ~missing;\n }\n \n+static bool hwprobe_all_cpus_have(const struct cpumask *cpus, unsigned long ext)\n+{\n+\tint cpu;\n+\n+\tfor_each_cpu(cpu, cpus) {\n+\t\tif (!__riscv_isa_extension_available(hart_isa[cpu].isa, ext))\n+\t\t\treturn false;\n+\t}\n+\n+\treturn true;\n+}\n+\n static bool hwprobe_ext0_has(const struct cpumask *cpus, u64 ext)\n {\n \tstruct riscv_hwprobe pair;\n@@ -257,6 +269,39 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)\n }\n #endif\n \n+#ifdef CONFIG_RISCV_PROBE_AMO_SPEED\n+static u64 hwprobe_amo(const struct cpumask *cpus)\n+{\n+\tint cpu;\n+\tu64 perf = -1ULL;\n+\n+\tfor_each_cpu(cpu, cpus) {\n+\t\tint this_perf = per_cpu(amo_speed, cpu);\n+\n+\t\tif (perf == -1ULL)\n+\t\t\tperf = this_perf;\n+\n+\t\tif (perf != this_perf) {\n+\t\t\tperf = RISCV_HWPROBE_AMO_UNKNOWN;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (perf == -1ULL)\n+\t\treturn RISCV_HWPROBE_AMO_UNKNOWN;\n+\n+\treturn perf;\n+}\n+#else\n+static u64 hwprobe_amo(const struct cpumask *cpus)\n+{\n+\tif (!hwprobe_ext0_has(cpus, RISCV_HWPROBE_EXT_ZAAMO))\n+\t\treturn RISCV_HWPROBE_AMO_UNSUPPORTED;\n+\n+\treturn RISCV_HWPROBE_AMO_UNKNOWN;\n+}\n+#endif\n+\n #ifdef CONFIG_RISCV_VECTOR_MISALIGNED\n static u64 hwprobe_vec_misaligned(const struct cpumask *cpus)\n {\n@@ -306,13 +351,14 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair,\n \t\thwprobe_arch_id(pair, cpus);\n \t\tbreak;\n \t/*\n-\t * The kernel already assumes that the base single-letter ISA\n-\t * extensions are supported on all harts, and only supports the\n-\t * IMA base, so just cheat a bit here and tell that to\n-\t * userspace.\n+\t * The kernel requires I and M on every hart, so IMA holds when\n+\t * every selected hart has A as well. A hart that lists only\n+\t * Zalrsc boots on a kernel built without Zaamo, but it has no\n+\t * AMO instructions and must not be reported as IMA.\n \t */\n \tcase RISCV_HWPROBE_KEY_BASE_BEHAVIOR:\n-\t\tpair-\u003evalue = RISCV_HWPROBE_BASE_BEHAVIOR_IMA;\n+\t\tif (hwprobe_all_cpus_have(cpus, RISCV_ISA_EXT_A))\n+\t\t\tpair-\u003evalue = RISCV_HWPROBE_BASE_BEHAVIOR_IMA;\n \t\tbreak;\n \n \tcase RISCV_HWPROBE_KEY_IMA_EXT_0:\n@@ -332,6 +378,10 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair,\n \t\tpair-\u003evalue = hwprobe_vec_misaligned(cpus);\n \t\tbreak;\n \n+\tcase RISCV_HWPROBE_KEY_AMO_PERF:\n+\t\tpair-\u003evalue = hwprobe_amo(cpus);\n+\t\tbreak;\n+\n \tcase RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE:\n \t\tif (hwprobe_ext0_has(cpus, RISCV_HWPROBE_EXT_ZICBOZ))\n \t\t\tpair-\u003evalue = riscv_cboz_block_size;\n@@ -521,17 +571,18 @@ static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,\n \n #ifdef CONFIG_MMU\n \n-static int __init init_hwprobe_vdso_data(void)\n+/*\n+ * Fill the vDSO data with the answers for the \"all CPUs\" case, to save a\n+ * syscall in the common case. Called once the ISA is known, and again by\n+ * the boot-time probes whose results feed some of the keys.\n+ */\n+void __init riscv_hwprobe_update_vdso_data(void)\n {\n \tstruct vdso_arch_data *avd = vdso_k_arch_data;\n \tu64 id_bitsmash = 0;\n \tstruct riscv_hwprobe pair;\n \tint key;\n \n-\t/*\n-\t * Initialize vDSO data with the answers for the \"all CPUs\" case, to\n-\t * save a syscall in the common case.\n-\t */\n \tfor (key = 0; key \u003c= RISCV_HWPROBE_MAX_KEY; key++) {\n \t\tpair.key = key;\n \t\thwprobe_one_pair(\u0026pair, cpu_online_mask);\n@@ -555,6 +606,11 @@ static int __init init_hwprobe_vdso_data(void)\n \t * vDSO should defer to the kernel for exotic cpu masks.\n \t */\n \tavd-\u003ehomogeneous_cpus = id_bitsmash != 0 \u0026\u0026 id_bitsmash != -1;\n+}\n+\n+static int __init init_hwprobe_vdso_data(void)\n+{\n+\triscv_hwprobe_update_vdso_data();\n \treturn 0;\n }\n \ndiff --git a/arch/riscv/kernel/unaligned_access_speed.c b/arch/riscv/kernel/unaligned_access_speed.c\nindex 67f1a03716597a..3a6f85e3f94c29 100644\n--- a/arch/riscv/kernel/unaligned_access_speed.c\n+++ b/arch/riscv/kernel/unaligned_access_speed.c\n@@ -393,13 +393,15 @@ static int __init check_unaligned_access_all_cpus(void)\n \tmodify_unaligned_access_branches(cpu_online_mask);\n \tcpus_read_unlock();\n \n+\t/* The vDSO was filled before the probes ran; give it the results. */\n+\triscv_hwprobe_update_vdso_data();\n+\n \treturn 0;\n }\n \n /*\n * Run after clocksource_done_booting() so measure_cycles() uses a stable\n- * clocksource, but before rootfs_initcall() enables usermode helpers. Those\n- * helpers can reach hwprobe and populate the vDSO cache, so async hwprobe\n- * probes must be registered first.\n+ * clocksource, but before rootfs_initcall() enables usermode helpers, which\n+ * can reach hwprobe and must see the results in the vDSO data.\n */\n fs_initcall_sync(check_unaligned_access_all_cpus);\ndiff --git a/arch/riscv/net/bpf_jit.h b/arch/riscv/net/bpf_jit.h\nindex 419b9d795f2a7a..8c1301289ea802 100644\n--- a/arch/riscv/net/bpf_jit.h\n+++ b/arch/riscv/net/bpf_jit.h\n@@ -79,6 +79,7 @@ struct rv_jit_context {\n \tint nexentries;\n \tint ex_insn_off;\n \tint ex_jmp_off;\n+\tint ex_sc_off;\t\t/* sc in an lr/sc pair; 0 if unused */\n \tunsigned long flags;\n \tint stack_size;\n \tint tcc_offset;\ndiff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp32.c\nindex a9e0bd5cc81d27..0fa0c013549acb 100644\n--- a/arch/riscv/net/bpf_jit_comp32.c\n+++ b/arch/riscv/net/bpf_jit_comp32.c\n@@ -874,11 +874,70 @@ static int emit_load_r64(const s8 *dst, const s8 *src, s16 off,\n \treturn 0;\n }\n \n-static int emit_bpf_atomic(s8 dst, const s8 *src, const s8 *rs,\n-\t\t\t struct rv_jit_context *ctx,\n-\t\t\t const struct bpf_insn *insn)\n+/*\n+ * Read-modify-write via lr/sc for kernels built without Zaamo. T0 holds\n+ * the address. T1/T2 are not BPF registers; the address was copied out of\n+ * lo(rd) before this is called, so clobbering T2 is fine. A fetch op\n+ * (including XCHG) is fully ordered as in asm/atomic.h: plain lr, sc.rl\n+ * and a trailing fence rw,rw.\n+ */\n+static int emit_bpf_atomic_lrsc(s8 dst, const s8 *rs, s32 imm,\n+\t\t\t\tstruct rv_jit_context *ctx)\n+{\n+\tbool is_fetch = (imm \u0026 BPF_FETCH) || (imm == BPF_XCHG);\n+\tbool xchg = imm == BPF_XCHG;\n+\tu8 rl = is_fetch ? 1 : 0;\n+\tu8 old = RV_REG_T1;\n+\tu8 tmp = RV_REG_T2;\n+\tint lr_off, jmp_off;\n+\ts32 op = xchg ? BPF_XCHG : imm \u0026 ~BPF_FETCH;\n+\n+\tswitch (op) {\n+\tcase BPF_ADD:\n+\tcase BPF_AND:\n+\tcase BPF_OR:\n+\tcase BPF_XOR:\n+\tcase BPF_XCHG:\n+\t\tbreak;\n+\tdefault:\n+\t\treturn -1;\n+\t}\n+\n+\tlr_off = ctx-\u003eninsns;\n+\temit(rv_lr_w(old, 0, dst, 0, 0), ctx);\n+\n+\tswitch (op) {\n+\tcase BPF_ADD:\n+\t\temit(rv_add(tmp, old, lo(rs)), ctx);\n+\t\tbreak;\n+\tcase BPF_AND:\n+\t\temit(rv_and(tmp, old, lo(rs)), ctx);\n+\t\tbreak;\n+\tcase BPF_OR:\n+\t\temit(rv_or(tmp, old, lo(rs)), ctx);\n+\t\tbreak;\n+\tcase BPF_XOR:\n+\t\temit(rv_xor(tmp, old, lo(rs)), ctx);\n+\t\tbreak;\n+\t}\n+\n+\tif (xchg)\n+\t\temit(rv_sc_w(tmp, lo(rs), dst, 0, rl), ctx);\n+\telse\n+\t\temit(rv_sc_w(tmp, tmp, dst, 0, rl), ctx);\n+\n+\tjmp_off = ninsns_rvoff(lr_off - ctx-\u003eninsns);\n+\temit(rv_bne(tmp, RV_REG_ZERO, jmp_off \u003e\u003e 1), ctx);\n+\tif (is_fetch) {\n+\t\temit_fence_rw_rw(ctx);\n+\t\temit(rv_addi(lo(rs), old, 0), ctx);\n+\t}\n+\treturn 0;\n+}\n+\n+static int emit_bpf_atomic_amo(s8 dst, const s8 *rs, s32 imm,\n+\t\t\t struct rv_jit_context *ctx)\n {\n-\ts32 imm = insn-\u003eimm;\n \tbool is_fetch = (imm \u0026 BPF_FETCH) || (imm == BPF_XCHG);\n \ts8 fetch_reg = is_fetch ? lo(rs) : RV_REG_ZERO;\n \tint aq = is_fetch ? 1 : 0;\n@@ -907,6 +966,23 @@ static int emit_bpf_atomic(s8 dst, const s8 *src, const s8 *rs,\n \tdefault:\n \t\treturn -1;\n \t}\n+\treturn 0;\n+}\n+\n+static int emit_bpf_atomic(s8 dst, const s8 *src, const s8 *rs,\n+\t\t\t struct rv_jit_context *ctx,\n+\t\t\t const struct bpf_insn *insn)\n+{\n+\ts32 imm = insn-\u003eimm;\n+\tbool is_fetch = (imm \u0026 BPF_FETCH) || (imm == BPF_XCHG);\n+\tint ret;\n+\n+\tif (IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO))\n+\t\tret = emit_bpf_atomic_amo(dst, rs, imm, ctx);\n+\telse\n+\t\tret = emit_bpf_atomic_lrsc(dst, rs, imm, ctx);\n+\tif (ret)\n+\t\treturn ret;\n \n \tif (is_fetch) {\n \t\temit(rv_addi(hi(rs), RV_REG_ZERO, 0), ctx);\ndiff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c\nindex 01fe66774f029f..4807b5800f6285 100644\n--- a/arch/riscv/net/bpf_jit_comp64.c\n+++ b/arch/riscv/net/bpf_jit_comp64.c\n@@ -573,6 +573,80 @@ static int emit_atomic_ld_st(u8 rd, u8 rs, const struct bpf_insn *insn,\n \treturn 0;\n }\n \n+/*\n+ * Read-modify-write via lr/sc for kernels built without Zaamo, matching\n+ * the Zalrsc sequences in arch/riscv/include/asm/atomic.h. A relaxed op\n+ * discards the old value. A fetch op (including XCHG) is fully ordered\n+ * the way emit_cmpxchg() is: plain lr, sc.rl and a trailing fence rw,rw.\n+ * It writes the old value back to val. T2/T3 are not BPF registers; addr\n+ * may already be T1.\n+ */\n+static void emit_atomic_lrsc(u8 addr, u8 val, bool is64, s32 imm,\n+\t\t\t struct rv_jit_context *ctx)\n+{\n+\tbool fetch = imm \u0026 BPF_FETCH;\n+\tu8 rl = fetch ? 1 : 0;\n+\tu8 old = RV_REG_T2;\n+\tu8 tmp = RV_REG_T3;\n+\tint lr_off, jmp_off;\n+\ts32 op = imm == BPF_XCHG ? BPF_XCHG : imm \u0026 ~BPF_FETCH;\n+\n+\tlr_off = ctx-\u003eninsns;\n+\tctx-\u003eex_insn_off = ctx-\u003eninsns;\n+\temit(is64 ? rv_lr_d(old, 0, addr, 0, 0) :\n+\t rv_lr_w(old, 0, addr, 0, 0), ctx);\n+\n+\tif (op != BPF_XCHG) {\n+\t\tif (!is64 \u0026\u0026 op == BPF_ADD)\n+\t\t\temit(rv_addw(tmp, old, val), ctx);\n+\t\telse if (op == BPF_ADD)\n+\t\t\temit_add(tmp, old, val, ctx);\n+\t\telse if (op == BPF_AND)\n+\t\t\temit_and(tmp, old, val, ctx);\n+\t\telse if (op == BPF_OR)\n+\t\t\temit_or(tmp, old, val, ctx);\n+\t\telse if (op == BPF_XOR)\n+\t\t\temit_xor(tmp, old, val, ctx);\n+\n+\t\tctx-\u003eex_sc_off = ctx-\u003eninsns;\n+\t\temit(is64 ? rv_sc_d(tmp, tmp, addr, 0, rl) :\n+\t\t rv_sc_w(tmp, tmp, addr, 0, rl), ctx);\n+\t} else {\n+\t\tctx-\u003eex_sc_off = ctx-\u003eninsns;\n+\t\temit(is64 ? rv_sc_d(tmp, val, addr, 0, rl) :\n+\t\t rv_sc_w(tmp, val, addr, 0, rl), ctx);\n+\t}\n+\n+\t/* Branch offset is relative to this instruction, back to the lr. */\n+\tjmp_off = ninsns_rvoff(lr_off - ctx-\u003eninsns);\n+\temit(rv_bne(tmp, RV_REG_ZERO, jmp_off \u003e\u003e 1), ctx);\n+\n+\tif (fetch) {\n+\t\temit_fence_rw_rw(ctx);\n+\t\temit_mv(val, old, ctx);\n+\t\tif (!is64)\n+\t\t\temit_zextw(val, val, ctx);\n+\t}\n+\tctx-\u003eex_jmp_off = ctx-\u003eninsns;\n+}\n+\n+static bool is_atomic_lrsc_op(s32 imm)\n+{\n+\tswitch (imm) {\n+\tcase BPF_ADD:\n+\tcase BPF_AND:\n+\tcase BPF_OR:\n+\tcase BPF_XOR:\n+\tcase BPF_ADD | BPF_FETCH:\n+\tcase BPF_AND | BPF_FETCH:\n+\tcase BPF_OR | BPF_FETCH:\n+\tcase BPF_XOR | BPF_FETCH:\n+\tcase BPF_XCHG:\n+\t\treturn true;\n+\t}\n+\treturn false;\n+}\n+\n static int emit_atomic_rmw(u8 rd, u8 rs, const struct bpf_insn *insn,\n \t\t\t struct rv_jit_context *ctx)\n {\n@@ -601,6 +675,12 @@ static int emit_atomic_rmw(u8 rd, u8 rs, const struct bpf_insn *insn,\n \t\trd = RV_REG_T1;\n \t}\n \n+\t/* Without Zaamo, every op except cmpxchg is an lr/sc loop. */\n+\tif (!IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO) \u0026\u0026 is_atomic_lrsc_op(imm)) {\n+\t\temit_atomic_lrsc(rd, rs, is64, imm, ctx);\n+\t\treturn 0;\n+\t}\n+\n \tswitch (imm) {\n \t/* lock *(u32/u64 *)(dst_reg + off16) \u003cop\u003e= src_reg */\n \tcase BPF_ADD:\n@@ -732,8 +812,8 @@ bool ex_handler_bpf(const struct exception_table_entry *ex,\n }\n \n /* For accesses to BTF pointers, add an entry to the exception table */\n-static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,\n-\t\t\t\t struct rv_jit_context *ctx)\n+static int add_exception_handler_one(const struct bpf_insn *insn, int dst_reg,\n+\t\t\t\t struct rv_jit_context *ctx)\n {\n \tstruct exception_table_entry *ex;\n \tunsigned long pc;\n@@ -804,6 +884,31 @@ static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,\n \treturn 0;\n }\n \n+/*\n+ * An lr/sc pair faults on either instruction. Both entries share the fixup\n+ * that follows the loop, so a probe fault skips the retry and the result move.\n+ */\n+static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,\n+\t\t\t\t struct rv_jit_context *ctx)\n+{\n+\tint sc_off = ctx-\u003eex_sc_off;\n+\tint jmp_off = ctx-\u003eex_jmp_off;\n+\tint ret;\n+\n+\tctx-\u003eex_sc_off = 0;\n+\t/* Offset discovery runs before the image exists. Entries are added later. */\n+\tif (!ctx-\u003einsns)\n+\t\treturn 0;\n+\n+\tret = add_exception_handler_one(insn, dst_reg, ctx);\n+\tif (ret || sc_off \u003c= 0)\n+\t\treturn ret;\n+\n+\tctx-\u003eex_insn_off = sc_off;\n+\tctx-\u003eex_jmp_off = jmp_off;\n+\treturn add_exception_handler_one(insn, dst_reg, ctx);\n+}\n+\n static int gen_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)\n {\n \ts64 rvoff;\ndiff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c\nindex cbfcd287ea1668..cba0d34c3e6fa0 100644\n--- a/arch/riscv/net/bpf_jit_core.c\n+++ b/arch/riscv/net/bpf_jit_core.c\n@@ -16,6 +16,34 @@\n /* Number of iterations to try until offsets converge. */\n #define NR_JIT_ITERATIONS\t32\n \n+/*\n+ * The verifier reserves one extable slot per BPF_PROBE_ATOMIC. Without\n+ * Zaamo a read-modify-write atomic is an lr/sc loop that can fault on\n+ * either instruction, so reserve a second slot for the sc.\n+ */\n+static void rv_jit_add_lrsc_exentries(struct bpf_prog *prog)\n+{\n+\tconst struct bpf_insn *insn = prog-\u003einsnsi;\n+\tint i;\n+\n+\tif (IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO) || __riscv_xlen != 64)\n+\t\treturn;\n+\n+\tfor (i = 0; i \u003c prog-\u003elen; i++, insn++) {\n+\t\tif (BPF_CLASS(insn-\u003ecode) != BPF_STX ||\n+\t\t BPF_MODE(insn-\u003ecode) != BPF_PROBE_ATOMIC ||\n+\t\t bpf_atomic_is_load_store(insn))\n+\t\t\tcontinue;\n+\t\tif (BPF_SIZE(insn-\u003ecode) != BPF_W \u0026\u0026\n+\t\t BPF_SIZE(insn-\u003ecode) != BPF_DW)\n+\t\t\tcontinue;\n+\t\t/* amocas is a single faulting instruction. */\n+\t\tif (insn-\u003eimm == BPF_CMPXCHG \u0026\u0026 rv_ext_enabled(ZACAS))\n+\t\t\tcontinue;\n+\t\tprog-\u003eaux-\u003enum_exentries++;\n+\t}\n+}\n+\n static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset)\n {\n \tconst struct bpf_prog *prog = ctx-\u003eprog;\n@@ -100,6 +128,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr\n \t\tif (ctx-\u003eninsns == prev_ninsns) {\n \t\t\tif (jit_data-\u003eheader)\n \t\t\t\tbreak;\n+\t\t\trv_jit_add_lrsc_exentries(prog);\n \t\t\t/* obtain the actual image size */\n \t\t\textable_size = prog-\u003eaux-\u003enum_exentries *\n \t\t\t\tsizeof(struct exception_table_entry);\n"
} |
| 2/1 | 2026/11/22 21:08 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": null,
"FocusSymbols": [],
"Reasoning": "All changes in this patch series are either documentation updates or code exclusive to the RISC-V architecture (arch/riscv/). Because the target architecture is amd64, none of the modified executable code can be compiled or executed during fuzzing.",
"WorthFuzzing": false
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 83842795ec1d4b3e01626923b7662826360ed224
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 22 21:08:00 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index e75344f4e0cdef..9bc5baff20ff0f 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -513,6 +513,13 @@ Kernel parameters
Format: <a>,<b>
See also Documentation/input/joydev/joystick.rst
+ amo_speed= [RISCV]
+ Format: {slow | fast}
+ Allow skipping the AMO instruction speed test. This is
+ useful for testing alternative code paths and to skip
+ the test in environments where it runs too slowly. All
+ CPUs must have the same AMO speed.
+
analog.map= [HW,JOY] Analog joystick and gamepad support
Specifies type or capabilities of an analog joystick
connected to one of 16 gameports
diff --git a/Documentation/arch/riscv/hwprobe.rst b/Documentation/arch/riscv/hwprobe.rst
index bb1e0cbab36f08..84aef6a0a79c22 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
@@ -423,3 +425,21 @@ The following keys are defined:
* :c:macro:`RISCV_HWPROBE_EXT_ZA64RS`: The Za64rs extension is supported,
as defined in the RISC-V Profiles specification starting from commit
b1d80660 ("Updated to ratified state.")
+
+* :c:macro:`RISCV_HWPROBE_KEY_AMO_PERF`: An enum value describing the
+ performance of the AMO instructions of the Zaamo extension, relative to
+ the equivalent LR/SC sequence, on the selected set of processors.
+
+ * :c:macro:`RISCV_HWPROBE_AMO_UNKNOWN`: The performance of AMO instructions
+ is unknown.
+
+ * :c:macro:`RISCV_HWPROBE_AMO_SLOW`: An uncontended AMO instruction takes
+ longer than the equivalent LR/SC sequence. This is the case when AMO
+ instructions are trapped and emulated by firmware. Code that can choose
+ should use LR/SC.
+
+ * :c:macro:`RISCV_HWPROBE_AMO_FAST`: An uncontended AMO instruction is at
+ least as fast as the equivalent LR/SC sequence.
+
+ * :c:macro:`RISCV_HWPROBE_AMO_UNSUPPORTED`: AMO instructions are not
+ supported, the Zaamo extension is absent.
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 5874ca30f4f6d3..dfd6237f879b83 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -237,6 +237,8 @@ config RISCV
config RUSTC_SUPPORTS_RISCV
def_bool y
depends on 64BIT
+ # The riscv64imac target emits AMO instructions
+ depends on RISCV_ISA_ZAAMO
config CLANG_SUPPORTS_DYNAMIC_FTRACE
def_bool CC_IS_CLANG
@@ -706,6 +708,7 @@ config RISCV_ISA_ZABHA
bool "Zabha extension support for atomic byte/halfword operations"
depends on TOOLCHAIN_HAS_ZABHA
depends on RISCV_ALTERNATIVE
+ depends on RISCV_ISA_ZAAMO
default y
help
Enable the use of the Zabha ISA-extension to implement kernel
@@ -723,6 +726,7 @@ config TOOLCHAIN_HAS_ZACAS
config RISCV_ISA_ZACAS
bool "Zacas extension support for atomic CAS"
depends on RISCV_ALTERNATIVE
+ depends on RISCV_ISA_ZAAMO
default y
help
Enable the use of the Zacas ISA-extension to implement kernel atomic
@@ -730,6 +734,49 @@ config RISCV_ISA_ZACAS
If you don't know what to do here, say Y.
+config TOOLCHAIN_HAS_ZALRSC_ZAAMO
+ bool
+ default y
+ depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64im_zaamo_zalrsc)
+ depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32im_zaamo_zalrsc)
+
+config RISCV_ISA_ZAAMO
+ bool "Zaamo extension support for AMO atomics" if TOOLCHAIN_HAS_ZALRSC_ZAAMO
+ 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 for a CPU where AMO is much slower than LR/SC. That
+ is the case when AMO instructions are emulated by a trap into
+ M-mode, as on the MIPS P8700: an emulated atomic costs hundreds
+ of cycles where LR/SC costs tens.
+
+ A kernel built without Zaamo runs on any CPU that implements the
+ A extension. Zabha, Zacas, userspace control flow integrity and
+ Rust need Zaamo and are unavailable without it.
+
+ The compiler must accept _zaamo and _zalrsc in -march to make
+ this choice. GCC 15.1 is the first release that does. With an
+ older compiler the option stays enabled and -march keeps the A
+ extension.
+
+ If you don't know what to do here, say Y.
+
+config RISCV_PROBE_AMO_SPEED
+ bool "Probe AMO instruction speed at boot"
+ default y
+ help
+ During boot, time an uncontended AMO instruction against the
+ equivalent LR/SC sequence on every CPU, and report which one is
+ faster to userspace through the hwprobe syscall, as
+ RISCV_HWPROBE_KEY_AMO_PERF. On a CPU that traps AMO instructions
+ into firmware and emulates them there, LR/SC is much faster, and
+ code that can choose should use it.
+
+ If you don't know what to do here, say Y.
+
config TOOLCHAIN_HAS_ZBB
bool
default y
@@ -1206,6 +1253,8 @@ config RISCV_USER_CFI
depends on 64BIT && MMU && \
$(cc-option,-mabi=lp64 -march=rv64ima_zicfiss_zicfilp -fcf-protection=full)
depends on RISCV_ALTERNATIVE
+ # ssamoswap is an AMO instruction
+ depends on RISCV_ISA_ZAAMO
select RISCV_SBI
select ARCH_HAS_USER_SHADOW_STACK
select ARCH_USES_HIGH_VMA_FLAGS
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 44f5a439741a91..59c5123c5485fa 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -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_ZAAMO) := $(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
+# Without Zaamo only the LR/SC half of "a" is used. The compiler accepts
+# _zalrsc here, see TOOLCHAIN_HAS_ZALRSC_ZAAMO.
+ifneq ($(CONFIG_RISCV_ISA_ZAAMO),y)
+riscv-march-y := $(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
diff --git a/arch/riscv/include/asm/atomic.h b/arch/riscv/include/asm/atomic.h
index 616b8b332ac56c..cca1fc3a68ffcd 100644
--- a/arch/riscv/include/asm/atomic.h
+++ b/arch/riscv/include/asm/atomic.h
@@ -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.
*/
+#ifdef CONFIG_RISCV_ISA_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,23 @@ void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
: "+A" (v->counter) \
: "r" (I) \
: "memory"); \
-} \
+}
+#else
+#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"); \
+}
+#endif
#ifdef CONFIG_GENERIC_ATOMIC64
#define ATOMIC_OPS(op, asm_op, I) \
@@ -84,6 +101,7 @@ ATOMIC_OPS(xor, xor, i)
* There's two flavors of these: the arithmetic ops have both fetch and return
* versions, while the logical ops only have fetch versions.
*/
+#ifdef CONFIG_RISCV_ISA_ZAAMO
#define ATOMIC_FETCH_OP(op, asm_op, I, asm_type, c_type, prefix) \
static __always_inline \
c_type arch_atomic##prefix##_fetch_##op##_relaxed(c_type i, \
@@ -108,6 +126,39 @@ c_type arch_atomic##prefix##_fetch_##op(c_type i, atomic##prefix##_t *v) \
: "memory"); \
return ret; \
}
+#else
+#define ATOMIC_FETCH_OP(op, asm_op, I, asm_type, c_type, prefix) \
+static __always_inline \
+c_type arch_atomic##prefix##_fetch_##op##_relaxed(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"); \
+ return ret; \
+} \
+static __always_inline \
+c_type arch_atomic##prefix##_fetch_##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 ".rl %2, %2, %0\n" \
+ " bnez %2, 1b\n" \
+ RISCV_FULL_BARRIER \
+ : "+A" (v->counter), "=&r" (ret), "=&r" (temp) \
+ : "r" (I) \
+ : "memory"); \
+ return ret; \
+}
+#endif
#define ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_type, c_type, prefix) \
static __always_inline \
diff --git a/arch/riscv/include/asm/bitops.h b/arch/riscv/include/asm/bitops.h
index 3c1a15be54d804..adcd7052577468 100644
--- a/arch/riscv/include/asm/bitops.h
+++ b/arch/riscv/include/asm/bitops.h
@@ -170,12 +170,17 @@ static __always_inline int variable_fls(unsigned int x)
#if (BITS_PER_LONG == 64)
#define __AMO(op) "amo" #op ".d"
+#define __LR "lr.d"
+#define __SC "sc.d"
#elif (BITS_PER_LONG == 32)
#define __AMO(op) "amo" #op ".w"
+#define __LR "lr.w"
+#define __SC "sc.w"
#else
#error "Unexpected BITS_PER_LONG"
#endif
+#ifdef CONFIG_RISCV_ISA_ZAAMO
#define __test_and_op_bit_ord(op, mod, nr, addr, ord) \
({ \
unsigned long __res, __mask; \
@@ -195,10 +200,58 @@ static __always_inline int variable_fls(unsigned int x)
: "r" (mod(BIT_MASK(nr))) \
: "memory");
-#define __test_and_op_bit(op, mod, nr, addr) \
+#define __test_and_op_bit(op, mod, nr, addr) \
__test_and_op_bit_ord(op, mod, nr, addr, .aqrl)
+#define __test_and_op_bit_acquire(op, mod, nr, addr) \
+ __test_and_op_bit_ord(op, mod, nr, addr, .aq)
#define __op_bit(op, mod, nr, addr) \
__op_bit_ord(op, mod, nr, addr, )
+#define __op_bit_release(op, mod, nr, addr) \
+ __op_bit_ord(op, mod, nr, addr, .rl)
+#else
+/*
+ * Fully ordered LR/SC is lr, sc.rl and a full fence; acquire is lr.aq and
+ * release is sc.rl, with no ordering on the other instruction. This is how
+ * the LR/SC loops in asm/atomic.h and asm/cmpxchg.h are built.
+ */
+#define __test_and_op_bit_lrsc(op, mod, nr, addr, lr_sfx, sc_sfx, append) \
+({ \
+ unsigned long __res, __mask, __temp; \
+ __mask = BIT_MASK(nr); \
+ __asm__ __volatile__ ( \
+ "1: " __LR lr_sfx " %0, %1\n" \
+ #op " %2, %0, %3\n" \
+ __SC sc_sfx " %2, %2, %1\n" \
+ "bnez %2, 1b\n" \
+ append \
+ : "=&r" (__res), "+A" (addr[BIT_WORD(nr)]), "=&r" (__temp) \
+ : "r" (mod(__mask)) \
+ : "memory"); \
+ ((__res & __mask) != 0); \
+})
+
+#define __op_bit_lrsc(op, mod, nr, addr, lr_sfx, sc_sfx) \
+do { \
+ unsigned long __res, __temp; \
+ __asm__ __volatile__ ( \
+ "1: " __LR lr_sfx " %0, %1\n" \
+ #op " %2, %0, %3\n" \
+ __SC sc_sfx " %2, %2, %1\n" \
+ "bnez %2, 1b\n" \
+ : "=&r" (__res), "+A" (addr[BIT_WORD(nr)]), "=&r" (__temp) \
+ : "r" (mod(BIT_MASK(nr))) \
+ : "memory"); \
+} while (0)
+
+#define __test_and_op_bit(op, mod, nr, addr) \
+ __test_and_op_bit_lrsc(op, mod, nr, addr, "", ".rl", RISCV_FULL_BARRIER)
+#define __test_and_op_bit_acquire(op, mod, nr, addr) \
+ __test_and_op_bit_lrsc(op, mod, nr, addr, ".aq", "", "")
+#define __op_bit(op, mod, nr, addr) \
+ __op_bit_lrsc(op, mod, nr, addr, "", "")
+#define __op_bit_release(op, mod, nr, addr) \
+ __op_bit_lrsc(op, mod, nr, addr, "", ".rl")
+#endif
/* Bitmask modifiers */
#define __NOP(x) (x)
@@ -297,7 +350,7 @@ static __always_inline void arch_change_bit(int nr, volatile unsigned long *addr
static __always_inline int arch_test_and_set_bit_lock(
unsigned long nr, volatile unsigned long *addr)
{
- return __test_and_op_bit_ord(or, __NOP, nr, addr, .aq);
+ return __test_and_op_bit_acquire(or, __NOP, nr, addr);
}
/**
@@ -310,7 +363,7 @@ static __always_inline int arch_test_and_set_bit_lock(
static __always_inline void arch_clear_bit_unlock(
unsigned long nr, volatile unsigned long *addr)
{
- __op_bit_ord(and, __NOT, nr, addr, .rl);
+ __op_bit_release(and, __NOT, nr, addr);
}
/**
@@ -337,20 +390,37 @@ static __always_inline void arch___clear_bit_unlock(
static __always_inline bool arch_xor_unlock_is_negative_byte(unsigned long mask,
volatile unsigned long *addr)
{
+#ifdef CONFIG_RISCV_ISA_ZAAMO
unsigned long res;
__asm__ __volatile__ (
__AMO(xor) ".rl %0, %2, %1"
: "=r" (res), "+A" (*addr)
: "r" (__NOP(mask))
: "memory");
+#else
+ unsigned long res, temp;
+
+ __asm__ __volatile__ (
+ "1: " __LR " %0, %1\n"
+ "xor %2, %0, %3\n"
+ __SC ".rl %2, %2, %1\n"
+ "bnez %2, 1b\n"
+ : "=&r" (res), "+A" (*addr), "=&r" (temp)
+ : "r" (__NOP(mask))
+ : "memory");
+#endif
return (res & BIT(7)) != 0;
}
#undef __test_and_op_bit
+#undef __test_and_op_bit_acquire
#undef __op_bit
+#undef __op_bit_release
#undef __NOP
#undef __NOT
#undef __AMO
+#undef __LR
+#undef __SC
#include <asm-generic/bitops/instrumented-atomic.h>
#include <asm-generic/bitops/instrumented-lock.h>
diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h
index 662e160b05229b..f0199432948a0e 100644
--- a/arch/riscv/include/asm/cmpxchg.h
+++ b/arch/riscv/include/asm/cmpxchg.h
@@ -57,16 +57,39 @@
} \
})
-#define __arch_xchg(sfx, prepend, append, r, p, n) \
+/*
+ * Word and double-word xchg is one amoswap, or an LR/SC loop without
+ * Zaamo. Each form uses its own pair of ordering arguments, as in
+ * __arch_xchg_masked() above.
+ */
+#ifdef CONFIG_RISCV_ISA_ZAAMO
+#define __arch_xchg(w, sc_sfx, swap_sfx, prepend, sc_append, \
+ swap_append, r, p, n) \
({ \
__asm__ __volatile__ ( \
prepend \
- " amoswap" sfx " %0, %2, %1\n" \
- append \
+ " amoswap" w swap_sfx " %0, %2, %1\n" \
+ swap_append \
: "=r" (r), "+A" (*(p)) \
: "r" (n) \
: "memory"); \
})
+#else
+#define __arch_xchg(w, sc_sfx, swap_sfx, prepend, sc_append, \
+ swap_append, r, p, n) \
+({ \
+ __typeof__(*(p)) __temp; \
+ __asm__ __volatile__ ( \
+ prepend \
+ "1: lr" w " %0, %1\n" \
+ " sc" w sc_sfx " %2, %3, %1\n" \
+ " bnez %2, 1b\n" \
+ sc_append \
+ : "=&r" (r), "+A" (*(p)), "=&r" (__temp) \
+ : "r" (n) \
+ : "memory"); \
+})
+#endif
#define _arch_xchg(ptr, new, sc_sfx, swap_sfx, prepend, \
sc_append, swap_append) \
@@ -87,12 +110,14 @@
__ret, __ptr, __new); \
break; \
case 4: \
- __arch_xchg(".w" swap_sfx, prepend, swap_append, \
- __ret, __ptr, __new); \
+ __arch_xchg(".w", sc_sfx, swap_sfx, \
+ prepend, sc_append, swap_append, \
+ __ret, __ptr, __new); \
break; \
case 8: \
- __arch_xchg(".d" swap_sfx, prepend, swap_append, \
- __ret, __ptr, __new); \
+ __arch_xchg(".d", sc_sfx, swap_sfx, \
+ prepend, sc_append, swap_append, \
+ __ret, __ptr, __new); \
break; \
default: \
BUILD_BUG(); \
diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h
index 5efa72823475fd..92cab4a7a0f127 100644
--- a/arch/riscv/include/asm/cpufeature.h
+++ b/arch/riscv/include/asm/cpufeature.h
@@ -83,6 +83,10 @@ static inline bool unaligned_ctl_available(void)
}
#endif
+#ifdef CONFIG_RISCV_PROBE_AMO_SPEED
+DECLARE_PER_CPU(long, amo_speed);
+#endif
+
#if defined(CONFIG_RISCV_MISALIGNED)
DECLARE_PER_CPU(long, misaligned_access_speed);
bool misaligned_traps_can_delegate(void);
diff --git a/arch/riscv/include/asm/futex.h b/arch/riscv/include/asm/futex.h
index 6f3bc9bdb85e39..e79d28af55cdfa 100644
--- a/arch/riscv/include/asm/futex.h
+++ b/arch/riscv/include/asm/futex.h
@@ -12,6 +12,7 @@
#include <linux/errno.h>
#include <asm/asm.h>
#include <asm/asm-extable.h>
+#include <asm/fence.h>
/* We don't even really need the extable code, but for now keep it simple */
#ifndef CONFIG_MMU
@@ -19,6 +20,7 @@
#define __disable_user_access() do { } while (0)
#endif
+#ifdef CONFIG_RISCV_ISA_ZAAMO
#define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
{ \
__enable_user_access(); \
@@ -32,6 +34,39 @@
: "memory"); \
__disable_user_access(); \
}
+#else
+/*
+ * Bound the number of LR/SC retries, as arm64 does for LL/SC: a user can
+ * keep the word changing from another thread forever. futex_wake_op()
+ * retries on -EAGAIN after dropping the hash bucket locks.
+ */
+#define FUTEX_MAX_LOOPS 128
+
+#define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
+{ \
+ int temp, loops = FUTEX_MAX_LOOPS; \
+ \
+ __enable_user_access(); \
+ __asm__ __volatile__ ( \
+ "1: lr.w %[ov], %[u] \n" \
+ " " insn " \n" \
+ "2: sc.w.rl %[t], %[t], %[u] \n" \
+ " beqz %[t], 3f \n" \
+ " addi %[l], %[l], -1 \n" \
+ " bnez %[l], 1b \n" \
+ " li %[r], %[ea] \n" \
+ "3: \n" \
+ RISCV_FULL_BARRIER \
+ _ASM_EXTABLE_UACCESS_ERR(1b, 3b, %[r]) \
+ _ASM_EXTABLE_UACCESS_ERR(2b, 3b, %[r]) \
+ : [r] "+r" (ret), [ov] "=&r" (oldval), \
+ [t] "=&r" (temp), [l] "+&r" (loops), \
+ [u] "+m" (*(uaddr)) \
+ : [op] "Jr" (oparg), [ea] "I" (-EAGAIN) \
+ : "memory"); \
+ __disable_user_access(); \
+}
+#endif
static inline int
arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
@@ -43,6 +78,7 @@ arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
uaddr = untagged_addr(uaddr);
switch (op) {
+#ifdef CONFIG_RISCV_ISA_ZAAMO
case FUTEX_OP_SET:
__futex_atomic_op("amoswap.w.aqrl %[ov],%z[op],%[u]",
ret, oldval, uaddr, oparg);
@@ -63,6 +99,28 @@ arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
__futex_atomic_op("amoxor.w.aqrl %[ov],%z[op],%[u]",
ret, oldval, uaddr, oparg);
break;
+#else
+ case FUTEX_OP_SET:
+ __futex_atomic_op("mv %[t], %z[op]",
+ ret, oldval, uaddr, oparg);
+ break;
+ case FUTEX_OP_ADD:
+ __futex_atomic_op("add %[t], %[ov], %z[op]",
+ ret, oldval, uaddr, oparg);
+ break;
+ case FUTEX_OP_OR:
+ __futex_atomic_op("or %[t], %[ov], %z[op]",
+ ret, oldval, uaddr, oparg);
+ break;
+ case FUTEX_OP_ANDN:
+ __futex_atomic_op("and %[t], %[ov], %z[op]",
+ ret, oldval, uaddr, ~oparg);
+ break;
+ case FUTEX_OP_XOR:
+ __futex_atomic_op("xor %[t], %[ov], %z[op]",
+ ret, oldval, uaddr, oparg);
+ break;
+#endif
default:
ret = -ENOSYS;
}
diff --git a/arch/riscv/include/asm/hwprobe.h b/arch/riscv/include/asm/hwprobe.h
index 9b04377c0f98fa..a7eb17c8180fe4 100644
--- a/arch/riscv/include/asm/hwprobe.h
+++ b/arch/riscv/include/asm/hwprobe.h
@@ -8,7 +8,7 @@
#include <uapi/asm/hwprobe.h>
-#define RISCV_HWPROBE_MAX_KEY 16
+#define RISCV_HWPROBE_MAX_KEY 17
static inline bool riscv_hwprobe_key_is_valid(__s64 key)
{
@@ -43,4 +43,10 @@ static inline bool riscv_hwprobe_pair_cmp(struct riscv_hwprobe *pair,
return pair->value == other_pair->value;
}
+#ifdef CONFIG_MMU
+void riscv_hwprobe_update_vdso_data(void);
+#else
+static inline void riscv_hwprobe_update_vdso_data(void) {}
+#endif
+
#endif
diff --git a/arch/riscv/include/asm/insn-def.h b/arch/riscv/include/asm/insn-def.h
index 8fcc848c196e00..5ca5c0b8ad66c4 100644
--- a/arch/riscv/include/asm/insn-def.h
+++ b/arch/riscv/include/asm/insn-def.h
@@ -219,6 +219,11 @@ ALTERNATIVE("nop", "fence rw, rw", SIFIVE_VENDOR_ID, \
__ASM_STR(.error "hlv.d requires 64-bit support")
#endif
+/* Spelled out so that it assembles when -march lacks Zaamo */
+#define AMOADD_W(dest, addr, val) \
+ INSN_R(OPCODE_AMO, FUNC3(2), FUNC7(0), \
+ RD(dest), RS1(addr), RS2(val))
+
#define LB_AQ(dest, addr) \
INSN_R(OPCODE_AMO, FUNC3(0), FUNC7(26), \
RD(dest), RS1(addr), __RS2(0))
diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h
index e6a394fbe7fce9..a5f726f94204cf 100644
--- a/arch/riscv/include/uapi/asm/hwprobe.h
+++ b/arch/riscv/include/uapi/asm/hwprobe.h
@@ -122,6 +122,11 @@ struct riscv_hwprobe {
#define RISCV_HWPROBE_EXT_ZICCIF _BITULL(3)
#define RISCV_HWPROBE_EXT_ZICCRSE _BITULL(4)
#define RISCV_HWPROBE_EXT_ZA64RS _BITULL(5)
+#define RISCV_HWPROBE_KEY_AMO_PERF 17
+#define RISCV_HWPROBE_AMO_UNKNOWN 0
+#define RISCV_HWPROBE_AMO_SLOW 1
+#define RISCV_HWPROBE_AMO_FAST 2
+#define RISCV_HWPROBE_AMO_UNSUPPORTED 3
/* Increase RISCV_HWPROBE_MAX_KEY when adding items. */
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index ebe1c3588177b4..fc935c877e8283 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -79,6 +79,7 @@ obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o
obj-$(CONFIG_RISCV_MISALIGNED) += unaligned_access_speed.o
obj-$(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS) += copy-unaligned.o
obj-$(CONFIG_RISCV_PROBE_VECTOR_UNALIGNED_ACCESS) += vec-copy-unaligned.o
+obj-$(CONFIG_RISCV_PROBE_AMO_SPEED) += amo_speed.o
obj-$(CONFIG_FPU) += fpu.o
obj-$(CONFIG_FPU) += kernel_mode_fpu.o
diff --git a/arch/riscv/kernel/amo_speed.c b/arch/riscv/kernel/amo_speed.c
new file mode 100644
index 00000000000000..8cd66941c3a61a
--- /dev/null
+++ b/arch/riscv/kernel/amo_speed.c
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Measure whether AMO instructions are faster than the equivalent LR/SC
+ * sequence. They are not on a CPU that traps them into firmware and
+ * emulates them there, such as MIPS P8700, where an AMO costs hundreds of
+ * cycles and LR/SC tens. The result goes to userspace through hwprobe,
+ * so code that can choose between the two knows which one to use.
+ */
+
+#include <linux/cpu.h>
+#include <linux/smp.h>
+#include <linux/timekeeping.h>
+#include <asm/cpufeature.h>
+#include <asm/hwprobe.h>
+#include <asm/insn-def.h>
+#include <asm/timex.h>
+
+/* Time window for each measurement, and atomics per sample within it */
+#define AMO_SPEED_NS 8000000
+#define AMO_SPEED_ITERS 1024
+
+DEFINE_PER_CPU(long, amo_speed) = RISCV_HWPROBE_AMO_UNKNOWN;
+
+static DEFINE_PER_CPU_ALIGNED(u32, amo_speed_word);
+static long amo_speed_param = RISCV_HWPROBE_AMO_UNKNOWN;
+
+static void amo_speed_amo(u32 *word, unsigned long n)
+{
+ unsigned long tmp;
+
+ asm volatile(
+ "1:\n"
+ AMOADD_W(%[tmp], %[word], %[one])
+ " addi %[n], %[n], -1\n"
+ " bnez %[n], 1b\n"
+ : [tmp] "=&r" (tmp), [n] "+&r" (n), "+m" (*word)
+ : [word] "r" (word), [one] "r" (1UL)
+ : "memory");
+}
+
+static void amo_speed_lrsc(u32 *word, unsigned long n)
+{
+ unsigned long tmp;
+
+ asm volatile(
+ "1: lr.w %[tmp], (%[word])\n"
+ " addi %[tmp], %[tmp], 1\n"
+ " sc.w %[tmp], %[tmp], (%[word])\n"
+ " bnez %[tmp], 1b\n"
+ " addi %[n], %[n], -1\n"
+ " bnez %[n], 1b\n"
+ : [tmp] "=&r" (tmp), [n] "+&r" (n), "+m" (*word)
+ : [word] "r" (word)
+ : "memory");
+}
+
+/*
+ * For a fixed amount of time, repeatedly run func and take the best time
+ * in cycles as the measurement.
+ */
+static u64 amo_speed_measure(void (*func)(u32 *word, unsigned long n),
+ u32 *word)
+{
+ u64 start_cycles, end_cycles, cycles = -1ULL;
+ u64 start_ns;
+
+ /* Do a warmup. */
+ func(word, AMO_SPEED_ITERS);
+
+ preempt_disable();
+
+ start_ns = ktime_get_mono_fast_ns();
+ while (ktime_get_mono_fast_ns() < start_ns + AMO_SPEED_NS) {
+ start_cycles = get_cycles64();
+ /* Ensure the CSR read can't reorder WRT to the atomics. */
+ mb();
+ func(word, AMO_SPEED_ITERS);
+ /* Ensure the atomics end before the end time is snapped. */
+ mb();
+ end_cycles = get_cycles64();
+ if ((end_cycles - start_cycles) < cycles)
+ cycles = end_cycles - start_cycles;
+ }
+
+ preempt_enable();
+
+ return cycles;
+}
+
+/*
+ * Settle the speed of one CPU: unsupported without Zaamo, the command
+ * line value if one was given, otherwise measure. Runs on that CPU.
+ */
+static void check_amo_speed(void *unused)
+{
+ int cpu = smp_processor_id();
+ u32 *word = this_cpu_ptr(&amo_speed_word);
+ u64 amo_cycles, lrsc_cycles;
+ long speed;
+ int ratio;
+
+ if (per_cpu(amo_speed, cpu) != RISCV_HWPROBE_AMO_UNKNOWN)
+ return;
+
+ if (!riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_ZAAMO)) {
+ per_cpu(amo_speed, cpu) = RISCV_HWPROBE_AMO_UNSUPPORTED;
+ return;
+ }
+
+ if (amo_speed_param != RISCV_HWPROBE_AMO_UNKNOWN) {
+ per_cpu(amo_speed, cpu) = amo_speed_param;
+ return;
+ }
+
+ amo_cycles = amo_speed_measure(amo_speed_amo, word);
+ lrsc_cycles = amo_speed_measure(amo_speed_lrsc, word);
+
+ /* Don't divide by zero. */
+ if (!amo_cycles || !lrsc_cycles) {
+ pr_warn("cpu%d: rdtime lacks granularity needed to measure AMO speed\n",
+ cpu);
+ return;
+ }
+
+ if (amo_cycles <= lrsc_cycles)
+ speed = RISCV_HWPROBE_AMO_FAST;
+ else
+ speed = RISCV_HWPROBE_AMO_SLOW;
+
+ ratio = div64_u64(lrsc_cycles * 100, amo_cycles);
+ pr_info("cpu%d: AMO speed is %d.%02dx LR/SC speed (%s)\n",
+ cpu, ratio / 100, ratio % 100,
+ speed == RISCV_HWPROBE_AMO_FAST ? "fast" : "slow");
+
+ per_cpu(amo_speed, cpu) = speed;
+
+ if (speed == RISCV_HWPROBE_AMO_SLOW && IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO))
+ pr_warn_once("AMO is slow but kernel atomics use it, consider disabling RISCV_ISA_ZAAMO\n");
+}
+
+static int amo_speed_online_cpu(unsigned int cpu)
+{
+ check_amo_speed(NULL);
+ return 0;
+}
+
+static int __init set_amo_speed_param(char *str)
+{
+ if (!strcmp(str, "slow"))
+ amo_speed_param = RISCV_HWPROBE_AMO_SLOW;
+ else if (!strcmp(str, "fast"))
+ amo_speed_param = RISCV_HWPROBE_AMO_FAST;
+ else
+ return 0;
+
+ return 1;
+}
+__setup("amo_speed=", set_amo_speed_param);
+
+static int __init check_amo_speed_all_cpus(void)
+{
+ if (amo_speed_param != RISCV_HWPROBE_AMO_UNKNOWN)
+ pr_info("AMO speed set to '%s' (%lu) by command line\n",
+ amo_speed_param == RISCV_HWPROBE_AMO_FAST ? "fast" : "slow",
+ amo_speed_param);
+
+ on_each_cpu(check_amo_speed, NULL, 1);
+
+ cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "riscv:amo_speed",
+ amo_speed_online_cpu, NULL);
+
+ /* The vDSO was filled before the probe ran; give it the result. */
+ riscv_hwprobe_update_vdso_data();
+
+ return 0;
+}
+
+/*
+ * Same level as the unaligned access probe: after clocksource_done_booting()
+ * so that amo_speed_measure() uses a stable clocksource, but before
+ * rootfs_initcall() enables usermode helpers that can reach hwprobe.
+ */
+fs_initcall_sync(check_amo_speed_all_cpus);
diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
index 3dbc8cc557dd1d..9ba0d49c87bc3b 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/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index 7329792978cc0f..45684e129be191 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -682,6 +682,15 @@ static void __init riscv_resolve_isa(unsigned long *source_isa,
int max_loop_count = riscv_isa_ext_count, ret;
unsigned int bit;
+ /*
+ * "a" is exactly Zaamo plus Zalrsc. A hart that lists both has A,
+ * however the firmware spelled it, so report it the same way in
+ * ELF hwcap and /proc/cpuinfo.
+ */
+ if (test_bit(RISCV_ISA_EXT_ZAAMO, source_isa) &&
+ test_bit(RISCV_ISA_EXT_ZALRSC, source_isa))
+ set_bit(RISCV_ISA_EXT_A, source_isa);
+
do {
loop = false;
if (max_loop_count-- < 0) {
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index 61b7a77166971d..ba2db6804a6c2c 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -73,7 +73,14 @@
beq a2, zero, .Lnew_valid_map_cpus_restore_context
/* Atomically reset the current cpu bit in new_valid_map_cpus */
+#ifdef CONFIG_RISCV_ISA_ZAAMO
amoxor.d a0, a1, (a0)
+#else
+1: lr.d a2, (a0)
+ xor a2, a1, a2
+ sc.d a2, a2, (a0)
+ bnez a2, 1b
+#endif
/*
* A sfence.vma is required here. Even if we had Svvptc, there's no
diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
index f6a8ca49e6277c..dc3862ed29c40e 100644
--- a/arch/riscv/kernel/head.S
+++ b/arch/riscv/kernel/head.S
@@ -265,7 +265,19 @@ SYM_CODE_START(_start_kernel)
/* Pick one hart to run the main boot sequence */
la a3, hart_lottery
li a2, 1
+#ifdef CONFIG_RISCV_ISA_ZAAMO
amoadd.w a3, a2, (a3)
+#else
+ /*
+ * 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
+#endif
bnez a3, .Lsecondary_start
#endif /* CONFIG_RISCV_BOOT_SPINWAIT */
diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c
index 7818e1d32622dd..32ce3ac8bc4a85 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;
@@ -257,6 +269,39 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)
}
#endif
+#ifdef CONFIG_RISCV_PROBE_AMO_SPEED
+static u64 hwprobe_amo(const struct cpumask *cpus)
+{
+ int cpu;
+ u64 perf = -1ULL;
+
+ for_each_cpu(cpu, cpus) {
+ int this_perf = per_cpu(amo_speed, cpu);
+
+ if (perf == -1ULL)
+ perf = this_perf;
+
+ if (perf != this_perf) {
+ perf = RISCV_HWPROBE_AMO_UNKNOWN;
+ break;
+ }
+ }
+
+ if (perf == -1ULL)
+ return RISCV_HWPROBE_AMO_UNKNOWN;
+
+ return perf;
+}
+#else
+static u64 hwprobe_amo(const struct cpumask *cpus)
+{
+ if (!hwprobe_ext0_has(cpus, RISCV_HWPROBE_EXT_ZAAMO))
+ return RISCV_HWPROBE_AMO_UNSUPPORTED;
+
+ return RISCV_HWPROBE_AMO_UNKNOWN;
+}
+#endif
+
#ifdef CONFIG_RISCV_VECTOR_MISALIGNED
static u64 hwprobe_vec_misaligned(const struct cpumask *cpus)
{
@@ -306,13 +351,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:
@@ -332,6 +378,10 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair,
pair->value = hwprobe_vec_misaligned(cpus);
break;
+ case RISCV_HWPROBE_KEY_AMO_PERF:
+ pair->value = hwprobe_amo(cpus);
+ break;
+
case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE:
if (hwprobe_ext0_has(cpus, RISCV_HWPROBE_EXT_ZICBOZ))
pair->value = riscv_cboz_block_size;
@@ -521,17 +571,18 @@ static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
#ifdef CONFIG_MMU
-static int __init init_hwprobe_vdso_data(void)
+/*
+ * Fill the vDSO data with the answers for the "all CPUs" case, to save a
+ * syscall in the common case. Called once the ISA is known, and again by
+ * the boot-time probes whose results feed some of the keys.
+ */
+void __init riscv_hwprobe_update_vdso_data(void)
{
struct vdso_arch_data *avd = vdso_k_arch_data;
u64 id_bitsmash = 0;
struct riscv_hwprobe pair;
int key;
- /*
- * Initialize vDSO data with the answers for the "all CPUs" case, to
- * save a syscall in the common case.
- */
for (key = 0; key <= RISCV_HWPROBE_MAX_KEY; key++) {
pair.key = key;
hwprobe_one_pair(&pair, cpu_online_mask);
@@ -555,6 +606,11 @@ static int __init init_hwprobe_vdso_data(void)
* vDSO should defer to the kernel for exotic cpu masks.
*/
avd->homogeneous_cpus = id_bitsmash != 0 && id_bitsmash != -1;
+}
+
+static int __init init_hwprobe_vdso_data(void)
+{
+ riscv_hwprobe_update_vdso_data();
return 0;
}
diff --git a/arch/riscv/kernel/unaligned_access_speed.c b/arch/riscv/kernel/unaligned_access_speed.c
index 67f1a03716597a..3a6f85e3f94c29 100644
--- a/arch/riscv/kernel/unaligned_access_speed.c
+++ b/arch/riscv/kernel/unaligned_access_speed.c
@@ -393,13 +393,15 @@ static int __init check_unaligned_access_all_cpus(void)
modify_unaligned_access_branches(cpu_online_mask);
cpus_read_unlock();
+ /* The vDSO was filled before the probes ran; give it the results. */
+ riscv_hwprobe_update_vdso_data();
+
return 0;
}
/*
* Run after clocksource_done_booting() so measure_cycles() uses a stable
- * clocksource, but before rootfs_initcall() enables usermode helpers. Those
- * helpers can reach hwprobe and populate the vDSO cache, so async hwprobe
- * probes must be registered first.
+ * clocksource, but before rootfs_initcall() enables usermode helpers, which
+ * can reach hwprobe and must see the results in the vDSO data.
*/
fs_initcall_sync(check_unaligned_access_all_cpus);
diff --git a/arch/riscv/net/bpf_jit.h b/arch/riscv/net/bpf_jit.h
index 419b9d795f2a7a..8c1301289ea802 100644
--- a/arch/riscv/net/bpf_jit.h
+++ b/arch/riscv/net/bpf_jit.h
@@ -79,6 +79,7 @@ struct rv_jit_context {
int nexentries;
int ex_insn_off;
int ex_jmp_off;
+ int ex_sc_off; /* sc in an lr/sc pair; 0 if unused */
unsigned long flags;
int stack_size;
int tcc_offset;
diff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp32.c
index a9e0bd5cc81d27..0fa0c013549acb 100644
--- a/arch/riscv/net/bpf_jit_comp32.c
+++ b/arch/riscv/net/bpf_jit_comp32.c
@@ -874,11 +874,70 @@ static int emit_load_r64(const s8 *dst, const s8 *src, s16 off,
return 0;
}
-static int emit_bpf_atomic(s8 dst, const s8 *src, const s8 *rs,
- struct rv_jit_context *ctx,
- const struct bpf_insn *insn)
+/*
+ * Read-modify-write via lr/sc for kernels built without Zaamo. T0 holds
+ * the address. T1/T2 are not BPF registers; the address was copied out of
+ * lo(rd) before this is called, so clobbering T2 is fine. A fetch op
+ * (including XCHG) is fully ordered as in asm/atomic.h: plain lr, sc.rl
+ * and a trailing fence rw,rw.
+ */
+static int emit_bpf_atomic_lrsc(s8 dst, const s8 *rs, s32 imm,
+ struct rv_jit_context *ctx)
+{
+ bool is_fetch = (imm & BPF_FETCH) || (imm == BPF_XCHG);
+ bool xchg = imm == BPF_XCHG;
+ u8 rl = is_fetch ? 1 : 0;
+ u8 old = RV_REG_T1;
+ u8 tmp = RV_REG_T2;
+ int lr_off, jmp_off;
+ s32 op = xchg ? BPF_XCHG : imm & ~BPF_FETCH;
+
+ switch (op) {
+ case BPF_ADD:
+ case BPF_AND:
+ case BPF_OR:
+ case BPF_XOR:
+ case BPF_XCHG:
+ break;
+ default:
+ return -1;
+ }
+
+ lr_off = ctx->ninsns;
+ emit(rv_lr_w(old, 0, dst, 0, 0), ctx);
+
+ switch (op) {
+ case BPF_ADD:
+ emit(rv_add(tmp, old, lo(rs)), ctx);
+ break;
+ case BPF_AND:
+ emit(rv_and(tmp, old, lo(rs)), ctx);
+ break;
+ case BPF_OR:
+ emit(rv_or(tmp, old, lo(rs)), ctx);
+ break;
+ case BPF_XOR:
+ emit(rv_xor(tmp, old, lo(rs)), ctx);
+ break;
+ }
+
+ if (xchg)
+ emit(rv_sc_w(tmp, lo(rs), dst, 0, rl), ctx);
+ else
+ emit(rv_sc_w(tmp, tmp, dst, 0, rl), ctx);
+
+ jmp_off = ninsns_rvoff(lr_off - ctx->ninsns);
+ emit(rv_bne(tmp, RV_REG_ZERO, jmp_off >> 1), ctx);
+ if (is_fetch) {
+ emit_fence_rw_rw(ctx);
+ emit(rv_addi(lo(rs), old, 0), ctx);
+ }
+ return 0;
+}
+
+static int emit_bpf_atomic_amo(s8 dst, const s8 *rs, s32 imm,
+ struct rv_jit_context *ctx)
{
- s32 imm = insn->imm;
bool is_fetch = (imm & BPF_FETCH) || (imm == BPF_XCHG);
s8 fetch_reg = is_fetch ? lo(rs) : RV_REG_ZERO;
int aq = is_fetch ? 1 : 0;
@@ -907,6 +966,23 @@ static int emit_bpf_atomic(s8 dst, const s8 *src, const s8 *rs,
default:
return -1;
}
+ return 0;
+}
+
+static int emit_bpf_atomic(s8 dst, const s8 *src, const s8 *rs,
+ struct rv_jit_context *ctx,
+ const struct bpf_insn *insn)
+{
+ s32 imm = insn->imm;
+ bool is_fetch = (imm & BPF_FETCH) || (imm == BPF_XCHG);
+ int ret;
+
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO))
+ ret = emit_bpf_atomic_amo(dst, rs, imm, ctx);
+ else
+ ret = emit_bpf_atomic_lrsc(dst, rs, imm, ctx);
+ if (ret)
+ return ret;
if (is_fetch) {
emit(rv_addi(hi(rs), RV_REG_ZERO, 0), ctx);
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 01fe66774f029f..4807b5800f6285 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -573,6 +573,80 @@ static int emit_atomic_ld_st(u8 rd, u8 rs, const struct bpf_insn *insn,
return 0;
}
+/*
+ * Read-modify-write via lr/sc for kernels built without Zaamo, matching
+ * the Zalrsc sequences in arch/riscv/include/asm/atomic.h. A relaxed op
+ * discards the old value. A fetch op (including XCHG) is fully ordered
+ * the way emit_cmpxchg() is: plain lr, sc.rl and a trailing fence rw,rw.
+ * It writes the old value back to val. T2/T3 are not BPF registers; addr
+ * may already be T1.
+ */
+static void emit_atomic_lrsc(u8 addr, u8 val, bool is64, s32 imm,
+ struct rv_jit_context *ctx)
+{
+ bool fetch = imm & BPF_FETCH;
+ u8 rl = fetch ? 1 : 0;
+ u8 old = RV_REG_T2;
+ u8 tmp = RV_REG_T3;
+ int lr_off, jmp_off;
+ s32 op = imm == BPF_XCHG ? BPF_XCHG : imm & ~BPF_FETCH;
+
+ lr_off = ctx->ninsns;
+ ctx->ex_insn_off = ctx->ninsns;
+ emit(is64 ? rv_lr_d(old, 0, addr, 0, 0) :
+ rv_lr_w(old, 0, addr, 0, 0), ctx);
+
+ if (op != BPF_XCHG) {
+ if (!is64 && op == BPF_ADD)
+ emit(rv_addw(tmp, old, val), ctx);
+ else if (op == BPF_ADD)
+ emit_add(tmp, old, val, ctx);
+ else if (op == BPF_AND)
+ emit_and(tmp, old, val, ctx);
+ else if (op == BPF_OR)
+ emit_or(tmp, old, val, ctx);
+ else if (op == BPF_XOR)
+ emit_xor(tmp, old, val, ctx);
+
+ ctx->ex_sc_off = ctx->ninsns;
+ emit(is64 ? rv_sc_d(tmp, tmp, addr, 0, rl) :
+ rv_sc_w(tmp, tmp, addr, 0, rl), ctx);
+ } else {
+ ctx->ex_sc_off = ctx->ninsns;
+ emit(is64 ? rv_sc_d(tmp, val, addr, 0, rl) :
+ rv_sc_w(tmp, val, addr, 0, rl), ctx);
+ }
+
+ /* Branch offset is relative to this instruction, back to the lr. */
+ jmp_off = ninsns_rvoff(lr_off - ctx->ninsns);
+ emit(rv_bne(tmp, RV_REG_ZERO, jmp_off >> 1), ctx);
+
+ if (fetch) {
+ emit_fence_rw_rw(ctx);
+ emit_mv(val, old, ctx);
+ if (!is64)
+ emit_zextw(val, val, ctx);
+ }
+ ctx->ex_jmp_off = ctx->ninsns;
+}
+
+static bool is_atomic_lrsc_op(s32 imm)
+{
+ switch (imm) {
+ case BPF_ADD:
+ case BPF_AND:
+ case BPF_OR:
+ case BPF_XOR:
+ case BPF_ADD | BPF_FETCH:
+ case BPF_AND | BPF_FETCH:
+ case BPF_OR | BPF_FETCH:
+ case BPF_XOR | BPF_FETCH:
+ case BPF_XCHG:
+ return true;
+ }
+ return false;
+}
+
static int emit_atomic_rmw(u8 rd, u8 rs, const struct bpf_insn *insn,
struct rv_jit_context *ctx)
{
@@ -601,6 +675,12 @@ static int emit_atomic_rmw(u8 rd, u8 rs, const struct bpf_insn *insn,
rd = RV_REG_T1;
}
+ /* Without Zaamo, every op except cmpxchg is an lr/sc loop. */
+ if (!IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO) && is_atomic_lrsc_op(imm)) {
+ emit_atomic_lrsc(rd, rs, is64, imm, ctx);
+ return 0;
+ }
+
switch (imm) {
/* lock *(u32/u64 *)(dst_reg + off16) <op>= src_reg */
case BPF_ADD:
@@ -732,8 +812,8 @@ bool ex_handler_bpf(const struct exception_table_entry *ex,
}
/* For accesses to BTF pointers, add an entry to the exception table */
-static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,
- struct rv_jit_context *ctx)
+static int add_exception_handler_one(const struct bpf_insn *insn, int dst_reg,
+ struct rv_jit_context *ctx)
{
struct exception_table_entry *ex;
unsigned long pc;
@@ -804,6 +884,31 @@ static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,
return 0;
}
+/*
+ * An lr/sc pair faults on either instruction. Both entries share the fixup
+ * that follows the loop, so a probe fault skips the retry and the result move.
+ */
+static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,
+ struct rv_jit_context *ctx)
+{
+ int sc_off = ctx->ex_sc_off;
+ int jmp_off = ctx->ex_jmp_off;
+ int ret;
+
+ ctx->ex_sc_off = 0;
+ /* Offset discovery runs before the image exists. Entries are added later. */
+ if (!ctx->insns)
+ return 0;
+
+ ret = add_exception_handler_one(insn, dst_reg, ctx);
+ if (ret || sc_off <= 0)
+ return ret;
+
+ ctx->ex_insn_off = sc_off;
+ ctx->ex_jmp_off = jmp_off;
+ return add_exception_handler_one(insn, dst_reg, ctx);
+}
+
static int gen_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)
{
s64 rvoff;
diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c
index cbfcd287ea1668..cba0d34c3e6fa0 100644
--- a/arch/riscv/net/bpf_jit_core.c
+++ b/arch/riscv/net/bpf_jit_core.c
@@ -16,6 +16,34 @@
/* Number of iterations to try until offsets converge. */
#define NR_JIT_ITERATIONS 32
+/*
+ * The verifier reserves one extable slot per BPF_PROBE_ATOMIC. Without
+ * Zaamo a read-modify-write atomic is an lr/sc loop that can fault on
+ * either instruction, so reserve a second slot for the sc.
+ */
+static void rv_jit_add_lrsc_exentries(struct bpf_prog *prog)
+{
+ const struct bpf_insn *insn = prog->insnsi;
+ int i;
+
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZAAMO) || __riscv_xlen != 64)
+ return;
+
+ for (i = 0; i < prog->len; i++, insn++) {
+ if (BPF_CLASS(insn->code) != BPF_STX ||
+ BPF_MODE(insn->code) != BPF_PROBE_ATOMIC ||
+ bpf_atomic_is_load_store(insn))
+ continue;
+ if (BPF_SIZE(insn->code) != BPF_W &&
+ BPF_SIZE(insn->code) != BPF_DW)
+ continue;
+ /* amocas is a single faulting instruction. */
+ if (insn->imm == BPF_CMPXCHG && rv_ext_enabled(ZACAS))
+ continue;
+ prog->aux->num_exentries++;
+ }
+}
+
static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset)
{
const struct bpf_prog *prog = ctx->prog;
@@ -100,6 +128,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
if (ctx->ninsns == prev_ninsns) {
if (jit_data->header)
break;
+ rv_jit_add_lrsc_exentries(prog);
/* obtain the actual image size */
extable_size = prog->aux->num_exentries *
sizeof(struct exception_table_entry);
|
| 3/2 | 2026/11/22 21:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22592 output: 29 thoughts: 1465 Thoughts: **Analysis of the Patch for Fuzzing Suitability** Okay, let's break this down. The task is to determine if this patch is suitable for fuzzing, given that the target architecture is amd64. First, I need to analyze the changes introduced by the patch. I see a comprehensive list of modified files: it touches files across `Documentation/` and the `arch/riscv/` directory. Initially, I noted that *every* modified file falls within these two directories. That raises a red flag in my mind, so I verified there were no files outside these two trees. The specific files modified are: `Documentation/admin-guide/kernel-parameters.txt`, `Documentation/arch/riscv/hwprobe.rst`, and then everything in `arch/riscv/`, covering includes (atomic, bitops, cmpxchg, cpufeature, futex, hwprobe, insn-def, uapi/hwprobe), kernel code (Makefile, amo_speed.c, cpu.c, cpufeature.c, entry.S, head.S, sys_hwprobe.c, unaligned_access_speed.c), and the networking code (bpf_jit.h, bpf_jit_comp32.c, bpf_jit_comp64.c, bpf_jit_core.c). The key here is the target architecture, which is specified as amd64 (x86_64). I'm looking for negative criteria, things that would suggest this patch isn't suitable for fuzzing under those constraints. Specifically, I'm considering the scenarios outlined in the instructions for determining when WorthFuzzing=false. I'm focusing on the last bullet point under the negative criteria: "Code paths exclusive to architectures other than the target architecture." This is where things become clear. All of the modified code is exclusively related to the RISC-V architecture. This includes the documentation changes specific to RISC-V features (e.g., `amo_speed` mentioned in `kernel-parameters.txt`). Given that it's all under `arch/riscv/`, and the target is amd64, I can be confident that none of this code will be compiled or executed on an amd64 system. I'm even double-checking the include paths and their usage to confirm that these uapi headers are strictly RISC-V specific. Therefore, the changes fall into the negative criteria, because they are exclusive to a different architecture. I want to be 100% sure nothing in the diff introduces something that could affect amd64, such as a Kconfig option that might accidentally be enabled. I may quickly check the logs to make sure this is the case. Based on the analysis, I conclude that `WorthFuzzing=false`. |
| 4/2 | 2026/11/22 21:08 | tool | grepper |
0mArgs:{
"Expression": "amo_speed",
"PathPrefixes": [
"Documentation",
"arch"
]
}Results: {
"Output": "Documentation/admin-guide/kernel-parameters.txt=98=Kernel parameters\n--\nDocumentation/admin-guide/kernel-parameters.txt-515-\nDocumentation/admin-guide/kernel-parameters.txt:516:\tamo_speed=\t[RISCV]\nDocumentation/admin-guide/kernel-parameters.txt-517-\t\t\tFormat: {slow | fast}\n--\narch/riscv/include/asm/cpufeature.h=80=static inline bool unaligned_ctl_available(void)\n--\narch/riscv/include/asm/cpufeature.h-86-#ifdef CONFIG_RISCV_PROBE_AMO_SPEED\narch/riscv/include/asm/cpufeature.h:87:DECLARE_PER_CPU(long, amo_speed);\narch/riscv/include/asm/cpufeature.h-88-#endif\n--\narch/riscv/kernel/Makefile=81=obj-$(CONFIG_RISCV_PROBE_VECTOR_UNALIGNED_ACCESS)\t+= vec-copy-unaligned.o\narch/riscv/kernel/Makefile:82:obj-$(CONFIG_RISCV_PROBE_AMO_SPEED)\t+= amo_speed.o\narch/riscv/kernel/Makefile-83-\n--\narch/riscv/kernel/amo_speed.c-21-\narch/riscv/kernel/amo_speed.c:22:DEFINE_PER_CPU(long, amo_speed) = RISCV_HWPROBE_AMO_UNKNOWN;\narch/riscv/kernel/amo_speed.c-23-\narch/riscv/kernel/amo_speed.c:24:static DEFINE_PER_CPU_ALIGNED(u32, amo_speed_word);\narch/riscv/kernel/amo_speed.c:25:static long amo_speed_param = RISCV_HWPROBE_AMO_UNKNOWN;\narch/riscv/kernel/amo_speed.c-26-\narch/riscv/kernel/amo_speed.c:27:static void amo_speed_amo(u32 *word, unsigned long n)\narch/riscv/kernel/amo_speed.c-28-{\n--\narch/riscv/kernel/amo_speed.c-40-\narch/riscv/kernel/amo_speed.c:41:static void amo_speed_lrsc(u32 *word, unsigned long n)\narch/riscv/kernel/amo_speed.c-42-{\n--\narch/riscv/kernel/amo_speed.c-60- */\narch/riscv/kernel/amo_speed.c:61:static u64 amo_speed_measure(void (*func)(u32 *word, unsigned long n),\narch/riscv/kernel/amo_speed.c-62-\t\t\t u32 *word)\n--\narch/riscv/kernel/amo_speed.c-93- */\narch/riscv/kernel/amo_speed.c:94:static void check_amo_speed(void *unused)\narch/riscv/kernel/amo_speed.c-95-{\narch/riscv/kernel/amo_speed.c-96-\tint cpu = smp_processor_id();\narch/riscv/kernel/amo_speed.c:97:\tu32 *word = this_cpu_ptr(\u0026amo_speed_word);\narch/riscv/kernel/amo_speed.c-98-\tu64 amo_cycles, lrsc_cycles;\n--\narch/riscv/kernel/amo_speed.c-101-\narch/riscv/kernel/amo_speed.c:102:\tif (per_cpu(amo_speed, cpu) != RISCV_HWPROBE_AMO_UNKNOWN)\narch/riscv/kernel/amo_speed.c-103-\t\treturn;\n--\narch/riscv/kernel/amo_speed.c-105-\tif (!riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_ZAAMO)) {\narch/riscv/kernel/amo_speed.c:106:\t\tper_cpu(amo_speed, cpu) = RISCV_HWPROBE_AMO_UNSUPPORTED;\narch/riscv/kernel/amo_speed.c-107-\t\treturn;\n--\narch/riscv/kernel/amo_speed.c-109-\narch/riscv/kernel/amo_speed.c:110:\tif (amo_speed_param != RISCV_HWPROBE_AMO_UNKNOWN) {\narch/riscv/kernel/amo_speed.c:111:\t\tper_cpu(amo_speed, cpu) = amo_speed_param;\narch/riscv/kernel/amo_speed.c-112-\t\treturn;\n--\narch/riscv/kernel/amo_speed.c-114-\narch/riscv/kernel/amo_speed.c:115:\tamo_cycles = amo_speed_measure(amo_speed_amo, word);\narch/riscv/kernel/amo_speed.c:116:\tlrsc_cycles = amo_speed_measure(amo_speed_lrsc, word);\narch/riscv/kernel/amo_speed.c-117-\n--\narch/riscv/kernel/amo_speed.c-134-\narch/riscv/kernel/amo_speed.c:135:\tper_cpu(amo_speed, cpu) = speed;\narch/riscv/kernel/amo_speed.c-136-\n--\narch/riscv/kernel/amo_speed.c-140-\narch/riscv/kernel/amo_speed.c:141:static int amo_speed_online_cpu(unsigned int cpu)\narch/riscv/kernel/amo_speed.c-142-{\narch/riscv/kernel/amo_speed.c:143:\tcheck_amo_speed(NULL);\narch/riscv/kernel/amo_speed.c-144-\treturn 0;\n--\narch/riscv/kernel/amo_speed.c-146-\narch/riscv/kernel/amo_speed.c:147:static int __init set_amo_speed_param(char *str)\narch/riscv/kernel/amo_speed.c-148-{\narch/riscv/kernel/amo_speed.c-149-\tif (!strcmp(str, \"slow\"))\narch/riscv/kernel/amo_speed.c:150:\t\tamo_speed_param = RISCV_HWPROBE_AMO_SLOW;\narch/riscv/kernel/amo_speed.c-151-\telse if (!strcmp(str, \"fast\"))\narch/riscv/kernel/amo_speed.c:152:\t\tamo_speed_param = RISCV_HWPROBE_AMO_FAST;\narch/riscv/kernel/amo_speed.c-153-\telse\n--\narch/riscv/kernel/amo_speed.c-157-}\narch/riscv/kernel/amo_speed.c:158:__setup(\"amo_speed=\", set_amo_speed_param);\narch/riscv/kernel/amo_speed.c-159-\narch/riscv/kernel/amo_speed.c:160:static int __init check_amo_speed_all_cpus(void)\narch/riscv/kernel/amo_speed.c-161-{\narch/riscv/kernel/amo_speed.c:162:\tif (amo_speed_param != RISCV_HWPROBE_AMO_UNKNOWN)\narch/riscv/kernel/amo_speed.c-163-\t\tpr_info(\"AMO speed set to '%s' (%lu) by command line\\n\",\narch/riscv/kernel/amo_speed.c:164:\t\t\tamo_speed_param == RISCV_HWPROBE_AMO_FAST ? \"fast\" : \"slow\",\narch/riscv/kernel/amo_speed.c:165:\t\t\tamo_speed_param);\narch/riscv/kernel/amo_speed.c-166-\narch/riscv/kernel/amo_speed.c:167:\ton_each_cpu(check_amo_speed, NULL, 1);\narch/riscv/kernel/amo_speed.c-168-\narch/riscv/kernel/amo_speed.c:169:\tcpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, \"riscv:amo_speed\",\narch/riscv/kernel/amo_speed.c:170:\t\t\t\t amo_speed_online_cpu, NULL);\narch/riscv/kernel/amo_speed.c-171-\n--\narch/riscv/kernel/amo_speed.c-179- * Same level as the unaligned access probe: after clocksource_done_booting()\narch/riscv/kernel/amo_speed.c:180: * so that amo_speed_measure() uses a stable clocksource, but before\narch/riscv/kernel/amo_speed.c-181- * rootfs_initcall() enables usermode helpers that can reach hwprobe.\narch/riscv/kernel/amo_speed.c-182- */\narch/riscv/kernel/amo_speed.c:183:fs_initcall_sync(check_amo_speed_all_cpus);\n--\narch/riscv/kernel/sys_hwprobe.c=273=static u64 hwprobe_amo(const struct cpumask *cpus)\n--\narch/riscv/kernel/sys_hwprobe.c-278-\tfor_each_cpu(cpu, cpus) {\narch/riscv/kernel/sys_hwprobe.c:279:\t\tint this_perf = per_cpu(amo_speed, cpu);\narch/riscv/kernel/sys_hwprobe.c-280-\n"
} |
| 5/2 | 2026/11/22 21:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27095 output: 81 thoughts: 257 Thoughts: **My Assessment of the Patch's Fuzzing Implications** Okay, let's break this down. From a quick glance, the changes are laser-focused on RISC-V specific code (`arch/riscv/*`) and documentation (`Documentation/*`). My immediate thought is, given that the target architecture for fuzzing is amd64, this should be a non-issue. The code isn't even *structurally* reachable on amd64; it's architecturally isolated. Now, let's consult section 2 of the relevant documentation (as I always do, to be thorough). The section specifies when we can confidently set `WorthFuzzing=false`. Basically, we don't need to fuzz if *all* modified code falls into certain categories. The key ones here are "Non-kernel and non-executable changes" (which covers documentation modifications, comments, spelling fixes) and "Unreachable execution paths" (which includes paths exclusive to architectures *other* than the target). Bingo. This patch does both. Everything touched is either documentation or exclusive to RISC-V, which means it will be completely ignored during an amd64 build. No chance of it influencing the fuzzer. Therefore: * `WorthFuzzing: false`. * `EnableConfigs: []` (no new configs required). * `FocusSymbols: []` (nothing to focus on). * The reasoning is solid: The changes are isolated to RISC-V and documentation, and thus, unreachable within our amd64 fuzzing environment. Right, time to `set-results`. Done. |
| 6/2 | 2026/11/22 21:09 | tool | set-results |
0mArgs:{
"EnableConfigs": [],
"FocusSymbols": [],
"Reasoning": "All changes in this patch series are either documentation updates or code exclusive to the RISC-V architecture (arch/riscv/). Because the target architecture is amd64, none of the modified executable code can be compiled or executed during fuzzing.",
"WorthFuzzing": false
}Results: {
"EnableConfigs": null,
"FocusSymbols": [],
"Reasoning": "All changes in this patch series are either documentation updates or code exclusive to the RISC-V architecture (arch/riscv/). Because the target architecture is amd64, none of the modified executable code can be compiled or executed during fuzzing.",
"WorthFuzzing": false
} |
| 7/1 | 2026/11/22 21:09 | action | If |
0mArgs:{
"WorthFuzzing": false
}Results: null |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|