Skip to contentSkip to conversation

ring-buffer: Fix memory leak of free page

PATCH In review Steven Rostedt 2 participants 2 messages Dec 11, 2023

Subsystems (2)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH 1/1 ring-buffer: Fix memory leak of free page

From: "Steven Rostedt (Google)" <[email protected]>

Reading the ring buffer does a swap of a sub-buffer within the ring buffer with a empty sub-buffer. This allows the reader to have full access to the content of the sub-buffer that was swapped out without having to worry about contention with the writer.

The readers call ring_buffer_alloc_read_page() to allocate a page that will be used to swap with the ring buffer. When the code is finished with the reader page, it calls ring_buffer_free_read_page(). Instead of freeing the page, it stores it as a spare. Then next call to ring_buffer_alloc_read_page() will return this spare instead of calling into the memory management system to allocate a new page.

Unfortunately, on freeing of the ring buffer, this spare page is not freed, and causes a memory leak.

Fixes 73a757e63114d ("ring-buffer: Return reader page back into existing ring buffer")
---
 kernel/trace/ring_buffer.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/ring_buffer.c +2 -0
index a38e5a3c6803..dd37d21d6e55 100644
@@ -1790,6 +1790,8 @@ static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
 		free_buffer_page(bpage);
 	}
 
+	free_page((unsigned long)cpu_buffer->free_page);
+
 	kfree(cpu_buffer);
 }
 
Signature · 1 line
2.42.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · On Sun, 10 Dec 2023 22:12:50 -0500 Steven Rostedt <[email protected]> wrote:

From: "Steven Rostedt (Google)" <[email protected]>

Reading the ring buffer does a swap of a sub-buffer within the ring buffer with a empty sub-buffer. This allows the reader to have full access to the content of the sub-buffer that was swapped out without having to worry about contention with the writer.

The readers call ring_buffer_alloc_read_page() to allocate a page that will be used to swap with the ring buffer. When the code is finished with the reader page, it calls ring_buffer_free_read_page(). Instead of freeing the page, it stores it as a spare. Then next call to ring_buffer_alloc_read_page() will return this spare instead of calling into the memory management system to allocate a new page.

Quoted 3 lines

Unfortunately, on freeing of the ring buffer, this spare page is not freed, and causes a memory leak.

Oops, Looks good to me.

Thanks,

Quoted 23 lines
Fixes 73a757e63114d ("ring-buffer: Return reader page back into existing ring buffer")
---
 kernel/trace/ring_buffer.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/ring_buffer.c +2 -0
index a38e5a3c6803..dd37d21d6e55 100644
@@ -1790,6 +1790,8 @@ static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
 		free_buffer_page(bpage);
 	}
 
+	free_page((unsigned long)cpu_buffer->free_page);
+
 	kfree(cpu_buffer);
 }
 
Signature · 1 line
2.42.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>