Compare

0fa993581aaa58a34ae3dcebebe0fbf593fcb441..13d530f2daeda9493f4d07ddc5ca13e21318fbeb

ring-buffer: Use subbuf_order in available memory estimation

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.

Link: https://patch.msgid.link/20261013102047.1488347-3-vdonnefort@google.com
Fixes: f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>

Vincent Donnefort <vdonnefort@google.com> · 2026-10-13 10:20 UTC · committed by Steven Rostedt

Browse files
5 files changed, +45 −31
modified fs/tracefs/event_inode.c
@@ -91,6 +91,14 @@ static void free_ei_rcu(struct rcu_head *rcu)
91 91 }
92 92 }
93 93
94 + /* read under eventfs_srcu and, by tracefs_d_revalidate(), in rcu pathwalk */
95 + static void free_ei_srcu(struct rcu_head *rcu)
96 + {
97 + struct eventfs_inode *ei = container_of(rcu, struct eventfs_inode, rcu);
98 +
99 + call_rcu(&ei->rcu, free_ei_rcu);
100 + }
101 +
94 102 /*
95 103 * eventfs_inode reference count management.
96 104 *
@@ -112,7 +120,7 @@ static void release_ei(struct kref *ref)
112 120 entry->release(entry->name, ei->data);
113 121 }
114 122
115 - call_srcu(&eventfs_srcu, &ei->rcu, free_ei_rcu);
123 + call_srcu(&eventfs_srcu, &ei->rcu, free_ei_srcu);
116 124 }
117 125
118 126 static inline void put_ei(struct eventfs_inode *ei)
modified kernel/trace/ring_buffer.c
@@ -2443,22 +2443,33 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
2443 2443 unsigned long nr_pages, struct list_head *pages)
2444 2444 {
2445 2445 struct trace_buffer *buffer = cpu_buffer->buffer;
2446 + unsigned int subbuf_order = buffer->subbuf_order;
2446 2447 struct ring_buffer_cpu_meta *meta = NULL;
2447 2448 struct buffer_page *bpage, *tmp;
2448 - bool user_thread = current->mm != NULL;
2449 2449 struct ring_buffer_desc *desc = NULL;
2450 + bool user_thread = false;
2450 2451 long i;
2451 2452
2452 - /*
2453 - * Check if the available memory is there first.
2454 - * Note, si_mem_available() only gives us a rough estimate of available
2455 - * memory. It may not be accurate. But we don't care, we just want
2456 - * to prevent doing any allocation when it is obvious that it is
2457 - * not going to succeed.
2458 - */
2459 - i = si_mem_available();
2460 - if (i < nr_pages)
2461 - return -ENOMEM;
2453 + if (buffer->range_addr_start) {
2454 + meta = rb_range_meta(buffer, nr_pages, cpu_buffer->cpu);
2455 + } else if (buffer->remote) {
2456 + desc = ring_buffer_desc(buffer->remote->desc, cpu_buffer->cpu);
2457 + if (!desc || WARN_ON(desc->nr_page_va != (nr_pages + 1)))
2458 + return -EINVAL;
2459 + } else {
2460 + /*
2461 + * Check if the available memory is there first.
2462 + * Note, si_mem_available() only gives us a rough estimate of available
2463 + * memory. It may not be accurate. But we don't care, we just want
2464 + * to prevent doing any allocation when it is obvious that it is
2465 + * not going to succeed.
2466 + */
2467 + i = si_mem_available();
2468 + if (i < (nr_pages << subbuf_order))
2469 + return -ENOMEM;
2470 +
2471 + user_thread = current->mm != NULL;
2472 + }
2462 2473
2463 2474 /*
2464 2475 * If a user thread allocates too much, and si_mem_available()
@@ -2472,15 +2483,6 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
2472 2483 if (user_thread)
2473 2484 set_current_oom_origin();
2474 2485
2475 - if (buffer->range_addr_start)
2476 - meta = rb_range_meta(buffer, nr_pages, cpu_buffer->cpu);
2477 -
2478 - if (buffer->remote) {
2479 - desc = ring_buffer_desc(buffer->remote->desc, cpu_buffer->cpu);
2480 - if (!desc || WARN_ON(desc->nr_page_va != (nr_pages + 1)))
2481 - return -EINVAL;
2482 - }
2483 -
2484 2486 for (i = 0; i < nr_pages; i++) {
2485 2487
2486 2488 bpage = alloc_cpu_page(cpu_buffer->cpu);
@@ -2516,12 +2518,11 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
2516 2518 bpage->id = i + 1;
2517 2519 cpu_buffer->subbuf_ids[i + 1] = bpage;
2518 2520 } else {
2519 - bpage->page = alloc_cpu_data(cpu_buffer->cpu,
2520 - cpu_buffer->buffer->subbuf_order);
2521 + bpage->page = alloc_cpu_data(cpu_buffer->cpu, subbuf_order);
2521 2522 if (!bpage->page)
2522 2523 goto free_pages;
2523 2524 }
2524 - bpage->order = cpu_buffer->buffer->subbuf_order;
2525 + bpage->order = subbuf_order;
2525 2526
2526 2527 if (user_thread && fatal_signal_pending(current))
2527 2528 goto free_pages;
@@ -7998,12 +7999,8 @@ consume:
7998 7999 goto out;
7999 8000 }
8000 8001
8001 - /* Did the reader catch up with the writer? */
8002 - if (cpu_buffer->reader_page == cpu_buffer->commit_page)
8003 - goto out;
8004 -
8005 8002 reader = rb_get_reader_page(cpu_buffer);
8006 - if (WARN_ON(!reader))
8003 + if (!reader)
8007 8004 goto out;
8008 8005
8009 8006 /* Check if any events were dropped */
modified kernel/trace/trace.c
@@ -9287,6 +9287,12 @@ static struct notifier_block trace_module_nb = {
9287 9287
9288 9288 static __init void tracer_init_tracefs_work_func(struct work_struct *work)
9289 9289 {
9290 + /*
9291 + * Do not attempt to initialize tracefs if tracing was disabled or
9292 + * failed to allocate
9293 + */
9294 + if (tracing_disabled)
9295 + return;
9290 9296
9291 9297 event_trace_init();
9292 9298
modified kernel/trace/trace_events_hist.c
@@ -873,7 +873,7 @@ static u64 hist_field_timestamp(struct hist_field *hist_field,
873 873 struct hist_trigger_data *hist_data = hist_field->hist_data;
874 874 struct trace_array *tr = hist_data->event_file->tr;
875 875
876 - u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
876 + u64 ts = tracing_event_time_stamp(buffer, rbe);
877 877
878 878 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
879 879 ts = ns2usecs(ts);
modified kernel/trace/trace_snapshot.c
@@ -675,9 +675,12 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
675 675 if (iter->cpu_file != RING_BUFFER_ALL_CPUS)
676 676 return -EINVAL;
677 677 #endif
678 - if (tr->allocated_snapshot)
678 + if (tr->allocated_snapshot) {
679 679 ret = resize_buffer_duplicate_size(&tr->snapshot_buffer,
680 680 &tr->array_buffer, iter->cpu_file);
681 + if (ret < 0)
682 + return ret;
683 + }
681 684
682 685 ret = tracing_arm_snapshot_locked(tr);
683 686 if (ret)