[PATCH v12 07/10] riscv: implement preemptible this_cpu xchg operations
From: Yunhui Cui
Date: Fri Dec 04 2026 - 04:01:57 EST
Apply the recoverable per-CPU address handling to exchange operations.
Use the shared AMO implementation for native-width and Zabha exchanges
to avoid generic interrupt masking. Provide an LR/SC fallback for subword
exchanges when Zabha is unavailable.
Signed-off-by: Yunhui Cui <cuiyunhui@xxxxxxxxxxxxx>
---
arch/riscv/include/asm/percpu.h | 70 +++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index f67dc601287eb..a6eb16cf2849c 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -240,6 +240,63 @@ __PERCPU_8_16_OP_CASE(and, 16, .h, &)
__PERCPU_8_16_OP_CASE(or, 8, .b, |)
__PERCPU_8_16_OP_CASE(or, 16, .h, |)
+#define __PERCPU_XCHG_AMO_CASE(sfx, sz) \
+static __always_inline unsigned long \
+__percpu_xchg_case_##sz(void __percpu *pcp, unsigned long val) \
+{ \
+ u##sz ret; \
+ \
+ __PERCPU_AMO("amoswap" #sfx, "a", ret, pcp, (u##sz)val); \
+ return ret; \
+}
+
+__PERCPU_XCHG_AMO_CASE(.w, 32)
+#ifdef CONFIG_64BIT
+__PERCPU_XCHG_AMO_CASE(.d, 64)
+#endif
+
+#define __PERCPU_8_16_XCHG_CASE(sz, sfx) \
+static __always_inline unsigned long \
+__percpu_xchg_case_##sz(void __percpu *pcp, unsigned long val) \
+{ \
+ u##sz ret; \
+ \
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) && \
+ riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) { \
+ __PERCPU_AMO("amoswap" #sfx, "zabha", ret, pcp, \
+ (u##sz)val); \
+ return ret; \
+ } \
+ \
+ __PCPU_OP_CONTEXT(); \
+ const u32 field_mask = PERCPU_8_16_GET_MASK(sz); \
+ const unsigned long shift = PERCPU_8_16_GET_SHIFT(pcp); \
+ const u32 mask = field_mask << shift; \
+ const u32 new_field = (val & field_mask) << shift; \
+ void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp); \
+ u32 old, new, rc; \
+ \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
+ "0: lr.w %[old], 0(%[addr])\n" \
+ "and %[new], %[old], %[inv_mask]\n" \
+ "or %[new], %[new], %[new_field]\n" \
+ "sc.w %[rc], %[new], 0(%[addr])\n" \
+ "bnez %[rc], 0b\n" \
+ __PCPU_GPRS_END(gprs) \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [old] "=&r" (old), [new] "=&r" (new), [rc] "=&r" (rc) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp32), [inv_mask] "r" (~mask), \
+ [new_field] "r" (new_field) \
+ : "memory"); \
+ \
+ return (old & mask) >> shift; \
+}
+
+__PERCPU_8_16_XCHG_CASE(8, .b)
+__PERCPU_8_16_XCHG_CASE(16, .h)
+
#define this_cpu_read_1(pcp) _pcp_wrap_return(__percpu_read_8, pcp)
#define this_cpu_read_2(pcp) _pcp_wrap_return(__percpu_read_16, pcp)
#define this_cpu_read_4(pcp) _pcp_wrap_return(__percpu_read_32, pcp)
@@ -292,12 +349,25 @@ __PERCPU_8_16_OP_CASE(or, 16, .h, |)
_pcp_wrap_return(__percpu_add_amo_case_64, pcp, val)
#endif
+#define this_cpu_xchg_1(pcp, val) \
+ _pcp_wrap_return(__percpu_xchg_case_8, pcp, (__force unsigned long)(val))
+#define this_cpu_xchg_2(pcp, val) \
+ _pcp_wrap_return(__percpu_xchg_case_16, pcp, (__force unsigned long)(val))
+#define this_cpu_xchg_4(pcp, val) \
+ _pcp_wrap_return(__percpu_xchg_case_32, pcp, (__force unsigned long)(val))
+#ifdef CONFIG_64BIT
+#define this_cpu_xchg_8(pcp, val) \
+ _pcp_wrap_return(__percpu_xchg_case_64, pcp, (__force unsigned long)(val))
+#endif
+
#undef __PERCPU_AMO
#undef PERCPU_RW_OPS
#undef __PERCPU_AMO_OP_CASE
#undef __PCPU_AMO_OPTION
#undef __PCPU_AMO_OPTION_END
#undef __PERCPU_8_16_OP_CASE
+#undef __PERCPU_XCHG_AMO_CASE
+#undef __PERCPU_8_16_XCHG_CASE
#undef PERCPU_8_16_GET_SHIFT
#undef PERCPU_8_16_GET_MASK
#undef PERCPU_8_16_GET_PCP32
--
2.39.5