From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796197645; b=4tYfZ1Dh482ir9iqvTlokjh8EEOhC7F4qSTSKdL9Oj0fGQH2/nufxQfWDHHdBfzrsLMu fgSxBl6f9v4pDIGAryYMJcttNz0vQQoVIfaDAnI1+ERWrOOQgIzseKl48eFeDFF5qtF0G k8cn7pxmVWO3x1xZTEvfUXcG1HyP9sd2sY/RpI2RR0YgFkxFRWKZG7S7GyIjFQJ2gsby2 91xFg3QuufbPsE5KGec+NueJyY56yYkx9xJ5iOeis5W+/h9tupKqugWWquOaA7bTnUTa7 bBIKOUh0gzCStyRTkV46W5FqemL+F7YsT1QBEOAeHVdWYbEbg8lgdP68XTm2jFuEz+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796197645; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=b0OgvsLvvpdBKAQeGlWy/RZZF1FpmGaup2XpEfoomY4=; b=gch8nELfDA0Rm1DjSPrMX+UjocElwEFnn+oSpJ/pFExGalmIVNJutjYBa4EvwGE/aN8C F+8JpBJiPuJyeYwgrJgN3Ezpmuxfszd4qZ3Y7x7dk/Uafm8M2Sx8R3aIyo0VZtdQV/KWk D8x+R5B/zbf8oLjLMWuthLTHA4y3bP7oimLZT99BAbCrcGML2rYfa8d/KEoL8EuykLPq/ d/h2md/Y2W/E1NDFlf2rlcGDcIftkBqR8WmhfB2jeNZJm6krpiT/IAwn+0aYp3Nu5fKRj lrqCfwiJSzt6SZyAsDU/fFY0oVyWw7yHBo1wBGsW0ey5Lg6T6w0j37OcbBjuT2VNj4A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="QL3/T3sG"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42AD8606A31A for ; Wed, 2 Dec 2026 07:47:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QL3/T3sG Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE8631F00896; Wed, 2 Dec 2026 07:47:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796197644; bh=b0OgvsLvvpdBKAQeGlWy/RZZF1FpmGaup2XpEfoomY4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=QL3/T3sG2SE5OgfdfyHXyOV+FlRuVVvUnJ4FSB26xq2as8wv5LTQkNPascIl4RRz/ quYE8mYGbB46yG1q04yCjJ56Fun28cc02h1LJY5PwTqROzkMGFbf0FyiwG9v0LiiWb O5B20Xo8zGMd6+HODcIQoSUXFa/+93VOWgmXv93h7RKKEPQiMvXNywm6E83zyMMFyU cp+MDeeEpJuk51kVsD2TWMwh2O5RbyjkUEWQ19LcUaokyZa3K3Gh+lWrXiE08yKq2E tLQ3/m9sHCzCdRn5QZWe4cV1AO5kyiZtGN8YvPTJUPb/eDZoLhNge7DlLD5cmiiD7A UuotvBNEqvYnQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v14 06/10] perf: RISC-V: support callchains with SSE delivery Reply-To: sashiko-reviews@lists.linux.dev To: "Zhanpeng Zhang" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261202070003.42491-18-zhangzhanpeng.jasper@bytedance.com> References: <20261202070003.42491-12-zhangzhanpeng.jasper@bytedance.com> <20261202070003.42491-18-zhangzhanpeng.jasper@bytedance.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 07:47:21 +0000 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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.co= m/ Link: https://sashiko.dev/#/patchset/20261119120824.13969-13-zhangzhanpeng.= jasper%40bytedance.com?part=3D7 > 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) =3D=3D 0; > } > =20 > +#ifdef CONFIG_RISCV_SBI_SSE > +static bool sse_addr_on_task_stack(unsigned long addr, unsigned long siz= e) > +{ > + unsigned long end =3D addr + size; > + unsigned long stack; > + > + if (end < addr) > + return false; > + > + stack =3D (unsigned long)task_stack_page(current); > + if (addr >=3D stack && end <=3D stack + THREAD_SIZE) > + return true; > + > + return false; > +} > + > +static bool sse_kernel_regs_safe(struct pt_regs *regs) > +{ > + unsigned long fp =3D frame_pointer(regs); > + unsigned long pc =3D instruction_pointer(regs); > + unsigned long sp =3D 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 =3D=3D 0, and RESTORE_HOST_GPRS restores the ho= st 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_co= ntext *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) =3D=3D 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 =3D riscv_sse_get_interrupted_context(); > + if (!context || context->regs !=3D regs) > + return false; [Severity: Medium] When context is non-NULL but context->regs !=3D 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 =3D current->mm; > + unsigned long ret; > + > + /* With SUPM, untagging even an empty access can dereference current->m= m. */ > + if (!n || !mm) > + return n; > + > + context =3D 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/stacktrac= e.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 =3D READ_ONCE_TASK_STACK(task, frame->ra); > pc =3D ftrace_graph_ret_addr(task, &graph_idx, pc, > &frame->ra); > - if (pc >=3D (unsigned long)handle_exception && > - pc < (unsigned long)&ret_from_exception_end) { > + if (riscv_pc_in_exception_entry(pc)) { > if (unlikely(!fn(arg, pc))) > break; > =20 [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 =3D ((struct pt_regs *)sp)->epc and fp =3D ((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 =3D 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_e= ntry(pc) is false, sse_kernel_regs_safe() can accept the interrupted sp in regs->s0 as a frame pointer on the task stack. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202070003.4249= 1-12-zhangzhanpeng.jasper@bytedance.com?part=3D6