ring-buffer: Fixes for available memory estimation
Subsystems (2)
Vincent Donnefort
maintainerv2 cover ring-buffer: Fixes for available memory estimation
__rb_allocate_pages() uses si_mem_available() to fail early when there is not enough memory for a ring buffer allocation. However it wasn't updated with the introduction of persistent and remote buffers and subbuf_order.
v2:
- Add "ring-buffer: Use subbuf_order in available memory estimation"
v1: https://lore.kernel.org/all/[email protected]/
Vincent Donnefort (2):
ring-buffer: Skip si_mem_available() for persistent and remote buffers
ring-buffer: Use subbuf_order in available memory estimation
kernel/trace/ring_buffer.c | 47 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 23 deletions(-)
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Vincent Donnefort
maintainerPATCH v2 1/2 ring-buffer: Skip si_mem_available() for persistent and remote buffers
Persistent and remote buffer memory is pre-allocated by the time __rb_allocate_pages() is called. Skip si_mem_available() to avoid wrongly failing the allocation under memory pressure.
For the same reason, unset user_thread.
--- kernel/trace/ring_buffer.c | 41 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-)
kernel/trace/ring_buffer.c +21 -20
@@ -2442,20 +2442,30 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,struct trace_buffer *buffer = cpu_buffer->buffer;struct ring_buffer_cpu_meta *meta =NULL ;struct buffer_page *bpage, *tmp; -bool user_thread = current->mm !=NULL ;struct ring_buffer_desc *desc =NULL ; +bool user_thread = false;long i; -/* -* Check if the available memory is there first. -* Note, si_mem_available() only gives us a rough estimate of available -* memory. It may not be accurate. But we don't care, we just want -* to prevent doing any allocation when it is obvious that it is -* not going to succeed. -*/ - i =si_mem_available (); -if (i < nr_pages) -return -ENOMEM ; +if (buffer->range_addr_start) { + meta =rb_range_meta (buffer, nr_pages, cpu_buffer->cpu); + }else if (buffer->remote) { + desc =ring_buffer_desc (buffer->remote->desc, cpu_buffer->cpu); +if (!desc ||WARN_ON (desc->nr_page_va != (nr_pages +1 ))) +return -EINVAL ; + }else { +/* +* Check if the available memory is there first. +* Note, si_mem_available() only gives us a rough estimate of available +* memory. It may not be accurate. But we don't care, we just want +* to prevent doing any allocation when it is obvious that it is +* not going to succeed. +*/ + i =si_mem_available (); +if (i < nr_pages) +return -ENOMEM ; + + user_thread = current->mm !=NULL ; + }/* * If a user thread allocates too much, and si_mem_available()
@@ -2469,15 +2479,6 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,if (user_thread)set_current_oom_origin (); -if (buffer->range_addr_start) - meta =rb_range_meta (buffer, nr_pages, cpu_buffer->cpu); - -if (buffer->remote) { - desc =ring_buffer_desc (buffer->remote->desc, cpu_buffer->cpu); -if (!desc ||WARN_ON (desc->nr_page_va != (nr_pages +1 ))) -return -EINVAL ; - } -for (i =0 ; i < nr_pages; i++) { bpage =alloc_cpu_page (cpu_buffer->cpu);
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers
Quoted 45 lines · On Tue, 13 Oct 2026 11:20:46 +0100 Vincent Donnefort <[email protected]>…
Persistent and remote buffer memory is pre-allocated by the time
__rb_allocate_pages()is called. Skipsi_mem_available()to avoid wrongly failing the allocation under memory pressure.For the same reason, unset
user_thread.Fixes be68d63a139b ("ring-buffer: Add ring_buffer_alloc_range()")Fixes 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")Signed-off-by Vincent Donnefort <[email protected]>--- kernel/trace/ring_buffer.c | 41 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-)kernel/trace/ring_buffer.c +10
-11@@ -2442,20 +2442,30 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,struct trace_buffer *buffer = cpu_buffer->buffer;struct ring_buffer_cpu_meta *meta =NULL ;struct buffer_page *bpage, *tmp; -bool user_thread = current->mm !=NULL ;struct ring_buffer_desc *desc =NULL ; +bool user_thread = false;long i; -/* -* Check if the available memory is there first. -* Note, si_mem_available() only gives us a rough estimate of available -* memory. It may not be accurate. But we don't care, we just want -* to prevent doing any allocation when it is obvious that it is -* not going to succeed. -*/ - i =si_mem_available (); -if (i < nr_pages) -return -ENOMEM ; +if (buffer->range_addr_start) { + meta =rb_range_meta (buffer, nr_pages, cpu_buffer->cpu); + }else if (buffer->remote) { + desc =ring_buffer_desc (buffer->remote->desc, cpu_buffer->cpu); +if (!desc ||WARN_ON (desc->nr_page_va != (nr_pages +1 ))) +return -EINVAL ; + }else { +/* +* Check if the available memory is there first.
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2467
Quoted 10 lines
kernel/trace/ring_buffer.c +10
-0+* Note, si_mem_available() only gives us a rough estimate of available +* memory. It may not be accurate. But we don't care, we just want +* to prevent doing any allocation when it is obvious that it is +* not going to succeed. +*/ + i =si_mem_available (); +if (i < nr_pages) +return -ENOMEM ; + + user_thread = current->mm !=NULL ;
Not caused by this patch but we need to update this to:
user_thread = is_user_task(current);
I may add that.
-- Steve
Quoted 20 lines
kernel/trace/ring_buffer.c +1
-9+ }/* * If a user thread allocates too much, and si_mem_available() @@ -2469,15 +2479,6 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,if (user_thread)set_current_oom_origin (); -if (buffer->range_addr_start) - meta =rb_range_meta (buffer, nr_pages, cpu_buffer->cpu); - -if (buffer->remote) { - desc =ring_buffer_desc (buffer->remote->desc, cpu_buffer->cpu); -if (!desc ||WARN_ON (desc->nr_page_va != (nr_pages +1 ))) -return -EINVAL ; - } -for (i =0 ; i < nr_pages; i++) { bpage =alloc_cpu_page (cpu_buffer->cpu);
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers
Quoted 61 lines · Steven Rostedt
Quoted 45 lines · On Tue, 13 Oct 2026 11:20:46 +0100 Vincent Donnefort <[email protected]>…
Persistent and remote buffer memory is pre-allocated by the time
__rb_allocate_pages()is called. Skipsi_mem_available()to avoid wrongly failing the allocation under memory pressure.For the same reason, unset
user_thread.Fixes be68d63a139b ("ring-buffer: Add ring_buffer_alloc_range()")Fixes 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")Signed-off-by Vincent Donnefort <[email protected]>--- kernel/trace/ring_buffer.c | 41 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-)kernel/trace/ring_buffer.c +10
-11@@ -2442,20 +2442,30 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,struct trace_buffer *buffer = cpu_buffer->buffer;struct ring_buffer_cpu_meta *meta =NULL ;struct buffer_page *bpage, *tmp; -bool user_thread = current->mm !=NULL ;struct ring_buffer_desc *desc =NULL ; +bool user_thread = false;long i; -/* -* Check if the available memory is there first. -* Note, si_mem_available() only gives us a rough estimate of available -* memory. It may not be accurate. But we don't care, we just want -* to prevent doing any allocation when it is obvious that it is -* not going to succeed. -*/ - i =si_mem_available (); -if (i < nr_pages) -return -ENOMEM ; +if (buffer->range_addr_start) { + meta =rb_range_meta (buffer, nr_pages, cpu_buffer->cpu); + }else if (buffer->remote) { + desc =ring_buffer_desc (buffer->remote->desc, cpu_buffer->cpu); +if (!desc ||WARN_ON (desc->nr_page_va != (nr_pages +1 ))) +return -EINVAL ; + }else { +/* +* Check if the available memory is there first. kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2467
Quoted 10 lines
kernel/trace/ring_buffer.c +10
-0+* Note, si_mem_available() only gives us a rough estimate of available +* memory. It may not be accurate. But we don't care, we just want +* to prevent doing any allocation when it is obvious that it is +* not going to succeed. +*/ + i =si_mem_available (); +if (i < nr_pages) +return -ENOMEM ; + + user_thread = current->mm !=NULL ;Not caused by this patch but we need to update this to:
Quoted 3 lines
user_thread = is_user_task(current);I may add that.
I can send a v3 if that helps.
Signature · 1 line
Vincent
Quoted 24 lines
-- Steve
Quoted 20 lines
kernel/trace/ring_buffer.c +1
-9+ }/* * If a user thread allocates too much, and si_mem_available() @@ -2469,15 +2479,6 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,if (user_thread)set_current_oom_origin (); -if (buffer->range_addr_start) - meta =rb_range_meta (buffer, nr_pages, cpu_buffer->cpu); - -if (buffer->remote) { - desc =ring_buffer_desc (buffer->remote->desc, cpu_buffer->cpu); -if (!desc ||WARN_ON (desc->nr_page_va != (nr_pages +1 ))) -return -EINVAL ; - } -for (i =0 ; i < nr_pages; i++) { bpage =alloc_cpu_page (cpu_buffer->cpu);
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers
Quoted 8 lines · On Thu, 15 Oct 2026 09:39:31 +0100 Vincent Donnefort <[email protected]>…
Quoted 5 lines
Not caused by this patch but we need to update this to:
user_thread = is_user_task(current);I may add that.
I can send a v3 if that helps.
I already pulled in the other two patches and tested them.
This can be a clean up for the merge window.
-- Steve
Vincent Donnefort
maintainerPATCH v2 2/2 ring-buffer: Use subbuf_order in available memory estimation
In reply to Vincent DonnefortTo avoid failing in the middle of an allocation of a ring-buffer, __rb_allocate_pages() looks at the available memory estimation with si_mem_available(). Use the subbuffer order with this estimation to fail early if necessary for ring-buffers with an order > 0.
--- kernel/trace/ring_buffer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/ring_buffer.c +4 -4
@@ -2440,6 +2440,7 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages, struct list_head *pages) { struct trace_buffer *buffer = cpu_buffer->buffer; +unsigned int subbuf_order = buffer->subbuf_order;struct ring_buffer_cpu_meta *meta =NULL ;struct buffer_page *bpage, *tmp;struct ring_buffer_desc *desc =NULL ;
@@ -2461,7 +2462,7 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* not going to succeed. */ i =si_mem_available (); -if (i < nr_pages) +if (i < (nr_pages << subbuf_order))return -ENOMEM ; user_thread = current->mm !=NULL ;
@@ -2514,12 +2515,11 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, bpage->id = i +1 ; cpu_buffer->subbuf_ids[i +1 ] = bpage; }else { - bpage->page =alloc_cpu_data (cpu_buffer->cpu, - cpu_buffer->buffer->subbuf_order); + bpage->page =alloc_cpu_data (cpu_buffer->cpu, subbuf_order);if (!bpage->page) goto free_pages; } - bpage->order = cpu_buffer->buffer->subbuf_order; + bpage->order = subbuf_order;if (user_thread &&fatal_signal_pending (current)) goto free_pages;
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Re: [PATCH v2 2/2] ring-buffer: Use subbuf_order in available memory estimation
Quoted 7 lines · Vincent Donnefort
To avoid failing in the middle of an allocation of a ring-buffer,
__rb_allocate_pages()looks at the available memory estimation withsi_mem_available(). Use the subbuffer order with this estimation to fail early if necessary for ring-buffers with an order > 0.Fixes f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")Signed-off-by Vincent Donnefort <[email protected]>
Looks good to me.
Thanks,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>