Skip to contentSkip to conversation

tracing: Fixes and minor clean ups for 5.19

PATCH New Steven Rostedt 1 participant 7 messages Jul 12, 2022

Subsystems (3)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

cover tracing: Fixes and minor clean ups for 5.19

Douglas Anderson (1):
tracing: Fix sleeping while atomic in kdb ftdump

Li kunyu (1):

      blk-iocost: tracing: atomic64_read(&ioc->vtime_rate) is assigned an extra semicolon

Steven Rostedt (Google) (1):
ftrace: Be more specific about arch impact when function tracer is enabled

Tiezhu Yang (1):
samples: Use KSYM_NAME_LEN for kprobes

Zheng Yejian (1):
tracing/histograms: Fix memory leak problem

sunliming (1):
fprobe/samples: Make sample_probe static

----

 include/trace/events/iocost.h       |  2 +-
 kernel/trace/Kconfig                |  3 ++-
 kernel/trace/trace.c                | 11 ++++++-----
 kernel/trace/trace_events_hist.c    |  2 ++
 samples/fprobe/fprobe_example.c     |  2 +-
 samples/kprobes/kprobe_example.c    |  5 ++---
 samples/kprobes/kretprobe_example.c |  5 ++---
 7 files changed, 16 insertions(+), 14 deletions(-)
author maintainer
#1 lore raw

PATCH 1/6 tracing/histograms: Fix memory leak problem

From: Zheng Yejian <[email protected]>

This reverts commit 46bbe5c671e06f070428b9be142cc4ee5cedebac.

As commit 46bbe5c671e0 ("tracing: fix double free") said, the "double free" problem reported by clang static analyzer is:

Quoted 4 lines

In parse_var_defs() if there is a problem allocating var_defs.expr, the earlier var_defs.name is freed. This free is duplicated by free_var_defs() which frees the rest of the list.

However, if there is a problem allocating N-th var_defs.expr:
+ in parse_var_defs(), the freed 'earlier var_defs.name' is

    actually the N-th var_defs.name;

+ then in free_var_defs(), the names from 0th to (N-1)-th are freed;

                        IF ALLOCATING PROBLEM HAPPENED HERE!!! -+
                                                                 \
                                                                  |
          0th           1th                 (N-1)-th      N-th    V
          +-------------+-------------+-----+-------------+-----------
var_defs: | name | expr | name | expr | ... | name | expr | name | ///
          +-------------+-------------+-----+-------------+-----------

These two frees don't act on same name, so there was no "double free" problem before. Conversely, after that commit, we get a "memory leak" problem because the above "N-th var_defs.name" is not freed.

If enable CONFIG_DEBUG_KMEMLEAK and inject a fault at where the N-th var_defs.expr allocated, then execute on shell like:

  $ echo 'hist:key=call_site:val=$v1,$v2:v1=bytes_req,v2=bytes_alloc' > \
/sys/kernel/debug/tracing/events/kmem/kmalloc/trigger

Then kmemleak reports:
unreferenced object 0xffff8fb100ef3518 (size 8):

    comm "bash", pid 196, jiffies 4295681690 (age 28.538s)
    hex dump (first 8 bytes):
      76 31 00 00 b1 8f ff ff                          v1......
    backtrace:
      [<0000000038fe4895>] kstrdup+0x2d/0x60
      [<00000000c99c049a>] event_hist_trigger_parse+0x206f/0x20e0
      [<00000000ae70d2cc>] trigger_process_regex+0xc0/0x110
      [<0000000066737a4c>] event_trigger_write+0x75/0xd0
      [<000000007341e40c>] vfs_write+0xbb/0x2a0
      [<0000000087fde4c2>] ksys_write+0x59/0xd0
      [<00000000581e9cdf>] do_syscall_64+0x3a/0x80
      [<00000000cf3b065c>] entry_SYSCALL_64_after_hwframe+0x46/0xb0
---
 kernel/trace/trace_events_hist.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/trace_events_hist.c +2 -0
index 48e82e141d54..e87a46794079 100644
@@ -4430,6 +4430,8 @@ static int parse_var_defs(struct hist_trigger_data *hist_data)
 
 			s = kstrdup(field_str, GFP_KERNEL);
 			if (!s) {
+				kfree(hist_data->attrs->var_defs.name[n_vars]);
+				hist_data->attrs->var_defs.name[n_vars] = NULL;
 				ret = -ENOMEM;
 				goto free;
 			}
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH 2/6 tracing: Fix sleeping while atomic in kdb ftdump

In reply to Steven Rostedt

From: Douglas Anderson <[email protected]>

If you drop into kdb and type "ftdump" you'll get a sleeping while atomic warning from memory allocation in trace_find_next_entry().

This appears to have been caused by commit ff895103a84a ("tracing:
Save off entry when peeking at next entry"), which added the allocation in that path. The problematic commit was already fixed by commit 8e99cf91b99b ("tracing: Do not allocate buffer in trace_find_next_entry() in atomic") but that fix missed the kdb case.

The fix here is easy: just move the assignment of the static buffer to the place where it should have been to begin with:
trace_init_global_iter(). That function is called in two places, once is right before the assignment of the static buffer added by the previous fix and once is in kdb.

Note that it appears that there's a second static buffer that we need to assign that was added in commit efbbdaa22bb7 ("tracing: Show real address for trace event arguments"), so we'll move that too.

Fixes ff895103a84a ("tracing: Save off entry when peeking at next entry")
Fixes efbbdaa22bb7 ("tracing: Show real address for trace event arguments")
---
 kernel/trace/trace.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/trace/trace.c +6 -5
index a8cfac0611bc..b8dd54627075 100644
@@ -9864,6 +9864,12 @@ void trace_init_global_iter(struct trace_iterator *iter)
 	/* Output in nanoseconds only if we are using a clock in nanoseconds. */
 	if (trace_clocks[iter->tr->clock_id].in_ns)
 		iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
+
+	/* Can not use kmalloc for iter.temp and iter.fmt */
+	iter->temp = static_temp_buf;
+	iter->temp_size = STATIC_TEMP_BUF_SIZE;
+	iter->fmt = static_fmt_buf;
+	iter->fmt_size = STATIC_FMT_BUF_SIZE;
 }
 
 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
@@ -9896,11 +9902,6 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
 
 	/* Simulate the iterator */
 	trace_init_global_iter(&iter);
-	/* Can not use kmalloc for iter.temp and iter.fmt */
-	iter.temp = static_temp_buf;
-	iter.temp_size = STATIC_TEMP_BUF_SIZE;
-	iter.fmt = static_fmt_buf;
-	iter.fmt_size = STATIC_FMT_BUF_SIZE;
 
 	for_each_tracing_cpu(cpu) {
 		atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
Signature · 1 line
2.35.1
author maintainer
#3 lore raw

PATCH 3/6 ftrace: Be more specific about arch impact when function tracer is enabled

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

It was brought up that on ARMv7, that because the FUNCTION_TRACER does not use nops to keep function tracing disabled because of the use of a link register, it does have some performance impact.

The start of functions when -pg is used to compile the kernel is:

	push    {lr}
	bl      8010e7c0 <__gnu_mcount_nc>

When function tracing is tuned off, it becomes:

	push    {lr}
	add   sp, sp, #4

Which just puts the stack back to its normal location. But these two instructions at the start of every function does incur some overhead.

Be more honest in the Kconfig FUNCTION_TRACER description and specify that the overhead being in the noise was x86 specific, but other architectures may vary.

---
 kernel/trace/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/Kconfig +2 -1
index debbbb083286..ccd6a5ade3e9 100644
@@ -194,7 +194,8 @@ config FUNCTION_TRACER
 	  sequence is then dynamically patched into a tracer call when
 	  tracing is enabled by the administrator. If it's runtime disabled
 	  (the bootup default), then the overhead of the instructions is very
-	  small and not measurable even in micro-benchmarks.
+	  small and not measurable even in micro-benchmarks (at least on
+	  x86, but may have impact on other architectures).
 
 config FUNCTION_GRAPH_TRACER
 	bool "Kernel Function Graph Tracer"
Signature · 1 line
2.35.1
author maintainer
#4 lore raw

PATCH 4/6 blk-iocost: tracing: atomic64_read(&ioc->vtime_rate) is assigned an extra semicolon

In reply to Steven Rostedt

From: Li kunyu <[email protected]>

Remove extra semicolon.

---
 include/trace/events/iocost.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/trace/events/iocost.h +1 -1
index e282ce02fa2d..6d1626e7a4ce 100644
@@ -160,7 +160,7 @@ TRACE_EVENT(iocost_ioc_vrate_adj,
 
 	TP_fast_assign(
 		__assign_str(devname, ioc_name(ioc));
-		__entry->old_vrate = atomic64_read(&ioc->vtime_rate);;
+		__entry->old_vrate = atomic64_read(&ioc->vtime_rate);
 		__entry->new_vrate = new_vrate;
 		__entry->busy_level = ioc->busy_level;
 		__entry->read_missed_ppm = missed_ppm[READ];
Signature · 1 line
2.35.1
author maintainer
#5 lore raw

PATCH 5/6 fprobe/samples: Make sample_probe static

In reply to Steven Rostedt

From: sunliming <[email protected]>

This symbol is not used outside of fprobe_example.c, so marks it static.

Fixes the following warning:

sparse warnings: (new ones prefixed by >>)

Quoted 1 line
Quoted 1 line

samples/fprobe/fprobe_example.c:23:15: sparse: sparse: symbol 'sample_probe'

was not declared. Should it be static?

---
 samples/fprobe/fprobe_example.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/fprobe/fprobe_example.c +1 -1
index 01ee6c8c8382..58f6e8358e97 100644
@@ -20,7 +20,7 @@
 
 #define BACKTRACE_DEPTH 16
 #define MAX_SYMBOL_LEN 4096
-struct fprobe sample_probe;
+static struct fprobe sample_probe;
 static unsigned long nhit;
 
 static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
Signature · 1 line
2.35.1
author maintainer
#6 lore raw

PATCH 6/6 samples: Use KSYM_NAME_LEN for kprobes

In reply to Steven Rostedt

From: Tiezhu Yang <[email protected]>

It is better and enough to use KSYM_NAME_LEN for kprobes in samples, no need to define and use the other values.

---
 samples/kprobes/kprobe_example.c    | 5 ++---
 samples/kprobes/kretprobe_example.c | 5 ++---
 2 files changed, 4 insertions(+), 6 deletions(-)
samples/kprobes/kprobe_example.c +2 -3
index f991a66b5b02..fd346f58ddba 100644
@@ -16,9 +16,8 @@
 #include <linux/module.h>
 #include <linux/kprobes.h>
 
-#define MAX_SYMBOL_LEN	64
-static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
-module_param_string(symbol, symbol, sizeof(symbol), 0644);
+static char symbol[KSYM_NAME_LEN] = "kernel_clone";
+module_param_string(symbol, symbol, KSYM_NAME_LEN, 0644);
 
 /* For each probe you need to allocate a kprobe structure */
 static struct kprobe kp = {
samples/kprobes/kretprobe_example.c +2 -3
index 228321ecb161..cbf16542d84e 100644
@@ -23,11 +23,10 @@
 #include <linux/module.h>
 #include <linux/kprobes.h>
 #include <linux/ktime.h>
-#include <linux/limits.h>
 #include <linux/sched.h>
 
-static char func_name[NAME_MAX] = "kernel_clone";
-module_param_string(func, func_name, NAME_MAX, S_IRUGO);
+static char func_name[KSYM_NAME_LEN] = "kernel_clone";
+module_param_string(func, func_name, KSYM_NAME_LEN, 0644);
 MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
 			" function's execution time");
 
Signature · 1 line
2.35.1