Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zhanpeng Zhang" <zhangzhanpeng.jasper@bytedance.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v13 07/11] perf: RISC-V: support callchains with SSE delivery
Date: Thu, 19 Nov 2026 12:45:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-223113@kernel.org> (raw)
In-Reply-To: <20261119120824.13969-20-zhangzhanpeng.jasper@bytedance.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] perf: riscv: missing sse_callchain_is_guest() check in
  riscv_perf_out_copy_user()
- [High] perf: riscv: missing current->mm and CSR_SATP checks in
  perf_callchain_user()
- [High] perf: riscv: unsafe entry unwind and stack OOB read in
  sse_kernel_regs_safe()
- [High] perf: riscv: NULL task pointer passed to walk_stackframe()
  crashes in fgraph
- [High] perf: riscv: NULL current->mm dereference in
  riscv_perf_out_copy_user()
--

commit 6c97040a7b3339404ccd87297e5e59c6632d3219
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>

perf: RISC-V: support callchains with SSE delivery

This commit adds perf kernel, frame-pointer user, and DWARF user-stack
callchain sampling support on RISC-V when PMU overflow events are
delivered via SBI Supervisor Software Events (SSE).

Link: https://lore.kernel.org/r/as79MHtNYd-VbUug@hu-himchau-blr.qualcomm.com/

> diff --git a/arch/riscv/kernel/perf_callchain.c b/arch/riscv/kernel/perf_callchain.c
> index b465bc9eb870e..216f5bd14c001 100644
> --- a/arch/riscv/kernel/perf_callchain.c
> +++ b/arch/riscv/kernel/perf_callchain.c

[ ... ]

> +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))
> +		return false;
> +	if (!sse_addr_on_task_stack(sp, sizeof(unsigned long)))
> +		return false;
> +	if (fp < sizeof(struct stackframe))
> +		return false;
> +
> +	return sse_addr_on_task_stack(fp - sizeof(struct stackframe),
> +				      sizeof(struct stackframe));
> +}

[Severity: High]
Can sse_kernel_regs_safe() accept an entry/exit frame where s0 is not a
valid frame pointer and cause walk_stackframe() to read past the top of
the task stack?

In ret_from_exception, sp points to task_pt_regs(current) (stack +
THREAD_SIZE - PT_SIZE_ON_STACK) and s0 (fp) is set to stack +
THREAD_SIZE:

arch/riscv/kernel/entry.S:ret_from_exception {
    ...
	/* Save unwound kernel stack pointer in thread_info */
	addi s0, sp, PT_SIZE_ON_STACK
	REG_S s0, TASK_TI_KERNEL_SP(tp)
    ...
}

Because sse_addr_on_task_stack() allows fp - sizeof(struct stackframe)
all the way up to stack + THREAD_SIZE, an SSE interrupting this window
passes sse_kernel_regs_safe().  Under CONFIG_FRAME_POINTER,
walk_stackframe() then reads frame = (struct stackframe *)fp - 1 (the
last two words of task_pt_regs(current), cause and orig_a0), advances sp
= fp = stack + THREAD_SIZE, and sets pc = frame->ra (pt_regs->orig_a0).
If orig_a0 falls in [handle_exception, ret_from_exception_end),
walk_stackframe() dereferences ((struct pt_regs *)sp)->epc at stack +
THREAD_SIZE, hitting the unmapped CONFIG_VMAP_STACK guard page.

Entry and exit windows in [handle_exception, ret_from_exception_end) also
leave s0 holding the user s0 register or sp + PT_SIZE_ON_STACK while sp
is already on the kernel stack.  Rejecting PCs in [handle_exception,
ret_from_exception_end) and requiring fp <= (unsigned
long)task_pt_regs(current) would avoid unwinding these windows.

> +
> +static bool sse_callchain_is_guest(const struct riscv_sse_interrupted_context *context)
> +{
> +	return context && (context->hstatus & HSTATUS_SPV);
> +}
> +
> +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 address space. */
> +	if (sse_callchain_is_guest(context))
> +		return true;
> +
> +	if (user_mode(regs))
> +		return true;
> +
> +	if (sse_kernel_regs_safe(regs)) {
> +		walk_stackframe(NULL, regs, fill_callchain, entry);
> +		return true;
> +	}

[Severity: High]
Does passing NULL as the task argument to walk_stackframe() here (and in
perf_callchain_kernel() below) cause a NULL pointer dereference when
function_graph tracing is active?

When regs is non-NULL, walk_stackframe() takes the if (regs) branch and
leaves task as NULL, then passes task to ftrace_graph_ret_addr():

arch/riscv/kernel/stacktrace.c:walk_stackframe() {
    ...
			fp = READ_ONCE_TASK_STACK(task, frame->fp);
			pc = READ_ONCE_TASK_STACK(task, frame->ra);
			pc = ftrace_graph_ret_addr(task, &graph_idx, pc,
						   &frame->ra);
    ...
}

When an unwound frame's return address is return_to_handler,
ftrace_graph_ret_addr() evaluates task->curr_ret_stack without a NULL
check, panicking in SSE context.  Passing current (or defaulting task to
current in walk_stackframe() when task is NULL) avoids this.

[ ... ]

> +unsigned long riscv_perf_out_copy_user(void *dst, const void *src,
> +				       unsigned long n)
> +{
> +	unsigned long ret;

[ ... ]

> +	if (riscv_sse_get_interrupted_context()) {
> +		if (!access_ok(src, n))
> +			return n;
> +		if (!current->mm ||
> +		    (csr_read(CSR_SATP) & SATP_PPN) != virt_to_pfn(current->mm->pgd))
> +			return n;
> +	}

[Severity: High]
Should riscv_perf_out_copy_user() also reject interrupted KVM guest
contexts with sse_callchain_is_guest(context), the same way
sse_callchain_kernel() and perf_callchain_user() do?

On RISC-V with the Hypervisor extension, guest stage-1 and stage-2
translations use CSR_VSATP and CSR_HGATP while the host HS-mode CSR_SATP
stays loaded with the host VMM task's current->mm->pgd across
__kvm_riscv_switch_to().  When an SSE PMU overflow traps from a running
guest into HS-mode, current->mm is non-NULL and (csr_read(CSR_SATP) &
SATP_PPN) == virt_to_pfn(current->mm->pgd) still holds, so
riscv_perf_out_copy_user() copies from the host VMM user address space at
the guest's sp into the perf ring buffer.

> +
> +	pagefault_disable();
> +	ret = __copy_from_user_inatomic(dst, src, n);
> +	pagefault_enable();
> +
> +	return ret;
> +}
> +#endif

[Severity: High]
Can calling access_ok() and __copy_from_user_inatomic() before verifying
current->mm != NULL dereference a NULL current->mm under
CONFIG_RISCV_ISA_SUPM?

On RISC-V, both access_ok() and raw_copy_from_user() expand
untagged_addr(), which passes current->mm to __untagged_addr_remote() and
dereferences mm->context.pmlen when RISCV_ISA_EXT_SUPM is active.  If a
perf sample hits a user task in do_exit() after exit_mm() has cleared
current->mm (where perf_output_sample_ustack() still invokes
__output_copy_user() with size 0), riscv_perf_out_copy_user() calls
access_ok() before checking !current->mm in the SSE path, and never
checks !current->mm in the non-SSE path.

> +
>  /*
>   * 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)
>  {
> +#ifdef CONFIG_RISCV_SBI_SSE
> +	const struct riscv_sse_interrupted_context *context;
> +
> +	context = riscv_sse_get_interrupted_context();
> +	/* A guest stack cannot be walked using the host address space. */
> +	if (sse_callchain_is_guest(context))
> +		return;
> +#endif

[Severity: High]
When context is non-NULL, does perf_callchain_user() also need the
!current->mm and (csr_read(CSR_SATP) & SATP_PPN) !=
virt_to_pfn(current->mm->pgd) check from riscv_perf_out_copy_user()?

When an SSE event interrupts the kernel while current is a user task,
get_perf_callchain() substitutes task_pt_regs(current) and calls
perf_callchain_user().  If the SSE arrived during an address-space
transition such as context_switch() (after switch_mm_irqs_off() writes
next->mm->pgd to CSR_SATP before __switch_to() updates current) or
efi_virtmap_load(), arch_stack_walk_user() -> unwind_user_frame() copies
user stack frames from the wrong page tables, and if current->mm is NULL
(reachable via bpf_get_stackid(BPF_F_USER_STACK) in do_exit() after
exit_mm()), access_ok() in unwind_user_frame() dereferences NULL
current->mm->context.pmlen under CONFIG_RISCV_ISA_SUPM.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119120824.13969-13-zhangzhanpeng.jasper@bytedance.com?part=7

  reply	other threads:[~2026-11-19 12:45 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 12:08 [PATCH v13 00/11] riscv: add SBI Supervisor Software Events support Zhanpeng Zhang
2026-11-19 12:08 ` [PATCH v13 01/11] riscv: add SBI SSE extension definitions Zhanpeng Zhang
2026-11-19 12:14   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 02/11] riscv: add support for SBI Supervisor Software Events extension Zhanpeng Zhang
2026-11-19 12:36   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 03/11] riscv: sse: mask events during shutdown and kexec Zhanpeng Zhang
2026-11-19 12:38   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 04/11] drivers: firmware: add riscv SSE support Zhanpeng Zhang
2026-11-19 12:38   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 05/11] riscv: mm: avoid enabling interrupts for nofault page faults Zhanpeng Zhang
2026-11-19 12:27   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 06/11] riscv: enable NMI-safe lockdep irqflags tracking Zhanpeng Zhang
2026-11-19 12:21   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 07/11] perf: RISC-V: support callchains with SSE delivery Zhanpeng Zhang
2026-11-19 12:45   ` sashiko-bot [this message]
2026-11-19 12:08 ` [PATCH v13 08/11] perf: RISC-V: add support for SSE event Zhanpeng Zhang
2026-11-19 12:34   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 09/11] MAINTAINERS: add RISC-V SSE maintainers and file coverage Zhanpeng Zhang
2026-11-19 12:08 ` [PATCH v13 10/11] selftests/riscv: add SSE test module Zhanpeng Zhang
2026-11-19 12:37   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 11/11] selftests/riscv: add perf user-stack SSE copy regression test Zhanpeng Zhang
2026-11-19 12:34   ` 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-223113@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox