[PATCH 2/3] tracing: Reject backup instance opens only after autoremove is queued

From: Masami Hiramatsu (Google)

Date: Wed Dec 02 2026 - 10:06:46 EST


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

Commit eca33fdab47b ("tracing: Remove the backup instance automatically
after read") sets tr->free_on_close in update_last_data() once all
previous-boot data has been read out or cleared, and queues
autoremove_work in __trace_array_put() when the last user reference is
dropped (tr->ref == 1). However, __trace_array_get() rejects new
references with -ENODEV as soon as tr->free_on_close is set, even while
existing file descriptors still hold tr->ref > 1 and autoremove has not
yet been queued.

This breaks userspace consumers that keep multiple file descriptors open
on the backup instance while finishing a dump. For example, a reader
that keeps per_cpu/cpu*/trace_pipe_raw open across CPUs and drains them
via read() triggers update_last_data_if_empty() on the final empty read,
setting tr->free_on_close while those pipe file descriptors are still
open. Any subsequent open of another file in the same instance before
closing the pipes -- such as reading per_cpu/cpu*/stats or opening trace
with O_WRONLY | O_TRUNC to clear the buffer -- then fails with -ENODEV.

Introduce a tr->autoremove_queued flag set when autoremove_work is
queued, and check it in __trace_array_get() instead of tr->free_on_close.
This allows opening files while existing references remain, while still
blocking new opens once autoremove is queued.

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 | 12 +++++++++---
kernel/trace/trace.h | 4 ++++
2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 1a355f919fdb..5914b970b34e 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -585,8 +585,12 @@ static struct workqueue_struct *autoremove_wq;

static void trace_array_kick_autoremove(struct trace_array *tr)
{
- if (autoremove_wq)
+ lockdep_assert_held(&trace_types_lock);
+
+ if (autoremove_wq && !tr->autoremove_queued) {
+ tr->autoremove_queued = true;
queue_work(autoremove_wq, &tr->autoremove_work);
+ }
}

static void trace_array_cancel_autoremove(struct trace_array *tr)
@@ -619,8 +623,10 @@ LIST_HEAD(ftrace_trace_arrays);

static int __trace_array_get(struct trace_array *this_tr)
{
- /* When free_on_close is set, this is not available anymore. */
- if (autoremove_wq && this_tr->free_on_close)
+ lockdep_assert_held(&trace_types_lock);
+
+ /* Once autoremove is queued, this is not available anymore. */
+ if (this_tr->autoremove_queued)
return -ENODEV;

this_tr->ref++;
diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 32997b58a8a0..25c9be11c5a0 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -462,8 +462,12 @@ struct trace_array {
/*
* If the ring buffer is a read only backup instance, it will be
* removed after dumping all data via pipe, because no readable data.
+ * @free_on_close is set once the buffer is drained or cleared, and
+ * @autoremove_queued is set when the last reference is released and
+ * @autoremove_work is queued.
*/
bool free_on_close;
+ bool autoremove_queued;
struct work_struct autoremove_work;
};