1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
3 * ring_buffer_frontend.c
5 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
8 * recorder (overwrite) modes. See thesis:
10 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
11 * dissertation, Ecole Polytechnique de Montreal.
12 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
14 * - Algorithm presentation in Chapter 5:
15 * "Lockless Multi-Core High-Throughput Buffering".
16 * - Algorithm formal verification in Section 8.6:
17 * "Formal verification of LTTng"
20 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
22 * Inspired from LTT and RelayFS:
23 * Karim Yaghmour <karim@opersys.com>
24 * Tom Zanussi <zanussi@us.ibm.com>
25 * Bob Wisniewski <bob@watson.ibm.com>
27 * Bob Wisniewski <bob@watson.ibm.com>
29 * Buffer reader semantic :
32 * while buffer is not finalized and empty
34 * - if return value != 0, continue
35 * - splice one subbuffer worth of data to a pipe
36 * - splice the data from pipe to disk/network
40 #include <linux/delay.h>
41 #include <linux/module.h>
42 #include <linux/percpu.h>
43 #include <linux/kref.h>
44 #include <linux/percpu-defs.h>
45 #include <linux/timer.h>
46 #include <asm/cacheflush.h>
48 #include <wrapper/ringbuffer/config.h>
49 #include <wrapper/ringbuffer/backend.h>
50 #include <wrapper/ringbuffer/frontend.h>
51 #include <wrapper/ringbuffer/iterator.h>
52 #include <wrapper/ringbuffer/nohz.h>
55 * Internal structure representing offsets to use at a sub-buffer switch.
57 struct switch_offsets
{
58 unsigned long begin
, end
, old
;
59 size_t pre_header_padding
, size
;
60 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
71 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
72 #endif /* CONFIG_NO_HZ */
74 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
76 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
77 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
80 void lib_ring_buffer_print_errors(struct channel
*chan
,
81 struct lib_ring_buffer
*buf
, int cpu
);
83 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
84 enum switch_mode mode
);
87 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
88 struct lib_ring_buffer
*buf
,
91 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
93 consumed_old
= atomic_long_read(&buf
->consumed
);
94 consumed_idx
= subbuf_index(consumed_old
, chan
);
95 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
97 * No memory barrier here, since we are only interested
98 * in a statistically correct polling result. The next poll will
99 * get the data is we are racing. The mb() that ensures correct
100 * memory order is in get_subbuf.
102 write_offset
= v_read(config
, &buf
->offset
);
105 * Check that the subbuffer we are trying to consume has been
106 * already fully committed.
109 if (((commit_count
- chan
->backend
.subbuf_size
)
110 & chan
->commit_count_mask
)
111 - (buf_trunc(consumed_old
, chan
)
112 >> chan
->backend
.num_subbuf_order
)
117 * Check that we are not about to read the same subbuffer in
118 * which the writer head is.
120 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
128 * Must be called under cpu hotplug protection.
130 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
132 struct channel
*chan
= buf
->backend
.chan
;
134 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
135 kvfree(buf
->commit_hot
);
136 kvfree(buf
->commit_cold
);
139 lib_ring_buffer_backend_free(&buf
->backend
);
143 * lib_ring_buffer_reset - Reset ring buffer to initial values.
146 * Effectively empty the ring buffer. Should be called when the buffer is not
147 * used for writing. The ring buffer can be opened for reading, but the reader
148 * should not be using the iterator concurrently with reset. The previous
149 * current iterator record is reset.
151 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
153 struct channel
*chan
= buf
->backend
.chan
;
154 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
158 * Reset iterator first. It will put the subbuffer if it currently holds
161 lib_ring_buffer_iterator_reset(buf
);
162 v_set(config
, &buf
->offset
, 0);
163 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
164 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
165 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
166 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
169 atomic_long_set(&buf
->consumed
, 0);
170 atomic_set(&buf
->record_disabled
, 0);
171 v_set(config
, &buf
->last_tsc
, 0);
172 lib_ring_buffer_backend_reset(&buf
->backend
);
173 /* Don't reset number of active readers */
174 v_set(config
, &buf
->records_lost_full
, 0);
175 v_set(config
, &buf
->records_lost_wrap
, 0);
176 v_set(config
, &buf
->records_lost_big
, 0);
177 v_set(config
, &buf
->records_count
, 0);
178 v_set(config
, &buf
->records_overrun
, 0);
181 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
184 * channel_reset - Reset channel to initial values.
187 * Effectively empty the channel. Should be called when the channel is not used
188 * for writing. The channel can be opened for reading, but the reader should not
189 * be using the iterator concurrently with reset. The previous current iterator
192 void channel_reset(struct channel
*chan
)
195 * Reset iterators first. Will put the subbuffer if held for reading.
197 channel_iterator_reset(chan
);
198 atomic_set(&chan
->record_disabled
, 0);
199 /* Don't reset commit_count_mask, still valid */
200 channel_backend_reset(&chan
->backend
);
201 /* Don't reset switch/read timer interval */
202 /* Don't reset notifiers and notifier enable bits */
203 /* Don't reset reader reference count */
205 EXPORT_SYMBOL_GPL(channel_reset
);
208 * Must be called under cpu hotplug protection.
210 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
211 struct channel_backend
*chanb
, int cpu
)
213 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
214 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
215 void *priv
= chanb
->priv
;
216 size_t subbuf_header_size
;
220 /* Test for cpu hotplug */
221 if (buf
->backend
.allocated
)
225 * Paranoia: per cpu dynamic allocation is not officially documented as
226 * zeroing the memory, so let's do it here too, just in case.
228 memset(buf
, 0, sizeof(*buf
));
230 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
235 kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
236 * chan
->backend
.num_subbuf
,
237 1 << INTERNODE_CACHE_SHIFT
),
238 GFP_KERNEL
| __GFP_NOWARN
,
239 cpu_to_node(max(cpu
, 0)));
240 if (!buf
->commit_hot
) {
246 kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
247 * chan
->backend
.num_subbuf
,
248 1 << INTERNODE_CACHE_SHIFT
),
249 GFP_KERNEL
| __GFP_NOWARN
,
250 cpu_to_node(max(cpu
, 0)));
251 if (!buf
->commit_cold
) {
257 kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
258 * chan
->backend
.num_subbuf
,
259 1 << INTERNODE_CACHE_SHIFT
),
260 GFP_KERNEL
| __GFP_NOWARN
,
261 cpu_to_node(max(cpu
, 0)));
264 goto free_commit_cold
;
267 init_waitqueue_head(&buf
->read_wait
);
268 init_waitqueue_head(&buf
->write_wait
);
269 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
272 * Write the subbuffer header for first subbuffer so we know the total
273 * duration of data gathering.
275 subbuf_header_size
= config
->cb
.subbuffer_header_size();
276 v_set(config
, &buf
->offset
, subbuf_header_size
);
277 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
278 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
279 config
->cb
.buffer_begin(buf
, tsc
, 0);
280 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
282 if (config
->cb
.buffer_create
) {
283 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
289 * Ensure the buffer is ready before setting it to allocated and setting
291 * Used for cpu hotplug vs cpumask iteration.
294 buf
->backend
.allocated
= 1;
296 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
297 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
298 chan
->backend
.cpumask
));
299 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
307 kvfree(buf
->commit_cold
);
309 kvfree(buf
->commit_hot
);
311 lib_ring_buffer_backend_free(&buf
->backend
);
315 static void switch_buffer_timer(struct timer_list
*t
)
317 struct lib_ring_buffer
*buf
= from_timer(buf
, t
, switch_timer
);
318 struct channel
*chan
= buf
->backend
.chan
;
321 * Only flush buffers periodically if readers are active.
323 if (atomic_long_read(&buf
->active_readers
))
324 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
326 mod_timer(&buf
->switch_timer
,
327 jiffies
+ chan
->switch_timer_interval
);
331 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
333 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
335 struct channel
*chan
= buf
->backend
.chan
;
336 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
337 unsigned int flags
= 0;
339 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
342 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
343 flags
= TIMER_PINNED
;
345 timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
);
346 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
348 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
349 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
351 add_timer(&buf
->switch_timer
);
353 buf
->switch_timer_enabled
= 1;
357 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
359 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
361 struct channel
*chan
= buf
->backend
.chan
;
363 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
366 del_timer_sync(&buf
->switch_timer
);
367 buf
->switch_timer_enabled
= 0;
371 * Polling timer to check the channels for data.
373 static void read_buffer_timer(struct timer_list
*t
)
375 struct lib_ring_buffer
*buf
= from_timer(buf
, t
, read_timer
);
376 struct channel
*chan
= buf
->backend
.chan
;
377 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
379 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
381 if (atomic_long_read(&buf
->active_readers
)
382 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
383 wake_up_interruptible(&buf
->read_wait
);
384 wake_up_interruptible(&chan
->read_wait
);
387 mod_timer(&buf
->read_timer
,
388 jiffies
+ chan
->read_timer_interval
);
392 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
394 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
396 struct channel
*chan
= buf
->backend
.chan
;
397 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
398 unsigned int flags
= 0;
400 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
401 || !chan
->read_timer_interval
402 || buf
->read_timer_enabled
)
405 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
406 flags
= TIMER_PINNED
;
408 timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
);
409 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
411 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
412 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
414 add_timer(&buf
->read_timer
);
416 buf
->read_timer_enabled
= 1;
420 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
422 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
424 struct channel
*chan
= buf
->backend
.chan
;
425 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
427 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
428 || !chan
->read_timer_interval
429 || !buf
->read_timer_enabled
)
432 del_timer_sync(&buf
->read_timer
);
434 * do one more check to catch data that has been written in the last
437 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
438 wake_up_interruptible(&buf
->read_wait
);
439 wake_up_interruptible(&chan
->read_wait
);
441 buf
->read_timer_enabled
= 0;
444 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
446 enum cpuhp_state lttng_rb_hp_prepare
;
447 enum cpuhp_state lttng_rb_hp_online
;
449 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
451 lttng_rb_hp_prepare
= val
;
453 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
455 void lttng_rb_set_hp_online(enum cpuhp_state val
)
457 lttng_rb_hp_online
= val
;
459 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
461 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
462 struct lttng_cpuhp_node
*node
)
464 struct channel
*chan
= container_of(node
, struct channel
,
466 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
467 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
469 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
472 * Performing a buffer switch on a remote CPU. Performed by
473 * the CPU responsible for doing the hotunplug after the target
474 * CPU stopped running completely. Ensures that all data
475 * from that remote CPU is flushed.
477 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
480 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
482 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
483 struct lttng_cpuhp_node
*node
)
485 struct channel
*chan
= container_of(node
, struct channel
,
487 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
488 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
490 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
492 wake_up_interruptible(&chan
->hp_wait
);
493 lib_ring_buffer_start_switch_timer(buf
);
494 lib_ring_buffer_start_read_timer(buf
);
497 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
499 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
500 struct lttng_cpuhp_node
*node
)
502 struct channel
*chan
= container_of(node
, struct channel
,
504 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
505 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
507 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
509 lib_ring_buffer_stop_switch_timer(buf
);
510 lib_ring_buffer_stop_read_timer(buf
);
513 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
515 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
517 #ifdef CONFIG_HOTPLUG_CPU
520 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
521 * @nb: notifier block
522 * @action: hotplug action to take
525 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
528 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
529 unsigned long action
,
532 unsigned int cpu
= (unsigned long)hcpu
;
533 struct channel
*chan
= container_of(nb
, struct channel
,
535 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
536 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
538 if (!chan
->cpu_hp_enable
)
541 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
544 case CPU_DOWN_FAILED
:
545 case CPU_DOWN_FAILED_FROZEN
:
547 case CPU_ONLINE_FROZEN
:
548 wake_up_interruptible(&chan
->hp_wait
);
549 lib_ring_buffer_start_switch_timer(buf
);
550 lib_ring_buffer_start_read_timer(buf
);
553 case CPU_DOWN_PREPARE
:
554 case CPU_DOWN_PREPARE_FROZEN
:
555 lib_ring_buffer_stop_switch_timer(buf
);
556 lib_ring_buffer_stop_read_timer(buf
);
560 case CPU_DEAD_FROZEN
:
562 * Performing a buffer switch on a remote CPU. Performed by
563 * the CPU responsible for doing the hotunplug after the target
564 * CPU stopped running completely. Ensures that all data
565 * from that remote CPU is flushed.
567 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
577 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
579 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
581 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
582 * that wake-up-tracing generated events are flushed before going idle (in
583 * tick_nohz). We test if the spinlock is locked to deal with the race where
584 * readers try to sample the ring buffer before we perform the switch. We let
585 * the readers retry in that case. If there is data in the buffer, the wake up
586 * is going to forbid the CPU running the reader thread from going idle.
588 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
592 struct channel
*chan
= container_of(nb
, struct channel
,
594 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
595 struct lib_ring_buffer
*buf
;
596 int cpu
= smp_processor_id();
598 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
600 * We don't support keeping the system idle with global buffers
601 * and streaming active. In order to do so, we would need to
602 * sample a non-nohz-cpumask racelessly with the nohz updates
603 * without adding synchronization overhead to nohz. Leave this
604 * use-case out for now.
609 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
611 case TICK_NOHZ_FLUSH
:
612 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
613 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
614 && chan
->read_timer_interval
615 && atomic_long_read(&buf
->active_readers
)
616 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
617 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
618 wake_up_interruptible(&buf
->read_wait
);
619 wake_up_interruptible(&chan
->read_wait
);
621 if (chan
->switch_timer_interval
)
622 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
623 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
626 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
627 lib_ring_buffer_stop_switch_timer(buf
);
628 lib_ring_buffer_stop_read_timer(buf
);
629 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
631 case TICK_NOHZ_RESTART
:
632 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
633 lib_ring_buffer_start_read_timer(buf
);
634 lib_ring_buffer_start_switch_timer(buf
);
635 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
642 void notrace
lib_ring_buffer_tick_nohz_flush(void)
644 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
648 void notrace
lib_ring_buffer_tick_nohz_stop(void)
650 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
654 void notrace
lib_ring_buffer_tick_nohz_restart(void)
656 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
659 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
664 static void channel_unregister_notifiers(struct channel
*chan
)
666 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
668 channel_iterator_unregister_notifiers(chan
);
669 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
672 * Remove the nohz notifier first, so we are certain we stop
675 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
676 &chan
->tick_nohz_notifier
);
678 * ring_buffer_nohz_lock will not be needed below, because
679 * we just removed the notifiers, which were the only source of
682 #endif /* CONFIG_NO_HZ */
683 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
687 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
688 &chan
->cpuhp_online
.node
);
690 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
691 &chan
->cpuhp_prepare
.node
);
694 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
698 #ifdef CONFIG_HOTPLUG_CPU
700 chan
->cpu_hp_enable
= 0;
701 for_each_online_cpu(cpu
) {
702 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
704 lib_ring_buffer_stop_switch_timer(buf
);
705 lib_ring_buffer_stop_read_timer(buf
);
708 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
710 for_each_possible_cpu(cpu
) {
711 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
713 lib_ring_buffer_stop_switch_timer(buf
);
714 lib_ring_buffer_stop_read_timer(buf
);
718 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
720 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
722 lib_ring_buffer_stop_switch_timer(buf
);
723 lib_ring_buffer_stop_read_timer(buf
);
725 channel_backend_unregister_notifiers(&chan
->backend
);
728 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
730 if (!buf
->quiescent
) {
731 buf
->quiescent
= true;
732 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
736 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
738 buf
->quiescent
= false;
741 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
744 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
746 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
748 for_each_channel_cpu(cpu
, chan
) {
749 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
752 lib_ring_buffer_set_quiescent(buf
);
756 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
758 lib_ring_buffer_set_quiescent(buf
);
761 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
763 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
766 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
768 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
770 for_each_channel_cpu(cpu
, chan
) {
771 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
774 lib_ring_buffer_clear_quiescent(buf
);
778 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
780 lib_ring_buffer_clear_quiescent(buf
);
783 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
785 static void channel_free(struct channel
*chan
)
787 if (chan
->backend
.release_priv_ops
) {
788 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
790 channel_iterator_free(chan
);
791 channel_backend_free(&chan
->backend
);
796 * channel_create - Create channel.
797 * @config: ring buffer instance configuration
798 * @name: name of the channel
799 * @priv: ring buffer client private data
800 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
801 * address mapping. It is used only by RING_BUFFER_STATIC
802 * configuration. It can be set to NULL for other backends.
803 * @subbuf_size: subbuffer size
804 * @num_subbuf: number of subbuffers
805 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
806 * padding to let readers get those sub-buffers.
807 * Used for live streaming.
808 * @read_timer_interval: Time interval (in us) to wake up pending readers.
811 * Returns NULL on failure.
813 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
814 const char *name
, void *priv
, void *buf_addr
,
816 size_t num_subbuf
, unsigned int switch_timer_interval
,
817 unsigned int read_timer_interval
)
820 struct channel
*chan
;
822 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
823 read_timer_interval
))
826 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
830 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
831 subbuf_size
, num_subbuf
);
835 ret
= channel_iterator_init(chan
);
837 goto error_free_backend
;
839 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
840 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
841 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
842 kref_init(&chan
->ref
);
843 init_waitqueue_head(&chan
->read_wait
);
844 init_waitqueue_head(&chan
->hp_wait
);
846 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
847 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
848 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
849 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
850 &chan
->cpuhp_prepare
.node
);
852 goto cpuhp_prepare_error
;
854 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
855 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
856 &chan
->cpuhp_online
.node
);
858 goto cpuhp_online_error
;
859 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
863 * In case of non-hotplug cpu, if the ring-buffer is allocated
864 * in early initcall, it will not be notified of secondary cpus.
865 * In that off case, we need to allocate for all possible cpus.
867 #ifdef CONFIG_HOTPLUG_CPU
868 chan
->cpu_hp_notifier
.notifier_call
=
869 lib_ring_buffer_cpu_hp_callback
;
870 chan
->cpu_hp_notifier
.priority
= 6;
871 register_cpu_notifier(&chan
->cpu_hp_notifier
);
874 for_each_online_cpu(cpu
) {
875 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
877 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
878 lib_ring_buffer_start_switch_timer(buf
);
879 lib_ring_buffer_start_read_timer(buf
);
880 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
882 chan
->cpu_hp_enable
= 1;
885 for_each_possible_cpu(cpu
) {
886 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
888 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
889 lib_ring_buffer_start_switch_timer(buf
);
890 lib_ring_buffer_start_read_timer(buf
);
891 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
895 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
897 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
898 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
899 chan
->tick_nohz_notifier
.notifier_call
=
900 ring_buffer_tick_nohz_callback
;
901 chan
->tick_nohz_notifier
.priority
= ~0U;
902 atomic_notifier_chain_register(&tick_nohz_notifier
,
903 &chan
->tick_nohz_notifier
);
904 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
907 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
909 lib_ring_buffer_start_switch_timer(buf
);
910 lib_ring_buffer_start_read_timer(buf
);
915 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
917 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
918 &chan
->cpuhp_prepare
.node
);
921 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
923 channel_backend_free(&chan
->backend
);
928 EXPORT_SYMBOL_GPL(channel_create
);
931 void channel_release(struct kref
*kref
)
933 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
938 * channel_destroy - Finalize, wait for q.s. and destroy channel.
939 * @chan: channel to destroy
942 * Call "destroy" callback, finalize channels, and then decrement the
943 * channel reference count. Note that when readers have completed data
944 * consumption of finalized channels, get_subbuf() will return -ENODATA.
945 * They should release their handle at that point. Returns the private
948 void *channel_destroy(struct channel
*chan
)
951 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
954 channel_unregister_notifiers(chan
);
956 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
958 * No need to hold cpu hotplug, because all notifiers have been
961 for_each_channel_cpu(cpu
, chan
) {
962 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
965 if (config
->cb
.buffer_finalize
)
966 config
->cb
.buffer_finalize(buf
,
970 * Perform flush before writing to finalized.
973 WRITE_ONCE(buf
->finalized
, 1);
974 wake_up_interruptible(&buf
->read_wait
);
977 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
979 if (config
->cb
.buffer_finalize
)
980 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
982 * Perform flush before writing to finalized.
985 WRITE_ONCE(buf
->finalized
, 1);
986 wake_up_interruptible(&buf
->read_wait
);
988 WRITE_ONCE(chan
->finalized
, 1);
989 wake_up_interruptible(&chan
->hp_wait
);
990 wake_up_interruptible(&chan
->read_wait
);
991 priv
= chan
->backend
.priv
;
992 kref_put(&chan
->ref
, channel_release
);
995 EXPORT_SYMBOL_GPL(channel_destroy
);
997 struct lib_ring_buffer
*channel_get_ring_buffer(
998 const struct lib_ring_buffer_config
*config
,
999 struct channel
*chan
, int cpu
)
1001 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1002 return chan
->backend
.buf
;
1004 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1006 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1008 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1010 struct channel
*chan
= buf
->backend
.chan
;
1012 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1014 kref_get(&chan
->ref
);
1015 smp_mb__after_atomic();
1018 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1020 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1022 struct channel
*chan
= buf
->backend
.chan
;
1024 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1025 smp_mb__before_atomic();
1026 atomic_long_dec(&buf
->active_readers
);
1027 kref_put(&chan
->ref
, channel_release
);
1029 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1032 * Promote compiler barrier to a smp_mb().
1033 * For the specific ring buffer case, this IPI call should be removed if the
1034 * architecture does not reorder writes. This should eventually be provided by
1035 * a separate architecture-specific infrastructure.
1037 static void remote_mb(void *info
)
1043 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1045 * @consumed: consumed count indicating the position where to read
1046 * @produced: produced count, indicates position when to stop reading
1048 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1049 * data to read at consumed position, or 0 if the get operation succeeds.
1050 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1053 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1054 unsigned long *consumed
, unsigned long *produced
)
1056 struct channel
*chan
= buf
->backend
.chan
;
1057 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1058 unsigned long consumed_cur
, write_offset
;
1062 finalized
= READ_ONCE(buf
->finalized
);
1064 * Read finalized before counters.
1067 consumed_cur
= atomic_long_read(&buf
->consumed
);
1069 * No need to issue a memory barrier between consumed count read and
1070 * write offset read, because consumed count can only change
1071 * concurrently in overwrite mode, and we keep a sequence counter
1072 * identifier derived from the write offset to check we are getting
1073 * the same sub-buffer we are expecting (the sub-buffers are atomically
1074 * "tagged" upon writes, tags are checked upon read).
1076 write_offset
= v_read(config
, &buf
->offset
);
1079 * Check that we are not about to read the same subbuffer in
1080 * which the writer head is.
1082 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1086 *consumed
= consumed_cur
;
1087 *produced
= subbuf_trunc(write_offset
, chan
);
1093 * The memory barriers __wait_event()/wake_up_interruptible() take care
1094 * of "raw_spin_is_locked" memory ordering.
1098 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1103 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1106 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1107 * are saved regardless of whether the consumed and produced positions are
1108 * in the same subbuffer.
1110 * @consumed: consumed byte count indicating the last position read
1111 * @produced: produced byte count indicating the last position written
1113 * This function is meant to provide information on the exact producer and
1114 * consumer positions without regard for the "snapshot" feature.
1116 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1117 unsigned long *consumed
, unsigned long *produced
)
1119 struct channel
*chan
= buf
->backend
.chan
;
1120 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1123 *consumed
= atomic_long_read(&buf
->consumed
);
1125 * No need to issue a memory barrier between consumed count read and
1126 * write offset read, because consumed count can only change
1127 * concurrently in overwrite mode, and we keep a sequence counter
1128 * identifier derived from the write offset to check we are getting
1129 * the same sub-buffer we are expecting (the sub-buffers are atomically
1130 * "tagged" upon writes, tags are checked upon read).
1132 *produced
= v_read(config
, &buf
->offset
);
1137 * lib_ring_buffer_put_snapshot - move consumed counter forward
1139 * Should only be called from consumer context.
1141 * @consumed_new: new consumed count value
1143 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1144 unsigned long consumed_new
)
1146 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1147 struct channel
*chan
= bufb
->chan
;
1148 unsigned long consumed
;
1150 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1153 * Only push the consumed value forward.
1154 * If the consumed cmpxchg fails, this is because we have been pushed by
1155 * the writer in flight recorder mode.
1157 consumed
= atomic_long_read(&buf
->consumed
);
1158 while ((long) consumed
- (long) consumed_new
< 0)
1159 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1161 /* Wake-up the metadata producer */
1162 wake_up_interruptible(&buf
->write_wait
);
1164 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1166 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1167 static void lib_ring_buffer_flush_read_subbuf_dcache(
1168 const struct lib_ring_buffer_config
*config
,
1169 struct channel
*chan
,
1170 struct lib_ring_buffer
*buf
)
1172 struct lib_ring_buffer_backend_pages
*pages
;
1173 unsigned long sb_bindex
, id
, i
, nr_pages
;
1175 if (config
->output
!= RING_BUFFER_MMAP
)
1179 * Architectures with caches aliased on virtual addresses may
1180 * use different cache lines for the linear mapping vs
1181 * user-space memory mapping. Given that the ring buffer is
1182 * based on the kernel linear mapping, aligning it with the
1183 * user-space mapping is not straightforward, and would require
1184 * extra TLB entries. Therefore, simply flush the dcache for the
1185 * entire sub-buffer before reading it.
1187 id
= buf
->backend
.buf_rsb
.id
;
1188 sb_bindex
= subbuffer_id_get_index(config
, id
);
1189 pages
= buf
->backend
.array
[sb_bindex
];
1190 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1191 for (i
= 0; i
< nr_pages
; i
++) {
1192 struct lib_ring_buffer_backend_page
*backend_page
;
1194 backend_page
= &pages
->p
[i
];
1195 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1199 static void lib_ring_buffer_flush_read_subbuf_dcache(
1200 const struct lib_ring_buffer_config
*config
,
1201 struct channel
*chan
,
1202 struct lib_ring_buffer
*buf
)
1208 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1210 * @consumed: consumed count indicating the position where to read
1212 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1213 * data to read at consumed position, or 0 if the get operation succeeds.
1214 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1216 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1217 unsigned long consumed
)
1219 struct channel
*chan
= buf
->backend
.chan
;
1220 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1221 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1225 if (buf
->get_subbuf
) {
1227 * Reader is trying to get a subbuffer twice.
1229 CHAN_WARN_ON(chan
, 1);
1233 finalized
= READ_ONCE(buf
->finalized
);
1235 * Read finalized before counters.
1238 consumed_cur
= atomic_long_read(&buf
->consumed
);
1239 consumed_idx
= subbuf_index(consumed
, chan
);
1240 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1242 * Make sure we read the commit count before reading the buffer
1243 * data and the write offset. Correct consumed offset ordering
1244 * wrt commit count is insured by the use of cmpxchg to update
1245 * the consumed offset.
1246 * smp_call_function_single can fail if the remote CPU is offline,
1247 * this is OK because then there is no wmb to execute there.
1248 * If our thread is executing on the same CPU as the on the buffers
1249 * belongs to, we don't have to synchronize it at all. If we are
1250 * migrated, the scheduler will take care of the memory barriers.
1251 * Normally, smp_call_function_single() should ensure program order when
1252 * executing the remote function, which implies that it surrounds the
1253 * function execution with :
1264 * However, smp_call_function_single() does not seem to clearly execute
1265 * such barriers. It depends on spinlock semantic to provide the barrier
1266 * before executing the IPI and, when busy-looping, csd_lock_wait only
1267 * executes smp_mb() when it has to wait for the other CPU.
1269 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1270 * required ourself, even if duplicated. It has no performance impact
1273 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1274 * read and write vs write. They do not ensure core synchronization. We
1275 * really have to ensure total order between the 3 barriers running on
1278 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1279 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1280 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1281 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1282 /* Total order with IPI handler smp_mb() */
1284 smp_call_function_single(buf
->backend
.cpu
,
1285 remote_mb
, NULL
, 1);
1286 /* Total order with IPI handler smp_mb() */
1290 /* Total order with IPI handler smp_mb() */
1292 smp_call_function(remote_mb
, NULL
, 1);
1293 /* Total order with IPI handler smp_mb() */
1298 * Local rmb to match the remote wmb to read the commit count
1299 * before the buffer data and the write offset.
1304 write_offset
= v_read(config
, &buf
->offset
);
1307 * Check that the buffer we are getting is after or at consumed_cur
1310 if ((long) subbuf_trunc(consumed
, chan
)
1311 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1315 * Check that the subbuffer we are trying to consume has been
1316 * already fully committed.
1318 if (((commit_count
- chan
->backend
.subbuf_size
)
1319 & chan
->commit_count_mask
)
1320 - (buf_trunc(consumed
, chan
)
1321 >> chan
->backend
.num_subbuf_order
)
1326 * Check that we are not about to read the same subbuffer in
1327 * which the writer head is.
1329 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1334 * Failure to get the subbuffer causes a busy-loop retry without going
1335 * to a wait queue. These are caused by short-lived race windows where
1336 * the writer is getting access to a subbuffer we were trying to get
1337 * access to. Also checks that the "consumed" buffer count we are
1338 * looking for matches the one contained in the subbuffer id.
1340 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1341 consumed_idx
, buf_trunc_val(consumed
, chan
));
1344 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1346 buf
->get_subbuf_consumed
= consumed
;
1347 buf
->get_subbuf
= 1;
1349 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1355 * The memory barriers __wait_event()/wake_up_interruptible() take care
1356 * of "raw_spin_is_locked" memory ordering.
1360 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1365 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1368 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1371 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1373 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1374 struct channel
*chan
= bufb
->chan
;
1375 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1376 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1378 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1380 if (!buf
->get_subbuf
) {
1382 * Reader puts a subbuffer it did not get.
1384 CHAN_WARN_ON(chan
, 1);
1387 consumed
= buf
->get_subbuf_consumed
;
1388 buf
->get_subbuf
= 0;
1391 * Clear the records_unread counter. (overruns counter)
1392 * Can still be non-zero if a file reader simply grabbed the data
1393 * without using iterators.
1394 * Can be below zero if an iterator is used on a snapshot more than
1397 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1398 v_add(config
, v_read(config
,
1399 &bufb
->array
[read_sb_bindex
]->records_unread
),
1400 &bufb
->records_read
);
1401 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1402 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1403 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1404 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1407 * Exchange the reader subbuffer with the one we put in its place in the
1408 * writer subbuffer table. Expect the original consumed count. If
1409 * update_read_sb_index fails, this is because the writer updated the
1410 * subbuffer concurrently. We should therefore keep the subbuffer we
1411 * currently have: it has become invalid to try reading this sub-buffer
1412 * consumed count value anyway.
1414 consumed_idx
= subbuf_index(consumed
, chan
);
1415 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1416 consumed_idx
, buf_trunc_val(consumed
, chan
));
1418 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1419 * if the writer concurrently updated it.
1422 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1425 * cons_offset is an iterator on all subbuffer offsets between the reader
1426 * position and the writer position. (inclusive)
1429 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1430 struct channel
*chan
,
1431 unsigned long cons_offset
,
1434 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1435 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1437 cons_idx
= subbuf_index(cons_offset
, chan
);
1438 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1439 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1441 if (subbuf_offset(commit_count
, chan
) != 0)
1443 "ring buffer %s, cpu %d: "
1444 "commit count in subbuffer %lu,\n"
1445 "expecting multiples of %lu bytes\n"
1446 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1447 chan
->backend
.name
, cpu
, cons_idx
,
1448 chan
->backend
.subbuf_size
,
1449 commit_count
, commit_count_sb
);
1451 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1452 chan
->backend
.name
, cpu
, commit_count
);
1456 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1457 struct channel
*chan
,
1458 void *priv
, int cpu
)
1460 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1461 unsigned long write_offset
, cons_offset
;
1464 * No need to order commit_count, write_offset and cons_offset reads
1465 * because we execute at teardown when no more writer nor reader
1466 * references are left.
1468 write_offset
= v_read(config
, &buf
->offset
);
1469 cons_offset
= atomic_long_read(&buf
->consumed
);
1470 if (write_offset
!= cons_offset
)
1472 "ring buffer %s, cpu %d: "
1473 "non-consumed data\n"
1474 " [ %lu bytes written, %lu bytes read ]\n",
1475 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1477 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1478 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1481 cons_offset
= subbuf_align(cons_offset
, chan
))
1482 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1486 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1488 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1489 struct lib_ring_buffer
*buf
,
1492 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1494 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1495 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1496 "%lu records overrun\n",
1498 v_read(config
, &buf
->records_count
),
1499 v_read(config
, &buf
->records_overrun
));
1501 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1502 "%lu records overrun\n",
1503 chan
->backend
.name
, cpu
,
1504 v_read(config
, &buf
->records_count
),
1505 v_read(config
, &buf
->records_overrun
));
1510 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1511 struct lib_ring_buffer
*buf
,
1518 void lib_ring_buffer_print_errors(struct channel
*chan
,
1519 struct lib_ring_buffer
*buf
, int cpu
)
1521 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1522 void *priv
= chan
->backend
.priv
;
1524 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1525 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1526 if (v_read(config
, &buf
->records_lost_full
)
1527 || v_read(config
, &buf
->records_lost_wrap
)
1528 || v_read(config
, &buf
->records_lost_big
))
1530 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1531 " [ %lu buffer full, %lu nest buffer wrap-around, "
1532 "%lu event too big ]\n",
1533 chan
->backend
.name
, cpu
,
1534 v_read(config
, &buf
->records_lost_full
),
1535 v_read(config
, &buf
->records_lost_wrap
),
1536 v_read(config
, &buf
->records_lost_big
));
1538 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1542 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1544 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1547 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1548 struct channel
*chan
,
1549 struct switch_offsets
*offsets
,
1552 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1553 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1554 unsigned long commit_count
;
1555 struct commit_counters_hot
*cc_hot
;
1557 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1560 * Order all writes to buffer before the commit count update that will
1561 * determine that the subbuffer is full.
1563 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1565 * Must write slot data before incrementing commit count. This
1566 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1572 cc_hot
= &buf
->commit_hot
[oldidx
];
1573 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1574 commit_count
= v_read(config
, &cc_hot
->cc
);
1575 /* Check if the written buffer has to be delivered */
1576 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1577 commit_count
, oldidx
, tsc
);
1578 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1579 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1580 commit_count
, cc_hot
);
1584 * lib_ring_buffer_switch_old_end: switch old subbuffer
1586 * Note : offset_old should never be 0 here. It is ok, because we never perform
1587 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1588 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1592 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1593 struct channel
*chan
,
1594 struct switch_offsets
*offsets
,
1597 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1598 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1599 unsigned long commit_count
, padding_size
, data_size
;
1600 struct commit_counters_hot
*cc_hot
;
1603 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1604 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1605 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1607 ts_end
= &buf
->ts_end
[oldidx
];
1609 * This is the last space reservation in that sub-buffer before
1610 * it gets delivered. This provides exclusive access to write to
1611 * this sub-buffer's ts_end. There are also no concurrent
1612 * readers of that ts_end because delivery of that sub-buffer is
1613 * postponed until the commit counter is incremented for the
1614 * current space reservation.
1619 * Order all writes to buffer and store to ts_end before the commit
1620 * count update that will determine that the subbuffer is full.
1622 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1624 * Must write slot data before incrementing commit count. This
1625 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1631 cc_hot
= &buf
->commit_hot
[oldidx
];
1632 v_add(config
, padding_size
, &cc_hot
->cc
);
1633 commit_count
= v_read(config
, &cc_hot
->cc
);
1634 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1635 commit_count
, oldidx
, tsc
);
1636 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1637 offsets
->old
+ padding_size
, commit_count
,
1642 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1644 * This code can be executed unordered : writers may already have written to the
1645 * sub-buffer before this code gets executed, caution. The commit makes sure
1646 * that this code is executed before the deliver of this sub-buffer.
1649 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1650 struct channel
*chan
,
1651 struct switch_offsets
*offsets
,
1654 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1655 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1656 unsigned long commit_count
;
1657 struct commit_counters_hot
*cc_hot
;
1659 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1662 * Order all writes to buffer before the commit count update that will
1663 * determine that the subbuffer is full.
1665 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1667 * Must write slot data before incrementing commit count. This
1668 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1674 cc_hot
= &buf
->commit_hot
[beginidx
];
1675 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1676 commit_count
= v_read(config
, &cc_hot
->cc
);
1677 /* Check if the written buffer has to be delivered */
1678 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1679 commit_count
, beginidx
, tsc
);
1680 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1681 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1682 commit_count
, cc_hot
);
1686 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1688 * Calls subbuffer_set_data_size() to set the data size of the current
1689 * sub-buffer. We do not need to perform check_deliver nor commit here,
1690 * since this task will be done by the "commit" of the event for which
1691 * we are currently doing the space reservation.
1694 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1695 struct channel
*chan
,
1696 struct switch_offsets
*offsets
,
1699 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1700 unsigned long endidx
, data_size
;
1703 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1704 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1705 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1706 ts_end
= &buf
->ts_end
[endidx
];
1708 * This is the last space reservation in that sub-buffer before
1709 * it gets delivered. This provides exclusive access to write to
1710 * this sub-buffer's ts_end. There are also no concurrent
1711 * readers of that ts_end because delivery of that sub-buffer is
1712 * postponed until the commit counter is incremented for the
1713 * current space reservation.
1721 * !0 if execution must be aborted.
1724 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1725 struct lib_ring_buffer
*buf
,
1726 struct channel
*chan
,
1727 struct switch_offsets
*offsets
,
1730 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1731 unsigned long off
, reserve_commit_diff
;
1733 offsets
->begin
= v_read(config
, &buf
->offset
);
1734 offsets
->old
= offsets
->begin
;
1735 offsets
->switch_old_start
= 0;
1736 off
= subbuf_offset(offsets
->begin
, chan
);
1738 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1741 * Ensure we flush the header of an empty subbuffer when doing the
1742 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1743 * total data gathering duration even if there were no records saved
1744 * after the last buffer switch.
1745 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1746 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1747 * subbuffer header as appropriate.
1748 * The next record that reserves space will be responsible for
1749 * populating the following subbuffer header. We choose not to populate
1750 * the next subbuffer header here because we want to be able to use
1751 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1752 * buffer flush, which must guarantee that all the buffer content
1753 * (records and header timestamps) are visible to the reader. This is
1754 * required for quiescence guarantees for the fusion merge.
1756 if (mode
!= SWITCH_FLUSH
&& !off
)
1757 return -1; /* we do not have to switch : buffer is empty */
1759 if (unlikely(off
== 0)) {
1760 unsigned long sb_index
, commit_count
;
1763 * We are performing a SWITCH_FLUSH. At this stage, there are no
1764 * concurrent writes into the buffer.
1766 * The client does not save any header information. Don't
1767 * switch empty subbuffer on finalize, because it is invalid to
1768 * deliver a completely empty subbuffer.
1770 if (!config
->cb
.subbuffer_header_size())
1773 /* Test new buffer integrity */
1774 sb_index
= subbuf_index(offsets
->begin
, chan
);
1775 commit_count
= v_read(config
,
1776 &buf
->commit_cold
[sb_index
].cc_sb
);
1777 reserve_commit_diff
=
1778 (buf_trunc(offsets
->begin
, chan
)
1779 >> chan
->backend
.num_subbuf_order
)
1780 - (commit_count
& chan
->commit_count_mask
);
1781 if (likely(reserve_commit_diff
== 0)) {
1782 /* Next subbuffer not being written to. */
1783 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1784 subbuf_trunc(offsets
->begin
, chan
)
1785 - subbuf_trunc((unsigned long)
1786 atomic_long_read(&buf
->consumed
), chan
)
1787 >= chan
->backend
.buf_size
)) {
1789 * We do not overwrite non consumed buffers
1790 * and we are full : don't switch.
1795 * Next subbuffer not being written to, and we
1796 * are either in overwrite mode or the buffer is
1797 * not full. It's safe to write in this new
1803 * Next subbuffer reserve offset does not match the
1804 * commit offset. Don't perform switch in
1805 * producer-consumer and overwrite mode. Caused by
1806 * either a writer OOPS or too many nested writes over a
1807 * reserve/commit pair.
1813 * Need to write the subbuffer start header on finalize.
1815 offsets
->switch_old_start
= 1;
1817 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1818 /* Note: old points to the next subbuf at offset 0 */
1819 offsets
->end
= offsets
->begin
;
1824 * Force a sub-buffer switch. This operation is completely reentrant : can be
1825 * called while tracing is active with absolutely no lock held.
1827 * Note, however, that as a v_cmpxchg is used for some atomic
1828 * operations, this function must be called from the CPU which owns the buffer
1829 * for a ACTIVE flush.
1831 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1833 struct channel
*chan
= buf
->backend
.chan
;
1834 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1835 struct switch_offsets offsets
;
1836 unsigned long oldidx
;
1842 * Perform retryable operations.
1845 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1847 return; /* Switch not needed */
1848 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1852 * Atomically update last_tsc. This update races against concurrent
1853 * atomic updates, but the race will always cause supplementary full TSC
1854 * records, never the opposite (missing a full TSC record when it would
1857 save_last_tsc(config
, buf
, tsc
);
1860 * Push the reader if necessary
1862 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1864 oldidx
= subbuf_index(offsets
.old
, chan
);
1865 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1868 * May need to populate header start on SWITCH_FLUSH.
1870 if (offsets
.switch_old_start
) {
1871 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1872 offsets
.old
+= config
->cb
.subbuffer_header_size();
1876 * Switch old subbuffer.
1878 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1880 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1882 struct switch_param
{
1883 struct lib_ring_buffer
*buf
;
1884 enum switch_mode mode
;
1887 static void remote_switch(void *info
)
1889 struct switch_param
*param
= info
;
1890 struct lib_ring_buffer
*buf
= param
->buf
;
1892 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1895 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1896 enum switch_mode mode
)
1898 struct channel
*chan
= buf
->backend
.chan
;
1899 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1901 struct switch_param param
;
1904 * With global synchronization we don't need to use the IPI scheme.
1906 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1907 lib_ring_buffer_switch_slow(buf
, mode
);
1912 * Disabling preemption ensures two things: first, that the
1913 * target cpu is not taken concurrently offline while we are within
1914 * smp_call_function_single(). Secondly, if it happens that the
1915 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1916 * needs to be protected from CPU hotplug handlers, which can
1917 * also perform a remote subbuffer switch.
1922 ret
= smp_call_function_single(buf
->backend
.cpu
,
1923 remote_switch
, ¶m
, 1);
1925 /* Remote CPU is offline, do it ourself. */
1926 lib_ring_buffer_switch_slow(buf
, mode
);
1931 /* Switch sub-buffer if current sub-buffer is non-empty. */
1932 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1934 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1936 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1938 /* Switch sub-buffer even if current sub-buffer is empty. */
1939 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1941 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1943 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1945 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
)
1947 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1948 struct channel
*chan
= bufb
->chan
;
1950 lib_ring_buffer_switch_remote(buf
);
1951 lib_ring_buffer_clear_reader(buf
, chan
);
1953 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
1958 * -ENOSPC if event size is too large for packet.
1959 * -ENOBUFS if there is currently not enough space in buffer for the event.
1960 * -EIO if data cannot be written into the buffer for any other reason.
1963 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1964 struct channel
*chan
,
1965 struct switch_offsets
*offsets
,
1966 struct lib_ring_buffer_ctx
*ctx
,
1969 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1970 unsigned long reserve_commit_diff
, offset_cmp
;
1973 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1974 offsets
->old
= offsets
->begin
;
1975 offsets
->switch_new_start
= 0;
1976 offsets
->switch_new_end
= 0;
1977 offsets
->switch_old_end
= 0;
1978 offsets
->pre_header_padding
= 0;
1980 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1981 if ((int64_t) ctx
->tsc
== -EIO
)
1984 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1985 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1987 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1988 offsets
->switch_new_start
= 1; /* For offsets->begin */
1990 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1992 &offsets
->pre_header_padding
,
1995 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1998 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1999 offsets
->size
> chan
->backend
.subbuf_size
)) {
2000 offsets
->switch_old_end
= 1; /* For offsets->old */
2001 offsets
->switch_new_start
= 1; /* For offsets->begin */
2004 if (unlikely(offsets
->switch_new_start
)) {
2005 unsigned long sb_index
, commit_count
;
2008 * We are typically not filling the previous buffer completely.
2010 if (likely(offsets
->switch_old_end
))
2011 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2012 offsets
->begin
= offsets
->begin
2013 + config
->cb
.subbuffer_header_size();
2014 /* Test new buffer integrity */
2015 sb_index
= subbuf_index(offsets
->begin
, chan
);
2017 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2018 * lib_ring_buffer_check_deliver() has the matching
2019 * memory barriers required around commit_cold cc_sb
2020 * updates to ensure reserve and commit counter updates
2021 * are not seen reordered when updated by another CPU.
2024 commit_count
= v_read(config
,
2025 &buf
->commit_cold
[sb_index
].cc_sb
);
2026 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2028 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2030 * The reserve counter have been concurrently updated
2031 * while we read the commit counter. This means the
2032 * commit counter we read might not match buf->offset
2033 * due to concurrent update. We therefore need to retry.
2037 reserve_commit_diff
=
2038 (buf_trunc(offsets
->begin
, chan
)
2039 >> chan
->backend
.num_subbuf_order
)
2040 - (commit_count
& chan
->commit_count_mask
);
2041 if (likely(reserve_commit_diff
== 0)) {
2042 /* Next subbuffer not being written to. */
2043 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2044 subbuf_trunc(offsets
->begin
, chan
)
2045 - subbuf_trunc((unsigned long)
2046 atomic_long_read(&buf
->consumed
), chan
)
2047 >= chan
->backend
.buf_size
)) {
2049 * We do not overwrite non consumed buffers
2050 * and we are full : record is lost.
2052 v_inc(config
, &buf
->records_lost_full
);
2056 * Next subbuffer not being written to, and we
2057 * are either in overwrite mode or the buffer is
2058 * not full. It's safe to write in this new
2064 * Next subbuffer reserve offset does not match the
2065 * commit offset, and this did not involve update to the
2066 * reserve counter. Drop record in producer-consumer and
2067 * overwrite mode. Caused by either a writer OOPS or
2068 * too many nested writes over a reserve/commit pair.
2070 v_inc(config
, &buf
->records_lost_wrap
);
2074 config
->cb
.record_header_size(config
, chan
,
2076 &offsets
->pre_header_padding
,
2079 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2082 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2083 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2085 * Record too big for subbuffers, report error, don't
2086 * complete the sub-buffer switch.
2088 v_inc(config
, &buf
->records_lost_big
);
2092 * We just made a successful buffer switch and the
2093 * record fits in the new subbuffer. Let's write.
2098 * Record fits in the current buffer and we are not on a switch
2099 * boundary. It's safe to write.
2102 offsets
->end
= offsets
->begin
+ offsets
->size
;
2104 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2106 * The offset_end will fall at the very beginning of the next
2109 offsets
->switch_new_end
= 1; /* For offsets->begin */
2114 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2116 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2118 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2119 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2121 return chan
->backend
.buf
;
2124 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2126 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2127 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2129 v_inc(config
, &buf
->records_lost_big
);
2131 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2134 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2135 * @ctx: ring buffer context.
2137 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2138 * -EIO for other errors, else returns 0.
2139 * It will take care of sub-buffer switching.
2141 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
,
2144 struct channel
*chan
= ctx
->chan
;
2145 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2146 struct lib_ring_buffer
*buf
;
2147 struct switch_offsets offsets
;
2150 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2154 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2158 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2163 * Atomically update last_tsc. This update races against concurrent
2164 * atomic updates, but the race will always cause supplementary full TSC
2165 * records, never the opposite (missing a full TSC record when it would
2168 save_last_tsc(config
, buf
, ctx
->tsc
);
2171 * Push the reader if necessary
2173 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2176 * Clear noref flag for this subbuffer.
2178 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2179 subbuf_index(offsets
.end
- 1, chan
));
2182 * Switch old subbuffer if needed.
2184 if (unlikely(offsets
.switch_old_end
)) {
2185 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2186 subbuf_index(offsets
.old
- 1, chan
));
2187 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2191 * Populate new subbuffer.
2193 if (unlikely(offsets
.switch_new_start
))
2194 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2196 if (unlikely(offsets
.switch_new_end
))
2197 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2199 ctx
->slot_size
= offsets
.size
;
2200 ctx
->pre_offset
= offsets
.begin
;
2201 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2204 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2207 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2208 struct lib_ring_buffer
*buf
,
2209 unsigned long commit_count
,
2212 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2213 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2217 * The ring buffer can count events recorded and overwritten per buffer,
2218 * but it is disabled by default due to its performance overhead.
2220 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2222 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2223 struct lib_ring_buffer
*buf
,
2226 v_add(config
, subbuffer_get_records_count(config
,
2227 &buf
->backend
, idx
),
2228 &buf
->records_count
);
2229 v_add(config
, subbuffer_count_records_overrun(config
,
2230 &buf
->backend
, idx
),
2231 &buf
->records_overrun
);
2233 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2235 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2236 struct lib_ring_buffer
*buf
,
2240 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2243 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2244 struct lib_ring_buffer
*buf
,
2245 struct channel
*chan
,
2246 unsigned long offset
,
2247 unsigned long commit_count
,
2251 unsigned long old_commit_count
= commit_count
2252 - chan
->backend
.subbuf_size
;
2255 * If we succeeded at updating cc_sb below, we are the subbuffer
2256 * writer delivering the subbuffer. Deals with concurrent
2257 * updates of the "cc" value without adding a add_return atomic
2258 * operation to the fast path.
2260 * We are doing the delivery in two steps:
2261 * - First, we cmpxchg() cc_sb to the new value
2262 * old_commit_count + 1. This ensures that we are the only
2263 * subbuffer user successfully filling the subbuffer, but we
2264 * do _not_ set the cc_sb value to "commit_count" yet.
2265 * Therefore, other writers that would wrap around the ring
2266 * buffer and try to start writing to our subbuffer would
2267 * have to drop records, because it would appear as
2269 * We therefore have exclusive access to the subbuffer control
2270 * structures. This mutual exclusion with other writers is
2271 * crucially important to perform record overruns count in
2272 * flight recorder mode locklessly.
2273 * - When we are ready to release the subbuffer (either for
2274 * reading or for overrun by other writers), we simply set the
2275 * cc_sb value to "commit_count" and perform delivery.
2277 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2278 * This guarantees that old_commit_count + 1 != commit_count.
2282 * Order prior updates to reserve count prior to the
2283 * commit_cold cc_sb update.
2286 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2287 old_commit_count
, old_commit_count
+ 1)
2288 == old_commit_count
)) {
2292 * Start of exclusive subbuffer access. We are
2293 * guaranteed to be the last writer in this subbuffer
2294 * and any other writer trying to access this subbuffer
2295 * in this state is required to drop records.
2297 * We can read the ts_end for the current sub-buffer
2298 * which has been saved by the very last space
2299 * reservation for the current sub-buffer.
2301 * Order increment of commit counter before reading ts_end.
2304 ts_end
= &buf
->ts_end
[idx
];
2305 deliver_count_events(config
, buf
, idx
);
2306 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2307 lib_ring_buffer_get_data_size(config
,
2312 * Increment the packet counter while we have exclusive
2315 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2318 * Set noref flag and offset for this subbuffer id.
2319 * Contains a memory barrier that ensures counter stores
2320 * are ordered before set noref and offset.
2322 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2323 buf_trunc_val(offset
, chan
));
2326 * Order set_noref and record counter updates before the
2327 * end of subbuffer exclusive access. Orders with
2328 * respect to writers coming into the subbuffer after
2329 * wrap around, and also order wrt concurrent readers.
2332 /* End of exclusive subbuffer access */
2333 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2336 * Order later updates to reserve count after
2337 * the commit_cold cc_sb update.
2340 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2344 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2346 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2347 && atomic_long_read(&buf
->active_readers
)
2348 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2349 wake_up_interruptible(&buf
->read_wait
);
2350 wake_up_interruptible(&chan
->read_wait
);
2355 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2357 int __init
init_lib_ring_buffer_frontend(void)
2361 for_each_possible_cpu(cpu
)
2362 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2366 module_init(init_lib_ring_buffer_frontend
);
2368 void __exit
exit_lib_ring_buffer_frontend(void)
2372 module_exit(exit_lib_ring_buffer_frontend
);