mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Rui Qi" <qirui.001@bytedance.com>
To: "Paul Walmsley" <pjw@kernel.org>
Cc: "Palmer Dabbelt" <palmer@dabbelt.com>,
	 "Albert Ou" <aou@eecs.berkeley.edu>,
	"Alexandre Ghiti" <alex@ghiti.fr>,
	 <linux-riscv@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v1 2/2] riscv: stacktrace: Use %pB for backtrace display
Date: Fri, 5 Jun 2026 16:12:42 +0800	[thread overview]
Message-ID: <226fe0d0-3d05-4c6b-95fb-a058da2d4169@bytedance.com> (raw)
In-Reply-To: <4197ee54-6c69-cb72-5966-c60ba321a6fa@kernel.org>

On 6/5/26 8:33 AM, Paul Walmsley wrote:
> On Wed, 3 Jun 2026, Rui Qi wrote:
> 
>> The print_trace_address callback uses print_ip_sym which formats
>> addresses with %pS. This does not adjust the address before symbol
>> lookup, so when a noreturn function (e.g. panic, do_exit) is the last
>> call in a function, the saved return address can point to the start of
>> the next function, and kallsyms resolves it to the wrong symbol.
>>
>> The kernel provides %pB (sprint_backtrace) specifically for this
>> purpose: it subtracts 1 from the address before symbol lookup, which
>> is sufficient to fall back into the calling function range. x86 uses
>> %pB in its printk_stack_address for the same reason.
>>
>> Replace print_ip_sym with a direct printk using %pB, matching the
>> pattern used by x86.
>>
>> Signed-off-by: Rui Qi <qirui.001@bytedance.com>
> 
> The Sashiko review mentions that the x86 backtrace code uses %pS for exact 
> instruction pointers coming from registers (show_regs_if_on_stack()), vs. 
> %pB for return addresses on the stack (printk_stack_address()):
> 
>     https://sashiko.dev/#/patchset/20260603115329.791603-1-qirui.001%40bytedance.com
> 
> Do we need to incorporate similar logic?
> 
> 
> - Paul

Hi Paul,

Yes, you are right. The Sashiko review raises a valid point that I
should have addressed in the original patch.

%pB subtracts 1 from the address before symbol lookup, which is correct
for return addresses but wrong for exact instruction pointers: if the
exact IP happens to be at offset 0 of a function, subtracting 1 causes
kallsyms to resolve it to the preceding function.

In walk_stackframe(), two kinds of addresses are passed to the callback:

Exact instruction pointers: instruction_pointer(regs) and the epc
from exception frames found during unwinding (when the return
address points into handle_exception..ret_from_exception_end).
These should use %pS.

Return addresses: frame->ra, regs->ra, task->thread.ra. These
should use %pB.

I have updated the patch to add an 'is_ra' parameter to the
walk_stackframe callback, indicating whether the address is a return
address.  print_trace_address() then uses %pB when is_ra is true and
%pS when is_ra is false, matching the x86 approach.

For the generic arch_stack_walk() path, since stack_trace_consume_fn
has a fixed 2-parameter signature, a thin wrapper (stack_walk_wrapper)
adapts between the two callback types, discarding the is_ra flag since
the generic stack trace infrastructure only records raw addresses.

One known limitation: the non-FP version of walk_stackframe() scans
the stack blindly and cannot distinguish pt_regs->epc values from
return addresses, so all stack-scanned values are treated as return
addresses (is_ra = true).  This means epc values from pt_regs
encountered during the scan may still use %pB incorrectly.  This is
an inherent limitation of the non-FP unwinder and not introduced by
this change; the FP version handles the epc case correctly.

The updated patch will be in v2.

  reply	other threads:[~2026-06-05  8:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-03 11:53 [PATCH v1 0/2] riscv: stacktrace: Fix return address handling in non-FP unwinder Rui Qi
2026-06-03 11:53 ` [PATCH v1 1/2] riscv: stacktrace: Remove bogus -0x4 offset in non-FP walk_stackframe Rui Qi
2026-06-04 23:41   ` Paul Walmsley
2026-06-03 11:53 ` [PATCH v1 2/2] riscv: stacktrace: Use %pB for backtrace display Rui Qi
2026-06-05  0:33   ` Paul Walmsley
2026-06-05  8:12     ` Rui Qi [this message]
2026-06-05  8:20 ` [PATCH v2 0/2] riscv: stacktrace: Fix return address handling in non-FP unwinder Rui Qi
2026-06-05  8:20   ` [PATCH v2 1/2] riscv: stacktrace: Remove bogus -0x4 offset in non-FP walk_stackframe Rui Qi
2026-06-05  8:20   ` [PATCH v2 2/2] riscv: stacktrace: Use %pB for return addresses and %pS for exact IPs Rui Qi
2026-06-07  5:10   ` [PATCH v2 0/2] riscv: stacktrace: Fix return address handling in non-FP unwinder patchwork-bot+linux-riscv

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=226fe0d0-3d05-4c6b-95fb-a058da2d4169@bytedance.com \
    --to=qirui.001@bytedance.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@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

all inboxes | Powered by JetHome®