* [PATCH v12 01/10] riscv: store percpu offset into thread_info
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
` (8 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
RISC-V currently derives the current per-CPU offset from the CPU number
and __per_cpu_offset[]. Cache it in thread_info so per-CPU accesses can
load the offset directly.
Initialize the cached offset for the boot and secondary CPUs, and
propagate it across context switches. Set it before each secondary CPU
startup, including parallel bringup and CPU hotplug.
Link: https://lists.riscv.org/g/tech-privileged/topic/risc_v_tech_arch_review/113437553?page=2
Link: https://lore.kernel.org/r/20260826012457.ig3rfmji7vywf6kc@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
arch/riscv/include/asm/asm.h | 6 +-----
arch/riscv/include/asm/percpu.h | 16 ++++++++++++++++
arch/riscv/include/asm/switch_to.h | 9 +++++++++
arch/riscv/include/asm/thread_info.h | 5 ++++-
arch/riscv/kernel/asm-offsets.c | 4 +++-
arch/riscv/kernel/smpboot.c | 10 +++++++++-
arch/riscv/net/bpf_jit_comp64.c | 15 ++-------------
7 files changed, 44 insertions(+), 21 deletions(-)
create mode 100644 arch/riscv/include/asm/percpu.h
diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h
index b8bf842d4c136..06ac38f733b0a 100644
--- a/arch/riscv/include/asm/asm.h
+++ b/arch/riscv/include/asm/asm.h
@@ -92,11 +92,7 @@
#ifdef CONFIG_SMP
.macro asm_per_cpu dst sym tmp
- lw \tmp, TASK_TI_CPU_NUM(tp)
- slli \tmp, \tmp, RISCV_LGPTR
- la \dst, __per_cpu_offset
- add \dst, \dst, \tmp
- REG_L \tmp, 0(\dst)
+ REG_L \tmp, TASK_TI_PCPU_OFFSET(tp)
la \dst, \sym
add \dst, \dst, \tmp
.endm
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
new file mode 100644
index 0000000000000..bbb88a42b48f8
--- /dev/null
+++ b/arch/riscv/include/asm/percpu.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ASM_PERCPU_H
+#define __ASM_PERCPU_H
+
+#include <linux/thread_info.h>
+
+#include <asm/current.h>
+
+#ifdef CONFIG_SMP
+#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
+#endif
+
+#include <asm-generic/percpu.h>
+
+#endif /* __ASM_PERCPU_H */
diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
index 970f89935f0ee..1b11c79847610 100644
--- a/arch/riscv/include/asm/switch_to.h
+++ b/arch/riscv/include/asm/switch_to.h
@@ -107,6 +107,14 @@ static inline void __switch_to_envcfg(struct task_struct *next)
:: "r" (next->thread.envcfg) : "memory");
}
+static inline void __switch_to_pcpu_offset(struct task_struct *prev,
+ struct task_struct *next)
+{
+#ifdef CONFIG_SMP
+ next->thread_info.pcpu_offset = prev->thread_info.pcpu_offset;
+#endif
+}
+
extern struct task_struct *__switch_to(struct task_struct *,
struct task_struct *);
@@ -143,6 +151,7 @@ do { \
if (switch_to_should_flush_icache(__next)) \
local_flush_icache_all(); \
__switch_to_envcfg(__next); \
+ __switch_to_pcpu_offset(__prev, __next); \
((last) = __switch_to(__prev, __next)); \
} while (0)
diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
index 55019fdfa9eca..523e95c1eedb8 100644
--- a/arch/riscv/include/asm/thread_info.h
+++ b/arch/riscv/include/asm/thread_info.h
@@ -53,6 +53,7 @@
struct thread_info {
unsigned long flags; /* low level flags */
int preempt_count; /* 0=>preemptible, <0=>BUG */
+ int cpu;
/*
* These stack pointers are overwritten on every system call or
* exception. SP is also saved to the stack it can be recovered when
@@ -60,8 +61,10 @@ struct thread_info {
*/
long kernel_sp; /* Kernel stack pointer */
long user_sp; /* User stack pointer */
- int cpu;
unsigned long syscall_work; /* SYSCALL_WORK_ flags */
+#ifdef CONFIG_SMP
+ unsigned long pcpu_offset;
+#endif
#ifdef CONFIG_SHADOW_CALL_STACK
void *scs_base;
void *scs_sp;
diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c
index a75f0cfea1e9f..50fdfe75eaf78 100644
--- a/arch/riscv/kernel/asm-offsets.c
+++ b/arch/riscv/kernel/asm-offsets.c
@@ -38,6 +38,9 @@ void asm_offsets(void)
OFFSET(TASK_THREAD_SUM, task_struct, thread.sum);
OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu);
+#ifdef CONFIG_SMP
+ OFFSET(TASK_TI_PCPU_OFFSET, task_struct, thread_info.pcpu_offset);
+#endif
OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count);
OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp);
OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp);
@@ -50,7 +53,6 @@ void asm_offsets(void)
OFFSET(TASK_TI_A2, task_struct, thread_info.a2);
#endif
- OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu);
#ifdef CONFIG_RISCV_USER_CFI
OFFSET(TASK_TI_CFI_STATE, task_struct, thread_info.user_cfi_state);
OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk);
diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c
index 6436cf1fac067..a40f0c47a238d 100644
--- a/arch/riscv/kernel/smpboot.c
+++ b/arch/riscv/kernel/smpboot.c
@@ -177,6 +177,10 @@ void __init setup_smp(void)
static int start_secondary_cpu(int cpu, struct task_struct *tidle)
{
+ /* The idle task is reused across CPU hotplug. */
+ tidle->thread_info.cpu = cpu;
+ tidle->thread_info.pcpu_offset = per_cpu_offset(cpu);
+
if (cpu_ops->cpu_start)
return cpu_ops->cpu_start(cpu, tidle);
@@ -192,7 +196,6 @@ int arch_cpuhp_kick_ap_alive(unsigned int cpu, struct task_struct *tidle)
int __cpu_up(unsigned int cpu, struct task_struct *tidle)
{
int ret;
- tidle->thread_info.cpu = cpu;
ret = start_secondary_cpu(cpu, tidle);
if (!ret) {
@@ -210,6 +213,11 @@ int __cpu_up(unsigned int cpu, struct task_struct *tidle)
}
#endif
+void __init smp_prepare_boot_cpu(void)
+{
+ __my_cpu_offset = per_cpu_offset(smp_processor_id());
+}
+
void __init smp_cpus_done(unsigned int max_cpus)
{
}
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 7d15617aee1ba..b7b2429f31be4 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -12,7 +12,6 @@
#include <linux/smp.h>
#include <asm/text-patching.h>
#include <asm/cfi.h>
-#include <asm/percpu.h>
#include "bpf_jit.h"
/*
@@ -1553,15 +1552,8 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
if (rd != rs)
emit_mv(rd, rs, ctx);
#ifdef CONFIG_SMP
- /* Load current CPU number in T1 */
- emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu),
+ emit_ld(RV_REG_T1, offsetof(struct thread_info, pcpu_offset),
RV_REG_TP, ctx);
- /* Load address of __per_cpu_offset array in T2 */
- emit_addr(RV_REG_T2, (u64)&__per_cpu_offset, extra_pass, ctx);
- /* Get address of __per_cpu_offset[cpu] in T1 */
- emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx);
- /* Load __per_cpu_offset[cpu] in T1 */
- emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx);
/* Add the offset to Rd */
emit_add(rd, rd, RV_REG_T1, ctx);
#endif
@@ -2228,10 +2220,7 @@ static void emit_priv_stack_frame(struct rv_jit_context *ctx)
fp += round_up(ctx->prog->aux->stack_depth, STACK_ALIGN);
emit_imm(RV_REG_S5, fp, ctx);
#ifdef CONFIG_SMP
- emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu), RV_REG_TP, ctx);
- emit_imm(RV_REG_T2, (u64)__per_cpu_offset, ctx);
- emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx);
- emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx);
+ emit_ld(RV_REG_T1, offsetof(struct thread_info, pcpu_offset), RV_REG_TP, ctx);
emit_add(RV_REG_S5, RV_REG_S5, RV_REG_T1, ctx);
#endif
}
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 02/10] riscv: add infrastructure for preemptible this_cpu operations
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 01/10] riscv: store percpu offset into thread_info Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 10:09 ` David Laight
2026-12-04 8:58 ` [PATCH v12 03/10] riscv: add common helpers " Yunhui Cui
` (7 subsequent siblings)
9 siblings, 1 reply; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
Generic this_cpu operations disable preemption or local interrupts to
keep per-CPU accesses on the current CPU. Provide infrastructure for
RISC-V operations to remain preemptible.
Track the registers used for per-CPU address generation so exception
return can repair an interrupted operation after CPU migration.
Preserve this state across exception handlers so handlers can use
per-CPU operations independently. Make the entry/exit protocol available
to NMI-like paths, including SBI Supervisor Software Events.
Suggested-by: David Laight <david.laight.linux@gmail.com>
Link: https://lore.kernel.org/r/20261108213254.67805ef0@pumpkin
Suggested-by: Jisheng Zhang <jszhang@kernel.org>
Link: https://lore.kernel.org/r/avG07pmpcLGkq5i-@xhacker
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
arch/riscv/include/asm/asm.h | 21 ++++++++++++
arch/riscv/include/asm/percpu.h | 50 +++++++++++++++++++++++++++-
arch/riscv/include/asm/ptrace.h | 26 +++++++++++++++
arch/riscv/include/asm/thread_info.h | 3 ++
arch/riscv/kernel/Makefile | 6 ++++
arch/riscv/kernel/asm-offsets.c | 4 +++
arch/riscv/kernel/entry-common.c | 30 +++++++++++++++++
arch/riscv/kernel/entry.S | 17 ++++++++++
arch/riscv/mm/extable.c | 18 ----------
9 files changed, 156 insertions(+), 19 deletions(-)
create mode 100644 arch/riscv/kernel/entry-common.c
diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h
index 06ac38f733b0a..6515adb29dc60 100644
--- a/arch/riscv/include/asm/asm.h
+++ b/arch/riscv/include/asm/asm.h
@@ -81,6 +81,27 @@
/* Common assembly source macros */
+#ifdef CONFIG_SMP
+/*
+ * Save and clear the active per-CPU GPR state on exception entry. Entry
+ * paths which bypass handle_exception, such as SBI Supervisor Software
+ * Events (SSE), must pair this with pcpu_gprs_exit around their handlers.
+ */
+.macro pcpu_gprs_entry tsk regs tmp
+ lhu \tmp, TASK_TI_PCPU_GPRS(\tsk)
+ REG_S \tmp, PT_PCPU_GPRS(\regs)
+ sh zero, TASK_TI_PCPU_GPRS(\tsk)
+.endm
+
+/*
+ * Restore the saved state before resuming the interrupted context. tmp must
+ * contain PT_PCPU_GPRS from the interrupted frame.
+ */
+.macro pcpu_gprs_exit tsk tmp
+ sh \tmp, TASK_TI_PCPU_GPRS(\tsk)
+.endm
+#endif
+
/*
* NOP sequence
*/
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index bbb88a42b48f8..4d88c6830a7e5 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -3,13 +3,61 @@
#ifndef __ASM_PERCPU_H
#define __ASM_PERCPU_H
+#include <linux/bits.h>
+#include <linux/stringify.h>
#include <linux/thread_info.h>
+#include <asm/asm.h>
#include <asm/current.h>
+#include <asm/gpr-num.h>
#ifdef CONFIG_SMP
#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
-#endif
+
+#define PCPU_GPR_PCP_SHIFT 0
+#define PCPU_GPR_PCP GENMASK(4, 0)
+#define PCPU_GPR_OFF_SHIFT 5
+#define PCPU_GPR_OFF GENMASK(9, 5)
+#define PCPU_GPR_ADDR_SHIFT 10
+#define PCPU_GPR_ADDR GENMASK(14, 10)
+
+#define __PCPU_GPR_NUM(gpr) ".L__gpr_num_%[" __stringify(gpr) "]"
+
+#define __PCPU_GPRS_VALUE \
+ "(" \
+ "(.L__pcpu_gpr_pcp << " __stringify(PCPU_GPR_PCP_SHIFT) ") | " \
+ "(.L__pcpu_gpr_off << " __stringify(PCPU_GPR_OFF_SHIFT) ") | " \
+ "(.L__pcpu_gpr_addr << " __stringify(PCPU_GPR_ADDR_SHIFT) ")" \
+ ")"
+
+#define __PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr) \
+ ".set .L__pcpu_gpr_pcp, " __PCPU_GPR_NUM(pcp) "\n" \
+ ".set .L__pcpu_gpr_off, " __PCPU_GPR_NUM(off) "\n" \
+ ".set .L__pcpu_gpr_addr, " __PCPU_GPR_NUM(addr) "\n" \
+ ".if ((.L__pcpu_gpr_pcp == .L__pcpu_gpr_off) || " \
+ "(.L__pcpu_gpr_pcp == .L__pcpu_gpr_addr) || " \
+ "(.L__pcpu_gpr_off == .L__pcpu_gpr_addr))\n" \
+ ".error \"PCPU GPRs overlap\"\n" \
+ ".endif\n"
+
+/*
+ * Mark a region where the per-CPU offset and address registers may need to
+ * be rewritten by exception return after migration. The three registers
+ * must remain distinct for the whole region.
+ */
+#define __PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
+ __DEFINE_ASM_GPR_NUMS \
+ __PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr) \
+ "li %[" __stringify(off) "], " __PCPU_GPRS_VALUE "\n" \
+ "sh %[" __stringify(off) "], %[" __stringify(gprs) "]\n" \
+ REG_L " %[" __stringify(off) "], %[" __stringify(pcpu_offset) "]\n" \
+ "add %[" __stringify(addr) "], %[" __stringify(pcp) "], " \
+ "%[" __stringify(off) "]\n"
+
+#define __PCPU_GPRS_END(gprs) \
+ "sh zero, %[" __stringify(gprs) "]\n"
+
+#endif /* CONFIG_SMP */
#include <asm-generic/percpu.h>
diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
index addc8188152f7..338612cb5d28d 100644
--- a/arch/riscv/include/asm/ptrace.h
+++ b/arch/riscv/include/asm/ptrace.h
@@ -53,6 +53,10 @@ struct pt_regs {
unsigned long cause;
/* a0 value before the syscall */
unsigned long orig_a0;
+#ifdef CONFIG_SMP
+ /* Active preemptible per-CPU operation, if any. */
+ unsigned long pcpu_gprs;
+#endif
};
#define PTRACE_SYSEMU 0x1f
@@ -145,6 +149,28 @@ static inline unsigned long regs_get_register(struct pt_regs *regs,
return *(unsigned long *)((unsigned long)regs + offset);
}
+static __always_inline unsigned long
+regs_get_gpr(struct pt_regs *regs, unsigned int offset)
+{
+ if (unlikely_notrace(!offset || offset > MAX_REG_OFFSET))
+ return 0;
+
+ return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+static __always_inline void regs_set_gpr(struct pt_regs *regs,
+ unsigned int offset, unsigned long val)
+{
+ if (unlikely_notrace(!offset || offset > MAX_REG_OFFSET))
+ return;
+
+ *(unsigned long *)((unsigned long)regs + offset) = val;
+}
+
+#ifdef CONFIG_SMP
+void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs);
+#endif
+
/**
* regs_get_kernel_argument() - get Nth function argument in kernel
* @regs: pt_regs of that context
diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
index 523e95c1eedb8..4a123d40fbf4d 100644
--- a/arch/riscv/include/asm/thread_info.h
+++ b/arch/riscv/include/asm/thread_info.h
@@ -79,6 +79,9 @@ struct thread_info {
#ifdef CONFIG_RISCV_USER_CFI
struct cfi_state user_cfi_state;
#endif
+#ifdef CONFIG_SMP
+ unsigned short pcpu_gprs;
+#endif
};
#ifdef CONFIG_SHADOW_CALL_STACK
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index f54c336bf547c..1f0639d807f1a 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -3,6 +3,11 @@
# Makefile for the RISC-V Linux kernel
#
+KASAN_SANITIZE_entry-common.o := n
+UBSAN_SANITIZE_entry-common.o := n
+KCOV_INSTRUMENT_entry-common.o := n
+CFLAGS_entry-common.o += -DDISABLE_BRANCH_PROFILING
+
CFLAGS_syscall_table.o += $(call cc-disable-warning, override-init)
CFLAGS_compat_syscall_table.o += $(call cc-disable-warning, override-init)
@@ -52,6 +57,7 @@ obj-y += alternative.o
obj-y += cpu.o
obj-y += cpufeature.o
obj-y += entry.o
+obj-$(CONFIG_SMP) += entry-common.o
obj-y += irq.o
obj-y += process.o
obj-y += ptrace.o
diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c
index 50fdfe75eaf78..9cfd0ee8e6316 100644
--- a/arch/riscv/kernel/asm-offsets.c
+++ b/arch/riscv/kernel/asm-offsets.c
@@ -40,6 +40,7 @@ void asm_offsets(void)
OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu);
#ifdef CONFIG_SMP
OFFSET(TASK_TI_PCPU_OFFSET, task_struct, thread_info.pcpu_offset);
+ OFFSET(TASK_TI_PCPU_GPRS, task_struct, thread_info.pcpu_gprs);
#endif
OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count);
OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp);
@@ -132,6 +133,9 @@ void asm_offsets(void)
OFFSET(PT_STATUS, pt_regs, status);
OFFSET(PT_BADADDR, pt_regs, badaddr);
OFFSET(PT_CAUSE, pt_regs, cause);
+#ifdef CONFIG_SMP
+ OFFSET(PT_PCPU_GPRS, pt_regs, pcpu_gprs);
+#endif
OFFSET(SUSPEND_CONTEXT_REGS, suspend_context, regs);
diff --git a/arch/riscv/kernel/entry-common.c b/arch/riscv/kernel/entry-common.c
new file mode 100644
index 0000000000000..164505962d131
--- /dev/null
+++ b/arch/riscv/kernel/entry-common.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/bitfield.h>
+#include <linux/compiler.h>
+#include <linux/percpu.h>
+
+#include <asm/current.h>
+#include <asm/ptrace.h>
+
+/*
+ * If the interrupted context had an active per-CPU operation, rewrite its
+ * saved offset and address registers for the CPU on which it will resume.
+ */
+void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs)
+{
+ unsigned long pcpu_offset = __my_cpu_offset;
+ unsigned long gprs = regs->pcpu_gprs;
+ unsigned long pcp;
+ unsigned int reg_pcp;
+ unsigned int reg_off;
+ unsigned int reg_addr;
+
+ reg_pcp = FIELD_GET(PCPU_GPR_PCP, gprs);
+ reg_off = FIELD_GET(PCPU_GPR_OFF, gprs);
+ reg_addr = FIELD_GET(PCPU_GPR_ADDR, gprs);
+
+ pcp = regs_get_gpr(regs, reg_pcp * sizeof(unsigned long));
+ regs_set_gpr(regs, reg_off * sizeof(unsigned long), pcpu_offset);
+ regs_set_gpr(regs, reg_addr * sizeof(unsigned long), pcp + pcpu_offset);
+}
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index 628b85e3915a7..bb50317deecc5 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -198,6 +198,10 @@ SYM_CODE_START(handle_exception)
REG_S s4, PT_CAUSE(sp)
REG_S s5, PT_TP(sp)
+#ifdef CONFIG_SMP
+ pcpu_gprs_entry tp sp s0
+#endif
+
/*
* Set the scratch register to 0, so that if a recursive exception
* occurs, the exception vector knows it came from the kernel
@@ -281,6 +285,16 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
move a0, sp
call riscv_v_context_nesting_end
#endif
+#ifdef CONFIG_SMP
+ REG_L t0, PT_PCPU_GPRS(sp)
+ beqz t0, .Lpcpu_regs_restored
+ move a0, sp
+ call riscv_pcpu_regs_fixup
+ REG_L t0, PT_PCPU_GPRS(sp)
+ pcpu_gprs_exit tp t0
+.Lpcpu_regs_restored:
+#endif
+
REG_L a0, PT_STATUS(sp)
restore_userssp s3, a0
/*
@@ -341,6 +355,9 @@ SYM_CODE_START_LOCAL(handle_kernel_stack_overflow)
REG_S x3, PT_GP(sp)
REG_S x5, PT_T0(sp)
save_from_x6_to_x31
+#ifdef CONFIG_SMP
+ pcpu_gprs_entry tp sp s0
+#endif
REG_L s0, TASK_TI_KERNEL_SP(tp)
csrr s1, CSR_STATUS
diff --git a/arch/riscv/mm/extable.c b/arch/riscv/mm/extable.c
index e252eba55edec..cb64c58148a83 100644
--- a/arch/riscv/mm/extable.c
+++ b/arch/riscv/mm/extable.c
@@ -27,24 +27,6 @@ static bool ex_handler_fixup(const struct exception_table_entry *ex,
return true;
}
-static inline unsigned long regs_get_gpr(struct pt_regs *regs, unsigned int offset)
-{
- if (unlikely(!offset || offset > MAX_REG_OFFSET))
- return 0;
-
- return *(unsigned long *)((unsigned long)regs + offset);
-}
-
-static inline void regs_set_gpr(struct pt_regs *regs, unsigned int offset,
- unsigned long val)
-{
- if (unlikely(offset > MAX_REG_OFFSET))
- return;
-
- if (offset)
- *(unsigned long *)((unsigned long)regs + offset) = val;
-}
-
static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
struct pt_regs *regs)
{
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH v12 02/10] riscv: add infrastructure for preemptible this_cpu operations
2026-12-04 8:58 ` [PATCH v12 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
@ 2026-12-04 10:09 ` David Laight
0 siblings, 0 replies; 12+ messages in thread
From: David Laight @ 2026-12-04 10:09 UTC (permalink / raw)
To: Yunhui Cui
Cc: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, dave, jszhang,
tglx, conor.dooley, thuth, ajones, ben.dooks, rkrcmar,
samuel.holland, zong.li, debug, seanwascoding, andybnac,
menglong8.dong, cyrilbur, wangruikang, atishp, apatel, arnd,
nathan, nick.desaulniers+lkml, morbo, justinstitt, qingfang.deng,
linux-riscv, linux-kernel, linux-mm, bpf, linux-arch, llvm
On Fri, 4 Dec 2026 16:58:50 +0800
Yunhui Cui <cuiyunhui@bytedance.com> wrote:
> Generic this_cpu operations disable preemption or local interrupts to
> keep per-CPU accesses on the current CPU. Provide infrastructure for
> RISC-V operations to remain preemptible.
>
> Track the registers used for per-CPU address generation so exception
> return can repair an interrupted operation after CPU migration.
>
> Preserve this state across exception handlers so handlers can use
> per-CPU operations independently. Make the entry/exit protocol available
> to NMI-like paths, including SBI Supervisor Software Events.
>
> Suggested-by: David Laight <david.laight.linux@gmail.com>
> Link: https://lore.kernel.org/r/20261108213254.67805ef0@pumpkin
> Suggested-by: Jisheng Zhang <jszhang@kernel.org>
> Link: https://lore.kernel.org/r/avG07pmpcLGkq5i-@xhacker
> Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
> ---
> arch/riscv/include/asm/asm.h | 21 ++++++++++++
> arch/riscv/include/asm/percpu.h | 50 +++++++++++++++++++++++++++-
> arch/riscv/include/asm/ptrace.h | 26 +++++++++++++++
> arch/riscv/include/asm/thread_info.h | 3 ++
> arch/riscv/kernel/Makefile | 6 ++++
> arch/riscv/kernel/asm-offsets.c | 4 +++
> arch/riscv/kernel/entry-common.c | 30 +++++++++++++++++
> arch/riscv/kernel/entry.S | 17 ++++++++++
> arch/riscv/mm/extable.c | 18 ----------
> 9 files changed, 156 insertions(+), 19 deletions(-)
> create mode 100644 arch/riscv/kernel/entry-common.c
>
> diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h
> index 06ac38f733b0a..6515adb29dc60 100644
> --- a/arch/riscv/include/asm/asm.h
> +++ b/arch/riscv/include/asm/asm.h
> @@ -81,6 +81,27 @@
>
> /* Common assembly source macros */
>
> +#ifdef CONFIG_SMP
> +/*
> + * Save and clear the active per-CPU GPR state on exception entry. Entry
> + * paths which bypass handle_exception, such as SBI Supervisor Software
> + * Events (SSE), must pair this with pcpu_gprs_exit around their handlers.
> + */
> +.macro pcpu_gprs_entry tsk regs tmp
> + lhu \tmp, TASK_TI_PCPU_GPRS(\tsk)
> + REG_S \tmp, PT_PCPU_GPRS(\regs)
> + sh zero, TASK_TI_PCPU_GPRS(\tsk)
> +.endm
> +
> +/*
> + * Restore the saved state before resuming the interrupted context. tmp must
> + * contain PT_PCPU_GPRS from the interrupted frame.
> + */
> +.macro pcpu_gprs_exit tsk tmp
> + sh \tmp, TASK_TI_PCPU_GPRS(\tsk)
> +.endm
> +#endif
> +
> /*
> * NOP sequence
> */
> diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
> index bbb88a42b48f8..4d88c6830a7e5 100644
> --- a/arch/riscv/include/asm/percpu.h
> +++ b/arch/riscv/include/asm/percpu.h
> @@ -3,13 +3,61 @@
> #ifndef __ASM_PERCPU_H
> #define __ASM_PERCPU_H
>
> +#include <linux/bits.h>
> +#include <linux/stringify.h>
> #include <linux/thread_info.h>
>
> +#include <asm/asm.h>
> #include <asm/current.h>
> +#include <asm/gpr-num.h>
>
> #ifdef CONFIG_SMP
> #define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
> -#endif
> +
> +#define PCPU_GPR_PCP_SHIFT 0
> +#define PCPU_GPR_PCP GENMASK(4, 0)
> +#define PCPU_GPR_OFF_SHIFT 5
> +#define PCPU_GPR_OFF GENMASK(9, 5)
> +#define PCPU_GPR_ADDR_SHIFT 10
> +#define PCPU_GPR_ADDR GENMASK(14, 10)
> +
> +#define __PCPU_GPR_NUM(gpr) ".L__gpr_num_%[" __stringify(gpr) "]"
> +
> +#define __PCPU_GPRS_VALUE \
> + "(" \
> + "(.L__pcpu_gpr_pcp << " __stringify(PCPU_GPR_PCP_SHIFT) ") | " \
> + "(.L__pcpu_gpr_off << " __stringify(PCPU_GPR_OFF_SHIFT) ") | " \
> + "(.L__pcpu_gpr_addr << " __stringify(PCPU_GPR_ADDR_SHIFT) ")" \
> + ")"
> +
> +#define __PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr) \
> + ".set .L__pcpu_gpr_pcp, " __PCPU_GPR_NUM(pcp) "\n" \
> + ".set .L__pcpu_gpr_off, " __PCPU_GPR_NUM(off) "\n" \
> + ".set .L__pcpu_gpr_addr, " __PCPU_GPR_NUM(addr) "\n" \
> + ".if ((.L__pcpu_gpr_pcp == .L__pcpu_gpr_off) || " \
> + "(.L__pcpu_gpr_pcp == .L__pcpu_gpr_addr) || " \
> + "(.L__pcpu_gpr_off == .L__pcpu_gpr_addr))\n" \
> + ".error \"PCPU GPRs overlap\"\n" \
> + ".endif\n"
> +
> +/*
> + * Mark a region where the per-CPU offset and address registers may need to
> + * be rewritten by exception return after migration. The three registers
> + * must remain distinct for the whole region.
> + */
> +#define __PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
> + __DEFINE_ASM_GPR_NUMS \
> + __PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr) \
> + "li %[" __stringify(off) "], " __PCPU_GPRS_VALUE "\n" \
> + "sh %[" __stringify(off) "], %[" __stringify(gprs) "]\n" \
> + REG_L " %[" __stringify(off) "], %[" __stringify(pcpu_offset) "]\n" \
> + "add %[" __stringify(addr) "], %[" __stringify(pcp) "], " \
> + "%[" __stringify(off) "]\n"
> +
> +#define __PCPU_GPRS_END(gprs) \
> + "sh zero, %[" __stringify(gprs) "]\n"
> +
> +#endif /* CONFIG_SMP */
>
> #include <asm-generic/percpu.h>
>
> diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> index addc8188152f7..338612cb5d28d 100644
> --- a/arch/riscv/include/asm/ptrace.h
> +++ b/arch/riscv/include/asm/ptrace.h
> @@ -53,6 +53,10 @@ struct pt_regs {
> unsigned long cause;
> /* a0 value before the syscall */
> unsigned long orig_a0;
> +#ifdef CONFIG_SMP
> + /* Active preemptible per-CPU operation, if any. */
> + unsigned long pcpu_gprs;
> +#endif
> };
>
> #define PTRACE_SYSEMU 0x1f
> @@ -145,6 +149,28 @@ static inline unsigned long regs_get_register(struct pt_regs *regs,
> return *(unsigned long *)((unsigned long)regs + offset);
> }
>
> +static __always_inline unsigned long
> +regs_get_gpr(struct pt_regs *regs, unsigned int offset)
> +{
> + if (unlikely_notrace(!offset || offset > MAX_REG_OFFSET))
> + return 0;
> +
> + return *(unsigned long *)((unsigned long)regs + offset);
> +}
> +
> +static __always_inline void regs_set_gpr(struct pt_regs *regs,
> + unsigned int offset, unsigned long val)
> +{
> + if (unlikely_notrace(!offset || offset > MAX_REG_OFFSET))
> + return;
> +
> + *(unsigned long *)((unsigned long)regs + offset) = val;
> +}
> +
> +#ifdef CONFIG_SMP
> +void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs);
> +#endif
> +
> /**
> * regs_get_kernel_argument() - get Nth function argument in kernel
> * @regs: pt_regs of that context
> diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
> index 523e95c1eedb8..4a123d40fbf4d 100644
> --- a/arch/riscv/include/asm/thread_info.h
> +++ b/arch/riscv/include/asm/thread_info.h
> @@ -79,6 +79,9 @@ struct thread_info {
> #ifdef CONFIG_RISCV_USER_CFI
> struct cfi_state user_cfi_state;
> #endif
> +#ifdef CONFIG_SMP
> + unsigned short pcpu_gprs;
> +#endif
While the value is only 16 bits, making this 'short' starts looking like
a 'recipe for disaster', especially since the field name is the same as
64bit fields in other structures and the local variables are 'long'
not even 'int'.
It isn't as though you've dropped into some padding.
David
> };
>
> #ifdef CONFIG_SHADOW_CALL_STACK
> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
> index f54c336bf547c..1f0639d807f1a 100644
> --- a/arch/riscv/kernel/Makefile
> +++ b/arch/riscv/kernel/Makefile
> @@ -3,6 +3,11 @@
> # Makefile for the RISC-V Linux kernel
> #
>
> +KASAN_SANITIZE_entry-common.o := n
> +UBSAN_SANITIZE_entry-common.o := n
> +KCOV_INSTRUMENT_entry-common.o := n
> +CFLAGS_entry-common.o += -DDISABLE_BRANCH_PROFILING
> +
> CFLAGS_syscall_table.o += $(call cc-disable-warning, override-init)
> CFLAGS_compat_syscall_table.o += $(call cc-disable-warning, override-init)
>
> @@ -52,6 +57,7 @@ obj-y += alternative.o
> obj-y += cpu.o
> obj-y += cpufeature.o
> obj-y += entry.o
> +obj-$(CONFIG_SMP) += entry-common.o
> obj-y += irq.o
> obj-y += process.o
> obj-y += ptrace.o
> diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c
> index 50fdfe75eaf78..9cfd0ee8e6316 100644
> --- a/arch/riscv/kernel/asm-offsets.c
> +++ b/arch/riscv/kernel/asm-offsets.c
> @@ -40,6 +40,7 @@ void asm_offsets(void)
> OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu);
> #ifdef CONFIG_SMP
> OFFSET(TASK_TI_PCPU_OFFSET, task_struct, thread_info.pcpu_offset);
> + OFFSET(TASK_TI_PCPU_GPRS, task_struct, thread_info.pcpu_gprs);
> #endif
> OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count);
> OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp);
> @@ -132,6 +133,9 @@ void asm_offsets(void)
> OFFSET(PT_STATUS, pt_regs, status);
> OFFSET(PT_BADADDR, pt_regs, badaddr);
> OFFSET(PT_CAUSE, pt_regs, cause);
> +#ifdef CONFIG_SMP
> + OFFSET(PT_PCPU_GPRS, pt_regs, pcpu_gprs);
> +#endif
>
> OFFSET(SUSPEND_CONTEXT_REGS, suspend_context, regs);
>
> diff --git a/arch/riscv/kernel/entry-common.c b/arch/riscv/kernel/entry-common.c
> new file mode 100644
> index 0000000000000..164505962d131
> --- /dev/null
> +++ b/arch/riscv/kernel/entry-common.c
> @@ -0,0 +1,30 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <linux/bitfield.h>
> +#include <linux/compiler.h>
> +#include <linux/percpu.h>
> +
> +#include <asm/current.h>
> +#include <asm/ptrace.h>
> +
> +/*
> + * If the interrupted context had an active per-CPU operation, rewrite its
> + * saved offset and address registers for the CPU on which it will resume.
> + */
> +void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs)
> +{
> + unsigned long pcpu_offset = __my_cpu_offset;
> + unsigned long gprs = regs->pcpu_gprs;
> + unsigned long pcp;
> + unsigned int reg_pcp;
> + unsigned int reg_off;
> + unsigned int reg_addr;
> +
> + reg_pcp = FIELD_GET(PCPU_GPR_PCP, gprs);
> + reg_off = FIELD_GET(PCPU_GPR_OFF, gprs);
> + reg_addr = FIELD_GET(PCPU_GPR_ADDR, gprs);
> +
> + pcp = regs_get_gpr(regs, reg_pcp * sizeof(unsigned long));
> + regs_set_gpr(regs, reg_off * sizeof(unsigned long), pcpu_offset);
> + regs_set_gpr(regs, reg_addr * sizeof(unsigned long), pcp + pcpu_offset);
> +}
> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
> index 628b85e3915a7..bb50317deecc5 100644
> --- a/arch/riscv/kernel/entry.S
> +++ b/arch/riscv/kernel/entry.S
> @@ -198,6 +198,10 @@ SYM_CODE_START(handle_exception)
> REG_S s4, PT_CAUSE(sp)
> REG_S s5, PT_TP(sp)
>
> +#ifdef CONFIG_SMP
> + pcpu_gprs_entry tp sp s0
> +#endif
> +
> /*
> * Set the scratch register to 0, so that if a recursive exception
> * occurs, the exception vector knows it came from the kernel
> @@ -281,6 +285,16 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
> move a0, sp
> call riscv_v_context_nesting_end
> #endif
> +#ifdef CONFIG_SMP
> + REG_L t0, PT_PCPU_GPRS(sp)
> + beqz t0, .Lpcpu_regs_restored
> + move a0, sp
> + call riscv_pcpu_regs_fixup
> + REG_L t0, PT_PCPU_GPRS(sp)
> + pcpu_gprs_exit tp t0
> +.Lpcpu_regs_restored:
> +#endif
> +
> REG_L a0, PT_STATUS(sp)
> restore_userssp s3, a0
> /*
> @@ -341,6 +355,9 @@ SYM_CODE_START_LOCAL(handle_kernel_stack_overflow)
> REG_S x3, PT_GP(sp)
> REG_S x5, PT_T0(sp)
> save_from_x6_to_x31
> +#ifdef CONFIG_SMP
> + pcpu_gprs_entry tp sp s0
> +#endif
>
> REG_L s0, TASK_TI_KERNEL_SP(tp)
> csrr s1, CSR_STATUS
> diff --git a/arch/riscv/mm/extable.c b/arch/riscv/mm/extable.c
> index e252eba55edec..cb64c58148a83 100644
> --- a/arch/riscv/mm/extable.c
> +++ b/arch/riscv/mm/extable.c
> @@ -27,24 +27,6 @@ static bool ex_handler_fixup(const struct exception_table_entry *ex,
> return true;
> }
>
> -static inline unsigned long regs_get_gpr(struct pt_regs *regs, unsigned int offset)
> -{
> - if (unlikely(!offset || offset > MAX_REG_OFFSET))
> - return 0;
> -
> - return *(unsigned long *)((unsigned long)regs + offset);
> -}
> -
> -static inline void regs_set_gpr(struct pt_regs *regs, unsigned int offset,
> - unsigned long val)
> -{
> - if (unlikely(offset > MAX_REG_OFFSET))
> - return;
> -
> - if (offset)
> - *(unsigned long *)((unsigned long)regs + offset) = val;
> -}
> -
> static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
> struct pt_regs *regs)
> {
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v12 03/10] riscv: add common helpers for preemptible this_cpu operations
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 01/10] riscv: store percpu offset into thread_info Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 04/10] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
` (6 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
Void RMW and add-return operations need the same atomic updates, while
exchange operations can share the AMO execution. Provide common
helpers 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@bytedance.com>
---
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 4d88c6830a7e5..6fc4cfd6b679e 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>
#ifdef CONFIG_SMP
#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
@@ -57,6 +60,134 @@
#define __PCPU_GPRS_END(gprs) \
"sh zero, %[" __stringify(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) \
+{ \
+ 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; \
+ } \
+ \
+ __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; \
+ \
+ 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); \
+ \
+ (__force 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
+
#endif /* CONFIG_SMP */
#include <asm-generic/percpu.h>
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 04/10] riscv: implement preemptible this_cpu read and write operations
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
` (2 preceding siblings ...)
2026-12-04 8:58 ` [PATCH v12 03/10] riscv: add common helpers " Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 05/10] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
` (5 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
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@bytedance.com>
---
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 6fc4cfd6b679e..d46a32c78c5dc 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -178,7 +178,73 @@ __percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val) \
(__force 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, (__force unsigned long)(val))
+#define this_cpu_write_2(pcp, val) \
+ _pcp_wrap(__percpu_write_16, pcp, (__force unsigned long)(val))
+#define this_cpu_write_4(pcp, val) \
+ _pcp_wrap(__percpu_write_32, pcp, (__force unsigned long)(val))
+
+#ifdef CONFIG_64BIT
+#define this_cpu_write_8(pcp, val) \
+ _pcp_wrap(__percpu_write_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
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 05/10] riscv: implement preemptible this_cpu void RMW operations
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
` (3 preceding siblings ...)
2026-12-04 8:58 ` [PATCH v12 04/10] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 06/10] riscv: implement preemptible this_cpu add_return operations Yunhui Cui
` (4 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
Apply the recoverable per-CPU address handling to addition and bitwise
AND/OR operations. Use the common RMW helpers through the void wrapper
to make the updates atomic against interrupt handlers without masking
local interrupts or disabling preemption.
Link: https://lore.kernel.org/r/20260826010748.zphu3wh2hlm4t4o2@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
arch/riscv/include/asm/percpu.h | 38 +++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index d46a32c78c5dc..6d0a11b49563f 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -223,6 +223,23 @@ PERCPU_RW_OPS("ld", "sd", 64)
PERCPU_RW_OPS("lw", "sw", 32)
#endif
+__PERCPU_AMO_OP_CASE(add, 32, .w, +)
+__PERCPU_AMO_OP_CASE(and, 32, .w, &)
+__PERCPU_AMO_OP_CASE(or, 32, .w, |)
+
+#ifdef CONFIG_64BIT
+__PERCPU_AMO_OP_CASE(add, 64, .d, +)
+__PERCPU_AMO_OP_CASE(and, 64, .d, &)
+__PERCPU_AMO_OP_CASE(or, 64, .d, |)
+#endif
+
+__PERCPU_8_16_OP_CASE(add, 8, .b, +)
+__PERCPU_8_16_OP_CASE(add, 16, .h, +)
+__PERCPU_8_16_OP_CASE(and, 8, .b, &)
+__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 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)
@@ -243,6 +260,27 @@ PERCPU_RW_OPS("lw", "sw", 32)
_pcp_wrap(__percpu_write_64, pcp, (__force unsigned long)(val))
#endif
+#define this_cpu_add_1(pcp, val) _pcp_wrap(__percpu_add_amo_case_8, pcp, val)
+#define this_cpu_add_2(pcp, val) _pcp_wrap(__percpu_add_amo_case_16, pcp, val)
+#define this_cpu_add_4(pcp, val) _pcp_wrap(__percpu_add_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_add_8(pcp, val) _pcp_wrap(__percpu_add_amo_case_64, pcp, val)
+#endif
+
+#define this_cpu_and_1(pcp, val) _pcp_wrap(__percpu_and_amo_case_8, pcp, val)
+#define this_cpu_and_2(pcp, val) _pcp_wrap(__percpu_and_amo_case_16, pcp, val)
+#define this_cpu_and_4(pcp, val) _pcp_wrap(__percpu_and_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_and_8(pcp, val) _pcp_wrap(__percpu_and_amo_case_64, pcp, val)
+#endif
+
+#define this_cpu_or_1(pcp, val) _pcp_wrap(__percpu_or_amo_case_8, pcp, val)
+#define this_cpu_or_2(pcp, val) _pcp_wrap(__percpu_or_amo_case_16, pcp, val)
+#define this_cpu_or_4(pcp, val) _pcp_wrap(__percpu_or_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_or_8(pcp, val) _pcp_wrap(__percpu_or_amo_case_64, pcp, val)
+#endif
+
#undef __PERCPU_AMO
#undef PERCPU_RW_OPS
#undef __PERCPU_AMO_OP_CASE
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 06/10] riscv: implement preemptible this_cpu add_return operations
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
` (4 preceding siblings ...)
2026-12-04 8:58 ` [PATCH v12 05/10] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 07/10] riscv: implement preemptible this_cpu xchg operations Yunhui Cui
` (3 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
Apply the recoverable per-CPU address handling to add-return operations.
Use the common add helpers and value-returning wrapper so void and
add-return operations share the same atomic implementation.
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
arch/riscv/include/asm/percpu.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index 6d0a11b49563f..f67dc601287eb 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -281,6 +281,17 @@ __PERCPU_8_16_OP_CASE(or, 16, .h, |)
#define this_cpu_or_8(pcp, val) _pcp_wrap(__percpu_or_amo_case_64, pcp, val)
#endif
+#define this_cpu_add_return_1(pcp, val) \
+ _pcp_wrap_return(__percpu_add_amo_case_8, pcp, val)
+#define this_cpu_add_return_2(pcp, val) \
+ _pcp_wrap_return(__percpu_add_amo_case_16, pcp, val)
+#define this_cpu_add_return_4(pcp, val) \
+ _pcp_wrap_return(__percpu_add_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_add_return_8(pcp, val) \
+ _pcp_wrap_return(__percpu_add_amo_case_64, pcp, val)
+#endif
+
#undef __PERCPU_AMO
#undef PERCPU_RW_OPS
#undef __PERCPU_AMO_OP_CASE
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 07/10] riscv: implement preemptible this_cpu xchg operations
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
` (5 preceding siblings ...)
2026-12-04 8:58 ` [PATCH v12 06/10] riscv: implement preemptible this_cpu add_return operations Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 08/10] riscv: implement preemptible this_cpu cmpxchg operations Yunhui Cui
` (2 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
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@bytedance.com>
---
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
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 08/10] riscv: implement preemptible this_cpu cmpxchg operations
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
` (6 preceding siblings ...)
2026-12-04 8:58 ` [PATCH v12 07/10] riscv: implement preemptible this_cpu xchg operations Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 09/10] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 10/10] riscv: select HAVE_CMPXCHG_LOCAL Yunhui Cui
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
Apply the recoverable per-CPU address handling to compare-and-exchange
operations so they do not require local interrupt masking. Use AMOCAS
where available and LR/SC otherwise, keeping the API usable without
optional Zacas support.
Link: https://lore.kernel.org/r/20260826010748.zphu3wh2hlm4t4o2@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
arch/riscv/include/asm/percpu.h | 136 ++++++++++++++++++++++++++++++++
1 file changed, 136 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index a6eb16cf2849c..d669adf3530d2 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -9,6 +9,7 @@
#include <linux/thread_info.h>
#include <asm/asm.h>
+#include <asm/cmpxchg.h>
#include <asm/cpufeature-macros.h>
#include <asm/current.h>
#include <asm/gpr-num.h>
@@ -297,6 +298,123 @@ __percpu_xchg_case_##sz(void __percpu *pcp, unsigned long val) \
__PERCPU_8_16_XCHG_CASE(8, .b)
__PERCPU_8_16_XCHG_CASE(16, .h)
+#define __PERCPU_CMPXCHG_CASE(sz, lr_sfx, sc_sfx, cas_sfx) \
+static __always_inline unsigned long \
+__percpu_cmpxchg_case_##sz(void __percpu *pcp, unsigned long old, \
+ unsigned long new) \
+{ \
+ __PCPU_OP_CONTEXT(); \
+ const long cmp = (long)(s##sz)(u##sz)old; \
+ unsigned long rc; \
+ u##sz ret = (u##sz)old; \
+ \
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZACAS) && \
+ IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZACAS) && \
+ riscv_has_extension_unlikely(RISCV_ISA_EXT_ZACAS)) { \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+ ".option push\n" \
+ ".option arch, +zacas\n" \
+ "amocas" #cas_sfx " %[ret], %[new], 0(%[addr])\n" \
+ ".option pop\n" \
+ __PCPU_GPRS_END(gprs) \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [ret] "+&r" (ret) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp), [new] "r" ((u##sz)new) \
+ : "memory"); \
+ } else { \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+ "0: lr" #lr_sfx " %[ret], 0(%[addr])\n" \
+ "bne %[ret], %[old], 1f\n" \
+ "sc" #sc_sfx " %[rc], %[new], 0(%[addr])\n" \
+ "bnez %[rc], 0b\n" \
+ "1:\n" \
+ __PCPU_GPRS_END(gprs) \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [ret] "=&r" (ret), [rc] "=&r" (rc) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp), [old] "r" (cmp), \
+ [new] "r" ((u##sz)new) \
+ : "memory"); \
+ } \
+ \
+ return ret; \
+}
+
+__PERCPU_CMPXCHG_CASE(32, .w, .w, .w)
+#ifdef CONFIG_64BIT
+__PERCPU_CMPXCHG_CASE(64, .d, .d, .d)
+#endif
+
+/*
+ * Use XLEN-wide masks to clear the upper bits of an RV64 sign-extended
+ * LR.W result before comparing a high byte or halfword.
+ */
+#define __PERCPU_8_16_CMPXCHG_CASE(sz, sfx) \
+static __always_inline unsigned long \
+__percpu_cmpxchg_case_##sz(void __percpu *pcp, unsigned long old, \
+ unsigned long new) \
+{ \
+ __PCPU_OP_CONTEXT(); \
+ const unsigned long shift = PERCPU_8_16_GET_SHIFT(pcp); \
+ const unsigned long field_mask = PERCPU_8_16_GET_MASK(sz); \
+ const unsigned long mask = field_mask << shift; \
+ const unsigned long old_field = (old & field_mask) << shift; \
+ const unsigned long new_field = (new & field_mask) << shift; \
+ void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp); \
+ unsigned long ret32, tmp, rc; \
+ u##sz ret = (u##sz)old; \
+ \
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) && \
+ IS_ENABLED(CONFIG_RISCV_ISA_ZACAS) && \
+ IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZACAS) && \
+ riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA) && \
+ riscv_has_extension_unlikely(RISCV_ISA_EXT_ZACAS)) { \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+ ".option push\n" \
+ ".option arch, +zacas, +zabha\n" \
+ "amocas" #sfx " %[ret], %[new], 0(%[addr])\n" \
+ ".option pop\n" \
+ __PCPU_GPRS_END(gprs) \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [ret] "+&r" (ret) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp), [new] "r" ((u##sz)new) \
+ : "memory"); \
+ } else { \
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+ "0: lr.w %[ret], 0(%[addr])\n" \
+ "and %[tmp], %[ret], %[mask]\n" \
+ "bne %[tmp], %[old], 1f\n" \
+ "and %[tmp], %[ret], %[inv_mask]\n" \
+ "or %[tmp], %[tmp], %[new]\n" \
+ "sc.w %[rc], %[tmp], 0(%[addr])\n" \
+ "bnez %[rc], 0b\n" \
+ "1:\n" \
+ __PCPU_GPRS_END(gprs) \
+ : [gprs] "=m" (__ti->pcpu_gprs), \
+ [off] "=&r" (__off), [addr] "=&r" (__addr), \
+ [ret] "=&r" (ret32), [tmp] "=&r" (tmp), \
+ [rc] "=&r" (rc) \
+ : [pcpu_offset] "m" (__ti->pcpu_offset), \
+ [pcp] "r" (pcp32), [mask] "r" (mask), \
+ [old] "r" (old_field), \
+ [new] "r" (new_field), \
+ [inv_mask] "r" (~mask) \
+ : "memory"); \
+ \
+ ret = (ret32 & mask) >> shift; \
+ } \
+ \
+ return ret; \
+}
+
+__PERCPU_8_16_CMPXCHG_CASE(8, .b)
+__PERCPU_8_16_CMPXCHG_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)
@@ -360,6 +478,22 @@ __PERCPU_8_16_XCHG_CASE(16, .h)
_pcp_wrap_return(__percpu_xchg_case_64, pcp, (__force unsigned long)(val))
#endif
+#define this_cpu_cmpxchg_1(pcp, o, n) \
+ _pcp_wrap_return(__percpu_cmpxchg_case_8, pcp, \
+ (__force unsigned long)(o), (__force unsigned long)(n))
+#define this_cpu_cmpxchg_2(pcp, o, n) \
+ _pcp_wrap_return(__percpu_cmpxchg_case_16, pcp, \
+ (__force unsigned long)(o), (__force unsigned long)(n))
+#define this_cpu_cmpxchg_4(pcp, o, n) \
+ _pcp_wrap_return(__percpu_cmpxchg_case_32, pcp, \
+ (__force unsigned long)(o), (__force unsigned long)(n))
+#ifdef CONFIG_64BIT
+#define this_cpu_cmpxchg_8(pcp, o, n) \
+ _pcp_wrap_return(__percpu_cmpxchg_case_64, pcp, \
+ (__force unsigned long)(o), (__force unsigned long)(n))
+#define this_cpu_cmpxchg64(pcp, o, n) this_cpu_cmpxchg_8(pcp, o, n)
+#endif
+
#undef __PERCPU_AMO
#undef PERCPU_RW_OPS
#undef __PERCPU_AMO_OP_CASE
@@ -368,6 +502,8 @@ __PERCPU_8_16_XCHG_CASE(16, .h)
#undef __PERCPU_8_16_OP_CASE
#undef __PERCPU_XCHG_AMO_CASE
#undef __PERCPU_8_16_XCHG_CASE
+#undef __PERCPU_CMPXCHG_CASE
+#undef __PERCPU_8_16_CMPXCHG_CASE
#undef PERCPU_8_16_GET_SHIFT
#undef PERCPU_8_16_GET_MASK
#undef PERCPU_8_16_GET_PCP32
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 09/10] riscv: implement preemptible this_cpu cmpxchg128
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
` (7 preceding siblings ...)
2026-12-04 8:58 ` [PATCH v12 08/10] riscv: implement preemptible this_cpu cmpxchg operations Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
2026-12-04 8:58 ` [PATCH v12 10/10] riscv: select HAVE_CMPXCHG_LOCAL Yunhui Cui
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
Apply the recoverable per-CPU address handling to native 128-bit
compare-and-exchange. Use AMOCAS.Q to provide an atomic 128-bit update,
with a local-IRQ-protected fallback where native support is unavailable.
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
arch/riscv/include/asm/percpu.h | 49 +++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index d669adf3530d2..f8d174a5cafe1 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -415,6 +415,38 @@ __percpu_cmpxchg_case_##sz(void __percpu *pcp, unsigned long old, \
__PERCPU_8_16_CMPXCHG_CASE(8, .b)
__PERCPU_8_16_CMPXCHG_CASE(16, .h)
+#ifdef system_has_cmpxchg128
+static __always_inline u128
+__percpu_cmpxchg128_case(void __percpu *pcp, u128 old, u128 new)
+{
+ __PCPU_OP_CONTEXT();
+ union __u128_halves old_halves = { .full = old };
+ union __u128_halves new_halves = { .full = new };
+
+ register unsigned long new_lo asm("t1") = new_halves.low;
+ register unsigned long new_hi asm("t2") = new_halves.high;
+ register unsigned long old_lo asm("t3") = old_halves.low;
+ register unsigned long old_hi asm("t4") = old_halves.high;
+
+ asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)
+ ".option push\n"
+ ".option arch, +zacas\n"
+ "amocas.q %[old_lo], %[new_lo], 0(%[addr])\n"
+ ".option pop\n"
+ __PCPU_GPRS_END(gprs)
+ : [gprs] "=m" (__ti->pcpu_gprs),
+ [off] "=&r" (__off), [addr] "=&r" (__addr),
+ [old_lo] "+&r" (old_lo), [old_hi] "+&r" (old_hi)
+ : [pcpu_offset] "m" (__ti->pcpu_offset),
+ [pcp] "r" (pcp), [new_lo] "r" (new_lo),
+ [new_hi] "r" (new_hi)
+ : "memory");
+
+ return ((u128)old_hi << 64) | old_lo;
+}
+
+#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)
@@ -494,6 +526,23 @@ __PERCPU_8_16_CMPXCHG_CASE(16, .h)
#define this_cpu_cmpxchg64(pcp, o, n) this_cpu_cmpxchg_8(pcp, o, n)
#endif
+#ifdef system_has_cmpxchg128
+#define this_cpu_cmpxchg128(pcp, o, n) \
+({ \
+ __auto_type __pcpu_cmpxchg128_ptr = &(pcp); \
+ u128 __pcpu_cmpxchg128_old = (u128)(o); \
+ u128 __pcpu_cmpxchg128_new = (u128)(n); \
+ \
+ system_has_cmpxchg128() ? \
+ __percpu_cmpxchg128_case(__pcpu_cmpxchg128_ptr, \
+ __pcpu_cmpxchg128_old, \
+ __pcpu_cmpxchg128_new) : \
+ this_cpu_generic_cmpxchg(*__pcpu_cmpxchg128_ptr, \
+ __pcpu_cmpxchg128_old, \
+ __pcpu_cmpxchg128_new); \
+})
+#endif
+
#undef __PERCPU_AMO
#undef PERCPU_RW_OPS
#undef __PERCPU_AMO_OP_CASE
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v12 10/10] riscv: select HAVE_CMPXCHG_LOCAL
2026-12-04 8:58 [PATCH v12 00/10] riscv: optimize percpu access Yunhui Cui
` (8 preceding siblings ...)
2026-12-04 8:58 ` [PATCH v12 09/10] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
@ 2026-12-04 8:58 ` Yunhui Cui
9 siblings, 0 replies; 12+ messages in thread
From: Yunhui Cui @ 2026-12-04 8:58 UTC (permalink / raw)
To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
linux-arch, llvm
Select HAVE_CMPXCHG_LOCAL so mm/vmstat.c and lib/percpu_counter.c can use
this_cpu_try_cmpxchg() for local counter updates instead of masking
local interrupts.
Suggested-by: Davidlohr Bueso <dave@stgolabs.net>
Link: https://lore.kernel.org/r/20260826010748.zphu3wh2hlm4t4o2@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
Documentation/features/locking/cmpxchg-local/arch-support.txt | 2 +-
arch/riscv/Kconfig | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/Documentation/features/locking/cmpxchg-local/arch-support.txt b/Documentation/features/locking/cmpxchg-local/arch-support.txt
index 127e8159adaa6..c9811fb5ddfd4 100644
--- a/Documentation/features/locking/cmpxchg-local/arch-support.txt
+++ b/Documentation/features/locking/cmpxchg-local/arch-support.txt
@@ -20,7 +20,7 @@
| openrisc: | TODO |
| parisc: | TODO |
| powerpc: | TODO |
- | riscv: | TODO |
+ | riscv: | ok |
| s390: | ok |
| sh: | TODO |
| sparc: | TODO |
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 5d1bab5ff3316..11cbc682bd628 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -163,6 +163,7 @@ config RISCV
select HAVE_ARCH_VMAP_STACK if MMU && 64BIT
select HAVE_ASM_MODVERSIONS
select HAVE_BUILDTIME_MCOUNT_SORT
+ select HAVE_CMPXCHG_LOCAL
select HAVE_CONTEXT_TRACKING_USER
select HAVE_DEBUG_KMEMLEAK
select HAVE_DMA_CONTIGUOUS if MMU
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 12+ messages in thread