Skip to contentSkip to conversation

tracing/boot: Add new options for tracing specific period

PATCH Superseded Masami Hiramatsu 3 participants 12 messages Aug 31, 2020 – Sep 1, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

cover tracing/boot: Add new options for tracing specific period

Hi,

Here is a series of patches to improve the boot-time tracing to support kretprobe and tracing_on option. The combination of these options allows us to trace events while a specific function call period.

This also includes a bugfix about boot-time kprobe event as [1/6].

For example, this bootconfig will make a function callgraph in the pci_proc_init() function at boot time.

ftrace {
	tracing_on = 0  # off at start
	tracer = function_graph
	event.kprobes {
		start_event {
			probes = "pci_proc_init"
			actions = "traceon"
		}
		end_event {
			probes = "pci_proc_init%return"
			actions = "traceoff"
		}
	}
}

Here is the example output;

# tracer: function_graph
#
# CPU DURATION FUNCTION CALLS
# | | | | | | |

 0)               |  pci_proc_init() {
 0)               |    proc_mkdir() {
 0)               |      proc_mkdir_data() {
 0)               |        __proc_create() {
 0)               |          _raw_read_lock() {
 0)   0.179 us    |            preempt_count_add();
 0)   0.203 us    |            do_raw_read_lock();
 0)   1.210 us    |          }
 0)               |          __xlate_proc_name() {
 0)   0.449 us    |            pde_subdir_find();
 0)   0.913 us    |          }
 0)               |          _raw_read_unlock() {
 0)   0.169 us    |            do_raw_read_unlock();
 0)   0.175 us    |            preempt_count_sub();
 0)   0.841 us    |          }
 0)               |          kmem_cache_alloc() {
 0)               |            fs_reclaim_acquire() {
 0)   0.154 us    |              __need_fs_reclaim();
 0)   0.240 us    |              fs_reclaim_acquire.part.0();
 0)   0.889 us    |            }
 0)               |            fs_reclaim_release() {
 0)   0.174 us    |              __need_fs_reclaim();
 0)   0.516 us    |            }
 0)   0.157 us    |            should_failslab();
 0)               |            rcu_read_lock_sched_held() {
 0)               |              rcu_read_lock_held_common() {
 0)   0.156 us    |                rcu_is_watching();
 0)   0.158 us    |                rcu_lockdep_current_cpu_online();
 0)   0.735 us    |              }
 0)   1.054 us    |            }
 0)   3.407 us    |          }
 0)   0.168 us    |          __raw_spin_lock_init();
 0)   7.575 us    |        }
 0)               |        proc_register() {
 0)               |          _raw_spin_lock_irqsave() {
 0)   0.187 us    |            preempt_count_add();

...

Thank you,

---

Masami Hiramatsu (6):
kprobes: tracing/kprobes: Fix to kill kprobes on initmem after boot tracing/boot: Add per-instance tracing_on option support
Documentation: tracing: Add tracing_on option to boot-time tracer tracing/kprobes: Support perf-style return probe
Documentation: tracing: Add %return suffix description
Documentation: tracing: boot: Add an example of tracing function-calls

 Documentation/trace/boottime-trace.rst |   24 ++++++++++++++++++++++++
 Documentation/trace/kprobetrace.rst    |    2 ++
 include/linux/kprobes.h                |    5 +++++
 init/main.c                            |    2 ++
 kernel/kprobes.c                       |   22 ++++++++++++++++++++++
 kernel/trace/trace_boot.c              |   10 ++++++++++
 kernel/trace/trace_kprobe.c            |   21 ++++++++++++++++++++-
 7 files changed, 85 insertions(+), 1 deletion(-)

--
Masami Hiramatsu (Linaro) <[email protected]>

author maintainer
#1 lore raw

PATCH 1/6 kprobes: tracing/kprobes: Fix to kill kprobes on initmem after boot

Since kprobe_event= cmdline option allows user to put kprobes on the functions in initmem, kprobe has to make such probes gone after boot. Currently the probes on the init functions in modules will be handled by module callback, but the kernel init text isn't handled. Without this, kprobes may access non-exist text area to disable or remove it.

Fixes 970988e19eb0 ("tracing/kprobe: Add kprobe_event= boot parameter")
---
 include/linux/kprobes.h |    5 +++++
 init/main.c             |    2 ++
 kernel/kprobes.c        |   22 ++++++++++++++++++++++
 3 files changed, 29 insertions(+)
include/linux/kprobes.h +5 -0
index 9be1bff4f586..8aab327b5539 100644
@@ -373,6 +373,8 @@ void unregister_kretprobes(struct kretprobe **rps, int num);
 void kprobe_flush_task(struct task_struct *tk);
 void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
 
+void kprobe_free_init_mem(void);
+
 int disable_kprobe(struct kprobe *kp);
 int enable_kprobe(struct kprobe *kp);
 
@@ -435,6 +437,9 @@ static inline void unregister_kretprobes(struct kretprobe **rps, int num)
 static inline void kprobe_flush_task(struct task_struct *tk)
 {
 }
+static inline void kprobe_free_init_mem(void)
+{
+}
 static inline int disable_kprobe(struct kprobe *kp)
 {
 	return -ENOSYS;
init/main.c +2 -0
index ae78fb68d231..038128b2a755 100644
@@ -33,6 +33,7 @@
 #include <linux/nmi.h>
 #include <linux/percpu.h>
 #include <linux/kmod.h>
+#include <linux/kprobes.h>
 #include <linux/vmalloc.h>
 #include <linux/kernel_stat.h>
 #include <linux/start_kernel.h>
@@ -1402,6 +1403,7 @@ static int __ref kernel_init(void *unused)
 	kernel_init_freeable();
 	/* need to finish all async __init code before freeing the memory */
 	async_synchronize_full();
+	kprobe_free_init_mem();
 	ftrace_free_init_mem();
 	free_initmem();
 	mark_readonly();
kernel/kprobes.c +22 -0
index 287b263c9cb9..48747bd60295 100644
@@ -2726,6 +2726,28 @@ static int disarm_all_kprobes(void)
 	return ret;
 }
 
+void kprobe_free_init_mem(void)
+{
+	void *start = (void *)(&__init_begin);
+	void *end = (void *)(&__init_end);
+	struct hlist_head *head;
+	struct kprobe *p;
+	int i;
+
+	mutex_lock(&kprobe_mutex);
+
+	/* Kill all kprobes on initmem */
+	for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
+		head = &kprobe_table[i];
+		hlist_for_each_entry(p, head, hlist) {
+			if (start <= (void *)p->addr && (void *)p->addr < end)
+				kill_kprobe(p);
+		}
+	}
+
+	mutex_unlock(&kprobe_mutex);
+}
+
 /*
  * XXX: The debugfs bool file interface doesn't allow for callbacks
  * when the bool state is switched. We can reuse that facility when
author maintainer
#4 lore raw

PATCH 2/6 tracing/boot: Add per-instance tracing_on option support

In reply to Masami Hiramatsu

Add per-instance tracing_on option, which will be useful with traceon/traceoff event trigger actions. For example, if we disable tracing_on by default and set traceon and traceoff on a pair of events, we can trace functions between the pair of events.

---
 kernel/trace/trace_boot.c |   10 ++++++++++
 1 file changed, 10 insertions(+)
kernel/trace/trace_boot.c +10 -0
index fa0fc08c6ef8..d52d441a17e8 100644
@@ -40,6 +40,16 @@ trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node)
 			pr_err("Failed to set option: %s\n", buf);
 	}
 
+	p = xbc_node_find_value(node, "tracing_on", NULL);
+	if (p && *p != '\0') {
+		if (kstrtoul(p, 10, &v))
+			pr_err("Failed to set tracing on: %s\n", p);
+		if (v)
+			tracer_tracing_on(tr);
+		else
+			tracer_tracing_off(tr);
+	}
+
 	p = xbc_node_find_value(node, "trace_clock", NULL);
 	if (p && *p != '\0') {
 		if (tracing_set_clock(tr, p) < 0)
author maintainer
#5 lore raw

PATCH 3/6 Documentation: tracing: Add tracing_on option to boot-time tracer

In reply to Masami Hiramatsu

Add tracing_on option description to the boot-time tracer.

---
 Documentation/trace/boottime-trace.rst |    4 ++++
 1 file changed, 4 insertions(+)
Documentation/trace/boottime-trace.rst +4 -0
index dcb390075ca1..1341b449acaa 100644
@@ -61,6 +61,10 @@ These options can be used for each instance including global ftrace node.
 ftrace.[instance.INSTANCE.]options = OPT1[, OPT2[...]]
    Enable given ftrace options.
 
+ftrace.[instance.INSTANCE.]tracing_on = 0|1
+   Enable/Disable tracing on this instance when boot.
+   (you can enable it by the "traceon" event trigger action)
+
 ftrace.[instance.INSTANCE.]trace_clock = CLOCK
    Set given CLOCK to ftrace's trace_clock.
 
author maintainer
#6 lore raw

PATCH 4/6 tracing/kprobes: Support perf-style return probe

In reply to Masami Hiramatsu

Support perf-style return probe ("SYMBOL%return") in kprobe events. This will allow boot-time tracing user to define a return probe event.

---
 kernel/trace/trace_kprobe.c |   21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
kernel/trace/trace_kprobe.c +20 -1
index aefb6065b508..391361b67c8f 100644
@@ -709,6 +709,18 @@ static inline void sanitize_event_name(char *name)
 			*name = '_';
 }
 
+static inline bool split_return_suffix(char *symbol)
+{
+	char *p = strchr(symbol, '%');
+
+	if (p && !strcmp(p, "%return")) {
+		*p = '\0';
+		return true;
+	}
+
+	return false;
+}
+
 static int trace_kprobe_create(int argc, const char *argv[])
 {
 	/*
@@ -717,6 +729,9 @@ static int trace_kprobe_create(int argc, const char *argv[])
 	 *      p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
 	 *  - Add kretprobe:
 	 *      r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
+	 *    Or
+	 *      p:[GRP/]EVENT] [MOD:]KSYM[+0]%return [FETCHARGS]
+	 *
 	 * Fetch args:
 	 *  $retval	: fetch return value
 	 *  $stack	: fetch stack address
@@ -746,7 +761,6 @@ static int trace_kprobe_create(int argc, const char *argv[])
 	switch (argv[0][0]) {
 	case 'r':
 		is_return = true;
-		flags |= TPARG_FL_RETURN;
 		break;
 	case 'p':
 		break;
@@ -804,12 +818,17 @@ static int trace_kprobe_create(int argc, const char *argv[])
 		symbol = kstrdup(argv[1], GFP_KERNEL);
 		if (!symbol)
 			return -ENOMEM;
+
+		is_return = split_return_suffix(symbol) || is_return;
+
 		/* TODO: support .init module functions */
 		ret = traceprobe_split_symbol_offset(symbol, &offset);
 		if (ret || offset < 0 || offset > UINT_MAX) {
 			trace_probe_log_err(0, BAD_PROBE_ADDR);
 			goto parse_error;
 		}
+		if (is_return)
+			flags |= TPARG_FL_RETURN;
 		if (kprobe_on_func_entry(NULL, symbol, offset))
 			flags |= TPARG_FL_FENTRY;
 		if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
author maintainer
#7 lore raw

PATCH 5/6 Documentation: tracing: Add %return suffix description

In reply to Masami Hiramatsu

Add a description of the %return suffix option for kprobe tracer.

---
 Documentation/trace/kprobetrace.rst |    2 ++
 1 file changed, 2 insertions(+)
Documentation/trace/kprobetrace.rst +2 -0
index c1709165c553..d29d1f9e6721 100644
@@ -30,6 +30,7 @@ Synopsis of kprobe_events
 
   p[:[GRP/]EVENT] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS]	: Set a probe
   r[MAXACTIVE][:[GRP/]EVENT] [MOD:]SYM[+0] [FETCHARGS]	: Set a return probe
+  p:[GRP/]EVENT] [MOD:]SYM[+0]%return [FETCHARGS]	: Set a return probe
   -:[GRP/]EVENT						: Clear a probe
 
  GRP		: Group name. If omitted, use "kprobes" for it.
@@ -37,6 +38,7 @@ Synopsis of kprobe_events
 		  based on SYM+offs or MEMADDR.
  MOD		: Module name which has given SYM.
  SYM[+offs]	: Symbol+offset where the probe is inserted.
+ SYM%return     : Return address of the symbol
  MEMADDR	: Address where the probe is inserted.
  MAXACTIVE	: Maximum number of instances of the specified function that
 		  can be probed simultaneously, or 0 for the default value
author maintainer
#11 lore raw

PATCH 6/6 Documentation: tracing: boot: Add an example of tracing function-calls

In reply to Masami Hiramatsu

Add an example of tracing function calls on a specific function.

---
 Documentation/trace/boottime-trace.rst |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
Documentation/trace/boottime-trace.rst +20 -0
index 1341b449acaa..c216f5695ae2 100644
@@ -168,6 +168,26 @@ is for tracing functions starting with "user\_", and others tracing
 The instance node also accepts event nodes so that each instance
 can customize its event tracing.
 
+With the trigger action and kprobes, you can trace function-graph while
+a function is called. For example, this will trace all function calls in
+the pci_proc_init()::
+
+  ftrace {
+        tracing_on = 0
+        tracer = function_graph
+        event.kprobes {
+                start_event {
+                        probes = "pci_proc_init"
+                        actions = "traceon"
+                }
+                end_event {
+                        probes = "pci_proc_init%return"
+                        actions = "traceoff"
+                }
+        }
+  }
+
+
 This boot-time tracing also supports ftrace kernel parameters via boot
 config.
 For example, following kernel parameters::
#2 lore raw

Re: [PATCH 1/6] kprobes: tracing/kprobes: Fix to kill kprobes on initmem after boot

In reply to Masami Hiramatsu
I love your patch! Yet something to improve:

Hi Masami,

I love your patch! Yet something to improve:

[auto build test ERROR on tip/perf/core]
[also build test ERROR on trace/for-next lwn/docs-next linus/master v5.9-rc3 next-20200828] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/tracing-boot-Add-new-options-for-tracing-specific-period/20200831-204738
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 2cb5383b30d47c446ec7d884cd80f93ffcc31817
config: arc-defconfig (attached as .config)
compiler: arc-elf-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arc

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

arc-elf-ld: init/main.o: in function `kernel_init':

>> main.c:(.ref.text+0xa6): undefined reference to `kprobe_free_init_mem'
>> arc-elf-ld: main.c:(.ref.text+0xa6): undefined reference to `kprobe_free_init_mem'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

reviewer
#8 lore raw

Re: [PATCH 5/6] Documentation: tracing: Add %return suffix description

In reply to Masami Hiramatsu

Documentation/trace/kprobetrace.rst

Quoted 11 lines · Masami Hiramatsu

Add a description of the %return suffix option for kprobe tracer.

---
 Documentation/trace/kprobetrace.rst |    2 ++
 1 file changed, 2 insertions(+)
Documentation/trace/kprobetrace.rst +0 -0
index c1709165c553..d29d1f9e6721 100644

Check spacing:

Documentation/trace/kprobetrace.rst Synopsis of kprobe_events · line 44

Quoted 8 lines
Documentation/trace/kprobetrace.rst +1 -0
@@ -37,6 +38,7 @@ Synopsis of kprobe_events
 		  based on SYM+offs or MEMADDR.
  MOD		: Module name which has given SYM.
  SYM[+offs]	: Symbol+offset where the probe is inserted.
+ SYM%return     : Return address of the symbol
  MEMADDR	: Address where the probe is inserted.
  MAXACTIVE	: Maximum number of instances of the specified function that
 		  can be probed simultaneously, or 0 for the default value

If I remove the '+', the ':' lines up but the SYM does not line up. Am I missing something?

@@ -37,6 +38,7 @@ Synopsis of kprobe_events
based on SYM+offs or MEMADDR.
MOD : Module name which has given SYM.
SYM[+offs] : Symbol+offset where the probe is inserted.
SYM%return : Return address of the symbol
MEMADDR : Address where the probe is inserted.
MAXACTIVE : Maximum number of instances of the specified function that
can be probed simultaneously, or 0 for the default value

thanks.

Signature · 1 line
~Randy
author maintainer
#9 lore raw

Re: [PATCH 5/6] Documentation: tracing: Add %return suffix description

Quoted 29 lines · On Mon, 31 Aug 2020 11:50:20 -0700 Randy Dunlap <[email protected]> wrote:

Documentation/trace/kprobetrace.rst

Quoted 11 lines · Masami Hiramatsu

Add a description of the %return suffix option for kprobe tracer.

---
 Documentation/trace/kprobetrace.rst |    2 ++
 1 file changed, 2 insertions(+)
Documentation/trace/kprobetrace.rst +0 -0
index c1709165c553..d29d1f9e6721 100644

Check spacing:

Documentation/trace/kprobetrace.rst Synopsis of kprobe_events · line 44

Quoted 8 lines
Documentation/trace/kprobetrace.rst +1 -0
@@ -37,6 +38,7 @@ Synopsis of kprobe_events
 		  based on SYM+offs or MEMADDR.
  MOD		: Module name which has given SYM.
  SYM[+offs]	: Symbol+offset where the probe is inserted.
+ SYM%return     : Return address of the symbol
  MEMADDR	: Address where the probe is inserted.
  MAXACTIVE	: Maximum number of instances of the specified function that
 		  can be probed simultaneously, or 0 for the default value

If I remove the '+', the ':' lines up but the SYM does not line up. Am I missing something?

Documentation/trace/kprobetrace.rst Synopsis of kprobe_events · line 44

Quoted 7 lines
Documentation/trace/kprobetrace.rst +0 -0
@@ -37,6 +38,7 @@ Synopsis of kprobe_events
 		  based on SYM+offs or MEMADDR.
  MOD		: Module name which has given SYM.
  SYM[+offs]	: Symbol+offset where the probe is inserted.
 SYM%return     : Return address of the symbol
  MEMADDR	: Address where the probe is inserted.
  MAXACTIVE	: Maximum number of instances of the specified function that
 		  can be probed simultaneously, or 0 for the default value

Oops, that's my typo.

Thank you!

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#10 lore raw

Re: [PATCH 5/6] Documentation: tracing: Add %return suffix description

Quoted 30 lines · On Tue, 1 Sep 2020 07:27:45 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 27 lines · On Mon, 31 Aug 2020 11:50:20 -0700 Randy Dunlap <[email protected]> wrote:

Documentation/trace/kprobetrace.rst

Quoted 11 lines · Masami Hiramatsu

Add a description of the %return suffix option for kprobe tracer.

---
 Documentation/trace/kprobetrace.rst |    2 ++
 1 file changed, 2 insertions(+)
Documentation/trace/kprobetrace.rst +0 -0
index c1709165c553..d29d1f9e6721 100644

Check spacing:

Documentation/trace/kprobetrace.rst Synopsis of kprobe_events · line 44

Quoted 8 lines
Documentation/trace/kprobetrace.rst +1 -0
@@ -37,6 +38,7 @@ Synopsis of kprobe_events
 		  based on SYM+offs or MEMADDR.
  MOD		: Module name which has given SYM.
  SYM[+offs]	: Symbol+offset where the probe is inserted.
+ SYM%return     : Return address of the symbol
  MEMADDR	: Address where the probe is inserted.
  MAXACTIVE	: Maximum number of instances of the specified function that
 		  can be probed simultaneously, or 0 for the default value

If I remove the '+', the ':' lines up but the SYM does not line up. Am I missing something?

Quoted 10 lines

Documentation/trace/kprobetrace.rst Synopsis of kprobe_events · line 44

Quoted 9 lines
Documentation/trace/kprobetrace.rst +0 -0
@@ -37,6 +38,7 @@ Synopsis of kprobe_events
 		  based on SYM+offs or MEMADDR.
  MOD		: Module name which has given SYM.
  SYM[+offs]	: Symbol+offset where the probe is inserted.
 SYM%return     : Return address of the symbol
  MEMADDR	: Address where the probe is inserted.
  MAXACTIVE	: Maximum number of instances of the specified function that
 		  can be probed simultaneously, or 0 for the default value

Shouldn't we use the horizontal tab in .rst ? It seems other lines use a tab to indent, but this line doesn't (VIM converted the tab to spaces.)

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#3 lore raw

Re: [PATCH 1/6] kprobes: tracing/kprobes: Fix to kill kprobes on initmem after boot

In reply to kernel test robot
Quoted 20 lines · On Tue, 1 Sep 2020 00:25:58 +0800 kernel test robot <[email protected]> wrote:

Hi Masami,

I love your patch! Yet something to improve:

[auto build test ERROR on tip/perf/core]
[also build test ERROR on trace/for-next lwn/docs-next linus/master v5.9-rc3 next-20200828] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/tracing-boot-Add-new-options-for-tracing-specific-period/20200831-204738
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 2cb5383b30d47c446ec7d884cd80f93ffcc31817
config: arc-defconfig (attached as .config)
compiler: arc-elf-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arc

Quoted 8 lines

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

arc-elf-ld: init/main.o: in function `kernel_init':

Quoted 2 lines
Quoted 2 lines

main.c:(.ref.text+0xa6): undefined reference to `kprobe_free_init_mem'
arc-elf-ld: main.c:(.ref.text+0xa6): undefined reference to `kprobe_free_init_mem'

OK, I missed to put the kprobe_free_init_mem() in the code depends on CONFIG_DEBUG_FS. I'll move this out.

Thank you,

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