[PATCH 1/2] tracing: Keep an instance alive while ftrace_dump() dumps it

From: Praneeth Balasubramanian

Date: Fri Dec 04 2026 - 13:50:09 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Praneeth Balasubramanian <praneethb2002@xxxxxxxxx>
---

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