From: Chen Zhongjin <chenzhongjin@huawei.com>
To: <linux-kernel@vger.kernel.org>, <linux-riscv@lists.infradead.org>,
<linux-perf-users@vger.kernel.org>
Cc: <paul.walmsley@sifive.com>, <palmer@dabbelt.com>,
<aou@eecs.berkeley.edu>, <peterz@infradead.org>,
<mingo@redhat.com>, <acme@kernel.org>, <mark.rutland@arm.com>,
<alexander.shishkin@linux.intel.com>, <namhyung@kernel.org>,
<jolsa@kernel.org>, <guoren@kernel.org>, <frederic@kernel.org>,
<vincent.chen@sifive.com>, <ardb@kernel.org>,
<mhiramat@kernel.org>, <rostedt@goodmis.org>,
<keescook@chromium.org>, <catalin.marinas@arm.com>,
<chenzhongjin@huawei.com>
Subject: [PATCH for-next v2 2/4] riscv: stacktrace: Introduce unwind functions
Date: Wed, 21 Sep 2022 20:51:25 +0800 [thread overview]
Message-ID: <20220921125128.33913-3-chenzhongjin@huawei.com> (raw)
In-Reply-To: <20220921125128.33913-1-chenzhongjin@huawei.com>
Now all riscv unwinding code is inside arch_stack_walk. It's
not same as other architectures.
Make some refactoring, to move unwinding code into unwind() and
unwind_next() functions, which walks through all stack frames
or single frame.
This patch only moves code but doesn't make any logical change.
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
---
arch/riscv/include/asm/stacktrace.h | 7 ++
arch/riscv/kernel/stacktrace.c | 104 ++++++++++++++++++----------
2 files changed, 74 insertions(+), 37 deletions(-)
diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/stacktrace.h
index b6cd3eddfd38..a39e4ef1dbd5 100644
--- a/arch/riscv/include/asm/stacktrace.h
+++ b/arch/riscv/include/asm/stacktrace.h
@@ -11,6 +11,13 @@ struct stackframe {
unsigned long ra;
};
+struct unwind_state {
+ unsigned long fp;
+ unsigned long sp;
+ unsigned long pc;
+ struct pt_regs *regs;
+};
+
extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
const char *loglvl);
diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index b51e32d50a0e..e84e21868a3e 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -16,54 +16,84 @@
#ifdef CONFIG_FRAME_POINTER
-noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
- void *cookie, struct task_struct *task,
- struct pt_regs *regs)
+static int notrace unwind_next(struct unwind_state *state)
{
- unsigned long fp, sp, pc;
- int level = 0;
+ unsigned long low, high, fp;
+ struct stackframe *frame;
- if (regs) {
- fp = frame_pointer(regs);
- sp = user_stack_pointer(regs);
- pc = instruction_pointer(regs);
- } else if (task == NULL || task == current) {
- fp = (unsigned long)__builtin_frame_address(0);
- sp = current_stack_pointer;
- pc = (unsigned long)arch_stack_walk;
+ fp = state->fp;
+
+ /* Validate frame pointer */
+ low = state->sp + sizeof(struct stackframe);
+ high = ALIGN(low, THREAD_SIZE);
+
+ if (fp < low || fp > high || fp & 0x7)
+ return -EINVAL;
+
+ /* Unwind stack frame */
+ frame = (struct stackframe *)fp - 1;
+ state->sp = fp;
+
+ if (state->regs && state->regs->epc == state->pc &&
+ fp & 0x7) {
+ state->fp = frame->ra;
+ state->pc = state->regs->ra;
} else {
- /* task blocked in __switch_to */
- fp = task->thread.s[0];
- sp = task->thread.sp;
- pc = task->thread.ra;
+ state->fp = frame->fp;
+ state->pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
+ (unsigned long *)fp - 1);
}
- for (;;) {
- unsigned long low, high;
- struct stackframe *frame;
+ return 0;
+}
- if (unlikely(!__kernel_text_address(pc) ||
- (level++ >= 1 && !consume_entry(cookie, pc))))
+static void notrace unwind(struct unwind_state *state,
+ stack_trace_consume_fn consume_entry, void *cookie)
+{
+ while (1) {
+ int ret;
+
+ if (!__kernel_text_address(state->pc))
+ break;
+
+ if (!consume_entry(cookie, state->pc))
break;
- /* Validate frame pointer */
- low = sp + sizeof(struct stackframe);
- high = ALIGN(sp, THREAD_SIZE);
- if (unlikely(fp < low || fp > high || fp & 0x7))
+ ret = unwind_next(state);
+ if (ret < 0)
break;
- /* Unwind stack frame */
- frame = (struct stackframe *)fp - 1;
- sp = fp;
- if (regs && (regs->epc == pc) && (frame->fp & 0x7)) {
- fp = frame->ra;
- pc = regs->ra;
- } else {
- fp = frame->fp;
- pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
- (unsigned long *)(fp - 8));
- }
+ }
+}
+
+noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
+ void *cookie, struct task_struct *task,
+ struct pt_regs *regs)
+{
+ struct unwind_state state;
+
+ if (task == NULL)
+ task = current;
+ if (regs) {
+ state.fp = frame_pointer(regs);
+ state.sp = user_stack_pointer(regs);
+ state.pc = instruction_pointer(regs);
+ state.regs = regs;
+ } else if (task == current) {
+ state.fp = (unsigned long)__builtin_frame_address(0);
+ state.sp = current_stack_pointer;
+ state.pc = (unsigned long)arch_stack_walk;
+
+ /* skip frame of arch_stack_walk */
+ unwind_next(&state);
+ } else {
+ /* task blocked in __switch_to */
+ state.fp = task->thread.s[0];
+ state.sp = task->thread.sp;
+ state.pc = task->thread.ra;
}
+
+ unwind(&state, consume_entry, cookie);
}
#else /* !CONFIG_FRAME_POINTER */
--
2.17.1
next prev parent reply other threads:[~2022-09-21 12:55 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-21 12:51 [PATCH for-next v2 0/4] riscv: Improvments for stacktrace Chen Zhongjin
2022-09-21 12:51 ` [PATCH for-next v2 1/4] riscv: stacktrace: Replace walk_stackframe with arch_stack_walk Chen Zhongjin
2022-09-21 12:51 ` Chen Zhongjin [this message]
2022-09-21 12:51 ` [PATCH for-next v2 3/4] riscv: stacktrace: Save pt_regs in ENCODE_FRAME_POINTER Chen Zhongjin
2022-09-21 12:51 ` [PATCH for-next v2 4/4] riscv: stacktrace: Implement stacktrace for irq Chen Zhongjin
2022-09-21 14:02 ` [PATCH for-next v2 0/4] riscv: Improvments for stacktrace Mark Rutland
2022-09-22 8:22 ` Chen Zhongjin
2022-09-23 13:34 ` Mark Rutland
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=20220921125128.33913-3-chenzhongjin@huawei.com \
--to=chenzhongjin@huawei.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=frederic@kernel.org \
--cc=guoren@kernel.org \
--cc=jolsa@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=vincent.chen@sifive.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