Skip to contentSkip to conversation

ring-buffer: Fix subbuf_ids memory leak in rb_allocate_cpu_buffer() error path

PATCH In review Masami Hiramatsu (Google) 2 participants 2 messages Jul 31

Subsystems (2)
lore
Share & tools
lore

Masami Hiramatsu (Google)

author maintainer
#0 lore raw

PATCH v2 1/1 ring-buffer: Fix subbuf_ids memory leak in rb_allocate_cpu_buffer() error path

From: Masami Hiramatsu (Google) <[email protected]>

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

---
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(+)
kernel/trace/ring_buffer.c +1 -0
index 78d3875a47a5..8e2485bb3aa8 100644
@@ -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;
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 31 lines · Masami Hiramatsu (Google)

From: Masami Hiramatsu (Google) <[email protected]>

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

---
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(+)
kernel/trace/ring_buffer.c +0 -0
index 78d3875a47a5..8e2485bb3aa8 100644

kernel/trace/ring_buffer.c rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) · line 2605

Quoted 7 lines
kernel/trace/ring_buffer.c +1 -0
@@ -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!

Signature · 1 line
Vincent