[PATCH 1/3] tracing: Return EOF instead of blocking on empty readonly instances
From: Masami Hiramatsu (Google)
Date: Wed Dec 02 2026 - 10:05:49 EST
From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
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, blocking pipe readers still enter wait_on_pipe() (or
ring_buffer_wait()) and hang indefinitely when no data is available to
read and update_last_data_if_empty() returns false. 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 entering the blocking wait in the
pipe read, splice, and mmap reader ioctl paths so they return EOF
(or -ENODEV for the ioctl) instead of hanging. Keep non-blocking -EAGAIN
and trace_poll() behavior unchanged to avoid breaking non-blocking
readers or causing poll() spin loops.
Fixes: 2c79da099aac ("tracing: Make the backup instance non-reusable")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
kernel/trace/trace.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index fa65ef8d7fbe..1a355f919fdb 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -5373,6 +5373,10 @@ static int tracing_wait_pipe(struct file *filp)
return -EAGAIN;
}
+ /* Readonly instances can never receive new events. */
+ if (trace_array_is_readonly(iter->tr))
+ return 0;
+
/*
* We block until we read something and tracing is disabled.
* We still block if tracing is disabled, but we have never
@@ -7162,6 +7166,9 @@ ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
if ((filp->f_flags & O_NONBLOCK))
return -EAGAIN;
+ if (trace_array_is_readonly(iter->tr))
+ return 0;
+
ret = wait_on_pipe(iter, 0);
if (ret)
return ret;
@@ -7375,8 +7382,12 @@ ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
if (woken)
goto out;
- ret = -EAGAIN;
- if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
+ if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
+ ret = -EAGAIN;
+ goto out;
+ }
+
+ if (trace_array_is_readonly(iter->tr))
goto out;
ret = wait_on_pipe(iter, iter->snapshot ? 0 : iter->tr->buffer_percent);
@@ -7407,7 +7418,8 @@ 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,