1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
3 * ring_buffer_frontend.c
5 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
8 * recorder (overwrite) modes. See thesis:
10 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
11 * dissertation, Ecole Polytechnique de Montreal.
12 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
14 * - Algorithm presentation in Chapter 5:
15 * "Lockless Multi-Core High-Throughput Buffering".
16 * - Algorithm formal verification in Section 8.6:
17 * "Formal verification of LTTng"
20 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
22 * Inspired from LTT and RelayFS:
23 * Karim Yaghmour <karim@opersys.com>
24 * Tom Zanussi <zanussi@us.ibm.com>
25 * Bob Wisniewski <bob@watson.ibm.com>
27 * Bob Wisniewski <bob@watson.ibm.com>
29 * Buffer reader semantic :
32 * while buffer is not finalized and empty
34 * - if return value != 0, continue
35 * - splice one subbuffer worth of data to a pipe
36 * - splice the data from pipe to disk/network
40 #include <linux/delay.h>
41 #include <linux/module.h>
42 #include <linux/percpu.h>
43 #include <linux/kref.h>
44 #include <asm/cacheflush.h>
46 #include <wrapper/ringbuffer/config.h>
47 #include <wrapper/ringbuffer/backend.h>
48 #include <wrapper/ringbuffer/frontend.h>
49 #include <wrapper/ringbuffer/iterator.h>
50 #include <wrapper/ringbuffer/nohz.h>
51 #include <wrapper/atomic.h>
52 #include <wrapper/percpu-defs.h>
53 #include <wrapper/timer.h>
56 * Internal structure representing offsets to use at a sub-buffer switch.
58 struct switch_offsets
{
59 unsigned long begin
, end
, old
;
60 size_t pre_header_padding
, size
;
61 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
72 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
73 #endif /* CONFIG_NO_HZ */
75 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
77 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
78 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
81 void lib_ring_buffer_print_errors(struct channel
*chan
,
82 struct lib_ring_buffer
*buf
, int cpu
);
84 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
85 enum switch_mode mode
);
88 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
89 struct lib_ring_buffer
*buf
,
92 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
94 consumed_old
= atomic_long_read(&buf
->consumed
);
95 consumed_idx
= subbuf_index(consumed_old
, chan
);
96 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
98 * No memory barrier here, since we are only interested
99 * in a statistically correct polling result. The next poll will
100 * get the data is we are racing. The mb() that ensures correct
101 * memory order is in get_subbuf.
103 write_offset
= v_read(config
, &buf
->offset
);
106 * Check that the subbuffer we are trying to consume has been
107 * already fully committed.
110 if (((commit_count
- chan
->backend
.subbuf_size
)
111 & chan
->commit_count_mask
)
112 - (buf_trunc(consumed_old
, chan
)
113 >> chan
->backend
.num_subbuf_order
)
118 * Check that we are not about to read the same subbuffer in
119 * which the writer head is.
121 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
129 * Must be called under cpu hotplug protection.
131 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
133 struct channel
*chan
= buf
->backend
.chan
;
135 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
136 kvfree(buf
->commit_hot
);
137 kvfree(buf
->commit_cold
);
140 lib_ring_buffer_backend_free(&buf
->backend
);
144 * lib_ring_buffer_reset - Reset ring buffer to initial values.
147 * Effectively empty the ring buffer. Should be called when the buffer is not
148 * used for writing. The ring buffer can be opened for reading, but the reader
149 * should not be using the iterator concurrently with reset. The previous
150 * current iterator record is reset.
152 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
154 struct channel
*chan
= buf
->backend
.chan
;
155 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
159 * Reset iterator first. It will put the subbuffer if it currently holds
162 lib_ring_buffer_iterator_reset(buf
);
163 v_set(config
, &buf
->offset
, 0);
164 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
165 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
166 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
167 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
170 atomic_long_set(&buf
->consumed
, 0);
171 atomic_set(&buf
->record_disabled
, 0);
172 v_set(config
, &buf
->last_tsc
, 0);
173 lib_ring_buffer_backend_reset(&buf
->backend
);
174 /* Don't reset number of active readers */
175 v_set(config
, &buf
->records_lost_full
, 0);
176 v_set(config
, &buf
->records_lost_wrap
, 0);
177 v_set(config
, &buf
->records_lost_big
, 0);
178 v_set(config
, &buf
->records_count
, 0);
179 v_set(config
, &buf
->records_overrun
, 0);
182 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
185 * channel_reset - Reset channel to initial values.
188 * Effectively empty the channel. Should be called when the channel is not used
189 * for writing. The channel can be opened for reading, but the reader should not
190 * be using the iterator concurrently with reset. The previous current iterator
193 void channel_reset(struct channel
*chan
)
196 * Reset iterators first. Will put the subbuffer if held for reading.
198 channel_iterator_reset(chan
);
199 atomic_set(&chan
->record_disabled
, 0);
200 /* Don't reset commit_count_mask, still valid */
201 channel_backend_reset(&chan
->backend
);
202 /* Don't reset switch/read timer interval */
203 /* Don't reset notifiers and notifier enable bits */
204 /* Don't reset reader reference count */
206 EXPORT_SYMBOL_GPL(channel_reset
);
209 * Must be called under cpu hotplug protection.
211 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
212 struct channel_backend
*chanb
, int cpu
)
214 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
215 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
216 void *priv
= chanb
->priv
;
217 size_t subbuf_header_size
;
221 /* Test for cpu hotplug */
222 if (buf
->backend
.allocated
)
226 * Paranoia: per cpu dynamic allocation is not officially documented as
227 * zeroing the memory, so let's do it here too, just in case.
229 memset(buf
, 0, sizeof(*buf
));
231 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
236 kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
237 * chan
->backend
.num_subbuf
,
238 1 << INTERNODE_CACHE_SHIFT
),
239 GFP_KERNEL
| __GFP_NOWARN
,
240 cpu_to_node(max(cpu
, 0)));
241 if (!buf
->commit_hot
) {
247 kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
248 * chan
->backend
.num_subbuf
,
249 1 << INTERNODE_CACHE_SHIFT
),
250 GFP_KERNEL
| __GFP_NOWARN
,
251 cpu_to_node(max(cpu
, 0)));
252 if (!buf
->commit_cold
) {
258 kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
259 * chan
->backend
.num_subbuf
,
260 1 << INTERNODE_CACHE_SHIFT
),
261 GFP_KERNEL
| __GFP_NOWARN
,
262 cpu_to_node(max(cpu
, 0)));
265 goto free_commit_cold
;
268 init_waitqueue_head(&buf
->read_wait
);
269 init_waitqueue_head(&buf
->write_wait
);
270 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
273 * Write the subbuffer header for first subbuffer so we know the total
274 * duration of data gathering.
276 subbuf_header_size
= config
->cb
.subbuffer_header_size();
277 v_set(config
, &buf
->offset
, subbuf_header_size
);
278 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
279 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
280 config
->cb
.buffer_begin(buf
, tsc
, 0);
281 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
283 if (config
->cb
.buffer_create
) {
284 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
290 * Ensure the buffer is ready before setting it to allocated and setting
292 * Used for cpu hotplug vs cpumask iteration.
295 buf
->backend
.allocated
= 1;
297 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
298 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
299 chan
->backend
.cpumask
));
300 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
308 kvfree(buf
->commit_cold
);
310 kvfree(buf
->commit_hot
);
312 lib_ring_buffer_backend_free(&buf
->backend
);
316 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
318 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, switch_timer
);
319 struct channel
*chan
= buf
->backend
.chan
;
320 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
323 * Only flush buffers periodically if readers are active.
325 if (atomic_long_read(&buf
->active_readers
))
326 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
328 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
329 lttng_mod_timer_pinned(&buf
->switch_timer
,
330 jiffies
+ chan
->switch_timer_interval
);
332 mod_timer(&buf
->switch_timer
,
333 jiffies
+ chan
->switch_timer_interval
);
337 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
339 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
341 struct channel
*chan
= buf
->backend
.chan
;
342 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
343 unsigned int flags
= 0;
345 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
348 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
349 flags
= LTTNG_TIMER_PINNED
;
351 lttng_timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
, buf
);
352 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
354 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
355 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
357 add_timer(&buf
->switch_timer
);
359 buf
->switch_timer_enabled
= 1;
363 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
365 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
367 struct channel
*chan
= buf
->backend
.chan
;
369 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
372 del_timer_sync(&buf
->switch_timer
);
373 buf
->switch_timer_enabled
= 0;
377 * Polling timer to check the channels for data.
379 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
381 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, read_timer
);
382 struct channel
*chan
= buf
->backend
.chan
;
383 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
385 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
387 if (atomic_long_read(&buf
->active_readers
)
388 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
389 wake_up_interruptible(&buf
->read_wait
);
390 wake_up_interruptible(&chan
->read_wait
);
393 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
394 lttng_mod_timer_pinned(&buf
->read_timer
,
395 jiffies
+ chan
->read_timer_interval
);
397 mod_timer(&buf
->read_timer
,
398 jiffies
+ chan
->read_timer_interval
);
402 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
404 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
406 struct channel
*chan
= buf
->backend
.chan
;
407 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
408 unsigned int flags
= 0;
410 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
411 || !chan
->read_timer_interval
412 || buf
->read_timer_enabled
)
415 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
416 flags
= LTTNG_TIMER_PINNED
;
418 lttng_timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
, buf
);
419 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
421 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
422 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
424 add_timer(&buf
->read_timer
);
426 buf
->read_timer_enabled
= 1;
430 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
432 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
434 struct channel
*chan
= buf
->backend
.chan
;
435 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
437 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
438 || !chan
->read_timer_interval
439 || !buf
->read_timer_enabled
)
442 del_timer_sync(&buf
->read_timer
);
444 * do one more check to catch data that has been written in the last
447 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
448 wake_up_interruptible(&buf
->read_wait
);
449 wake_up_interruptible(&chan
->read_wait
);
451 buf
->read_timer_enabled
= 0;
454 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
456 enum cpuhp_state lttng_rb_hp_prepare
;
457 enum cpuhp_state lttng_rb_hp_online
;
459 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
461 lttng_rb_hp_prepare
= val
;
463 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
465 void lttng_rb_set_hp_online(enum cpuhp_state val
)
467 lttng_rb_hp_online
= val
;
469 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
471 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
472 struct lttng_cpuhp_node
*node
)
474 struct channel
*chan
= container_of(node
, struct channel
,
476 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
477 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
479 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
482 * Performing a buffer switch on a remote CPU. Performed by
483 * the CPU responsible for doing the hotunplug after the target
484 * CPU stopped running completely. Ensures that all data
485 * from that remote CPU is flushed.
487 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
490 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
492 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
493 struct lttng_cpuhp_node
*node
)
495 struct channel
*chan
= container_of(node
, struct channel
,
497 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
498 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
500 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
502 wake_up_interruptible(&chan
->hp_wait
);
503 lib_ring_buffer_start_switch_timer(buf
);
504 lib_ring_buffer_start_read_timer(buf
);
507 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
509 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
510 struct lttng_cpuhp_node
*node
)
512 struct channel
*chan
= container_of(node
, struct channel
,
514 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
515 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
517 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
519 lib_ring_buffer_stop_switch_timer(buf
);
520 lib_ring_buffer_stop_read_timer(buf
);
523 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
525 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
527 #ifdef CONFIG_HOTPLUG_CPU
530 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
531 * @nb: notifier block
532 * @action: hotplug action to take
535 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
538 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
539 unsigned long action
,
542 unsigned int cpu
= (unsigned long)hcpu
;
543 struct channel
*chan
= container_of(nb
, struct channel
,
545 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
546 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
548 if (!chan
->cpu_hp_enable
)
551 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
554 case CPU_DOWN_FAILED
:
555 case CPU_DOWN_FAILED_FROZEN
:
557 case CPU_ONLINE_FROZEN
:
558 wake_up_interruptible(&chan
->hp_wait
);
559 lib_ring_buffer_start_switch_timer(buf
);
560 lib_ring_buffer_start_read_timer(buf
);
563 case CPU_DOWN_PREPARE
:
564 case CPU_DOWN_PREPARE_FROZEN
:
565 lib_ring_buffer_stop_switch_timer(buf
);
566 lib_ring_buffer_stop_read_timer(buf
);
570 case CPU_DEAD_FROZEN
:
572 * Performing a buffer switch on a remote CPU. Performed by
573 * the CPU responsible for doing the hotunplug after the target
574 * CPU stopped running completely. Ensures that all data
575 * from that remote CPU is flushed.
577 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
587 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
589 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
591 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
592 * that wake-up-tracing generated events are flushed before going idle (in
593 * tick_nohz). We test if the spinlock is locked to deal with the race where
594 * readers try to sample the ring buffer before we perform the switch. We let
595 * the readers retry in that case. If there is data in the buffer, the wake up
596 * is going to forbid the CPU running the reader thread from going idle.
598 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
602 struct channel
*chan
= container_of(nb
, struct channel
,
604 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
605 struct lib_ring_buffer
*buf
;
606 int cpu
= smp_processor_id();
608 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
610 * We don't support keeping the system idle with global buffers
611 * and streaming active. In order to do so, we would need to
612 * sample a non-nohz-cpumask racelessly with the nohz updates
613 * without adding synchronization overhead to nohz. Leave this
614 * use-case out for now.
619 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
621 case TICK_NOHZ_FLUSH
:
622 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
623 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
624 && chan
->read_timer_interval
625 && atomic_long_read(&buf
->active_readers
)
626 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
627 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
628 wake_up_interruptible(&buf
->read_wait
);
629 wake_up_interruptible(&chan
->read_wait
);
631 if (chan
->switch_timer_interval
)
632 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
633 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
636 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
637 lib_ring_buffer_stop_switch_timer(buf
);
638 lib_ring_buffer_stop_read_timer(buf
);
639 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
641 case TICK_NOHZ_RESTART
:
642 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
643 lib_ring_buffer_start_read_timer(buf
);
644 lib_ring_buffer_start_switch_timer(buf
);
645 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
652 void notrace
lib_ring_buffer_tick_nohz_flush(void)
654 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
658 void notrace
lib_ring_buffer_tick_nohz_stop(void)
660 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
664 void notrace
lib_ring_buffer_tick_nohz_restart(void)
666 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
669 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
674 static void channel_unregister_notifiers(struct channel
*chan
)
676 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
678 channel_iterator_unregister_notifiers(chan
);
679 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
682 * Remove the nohz notifier first, so we are certain we stop
685 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
686 &chan
->tick_nohz_notifier
);
688 * ring_buffer_nohz_lock will not be needed below, because
689 * we just removed the notifiers, which were the only source of
692 #endif /* CONFIG_NO_HZ */
693 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
697 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
698 &chan
->cpuhp_online
.node
);
700 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
701 &chan
->cpuhp_prepare
.node
);
704 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
708 #ifdef CONFIG_HOTPLUG_CPU
710 chan
->cpu_hp_enable
= 0;
711 for_each_online_cpu(cpu
) {
712 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
714 lib_ring_buffer_stop_switch_timer(buf
);
715 lib_ring_buffer_stop_read_timer(buf
);
718 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
720 for_each_possible_cpu(cpu
) {
721 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
723 lib_ring_buffer_stop_switch_timer(buf
);
724 lib_ring_buffer_stop_read_timer(buf
);
728 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
730 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
732 lib_ring_buffer_stop_switch_timer(buf
);
733 lib_ring_buffer_stop_read_timer(buf
);
735 channel_backend_unregister_notifiers(&chan
->backend
);
738 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
740 if (!buf
->quiescent
) {
741 buf
->quiescent
= true;
742 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
746 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
748 buf
->quiescent
= false;
751 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
754 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
756 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
758 for_each_channel_cpu(cpu
, chan
) {
759 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
762 lib_ring_buffer_set_quiescent(buf
);
766 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
768 lib_ring_buffer_set_quiescent(buf
);
771 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
773 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
776 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
778 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
780 for_each_channel_cpu(cpu
, chan
) {
781 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
784 lib_ring_buffer_clear_quiescent(buf
);
788 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
790 lib_ring_buffer_clear_quiescent(buf
);
793 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
795 static void channel_free(struct channel
*chan
)
797 if (chan
->backend
.release_priv_ops
) {
798 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
800 channel_iterator_free(chan
);
801 channel_backend_free(&chan
->backend
);
806 * channel_create - Create channel.
807 * @config: ring buffer instance configuration
808 * @name: name of the channel
809 * @priv: ring buffer client private data
810 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
811 * address mapping. It is used only by RING_BUFFER_STATIC
812 * configuration. It can be set to NULL for other backends.
813 * @subbuf_size: subbuffer size
814 * @num_subbuf: number of subbuffers
815 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
816 * padding to let readers get those sub-buffers.
817 * Used for live streaming.
818 * @read_timer_interval: Time interval (in us) to wake up pending readers.
821 * Returns NULL on failure.
823 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
824 const char *name
, void *priv
, void *buf_addr
,
826 size_t num_subbuf
, unsigned int switch_timer_interval
,
827 unsigned int read_timer_interval
)
830 struct channel
*chan
;
832 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
833 read_timer_interval
))
836 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
840 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
841 subbuf_size
, num_subbuf
);
845 ret
= channel_iterator_init(chan
);
847 goto error_free_backend
;
849 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
850 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
851 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
852 kref_init(&chan
->ref
);
853 init_waitqueue_head(&chan
->read_wait
);
854 init_waitqueue_head(&chan
->hp_wait
);
856 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
857 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
858 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
859 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
860 &chan
->cpuhp_prepare
.node
);
862 goto cpuhp_prepare_error
;
864 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
865 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
866 &chan
->cpuhp_online
.node
);
868 goto cpuhp_online_error
;
869 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
873 * In case of non-hotplug cpu, if the ring-buffer is allocated
874 * in early initcall, it will not be notified of secondary cpus.
875 * In that off case, we need to allocate for all possible cpus.
877 #ifdef CONFIG_HOTPLUG_CPU
878 chan
->cpu_hp_notifier
.notifier_call
=
879 lib_ring_buffer_cpu_hp_callback
;
880 chan
->cpu_hp_notifier
.priority
= 6;
881 register_cpu_notifier(&chan
->cpu_hp_notifier
);
884 for_each_online_cpu(cpu
) {
885 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
887 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
888 lib_ring_buffer_start_switch_timer(buf
);
889 lib_ring_buffer_start_read_timer(buf
);
890 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
892 chan
->cpu_hp_enable
= 1;
895 for_each_possible_cpu(cpu
) {
896 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
898 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
899 lib_ring_buffer_start_switch_timer(buf
);
900 lib_ring_buffer_start_read_timer(buf
);
901 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
905 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
907 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
908 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
909 chan
->tick_nohz_notifier
.notifier_call
=
910 ring_buffer_tick_nohz_callback
;
911 chan
->tick_nohz_notifier
.priority
= ~0U;
912 atomic_notifier_chain_register(&tick_nohz_notifier
,
913 &chan
->tick_nohz_notifier
);
914 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
917 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
919 lib_ring_buffer_start_switch_timer(buf
);
920 lib_ring_buffer_start_read_timer(buf
);
925 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
927 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
928 &chan
->cpuhp_prepare
.node
);
931 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
933 channel_backend_free(&chan
->backend
);
938 EXPORT_SYMBOL_GPL(channel_create
);
941 void channel_release(struct kref
*kref
)
943 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
948 * channel_destroy - Finalize, wait for q.s. and destroy channel.
949 * @chan: channel to destroy
952 * Call "destroy" callback, finalize channels, and then decrement the
953 * channel reference count. Note that when readers have completed data
954 * consumption of finalized channels, get_subbuf() will return -ENODATA.
955 * They should release their handle at that point. Returns the private
958 void *channel_destroy(struct channel
*chan
)
961 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
964 channel_unregister_notifiers(chan
);
966 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
968 * No need to hold cpu hotplug, because all notifiers have been
971 for_each_channel_cpu(cpu
, chan
) {
972 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
975 if (config
->cb
.buffer_finalize
)
976 config
->cb
.buffer_finalize(buf
,
980 * Perform flush before writing to finalized.
983 WRITE_ONCE(buf
->finalized
, 1);
984 wake_up_interruptible(&buf
->read_wait
);
987 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
989 if (config
->cb
.buffer_finalize
)
990 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
992 * Perform flush before writing to finalized.
995 WRITE_ONCE(buf
->finalized
, 1);
996 wake_up_interruptible(&buf
->read_wait
);
998 WRITE_ONCE(chan
->finalized
, 1);
999 wake_up_interruptible(&chan
->hp_wait
);
1000 wake_up_interruptible(&chan
->read_wait
);
1001 priv
= chan
->backend
.priv
;
1002 kref_put(&chan
->ref
, channel_release
);
1005 EXPORT_SYMBOL_GPL(channel_destroy
);
1007 struct lib_ring_buffer
*channel_get_ring_buffer(
1008 const struct lib_ring_buffer_config
*config
,
1009 struct channel
*chan
, int cpu
)
1011 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1012 return chan
->backend
.buf
;
1014 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1016 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1018 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1020 struct channel
*chan
= buf
->backend
.chan
;
1022 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1024 kref_get(&chan
->ref
);
1025 lttng_smp_mb__after_atomic();
1028 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1030 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1032 struct channel
*chan
= buf
->backend
.chan
;
1034 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1035 lttng_smp_mb__before_atomic();
1036 atomic_long_dec(&buf
->active_readers
);
1037 kref_put(&chan
->ref
, channel_release
);
1039 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1042 * Promote compiler barrier to a smp_mb().
1043 * For the specific ring buffer case, this IPI call should be removed if the
1044 * architecture does not reorder writes. This should eventually be provided by
1045 * a separate architecture-specific infrastructure.
1047 static void remote_mb(void *info
)
1053 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1055 * @consumed: consumed count indicating the position where to read
1056 * @produced: produced count, indicates position when to stop reading
1058 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1059 * data to read at consumed position, or 0 if the get operation succeeds.
1060 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1063 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1064 unsigned long *consumed
, unsigned long *produced
)
1066 struct channel
*chan
= buf
->backend
.chan
;
1067 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1068 unsigned long consumed_cur
, write_offset
;
1072 finalized
= READ_ONCE(buf
->finalized
);
1074 * Read finalized before counters.
1077 consumed_cur
= atomic_long_read(&buf
->consumed
);
1079 * No need to issue a memory barrier between consumed count read and
1080 * write offset read, because consumed count can only change
1081 * concurrently in overwrite mode, and we keep a sequence counter
1082 * identifier derived from the write offset to check we are getting
1083 * the same sub-buffer we are expecting (the sub-buffers are atomically
1084 * "tagged" upon writes, tags are checked upon read).
1086 write_offset
= v_read(config
, &buf
->offset
);
1089 * Check that we are not about to read the same subbuffer in
1090 * which the writer head is.
1092 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1096 *consumed
= consumed_cur
;
1097 *produced
= subbuf_trunc(write_offset
, chan
);
1103 * The memory barriers __wait_event()/wake_up_interruptible() take care
1104 * of "raw_spin_is_locked" memory ordering.
1108 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1113 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1116 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1117 * are saved regardless of whether the consumed and produced positions are
1118 * in the same subbuffer.
1120 * @consumed: consumed byte count indicating the last position read
1121 * @produced: produced byte count indicating the last position written
1123 * This function is meant to provide information on the exact producer and
1124 * consumer positions without regard for the "snapshot" feature.
1126 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1127 unsigned long *consumed
, unsigned long *produced
)
1129 struct channel
*chan
= buf
->backend
.chan
;
1130 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1133 *consumed
= atomic_long_read(&buf
->consumed
);
1135 * No need to issue a memory barrier between consumed count read and
1136 * write offset read, because consumed count can only change
1137 * concurrently in overwrite mode, and we keep a sequence counter
1138 * identifier derived from the write offset to check we are getting
1139 * the same sub-buffer we are expecting (the sub-buffers are atomically
1140 * "tagged" upon writes, tags are checked upon read).
1142 *produced
= v_read(config
, &buf
->offset
);
1147 * lib_ring_buffer_put_snapshot - move consumed counter forward
1149 * Should only be called from consumer context.
1151 * @consumed_new: new consumed count value
1153 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1154 unsigned long consumed_new
)
1156 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1157 struct channel
*chan
= bufb
->chan
;
1158 unsigned long consumed
;
1160 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1163 * Only push the consumed value forward.
1164 * If the consumed cmpxchg fails, this is because we have been pushed by
1165 * the writer in flight recorder mode.
1167 consumed
= atomic_long_read(&buf
->consumed
);
1168 while ((long) consumed
- (long) consumed_new
< 0)
1169 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1171 /* Wake-up the metadata producer */
1172 wake_up_interruptible(&buf
->write_wait
);
1174 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1176 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1177 static void lib_ring_buffer_flush_read_subbuf_dcache(
1178 const struct lib_ring_buffer_config
*config
,
1179 struct channel
*chan
,
1180 struct lib_ring_buffer
*buf
)
1182 struct lib_ring_buffer_backend_pages
*pages
;
1183 unsigned long sb_bindex
, id
, i
, nr_pages
;
1185 if (config
->output
!= RING_BUFFER_MMAP
)
1189 * Architectures with caches aliased on virtual addresses may
1190 * use different cache lines for the linear mapping vs
1191 * user-space memory mapping. Given that the ring buffer is
1192 * based on the kernel linear mapping, aligning it with the
1193 * user-space mapping is not straightforward, and would require
1194 * extra TLB entries. Therefore, simply flush the dcache for the
1195 * entire sub-buffer before reading it.
1197 id
= buf
->backend
.buf_rsb
.id
;
1198 sb_bindex
= subbuffer_id_get_index(config
, id
);
1199 pages
= buf
->backend
.array
[sb_bindex
];
1200 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1201 for (i
= 0; i
< nr_pages
; i
++) {
1202 struct lib_ring_buffer_backend_page
*backend_page
;
1204 backend_page
= &pages
->p
[i
];
1205 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1209 static void lib_ring_buffer_flush_read_subbuf_dcache(
1210 const struct lib_ring_buffer_config
*config
,
1211 struct channel
*chan
,
1212 struct lib_ring_buffer
*buf
)
1218 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1220 * @consumed: consumed count indicating the position where to read
1222 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1223 * data to read at consumed position, or 0 if the get operation succeeds.
1224 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1226 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1227 unsigned long consumed
)
1229 struct channel
*chan
= buf
->backend
.chan
;
1230 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1231 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1235 if (buf
->get_subbuf
) {
1237 * Reader is trying to get a subbuffer twice.
1239 CHAN_WARN_ON(chan
, 1);
1243 finalized
= READ_ONCE(buf
->finalized
);
1245 * Read finalized before counters.
1248 consumed_cur
= atomic_long_read(&buf
->consumed
);
1249 consumed_idx
= subbuf_index(consumed
, chan
);
1250 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1252 * Make sure we read the commit count before reading the buffer
1253 * data and the write offset. Correct consumed offset ordering
1254 * wrt commit count is insured by the use of cmpxchg to update
1255 * the consumed offset.
1256 * smp_call_function_single can fail if the remote CPU is offline,
1257 * this is OK because then there is no wmb to execute there.
1258 * If our thread is executing on the same CPU as the on the buffers
1259 * belongs to, we don't have to synchronize it at all. If we are
1260 * migrated, the scheduler will take care of the memory barriers.
1261 * Normally, smp_call_function_single() should ensure program order when
1262 * executing the remote function, which implies that it surrounds the
1263 * function execution with :
1274 * However, smp_call_function_single() does not seem to clearly execute
1275 * such barriers. It depends on spinlock semantic to provide the barrier
1276 * before executing the IPI and, when busy-looping, csd_lock_wait only
1277 * executes smp_mb() when it has to wait for the other CPU.
1279 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1280 * required ourself, even if duplicated. It has no performance impact
1283 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1284 * read and write vs write. They do not ensure core synchronization. We
1285 * really have to ensure total order between the 3 barriers running on
1288 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1289 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1290 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1291 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1292 /* Total order with IPI handler smp_mb() */
1294 smp_call_function_single(buf
->backend
.cpu
,
1295 remote_mb
, NULL
, 1);
1296 /* Total order with IPI handler smp_mb() */
1300 /* Total order with IPI handler smp_mb() */
1302 smp_call_function(remote_mb
, NULL
, 1);
1303 /* Total order with IPI handler smp_mb() */
1308 * Local rmb to match the remote wmb to read the commit count
1309 * before the buffer data and the write offset.
1314 write_offset
= v_read(config
, &buf
->offset
);
1317 * Check that the buffer we are getting is after or at consumed_cur
1320 if ((long) subbuf_trunc(consumed
, chan
)
1321 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1325 * Check that the subbuffer we are trying to consume has been
1326 * already fully committed.
1328 if (((commit_count
- chan
->backend
.subbuf_size
)
1329 & chan
->commit_count_mask
)
1330 - (buf_trunc(consumed
, chan
)
1331 >> chan
->backend
.num_subbuf_order
)
1336 * Check that we are not about to read the same subbuffer in
1337 * which the writer head is.
1339 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1344 * Failure to get the subbuffer causes a busy-loop retry without going
1345 * to a wait queue. These are caused by short-lived race windows where
1346 * the writer is getting access to a subbuffer we were trying to get
1347 * access to. Also checks that the "consumed" buffer count we are
1348 * looking for matches the one contained in the subbuffer id.
1350 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1351 consumed_idx
, buf_trunc_val(consumed
, chan
));
1354 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1356 buf
->get_subbuf_consumed
= consumed
;
1357 buf
->get_subbuf
= 1;
1359 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1365 * The memory barriers __wait_event()/wake_up_interruptible() take care
1366 * of "raw_spin_is_locked" memory ordering.
1370 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1375 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1378 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1381 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1383 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1384 struct channel
*chan
= bufb
->chan
;
1385 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1386 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1388 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1390 if (!buf
->get_subbuf
) {
1392 * Reader puts a subbuffer it did not get.
1394 CHAN_WARN_ON(chan
, 1);
1397 consumed
= buf
->get_subbuf_consumed
;
1398 buf
->get_subbuf
= 0;
1401 * Clear the records_unread counter. (overruns counter)
1402 * Can still be non-zero if a file reader simply grabbed the data
1403 * without using iterators.
1404 * Can be below zero if an iterator is used on a snapshot more than
1407 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1408 v_add(config
, v_read(config
,
1409 &bufb
->array
[read_sb_bindex
]->records_unread
),
1410 &bufb
->records_read
);
1411 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1412 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1413 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1414 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1417 * Exchange the reader subbuffer with the one we put in its place in the
1418 * writer subbuffer table. Expect the original consumed count. If
1419 * update_read_sb_index fails, this is because the writer updated the
1420 * subbuffer concurrently. We should therefore keep the subbuffer we
1421 * currently have: it has become invalid to try reading this sub-buffer
1422 * consumed count value anyway.
1424 consumed_idx
= subbuf_index(consumed
, chan
);
1425 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1426 consumed_idx
, buf_trunc_val(consumed
, chan
));
1428 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1429 * if the writer concurrently updated it.
1432 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1435 * cons_offset is an iterator on all subbuffer offsets between the reader
1436 * position and the writer position. (inclusive)
1439 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1440 struct channel
*chan
,
1441 unsigned long cons_offset
,
1444 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1445 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1447 cons_idx
= subbuf_index(cons_offset
, chan
);
1448 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1449 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1451 if (subbuf_offset(commit_count
, chan
) != 0)
1453 "ring buffer %s, cpu %d: "
1454 "commit count in subbuffer %lu,\n"
1455 "expecting multiples of %lu bytes\n"
1456 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1457 chan
->backend
.name
, cpu
, cons_idx
,
1458 chan
->backend
.subbuf_size
,
1459 commit_count
, commit_count_sb
);
1461 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1462 chan
->backend
.name
, cpu
, commit_count
);
1466 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1467 struct channel
*chan
,
1468 void *priv
, int cpu
)
1470 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1471 unsigned long write_offset
, cons_offset
;
1474 * No need to order commit_count, write_offset and cons_offset reads
1475 * because we execute at teardown when no more writer nor reader
1476 * references are left.
1478 write_offset
= v_read(config
, &buf
->offset
);
1479 cons_offset
= atomic_long_read(&buf
->consumed
);
1480 if (write_offset
!= cons_offset
)
1482 "ring buffer %s, cpu %d: "
1483 "non-consumed data\n"
1484 " [ %lu bytes written, %lu bytes read ]\n",
1485 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1487 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1488 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1491 cons_offset
= subbuf_align(cons_offset
, chan
))
1492 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1496 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1498 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1499 struct lib_ring_buffer
*buf
,
1502 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1504 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1505 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1506 "%lu records overrun\n",
1508 v_read(config
, &buf
->records_count
),
1509 v_read(config
, &buf
->records_overrun
));
1511 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1512 "%lu records overrun\n",
1513 chan
->backend
.name
, cpu
,
1514 v_read(config
, &buf
->records_count
),
1515 v_read(config
, &buf
->records_overrun
));
1520 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1521 struct lib_ring_buffer
*buf
,
1528 void lib_ring_buffer_print_errors(struct channel
*chan
,
1529 struct lib_ring_buffer
*buf
, int cpu
)
1531 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1532 void *priv
= chan
->backend
.priv
;
1534 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1535 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1536 if (v_read(config
, &buf
->records_lost_full
)
1537 || v_read(config
, &buf
->records_lost_wrap
)
1538 || v_read(config
, &buf
->records_lost_big
))
1540 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1541 " [ %lu buffer full, %lu nest buffer wrap-around, "
1542 "%lu event too big ]\n",
1543 chan
->backend
.name
, cpu
,
1544 v_read(config
, &buf
->records_lost_full
),
1545 v_read(config
, &buf
->records_lost_wrap
),
1546 v_read(config
, &buf
->records_lost_big
));
1548 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1552 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1554 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1557 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1558 struct channel
*chan
,
1559 struct switch_offsets
*offsets
,
1562 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1563 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1564 unsigned long commit_count
;
1565 struct commit_counters_hot
*cc_hot
;
1567 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1570 * Order all writes to buffer before the commit count update that will
1571 * determine that the subbuffer is full.
1573 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1575 * Must write slot data before incrementing commit count. This
1576 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1582 cc_hot
= &buf
->commit_hot
[oldidx
];
1583 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1584 commit_count
= v_read(config
, &cc_hot
->cc
);
1585 /* Check if the written buffer has to be delivered */
1586 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1587 commit_count
, oldidx
, tsc
);
1588 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1589 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1590 commit_count
, cc_hot
);
1594 * lib_ring_buffer_switch_old_end: switch old subbuffer
1596 * Note : offset_old should never be 0 here. It is ok, because we never perform
1597 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1598 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1602 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1603 struct channel
*chan
,
1604 struct switch_offsets
*offsets
,
1607 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1608 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1609 unsigned long commit_count
, padding_size
, data_size
;
1610 struct commit_counters_hot
*cc_hot
;
1613 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1614 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1615 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1617 ts_end
= &buf
->ts_end
[oldidx
];
1619 * This is the last space reservation in that sub-buffer before
1620 * it gets delivered. This provides exclusive access to write to
1621 * this sub-buffer's ts_end. There are also no concurrent
1622 * readers of that ts_end because delivery of that sub-buffer is
1623 * postponed until the commit counter is incremented for the
1624 * current space reservation.
1629 * Order all writes to buffer and store to ts_end before the commit
1630 * count update that will determine that the subbuffer is full.
1632 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1634 * Must write slot data before incrementing commit count. This
1635 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1641 cc_hot
= &buf
->commit_hot
[oldidx
];
1642 v_add(config
, padding_size
, &cc_hot
->cc
);
1643 commit_count
= v_read(config
, &cc_hot
->cc
);
1644 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1645 commit_count
, oldidx
, tsc
);
1646 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1647 offsets
->old
+ padding_size
, commit_count
,
1652 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1654 * This code can be executed unordered : writers may already have written to the
1655 * sub-buffer before this code gets executed, caution. The commit makes sure
1656 * that this code is executed before the deliver of this sub-buffer.
1659 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1660 struct channel
*chan
,
1661 struct switch_offsets
*offsets
,
1664 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1665 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1666 unsigned long commit_count
;
1667 struct commit_counters_hot
*cc_hot
;
1669 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1672 * Order all writes to buffer before the commit count update that will
1673 * determine that the subbuffer is full.
1675 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1677 * Must write slot data before incrementing commit count. This
1678 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1684 cc_hot
= &buf
->commit_hot
[beginidx
];
1685 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1686 commit_count
= v_read(config
, &cc_hot
->cc
);
1687 /* Check if the written buffer has to be delivered */
1688 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1689 commit_count
, beginidx
, tsc
);
1690 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1691 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1692 commit_count
, cc_hot
);
1696 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1698 * Calls subbuffer_set_data_size() to set the data size of the current
1699 * sub-buffer. We do not need to perform check_deliver nor commit here,
1700 * since this task will be done by the "commit" of the event for which
1701 * we are currently doing the space reservation.
1704 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1705 struct channel
*chan
,
1706 struct switch_offsets
*offsets
,
1709 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1710 unsigned long endidx
, data_size
;
1713 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1714 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1715 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1716 ts_end
= &buf
->ts_end
[endidx
];
1718 * This is the last space reservation in that sub-buffer before
1719 * it gets delivered. This provides exclusive access to write to
1720 * this sub-buffer's ts_end. There are also no concurrent
1721 * readers of that ts_end because delivery of that sub-buffer is
1722 * postponed until the commit counter is incremented for the
1723 * current space reservation.
1731 * !0 if execution must be aborted.
1734 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1735 struct lib_ring_buffer
*buf
,
1736 struct channel
*chan
,
1737 struct switch_offsets
*offsets
,
1740 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1741 unsigned long off
, reserve_commit_diff
;
1743 offsets
->begin
= v_read(config
, &buf
->offset
);
1744 offsets
->old
= offsets
->begin
;
1745 offsets
->switch_old_start
= 0;
1746 off
= subbuf_offset(offsets
->begin
, chan
);
1748 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1751 * Ensure we flush the header of an empty subbuffer when doing the
1752 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1753 * total data gathering duration even if there were no records saved
1754 * after the last buffer switch.
1755 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1756 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1757 * subbuffer header as appropriate.
1758 * The next record that reserves space will be responsible for
1759 * populating the following subbuffer header. We choose not to populate
1760 * the next subbuffer header here because we want to be able to use
1761 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1762 * buffer flush, which must guarantee that all the buffer content
1763 * (records and header timestamps) are visible to the reader. This is
1764 * required for quiescence guarantees for the fusion merge.
1766 if (mode
!= SWITCH_FLUSH
&& !off
)
1767 return -1; /* we do not have to switch : buffer is empty */
1769 if (unlikely(off
== 0)) {
1770 unsigned long sb_index
, commit_count
;
1773 * We are performing a SWITCH_FLUSH. At this stage, there are no
1774 * concurrent writes into the buffer.
1776 * The client does not save any header information. Don't
1777 * switch empty subbuffer on finalize, because it is invalid to
1778 * deliver a completely empty subbuffer.
1780 if (!config
->cb
.subbuffer_header_size())
1783 /* Test new buffer integrity */
1784 sb_index
= subbuf_index(offsets
->begin
, chan
);
1785 commit_count
= v_read(config
,
1786 &buf
->commit_cold
[sb_index
].cc_sb
);
1787 reserve_commit_diff
=
1788 (buf_trunc(offsets
->begin
, chan
)
1789 >> chan
->backend
.num_subbuf_order
)
1790 - (commit_count
& chan
->commit_count_mask
);
1791 if (likely(reserve_commit_diff
== 0)) {
1792 /* Next subbuffer not being written to. */
1793 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1794 subbuf_trunc(offsets
->begin
, chan
)
1795 - subbuf_trunc((unsigned long)
1796 atomic_long_read(&buf
->consumed
), chan
)
1797 >= chan
->backend
.buf_size
)) {
1799 * We do not overwrite non consumed buffers
1800 * and we are full : don't switch.
1805 * Next subbuffer not being written to, and we
1806 * are either in overwrite mode or the buffer is
1807 * not full. It's safe to write in this new
1813 * Next subbuffer reserve offset does not match the
1814 * commit offset. Don't perform switch in
1815 * producer-consumer and overwrite mode. Caused by
1816 * either a writer OOPS or too many nested writes over a
1817 * reserve/commit pair.
1823 * Need to write the subbuffer start header on finalize.
1825 offsets
->switch_old_start
= 1;
1827 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1828 /* Note: old points to the next subbuf at offset 0 */
1829 offsets
->end
= offsets
->begin
;
1834 * Force a sub-buffer switch. This operation is completely reentrant : can be
1835 * called while tracing is active with absolutely no lock held.
1837 * Note, however, that as a v_cmpxchg is used for some atomic
1838 * operations, this function must be called from the CPU which owns the buffer
1839 * for a ACTIVE flush.
1841 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1843 struct channel
*chan
= buf
->backend
.chan
;
1844 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1845 struct switch_offsets offsets
;
1846 unsigned long oldidx
;
1852 * Perform retryable operations.
1855 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1857 return; /* Switch not needed */
1858 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1862 * Atomically update last_tsc. This update races against concurrent
1863 * atomic updates, but the race will always cause supplementary full TSC
1864 * records, never the opposite (missing a full TSC record when it would
1867 save_last_tsc(config
, buf
, tsc
);
1870 * Push the reader if necessary
1872 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1874 oldidx
= subbuf_index(offsets
.old
, chan
);
1875 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1878 * May need to populate header start on SWITCH_FLUSH.
1880 if (offsets
.switch_old_start
) {
1881 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1882 offsets
.old
+= config
->cb
.subbuffer_header_size();
1886 * Switch old subbuffer.
1888 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1890 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1892 struct switch_param
{
1893 struct lib_ring_buffer
*buf
;
1894 enum switch_mode mode
;
1897 static void remote_switch(void *info
)
1899 struct switch_param
*param
= info
;
1900 struct lib_ring_buffer
*buf
= param
->buf
;
1902 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1905 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1906 enum switch_mode mode
)
1908 struct channel
*chan
= buf
->backend
.chan
;
1909 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1911 struct switch_param param
;
1914 * With global synchronization we don't need to use the IPI scheme.
1916 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1917 lib_ring_buffer_switch_slow(buf
, mode
);
1922 * Disabling preemption ensures two things: first, that the
1923 * target cpu is not taken concurrently offline while we are within
1924 * smp_call_function_single(). Secondly, if it happens that the
1925 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1926 * needs to be protected from CPU hotplug handlers, which can
1927 * also perform a remote subbuffer switch.
1932 ret
= smp_call_function_single(buf
->backend
.cpu
,
1933 remote_switch
, ¶m
, 1);
1935 /* Remote CPU is offline, do it ourself. */
1936 lib_ring_buffer_switch_slow(buf
, mode
);
1941 /* Switch sub-buffer if current sub-buffer is non-empty. */
1942 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1944 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1946 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1948 /* Switch sub-buffer even if current sub-buffer is empty. */
1949 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1951 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1953 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1955 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
)
1957 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1958 struct channel
*chan
= bufb
->chan
;
1960 lib_ring_buffer_switch_remote(buf
);
1961 lib_ring_buffer_clear_reader(buf
, chan
);
1963 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
1968 * -ENOSPC if event size is too large for packet.
1969 * -ENOBUFS if there is currently not enough space in buffer for the event.
1970 * -EIO if data cannot be written into the buffer for any other reason.
1973 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1974 struct channel
*chan
,
1975 struct switch_offsets
*offsets
,
1976 struct lib_ring_buffer_ctx
*ctx
,
1979 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1980 unsigned long reserve_commit_diff
, offset_cmp
;
1983 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1984 offsets
->old
= offsets
->begin
;
1985 offsets
->switch_new_start
= 0;
1986 offsets
->switch_new_end
= 0;
1987 offsets
->switch_old_end
= 0;
1988 offsets
->pre_header_padding
= 0;
1990 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1991 if ((int64_t) ctx
->tsc
== -EIO
)
1994 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1995 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1997 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1998 offsets
->switch_new_start
= 1; /* For offsets->begin */
2000 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2002 &offsets
->pre_header_padding
,
2005 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2008 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2009 offsets
->size
> chan
->backend
.subbuf_size
)) {
2010 offsets
->switch_old_end
= 1; /* For offsets->old */
2011 offsets
->switch_new_start
= 1; /* For offsets->begin */
2014 if (unlikely(offsets
->switch_new_start
)) {
2015 unsigned long sb_index
, commit_count
;
2018 * We are typically not filling the previous buffer completely.
2020 if (likely(offsets
->switch_old_end
))
2021 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2022 offsets
->begin
= offsets
->begin
2023 + config
->cb
.subbuffer_header_size();
2024 /* Test new buffer integrity */
2025 sb_index
= subbuf_index(offsets
->begin
, chan
);
2027 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2028 * lib_ring_buffer_check_deliver() has the matching
2029 * memory barriers required around commit_cold cc_sb
2030 * updates to ensure reserve and commit counter updates
2031 * are not seen reordered when updated by another CPU.
2034 commit_count
= v_read(config
,
2035 &buf
->commit_cold
[sb_index
].cc_sb
);
2036 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2038 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2040 * The reserve counter have been concurrently updated
2041 * while we read the commit counter. This means the
2042 * commit counter we read might not match buf->offset
2043 * due to concurrent update. We therefore need to retry.
2047 reserve_commit_diff
=
2048 (buf_trunc(offsets
->begin
, chan
)
2049 >> chan
->backend
.num_subbuf_order
)
2050 - (commit_count
& chan
->commit_count_mask
);
2051 if (likely(reserve_commit_diff
== 0)) {
2052 /* Next subbuffer not being written to. */
2053 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2054 subbuf_trunc(offsets
->begin
, chan
)
2055 - subbuf_trunc((unsigned long)
2056 atomic_long_read(&buf
->consumed
), chan
)
2057 >= chan
->backend
.buf_size
)) {
2059 * We do not overwrite non consumed buffers
2060 * and we are full : record is lost.
2062 v_inc(config
, &buf
->records_lost_full
);
2066 * Next subbuffer not being written to, and we
2067 * are either in overwrite mode or the buffer is
2068 * not full. It's safe to write in this new
2074 * Next subbuffer reserve offset does not match the
2075 * commit offset, and this did not involve update to the
2076 * reserve counter. Drop record in producer-consumer and
2077 * overwrite mode. Caused by either a writer OOPS or
2078 * too many nested writes over a reserve/commit pair.
2080 v_inc(config
, &buf
->records_lost_wrap
);
2084 config
->cb
.record_header_size(config
, chan
,
2086 &offsets
->pre_header_padding
,
2089 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2092 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2093 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2095 * Record too big for subbuffers, report error, don't
2096 * complete the sub-buffer switch.
2098 v_inc(config
, &buf
->records_lost_big
);
2102 * We just made a successful buffer switch and the
2103 * record fits in the new subbuffer. Let's write.
2108 * Record fits in the current buffer and we are not on a switch
2109 * boundary. It's safe to write.
2112 offsets
->end
= offsets
->begin
+ offsets
->size
;
2114 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2116 * The offset_end will fall at the very beginning of the next
2119 offsets
->switch_new_end
= 1; /* For offsets->begin */
2124 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2126 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2128 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2129 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2131 return chan
->backend
.buf
;
2134 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2136 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2137 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2139 v_inc(config
, &buf
->records_lost_big
);
2141 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2144 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2145 * @ctx: ring buffer context.
2147 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2148 * -EIO for other errors, else returns 0.
2149 * It will take care of sub-buffer switching.
2151 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
,
2154 struct channel
*chan
= ctx
->chan
;
2155 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2156 struct lib_ring_buffer
*buf
;
2157 struct switch_offsets offsets
;
2160 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2164 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2168 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2173 * Atomically update last_tsc. This update races against concurrent
2174 * atomic updates, but the race will always cause supplementary full TSC
2175 * records, never the opposite (missing a full TSC record when it would
2178 save_last_tsc(config
, buf
, ctx
->tsc
);
2181 * Push the reader if necessary
2183 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2186 * Clear noref flag for this subbuffer.
2188 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2189 subbuf_index(offsets
.end
- 1, chan
));
2192 * Switch old subbuffer if needed.
2194 if (unlikely(offsets
.switch_old_end
)) {
2195 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2196 subbuf_index(offsets
.old
- 1, chan
));
2197 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2201 * Populate new subbuffer.
2203 if (unlikely(offsets
.switch_new_start
))
2204 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2206 if (unlikely(offsets
.switch_new_end
))
2207 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2209 ctx
->slot_size
= offsets
.size
;
2210 ctx
->pre_offset
= offsets
.begin
;
2211 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2214 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2217 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2218 struct lib_ring_buffer
*buf
,
2219 unsigned long commit_count
,
2222 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2223 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2227 * The ring buffer can count events recorded and overwritten per buffer,
2228 * but it is disabled by default due to its performance overhead.
2230 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2232 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2233 struct lib_ring_buffer
*buf
,
2236 v_add(config
, subbuffer_get_records_count(config
,
2237 &buf
->backend
, idx
),
2238 &buf
->records_count
);
2239 v_add(config
, subbuffer_count_records_overrun(config
,
2240 &buf
->backend
, idx
),
2241 &buf
->records_overrun
);
2243 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2245 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2246 struct lib_ring_buffer
*buf
,
2250 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2253 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2254 struct lib_ring_buffer
*buf
,
2255 struct channel
*chan
,
2256 unsigned long offset
,
2257 unsigned long commit_count
,
2261 unsigned long old_commit_count
= commit_count
2262 - chan
->backend
.subbuf_size
;
2265 * If we succeeded at updating cc_sb below, we are the subbuffer
2266 * writer delivering the subbuffer. Deals with concurrent
2267 * updates of the "cc" value without adding a add_return atomic
2268 * operation to the fast path.
2270 * We are doing the delivery in two steps:
2271 * - First, we cmpxchg() cc_sb to the new value
2272 * old_commit_count + 1. This ensures that we are the only
2273 * subbuffer user successfully filling the subbuffer, but we
2274 * do _not_ set the cc_sb value to "commit_count" yet.
2275 * Therefore, other writers that would wrap around the ring
2276 * buffer and try to start writing to our subbuffer would
2277 * have to drop records, because it would appear as
2279 * We therefore have exclusive access to the subbuffer control
2280 * structures. This mutual exclusion with other writers is
2281 * crucially important to perform record overruns count in
2282 * flight recorder mode locklessly.
2283 * - When we are ready to release the subbuffer (either for
2284 * reading or for overrun by other writers), we simply set the
2285 * cc_sb value to "commit_count" and perform delivery.
2287 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2288 * This guarantees that old_commit_count + 1 != commit_count.
2292 * Order prior updates to reserve count prior to the
2293 * commit_cold cc_sb update.
2296 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2297 old_commit_count
, old_commit_count
+ 1)
2298 == old_commit_count
)) {
2302 * Start of exclusive subbuffer access. We are
2303 * guaranteed to be the last writer in this subbuffer
2304 * and any other writer trying to access this subbuffer
2305 * in this state is required to drop records.
2307 * We can read the ts_end for the current sub-buffer
2308 * which has been saved by the very last space
2309 * reservation for the current sub-buffer.
2311 * Order increment of commit counter before reading ts_end.
2314 ts_end
= &buf
->ts_end
[idx
];
2315 deliver_count_events(config
, buf
, idx
);
2316 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2317 lib_ring_buffer_get_data_size(config
,
2322 * Increment the packet counter while we have exclusive
2325 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2328 * Set noref flag and offset for this subbuffer id.
2329 * Contains a memory barrier that ensures counter stores
2330 * are ordered before set noref and offset.
2332 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2333 buf_trunc_val(offset
, chan
));
2336 * Order set_noref and record counter updates before the
2337 * end of subbuffer exclusive access. Orders with
2338 * respect to writers coming into the subbuffer after
2339 * wrap around, and also order wrt concurrent readers.
2342 /* End of exclusive subbuffer access */
2343 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2346 * Order later updates to reserve count after
2347 * the commit_cold cc_sb update.
2350 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2354 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2356 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2357 && atomic_long_read(&buf
->active_readers
)
2358 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2359 wake_up_interruptible(&buf
->read_wait
);
2360 wake_up_interruptible(&chan
->read_wait
);
2365 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2367 int __init
init_lib_ring_buffer_frontend(void)
2371 for_each_possible_cpu(cpu
)
2372 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2376 module_init(init_lib_ring_buffer_frontend
);
2378 void __exit
exit_lib_ring_buffer_frontend(void)
2382 module_exit(exit_lib_ring_buffer_frontend
);