[PATCH v14 06/10] perf: RISC-V: support callchains with SSE delivery

From: Zhanpeng Zhang

Date: Wed Dec 02 2026 - 02:02:57 EST


PMU overflow delivery through SSE enters Linux with a synthetic supervisor
context on a dedicated event stack. The perf unwinder must use the context
interrupted by the overflow rather than treating the SSE handler frame as
the sampled frame.

Use the interrupted pt_regs published by the RISC-V SSE entry path.
Walk a kernel callchain only when the interrupted PC, SP and frame
pointer describe a frame on the current task stack below the saved
user registers. In exception entry or return code the frame pointer
can be transiently invalid, even if it lies on the task stack. Keep
only the interrupted PC in these windows and on unverified IRQ stacks.
Pass current to the walker for function graph return-address recovery.

DWARF callchains additionally copy a raw user stack. Provide an
SSE-aware riscv_perf_out_copy_user() following x86's copy_from_user_nmi().
Disable page faults so an unmapped source is handled by the exception
table instead of the normal fault handler, and return the uncopied
length so perf truncates the stack dump. The SSE entry saves and
restores the interrupted task's stack metadata across that nested trap.

Require a user mm before either form of user stack access: on systems
with pointer masking, untagging a user address already needs that mm.
For both frame-pointer unwinding and DWARF copying in SSE context,
reject an interrupted guest and require the active page-table root
to match current->mm. A matching root does not exclude stale TLB
translations during switch_mm(). Also check access_ok() before an SSE
raw stack copy.

Susheng Yang reported this failure with perf callchain workloads.

Link: https://lore.kernel.org/r/as79MHtNYd-VbUug@xxxxxxxxxxxxxxxxxxxxxxxxxxx/
Link: https://sashiko.dev/#/patchset/20261119120824.13969-13-zhangzhanpeng.jasper%40bytedance.com?part=7
Reported-by: Susheng Yang <yang.susheng@xxxxxxxxxx>
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
---
arch/riscv/include/asm/perf_event.h | 10 ++
arch/riscv/include/asm/stacktrace.h | 9 ++
arch/riscv/kernel/perf_callchain.c | 142 +++++++++++++++++++++++++++-
arch/riscv/kernel/stacktrace.c | 6 +-
4 files changed, 161 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/include/asm/perf_event.h b/arch/riscv/include/asm/perf_event.h
index bcc928fd3785..ddc404794751 100644
--- a/arch/riscv/include/asm/perf_event.h
+++ b/arch/riscv/include/asm/perf_event.h
@@ -18,6 +18,16 @@
(regs)->sp = current_stack_pointer; \
(regs)->status = SR_PP; \
}
+
+#ifdef CONFIG_RISCV_SBI_SSE
+/*
+ * Raw user-stack sampling can run in the NMI-like SSE context. Route it
+ * through an implementation that does not fault on a non-resident page.
+ */
+unsigned long riscv_perf_out_copy_user(void *dst, const void *src,
+ unsigned long n);
+#define arch_perf_out_copy_user riscv_perf_out_copy_user
+#endif
#endif

#endif /* _ASM_RISCV_PERF_EVENT_H */
diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/stacktrace.h
index b1495a7e06ce..ceb506f97b42 100644
--- a/arch/riscv/include/asm/stacktrace.h
+++ b/arch/riscv/include/asm/stacktrace.h
@@ -11,6 +11,15 @@ struct stackframe {
unsigned long ra;
};

+extern asmlinkage void handle_exception(void);
+extern unsigned long ret_from_exception_end;
+
+static inline bool riscv_pc_in_exception_entry(unsigned long pc)
+{
+ return pc >= (unsigned long)handle_exception &&
+ pc < (unsigned long)&ret_from_exception_end;
+}
+
extern void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
bool (*fn)(void *, unsigned long), void *arg);
extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
diff --git a/arch/riscv/kernel/perf_callchain.c b/arch/riscv/kernel/perf_callchain.c
index b465bc9eb870..30ec3a15e009 100644
--- a/arch/riscv/kernel/perf_callchain.c
+++ b/arch/riscv/kernel/perf_callchain.c
@@ -1,9 +1,15 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */

+#include <linux/kallsyms.h>
+#include <linux/mm.h>
#include <linux/perf_event.h>
+#include <linux/sched/task_stack.h>
+#include <linux/thread_info.h>
#include <linux/uaccess.h>

+#include <asm/csr.h>
+#include <asm/sse.h>
#include <asm/stacktrace.h>

static bool fill_callchain(void *entry, unsigned long pc)
@@ -11,6 +17,126 @@ static bool fill_callchain(void *entry, unsigned long pc)
return perf_callchain_store(entry, pc) == 0;
}

+#ifdef CONFIG_RISCV_SBI_SSE
+static bool sse_addr_on_task_stack(unsigned long addr, unsigned long size)
+{
+ unsigned long end = addr + size;
+ unsigned long stack;
+
+ if (end < addr)
+ return false;
+
+ stack = (unsigned long)task_stack_page(current);
+ if (addr >= stack && end <= stack + THREAD_SIZE)
+ return true;
+
+ return false;
+}
+
+static bool sse_kernel_regs_safe(struct pt_regs *regs)
+{
+ unsigned long fp = frame_pointer(regs);
+ unsigned long pc = instruction_pointer(regs);
+ unsigned long sp = user_stack_pointer(regs);
+
+ if (!__kernel_text_address(pc) || riscv_pc_in_exception_entry(pc))
+ return false;
+ if (!sse_addr_on_task_stack(sp, sizeof(unsigned long)))
+ return false;
+ /* The saved user registers at the top of the stack are not frames. */
+ if (fp < sizeof(struct stackframe) || fp > (unsigned long)task_pt_regs(current))
+ return false;
+
+ return sse_addr_on_task_stack(fp - sizeof(struct stackframe),
+ sizeof(struct stackframe));
+}
+
+static bool sse_callchain_is_guest(const struct riscv_sse_interrupted_context *context)
+{
+ return context && (context->hstatus & HSTATUS_SPV);
+}
+
+static bool sse_can_access_user_stack(const struct riscv_sse_interrupted_context *context,
+ const struct mm_struct *mm)
+{
+ if (!context)
+ return true;
+ if (sse_callchain_is_guest(context))
+ return false;
+
+ /* The page-table root cannot detect stale TLB entries in switch_mm(). */
+ return (csr_read(CSR_SATP) & SATP_PPN) == virt_to_pfn(mm->pgd);
+}
+
+static bool sse_callchain_kernel(struct perf_callchain_entry_ctx *entry,
+ struct pt_regs *regs)
+{
+ const struct riscv_sse_interrupted_context *context;
+ unsigned long pc;
+
+ context = riscv_sse_get_interrupted_context();
+ if (!context || context->regs != regs)
+ return false;
+
+ /* A guest stack cannot be walked using the host kernel address space. */
+ if (sse_callchain_is_guest(context))
+ return true;
+
+ if (user_mode(regs))
+ return true;
+
+ if (sse_kernel_regs_safe(regs)) {
+ walk_stackframe(current, regs, fill_callchain, entry);
+ return true;
+ }
+
+ /*
+ * Keep the sample useful for sensitive entry paths and IRQ stacks. The
+ * generic walker does not take explicit IRQ stack bounds, and its
+ * THREAD_SIZE alignment assumption fails for non-vmapped IRQ stacks.
+ * Conservatively avoid walking IRQ stacks in every configuration.
+ */
+ pc = instruction_pointer(regs);
+ if (__kernel_text_address(pc))
+ perf_callchain_store(entry, pc);
+
+ return true;
+}
+
+unsigned long riscv_perf_out_copy_user(void *dst, const void *src,
+ unsigned long n)
+{
+ const struct riscv_sse_interrupted_context *context;
+ struct mm_struct *mm = current->mm;
+ unsigned long ret;
+
+ /* With SUPM, untagging even an empty access can dereference current->mm. */
+ if (!n || !mm)
+ return n;
+
+ context = riscv_sse_get_interrupted_context();
+ if (!sse_can_access_user_stack(context, mm) ||
+ (context && !access_ok(src, n)))
+ return n;
+
+ /*
+ * Raw user-stack sampling can run in the NMI-like SSE context. Mirror
+ * x86's copy_from_user_nmi(): the copy may take a page fault, but it
+ * must not fault a non-resident page in. pagefault_disable() keeps the
+ * RISC-V fault handler on the nofault exception-table fixup (no
+ * handle_mm_fault(), no interrupt window), and the SSE entry saves and
+ * restores the interrupted task's stack metadata across that nested
+ * trap. A fault therefore returns the number of bytes not copied, and
+ * perf truncates the user-stack dump accordingly.
+ */
+ pagefault_disable();
+ ret = __copy_from_user_inatomic(dst, src, n);
+ pagefault_enable();
+
+ return ret;
+}
+#endif
+
/*
* This will be called when the target is in user mode
* This function will only be called when we use
@@ -28,6 +154,15 @@ static bool fill_callchain(void *entry, unsigned long pc)
void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
struct pt_regs *regs)
{
+ struct mm_struct *mm = current->mm;
+
+ if (!mm)
+ return;
+#ifdef CONFIG_RISCV_SBI_SSE
+ if (!sse_can_access_user_stack(riscv_sse_get_interrupted_context(), mm))
+ return;
+#endif
+
if (perf_guest_state()) {
/* TODO: We don't support guest os callchain now */
return;
@@ -39,10 +174,15 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
struct pt_regs *regs)
{
+#ifdef CONFIG_RISCV_SBI_SSE
+ if (sse_callchain_kernel(entry, regs))
+ return;
+#endif
+
if (perf_guest_state()) {
/* TODO: We don't support guest os callchain now */
return;
}

- walk_stackframe(NULL, regs, fill_callchain, entry);
+ walk_stackframe(current, regs, fill_callchain, entry);
}
diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index c7555447149b..b928d910549a 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -32,9 +32,6 @@
val; \
})

-extern asmlinkage void handle_exception(void);
-extern unsigned long ret_from_exception_end;
-
static inline int fp_is_valid(unsigned long fp, unsigned long sp)
{
unsigned long low, high;
@@ -89,8 +86,7 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
pc = READ_ONCE_TASK_STACK(task, frame->ra);
pc = ftrace_graph_ret_addr(task, &graph_idx, pc,
&frame->ra);
- if (pc >= (unsigned long)handle_exception &&
- pc < (unsigned long)&ret_from_exception_end) {
+ if (riscv_pc_in_exception_entry(pc)) {
if (unlikely(!fn(arg, pc)))
break;

--
2.50.1 (Apple Git-155)