[PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations
From: Yunhui Cui
Date: Thu Dec 03 2026 - 08:56:34 EST
Apply the recoverable per-CPU address handling to read and write
operations so they can remain preemptible across CPU migration.
This avoids the preemption or interrupt masking required by the
generic fallbacks.
Signed-off-by: Yunhui Cui <cuiyunhui@xxxxxxxxxxxxx>
---
arch/riscv/include/asm/percpu.h | 66 +++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index a5ceedc934b54..cd88618889494 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -176,7 +176,73 @@ __percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val) \
(TYPEOF_UNQUAL(*__pcp_ptr))op(__pcp_ptr, ##args); \
})
+#define PERCPU_RW_OPS(load, store, sz) \
+static __always_inline unsigned long \
+__percpu_read_##sz(const void __percpu *pcp) \
+{ \
+ __PCPU_OP_CONTEXT(); \
+ unsigned long val; \
+ \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
+ load " %[val], 0(%[addr])\n" \
+ __PCPU_GPRS_END("%[gprs]") \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [val] "=&r" (val) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp) \
+ : "memory"); \
+ \
+ return (u##sz)val; \
+} \
+ \
+static __always_inline void __percpu_write_##sz(void __percpu *pcp, \
+ unsigned long val) \
+{ \
+ __PCPU_OP_CONTEXT(); \
+ \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
+ store " %[val], 0(%[addr])\n" \
+ __PCPU_GPRS_END("%[gprs]") \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp), [val] "r" ((u##sz)val) \
+ : "memory"); \
+}
+
+PERCPU_RW_OPS("lbu", "sb", 8)
+PERCPU_RW_OPS("lhu", "sh", 16)
+
+#ifdef CONFIG_64BIT
+PERCPU_RW_OPS("lwu", "sw", 32)
+PERCPU_RW_OPS("ld", "sd", 64)
+#else
+PERCPU_RW_OPS("lw", "sw", 32)
+#endif
+
+#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)
+
+#ifdef CONFIG_64BIT
+#define this_cpu_read_8(pcp) _pcp_wrap_return(__percpu_read_64, pcp)
+#endif
+
+#define this_cpu_write_1(pcp, val) \
+ _pcp_wrap(__percpu_write_8, pcp, (unsigned long)(val))
+#define this_cpu_write_2(pcp, val) \
+ _pcp_wrap(__percpu_write_16, pcp, (unsigned long)(val))
+#define this_cpu_write_4(pcp, val) \
+ _pcp_wrap(__percpu_write_32, pcp, (unsigned long)(val))
+
+#ifdef CONFIG_64BIT
+#define this_cpu_write_8(pcp, val) \
+ _pcp_wrap(__percpu_write_64, pcp, (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