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 finalized
= ACCESS_ONCE(buf
->finalized
);
1040 * Read finalized before counters.
1043 consumed_cur
= atomic_long_read(&buf
->consumed
);
1045 * No need to issue a memory barrier between consumed count read and
1046 * write offset read, because consumed count can only change
1047 * concurrently in overwrite mode, and we keep a sequence counter
1048 * identifier derived from the write offset to check we are getting
1049 * the same sub-buffer we are expecting (the sub-buffers are atomically
1050 * "tagged" upon writes, tags are checked upon read).
1052 write_offset
= v_read(config
, &buf
->offset
);
1055 * Check that we are not about to read the same subbuffer in
1056 * which the writer head is.
1058 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1062 *consumed
= consumed_cur
;
1063 *produced
= subbuf_trunc(write_offset
, chan
);
1069 * The memory barriers __wait_event()/wake_up_interruptible() take care
1070 * of "raw_spin_is_locked" memory ordering.
1074 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1079 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1082 * lib_ring_buffer_put_snapshot - move consumed counter forward
1084 * Should only be called from consumer context.
1086 * @consumed_new: new consumed count value
1088 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1089 unsigned long consumed_new
)
1091 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1092 struct channel
*chan
= bufb
->chan
;
1093 unsigned long consumed
;
1095 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1098 * Only push the consumed value forward.
1099 * If the consumed cmpxchg fails, this is because we have been pushed by
1100 * the writer in flight recorder mode.
1102 consumed
= atomic_long_read(&buf
->consumed
);
1103 while ((long) consumed
- (long) consumed_new
< 0)
1104 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1106 /* Wake-up the metadata producer */
1107 wake_up_interruptible(&buf
->write_wait
);
1109 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1112 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1114 * @consumed: consumed count indicating the position where to read
1116 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1117 * data to read at consumed position, or 0 if the get operation succeeds.
1118 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1120 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1121 unsigned long consumed
)
1123 struct channel
*chan
= buf
->backend
.chan
;
1124 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1125 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1129 if (buf
->get_subbuf
) {
1131 * Reader is trying to get a subbuffer twice.
1133 CHAN_WARN_ON(chan
, 1);
1137 finalized
= ACCESS_ONCE(buf
->finalized
);
1139 * Read finalized before counters.
1142 consumed_cur
= atomic_long_read(&buf
->consumed
);
1143 consumed_idx
= subbuf_index(consumed
, chan
);
1144 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1146 * Make sure we read the commit count before reading the buffer
1147 * data and the write offset. Correct consumed offset ordering
1148 * wrt commit count is insured by the use of cmpxchg to update
1149 * the consumed offset.
1150 * smp_call_function_single can fail if the remote CPU is offline,
1151 * this is OK because then there is no wmb to execute there.
1152 * If our thread is executing on the same CPU as the on the buffers
1153 * belongs to, we don't have to synchronize it at all. If we are
1154 * migrated, the scheduler will take care of the memory barriers.
1155 * Normally, smp_call_function_single() should ensure program order when
1156 * executing the remote function, which implies that it surrounds the
1157 * function execution with :
1168 * However, smp_call_function_single() does not seem to clearly execute
1169 * such barriers. It depends on spinlock semantic to provide the barrier
1170 * before executing the IPI and, when busy-looping, csd_lock_wait only
1171 * executes smp_mb() when it has to wait for the other CPU.
1173 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1174 * required ourself, even if duplicated. It has no performance impact
1177 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1178 * read and write vs write. They do not ensure core synchronization. We
1179 * really have to ensure total order between the 3 barriers running on
1182 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1183 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1184 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1185 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1186 /* Total order with IPI handler smp_mb() */
1188 smp_call_function_single(buf
->backend
.cpu
,
1189 remote_mb
, NULL
, 1);
1190 /* Total order with IPI handler smp_mb() */
1194 /* Total order with IPI handler smp_mb() */
1196 smp_call_function(remote_mb
, NULL
, 1);
1197 /* Total order with IPI handler smp_mb() */
1202 * Local rmb to match the remote wmb to read the commit count
1203 * before the buffer data and the write offset.
1208 write_offset
= v_read(config
, &buf
->offset
);
1211 * Check that the buffer we are getting is after or at consumed_cur
1214 if ((long) subbuf_trunc(consumed
, chan
)
1215 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1219 * Check that the subbuffer we are trying to consume has been
1220 * already fully committed.
1222 if (((commit_count
- chan
->backend
.subbuf_size
)
1223 & chan
->commit_count_mask
)
1224 - (buf_trunc(consumed
, chan
)
1225 >> chan
->backend
.num_subbuf_order
)
1230 * Check that we are not about to read the same subbuffer in
1231 * which the writer head is.
1233 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1238 * Failure to get the subbuffer causes a busy-loop retry without going
1239 * to a wait queue. These are caused by short-lived race windows where
1240 * the writer is getting access to a subbuffer we were trying to get
1241 * access to. Also checks that the "consumed" buffer count we are
1242 * looking for matches the one contained in the subbuffer id.
1244 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1245 consumed_idx
, buf_trunc_val(consumed
, chan
));
1248 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1250 buf
->get_subbuf_consumed
= consumed
;
1251 buf
->get_subbuf
= 1;
1257 * The memory barriers __wait_event()/wake_up_interruptible() take care
1258 * of "raw_spin_is_locked" memory ordering.
1262 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1267 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1270 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1273 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1275 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1276 struct channel
*chan
= bufb
->chan
;
1277 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1278 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1280 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1282 if (!buf
->get_subbuf
) {
1284 * Reader puts a subbuffer it did not get.
1286 CHAN_WARN_ON(chan
, 1);
1289 consumed
= buf
->get_subbuf_consumed
;
1290 buf
->get_subbuf
= 0;
1293 * Clear the records_unread counter. (overruns counter)
1294 * Can still be non-zero if a file reader simply grabbed the data
1295 * without using iterators.
1296 * Can be below zero if an iterator is used on a snapshot more than
1299 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1300 v_add(config
, v_read(config
,
1301 &bufb
->array
[read_sb_bindex
]->records_unread
),
1302 &bufb
->records_read
);
1303 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1304 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1305 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1306 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1309 * Exchange the reader subbuffer with the one we put in its place in the
1310 * writer subbuffer table. Expect the original consumed count. If
1311 * update_read_sb_index fails, this is because the writer updated the
1312 * subbuffer concurrently. We should therefore keep the subbuffer we
1313 * currently have: it has become invalid to try reading this sub-buffer
1314 * consumed count value anyway.
1316 consumed_idx
= subbuf_index(consumed
, chan
);
1317 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1318 consumed_idx
, buf_trunc_val(consumed
, chan
));
1320 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1321 * if the writer concurrently updated it.
1324 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1327 * cons_offset is an iterator on all subbuffer offsets between the reader
1328 * position and the writer position. (inclusive)
1331 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1332 struct channel
*chan
,
1333 unsigned long cons_offset
,
1336 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1337 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1339 cons_idx
= subbuf_index(cons_offset
, chan
);
1340 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1341 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1343 if (subbuf_offset(commit_count
, chan
) != 0)
1345 "ring buffer %s, cpu %d: "
1346 "commit count in subbuffer %lu,\n"
1347 "expecting multiples of %lu bytes\n"
1348 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1349 chan
->backend
.name
, cpu
, cons_idx
,
1350 chan
->backend
.subbuf_size
,
1351 commit_count
, commit_count_sb
);
1353 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1354 chan
->backend
.name
, cpu
, commit_count
);
1358 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1359 struct channel
*chan
,
1360 void *priv
, int cpu
)
1362 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1363 unsigned long write_offset
, cons_offset
;
1366 * No need to order commit_count, write_offset and cons_offset reads
1367 * because we execute at teardown when no more writer nor reader
1368 * references are left.
1370 write_offset
= v_read(config
, &buf
->offset
);
1371 cons_offset
= atomic_long_read(&buf
->consumed
);
1372 if (write_offset
!= cons_offset
)
1374 "ring buffer %s, cpu %d: "
1375 "non-consumed data\n"
1376 " [ %lu bytes written, %lu bytes read ]\n",
1377 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1379 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1380 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1383 cons_offset
= subbuf_align(cons_offset
, chan
))
1384 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1389 void lib_ring_buffer_print_errors(struct channel
*chan
,
1390 struct lib_ring_buffer
*buf
, int cpu
)
1392 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1393 void *priv
= chan
->backend
.priv
;
1395 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1396 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1397 "%lu records overrun\n",
1399 v_read(config
, &buf
->records_count
),
1400 v_read(config
, &buf
->records_overrun
));
1402 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1403 "%lu records overrun\n",
1404 chan
->backend
.name
, cpu
,
1405 v_read(config
, &buf
->records_count
),
1406 v_read(config
, &buf
->records_overrun
));
1408 if (v_read(config
, &buf
->records_lost_full
)
1409 || v_read(config
, &buf
->records_lost_wrap
)
1410 || v_read(config
, &buf
->records_lost_big
))
1412 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1413 " [ %lu buffer full, %lu nest buffer wrap-around, "
1414 "%lu event too big ]\n",
1415 chan
->backend
.name
, cpu
,
1416 v_read(config
, &buf
->records_lost_full
),
1417 v_read(config
, &buf
->records_lost_wrap
),
1418 v_read(config
, &buf
->records_lost_big
));
1420 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1424 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1426 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1429 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1430 struct channel
*chan
,
1431 struct switch_offsets
*offsets
,
1434 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1435 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1436 unsigned long commit_count
;
1438 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1441 * Order all writes to buffer before the commit count update that will
1442 * determine that the subbuffer is full.
1444 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1446 * Must write slot data before incrementing commit count. This
1447 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1453 v_add(config
, config
->cb
.subbuffer_header_size(),
1454 &buf
->commit_hot
[oldidx
].cc
);
1455 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1456 /* Check if the written buffer has to be delivered */
1457 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1458 commit_count
, oldidx
, tsc
);
1459 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1460 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1465 * lib_ring_buffer_switch_old_end: switch old subbuffer
1467 * Note : offset_old should never be 0 here. It is ok, because we never perform
1468 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1469 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1473 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1474 struct channel
*chan
,
1475 struct switch_offsets
*offsets
,
1478 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1479 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1480 unsigned long commit_count
, padding_size
, data_size
;
1482 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1483 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1484 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1487 * Order all writes to buffer before the commit count update that will
1488 * determine that the subbuffer is full.
1490 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1492 * Must write slot data before incrementing commit count. This
1493 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1499 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1500 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1501 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1502 commit_count
, oldidx
, tsc
);
1503 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1504 offsets
->old
+ padding_size
, commit_count
);
1508 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1510 * This code can be executed unordered : writers may already have written to the
1511 * sub-buffer before this code gets executed, caution. The commit makes sure
1512 * that this code is executed before the deliver of this sub-buffer.
1515 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1516 struct channel
*chan
,
1517 struct switch_offsets
*offsets
,
1520 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1521 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1522 unsigned long commit_count
;
1524 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1527 * Order all writes to buffer before the commit count update that will
1528 * determine that the subbuffer is full.
1530 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1532 * Must write slot data before incrementing commit count. This
1533 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1539 v_add(config
, config
->cb
.subbuffer_header_size(),
1540 &buf
->commit_hot
[beginidx
].cc
);
1541 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1542 /* Check if the written buffer has to be delivered */
1543 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1544 commit_count
, beginidx
, tsc
);
1545 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1546 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1551 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1553 * Calls subbuffer_set_data_size() to set the data size of the current
1554 * sub-buffer. We do not need to perform check_deliver nor commit here,
1555 * since this task will be done by the "commit" of the event for which
1556 * we are currently doing the space reservation.
1559 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1560 struct channel
*chan
,
1561 struct switch_offsets
*offsets
,
1564 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1565 unsigned long endidx
, data_size
;
1567 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1568 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1569 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1575 * !0 if execution must be aborted.
1578 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1579 struct lib_ring_buffer
*buf
,
1580 struct channel
*chan
,
1581 struct switch_offsets
*offsets
,
1584 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1585 unsigned long off
, reserve_commit_diff
;
1587 offsets
->begin
= v_read(config
, &buf
->offset
);
1588 offsets
->old
= offsets
->begin
;
1589 offsets
->switch_old_start
= 0;
1590 off
= subbuf_offset(offsets
->begin
, chan
);
1592 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1595 * Ensure we flush the header of an empty subbuffer when doing the
1596 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1597 * total data gathering duration even if there were no records saved
1598 * after the last buffer switch.
1599 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1600 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1601 * subbuffer header as appropriate.
1602 * The next record that reserves space will be responsible for
1603 * populating the following subbuffer header. We choose not to populate
1604 * the next subbuffer header here because we want to be able to use
1605 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1606 * buffer flush, which must guarantee that all the buffer content
1607 * (records and header timestamps) are visible to the reader. This is
1608 * required for quiescence guarantees for the fusion merge.
1610 if (mode
!= SWITCH_FLUSH
&& !off
)
1611 return -1; /* we do not have to switch : buffer is empty */
1613 if (unlikely(off
== 0)) {
1614 unsigned long sb_index
, commit_count
;
1617 * We are performing a SWITCH_FLUSH. At this stage, there are no
1618 * concurrent writes into the buffer.
1620 * The client does not save any header information. Don't
1621 * switch empty subbuffer on finalize, because it is invalid to
1622 * deliver a completely empty subbuffer.
1624 if (!config
->cb
.subbuffer_header_size())
1627 /* Test new buffer integrity */
1628 sb_index
= subbuf_index(offsets
->begin
, chan
);
1629 commit_count
= v_read(config
,
1630 &buf
->commit_cold
[sb_index
].cc_sb
);
1631 reserve_commit_diff
=
1632 (buf_trunc(offsets
->begin
, chan
)
1633 >> chan
->backend
.num_subbuf_order
)
1634 - (commit_count
& chan
->commit_count_mask
);
1635 if (likely(reserve_commit_diff
== 0)) {
1636 /* Next subbuffer not being written to. */
1637 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1638 subbuf_trunc(offsets
->begin
, chan
)
1639 - subbuf_trunc((unsigned long)
1640 atomic_long_read(&buf
->consumed
), chan
)
1641 >= chan
->backend
.buf_size
)) {
1643 * We do not overwrite non consumed buffers
1644 * and we are full : don't switch.
1649 * Next subbuffer not being written to, and we
1650 * are either in overwrite mode or the buffer is
1651 * not full. It's safe to write in this new
1657 * Next subbuffer reserve offset does not match the
1658 * commit offset. Don't perform switch in
1659 * producer-consumer and overwrite mode. Caused by
1660 * either a writer OOPS or too many nested writes over a
1661 * reserve/commit pair.
1667 * Need to write the subbuffer start header on finalize.
1669 offsets
->switch_old_start
= 1;
1671 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1672 /* Note: old points to the next subbuf at offset 0 */
1673 offsets
->end
= offsets
->begin
;
1678 * Force a sub-buffer switch. This operation is completely reentrant : can be
1679 * called while tracing is active with absolutely no lock held.
1681 * Note, however, that as a v_cmpxchg is used for some atomic
1682 * operations, this function must be called from the CPU which owns the buffer
1683 * for a ACTIVE flush.
1685 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1687 struct channel
*chan
= buf
->backend
.chan
;
1688 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1689 struct switch_offsets offsets
;
1690 unsigned long oldidx
;
1696 * Perform retryable operations.
1699 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1701 return; /* Switch not needed */
1702 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1706 * Atomically update last_tsc. This update races against concurrent
1707 * atomic updates, but the race will always cause supplementary full TSC
1708 * records, never the opposite (missing a full TSC record when it would
1711 save_last_tsc(config
, buf
, tsc
);
1714 * Push the reader if necessary
1716 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1718 oldidx
= subbuf_index(offsets
.old
, chan
);
1719 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1722 * May need to populate header start on SWITCH_FLUSH.
1724 if (offsets
.switch_old_start
) {
1725 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1726 offsets
.old
+= config
->cb
.subbuffer_header_size();
1730 * Switch old subbuffer.
1732 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1734 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1736 struct switch_param
{
1737 struct lib_ring_buffer
*buf
;
1738 enum switch_mode mode
;
1741 static void remote_switch(void *info
)
1743 struct switch_param
*param
= info
;
1744 struct lib_ring_buffer
*buf
= param
->buf
;
1746 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1749 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1750 enum switch_mode mode
)
1752 struct channel
*chan
= buf
->backend
.chan
;
1753 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1755 struct switch_param param
;
1758 * With global synchronization we don't need to use the IPI scheme.
1760 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1761 lib_ring_buffer_switch_slow(buf
, mode
);
1766 * Taking lock on CPU hotplug to ensure two things: first, that the
1767 * target cpu is not taken concurrently offline while we are within
1768 * smp_call_function_single() (I don't trust that get_cpu() on the
1769 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1770 * confirmed)). Secondly, if it happens that the CPU is not online, our
1771 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1772 * CPU hotplug handlers, which can also perform a remote subbuffer
1778 ret
= smp_call_function_single(buf
->backend
.cpu
,
1779 remote_switch
, ¶m
, 1);
1781 /* Remote CPU is offline, do it ourself. */
1782 lib_ring_buffer_switch_slow(buf
, mode
);
1787 /* Switch sub-buffer if current sub-buffer is non-empty. */
1788 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1790 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1792 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1794 /* Switch sub-buffer even if current sub-buffer is empty. */
1795 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1797 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1799 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1804 * -ENOSPC if event size is too large for packet.
1805 * -ENOBUFS if there is currently not enough space in buffer for the event.
1806 * -EIO if data cannot be written into the buffer for any other reason.
1809 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1810 struct channel
*chan
,
1811 struct switch_offsets
*offsets
,
1812 struct lib_ring_buffer_ctx
*ctx
)
1814 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1815 unsigned long reserve_commit_diff
, offset_cmp
;
1818 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1819 offsets
->old
= offsets
->begin
;
1820 offsets
->switch_new_start
= 0;
1821 offsets
->switch_new_end
= 0;
1822 offsets
->switch_old_end
= 0;
1823 offsets
->pre_header_padding
= 0;
1825 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1826 if ((int64_t) ctx
->tsc
== -EIO
)
1829 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1830 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1832 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1833 offsets
->switch_new_start
= 1; /* For offsets->begin */
1835 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1837 &offsets
->pre_header_padding
,
1840 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1843 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1844 offsets
->size
> chan
->backend
.subbuf_size
)) {
1845 offsets
->switch_old_end
= 1; /* For offsets->old */
1846 offsets
->switch_new_start
= 1; /* For offsets->begin */
1849 if (unlikely(offsets
->switch_new_start
)) {
1850 unsigned long sb_index
, commit_count
;
1853 * We are typically not filling the previous buffer completely.
1855 if (likely(offsets
->switch_old_end
))
1856 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1857 offsets
->begin
= offsets
->begin
1858 + config
->cb
.subbuffer_header_size();
1859 /* Test new buffer integrity */
1860 sb_index
= subbuf_index(offsets
->begin
, chan
);
1862 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1863 * lib_ring_buffer_check_deliver() has the matching
1864 * memory barriers required around commit_cold cc_sb
1865 * updates to ensure reserve and commit counter updates
1866 * are not seen reordered when updated by another CPU.
1869 commit_count
= v_read(config
,
1870 &buf
->commit_cold
[sb_index
].cc_sb
);
1871 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1873 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1875 * The reserve counter have been concurrently updated
1876 * while we read the commit counter. This means the
1877 * commit counter we read might not match buf->offset
1878 * due to concurrent update. We therefore need to retry.
1882 reserve_commit_diff
=
1883 (buf_trunc(offsets
->begin
, chan
)
1884 >> chan
->backend
.num_subbuf_order
)
1885 - (commit_count
& chan
->commit_count_mask
);
1886 if (likely(reserve_commit_diff
== 0)) {
1887 /* Next subbuffer not being written to. */
1888 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1889 subbuf_trunc(offsets
->begin
, chan
)
1890 - subbuf_trunc((unsigned long)
1891 atomic_long_read(&buf
->consumed
), chan
)
1892 >= chan
->backend
.buf_size
)) {
1894 * We do not overwrite non consumed buffers
1895 * and we are full : record is lost.
1897 v_inc(config
, &buf
->records_lost_full
);
1901 * Next subbuffer not being written to, and we
1902 * are either in overwrite mode or the buffer is
1903 * not full. It's safe to write in this new
1909 * Next subbuffer reserve offset does not match the
1910 * commit offset, and this did not involve update to the
1911 * reserve counter. Drop record in producer-consumer and
1912 * overwrite mode. Caused by either a writer OOPS or
1913 * too many nested writes over a reserve/commit pair.
1915 v_inc(config
, &buf
->records_lost_wrap
);
1919 config
->cb
.record_header_size(config
, chan
,
1921 &offsets
->pre_header_padding
,
1924 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1927 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1928 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1930 * Record too big for subbuffers, report error, don't
1931 * complete the sub-buffer switch.
1933 v_inc(config
, &buf
->records_lost_big
);
1937 * We just made a successful buffer switch and the
1938 * record fits in the new subbuffer. Let's write.
1943 * Record fits in the current buffer and we are not on a switch
1944 * boundary. It's safe to write.
1947 offsets
->end
= offsets
->begin
+ offsets
->size
;
1949 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1951 * The offset_end will fall at the very beginning of the next
1954 offsets
->switch_new_end
= 1; /* For offsets->begin */
1960 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1961 * @ctx: ring buffer context.
1963 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1964 * -EIO for other errors, else returns 0.
1965 * It will take care of sub-buffer switching.
1967 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1969 struct channel
*chan
= ctx
->chan
;
1970 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1971 struct lib_ring_buffer
*buf
;
1972 struct switch_offsets offsets
;
1975 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1976 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1978 buf
= chan
->backend
.buf
;
1984 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1988 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1993 * Atomically update last_tsc. This update races against concurrent
1994 * atomic updates, but the race will always cause supplementary full TSC
1995 * records, never the opposite (missing a full TSC record when it would
1998 save_last_tsc(config
, buf
, ctx
->tsc
);
2001 * Push the reader if necessary
2003 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2006 * Clear noref flag for this subbuffer.
2008 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2009 subbuf_index(offsets
.end
- 1, chan
));
2012 * Switch old subbuffer if needed.
2014 if (unlikely(offsets
.switch_old_end
)) {
2015 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2016 subbuf_index(offsets
.old
- 1, chan
));
2017 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2021 * Populate new subbuffer.
2023 if (unlikely(offsets
.switch_new_start
))
2024 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2026 if (unlikely(offsets
.switch_new_end
))
2027 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2029 ctx
->slot_size
= offsets
.size
;
2030 ctx
->pre_offset
= offsets
.begin
;
2031 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2034 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2036 int __init
init_lib_ring_buffer_frontend(void)
2040 for_each_possible_cpu(cpu
)
2041 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2045 module_init(init_lib_ring_buffer_frontend
);
2047 void __exit
exit_lib_ring_buffer_frontend(void)
2051 module_exit(exit_lib_ring_buffer_frontend
);