Skip to contentSkip to conversation

tracing: Fixes for 5.17-rc1

PATCH New Steven Rostedt 1 participant 11 messages Jan 28, 2022

Subsystems (6)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

cover tracing: Fixes for 5.17-rc1

Tracing fixes for 5.17-rc1:

- Limit mcount build time sorting to only those archs that we know it works for.

- Fix memory leak in error path of historgam setup

- Fix and clean up rel_loc array out of bounds issue

- tools/rtla documentation fixes

- Fix issues with histogram logic

git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git
ftrace/urgent

Head SHA1: 67ab5eb71b37b55f7c5522d080a1b42823351776

Kees Cook (1):
tracing/perf: Avoid -Warray-bounds warning for __rel_loc macro

Masami Hiramatsu (1):
tracing: Avoid -Warray-bounds warning for __rel_loc macro

Shuah Khan (2):
rtla: Make doc build optional tools/tracing: Update Makefile to build rtla

Steven Rostedt (Google) (1):
ftrace: Have architectures opt-in for mcount build time sorting

Tom Zanussi (4):
tracing: Fix smatch warning for null glob in event_hist_trigger_parse()
tracing: Fix smatch warning for do while check in event_hist_trigger_parse()
tracing: Propagate is_signed to expression
tracing: Don't inc err_log entry count if entry allocation fails

Xiaoke Wang (1):
tracing/histogram: Fix a potential memory leak for kstrdup()

----

 arch/arm/Kconfig                 |  1 +
 arch/x86/Kconfig                 |  1 +
 include/trace/perf.h             |  5 +++--
 include/trace/trace_events.h     |  9 +++++----
 kernel/trace/Kconfig             |  8 +++++++-
 kernel/trace/trace.c             |  3 ++-
 kernel/trace/trace_events_hist.c | 10 ++++++++--
 scripts/Makefile                 |  2 +-
 tools/tracing/Makefile           | 18 ++++++++++++++----
 tools/tracing/rtla/Makefile      |  2 +-
 10 files changed, 43 insertions(+), 16 deletions(-)
author maintainer
#1 lore raw

PATCH 1/10 ftrace: Have architectures opt-in for mcount build time sorting

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

First S390 complained that the sorting of the mcount sections at build time caused the kernel to crash on their architecture. Now PowerPC is complaining about it too. And also ARM64 appears to be having issues.

It may be necessary to also update the relocation table for the values in the mcount table. Not only do we have to sort the table, but also update the relocations that may be applied to the items in the table.

If the system is not relocatable, then it is fine to sort, but if it is, some architectures may have issues (although x86 does not as it shifts all addresses the same).

Add a HAVE_BUILDTIME_MCOUNT_SORT that an architecture can set to say it is safe to do the sorting at build time.

Also update the config to compile in build time sorting in the sorttable code in scripts/ to depend on CONFIG_BUILDTIME_MCOUNT_SORT.

Cc Ingo Molnar <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Russell King <[email protected]>
Cc Yinan Liu <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Kees Cook <[email protected]>
Fixes 72b3942a173c ("scripts: ftrace - move the sort-processing in ftrace_init")
---
 arch/arm/Kconfig     | 1 +
 arch/x86/Kconfig     | 1 +
 kernel/trace/Kconfig | 8 +++++++-
 scripts/Makefile     | 2 +-
 4 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/Kconfig +1 -0
index fabe39169b12..4c97cb40eebb 100644
@@ -83,6 +83,7 @@ config ARM
 	select HAVE_EBPF_JIT if !CPU_ENDIAN_BE32
 	select HAVE_CONTEXT_TRACKING
 	select HAVE_C_RECORDMCOUNT
+	select HAVE_BUILDTIME_MCOUNT_SORT
 	select HAVE_DEBUG_KMEMLEAK if !XIP_KERNEL
 	select HAVE_DMA_CONTIGUOUS if MMU
 	select HAVE_DYNAMIC_FTRACE if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU
arch/x86/Kconfig +1 -0
index ebe8fc76949a..9f5bd41bf660 100644
@@ -186,6 +186,7 @@ config X86
 	select HAVE_CONTEXT_TRACKING_OFFSTACK	if HAVE_CONTEXT_TRACKING
 	select HAVE_C_RECORDMCOUNT
 	select HAVE_OBJTOOL_MCOUNT		if STACK_VALIDATION
+	select HAVE_BUILDTIME_MCOUNT_SORT
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS
 	select HAVE_DYNAMIC_FTRACE
kernel/trace/Kconfig +7 -1
index 752ed89a293b..a5eb5e7fd624 100644
@@ -70,10 +70,16 @@ config HAVE_C_RECORDMCOUNT
 	help
 	  C version of recordmcount available?
 
+config HAVE_BUILDTIME_MCOUNT_SORT
+       bool
+       help
+         An architecture selects this if it sorts the mcount_loc section
+	 at build time.
+
 config BUILDTIME_MCOUNT_SORT
        bool
        default y
-       depends on BUILDTIME_TABLE_SORT && !S390
+       depends on HAVE_BUILDTIME_MCOUNT_SORT && DYNAMIC_FTRACE
        help
          Sort the mcount_loc section at build time.
 
scripts/Makefile +1 -1
index ecd3acacd0ec..ce5aa9030b74 100644
@@ -25,7 +25,7 @@ HOSTCFLAGS_sorttable.o += -I$(srctree)/tools/arch/x86/include
 HOSTCFLAGS_sorttable.o += -DUNWINDER_ORC_ENABLED
 endif
 
-ifdef CONFIG_DYNAMIC_FTRACE
+ifdef CONFIG_BUILDTIME_MCOUNT_SORT
 HOSTCFLAGS_sorttable.o += -DMCOUNT_SORT_ENABLED
 endif
 
Signature · 1 line
2.33.0
author maintainer
#2 lore raw

PATCH 2/10 tracing/histogram: Fix a potential memory leak for kstrdup()

In reply to Steven Rostedt

From: Xiaoke Wang <[email protected]>

kfree() is missing on an error path to free the memory allocated by kstrdup():

  p = param = kstrdup(data->params[i], GFP_KERNEL);

So it is better to free it via kfree(p).

Fixes d380dcde9a07c ("tracing: Fix now invalid var_ref_vals assumption in trace action")
---
 kernel/trace/trace_events_hist.c | 1 +
 1 file changed, 1 insertion(+)
kernel/trace/trace_events_hist.c +1 -0
index 5e6a988a8a51..cd9610688ddc 100644
@@ -3935,6 +3935,7 @@ static int trace_action_create(struct hist_trigger_data *hist_data,
 
 			var_ref_idx = find_var_ref_idx(hist_data, var_ref);
 			if (WARN_ON(var_ref_idx < 0)) {
+				kfree(p);
 				ret = var_ref_idx;
 				goto err;
 			}
Signature · 1 line
2.33.0
author maintainer
#3 lore raw

PATCH 3/10 tracing: Avoid -Warray-bounds warning for __rel_loc macro

In reply to Steven Rostedt

From: Masami Hiramatsu <[email protected]>

Since -Warray-bounds checks the destination size from the type of given pointer, __assign_rel_str() macro gets warned because it passes the pointer to the 'u32' field instead of 'trace_event_raw_*' data structure. Pass the data address calculated from the 'trace_event_raw_*' instead of 'u32' __rel_loc field.

Cc Stephen Rothwell <[email protected]>
Cc Kees Cook <[email protected]>
[ This did not fix the warning, but is still a nice clean up ]
---
 include/trace/trace_events.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/trace/trace_events.h +4 -3
index 8c6f7c433518..65d927e059d3 100644
@@ -318,9 +318,10 @@ TRACE_MAKE_SYSTEM_STR();
 #define __get_str(field) ((char *)__get_dynamic_array(field))
 
 #undef __get_rel_dynamic_array
-#define __get_rel_dynamic_array(field)	\
-		((void *)(&__entry->__rel_loc_##field) +	\
-		 sizeof(__entry->__rel_loc_##field) +		\
+#define __get_rel_dynamic_array(field)					\
+		((void *)__entry + 					\
+		 offsetof(typeof(*__entry), __rel_loc_##field) +	\
+		 sizeof(__entry->__rel_loc_##field) +			\
 		 (__entry->__rel_loc_##field & 0xffff))
 
 #undef __get_rel_dynamic_array_len
Signature · 1 line
2.33.0
author maintainer
#4 lore raw

PATCH 4/10 tracing/perf: Avoid -Warray-bounds warning for __rel_loc macro

In reply to Steven Rostedt

From: Kees Cook <[email protected]>

As done for trace_events.h, also fix the __rel_loc macro in perf.h, which silences the -Warray-bounds warning:

In file included from ./include/linux/string.h:253,

                 from ./include/linux/bitmap.h:11,
                 from ./include/linux/cpumask.h:12,
                 from ./include/linux/mm_types_task.h:14,
                 from ./include/linux/mm_types.h:5,
                 from ./include/linux/buildid.h:5,
                 from ./include/linux/module.h:14,
                 from samples/trace_events/trace-events-sample.c:2:

In function '__fortify_strcpy',
inlined from 'perf_trace_foo_rel_loc' at samples/trace_events/./trace-events-sample.h:519:1:
./include/linux/fortify-string.h:47:33: warning: '__builtin_strcpy' offset 12 is out of the bounds [ 0, 4] [-Warray-bounds]

   47 | #define __underlying_strcpy     __builtin_strcpy
      |                                 ^
./include/linux/fortify-string.h:445:24: note: in expansion of macro '__underlying_strcpy'
  445 |                 return __underlying_strcpy(p, q);
      |                        ^~~~~~~~~~~~~~~~~~~

Also make __data struct member a proper flexible array to avoid future problems.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Fixes 55de2c0b5610c ("tracing: Add '__rel_loc' using trace event macros")
---
 include/trace/perf.h         | 5 +++--
 include/trace/trace_events.h | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)
include/trace/perf.h +3 -2
index ea4405de175a..5d48c46a3008 100644
@@ -23,8 +23,9 @@
 
 #undef __get_rel_dynamic_array
 #define __get_rel_dynamic_array(field)	\
-		((void *)(&__entry->__rel_loc_##field) +	\
-		 sizeof(__entry->__rel_loc_##field) +		\
+		((void *)__entry +					\
+		 offsetof(typeof(*__entry), __rel_loc_##field) +	\
+		 sizeof(__entry->__rel_loc_##field) +			\
 		 (__entry->__rel_loc_##field & 0xffff))
 
 #undef __get_rel_dynamic_array_len
include/trace/trace_events.h +1 -1
index 65d927e059d3..3d29919045af 100644
@@ -128,7 +128,7 @@ TRACE_MAKE_SYSTEM_STR();
 	struct trace_event_raw_##name {					\
 		struct trace_entry	ent;				\
 		tstruct							\
-		char			__data[0];			\
+		char			__data[];			\
 	};								\
 									\
 	static struct trace_event_class event_class_##name;
Signature · 1 line
2.33.0
author maintainer
#5 lore raw

PATCH 5/10 rtla: Make doc build optional

In reply to Steven Rostedt

From: Shuah Khan <[email protected]>

rtla build fails due to doc build dependency on rst2man. Make doc build optional so rtla could be built without docs. Leave the install dependency on doc_install alone.

---
 tools/tracing/rtla/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/tracing/rtla/Makefile +1 -1
index 2d52ff0bff7d..7c39728d08de 100644
@@ -59,7 +59,7 @@ endif
 .PHONY:	all
 all:	rtla
 
-rtla: $(OBJ) doc
+rtla: $(OBJ)
 	$(CC) -o rtla $(LDFLAGS) $(OBJ) $(LIBS)
 
 static: $(OBJ)
Signature · 1 line
2.33.0
author maintainer
#6 lore raw

PATCH 6/10 tools/tracing: Update Makefile to build rtla

In reply to Steven Rostedt

From: Shuah Khan <[email protected]>

Update tracing Makefile to build/install/clean rtla tragets.

---
 tools/tracing/Makefile | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
tools/tracing/Makefile +14 -4
index 87e0ec48e2e7..95e485f12d97 100644
@@ -1,11 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0
 include ../scripts/Makefile.include
 
-all: latency
+all: latency rtla
 
-clean: latency_clean
+clean: latency_clean rtla_clean
 
-install: latency_install
+install: latency_install rtla_install
 
 latency:
 	$(call descend,latency)
@@ -16,4 +16,14 @@ latency_install:
 latency_clean:
 	$(call descend,latency,clean)
 
-.PHONY: all install clean latency latency_install latency_clean
+rtla:
+	$(call descend,rtla)
+
+rtla_install:
+	$(call descend,rtla,install)
+
+rtla_clean:
+	$(call descend,rtla,clean)
+
+.PHONY: all install clean latency latency_install latency_clean \
+	rtla rtla_install rtla_clean
Signature · 1 line
2.33.0
author maintainer
#7 lore raw

PATCH 7/10 tracing: Fix smatch warning for null glob in event_hist_trigger_parse()

In reply to Steven Rostedt

From: Tom Zanussi <[email protected]>

The recent rename of event_hist_trigger_parse() caused smatch re-evaluation of trace_events_hist.c and as a result an old warning was found:

kernel/trace/trace_events_hist.c:6174 event_hist_trigger_parse()
error: we previously assumed 'glob' could be null (see line 6166)

glob should never be null (and apparently smatch can also figure that out and skip the warning when using the cross-function DB (but which can't be used with a 0day build as it takes too much time to generate)).

Nonetheless for clarity, remove the test but add a WARN_ON() in case the code ever changes.

Fixes f404da6e1d46c ("tracing: Add 'last error' error facility for hist triggers")
---
 kernel/trace/trace_events_hist.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +3 -1
index cd9610688ddc..e0860146dd39 100644
@@ -6164,7 +6164,9 @@ static int event_hist_trigger_parse(struct event_command *cmd_ops,
 
 	lockdep_assert_held(&event_mutex);
 
-	if (glob && strlen(glob)) {
+	WARN_ON(!glob);
+
+	if (strlen(glob)) {
 		hist_err_clear();
 		last_cmd_set(file, param);
 	}
Signature · 1 line
2.33.0
author maintainer
#8 lore raw

PATCH 8/10 tracing: Fix smatch warning for do while check in event_hist_trigger_parse()

In reply to Steven Rostedt

From: Tom Zanussi <[email protected]>

The patch ec5ce0987541: "tracing: Allow whitespace to surround hist trigger filter" from Jan 15, 2018, leads to the following Smatch static checker warning:

kernel/trace/trace_events_hist.c:6199 event_hist_trigger_parse()
warn: 'p' can't be NULL.

Since p is always checked for a NULL value at the top of loop and nothing in the rest of the loop will set it to NULL, the warning is correct and might as well be 1 to silence the warning.

Fixes ec5ce09875410 ("tracing: Allow whitespace to surround hist trigger filter")
---
 kernel/trace/trace_events_hist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +1 -1
index e0860146dd39..b894d68082ea 100644
@@ -6199,7 +6199,7 @@ static int event_hist_trigger_parse(struct event_command *cmd_ops,
 			continue;
 		}
 		break;
-	} while (p);
+	} while (1);
 
 	if (!p)
 		param = NULL;
Signature · 1 line
2.33.0
author maintainer
#9 lore raw

PATCH 9/10 tracing: Propagate is_signed to expression

In reply to Steven Rostedt

From: Tom Zanussi <[email protected]>

During expression parsing, a new expression field is created which should inherit the properties of the operands, such as size and is_signed.

is_signed propagation was missing, causing spurious errors with signed operands. Add it in parse_expr() and parse_unary() to fix the problem.

Fixes 100719dcef447 ("tracing: Add simple expression support to hist triggers")
---
 kernel/trace/trace_events_hist.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_events_hist.c +3 -0
index b894d68082ea..ada87bfb5bb8 100644
@@ -2503,6 +2503,8 @@ static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
 	expr->fn = hist_field_unary_minus;
 	expr->operands[0] = operand1;
+	expr->size = operand1->size;
+	expr->is_signed = operand1->is_signed;
 	expr->operator = FIELD_OP_UNARY_MINUS;
 	expr->name = expr_str(expr, 0);
 	expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
@@ -2719,6 +2721,7 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
 
 		/* The operand sizes should be the same, so just pick one */
 		expr->size = operand1->size;
+		expr->is_signed = operand1->is_signed;
 
 		expr->operator = field_op;
 		expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
Signature · 1 line
2.33.0
author maintainer
#10 lore raw

PATCH 10/10 tracing: Dont inc err_log entry count if entry allocation fails

In reply to Steven Rostedt

From: Tom Zanussi <[email protected]>

tr->n_err_log_entries should only be increased if entry allocation succeeds.

Doing it when it fails won't cause any problems other than wasting an entry, but should be fixed anyway.

Fixes 2f754e771b1a6 ("tracing: Don't inc err_log entry count if entry allocation fails")
---
 kernel/trace/trace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace.c +2 -1
index a569a0cb81ee..c860f582b078 100644
@@ -7740,7 +7740,8 @@ static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
 		err = kzalloc(sizeof(*err), GFP_KERNEL);
 		if (!err)
 			err = ERR_PTR(-ENOMEM);
-		tr->n_err_log_entries++;
+		else
+			tr->n_err_log_entries++;
 
 		return err;
 	}
Signature · 1 line
2.33.0