mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [for-next][PATCH 04/10] tracing: Reset last-boot buffers when reading out all cpu buffers
Date: Wed, 28 May 2025 18:27:08 -0400	[thread overview]
Message-ID: <20250528222729.919034654@goodmis.org> (raw)
In-Reply-To: <20250528222704.623477429@goodmis.org>

From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>

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 | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 73d75cdb7e5b..ecb774cc01ab 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -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;
 
-- 
2.47.2



  parent reply	other threads:[~2025-05-28 22:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-28 22:27 [for-next][PATCH 00/10] ring-buffer: Updates for v6.16 Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 01/10] ring-buffer: Move cpus_read_lock() outside of buffer->mutex Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 02/10] ring-buffer: Do not trigger WARN_ON() due to a commit_overrun Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 03/10] ring-buffer: Allow reserve_mem persistent ring buffers to be mmapped Steven Rostedt
2025-05-28 22:27 ` Steven Rostedt [this message]
2025-05-28 22:27 ` [for-next][PATCH 05/10] ring-buffer: Removed unnecessary if() goto out where out is the next line Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 06/10] ring-buffer: Remove jump to out label in ring_buffer_swap_cpu() Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 07/10] ring-buffer: Simplify reset_disabled_cpu_buffer() with use of guard() Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 08/10] ring-buffer: Simplify ring_buffer_read_page() with guard() Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 09/10] ring-buffer: Make ring_buffer_{un}map() simpler with guard(mutex) Steven Rostedt
2025-05-28 22:27 ` [for-next][PATCH 10/10] ring-buffer: Simplify functions with __free(kfree) to free allocations Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250528222729.919034654@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®