[PATCH v2 1/3] tracing: Return EOF on empty readonly instances

From: Masami Hiramatsu (Google)

Date: Thu Dec 03 2026 - 04:54:07 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, 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@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>

---
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,