[PATCH v3 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove

From: Masami Hiramatsu (Google)

Date: Fri Dec 04 2026 - 01:08:05 EST


From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>

__remove_instance() is called with event_mutex and trace_types_lock held
and calls trace_array_cancel_autoremove(), which invokes
cancel_work_sync(&tr->autoremove_work) when work_pending() is true.
However, trace_array_autoremove() calls trace_array_destroy(), which
acquires event_mutex and trace_types_lock.

When a manual rmdir (or trace_array_destroy()) races with a queued
autoremove_work, this creates two bugs:

1. ABBA deadlock: if the worker dequeues autoremove_work between
work_pending() and cancel_work_sync() and blocks on event_mutex,
instance_rmdir() blocks forever inside cancel_work_sync() while
holding event_mutex and trace_types_lock. Lockdep also flags the
circular dependency:

WARNING: possible circular locking dependency detected
repro_rmdir/89 is trying to acquire lock:
((work_completion)(&tr->autoremove_work)), at: __flush_work
but task is already holding lock:
(trace_types_lock), at: instance_rmdir

Chain exists of:
(work_completion)(&tr->autoremove_work)
--> event_mutex --> trace_types_lock

2. Use-after-free / ABA race: if autoremove_work has already been
dequeued (so work_pending() returns false) and is waiting on
event_mutex, trace_array_cancel_autoremove() skips cancellation and
__remove_instance() frees tr while the work item is still executing
in the worker. If another trace_array is later allocated at the same
address before the worker acquires event_mutex, trace_array_destroy()
could also destroy the wrong instance.

Remove trace_array_cancel_autoremove() and let trace_array_autoremove()
exclusively destroy tr once tr->autoremove_queued is set. Consolidate
trace_array_destroy() and instance_rmdir() so that if tr->autoremove_queued
is set, they release the locks, wait for autoremove_wq to flush, and
return -ENODEV (avoiding double dentry removal in vfs_rmdir()).

Fixes: eca33fdab47b ("tracing: Remove the backup instance automatically after read")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
Changes in v2:
- Consolidate trace_array_destroy() and instance_rmdir() via
try_remove_instance() and drop nested scoped_guard().
- Keep returning -ENODEV after flush_workqueue() so vfs_rmdir()
does not run d_delete_notify() on an already-removed dentry.
---
kernel/trace/trace.c | 82 +++++++++++++++++++++++++++++++-------------------
1 file changed, 51 insertions(+), 31 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 918cc463b23f..bfe405f1fbe9 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -575,11 +575,14 @@ void trace_set_ring_buffer_expanded(struct trace_array *tr)
tr->ring_buffer_expanded = true;
}

+static int __trace_array_destroy(struct trace_array *this_tr, const char *name,
+ bool from_autoremove);
+
static void trace_array_autoremove(struct work_struct *work)
{
struct trace_array *tr = container_of(work, struct trace_array, autoremove_work);

- trace_array_destroy(tr);
+ __trace_array_destroy(tr, NULL, true);
}

static struct workqueue_struct *autoremove_wq;
@@ -594,16 +597,6 @@ static void trace_array_kick_autoremove(struct trace_array *tr)
}
}

-static void trace_array_cancel_autoremove(struct trace_array *tr)
-{
- /*
- * Since this can be called inside trace_array_autoremove(),
- * it has to avoid deadlock of the workqueue.
- */
- if (work_pending(&tr->autoremove_work))
- cancel_work_sync(&tr->autoremove_work);
-}
-
static void trace_array_init_autoremove(struct trace_array *tr)
{
INIT_WORK(&tr->autoremove_work, trace_array_autoremove);
@@ -8947,7 +8940,6 @@ static int __remove_instance(struct trace_array *tr)
set_tracer_flag(tr, 1ULL << i, 0);
}

- trace_array_cancel_autoremove(tr);
tracing_set_nop(tr);
clear_ftrace_function_probes(tr);
event_trace_del_tracer(tr);
@@ -8977,39 +8969,67 @@ static int __remove_instance(struct trace_array *tr)
return 0;
}

-int trace_array_destroy(struct trace_array *this_tr)
+static int try_remove_instance(struct trace_array *this_tr, const char *name,
+ bool from_autoremove)
{
struct trace_array *tr;

- if (!this_tr)
- return -EINVAL;
-
+ /* Making sure trace array exists before destroying it. */
guard(mutex)(&event_mutex);
guard(mutex)(&trace_types_lock);

-
- /* Making sure trace array exists before destroying it. */
- list_for_each_entry(tr, &ftrace_trace_arrays, list) {
- if (tr == this_tr)
- return __remove_instance(tr);
+ if (name) {
+ tr = trace_array_find(name);
+ if (!tr)
+ return -ENODEV;
+ } else {
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+ if (tr == this_tr)
+ break;
+ }
+ if (list_entry_is_head(tr, &ftrace_trace_arrays, list))
+ return -ENODEV;
}

- return -ENODEV;
+ if (!tr->autoremove_queued || from_autoremove)
+ return __remove_instance(tr);
+
+ /* Return positive to flush autoremove work queue */
+ return 1;
}
-EXPORT_SYMBOL_GPL(trace_array_destroy);

-static int instance_rmdir(const char *name)
+static int __trace_array_destroy(struct trace_array *this_tr, const char *name,
+ bool from_autoremove)
{
- struct trace_array *tr;
+ int ret;

- guard(mutex)(&event_mutex);
- guard(mutex)(&trace_types_lock);
+ if ((!this_tr && !name) || (this_tr && name))
+ return -EINVAL;

- tr = trace_array_find(name);
- if (!tr)
- return -ENODEV;
+ ret = try_remove_instance(this_tr, name, from_autoremove);
+ if (ret <= 0)
+ return ret;
+
+ /*
+ * Wait outside event_mutex and trace_types_lock for
+ * trace_array_autoremove() to finish destroying this instance,
+ * then return -ENODEV so vfs_rmdir() will not run d_delete_notify()
+ * on the dentry already removed by tracefs_remove().
+ */
+ flush_workqueue(autoremove_wq);
+
+ return -ENODEV;
+}

- return __remove_instance(tr);
+int trace_array_destroy(struct trace_array *this_tr)
+{
+ return __trace_array_destroy(this_tr, NULL, false);
+}
+EXPORT_SYMBOL_GPL(trace_array_destroy);
+
+static int instance_rmdir(const char *name)
+{
+ return __trace_array_destroy(NULL, name, false);
}

static __init void create_trace_instances(struct dentry *d_tracer)