* [PATCH 1/2] tracing: Keep an instance alive while ftrace_dump() dumps it
@ 2026-12-04 18:49 Praneeth Balasubramanian
2026-12-04 18:49 ` [PATCH 2/2] tracing: Hold trace_event_read_lock() in ftrace_dump() when possible Praneeth Balasubramanian
0 siblings, 1 reply; 2+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:49 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-trace-kernel, linux-kernel,
Praneeth Balasubramanian, stable
ftrace_dump_by_param() looks up the instances named in
ftrace_dump_on_oops with trace_array_find(), whose comment says that
trace_types_lock must be held, and then dumps them with
ftrace_dump_one(). ftrace_dump() runs from the oops, panic and SysRq
paths and cannot take that mutex, so it holds neither the lock nor a
reference. If the instance is removed meanwhile (instance_rmdir() ->
__remove_instance()), the dump can follow a list entry that list_del()
has poisoned, or use a trace_array and buffers that have been freed.
ftrace_dump_one() does not sleep, so the dump can stay in an RCU
read-side critical section. Look the instance up with a new
trace_array_find_rcu() under rcu_read_lock() and stay in the critical
section until the instance has been dumped. On the update side, use
list_add_rcu() and list_del_rcu() for ftrace_trace_arrays, and always
wait for a grace period in __remove_instance() before the instance is
torn down. It already waited when the trace marker copy was enabled.
The unlocked lookup was found by a static check that turns the locking
rule in a function's comment into a Clang context-analysis requirement
on its callers.
Fixes: 19f0423fd55c ("tracing: Support to dump instance traces by ftrace_dump_on_oops")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---
Notes:
This was found by a compiler-based check, part of research at UCSB SecLab.
The locking rule stated in the kernel-doc comment of the called function
was turned into a Clang context-analysis requirement (__must_hold) on that
function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
caller. An LLM was used only to turn the comment into a machine-readable
rule; the report itself comes from the compiler. The patch and changelog
were drafted with Claude Opus 5.5. I reviewed the patch and take
responsibility for it.
Testing: builds without new warnings with W=1 (x86_64, clang 23,
allmodconfig). Not runtime-tested, and I have not observed the race
myself.
kernel/trace/trace.c | 40 +++++++++++++++++++++++++++++++++++-----
1 file changed, 35 insertions(+), 5 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 4ccdefb5c3ef..faa17589df70 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -8765,7 +8765,7 @@ trace_array_create_systems(const char *name, const char *systems,
} else
__trace_early_add_events(tr);
- list_add(&tr->list, &ftrace_trace_arrays);
+ list_add_rcu(&tr->list, &ftrace_trace_arrays);
tr->ref++;
@@ -8890,14 +8890,20 @@ static int __remove_instance(struct trace_array *tr)
if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
return -EBUSY;
- list_del(&tr->list);
+ list_del_rcu(&tr->list);
if (printk_trace == tr)
update_printk_trace(&global_trace);
/* Must be done before disabling all the flags */
- if (update_marker_trace(tr, 0))
- synchronize_rcu();
+ update_marker_trace(tr, 0);
+
+ /*
+ * Wait for RCU readers that may still use tr: the trace marker
+ * copies, and ftrace_dump(), which finds instances without taking
+ * trace_types_lock.
+ */
+ synchronize_rcu();
/* Disable all the flags that were enabled coming in */
for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
@@ -9570,6 +9576,23 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
local_irq_restore(flags);
}
+/*
+ * Like trace_array_find(), for ftrace_dump(), which cannot take
+ * trace_types_lock. Must be called under rcu_read_lock(), and the
+ * returned trace array may only be used until rcu_read_unlock().
+ */
+static struct trace_array *trace_array_find_rcu(const char *instance)
+{
+ struct trace_array *tr;
+
+ list_for_each_entry_rcu(tr, &ftrace_trace_arrays, list) {
+ if (tr->name && strcmp(tr->name, instance) == 0)
+ return tr;
+ }
+
+ return NULL;
+}
+
static void ftrace_dump_by_param(void)
{
bool first_param = true;
@@ -9597,8 +9620,14 @@ static void ftrace_dump_by_param(void)
}
inst_name = strsep(&token, "=");
- tr = trace_array_find(inst_name);
+ /*
+ * trace_types_lock cannot be taken here; RCU keeps the
+ * instance from being freed until the dump is done.
+ */
+ rcu_read_lock();
+ tr = trace_array_find_rcu(inst_name);
if (!tr) {
+ rcu_read_unlock();
printk(KERN_TRACE "Instance %s not found\n", inst_name);
continue;
}
@@ -9608,6 +9637,7 @@ static void ftrace_dump_by_param(void)
ftrace_dump_one(tr, DUMP_ORIG);
else
ftrace_dump_one(tr, DUMP_ALL);
+ rcu_read_unlock();
}
}
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* [PATCH 2/2] tracing: Hold trace_event_read_lock() in ftrace_dump() when possible
2026-12-04 18:49 [PATCH 1/2] tracing: Keep an instance alive while ftrace_dump() dumps it Praneeth Balasubramanian
@ 2026-12-04 18:49 ` Praneeth Balasubramanian
0 siblings, 0 replies; 2+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:49 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-trace-kernel, linux-kernel,
Praneeth Balasubramanian, stable
ftrace_dump_one() formats each entry with print_trace_line(), which
looks up the entry's event with ftrace_find_event(). The comment above
ftrace_find_event() says that it must be called with
trace_event_read_lock() held, and the trace and trace_pipe readers take
it, but the dump does not. If a dynamic event is removed during a dump,
for example by writing "-:group/event" to kprobe_events, the event is
unregistered under trace_event_sem and then freed while the dump may
still be using it.
ftrace_dump() runs from the oops, panic and SysRq paths and must not
sleep; SysRq handlers even run under rcu_read_lock(). So add
trace_event_read_trylock(), take the lock with it in task context only,
and dump without it, as before, when it is not available, for example
in NMI context or when the crashing task holds trace_event_sem.
The missing lock was found by a static check that turns the locking rule
in a function's comment into a Clang context-analysis requirement on its
callers.
Fixes: 4f5359685af6 ("tracing: add trace_event_read_lock()")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---
Notes:
This was found by a compiler-based check, part of research at UCSB SecLab.
The locking rule stated in the kernel-doc comment of the called function
was turned into a Clang context-analysis requirement (__must_hold) on that
function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
caller. An LLM was used only to turn the comment into a machine-readable
rule; the report itself comes from the compiler. The patch and changelog
were drafted with Claude Opus 5.5. I reviewed the patch and take
responsibility for it.
Testing: builds without new warnings with W=1 (x86_64, clang 23,
allmodconfig). Not runtime-tested, and I have not observed the race
myself.
kernel/trace/trace.c | 13 +++++++++++++
kernel/trace/trace_output.c | 6 ++++++
kernel/trace/trace_output.h | 1 +
3 files changed, 20 insertions(+)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index faa17589df70..754bbe55666f 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -9495,6 +9495,7 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
static struct trace_iterator iter;
unsigned int old_userobj;
unsigned long flags;
+ bool event_locked;
int cnt = 0;
/*
@@ -9507,6 +9508,15 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
*/
tracer_tracing_off(tr);
+ /*
+ * print_trace_line() looks up the event of each entry, which needs
+ * trace_event_read_lock() so that the event is not unregistered and
+ * freed meanwhile. A dump must not sleep and can run in any context,
+ * so only try the lock, and only in task context. If it is not
+ * available, dump without it as before.
+ */
+ event_locked = in_task() && trace_event_read_trylock();
+
local_irq_save(flags);
/* Simulate the iterator */
@@ -9574,6 +9584,9 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
tracer_tracing_enable(tr);
local_irq_restore(flags);
+
+ if (event_locked)
+ trace_event_read_unlock();
}
/*
diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
index a5ad76175d10..c9118f6d7be0 100644
--- a/kernel/trace/trace_output.c
+++ b/kernel/trace/trace_output.c
@@ -864,6 +864,12 @@ void trace_event_read_unlock(void)
up_read(&trace_event_sem);
}
+/* Returns true if the lock was taken; release it with trace_event_read_unlock() */
+bool trace_event_read_trylock(void)
+{
+ return down_read_trylock(&trace_event_sem);
+}
+
/**
* register_trace_event - register output for an event type
* @event: the event type to register
diff --git a/kernel/trace/trace_output.h b/kernel/trace/trace_output.h
index 99b676733d46..620c6d8f5015 100644
--- a/kernel/trace/trace_output.h
+++ b/kernel/trace/trace_output.h
@@ -35,6 +35,7 @@ extern enum print_line_t print_event_fields(struct trace_iterator *iter,
extern void trace_event_read_lock(void);
extern void trace_event_read_unlock(void);
+extern bool trace_event_read_trylock(void);
extern struct trace_event *ftrace_find_event(int type);
extern enum print_line_t trace_nop_print(struct trace_iterator *iter,
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 18:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:49 [PATCH 1/2] tracing: Keep an instance alive while ftrace_dump() dumps it Praneeth Balasubramanian
2026-12-04 18:49 ` [PATCH 2/2] tracing: Hold trace_event_read_lock() in ftrace_dump() when possible Praneeth Balasubramanian
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox