* [PATCH v2 1/3] tracing: Return EOF on empty readonly instances
2026-12-03 9:53 [PATCH v2 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races Masami Hiramatsu (Google)
@ 2026-12-03 9:54 ` Masami Hiramatsu (Google)
2026-12-03 10:17 ` sashiko-bot
2026-12-03 9:54 ` [PATCH v2 2/3] tracing: Reject backup instance opens only after autoremove is queued Masami Hiramatsu (Google)
2026-12-03 9:54 ` [PATCH v2 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove Masami Hiramatsu (Google)
2 siblings, 1 reply; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-03 9:54 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Commit 2c79da099aac ("tracing: Make the backup instance non-reusable")
marked backup instances with TRACE_ARRAY_FL_RDONLY so that no new events
can ever be written to them.
However, when no data is available and update_last_data_if_empty()
returns false, blocking pipe readers still hang indefinitely in
wait_on_pipe() / ring_buffer_wait(), non-blocking readers return -EAGAIN
as if more events might arrive later, and poll() waits indefinitely (or
fails to report readable when unread data is below buffer_percent). This
happens in several cases:
- Reading a per-CPU trace_pipe or trace_pipe_raw on a CPU whose buffer
is empty while another CPU still holds previous-boot data.
- Calling splice() on trace_pipe or trace_pipe_raw when the CPU buffer
is empty or only contains a partial commit page (which cannot be
spliced as a full sub-buffer).
- Reading an empty readonly instance after TRACE_ARRAY_FL_LAST_BOOT has
already been cleared (for example, while another file descriptor still
keeps the backup instance alive).
- Issuing a blocking TRACE_MMAP_IOCTL_GET_READER ioctl on a readonly
trace_pipe_raw file.
Check trace_array_is_readonly() before the non-blocking checks and
blocking waits in the pipe read, splice, and mmap reader ioctl paths so
they return EOF (or -ENODEV for the ioctl) instead of blocking or
returning -EAGAIN. Also return EPOLLIN | EPOLLRDNORM in trace_poll() for
readonly instances since their buffers are static.
Fixes: 2c79da099aac ("tracing: Make the backup instance non-reusable")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
Changes in v2:
- Check trace_array_is_readonly() before O_NONBLOCK / SPLICE_F_NONBLOCK
so non-blocking reads and splices also return EOF instead of -EAGAIN.
- Return EPOLLIN | EPOLLRDNORM in trace_poll() for readonly instances
since their buffers are static.
---
kernel/trace/trace.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index fa65ef8d7fbe..b431606c6375 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -5339,8 +5339,8 @@ trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_tabl
{
struct trace_array *tr = iter->tr;
- /* Iterators are static, they should be filled or empty */
- if (trace_buffer_iter(iter, iter->cpu_file))
+ /* Iterators and readonly instances are static, they are filled or empty */
+ if (trace_buffer_iter(iter, iter->cpu_file) || trace_array_is_readonly(tr))
return EPOLLIN | EPOLLRDNORM;
if (tr->trace_flags & TRACE_ITER(BLOCK))
@@ -5369,6 +5369,10 @@ static int tracing_wait_pipe(struct file *filp)
while (trace_empty(iter)) {
+ /* Readonly instances can never receive new events. */
+ if (trace_array_is_readonly(iter->tr))
+ return 0;
+
if ((filp->f_flags & O_NONBLOCK)) {
return -EAGAIN;
}
@@ -7159,6 +7163,9 @@ ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
if (update_last_data_if_empty(iter->tr))
return 0;
+ if (trace_array_is_readonly(iter->tr))
+ return 0;
+
if ((filp->f_flags & O_NONBLOCK))
return -EAGAIN;
@@ -7375,6 +7382,9 @@ ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
if (woken)
goto out;
+ if (trace_array_is_readonly(iter->tr))
+ goto out;
+
ret = -EAGAIN;
if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
goto out;
@@ -7407,7 +7417,7 @@ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned
int err;
if (cmd == TRACE_MMAP_IOCTL_GET_READER) {
- if (!(file->f_flags & O_NONBLOCK)) {
+ if (!(file->f_flags & O_NONBLOCK) && !trace_array_is_readonly(iter->tr)) {
err = ring_buffer_wait(iter->array_buffer->buffer,
iter->cpu_file,
iter->tr->buffer_percent,
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2 1/3] tracing: Return EOF on empty readonly instances
2026-12-03 9:54 ` [PATCH v2 1/3] tracing: Return EOF on empty readonly instances Masami Hiramatsu (Google)
@ 2026-12-03 10:17 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 10:17 UTC (permalink / raw)
To: Masami Hiramatsu (Google); +Cc: linux-trace-kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tracing: readonly splice EOF skips
update_last_data_if_empty()
--
commit 6492f59da7819633891017bc6387b13b6a84a5c4
Author: Masami Hiramatsu (Google) <mhiramat@kernel.org>
tracing: Return EOF on empty readonly instances
This commit returns EOF instead of blocking or returning -EAGAIN on
empty readonly trace instances in the pipe read, splice, and mmap reader
ioctl paths, and reports them as readable in trace_poll().
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index fa65ef8d7fbea..b431606c6375f 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
[ ... ]
> @@ -5369,6 +5369,10 @@ static int tracing_wait_pipe(struct file *filp)
>
> while (trace_empty(iter)) {
>
> + /* Readonly instances can never receive new events. */
> + if (trace_array_is_readonly(iter->tr))
> + return 0;
> +
> if ((filp->f_flags & O_NONBLOCK)) {
> return -EAGAIN;
> }
[ ... ]
> @@ -7375,6 +7382,9 @@ ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
> if (woken)
> goto out;
>
> + if (trace_array_is_readonly(iter->tr))
> + goto out;
> +
> ret = -EAGAIN;
> if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
> goto out;
[Severity: Medium]
Should these splice EOF paths also call update_last_data_if_empty(iter->tr)?
Unlike tracing_read_pipe() and tracing_buffers_read(), neither
tracing_splice_read_pipe() (via tracing_wait_pipe()) nor
tracing_buffers_splice_read() calls update_last_data_if_empty(iter->tr)
before returning 0 on an empty readonly instance.
When a readonly backup instance is drained via splice() on trace_pipe or
trace_pipe_raw, TRACE_ARRAY_FL_LAST_BOOT is never cleared and
tr->free_on_close stays false, so closing the file descriptor does not
schedule automatic removal and the backup ring buffer stays allocated.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179629163198.2691692.6206990932650792224.stgit@mhiramat.c.googlers.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/3] tracing: Reject backup instance opens only after autoremove is queued
2026-12-03 9:53 [PATCH v2 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races Masami Hiramatsu (Google)
2026-12-03 9:54 ` [PATCH v2 1/3] tracing: Return EOF on empty readonly instances Masami Hiramatsu (Google)
@ 2026-12-03 9:54 ` Masami Hiramatsu (Google)
2026-12-03 9:54 ` [PATCH v2 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove Masami Hiramatsu (Google)
2 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-03 9:54 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
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@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
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 b431606c6375..c50ffb3b4b19 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;
};
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove
2026-12-03 9:53 [PATCH v2 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races Masami Hiramatsu (Google)
2026-12-03 9:54 ` [PATCH v2 1/3] tracing: Return EOF on empty readonly instances Masami Hiramatsu (Google)
2026-12-03 9:54 ` [PATCH v2 2/3] tracing: Reject backup instance opens only after autoremove is queued Masami Hiramatsu (Google)
@ 2026-12-03 9:54 ` Masami Hiramatsu (Google)
2 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-03 9:54 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
__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@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
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 c50ffb3b4b19..7a596003d194 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, 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;
@@ -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);
@@ -8940,7 +8933,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);
@@ -8970,39 +8962,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)
^ permalink raw reply related [flat|nested] 5+ messages in thread