Linux EFI development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>, linux-trace-kernel@vger.kernel.org
Cc: jpoimboe@kernel.org, rostedt@goodmis.org, peterz@infradead.org,
	mingo@redhat.com, tglx@kernel.org, bp@alien8.de,
	dave.hansen@linux.intel.com, x86@kernel.org,
	catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com,
	pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
	alex@ghiti.fr, acme@kernel.org, namhyung@kernel.org,
	linux-perf-users@vger.kernel.org, mhiramat@kernel.org,
	ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	bpf@vger.kernel.org, nathan@kernel.org, nsc@kernel.org,
	linux-kbuild@vger.kernel.org, ojeda@kernel.org, ardb@kernel.org,
	linux-efi@vger.kernel.org, "Marc Zyngier" <maz@kernel.org>,
	"Oliver Upton" <oupton@kernel.org>,
	kvmarm@lists.linux.dev, shuah@kernel.org, indu.bhagat@oracle.com,
	jremus@linux.ibm.com, wnliu@google.com, dylanbhatch@google.com,
	gary@garyguo.net
Subject: Re: [RFC PATCH v1 09/11] riscv/unwind: Add Minimal DWARF CFI kernel unwinder
Date: Mon, 23 Nov 2026 00:24:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-228469@kernel.org> (raw)
In-Reply-To: <20261122235757.3456048-10-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] riscv/unwind: off-by-one in return_address() from
  arch_stack_walk() tail call
- [Medium] riscv/unwind: broken leaf BPF frame detection in
  walk_stackframe_cfi()
- [Medium] riscv/unwind: riscv_stack_bounds() and handle_exception
  reject sp == high
--

commit fd04e120d9c16cff3a5efd054c9692101ae1abd1
Author: Ian Rogers <irogers@google.com>

riscv/unwind: Add Minimal DWARF CFI kernel unwinder

This commit adds CONFIG_UNWINDER_DWARF_CFI to enable frame-pointer-free
kernel stack unwinding and reliable stack traces on 64-bit MMU-enabled
RISC-V kernels using Minimal DWARF CFI.

> diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
> index c7555447149bf..05f846c147877 100644
> --- a/arch/riscv/kernel/stacktrace.c
> +++ b/arch/riscv/kernel/stacktrace.c
[ ... ]
> +static __always_inline unsigned int
> +riscv_stack_bounds(struct task_struct *task, unsigned long addr,
> +		   size_t size, unsigned long *out_low,
> +		   unsigned long *out_high)
> +{
> +	unsigned long low, high;
> +
> +	low = (unsigned long)task_stack_page(task);
> +	high = low + THREAD_SIZE;
> +	if (addr >= low && addr < high && addr + size >= addr &&
> +	    addr + size <= high) {
> +		*out_low = low;
> +		*out_high = high;
> +		return RISCV_STACK_TASK;
> +	}
> +
> +	if (task == current && !preemptible()) {
> +#ifdef CONFIG_IRQ_STACKS
> +		low = (unsigned long)raw_cpu_read(irq_stack_ptr);
> +		high = low + IRQ_STACK_SIZE;
> +		if (low && addr >= low && addr < high && addr + size >= addr &&
> +		    addr + size <= high) {
> +			*out_low = low;
> +			*out_high = high;
> +			return RISCV_STACK_IRQ;
> +		}
> +#endif
> +#ifdef CONFIG_VMAP_STACK
> +		low = (unsigned long)raw_cpu_ptr(overflow_stack);
> +		high = low + OVERFLOW_STACK_SIZE;
> +		if (addr >= low && addr < high && addr + size >= addr &&
> +		    addr + size <= high) {
> +			*out_low = low;
> +			*out_high = high;
> +			return RISCV_STACK_OVERFLOW;
> +		}
> +#endif
> +	}
> +
> +	return 0;
> +}
[ ... ]
> +		if (pc >= (unsigned long)handle_exception &&
> +		    pc < (unsigned long)&ret_from_exception_end) {
> +			struct pt_regs *er = (struct pt_regs *)sp;
> +			unsigned long status, next_sp, next_ra;
> +
> +			if (sp & (sizeof(long) - 1))
> +				return -EINVAL;
> +			cur_stk = riscv_stack_bounds(task, sp, sizeof(*er),
> +						     &cur_low, &cur_high);
> +			if (!cur_stk)
> +				return -EINVAL;
> +			status = READ_ONCE_TASK_STACK(task, er->status);
> +			if ((status & SR_PP) == 0)
> +				return 0;
> +			if (require_reliable)
> +				return -EINVAL;
> +
> +			next_pc = READ_ONCE_TASK_STACK(task, er->epc);
> +			next_sp = READ_ONCE_TASK_STACK(task, er->sp);
> +			next_fp = READ_ONCE_TASK_STACK(task, er->s0);
> +			next_ra = READ_ONCE_TASK_STACK(task, er->ra);
> +			if (next_sp & (sizeof(long) - 1))
> +				return -EINVAL;
> +			next_stk = riscv_stack_bounds(task, next_sp,
> +						      sizeof(long),
> +						      &low, &high);
> +			if (!next_stk)
> +				return -EINVAL;

[Severity: Medium]
Does riscv_stack_bounds() reject a valid stack pointer at the top of the
IRQ stack (sp == irq_stack_ptr + IRQ_STACK_SIZE)?

call_on_irq_stack() sets sp to irq_stack_ptr + IRQ_STACK_SIZE before
calling handle_riscv_irq() or ___do_softirq().  At the pre-prologue entry
(such as after unwinding ftrace_caller) or post-epilogue ret of that root
function, sp equals high.  Because riscv_stack_bounds() requires addr <
high even when size == 0, and the handle_exception check passes size =
sizeof(long) instead of 0 for next_sp, unwinding from or across that point
aborts with -EINVAL.

[ ... ]

> +			/*
> +			 * Fallback to frame-pointer record at (fp - 16) for
> +			 * JITed BPF programs and BPF trampolines.
> +			 */
> +			if (!require_reliable && !(sp & 0x7) &&
> +			    riscv_stack_bounds(task, sp, /*size=*/0, &cur_low,
> +					       &cur_high) &&
> +			    !(fp & 0x7) &&
> +			    fp >= sp + sizeof(struct stackframe) &&
> +			    fp <= cur_high) {
> +				struct stackframe *fr = (struct stackframe *)fp - 1;
> +				unsigned long old_fp = fp;
> +
> +				next_fp = READ_ONCE_TASK_STACK(task, fr->fp);
> +				next_pc = READ_ONCE_TASK_STACK(task, fr->ra);
> +				if (exact_pc && ra_valid &&
> +				    !(next_pc & 0x7) &&
> +				    next_pc >= old_fp && next_pc <= cur_high) {
> +					/* Leaf BPF prog without saved ra */
> +					fp = next_pc;
> +					pc = ra;
> +				} else {
> +					next_pc = riscv_recover_ret_addr(task,
> +							&graph_idx, &kr_cur,
> +							next_pc, old_fp,
> +							&fr->ra);
> +					if (!next_pc)
> +						return 0;
> +					fp = next_fp;
> +					pc = next_pc;
> +				}
> +				sp = old_fp;
> +				ra_valid = false;
> +				exact_pc = false;
> +				unreliable = true;
> +				continue;
> +			}

[Severity: Medium]
Will the leaf BPF program check fail when its C caller was built with
-fomit-frame-pointer?

In bpf_jit_build_prologue(), a leaf BPF program omits saving RV_REG_RA and
saves the caller's s0 at fp - 8 (fr->ra):

arch/riscv/net/bpf_jit_comp64.c:bpf_jit_build_prologue() {
    ...
	if (seen_reg(RV_REG_RA, ctx)) {
		emit_sd(RV_REG_SP, store_offset, RV_REG_RA, ctx);
		store_offset -= 8;
	}
	emit_sd(RV_REG_SP, store_offset, RV_REG_FP, ctx);
	store_offset -= 8;
    ...
}

Under CONFIG_UNWINDER_DWARF_CFI=y, the C caller uses s0 as an ordinary
callee-saved register rather than a frame pointer in [old_fp, cur_high].
When an interrupt or perf sample lands inside a leaf BPF program, next_pc
(the caller's s0) fails the [old_fp, cur_high] check and the else branch
treats that s0 value as the return PC, truncating the stack trace above the
BPF frame.

[ ... ]

> +noinline void notrace walk_stackframe(struct task_struct *task,
> +				      struct pt_regs *regs,
> +				      bool (*fn)(void *, unsigned long),
> +				      void *arg)
> +{
> +	walk_stackframe_cfi(task, regs, fn, arg, /*require_reliable=*/false);
> +}

[Severity: Medium]
Can tail-call optimization of arch_stack_walk() -> walk_stackframe() shift
return_address(level) by one frame under CONFIG_UNWINDER_DWARF_CFI=y?

return_address() sets data.level = level + 3 expecting return_address(),
arch_stack_walk(), and walk_stackframe() to be separate frames:

arch/riscv/kernel/return_address.c:return_address() {
    ...
	data.level = level + 3;
	data.addr = NULL;

	arch_stack_walk(save_return_addr, &data, current, NULL);
    ...
}

Unlike CONFIG_FRAME_POINTER=y, CONFIG_UNWINDER_DWARF_CFI=y does not add
-fno-optimize-sibling-calls, so arch_stack_walk() tail-calls the noinline
walk_stackframe() without creating a stack frame.  When
walk_stackframe_cfi() skips walk_stackframe() via level = -1, the first
frame reported to save_return_addr() is return_address() instead of
arch_stack_walk(), causing return_address(level) and CALLER_ADDR* to return
the caller at level + 1.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122235757.3456048-1-irogers@google.com?part=9

  reply	other threads:[~2026-11-23  0:24 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 23:57 [RFC PATCH v1 00/11] unwind: Minimal DWARF CFI stack unwinding for x86, arm64, and riscv Ian Rogers
2026-11-22 23:57 ` [RFC PATCH v1 01/11] unwind: Add Minimal DWARF CFI bytecode interpreter and KUnit tests Ian Rogers
2026-11-23  0:11   ` sashiko-bot
2026-11-23  0:40   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 02/11] unwind_user: Support Minimal DWARF CFI user-space unwinding Ian Rogers
2026-11-23  0:11   ` sashiko-bot
2026-11-23  0:39   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 03/11] scripts, unwind: Add build-time .eh_frame minifier Ian Rogers
2026-11-23  0:40   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 04/11] objtool/ibt: Ignore .eh_frame relocations during ENDBR validation Ian Rogers
2026-11-22 23:57 ` [RFC PATCH v1 05/11] x86/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-23  0:56   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 06/11] arm64: unwind_user: Support Minimal DWARF CFI and FP user unwinding Ian Rogers
2026-11-23  0:16   ` sashiko-bot
2026-11-22 23:57 ` [RFC PATCH v1 07/11] arm64/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-23  0:24   ` sashiko-bot
2026-11-23  0:56   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 08/11] riscv: unwind_user: Support Minimal DWARF CFI and FP user unwinding Ian Rogers
2026-11-23  0:39   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 09/11] riscv/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-23  0:24   ` sashiko-bot [this message]
2026-11-23  0:56   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 10/11] selftests/perf_events: Add Minimal DWARF CFI deferred user unwind test Ian Rogers
2026-11-23  0:40   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 11/11] scripts: Add eh_frame_min.py .eh_frame coverage and size analyzer Ian Rogers
2026-11-23  0:39   ` bot+bpf-ci

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-228469@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acme@kernel.org \
    --cc=alex@ghiti.fr \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ardb@kernel.org \
    --cc=ast@kernel.org \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dylanbhatch@google.com \
    --cc=gary@garyguo.net \
    --cc=indu.bhagat@oracle.com \
    --cc=irogers@google.com \
    --cc=jpoimboe@kernel.org \
    --cc=jremus@linux.ibm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=nsc@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=oupton@kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=peterz@infradead.org \
    --cc=pjw@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shuah@kernel.org \
    --cc=tglx@kernel.org \
    --cc=will@kernel.org \
    --cc=wnliu@google.com \
    --cc=x86@kernel.org \
    /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