2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
54 #include <linux/delay.h>
55 #include <linux/module.h>
56 #include <linux/percpu.h>
58 #include <wrapper/ringbuffer/config.h>
59 #include <wrapper/ringbuffer/backend.h>
60 #include <wrapper/ringbuffer/frontend.h>
61 #include <wrapper/ringbuffer/iterator.h>
62 #include <wrapper/ringbuffer/nohz.h>
63 #include <wrapper/atomic.h>
64 #include <wrapper/kref.h>
65 #include <wrapper/percpu-defs.h>
66 #include <wrapper/timer.h>
69 * Internal structure representing offsets to use at a sub-buffer switch.
71 struct switch_offsets
{
72 unsigned long begin
, end
, old
;
73 size_t pre_header_padding
, size
;
74 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
85 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
86 #endif /* CONFIG_NO_HZ */
88 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
90 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
91 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
94 void lib_ring_buffer_print_errors(struct channel
*chan
,
95 struct lib_ring_buffer
*buf
, int cpu
);
97 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
98 enum switch_mode mode
);
101 * Must be called under cpu hotplug protection.
103 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
105 struct channel
*chan
= buf
->backend
.chan
;
107 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
108 kfree(buf
->commit_hot
);
109 kfree(buf
->commit_cold
);
111 lib_ring_buffer_backend_free(&buf
->backend
);
115 * lib_ring_buffer_reset - Reset ring buffer to initial values.
118 * Effectively empty the ring buffer. Should be called when the buffer is not
119 * used for writing. The ring buffer can be opened for reading, but the reader
120 * should not be using the iterator concurrently with reset. The previous
121 * current iterator record is reset.
123 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
125 struct channel
*chan
= buf
->backend
.chan
;
126 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
130 * Reset iterator first. It will put the subbuffer if it currently holds
133 lib_ring_buffer_iterator_reset(buf
);
134 v_set(config
, &buf
->offset
, 0);
135 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
136 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
137 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
138 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
140 atomic_long_set(&buf
->consumed
, 0);
141 atomic_set(&buf
->record_disabled
, 0);
142 v_set(config
, &buf
->last_tsc
, 0);
143 lib_ring_buffer_backend_reset(&buf
->backend
);
144 /* Don't reset number of active readers */
145 v_set(config
, &buf
->records_lost_full
, 0);
146 v_set(config
, &buf
->records_lost_wrap
, 0);
147 v_set(config
, &buf
->records_lost_big
, 0);
148 v_set(config
, &buf
->records_count
, 0);
149 v_set(config
, &buf
->records_overrun
, 0);
152 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
155 * channel_reset - Reset channel to initial values.
158 * Effectively empty the channel. Should be called when the channel is not used
159 * for writing. The channel can be opened for reading, but the reader should not
160 * be using the iterator concurrently with reset. The previous current iterator
163 void channel_reset(struct channel
*chan
)
166 * Reset iterators first. Will put the subbuffer if held for reading.
168 channel_iterator_reset(chan
);
169 atomic_set(&chan
->record_disabled
, 0);
170 /* Don't reset commit_count_mask, still valid */
171 channel_backend_reset(&chan
->backend
);
172 /* Don't reset switch/read timer interval */
173 /* Don't reset notifiers and notifier enable bits */
174 /* Don't reset reader reference count */
176 EXPORT_SYMBOL_GPL(channel_reset
);
179 * Must be called under cpu hotplug protection.
181 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
182 struct channel_backend
*chanb
, int cpu
)
184 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
185 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
186 void *priv
= chanb
->priv
;
187 size_t subbuf_header_size
;
191 /* Test for cpu hotplug */
192 if (buf
->backend
.allocated
)
196 * Paranoia: per cpu dynamic allocation is not officially documented as
197 * zeroing the memory, so let's do it here too, just in case.
199 memset(buf
, 0, sizeof(*buf
));
201 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
206 kzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
207 * chan
->backend
.num_subbuf
,
208 1 << INTERNODE_CACHE_SHIFT
),
209 GFP_KERNEL
| __GFP_NOWARN
,
210 cpu_to_node(max(cpu
, 0)));
211 if (!buf
->commit_hot
) {
217 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
218 * chan
->backend
.num_subbuf
,
219 1 << INTERNODE_CACHE_SHIFT
),
220 GFP_KERNEL
| __GFP_NOWARN
,
221 cpu_to_node(max(cpu
, 0)));
222 if (!buf
->commit_cold
) {
227 init_waitqueue_head(&buf
->read_wait
);
228 init_waitqueue_head(&buf
->write_wait
);
229 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
232 * Write the subbuffer header for first subbuffer so we know the total
233 * duration of data gathering.
235 subbuf_header_size
= config
->cb
.subbuffer_header_size();
236 v_set(config
, &buf
->offset
, subbuf_header_size
);
237 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
238 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
239 config
->cb
.buffer_begin(buf
, tsc
, 0);
240 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
242 if (config
->cb
.buffer_create
) {
243 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
249 * Ensure the buffer is ready before setting it to allocated and setting
251 * Used for cpu hotplug vs cpumask iteration.
254 buf
->backend
.allocated
= 1;
256 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
257 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
258 chan
->backend
.cpumask
));
259 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
266 kfree(buf
->commit_cold
);
268 kfree(buf
->commit_hot
);
270 lib_ring_buffer_backend_free(&buf
->backend
);
274 static void switch_buffer_timer(unsigned long data
)
276 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
277 struct channel
*chan
= buf
->backend
.chan
;
278 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
281 * Only flush buffers periodically if readers are active.
283 if (atomic_long_read(&buf
->active_readers
))
284 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
286 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
287 lttng_mod_timer_pinned(&buf
->switch_timer
,
288 jiffies
+ chan
->switch_timer_interval
);
290 mod_timer(&buf
->switch_timer
,
291 jiffies
+ chan
->switch_timer_interval
);
295 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
297 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
299 struct channel
*chan
= buf
->backend
.chan
;
300 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
302 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
305 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
306 lttng_init_timer_pinned(&buf
->switch_timer
);
308 init_timer(&buf
->switch_timer
);
310 buf
->switch_timer
.function
= switch_buffer_timer
;
311 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
312 buf
->switch_timer
.data
= (unsigned long)buf
;
313 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
314 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
316 add_timer(&buf
->switch_timer
);
317 buf
->switch_timer_enabled
= 1;
321 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
323 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
325 struct channel
*chan
= buf
->backend
.chan
;
327 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
330 del_timer_sync(&buf
->switch_timer
);
331 buf
->switch_timer_enabled
= 0;
335 * Polling timer to check the channels for data.
337 static void read_buffer_timer(unsigned long data
)
339 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
340 struct channel
*chan
= buf
->backend
.chan
;
341 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
343 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
345 if (atomic_long_read(&buf
->active_readers
)
346 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
347 wake_up_interruptible(&buf
->read_wait
);
348 wake_up_interruptible(&chan
->read_wait
);
351 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
352 lttng_mod_timer_pinned(&buf
->read_timer
,
353 jiffies
+ chan
->read_timer_interval
);
355 mod_timer(&buf
->read_timer
,
356 jiffies
+ chan
->read_timer_interval
);
360 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
362 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
364 struct channel
*chan
= buf
->backend
.chan
;
365 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
367 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
368 || !chan
->read_timer_interval
369 || buf
->read_timer_enabled
)
372 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
373 lttng_init_timer_pinned(&buf
->read_timer
);
375 init_timer(&buf
->read_timer
);
377 buf
->read_timer
.function
= read_buffer_timer
;
378 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
379 buf
->read_timer
.data
= (unsigned long)buf
;
381 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
382 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
384 add_timer(&buf
->read_timer
);
385 buf
->read_timer_enabled
= 1;
389 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
391 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
393 struct channel
*chan
= buf
->backend
.chan
;
394 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
396 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
397 || !chan
->read_timer_interval
398 || !buf
->read_timer_enabled
)
401 del_timer_sync(&buf
->read_timer
);
403 * do one more check to catch data that has been written in the last
406 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
407 wake_up_interruptible(&buf
->read_wait
);
408 wake_up_interruptible(&chan
->read_wait
);
410 buf
->read_timer_enabled
= 0;
413 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
415 enum cpuhp_state lttng_rb_hp_prepare
;
416 enum cpuhp_state lttng_rb_hp_online
;
418 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
420 lttng_rb_hp_prepare
= val
;
422 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
424 void lttng_rb_set_hp_online(enum cpuhp_state val
)
426 lttng_rb_hp_online
= val
;
428 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
430 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
431 struct lttng_cpuhp_node
*node
)
433 struct channel
*chan
= container_of(node
, struct channel
,
435 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
436 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
438 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
441 * Performing a buffer switch on a remote CPU. Performed by
442 * the CPU responsible for doing the hotunplug after the target
443 * CPU stopped running completely. Ensures that all data
444 * from that remote CPU is flushed.
446 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
449 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
451 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
452 struct lttng_cpuhp_node
*node
)
454 struct channel
*chan
= container_of(node
, struct channel
,
456 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
457 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
459 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
461 wake_up_interruptible(&chan
->hp_wait
);
462 lib_ring_buffer_start_switch_timer(buf
);
463 lib_ring_buffer_start_read_timer(buf
);
466 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
468 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
469 struct lttng_cpuhp_node
*node
)
471 struct channel
*chan
= container_of(node
, struct channel
,
473 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
474 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
476 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
478 lib_ring_buffer_stop_switch_timer(buf
);
479 lib_ring_buffer_stop_read_timer(buf
);
482 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
484 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
486 #ifdef CONFIG_HOTPLUG_CPU
489 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
490 * @nb: notifier block
491 * @action: hotplug action to take
494 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
497 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
498 unsigned long action
,
501 unsigned int cpu
= (unsigned long)hcpu
;
502 struct channel
*chan
= container_of(nb
, 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 if (!chan
->cpu_hp_enable
)
510 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
513 case CPU_DOWN_FAILED
:
514 case CPU_DOWN_FAILED_FROZEN
:
516 case CPU_ONLINE_FROZEN
:
517 wake_up_interruptible(&chan
->hp_wait
);
518 lib_ring_buffer_start_switch_timer(buf
);
519 lib_ring_buffer_start_read_timer(buf
);
522 case CPU_DOWN_PREPARE
:
523 case CPU_DOWN_PREPARE_FROZEN
:
524 lib_ring_buffer_stop_switch_timer(buf
);
525 lib_ring_buffer_stop_read_timer(buf
);
529 case CPU_DEAD_FROZEN
:
531 * Performing a buffer switch on a remote CPU. Performed by
532 * the CPU responsible for doing the hotunplug after the target
533 * CPU stopped running completely. Ensures that all data
534 * from that remote CPU is flushed.
536 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
546 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
548 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
550 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
551 * that wake-up-tracing generated events are flushed before going idle (in
552 * tick_nohz). We test if the spinlock is locked to deal with the race where
553 * readers try to sample the ring buffer before we perform the switch. We let
554 * the readers retry in that case. If there is data in the buffer, the wake up
555 * is going to forbid the CPU running the reader thread from going idle.
557 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
561 struct channel
*chan
= container_of(nb
, struct channel
,
563 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
564 struct lib_ring_buffer
*buf
;
565 int cpu
= smp_processor_id();
567 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
569 * We don't support keeping the system idle with global buffers
570 * and streaming active. In order to do so, we would need to
571 * sample a non-nohz-cpumask racelessly with the nohz updates
572 * without adding synchronization overhead to nohz. Leave this
573 * use-case out for now.
578 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
580 case TICK_NOHZ_FLUSH
:
581 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
582 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
583 && chan
->read_timer_interval
584 && atomic_long_read(&buf
->active_readers
)
585 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
586 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
587 wake_up_interruptible(&buf
->read_wait
);
588 wake_up_interruptible(&chan
->read_wait
);
590 if (chan
->switch_timer_interval
)
591 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
592 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
595 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
596 lib_ring_buffer_stop_switch_timer(buf
);
597 lib_ring_buffer_stop_read_timer(buf
);
598 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
600 case TICK_NOHZ_RESTART
:
601 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
602 lib_ring_buffer_start_read_timer(buf
);
603 lib_ring_buffer_start_switch_timer(buf
);
604 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
611 void notrace
lib_ring_buffer_tick_nohz_flush(void)
613 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
617 void notrace
lib_ring_buffer_tick_nohz_stop(void)
619 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
623 void notrace
lib_ring_buffer_tick_nohz_restart(void)
625 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
628 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
633 static void channel_unregister_notifiers(struct channel
*chan
)
635 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
637 channel_iterator_unregister_notifiers(chan
);
638 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
641 * Remove the nohz notifier first, so we are certain we stop
644 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
645 &chan
->tick_nohz_notifier
);
647 * ring_buffer_nohz_lock will not be needed below, because
648 * we just removed the notifiers, which were the only source of
651 #endif /* CONFIG_NO_HZ */
652 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
656 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
657 &chan
->cpuhp_online
.node
);
659 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
660 &chan
->cpuhp_prepare
.node
);
663 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
667 #ifdef CONFIG_HOTPLUG_CPU
669 chan
->cpu_hp_enable
= 0;
670 for_each_online_cpu(cpu
) {
671 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
673 lib_ring_buffer_stop_switch_timer(buf
);
674 lib_ring_buffer_stop_read_timer(buf
);
677 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
679 for_each_possible_cpu(cpu
) {
680 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
682 lib_ring_buffer_stop_switch_timer(buf
);
683 lib_ring_buffer_stop_read_timer(buf
);
687 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
689 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
691 lib_ring_buffer_stop_switch_timer(buf
);
692 lib_ring_buffer_stop_read_timer(buf
);
694 channel_backend_unregister_notifiers(&chan
->backend
);
697 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
699 if (!buf
->quiescent
) {
700 buf
->quiescent
= true;
701 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
705 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
707 buf
->quiescent
= false;
710 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
713 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
715 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
717 for_each_channel_cpu(cpu
, chan
) {
718 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
721 lib_ring_buffer_set_quiescent(buf
);
725 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
727 lib_ring_buffer_set_quiescent(buf
);
730 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
732 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
735 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
737 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
739 for_each_channel_cpu(cpu
, chan
) {
740 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
743 lib_ring_buffer_clear_quiescent(buf
);
747 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
749 lib_ring_buffer_clear_quiescent(buf
);
752 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
754 static void channel_free(struct channel
*chan
)
756 if (chan
->backend
.release_priv_ops
) {
757 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
759 channel_iterator_free(chan
);
760 channel_backend_free(&chan
->backend
);
765 * channel_create - Create channel.
766 * @config: ring buffer instance configuration
767 * @name: name of the channel
768 * @priv: ring buffer client private data
769 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
770 * address mapping. It is used only by RING_BUFFER_STATIC
771 * configuration. It can be set to NULL for other backends.
772 * @subbuf_size: subbuffer size
773 * @num_subbuf: number of subbuffers
774 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
775 * padding to let readers get those sub-buffers.
776 * Used for live streaming.
777 * @read_timer_interval: Time interval (in us) to wake up pending readers.
780 * Returns NULL on failure.
782 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
783 const char *name
, void *priv
, void *buf_addr
,
785 size_t num_subbuf
, unsigned int switch_timer_interval
,
786 unsigned int read_timer_interval
)
789 struct channel
*chan
;
791 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
792 read_timer_interval
))
795 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
799 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
800 subbuf_size
, num_subbuf
);
804 ret
= channel_iterator_init(chan
);
806 goto error_free_backend
;
808 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
809 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
810 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
811 kref_init(&chan
->ref
);
812 init_waitqueue_head(&chan
->read_wait
);
813 init_waitqueue_head(&chan
->hp_wait
);
815 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
816 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
817 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
818 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
819 &chan
->cpuhp_prepare
.node
);
821 goto cpuhp_prepare_error
;
823 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
824 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
825 &chan
->cpuhp_online
.node
);
827 goto cpuhp_online_error
;
828 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
832 * In case of non-hotplug cpu, if the ring-buffer is allocated
833 * in early initcall, it will not be notified of secondary cpus.
834 * In that off case, we need to allocate for all possible cpus.
836 #ifdef CONFIG_HOTPLUG_CPU
837 chan
->cpu_hp_notifier
.notifier_call
=
838 lib_ring_buffer_cpu_hp_callback
;
839 chan
->cpu_hp_notifier
.priority
= 6;
840 register_cpu_notifier(&chan
->cpu_hp_notifier
);
843 for_each_online_cpu(cpu
) {
844 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
846 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
847 lib_ring_buffer_start_switch_timer(buf
);
848 lib_ring_buffer_start_read_timer(buf
);
849 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
851 chan
->cpu_hp_enable
= 1;
854 for_each_possible_cpu(cpu
) {
855 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
857 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
858 lib_ring_buffer_start_switch_timer(buf
);
859 lib_ring_buffer_start_read_timer(buf
);
860 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
864 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
866 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
867 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
868 chan
->tick_nohz_notifier
.notifier_call
=
869 ring_buffer_tick_nohz_callback
;
870 chan
->tick_nohz_notifier
.priority
= ~0U;
871 atomic_notifier_chain_register(&tick_nohz_notifier
,
872 &chan
->tick_nohz_notifier
);
873 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
876 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
878 lib_ring_buffer_start_switch_timer(buf
);
879 lib_ring_buffer_start_read_timer(buf
);
884 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
886 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
887 &chan
->cpuhp_prepare
.node
);
890 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
892 channel_backend_free(&chan
->backend
);
897 EXPORT_SYMBOL_GPL(channel_create
);
900 void channel_release(struct kref
*kref
)
902 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
907 * channel_destroy - Finalize, wait for q.s. and destroy channel.
908 * @chan: channel to destroy
911 * Call "destroy" callback, finalize channels, and then decrement the
912 * channel reference count. Note that when readers have completed data
913 * consumption of finalized channels, get_subbuf() will return -ENODATA.
914 * They should release their handle at that point. Returns the private
917 void *channel_destroy(struct channel
*chan
)
920 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
923 channel_unregister_notifiers(chan
);
925 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
927 * No need to hold cpu hotplug, because all notifiers have been
930 for_each_channel_cpu(cpu
, chan
) {
931 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
934 if (config
->cb
.buffer_finalize
)
935 config
->cb
.buffer_finalize(buf
,
938 if (buf
->backend
.allocated
)
939 lib_ring_buffer_set_quiescent(buf
);
941 * Perform flush before writing to finalized.
944 ACCESS_ONCE(buf
->finalized
) = 1;
945 wake_up_interruptible(&buf
->read_wait
);
948 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
950 if (config
->cb
.buffer_finalize
)
951 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
952 if (buf
->backend
.allocated
)
953 lib_ring_buffer_set_quiescent(buf
);
955 * Perform flush before writing to finalized.
958 ACCESS_ONCE(buf
->finalized
) = 1;
959 wake_up_interruptible(&buf
->read_wait
);
961 ACCESS_ONCE(chan
->finalized
) = 1;
962 wake_up_interruptible(&chan
->hp_wait
);
963 wake_up_interruptible(&chan
->read_wait
);
964 priv
= chan
->backend
.priv
;
965 kref_put(&chan
->ref
, channel_release
);
968 EXPORT_SYMBOL_GPL(channel_destroy
);
970 struct lib_ring_buffer
*channel_get_ring_buffer(
971 const struct lib_ring_buffer_config
*config
,
972 struct channel
*chan
, int cpu
)
974 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
975 return chan
->backend
.buf
;
977 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
979 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
981 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
983 struct channel
*chan
= buf
->backend
.chan
;
985 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
987 if (!lttng_kref_get(&chan
->ref
)) {
988 atomic_long_dec(&buf
->active_readers
);
991 lttng_smp_mb__after_atomic();
994 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
996 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
998 struct channel
*chan
= buf
->backend
.chan
;
1000 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1001 lttng_smp_mb__before_atomic();
1002 atomic_long_dec(&buf
->active_readers
);
1003 kref_put(&chan
->ref
, channel_release
);
1005 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1008 * Promote compiler barrier to a smp_mb().
1009 * For the specific ring buffer case, this IPI call should be removed if the
1010 * architecture does not reorder writes. This should eventually be provided by
1011 * a separate architecture-specific infrastructure.
1013 static void remote_mb(void *info
)
1019 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1021 * @consumed: consumed count indicating the position where to read
1022 * @produced: produced count, indicates position when to stop reading
1024 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1025 * data to read at consumed position, or 0 if the get operation succeeds.
1026 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1029 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1030 unsigned long *consumed
, unsigned long *produced
)
1032 struct channel
*chan
= buf
->backend
.chan
;
1033 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1034 unsigned long consumed_cur
, write_offset
;
1038 * First, ensure we perform a "final" flush onto the stream. This will
1039 * ensure we create a packet of padding if we encounter an empty
1040 * packet. This ensures the time-stamps right before the snapshot is
1041 * used as end of packet timestamp.
1043 if (!buf
->quiescent
)
1044 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1047 finalized
= ACCESS_ONCE(buf
->finalized
);
1049 * Read finalized before counters.
1052 consumed_cur
= atomic_long_read(&buf
->consumed
);
1054 * No need to issue a memory barrier between consumed count read and
1055 * write offset read, because consumed count can only change
1056 * concurrently in overwrite mode, and we keep a sequence counter
1057 * identifier derived from the write offset to check we are getting
1058 * the same sub-buffer we are expecting (the sub-buffers are atomically
1059 * "tagged" upon writes, tags are checked upon read).
1061 write_offset
= v_read(config
, &buf
->offset
);
1064 * Check that we are not about to read the same subbuffer in
1065 * which the writer head is.
1067 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1071 *consumed
= consumed_cur
;
1072 *produced
= subbuf_trunc(write_offset
, chan
);
1078 * The memory barriers __wait_event()/wake_up_interruptible() take care
1079 * of "raw_spin_is_locked" memory ordering.
1083 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1088 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1091 * lib_ring_buffer_put_snapshot - move consumed counter forward
1093 * Should only be called from consumer context.
1095 * @consumed_new: new consumed count value
1097 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1098 unsigned long consumed_new
)
1100 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1101 struct channel
*chan
= bufb
->chan
;
1102 unsigned long consumed
;
1104 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1107 * Only push the consumed value forward.
1108 * If the consumed cmpxchg fails, this is because we have been pushed by
1109 * the writer in flight recorder mode.
1111 consumed
= atomic_long_read(&buf
->consumed
);
1112 while ((long) consumed
- (long) consumed_new
< 0)
1113 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1115 /* Wake-up the metadata producer */
1116 wake_up_interruptible(&buf
->write_wait
);
1118 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1121 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1123 * @consumed: consumed count indicating the position where to read
1125 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1126 * data to read at consumed position, or 0 if the get operation succeeds.
1127 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1129 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1130 unsigned long consumed
)
1132 struct channel
*chan
= buf
->backend
.chan
;
1133 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1134 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1138 if (buf
->get_subbuf
) {
1140 * Reader is trying to get a subbuffer twice.
1142 CHAN_WARN_ON(chan
, 1);
1146 finalized
= ACCESS_ONCE(buf
->finalized
);
1148 * Read finalized before counters.
1151 consumed_cur
= atomic_long_read(&buf
->consumed
);
1152 consumed_idx
= subbuf_index(consumed
, chan
);
1153 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1155 * Make sure we read the commit count before reading the buffer
1156 * data and the write offset. Correct consumed offset ordering
1157 * wrt commit count is insured by the use of cmpxchg to update
1158 * the consumed offset.
1159 * smp_call_function_single can fail if the remote CPU is offline,
1160 * this is OK because then there is no wmb to execute there.
1161 * If our thread is executing on the same CPU as the on the buffers
1162 * belongs to, we don't have to synchronize it at all. If we are
1163 * migrated, the scheduler will take care of the memory barriers.
1164 * Normally, smp_call_function_single() should ensure program order when
1165 * executing the remote function, which implies that it surrounds the
1166 * function execution with :
1177 * However, smp_call_function_single() does not seem to clearly execute
1178 * such barriers. It depends on spinlock semantic to provide the barrier
1179 * before executing the IPI and, when busy-looping, csd_lock_wait only
1180 * executes smp_mb() when it has to wait for the other CPU.
1182 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1183 * required ourself, even if duplicated. It has no performance impact
1186 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1187 * read and write vs write. They do not ensure core synchronization. We
1188 * really have to ensure total order between the 3 barriers running on
1191 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1192 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1193 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1194 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1195 /* Total order with IPI handler smp_mb() */
1197 smp_call_function_single(buf
->backend
.cpu
,
1198 remote_mb
, NULL
, 1);
1199 /* Total order with IPI handler smp_mb() */
1203 /* Total order with IPI handler smp_mb() */
1205 smp_call_function(remote_mb
, NULL
, 1);
1206 /* Total order with IPI handler smp_mb() */
1211 * Local rmb to match the remote wmb to read the commit count
1212 * before the buffer data and the write offset.
1217 write_offset
= v_read(config
, &buf
->offset
);
1220 * Check that the buffer we are getting is after or at consumed_cur
1223 if ((long) subbuf_trunc(consumed
, chan
)
1224 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1228 * Check that the subbuffer we are trying to consume has been
1229 * already fully committed.
1231 if (((commit_count
- chan
->backend
.subbuf_size
)
1232 & chan
->commit_count_mask
)
1233 - (buf_trunc(consumed
, chan
)
1234 >> chan
->backend
.num_subbuf_order
)
1239 * Check that we are not about to read the same subbuffer in
1240 * which the writer head is.
1242 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1247 * Failure to get the subbuffer causes a busy-loop retry without going
1248 * to a wait queue. These are caused by short-lived race windows where
1249 * the writer is getting access to a subbuffer we were trying to get
1250 * access to. Also checks that the "consumed" buffer count we are
1251 * looking for matches the one contained in the subbuffer id.
1253 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1254 consumed_idx
, buf_trunc_val(consumed
, chan
));
1257 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1259 buf
->get_subbuf_consumed
= consumed
;
1260 buf
->get_subbuf
= 1;
1266 * The memory barriers __wait_event()/wake_up_interruptible() take care
1267 * of "raw_spin_is_locked" memory ordering.
1271 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1276 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1279 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1282 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1284 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1285 struct channel
*chan
= bufb
->chan
;
1286 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1287 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1289 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1291 if (!buf
->get_subbuf
) {
1293 * Reader puts a subbuffer it did not get.
1295 CHAN_WARN_ON(chan
, 1);
1298 consumed
= buf
->get_subbuf_consumed
;
1299 buf
->get_subbuf
= 0;
1302 * Clear the records_unread counter. (overruns counter)
1303 * Can still be non-zero if a file reader simply grabbed the data
1304 * without using iterators.
1305 * Can be below zero if an iterator is used on a snapshot more than
1308 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1309 v_add(config
, v_read(config
,
1310 &bufb
->array
[read_sb_bindex
]->records_unread
),
1311 &bufb
->records_read
);
1312 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1313 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1314 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1315 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1318 * Exchange the reader subbuffer with the one we put in its place in the
1319 * writer subbuffer table. Expect the original consumed count. If
1320 * update_read_sb_index fails, this is because the writer updated the
1321 * subbuffer concurrently. We should therefore keep the subbuffer we
1322 * currently have: it has become invalid to try reading this sub-buffer
1323 * consumed count value anyway.
1325 consumed_idx
= subbuf_index(consumed
, chan
);
1326 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1327 consumed_idx
, buf_trunc_val(consumed
, chan
));
1329 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1330 * if the writer concurrently updated it.
1333 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1336 * cons_offset is an iterator on all subbuffer offsets between the reader
1337 * position and the writer position. (inclusive)
1340 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1341 struct channel
*chan
,
1342 unsigned long cons_offset
,
1345 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1346 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1348 cons_idx
= subbuf_index(cons_offset
, chan
);
1349 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1350 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1352 if (subbuf_offset(commit_count
, chan
) != 0)
1354 "ring buffer %s, cpu %d: "
1355 "commit count in subbuffer %lu,\n"
1356 "expecting multiples of %lu bytes\n"
1357 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1358 chan
->backend
.name
, cpu
, cons_idx
,
1359 chan
->backend
.subbuf_size
,
1360 commit_count
, commit_count_sb
);
1362 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1363 chan
->backend
.name
, cpu
, commit_count
);
1367 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1368 struct channel
*chan
,
1369 void *priv
, int cpu
)
1371 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1372 unsigned long write_offset
, cons_offset
;
1375 * No need to order commit_count, write_offset and cons_offset reads
1376 * because we execute at teardown when no more writer nor reader
1377 * references are left.
1379 write_offset
= v_read(config
, &buf
->offset
);
1380 cons_offset
= atomic_long_read(&buf
->consumed
);
1381 if (write_offset
!= cons_offset
)
1383 "ring buffer %s, cpu %d: "
1384 "non-consumed data\n"
1385 " [ %lu bytes written, %lu bytes read ]\n",
1386 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1388 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1389 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1392 cons_offset
= subbuf_align(cons_offset
, chan
))
1393 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1398 void lib_ring_buffer_print_errors(struct channel
*chan
,
1399 struct lib_ring_buffer
*buf
, int cpu
)
1401 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1402 void *priv
= chan
->backend
.priv
;
1404 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1405 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1406 "%lu records overrun\n",
1408 v_read(config
, &buf
->records_count
),
1409 v_read(config
, &buf
->records_overrun
));
1411 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1412 "%lu records overrun\n",
1413 chan
->backend
.name
, cpu
,
1414 v_read(config
, &buf
->records_count
),
1415 v_read(config
, &buf
->records_overrun
));
1417 if (v_read(config
, &buf
->records_lost_full
)
1418 || v_read(config
, &buf
->records_lost_wrap
)
1419 || v_read(config
, &buf
->records_lost_big
))
1421 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1422 " [ %lu buffer full, %lu nest buffer wrap-around, "
1423 "%lu event too big ]\n",
1424 chan
->backend
.name
, cpu
,
1425 v_read(config
, &buf
->records_lost_full
),
1426 v_read(config
, &buf
->records_lost_wrap
),
1427 v_read(config
, &buf
->records_lost_big
));
1429 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1433 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1435 * Only executed by SWITCH_FLUSH, which can be issued while tracing is active
1436 * or at buffer finalization (destroy).
1439 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1440 struct channel
*chan
,
1441 struct switch_offsets
*offsets
,
1444 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1445 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1446 unsigned long commit_count
;
1448 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1451 * Order all writes to buffer before the commit count update that will
1452 * determine that the subbuffer is full.
1454 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1456 * Must write slot data before incrementing commit count. This
1457 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1463 v_add(config
, config
->cb
.subbuffer_header_size(),
1464 &buf
->commit_hot
[oldidx
].cc
);
1465 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1466 /* Check if the written buffer has to be delivered */
1467 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1468 commit_count
, oldidx
, tsc
);
1469 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1470 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1475 * lib_ring_buffer_switch_old_end: switch old subbuffer
1477 * Note : offset_old should never be 0 here. It is ok, because we never perform
1478 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1479 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1483 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1484 struct channel
*chan
,
1485 struct switch_offsets
*offsets
,
1488 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1489 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1490 unsigned long commit_count
, padding_size
, data_size
;
1492 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1493 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1494 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1497 * Order all writes to buffer before the commit count update that will
1498 * determine that the subbuffer is full.
1500 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1502 * Must write slot data before incrementing commit count. This
1503 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1509 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1510 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1511 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1512 commit_count
, oldidx
, tsc
);
1513 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1514 offsets
->old
+ padding_size
, commit_count
);
1518 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1520 * This code can be executed unordered : writers may already have written to the
1521 * sub-buffer before this code gets executed, caution. The commit makes sure
1522 * that this code is executed before the deliver of this sub-buffer.
1525 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1526 struct channel
*chan
,
1527 struct switch_offsets
*offsets
,
1530 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1531 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1532 unsigned long commit_count
;
1534 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1537 * Order all writes to buffer before the commit count update that will
1538 * determine that the subbuffer is full.
1540 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1542 * Must write slot data before incrementing commit count. This
1543 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1549 v_add(config
, config
->cb
.subbuffer_header_size(),
1550 &buf
->commit_hot
[beginidx
].cc
);
1551 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1552 /* Check if the written buffer has to be delivered */
1553 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1554 commit_count
, beginidx
, tsc
);
1555 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1556 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1561 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1563 * Calls subbuffer_set_data_size() to set the data size of the current
1564 * sub-buffer. We do not need to perform check_deliver nor commit here,
1565 * since this task will be done by the "commit" of the event for which
1566 * we are currently doing the space reservation.
1569 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1570 struct channel
*chan
,
1571 struct switch_offsets
*offsets
,
1574 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1575 unsigned long endidx
, data_size
;
1577 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1578 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1579 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1585 * !0 if execution must be aborted.
1588 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1589 struct lib_ring_buffer
*buf
,
1590 struct channel
*chan
,
1591 struct switch_offsets
*offsets
,
1594 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1595 unsigned long off
, reserve_commit_diff
;
1597 offsets
->begin
= v_read(config
, &buf
->offset
);
1598 offsets
->old
= offsets
->begin
;
1599 offsets
->switch_old_start
= 0;
1600 off
= subbuf_offset(offsets
->begin
, chan
);
1602 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1605 * Ensure we flush the header of an empty subbuffer when doing the
1606 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1607 * total data gathering duration even if there were no records saved
1608 * after the last buffer switch.
1609 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1610 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1611 * subbuffer header as appropriate.
1612 * The next record that reserves space will be responsible for
1613 * populating the following subbuffer header. We choose not to populate
1614 * the next subbuffer header here because we want to be able to use
1615 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1616 * buffer flush, which must guarantee that all the buffer content
1617 * (records and header timestamps) are visible to the reader. This is
1618 * required for quiescence guarantees for the fusion merge.
1620 if (mode
!= SWITCH_FLUSH
&& !off
)
1621 return -1; /* we do not have to switch : buffer is empty */
1623 if (unlikely(off
== 0)) {
1624 unsigned long sb_index
, commit_count
;
1627 * We are performing a SWITCH_FLUSH. There may be concurrent
1628 * writes into the buffer if e.g. invoked while performing a
1629 * snapshot on an active trace.
1631 * If the client does not save any header information (sub-buffer
1632 * header size == 0), don't switch empty subbuffer on finalize,
1633 * because it is invalid to deliver a completely empty
1636 if (!config
->cb
.subbuffer_header_size())
1639 /* Test new buffer integrity */
1640 sb_index
= subbuf_index(offsets
->begin
, chan
);
1641 commit_count
= v_read(config
,
1642 &buf
->commit_cold
[sb_index
].cc_sb
);
1643 reserve_commit_diff
=
1644 (buf_trunc(offsets
->begin
, chan
)
1645 >> chan
->backend
.num_subbuf_order
)
1646 - (commit_count
& chan
->commit_count_mask
);
1647 if (likely(reserve_commit_diff
== 0)) {
1648 /* Next subbuffer not being written to. */
1649 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1650 subbuf_trunc(offsets
->begin
, chan
)
1651 - subbuf_trunc((unsigned long)
1652 atomic_long_read(&buf
->consumed
), chan
)
1653 >= chan
->backend
.buf_size
)) {
1655 * We do not overwrite non consumed buffers
1656 * and we are full : don't switch.
1661 * Next subbuffer not being written to, and we
1662 * are either in overwrite mode or the buffer is
1663 * not full. It's safe to write in this new
1669 * Next subbuffer reserve offset does not match the
1670 * commit offset. Don't perform switch in
1671 * producer-consumer and overwrite mode. Caused by
1672 * either a writer OOPS or too many nested writes over a
1673 * reserve/commit pair.
1679 * Need to write the subbuffer start header on finalize.
1681 offsets
->switch_old_start
= 1;
1683 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1684 /* Note: old points to the next subbuf at offset 0 */
1685 offsets
->end
= offsets
->begin
;
1690 * Force a sub-buffer switch. This operation is completely reentrant : can be
1691 * called while tracing is active with absolutely no lock held.
1693 * Note, however, that as a v_cmpxchg is used for some atomic
1694 * operations, this function must be called from the CPU which owns the buffer
1695 * for a ACTIVE flush.
1697 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1699 struct channel
*chan
= buf
->backend
.chan
;
1700 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1701 struct switch_offsets offsets
;
1702 unsigned long oldidx
;
1708 * Perform retryable operations.
1711 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1713 return; /* Switch not needed */
1714 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1718 * Atomically update last_tsc. This update races against concurrent
1719 * atomic updates, but the race will always cause supplementary full TSC
1720 * records, never the opposite (missing a full TSC record when it would
1723 save_last_tsc(config
, buf
, tsc
);
1726 * Push the reader if necessary
1728 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1730 oldidx
= subbuf_index(offsets
.old
, chan
);
1731 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1734 * May need to populate header start on SWITCH_FLUSH.
1736 if (offsets
.switch_old_start
) {
1737 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1738 offsets
.old
+= config
->cb
.subbuffer_header_size();
1742 * Switch old subbuffer.
1744 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1746 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1748 struct switch_param
{
1749 struct lib_ring_buffer
*buf
;
1750 enum switch_mode mode
;
1753 static void remote_switch(void *info
)
1755 struct switch_param
*param
= info
;
1756 struct lib_ring_buffer
*buf
= param
->buf
;
1758 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1761 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1762 enum switch_mode mode
)
1764 struct channel
*chan
= buf
->backend
.chan
;
1765 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1767 struct switch_param param
;
1770 * With global synchronization we don't need to use the IPI scheme.
1772 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1773 lib_ring_buffer_switch_slow(buf
, mode
);
1778 * Taking lock on CPU hotplug to ensure two things: first, that the
1779 * target cpu is not taken concurrently offline while we are within
1780 * smp_call_function_single() (I don't trust that get_cpu() on the
1781 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1782 * confirmed)). Secondly, if it happens that the CPU is not online, our
1783 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1784 * CPU hotplug handlers, which can also perform a remote subbuffer
1790 ret
= smp_call_function_single(buf
->backend
.cpu
,
1791 remote_switch
, ¶m
, 1);
1793 /* Remote CPU is offline, do it ourself. */
1794 lib_ring_buffer_switch_slow(buf
, mode
);
1799 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1801 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1803 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1808 * -ENOSPC if event size is too large for packet.
1809 * -ENOBUFS if there is currently not enough space in buffer for the event.
1810 * -EIO if data cannot be written into the buffer for any other reason.
1813 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1814 struct channel
*chan
,
1815 struct switch_offsets
*offsets
,
1816 struct lib_ring_buffer_ctx
*ctx
)
1818 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1819 unsigned long reserve_commit_diff
, offset_cmp
;
1822 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1823 offsets
->old
= offsets
->begin
;
1824 offsets
->switch_new_start
= 0;
1825 offsets
->switch_new_end
= 0;
1826 offsets
->switch_old_end
= 0;
1827 offsets
->pre_header_padding
= 0;
1829 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1830 if ((int64_t) ctx
->tsc
== -EIO
)
1833 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1834 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1836 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1837 offsets
->switch_new_start
= 1; /* For offsets->begin */
1839 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1841 &offsets
->pre_header_padding
,
1844 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1847 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1848 offsets
->size
> chan
->backend
.subbuf_size
)) {
1849 offsets
->switch_old_end
= 1; /* For offsets->old */
1850 offsets
->switch_new_start
= 1; /* For offsets->begin */
1853 if (unlikely(offsets
->switch_new_start
)) {
1854 unsigned long sb_index
, commit_count
;
1857 * We are typically not filling the previous buffer completely.
1859 if (likely(offsets
->switch_old_end
))
1860 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1861 offsets
->begin
= offsets
->begin
1862 + config
->cb
.subbuffer_header_size();
1863 /* Test new buffer integrity */
1864 sb_index
= subbuf_index(offsets
->begin
, chan
);
1866 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1867 * lib_ring_buffer_check_deliver() has the matching
1868 * memory barriers required around commit_cold cc_sb
1869 * updates to ensure reserve and commit counter updates
1870 * are not seen reordered when updated by another CPU.
1873 commit_count
= v_read(config
,
1874 &buf
->commit_cold
[sb_index
].cc_sb
);
1875 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1877 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1879 * The reserve counter have been concurrently updated
1880 * while we read the commit counter. This means the
1881 * commit counter we read might not match buf->offset
1882 * due to concurrent update. We therefore need to retry.
1886 reserve_commit_diff
=
1887 (buf_trunc(offsets
->begin
, chan
)
1888 >> chan
->backend
.num_subbuf_order
)
1889 - (commit_count
& chan
->commit_count_mask
);
1890 if (likely(reserve_commit_diff
== 0)) {
1891 /* Next subbuffer not being written to. */
1892 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1893 subbuf_trunc(offsets
->begin
, chan
)
1894 - subbuf_trunc((unsigned long)
1895 atomic_long_read(&buf
->consumed
), chan
)
1896 >= chan
->backend
.buf_size
)) {
1898 * We do not overwrite non consumed buffers
1899 * and we are full : record is lost.
1901 v_inc(config
, &buf
->records_lost_full
);
1905 * Next subbuffer not being written to, and we
1906 * are either in overwrite mode or the buffer is
1907 * not full. It's safe to write in this new
1913 * Next subbuffer reserve offset does not match the
1914 * commit offset, and this did not involve update to the
1915 * reserve counter. Drop record in producer-consumer and
1916 * overwrite mode. Caused by either a writer OOPS or
1917 * too many nested writes over a reserve/commit pair.
1919 v_inc(config
, &buf
->records_lost_wrap
);
1923 config
->cb
.record_header_size(config
, chan
,
1925 &offsets
->pre_header_padding
,
1928 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1931 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1932 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1934 * Record too big for subbuffers, report error, don't
1935 * complete the sub-buffer switch.
1937 v_inc(config
, &buf
->records_lost_big
);
1941 * We just made a successful buffer switch and the
1942 * record fits in the new subbuffer. Let's write.
1947 * Record fits in the current buffer and we are not on a switch
1948 * boundary. It's safe to write.
1951 offsets
->end
= offsets
->begin
+ offsets
->size
;
1953 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1955 * The offset_end will fall at the very beginning of the next
1958 offsets
->switch_new_end
= 1; /* For offsets->begin */
1964 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1965 * @ctx: ring buffer context.
1967 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1968 * -EIO for other errors, else returns 0.
1969 * It will take care of sub-buffer switching.
1971 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1973 struct channel
*chan
= ctx
->chan
;
1974 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1975 struct lib_ring_buffer
*buf
;
1976 struct switch_offsets offsets
;
1979 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1980 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1982 buf
= chan
->backend
.buf
;
1988 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1992 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1997 * Atomically update last_tsc. This update races against concurrent
1998 * atomic updates, but the race will always cause supplementary full TSC
1999 * records, never the opposite (missing a full TSC record when it would
2002 save_last_tsc(config
, buf
, ctx
->tsc
);
2005 * Push the reader if necessary
2007 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2010 * Clear noref flag for this subbuffer.
2012 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2013 subbuf_index(offsets
.end
- 1, chan
));
2016 * Switch old subbuffer if needed.
2018 if (unlikely(offsets
.switch_old_end
)) {
2019 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2020 subbuf_index(offsets
.old
- 1, chan
));
2021 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2025 * Populate new subbuffer.
2027 if (unlikely(offsets
.switch_new_start
))
2028 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2030 if (unlikely(offsets
.switch_new_end
))
2031 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2033 ctx
->slot_size
= offsets
.size
;
2034 ctx
->pre_offset
= offsets
.begin
;
2035 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2038 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2040 int __init
init_lib_ring_buffer_frontend(void)
2044 for_each_possible_cpu(cpu
)
2045 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2049 module_init(init_lib_ring_buffer_frontend
);
2051 void __exit
exit_lib_ring_buffer_frontend(void)
2055 module_exit(exit_lib_ring_buffer_frontend
);