Skip to contentSkip to conversation

tracing: Fix snapshot accounting

PATCH New Steven Rostedt 1 participant 3 messages Feb 23, 2024 Tracing

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

cover tracing: Fix snapshot accounting

The ring buffer mapping test failed after running the ftrace tests. This was due to some mismatched snapshot accounting that left the snapshot counter enabled when it was not, which prevents the ring buffer from being mapped.

Steven Rostedt (Google) (2):
tracing: Fix snapshot counter going between two tracers that use it
tracing: Decrement the snapshot if the snapshot trigger fails to register

----

 kernel/trace/trace.c                | 2 +-
 kernel/trace/trace_events_trigger.c | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)
author maintainer
#1 lore raw

PATCH 1/2 tracing: Fix snapshot counter going between two tracers that use it

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

Running the ftrace selftests caused the ring buffer mapping test to fail. Investigating, I found that the snapshot counter would be incremented every time a tracer that uses the snapshot is enabled even if the snapshot was used by the previous tracer.

That is:

 # cd /sys/kernel/tracing
 # echo wakeup_rt > current_tracer
 # echo wakeup_dl > current_tracer
 # echo nop > current_tracer

would leave the snapshot counter at 1 and not zero. That's because the enabling of wakeup_dl would increment the counter again but the setting the tracer to nop would only decrement it once.

Do not arm the snapshot for a tracer if the previous tracer already had it armed.

Fixes 16f7e48ffc53a ("tracing: Add snapshot refcount")
---
 kernel/trace/trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace.c +1 -1
index b7a870c8ae2a..480201c3b36e 100644
@@ -6164,7 +6164,7 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf)
 		tracing_disarm_snapshot(tr);
 	}
 
-	if (t->use_max_tr) {
+	if (!had_max_tr && t->use_max_tr) {
 		ret = tracing_arm_snapshot_locked(tr);
 		if (ret)
 			goto out;
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/2 tracing: Decrement the snapshot if the snapshot trigger fails to register

In reply to Steven Rostedt

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

Running the ftrace selftests caused the ring buffer mapping test to fail. Investigating, I found that the snapshot counter would be incremented every time a snapshot trigger was added, even if that snapshot trigger failed.

 # cd /sys/kernel/tracing
 # echo "snapshot" > events/sched/sched_process_fork/trigger
 # echo "snapshot" > events/sched/sched_process_fork/trigger
 -bash: echo: write error: File exists

That second one that fails increments the snapshot counter but doesn't decrement it. It needs to be decremented when the snapshot fails.

Fixes 16f7e48ffc53a ("tracing: Add snapshot refcount")
---
 kernel/trace/trace_events_trigger.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/trace/trace_events_trigger.c +4 -1
index 62e4f58b8671..4bec043c8690 100644
@@ -1491,7 +1491,10 @@ register_snapshot_trigger(char *glob,
 	if (ret < 0)
 		return ret;
 
-	return register_trigger(glob, data, file);
+	ret = register_trigger(glob, data, file);
+	if (ret < 0)
+		tracing_disarm_snapshot(file->tr);
+	return ret;
 }
 
 static void unregister_snapshot_trigger(char *glob,
Signature · 1 line
2.43.0