Re: [PATCH 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove
From: Masami Hiramatsu (Google)
Date: Wed Dec 02 2026 - 20:37:00 EST
On Wed, 2 Dec 2026 13:35:10 -0500, Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
> On Thu, 3 Dec 2026 00:05:42 +0900
> "Masami Hiramatsu (Google)" <mhiramat@xxxxxxxxxx> wrote:
>
> > Fixes: eca33fdab47b ("tracing: Remove the backup instance automatically after read")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
> > ---
> > kernel/trace/trace.c | 78 +++++++++++++++++++++++++++++++++-----------------
> > 1 file changed, 51 insertions(+), 27 deletions(-)
> >
> > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> > index 5914b970b34e..f4aa6c06ee8a 100644
> > --- a/kernel/trace/trace.c
> > +++ b/kernel/trace/trace.c
> > @@ -574,11 +574,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,
> > + 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, true);
> > }
> >
> > static struct workqueue_struct *autoremove_wq;
> > @@ -593,16 +596,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);
> > @@ -8942,7 +8935,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);
> > @@ -8972,39 +8964,71 @@ static int __remove_instance(struct trace_array *tr)
> > return 0;
> > }
> >
> > -int trace_array_destroy(struct trace_array *this_tr)
> > +static int __trace_array_destroy(struct trace_array *this_tr,
> > + bool from_autoremove)
>
> Nit, but do not do the line break here. Without it, it's only 84
> characters. We live in the 21st century now and we can have lines up to 100
> characters.
OK.
>
>
> > {
> > struct trace_array *tr;
> > + bool wait_autoremove = false;
> > + int ret = -ENODEV;
> >
> > if (!this_tr)
> > return -EINVAL;
> >
> > - guard(mutex)(&event_mutex);
> > - guard(mutex)(&trace_types_lock);
> > -
> > + scoped_guard(mutex, &event_mutex) {
> > + scoped_guard(mutex, &trace_types_lock) {
>
> If you have two locks with the same scope, please only use one scoped_guard().
>
> scoped_guard(mutex, &event_mutex) {
> guard(mutex)(&trace_types_lock);
>
> It does the same thing without the extra indentation.
Ah, indeed.
>
>
> > + /* Making sure trace array exists before destroying it. */
> > + list_for_each_entry(tr, &ftrace_trace_arrays, list) {
> > + if (tr != this_tr)
> > + continue;
> > + if (!from_autoremove && tr->autoremove_queued)
> > + wait_autoremove = true;
> > + else
> > + ret = __remove_instance(tr);
> > + break;
> > + }
> > + }
> > + }
> >
> > - /* 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 (wait_autoremove) {
> > + /*
> > + * Wait outside event_mutex and trace_types_lock for
> > + * trace_array_autoremove() to finish destroying this instance.
> > + */
> > + flush_workqueue(autoremove_wq);
> > }
> >
> > - return -ENODEV;
> > + return ret;
> > +}
> > +
> > +int trace_array_destroy(struct trace_array *this_tr)
> > +{
> > + return __trace_array_destroy(this_tr, false);
> > }
> > EXPORT_SYMBOL_GPL(trace_array_destroy);
> >
> > static int instance_rmdir(const char *name)
> > {
> > struct trace_array *tr;
> > + bool wait_autoremove = false;
> > + int ret = -ENODEV;
> >
> > - guard(mutex)(&event_mutex);
> > - guard(mutex)(&trace_types_lock);
> > + scoped_guard(mutex, &event_mutex) {
> > + scoped_guard(mutex, &trace_types_lock) {
>
> Same here.
Got it.
>
>
> > + tr = trace_array_find(name);
> > + if (!tr)
> > + return -ENODEV;
> >
> > - tr = trace_array_find(name);
> > - if (!tr)
> > - return -ENODEV;
> > + if (tr->autoremove_queued)
> > + wait_autoremove = true;
> > + else
> > + ret = __remove_instance(tr);
> > + }
> > + }
> >
> > - return __remove_instance(tr);
> > + if (wait_autoremove)
> > + flush_workqueue(autoremove_wq);
> > +
> > + return ret;
> > }
> >
> > static __init void create_trace_instances(struct dentry *d_tracer)
>
> Actually, instead of duplicating the logic, consolidate it to something
> like this. Not tested nor compiled, but I created this on top of this
> patch, so you can fold it into this commit.
OK, thanks! Let me send v2.
>
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 842efb5efd22..35ee03a97617 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -574,14 +574,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,
> +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, true);
> + __trace_array_destroy(tr, NULL, true);
> }
>
> static struct workqueue_struct *autoremove_wq;
> @@ -8946,71 +8946,63 @@ static int __remove_instance(struct trace_array *tr)
> return 0;
> }
>
> -static int __trace_array_destroy(struct trace_array *this_tr,
> - bool from_autoremove)
> +static int try_remove_instance(struct trace_array *this_tr, const char *name,
> + bool from_autoremove)
> {
> struct trace_array *tr;
> - bool wait_autoremove = false;
> - int ret = -ENODEV;
>
> - if (!this_tr)
> - return -EINVAL;
> + /* Making sure trace array exists before destroying it. */
> + guard(mutex)(&event_mutex);
> + guard(mutex)(&trace_types_lock);
>
> - scoped_guard(mutex, &event_mutex) {
> - scoped_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)
> - continue;
> - if (!from_autoremove && tr->autoremove_queued)
> - wait_autoremove = true;
> - else
> - ret = __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 (tr != this_tr)
> + return -ENODEV;
> }
>
> - if (wait_autoremove) {
> - /*
> - * Wait outside event_mutex and trace_types_lock for
> - * trace_array_autoremove() to finish destroying this instance.
> - */
> - flush_workqueue(autoremove_wq);
> - }
> + if (!tr->autoremove_queued || from_autoremove)
> + return __remove_instance(tr);
>
> - return ret;
> + /* Return positive to flush autoremove work queue */
> + return 1;
> +}
> +
> +static int __trace_array_destroy(struct trace_array *this_tr, const char *name,
> + bool from_autoremove)
> +{
> + struct trace_array *tr;
> + int ret;
> +
> + if (!this_tr && !name)
> + return -EINVAL;
> +
> + ret = try_remove_instance(this_tr, name, from_autoremove);
> + if (ret <= 0)
> + return ret;
> +
> + flush_workqueue(autoremove_wq);
> +
> + return 0;
> }
>
> int trace_array_destroy(struct trace_array *this_tr)
> {
> - return __trace_array_destroy(this_tr, false);
> + return __trace_array_destroy(this_tr, NULL, false);
> }
> EXPORT_SYMBOL_GPL(trace_array_destroy);
>
> static int instance_rmdir(const char *name)
> {
> - struct trace_array *tr;
> - bool wait_autoremove = false;
> - int ret = -ENODEV;
> -
> - scoped_guard(mutex, &event_mutex) {
> - scoped_guard(mutex, &trace_types_lock) {
> - tr = trace_array_find(name);
> - if (!tr)
> - return -ENODEV;
> -
> - if (tr->autoremove_queued)
> - wait_autoremove = true;
> - else
> - ret = __remove_instance(tr);
> - }
> - }
> -
> - if (wait_autoremove)
> - flush_workqueue(autoremove_wq);
> -
> - return ret;
> + return __trace_array_destroy(NULL, name, false);
> }
>
> static __init void create_trace_instances(struct dentry *d_tracer)
>
>
--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>