mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/3] ring-buffer: Use kvmalloc variants for big allocations
@ 2026-11-27 15:13 Vincent Donnefort
  2026-11-27 15:13 ` [PATCH v2 1/3] ring-buffer: Use kvzalloc_objs for user mapping arrays Vincent Donnefort
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Vincent Donnefort @ 2026-11-27 15:13 UTC (permalink / raw)
  To: rostedt, mhiramat, linux-trace-kernel
  Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort

Several allocations in the ring buffer code scale with buffer capacity
or sub-buffer order and require high-order contiguous physical memory
when using plain kmalloc. Under memory fragmentation or with large
buffers, these allocations can fail unnecessarily.

This series converts those allocations to their kvmalloc equivalents.

Changelog:

v2:
  * Drop "ring-buffer: Drop redundant ring_buffer_iter::event_size". (Steven)
  * Rebase on 7.4-rc4.

v1 (https://lore.kernel.org/all/20260922072944.1281427-1-vdonnefort@google.com/)

Vincent Donnefort (3):
  ring-buffer: Use kvzalloc_objs for user mapping arrays
  ring-buffer: Use kvzalloc_objs for remote buffer subbuf_ids
  ring-buffer: Use kvmalloc for ring_buffer_iter::event

 kernel/trace/ring_buffer.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)


base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
-- 
2.56.1.839.g22a8290db9-goog


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

* [PATCH v2 1/3] ring-buffer: Use kvzalloc_objs for user mapping arrays
  2026-11-27 15:13 [PATCH v2 0/3] ring-buffer: Use kvmalloc variants for big allocations Vincent Donnefort
@ 2026-11-27 15:13 ` Vincent Donnefort
  2026-11-27 15:13 ` [PATCH v2 2/3] ring-buffer: Use kvzalloc_objs for remote buffer subbuf_ids Vincent Donnefort
  2026-11-27 15:13 ` [PATCH v2 3/3] ring-buffer: Use kvmalloc for ring_buffer_iter::event Vincent Donnefort
  2 siblings, 0 replies; 4+ messages in thread
From: Vincent Donnefort @ 2026-11-27 15:13 UTC (permalink / raw)
  To: rostedt, mhiramat, linux-trace-kernel
  Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort

Mapping a large trace buffer to user space requires allocating arrays of
struct buffer_page * and struct page *. For large ring buffers, these
contiguous allocations can fail under memory fragmentation.

Switch both to kvzalloc_objs() to use the vmalloc fallback under memory
pressure.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
 kernel/trace/ring_buffer.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index a5a0adc934fa..484fb1c08682 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -7762,9 +7762,9 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
 			struct vm_area_struct *vma)
 {
 	unsigned long nr_subbufs, nr_pages, nr_vma_pages;
-	pgoff_t pgoff = vma_start_pgoff(vma);
+	struct page **pages __free(kvfree) = NULL;
 	unsigned int subbuf_pages, subbuf_order;
-	struct page **pages __free(kfree) = NULL;
+	pgoff_t pgoff = vma_start_pgoff(vma);
 	int p = 0, s = 0;
 	int err;
 
@@ -7801,7 +7801,7 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
 
 	nr_pages = nr_vma_pages;
 
-	pages = kzalloc_objs(*pages, nr_pages);
+	pages = kvzalloc_objs(*pages, nr_pages);
 	if (!pages)
 		return -ENOMEM;
 
@@ -7893,7 +7893,7 @@ int ring_buffer_map(struct trace_buffer *buffer, int cpu,
 		return err;
 
 	/* subbuf_ids includes the reader while nr_pages does not */
-	subbuf_ids = kzalloc_objs(*subbuf_ids, cpu_buffer->nr_pages + 1);
+	subbuf_ids = kvzalloc_objs(*subbuf_ids, cpu_buffer->nr_pages + 1);
 	if (!subbuf_ids) {
 		rb_free_meta_page(cpu_buffer);
 		return -ENOMEM;
@@ -7917,7 +7917,7 @@ int ring_buffer_map(struct trace_buffer *buffer, int cpu,
 		cpu_buffer->user_mapped = 1;
 		raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 	} else {
-		kfree(cpu_buffer->subbuf_ids);
+		kvfree(cpu_buffer->subbuf_ids);
 		cpu_buffer->subbuf_ids = NULL;
 		rb_free_meta_page(cpu_buffer);
 		atomic_dec(&cpu_buffer->resize_disabled);
@@ -7974,7 +7974,7 @@ int ring_buffer_unmap(struct trace_buffer *buffer, int cpu)
 
 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 
-	kfree(cpu_buffer->subbuf_ids);
+	kvfree(cpu_buffer->subbuf_ids);
 	cpu_buffer->subbuf_ids = NULL;
 	rb_free_meta_page(cpu_buffer);
 	atomic_dec(&cpu_buffer->resize_disabled);
-- 
2.56.1.839.g22a8290db9-goog


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

* [PATCH v2 2/3] ring-buffer: Use kvzalloc_objs for remote buffer subbuf_ids
  2026-11-27 15:13 [PATCH v2 0/3] ring-buffer: Use kvmalloc variants for big allocations Vincent Donnefort
  2026-11-27 15:13 ` [PATCH v2 1/3] ring-buffer: Use kvzalloc_objs for user mapping arrays Vincent Donnefort
@ 2026-11-27 15:13 ` Vincent Donnefort
  2026-11-27 15:13 ` [PATCH v2 3/3] ring-buffer: Use kvmalloc for ring_buffer_iter::event Vincent Donnefort
  2 siblings, 0 replies; 4+ messages in thread
From: Vincent Donnefort @ 2026-11-27 15:13 UTC (permalink / raw)
  To: rostedt, mhiramat, linux-trace-kernel
  Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort

Creating a remote ring buffer requires allocating an array of struct
buffer_page *. For large ring buffers, this contiguous allocation can
fail under memory fragmentation.

Switch it to kvzalloc_objs() to use the vmalloc fallback under memory
pressure.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
 kernel/trace/ring_buffer.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 484fb1c08682..bcff4c12680f 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -2638,8 +2638,8 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, unsigned long nr_pages, int
 		cpu_buffer->remote = buffer->remote;
 		cpu_buffer->meta_page = (struct trace_buffer_meta *)(void *)desc->meta_va;
 		cpu_buffer->nr_pages = nr_pages;
-		cpu_buffer->subbuf_ids = kzalloc_objs(*cpu_buffer->subbuf_ids,
-						      cpu_buffer->nr_pages + 1);
+		cpu_buffer->subbuf_ids = kvzalloc_objs(*cpu_buffer->subbuf_ids,
+						       cpu_buffer->nr_pages + 1);
 		if (!cpu_buffer->subbuf_ids)
 			goto fail_free_reader;
 
@@ -2699,7 +2699,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, unsigned long nr_pages, int
 	return_ptr(cpu_buffer);
 
  fail_free_reader:
-	kfree(cpu_buffer->subbuf_ids);
+	kvfree(cpu_buffer->subbuf_ids);
 	free_buffer_page(cpu_buffer->reader_page);
 
 	return NULL;
@@ -2712,8 +2712,8 @@ static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
 
 	irq_work_sync(&cpu_buffer->irq_work.work);
 
-	if (cpu_buffer->remote)
-		kfree(cpu_buffer->subbuf_ids);
+	kvfree(cpu_buffer->subbuf_ids);
+	cpu_buffer->subbuf_ids = NULL;
 
 	free_buffer_page(cpu_buffer->reader_page);
 
-- 
2.56.1.839.g22a8290db9-goog


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

* [PATCH v2 3/3] ring-buffer: Use kvmalloc for ring_buffer_iter::event
  2026-11-27 15:13 [PATCH v2 0/3] ring-buffer: Use kvmalloc variants for big allocations Vincent Donnefort
  2026-11-27 15:13 ` [PATCH v2 1/3] ring-buffer: Use kvzalloc_objs for user mapping arrays Vincent Donnefort
  2026-11-27 15:13 ` [PATCH v2 2/3] ring-buffer: Use kvzalloc_objs for remote buffer subbuf_ids Vincent Donnefort
@ 2026-11-27 15:13 ` Vincent Donnefort
  2 siblings, 0 replies; 4+ messages in thread
From: Vincent Donnefort @ 2026-11-27 15:13 UTC (permalink / raw)
  To: rostedt, mhiramat, linux-trace-kernel
  Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort

The maximum size for an event corresponds to the capacity of a single
ring-buffer sub-buffer, which can span multiple pages and fail
contiguous allocations under memory fragmentation.

Switch it to kvmalloc() to fall back to vmalloc under memory pressure,
keeping kmalloc() when blocking is disallowed.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
 kernel/trace/ring_buffer.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index bcff4c12680f..c543c200f769 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -6517,7 +6517,8 @@ ring_buffer_read_start(struct trace_buffer *buffer, int cpu, gfp_t flags)
 
 	/* Holds the entire event: data and meta data. */
 	iter->event_size = rb_page_capacity(READ_ONCE(cpu_buffer->reader_page));
-	iter->event = kmalloc(iter->event_size, flags);
+	iter->event = gfpflags_allow_blocking(flags) ?
+		kvmalloc(iter->event_size, flags) : kmalloc(iter->event_size, flags);
 	if (!iter->event) {
 		atomic_dec(&cpu_buffer->resize_disabled);
 		return NULL;
@@ -6548,7 +6549,7 @@ ring_buffer_read_finish(struct ring_buffer_iter *iter)
 	rb_check_pages(cpu_buffer);
 
 	atomic_dec(&cpu_buffer->resize_disabled);
-	kfree(iter->event);
+	kvfree(iter->event);
 	kfree(iter);
 }
 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
-- 
2.56.1.839.g22a8290db9-goog


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

end of thread, other threads:[~2026-11-27 15:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 15:13 [PATCH v2 0/3] ring-buffer: Use kvmalloc variants for big allocations Vincent Donnefort
2026-11-27 15:13 ` [PATCH v2 1/3] ring-buffer: Use kvzalloc_objs for user mapping arrays Vincent Donnefort
2026-11-27 15:13 ` [PATCH v2 2/3] ring-buffer: Use kvzalloc_objs for remote buffer subbuf_ids Vincent Donnefort
2026-11-27 15:13 ` [PATCH v2 3/3] ring-buffer: Use kvmalloc for ring_buffer_iter::event Vincent Donnefort

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

all inboxes | Powered by JetHome®