Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH v2] ring-buffer: Fix subbuf_ids memory leak in rb_allocate_cpu_buffer() error path
@ 2026-07-31 14:16 Masami Hiramatsu (Google)
  2026-07-31 14:27 ` Vincent Donnefort
  0 siblings, 1 reply; 2+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-07-31 14:16 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel,
	Vincent Donnefort

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

In rb_allocate_cpu_buffer(), cpu_buffer->subbuf_ids is allocated using
kcalloc() when buffer->remote is non-NULL. If a subsequent page allocation
fails (e.g., ring_buffer_desc_page() returns NULL or rb_allocate_pages()
fails), execution jumps to fail_free_reader.

While __free(kfree) automatically frees the outer cpu_buffer structure
at scope exit, kfree(cpu_buffer) does not recursively free nested heap
pointers such as cpu_buffer->subbuf_ids, resulting in a memory leak.

Fix this by explicitly freeing cpu_buffer->subbuf_ids in the
fail_free_reader error unwinding path when cpu_buffer->remote is set.

Fixes: 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")
Assisted-by: Antigravity:gemini-3.6-flash
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 Changes in v2:
  - Call kfree(subbuf_ids) unconditionally because it should be NULL
    if !remote.
---
 kernel/trace/ring_buffer.c |    1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 78d3875a47a5..8e2485bb3aa8 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -2599,6 +2599,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
 	return_ptr(cpu_buffer);
 
  fail_free_reader:
+	kfree(cpu_buffer->subbuf_ids);
 	free_buffer_page(cpu_buffer->reader_page);
 
 	return NULL;


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] ring-buffer: Fix subbuf_ids memory leak in rb_allocate_cpu_buffer() error path
  2026-07-31 14:16 [PATCH v2] ring-buffer: Fix subbuf_ids memory leak in rb_allocate_cpu_buffer() error path Masami Hiramatsu (Google)
@ 2026-07-31 14:27 ` Vincent Donnefort
  0 siblings, 0 replies; 2+ messages in thread
From: Vincent Donnefort @ 2026-07-31 14:27 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Steven Rostedt, Mathieu Desnoyers, linux-kernel,
	linux-trace-kernel

On Fri, Jul 31, 2026 at 11:16:46PM +0900, Masami Hiramatsu (Google) wrote:
> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> In rb_allocate_cpu_buffer(), cpu_buffer->subbuf_ids is allocated using
> kcalloc() when buffer->remote is non-NULL. If a subsequent page allocation
> fails (e.g., ring_buffer_desc_page() returns NULL or rb_allocate_pages()
> fails), execution jumps to fail_free_reader.
> 
> While __free(kfree) automatically frees the outer cpu_buffer structure
> at scope exit, kfree(cpu_buffer) does not recursively free nested heap
> pointers such as cpu_buffer->subbuf_ids, resulting in a memory leak.
> 
> Fix this by explicitly freeing cpu_buffer->subbuf_ids in the
> fail_free_reader error unwinding path when cpu_buffer->remote is set.
> 
> Fixes: 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")
> Assisted-by: Antigravity:gemini-3.6-flash
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> ---
>  Changes in v2:
>   - Call kfree(subbuf_ids) unconditionally because it should be NULL
>     if !remote.
> ---
>  kernel/trace/ring_buffer.c |    1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> index 78d3875a47a5..8e2485bb3aa8 100644
> --- a/kernel/trace/ring_buffer.c
> +++ b/kernel/trace/ring_buffer.c
> @@ -2599,6 +2599,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
>  	return_ptr(cpu_buffer);
>  
>   fail_free_reader:
> +	kfree(cpu_buffer->subbuf_ids);
>  	free_buffer_page(cpu_buffer->reader_page);
>  
>  	return NULL;
> 

Thanks!

Reviewed-by: Vincent Donnefort <vdonnefort@google.com>

-- 
Vincent

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-31 14:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-31 14:16 [PATCH v2] ring-buffer: Fix subbuf_ids memory leak in rb_allocate_cpu_buffer() error path Masami Hiramatsu (Google)
2026-07-31 14:27 ` Vincent Donnefort

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox