1 /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
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 <wrapper/ringbuffer/config.h>
46 #include <wrapper/ringbuffer/backend.h>
47 #include <wrapper/ringbuffer/frontend.h>
48 #include <wrapper/ringbuffer/iterator.h>
49 #include <wrapper/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 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
137 lttng_kvfree(buf
->commit_hot
);
138 lttng_kvfree(buf
->commit_cold
);
139 lttng_kvfree(buf
->ts_end
);
141 lib_ring_buffer_backend_free(&buf
->backend
);
145 * lib_ring_buffer_reset - Reset ring buffer to initial values.
148 * Effectively empty the ring buffer. Should be called when the buffer is not
149 * used for writing. The ring buffer can be opened for reading, but the reader
150 * should not be using the iterator concurrently with reset. The previous
151 * current iterator record is reset.
153 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
155 struct channel
*chan
= buf
->backend
.chan
;
156 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
160 * Reset iterator first. It will put the subbuffer if it currently holds
163 lib_ring_buffer_iterator_reset(buf
);
164 v_set(config
, &buf
->offset
, 0);
165 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
166 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
167 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
168 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
171 atomic_long_set(&buf
->consumed
, 0);
172 atomic_set(&buf
->record_disabled
, 0);
173 v_set(config
, &buf
->last_tsc
, 0);
174 lib_ring_buffer_backend_reset(&buf
->backend
);
175 /* Don't reset number of active readers */
176 v_set(config
, &buf
->records_lost_full
, 0);
177 v_set(config
, &buf
->records_lost_wrap
, 0);
178 v_set(config
, &buf
->records_lost_big
, 0);
179 v_set(config
, &buf
->records_count
, 0);
180 v_set(config
, &buf
->records_overrun
, 0);
183 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
186 * channel_reset - Reset channel to initial values.
189 * Effectively empty the channel. Should be called when the channel is not used
190 * for writing. The channel can be opened for reading, but the reader should not
191 * be using the iterator concurrently with reset. The previous current iterator
194 void channel_reset(struct channel
*chan
)
197 * Reset iterators first. Will put the subbuffer if held for reading.
199 channel_iterator_reset(chan
);
200 atomic_set(&chan
->record_disabled
, 0);
201 /* Don't reset commit_count_mask, still valid */
202 channel_backend_reset(&chan
->backend
);
203 /* Don't reset switch/read timer interval */
204 /* Don't reset notifiers and notifier enable bits */
205 /* Don't reset reader reference count */
207 EXPORT_SYMBOL_GPL(channel_reset
);
210 * Must be called under cpu hotplug protection.
212 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
213 struct channel_backend
*chanb
, int cpu
)
215 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
216 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
217 void *priv
= chanb
->priv
;
218 size_t subbuf_header_size
;
222 /* Test for cpu hotplug */
223 if (buf
->backend
.allocated
)
227 * Paranoia: per cpu dynamic allocation is not officially documented as
228 * zeroing the memory, so let's do it here too, just in case.
230 memset(buf
, 0, sizeof(*buf
));
232 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
237 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
238 * chan
->backend
.num_subbuf
,
239 1 << INTERNODE_CACHE_SHIFT
),
240 GFP_KERNEL
| __GFP_NOWARN
,
241 cpu_to_node(max(cpu
, 0)));
242 if (!buf
->commit_hot
) {
248 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
249 * chan
->backend
.num_subbuf
,
250 1 << INTERNODE_CACHE_SHIFT
),
251 GFP_KERNEL
| __GFP_NOWARN
,
252 cpu_to_node(max(cpu
, 0)));
253 if (!buf
->commit_cold
) {
259 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
260 * chan
->backend
.num_subbuf
,
261 1 << INTERNODE_CACHE_SHIFT
),
262 GFP_KERNEL
| __GFP_NOWARN
,
263 cpu_to_node(max(cpu
, 0)));
266 goto free_commit_cold
;
269 init_waitqueue_head(&buf
->read_wait
);
270 init_waitqueue_head(&buf
->write_wait
);
271 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
274 * Write the subbuffer header for first subbuffer so we know the total
275 * duration of data gathering.
277 subbuf_header_size
= config
->cb
.subbuffer_header_size();
278 v_set(config
, &buf
->offset
, subbuf_header_size
);
279 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
280 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
281 config
->cb
.buffer_begin(buf
, tsc
, 0);
282 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
284 if (config
->cb
.buffer_create
) {
285 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
291 * Ensure the buffer is ready before setting it to allocated and setting
293 * Used for cpu hotplug vs cpumask iteration.
296 buf
->backend
.allocated
= 1;
298 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
299 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
300 chan
->backend
.cpumask
));
301 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
308 lttng_kvfree(buf
->ts_end
);
310 lttng_kvfree(buf
->commit_cold
);
312 lttng_kvfree(buf
->commit_hot
);
314 lib_ring_buffer_backend_free(&buf
->backend
);
318 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
320 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, switch_timer
);
321 struct channel
*chan
= buf
->backend
.chan
;
322 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
325 * Only flush buffers periodically if readers are active.
327 if (atomic_long_read(&buf
->active_readers
))
328 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
330 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
331 lttng_mod_timer_pinned(&buf
->switch_timer
,
332 jiffies
+ chan
->switch_timer_interval
);
334 mod_timer(&buf
->switch_timer
,
335 jiffies
+ chan
->switch_timer_interval
);
339 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
341 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
343 struct channel
*chan
= buf
->backend
.chan
;
344 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
345 unsigned int flags
= 0;
347 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
350 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
351 flags
= LTTNG_TIMER_PINNED
;
353 lttng_timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
, buf
);
354 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
356 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
357 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
359 add_timer(&buf
->switch_timer
);
361 buf
->switch_timer_enabled
= 1;
365 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
367 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
369 struct channel
*chan
= buf
->backend
.chan
;
371 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
374 del_timer_sync(&buf
->switch_timer
);
375 buf
->switch_timer_enabled
= 0;
379 * Polling timer to check the channels for data.
381 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
383 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, read_timer
);
384 struct channel
*chan
= buf
->backend
.chan
;
385 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
387 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
389 if (atomic_long_read(&buf
->active_readers
)
390 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
391 wake_up_interruptible(&buf
->read_wait
);
392 wake_up_interruptible(&chan
->read_wait
);
395 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
396 lttng_mod_timer_pinned(&buf
->read_timer
,
397 jiffies
+ chan
->read_timer_interval
);
399 mod_timer(&buf
->read_timer
,
400 jiffies
+ chan
->read_timer_interval
);
404 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
406 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
408 struct channel
*chan
= buf
->backend
.chan
;
409 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
410 unsigned int flags
= 0;
412 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
413 || !chan
->read_timer_interval
414 || buf
->read_timer_enabled
)
417 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
418 flags
= LTTNG_TIMER_PINNED
;
420 lttng_timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
, buf
);
421 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
423 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
424 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
426 add_timer(&buf
->read_timer
);
428 buf
->read_timer_enabled
= 1;
432 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
434 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
436 struct channel
*chan
= buf
->backend
.chan
;
437 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
439 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
440 || !chan
->read_timer_interval
441 || !buf
->read_timer_enabled
)
444 del_timer_sync(&buf
->read_timer
);
446 * do one more check to catch data that has been written in the last
449 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
450 wake_up_interruptible(&buf
->read_wait
);
451 wake_up_interruptible(&chan
->read_wait
);
453 buf
->read_timer_enabled
= 0;
456 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
458 enum cpuhp_state lttng_rb_hp_prepare
;
459 enum cpuhp_state lttng_rb_hp_online
;
461 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
463 lttng_rb_hp_prepare
= val
;
465 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
467 void lttng_rb_set_hp_online(enum cpuhp_state val
)
469 lttng_rb_hp_online
= val
;
471 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
473 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
474 struct lttng_cpuhp_node
*node
)
476 struct channel
*chan
= container_of(node
, struct channel
,
478 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
479 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
481 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
484 * Performing a buffer switch on a remote CPU. Performed by
485 * the CPU responsible for doing the hotunplug after the target
486 * CPU stopped running completely. Ensures that all data
487 * from that remote CPU is flushed.
489 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
492 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
494 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
495 struct lttng_cpuhp_node
*node
)
497 struct channel
*chan
= container_of(node
, struct channel
,
499 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
500 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
502 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
504 wake_up_interruptible(&chan
->hp_wait
);
505 lib_ring_buffer_start_switch_timer(buf
);
506 lib_ring_buffer_start_read_timer(buf
);
509 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
511 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
512 struct lttng_cpuhp_node
*node
)
514 struct channel
*chan
= container_of(node
, struct channel
,
516 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
517 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
519 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
521 lib_ring_buffer_stop_switch_timer(buf
);
522 lib_ring_buffer_stop_read_timer(buf
);
525 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
527 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
529 #ifdef CONFIG_HOTPLUG_CPU
532 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
533 * @nb: notifier block
534 * @action: hotplug action to take
537 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
540 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
541 unsigned long action
,
544 unsigned int cpu
= (unsigned long)hcpu
;
545 struct channel
*chan
= container_of(nb
, struct channel
,
547 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
548 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
550 if (!chan
->cpu_hp_enable
)
553 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
556 case CPU_DOWN_FAILED
:
557 case CPU_DOWN_FAILED_FROZEN
:
559 case CPU_ONLINE_FROZEN
:
560 wake_up_interruptible(&chan
->hp_wait
);
561 lib_ring_buffer_start_switch_timer(buf
);
562 lib_ring_buffer_start_read_timer(buf
);
565 case CPU_DOWN_PREPARE
:
566 case CPU_DOWN_PREPARE_FROZEN
:
567 lib_ring_buffer_stop_switch_timer(buf
);
568 lib_ring_buffer_stop_read_timer(buf
);
572 case CPU_DEAD_FROZEN
:
574 * Performing a buffer switch on a remote CPU. Performed by
575 * the CPU responsible for doing the hotunplug after the target
576 * CPU stopped running completely. Ensures that all data
577 * from that remote CPU is flushed.
579 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
589 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
591 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
593 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
594 * that wake-up-tracing generated events are flushed before going idle (in
595 * tick_nohz). We test if the spinlock is locked to deal with the race where
596 * readers try to sample the ring buffer before we perform the switch. We let
597 * the readers retry in that case. If there is data in the buffer, the wake up
598 * is going to forbid the CPU running the reader thread from going idle.
600 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
604 struct channel
*chan
= container_of(nb
, struct channel
,
606 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
607 struct lib_ring_buffer
*buf
;
608 int cpu
= smp_processor_id();
610 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
612 * We don't support keeping the system idle with global buffers
613 * and streaming active. In order to do so, we would need to
614 * sample a non-nohz-cpumask racelessly with the nohz updates
615 * without adding synchronization overhead to nohz. Leave this
616 * use-case out for now.
621 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
623 case TICK_NOHZ_FLUSH
:
624 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
625 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
626 && chan
->read_timer_interval
627 && atomic_long_read(&buf
->active_readers
)
628 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
629 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
630 wake_up_interruptible(&buf
->read_wait
);
631 wake_up_interruptible(&chan
->read_wait
);
633 if (chan
->switch_timer_interval
)
634 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
635 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
638 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
639 lib_ring_buffer_stop_switch_timer(buf
);
640 lib_ring_buffer_stop_read_timer(buf
);
641 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
643 case TICK_NOHZ_RESTART
:
644 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
645 lib_ring_buffer_start_read_timer(buf
);
646 lib_ring_buffer_start_switch_timer(buf
);
647 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
654 void notrace
lib_ring_buffer_tick_nohz_flush(void)
656 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
660 void notrace
lib_ring_buffer_tick_nohz_stop(void)
662 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
666 void notrace
lib_ring_buffer_tick_nohz_restart(void)
668 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
671 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
676 static void channel_unregister_notifiers(struct channel
*chan
)
678 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
680 channel_iterator_unregister_notifiers(chan
);
681 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
684 * Remove the nohz notifier first, so we are certain we stop
687 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
688 &chan
->tick_nohz_notifier
);
690 * ring_buffer_nohz_lock will not be needed below, because
691 * we just removed the notifiers, which were the only source of
694 #endif /* CONFIG_NO_HZ */
695 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
699 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
700 &chan
->cpuhp_online
.node
);
702 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
703 &chan
->cpuhp_prepare
.node
);
706 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
710 #ifdef CONFIG_HOTPLUG_CPU
712 chan
->cpu_hp_enable
= 0;
713 for_each_online_cpu(cpu
) {
714 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
716 lib_ring_buffer_stop_switch_timer(buf
);
717 lib_ring_buffer_stop_read_timer(buf
);
720 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
722 for_each_possible_cpu(cpu
) {
723 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
725 lib_ring_buffer_stop_switch_timer(buf
);
726 lib_ring_buffer_stop_read_timer(buf
);
730 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
732 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
734 lib_ring_buffer_stop_switch_timer(buf
);
735 lib_ring_buffer_stop_read_timer(buf
);
737 channel_backend_unregister_notifiers(&chan
->backend
);
740 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
742 if (!buf
->quiescent
) {
743 buf
->quiescent
= true;
744 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
748 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
750 buf
->quiescent
= false;
753 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
756 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
758 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
760 for_each_channel_cpu(cpu
, chan
) {
761 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
764 lib_ring_buffer_set_quiescent(buf
);
768 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
770 lib_ring_buffer_set_quiescent(buf
);
773 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
775 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
778 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
780 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
782 for_each_channel_cpu(cpu
, chan
) {
783 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
786 lib_ring_buffer_clear_quiescent(buf
);
790 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
792 lib_ring_buffer_clear_quiescent(buf
);
795 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
797 static void channel_free(struct channel
*chan
)
799 if (chan
->backend
.release_priv_ops
) {
800 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
802 channel_iterator_free(chan
);
803 channel_backend_free(&chan
->backend
);
808 * channel_create - Create channel.
809 * @config: ring buffer instance configuration
810 * @name: name of the channel
811 * @priv: ring buffer client private data
812 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
813 * address mapping. It is used only by RING_BUFFER_STATIC
814 * configuration. It can be set to NULL for other backends.
815 * @subbuf_size: subbuffer size
816 * @num_subbuf: number of subbuffers
817 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
818 * padding to let readers get those sub-buffers.
819 * Used for live streaming.
820 * @read_timer_interval: Time interval (in us) to wake up pending readers.
823 * Returns NULL on failure.
825 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
826 const char *name
, void *priv
, void *buf_addr
,
828 size_t num_subbuf
, unsigned int switch_timer_interval
,
829 unsigned int read_timer_interval
)
832 struct channel
*chan
;
834 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
835 read_timer_interval
))
838 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
842 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
843 subbuf_size
, num_subbuf
);
847 ret
= channel_iterator_init(chan
);
849 goto error_free_backend
;
851 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
852 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
853 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
854 kref_init(&chan
->ref
);
855 init_waitqueue_head(&chan
->read_wait
);
856 init_waitqueue_head(&chan
->hp_wait
);
858 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
859 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
860 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
861 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
862 &chan
->cpuhp_prepare
.node
);
864 goto cpuhp_prepare_error
;
866 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
867 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
868 &chan
->cpuhp_online
.node
);
870 goto cpuhp_online_error
;
871 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
875 * In case of non-hotplug cpu, if the ring-buffer is allocated
876 * in early initcall, it will not be notified of secondary cpus.
877 * In that off case, we need to allocate for all possible cpus.
879 #ifdef CONFIG_HOTPLUG_CPU
880 chan
->cpu_hp_notifier
.notifier_call
=
881 lib_ring_buffer_cpu_hp_callback
;
882 chan
->cpu_hp_notifier
.priority
= 6;
883 register_cpu_notifier(&chan
->cpu_hp_notifier
);
886 for_each_online_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
));
894 chan
->cpu_hp_enable
= 1;
897 for_each_possible_cpu(cpu
) {
898 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
900 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
901 lib_ring_buffer_start_switch_timer(buf
);
902 lib_ring_buffer_start_read_timer(buf
);
903 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
907 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
909 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
910 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
911 chan
->tick_nohz_notifier
.notifier_call
=
912 ring_buffer_tick_nohz_callback
;
913 chan
->tick_nohz_notifier
.priority
= ~0U;
914 atomic_notifier_chain_register(&tick_nohz_notifier
,
915 &chan
->tick_nohz_notifier
);
916 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
919 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
921 lib_ring_buffer_start_switch_timer(buf
);
922 lib_ring_buffer_start_read_timer(buf
);
927 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
929 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
930 &chan
->cpuhp_prepare
.node
);
933 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
935 channel_backend_free(&chan
->backend
);
940 EXPORT_SYMBOL_GPL(channel_create
);
943 void channel_release(struct kref
*kref
)
945 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
950 * channel_destroy - Finalize, wait for q.s. and destroy channel.
951 * @chan: channel to destroy
954 * Call "destroy" callback, finalize channels, and then decrement the
955 * channel reference count. Note that when readers have completed data
956 * consumption of finalized channels, get_subbuf() will return -ENODATA.
957 * They should release their handle at that point. Returns the private
960 void *channel_destroy(struct channel
*chan
)
963 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
966 channel_unregister_notifiers(chan
);
968 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
970 * No need to hold cpu hotplug, because all notifiers have been
973 for_each_channel_cpu(cpu
, chan
) {
974 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
977 if (config
->cb
.buffer_finalize
)
978 config
->cb
.buffer_finalize(buf
,
982 * Perform flush before writing to finalized.
985 WRITE_ONCE(buf
->finalized
, 1);
986 wake_up_interruptible(&buf
->read_wait
);
989 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
991 if (config
->cb
.buffer_finalize
)
992 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
994 * Perform flush before writing to finalized.
997 WRITE_ONCE(buf
->finalized
, 1);
998 wake_up_interruptible(&buf
->read_wait
);
1000 WRITE_ONCE(chan
->finalized
, 1);
1001 wake_up_interruptible(&chan
->hp_wait
);
1002 wake_up_interruptible(&chan
->read_wait
);
1003 priv
= chan
->backend
.priv
;
1004 kref_put(&chan
->ref
, channel_release
);
1007 EXPORT_SYMBOL_GPL(channel_destroy
);
1009 struct lib_ring_buffer
*channel_get_ring_buffer(
1010 const struct lib_ring_buffer_config
*config
,
1011 struct channel
*chan
, int cpu
)
1013 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1014 return chan
->backend
.buf
;
1016 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1018 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1020 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1022 struct channel
*chan
= buf
->backend
.chan
;
1024 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1026 if (!lttng_kref_get(&chan
->ref
)) {
1027 atomic_long_dec(&buf
->active_readers
);
1030 lttng_smp_mb__after_atomic();
1033 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1035 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1037 struct channel
*chan
= buf
->backend
.chan
;
1039 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1040 lttng_smp_mb__before_atomic();
1041 atomic_long_dec(&buf
->active_readers
);
1042 kref_put(&chan
->ref
, channel_release
);
1044 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1047 * Promote compiler barrier to a smp_mb().
1048 * For the specific ring buffer case, this IPI call should be removed if the
1049 * architecture does not reorder writes. This should eventually be provided by
1050 * a separate architecture-specific infrastructure.
1052 static void remote_mb(void *info
)
1058 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1060 * @consumed: consumed count indicating the position where to read
1061 * @produced: produced count, indicates position when to stop reading
1063 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1064 * data to read at consumed position, or 0 if the get operation succeeds.
1065 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1068 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1069 unsigned long *consumed
, unsigned long *produced
)
1071 struct channel
*chan
= buf
->backend
.chan
;
1072 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1073 unsigned long consumed_cur
, write_offset
;
1077 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1079 * Read finalized before counters.
1082 consumed_cur
= atomic_long_read(&buf
->consumed
);
1084 * No need to issue a memory barrier between consumed count read and
1085 * write offset read, because consumed count can only change
1086 * concurrently in overwrite mode, and we keep a sequence counter
1087 * identifier derived from the write offset to check we are getting
1088 * the same sub-buffer we are expecting (the sub-buffers are atomically
1089 * "tagged" upon writes, tags are checked upon read).
1091 write_offset
= v_read(config
, &buf
->offset
);
1094 * Check that we are not about to read the same subbuffer in
1095 * which the writer head is.
1097 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1101 *consumed
= consumed_cur
;
1102 *produced
= subbuf_trunc(write_offset
, chan
);
1108 * The memory barriers __wait_event()/wake_up_interruptible() take care
1109 * of "raw_spin_is_locked" memory ordering.
1113 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1118 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1121 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1122 * are saved regardless of whether the consumed and produced positions are
1123 * in the same subbuffer.
1125 * @consumed: consumed byte count indicating the last position read
1126 * @produced: produced byte count indicating the last position written
1128 * This function is meant to provide information on the exact producer and
1129 * consumer positions without regard for the "snapshot" feature.
1131 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1132 unsigned long *consumed
, unsigned long *produced
)
1134 struct channel
*chan
= buf
->backend
.chan
;
1135 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1138 *consumed
= atomic_long_read(&buf
->consumed
);
1140 * No need to issue a memory barrier between consumed count read and
1141 * write offset read, because consumed count can only change
1142 * concurrently in overwrite mode, and we keep a sequence counter
1143 * identifier derived from the write offset to check we are getting
1144 * the same sub-buffer we are expecting (the sub-buffers are atomically
1145 * "tagged" upon writes, tags are checked upon read).
1147 *produced
= v_read(config
, &buf
->offset
);
1152 * lib_ring_buffer_put_snapshot - move consumed counter forward
1154 * Should only be called from consumer context.
1156 * @consumed_new: new consumed count value
1158 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1159 unsigned long consumed_new
)
1161 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1162 struct channel
*chan
= bufb
->chan
;
1163 unsigned long consumed
;
1165 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1168 * Only push the consumed value forward.
1169 * If the consumed cmpxchg fails, this is because we have been pushed by
1170 * the writer in flight recorder mode.
1172 consumed
= atomic_long_read(&buf
->consumed
);
1173 while ((long) consumed
- (long) consumed_new
< 0)
1174 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1176 /* Wake-up the metadata producer */
1177 wake_up_interruptible(&buf
->write_wait
);
1179 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1181 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1182 static void lib_ring_buffer_flush_read_subbuf_dcache(
1183 const struct lib_ring_buffer_config
*config
,
1184 struct channel
*chan
,
1185 struct lib_ring_buffer
*buf
)
1187 struct lib_ring_buffer_backend_pages
*pages
;
1188 unsigned long sb_bindex
, id
, i
, nr_pages
;
1190 if (config
->output
!= RING_BUFFER_MMAP
)
1194 * Architectures with caches aliased on virtual addresses may
1195 * use different cache lines for the linear mapping vs
1196 * user-space memory mapping. Given that the ring buffer is
1197 * based on the kernel linear mapping, aligning it with the
1198 * user-space mapping is not straightforward, and would require
1199 * extra TLB entries. Therefore, simply flush the dcache for the
1200 * entire sub-buffer before reading it.
1202 id
= buf
->backend
.buf_rsb
.id
;
1203 sb_bindex
= subbuffer_id_get_index(config
, id
);
1204 pages
= buf
->backend
.array
[sb_bindex
];
1205 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1206 for (i
= 0; i
< nr_pages
; i
++) {
1207 struct lib_ring_buffer_backend_page
*backend_page
;
1209 backend_page
= &pages
->p
[i
];
1210 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1214 static void lib_ring_buffer_flush_read_subbuf_dcache(
1215 const struct lib_ring_buffer_config
*config
,
1216 struct channel
*chan
,
1217 struct lib_ring_buffer
*buf
)
1223 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1225 * @consumed: consumed count indicating the position where to read
1227 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1228 * data to read at consumed position, or 0 if the get operation succeeds.
1229 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1231 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1232 unsigned long consumed
)
1234 struct channel
*chan
= buf
->backend
.chan
;
1235 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1236 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1240 if (buf
->get_subbuf
) {
1242 * Reader is trying to get a subbuffer twice.
1244 CHAN_WARN_ON(chan
, 1);
1248 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1250 * Read finalized before counters.
1253 consumed_cur
= atomic_long_read(&buf
->consumed
);
1254 consumed_idx
= subbuf_index(consumed
, chan
);
1255 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1257 * Make sure we read the commit count before reading the buffer
1258 * data and the write offset. Correct consumed offset ordering
1259 * wrt commit count is insured by the use of cmpxchg to update
1260 * the consumed offset.
1261 * smp_call_function_single can fail if the remote CPU is offline,
1262 * this is OK because then there is no wmb to execute there.
1263 * If our thread is executing on the same CPU as the on the buffers
1264 * belongs to, we don't have to synchronize it at all. If we are
1265 * migrated, the scheduler will take care of the memory barriers.
1266 * Normally, smp_call_function_single() should ensure program order when
1267 * executing the remote function, which implies that it surrounds the
1268 * function execution with :
1279 * However, smp_call_function_single() does not seem to clearly execute
1280 * such barriers. It depends on spinlock semantic to provide the barrier
1281 * before executing the IPI and, when busy-looping, csd_lock_wait only
1282 * executes smp_mb() when it has to wait for the other CPU.
1284 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1285 * required ourself, even if duplicated. It has no performance impact
1288 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1289 * read and write vs write. They do not ensure core synchronization. We
1290 * really have to ensure total order between the 3 barriers running on
1293 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1294 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1295 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1296 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1297 /* Total order with IPI handler smp_mb() */
1299 smp_call_function_single(buf
->backend
.cpu
,
1300 remote_mb
, NULL
, 1);
1301 /* Total order with IPI handler smp_mb() */
1305 /* Total order with IPI handler smp_mb() */
1307 smp_call_function(remote_mb
, NULL
, 1);
1308 /* Total order with IPI handler smp_mb() */
1313 * Local rmb to match the remote wmb to read the commit count
1314 * before the buffer data and the write offset.
1319 write_offset
= v_read(config
, &buf
->offset
);
1322 * Check that the buffer we are getting is after or at consumed_cur
1325 if ((long) subbuf_trunc(consumed
, chan
)
1326 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1330 * Check that the subbuffer we are trying to consume has been
1331 * already fully committed.
1333 if (((commit_count
- chan
->backend
.subbuf_size
)
1334 & chan
->commit_count_mask
)
1335 - (buf_trunc(consumed
, chan
)
1336 >> chan
->backend
.num_subbuf_order
)
1341 * Check that we are not about to read the same subbuffer in
1342 * which the writer head is.
1344 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1349 * Failure to get the subbuffer causes a busy-loop retry without going
1350 * to a wait queue. These are caused by short-lived race windows where
1351 * the writer is getting access to a subbuffer we were trying to get
1352 * access to. Also checks that the "consumed" buffer count we are
1353 * looking for matches the one contained in the subbuffer id.
1355 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1356 consumed_idx
, buf_trunc_val(consumed
, chan
));
1359 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1361 buf
->get_subbuf_consumed
= consumed
;
1362 buf
->get_subbuf
= 1;
1364 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1370 * The memory barriers __wait_event()/wake_up_interruptible() take care
1371 * of "raw_spin_is_locked" memory ordering.
1375 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1380 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1383 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1386 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1388 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1389 struct channel
*chan
= bufb
->chan
;
1390 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1391 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1393 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1395 if (!buf
->get_subbuf
) {
1397 * Reader puts a subbuffer it did not get.
1399 CHAN_WARN_ON(chan
, 1);
1402 consumed
= buf
->get_subbuf_consumed
;
1403 buf
->get_subbuf
= 0;
1406 * Clear the records_unread counter. (overruns counter)
1407 * Can still be non-zero if a file reader simply grabbed the data
1408 * without using iterators.
1409 * Can be below zero if an iterator is used on a snapshot more than
1412 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1413 v_add(config
, v_read(config
,
1414 &bufb
->array
[read_sb_bindex
]->records_unread
),
1415 &bufb
->records_read
);
1416 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1417 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1418 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1419 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1422 * Exchange the reader subbuffer with the one we put in its place in the
1423 * writer subbuffer table. Expect the original consumed count. If
1424 * update_read_sb_index fails, this is because the writer updated the
1425 * subbuffer concurrently. We should therefore keep the subbuffer we
1426 * currently have: it has become invalid to try reading this sub-buffer
1427 * consumed count value anyway.
1429 consumed_idx
= subbuf_index(consumed
, chan
);
1430 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1431 consumed_idx
, buf_trunc_val(consumed
, chan
));
1433 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1434 * if the writer concurrently updated it.
1437 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1440 * cons_offset is an iterator on all subbuffer offsets between the reader
1441 * position and the writer position. (inclusive)
1444 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1445 struct channel
*chan
,
1446 unsigned long cons_offset
,
1449 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1450 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1452 cons_idx
= subbuf_index(cons_offset
, chan
);
1453 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1454 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1456 if (subbuf_offset(commit_count
, chan
) != 0)
1458 "ring buffer %s, cpu %d: "
1459 "commit count in subbuffer %lu,\n"
1460 "expecting multiples of %lu bytes\n"
1461 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1462 chan
->backend
.name
, cpu
, cons_idx
,
1463 chan
->backend
.subbuf_size
,
1464 commit_count
, commit_count_sb
);
1466 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1467 chan
->backend
.name
, cpu
, commit_count
);
1471 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1472 struct channel
*chan
,
1473 void *priv
, int cpu
)
1475 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1476 unsigned long write_offset
, cons_offset
;
1479 * No need to order commit_count, write_offset and cons_offset reads
1480 * because we execute at teardown when no more writer nor reader
1481 * references are left.
1483 write_offset
= v_read(config
, &buf
->offset
);
1484 cons_offset
= atomic_long_read(&buf
->consumed
);
1485 if (write_offset
!= cons_offset
)
1487 "ring buffer %s, cpu %d: "
1488 "non-consumed data\n"
1489 " [ %lu bytes written, %lu bytes read ]\n",
1490 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1492 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1493 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1496 cons_offset
= subbuf_align(cons_offset
, chan
))
1497 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1501 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1503 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1504 struct lib_ring_buffer
*buf
,
1507 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1509 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1510 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1511 "%lu records overrun\n",
1513 v_read(config
, &buf
->records_count
),
1514 v_read(config
, &buf
->records_overrun
));
1516 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1517 "%lu records overrun\n",
1518 chan
->backend
.name
, cpu
,
1519 v_read(config
, &buf
->records_count
),
1520 v_read(config
, &buf
->records_overrun
));
1525 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1526 struct lib_ring_buffer
*buf
,
1533 void lib_ring_buffer_print_errors(struct channel
*chan
,
1534 struct lib_ring_buffer
*buf
, int cpu
)
1536 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1537 void *priv
= chan
->backend
.priv
;
1539 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1540 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1541 if (v_read(config
, &buf
->records_lost_full
)
1542 || v_read(config
, &buf
->records_lost_wrap
)
1543 || v_read(config
, &buf
->records_lost_big
))
1545 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1546 " [ %lu buffer full, %lu nest buffer wrap-around, "
1547 "%lu event too big ]\n",
1548 chan
->backend
.name
, cpu
,
1549 v_read(config
, &buf
->records_lost_full
),
1550 v_read(config
, &buf
->records_lost_wrap
),
1551 v_read(config
, &buf
->records_lost_big
));
1553 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1557 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1559 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1562 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1563 struct channel
*chan
,
1564 struct switch_offsets
*offsets
,
1567 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1568 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1569 unsigned long commit_count
;
1570 struct commit_counters_hot
*cc_hot
;
1572 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1575 * Order all writes to buffer before the commit count update that will
1576 * determine that the subbuffer is full.
1578 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1580 * Must write slot data before incrementing commit count. This
1581 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1587 cc_hot
= &buf
->commit_hot
[oldidx
];
1588 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1589 commit_count
= v_read(config
, &cc_hot
->cc
);
1590 /* Check if the written buffer has to be delivered */
1591 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1592 commit_count
, oldidx
, tsc
);
1593 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1594 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1595 commit_count
, cc_hot
);
1599 * lib_ring_buffer_switch_old_end: switch old subbuffer
1601 * Note : offset_old should never be 0 here. It is ok, because we never perform
1602 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1603 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1607 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1608 struct channel
*chan
,
1609 struct switch_offsets
*offsets
,
1612 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1613 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1614 unsigned long commit_count
, padding_size
, data_size
;
1615 struct commit_counters_hot
*cc_hot
;
1618 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1619 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1620 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1622 ts_end
= &buf
->ts_end
[oldidx
];
1624 * This is the last space reservation in that sub-buffer before
1625 * it gets delivered. This provides exclusive access to write to
1626 * this sub-buffer's ts_end. There are also no concurrent
1627 * readers of that ts_end because delivery of that sub-buffer is
1628 * postponed until the commit counter is incremented for the
1629 * current space reservation.
1634 * Order all writes to buffer and store to ts_end before the commit
1635 * count update that will determine that the subbuffer is full.
1637 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1639 * Must write slot data before incrementing commit count. This
1640 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1646 cc_hot
= &buf
->commit_hot
[oldidx
];
1647 v_add(config
, padding_size
, &cc_hot
->cc
);
1648 commit_count
= v_read(config
, &cc_hot
->cc
);
1649 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1650 commit_count
, oldidx
, tsc
);
1651 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1652 offsets
->old
+ padding_size
, commit_count
,
1657 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1659 * This code can be executed unordered : writers may already have written to the
1660 * sub-buffer before this code gets executed, caution. The commit makes sure
1661 * that this code is executed before the deliver of this sub-buffer.
1664 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1665 struct channel
*chan
,
1666 struct switch_offsets
*offsets
,
1669 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1670 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1671 unsigned long commit_count
;
1672 struct commit_counters_hot
*cc_hot
;
1674 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1677 * Order all writes to buffer before the commit count update that will
1678 * determine that the subbuffer is full.
1680 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1682 * Must write slot data before incrementing commit count. This
1683 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1689 cc_hot
= &buf
->commit_hot
[beginidx
];
1690 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1691 commit_count
= v_read(config
, &cc_hot
->cc
);
1692 /* Check if the written buffer has to be delivered */
1693 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1694 commit_count
, beginidx
, tsc
);
1695 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1696 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1697 commit_count
, cc_hot
);
1701 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1703 * Calls subbuffer_set_data_size() to set the data size of the current
1704 * sub-buffer. We do not need to perform check_deliver nor commit here,
1705 * since this task will be done by the "commit" of the event for which
1706 * we are currently doing the space reservation.
1709 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1710 struct channel
*chan
,
1711 struct switch_offsets
*offsets
,
1714 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1715 unsigned long endidx
, data_size
;
1718 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1719 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1720 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1721 ts_end
= &buf
->ts_end
[endidx
];
1723 * This is the last space reservation in that sub-buffer before
1724 * it gets delivered. This provides exclusive access to write to
1725 * this sub-buffer's ts_end. There are also no concurrent
1726 * readers of that ts_end because delivery of that sub-buffer is
1727 * postponed until the commit counter is incremented for the
1728 * current space reservation.
1736 * !0 if execution must be aborted.
1739 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1740 struct lib_ring_buffer
*buf
,
1741 struct channel
*chan
,
1742 struct switch_offsets
*offsets
,
1745 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1746 unsigned long off
, reserve_commit_diff
;
1748 offsets
->begin
= v_read(config
, &buf
->offset
);
1749 offsets
->old
= offsets
->begin
;
1750 offsets
->switch_old_start
= 0;
1751 off
= subbuf_offset(offsets
->begin
, chan
);
1753 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1756 * Ensure we flush the header of an empty subbuffer when doing the
1757 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1758 * total data gathering duration even if there were no records saved
1759 * after the last buffer switch.
1760 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1761 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1762 * subbuffer header as appropriate.
1763 * The next record that reserves space will be responsible for
1764 * populating the following subbuffer header. We choose not to populate
1765 * the next subbuffer header here because we want to be able to use
1766 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1767 * buffer flush, which must guarantee that all the buffer content
1768 * (records and header timestamps) are visible to the reader. This is
1769 * required for quiescence guarantees for the fusion merge.
1771 if (mode
!= SWITCH_FLUSH
&& !off
)
1772 return -1; /* we do not have to switch : buffer is empty */
1774 if (unlikely(off
== 0)) {
1775 unsigned long sb_index
, commit_count
;
1778 * We are performing a SWITCH_FLUSH. At this stage, there are no
1779 * concurrent writes into the buffer.
1781 * The client does not save any header information. Don't
1782 * switch empty subbuffer on finalize, because it is invalid to
1783 * deliver a completely empty subbuffer.
1785 if (!config
->cb
.subbuffer_header_size())
1788 /* Test new buffer integrity */
1789 sb_index
= subbuf_index(offsets
->begin
, chan
);
1790 commit_count
= v_read(config
,
1791 &buf
->commit_cold
[sb_index
].cc_sb
);
1792 reserve_commit_diff
=
1793 (buf_trunc(offsets
->begin
, chan
)
1794 >> chan
->backend
.num_subbuf_order
)
1795 - (commit_count
& chan
->commit_count_mask
);
1796 if (likely(reserve_commit_diff
== 0)) {
1797 /* Next subbuffer not being written to. */
1798 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1799 subbuf_trunc(offsets
->begin
, chan
)
1800 - subbuf_trunc((unsigned long)
1801 atomic_long_read(&buf
->consumed
), chan
)
1802 >= chan
->backend
.buf_size
)) {
1804 * We do not overwrite non consumed buffers
1805 * and we are full : don't switch.
1810 * Next subbuffer not being written to, and we
1811 * are either in overwrite mode or the buffer is
1812 * not full. It's safe to write in this new
1818 * Next subbuffer reserve offset does not match the
1819 * commit offset. Don't perform switch in
1820 * producer-consumer and overwrite mode. Caused by
1821 * either a writer OOPS or too many nested writes over a
1822 * reserve/commit pair.
1828 * Need to write the subbuffer start header on finalize.
1830 offsets
->switch_old_start
= 1;
1832 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1833 /* Note: old points to the next subbuf at offset 0 */
1834 offsets
->end
= offsets
->begin
;
1839 * Force a sub-buffer switch. This operation is completely reentrant : can be
1840 * called while tracing is active with absolutely no lock held.
1842 * Note, however, that as a v_cmpxchg is used for some atomic
1843 * operations, this function must be called from the CPU which owns the buffer
1844 * for a ACTIVE flush.
1846 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1848 struct channel
*chan
= buf
->backend
.chan
;
1849 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1850 struct switch_offsets offsets
;
1851 unsigned long oldidx
;
1857 * Perform retryable operations.
1860 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1862 return; /* Switch not needed */
1863 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1867 * Atomically update last_tsc. This update races against concurrent
1868 * atomic updates, but the race will always cause supplementary full TSC
1869 * records, never the opposite (missing a full TSC record when it would
1872 save_last_tsc(config
, buf
, tsc
);
1875 * Push the reader if necessary
1877 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1879 oldidx
= subbuf_index(offsets
.old
, chan
);
1880 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1883 * May need to populate header start on SWITCH_FLUSH.
1885 if (offsets
.switch_old_start
) {
1886 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1887 offsets
.old
+= config
->cb
.subbuffer_header_size();
1891 * Switch old subbuffer.
1893 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1895 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1897 struct switch_param
{
1898 struct lib_ring_buffer
*buf
;
1899 enum switch_mode mode
;
1902 static void remote_switch(void *info
)
1904 struct switch_param
*param
= info
;
1905 struct lib_ring_buffer
*buf
= param
->buf
;
1907 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1910 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1911 enum switch_mode mode
)
1913 struct channel
*chan
= buf
->backend
.chan
;
1914 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1916 struct switch_param param
;
1919 * With global synchronization we don't need to use the IPI scheme.
1921 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1922 lib_ring_buffer_switch_slow(buf
, mode
);
1927 * Disabling preemption ensures two things: first, that the
1928 * target cpu is not taken concurrently offline while we are within
1929 * smp_call_function_single(). Secondly, if it happens that the
1930 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1931 * needs to be protected from CPU hotplug handlers, which can
1932 * also perform a remote subbuffer switch.
1937 ret
= smp_call_function_single(buf
->backend
.cpu
,
1938 remote_switch
, ¶m
, 1);
1940 /* Remote CPU is offline, do it ourself. */
1941 lib_ring_buffer_switch_slow(buf
, mode
);
1946 /* Switch sub-buffer if current sub-buffer is non-empty. */
1947 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1949 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1951 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1953 /* Switch sub-buffer even if current sub-buffer is empty. */
1954 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1956 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1958 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1960 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
)
1962 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1963 struct channel
*chan
= bufb
->chan
;
1965 lib_ring_buffer_switch_remote(buf
);
1966 lib_ring_buffer_clear_reader(buf
, chan
);
1968 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
1973 * -ENOSPC if event size is too large for packet.
1974 * -ENOBUFS if there is currently not enough space in buffer for the event.
1975 * -EIO if data cannot be written into the buffer for any other reason.
1978 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1979 struct channel
*chan
,
1980 struct switch_offsets
*offsets
,
1981 struct lib_ring_buffer_ctx
*ctx
,
1984 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1985 unsigned long reserve_commit_diff
, offset_cmp
;
1988 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1989 offsets
->old
= offsets
->begin
;
1990 offsets
->switch_new_start
= 0;
1991 offsets
->switch_new_end
= 0;
1992 offsets
->switch_old_end
= 0;
1993 offsets
->pre_header_padding
= 0;
1995 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1996 if ((int64_t) ctx
->tsc
== -EIO
)
1999 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2000 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2002 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2003 offsets
->switch_new_start
= 1; /* For offsets->begin */
2005 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2007 &offsets
->pre_header_padding
,
2010 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2013 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2014 offsets
->size
> chan
->backend
.subbuf_size
)) {
2015 offsets
->switch_old_end
= 1; /* For offsets->old */
2016 offsets
->switch_new_start
= 1; /* For offsets->begin */
2019 if (unlikely(offsets
->switch_new_start
)) {
2020 unsigned long sb_index
, commit_count
;
2023 * We are typically not filling the previous buffer completely.
2025 if (likely(offsets
->switch_old_end
))
2026 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2027 offsets
->begin
= offsets
->begin
2028 + config
->cb
.subbuffer_header_size();
2029 /* Test new buffer integrity */
2030 sb_index
= subbuf_index(offsets
->begin
, chan
);
2032 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2033 * lib_ring_buffer_check_deliver() has the matching
2034 * memory barriers required around commit_cold cc_sb
2035 * updates to ensure reserve and commit counter updates
2036 * are not seen reordered when updated by another CPU.
2039 commit_count
= v_read(config
,
2040 &buf
->commit_cold
[sb_index
].cc_sb
);
2041 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2043 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2045 * The reserve counter have been concurrently updated
2046 * while we read the commit counter. This means the
2047 * commit counter we read might not match buf->offset
2048 * due to concurrent update. We therefore need to retry.
2052 reserve_commit_diff
=
2053 (buf_trunc(offsets
->begin
, chan
)
2054 >> chan
->backend
.num_subbuf_order
)
2055 - (commit_count
& chan
->commit_count_mask
);
2056 if (likely(reserve_commit_diff
== 0)) {
2057 /* Next subbuffer not being written to. */
2058 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2059 subbuf_trunc(offsets
->begin
, chan
)
2060 - subbuf_trunc((unsigned long)
2061 atomic_long_read(&buf
->consumed
), chan
)
2062 >= chan
->backend
.buf_size
)) {
2064 * We do not overwrite non consumed buffers
2065 * and we are full : record is lost.
2067 v_inc(config
, &buf
->records_lost_full
);
2071 * Next subbuffer not being written to, and we
2072 * are either in overwrite mode or the buffer is
2073 * not full. It's safe to write in this new
2079 * Next subbuffer reserve offset does not match the
2080 * commit offset, and this did not involve update to the
2081 * reserve counter. Drop record in producer-consumer and
2082 * overwrite mode. Caused by either a writer OOPS or
2083 * too many nested writes over a reserve/commit pair.
2085 v_inc(config
, &buf
->records_lost_wrap
);
2089 config
->cb
.record_header_size(config
, chan
,
2091 &offsets
->pre_header_padding
,
2094 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2097 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2098 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2100 * Record too big for subbuffers, report error, don't
2101 * complete the sub-buffer switch.
2103 v_inc(config
, &buf
->records_lost_big
);
2107 * We just made a successful buffer switch and the
2108 * record fits in the new subbuffer. Let's write.
2113 * Record fits in the current buffer and we are not on a switch
2114 * boundary. It's safe to write.
2117 offsets
->end
= offsets
->begin
+ offsets
->size
;
2119 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2121 * The offset_end will fall at the very beginning of the next
2124 offsets
->switch_new_end
= 1; /* For offsets->begin */
2129 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2131 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2133 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2134 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2136 return chan
->backend
.buf
;
2139 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2141 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2142 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2144 v_inc(config
, &buf
->records_lost_big
);
2146 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2149 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2150 * @ctx: ring buffer context.
2152 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2153 * -EIO for other errors, else returns 0.
2154 * It will take care of sub-buffer switching.
2156 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
,
2159 struct channel
*chan
= ctx
->chan
;
2160 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2161 struct lib_ring_buffer
*buf
;
2162 struct switch_offsets offsets
;
2165 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2169 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2173 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2178 * Atomically update last_tsc. This update races against concurrent
2179 * atomic updates, but the race will always cause supplementary full TSC
2180 * records, never the opposite (missing a full TSC record when it would
2183 save_last_tsc(config
, buf
, ctx
->tsc
);
2186 * Push the reader if necessary
2188 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2191 * Clear noref flag for this subbuffer.
2193 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2194 subbuf_index(offsets
.end
- 1, chan
));
2197 * Switch old subbuffer if needed.
2199 if (unlikely(offsets
.switch_old_end
)) {
2200 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2201 subbuf_index(offsets
.old
- 1, chan
));
2202 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2206 * Populate new subbuffer.
2208 if (unlikely(offsets
.switch_new_start
))
2209 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2211 if (unlikely(offsets
.switch_new_end
))
2212 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2214 ctx
->slot_size
= offsets
.size
;
2215 ctx
->pre_offset
= offsets
.begin
;
2216 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2219 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2222 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2223 struct lib_ring_buffer
*buf
,
2224 unsigned long commit_count
,
2227 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2228 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2232 * The ring buffer can count events recorded and overwritten per buffer,
2233 * but it is disabled by default due to its performance overhead.
2235 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2237 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2238 struct lib_ring_buffer
*buf
,
2241 v_add(config
, subbuffer_get_records_count(config
,
2242 &buf
->backend
, idx
),
2243 &buf
->records_count
);
2244 v_add(config
, subbuffer_count_records_overrun(config
,
2245 &buf
->backend
, idx
),
2246 &buf
->records_overrun
);
2248 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2250 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2251 struct lib_ring_buffer
*buf
,
2255 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2258 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2259 struct lib_ring_buffer
*buf
,
2260 struct channel
*chan
,
2261 unsigned long offset
,
2262 unsigned long commit_count
,
2266 unsigned long old_commit_count
= commit_count
2267 - chan
->backend
.subbuf_size
;
2270 * If we succeeded at updating cc_sb below, we are the subbuffer
2271 * writer delivering the subbuffer. Deals with concurrent
2272 * updates of the "cc" value without adding a add_return atomic
2273 * operation to the fast path.
2275 * We are doing the delivery in two steps:
2276 * - First, we cmpxchg() cc_sb to the new value
2277 * old_commit_count + 1. This ensures that we are the only
2278 * subbuffer user successfully filling the subbuffer, but we
2279 * do _not_ set the cc_sb value to "commit_count" yet.
2280 * Therefore, other writers that would wrap around the ring
2281 * buffer and try to start writing to our subbuffer would
2282 * have to drop records, because it would appear as
2284 * We therefore have exclusive access to the subbuffer control
2285 * structures. This mutual exclusion with other writers is
2286 * crucially important to perform record overruns count in
2287 * flight recorder mode locklessly.
2288 * - When we are ready to release the subbuffer (either for
2289 * reading or for overrun by other writers), we simply set the
2290 * cc_sb value to "commit_count" and perform delivery.
2292 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2293 * This guarantees that old_commit_count + 1 != commit_count.
2297 * Order prior updates to reserve count prior to the
2298 * commit_cold cc_sb update.
2301 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2302 old_commit_count
, old_commit_count
+ 1)
2303 == old_commit_count
)) {
2307 * Start of exclusive subbuffer access. We are
2308 * guaranteed to be the last writer in this subbuffer
2309 * and any other writer trying to access this subbuffer
2310 * in this state is required to drop records.
2312 * We can read the ts_end for the current sub-buffer
2313 * which has been saved by the very last space
2314 * reservation for the current sub-buffer.
2316 * Order increment of commit counter before reading ts_end.
2319 ts_end
= &buf
->ts_end
[idx
];
2320 deliver_count_events(config
, buf
, idx
);
2321 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2322 lib_ring_buffer_get_data_size(config
,
2327 * Increment the packet counter while we have exclusive
2330 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2333 * Set noref flag and offset for this subbuffer id.
2334 * Contains a memory barrier that ensures counter stores
2335 * are ordered before set noref and offset.
2337 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2338 buf_trunc_val(offset
, chan
));
2341 * Order set_noref and record counter updates before the
2342 * end of subbuffer exclusive access. Orders with
2343 * respect to writers coming into the subbuffer after
2344 * wrap around, and also order wrt concurrent readers.
2347 /* End of exclusive subbuffer access */
2348 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2351 * Order later updates to reserve count after
2352 * the commit_cold cc_sb update.
2355 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2359 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2361 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2362 && atomic_long_read(&buf
->active_readers
)
2363 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2364 wake_up_interruptible(&buf
->read_wait
);
2365 wake_up_interruptible(&chan
->read_wait
);
2370 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2372 int __init
init_lib_ring_buffer_frontend(void)
2376 for_each_possible_cpu(cpu
)
2377 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2381 module_init(init_lib_ring_buffer_frontend
);
2383 void __exit
exit_lib_ring_buffer_frontend(void)
2387 module_exit(exit_lib_ring_buffer_frontend
);