stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [for-linus][PATCH 1/3] tracing: Have tracing_snapshot_instance_cond() write errors to the appropriate instance
       [not found] <20230405135813.735507007@goodmis.org>
@ 2023-04-05 13:58 ` Steven Rostedt
  2023-04-05 13:58 ` [for-linus][PATCH 2/3] tracing: Fix ftrace_boot_snapshot command line logic Steven Rostedt
  2023-04-05 13:58 ` [for-linus][PATCH 3/3] tracing: Free error logs of tracing instances Steven Rostedt
  2 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2023-04-05 13:58 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Andrew Morton, stable,
	Ross Zwisler

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

If a trace instance has a failure with its snapshot code, the error
message is to be written to that instance's buffer. But currently, the
message is written to the top level buffer. Worse yet, it may also disable
the top level buffer and not the instance that had the issue.

Link: https://lkml.kernel.org/r/20230405022341.688730321@goodmis.org

Cc: stable@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ross Zwisler <zwisler@google.com>
Fixes: 2824f50332486 ("tracing: Make the snapshot trigger work with instances")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/trace.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 937e9676dfd4..ed1d1093f5e9 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -1149,22 +1149,22 @@ static void tracing_snapshot_instance_cond(struct trace_array *tr,
 	unsigned long flags;
 
 	if (in_nmi()) {
-		internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
-		internal_trace_puts("*** snapshot is being ignored        ***\n");
+		trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
+		trace_array_puts(tr, "*** snapshot is being ignored        ***\n");
 		return;
 	}
 
 	if (!tr->allocated_snapshot) {
-		internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
-		internal_trace_puts("*** stopping trace here!   ***\n");
-		tracing_off();
+		trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n");
+		trace_array_puts(tr, "*** stopping trace here!   ***\n");
+		tracer_tracing_off(tr);
 		return;
 	}
 
 	/* Note, snapshot can not be used when the tracer uses it */
 	if (tracer->use_max_tr) {
-		internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
-		internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
+		trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n");
+		trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
 		return;
 	}
 
-- 
2.39.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [for-linus][PATCH 2/3] tracing: Fix ftrace_boot_snapshot command line logic
       [not found] <20230405135813.735507007@goodmis.org>
  2023-04-05 13:58 ` [for-linus][PATCH 1/3] tracing: Have tracing_snapshot_instance_cond() write errors to the appropriate instance Steven Rostedt
@ 2023-04-05 13:58 ` Steven Rostedt
  2023-04-05 19:00   ` Steven Rostedt
  2023-04-05 13:58 ` [for-linus][PATCH 3/3] tracing: Free error logs of tracing instances Steven Rostedt
  2 siblings, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2023-04-05 13:58 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Andrew Morton, stable,
	Ross Zwisler

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

The kernel command line ftrace_boot_snapshot by itself is supposed to
trigger a snapshot at the end of boot up of the main top level trace
buffer. A ftrace_boot_snapshot=foo will do the same for an instance called
foo that was created by trace_instance=foo,...

The logic was broken where if ftrace_boot_snapshot was by itself, it would
trigger a snapshot for all instances that had tracing enabled, regardless
if it asked for a snapshot or not.

When a snapshot is requested for a buffer, the buffer's
tr->allocated_snapshot is set to true. Use that to know if a trace buffer
wants a snapshot at boot up or not.

Since the top level buffer is part of the ftrace_trace_arrays list,
there's no reason to treat it differently than the other buffers. Just
iterate the list if ftrace_boot_snapshot was specified.

Link: https://lkml.kernel.org/r/20230405022341.895334039@goodmis.org

Cc: stable@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ross Zwisler <zwisler@google.com>
Fixes: 9c1c251d670bc ("tracing: Allow boot instances to have snapshot buffers")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/trace.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index ed1d1093f5e9..4686473b8497 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -10393,19 +10393,20 @@ __init static int tracer_alloc_buffers(void)
 
 void __init ftrace_boot_snapshot(void)
 {
+#ifdef CONFIG_TRACER_MAX_TRACE
 	struct trace_array *tr;
 
-	if (snapshot_at_boot) {
-		tracing_snapshot();
-		internal_trace_puts("** Boot snapshot taken **\n");
-	}
+	if (!snapshot_at_boot)
+		return;
 
 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
-		if (tr == &global_trace)
+		if (!tr->allocated_snapshot)
 			continue;
-		trace_array_puts(tr, "** Boot snapshot taken **\n");
+
 		tracing_snapshot_instance(tr);
+		trace_array_puts(tr, "** Boot snapshot taken **\n");
 	}
+#endif
 }
 
 void __init early_trace_init(void)
-- 
2.39.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [for-linus][PATCH 3/3] tracing: Free error logs of tracing instances
       [not found] <20230405135813.735507007@goodmis.org>
  2023-04-05 13:58 ` [for-linus][PATCH 1/3] tracing: Have tracing_snapshot_instance_cond() write errors to the appropriate instance Steven Rostedt
  2023-04-05 13:58 ` [for-linus][PATCH 2/3] tracing: Fix ftrace_boot_snapshot command line logic Steven Rostedt
@ 2023-04-05 13:58 ` Steven Rostedt
  2 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2023-04-05 13:58 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Andrew Morton, stable,
	Thorsten Leemhuis, Ulf Hansson, Eric Biggers,
	Mirsad Goran Todorovac

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

When a tracing instance is removed, the error messages that hold errors
that occurred in the instance needs to be freed. The following reports a
memory leak:

 # cd /sys/kernel/tracing
 # mkdir instances/foo
 # echo 'hist:keys=x' > instances/foo/events/sched/sched_switch/trigger
 # cat instances/foo/error_log
 [  117.404795] hist:sched:sched_switch: error: Couldn't find field
   Command: hist:keys=x
                      ^
 # rmdir instances/foo

Then check for memory leaks:

 # echo scan > /sys/kernel/debug/kmemleak
 # cat /sys/kernel/debug/kmemleak
unreferenced object 0xffff88810d8ec700 (size 192):
  comm "bash", pid 869, jiffies 4294950577 (age 215.752s)
  hex dump (first 32 bytes):
    60 dd 68 61 81 88 ff ff 60 dd 68 61 81 88 ff ff  `.ha....`.ha....
    a0 30 8c 83 ff ff ff ff 26 00 0a 00 00 00 00 00  .0......&.......
  backtrace:
    [<00000000dae26536>] kmalloc_trace+0x2a/0xa0
    [<00000000b2938940>] tracing_log_err+0x277/0x2e0
    [<000000004a0e1b07>] parse_atom+0x966/0xb40
    [<0000000023b24337>] parse_expr+0x5f3/0xdb0
    [<00000000594ad074>] event_hist_trigger_parse+0x27f8/0x3560
    [<00000000293a9645>] trigger_process_regex+0x135/0x1a0
    [<000000005c22b4f2>] event_trigger_write+0x87/0xf0
    [<000000002cadc509>] vfs_write+0x162/0x670
    [<0000000059c3b9be>] ksys_write+0xca/0x170
    [<00000000f1cddc00>] do_syscall_64+0x3e/0xc0
    [<00000000868ac68c>] entry_SYSCALL_64_after_hwframe+0x72/0xdc
unreferenced object 0xffff888170c35a00 (size 32):
  comm "bash", pid 869, jiffies 4294950577 (age 215.752s)
  hex dump (first 32 bytes):
    0a 20 20 43 6f 6d 6d 61 6e 64 3a 20 68 69 73 74  .  Command: hist
    3a 6b 65 79 73 3d 78 0a 00 00 00 00 00 00 00 00  :keys=x.........
  backtrace:
    [<000000006a747de5>] __kmalloc+0x4d/0x160
    [<000000000039df5f>] tracing_log_err+0x29b/0x2e0
    [<000000004a0e1b07>] parse_atom+0x966/0xb40
    [<0000000023b24337>] parse_expr+0x5f3/0xdb0
    [<00000000594ad074>] event_hist_trigger_parse+0x27f8/0x3560
    [<00000000293a9645>] trigger_process_regex+0x135/0x1a0
    [<000000005c22b4f2>] event_trigger_write+0x87/0xf0
    [<000000002cadc509>] vfs_write+0x162/0x670
    [<0000000059c3b9be>] ksys_write+0xca/0x170
    [<00000000f1cddc00>] do_syscall_64+0x3e/0xc0
    [<00000000868ac68c>] entry_SYSCALL_64_after_hwframe+0x72/0xdc

The problem is that the error log needs to be freed when the instance is
removed.

Link: https://lore.kernel.org/lkml/76134d9f-a5ba-6a0d-37b3-28310b4a1e91@alu.unizg.hr/
Link: https://lore.kernel.org/linux-trace-kernel/20230404194504.5790b95f@gandalf.local.home

Cc: stable@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Thorsten Leemhuis <regressions@leemhuis.info>
Cc: Ulf Hansson <ulf.hansson@linaro.org>
Cc: Eric Biggers <ebiggers@kernel.org>
Fixes: 2f754e771b1a6 ("tracing: Have the error logs show up in the proper instances")
Reported-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
Tested-by: Mirsad Todorovac <mirsad.todorovac@alu.unizg.hr>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/trace.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 4686473b8497..36a6037823cd 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -9516,6 +9516,7 @@ static int __remove_instance(struct trace_array *tr)
 	tracefs_remove(tr->dir);
 	free_percpu(tr->last_func_repeats);
 	free_trace_buffers(tr);
+	clear_tracing_err_log(tr);
 
 	for (i = 0; i < tr->nr_topts; i++) {
 		kfree(tr->topts[i].topts);
-- 
2.39.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [for-linus][PATCH 2/3] tracing: Fix ftrace_boot_snapshot command line logic
  2023-04-05 13:58 ` [for-linus][PATCH 2/3] tracing: Fix ftrace_boot_snapshot command line logic Steven Rostedt
@ 2023-04-05 19:00   ` Steven Rostedt
  0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2023-04-05 19:00 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Andrew Morton, stable,
	Ross Zwisler

On Wed, 05 Apr 2023 09:58:15 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> 
> The kernel command line ftrace_boot_snapshot by itself is supposed to
> trigger a snapshot at the end of boot up of the main top level trace
> buffer. A ftrace_boot_snapshot=foo will do the same for an instance called
> foo that was created by trace_instance=foo,...
> 
> The logic was broken where if ftrace_boot_snapshot was by itself, it would
> trigger a snapshot for all instances that had tracing enabled, regardless
> if it asked for a snapshot or not.
> 
> When a snapshot is requested for a buffer, the buffer's
> tr->allocated_snapshot is set to true. Use that to know if a trace buffer
> wants a snapshot at boot up or not.
> 
> Since the top level buffer is part of the ftrace_trace_arrays list,
> there's no reason to treat it differently than the other buffers. Just
> iterate the list if ftrace_boot_snapshot was specified.
> 
> Link: https://lkml.kernel.org/r/20230405022341.895334039@goodmis.org
> 
> Cc: stable@vger.kernel.org
> Cc: Masami Hiramatsu <mhiramat@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Ross Zwisler <zwisler@google.com>
> Fixes: 9c1c251d670bc ("tracing: Allow boot instances to have snapshot buffers")

I guess I didn't need to Cc stable here, as the commit it fixed was added
in the 6.3 merge window.

 $ git describe --contains 9c1c251d670bc
 v6.3-rc1~126^2~8

Oh well.

-- Steve


> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> ---
>  kernel/trace/trace.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index ed1d1093f5e9..4686473b8497 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -10393,19 +10393,20 @@ __init static int tracer_alloc_buffers(void)
>  
>  void __init ftrace_boot_snapshot(void)
>  {
> +#ifdef CONFIG_TRACER_MAX_TRACE
>  	struct trace_array *tr;
>  
> -	if (snapshot_at_boot) {
> -		tracing_snapshot();
> -		internal_trace_puts("** Boot snapshot taken **\n");
> -	}
> +	if (!snapshot_at_boot)
> +		return;
>  
>  	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
> -		if (tr == &global_trace)
> +		if (!tr->allocated_snapshot)
>  			continue;
> -		trace_array_puts(tr, "** Boot snapshot taken **\n");
> +
>  		tracing_snapshot_instance(tr);
> +		trace_array_puts(tr, "** Boot snapshot taken **\n");
>  	}
> +#endif
>  }
>  
>  void __init early_trace_init(void)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-05 19:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20230405135813.735507007@goodmis.org>
2023-04-05 13:58 ` [for-linus][PATCH 1/3] tracing: Have tracing_snapshot_instance_cond() write errors to the appropriate instance Steven Rostedt
2023-04-05 13:58 ` [for-linus][PATCH 2/3] tracing: Fix ftrace_boot_snapshot command line logic Steven Rostedt
2023-04-05 19:00   ` Steven Rostedt
2023-04-05 13:58 ` [for-linus][PATCH 3/3] tracing: Free error logs of tracing instances Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).