Compare

89bab1dba8a0167ef0d819e0f55b6bafa038e87f..c9fc60c18b6d42efdea0cd88ca132b2f452157c3

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.

Link: https://patch.msgid.link/20261013102047.1488347-2-vdonnefort@google.com
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>
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, +41 −23
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
@@ -2445,20 +2445,30 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
2445 2445 struct trace_buffer *buffer = cpu_buffer->buffer;
2446 2446 struct ring_buffer_cpu_meta *meta = NULL;
2447 2447 struct buffer_page *bpage, *tmp;
2448 - bool user_thread = current->mm != NULL;
2449 2448 struct ring_buffer_desc *desc = NULL;
2449 + bool user_thread = false;
2450 2450 long i;
2451 2451
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;
2452 + if (buffer->range_addr_start) {
2453 + meta = rb_range_meta(buffer, nr_pages, cpu_buffer->cpu);
2454 + } else if (buffer->remote) {
2455 + desc = ring_buffer_desc(buffer->remote->desc, cpu_buffer->cpu);
2456 + if (!desc || WARN_ON(desc->nr_page_va != (nr_pages + 1)))
2457 + return -EINVAL;
2458 + } else {
2459 + /*
2460 + * Check if the available memory is there first.
2461 + * Note, si_mem_available() only gives us a rough estimate of available
2462 + * memory. It may not be accurate. But we don't care, we just want
2463 + * to prevent doing any allocation when it is obvious that it is
2464 + * not going to succeed.
2465 + */
2466 + i = si_mem_available();
2467 + if (i < nr_pages)
2468 + return -ENOMEM;
2469 +
2470 + user_thread = current->mm != NULL;
2471 + }
2462 2472
2463 2473 /*
2464 2474 * If a user thread allocates too much, and si_mem_available()
@@ -2472,15 +2482,6 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
2472 2482 if (user_thread)
2473 2483 set_current_oom_origin();
2474 2484
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 2485 for (i = 0; i < nr_pages; i++) {
2485 2486
2486 2487 bpage = alloc_cpu_page(cpu_buffer->cpu);
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)