1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
3 * ring_buffer_frontend.c
5 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
8 * recorder (overwrite) modes. See thesis:
10 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
11 * dissertation, Ecole Polytechnique de Montreal.
12 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
14 * - Algorithm presentation in Chapter 5:
15 * "Lockless Multi-Core High-Throughput Buffering".
16 * - Algorithm formal verification in Section 8.6:
17 * "Formal verification of LTTng"
20 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
22 * Inspired from LTT and RelayFS:
23 * Karim Yaghmour <karim@opersys.com>
24 * Tom Zanussi <zanussi@us.ibm.com>
25 * Bob Wisniewski <bob@watson.ibm.com>
27 * Bob Wisniewski <bob@watson.ibm.com>
29 * Buffer reader semantic :
32 * while buffer is not finalized and empty
34 * - if return value != 0, continue
35 * - splice one subbuffer worth of data to a pipe
36 * - splice the data from pipe to disk/network
40 #include <linux/atomic.h>
41 #include <linux/delay.h>
42 #include <linux/module.h>
43 #include <linux/percpu.h>
44 #include <linux/percpu-defs.h>
45 #include <asm/cacheflush.h>
47 #include <ringbuffer/config.h>
48 #include <ringbuffer/backend.h>
49 #include <ringbuffer/frontend.h>
50 #include <ringbuffer/iterator.h>
51 #include <ringbuffer/nohz.h>
52 #include <wrapper/cpu.h>
53 #include <wrapper/kref.h>
54 #include <wrapper/timer.h>
55 #include <wrapper/vmalloc.h>
58 * Internal structure representing offsets to use at a sub-buffer switch.
60 struct switch_offsets
{
61 unsigned long begin
, end
, old
;
62 size_t pre_header_padding
, size
;
63 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
74 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
75 #endif /* CONFIG_NO_HZ */
77 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
79 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
80 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
83 void lib_ring_buffer_print_errors(struct lttng_kernel_ring_buffer_channel
*chan
,
84 struct lttng_kernel_ring_buffer
*buf
, int cpu
);
86 void _lib_ring_buffer_switch_remote(struct lttng_kernel_ring_buffer
*buf
,
87 enum switch_mode mode
);
90 int lib_ring_buffer_poll_deliver(const struct lttng_kernel_ring_buffer_config
*config
,
91 struct lttng_kernel_ring_buffer
*buf
,
92 struct lttng_kernel_ring_buffer_channel
*chan
)
94 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
96 consumed_old
= atomic_long_read(&buf
->consumed
);
97 consumed_idx
= subbuf_index(consumed_old
, chan
);
98 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
100 * No memory barrier here, since we are only interested
101 * in a statistically correct polling result. The next poll will
102 * get the data is we are racing. The mb() that ensures correct
103 * memory order is in get_subbuf.
105 write_offset
= v_read(config
, &buf
->offset
);
108 * Check that the subbuffer we are trying to consume has been
109 * already fully committed.
112 if (((commit_count
- chan
->backend
.subbuf_size
)
113 & chan
->commit_count_mask
)
114 - (buf_trunc(consumed_old
, chan
)
115 >> chan
->backend
.num_subbuf_order
)
120 * Check that we are not about to read the same subbuffer in
121 * which the writer head is.
123 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
131 * Must be called under cpu hotplug protection.
133 void lib_ring_buffer_free(struct lttng_kernel_ring_buffer
*buf
)
135 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
137 irq_work_sync(&buf
->wakeup_pending
);
139 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
140 lttng_kvfree(buf
->commit_hot
);
141 lttng_kvfree(buf
->commit_cold
);
142 lttng_kvfree(buf
->ts_end
);
144 lib_ring_buffer_backend_free(&buf
->backend
);
148 * lib_ring_buffer_reset - Reset ring buffer to initial values.
151 * Effectively empty the ring buffer. Should be called when the buffer is not
152 * used for writing. The ring buffer can be opened for reading, but the reader
153 * should not be using the iterator concurrently with reset. The previous
154 * current iterator record is reset.
156 void lib_ring_buffer_reset(struct lttng_kernel_ring_buffer
*buf
)
158 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
159 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
163 * Reset iterator first. It will put the subbuffer if it currently holds
166 lib_ring_buffer_iterator_reset(buf
);
167 v_set(config
, &buf
->offset
, 0);
168 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
169 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
170 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
171 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
174 atomic_long_set(&buf
->consumed
, 0);
175 atomic_set(&buf
->record_disabled
, 0);
176 v_set(config
, &buf
->last_tsc
, 0);
177 lib_ring_buffer_backend_reset(&buf
->backend
);
178 /* Don't reset number of active readers */
179 v_set(config
, &buf
->records_lost_full
, 0);
180 v_set(config
, &buf
->records_lost_wrap
, 0);
181 v_set(config
, &buf
->records_lost_big
, 0);
182 v_set(config
, &buf
->records_count
, 0);
183 v_set(config
, &buf
->records_overrun
, 0);
186 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
189 * channel_reset - Reset channel to initial values.
192 * Effectively empty the channel. Should be called when the channel is not used
193 * for writing. The channel can be opened for reading, but the reader should not
194 * be using the iterator concurrently with reset. The previous current iterator
197 void channel_reset(struct lttng_kernel_ring_buffer_channel
*chan
)
200 * Reset iterators first. Will put the subbuffer if held for reading.
202 channel_iterator_reset(chan
);
203 atomic_set(&chan
->record_disabled
, 0);
204 /* Don't reset commit_count_mask, still valid */
205 channel_backend_reset(&chan
->backend
);
206 /* Don't reset switch/read timer interval */
207 /* Don't reset notifiers and notifier enable bits */
208 /* Don't reset reader reference count */
210 EXPORT_SYMBOL_GPL(channel_reset
);
212 static void lib_ring_buffer_pending_wakeup_buf(struct irq_work
*entry
)
214 struct lttng_kernel_ring_buffer
*buf
= container_of(entry
, struct lttng_kernel_ring_buffer
,
216 wake_up_interruptible(&buf
->read_wait
);
219 static void lib_ring_buffer_pending_wakeup_chan(struct irq_work
*entry
)
221 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(entry
, struct lttng_kernel_ring_buffer_channel
, wakeup_pending
);
222 wake_up_interruptible(&chan
->read_wait
);
226 * Must be called under cpu hotplug protection.
228 int lib_ring_buffer_create(struct lttng_kernel_ring_buffer
*buf
,
229 struct channel_backend
*chanb
, int cpu
)
231 const struct lttng_kernel_ring_buffer_config
*config
= &chanb
->config
;
232 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(chanb
, struct lttng_kernel_ring_buffer_channel
, backend
);
233 void *priv
= chanb
->priv
;
234 size_t subbuf_header_size
;
238 /* Test for cpu hotplug */
239 if (buf
->backend
.allocated
)
243 * Paranoia: per cpu dynamic allocation is not officially documented as
244 * zeroing the memory, so let's do it here too, just in case.
246 memset(buf
, 0, sizeof(*buf
));
248 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
253 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
254 * chan
->backend
.num_subbuf
,
255 1 << INTERNODE_CACHE_SHIFT
),
256 GFP_KERNEL
| __GFP_NOWARN
,
257 cpu_to_node(max(cpu
, 0)));
258 if (!buf
->commit_hot
) {
264 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
265 * chan
->backend
.num_subbuf
,
266 1 << INTERNODE_CACHE_SHIFT
),
267 GFP_KERNEL
| __GFP_NOWARN
,
268 cpu_to_node(max(cpu
, 0)));
269 if (!buf
->commit_cold
) {
275 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
276 * chan
->backend
.num_subbuf
,
277 1 << INTERNODE_CACHE_SHIFT
),
278 GFP_KERNEL
| __GFP_NOWARN
,
279 cpu_to_node(max(cpu
, 0)));
282 goto free_commit_cold
;
285 init_waitqueue_head(&buf
->read_wait
);
286 init_waitqueue_head(&buf
->write_wait
);
287 init_irq_work(&buf
->wakeup_pending
, lib_ring_buffer_pending_wakeup_buf
);
288 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
291 * Write the subbuffer header for first subbuffer so we know the total
292 * duration of data gathering.
294 subbuf_header_size
= config
->cb
.subbuffer_header_size();
295 v_set(config
, &buf
->offset
, subbuf_header_size
);
296 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
297 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
298 config
->cb
.buffer_begin(buf
, tsc
, 0);
299 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
301 if (config
->cb
.buffer_create
) {
302 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
308 * Ensure the buffer is ready before setting it to allocated and setting
310 * Used for cpu hotplug vs cpumask iteration.
313 buf
->backend
.allocated
= 1;
315 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
316 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
317 chan
->backend
.cpumask
));
318 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
325 lttng_kvfree(buf
->ts_end
);
327 lttng_kvfree(buf
->commit_cold
);
329 lttng_kvfree(buf
->commit_hot
);
331 lib_ring_buffer_backend_free(&buf
->backend
);
335 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
337 struct lttng_kernel_ring_buffer
*buf
= lttng_from_timer(buf
, t
, switch_timer
);
338 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
339 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
342 * Only flush buffers periodically if readers are active.
344 if (atomic_long_read(&buf
->active_readers
))
345 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
347 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
348 lttng_mod_timer_pinned(&buf
->switch_timer
,
349 jiffies
+ chan
->switch_timer_interval
);
351 mod_timer(&buf
->switch_timer
,
352 jiffies
+ chan
->switch_timer_interval
);
356 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
358 static void lib_ring_buffer_start_switch_timer(struct lttng_kernel_ring_buffer
*buf
)
360 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
361 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
362 unsigned int flags
= 0;
364 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
367 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
368 flags
= LTTNG_TIMER_PINNED
;
370 lttng_timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
, buf
);
371 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
373 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
374 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
376 add_timer(&buf
->switch_timer
);
378 buf
->switch_timer_enabled
= 1;
382 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
384 static void lib_ring_buffer_stop_switch_timer(struct lttng_kernel_ring_buffer
*buf
)
386 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
388 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
391 del_timer_sync(&buf
->switch_timer
);
392 buf
->switch_timer_enabled
= 0;
396 * Polling timer to check the channels for data.
398 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
400 struct lttng_kernel_ring_buffer
*buf
= lttng_from_timer(buf
, t
, read_timer
);
401 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
402 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
404 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
406 if (atomic_long_read(&buf
->active_readers
)
407 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
408 wake_up_interruptible(&buf
->read_wait
);
409 wake_up_interruptible(&chan
->read_wait
);
412 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
413 lttng_mod_timer_pinned(&buf
->read_timer
,
414 jiffies
+ chan
->read_timer_interval
);
416 mod_timer(&buf
->read_timer
,
417 jiffies
+ chan
->read_timer_interval
);
421 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
423 static void lib_ring_buffer_start_read_timer(struct lttng_kernel_ring_buffer
*buf
)
425 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
426 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
427 unsigned int flags
= 0;
429 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
430 || !chan
->read_timer_interval
431 || buf
->read_timer_enabled
)
434 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
435 flags
= LTTNG_TIMER_PINNED
;
437 lttng_timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
, buf
);
438 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
440 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
441 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
443 add_timer(&buf
->read_timer
);
445 buf
->read_timer_enabled
= 1;
449 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
451 static void lib_ring_buffer_stop_read_timer(struct lttng_kernel_ring_buffer
*buf
)
453 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
454 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
456 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
457 || !chan
->read_timer_interval
458 || !buf
->read_timer_enabled
)
461 del_timer_sync(&buf
->read_timer
);
463 * do one more check to catch data that has been written in the last
466 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
467 wake_up_interruptible(&buf
->read_wait
);
468 wake_up_interruptible(&chan
->read_wait
);
470 buf
->read_timer_enabled
= 0;
473 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
475 enum cpuhp_state lttng_rb_hp_prepare
;
476 enum cpuhp_state lttng_rb_hp_online
;
478 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
480 lttng_rb_hp_prepare
= val
;
482 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
484 void lttng_rb_set_hp_online(enum cpuhp_state val
)
486 lttng_rb_hp_online
= val
;
488 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
490 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
491 struct lttng_cpuhp_node
*node
)
493 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(node
, struct lttng_kernel_ring_buffer_channel
,
495 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
496 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
498 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
501 * Performing a buffer switch on a remote CPU. Performed by
502 * the CPU responsible for doing the hotunplug after the target
503 * CPU stopped running completely. Ensures that all data
504 * from that remote CPU is flushed.
506 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
509 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
511 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
512 struct lttng_cpuhp_node
*node
)
514 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(node
, struct lttng_kernel_ring_buffer_channel
,
516 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
517 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
519 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
521 wake_up_interruptible(&chan
->hp_wait
);
522 lib_ring_buffer_start_switch_timer(buf
);
523 lib_ring_buffer_start_read_timer(buf
);
526 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
528 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
529 struct lttng_cpuhp_node
*node
)
531 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(node
, struct lttng_kernel_ring_buffer_channel
,
533 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
534 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
536 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
538 lib_ring_buffer_stop_switch_timer(buf
);
539 lib_ring_buffer_stop_read_timer(buf
);
542 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
544 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
546 #ifdef CONFIG_HOTPLUG_CPU
549 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
550 * @nb: notifier block
551 * @action: hotplug action to take
554 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
557 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
558 unsigned long action
,
561 unsigned int cpu
= (unsigned long)hcpu
;
562 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(nb
, struct lttng_kernel_ring_buffer_channel
,
564 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
565 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
567 if (!chan
->cpu_hp_enable
)
570 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
573 case CPU_DOWN_FAILED
:
574 case CPU_DOWN_FAILED_FROZEN
:
576 case CPU_ONLINE_FROZEN
:
577 wake_up_interruptible(&chan
->hp_wait
);
578 lib_ring_buffer_start_switch_timer(buf
);
579 lib_ring_buffer_start_read_timer(buf
);
582 case CPU_DOWN_PREPARE
:
583 case CPU_DOWN_PREPARE_FROZEN
:
584 lib_ring_buffer_stop_switch_timer(buf
);
585 lib_ring_buffer_stop_read_timer(buf
);
589 case CPU_DEAD_FROZEN
:
591 * Performing a buffer switch on a remote CPU. Performed by
592 * the CPU responsible for doing the hotunplug after the target
593 * CPU stopped running completely. Ensures that all data
594 * from that remote CPU is flushed.
596 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
606 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
608 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
610 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
611 * that wake-up-tracing generated events are flushed before going idle (in
612 * tick_nohz). We test if the spinlock is locked to deal with the race where
613 * readers try to sample the ring buffer before we perform the switch. We let
614 * the readers retry in that case. If there is data in the buffer, the wake up
615 * is going to forbid the CPU running the reader thread from going idle.
617 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
621 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(nb
, struct lttng_kernel_ring_buffer_channel
,
623 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
624 struct lttng_kernel_ring_buffer
*buf
;
625 int cpu
= smp_processor_id();
627 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
629 * We don't support keeping the system idle with global buffers
630 * and streaming active. In order to do so, we would need to
631 * sample a non-nohz-cpumask racelessly with the nohz updates
632 * without adding synchronization overhead to nohz. Leave this
633 * use-case out for now.
638 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
640 case TICK_NOHZ_FLUSH
:
641 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
642 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
643 && chan
->read_timer_interval
644 && atomic_long_read(&buf
->active_readers
)
645 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
646 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
647 wake_up_interruptible(&buf
->read_wait
);
648 wake_up_interruptible(&chan
->read_wait
);
650 if (chan
->switch_timer_interval
)
651 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
652 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
655 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
656 lib_ring_buffer_stop_switch_timer(buf
);
657 lib_ring_buffer_stop_read_timer(buf
);
658 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
660 case TICK_NOHZ_RESTART
:
661 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
662 lib_ring_buffer_start_read_timer(buf
);
663 lib_ring_buffer_start_switch_timer(buf
);
664 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
671 void notrace
lib_ring_buffer_tick_nohz_flush(void)
673 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
677 void notrace
lib_ring_buffer_tick_nohz_stop(void)
679 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
683 void notrace
lib_ring_buffer_tick_nohz_restart(void)
685 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
688 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
693 static void channel_unregister_notifiers(struct lttng_kernel_ring_buffer_channel
*chan
)
695 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
697 channel_iterator_unregister_notifiers(chan
);
698 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
701 * Remove the nohz notifier first, so we are certain we stop
704 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
705 &chan
->tick_nohz_notifier
);
707 * ring_buffer_nohz_lock will not be needed below, because
708 * we just removed the notifiers, which were the only source of
711 #endif /* CONFIG_NO_HZ */
712 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
716 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
717 &chan
->cpuhp_online
.node
);
719 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
720 &chan
->cpuhp_prepare
.node
);
723 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
727 #ifdef CONFIG_HOTPLUG_CPU
728 lttng_cpus_read_lock();
729 chan
->cpu_hp_enable
= 0;
730 for_each_online_cpu(cpu
) {
731 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
733 lib_ring_buffer_stop_switch_timer(buf
);
734 lib_ring_buffer_stop_read_timer(buf
);
736 lttng_cpus_read_unlock();
737 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
739 for_each_possible_cpu(cpu
) {
740 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
742 lib_ring_buffer_stop_switch_timer(buf
);
743 lib_ring_buffer_stop_read_timer(buf
);
747 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
749 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
751 lib_ring_buffer_stop_switch_timer(buf
);
752 lib_ring_buffer_stop_read_timer(buf
);
754 channel_backend_unregister_notifiers(&chan
->backend
);
757 static void lib_ring_buffer_set_quiescent(struct lttng_kernel_ring_buffer
*buf
)
759 if (!buf
->quiescent
) {
760 buf
->quiescent
= true;
761 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
765 static void lib_ring_buffer_clear_quiescent(struct lttng_kernel_ring_buffer
*buf
)
767 buf
->quiescent
= false;
770 void lib_ring_buffer_set_quiescent_channel(struct lttng_kernel_ring_buffer_channel
*chan
)
773 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
775 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
776 lttng_cpus_read_lock();
777 for_each_channel_cpu(cpu
, chan
) {
778 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
781 lib_ring_buffer_set_quiescent(buf
);
783 lttng_cpus_read_unlock();
785 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
787 lib_ring_buffer_set_quiescent(buf
);
790 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
792 void lib_ring_buffer_clear_quiescent_channel(struct lttng_kernel_ring_buffer_channel
*chan
)
795 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
797 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
798 lttng_cpus_read_lock();
799 for_each_channel_cpu(cpu
, chan
) {
800 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
803 lib_ring_buffer_clear_quiescent(buf
);
805 lttng_cpus_read_unlock();
807 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
809 lib_ring_buffer_clear_quiescent(buf
);
812 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
814 static void channel_free(struct lttng_kernel_ring_buffer_channel
*chan
)
816 if (chan
->backend
.release_priv_ops
) {
817 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
819 channel_iterator_free(chan
);
820 channel_backend_free(&chan
->backend
);
825 * channel_create - Create channel.
826 * @config: ring buffer instance configuration
827 * @name: name of the channel
828 * @priv: ring buffer client private data
829 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
830 * address mapping. It is used only by RING_BUFFER_STATIC
831 * configuration. It can be set to NULL for other backends.
832 * @subbuf_size: subbuffer size
833 * @num_subbuf: number of subbuffers
834 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
835 * padding to let readers get those sub-buffers.
836 * Used for live streaming.
837 * @read_timer_interval: Time interval (in us) to wake up pending readers.
840 * Returns NULL on failure.
842 struct lttng_kernel_ring_buffer_channel
*channel_create(const struct lttng_kernel_ring_buffer_config
*config
,
843 const char *name
, void *priv
, void *buf_addr
,
845 size_t num_subbuf
, unsigned int switch_timer_interval
,
846 unsigned int read_timer_interval
)
849 struct lttng_kernel_ring_buffer_channel
*chan
;
851 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
852 read_timer_interval
))
855 chan
= kzalloc(sizeof(struct lttng_kernel_ring_buffer_channel
), GFP_KERNEL
);
859 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
860 subbuf_size
, num_subbuf
);
864 ret
= channel_iterator_init(chan
);
866 goto error_free_backend
;
868 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
869 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
870 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
871 kref_init(&chan
->ref
);
872 init_waitqueue_head(&chan
->read_wait
);
873 init_waitqueue_head(&chan
->hp_wait
);
874 init_irq_work(&chan
->wakeup_pending
, lib_ring_buffer_pending_wakeup_chan
);
876 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
877 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
878 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
879 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
880 &chan
->cpuhp_prepare
.node
);
882 goto cpuhp_prepare_error
;
884 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
885 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
886 &chan
->cpuhp_online
.node
);
888 goto cpuhp_online_error
;
889 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
893 * In case of non-hotplug cpu, if the ring-buffer is allocated
894 * in early initcall, it will not be notified of secondary cpus.
895 * In that off case, we need to allocate for all possible cpus.
897 #ifdef CONFIG_HOTPLUG_CPU
898 chan
->cpu_hp_notifier
.notifier_call
=
899 lib_ring_buffer_cpu_hp_callback
;
900 chan
->cpu_hp_notifier
.priority
= 6;
901 register_cpu_notifier(&chan
->cpu_hp_notifier
);
903 lttng_cpus_read_lock();
904 for_each_online_cpu(cpu
) {
905 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
907 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
908 lib_ring_buffer_start_switch_timer(buf
);
909 lib_ring_buffer_start_read_timer(buf
);
910 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
912 chan
->cpu_hp_enable
= 1;
913 lttng_cpus_read_unlock();
915 for_each_possible_cpu(cpu
) {
916 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
918 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
919 lib_ring_buffer_start_switch_timer(buf
);
920 lib_ring_buffer_start_read_timer(buf
);
921 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
925 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
927 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
928 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
929 chan
->tick_nohz_notifier
.notifier_call
=
930 ring_buffer_tick_nohz_callback
;
931 chan
->tick_nohz_notifier
.priority
= ~0U;
932 atomic_notifier_chain_register(&tick_nohz_notifier
,
933 &chan
->tick_nohz_notifier
);
934 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
937 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
939 lib_ring_buffer_start_switch_timer(buf
);
940 lib_ring_buffer_start_read_timer(buf
);
945 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
947 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
948 &chan
->cpuhp_prepare
.node
);
951 #endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
953 channel_backend_free(&chan
->backend
);
958 EXPORT_SYMBOL_GPL(channel_create
);
961 void channel_release(struct kref
*kref
)
963 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(kref
, struct lttng_kernel_ring_buffer_channel
, ref
);
968 * channel_destroy - Finalize, wait for q.s. and destroy channel.
969 * @chan: channel to destroy
972 * Call "destroy" callback, finalize channels, and then decrement the
973 * channel reference count. Note that when readers have completed data
974 * consumption of finalized channels, get_subbuf() will return -ENODATA.
975 * They should release their handle at that point. Returns the private
978 void *channel_destroy(struct lttng_kernel_ring_buffer_channel
*chan
)
981 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
984 irq_work_sync(&chan
->wakeup_pending
);
986 channel_unregister_notifiers(chan
);
988 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
990 * No need to hold cpu hotplug, because all notifiers have been
993 for_each_channel_cpu(cpu
, chan
) {
994 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
997 if (config
->cb
.buffer_finalize
)
998 config
->cb
.buffer_finalize(buf
,
1002 * Perform flush before writing to finalized.
1005 WRITE_ONCE(buf
->finalized
, 1);
1006 wake_up_interruptible(&buf
->read_wait
);
1009 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
1011 if (config
->cb
.buffer_finalize
)
1012 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
1014 * Perform flush before writing to finalized.
1017 WRITE_ONCE(buf
->finalized
, 1);
1018 wake_up_interruptible(&buf
->read_wait
);
1020 WRITE_ONCE(chan
->finalized
, 1);
1021 wake_up_interruptible(&chan
->hp_wait
);
1022 wake_up_interruptible(&chan
->read_wait
);
1023 priv
= chan
->backend
.priv
;
1024 kref_put(&chan
->ref
, channel_release
);
1027 EXPORT_SYMBOL_GPL(channel_destroy
);
1029 struct lttng_kernel_ring_buffer
*channel_get_ring_buffer(
1030 const struct lttng_kernel_ring_buffer_config
*config
,
1031 struct lttng_kernel_ring_buffer_channel
*chan
, int cpu
)
1033 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1034 return chan
->backend
.buf
;
1036 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1038 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1040 int lib_ring_buffer_open_read(struct lttng_kernel_ring_buffer
*buf
)
1042 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1044 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1046 if (!lttng_kref_get(&chan
->ref
)) {
1047 atomic_long_dec(&buf
->active_readers
);
1050 smp_mb__after_atomic();
1053 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1055 void lib_ring_buffer_release_read(struct lttng_kernel_ring_buffer
*buf
)
1057 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1059 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1060 smp_mb__before_atomic();
1061 atomic_long_dec(&buf
->active_readers
);
1062 kref_put(&chan
->ref
, channel_release
);
1064 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1067 * Promote compiler barrier to a smp_mb().
1068 * For the specific ring buffer case, this IPI call should be removed if the
1069 * architecture does not reorder writes. This should eventually be provided by
1070 * a separate architecture-specific infrastructure.
1072 static void remote_mb(void *info
)
1078 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1080 * @consumed: consumed count indicating the position where to read
1081 * @produced: produced count, indicates position when to stop reading
1083 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1084 * data to read at consumed position, or 0 if the get operation succeeds.
1085 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1088 int lib_ring_buffer_snapshot(struct lttng_kernel_ring_buffer
*buf
,
1089 unsigned long *consumed
, unsigned long *produced
)
1091 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1092 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1093 unsigned long consumed_cur
, write_offset
;
1097 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1099 * Read finalized before counters.
1102 consumed_cur
= atomic_long_read(&buf
->consumed
);
1104 * No need to issue a memory barrier between consumed count read and
1105 * write offset read, because consumed count can only change
1106 * concurrently in overwrite mode, and we keep a sequence counter
1107 * identifier derived from the write offset to check we are getting
1108 * the same sub-buffer we are expecting (the sub-buffers are atomically
1109 * "tagged" upon writes, tags are checked upon read).
1111 write_offset
= v_read(config
, &buf
->offset
);
1114 * Check that we are not about to read the same subbuffer in
1115 * which the writer head is.
1117 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1121 *consumed
= consumed_cur
;
1122 *produced
= subbuf_trunc(write_offset
, chan
);
1128 * The memory barriers __wait_event()/wake_up_interruptible() take care
1129 * of "raw_spin_is_locked" memory ordering.
1133 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1138 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1141 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1142 * are saved regardless of whether the consumed and produced positions are
1143 * in the same subbuffer.
1145 * @consumed: consumed byte count indicating the last position read
1146 * @produced: produced byte count indicating the last position written
1148 * This function is meant to provide information on the exact producer and
1149 * consumer positions without regard for the "snapshot" feature.
1151 int lib_ring_buffer_snapshot_sample_positions(struct lttng_kernel_ring_buffer
*buf
,
1152 unsigned long *consumed
, unsigned long *produced
)
1154 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1155 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1158 *consumed
= atomic_long_read(&buf
->consumed
);
1160 * No need to issue a memory barrier between consumed count read and
1161 * write offset read, because consumed count can only change
1162 * concurrently in overwrite mode, and we keep a sequence counter
1163 * identifier derived from the write offset to check we are getting
1164 * the same sub-buffer we are expecting (the sub-buffers are atomically
1165 * "tagged" upon writes, tags are checked upon read).
1167 *produced
= v_read(config
, &buf
->offset
);
1172 * lib_ring_buffer_put_snapshot - move consumed counter forward
1174 * Should only be called from consumer context.
1176 * @consumed_new: new consumed count value
1178 void lib_ring_buffer_move_consumer(struct lttng_kernel_ring_buffer
*buf
,
1179 unsigned long consumed_new
)
1181 struct lttng_kernel_ring_buffer_backend
*bufb
= &buf
->backend
;
1182 struct lttng_kernel_ring_buffer_channel
*chan
= bufb
->chan
;
1183 unsigned long consumed
;
1185 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1188 * Only push the consumed value forward.
1189 * If the consumed cmpxchg fails, this is because we have been pushed by
1190 * the writer in flight recorder mode.
1192 consumed
= atomic_long_read(&buf
->consumed
);
1193 while ((long) consumed
- (long) consumed_new
< 0)
1194 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1196 /* Wake-up the metadata producer */
1197 wake_up_interruptible(&buf
->write_wait
);
1199 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1201 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1202 static void lib_ring_buffer_flush_read_subbuf_dcache(
1203 const struct lttng_kernel_ring_buffer_config
*config
,
1204 struct lttng_kernel_ring_buffer_channel
*chan
,
1205 struct lttng_kernel_ring_buffer
*buf
)
1207 struct lttng_kernel_ring_buffer_backend_pages
*pages
;
1208 unsigned long sb_bindex
, id
, i
, nr_pages
;
1210 if (config
->output
!= RING_BUFFER_MMAP
)
1213 #ifdef cpu_dcache_is_aliasing
1215 * Some architectures implement flush_dcache_page() but don't
1216 * actually have aliasing dcache. cpu_dcache_is_aliasing() was
1217 * introduced in kernel v6.9 to query this more precisely.
1219 if (!cpu_dcache_is_aliasing())
1224 * Architectures with caches aliased on virtual addresses may
1225 * use different cache lines for the linear mapping vs
1226 * user-space memory mapping. Given that the ring buffer is
1227 * based on the kernel linear mapping, aligning it with the
1228 * user-space mapping is not straightforward, and would require
1229 * extra TLB entries. Therefore, simply flush the dcache for the
1230 * entire sub-buffer before reading it.
1232 id
= buf
->backend
.buf_rsb
.id
;
1233 sb_bindex
= subbuffer_id_get_index(config
, id
);
1234 pages
= buf
->backend
.array
[sb_bindex
];
1235 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1236 for (i
= 0; i
< nr_pages
; i
++) {
1237 struct lttng_kernel_ring_buffer_backend_page
*backend_page
;
1239 backend_page
= &pages
->p
[i
];
1240 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1244 static void lib_ring_buffer_flush_read_subbuf_dcache(
1245 const struct lttng_kernel_ring_buffer_config
*config
,
1246 struct lttng_kernel_ring_buffer_channel
*chan
,
1247 struct lttng_kernel_ring_buffer
*buf
)
1253 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1255 * @consumed: consumed count indicating the position where to read
1257 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1258 * data to read at consumed position, or 0 if the get operation succeeds.
1259 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1261 int lib_ring_buffer_get_subbuf(struct lttng_kernel_ring_buffer
*buf
,
1262 unsigned long consumed
)
1264 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1265 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1266 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1270 if (buf
->get_subbuf
) {
1272 * Reader is trying to get a subbuffer twice.
1274 CHAN_WARN_ON(chan
, 1);
1278 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1280 * Read finalized before counters.
1283 consumed_cur
= atomic_long_read(&buf
->consumed
);
1284 consumed_idx
= subbuf_index(consumed
, chan
);
1285 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1287 * Make sure we read the commit count before reading the buffer
1288 * data and the write offset. Correct consumed offset ordering
1289 * wrt commit count is insured by the use of cmpxchg to update
1290 * the consumed offset.
1291 * smp_call_function_single can fail if the remote CPU is offline,
1292 * this is OK because then there is no wmb to execute there.
1293 * If our thread is executing on the same CPU as the on the buffers
1294 * belongs to, we don't have to synchronize it at all. If we are
1295 * migrated, the scheduler will take care of the memory barriers.
1296 * Normally, smp_call_function_single() should ensure program order when
1297 * executing the remote function, which implies that it surrounds the
1298 * function execution with :
1309 * However, smp_call_function_single() does not seem to clearly execute
1310 * such barriers. It depends on spinlock semantic to provide the barrier
1311 * before executing the IPI and, when busy-looping, csd_lock_wait only
1312 * executes smp_mb() when it has to wait for the other CPU.
1314 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1315 * required ourself, even if duplicated. It has no performance impact
1318 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1319 * read and write vs write. They do not ensure core synchronization. We
1320 * really have to ensure total order between the 3 barriers running on
1323 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1324 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1325 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1326 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1327 /* Total order with IPI handler smp_mb() */
1329 smp_call_function_single(buf
->backend
.cpu
,
1330 remote_mb
, NULL
, 1);
1331 /* Total order with IPI handler smp_mb() */
1335 /* Total order with IPI handler smp_mb() */
1337 smp_call_function(remote_mb
, NULL
, 1);
1338 /* Total order with IPI handler smp_mb() */
1343 * Local rmb to match the remote wmb to read the commit count
1344 * before the buffer data and the write offset.
1349 write_offset
= v_read(config
, &buf
->offset
);
1352 * Check that the buffer we are getting is after or at consumed_cur
1355 if ((long) subbuf_trunc(consumed
, chan
)
1356 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1360 * Check that the subbuffer we are trying to consume has been
1361 * already fully committed.
1363 if (((commit_count
- chan
->backend
.subbuf_size
)
1364 & chan
->commit_count_mask
)
1365 - (buf_trunc(consumed
, chan
)
1366 >> chan
->backend
.num_subbuf_order
)
1371 * Check that we are not about to read the same subbuffer in
1372 * which the writer head is.
1374 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1379 * Failure to get the subbuffer causes a busy-loop retry without going
1380 * to a wait queue. These are caused by short-lived race windows where
1381 * the writer is getting access to a subbuffer we were trying to get
1382 * access to. Also checks that the "consumed" buffer count we are
1383 * looking for matches the one contained in the subbuffer id.
1385 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1386 consumed_idx
, buf_trunc_val(consumed
, chan
));
1389 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1391 buf
->get_subbuf_consumed
= consumed
;
1392 buf
->get_subbuf
= 1;
1394 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1400 * The memory barriers __wait_event()/wake_up_interruptible() take care
1401 * of "raw_spin_is_locked" memory ordering.
1405 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1410 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1413 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1416 void lib_ring_buffer_put_subbuf(struct lttng_kernel_ring_buffer
*buf
)
1418 struct lttng_kernel_ring_buffer_backend
*bufb
= &buf
->backend
;
1419 struct lttng_kernel_ring_buffer_channel
*chan
= bufb
->chan
;
1420 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1421 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1423 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1425 if (!buf
->get_subbuf
) {
1427 * Reader puts a subbuffer it did not get.
1429 CHAN_WARN_ON(chan
, 1);
1432 consumed
= buf
->get_subbuf_consumed
;
1433 buf
->get_subbuf
= 0;
1436 * Clear the records_unread counter. (overruns counter)
1437 * Can still be non-zero if a file reader simply grabbed the data
1438 * without using iterators.
1439 * Can be below zero if an iterator is used on a snapshot more than
1442 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1443 v_add(config
, v_read(config
,
1444 &bufb
->array
[read_sb_bindex
]->records_unread
),
1445 &bufb
->records_read
);
1446 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1447 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1448 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1449 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1452 * Exchange the reader subbuffer with the one we put in its place in the
1453 * writer subbuffer table. Expect the original consumed count. If
1454 * update_read_sb_index fails, this is because the writer updated the
1455 * subbuffer concurrently. We should therefore keep the subbuffer we
1456 * currently have: it has become invalid to try reading this sub-buffer
1457 * consumed count value anyway.
1459 consumed_idx
= subbuf_index(consumed
, chan
);
1460 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1461 consumed_idx
, buf_trunc_val(consumed
, chan
));
1463 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1464 * if the writer concurrently updated it.
1467 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1470 * cons_offset is an iterator on all subbuffer offsets between the reader
1471 * position and the writer position. (inclusive)
1474 void lib_ring_buffer_print_subbuffer_errors(struct lttng_kernel_ring_buffer
*buf
,
1475 struct lttng_kernel_ring_buffer_channel
*chan
,
1476 unsigned long cons_offset
,
1479 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1480 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1482 cons_idx
= subbuf_index(cons_offset
, chan
);
1483 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1484 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1486 if (subbuf_offset(commit_count
, chan
) != 0)
1488 "LTTng: ring buffer %s, cpu %d: "
1489 "commit count in subbuffer %lu,\n"
1490 "expecting multiples of %lu bytes\n"
1491 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1492 chan
->backend
.name
, cpu
, cons_idx
,
1493 chan
->backend
.subbuf_size
,
1494 commit_count
, commit_count_sb
);
1496 printk(KERN_DEBUG
"LTTng: ring buffer: %s, cpu %d: %lu bytes committed\n",
1497 chan
->backend
.name
, cpu
, commit_count
);
1501 void lib_ring_buffer_print_buffer_errors(struct lttng_kernel_ring_buffer
*buf
,
1502 struct lttng_kernel_ring_buffer_channel
*chan
,
1503 void *priv
, int cpu
)
1505 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1506 unsigned long write_offset
, cons_offset
;
1509 * No need to order commit_count, write_offset and cons_offset reads
1510 * because we execute at teardown when no more writer nor reader
1511 * references are left.
1513 write_offset
= v_read(config
, &buf
->offset
);
1514 cons_offset
= atomic_long_read(&buf
->consumed
);
1515 if (write_offset
!= cons_offset
)
1517 "LTTng: ring buffer %s, cpu %d: "
1518 "non-consumed data\n"
1519 " [ %lu bytes written, %lu bytes read ]\n",
1520 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1522 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1523 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1526 cons_offset
= subbuf_align(cons_offset
, chan
))
1527 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1531 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1533 void lib_ring_buffer_print_records_count(struct lttng_kernel_ring_buffer_channel
*chan
,
1534 struct lttng_kernel_ring_buffer
*buf
,
1537 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1539 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1540 printk(KERN_DEBUG
"LTTng: ring buffer %s: %lu records written, "
1541 "%lu records overrun\n",
1543 v_read(config
, &buf
->records_count
),
1544 v_read(config
, &buf
->records_overrun
));
1546 printk(KERN_DEBUG
"LTTng: ring buffer %s, cpu %d: %lu records written, "
1547 "%lu records overrun\n",
1548 chan
->backend
.name
, cpu
,
1549 v_read(config
, &buf
->records_count
),
1550 v_read(config
, &buf
->records_overrun
));
1555 void lib_ring_buffer_print_records_count(struct lttng_kernel_ring_buffer_channel
*chan
,
1556 struct lttng_kernel_ring_buffer
*buf
,
1563 void lib_ring_buffer_print_errors(struct lttng_kernel_ring_buffer_channel
*chan
,
1564 struct lttng_kernel_ring_buffer
*buf
, int cpu
)
1566 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1567 void *priv
= chan
->backend
.priv
;
1569 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1570 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1571 if (v_read(config
, &buf
->records_lost_full
)
1572 || v_read(config
, &buf
->records_lost_wrap
)
1573 || v_read(config
, &buf
->records_lost_big
))
1575 "LTTng: ring buffer %s, cpu %d: records were lost. Caused by:\n"
1576 " [ %lu buffer full, %lu nest buffer wrap-around, "
1577 "%lu event too big ]\n",
1578 chan
->backend
.name
, cpu
,
1579 v_read(config
, &buf
->records_lost_full
),
1580 v_read(config
, &buf
->records_lost_wrap
),
1581 v_read(config
, &buf
->records_lost_big
));
1583 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1587 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1589 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1592 void lib_ring_buffer_switch_old_start(struct lttng_kernel_ring_buffer
*buf
,
1593 struct lttng_kernel_ring_buffer_channel
*chan
,
1594 struct switch_offsets
*offsets
,
1595 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1597 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1598 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1599 unsigned long commit_count
;
1600 struct commit_counters_hot
*cc_hot
;
1602 config
->cb
.buffer_begin(buf
, ctx
->priv
.tsc
, oldidx
);
1605 * Order all writes to buffer before the commit count update that will
1606 * determine that the subbuffer is full.
1608 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1610 * Must write slot data before incrementing commit count. This
1611 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1617 cc_hot
= &buf
->commit_hot
[oldidx
];
1618 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1619 commit_count
= v_read(config
, &cc_hot
->cc
);
1620 /* Check if the written buffer has to be delivered */
1621 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1622 commit_count
, oldidx
, ctx
);
1623 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1624 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1625 commit_count
, cc_hot
);
1629 * lib_ring_buffer_switch_old_end: switch old subbuffer
1631 * Note : offset_old should never be 0 here. It is ok, because we never perform
1632 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1633 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1637 void lib_ring_buffer_switch_old_end(struct lttng_kernel_ring_buffer
*buf
,
1638 struct lttng_kernel_ring_buffer_channel
*chan
,
1639 struct switch_offsets
*offsets
,
1640 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1642 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1643 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1644 unsigned long commit_count
, padding_size
, data_size
;
1645 struct commit_counters_hot
*cc_hot
;
1648 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1649 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1650 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1652 ts_end
= &buf
->ts_end
[oldidx
];
1654 * This is the last space reservation in that sub-buffer before
1655 * it gets delivered. This provides exclusive access to write to
1656 * this sub-buffer's ts_end. There are also no concurrent
1657 * readers of that ts_end because delivery of that sub-buffer is
1658 * postponed until the commit counter is incremented for the
1659 * current space reservation.
1661 *ts_end
= ctx
->priv
.tsc
;
1664 * Order all writes to buffer and store to ts_end before the commit
1665 * count update that will determine that the subbuffer is full.
1667 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1669 * Must write slot data before incrementing commit count. This
1670 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1676 cc_hot
= &buf
->commit_hot
[oldidx
];
1677 v_add(config
, padding_size
, &cc_hot
->cc
);
1678 commit_count
= v_read(config
, &cc_hot
->cc
);
1679 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1680 commit_count
, oldidx
, ctx
);
1681 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1682 offsets
->old
+ padding_size
, commit_count
,
1687 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1689 * This code can be executed unordered : writers may already have written to the
1690 * sub-buffer before this code gets executed, caution. The commit makes sure
1691 * that this code is executed before the deliver of this sub-buffer.
1694 void lib_ring_buffer_switch_new_start(struct lttng_kernel_ring_buffer
*buf
,
1695 struct lttng_kernel_ring_buffer_channel
*chan
,
1696 struct switch_offsets
*offsets
,
1697 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1699 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1700 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1701 unsigned long commit_count
;
1702 struct commit_counters_hot
*cc_hot
;
1704 config
->cb
.buffer_begin(buf
, ctx
->priv
.tsc
, beginidx
);
1707 * Order all writes to buffer before the commit count update that will
1708 * determine that the subbuffer is full.
1710 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1712 * Must write slot data before incrementing commit count. This
1713 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1719 cc_hot
= &buf
->commit_hot
[beginidx
];
1720 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1721 commit_count
= v_read(config
, &cc_hot
->cc
);
1722 /* Check if the written buffer has to be delivered */
1723 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1724 commit_count
, beginidx
, ctx
);
1725 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1726 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1727 commit_count
, cc_hot
);
1731 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1733 * Calls subbuffer_set_data_size() to set the data size of the current
1734 * sub-buffer. We do not need to perform check_deliver nor commit here,
1735 * since this task will be done by the "commit" of the event for which
1736 * we are currently doing the space reservation.
1739 void lib_ring_buffer_switch_new_end(struct lttng_kernel_ring_buffer
*buf
,
1740 struct lttng_kernel_ring_buffer_channel
*chan
,
1741 struct switch_offsets
*offsets
,
1742 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1744 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1745 unsigned long endidx
, data_size
;
1748 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1749 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1750 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1751 ts_end
= &buf
->ts_end
[endidx
];
1753 * This is the last space reservation in that sub-buffer before
1754 * it gets delivered. This provides exclusive access to write to
1755 * this sub-buffer's ts_end. There are also no concurrent
1756 * readers of that ts_end because delivery of that sub-buffer is
1757 * postponed until the commit counter is incremented for the
1758 * current space reservation.
1760 *ts_end
= ctx
->priv
.tsc
;
1766 * !0 if execution must be aborted.
1769 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1770 struct lttng_kernel_ring_buffer
*buf
,
1771 struct lttng_kernel_ring_buffer_channel
*chan
,
1772 struct switch_offsets
*offsets
,
1773 struct lttng_kernel_ring_buffer_ctx
*ctx
)
1775 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1776 unsigned long off
, reserve_commit_diff
;
1778 offsets
->begin
= v_read(config
, &buf
->offset
);
1779 offsets
->old
= offsets
->begin
;
1780 offsets
->switch_old_start
= 0;
1781 off
= subbuf_offset(offsets
->begin
, chan
);
1783 ctx
->priv
.tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1786 * Ensure we flush the header of an empty subbuffer when doing the
1787 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1788 * total data gathering duration even if there were no records saved
1789 * after the last buffer switch.
1790 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1791 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1792 * subbuffer header as appropriate.
1793 * The next record that reserves space will be responsible for
1794 * populating the following subbuffer header. We choose not to populate
1795 * the next subbuffer header here because we want to be able to use
1796 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1797 * buffer flush, which must guarantee that all the buffer content
1798 * (records and header timestamps) are visible to the reader. This is
1799 * required for quiescence guarantees for the fusion merge.
1801 if (mode
!= SWITCH_FLUSH
&& !off
)
1802 return -1; /* we do not have to switch : buffer is empty */
1804 if (unlikely(off
== 0)) {
1805 unsigned long sb_index
, commit_count
;
1808 * We are performing a SWITCH_FLUSH. At this stage, there are no
1809 * concurrent writes into the buffer.
1811 * The client does not save any header information. Don't
1812 * switch empty subbuffer on finalize, because it is invalid to
1813 * deliver a completely empty subbuffer.
1815 if (!config
->cb
.subbuffer_header_size())
1818 /* Test new buffer integrity */
1819 sb_index
= subbuf_index(offsets
->begin
, chan
);
1820 commit_count
= v_read(config
,
1821 &buf
->commit_cold
[sb_index
].cc_sb
);
1822 reserve_commit_diff
=
1823 (buf_trunc(offsets
->begin
, chan
)
1824 >> chan
->backend
.num_subbuf_order
)
1825 - (commit_count
& chan
->commit_count_mask
);
1826 if (likely(reserve_commit_diff
== 0)) {
1827 /* Next subbuffer not being written to. */
1828 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1829 subbuf_trunc(offsets
->begin
, chan
)
1830 - subbuf_trunc((unsigned long)
1831 atomic_long_read(&buf
->consumed
), chan
)
1832 >= chan
->backend
.buf_size
)) {
1834 * We do not overwrite non consumed buffers
1835 * and we are full : don't switch.
1840 * Next subbuffer not being written to, and we
1841 * are either in overwrite mode or the buffer is
1842 * not full. It's safe to write in this new
1848 * Next subbuffer reserve offset does not match the
1849 * commit offset. Don't perform switch in
1850 * producer-consumer and overwrite mode. Caused by
1851 * either a writer OOPS or too many nested writes over a
1852 * reserve/commit pair.
1858 * Need to write the subbuffer start header on finalize.
1860 offsets
->switch_old_start
= 1;
1862 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1863 /* Note: old points to the next subbuf at offset 0 */
1864 offsets
->end
= offsets
->begin
;
1866 * Populate the records lost counters prior to performing a
1867 * sub-buffer switch.
1869 ctx
->priv
.records_lost_full
= v_read(config
, &buf
->records_lost_full
);
1870 ctx
->priv
.records_lost_wrap
= v_read(config
, &buf
->records_lost_wrap
);
1871 ctx
->priv
.records_lost_big
= v_read(config
, &buf
->records_lost_big
);
1876 * Force a sub-buffer switch. This operation is completely reentrant : can be
1877 * called while tracing is active with absolutely no lock held.
1879 * Note, however, that as a v_cmpxchg is used for some atomic
1880 * operations, this function must be called from the CPU which owns the buffer
1881 * for a ACTIVE flush.
1883 void lib_ring_buffer_switch_slow(struct lttng_kernel_ring_buffer
*buf
, enum switch_mode mode
)
1885 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1886 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1887 struct lttng_kernel_ring_buffer_ctx ctx
;
1888 struct switch_offsets offsets
;
1889 unsigned long oldidx
;
1894 * Perform retryable operations.
1897 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1899 return; /* Switch not needed */
1900 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1904 * Atomically update last_tsc. This update races against concurrent
1905 * atomic updates, but the race will always cause supplementary full TSC
1906 * records, never the opposite (missing a full TSC record when it would
1909 save_last_tsc(config
, buf
, ctx
.priv
.tsc
);
1912 * Push the reader if necessary
1914 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1916 oldidx
= subbuf_index(offsets
.old
, chan
);
1917 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1920 * May need to populate header start on SWITCH_FLUSH.
1922 if (offsets
.switch_old_start
) {
1923 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, &ctx
);
1924 offsets
.old
+= config
->cb
.subbuffer_header_size();
1928 * Switch old subbuffer.
1930 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, &ctx
);
1932 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1934 struct switch_param
{
1935 struct lttng_kernel_ring_buffer
*buf
;
1936 enum switch_mode mode
;
1939 static void remote_switch(void *info
)
1941 struct switch_param
*param
= info
;
1942 struct lttng_kernel_ring_buffer
*buf
= param
->buf
;
1944 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1947 static void _lib_ring_buffer_switch_remote(struct lttng_kernel_ring_buffer
*buf
,
1948 enum switch_mode mode
)
1950 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1951 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1953 struct switch_param param
;
1956 * With global synchronization we don't need to use the IPI scheme.
1958 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1959 lib_ring_buffer_switch_slow(buf
, mode
);
1964 * Disabling preemption ensures two things: first, that the
1965 * target cpu is not taken concurrently offline while we are within
1966 * smp_call_function_single(). Secondly, if it happens that the
1967 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1968 * needs to be protected from CPU hotplug handlers, which can
1969 * also perform a remote subbuffer switch.
1974 ret
= smp_call_function_single(buf
->backend
.cpu
,
1975 remote_switch
, ¶m
, 1);
1977 /* Remote CPU is offline, do it ourself. */
1978 lib_ring_buffer_switch_slow(buf
, mode
);
1983 /* Switch sub-buffer if current sub-buffer is non-empty. */
1984 void lib_ring_buffer_switch_remote(struct lttng_kernel_ring_buffer
*buf
)
1986 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1988 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1990 /* Switch sub-buffer even if current sub-buffer is empty. */
1991 void lib_ring_buffer_switch_remote_empty(struct lttng_kernel_ring_buffer
*buf
)
1993 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1995 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1997 void lib_ring_buffer_clear(struct lttng_kernel_ring_buffer
*buf
)
1999 struct lttng_kernel_ring_buffer_backend
*bufb
= &buf
->backend
;
2000 struct lttng_kernel_ring_buffer_channel
*chan
= bufb
->chan
;
2002 lib_ring_buffer_switch_remote(buf
);
2003 lib_ring_buffer_clear_reader(buf
, chan
);
2005 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
2010 * -ENOSPC if event size is too large for packet.
2011 * -ENOBUFS if there is currently not enough space in buffer for the event.
2012 * -EIO if data cannot be written into the buffer for any other reason.
2015 int lib_ring_buffer_try_reserve_slow(struct lttng_kernel_ring_buffer
*buf
,
2016 struct lttng_kernel_ring_buffer_channel
*chan
,
2017 struct switch_offsets
*offsets
,
2018 struct lttng_kernel_ring_buffer_ctx
*ctx
,
2021 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2022 unsigned long reserve_commit_diff
, offset_cmp
;
2025 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2026 offsets
->old
= offsets
->begin
;
2027 offsets
->switch_new_start
= 0;
2028 offsets
->switch_new_end
= 0;
2029 offsets
->switch_old_end
= 0;
2030 offsets
->pre_header_padding
= 0;
2032 ctx
->priv
.tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2033 if ((int64_t) ctx
->priv
.tsc
== -EIO
)
2036 if (last_tsc_overflow(config
, buf
, ctx
->priv
.tsc
))
2037 ctx
->priv
.rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2039 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->priv
.chan
) == 0)) {
2040 offsets
->switch_new_start
= 1; /* For offsets->begin */
2042 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2044 &offsets
->pre_header_padding
,
2047 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2050 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2051 offsets
->size
> chan
->backend
.subbuf_size
)) {
2052 offsets
->switch_old_end
= 1; /* For offsets->old */
2053 offsets
->switch_new_start
= 1; /* For offsets->begin */
2056 if (unlikely(offsets
->switch_new_start
)) {
2057 unsigned long sb_index
, commit_count
;
2060 * We are typically not filling the previous buffer completely.
2062 if (likely(offsets
->switch_old_end
))
2063 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2064 offsets
->begin
= offsets
->begin
2065 + config
->cb
.subbuffer_header_size();
2066 /* Test new buffer integrity */
2067 sb_index
= subbuf_index(offsets
->begin
, chan
);
2069 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2070 * lib_ring_buffer_check_deliver() has the matching
2071 * memory barriers required around commit_cold cc_sb
2072 * updates to ensure reserve and commit counter updates
2073 * are not seen reordered when updated by another CPU.
2076 commit_count
= v_read(config
,
2077 &buf
->commit_cold
[sb_index
].cc_sb
);
2078 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2080 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2082 * The reserve counter have been concurrently updated
2083 * while we read the commit counter. This means the
2084 * commit counter we read might not match buf->offset
2085 * due to concurrent update. We therefore need to retry.
2089 reserve_commit_diff
=
2090 (buf_trunc(offsets
->begin
, chan
)
2091 >> chan
->backend
.num_subbuf_order
)
2092 - (commit_count
& chan
->commit_count_mask
);
2093 if (likely(reserve_commit_diff
== 0)) {
2094 /* Next subbuffer not being written to. */
2095 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2096 subbuf_trunc(offsets
->begin
, chan
)
2097 - subbuf_trunc((unsigned long)
2098 atomic_long_read(&buf
->consumed
), chan
)
2099 >= chan
->backend
.buf_size
)) {
2101 * We do not overwrite non consumed buffers
2102 * and we are full : record is lost.
2104 v_inc(config
, &buf
->records_lost_full
);
2108 * Next subbuffer not being written to, and we
2109 * are either in overwrite mode or the buffer is
2110 * not full. It's safe to write in this new
2116 * Next subbuffer reserve offset does not match the
2117 * commit offset, and this did not involve update to the
2118 * reserve counter. Drop record in producer-consumer and
2119 * overwrite mode. Caused by either a writer OOPS or
2120 * too many nested writes over a reserve/commit pair.
2122 v_inc(config
, &buf
->records_lost_wrap
);
2126 config
->cb
.record_header_size(config
, chan
,
2128 &offsets
->pre_header_padding
,
2131 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2134 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2135 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2137 * Record too big for subbuffers, report error, don't
2138 * complete the sub-buffer switch.
2140 v_inc(config
, &buf
->records_lost_big
);
2144 * We just made a successful buffer switch and the
2145 * record fits in the new subbuffer. Let's write.
2150 * Record fits in the current buffer and we are not on a switch
2151 * boundary. It's safe to write.
2154 offsets
->end
= offsets
->begin
+ offsets
->size
;
2156 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2158 * The offset_end will fall at the very beginning of the next
2161 offsets
->switch_new_end
= 1; /* For offsets->begin */
2164 * Populate the records lost counters when the space reservation
2165 * may cause a sub-buffer switch.
2167 if (offsets
->switch_new_end
|| offsets
->switch_old_end
) {
2168 ctx
->priv
.records_lost_full
= v_read(config
, &buf
->records_lost_full
);
2169 ctx
->priv
.records_lost_wrap
= v_read(config
, &buf
->records_lost_wrap
);
2170 ctx
->priv
.records_lost_big
= v_read(config
, &buf
->records_lost_big
);
2175 static struct lttng_kernel_ring_buffer
*get_current_buf(struct lttng_kernel_ring_buffer_channel
*chan
, int cpu
)
2177 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2179 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2180 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2182 return chan
->backend
.buf
;
2185 void lib_ring_buffer_lost_event_too_big(struct lttng_kernel_ring_buffer_channel
*chan
)
2187 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2188 struct lttng_kernel_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2190 v_inc(config
, &buf
->records_lost_big
);
2192 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2195 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2196 * @ctx: ring buffer context.
2198 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2199 * -EIO for other errors, else returns 0.
2200 * It will take care of sub-buffer switching.
2202 int lib_ring_buffer_reserve_slow(struct lttng_kernel_ring_buffer_ctx
*ctx
,
2205 struct lttng_kernel_ring_buffer_channel
*chan
= ctx
->priv
.chan
;
2206 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2207 struct lttng_kernel_ring_buffer
*buf
;
2208 struct switch_offsets offsets
;
2211 ctx
->priv
.buf
= buf
= get_current_buf(chan
, ctx
->priv
.reserve_cpu
);
2215 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2219 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2224 * Atomically update last_tsc. This update races against concurrent
2225 * atomic updates, but the race will always cause supplementary full TSC
2226 * records, never the opposite (missing a full TSC record when it would
2229 save_last_tsc(config
, buf
, ctx
->priv
.tsc
);
2232 * Push the reader if necessary
2234 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2237 * Clear noref flag for this subbuffer.
2239 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2240 subbuf_index(offsets
.end
- 1, chan
));
2243 * Switch old subbuffer if needed.
2245 if (unlikely(offsets
.switch_old_end
)) {
2246 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2247 subbuf_index(offsets
.old
- 1, chan
));
2248 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
);
2252 * Populate new subbuffer.
2254 if (unlikely(offsets
.switch_new_start
))
2255 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
);
2257 if (unlikely(offsets
.switch_new_end
))
2258 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
);
2260 ctx
->priv
.slot_size
= offsets
.size
;
2261 ctx
->priv
.pre_offset
= offsets
.begin
;
2262 ctx
->priv
.buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2265 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2268 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_kernel_ring_buffer_config
*config
,
2269 struct lttng_kernel_ring_buffer
*buf
,
2270 unsigned long commit_count
,
2273 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2274 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2278 * The ring buffer can count events recorded and overwritten per buffer,
2279 * but it is disabled by default due to its performance overhead.
2281 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2283 void deliver_count_events(const struct lttng_kernel_ring_buffer_config
*config
,
2284 struct lttng_kernel_ring_buffer
*buf
,
2287 v_add(config
, subbuffer_get_records_count(config
,
2288 &buf
->backend
, idx
),
2289 &buf
->records_count
);
2290 v_add(config
, subbuffer_count_records_overrun(config
,
2291 &buf
->backend
, idx
),
2292 &buf
->records_overrun
);
2294 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2296 void deliver_count_events(const struct lttng_kernel_ring_buffer_config
*config
,
2297 struct lttng_kernel_ring_buffer
*buf
,
2301 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2304 void lib_ring_buffer_check_deliver_slow(const struct lttng_kernel_ring_buffer_config
*config
,
2305 struct lttng_kernel_ring_buffer
*buf
,
2306 struct lttng_kernel_ring_buffer_channel
*chan
,
2307 unsigned long offset
,
2308 unsigned long commit_count
,
2310 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
2312 unsigned long old_commit_count
= commit_count
2313 - chan
->backend
.subbuf_size
;
2316 * If we succeeded at updating cc_sb below, we are the subbuffer
2317 * writer delivering the subbuffer. Deals with concurrent
2318 * updates of the "cc" value without adding a add_return atomic
2319 * operation to the fast path.
2321 * We are doing the delivery in two steps:
2322 * - First, we cmpxchg() cc_sb to the new value
2323 * old_commit_count + 1. This ensures that we are the only
2324 * subbuffer user successfully filling the subbuffer, but we
2325 * do _not_ set the cc_sb value to "commit_count" yet.
2326 * Therefore, other writers that would wrap around the ring
2327 * buffer and try to start writing to our subbuffer would
2328 * have to drop records, because it would appear as
2330 * We therefore have exclusive access to the subbuffer control
2331 * structures. This mutual exclusion with other writers is
2332 * crucially important to perform record overruns count in
2333 * flight recorder mode locklessly.
2334 * - When we are ready to release the subbuffer (either for
2335 * reading or for overrun by other writers), we simply set the
2336 * cc_sb value to "commit_count" and perform delivery.
2338 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2339 * This guarantees that old_commit_count + 1 != commit_count.
2343 * Order prior updates to reserve count prior to the
2344 * commit_cold cc_sb update.
2347 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2348 old_commit_count
, old_commit_count
+ 1)
2349 == old_commit_count
)) {
2353 * Start of exclusive subbuffer access. We are
2354 * guaranteed to be the last writer in this subbuffer
2355 * and any other writer trying to access this subbuffer
2356 * in this state is required to drop records.
2358 * We can read the ts_end for the current sub-buffer
2359 * which has been saved by the very last space
2360 * reservation for the current sub-buffer.
2362 * Order increment of commit counter before reading ts_end.
2365 ts_end
= &buf
->ts_end
[idx
];
2366 deliver_count_events(config
, buf
, idx
);
2367 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2368 lib_ring_buffer_get_data_size(config
,
2373 * Increment the packet counter while we have exclusive
2376 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2379 * Set noref flag and offset for this subbuffer id.
2380 * Contains a memory barrier that ensures counter stores
2381 * are ordered before set noref and offset.
2383 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2384 buf_trunc_val(offset
, chan
));
2387 * Order set_noref and record counter updates before the
2388 * end of subbuffer exclusive access. Orders with
2389 * respect to writers coming into the subbuffer after
2390 * wrap around, and also order wrt concurrent readers.
2393 /* End of exclusive subbuffer access */
2394 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2397 * Order later updates to reserve count after
2398 * the commit_cold cc_sb update.
2401 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2405 * RING_BUFFER_WAKEUP_BY_WRITER uses an irq_work to issue
2408 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2409 && atomic_long_read(&buf
->active_readers
)
2410 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2411 irq_work_queue(&buf
->wakeup_pending
);
2412 irq_work_queue(&chan
->wakeup_pending
);
2417 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2419 int __init
init_lib_ring_buffer_frontend(void)
2423 for_each_possible_cpu(cpu
)
2424 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2428 module_init(init_lib_ring_buffer_frontend
);
2430 void __exit
exit_lib_ring_buffer_frontend(void)
2434 module_exit(exit_lib_ring_buffer_frontend
);