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 #ifdef CONFIG_HOTPLUG_CPU
415 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
416 * @nb: notifier block
417 * @action: hotplug action to take
420 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
423 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
424 unsigned long action
,
427 unsigned int cpu
= (unsigned long)hcpu
;
428 struct channel
*chan
= container_of(nb
, struct channel
,
430 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
431 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
433 if (!chan
->cpu_hp_enable
)
436 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
439 case CPU_DOWN_FAILED
:
440 case CPU_DOWN_FAILED_FROZEN
:
442 case CPU_ONLINE_FROZEN
:
443 wake_up_interruptible(&chan
->hp_wait
);
444 lib_ring_buffer_start_switch_timer(buf
);
445 lib_ring_buffer_start_read_timer(buf
);
448 case CPU_DOWN_PREPARE
:
449 case CPU_DOWN_PREPARE_FROZEN
:
450 lib_ring_buffer_stop_switch_timer(buf
);
451 lib_ring_buffer_stop_read_timer(buf
);
455 case CPU_DEAD_FROZEN
:
457 * Performing a buffer switch on a remote CPU. Performed by
458 * the CPU responsible for doing the hotunplug after the target
459 * CPU stopped running completely. Ensures that all data
460 * from that remote CPU is flushed.
462 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
471 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
473 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
474 * that wake-up-tracing generated events are flushed before going idle (in
475 * tick_nohz). We test if the spinlock is locked to deal with the race where
476 * readers try to sample the ring buffer before we perform the switch. We let
477 * the readers retry in that case. If there is data in the buffer, the wake up
478 * is going to forbid the CPU running the reader thread from going idle.
480 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
484 struct channel
*chan
= container_of(nb
, struct channel
,
486 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
487 struct lib_ring_buffer
*buf
;
488 int cpu
= smp_processor_id();
490 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
492 * We don't support keeping the system idle with global buffers
493 * and streaming active. In order to do so, we would need to
494 * sample a non-nohz-cpumask racelessly with the nohz updates
495 * without adding synchronization overhead to nohz. Leave this
496 * use-case out for now.
501 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
503 case TICK_NOHZ_FLUSH
:
504 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
505 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
506 && chan
->read_timer_interval
507 && atomic_long_read(&buf
->active_readers
)
508 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
509 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
510 wake_up_interruptible(&buf
->read_wait
);
511 wake_up_interruptible(&chan
->read_wait
);
513 if (chan
->switch_timer_interval
)
514 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
515 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
518 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
519 lib_ring_buffer_stop_switch_timer(buf
);
520 lib_ring_buffer_stop_read_timer(buf
);
521 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
523 case TICK_NOHZ_RESTART
:
524 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
525 lib_ring_buffer_start_read_timer(buf
);
526 lib_ring_buffer_start_switch_timer(buf
);
527 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
534 void notrace
lib_ring_buffer_tick_nohz_flush(void)
536 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
540 void notrace
lib_ring_buffer_tick_nohz_stop(void)
542 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
546 void notrace
lib_ring_buffer_tick_nohz_restart(void)
548 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
551 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
556 static void channel_unregister_notifiers(struct channel
*chan
)
558 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
561 channel_iterator_unregister_notifiers(chan
);
562 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
565 * Remove the nohz notifier first, so we are certain we stop
568 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
569 &chan
->tick_nohz_notifier
);
571 * ring_buffer_nohz_lock will not be needed below, because
572 * we just removed the notifiers, which were the only source of
575 #endif /* CONFIG_NO_HZ */
576 #ifdef CONFIG_HOTPLUG_CPU
578 chan
->cpu_hp_enable
= 0;
579 for_each_online_cpu(cpu
) {
580 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
582 lib_ring_buffer_stop_switch_timer(buf
);
583 lib_ring_buffer_stop_read_timer(buf
);
586 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
588 for_each_possible_cpu(cpu
) {
589 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
591 lib_ring_buffer_stop_switch_timer(buf
);
592 lib_ring_buffer_stop_read_timer(buf
);
596 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
598 lib_ring_buffer_stop_switch_timer(buf
);
599 lib_ring_buffer_stop_read_timer(buf
);
601 channel_backend_unregister_notifiers(&chan
->backend
);
604 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
606 if (!buf
->quiescent
) {
607 buf
->quiescent
= true;
608 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
612 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
614 buf
->quiescent
= false;
617 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
620 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
622 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
624 for_each_channel_cpu(cpu
, chan
) {
625 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
628 lib_ring_buffer_set_quiescent(buf
);
632 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
634 lib_ring_buffer_set_quiescent(buf
);
637 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
639 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
642 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
644 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
646 for_each_channel_cpu(cpu
, chan
) {
647 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
650 lib_ring_buffer_clear_quiescent(buf
);
654 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
656 lib_ring_buffer_clear_quiescent(buf
);
659 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
661 static void channel_free(struct channel
*chan
)
663 if (chan
->backend
.release_priv_ops
) {
664 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
666 channel_iterator_free(chan
);
667 channel_backend_free(&chan
->backend
);
672 * channel_create - Create channel.
673 * @config: ring buffer instance configuration
674 * @name: name of the channel
675 * @priv: ring buffer client private data
676 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
677 * address mapping. It is used only by RING_BUFFER_STATIC
678 * configuration. It can be set to NULL for other backends.
679 * @subbuf_size: subbuffer size
680 * @num_subbuf: number of subbuffers
681 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
682 * padding to let readers get those sub-buffers.
683 * Used for live streaming.
684 * @read_timer_interval: Time interval (in us) to wake up pending readers.
687 * Returns NULL on failure.
689 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
690 const char *name
, void *priv
, void *buf_addr
,
692 size_t num_subbuf
, unsigned int switch_timer_interval
,
693 unsigned int read_timer_interval
)
696 struct channel
*chan
;
698 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
699 read_timer_interval
))
702 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
706 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
707 subbuf_size
, num_subbuf
);
711 ret
= channel_iterator_init(chan
);
713 goto error_free_backend
;
715 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
716 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
717 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
718 kref_init(&chan
->ref
);
719 init_waitqueue_head(&chan
->read_wait
);
720 init_waitqueue_head(&chan
->hp_wait
);
722 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
723 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
724 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
725 chan
->tick_nohz_notifier
.notifier_call
=
726 ring_buffer_tick_nohz_callback
;
727 chan
->tick_nohz_notifier
.priority
= ~0U;
728 atomic_notifier_chain_register(&tick_nohz_notifier
,
729 &chan
->tick_nohz_notifier
);
730 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
733 * In case of non-hotplug cpu, if the ring-buffer is allocated
734 * in early initcall, it will not be notified of secondary cpus.
735 * In that off case, we need to allocate for all possible cpus.
737 #ifdef CONFIG_HOTPLUG_CPU
738 chan
->cpu_hp_notifier
.notifier_call
=
739 lib_ring_buffer_cpu_hp_callback
;
740 chan
->cpu_hp_notifier
.priority
= 6;
741 register_cpu_notifier(&chan
->cpu_hp_notifier
);
744 for_each_online_cpu(cpu
) {
745 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
747 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
748 lib_ring_buffer_start_switch_timer(buf
);
749 lib_ring_buffer_start_read_timer(buf
);
750 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
752 chan
->cpu_hp_enable
= 1;
755 for_each_possible_cpu(cpu
) {
756 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
758 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
759 lib_ring_buffer_start_switch_timer(buf
);
760 lib_ring_buffer_start_read_timer(buf
);
761 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
765 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
767 lib_ring_buffer_start_switch_timer(buf
);
768 lib_ring_buffer_start_read_timer(buf
);
774 channel_backend_free(&chan
->backend
);
779 EXPORT_SYMBOL_GPL(channel_create
);
782 void channel_release(struct kref
*kref
)
784 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
789 * channel_destroy - Finalize, wait for q.s. and destroy channel.
790 * @chan: channel to destroy
793 * Call "destroy" callback, finalize channels, and then decrement the
794 * channel reference count. Note that when readers have completed data
795 * consumption of finalized channels, get_subbuf() will return -ENODATA.
796 * They should release their handle at that point. Returns the private
799 void *channel_destroy(struct channel
*chan
)
802 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
805 channel_unregister_notifiers(chan
);
807 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
809 * No need to hold cpu hotplug, because all notifiers have been
812 for_each_channel_cpu(cpu
, chan
) {
813 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
816 if (config
->cb
.buffer_finalize
)
817 config
->cb
.buffer_finalize(buf
,
820 if (buf
->backend
.allocated
)
821 lib_ring_buffer_set_quiescent(buf
);
823 * Perform flush before writing to finalized.
826 ACCESS_ONCE(buf
->finalized
) = 1;
827 wake_up_interruptible(&buf
->read_wait
);
830 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
832 if (config
->cb
.buffer_finalize
)
833 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
834 if (buf
->backend
.allocated
)
835 lib_ring_buffer_set_quiescent(buf
);
837 * Perform flush before writing to finalized.
840 ACCESS_ONCE(buf
->finalized
) = 1;
841 wake_up_interruptible(&buf
->read_wait
);
843 ACCESS_ONCE(chan
->finalized
) = 1;
844 wake_up_interruptible(&chan
->hp_wait
);
845 wake_up_interruptible(&chan
->read_wait
);
846 priv
= chan
->backend
.priv
;
847 kref_put(&chan
->ref
, channel_release
);
850 EXPORT_SYMBOL_GPL(channel_destroy
);
852 struct lib_ring_buffer
*channel_get_ring_buffer(
853 const struct lib_ring_buffer_config
*config
,
854 struct channel
*chan
, int cpu
)
856 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
857 return chan
->backend
.buf
;
859 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
861 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
863 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
865 struct channel
*chan
= buf
->backend
.chan
;
867 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
869 if (!lttng_kref_get(&chan
->ref
)) {
870 atomic_long_dec(&buf
->active_readers
);
873 lttng_smp_mb__after_atomic();
876 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
878 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
880 struct channel
*chan
= buf
->backend
.chan
;
882 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
883 lttng_smp_mb__before_atomic();
884 atomic_long_dec(&buf
->active_readers
);
885 kref_put(&chan
->ref
, channel_release
);
887 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
890 * Promote compiler barrier to a smp_mb().
891 * For the specific ring buffer case, this IPI call should be removed if the
892 * architecture does not reorder writes. This should eventually be provided by
893 * a separate architecture-specific infrastructure.
895 static void remote_mb(void *info
)
901 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
903 * @consumed: consumed count indicating the position where to read
904 * @produced: produced count, indicates position when to stop reading
906 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
907 * data to read at consumed position, or 0 if the get operation succeeds.
908 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
911 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
912 unsigned long *consumed
, unsigned long *produced
)
914 struct channel
*chan
= buf
->backend
.chan
;
915 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
916 unsigned long consumed_cur
, write_offset
;
920 finalized
= ACCESS_ONCE(buf
->finalized
);
922 * Read finalized before counters.
925 consumed_cur
= atomic_long_read(&buf
->consumed
);
927 * No need to issue a memory barrier between consumed count read and
928 * write offset read, because consumed count can only change
929 * concurrently in overwrite mode, and we keep a sequence counter
930 * identifier derived from the write offset to check we are getting
931 * the same sub-buffer we are expecting (the sub-buffers are atomically
932 * "tagged" upon writes, tags are checked upon read).
934 write_offset
= v_read(config
, &buf
->offset
);
937 * Check that we are not about to read the same subbuffer in
938 * which the writer head is.
940 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
944 *consumed
= consumed_cur
;
945 *produced
= subbuf_trunc(write_offset
, chan
);
951 * The memory barriers __wait_event()/wake_up_interruptible() take care
952 * of "raw_spin_is_locked" memory ordering.
956 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
961 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
964 * lib_ring_buffer_put_snapshot - move consumed counter forward
966 * Should only be called from consumer context.
968 * @consumed_new: new consumed count value
970 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
971 unsigned long consumed_new
)
973 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
974 struct channel
*chan
= bufb
->chan
;
975 unsigned long consumed
;
977 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
980 * Only push the consumed value forward.
981 * If the consumed cmpxchg fails, this is because we have been pushed by
982 * the writer in flight recorder mode.
984 consumed
= atomic_long_read(&buf
->consumed
);
985 while ((long) consumed
- (long) consumed_new
< 0)
986 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
988 /* Wake-up the metadata producer */
989 wake_up_interruptible(&buf
->write_wait
);
991 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
994 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
996 * @consumed: consumed count indicating the position where to read
998 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
999 * data to read at consumed position, or 0 if the get operation succeeds.
1000 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1002 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1003 unsigned long consumed
)
1005 struct channel
*chan
= buf
->backend
.chan
;
1006 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1007 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1011 if (buf
->get_subbuf
) {
1013 * Reader is trying to get a subbuffer twice.
1015 CHAN_WARN_ON(chan
, 1);
1019 finalized
= ACCESS_ONCE(buf
->finalized
);
1021 * Read finalized before counters.
1024 consumed_cur
= atomic_long_read(&buf
->consumed
);
1025 consumed_idx
= subbuf_index(consumed
, chan
);
1026 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1028 * Make sure we read the commit count before reading the buffer
1029 * data and the write offset. Correct consumed offset ordering
1030 * wrt commit count is insured by the use of cmpxchg to update
1031 * the consumed offset.
1032 * smp_call_function_single can fail if the remote CPU is offline,
1033 * this is OK because then there is no wmb to execute there.
1034 * If our thread is executing on the same CPU as the on the buffers
1035 * belongs to, we don't have to synchronize it at all. If we are
1036 * migrated, the scheduler will take care of the memory barriers.
1037 * Normally, smp_call_function_single() should ensure program order when
1038 * executing the remote function, which implies that it surrounds the
1039 * function execution with :
1050 * However, smp_call_function_single() does not seem to clearly execute
1051 * such barriers. It depends on spinlock semantic to provide the barrier
1052 * before executing the IPI and, when busy-looping, csd_lock_wait only
1053 * executes smp_mb() when it has to wait for the other CPU.
1055 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1056 * required ourself, even if duplicated. It has no performance impact
1059 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1060 * read and write vs write. They do not ensure core synchronization. We
1061 * really have to ensure total order between the 3 barriers running on
1064 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1065 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1066 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1067 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1068 /* Total order with IPI handler smp_mb() */
1070 smp_call_function_single(buf
->backend
.cpu
,
1071 remote_mb
, NULL
, 1);
1072 /* Total order with IPI handler smp_mb() */
1076 /* Total order with IPI handler smp_mb() */
1078 smp_call_function(remote_mb
, NULL
, 1);
1079 /* Total order with IPI handler smp_mb() */
1084 * Local rmb to match the remote wmb to read the commit count
1085 * before the buffer data and the write offset.
1090 write_offset
= v_read(config
, &buf
->offset
);
1093 * Check that the buffer we are getting is after or at consumed_cur
1096 if ((long) subbuf_trunc(consumed
, chan
)
1097 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1101 * Check that the subbuffer we are trying to consume has been
1102 * already fully committed.
1104 if (((commit_count
- chan
->backend
.subbuf_size
)
1105 & chan
->commit_count_mask
)
1106 - (buf_trunc(consumed
, chan
)
1107 >> chan
->backend
.num_subbuf_order
)
1112 * Check that we are not about to read the same subbuffer in
1113 * which the writer head is.
1115 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1120 * Failure to get the subbuffer causes a busy-loop retry without going
1121 * to a wait queue. These are caused by short-lived race windows where
1122 * the writer is getting access to a subbuffer we were trying to get
1123 * access to. Also checks that the "consumed" buffer count we are
1124 * looking for matches the one contained in the subbuffer id.
1126 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1127 consumed_idx
, buf_trunc_val(consumed
, chan
));
1130 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1132 buf
->get_subbuf_consumed
= consumed
;
1133 buf
->get_subbuf
= 1;
1139 * The memory barriers __wait_event()/wake_up_interruptible() take care
1140 * of "raw_spin_is_locked" memory ordering.
1144 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1149 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1152 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1155 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1157 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1158 struct channel
*chan
= bufb
->chan
;
1159 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1160 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1162 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1164 if (!buf
->get_subbuf
) {
1166 * Reader puts a subbuffer it did not get.
1168 CHAN_WARN_ON(chan
, 1);
1171 consumed
= buf
->get_subbuf_consumed
;
1172 buf
->get_subbuf
= 0;
1175 * Clear the records_unread counter. (overruns counter)
1176 * Can still be non-zero if a file reader simply grabbed the data
1177 * without using iterators.
1178 * Can be below zero if an iterator is used on a snapshot more than
1181 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1182 v_add(config
, v_read(config
,
1183 &bufb
->array
[read_sb_bindex
]->records_unread
),
1184 &bufb
->records_read
);
1185 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1186 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1187 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1188 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1191 * Exchange the reader subbuffer with the one we put in its place in the
1192 * writer subbuffer table. Expect the original consumed count. If
1193 * update_read_sb_index fails, this is because the writer updated the
1194 * subbuffer concurrently. We should therefore keep the subbuffer we
1195 * currently have: it has become invalid to try reading this sub-buffer
1196 * consumed count value anyway.
1198 consumed_idx
= subbuf_index(consumed
, chan
);
1199 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1200 consumed_idx
, buf_trunc_val(consumed
, chan
));
1202 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1203 * if the writer concurrently updated it.
1206 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1209 * cons_offset is an iterator on all subbuffer offsets between the reader
1210 * position and the writer position. (inclusive)
1213 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1214 struct channel
*chan
,
1215 unsigned long cons_offset
,
1218 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1219 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1221 cons_idx
= subbuf_index(cons_offset
, chan
);
1222 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1223 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1225 if (subbuf_offset(commit_count
, chan
) != 0)
1227 "ring buffer %s, cpu %d: "
1228 "commit count in subbuffer %lu,\n"
1229 "expecting multiples of %lu bytes\n"
1230 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1231 chan
->backend
.name
, cpu
, cons_idx
,
1232 chan
->backend
.subbuf_size
,
1233 commit_count
, commit_count_sb
);
1235 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1236 chan
->backend
.name
, cpu
, commit_count
);
1240 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1241 struct channel
*chan
,
1242 void *priv
, int cpu
)
1244 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1245 unsigned long write_offset
, cons_offset
;
1248 * No need to order commit_count, write_offset and cons_offset reads
1249 * because we execute at teardown when no more writer nor reader
1250 * references are left.
1252 write_offset
= v_read(config
, &buf
->offset
);
1253 cons_offset
= atomic_long_read(&buf
->consumed
);
1254 if (write_offset
!= cons_offset
)
1256 "ring buffer %s, cpu %d: "
1257 "non-consumed data\n"
1258 " [ %lu bytes written, %lu bytes read ]\n",
1259 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1261 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1262 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1265 cons_offset
= subbuf_align(cons_offset
, chan
))
1266 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1271 void lib_ring_buffer_print_errors(struct channel
*chan
,
1272 struct lib_ring_buffer
*buf
, int cpu
)
1274 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1275 void *priv
= chan
->backend
.priv
;
1277 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1278 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1279 "%lu records overrun\n",
1281 v_read(config
, &buf
->records_count
),
1282 v_read(config
, &buf
->records_overrun
));
1284 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1285 "%lu records overrun\n",
1286 chan
->backend
.name
, cpu
,
1287 v_read(config
, &buf
->records_count
),
1288 v_read(config
, &buf
->records_overrun
));
1290 if (v_read(config
, &buf
->records_lost_full
)
1291 || v_read(config
, &buf
->records_lost_wrap
)
1292 || v_read(config
, &buf
->records_lost_big
))
1294 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1295 " [ %lu buffer full, %lu nest buffer wrap-around, "
1296 "%lu event too big ]\n",
1297 chan
->backend
.name
, cpu
,
1298 v_read(config
, &buf
->records_lost_full
),
1299 v_read(config
, &buf
->records_lost_wrap
),
1300 v_read(config
, &buf
->records_lost_big
));
1302 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1306 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1308 * Only executed by SWITCH_FLUSH, which can be issued while tracing is active
1309 * or at buffer finalization (destroy).
1312 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1313 struct channel
*chan
,
1314 struct switch_offsets
*offsets
,
1317 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1318 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1319 unsigned long commit_count
;
1321 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1324 * Order all writes to buffer before the commit count update that will
1325 * determine that the subbuffer is full.
1327 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1329 * Must write slot data before incrementing commit count. This
1330 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1336 v_add(config
, config
->cb
.subbuffer_header_size(),
1337 &buf
->commit_hot
[oldidx
].cc
);
1338 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1339 /* Check if the written buffer has to be delivered */
1340 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1341 commit_count
, oldidx
, tsc
);
1342 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1343 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1348 * lib_ring_buffer_switch_old_end: switch old subbuffer
1350 * Note : offset_old should never be 0 here. It is ok, because we never perform
1351 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1352 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1356 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1357 struct channel
*chan
,
1358 struct switch_offsets
*offsets
,
1361 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1362 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1363 unsigned long commit_count
, padding_size
, data_size
;
1365 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1366 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1367 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1370 * Order all writes to buffer before the commit count update that will
1371 * determine that the subbuffer is full.
1373 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1375 * Must write slot data before incrementing commit count. This
1376 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1382 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1383 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1384 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1385 commit_count
, oldidx
, tsc
);
1386 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1387 offsets
->old
+ padding_size
, commit_count
);
1391 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1393 * This code can be executed unordered : writers may already have written to the
1394 * sub-buffer before this code gets executed, caution. The commit makes sure
1395 * that this code is executed before the deliver of this sub-buffer.
1398 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1399 struct channel
*chan
,
1400 struct switch_offsets
*offsets
,
1403 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1404 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1405 unsigned long commit_count
;
1407 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1410 * Order all writes to buffer before the commit count update that will
1411 * determine that the subbuffer is full.
1413 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1415 * Must write slot data before incrementing commit count. This
1416 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1422 v_add(config
, config
->cb
.subbuffer_header_size(),
1423 &buf
->commit_hot
[beginidx
].cc
);
1424 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1425 /* Check if the written buffer has to be delivered */
1426 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1427 commit_count
, beginidx
, tsc
);
1428 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1429 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1434 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1436 * Calls subbuffer_set_data_size() to set the data size of the current
1437 * sub-buffer. We do not need to perform check_deliver nor commit here,
1438 * since this task will be done by the "commit" of the event for which
1439 * we are currently doing the space reservation.
1442 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1443 struct channel
*chan
,
1444 struct switch_offsets
*offsets
,
1447 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1448 unsigned long endidx
, data_size
;
1450 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1451 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1452 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1458 * !0 if execution must be aborted.
1461 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1462 struct lib_ring_buffer
*buf
,
1463 struct channel
*chan
,
1464 struct switch_offsets
*offsets
,
1467 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1468 unsigned long off
, reserve_commit_diff
;
1470 offsets
->begin
= v_read(config
, &buf
->offset
);
1471 offsets
->old
= offsets
->begin
;
1472 offsets
->switch_old_start
= 0;
1473 off
= subbuf_offset(offsets
->begin
, chan
);
1475 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1478 * Ensure we flush the header of an empty subbuffer when doing the
1479 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1480 * total data gathering duration even if there were no records saved
1481 * after the last buffer switch.
1482 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1483 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1484 * subbuffer header as appropriate.
1485 * The next record that reserves space will be responsible for
1486 * populating the following subbuffer header. We choose not to populate
1487 * the next subbuffer header here because we want to be able to use
1488 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1489 * buffer flush, which must guarantee that all the buffer content
1490 * (records and header timestamps) are visible to the reader. This is
1491 * required for quiescence guarantees for the fusion merge.
1493 if (mode
!= SWITCH_FLUSH
&& !off
)
1494 return -1; /* we do not have to switch : buffer is empty */
1496 if (unlikely(off
== 0)) {
1497 unsigned long sb_index
, commit_count
;
1500 * We are performing a SWITCH_FLUSH. There may be concurrent
1501 * writes into the buffer if e.g. invoked while performing a
1502 * snapshot on an active trace.
1504 * If the client does not save any header information (sub-buffer
1505 * header size == 0), don't switch empty subbuffer on finalize,
1506 * because it is invalid to deliver a completely empty
1509 if (!config
->cb
.subbuffer_header_size())
1512 /* Test new buffer integrity */
1513 sb_index
= subbuf_index(offsets
->begin
, chan
);
1514 commit_count
= v_read(config
,
1515 &buf
->commit_cold
[sb_index
].cc_sb
);
1516 reserve_commit_diff
=
1517 (buf_trunc(offsets
->begin
, chan
)
1518 >> chan
->backend
.num_subbuf_order
)
1519 - (commit_count
& chan
->commit_count_mask
);
1520 if (likely(reserve_commit_diff
== 0)) {
1521 /* Next subbuffer not being written to. */
1522 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1523 subbuf_trunc(offsets
->begin
, chan
)
1524 - subbuf_trunc((unsigned long)
1525 atomic_long_read(&buf
->consumed
), chan
)
1526 >= chan
->backend
.buf_size
)) {
1528 * We do not overwrite non consumed buffers
1529 * and we are full : don't switch.
1534 * Next subbuffer not being written to, and we
1535 * are either in overwrite mode or the buffer is
1536 * not full. It's safe to write in this new
1542 * Next subbuffer reserve offset does not match the
1543 * commit offset. Don't perform switch in
1544 * producer-consumer and overwrite mode. Caused by
1545 * either a writer OOPS or too many nested writes over a
1546 * reserve/commit pair.
1552 * Need to write the subbuffer start header on finalize.
1554 offsets
->switch_old_start
= 1;
1556 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1557 /* Note: old points to the next subbuf at offset 0 */
1558 offsets
->end
= offsets
->begin
;
1563 * Force a sub-buffer switch. This operation is completely reentrant : can be
1564 * called while tracing is active with absolutely no lock held.
1566 * Note, however, that as a v_cmpxchg is used for some atomic
1567 * operations, this function must be called from the CPU which owns the buffer
1568 * for a ACTIVE flush.
1570 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1572 struct channel
*chan
= buf
->backend
.chan
;
1573 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1574 struct switch_offsets offsets
;
1575 unsigned long oldidx
;
1581 * Perform retryable operations.
1584 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1586 return; /* Switch not needed */
1587 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1591 * Atomically update last_tsc. This update races against concurrent
1592 * atomic updates, but the race will always cause supplementary full TSC
1593 * records, never the opposite (missing a full TSC record when it would
1596 save_last_tsc(config
, buf
, tsc
);
1599 * Push the reader if necessary
1601 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1603 oldidx
= subbuf_index(offsets
.old
, chan
);
1604 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1607 * May need to populate header start on SWITCH_FLUSH.
1609 if (offsets
.switch_old_start
) {
1610 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1611 offsets
.old
+= config
->cb
.subbuffer_header_size();
1615 * Switch old subbuffer.
1617 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1619 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1621 struct switch_param
{
1622 struct lib_ring_buffer
*buf
;
1623 enum switch_mode mode
;
1626 static void remote_switch(void *info
)
1628 struct switch_param
*param
= info
;
1629 struct lib_ring_buffer
*buf
= param
->buf
;
1631 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1634 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1635 enum switch_mode mode
)
1637 struct channel
*chan
= buf
->backend
.chan
;
1638 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1640 struct switch_param param
;
1643 * With global synchronization we don't need to use the IPI scheme.
1645 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1646 lib_ring_buffer_switch_slow(buf
, mode
);
1651 * Taking lock on CPU hotplug to ensure two things: first, that the
1652 * target cpu is not taken concurrently offline while we are within
1653 * smp_call_function_single() (I don't trust that get_cpu() on the
1654 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1655 * confirmed)). Secondly, if it happens that the CPU is not online, our
1656 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1657 * CPU hotplug handlers, which can also perform a remote subbuffer
1663 ret
= smp_call_function_single(buf
->backend
.cpu
,
1664 remote_switch
, ¶m
, 1);
1666 /* Remote CPU is offline, do it ourself. */
1667 lib_ring_buffer_switch_slow(buf
, mode
);
1672 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1674 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1676 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1678 /* Switch sub-buffer even if current sub-buffer is empty. */
1679 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1681 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1683 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1688 * -ENOSPC if event size is too large for packet.
1689 * -ENOBUFS if there is currently not enough space in buffer for the event.
1690 * -EIO if data cannot be written into the buffer for any other reason.
1693 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1694 struct channel
*chan
,
1695 struct switch_offsets
*offsets
,
1696 struct lib_ring_buffer_ctx
*ctx
)
1698 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1699 unsigned long reserve_commit_diff
, offset_cmp
;
1702 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1703 offsets
->old
= offsets
->begin
;
1704 offsets
->switch_new_start
= 0;
1705 offsets
->switch_new_end
= 0;
1706 offsets
->switch_old_end
= 0;
1707 offsets
->pre_header_padding
= 0;
1709 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1710 if ((int64_t) ctx
->tsc
== -EIO
)
1713 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1714 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1716 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1717 offsets
->switch_new_start
= 1; /* For offsets->begin */
1719 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1721 &offsets
->pre_header_padding
,
1724 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1727 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1728 offsets
->size
> chan
->backend
.subbuf_size
)) {
1729 offsets
->switch_old_end
= 1; /* For offsets->old */
1730 offsets
->switch_new_start
= 1; /* For offsets->begin */
1733 if (unlikely(offsets
->switch_new_start
)) {
1734 unsigned long sb_index
, commit_count
;
1737 * We are typically not filling the previous buffer completely.
1739 if (likely(offsets
->switch_old_end
))
1740 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1741 offsets
->begin
= offsets
->begin
1742 + config
->cb
.subbuffer_header_size();
1743 /* Test new buffer integrity */
1744 sb_index
= subbuf_index(offsets
->begin
, chan
);
1746 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1747 * lib_ring_buffer_check_deliver() has the matching
1748 * memory barriers required around commit_cold cc_sb
1749 * updates to ensure reserve and commit counter updates
1750 * are not seen reordered when updated by another CPU.
1753 commit_count
= v_read(config
,
1754 &buf
->commit_cold
[sb_index
].cc_sb
);
1755 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1757 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1759 * The reserve counter have been concurrently updated
1760 * while we read the commit counter. This means the
1761 * commit counter we read might not match buf->offset
1762 * due to concurrent update. We therefore need to retry.
1766 reserve_commit_diff
=
1767 (buf_trunc(offsets
->begin
, chan
)
1768 >> chan
->backend
.num_subbuf_order
)
1769 - (commit_count
& chan
->commit_count_mask
);
1770 if (likely(reserve_commit_diff
== 0)) {
1771 /* Next subbuffer not being written to. */
1772 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1773 subbuf_trunc(offsets
->begin
, chan
)
1774 - subbuf_trunc((unsigned long)
1775 atomic_long_read(&buf
->consumed
), chan
)
1776 >= chan
->backend
.buf_size
)) {
1778 * We do not overwrite non consumed buffers
1779 * and we are full : record is lost.
1781 v_inc(config
, &buf
->records_lost_full
);
1785 * Next subbuffer not being written to, and we
1786 * are either in overwrite mode or the buffer is
1787 * not full. It's safe to write in this new
1793 * Next subbuffer reserve offset does not match the
1794 * commit offset, and this did not involve update to the
1795 * reserve counter. Drop record in producer-consumer and
1796 * overwrite mode. Caused by either a writer OOPS or
1797 * too many nested writes over a reserve/commit pair.
1799 v_inc(config
, &buf
->records_lost_wrap
);
1803 config
->cb
.record_header_size(config
, chan
,
1805 &offsets
->pre_header_padding
,
1808 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1811 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1812 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1814 * Record too big for subbuffers, report error, don't
1815 * complete the sub-buffer switch.
1817 v_inc(config
, &buf
->records_lost_big
);
1821 * We just made a successful buffer switch and the
1822 * record fits in the new subbuffer. Let's write.
1827 * Record fits in the current buffer and we are not on a switch
1828 * boundary. It's safe to write.
1831 offsets
->end
= offsets
->begin
+ offsets
->size
;
1833 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1835 * The offset_end will fall at the very beginning of the next
1838 offsets
->switch_new_end
= 1; /* For offsets->begin */
1844 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1845 * @ctx: ring buffer context.
1847 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1848 * -EIO for other errors, else returns 0.
1849 * It will take care of sub-buffer switching.
1851 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1853 struct channel
*chan
= ctx
->chan
;
1854 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1855 struct lib_ring_buffer
*buf
;
1856 struct switch_offsets offsets
;
1859 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1860 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1862 buf
= chan
->backend
.buf
;
1868 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1872 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1877 * Atomically update last_tsc. This update races against concurrent
1878 * atomic updates, but the race will always cause supplementary full TSC
1879 * records, never the opposite (missing a full TSC record when it would
1882 save_last_tsc(config
, buf
, ctx
->tsc
);
1885 * Push the reader if necessary
1887 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1890 * Clear noref flag for this subbuffer.
1892 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1893 subbuf_index(offsets
.end
- 1, chan
));
1896 * Switch old subbuffer if needed.
1898 if (unlikely(offsets
.switch_old_end
)) {
1899 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1900 subbuf_index(offsets
.old
- 1, chan
));
1901 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1905 * Populate new subbuffer.
1907 if (unlikely(offsets
.switch_new_start
))
1908 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1910 if (unlikely(offsets
.switch_new_end
))
1911 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1913 ctx
->slot_size
= offsets
.size
;
1914 ctx
->pre_offset
= offsets
.begin
;
1915 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1918 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
1920 int __init
init_lib_ring_buffer_frontend(void)
1924 for_each_possible_cpu(cpu
)
1925 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
1929 module_init(init_lib_ring_buffer_frontend
);
1931 void __exit
exit_lib_ring_buffer_frontend(void)
1935 module_exit(exit_lib_ring_buffer_frontend
);