From: guoren@kernel.org
To: palmerdabbelt@google.com, paul.walmsley@sifive.com,
mhiramat@kernel.org, alankao@andestech.com, rostedt@goodmis.org
Cc: me@packi.ch, "Guo Ren" <guoren@linux.alibaba.com>,
anup@brainfault.org, linux-kernel@vger.kernel.org,
linux-csky@vger.kernel.org, "Pekka Enberg" <penberg@kernel.org>,
guoren@kernel.org, zong.li@sifive.com,
"Björn Töpel" <bjorn.topel@gmail.com>,
greentime.hu@sifive.com, linux-riscv@lists.infradead.org
Subject: [PATCH v4 6/9] riscv: Add KPROBES_ON_FTRACE supported
Date: Sat, 17 Oct 2020 07:06:14 +0000 [thread overview]
Message-ID: <1602918377-23573-7-git-send-email-guoren@kernel.org> (raw)
In-Reply-To: <1602918377-23573-1-git-send-email-guoren@kernel.org>
From: Guo Ren <guoren@linux.alibaba.com>
This patch adds support for kprobes on ftrace call sites to avoids
much of the overhead with regular kprobes. Try it with simple
steps:
1. Get _do_fork ftrace call site.
Dump of assembler code for function _do_fork:
0xffffffe00020af64 <+0>: addi sp,sp,-128
0xffffffe00020af66 <+2>: sd s0,112(sp)
0xffffffe00020af68 <+4>: sd ra,120(sp)
0xffffffe00020af6a <+6>: addi s0,sp,128
0xffffffe00020af6c <+8>: sd s1,104(sp)
0xffffffe00020af6e <+10>: sd s2,96(sp)
0xffffffe00020af70 <+12>: sd s3,88(sp)
0xffffffe00020af72 <+14>: sd s4,80(sp)
0xffffffe00020af74 <+16>: sd s5,72(sp)
0xffffffe00020af76 <+18>: sd s6,64(sp)
0xffffffe00020af78 <+20>: sd s7,56(sp)
0xffffffe00020af7a <+22>: mv s4,a0
0xffffffe00020af7c <+24>: mv a0,ra
0xffffffe00020af7e <+26>: nop <<<<<<<< here!
0xffffffe00020af82 <+30>: nop
0xffffffe00020af86 <+34>: ld s3,0(s4)
2. Set _do_fork+26 as the kprobe.
echo 'p:myprobe _do_fork+26 dfd=%a0 filename=%a1 flags=%a2 mode=+4($stack)' > /sys/kernel/debug/tracing/kprobe_events
echo 1 > /sys/kernel/debug/tracing/events/kprobes/enable
cat /sys/kernel/debug/tracing/trace
tracer: nop
entries-in-buffer/entries-written: 3/3 #P:1
_-----=> irqs-off
/ _----=> need-resched
| / _---=> hardirq/softirq
|| / _--=> preempt-depth
||| / delay
TASK-PID CPU# |||| TIMESTAMP FUNCTION
| | | |||| | |
sh-87 [000] .... 551.557031: myprobe: (_do_fork+0x1a/0x2e6) dfd=0xffffffe00020af7e filename=0xffffffe00020b34e flags=0xffffffe00101e7c0 mode=0x20af86ffffffe0
cat /sys/kernel/debug/kprobes/list
ffffffe00020af7e k _do_fork+0x1a [FTRACE]
^^^^^^
Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Palmer Dabbelt <palmerdabbelt@google.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Björn Töpel <bjorn.topel@gmail.com>
Cc: Zong Li <zong.li@sifive.com>
Cc: Pekka Enberg <penberg@kernel.org>
---
arch/riscv/Kconfig | 1 +
arch/riscv/kernel/probes/Makefile | 1 +
arch/riscv/kernel/probes/ftrace.c | 52 +++++++++++++++++++++++++++++++++++++++
3 files changed, 54 insertions(+)
create mode 100644 arch/riscv/kernel/probes/ftrace.c
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index c07214a..feafc9d 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -68,6 +68,7 @@ config RISCV
select HAVE_GCC_PLUGINS
select HAVE_GENERIC_VDSO if MMU && 64BIT
select HAVE_KPROBES
+ select HAVE_KPROBES_ON_FTRACE
select HAVE_KRETPROBES
select HAVE_PCI
select HAVE_PERF_EVENTS
diff --git a/arch/riscv/kernel/probes/Makefile b/arch/riscv/kernel/probes/Makefile
index 8a39507..abbd131 100644
--- a/arch/riscv/kernel/probes/Makefile
+++ b/arch/riscv/kernel/probes/Makefile
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o simulate-insn.o
obj-$(CONFIG_KPROBES) += kprobes_trampoline.o
+obj-$(CONFIG_KPROBES_ON_FTRACE) += ftrace.o
CFLAGS_REMOVE_simulate-insn.o = $(CC_FLAGS_FTRACE)
diff --git a/arch/riscv/kernel/probes/ftrace.c b/arch/riscv/kernel/probes/ftrace.c
new file mode 100644
index 00000000..e0fe58a
--- /dev/null
+++ b/arch/riscv/kernel/probes/ftrace.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/kprobes.h>
+
+/* Ftrace callback handler for kprobes -- called under preepmt disabed */
+void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
+ struct ftrace_ops *ops, struct pt_regs *regs)
+{
+ struct kprobe *p;
+ struct kprobe_ctlblk *kcb;
+
+ p = get_kprobe((kprobe_opcode_t *)ip);
+ if (unlikely(!p) || kprobe_disabled(p))
+ return;
+
+ kcb = get_kprobe_ctlblk();
+ if (kprobe_running()) {
+ kprobes_inc_nmissed_count(p);
+ } else {
+ unsigned long orig_ip = instruction_pointer(regs);
+ instruction_pointer_set(regs, ip);
+
+ __this_cpu_write(current_kprobe, p);
+ kcb->kprobe_status = KPROBE_HIT_ACTIVE;
+ if (!p->pre_handler || !p->pre_handler(p, regs)) {
+ /*
+ * Emulate singlestep (and also recover regs->pc)
+ * as if there is a nop
+ */
+ instruction_pointer_set(regs,
+ (unsigned long)p->addr + MCOUNT_INSN_SIZE);
+ if (unlikely(p->post_handler)) {
+ kcb->kprobe_status = KPROBE_HIT_SSDONE;
+ p->post_handler(p, regs, 0);
+ }
+ instruction_pointer_set(regs, orig_ip);
+ }
+
+ /*
+ * If pre_handler returns !0, it changes regs->pc. We have to
+ * skip emulating post_handler.
+ */
+ __this_cpu_write(current_kprobe, NULL);
+ }
+}
+NOKPROBE_SYMBOL(kprobe_ftrace_handler);
+
+int arch_prepare_kprobe_ftrace(struct kprobe *p)
+{
+ p->ainsn.api.insn = NULL;
+ return 0;
+}
--
2.7.4
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2020-10-17 7:07 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-17 7:06 [PATCH v4 0/9] Add k/uprobe & fentry & error_injection supported guoren
2020-10-17 7:06 ` [PATCH v4 1/9] RISC-V: Implement ptrace regs and stack API guoren
2020-11-06 1:03 ` Palmer Dabbelt
2020-11-06 2:54 ` Guo Ren
2020-10-17 7:06 ` [PATCH v4 2/9] riscv: Fixup compile error BUILD_BUG_ON failed guoren
2020-10-17 7:06 ` [PATCH v4 3/9] riscv: Fixup wrong ftrace remove cflag guoren
2020-10-17 7:06 ` [PATCH v4 4/9] riscv: Using PATCHABLE_FUNCTION_ENTRY instead of MCOUNT guoren
2020-10-17 7:06 ` [PATCH v4 5/9] riscv: Add kprobes supported guoren
2020-11-06 1:02 ` Palmer Dabbelt
2020-11-06 2:56 ` Guo Ren
2020-10-17 7:06 ` guoren [this message]
2020-10-17 7:06 ` [PATCH v4 7/9] riscv: Add uprobes supported guoren
2020-10-17 7:06 ` [PATCH v4 8/9] riscv: Add support for function error injection guoren
2020-10-17 7:06 ` [PATCH v4 9/9] riscv: Fixup lockdep_assert_held(&text_mutex) in patch_insn_write guoren
2020-10-19 8:36 ` Masami Hiramatsu
2020-10-19 11:03 ` Guo Ren
2020-10-20 11:14 ` Guo Ren
2020-10-19 18:54 ` Atish Patra
2020-10-20 11:18 ` Guo Ren
2020-10-20 20:41 ` Steven Rostedt
2020-10-21 7:10 ` Zong Li
2020-10-21 8:27 ` Guo Ren
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=1602918377-23573-7-git-send-email-guoren@kernel.org \
--to=guoren@kernel.org \
--cc=alankao@andestech.com \
--cc=anup@brainfault.org \
--cc=bjorn.topel@gmail.com \
--cc=greentime.hu@sifive.com \
--cc=guoren@linux.alibaba.com \
--cc=linux-csky@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=me@packi.ch \
--cc=mhiramat@kernel.org \
--cc=palmerdabbelt@google.com \
--cc=paul.walmsley@sifive.com \
--cc=penberg@kernel.org \
--cc=rostedt@goodmis.org \
--cc=zong.li@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;
as well as URLs for NNTP newsgroup(s).