ring-buffer: Fix memory leak of free page
Subsystems (2)
Steven Rostedt
maintainerPATCH 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.
--- kernel/trace/ring_buffer.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/trace/ring_buffer.c +2 -0
@@ -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
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 callsring_buffer_free_read_page(). Instead of freeing the page, it stores it as a spare. Then next call toring_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")Signed-off-by Steven Rostedt (Google) <[email protected]>--- kernel/trace/ring_buffer.c | 2 ++ 1 file changed, 2 insertions(+)kernel/trace/ring_buffer.c +2
-0@@ -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]>