Skip to contentSkip to conversation

libtracefs: Read the sub buffer size of the instance being opened

PATCH New Li Pengfei 1 participant 1 message Nov 21

lore
Share & tools
lore

Li Pengfei

author
#0 lore raw

PATCH 1/1 libtracefs: Read the sub buffer size of the instance being opened

From: Li Pengfei <[email protected]>

cpu_open() loads the top level headers, but the file it opens belongs to @instance, which can have a buffer_subbuf_size_kb of its own. Load the headers of the instance being opened.

A mapped read copies whole sub buffers, so bound it by the size the caller was told to allocate, for the case where the two still disagree.

Fixes 4ede86e9a48d ("libtracefs: Enable mmapped ring buffer")

---
The size tracefs_cpu_read_size() reports is what callers allocate with, so with a 4K top level instance and an instance at 128K a mapped read copies 128K into a 4K allocation. A test that writes 5000 trace_marker records with "-B foo --subbuf-size=128 --nosplice" read back none of them on arm64 6.12, and all 5000 with this applied.

---
 include/tracefs-local.h |  2 +-
 src/tracefs-mmap.c      | 11 +++++++++--
 src/tracefs-record.c    | 17 ++++++++++++++---
 3 files changed, 24 insertions(+), 6 deletions(-)
include/tracefs-local.h +1 -1
index 056e7ca..ea9145a 100644
@@ -120,7 +120,7 @@ int tfs_append_filter(char **filter, unsigned int *state,
 void *tfs_mmap(int fd, struct kbuffer *kbuf);
 void tfs_unmap(void *mapping);
 int tfs_mmap_load_subbuf(void *mapping, struct kbuffer *kbuf);
-int tfs_mmap_read(void *mapping, void *buffer);
+int tfs_mmap_read(void *mapping, void *buffer, int len);
 
 struct tracefs_synth *
 tfs_synth_init_from(struct tep_handle *tep,
src/tracefs-mmap.c +9 -2
index 7530805..2129c4e 100644
@@ -222,7 +222,7 @@ __hidden int tfs_mmap_load_subbuf(void *mapping, struct kbuffer *kbuf)
 	return 1;
 }
 
-__hidden int tfs_mmap_read(void *mapping, void *buffer)
+__hidden int tfs_mmap_read(void *mapping, void *buffer, int len)
 {
 	struct trace_mmap *tmap = mapping;
 	struct kbuffer *kbuf;
@@ -231,6 +231,13 @@ __hidden int tfs_mmap_read(void *mapping, void *buffer)
 	if (!tmap)
 		return -1;
 
+	/*
+	 * @len is the sub buffer size the caller was told to allocate, which
+	 * can be smaller than the mapping's if the two disagree.
+	 */
+	if (len > tmap->map->subbuf_size)
+		len = tmap->map->subbuf_size;
+
 	kbuf = tmap->kbuf;
 
 	ret = tfs_mmap_load_subbuf(mapping, kbuf);
@@ -239,7 +246,7 @@ __hidden int tfs_mmap_read(void *mapping, void *buffer)
 		return ret;
 
 	/* Update the buffer */
-	ret = kbuffer_read_buffer(kbuf, buffer, tmap->map->subbuf_size);
+	ret = kbuffer_read_buffer(kbuf, buffer, len);
 	if (ret <= 0)
 		return ret;
 
src/tracefs-record.c +14 -3
index f5197c4..13cef71 100644
@@ -99,6 +99,7 @@ static struct tracefs_cpu *cpu_open(struct tracefs_instance *instance,
 	struct tracefs_cpu *tcpu;
 	struct tep_handle *tep;
 	struct kbuffer *kbuf;
+	char *instance_dir = NULL;
 	char path[128];
 	int mode = O_RDONLY;
 	int subbuf_size;
@@ -118,8 +119,18 @@ static struct tracefs_cpu *cpu_open(struct tracefs_instance *instance,
 	if (!tep)
 		goto fail;
 
-	/* Get the size of the page */
-	ret = tracefs_load_headers(NULL, tep);
+	/*
+	 * Get the size of the sub buffer from the instance being opened, as it
+	 * can have a buffer_subbuf_size_kb of its own.
+	 */
+	if (instance) {
+		instance_dir = tracefs_instance_get_dir(instance);
+		if (!instance_dir)
+			goto fail;
+	}
+
+	ret = tracefs_load_headers(instance_dir, tep);
+	tracefs_put_tracing_file(instance_dir);
 	if (ret < 0)
 		goto fail;
 
@@ -429,7 +440,7 @@ static int mmap_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblock)
 	void *mapping = tcpu->mapping;
 	int ret;
 
-	ret = tfs_mmap_read(mapping, buffer);
+	ret = tfs_mmap_read(mapping, buffer, tcpu->subbuf_size);
 	if (ret <= 0) {
 		if (!ret && nonblock)
 			errno = EAGAIN;

--
2.34.1