[PATCH v11 03/10] riscv: add common primitives for preemptible this_cpu operations
From: Yunhui Cui
Date: Thu Dec 03 2026 - 08:56:21 EST
Void RMW and add-return operations need the same atomic updates, while
exchange operations can share the AMO execution. Provide common
primitives for these paths to avoid separate implementations of the
same address handling and atomic instructions.
Link: https://lore.kernel.org/r/20261112120928.24145d44@pumpkin
Signed-off-by: Yunhui Cui <cuiyunhui@xxxxxxxxxxxxx>
---
arch/riscv/include/asm/percpu.h | 131 ++++++++++++++++++++++++++++++++
1 file changed, 131 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index 3cd60ef18815e..a5ceedc934b54 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -4,12 +4,15 @@
#define __ASM_PERCPU_H
#include <linux/bits.h>
+#include <linux/compiler.h>
#include <linux/stringify.h>
#include <linux/thread_info.h>
#include <asm/asm.h>
+#include <asm/cpufeature-macros.h>
#include <asm/current.h>
#include <asm/gpr-num.h>
+#include <asm/hwcap.h>
#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
@@ -55,6 +58,134 @@
#define __PCPU_GPRS_END(gprs) \
"sh zero, " gprs "\n"
+#define __PCPU_OP_CONTEXT() \
+ struct thread_info *__ti = (struct thread_info *)current; \
+ unsigned long __off, __addr
+
+#define PERCPU_8_16_GET_SHIFT(pcp) \
+ (((__force unsigned long)(pcp) & 0x3) * BITS_PER_BYTE)
+#define PERCPU_8_16_GET_MASK(sz) GENMASK((sz) - 1, 0)
+#define PERCPU_8_16_GET_PCP32(pcp) \
+ ((void __percpu *)((__force unsigned long)(pcp) & ~0x3UL))
+
+#ifdef CONFIG_AS_HAS_OPTION_ARCH
+#define __PCPU_AMO_OPTION(isa) \
+ ".option push\n" \
+ ".option arch, +" isa "\n"
+#define __PCPU_AMO_OPTION_END \
+ ".option pop\n"
+#else
+#define __PCPU_AMO_OPTION(isa)
+#define __PCPU_AMO_OPTION_END
+#endif
+
+/*
+ * Keep address generation and the AMO in one recoverable region. A real
+ * output register is required even when the caller discards the old value,
+ * to favor execution near the CPU for CPU-local data.
+ */
+#define __PERCPU_AMO(insn, isa, ret_arg, pcp_arg, val_arg) \
+do { \
+ __PCPU_OP_CONTEXT(); \
+ \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
+ __PCPU_AMO_OPTION(isa) \
+ insn " %[ret], %[val], 0(%[addr])\n" \
+ __PCPU_AMO_OPTION_END \
+ __PCPU_GPRS_END("%[gprs]") \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [ret] "=&r" (ret_arg) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp_arg), [val] "r" (val_arg) \
+ : "memory"); \
+ \
+} while (0)
+
+/* Generate RMW helpers returning the new value; void interfaces discard it. */
+#define __PERCPU_AMO_OP_CASE(name, sz, sfx, op) \
+static __always_inline unsigned long \
+__percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val) \
+{ \
+ u##sz tmp; \
+ \
+ __PERCPU_AMO("amo" #name #sfx, "a", tmp, pcp, (u##sz)val); \
+ \
+ tmp = tmp op val; \
+ return tmp; \
+}
+
+#define __PERCPU_8_16_OP_CASE(name, sz, sfx, op) \
+static __always_inline unsigned long \
+__percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val) \
+{ \
+ __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 val_trunc = val & field_mask; \
+ void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp); \
+ u32 old, new, field; \
+ \
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) && \
+ riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) { \
+ u##sz tmp; \
+ \
+ __PERCPU_AMO("amo" #name #sfx, "zabha", \
+ tmp, pcp, (u##sz)val); \
+ \
+ tmp = tmp op val; \
+ return tmp; \
+ } \
+ \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
+ "0: lr.w %[old], 0(%[addr])\n" \
+ "and %[field], %[old], %[mask]\n" \
+ "srl %[field], %[field], %[shift]\n" \
+ #name " %[field], %[field], %[val]\n" \
+ "and %[field], %[field], %[field_mask]\n" \
+ "sll %[field], %[field], %[shift]\n" \
+ "and %[new], %[old], %[inv_mask]\n" \
+ "or %[new], %[new], %[field]\n" \
+ "sc.w %[new], %[new], 0(%[addr])\n" \
+ "bnez %[new], 0b\n" \
+ __PCPU_GPRS_END("%[gprs]") \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [old] "=&r" (old), [new] "=&r" (new), \
+ [field] "=&r" (field) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp32), [mask] "r" (mask), \
+ [shift] "r" (shift), [val] "r" (val_trunc), \
+ [field_mask] "r" (field_mask), \
+ [inv_mask] "r" (~mask) \
+ : "memory"); \
+ \
+ return (u##sz)(field >> shift); \
+}
+
+#define _pcp_wrap(op, pcp, ...) \
+({ \
+ (void)op(&(pcp), __VA_ARGS__); \
+})
+
+#define _pcp_wrap_return(op, pcp, args...) \
+({ \
+ __auto_type __pcp_ptr = &(pcp); \
+ \
+ (TYPEOF_UNQUAL(*__pcp_ptr))op(__pcp_ptr, ##args); \
+})
+
+#undef __PERCPU_AMO
+#undef __PERCPU_AMO_OP_CASE
+#undef __PCPU_AMO_OPTION
+#undef __PCPU_AMO_OPTION_END
+#undef __PERCPU_8_16_OP_CASE
+#undef PERCPU_8_16_GET_SHIFT
+#undef PERCPU_8_16_GET_MASK
+#undef PERCPU_8_16_GET_PCP32
+#undef __PCPU_OP_CONTEXT
+
#include <asm-generic/percpu.h>
#endif /* __ASM_PERCPU_H */