tracing: Reset last-boot buffers when reading out all cpu buffers
authorMasami Hiramatsu (Google) <mhiramat@kernel.org>
Thu, 22 May 2025 15:54:52 +0000 (00:54 +0900)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Thu, 29 May 2025 12:24:07 +0000 (08:24 -0400)
Reset the last-boot ring buffers when read() reads out all cpu
buffers through trace_pipe/trace_pipe_raw. This prevents ftrace to
unwind ring buffer read pointer next boot.

Note that this resets only when all per-cpu buffers are empty, and
read via read(2) syscall. For example, if you read only one of the
per-cpu trace_pipe, it does not reset it. Also, reading buffer by
splice(2) syscall does not reset because some data in the reader
(the last) page.

Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Link: https://lore.kernel.org/174792929202.496143.8184644221859580999.stgit@mhiramat.tok.corp.google.com
Suggested-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
kernel/trace/trace.c

index 73d75cdb7e5bea048f2907459ea60f8fbc2b4bf4..ecb774cc01ab36d9fa9871097ddf1810eecccd1c 100644 (file)
@@ -6610,6 +6610,22 @@ static int tracing_wait_pipe(struct file *filp)
        return 1;
 }
 
+static bool update_last_data_if_empty(struct trace_array *tr)
+{
+       if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
+               return false;
+
+       if (!ring_buffer_empty(tr->array_buffer.buffer))
+               return false;
+
+       /*
+        * If the buffer contains the last boot data and all per-cpu
+        * buffers are empty, reset it from the kernel side.
+        */
+       update_last_data(tr);
+       return true;
+}
+
 /*
  * Consumer reader.
  */
@@ -6641,6 +6657,9 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,
        }
 
 waitagain:
+       if (update_last_data_if_empty(iter->tr))
+               return 0;
+
        sret = tracing_wait_pipe(filp);
        if (sret <= 0)
                return sret;
@@ -8167,6 +8186,9 @@ tracing_buffers_read(struct file *filp, char __user *ubuf,
 
        if (ret < 0) {
                if (trace_empty(iter) && !iter->closed) {
+                       if (update_last_data_if_empty(iter->tr))
+                               return 0;
+
                        if ((filp->f_flags & O_NONBLOCK))
                                return -EAGAIN;