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/delay.h>
41 #include <linux/module.h>
42 #include <linux/percpu.h>
43 #include <linux/kref.h>
44 #include <linux/percpu-defs.h>
45 #include <linux/timer.h>
46 #include <asm/cacheflush.h>
48 #include <wrapper/ringbuffer/config.h>
49 #include <wrapper/ringbuffer/backend.h>
50 #include <wrapper/ringbuffer/frontend.h>
51 #include <wrapper/ringbuffer/iterator.h>
52 #include <wrapper/ringbuffer/nohz.h>
53 #include <wrapper/atomic.h>
56 * Internal structure representing offsets to use at a sub-buffer switch.
58 struct switch_offsets
{
59 unsigned long begin
, end
, old
;
60 size_t pre_header_padding
, size
;
61 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
72 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
73 #endif /* CONFIG_NO_HZ */
75 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
77 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
78 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
81 void lib_ring_buffer_print_errors(struct channel
*chan
,
82 struct lib_ring_buffer
*buf
, int cpu
);
84 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
85 enum switch_mode mode
);
88 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
89 struct lib_ring_buffer
*buf
,
92 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
94 consumed_old
= atomic_long_read(&buf
->consumed
);
95 consumed_idx
= subbuf_index(consumed_old
, chan
);
96 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
98 * No memory barrier here, since we are only interested
99 * in a statistically correct polling result. The next poll will
100 * get the data is we are racing. The mb() that ensures correct
101 * memory order is in get_subbuf.
103 write_offset
= v_read(config
, &buf
->offset
);
106 * Check that the subbuffer we are trying to consume has been
107 * already fully committed.
110 if (((commit_count
- chan
->backend
.subbuf_size
)
111 & chan
->commit_count_mask
)
112 - (buf_trunc(consumed_old
, chan
)
113 >> chan
->backend
.num_subbuf_order
)
118 * Check that we are not about to read the same subbuffer in
119 * which the writer head is.
121 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
129 * Must be called under cpu hotplug protection.
131 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
133 struct channel
*chan
= buf
->backend
.chan
;
135 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
136 kvfree(buf
->commit_hot
);
137 kvfree(buf
->commit_cold
);
140 lib_ring_buffer_backend_free(&buf
->backend
);
144 * lib_ring_buffer_reset - Reset ring buffer to initial values.
147 * Effectively empty the ring buffer. Should be called when the buffer is not
148 * used for writing. The ring buffer can be opened for reading, but the reader
149 * should not be using the iterator concurrently with reset. The previous
150 * current iterator record is reset.
152 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
154 struct channel
*chan
= buf
->backend
.chan
;
155 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
159 * Reset iterator first. It will put the subbuffer if it currently holds
162 lib_ring_buffer_iterator_reset(buf
);
163 v_set(config
, &buf
->offset
, 0);
164 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
165 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
166 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
167 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
170 atomic_long_set(&buf
->consumed
, 0);
171 atomic_set(&buf
->record_disabled
, 0);
172 v_set(config
, &buf
->last_tsc
, 0);
173 lib_ring_buffer_backend_reset(&buf
->backend
);
174 /* Don't reset number of active readers */
175 v_set(config
, &buf
->records_lost_full
, 0);
176 v_set(config
, &buf
->records_lost_wrap
, 0);
177 v_set(config
, &buf
->records_lost_big
, 0);
178 v_set(config
, &buf
->records_count
, 0);
179 v_set(config
, &buf
->records_overrun
, 0);
182 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
185 * channel_reset - Reset channel to initial values.
188 * Effectively empty the channel. Should be called when the channel is not used
189 * for writing. The channel can be opened for reading, but the reader should not
190 * be using the iterator concurrently with reset. The previous current iterator
193 void channel_reset(struct channel
*chan
)
196 * Reset iterators first. Will put the subbuffer if held for reading.
198 channel_iterator_reset(chan
);
199 atomic_set(&chan
->record_disabled
, 0);
200 /* Don't reset commit_count_mask, still valid */
201 channel_backend_reset(&chan
->backend
);
202 /* Don't reset switch/read timer interval */
203 /* Don't reset notifiers and notifier enable bits */
204 /* Don't reset reader reference count */
206 EXPORT_SYMBOL_GPL(channel_reset
);
209 * Must be called under cpu hotplug protection.
211 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
212 struct channel_backend
*chanb
, int cpu
)
214 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
215 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
216 void *priv
= chanb
->priv
;
217 size_t subbuf_header_size
;
221 /* Test for cpu hotplug */
222 if (buf
->backend
.allocated
)
226 * Paranoia: per cpu dynamic allocation is not officially documented as
227 * zeroing the memory, so let's do it here too, just in case.
229 memset(buf
, 0, sizeof(*buf
));
231 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
236 kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
237 * chan
->backend
.num_subbuf
,
238 1 << INTERNODE_CACHE_SHIFT
),
239 GFP_KERNEL
| __GFP_NOWARN
,
240 cpu_to_node(max(cpu
, 0)));
241 if (!buf
->commit_hot
) {
247 kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
248 * chan
->backend
.num_subbuf
,
249 1 << INTERNODE_CACHE_SHIFT
),
250 GFP_KERNEL
| __GFP_NOWARN
,
251 cpu_to_node(max(cpu
, 0)));
252 if (!buf
->commit_cold
) {
258 kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
259 * chan
->backend
.num_subbuf
,
260 1 << INTERNODE_CACHE_SHIFT
),
261 GFP_KERNEL
| __GFP_NOWARN
,
262 cpu_to_node(max(cpu
, 0)));
265 goto free_commit_cold
;
268 init_waitqueue_head(&buf
->read_wait
);
269 init_waitqueue_head(&buf
->write_wait
);
270 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
273 * Write the subbuffer header for first subbuffer so we know the total
274 * duration of data gathering.
276 subbuf_header_size
= config
->cb
.subbuffer_header_size();
277 v_set(config
, &buf
->offset
, subbuf_header_size
);
278 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
279 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
280 config
->cb
.buffer_begin(buf
, tsc
, 0);
281 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
283 if (config
->cb
.buffer_create
) {
284 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
290 * Ensure the buffer is ready before setting it to allocated and setting
292 * Used for cpu hotplug vs cpumask iteration.
295 buf
->backend
.allocated
= 1;
297 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
298 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
299 chan
->backend
.cpumask
));
300 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
308 kvfree(buf
->commit_cold
);
310 kvfree(buf
->commit_hot
);
312 lib_ring_buffer_backend_free(&buf
->backend
);
316 static void switch_buffer_timer(struct timer_list
*t
)
318 struct lib_ring_buffer
*buf
= from_timer(buf
, t
, switch_timer
);
319 struct channel
*chan
= buf
->backend
.chan
;
322 * Only flush buffers periodically if readers are active.
324 if (atomic_long_read(&buf
->active_readers
))
325 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
327 mod_timer(&buf
->switch_timer
,
328 jiffies
+ chan
->switch_timer_interval
);
332 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
334 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
336 struct channel
*chan
= buf
->backend
.chan
;
337 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
338 unsigned int flags
= 0;
340 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
343 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
344 flags
= TIMER_PINNED
;
346 timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
);
347 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
349 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
350 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
352 add_timer(&buf
->switch_timer
);
354 buf
->switch_timer_enabled
= 1;
358 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
360 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
362 struct channel
*chan
= buf
->backend
.chan
;
364 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
367 del_timer_sync(&buf
->switch_timer
);
368 buf
->switch_timer_enabled
= 0;
372 * Polling timer to check the channels for data.
374 static void read_buffer_timer(struct timer_list
*t
)
376 struct lib_ring_buffer
*buf
= from_timer(buf
, t
, read_timer
);
377 struct channel
*chan
= buf
->backend
.chan
;
378 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
380 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
382 if (atomic_long_read(&buf
->active_readers
)
383 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
384 wake_up_interruptible(&buf
->read_wait
);
385 wake_up_interruptible(&chan
->read_wait
);
388 mod_timer(&buf
->read_timer
,
389 jiffies
+ chan
->read_timer_interval
);
393 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
395 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
397 struct channel
*chan
= buf
->backend
.chan
;
398 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
399 unsigned int flags
= 0;
401 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
402 || !chan
->read_timer_interval
403 || buf
->read_timer_enabled
)
406 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
407 flags
= TIMER_PINNED
;
409 timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
);
410 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
412 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
413 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
415 add_timer(&buf
->read_timer
);
417 buf
->read_timer_enabled
= 1;
421 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
423 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
425 struct channel
*chan
= buf
->backend
.chan
;
426 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
428 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
429 || !chan
->read_timer_interval
430 || !buf
->read_timer_enabled
)
433 del_timer_sync(&buf
->read_timer
);
435 * do one more check to catch data that has been written in the last
438 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
439 wake_up_interruptible(&buf
->read_wait
);
440 wake_up_interruptible(&chan
->read_wait
);
442 buf
->read_timer_enabled
= 0;
445 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
447 enum cpuhp_state lttng_rb_hp_prepare
;
448 enum cpuhp_state lttng_rb_hp_online
;
450 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
452 lttng_rb_hp_prepare
= val
;
454 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
456 void lttng_rb_set_hp_online(enum cpuhp_state val
)
458 lttng_rb_hp_online
= val
;
460 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
462 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
463 struct lttng_cpuhp_node
*node
)
465 struct channel
*chan
= container_of(node
, struct channel
,
467 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
468 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
470 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
473 * Performing a buffer switch on a remote CPU. Performed by
474 * the CPU responsible for doing the hotunplug after the target
475 * CPU stopped running completely. Ensures that all data
476 * from that remote CPU is flushed.
478 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
481 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
483 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
484 struct lttng_cpuhp_node
*node
)
486 struct channel
*chan
= container_of(node
, struct channel
,
488 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
489 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
491 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
493 wake_up_interruptible(&chan
->hp_wait
);
494 lib_ring_buffer_start_switch_timer(buf
);
495 lib_ring_buffer_start_read_timer(buf
);
498 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
500 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
501 struct lttng_cpuhp_node
*node
)
503 struct channel
*chan
= container_of(node
, struct channel
,
505 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
506 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
508 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
510 lib_ring_buffer_stop_switch_timer(buf
);
511 lib_ring_buffer_stop_read_timer(buf
);
514 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
516 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
518 #ifdef CONFIG_HOTPLUG_CPU
521 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
522 * @nb: notifier block
523 * @action: hotplug action to take
526 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
529 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
530 unsigned long action
,
533 unsigned int cpu
= (unsigned long)hcpu
;
534 struct channel
*chan
= container_of(nb
, struct channel
,
536 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
537 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
539 if (!chan
->cpu_hp_enable
)
542 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
545 case CPU_DOWN_FAILED
:
546 case CPU_DOWN_FAILED_FROZEN
:
548 case CPU_ONLINE_FROZEN
:
549 wake_up_interruptible(&chan
->hp_wait
);
550 lib_ring_buffer_start_switch_timer(buf
);
551 lib_ring_buffer_start_read_timer(buf
);
554 case CPU_DOWN_PREPARE
:
555 case CPU_DOWN_PREPARE_FROZEN
:
556 lib_ring_buffer_stop_switch_timer(buf
);
557 lib_ring_buffer_stop_read_timer(buf
);
561 case CPU_DEAD_FROZEN
:
563 * Performing a buffer switch on a remote CPU. Performed by
564 * the CPU responsible for doing the hotunplug after the target
565 * CPU stopped running completely. Ensures that all data
566 * from that remote CPU is flushed.
568 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
578 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
580 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
582 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
583 * that wake-up-tracing generated events are flushed before going idle (in
584 * tick_nohz). We test if the spinlock is locked to deal with the race where
585 * readers try to sample the ring buffer before we perform the switch. We let
586 * the readers retry in that case. If there is data in the buffer, the wake up
587 * is going to forbid the CPU running the reader thread from going idle.
589 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
593 struct channel
*chan
= container_of(nb
, struct channel
,
595 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
596 struct lib_ring_buffer
*buf
;
597 int cpu
= smp_processor_id();
599 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
601 * We don't support keeping the system idle with global buffers
602 * and streaming active. In order to do so, we would need to
603 * sample a non-nohz-cpumask racelessly with the nohz updates
604 * without adding synchronization overhead to nohz. Leave this
605 * use-case out for now.
610 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
612 case TICK_NOHZ_FLUSH
:
613 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
614 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
615 && chan
->read_timer_interval
616 && atomic_long_read(&buf
->active_readers
)
617 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
618 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
619 wake_up_interruptible(&buf
->read_wait
);
620 wake_up_interruptible(&chan
->read_wait
);
622 if (chan
->switch_timer_interval
)
623 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
624 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
627 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
628 lib_ring_buffer_stop_switch_timer(buf
);
629 lib_ring_buffer_stop_read_timer(buf
);
630 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
632 case TICK_NOHZ_RESTART
:
633 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
634 lib_ring_buffer_start_read_timer(buf
);
635 lib_ring_buffer_start_switch_timer(buf
);
636 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
643 void notrace
lib_ring_buffer_tick_nohz_flush(void)
645 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
649 void notrace
lib_ring_buffer_tick_nohz_stop(void)
651 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
655 void notrace
lib_ring_buffer_tick_nohz_restart(void)
657 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
660 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
665 static void channel_unregister_notifiers(struct channel
*chan
)
667 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
669 channel_iterator_unregister_notifiers(chan
);
670 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
673 * Remove the nohz notifier first, so we are certain we stop
676 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
677 &chan
->tick_nohz_notifier
);
679 * ring_buffer_nohz_lock will not be needed below, because
680 * we just removed the notifiers, which were the only source of
683 #endif /* CONFIG_NO_HZ */
684 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
688 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
689 &chan
->cpuhp_online
.node
);
691 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
692 &chan
->cpuhp_prepare
.node
);
695 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
699 #ifdef CONFIG_HOTPLUG_CPU
701 chan
->cpu_hp_enable
= 0;
702 for_each_online_cpu(cpu
) {
703 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
705 lib_ring_buffer_stop_switch_timer(buf
);
706 lib_ring_buffer_stop_read_timer(buf
);
709 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
711 for_each_possible_cpu(cpu
) {
712 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
714 lib_ring_buffer_stop_switch_timer(buf
);
715 lib_ring_buffer_stop_read_timer(buf
);
719 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
721 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
723 lib_ring_buffer_stop_switch_timer(buf
);
724 lib_ring_buffer_stop_read_timer(buf
);
726 channel_backend_unregister_notifiers(&chan
->backend
);
729 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
731 if (!buf
->quiescent
) {
732 buf
->quiescent
= true;
733 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
737 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
739 buf
->quiescent
= false;
742 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
745 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
747 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
749 for_each_channel_cpu(cpu
, chan
) {
750 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
753 lib_ring_buffer_set_quiescent(buf
);
757 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
759 lib_ring_buffer_set_quiescent(buf
);
762 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
764 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
767 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
769 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
771 for_each_channel_cpu(cpu
, chan
) {
772 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
775 lib_ring_buffer_clear_quiescent(buf
);
779 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
781 lib_ring_buffer_clear_quiescent(buf
);
784 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
786 static void channel_free(struct channel
*chan
)
788 if (chan
->backend
.release_priv_ops
) {
789 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
791 channel_iterator_free(chan
);
792 channel_backend_free(&chan
->backend
);
797 * channel_create - Create channel.
798 * @config: ring buffer instance configuration
799 * @name: name of the channel
800 * @priv: ring buffer client private data
801 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
802 * address mapping. It is used only by RING_BUFFER_STATIC
803 * configuration. It can be set to NULL for other backends.
804 * @subbuf_size: subbuffer size
805 * @num_subbuf: number of subbuffers
806 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
807 * padding to let readers get those sub-buffers.
808 * Used for live streaming.
809 * @read_timer_interval: Time interval (in us) to wake up pending readers.
812 * Returns NULL on failure.
814 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
815 const char *name
, void *priv
, void *buf_addr
,
817 size_t num_subbuf
, unsigned int switch_timer_interval
,
818 unsigned int read_timer_interval
)
821 struct channel
*chan
;
823 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
824 read_timer_interval
))
827 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
831 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
832 subbuf_size
, num_subbuf
);
836 ret
= channel_iterator_init(chan
);
838 goto error_free_backend
;
840 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
841 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
842 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
843 kref_init(&chan
->ref
);
844 init_waitqueue_head(&chan
->read_wait
);
845 init_waitqueue_head(&chan
->hp_wait
);
847 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
848 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
849 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
850 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
851 &chan
->cpuhp_prepare
.node
);
853 goto cpuhp_prepare_error
;
855 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
856 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
857 &chan
->cpuhp_online
.node
);
859 goto cpuhp_online_error
;
860 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
864 * In case of non-hotplug cpu, if the ring-buffer is allocated
865 * in early initcall, it will not be notified of secondary cpus.
866 * In that off case, we need to allocate for all possible cpus.
868 #ifdef CONFIG_HOTPLUG_CPU
869 chan
->cpu_hp_notifier
.notifier_call
=
870 lib_ring_buffer_cpu_hp_callback
;
871 chan
->cpu_hp_notifier
.priority
= 6;
872 register_cpu_notifier(&chan
->cpu_hp_notifier
);
875 for_each_online_cpu(cpu
) {
876 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
878 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
879 lib_ring_buffer_start_switch_timer(buf
);
880 lib_ring_buffer_start_read_timer(buf
);
881 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
883 chan
->cpu_hp_enable
= 1;
886 for_each_possible_cpu(cpu
) {
887 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
889 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
890 lib_ring_buffer_start_switch_timer(buf
);
891 lib_ring_buffer_start_read_timer(buf
);
892 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
896 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
898 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
899 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
900 chan
->tick_nohz_notifier
.notifier_call
=
901 ring_buffer_tick_nohz_callback
;
902 chan
->tick_nohz_notifier
.priority
= ~0U;
903 atomic_notifier_chain_register(&tick_nohz_notifier
,
904 &chan
->tick_nohz_notifier
);
905 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
908 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
910 lib_ring_buffer_start_switch_timer(buf
);
911 lib_ring_buffer_start_read_timer(buf
);
916 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
918 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
919 &chan
->cpuhp_prepare
.node
);
922 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
924 channel_backend_free(&chan
->backend
);
929 EXPORT_SYMBOL_GPL(channel_create
);
932 void channel_release(struct kref
*kref
)
934 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
939 * channel_destroy - Finalize, wait for q.s. and destroy channel.
940 * @chan: channel to destroy
943 * Call "destroy" callback, finalize channels, and then decrement the
944 * channel reference count. Note that when readers have completed data
945 * consumption of finalized channels, get_subbuf() will return -ENODATA.
946 * They should release their handle at that point. Returns the private
949 void *channel_destroy(struct channel
*chan
)
952 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
955 channel_unregister_notifiers(chan
);
957 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
959 * No need to hold cpu hotplug, because all notifiers have been
962 for_each_channel_cpu(cpu
, chan
) {
963 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
966 if (config
->cb
.buffer_finalize
)
967 config
->cb
.buffer_finalize(buf
,
971 * Perform flush before writing to finalized.
974 WRITE_ONCE(buf
->finalized
, 1);
975 wake_up_interruptible(&buf
->read_wait
);
978 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
980 if (config
->cb
.buffer_finalize
)
981 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
983 * Perform flush before writing to finalized.
986 WRITE_ONCE(buf
->finalized
, 1);
987 wake_up_interruptible(&buf
->read_wait
);
989 WRITE_ONCE(chan
->finalized
, 1);
990 wake_up_interruptible(&chan
->hp_wait
);
991 wake_up_interruptible(&chan
->read_wait
);
992 priv
= chan
->backend
.priv
;
993 kref_put(&chan
->ref
, channel_release
);
996 EXPORT_SYMBOL_GPL(channel_destroy
);
998 struct lib_ring_buffer
*channel_get_ring_buffer(
999 const struct lib_ring_buffer_config
*config
,
1000 struct channel
*chan
, int cpu
)
1002 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1003 return chan
->backend
.buf
;
1005 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1007 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1009 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1011 struct channel
*chan
= buf
->backend
.chan
;
1013 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1015 kref_get(&chan
->ref
);
1016 lttng_smp_mb__after_atomic();
1019 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1021 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1023 struct channel
*chan
= buf
->backend
.chan
;
1025 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1026 lttng_smp_mb__before_atomic();
1027 atomic_long_dec(&buf
->active_readers
);
1028 kref_put(&chan
->ref
, channel_release
);
1030 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1033 * Promote compiler barrier to a smp_mb().
1034 * For the specific ring buffer case, this IPI call should be removed if the
1035 * architecture does not reorder writes. This should eventually be provided by
1036 * a separate architecture-specific infrastructure.
1038 static void remote_mb(void *info
)
1044 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1046 * @consumed: consumed count indicating the position where to read
1047 * @produced: produced count, indicates position when to stop reading
1049 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1050 * data to read at consumed position, or 0 if the get operation succeeds.
1051 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1054 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1055 unsigned long *consumed
, unsigned long *produced
)
1057 struct channel
*chan
= buf
->backend
.chan
;
1058 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1059 unsigned long consumed_cur
, write_offset
;
1063 finalized
= READ_ONCE(buf
->finalized
);
1065 * Read finalized before counters.
1068 consumed_cur
= atomic_long_read(&buf
->consumed
);
1070 * No need to issue a memory barrier between consumed count read and
1071 * write offset read, because consumed count can only change
1072 * concurrently in overwrite mode, and we keep a sequence counter
1073 * identifier derived from the write offset to check we are getting
1074 * the same sub-buffer we are expecting (the sub-buffers are atomically
1075 * "tagged" upon writes, tags are checked upon read).
1077 write_offset
= v_read(config
, &buf
->offset
);
1080 * Check that we are not about to read the same subbuffer in
1081 * which the writer head is.
1083 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1087 *consumed
= consumed_cur
;
1088 *produced
= subbuf_trunc(write_offset
, chan
);
1094 * The memory barriers __wait_event()/wake_up_interruptible() take care
1095 * of "raw_spin_is_locked" memory ordering.
1099 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1104 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1107 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1108 * are saved regardless of whether the consumed and produced positions are
1109 * in the same subbuffer.
1111 * @consumed: consumed byte count indicating the last position read
1112 * @produced: produced byte count indicating the last position written
1114 * This function is meant to provide information on the exact producer and
1115 * consumer positions without regard for the "snapshot" feature.
1117 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1118 unsigned long *consumed
, unsigned long *produced
)
1120 struct channel
*chan
= buf
->backend
.chan
;
1121 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1124 *consumed
= atomic_long_read(&buf
->consumed
);
1126 * No need to issue a memory barrier between consumed count read and
1127 * write offset read, because consumed count can only change
1128 * concurrently in overwrite mode, and we keep a sequence counter
1129 * identifier derived from the write offset to check we are getting
1130 * the same sub-buffer we are expecting (the sub-buffers are atomically
1131 * "tagged" upon writes, tags are checked upon read).
1133 *produced
= v_read(config
, &buf
->offset
);
1138 * lib_ring_buffer_put_snapshot - move consumed counter forward
1140 * Should only be called from consumer context.
1142 * @consumed_new: new consumed count value
1144 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1145 unsigned long consumed_new
)
1147 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1148 struct channel
*chan
= bufb
->chan
;
1149 unsigned long consumed
;
1151 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1154 * Only push the consumed value forward.
1155 * If the consumed cmpxchg fails, this is because we have been pushed by
1156 * the writer in flight recorder mode.
1158 consumed
= atomic_long_read(&buf
->consumed
);
1159 while ((long) consumed
- (long) consumed_new
< 0)
1160 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1162 /* Wake-up the metadata producer */
1163 wake_up_interruptible(&buf
->write_wait
);
1165 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1167 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1168 static void lib_ring_buffer_flush_read_subbuf_dcache(
1169 const struct lib_ring_buffer_config
*config
,
1170 struct channel
*chan
,
1171 struct lib_ring_buffer
*buf
)
1173 struct lib_ring_buffer_backend_pages
*pages
;
1174 unsigned long sb_bindex
, id
, i
, nr_pages
;
1176 if (config
->output
!= RING_BUFFER_MMAP
)
1180 * Architectures with caches aliased on virtual addresses may
1181 * use different cache lines for the linear mapping vs
1182 * user-space memory mapping. Given that the ring buffer is
1183 * based on the kernel linear mapping, aligning it with the
1184 * user-space mapping is not straightforward, and would require
1185 * extra TLB entries. Therefore, simply flush the dcache for the
1186 * entire sub-buffer before reading it.
1188 id
= buf
->backend
.buf_rsb
.id
;
1189 sb_bindex
= subbuffer_id_get_index(config
, id
);
1190 pages
= buf
->backend
.array
[sb_bindex
];
1191 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1192 for (i
= 0; i
< nr_pages
; i
++) {
1193 struct lib_ring_buffer_backend_page
*backend_page
;
1195 backend_page
= &pages
->p
[i
];
1196 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1200 static void lib_ring_buffer_flush_read_subbuf_dcache(
1201 const struct lib_ring_buffer_config
*config
,
1202 struct channel
*chan
,
1203 struct lib_ring_buffer
*buf
)
1209 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1211 * @consumed: consumed count indicating the position where to read
1213 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1214 * data to read at consumed position, or 0 if the get operation succeeds.
1215 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1217 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1218 unsigned long consumed
)
1220 struct channel
*chan
= buf
->backend
.chan
;
1221 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1222 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1226 if (buf
->get_subbuf
) {
1228 * Reader is trying to get a subbuffer twice.
1230 CHAN_WARN_ON(chan
, 1);
1234 finalized
= READ_ONCE(buf
->finalized
);
1236 * Read finalized before counters.
1239 consumed_cur
= atomic_long_read(&buf
->consumed
);
1240 consumed_idx
= subbuf_index(consumed
, chan
);
1241 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1243 * Make sure we read the commit count before reading the buffer
1244 * data and the write offset. Correct consumed offset ordering
1245 * wrt commit count is insured by the use of cmpxchg to update
1246 * the consumed offset.
1247 * smp_call_function_single can fail if the remote CPU is offline,
1248 * this is OK because then there is no wmb to execute there.
1249 * If our thread is executing on the same CPU as the on the buffers
1250 * belongs to, we don't have to synchronize it at all. If we are
1251 * migrated, the scheduler will take care of the memory barriers.
1252 * Normally, smp_call_function_single() should ensure program order when
1253 * executing the remote function, which implies that it surrounds the
1254 * function execution with :
1265 * However, smp_call_function_single() does not seem to clearly execute
1266 * such barriers. It depends on spinlock semantic to provide the barrier
1267 * before executing the IPI and, when busy-looping, csd_lock_wait only
1268 * executes smp_mb() when it has to wait for the other CPU.
1270 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1271 * required ourself, even if duplicated. It has no performance impact
1274 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1275 * read and write vs write. They do not ensure core synchronization. We
1276 * really have to ensure total order between the 3 barriers running on
1279 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1280 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1281 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1282 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1283 /* Total order with IPI handler smp_mb() */
1285 smp_call_function_single(buf
->backend
.cpu
,
1286 remote_mb
, NULL
, 1);
1287 /* Total order with IPI handler smp_mb() */
1291 /* Total order with IPI handler smp_mb() */
1293 smp_call_function(remote_mb
, NULL
, 1);
1294 /* Total order with IPI handler smp_mb() */
1299 * Local rmb to match the remote wmb to read the commit count
1300 * before the buffer data and the write offset.
1305 write_offset
= v_read(config
, &buf
->offset
);
1308 * Check that the buffer we are getting is after or at consumed_cur
1311 if ((long) subbuf_trunc(consumed
, chan
)
1312 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1316 * Check that the subbuffer we are trying to consume has been
1317 * already fully committed.
1319 if (((commit_count
- chan
->backend
.subbuf_size
)
1320 & chan
->commit_count_mask
)
1321 - (buf_trunc(consumed
, chan
)
1322 >> chan
->backend
.num_subbuf_order
)
1327 * Check that we are not about to read the same subbuffer in
1328 * which the writer head is.
1330 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1335 * Failure to get the subbuffer causes a busy-loop retry without going
1336 * to a wait queue. These are caused by short-lived race windows where
1337 * the writer is getting access to a subbuffer we were trying to get
1338 * access to. Also checks that the "consumed" buffer count we are
1339 * looking for matches the one contained in the subbuffer id.
1341 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1342 consumed_idx
, buf_trunc_val(consumed
, chan
));
1345 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1347 buf
->get_subbuf_consumed
= consumed
;
1348 buf
->get_subbuf
= 1;
1350 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1356 * The memory barriers __wait_event()/wake_up_interruptible() take care
1357 * of "raw_spin_is_locked" memory ordering.
1361 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1366 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1369 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1372 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1374 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1375 struct channel
*chan
= bufb
->chan
;
1376 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1377 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1379 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1381 if (!buf
->get_subbuf
) {
1383 * Reader puts a subbuffer it did not get.
1385 CHAN_WARN_ON(chan
, 1);
1388 consumed
= buf
->get_subbuf_consumed
;
1389 buf
->get_subbuf
= 0;
1392 * Clear the records_unread counter. (overruns counter)
1393 * Can still be non-zero if a file reader simply grabbed the data
1394 * without using iterators.
1395 * Can be below zero if an iterator is used on a snapshot more than
1398 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1399 v_add(config
, v_read(config
,
1400 &bufb
->array
[read_sb_bindex
]->records_unread
),
1401 &bufb
->records_read
);
1402 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1403 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1404 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1405 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1408 * Exchange the reader subbuffer with the one we put in its place in the
1409 * writer subbuffer table. Expect the original consumed count. If
1410 * update_read_sb_index fails, this is because the writer updated the
1411 * subbuffer concurrently. We should therefore keep the subbuffer we
1412 * currently have: it has become invalid to try reading this sub-buffer
1413 * consumed count value anyway.
1415 consumed_idx
= subbuf_index(consumed
, chan
);
1416 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1417 consumed_idx
, buf_trunc_val(consumed
, chan
));
1419 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1420 * if the writer concurrently updated it.
1423 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1426 * cons_offset is an iterator on all subbuffer offsets between the reader
1427 * position and the writer position. (inclusive)
1430 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1431 struct channel
*chan
,
1432 unsigned long cons_offset
,
1435 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1436 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1438 cons_idx
= subbuf_index(cons_offset
, chan
);
1439 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1440 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1442 if (subbuf_offset(commit_count
, chan
) != 0)
1444 "ring buffer %s, cpu %d: "
1445 "commit count in subbuffer %lu,\n"
1446 "expecting multiples of %lu bytes\n"
1447 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1448 chan
->backend
.name
, cpu
, cons_idx
,
1449 chan
->backend
.subbuf_size
,
1450 commit_count
, commit_count_sb
);
1452 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1453 chan
->backend
.name
, cpu
, commit_count
);
1457 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1458 struct channel
*chan
,
1459 void *priv
, int cpu
)
1461 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1462 unsigned long write_offset
, cons_offset
;
1465 * No need to order commit_count, write_offset and cons_offset reads
1466 * because we execute at teardown when no more writer nor reader
1467 * references are left.
1469 write_offset
= v_read(config
, &buf
->offset
);
1470 cons_offset
= atomic_long_read(&buf
->consumed
);
1471 if (write_offset
!= cons_offset
)
1473 "ring buffer %s, cpu %d: "
1474 "non-consumed data\n"
1475 " [ %lu bytes written, %lu bytes read ]\n",
1476 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1478 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1479 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1482 cons_offset
= subbuf_align(cons_offset
, chan
))
1483 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1487 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1489 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1490 struct lib_ring_buffer
*buf
,
1493 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1495 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1496 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1497 "%lu records overrun\n",
1499 v_read(config
, &buf
->records_count
),
1500 v_read(config
, &buf
->records_overrun
));
1502 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1503 "%lu records overrun\n",
1504 chan
->backend
.name
, cpu
,
1505 v_read(config
, &buf
->records_count
),
1506 v_read(config
, &buf
->records_overrun
));
1511 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1512 struct lib_ring_buffer
*buf
,
1519 void lib_ring_buffer_print_errors(struct channel
*chan
,
1520 struct lib_ring_buffer
*buf
, int cpu
)
1522 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1523 void *priv
= chan
->backend
.priv
;
1525 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1526 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1527 if (v_read(config
, &buf
->records_lost_full
)
1528 || v_read(config
, &buf
->records_lost_wrap
)
1529 || v_read(config
, &buf
->records_lost_big
))
1531 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1532 " [ %lu buffer full, %lu nest buffer wrap-around, "
1533 "%lu event too big ]\n",
1534 chan
->backend
.name
, cpu
,
1535 v_read(config
, &buf
->records_lost_full
),
1536 v_read(config
, &buf
->records_lost_wrap
),
1537 v_read(config
, &buf
->records_lost_big
));
1539 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1543 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1545 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1548 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1549 struct channel
*chan
,
1550 struct switch_offsets
*offsets
,
1553 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1554 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1555 unsigned long commit_count
;
1556 struct commit_counters_hot
*cc_hot
;
1558 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1561 * Order all writes to buffer before the commit count update that will
1562 * determine that the subbuffer is full.
1564 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1566 * Must write slot data before incrementing commit count. This
1567 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1573 cc_hot
= &buf
->commit_hot
[oldidx
];
1574 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1575 commit_count
= v_read(config
, &cc_hot
->cc
);
1576 /* Check if the written buffer has to be delivered */
1577 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1578 commit_count
, oldidx
, tsc
);
1579 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1580 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1581 commit_count
, cc_hot
);
1585 * lib_ring_buffer_switch_old_end: switch old subbuffer
1587 * Note : offset_old should never be 0 here. It is ok, because we never perform
1588 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1589 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1593 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1594 struct channel
*chan
,
1595 struct switch_offsets
*offsets
,
1598 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1599 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1600 unsigned long commit_count
, padding_size
, data_size
;
1601 struct commit_counters_hot
*cc_hot
;
1604 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1605 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1606 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1608 ts_end
= &buf
->ts_end
[oldidx
];
1610 * This is the last space reservation in that sub-buffer before
1611 * it gets delivered. This provides exclusive access to write to
1612 * this sub-buffer's ts_end. There are also no concurrent
1613 * readers of that ts_end because delivery of that sub-buffer is
1614 * postponed until the commit counter is incremented for the
1615 * current space reservation.
1620 * Order all writes to buffer and store to ts_end before the commit
1621 * count update that will determine that the subbuffer is full.
1623 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1625 * Must write slot data before incrementing commit count. This
1626 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1632 cc_hot
= &buf
->commit_hot
[oldidx
];
1633 v_add(config
, padding_size
, &cc_hot
->cc
);
1634 commit_count
= v_read(config
, &cc_hot
->cc
);
1635 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1636 commit_count
, oldidx
, tsc
);
1637 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1638 offsets
->old
+ padding_size
, commit_count
,
1643 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1645 * This code can be executed unordered : writers may already have written to the
1646 * sub-buffer before this code gets executed, caution. The commit makes sure
1647 * that this code is executed before the deliver of this sub-buffer.
1650 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1651 struct channel
*chan
,
1652 struct switch_offsets
*offsets
,
1655 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1656 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1657 unsigned long commit_count
;
1658 struct commit_counters_hot
*cc_hot
;
1660 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1663 * Order all writes to buffer before the commit count update that will
1664 * determine that the subbuffer is full.
1666 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1668 * Must write slot data before incrementing commit count. This
1669 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1675 cc_hot
= &buf
->commit_hot
[beginidx
];
1676 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1677 commit_count
= v_read(config
, &cc_hot
->cc
);
1678 /* Check if the written buffer has to be delivered */
1679 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1680 commit_count
, beginidx
, tsc
);
1681 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1682 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1683 commit_count
, cc_hot
);
1687 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1689 * Calls subbuffer_set_data_size() to set the data size of the current
1690 * sub-buffer. We do not need to perform check_deliver nor commit here,
1691 * since this task will be done by the "commit" of the event for which
1692 * we are currently doing the space reservation.
1695 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1696 struct channel
*chan
,
1697 struct switch_offsets
*offsets
,
1700 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1701 unsigned long endidx
, data_size
;
1704 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1705 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1706 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1707 ts_end
= &buf
->ts_end
[endidx
];
1709 * This is the last space reservation in that sub-buffer before
1710 * it gets delivered. This provides exclusive access to write to
1711 * this sub-buffer's ts_end. There are also no concurrent
1712 * readers of that ts_end because delivery of that sub-buffer is
1713 * postponed until the commit counter is incremented for the
1714 * current space reservation.
1722 * !0 if execution must be aborted.
1725 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1726 struct lib_ring_buffer
*buf
,
1727 struct channel
*chan
,
1728 struct switch_offsets
*offsets
,
1731 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1732 unsigned long off
, reserve_commit_diff
;
1734 offsets
->begin
= v_read(config
, &buf
->offset
);
1735 offsets
->old
= offsets
->begin
;
1736 offsets
->switch_old_start
= 0;
1737 off
= subbuf_offset(offsets
->begin
, chan
);
1739 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1742 * Ensure we flush the header of an empty subbuffer when doing the
1743 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1744 * total data gathering duration even if there were no records saved
1745 * after the last buffer switch.
1746 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1747 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1748 * subbuffer header as appropriate.
1749 * The next record that reserves space will be responsible for
1750 * populating the following subbuffer header. We choose not to populate
1751 * the next subbuffer header here because we want to be able to use
1752 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1753 * buffer flush, which must guarantee that all the buffer content
1754 * (records and header timestamps) are visible to the reader. This is
1755 * required for quiescence guarantees for the fusion merge.
1757 if (mode
!= SWITCH_FLUSH
&& !off
)
1758 return -1; /* we do not have to switch : buffer is empty */
1760 if (unlikely(off
== 0)) {
1761 unsigned long sb_index
, commit_count
;
1764 * We are performing a SWITCH_FLUSH. At this stage, there are no
1765 * concurrent writes into the buffer.
1767 * The client does not save any header information. Don't
1768 * switch empty subbuffer on finalize, because it is invalid to
1769 * deliver a completely empty subbuffer.
1771 if (!config
->cb
.subbuffer_header_size())
1774 /* Test new buffer integrity */
1775 sb_index
= subbuf_index(offsets
->begin
, chan
);
1776 commit_count
= v_read(config
,
1777 &buf
->commit_cold
[sb_index
].cc_sb
);
1778 reserve_commit_diff
=
1779 (buf_trunc(offsets
->begin
, chan
)
1780 >> chan
->backend
.num_subbuf_order
)
1781 - (commit_count
& chan
->commit_count_mask
);
1782 if (likely(reserve_commit_diff
== 0)) {
1783 /* Next subbuffer not being written to. */
1784 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1785 subbuf_trunc(offsets
->begin
, chan
)
1786 - subbuf_trunc((unsigned long)
1787 atomic_long_read(&buf
->consumed
), chan
)
1788 >= chan
->backend
.buf_size
)) {
1790 * We do not overwrite non consumed buffers
1791 * and we are full : don't switch.
1796 * Next subbuffer not being written to, and we
1797 * are either in overwrite mode or the buffer is
1798 * not full. It's safe to write in this new
1804 * Next subbuffer reserve offset does not match the
1805 * commit offset. Don't perform switch in
1806 * producer-consumer and overwrite mode. Caused by
1807 * either a writer OOPS or too many nested writes over a
1808 * reserve/commit pair.
1814 * Need to write the subbuffer start header on finalize.
1816 offsets
->switch_old_start
= 1;
1818 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1819 /* Note: old points to the next subbuf at offset 0 */
1820 offsets
->end
= offsets
->begin
;
1825 * Force a sub-buffer switch. This operation is completely reentrant : can be
1826 * called while tracing is active with absolutely no lock held.
1828 * Note, however, that as a v_cmpxchg is used for some atomic
1829 * operations, this function must be called from the CPU which owns the buffer
1830 * for a ACTIVE flush.
1832 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1834 struct channel
*chan
= buf
->backend
.chan
;
1835 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1836 struct switch_offsets offsets
;
1837 unsigned long oldidx
;
1843 * Perform retryable operations.
1846 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1848 return; /* Switch not needed */
1849 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1853 * Atomically update last_tsc. This update races against concurrent
1854 * atomic updates, but the race will always cause supplementary full TSC
1855 * records, never the opposite (missing a full TSC record when it would
1858 save_last_tsc(config
, buf
, tsc
);
1861 * Push the reader if necessary
1863 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1865 oldidx
= subbuf_index(offsets
.old
, chan
);
1866 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1869 * May need to populate header start on SWITCH_FLUSH.
1871 if (offsets
.switch_old_start
) {
1872 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1873 offsets
.old
+= config
->cb
.subbuffer_header_size();
1877 * Switch old subbuffer.
1879 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1881 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1883 struct switch_param
{
1884 struct lib_ring_buffer
*buf
;
1885 enum switch_mode mode
;
1888 static void remote_switch(void *info
)
1890 struct switch_param
*param
= info
;
1891 struct lib_ring_buffer
*buf
= param
->buf
;
1893 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1896 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1897 enum switch_mode mode
)
1899 struct channel
*chan
= buf
->backend
.chan
;
1900 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1902 struct switch_param param
;
1905 * With global synchronization we don't need to use the IPI scheme.
1907 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1908 lib_ring_buffer_switch_slow(buf
, mode
);
1913 * Disabling preemption ensures two things: first, that the
1914 * target cpu is not taken concurrently offline while we are within
1915 * smp_call_function_single(). Secondly, if it happens that the
1916 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1917 * needs to be protected from CPU hotplug handlers, which can
1918 * also perform a remote subbuffer switch.
1923 ret
= smp_call_function_single(buf
->backend
.cpu
,
1924 remote_switch
, ¶m
, 1);
1926 /* Remote CPU is offline, do it ourself. */
1927 lib_ring_buffer_switch_slow(buf
, mode
);
1932 /* Switch sub-buffer if current sub-buffer is non-empty. */
1933 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1935 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1937 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1939 /* Switch sub-buffer even if current sub-buffer is empty. */
1940 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1942 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1944 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1946 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
)
1948 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1949 struct channel
*chan
= bufb
->chan
;
1951 lib_ring_buffer_switch_remote(buf
);
1952 lib_ring_buffer_clear_reader(buf
, chan
);
1954 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
1959 * -ENOSPC if event size is too large for packet.
1960 * -ENOBUFS if there is currently not enough space in buffer for the event.
1961 * -EIO if data cannot be written into the buffer for any other reason.
1964 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1965 struct channel
*chan
,
1966 struct switch_offsets
*offsets
,
1967 struct lib_ring_buffer_ctx
*ctx
,
1970 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1971 unsigned long reserve_commit_diff
, offset_cmp
;
1974 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1975 offsets
->old
= offsets
->begin
;
1976 offsets
->switch_new_start
= 0;
1977 offsets
->switch_new_end
= 0;
1978 offsets
->switch_old_end
= 0;
1979 offsets
->pre_header_padding
= 0;
1981 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1982 if ((int64_t) ctx
->tsc
== -EIO
)
1985 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1986 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1988 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1989 offsets
->switch_new_start
= 1; /* For offsets->begin */
1991 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1993 &offsets
->pre_header_padding
,
1996 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1999 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2000 offsets
->size
> chan
->backend
.subbuf_size
)) {
2001 offsets
->switch_old_end
= 1; /* For offsets->old */
2002 offsets
->switch_new_start
= 1; /* For offsets->begin */
2005 if (unlikely(offsets
->switch_new_start
)) {
2006 unsigned long sb_index
, commit_count
;
2009 * We are typically not filling the previous buffer completely.
2011 if (likely(offsets
->switch_old_end
))
2012 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2013 offsets
->begin
= offsets
->begin
2014 + config
->cb
.subbuffer_header_size();
2015 /* Test new buffer integrity */
2016 sb_index
= subbuf_index(offsets
->begin
, chan
);
2018 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2019 * lib_ring_buffer_check_deliver() has the matching
2020 * memory barriers required around commit_cold cc_sb
2021 * updates to ensure reserve and commit counter updates
2022 * are not seen reordered when updated by another CPU.
2025 commit_count
= v_read(config
,
2026 &buf
->commit_cold
[sb_index
].cc_sb
);
2027 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2029 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2031 * The reserve counter have been concurrently updated
2032 * while we read the commit counter. This means the
2033 * commit counter we read might not match buf->offset
2034 * due to concurrent update. We therefore need to retry.
2038 reserve_commit_diff
=
2039 (buf_trunc(offsets
->begin
, chan
)
2040 >> chan
->backend
.num_subbuf_order
)
2041 - (commit_count
& chan
->commit_count_mask
);
2042 if (likely(reserve_commit_diff
== 0)) {
2043 /* Next subbuffer not being written to. */
2044 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2045 subbuf_trunc(offsets
->begin
, chan
)
2046 - subbuf_trunc((unsigned long)
2047 atomic_long_read(&buf
->consumed
), chan
)
2048 >= chan
->backend
.buf_size
)) {
2050 * We do not overwrite non consumed buffers
2051 * and we are full : record is lost.
2053 v_inc(config
, &buf
->records_lost_full
);
2057 * Next subbuffer not being written to, and we
2058 * are either in overwrite mode or the buffer is
2059 * not full. It's safe to write in this new
2065 * Next subbuffer reserve offset does not match the
2066 * commit offset, and this did not involve update to the
2067 * reserve counter. Drop record in producer-consumer and
2068 * overwrite mode. Caused by either a writer OOPS or
2069 * too many nested writes over a reserve/commit pair.
2071 v_inc(config
, &buf
->records_lost_wrap
);
2075 config
->cb
.record_header_size(config
, chan
,
2077 &offsets
->pre_header_padding
,
2080 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2083 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2084 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2086 * Record too big for subbuffers, report error, don't
2087 * complete the sub-buffer switch.
2089 v_inc(config
, &buf
->records_lost_big
);
2093 * We just made a successful buffer switch and the
2094 * record fits in the new subbuffer. Let's write.
2099 * Record fits in the current buffer and we are not on a switch
2100 * boundary. It's safe to write.
2103 offsets
->end
= offsets
->begin
+ offsets
->size
;
2105 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2107 * The offset_end will fall at the very beginning of the next
2110 offsets
->switch_new_end
= 1; /* For offsets->begin */
2115 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2117 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2119 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2120 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2122 return chan
->backend
.buf
;
2125 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2127 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2128 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2130 v_inc(config
, &buf
->records_lost_big
);
2132 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2135 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2136 * @ctx: ring buffer context.
2138 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2139 * -EIO for other errors, else returns 0.
2140 * It will take care of sub-buffer switching.
2142 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
,
2145 struct channel
*chan
= ctx
->chan
;
2146 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2147 struct lib_ring_buffer
*buf
;
2148 struct switch_offsets offsets
;
2151 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2155 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2159 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2164 * Atomically update last_tsc. This update races against concurrent
2165 * atomic updates, but the race will always cause supplementary full TSC
2166 * records, never the opposite (missing a full TSC record when it would
2169 save_last_tsc(config
, buf
, ctx
->tsc
);
2172 * Push the reader if necessary
2174 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2177 * Clear noref flag for this subbuffer.
2179 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2180 subbuf_index(offsets
.end
- 1, chan
));
2183 * Switch old subbuffer if needed.
2185 if (unlikely(offsets
.switch_old_end
)) {
2186 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2187 subbuf_index(offsets
.old
- 1, chan
));
2188 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2192 * Populate new subbuffer.
2194 if (unlikely(offsets
.switch_new_start
))
2195 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2197 if (unlikely(offsets
.switch_new_end
))
2198 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2200 ctx
->slot_size
= offsets
.size
;
2201 ctx
->pre_offset
= offsets
.begin
;
2202 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2205 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2208 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2209 struct lib_ring_buffer
*buf
,
2210 unsigned long commit_count
,
2213 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2214 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2218 * The ring buffer can count events recorded and overwritten per buffer,
2219 * but it is disabled by default due to its performance overhead.
2221 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2223 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2224 struct lib_ring_buffer
*buf
,
2227 v_add(config
, subbuffer_get_records_count(config
,
2228 &buf
->backend
, idx
),
2229 &buf
->records_count
);
2230 v_add(config
, subbuffer_count_records_overrun(config
,
2231 &buf
->backend
, idx
),
2232 &buf
->records_overrun
);
2234 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2236 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2237 struct lib_ring_buffer
*buf
,
2241 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2244 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2245 struct lib_ring_buffer
*buf
,
2246 struct channel
*chan
,
2247 unsigned long offset
,
2248 unsigned long commit_count
,
2252 unsigned long old_commit_count
= commit_count
2253 - chan
->backend
.subbuf_size
;
2256 * If we succeeded at updating cc_sb below, we are the subbuffer
2257 * writer delivering the subbuffer. Deals with concurrent
2258 * updates of the "cc" value without adding a add_return atomic
2259 * operation to the fast path.
2261 * We are doing the delivery in two steps:
2262 * - First, we cmpxchg() cc_sb to the new value
2263 * old_commit_count + 1. This ensures that we are the only
2264 * subbuffer user successfully filling the subbuffer, but we
2265 * do _not_ set the cc_sb value to "commit_count" yet.
2266 * Therefore, other writers that would wrap around the ring
2267 * buffer and try to start writing to our subbuffer would
2268 * have to drop records, because it would appear as
2270 * We therefore have exclusive access to the subbuffer control
2271 * structures. This mutual exclusion with other writers is
2272 * crucially important to perform record overruns count in
2273 * flight recorder mode locklessly.
2274 * - When we are ready to release the subbuffer (either for
2275 * reading or for overrun by other writers), we simply set the
2276 * cc_sb value to "commit_count" and perform delivery.
2278 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2279 * This guarantees that old_commit_count + 1 != commit_count.
2283 * Order prior updates to reserve count prior to the
2284 * commit_cold cc_sb update.
2287 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2288 old_commit_count
, old_commit_count
+ 1)
2289 == old_commit_count
)) {
2293 * Start of exclusive subbuffer access. We are
2294 * guaranteed to be the last writer in this subbuffer
2295 * and any other writer trying to access this subbuffer
2296 * in this state is required to drop records.
2298 * We can read the ts_end for the current sub-buffer
2299 * which has been saved by the very last space
2300 * reservation for the current sub-buffer.
2302 * Order increment of commit counter before reading ts_end.
2305 ts_end
= &buf
->ts_end
[idx
];
2306 deliver_count_events(config
, buf
, idx
);
2307 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2308 lib_ring_buffer_get_data_size(config
,
2313 * Increment the packet counter while we have exclusive
2316 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2319 * Set noref flag and offset for this subbuffer id.
2320 * Contains a memory barrier that ensures counter stores
2321 * are ordered before set noref and offset.
2323 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2324 buf_trunc_val(offset
, chan
));
2327 * Order set_noref and record counter updates before the
2328 * end of subbuffer exclusive access. Orders with
2329 * respect to writers coming into the subbuffer after
2330 * wrap around, and also order wrt concurrent readers.
2333 /* End of exclusive subbuffer access */
2334 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2337 * Order later updates to reserve count after
2338 * the commit_cold cc_sb update.
2341 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2345 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2347 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2348 && atomic_long_read(&buf
->active_readers
)
2349 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2350 wake_up_interruptible(&buf
->read_wait
);
2351 wake_up_interruptible(&chan
->read_wait
);
2356 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2358 int __init
init_lib_ring_buffer_frontend(void)
2362 for_each_possible_cpu(cpu
)
2363 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2367 module_init(init_lib_ring_buffer_frontend
);
2369 void __exit
exit_lib_ring_buffer_frontend(void)
2373 module_exit(exit_lib_ring_buffer_frontend
);