* [PATCH v2 0/2] ring-buffer: Fixes for available memory estimation
@ 2026-10-13 10:20 Vincent Donnefort
2026-10-13 10:20 ` [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers Vincent Donnefort
2026-10-13 10:20 ` [PATCH v2 2/2] ring-buffer: Use subbuf_order in available memory estimation Vincent Donnefort
0 siblings, 2 replies; 5+ messages in thread
From: Vincent Donnefort @ 2026-10-13 10:20 UTC (permalink / raw)
To: rostedt, mhiramat, linux-trace-kernel
Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort
__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/20261012163502.687061-1-vdonnefort@google.com/
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
--
2.56.0.385.gd3acb90ef8-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers
2026-10-13 10:20 [PATCH v2 0/2] ring-buffer: Fixes for available memory estimation Vincent Donnefort
@ 2026-10-13 10:20 ` Vincent Donnefort
2026-10-14 21:51 ` Steven Rostedt
2026-10-13 10:20 ` [PATCH v2 2/2] ring-buffer: Use subbuf_order in available memory estimation Vincent Donnefort
1 sibling, 1 reply; 5+ messages in thread
From: Vincent Donnefort @ 2026-10-13 10:20 UTC (permalink / raw)
To: rostedt, mhiramat, linux-trace-kernel
Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort
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.
Fixes: be68d63a139b ("ring-buffer: Add ring_buffer_alloc_range()")
Fixes: 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
kernel/trace/ring_buffer.c | 41 +++++++++++++++++++-------------------
1 file changed, 21 insertions(+), 20 deletions(-)
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 04bb94c29f58..b3a30a0413a5 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -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);
--
2.56.0.385.gd3acb90ef8-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] ring-buffer: Use subbuf_order in available memory estimation
2026-10-13 10:20 [PATCH v2 0/2] ring-buffer: Fixes for available memory estimation Vincent Donnefort
2026-10-13 10:20 ` [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers Vincent Donnefort
@ 2026-10-13 10:20 ` Vincent Donnefort
2026-10-14 23:22 ` Masami Hiramatsu
1 sibling, 1 reply; 5+ messages in thread
From: Vincent Donnefort @ 2026-10-13 10:20 UTC (permalink / raw)
To: rostedt, mhiramat, linux-trace-kernel
Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort
To 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.
Fixes: f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
kernel/trace/ring_buffer.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index b3a30a0413a5..63001ab1eef2 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -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;
--
2.56.0.385.gd3acb90ef8-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers
2026-10-13 10:20 ` [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers Vincent Donnefort
@ 2026-10-14 21:51 ` Steven Rostedt
0 siblings, 0 replies; 5+ messages in thread
From: Steven Rostedt @ 2026-10-14 21:51 UTC (permalink / raw)
To: Vincent Donnefort
Cc: mhiramat, linux-trace-kernel, mathieu.desnoyers, kernel-team,
linux-kernel
On Tue, 13 Oct 2026 11:20:46 +0100
Vincent Donnefort <vdonnefort@google.com> wrote:
> 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.
>
> Fixes: be68d63a139b ("ring-buffer: Add ring_buffer_alloc_range()")
> Fixes: 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> ---
> kernel/trace/ring_buffer.c | 41 +++++++++++++++++++-------------------
> 1 file changed, 21 insertions(+), 20 deletions(-)
>
> diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> index 04bb94c29f58..b3a30a0413a5 100644
> --- a/kernel/trace/ring_buffer.c
> +++ b/kernel/trace/ring_buffer.c
> @@ -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;
Not caused by this patch but we need to update this to:
user_thread = is_user_task(current);
I may add that.
-- Steve
> + }
>
> /*
> * 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);
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] ring-buffer: Use subbuf_order in available memory estimation
2026-10-13 10:20 ` [PATCH v2 2/2] ring-buffer: Use subbuf_order in available memory estimation Vincent Donnefort
@ 2026-10-14 23:22 ` Masami Hiramatsu
0 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu @ 2026-10-14 23:22 UTC (permalink / raw)
To: Vincent Donnefort
Cc: rostedt, linux-trace-kernel, mathieu.desnoyers, kernel-team,
linux-kernel
On Tue, 13 Oct 2026 11:20:47 +0100, Vincent Donnefort <vdonnefort@google.com> wrote:
> To 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.
>
> Fixes: f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Looks good to me.
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Thanks,
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-10-14 23:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-13 10:20 [PATCH v2 0/2] ring-buffer: Fixes for available memory estimation Vincent Donnefort
2026-10-13 10:20 ` [PATCH v2 1/2] ring-buffer: Skip si_mem_available() for persistent and remote buffers Vincent Donnefort
2026-10-14 21:51 ` Steven Rostedt
2026-10-13 10:20 ` [PATCH v2 2/2] ring-buffer: Use subbuf_order in available memory estimation Vincent Donnefort
2026-10-14 23:22 ` Masami Hiramatsu
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®