[PATCH v2 1/3] tracing: Clear RECORDED_CMD and RECORDED_TGID flags on register failure
From: Masami Hiramatsu (Google)
Date: Sun Dec 06 2026 - 09:31:46 EST
In __ftrace_event_enable_disable(), when RECORD_CMD or RECORD_TGID is
enabled, tracing_start_cmdline_record() / tracing_start_tgid_record() is
called and EVENT_FILE_FL_RECORDED_CMD_BIT / EVENT_FILE_FL_RECORDED_TGID_BIT
is set before calling call->class->reg(call, TRACE_REG_REGISTER, file).
If reg() fails, tracing_stop_cmdline_record() and
tracing_stop_tgid_record() are called, but the corresponding flag bits
remain set on file->flags.
If record-cmd or record-tgid is later turned off and the event is
subsequently enabled and disabled, the stale flag bit triggers an extra
stop call and underflows the global cmdline/tgid recording refcount
(sched_cmdline_ref / sched_tgid_ref drops from 0 to -1), preventing
tracing_sched_register() from running on the next enable.
Reproducer (using trace-events-sample module to fail register_fprobe()):
cd /sys/kernel/tracing
echo 0 > options/record-cmd
echo 0 > options/record-tgid
modprobe trace-events-sample
echo 'f:fprobes/testfp sample_timer_cb' >> dynamic_events
rmmod trace-events-sample
echo 1 > options/record-tgid
echo 1 > events/fprobes/testfp/enable # fails with -ENOENT
echo 0 > options/record-tgid
modprobe trace-events-sample
echo 1 > events/fprobes/testfp/enable
echo 0 > events/fprobes/testfp/enable # underflows sched_tgid_ref
echo 1 > options/record-tgid
echo 1 > events/fprobes/testfp/enable
sleep 1
cat saved_tgids
Before:
`cat saved_tgids` prints nothing (empty), because sched_tgid_ref only
increments from -1 to 0 and tracing_sched_register() is never called.
Expected:
`cat saved_tgids` prints recorded "<pid> <tgid>" entries (non-empty).
Clear EVENT_FILE_FL_RECORDED_CMD_BIT and EVENT_FILE_FL_RECORDED_TGID_BIT
in the registration error path alongside the stop calls.
Fixes: e870e9a1240b ("tracing: Allow to disable cmdline recording")
Fixes: d914ba37d714 ("tracing: Add support for recording tgid of tasks")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
kernel/trace/trace_events.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 60720d9236a1..f33fd00d2604 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -912,10 +912,16 @@ static int __ftrace_event_enable_disable(struct trace_event_file *file,
ret = call->class->reg(call, TRACE_REG_REGISTER, file);
if (ret) {
- if (cmd)
+ if (cmd) {
+ clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT,
+ &file->flags);
tracing_stop_cmdline_record();
- if (tgid)
+ }
+ if (tgid) {
+ clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
+ &file->flags);
tracing_stop_tgid_record();
+ }
pr_info("event trace: Could not enable event "
"%s\n", trace_event_name(call));
break;
--
2.43.0