[PATCH] arm: Add KPROBES_ON_FTRACE supported

2024-06-13 Thread Jinjie Ruan
Add support for kprobes on ftrace call sites to avoid much of the overhead
with regular kprobes. Try it with simple steps:

cd /sys/kernel/debug/tracing/
echo 'p:myprobe sys_clone r0=%r0 r1=%r1 r2=%r2' > kprobe_events
echo 1 > events/kprobes/enable
echo  1 > events/kprobes/myprobe/enable
cat trace
# tracer: nop
#
# entries-in-buffer/entries-written: 2/2   #P:4
#
#_-=> irqs-off/BH-disabled
#   / _=> need-resched
#  | / _---=> hardirq/softirq
#  || / _--=> preempt-depth
#  ||| / _-=> migrate-disable
#   / delay
#   TASK-PID CPU#  |  TIMESTAMP  FUNCTION
#  | | |   | | |
  sh-75  [000] .33.793362: myprobe: 
(sys_clone+0xc/0xa0) r0=0x1200011 r1=0x0 r2=0x0
  sh-75  [000] .34.817804: myprobe: 
(sys_clone+0xc/0xa0) r0=0x1200011 r1=0x0 r2=0x0

cat /sys/kernel/debug/kprobes/list
c03453e8  k  sys_clone+0xc[FTRACE]
   ^^

Signed-off-by: Jinjie Ruan 
---
 .../debug/kprobes-on-ftrace/arch-support.txt  |  2 +-
 arch/arm/Kconfig  |  1 +
 arch/arm/include/asm/ftrace.h | 17 ++
 arch/arm/kernel/ftrace.c  | 17 --
 arch/arm/probes/Makefile  |  1 +
 arch/arm/probes/ftrace.c  | 53 +++
 arch/arm/probes/kprobes/core.c| 32 +++
 7 files changed, 105 insertions(+), 18 deletions(-)
 create mode 100644 arch/arm/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 02febc883588..4ecd7d53e859 100644
--- a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
+++ b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
@@ -8,7 +8,7 @@
 ---
 |   alpha: | TODO |
 | arc: | TODO |
-| arm: | TODO |
+| arm: |  ok  |
 |   arm64: | TODO |
 |csky: |  ok  |
 | hexagon: | TODO |
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index ee5115252aac..ed13b1743f94 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -114,6 +114,7 @@ config ARM
select HAVE_KERNEL_LZO
select HAVE_KERNEL_XZ
select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
+   select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL && !CPU_ENDIAN_BE32 && 
!CPU_V7M
select HAVE_KRETPROBES if HAVE_KPROBES
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI
diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h
index 5be3ddc96a50..c8e3f808b70c 100644
--- a/arch/arm/include/asm/ftrace.h
+++ b/arch/arm/include/asm/ftrace.h
@@ -22,6 +22,23 @@ struct dyn_arch_ftrace {
 #endif
 };
 
+/*
+ * The compiler emitted profiling hook consists of
+ *
+ *   PUSH{LR}
+ *   BL __gnu_mcount_nc
+ *
+ * To turn this combined sequence into a NOP, we need to restore the value of
+ * SP before the PUSH. Let's use an ADD rather than a POP into LR, as LR is not
+ * modified anyway, and reloading LR from memory is highly likely to be less
+ * efficient.
+ */
+#ifdef CONFIG_THUMB2_KERNEL
+#defineNOP 0xf10d0d04  /* add.w sp, sp, #4 */
+#else
+#defineNOP 0xe28dd004  /* add   sp, sp, #4 */
+#endif
+
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
/* With Thumb-2, the recorded addresses have the lsb set */
diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c
index a0b6d1e3812f..f0f1bdf27637 100644
--- a/arch/arm/kernel/ftrace.c
+++ b/arch/arm/kernel/ftrace.c
@@ -25,23 +25,6 @@
 #include 
 #include 
 
-/*
- * The compiler emitted profiling hook consists of
- *
- *   PUSH{LR}
- *   BL __gnu_mcount_nc
- *
- * To turn this combined sequence into a NOP, we need to restore the value of
- * SP before the PUSH. Let's use an ADD rather than a POP into LR, as LR is not
- * modified anyway, and reloading LR from memory is highly likely to be less
- * efficient.
- */
-#ifdef CONFIG_THUMB2_KERNEL
-#defineNOP 0xf10d0d04  /* add.w sp, sp, #4 */
-#else
-#defineNOP 0xe28dd004  /* add   sp, sp, #4 */
-#endif
-
 #ifdef CONFIG_DYNAMIC_FTRACE
 
 static int __ftrace_modify_code(void *data)
diff --git a/arch/arm/probes/Makefile b/arch/arm/probes/Makefile
index 8b0ea5ace100..b3c355942a21 100644
--- a/arch/arm/probes/Makefile
+++ b/arch/arm/probes/Makefi

[PATCH v2] arm: Add KPROBES_ON_FTRACE supported

2024-06-17 Thread Jinjie Ruan
Add support for kprobes on ftrace call sites to avoid much of the overhead
with regular kprobes. Try it with simple steps:

cd /sys/kernel/debug/tracing/
echo 'p:myprobe sys_clone r0=%r0 r1=%r1 r2=%r2' > kprobe_events
echo 1 > events/kprobes/enable
echo  1 > events/kprobes/myprobe/enable
cat trace
# tracer: nop
#
# entries-in-buffer/entries-written: 2/2   #P:4
#
#_-=> irqs-off/BH-disabled
#   / _=> need-resched
#  | / _---=> hardirq/softirq
#  || / _--=> preempt-depth
#  ||| / _-=> migrate-disable
#   / delay
#   TASK-PID CPU#  |  TIMESTAMP  FUNCTION
#  | | |   | | |
  sh-75  [000] .33.793362: myprobe: 
(sys_clone+0xc/0xa0) r0=0x1200011 r1=0x0 r2=0x0
  sh-75  [000] .34.817804: myprobe: 
(sys_clone+0xc/0xa0) r0=0x1200011 r1=0x0 r2=0x0

cat /sys/kernel/debug/kprobes/list
c03453e8  k  sys_clone+0xc[FTRACE]
   ^^

Signed-off-by: Jinjie Ruan 
Reported-by: kernel test robot 
Closes: 
https://lore.kernel.org/oe-kbuild-all/202406160646.j89u1ukk-...@intel.com/
---
v2:
- Fix the allmodconfig compile issue by renaming "NOP" to "FTRACE_NOP".
---
 .../debug/kprobes-on-ftrace/arch-support.txt  |  2 +-
 arch/arm/Kconfig  |  1 +
 arch/arm/include/asm/ftrace.h | 17 ++
 arch/arm/kernel/ftrace.c  | 19 +--
 arch/arm/probes/Makefile  |  1 +
 arch/arm/probes/ftrace.c  | 53 +++
 arch/arm/probes/kprobes/core.c| 32 +++
 7 files changed, 106 insertions(+), 19 deletions(-)
 create mode 100644 arch/arm/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 02febc883588..4ecd7d53e859 100644
--- a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
+++ b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
@@ -8,7 +8,7 @@
 ---
 |   alpha: | TODO |
 | arc: | TODO |
-| arm: | TODO |
+| arm: |  ok  |
 |   arm64: | TODO |
 |csky: |  ok  |
 | hexagon: | TODO |
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 9f09a16338e3..036381c5d42f 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -114,6 +114,7 @@ config ARM
select HAVE_KERNEL_LZO
select HAVE_KERNEL_XZ
select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
+   select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL && !CPU_ENDIAN_BE32 && 
!CPU_V7M
select HAVE_KRETPROBES if HAVE_KPROBES
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI
diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h
index 5be3ddc96a50..ecf5590f3657 100644
--- a/arch/arm/include/asm/ftrace.h
+++ b/arch/arm/include/asm/ftrace.h
@@ -22,6 +22,23 @@ struct dyn_arch_ftrace {
 #endif
 };
 
+/*
+ * The compiler emitted profiling hook consists of
+ *
+ *   PUSH{LR}
+ *   BL __gnu_mcount_nc
+ *
+ * To turn this combined sequence into a NOP, we need to restore the value of
+ * SP before the PUSH. Let's use an ADD rather than a POP into LR, as LR is not
+ * modified anyway, and reloading LR from memory is highly likely to be less
+ * efficient.
+ */
+#ifdef CONFIG_THUMB2_KERNEL
+#defineFTRACE_NOP  0xf10d0d04  /* add.w sp, sp, #4 */
+#else
+#defineFTRACE_NOP  0xe28dd004  /* add   sp, sp, #4 */
+#endif
+
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
/* With Thumb-2, the recorded addresses have the lsb set */
diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c
index e61591f33a6c..0bb372f5aa1d 100644
--- a/arch/arm/kernel/ftrace.c
+++ b/arch/arm/kernel/ftrace.c
@@ -25,23 +25,6 @@
 #include 
 #include 
 
-/*
- * The compiler emitted profiling hook consists of
- *
- *   PUSH{LR}
- *   BL __gnu_mcount_nc
- *
- * To turn this combined sequence into a NOP, we need to restore the value of
- * SP before the PUSH. Let's use an ADD rather than a POP into LR, as LR is not
- * modified anyway, and reloading LR from memory is highly likely to be less
- * efficient.
- */
-#ifdef CONFIG_THUMB2_KERNEL
-#defineNOP 0xf10d0d04  /* add.w sp, sp, #4 */
-#else
-#defineNOP 0xe28dd004  /* add   sp, sp, #4 */
-#endif
-
 #i

Re: [PATCH v2] arm: Add KPROBES_ON_FTRACE supported

2024-07-29 Thread Jinjie Ruan
Gentle ping.

On 2024/6/18 11:52, Jinjie Ruan wrote:
> Add support for kprobes on ftrace call sites to avoid much of the overhead
> with regular kprobes. Try it with simple steps:
> 
>   cd /sys/kernel/debug/tracing/
>   echo 'p:myprobe sys_clone r0=%r0 r1=%r1 r2=%r2' > kprobe_events
>   echo 1 > events/kprobes/enable
>   echo  1 > events/kprobes/myprobe/enable
>   cat trace
>   # tracer: nop
>   #
>   # entries-in-buffer/entries-written: 2/2   #P:4
>   #
>   #_-=> irqs-off/BH-disabled
>   #   / _=> need-resched
>   #  | / _---=> hardirq/softirq
>   #  || / _--=> preempt-depth
>   #  ||| / _-=> migrate-disable
>   #   / delay
>   #   TASK-PID CPU#  |  TIMESTAMP  FUNCTION
>   #  | | |   | | |
> sh-75  [000] .33.793362: myprobe: 
> (sys_clone+0xc/0xa0) r0=0x1200011 r1=0x0 r2=0x0
> sh-75  [000] .34.817804: myprobe: 
> (sys_clone+0xc/0xa0) r0=0x1200011 r1=0x0 r2=0x0
> 
>   cat /sys/kernel/debug/kprobes/list
>   c03453e8  k  sys_clone+0xc[FTRACE]
>  ^^
> 
> Signed-off-by: Jinjie Ruan 
> Reported-by: kernel test robot 
> Closes: 
> https://lore.kernel.org/oe-kbuild-all/202406160646.j89u1ukk-...@intel.com/
> ---
> v2:
> - Fix the allmodconfig compile issue by renaming "NOP" to "FTRACE_NOP".
> ---
>  .../debug/kprobes-on-ftrace/arch-support.txt  |  2 +-
>  arch/arm/Kconfig  |  1 +
>  arch/arm/include/asm/ftrace.h | 17 ++
>  arch/arm/kernel/ftrace.c  | 19 +--
>  arch/arm/probes/Makefile  |  1 +
>  arch/arm/probes/ftrace.c  | 53 +++
>  arch/arm/probes/kprobes/core.c| 32 +++
>  7 files changed, 106 insertions(+), 19 deletions(-)
>  create mode 100644 arch/arm/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 02febc883588..4ecd7d53e859 100644
> --- a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
> +++ b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt
> @@ -8,7 +8,7 @@
>  ---
>  |   alpha: | TODO |
>  | arc: | TODO |
> -| arm: | TODO |
> +| arm: |  ok  |
>  |   arm64: | TODO |
>  |csky: |  ok  |
>  | hexagon: | TODO |
> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
> index 9f09a16338e3..036381c5d42f 100644
> --- a/arch/arm/Kconfig
> +++ b/arch/arm/Kconfig
> @@ -114,6 +114,7 @@ config ARM
>   select HAVE_KERNEL_LZO
>   select HAVE_KERNEL_XZ
>   select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
> + select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL && !CPU_ENDIAN_BE32 && 
> !CPU_V7M
>   select HAVE_KRETPROBES if HAVE_KPROBES
>   select HAVE_MOD_ARCH_SPECIFIC
>   select HAVE_NMI
> diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h
> index 5be3ddc96a50..ecf5590f3657 100644
> --- a/arch/arm/include/asm/ftrace.h
> +++ b/arch/arm/include/asm/ftrace.h
> @@ -22,6 +22,23 @@ struct dyn_arch_ftrace {
>  #endif
>  };
>  
> +/*
> + * The compiler emitted profiling hook consists of
> + *
> + *   PUSH{LR}
> + *   BL   __gnu_mcount_nc
> + *
> + * To turn this combined sequence into a NOP, we need to restore the value of
> + * SP before the PUSH. Let's use an ADD rather than a POP into LR, as LR is 
> not
> + * modified anyway, and reloading LR from memory is highly likely to be less
> + * efficient.
> + */
> +#ifdef CONFIG_THUMB2_KERNEL
> +#define  FTRACE_NOP  0xf10d0d04  /* add.w sp, sp, #4 */
> +#else
> +#define  FTRACE_NOP  0xe28dd004  /* add   sp, sp, #4 */
> +#endif
> +
>  static inline unsigned long ftrace_call_adjust(unsigned long addr)
>  {
>   /* With Thumb-2, the recorded addresses have the lsb set */
> diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c
> index e61591f33a6c..0bb372f5aa1d 100644
> --- a/arch/arm/kernel/ftrace.c
> +++ b/arch/arm/kernel/ftrace.c
> @@ -25,23 +25,6 @@
>  #include 
>  #include 
>  
> -/*
> - * The compi