[PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations

From: Yunhui Cui

Date: Thu Dec 03 2026 - 08:56:00 EST


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@xxxxxxxxx>
Link: https://lore.kernel.org/r/20261108213254.67805ef0@pumpkin
Suggested-by: Jisheng Zhang <jszhang@xxxxxxxxxx>
Link: https://lore.kernel.org/r/avG07pmpcLGkq5i-@xhacker
Signed-off-by: Yunhui Cui <cuiyunhui@xxxxxxxxxxxxx>
---
arch/riscv/include/asm/asm.h | 19 ++++++++++++
arch/riscv/include/asm/percpu.h | 46 ++++++++++++++++++++++++++++
arch/riscv/include/asm/ptrace.h | 22 +++++++++++++
arch/riscv/include/asm/thread_info.h | 1 +
arch/riscv/kernel/Makefile | 1 +
arch/riscv/kernel/asm-offsets.c | 2 ++
arch/riscv/kernel/entry-common.c | 30 ++++++++++++++++++
arch/riscv/kernel/entry.S | 10 ++++++
arch/riscv/mm/extable.c | 18 -----------
9 files changed, 131 insertions(+), 18 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..57af10a936d9b 100644
--- a/arch/riscv/include/asm/asm.h
+++ b/arch/riscv/include/asm/asm.h
@@ -81,6 +81,25 @@

/* Common assembly source macros */

+/*
+ * 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
+
/*
* NOP sequence
*/
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index faf9a75ec66d7..3cd60ef18815e 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -3,12 +3,58 @@
#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>

#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)

+#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 %[" #off "], " __PCPU_GPRS_VALUE "\n"\
+ "sh %[" #off "], %[" #gprs "]\n" \
+ REG_L " %[" #off "], %[" #pcpu_offset "]\n"\
+ "add %[" #addr "], %[" #pcp "], %[" #off "]\n"
+
+#define __PCPU_GPRS_END(gprs) \
+ "sh zero, " gprs "\n"
+
#include <asm-generic/percpu.h>

#endif /* __ASM_PERCPU_H */
diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
index addc8188152f7..355818c709b36 100644
--- a/arch/riscv/include/asm/ptrace.h
+++ b/arch/riscv/include/asm/ptrace.h
@@ -53,6 +53,8 @@ struct pt_regs {
unsigned long cause;
/* a0 value before the syscall */
unsigned long orig_a0;
+ /* Active preemptible per-CPU operation, if any. */
+ unsigned long pcpu_gprs;
};

#define PTRACE_SYSEMU 0x1f
@@ -145,6 +147,26 @@ 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(!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(!offset || offset > MAX_REG_OFFSET))
+ return;
+
+ *(unsigned long *)((unsigned long)regs + offset) = val;
+}
+
+void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs);
+
/**
* 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 f10ba62b61016..51c4076763c1b 100644
--- a/arch/riscv/include/asm/thread_info.h
+++ b/arch/riscv/include/asm/thread_info.h
@@ -63,6 +63,7 @@ struct thread_info {
long user_sp; /* User stack pointer */
unsigned long syscall_work; /* SYSCALL_WORK_ flags */
unsigned long pcpu_offset;
+ unsigned short pcpu_gprs;
#ifdef CONFIG_SHADOW_CALL_STACK
void *scs_base;
void *scs_sp;
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index f54c336bf547c..bf786d5dd4de3 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -52,6 +52,7 @@ obj-y += alternative.o
obj-y += cpu.o
obj-y += cpufeature.o
obj-y += entry.o
+obj-y += 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 f4f7ee976cd94..38f246a691c60 100644
--- a/arch/riscv/kernel/asm-offsets.c
+++ b/arch/riscv/kernel/asm-offsets.c
@@ -39,6 +39,7 @@ void asm_offsets(void)

OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu);
OFFSET(TASK_TI_PCPU_OFFSET, task_struct, thread_info.pcpu_offset);
+ OFFSET(TASK_TI_PCPU_GPRS, task_struct, thread_info.pcpu_gprs);
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);
@@ -130,6 +131,7 @@ void asm_offsets(void)
OFFSET(PT_STATUS, pt_regs, status);
OFFSET(PT_BADADDR, pt_regs, badaddr);
OFFSET(PT_CAUSE, pt_regs, cause);
+ OFFSET(PT_PCPU_GPRS, pt_regs, pcpu_gprs);

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..65298ac828959 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -198,6 +198,8 @@ SYM_CODE_START(handle_exception)
REG_S s4, PT_CAUSE(sp)
REG_S s5, PT_TP(sp)

+ pcpu_gprs_entry tp sp s0
+
/*
* Set the scratch register to 0, so that if a recursive exception
* occurs, the exception vector knows it came from the kernel
@@ -281,6 +283,14 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
move a0, sp
call riscv_v_context_nesting_end
#endif
+ 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)
+.Lpcpu_regs_restored:
+ pcpu_gprs_exit tp t0
+
REG_L a0, PT_STATUS(sp)
restore_userssp s3, a0
/*
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)
{