From: sashiko-bot@kernel.org
To: "Zhanpeng Zhang" <zhangzhanpeng.jasper@bytedance.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v14 06/10] perf: RISC-V: support callchains with SSE delivery
Date: Wed, 02 Dec 2026 07:47:21 +0000 [thread overview]
Message-ID: <sashiko-outbox-245091@kernel.org> (raw)
In-Reply-To: <20261202070003.42491-18-zhangzhanpeng.jasper@bytedance.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] perf: riscv: missing access_ok() for non-SSE copies in
riscv_perf_out_copy_user()
- [High] riscv: stacktrace: OOB read unwinding across exception frames
- [Medium] perf: riscv: unvalidated SSE stack walk in
sse_callchain_kernel() on nested events
- [Medium] perf: riscv: torn task_pt_regs() access in
sse_can_access_user_stack()
- [Medium] riscv: perf: guest s0 unwound as host frame in
sse_callchain_kernel()
--
commit 180a6ffce6da1970795d364e54cbff72d0304b04
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
perf: RISC-V: support callchains with SSE delivery
This commit adds kernel and user callchain unwinding and raw user-stack
copying support when PMU overflow events are delivered via RISC-V SBI SSE.
Link: https://lore.kernel.org/r/as79MHtNYd-VbUug@hu-himchau-blr.qualcomm.com/
Link: https://sashiko.dev/#/patchset/20261119120824.13969-13-zhangzhanpeng.jasper%40bytedance.com?part=7
> diff --git a/arch/riscv/kernel/perf_callchain.c b/arch/riscv/kernel/perf_callchain.c
> index b465bc9eb870e..30ec3a15e009d 100644
> --- a/arch/riscv/kernel/perf_callchain.c
> +++ b/arch/riscv/kernel/perf_callchain.c
> @@ -1,9 +1,15 @@
[ ... ]
> @@ -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));
> +}
[Severity: Medium]
Can sse_kernel_regs_safe() accept a guest s0 register as a host frame
pointer if an SSE arrives during the KVM world-switch assembly?
In __kvm_riscv_switch_to() and __kvm_riscv_nacl_switch_to(), the CPU runs
in HS-mode with HSTATUS_SPV == 0, and RESTORE_HOST_GPRS restores the host
sp three instructions before restoring the host s0:
arch/riscv/kvm/vcpu_switch.S:RESTORE_HOST_GPRS {
...
REG_L sp, (KVM_ARCH_HOST_SP)(a0)
REG_L gp, (KVM_ARCH_HOST_GP)(a0)
REG_L tp, (KVM_ARCH_HOST_TP)(a0)
REG_L s0, (KVM_ARCH_HOST_S0)(a0)
...
}
Because sse_callchain_kernel() runs before perf_guest_state() and pc lies
outside riscv_pc_in_exception_entry(), sse_kernel_regs_safe() will pass
whenever the guest s0 happens to fall within the host task stack, causing
walk_stackframe() to unwind the host stack from a guest-controlled address
and attach a host callchain to a guest sample.
> +
> +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);
> +}
[Severity: Medium]
Does sse_can_access_user_stack() allow perf_callchain_user() and
riscv_perf_out_copy_user() to read stale or partially saved registers from
task_pt_regs(current) when an SSE interrupts kernel exception entry or
exit?
When an SSE interrupts S-mode (!user_mode(context->regs)),
get_perf_callchain() and perf_sample_regs_user() pass task_pt_regs(current)
rather than context->regs to the user unwinder and user stack copy. If a
user trap was interrupted in handle_exception before the register save to
task_pt_regs(current) completes, sse_can_access_user_stack() still returns
true, so unwinding and stack copying use stale or torn epc, sp, s0, and ra
values. Rejecting !user_mode(context->regs) &&
riscv_pc_in_exception_entry(instruction_pointer(context->regs)) here would
avoid reading an incomplete pt_regs frame.
> +
> +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;
[Severity: Medium]
When context is non-NULL but context->regs != regs (for example, a nested
tracepoint, kprobe, or software perf event firing while do_sse() runs on
the SSE stack), returning false causes perf_callchain_kernel() to call
walk_stackframe(current, regs, ...) on the dedicated SSE event stack.
Can walk_stackframe() safely unwind the SSE stack? When CONFIG_VMAP_STACK
is disabled, sse_stack_alloc() allocates the stack with kmalloc(), so
fp_is_valid()'s ALIGN(sp, THREAD_SIZE) bound does not match the allocation.
Even with vmapped stacks, handle_sse places the interrupted pt_regs at the
top of the SSE stack and leaves the interrupted s0 live when calling
do_sse(), so unwinding past do_sse() can treat the saved pt_regs (or memory
past a misaligned kmalloc stack) as stack frames.
[ ... ]
> +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;
[Severity: High]
Does gating !access_ok(src, n) on context allow a non-SSE perf event to
copy kernel memory into the user's perf ring buffer when pointer masking
(CONFIG_RISCV_ISA_SUPM) is enabled on Sv57?
arch_perf_out_copy_user routes all perf user-stack copies through
riscv_perf_out_copy_user(), including non-SSE events where context is NULL.
Before calling __output_copy_user(), perf_ustack_task_size() only checks
the raw sp against TASK_SIZE (1UL << 56 on Sv57) without untagging it. If
a user task enables PMLEN_16 pointer masking and sets sp to an address such
as 0x0000ffff80000000 (below Sv57 TASK_SIZE, with bit 47 set),
raw_copy_from_user() sign-extends bit 47 via untagged_addr(src) to the
kernel virtual address 0xffffffff80000000 and copies kernel memory without
an access_ok() check. Checking !access_ok(src, n) unconditionally fixes
this.
> diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
> index c7555447149bf..b928d910549ae 100644
> --- a/arch/riscv/kernel/stacktrace.c
> +++ b/arch/riscv/kernel/stacktrace.c
> @@ -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;
>
[Severity: High]
Can walk_stackframe() read past the top of the kernel stack into the
unmapped vmap guard page when unwinding across an exception frame?
When walk_stackframe() reaches the outermost C frame called from
handle_exception, sp equals (unsigned long)task_pt_regs(task) and lines
93-94 load pc = ((struct pt_regs *)sp)->epc and
fp = ((struct pt_regs *)sp)->s0 without checking !user_mode(regs) or
bounding fp to task_pt_regs(task) in fp_is_valid() (which allows fp up to
ALIGN(sp, THREAD_SIZE)). If a user task triggers an instruction page fault
at a kernel text address while user s0 points to &task_pt_regs(task)->status
(stack + THREAD_SIZE - 32) and user t6 (at fp - 8) holds an address in
[handle_exception, ret_from_exception_end), the next iteration matches
riscv_pc_in_exception_entry(pc) with sp = stack + THREAD_SIZE - 32 and
dereferences ((struct pt_regs *)sp)->s0 32 bytes past stack + THREAD_SIZE.
In addition, handle_exception loads s0 with TASK_TI_USER_SP(tp) and calls C
handlers without setting up a frame pointer in s0, so if an SSE interrupts
the prologue or epilogue of a C entry handler while riscv_pc_in_exception_entry(pc)
is false, sse_kernel_regs_safe() can accept the interrupted sp in regs->s0
as a frame pointer on the task stack.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202070003.42491-12-zhangzhanpeng.jasper@bytedance.com?part=6
next prev parent reply other threads:[~2026-12-02 7:47 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 7:00 [PATCH v14 00/10] riscv: add SBI Supervisor Software Events support Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:00 ` [PATCH v14 01/10] riscv: add SBI SSE extension definitions Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:00 ` [PATCH v14 02/10] riscv: add support for SBI Supervisor Software Events extension Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:21 ` sashiko-bot
2026-12-02 7:00 ` [PATCH v14 03/10] riscv: sse: mask events during shutdown and kexec Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:29 ` sashiko-bot
2026-12-02 7:00 ` [PATCH v14 04/10] drivers: firmware: add riscv SSE support Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:25 ` sashiko-bot
2026-12-02 7:00 ` [PATCH v14 05/10] riscv: mm: avoid enabling interrupts for nofault page faults Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:00 ` [PATCH v14 06/10] perf: RISC-V: support callchains with SSE delivery Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:47 ` sashiko-bot [this message]
2026-12-02 7:00 ` [PATCH v14 07/10] perf: RISC-V: add support for SSE event Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:33 ` sashiko-bot
2026-12-02 7:00 ` [PATCH v14 08/10] MAINTAINERS: add RISC-V SSE maintainers and file coverage Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:00 ` [PATCH v14 09/10] selftests/riscv: add SSE test module Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:26 ` sashiko-bot
2026-12-02 7:00 ` [PATCH v14 10/10] selftests/riscv: add perf user-stack SSE copy regression test Zhanpeng Zhang
2026-12-02 7:00 ` Zhanpeng Zhang
2026-12-02 7:24 ` sashiko-bot
2026-12-02 7:47 ` [PATCH v14 00/10] riscv: add SBI Supervisor Software Events support sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-245091@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=zhangzhanpeng.jasper@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.