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>, <nsaenzju@redhat.com>,
<frederic@kernel.org>, <changbin.du@intel.com>,
<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 -next 7/7] riscv: stacktrace: Fix unwinding on __kretporbe_trampoline
Date: Tue, 20 Sep 2022 23:12:02 +0800 [thread overview]
Message-ID: <20220920151202.180057-8-chenzhongjin@huawei.com> (raw)
In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com>
When unwinding on __kretprobe_trampoline, the caller of traced function
will be skipped because unwinder doesn't read the saved pt_regs.
Things going like this:
caller's caller | ... |<---+
caller +---------------------------+ |
| ra caller's caller | |
| s0 of caller's caller | |
| ... | |
probed func returned +---------------------------+ |
__kretprobe_trampoline | pt_regs: | |
| epc caller | |
| ra __kretprobe_trampoline| |
| ... | |
| s0 of caller | {ra, fp}
| ... |
Since from caller to __kretprobe_trampoline, the {ra, fp} are not
changed, unwinder will go directly to caller's caller.
Now we can have an ENCODED_FRAME_POINTER on stack and read the pt_regs,
kretporbe will set the epc to correct_ret_addr so that we can unwind
to the correct caller.
Stacktrace before this patch:
Call Trace:
...
[<ffffffff800d5d48>] __kretprobe_trampoline_handler+0xc2/0x13e
[<ffffffff808b766c>] trampoline_probe_handler+0x30/0x46
[<ffffffff800070de>] __kretprobe_trampoline+0x52/0x92
[<ffffffff0163809c>] kprobe_init+0x9c/0x1000 [kprobe_unwind]
[<ffffffff800027c8>] do_one_initcall+0x4c/0x1f2
...
Stacktrace after this patch:
Call Trace:
...
[<ffffffff800d5d48>] __kretprobe_trampoline_handler+0xc2/0x13e
[<ffffffff808b766c>] trampoline_probe_handler+0x30/0x46
[<ffffffff800070de>] __kretprobe_trampoline+0x52/0x92
+ [<ffffffff01633076>] the_caller+0x2c/0x38 [kprobe_unwind]
[<ffffffff0163809c>] kprobe_init+0x9c/0x1000 [kprobe_unwind]
[<ffffffff800027c8>] do_one_initcall+0x4c/0x1f2
...
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
---
arch/riscv/include/asm/stacktrace.h | 4 ++++
arch/riscv/kernel/probes/kprobes_trampoline.S | 8 ++++++++
arch/riscv/kernel/stacktrace.c | 5 +++++
3 files changed, 17 insertions(+)
diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/stacktrace.h
index a39e4ef1dbd5..506c7c38b6cb 100644
--- a/arch/riscv/include/asm/stacktrace.h
+++ b/arch/riscv/include/asm/stacktrace.h
@@ -16,6 +16,10 @@ struct unwind_state {
unsigned long sp;
unsigned long pc;
struct pt_regs *regs;
+#ifdef CONFIG_KRETPROBES
+ struct llist_node *kr_cur;
+ struct task_struct *task;
+#endif
};
extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
diff --git a/arch/riscv/kernel/probes/kprobes_trampoline.S b/arch/riscv/kernel/probes/kprobes_trampoline.S
index 7bdb09ded39b..3c0677a714a6 100644
--- a/arch/riscv/kernel/probes/kprobes_trampoline.S
+++ b/arch/riscv/kernel/probes/kprobes_trampoline.S
@@ -6,6 +6,8 @@
#include <asm/asm.h>
#include <asm/asm-offsets.h>
+#include <asm/frame.h>
+#include <asm/csr.h>
.text
.altmacro
@@ -79,6 +81,12 @@ ENTRY(__kretprobe_trampoline)
addi sp, sp, -(PT_SIZE_ON_STACK)
save_all_base_regs
+#ifdef CONFIG_FRAME_POINTER
+ li s0, SR_PP
+ REG_S s0, PT_STATUS(sp)
+ ENCODE_FRAME_POINTER
+#endif
+
move a0, sp /* pt_regs */
call trampoline_probe_handler
diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index 976dc298ab3b..53edc685ca18 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -11,6 +11,7 @@
#include <linux/sched/task_stack.h>
#include <linux/stacktrace.h>
#include <linux/ftrace.h>
+#include <linux/kprobes.h>
#include <asm/stacktrace.h>
@@ -123,6 +124,10 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
state.sp = task->thread.sp;
state.pc = task->thread.ra;
}
+#ifdef CONFIG_KRETPROBES
+ state.kr_cur = NULL;
+ state.task = task;
+#endif
unwind(&state, consume_entry, cookie);
}
--
2.17.1
next prev parent reply other threads:[~2022-09-20 15:16 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-20 15:11 [PATCH -next 0/7] riscv: Improvments for stacktrace Chen Zhongjin
2022-09-20 15:11 ` [PATCH -next 1/7] riscv: stacktrace: Replace walk_stackframe with arch_stack_walk Chen Zhongjin
2022-09-20 15:11 ` [PATCH -next 2/7] riscv: stacktrace: Introduce unwind functions Chen Zhongjin
2022-09-20 15:11 ` [PATCH -next 3/7] riscv: stacktrace: Save pt_regs in encoded fp on irq entry Chen Zhongjin
2022-09-21 2:13 ` Guo Ren
2022-09-20 15:11 ` [PATCH -next 4/7] riscv: syscall: Don't clobber s0 when syscall Chen Zhongjin
2022-09-21 1:30 ` Guo Ren
2022-09-21 2:16 ` Guo Ren
2022-09-20 15:12 ` [PATCH -next 5/7] riscv: stacktrace: Implement stacktrace for irq Chen Zhongjin
2022-09-20 15:12 ` [PATCH -next 6/7] riscv: stacktrace: Fix unwinding on ftrace_regs_call Chen Zhongjin
2022-09-21 1:43 ` Guo Ren
2022-09-21 3:00 ` Chen Zhongjin
2022-09-20 15:12 ` Chen Zhongjin [this message]
2022-09-21 2:30 ` [PATCH -next 0/7] riscv: Improvments for stacktrace Guo Ren
2022-09-21 12:33 ` Chen Zhongjin
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=20220920151202.180057-8-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=changbin.du@intel.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=nsaenzju@redhat.com \
--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
all inboxes | Powered by JetHome®