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 <asm/cacheflush.h>
45 #include <ringbuffer/config.h>
46 #include <ringbuffer/backend.h>
47 #include <ringbuffer/frontend.h>
48 #include <ringbuffer/iterator.h>
49 #include <ringbuffer/nohz.h>
50 #include <wrapper/atomic.h>
51 #include <wrapper/kref.h>
52 #include <wrapper/percpu-defs.h>
53 #include <wrapper/timer.h>
54 #include <wrapper/vmalloc.h>
57 * Internal structure representing offsets to use at a sub-buffer switch.
59 struct switch_offsets
{
60 unsigned long begin
, end
, old
;
61 size_t pre_header_padding
, size
;
62 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
73 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
74 #endif /* CONFIG_NO_HZ */
76 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
78 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
79 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
82 void lib_ring_buffer_print_errors(struct channel
*chan
,
83 struct lib_ring_buffer
*buf
, int cpu
);
85 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
86 enum switch_mode mode
);
89 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
90 struct lib_ring_buffer
*buf
,
93 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
95 consumed_old
= atomic_long_read(&buf
->consumed
);
96 consumed_idx
= subbuf_index(consumed_old
, chan
);
97 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
99 * No memory barrier here, since we are only interested
100 * in a statistically correct polling result. The next poll will
101 * get the data is we are racing. The mb() that ensures correct
102 * memory order is in get_subbuf.
104 write_offset
= v_read(config
, &buf
->offset
);
107 * Check that the subbuffer we are trying to consume has been
108 * already fully committed.
111 if (((commit_count
- chan
->backend
.subbuf_size
)
112 & chan
->commit_count_mask
)
113 - (buf_trunc(consumed_old
, chan
)
114 >> chan
->backend
.num_subbuf_order
)
119 * Check that we are not about to read the same subbuffer in
120 * which the writer head is.
122 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
130 * Must be called under cpu hotplug protection.
132 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
134 struct channel
*chan
= buf
->backend
.chan
;
136 irq_work_sync(&buf
->wakeup_pending
);
138 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
139 lttng_kvfree(buf
->commit_hot
);
140 lttng_kvfree(buf
->commit_cold
);
141 lttng_kvfree(buf
->ts_end
);
143 lib_ring_buffer_backend_free(&buf
->backend
);
147 * lib_ring_buffer_reset - Reset ring buffer to initial values.
150 * Effectively empty the ring buffer. Should be called when the buffer is not
151 * used for writing. The ring buffer can be opened for reading, but the reader
152 * should not be using the iterator concurrently with reset. The previous
153 * current iterator record is reset.
155 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
157 struct channel
*chan
= buf
->backend
.chan
;
158 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
162 * Reset iterator first. It will put the subbuffer if it currently holds
165 lib_ring_buffer_iterator_reset(buf
);
166 v_set(config
, &buf
->offset
, 0);
167 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
168 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
169 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
170 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
173 atomic_long_set(&buf
->consumed
, 0);
174 atomic_set(&buf
->record_disabled
, 0);
175 v_set(config
, &buf
->last_tsc
, 0);
176 lib_ring_buffer_backend_reset(&buf
->backend
);
177 /* Don't reset number of active readers */
178 v_set(config
, &buf
->records_lost_full
, 0);
179 v_set(config
, &buf
->records_lost_wrap
, 0);
180 v_set(config
, &buf
->records_lost_big
, 0);
181 v_set(config
, &buf
->records_count
, 0);
182 v_set(config
, &buf
->records_overrun
, 0);
185 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
188 * channel_reset - Reset channel to initial values.
191 * Effectively empty the channel. Should be called when the channel is not used
192 * for writing. The channel can be opened for reading, but the reader should not
193 * be using the iterator concurrently with reset. The previous current iterator
196 void channel_reset(struct channel
*chan
)
199 * Reset iterators first. Will put the subbuffer if held for reading.
201 channel_iterator_reset(chan
);
202 atomic_set(&chan
->record_disabled
, 0);
203 /* Don't reset commit_count_mask, still valid */
204 channel_backend_reset(&chan
->backend
);
205 /* Don't reset switch/read timer interval */
206 /* Don't reset notifiers and notifier enable bits */
207 /* Don't reset reader reference count */
209 EXPORT_SYMBOL_GPL(channel_reset
);
211 static void lib_ring_buffer_pending_wakeup_buf(struct irq_work
*entry
)
213 struct lib_ring_buffer
*buf
= container_of(entry
, struct lib_ring_buffer
,
215 wake_up_interruptible(&buf
->read_wait
);
218 static void lib_ring_buffer_pending_wakeup_chan(struct irq_work
*entry
)
220 struct channel
*chan
= container_of(entry
, struct channel
, wakeup_pending
);
221 wake_up_interruptible(&chan
->read_wait
);
225 * Must be called under cpu hotplug protection.
227 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
228 struct channel_backend
*chanb
, int cpu
)
230 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
231 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
232 void *priv
= chanb
->priv
;
233 size_t subbuf_header_size
;
237 /* Test for cpu hotplug */
238 if (buf
->backend
.allocated
)
242 * Paranoia: per cpu dynamic allocation is not officially documented as
243 * zeroing the memory, so let's do it here too, just in case.
245 memset(buf
, 0, sizeof(*buf
));
247 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
252 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
253 * chan
->backend
.num_subbuf
,
254 1 << INTERNODE_CACHE_SHIFT
),
255 GFP_KERNEL
| __GFP_NOWARN
,
256 cpu_to_node(max(cpu
, 0)));
257 if (!buf
->commit_hot
) {
263 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
264 * chan
->backend
.num_subbuf
,
265 1 << INTERNODE_CACHE_SHIFT
),
266 GFP_KERNEL
| __GFP_NOWARN
,
267 cpu_to_node(max(cpu
, 0)));
268 if (!buf
->commit_cold
) {
274 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
275 * chan
->backend
.num_subbuf
,
276 1 << INTERNODE_CACHE_SHIFT
),
277 GFP_KERNEL
| __GFP_NOWARN
,
278 cpu_to_node(max(cpu
, 0)));
281 goto free_commit_cold
;
284 init_waitqueue_head(&buf
->read_wait
);
285 init_waitqueue_head(&buf
->write_wait
);
286 init_irq_work(&buf
->wakeup_pending
, lib_ring_buffer_pending_wakeup_buf
);
287 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
290 * Write the subbuffer header for first subbuffer so we know the total
291 * duration of data gathering.
293 subbuf_header_size
= config
->cb
.subbuffer_header_size();
294 v_set(config
, &buf
->offset
, subbuf_header_size
);
295 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
296 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
297 config
->cb
.buffer_begin(buf
, tsc
, 0);
298 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
300 if (config
->cb
.buffer_create
) {
301 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
307 * Ensure the buffer is ready before setting it to allocated and setting
309 * Used for cpu hotplug vs cpumask iteration.
312 buf
->backend
.allocated
= 1;
314 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
315 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
316 chan
->backend
.cpumask
));
317 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
324 lttng_kvfree(buf
->ts_end
);
326 lttng_kvfree(buf
->commit_cold
);
328 lttng_kvfree(buf
->commit_hot
);
330 lib_ring_buffer_backend_free(&buf
->backend
);
334 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
336 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, switch_timer
);
337 struct channel
*chan
= buf
->backend
.chan
;
338 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
341 * Only flush buffers periodically if readers are active.
343 if (atomic_long_read(&buf
->active_readers
))
344 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
346 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
347 lttng_mod_timer_pinned(&buf
->switch_timer
,
348 jiffies
+ chan
->switch_timer_interval
);
350 mod_timer(&buf
->switch_timer
,
351 jiffies
+ chan
->switch_timer_interval
);
355 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
357 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
359 struct channel
*chan
= buf
->backend
.chan
;
360 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
361 unsigned int flags
= 0;
363 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
366 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
367 flags
= LTTNG_TIMER_PINNED
;
369 lttng_timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
, buf
);
370 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
372 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
373 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
375 add_timer(&buf
->switch_timer
);
377 buf
->switch_timer_enabled
= 1;
381 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
383 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
385 struct channel
*chan
= buf
->backend
.chan
;
387 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
390 del_timer_sync(&buf
->switch_timer
);
391 buf
->switch_timer_enabled
= 0;
395 * Polling timer to check the channels for data.
397 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
399 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, read_timer
);
400 struct channel
*chan
= buf
->backend
.chan
;
401 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
403 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
405 if (atomic_long_read(&buf
->active_readers
)
406 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
407 wake_up_interruptible(&buf
->read_wait
);
408 wake_up_interruptible(&chan
->read_wait
);
411 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
412 lttng_mod_timer_pinned(&buf
->read_timer
,
413 jiffies
+ chan
->read_timer_interval
);
415 mod_timer(&buf
->read_timer
,
416 jiffies
+ chan
->read_timer_interval
);
420 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
422 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
424 struct channel
*chan
= buf
->backend
.chan
;
425 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
426 unsigned int flags
= 0;
428 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
429 || !chan
->read_timer_interval
430 || buf
->read_timer_enabled
)
433 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
434 flags
= LTTNG_TIMER_PINNED
;
436 lttng_timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
, buf
);
437 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
439 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
440 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
442 add_timer(&buf
->read_timer
);
444 buf
->read_timer_enabled
= 1;
448 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
450 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
452 struct channel
*chan
= buf
->backend
.chan
;
453 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
455 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
456 || !chan
->read_timer_interval
457 || !buf
->read_timer_enabled
)
460 del_timer_sync(&buf
->read_timer
);
462 * do one more check to catch data that has been written in the last
465 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
466 wake_up_interruptible(&buf
->read_wait
);
467 wake_up_interruptible(&chan
->read_wait
);
469 buf
->read_timer_enabled
= 0;
472 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
474 enum cpuhp_state lttng_rb_hp_prepare
;
475 enum cpuhp_state lttng_rb_hp_online
;
477 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
479 lttng_rb_hp_prepare
= val
;
481 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
483 void lttng_rb_set_hp_online(enum cpuhp_state val
)
485 lttng_rb_hp_online
= val
;
487 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
489 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
490 struct lttng_cpuhp_node
*node
)
492 struct channel
*chan
= container_of(node
, struct channel
,
494 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
495 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
497 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
500 * Performing a buffer switch on a remote CPU. Performed by
501 * the CPU responsible for doing the hotunplug after the target
502 * CPU stopped running completely. Ensures that all data
503 * from that remote CPU is flushed.
505 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
508 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
510 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
511 struct lttng_cpuhp_node
*node
)
513 struct channel
*chan
= container_of(node
, struct channel
,
515 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
516 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
518 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
520 wake_up_interruptible(&chan
->hp_wait
);
521 lib_ring_buffer_start_switch_timer(buf
);
522 lib_ring_buffer_start_read_timer(buf
);
525 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
527 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
528 struct lttng_cpuhp_node
*node
)
530 struct channel
*chan
= container_of(node
, struct channel
,
532 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
533 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
535 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
537 lib_ring_buffer_stop_switch_timer(buf
);
538 lib_ring_buffer_stop_read_timer(buf
);
541 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
543 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
545 #ifdef CONFIG_HOTPLUG_CPU
548 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
549 * @nb: notifier block
550 * @action: hotplug action to take
553 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
556 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
557 unsigned long action
,
560 unsigned int cpu
= (unsigned long)hcpu
;
561 struct channel
*chan
= container_of(nb
, struct channel
,
563 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
564 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
566 if (!chan
->cpu_hp_enable
)
569 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
572 case CPU_DOWN_FAILED
:
573 case CPU_DOWN_FAILED_FROZEN
:
575 case CPU_ONLINE_FROZEN
:
576 wake_up_interruptible(&chan
->hp_wait
);
577 lib_ring_buffer_start_switch_timer(buf
);
578 lib_ring_buffer_start_read_timer(buf
);
581 case CPU_DOWN_PREPARE
:
582 case CPU_DOWN_PREPARE_FROZEN
:
583 lib_ring_buffer_stop_switch_timer(buf
);
584 lib_ring_buffer_stop_read_timer(buf
);
588 case CPU_DEAD_FROZEN
:
590 * Performing a buffer switch on a remote CPU. Performed by
591 * the CPU responsible for doing the hotunplug after the target
592 * CPU stopped running completely. Ensures that all data
593 * from that remote CPU is flushed.
595 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
605 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
607 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
609 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
610 * that wake-up-tracing generated events are flushed before going idle (in
611 * tick_nohz). We test if the spinlock is locked to deal with the race where
612 * readers try to sample the ring buffer before we perform the switch. We let
613 * the readers retry in that case. If there is data in the buffer, the wake up
614 * is going to forbid the CPU running the reader thread from going idle.
616 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
620 struct channel
*chan
= container_of(nb
, struct channel
,
622 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
623 struct lib_ring_buffer
*buf
;
624 int cpu
= smp_processor_id();
626 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
628 * We don't support keeping the system idle with global buffers
629 * and streaming active. In order to do so, we would need to
630 * sample a non-nohz-cpumask racelessly with the nohz updates
631 * without adding synchronization overhead to nohz. Leave this
632 * use-case out for now.
637 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
639 case TICK_NOHZ_FLUSH
:
640 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
641 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
642 && chan
->read_timer_interval
643 && atomic_long_read(&buf
->active_readers
)
644 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
645 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
646 wake_up_interruptible(&buf
->read_wait
);
647 wake_up_interruptible(&chan
->read_wait
);
649 if (chan
->switch_timer_interval
)
650 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
651 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
654 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
655 lib_ring_buffer_stop_switch_timer(buf
);
656 lib_ring_buffer_stop_read_timer(buf
);
657 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
659 case TICK_NOHZ_RESTART
:
660 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
661 lib_ring_buffer_start_read_timer(buf
);
662 lib_ring_buffer_start_switch_timer(buf
);
663 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
670 void notrace
lib_ring_buffer_tick_nohz_flush(void)
672 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
676 void notrace
lib_ring_buffer_tick_nohz_stop(void)
678 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
682 void notrace
lib_ring_buffer_tick_nohz_restart(void)
684 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
687 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
692 static void channel_unregister_notifiers(struct channel
*chan
)
694 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
696 channel_iterator_unregister_notifiers(chan
);
697 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
700 * Remove the nohz notifier first, so we are certain we stop
703 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
704 &chan
->tick_nohz_notifier
);
706 * ring_buffer_nohz_lock will not be needed below, because
707 * we just removed the notifiers, which were the only source of
710 #endif /* CONFIG_NO_HZ */
711 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
715 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
716 &chan
->cpuhp_online
.node
);
718 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
719 &chan
->cpuhp_prepare
.node
);
722 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
726 #ifdef CONFIG_HOTPLUG_CPU
728 chan
->cpu_hp_enable
= 0;
729 for_each_online_cpu(cpu
) {
730 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
732 lib_ring_buffer_stop_switch_timer(buf
);
733 lib_ring_buffer_stop_read_timer(buf
);
736 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
738 for_each_possible_cpu(cpu
) {
739 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
741 lib_ring_buffer_stop_switch_timer(buf
);
742 lib_ring_buffer_stop_read_timer(buf
);
746 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
748 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
750 lib_ring_buffer_stop_switch_timer(buf
);
751 lib_ring_buffer_stop_read_timer(buf
);
753 channel_backend_unregister_notifiers(&chan
->backend
);
756 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
758 if (!buf
->quiescent
) {
759 buf
->quiescent
= true;
760 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
764 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
766 buf
->quiescent
= false;
769 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
772 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
774 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
776 for_each_channel_cpu(cpu
, chan
) {
777 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
780 lib_ring_buffer_set_quiescent(buf
);
784 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
786 lib_ring_buffer_set_quiescent(buf
);
789 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
791 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
794 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
796 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
798 for_each_channel_cpu(cpu
, chan
) {
799 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
802 lib_ring_buffer_clear_quiescent(buf
);
806 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
808 lib_ring_buffer_clear_quiescent(buf
);
811 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
813 static void channel_free(struct channel
*chan
)
815 if (chan
->backend
.release_priv_ops
) {
816 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
818 channel_iterator_free(chan
);
819 channel_backend_free(&chan
->backend
);
824 * channel_create - Create channel.
825 * @config: ring buffer instance configuration
826 * @name: name of the channel
827 * @priv: ring buffer client private data
828 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
829 * address mapping. It is used only by RING_BUFFER_STATIC
830 * configuration. It can be set to NULL for other backends.
831 * @subbuf_size: subbuffer size
832 * @num_subbuf: number of subbuffers
833 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
834 * padding to let readers get those sub-buffers.
835 * Used for live streaming.
836 * @read_timer_interval: Time interval (in us) to wake up pending readers.
839 * Returns NULL on failure.
841 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
842 const char *name
, void *priv
, void *buf_addr
,
844 size_t num_subbuf
, unsigned int switch_timer_interval
,
845 unsigned int read_timer_interval
)
848 struct channel
*chan
;
850 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
851 read_timer_interval
))
854 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
858 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
859 subbuf_size
, num_subbuf
);
863 ret
= channel_iterator_init(chan
);
865 goto error_free_backend
;
867 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
868 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
869 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
870 kref_init(&chan
->ref
);
871 init_waitqueue_head(&chan
->read_wait
);
872 init_waitqueue_head(&chan
->hp_wait
);
873 init_irq_work(&chan
->wakeup_pending
, lib_ring_buffer_pending_wakeup_chan
);
875 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
876 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
877 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
878 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
879 &chan
->cpuhp_prepare
.node
);
881 goto cpuhp_prepare_error
;
883 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
884 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
885 &chan
->cpuhp_online
.node
);
887 goto cpuhp_online_error
;
888 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
892 * In case of non-hotplug cpu, if the ring-buffer is allocated
893 * in early initcall, it will not be notified of secondary cpus.
894 * In that off case, we need to allocate for all possible cpus.
896 #ifdef CONFIG_HOTPLUG_CPU
897 chan
->cpu_hp_notifier
.notifier_call
=
898 lib_ring_buffer_cpu_hp_callback
;
899 chan
->cpu_hp_notifier
.priority
= 6;
900 register_cpu_notifier(&chan
->cpu_hp_notifier
);
903 for_each_online_cpu(cpu
) {
904 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
906 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
907 lib_ring_buffer_start_switch_timer(buf
);
908 lib_ring_buffer_start_read_timer(buf
);
909 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
911 chan
->cpu_hp_enable
= 1;
914 for_each_possible_cpu(cpu
) {
915 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
917 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
918 lib_ring_buffer_start_switch_timer(buf
);
919 lib_ring_buffer_start_read_timer(buf
);
920 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
924 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
926 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
927 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
928 chan
->tick_nohz_notifier
.notifier_call
=
929 ring_buffer_tick_nohz_callback
;
930 chan
->tick_nohz_notifier
.priority
= ~0U;
931 atomic_notifier_chain_register(&tick_nohz_notifier
,
932 &chan
->tick_nohz_notifier
);
933 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
936 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
938 lib_ring_buffer_start_switch_timer(buf
);
939 lib_ring_buffer_start_read_timer(buf
);
944 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
946 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
947 &chan
->cpuhp_prepare
.node
);
950 #endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
952 channel_backend_free(&chan
->backend
);
957 EXPORT_SYMBOL_GPL(channel_create
);
960 void channel_release(struct kref
*kref
)
962 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
967 * channel_destroy - Finalize, wait for q.s. and destroy channel.
968 * @chan: channel to destroy
971 * Call "destroy" callback, finalize channels, and then decrement the
972 * channel reference count. Note that when readers have completed data
973 * consumption of finalized channels, get_subbuf() will return -ENODATA.
974 * They should release their handle at that point. Returns the private
977 void *channel_destroy(struct channel
*chan
)
980 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
983 irq_work_sync(&chan
->wakeup_pending
);
985 channel_unregister_notifiers(chan
);
987 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
989 * No need to hold cpu hotplug, because all notifiers have been
992 for_each_channel_cpu(cpu
, chan
) {
993 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
996 if (config
->cb
.buffer_finalize
)
997 config
->cb
.buffer_finalize(buf
,
1001 * Perform flush before writing to finalized.
1004 WRITE_ONCE(buf
->finalized
, 1);
1005 wake_up_interruptible(&buf
->read_wait
);
1008 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
1010 if (config
->cb
.buffer_finalize
)
1011 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
1013 * Perform flush before writing to finalized.
1016 WRITE_ONCE(buf
->finalized
, 1);
1017 wake_up_interruptible(&buf
->read_wait
);
1019 WRITE_ONCE(chan
->finalized
, 1);
1020 wake_up_interruptible(&chan
->hp_wait
);
1021 wake_up_interruptible(&chan
->read_wait
);
1022 priv
= chan
->backend
.priv
;
1023 kref_put(&chan
->ref
, channel_release
);
1026 EXPORT_SYMBOL_GPL(channel_destroy
);
1028 struct lib_ring_buffer
*channel_get_ring_buffer(
1029 const struct lib_ring_buffer_config
*config
,
1030 struct channel
*chan
, int cpu
)
1032 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1033 return chan
->backend
.buf
;
1035 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1037 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1039 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1041 struct channel
*chan
= buf
->backend
.chan
;
1043 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1045 if (!lttng_kref_get(&chan
->ref
)) {
1046 atomic_long_dec(&buf
->active_readers
);
1049 lttng_smp_mb__after_atomic();
1052 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1054 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1056 struct channel
*chan
= buf
->backend
.chan
;
1058 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1059 lttng_smp_mb__before_atomic();
1060 atomic_long_dec(&buf
->active_readers
);
1061 kref_put(&chan
->ref
, channel_release
);
1063 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1066 * Promote compiler barrier to a smp_mb().
1067 * For the specific ring buffer case, this IPI call should be removed if the
1068 * architecture does not reorder writes. This should eventually be provided by
1069 * a separate architecture-specific infrastructure.
1071 static void remote_mb(void *info
)
1077 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1079 * @consumed: consumed count indicating the position where to read
1080 * @produced: produced count, indicates position when to stop reading
1082 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1083 * data to read at consumed position, or 0 if the get operation succeeds.
1084 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1087 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1088 unsigned long *consumed
, unsigned long *produced
)
1090 struct channel
*chan
= buf
->backend
.chan
;
1091 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1092 unsigned long consumed_cur
, write_offset
;
1096 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1098 * Read finalized before counters.
1101 consumed_cur
= atomic_long_read(&buf
->consumed
);
1103 * No need to issue a memory barrier between consumed count read and
1104 * write offset read, because consumed count can only change
1105 * concurrently in overwrite mode, and we keep a sequence counter
1106 * identifier derived from the write offset to check we are getting
1107 * the same sub-buffer we are expecting (the sub-buffers are atomically
1108 * "tagged" upon writes, tags are checked upon read).
1110 write_offset
= v_read(config
, &buf
->offset
);
1113 * Check that we are not about to read the same subbuffer in
1114 * which the writer head is.
1116 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1120 *consumed
= consumed_cur
;
1121 *produced
= subbuf_trunc(write_offset
, chan
);
1127 * The memory barriers __wait_event()/wake_up_interruptible() take care
1128 * of "raw_spin_is_locked" memory ordering.
1132 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1137 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1140 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1141 * are saved regardless of whether the consumed and produced positions are
1142 * in the same subbuffer.
1144 * @consumed: consumed byte count indicating the last position read
1145 * @produced: produced byte count indicating the last position written
1147 * This function is meant to provide information on the exact producer and
1148 * consumer positions without regard for the "snapshot" feature.
1150 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1151 unsigned long *consumed
, unsigned long *produced
)
1153 struct channel
*chan
= buf
->backend
.chan
;
1154 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1157 *consumed
= atomic_long_read(&buf
->consumed
);
1159 * No need to issue a memory barrier between consumed count read and
1160 * write offset read, because consumed count can only change
1161 * concurrently in overwrite mode, and we keep a sequence counter
1162 * identifier derived from the write offset to check we are getting
1163 * the same sub-buffer we are expecting (the sub-buffers are atomically
1164 * "tagged" upon writes, tags are checked upon read).
1166 *produced
= v_read(config
, &buf
->offset
);
1171 * lib_ring_buffer_put_snapshot - move consumed counter forward
1173 * Should only be called from consumer context.
1175 * @consumed_new: new consumed count value
1177 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1178 unsigned long consumed_new
)
1180 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1181 struct channel
*chan
= bufb
->chan
;
1182 unsigned long consumed
;
1184 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1187 * Only push the consumed value forward.
1188 * If the consumed cmpxchg fails, this is because we have been pushed by
1189 * the writer in flight recorder mode.
1191 consumed
= atomic_long_read(&buf
->consumed
);
1192 while ((long) consumed
- (long) consumed_new
< 0)
1193 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1195 /* Wake-up the metadata producer */
1196 wake_up_interruptible(&buf
->write_wait
);
1198 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1200 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1201 static void lib_ring_buffer_flush_read_subbuf_dcache(
1202 const struct lib_ring_buffer_config
*config
,
1203 struct channel
*chan
,
1204 struct lib_ring_buffer
*buf
)
1206 struct lib_ring_buffer_backend_pages
*pages
;
1207 unsigned long sb_bindex
, id
, i
, nr_pages
;
1209 if (config
->output
!= RING_BUFFER_MMAP
)
1213 * Architectures with caches aliased on virtual addresses may
1214 * use different cache lines for the linear mapping vs
1215 * user-space memory mapping. Given that the ring buffer is
1216 * based on the kernel linear mapping, aligning it with the
1217 * user-space mapping is not straightforward, and would require
1218 * extra TLB entries. Therefore, simply flush the dcache for the
1219 * entire sub-buffer before reading it.
1221 id
= buf
->backend
.buf_rsb
.id
;
1222 sb_bindex
= subbuffer_id_get_index(config
, id
);
1223 pages
= buf
->backend
.array
[sb_bindex
];
1224 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1225 for (i
= 0; i
< nr_pages
; i
++) {
1226 struct lib_ring_buffer_backend_page
*backend_page
;
1228 backend_page
= &pages
->p
[i
];
1229 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1233 static void lib_ring_buffer_flush_read_subbuf_dcache(
1234 const struct lib_ring_buffer_config
*config
,
1235 struct channel
*chan
,
1236 struct lib_ring_buffer
*buf
)
1242 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1244 * @consumed: consumed count indicating the position where to read
1246 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1247 * data to read at consumed position, or 0 if the get operation succeeds.
1248 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1250 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1251 unsigned long consumed
)
1253 struct channel
*chan
= buf
->backend
.chan
;
1254 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1255 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1259 if (buf
->get_subbuf
) {
1261 * Reader is trying to get a subbuffer twice.
1263 CHAN_WARN_ON(chan
, 1);
1267 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1269 * Read finalized before counters.
1272 consumed_cur
= atomic_long_read(&buf
->consumed
);
1273 consumed_idx
= subbuf_index(consumed
, chan
);
1274 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1276 * Make sure we read the commit count before reading the buffer
1277 * data and the write offset. Correct consumed offset ordering
1278 * wrt commit count is insured by the use of cmpxchg to update
1279 * the consumed offset.
1280 * smp_call_function_single can fail if the remote CPU is offline,
1281 * this is OK because then there is no wmb to execute there.
1282 * If our thread is executing on the same CPU as the on the buffers
1283 * belongs to, we don't have to synchronize it at all. If we are
1284 * migrated, the scheduler will take care of the memory barriers.
1285 * Normally, smp_call_function_single() should ensure program order when
1286 * executing the remote function, which implies that it surrounds the
1287 * function execution with :
1298 * However, smp_call_function_single() does not seem to clearly execute
1299 * such barriers. It depends on spinlock semantic to provide the barrier
1300 * before executing the IPI and, when busy-looping, csd_lock_wait only
1301 * executes smp_mb() when it has to wait for the other CPU.
1303 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1304 * required ourself, even if duplicated. It has no performance impact
1307 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1308 * read and write vs write. They do not ensure core synchronization. We
1309 * really have to ensure total order between the 3 barriers running on
1312 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1313 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1314 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1315 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1316 /* Total order with IPI handler smp_mb() */
1318 smp_call_function_single(buf
->backend
.cpu
,
1319 remote_mb
, NULL
, 1);
1320 /* Total order with IPI handler smp_mb() */
1324 /* Total order with IPI handler smp_mb() */
1326 smp_call_function(remote_mb
, NULL
, 1);
1327 /* Total order with IPI handler smp_mb() */
1332 * Local rmb to match the remote wmb to read the commit count
1333 * before the buffer data and the write offset.
1338 write_offset
= v_read(config
, &buf
->offset
);
1341 * Check that the buffer we are getting is after or at consumed_cur
1344 if ((long) subbuf_trunc(consumed
, chan
)
1345 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1349 * Check that the subbuffer we are trying to consume has been
1350 * already fully committed.
1352 if (((commit_count
- chan
->backend
.subbuf_size
)
1353 & chan
->commit_count_mask
)
1354 - (buf_trunc(consumed
, chan
)
1355 >> chan
->backend
.num_subbuf_order
)
1360 * Check that we are not about to read the same subbuffer in
1361 * which the writer head is.
1363 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1368 * Failure to get the subbuffer causes a busy-loop retry without going
1369 * to a wait queue. These are caused by short-lived race windows where
1370 * the writer is getting access to a subbuffer we were trying to get
1371 * access to. Also checks that the "consumed" buffer count we are
1372 * looking for matches the one contained in the subbuffer id.
1374 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1375 consumed_idx
, buf_trunc_val(consumed
, chan
));
1378 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1380 buf
->get_subbuf_consumed
= consumed
;
1381 buf
->get_subbuf
= 1;
1383 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1389 * The memory barriers __wait_event()/wake_up_interruptible() take care
1390 * of "raw_spin_is_locked" memory ordering.
1394 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1399 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1402 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1405 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1407 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1408 struct channel
*chan
= bufb
->chan
;
1409 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1410 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1412 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1414 if (!buf
->get_subbuf
) {
1416 * Reader puts a subbuffer it did not get.
1418 CHAN_WARN_ON(chan
, 1);
1421 consumed
= buf
->get_subbuf_consumed
;
1422 buf
->get_subbuf
= 0;
1425 * Clear the records_unread counter. (overruns counter)
1426 * Can still be non-zero if a file reader simply grabbed the data
1427 * without using iterators.
1428 * Can be below zero if an iterator is used on a snapshot more than
1431 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1432 v_add(config
, v_read(config
,
1433 &bufb
->array
[read_sb_bindex
]->records_unread
),
1434 &bufb
->records_read
);
1435 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1436 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1437 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1438 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1441 * Exchange the reader subbuffer with the one we put in its place in the
1442 * writer subbuffer table. Expect the original consumed count. If
1443 * update_read_sb_index fails, this is because the writer updated the
1444 * subbuffer concurrently. We should therefore keep the subbuffer we
1445 * currently have: it has become invalid to try reading this sub-buffer
1446 * consumed count value anyway.
1448 consumed_idx
= subbuf_index(consumed
, chan
);
1449 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1450 consumed_idx
, buf_trunc_val(consumed
, chan
));
1452 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1453 * if the writer concurrently updated it.
1456 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1459 * cons_offset is an iterator on all subbuffer offsets between the reader
1460 * position and the writer position. (inclusive)
1463 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1464 struct channel
*chan
,
1465 unsigned long cons_offset
,
1468 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1469 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1471 cons_idx
= subbuf_index(cons_offset
, chan
);
1472 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1473 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1475 if (subbuf_offset(commit_count
, chan
) != 0)
1477 "LTTng: ring buffer %s, cpu %d: "
1478 "commit count in subbuffer %lu,\n"
1479 "expecting multiples of %lu bytes\n"
1480 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1481 chan
->backend
.name
, cpu
, cons_idx
,
1482 chan
->backend
.subbuf_size
,
1483 commit_count
, commit_count_sb
);
1485 printk(KERN_DEBUG
"LTTng: ring buffer: %s, cpu %d: %lu bytes committed\n",
1486 chan
->backend
.name
, cpu
, commit_count
);
1490 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1491 struct channel
*chan
,
1492 void *priv
, int cpu
)
1494 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1495 unsigned long write_offset
, cons_offset
;
1498 * No need to order commit_count, write_offset and cons_offset reads
1499 * because we execute at teardown when no more writer nor reader
1500 * references are left.
1502 write_offset
= v_read(config
, &buf
->offset
);
1503 cons_offset
= atomic_long_read(&buf
->consumed
);
1504 if (write_offset
!= cons_offset
)
1506 "LTTng: ring buffer %s, cpu %d: "
1507 "non-consumed data\n"
1508 " [ %lu bytes written, %lu bytes read ]\n",
1509 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1511 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1512 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1515 cons_offset
= subbuf_align(cons_offset
, chan
))
1516 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1520 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1522 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1523 struct lib_ring_buffer
*buf
,
1526 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1528 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1529 printk(KERN_DEBUG
"LTTng: ring buffer %s: %lu records written, "
1530 "%lu records overrun\n",
1532 v_read(config
, &buf
->records_count
),
1533 v_read(config
, &buf
->records_overrun
));
1535 printk(KERN_DEBUG
"LTTng: ring buffer %s, cpu %d: %lu records written, "
1536 "%lu records overrun\n",
1537 chan
->backend
.name
, cpu
,
1538 v_read(config
, &buf
->records_count
),
1539 v_read(config
, &buf
->records_overrun
));
1544 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1545 struct lib_ring_buffer
*buf
,
1552 void lib_ring_buffer_print_errors(struct channel
*chan
,
1553 struct lib_ring_buffer
*buf
, int cpu
)
1555 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1556 void *priv
= chan
->backend
.priv
;
1558 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1559 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1560 if (v_read(config
, &buf
->records_lost_full
)
1561 || v_read(config
, &buf
->records_lost_wrap
)
1562 || v_read(config
, &buf
->records_lost_big
))
1564 "LTTng: ring buffer %s, cpu %d: records were lost. Caused by:\n"
1565 " [ %lu buffer full, %lu nest buffer wrap-around, "
1566 "%lu event too big ]\n",
1567 chan
->backend
.name
, cpu
,
1568 v_read(config
, &buf
->records_lost_full
),
1569 v_read(config
, &buf
->records_lost_wrap
),
1570 v_read(config
, &buf
->records_lost_big
));
1572 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1576 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1578 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1581 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1582 struct channel
*chan
,
1583 struct switch_offsets
*offsets
,
1586 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1587 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1588 unsigned long commit_count
;
1589 struct commit_counters_hot
*cc_hot
;
1591 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1594 * Order all writes to buffer before the commit count update that will
1595 * determine that the subbuffer is full.
1597 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1599 * Must write slot data before incrementing commit count. This
1600 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1606 cc_hot
= &buf
->commit_hot
[oldidx
];
1607 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1608 commit_count
= v_read(config
, &cc_hot
->cc
);
1609 /* Check if the written buffer has to be delivered */
1610 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1611 commit_count
, oldidx
, tsc
);
1612 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1613 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1614 commit_count
, cc_hot
);
1618 * lib_ring_buffer_switch_old_end: switch old subbuffer
1620 * Note : offset_old should never be 0 here. It is ok, because we never perform
1621 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1622 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1626 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1627 struct channel
*chan
,
1628 struct switch_offsets
*offsets
,
1631 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1632 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1633 unsigned long commit_count
, padding_size
, data_size
;
1634 struct commit_counters_hot
*cc_hot
;
1637 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1638 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1639 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1641 ts_end
= &buf
->ts_end
[oldidx
];
1643 * This is the last space reservation in that sub-buffer before
1644 * it gets delivered. This provides exclusive access to write to
1645 * this sub-buffer's ts_end. There are also no concurrent
1646 * readers of that ts_end because delivery of that sub-buffer is
1647 * postponed until the commit counter is incremented for the
1648 * current space reservation.
1653 * Order all writes to buffer and store to ts_end before the commit
1654 * count update that will determine that the subbuffer is full.
1656 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1658 * Must write slot data before incrementing commit count. This
1659 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1665 cc_hot
= &buf
->commit_hot
[oldidx
];
1666 v_add(config
, padding_size
, &cc_hot
->cc
);
1667 commit_count
= v_read(config
, &cc_hot
->cc
);
1668 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1669 commit_count
, oldidx
, tsc
);
1670 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1671 offsets
->old
+ padding_size
, commit_count
,
1676 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1678 * This code can be executed unordered : writers may already have written to the
1679 * sub-buffer before this code gets executed, caution. The commit makes sure
1680 * that this code is executed before the deliver of this sub-buffer.
1683 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1684 struct channel
*chan
,
1685 struct switch_offsets
*offsets
,
1688 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1689 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1690 unsigned long commit_count
;
1691 struct commit_counters_hot
*cc_hot
;
1693 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1696 * Order all writes to buffer before the commit count update that will
1697 * determine that the subbuffer is full.
1699 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1701 * Must write slot data before incrementing commit count. This
1702 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1708 cc_hot
= &buf
->commit_hot
[beginidx
];
1709 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1710 commit_count
= v_read(config
, &cc_hot
->cc
);
1711 /* Check if the written buffer has to be delivered */
1712 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1713 commit_count
, beginidx
, tsc
);
1714 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1715 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1716 commit_count
, cc_hot
);
1720 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1722 * Calls subbuffer_set_data_size() to set the data size of the current
1723 * sub-buffer. We do not need to perform check_deliver nor commit here,
1724 * since this task will be done by the "commit" of the event for which
1725 * we are currently doing the space reservation.
1728 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1729 struct channel
*chan
,
1730 struct switch_offsets
*offsets
,
1733 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1734 unsigned long endidx
, data_size
;
1737 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1738 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1739 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1740 ts_end
= &buf
->ts_end
[endidx
];
1742 * This is the last space reservation in that sub-buffer before
1743 * it gets delivered. This provides exclusive access to write to
1744 * this sub-buffer's ts_end. There are also no concurrent
1745 * readers of that ts_end because delivery of that sub-buffer is
1746 * postponed until the commit counter is incremented for the
1747 * current space reservation.
1755 * !0 if execution must be aborted.
1758 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1759 struct lib_ring_buffer
*buf
,
1760 struct channel
*chan
,
1761 struct switch_offsets
*offsets
,
1764 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1765 unsigned long off
, reserve_commit_diff
;
1767 offsets
->begin
= v_read(config
, &buf
->offset
);
1768 offsets
->old
= offsets
->begin
;
1769 offsets
->switch_old_start
= 0;
1770 off
= subbuf_offset(offsets
->begin
, chan
);
1772 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1775 * Ensure we flush the header of an empty subbuffer when doing the
1776 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1777 * total data gathering duration even if there were no records saved
1778 * after the last buffer switch.
1779 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1780 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1781 * subbuffer header as appropriate.
1782 * The next record that reserves space will be responsible for
1783 * populating the following subbuffer header. We choose not to populate
1784 * the next subbuffer header here because we want to be able to use
1785 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1786 * buffer flush, which must guarantee that all the buffer content
1787 * (records and header timestamps) are visible to the reader. This is
1788 * required for quiescence guarantees for the fusion merge.
1790 if (mode
!= SWITCH_FLUSH
&& !off
)
1791 return -1; /* we do not have to switch : buffer is empty */
1793 if (unlikely(off
== 0)) {
1794 unsigned long sb_index
, commit_count
;
1797 * We are performing a SWITCH_FLUSH. At this stage, there are no
1798 * concurrent writes into the buffer.
1800 * The client does not save any header information. Don't
1801 * switch empty subbuffer on finalize, because it is invalid to
1802 * deliver a completely empty subbuffer.
1804 if (!config
->cb
.subbuffer_header_size())
1807 /* Test new buffer integrity */
1808 sb_index
= subbuf_index(offsets
->begin
, chan
);
1809 commit_count
= v_read(config
,
1810 &buf
->commit_cold
[sb_index
].cc_sb
);
1811 reserve_commit_diff
=
1812 (buf_trunc(offsets
->begin
, chan
)
1813 >> chan
->backend
.num_subbuf_order
)
1814 - (commit_count
& chan
->commit_count_mask
);
1815 if (likely(reserve_commit_diff
== 0)) {
1816 /* Next subbuffer not being written to. */
1817 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1818 subbuf_trunc(offsets
->begin
, chan
)
1819 - subbuf_trunc((unsigned long)
1820 atomic_long_read(&buf
->consumed
), chan
)
1821 >= chan
->backend
.buf_size
)) {
1823 * We do not overwrite non consumed buffers
1824 * and we are full : don't switch.
1829 * Next subbuffer not being written to, and we
1830 * are either in overwrite mode or the buffer is
1831 * not full. It's safe to write in this new
1837 * Next subbuffer reserve offset does not match the
1838 * commit offset. Don't perform switch in
1839 * producer-consumer and overwrite mode. Caused by
1840 * either a writer OOPS or too many nested writes over a
1841 * reserve/commit pair.
1847 * Need to write the subbuffer start header on finalize.
1849 offsets
->switch_old_start
= 1;
1851 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1852 /* Note: old points to the next subbuf at offset 0 */
1853 offsets
->end
= offsets
->begin
;
1858 * Force a sub-buffer switch. This operation is completely reentrant : can be
1859 * called while tracing is active with absolutely no lock held.
1861 * Note, however, that as a v_cmpxchg is used for some atomic
1862 * operations, this function must be called from the CPU which owns the buffer
1863 * for a ACTIVE flush.
1865 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1867 struct channel
*chan
= buf
->backend
.chan
;
1868 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1869 struct switch_offsets offsets
;
1870 unsigned long oldidx
;
1876 * Perform retryable operations.
1879 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1881 return; /* Switch not needed */
1882 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1886 * Atomically update last_tsc. This update races against concurrent
1887 * atomic updates, but the race will always cause supplementary full TSC
1888 * records, never the opposite (missing a full TSC record when it would
1891 save_last_tsc(config
, buf
, tsc
);
1894 * Push the reader if necessary
1896 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1898 oldidx
= subbuf_index(offsets
.old
, chan
);
1899 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1902 * May need to populate header start on SWITCH_FLUSH.
1904 if (offsets
.switch_old_start
) {
1905 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1906 offsets
.old
+= config
->cb
.subbuffer_header_size();
1910 * Switch old subbuffer.
1912 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1914 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1916 struct switch_param
{
1917 struct lib_ring_buffer
*buf
;
1918 enum switch_mode mode
;
1921 static void remote_switch(void *info
)
1923 struct switch_param
*param
= info
;
1924 struct lib_ring_buffer
*buf
= param
->buf
;
1926 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1929 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1930 enum switch_mode mode
)
1932 struct channel
*chan
= buf
->backend
.chan
;
1933 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1935 struct switch_param param
;
1938 * With global synchronization we don't need to use the IPI scheme.
1940 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1941 lib_ring_buffer_switch_slow(buf
, mode
);
1946 * Disabling preemption ensures two things: first, that the
1947 * target cpu is not taken concurrently offline while we are within
1948 * smp_call_function_single(). Secondly, if it happens that the
1949 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1950 * needs to be protected from CPU hotplug handlers, which can
1951 * also perform a remote subbuffer switch.
1956 ret
= smp_call_function_single(buf
->backend
.cpu
,
1957 remote_switch
, ¶m
, 1);
1959 /* Remote CPU is offline, do it ourself. */
1960 lib_ring_buffer_switch_slow(buf
, mode
);
1965 /* Switch sub-buffer if current sub-buffer is non-empty. */
1966 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1968 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1970 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1972 /* Switch sub-buffer even if current sub-buffer is empty. */
1973 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1975 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1977 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1979 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
)
1981 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1982 struct channel
*chan
= bufb
->chan
;
1984 lib_ring_buffer_switch_remote(buf
);
1985 lib_ring_buffer_clear_reader(buf
, chan
);
1987 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
1992 * -ENOSPC if event size is too large for packet.
1993 * -ENOBUFS if there is currently not enough space in buffer for the event.
1994 * -EIO if data cannot be written into the buffer for any other reason.
1997 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1998 struct channel
*chan
,
1999 struct switch_offsets
*offsets
,
2000 struct lttng_kernel_ring_buffer_ctx
*ctx
,
2003 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2004 unsigned long reserve_commit_diff
, offset_cmp
;
2007 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2008 offsets
->old
= offsets
->begin
;
2009 offsets
->switch_new_start
= 0;
2010 offsets
->switch_new_end
= 0;
2011 offsets
->switch_old_end
= 0;
2012 offsets
->pre_header_padding
= 0;
2014 ctx
->priv
.tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2015 if ((int64_t) ctx
->priv
.tsc
== -EIO
)
2018 if (last_tsc_overflow(config
, buf
, ctx
->priv
.tsc
))
2019 ctx
->priv
.rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2021 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->priv
.chan
) == 0)) {
2022 offsets
->switch_new_start
= 1; /* For offsets->begin */
2024 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2026 &offsets
->pre_header_padding
,
2029 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2032 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2033 offsets
->size
> chan
->backend
.subbuf_size
)) {
2034 offsets
->switch_old_end
= 1; /* For offsets->old */
2035 offsets
->switch_new_start
= 1; /* For offsets->begin */
2038 if (unlikely(offsets
->switch_new_start
)) {
2039 unsigned long sb_index
, commit_count
;
2042 * We are typically not filling the previous buffer completely.
2044 if (likely(offsets
->switch_old_end
))
2045 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2046 offsets
->begin
= offsets
->begin
2047 + config
->cb
.subbuffer_header_size();
2048 /* Test new buffer integrity */
2049 sb_index
= subbuf_index(offsets
->begin
, chan
);
2051 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2052 * lib_ring_buffer_check_deliver() has the matching
2053 * memory barriers required around commit_cold cc_sb
2054 * updates to ensure reserve and commit counter updates
2055 * are not seen reordered when updated by another CPU.
2058 commit_count
= v_read(config
,
2059 &buf
->commit_cold
[sb_index
].cc_sb
);
2060 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2062 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2064 * The reserve counter have been concurrently updated
2065 * while we read the commit counter. This means the
2066 * commit counter we read might not match buf->offset
2067 * due to concurrent update. We therefore need to retry.
2071 reserve_commit_diff
=
2072 (buf_trunc(offsets
->begin
, chan
)
2073 >> chan
->backend
.num_subbuf_order
)
2074 - (commit_count
& chan
->commit_count_mask
);
2075 if (likely(reserve_commit_diff
== 0)) {
2076 /* Next subbuffer not being written to. */
2077 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2078 subbuf_trunc(offsets
->begin
, chan
)
2079 - subbuf_trunc((unsigned long)
2080 atomic_long_read(&buf
->consumed
), chan
)
2081 >= chan
->backend
.buf_size
)) {
2083 * We do not overwrite non consumed buffers
2084 * and we are full : record is lost.
2086 v_inc(config
, &buf
->records_lost_full
);
2090 * Next subbuffer not being written to, and we
2091 * are either in overwrite mode or the buffer is
2092 * not full. It's safe to write in this new
2098 * Next subbuffer reserve offset does not match the
2099 * commit offset, and this did not involve update to the
2100 * reserve counter. Drop record in producer-consumer and
2101 * overwrite mode. Caused by either a writer OOPS or
2102 * too many nested writes over a reserve/commit pair.
2104 v_inc(config
, &buf
->records_lost_wrap
);
2108 config
->cb
.record_header_size(config
, chan
,
2110 &offsets
->pre_header_padding
,
2113 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2116 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2117 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2119 * Record too big for subbuffers, report error, don't
2120 * complete the sub-buffer switch.
2122 v_inc(config
, &buf
->records_lost_big
);
2126 * We just made a successful buffer switch and the
2127 * record fits in the new subbuffer. Let's write.
2132 * Record fits in the current buffer and we are not on a switch
2133 * boundary. It's safe to write.
2136 offsets
->end
= offsets
->begin
+ offsets
->size
;
2138 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2140 * The offset_end will fall at the very beginning of the next
2143 offsets
->switch_new_end
= 1; /* For offsets->begin */
2148 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2150 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2152 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2153 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2155 return chan
->backend
.buf
;
2158 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2160 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2161 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2163 v_inc(config
, &buf
->records_lost_big
);
2165 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2168 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2169 * @ctx: ring buffer context.
2171 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2172 * -EIO for other errors, else returns 0.
2173 * It will take care of sub-buffer switching.
2175 int lib_ring_buffer_reserve_slow(struct lttng_kernel_ring_buffer_ctx
*ctx
,
2178 struct channel
*chan
= ctx
->priv
.chan
;
2179 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2180 struct lib_ring_buffer
*buf
;
2181 struct switch_offsets offsets
;
2184 ctx
->priv
.buf
= buf
= get_current_buf(chan
, ctx
->priv
.reserve_cpu
);
2188 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2192 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2197 * Atomically update last_tsc. This update races against concurrent
2198 * atomic updates, but the race will always cause supplementary full TSC
2199 * records, never the opposite (missing a full TSC record when it would
2202 save_last_tsc(config
, buf
, ctx
->priv
.tsc
);
2205 * Push the reader if necessary
2207 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2210 * Clear noref flag for this subbuffer.
2212 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2213 subbuf_index(offsets
.end
- 1, chan
));
2216 * Switch old subbuffer if needed.
2218 if (unlikely(offsets
.switch_old_end
)) {
2219 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2220 subbuf_index(offsets
.old
- 1, chan
));
2221 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->priv
.tsc
);
2225 * Populate new subbuffer.
2227 if (unlikely(offsets
.switch_new_start
))
2228 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->priv
.tsc
);
2230 if (unlikely(offsets
.switch_new_end
))
2231 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->priv
.tsc
);
2233 ctx
->priv
.slot_size
= offsets
.size
;
2234 ctx
->priv
.pre_offset
= offsets
.begin
;
2235 ctx
->priv
.buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2238 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2241 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2242 struct lib_ring_buffer
*buf
,
2243 unsigned long commit_count
,
2246 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2247 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2251 * The ring buffer can count events recorded and overwritten per buffer,
2252 * but it is disabled by default due to its performance overhead.
2254 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2256 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2257 struct lib_ring_buffer
*buf
,
2260 v_add(config
, subbuffer_get_records_count(config
,
2261 &buf
->backend
, idx
),
2262 &buf
->records_count
);
2263 v_add(config
, subbuffer_count_records_overrun(config
,
2264 &buf
->backend
, idx
),
2265 &buf
->records_overrun
);
2267 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2269 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2270 struct lib_ring_buffer
*buf
,
2274 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2277 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2278 struct lib_ring_buffer
*buf
,
2279 struct channel
*chan
,
2280 unsigned long offset
,
2281 unsigned long commit_count
,
2285 unsigned long old_commit_count
= commit_count
2286 - chan
->backend
.subbuf_size
;
2289 * If we succeeded at updating cc_sb below, we are the subbuffer
2290 * writer delivering the subbuffer. Deals with concurrent
2291 * updates of the "cc" value without adding a add_return atomic
2292 * operation to the fast path.
2294 * We are doing the delivery in two steps:
2295 * - First, we cmpxchg() cc_sb to the new value
2296 * old_commit_count + 1. This ensures that we are the only
2297 * subbuffer user successfully filling the subbuffer, but we
2298 * do _not_ set the cc_sb value to "commit_count" yet.
2299 * Therefore, other writers that would wrap around the ring
2300 * buffer and try to start writing to our subbuffer would
2301 * have to drop records, because it would appear as
2303 * We therefore have exclusive access to the subbuffer control
2304 * structures. This mutual exclusion with other writers is
2305 * crucially important to perform record overruns count in
2306 * flight recorder mode locklessly.
2307 * - When we are ready to release the subbuffer (either for
2308 * reading or for overrun by other writers), we simply set the
2309 * cc_sb value to "commit_count" and perform delivery.
2311 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2312 * This guarantees that old_commit_count + 1 != commit_count.
2316 * Order prior updates to reserve count prior to the
2317 * commit_cold cc_sb update.
2320 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2321 old_commit_count
, old_commit_count
+ 1)
2322 == old_commit_count
)) {
2326 * Start of exclusive subbuffer access. We are
2327 * guaranteed to be the last writer in this subbuffer
2328 * and any other writer trying to access this subbuffer
2329 * in this state is required to drop records.
2331 * We can read the ts_end for the current sub-buffer
2332 * which has been saved by the very last space
2333 * reservation for the current sub-buffer.
2335 * Order increment of commit counter before reading ts_end.
2338 ts_end
= &buf
->ts_end
[idx
];
2339 deliver_count_events(config
, buf
, idx
);
2340 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2341 lib_ring_buffer_get_data_size(config
,
2346 * Increment the packet counter while we have exclusive
2349 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2352 * Set noref flag and offset for this subbuffer id.
2353 * Contains a memory barrier that ensures counter stores
2354 * are ordered before set noref and offset.
2356 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2357 buf_trunc_val(offset
, chan
));
2360 * Order set_noref and record counter updates before the
2361 * end of subbuffer exclusive access. Orders with
2362 * respect to writers coming into the subbuffer after
2363 * wrap around, and also order wrt concurrent readers.
2366 /* End of exclusive subbuffer access */
2367 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2370 * Order later updates to reserve count after
2371 * the commit_cold cc_sb update.
2374 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2378 * RING_BUFFER_WAKEUP_BY_WRITER uses an irq_work to issue
2381 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2382 && atomic_long_read(&buf
->active_readers
)
2383 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2384 irq_work_queue(&buf
->wakeup_pending
);
2385 irq_work_queue(&chan
->wakeup_pending
);
2390 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2392 int __init
init_lib_ring_buffer_frontend(void)
2396 for_each_possible_cpu(cpu
)
2397 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2401 module_init(init_lib_ring_buffer_frontend
);
2403 void __exit
exit_lib_ring_buffer_frontend(void)
2407 module_exit(exit_lib_ring_buffer_frontend
);