linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64/kprobe: add support for KPROBES_ON_FTRACE
@ 2019-12-23 12:38 Cheng Jian
  2019-12-23 12:39 ` chengjian (D)
  0 siblings, 1 reply; 2+ messages in thread
From: Cheng Jian @ 2019-12-23 12:38 UTC (permalink / raw)
  To: linux-arm-kernel, linux-kernel
  Cc: will, catalin.marinas, mhiramat, naveen.n.rao, sandeepa.s.prabhu,
	cj.chengjian, bobo.shaobowang

Allow KPROBES to use the ftrace infrastructure.
This depends on HAVE_DYNAMIC_FTRACE_WITH_REGS
which introduce by :
        commit 3b23e4991fb6 ("arm64: implement ftrace with regs")

Based on the x86 code by Masami.

With:
        # cd /sys/kernel/debug/tracing
        # echo 'p _do_fork+0x4' > kprobe_events

before patch:
	# cat ../kprobes/list
        sh: write error: Invalid argument

after patch:
	# cat ../kprobes/list
        ffffa00016aa9d4c  k  _do_fork+0x4    [FTRACE]

Signed-off-by: Cheng Jian <cj.chengjian@huawei.com>
---
 .../debug/kprobes-on-ftrace/arch-support.txt  |  2 +-
 arch/arm64/Kconfig                            |  1 +
 arch/arm64/kernel/probes/Makefile             |  2 +
 arch/arm64/kernel/probes/ftrace.c             | 65 +++++++++++++++++++
 4 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/kernel/probes/ftrace.c

diff --git a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
index 4fae0464ddff..f9dd9dd91e0c 100644
--- a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
+++ b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
@@ -9,7 +9,7 @@
     |       alpha: | TODO |
     |         arc: | TODO |
     |         arm: | TODO |
-    |       arm64: | TODO |
+    |       arm64: |  ok  |
     |         c6x: | TODO |
     |        csky: | TODO |
     |       h8300: | TODO |
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index b1b4476ddb83..d613be9c2346 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -144,6 +144,7 @@ config ARM64
 	select HAVE_DYNAMIC_FTRACE
 	select HAVE_DYNAMIC_FTRACE_WITH_REGS \
 		if $(cc-option,-fpatchable-function-entry=2)
+	select HAVE_KPROBES_ON_FTRACE if HAVE_DYNAMIC_FTRACE_WITH_REGS
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	select HAVE_FAST_GUP
 	select HAVE_FTRACE_MCOUNT_RECORD
diff --git a/arch/arm64/kernel/probes/Makefile b/arch/arm64/kernel/probes/Makefile
index 8e4be92e25b1..45a9d916a47d 100644
--- a/arch/arm64/kernel/probes/Makefile
+++ b/arch/arm64/kernel/probes/Makefile
@@ -4,3 +4,5 @@ obj-$(CONFIG_KPROBES)		+= kprobes.o decode-insn.o	\
 				   simulate-insn.o
 obj-$(CONFIG_UPROBES)		+= uprobes.o decode-insn.o	\
 				   simulate-insn.o
+
+obj-$(CONFIG_KPROBES_ON_FTRACE)	+= ftrace.o
diff --git a/arch/arm64/kernel/probes/ftrace.c b/arch/arm64/kernel/probes/ftrace.c
new file mode 100644
index 000000000000..f89bfaa3e5f3
--- /dev/null
+++ b/arch/arm64/kernel/probes/ftrace.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Dynamic Ftrace based Kprobes Optimization
+ *
+ * Copyright (C) Hitachi Ltd., 2012
+ * Copyright (C) 2019 Huawei Inc.
+ * Copyright 2019 Cheng Jian <cj.chengjian@huawei.com>
+ */
+#include <linux/kprobes.h>
+#include <linux/ptrace.h>
+#include <linux/hardirq.h>
+#include <linux/preempt.h>
+#include <linux/ftrace.h>
+
+
+/* Ftrace callback handler for kprobes -- called under preepmt disabed */
+void kprobe_ftrace_handler(unsigned long pc, unsigned long parent_pc,
+			   struct ftrace_ops *ops, struct pt_regs *regs)
+{
+	struct kprobe *p;
+	struct kprobe_ctlblk *kcb;
+
+	/* Preempt is disabled by ftrace */
+	p = get_kprobe((kprobe_opcode_t *)pc);
+	if (unlikely(!p) || kprobe_disabled(p))
+		return;
+
+	kcb = get_kprobe_ctlblk();
+	if (kprobe_running()) {
+		kprobes_inc_nmissed_count(p);
+	} else {
+		/*
+		 * On ARM64, recg->pc is *before* this instruction for the
+		 * pre handler.
+		 */
+		regs->pc -= MCOUNT_INSN_SIZE;
+
+		__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->ip)
+			 * as if there is a nop
+			 */
+			regs->pc += MCOUNT_INSN_SIZE;
+			if (unlikely(p->post_handler)) {
+				kcb->kprobe_status = KPROBE_HIT_SSDONE;
+				p->post_handler(p, regs, 0);
+			}
+		}
+		/*
+		 * If pre_handler returns !0, it changes regs->ip. 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.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] arm64/kprobe: add support for KPROBES_ON_FTRACE
  2019-12-23 12:38 [PATCH] arm64/kprobe: add support for KPROBES_ON_FTRACE Cheng Jian
@ 2019-12-23 12:39 ` chengjian (D)
  0 siblings, 0 replies; 2+ messages in thread
From: chengjian (D) @ 2019-12-23 12:39 UTC (permalink / raw)
  To: linux-arm-kernel, linux-kernel
  Cc: will, catalin.marinas, mhiramat, naveen.n.rao, sandeepa.s.prabhu,
	bobo.shaobowang

I am SORRY.

I searched on patchwork and found this :

arm64: implement KPROBES_ON_FTRACE

https://lore.kernel.org/patchwork/patch/1169445/

So please ignore my EMAIL.

Thank you.

---- Cheng Jian


On 2019/12/23 20:38, Cheng Jian wrote:
> Allow KPROBES to use the ftrace infrastructure.
> This depends on HAVE_DYNAMIC_FTRACE_WITH_REGS
> which introduce by :
>          commit 3b23e4991fb6 ("arm64: implement ftrace with regs")
>
> Based on the x86 code by Masami.
>
> With:
>          # cd /sys/kernel/debug/tracing
>          # echo 'p _do_fork+0x4' > kprobe_events
>
> before patch:
> 	# cat ../kprobes/list
>          sh: write error: Invalid argument
>
> after patch:
> 	# cat ../kprobes/list
>          ffffa00016aa9d4c  k  _do_fork+0x4    [FTRACE]
>
> Signed-off-by: Cheng Jian <cj.chengjian@huawei.com>
> ---
>   .../debug/kprobes-on-ftrace/arch-support.txt  |  2 +-
>   arch/arm64/Kconfig                            |  1 +
>   arch/arm64/kernel/probes/Makefile             |  2 +
>   arch/arm64/kernel/probes/ftrace.c             | 65 +++++++++++++++++++
>   4 files changed, 69 insertions(+), 1 deletion(-)
>   create mode 100644 arch/arm64/kernel/probes/ftrace.c
>
> diff --git a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
> index 4fae0464ddff..f9dd9dd91e0c 100644
> --- a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
> +++ b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
> @@ -9,7 +9,7 @@
>       |       alpha: | TODO |
>       |         arc: | TODO |
>       |         arm: | TODO |
> -    |       arm64: | TODO |
> +    |       arm64: |  ok  |
>       |         c6x: | TODO |
>       |        csky: | TODO |
>       |       h8300: | TODO |
> diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
> index b1b4476ddb83..d613be9c2346 100644
> --- a/arch/arm64/Kconfig
> +++ b/arch/arm64/Kconfig
> @@ -144,6 +144,7 @@ config ARM64
>   	select HAVE_DYNAMIC_FTRACE
>   	select HAVE_DYNAMIC_FTRACE_WITH_REGS \
>   		if $(cc-option,-fpatchable-function-entry=2)
> +	select HAVE_KPROBES_ON_FTRACE if HAVE_DYNAMIC_FTRACE_WITH_REGS
>   	select HAVE_EFFICIENT_UNALIGNED_ACCESS
>   	select HAVE_FAST_GUP
>   	select HAVE_FTRACE_MCOUNT_RECORD
> diff --git a/arch/arm64/kernel/probes/Makefile b/arch/arm64/kernel/probes/Makefile
> index 8e4be92e25b1..45a9d916a47d 100644
> --- a/arch/arm64/kernel/probes/Makefile
> +++ b/arch/arm64/kernel/probes/Makefile
> @@ -4,3 +4,5 @@ obj-$(CONFIG_KPROBES)		+= kprobes.o decode-insn.o	\
>   				   simulate-insn.o
>   obj-$(CONFIG_UPROBES)		+= uprobes.o decode-insn.o	\
>   				   simulate-insn.o
> +
> +obj-$(CONFIG_KPROBES_ON_FTRACE)	+= ftrace.o
> diff --git a/arch/arm64/kernel/probes/ftrace.c b/arch/arm64/kernel/probes/ftrace.c
> new file mode 100644
> index 000000000000..f89bfaa3e5f3
> --- /dev/null
> +++ b/arch/arm64/kernel/probes/ftrace.c
> @@ -0,0 +1,65 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Dynamic Ftrace based Kprobes Optimization
> + *
> + * Copyright (C) Hitachi Ltd., 2012
> + * Copyright (C) 2019 Huawei Inc.
> + * Copyright 2019 Cheng Jian <cj.chengjian@huawei.com>
> + */
> +#include <linux/kprobes.h>
> +#include <linux/ptrace.h>
> +#include <linux/hardirq.h>
> +#include <linux/preempt.h>
> +#include <linux/ftrace.h>
> +
> +
> +/* Ftrace callback handler for kprobes -- called under preepmt disabed */
> +void kprobe_ftrace_handler(unsigned long pc, unsigned long parent_pc,
> +			   struct ftrace_ops *ops, struct pt_regs *regs)
> +{
> +	struct kprobe *p;
> +	struct kprobe_ctlblk *kcb;
> +
> +	/* Preempt is disabled by ftrace */
> +	p = get_kprobe((kprobe_opcode_t *)pc);
> +	if (unlikely(!p) || kprobe_disabled(p))
> +		return;
> +
> +	kcb = get_kprobe_ctlblk();
> +	if (kprobe_running()) {
> +		kprobes_inc_nmissed_count(p);
> +	} else {
> +		/*
> +		 * On ARM64, recg->pc is *before* this instruction for the
> +		 * pre handler.
> +		 */
> +		regs->pc -= MCOUNT_INSN_SIZE;
> +
> +		__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->ip)
> +			 * as if there is a nop
> +			 */
> +			regs->pc += MCOUNT_INSN_SIZE;
> +			if (unlikely(p->post_handler)) {
> +				kcb->kprobe_status = KPROBE_HIT_SSDONE;
> +				p->post_handler(p, regs, 0);
> +			}
> +		}
> +		/*
> +		 * If pre_handler returns !0, it changes regs->ip. 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;
> +}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-12-23 12:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-23 12:38 [PATCH] arm64/kprobe: add support for KPROBES_ON_FTRACE Cheng Jian
2019-12-23 12:39 ` chengjian (D)

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).