From: "Björn Töpel" <bjorn@kernel.org>
To: "Puranjay Mohan" <puranjay12@gmail.com>,
"Paul Walmsley" <paul.walmsley@sifive.com>,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Albert Ou" <aou@eecs.berkeley.edu>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
"Björn Töpel" <bjorn@rivosinc.com>
Cc: puranjay12@gmail.com
Subject: Re: [PATCH 2/2] riscv: stacktrace: make walk_stackframe() more robust
Date: Tue, 02 Apr 2024 15:20:01 +0200 [thread overview]
Message-ID: <87v84zrjem.fsf@all.your.base.are.belong.to.us> (raw)
In-Reply-To: <20240328184020.34278-3-puranjay12@gmail.com>
Puranjay Mohan <puranjay12@gmail.com> writes:
> Currently walk_stackframe() provides only a cookie and the PC to the
> consume_entry function. This doesn't allow the implementation of
> advanced stack walkers that need access to SP and FP as well.
>
> Change walk_stackframe to provide a struct unwind_state to the
> consume_entry function. This unwind_state has all information that is
> available to walk_stackframe. The information provided to the callback
> will not always be live/useful, the callback would be aware of the
> different configurations the information in unwind_state can be.
>
> For example: if CONFIG_FRAME_POINTER is not available, unwind_state->fp
> will always be zero.
>
> This commit doesn't make any functional changes.
>
> Signed-off-by: Puranjay Mohan <puranjay12@gmail.com>
> ---
> arch/riscv/kernel/stacktrace.c | 55 ++++++++++++++++++++++++++++++----
> 1 file changed, 49 insertions(+), 6 deletions(-)
>
> diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
> index e28f7b2e4b6a6..92c41c87b267b 100644
> --- a/arch/riscv/kernel/stacktrace.c
> +++ b/arch/riscv/kernel/stacktrace.c
> @@ -14,15 +14,26 @@
>
> #include <asm/stacktrace.h>
>
> +struct unwind_state {
> + unsigned long fp;
> + unsigned long sp;
> + unsigned long pc;
> + struct pt_regs *regs;
> + struct task_struct *task;
> +};
> +
> +typedef bool (*unwind_consume_fn)(void *cookie, const struct unwind_state *state);
> +
> #ifdef CONFIG_FRAME_POINTER
>
> extern asmlinkage void ret_from_exception(void);
>
> static __always_inline void
> walk_stackframe(struct task_struct *task, struct pt_regs *regs,
> - bool (*fn)(void *, unsigned long), void *arg)
> + unwind_consume_fn fn, void *arg)
> {
> unsigned long fp, sp, pc;
> + struct unwind_state state;
> int level = 0;
>
> if (regs) {
> @@ -40,12 +51,17 @@ walk_stackframe(struct task_struct *task, struct pt_regs *regs,
> sp = task->thread.sp;
> pc = task->thread.ra;
> }
> + state.task = task;
> + state.regs = regs;
>
> for (;;) {
> unsigned long low, high;
> struct stackframe *frame;
>
> - if (unlikely(!__kernel_text_address(pc) || (level++ >= 0 && !fn(arg, pc))))
> + state.sp = sp;
> + state.fp = fp;
> + state.pc = pc;
> + if (unlikely(!__kernel_text_address(pc) || (level++ >= 0 && !fn(arg, &state))))
> break;
>
> /* Validate frame pointer */
> @@ -64,7 +80,10 @@ walk_stackframe(struct task_struct *task, struct pt_regs *regs,
> pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
> &frame->ra);
> if (pc == (unsigned long)ret_from_exception) {
> - if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc)))
> + state.sp = sp;
> + state.fp = fp;
> + state.pc = pc;
> + if (unlikely(!__kernel_text_address(pc) || !fn(arg, &state)))
> break;
>
> pc = ((struct pt_regs *)sp)->epc;
> @@ -79,9 +98,10 @@ walk_stackframe(struct task_struct *task, struct pt_regs *regs,
>
> static __always_inline void
> walk_stackframe(struct task_struct *task, struct pt_regs *regs,
> - bool (*fn)(void *, unsigned long), void *arg)
> + unwind_consume_fn fn, void *arg)
> {
> unsigned long sp, pc;
> + struct unwind_state state;
> unsigned long *ksp;
>
> if (regs) {
> @@ -99,9 +119,14 @@ walk_stackframe(struct task_struct *task, struct pt_regs *regs,
> if (unlikely(sp & 0x7))
> return;
>
> + state.task = task;
> + state.regs = regs;
> + state.sp = sp;
> + state.fp = 0;
> ksp = (unsigned long *)sp;
> while (!kstack_end(ksp)) {
> - if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
> + state.pc = pc;
> + if (__kernel_text_address(pc) && unlikely(!fn(arg, &state)))
> break;
> pc = READ_ONCE_NOCHECK(*ksp++) - 0x4;
> }
> @@ -109,10 +134,28 @@ walk_stackframe(struct task_struct *task, struct pt_regs *regs,
>
> #endif /* CONFIG_FRAME_POINTER */
>
> +struct unwind_consume_entry_data {
> + stack_trace_consume_fn consume_entry;
> + void *cookie;
> +};
> +
> +static __always_inline bool
> +arch_unwind_consume_entry(void *cookie, const struct unwind_state *state)
Same comment as patch 1.
Björn
next prev parent reply other threads:[~2024-04-02 13:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-28 18:40 [PATCH 0/2] riscv: ftrace: make stack walk " Puranjay Mohan
2024-03-28 18:40 ` [PATCH 1/2] riscv: stacktrace: use arch_stack_walk() in place of walk_stackframe Puranjay Mohan
2024-04-02 13:19 ` Björn Töpel
2024-03-28 18:40 ` [PATCH 2/2] riscv: stacktrace: make walk_stackframe() more robust Puranjay Mohan
2024-04-02 13:20 ` Björn Töpel [this message]
2024-04-02 13:18 ` [PATCH 0/2] riscv: ftrace: make stack walk " Björn Töpel
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=87v84zrjem.fsf@all.your.base.are.belong.to.us \
--to=bjorn@kernel.org \
--cc=aou@eecs.berkeley.edu \
--cc=bjorn@rivosinc.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=puranjay12@gmail.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
all inboxes | Powered by JetHome®