Skip to contentSkip to conversation

Enable new kprobe event at boot

PATCH Superseded Masami Hiramatsu 2 participants 7 messages May 21, 2019 – May 22, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

cover Enable new kprobe event at boot

This series adds a kernel parameter, 'kprobe_event=' to add and enable new kprobe events at boot time.

Currently ftrace can enable some existing trace events at boot time. This also allows admin/developer to add new kprobe-events at boot time to debug device drivers etc.

The syntax is similar to tracing/kprobe_events interface, but uses ',' and ';' instead of ' ' and '\n' respectively. e.g.

kprobe_event=p,func1,$arg1,$arg2;p,func2,$arg1

will add probes on func1 with the 1st and the 2nd arguments and on func2 with the 1st argument.

Note that 'trace_event=' option enables trace event at very early timing, but the events added by 'kprobe_event=' are enabled right before enabling device drivers at this point. It is enough for tracing device driver initialization etc.

Thank you,

---

Masami Hiramatsu (2):
kprobes: Initialize kprobes at subsys_initcall tracing/kprobe: Add kprobe_event= boot parameter

 Documentation/admin-guide/kernel-parameters.txt |   13 ++++++
 Documentation/trace/kprobetrace.rst             |   14 ++++++
 kernel/kprobes.c                                |    2 -
 kernel/trace/trace_kprobe.c                     |   54 +++++++++++++++++++++++
 4 files changed, 82 insertions(+), 1 deletion(-)

--
Masami Hiramatsu (Linaro)

author maintainer
#1 lore raw

PATCH 1/2 kprobes: Initialize kprobes at subsys_initcall

Initialize kprobes at subsys_initcall stage instead of module_init since kprobes is not a module. This will allow ftrace kprobe event to add new events when it is initializing because ftrace kprobe event is initialized at fs_initcall.

---
 kernel/kprobes.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kprobes.c +1 -1
index c83e54727131..b7b6c90bb59c 100644
@@ -2617,4 +2617,4 @@ static int __init debugfs_kprobe_init(void)
 late_initcall(debugfs_kprobe_init);
 #endif /* CONFIG_DEBUG_FS */
 
-module_init(init_kprobes);
+subsys_initcall(init_kprobes);
author maintainer
#2 lore raw

PATCH 2/2 tracing/kprobe: Add kprobe_event= boot parameter

In reply to Masami Hiramatsu

Add kprobe_event= boot parameter to define kprobe events at boot time.
The definition syntax is similar to tracefs/kprobe_events interface, but use ',' and ';' instead of ' ' and '\n' respectively. e.g.

kprobe_event=p,vfs_read,$arg1,$arg2

This puts a probe on vfs_read with argument1 and 2, and enable the new event.

---
 Documentation/admin-guide/kernel-parameters.txt |   13 ++++++
 Documentation/trace/kprobetrace.rst             |   14 ++++++
 kernel/trace/trace_kprobe.c                     |   54 +++++++++++++++++++++++
 3 files changed, 81 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +13 -0
index 2b8ee90bb644..96b22db7f763 100644
@@ -2002,6 +2002,19 @@
 			Built with CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=y,
 			the default is off.
 
+	kprobe_event=[probe-list]
+			[FTRACE] Add kprobe events and enable at boot time.
+			The probe-list is a semicolon delimited list of probe
+			definitions. Each definition is same as kprobe_events
+			interface, but the parameters are comma delimited.
+			For example, to add a kprobe event on vfs_read with
+			arg1 and arg2, add to the command line;
+
+			      kprobe_event=p,vfs_read,$arg1,$arg2
+
+			See also Documentation/trace/kprobetrace.rst "Kernel
+			Boot Parameter" section.
+
 	kpti=		[ARM64] Control page table isolation of user
 			and kernel address spaces.
 			Default: enabled on cores which need mitigation.
Documentation/trace/kprobetrace.rst +14 -0
index 235ce2ab131a..5b129215ba33 100644
@@ -124,6 +124,20 @@ You can check the total number of probe hits and probe miss-hits via
 The first column is event name, the second is the number of probe hits,
 the third is the number of probe miss-hits.
 
+Kernel Boot Parameter
+---------------------
+You can add and enable new kprobe events when booting up the kernel by
+"kprobe_event=" parameter. The parameter accepts a semicolon-delimited
+kprobe events, which format is similar to the kprobe_events.
+The difference is that the probe definition parameters are comma-delimited
+instead of space. For example, adding myprobe event on do_sys_open like below
+
+  p:myprobe do_sys_open dfd=%ax filename=%dx flags=%cx mode=+4($stack)
+
+should be below for kernel boot parameter (just replace spaces with comma)
+
+  p:myprobe,do_sys_open,dfd=%ax,filename=%dx,flags=%cx,mode=+4($stack)
+
 
 Usage examples
 --------------
kernel/trace/trace_kprobe.c +54 -0

78 lines not on this page · raw message

maintainer
#3 lore raw
In reply to Masami Hiramatsu
Quoted 4 lines · On Tue, 21 May 2019 16:56:16 +0900 Masami Hiramatsu <[email protected]> wrote:

Note that 'trace_event=' option enables trace event at very early timing, but the events added by 'kprobe_event=' are enabled right before enabling device drivers at this point. It is enough for tracing device driver initialization etc.

Nice!

I wonder if we can have this called before the trace_event boot is analyzed. Then have the kprobe_event work more like the kprobe_events file, and not enable the kprobes but only create them. If you want to enable them you do a trace_event=kprobes as well.

Perhaps we could enable kprobes at early init?

What do you think? Or is there something else in kprobes that prevents such an early enabling of it?

-- Steve

author maintainer
#4 lore raw
Quoted 8 lines · On Tue, 21 May 2019 09:33:17 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Tue, 21 May 2019 16:56:16 +0900 Masami Hiramatsu <[email protected]> wrote:

Note that 'trace_event=' option enables trace event at very early timing, but the events added by 'kprobe_event=' are enabled right before enabling device drivers at this point. It is enough for tracing device driver initialization etc.

Quoted 6 lines

Nice!

I wonder if we can have this called before the trace_event boot is analyzed. Then have the kprobe_event work more like the kprobe_events file, and not enable the kprobes but only create them. If you want to enable them you do a trace_event=kprobes as well.

Yeah, I considered that, but there are several reasons to not to do that.
- trace_event seems enabled very early point than kprobes itself.(but this can be fixable)
- if user specifies kprobes at boot, he/she wants to enable that point at boot.
- it is redundant to specify kprobe_event= and trace_event=, especially command line size is very limited.

Quoted 1 line

Perhaps we could enable kprobes at early init?

It should be possible, I will try to find what blocks it. I guess after we switch early_text_poke() to text_poke(), we can use kprobes on x86. But for other archs, I need to investigate more.

Quoted 2 lines

What do you think? Or is there something else in kprobes that prevents such an early enabling of it?

As I pointed above, I think we should enable it if user specify it. That's less typing :). Anyway I'll recheck early kprobe availablity.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#5 lore raw
Quoted 5 lines · On Wed, 22 May 2019 00:39:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 1 line

Perhaps we could enable kprobes at early init?

It should be possible, I will try to find what blocks it. I guess after we switch early_text_poke() to text_poke(), we can use kprobes on x86. But for other archs, I need to investigate more.

OK, I just follow the kernel init related to kprobes

start_kernel()
-> trace_init()
-> rcu_init_nohz()
-> perf_event_init()
-> arch_call_rest_init()
-> rest_init()

     -> rcu_scheduler_starting()
     -> kernel_thread(kernel_init)

kernel_init()
-> kernel_init_freeable()
-> wait_for_completion(&kthreadd_done);
-> workqueue_init()
-> smp_init()
-> do_basic_setup()

     -> do_initcalls()
       -> do_initcall_level()
         (in subsys-level)
         -> init_kprobes()

Since optprobe uses workqueue, we can not move it before workqueue_init(). (but maybe we can disable it in early stage)
Also, since kprobes depends on rcu, I guess we can not move it before rcu_scheduler_starting().
for kretprobe, we need to get the possible cpus, we need a fix if we move it before before smp_init().
However, there is no reason we need to run it in subsys level. We can move init_kprobes() in core or pure level safely.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#6 lore raw
Quoted 30 lines · On Wed, 22 May 2019 16:30:21 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 5 lines · On Wed, 22 May 2019 00:39:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 1 line

Perhaps we could enable kprobes at early init?

It should be possible, I will try to find what blocks it. I guess after we switch early_text_poke() to text_poke(), we can use kprobes on x86. But for other archs, I need to investigate more.

OK, I just follow the kernel init related to kprobes

start_kernel()
-> trace_init()
-> rcu_init_nohz()
-> perf_event_init()
-> arch_call_rest_init()
-> rest_init()

     -> rcu_scheduler_starting()
     -> kernel_thread(kernel_init)
kernel_init()
 -> kernel_init_freeable()
   -> wait_for_completion(&kthreadd_done);
   -> workqueue_init()
   -> smp_init()
   -> do_basic_setup()
     -> do_initcalls()
       -> do_initcall_level()
         (in subsys-level)
         -> init_kprobes()
Quoted 4 lines

Since optprobe uses workqueue, we can not move it before workqueue_init(). (but maybe we can disable it in early stage)
Also, since kprobes depends on rcu, I guess we can not move it before rcu_scheduler_starting().

It seems rcu is not initialized yet at this point. It is initialized fully at core_initcall.

Quoted 4 lines

for kretprobe, we need to get the possible cpus, we need a fix if we move it before before smp_init().
However, there is no reason we need to run it in subsys level. We can move init_kprobes() in core or pure level safely.

Also, I missed ftrace (function tracer) is initialized at core_initcall(). So we can move init_kprobes() at postcore_initcall() safely.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>