diff mbox series

[5/7] libtracefs: Call mmap ioctl if a refresh happens

Message ID 20240110030116.81837-6-rostedt@goodmis.org (mailing list archive)
State Accepted
Commit 805f650b9443bbd6043832938204df23789a4757
Headers show
Series libtracefs: More fixes for memory mapping ring buffer API | expand

Commit Message

Steven Rostedt Jan. 10, 2024, 2:51 a.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

If the reader sub-buffer gets more data on it due to a writer still on the
reader sub-buffer, just updating the kbuf is not enough. An ioctl() needs to
be called again to update the "read" pointer, otherwise after reading the
full reader sub-buffer and calling the next ioctl(), the kernel will not
swap out for a new reader sub-buffer as it still thinks there's more to be
read.

Fixes: 2ed14b594f669 ("libtracefs: Add ring buffer memory mapping APIs")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 src/tracefs-mmap.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/src/tracefs-mmap.c b/src/tracefs-mmap.c
index e0e37681e019..499233ae396c 100644
--- a/src/tracefs-mmap.c
+++ b/src/tracefs-mmap.c
@@ -143,6 +143,11 @@  __hidden void trace_unmap(void *mapping)
 	free(tmap);
 }
 
+static int get_reader(struct trace_mmap *tmap)
+{
+	return ioctl(tmap->fd, TRACE_MMAP_IOCTL_GET_READER);
+}
+
 __hidden int trace_mmap_load_subbuf(void *mapping, struct kbuffer *kbuf)
 {
 	struct trace_mmap *tmap = mapping;
@@ -171,11 +176,18 @@  __hidden int trace_mmap_load_subbuf(void *mapping, struct kbuffer *kbuf)
 	kbuffer_refresh(kbuf);
 
 	/* Are there still events to read? */
-	if (kbuffer_curr_size(kbuf))
+	if (kbuffer_curr_size(kbuf)) {
+		/* If current is greater than what was read, refresh */
+		if (kbuffer_curr_offset(kbuf) + kbuffer_curr_size(kbuf) >
+		    tmap->map->reader.read) {
+			if (get_reader(tmap) < 0)
+				return -1;
+		}
 		return 1;
+	}
 
 	/* See if a new page is ready? */
-	if (ioctl(tmap->fd, TRACE_MMAP_IOCTL_GET_READER) < 0)
+	if (get_reader(tmap) < 0)
 		return -1;
 	id = tmap->map->reader.id;
 	data = tmap->data + tmap->map->subbuf_size * id;