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
, cpu_to_node(max(cpu
, 0)));
210 if (!buf
->commit_hot
) {
216 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
217 * chan
->backend
.num_subbuf
,
218 1 << INTERNODE_CACHE_SHIFT
),
219 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
220 if (!buf
->commit_cold
) {
225 init_waitqueue_head(&buf
->read_wait
);
226 init_waitqueue_head(&buf
->write_wait
);
227 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
230 * Write the subbuffer header for first subbuffer so we know the total
231 * duration of data gathering.
233 subbuf_header_size
= config
->cb
.subbuffer_header_size();
234 v_set(config
, &buf
->offset
, subbuf_header_size
);
235 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
236 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
237 config
->cb
.buffer_begin(buf
, tsc
, 0);
238 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
240 if (config
->cb
.buffer_create
) {
241 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
247 * Ensure the buffer is ready before setting it to allocated and setting
249 * Used for cpu hotplug vs cpumask iteration.
252 buf
->backend
.allocated
= 1;
254 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
255 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
256 chan
->backend
.cpumask
));
257 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
264 kfree(buf
->commit_cold
);
266 kfree(buf
->commit_hot
);
268 lib_ring_buffer_backend_free(&buf
->backend
);
272 static void switch_buffer_timer(unsigned long data
)
274 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
275 struct channel
*chan
= buf
->backend
.chan
;
276 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
279 * Only flush buffers periodically if readers are active.
281 if (atomic_long_read(&buf
->active_readers
))
282 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
284 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
285 lttng_mod_timer_pinned(&buf
->switch_timer
,
286 jiffies
+ chan
->switch_timer_interval
);
288 mod_timer(&buf
->switch_timer
,
289 jiffies
+ chan
->switch_timer_interval
);
293 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
295 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
297 struct channel
*chan
= buf
->backend
.chan
;
298 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
300 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
303 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
304 lttng_init_timer_pinned(&buf
->switch_timer
);
306 init_timer(&buf
->switch_timer
);
308 buf
->switch_timer
.function
= switch_buffer_timer
;
309 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
310 buf
->switch_timer
.data
= (unsigned long)buf
;
311 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
312 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
314 add_timer(&buf
->switch_timer
);
315 buf
->switch_timer_enabled
= 1;
319 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
321 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
323 struct channel
*chan
= buf
->backend
.chan
;
325 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
328 del_timer_sync(&buf
->switch_timer
);
329 buf
->switch_timer_enabled
= 0;
333 * Polling timer to check the channels for data.
335 static void read_buffer_timer(unsigned long data
)
337 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
338 struct channel
*chan
= buf
->backend
.chan
;
339 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
341 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
343 if (atomic_long_read(&buf
->active_readers
)
344 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
345 wake_up_interruptible(&buf
->read_wait
);
346 wake_up_interruptible(&chan
->read_wait
);
349 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
350 lttng_mod_timer_pinned(&buf
->read_timer
,
351 jiffies
+ chan
->read_timer_interval
);
353 mod_timer(&buf
->read_timer
,
354 jiffies
+ chan
->read_timer_interval
);
358 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
360 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
362 struct channel
*chan
= buf
->backend
.chan
;
363 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
365 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
366 || !chan
->read_timer_interval
367 || buf
->read_timer_enabled
)
370 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
371 lttng_init_timer_pinned(&buf
->read_timer
);
373 init_timer(&buf
->read_timer
);
375 buf
->read_timer
.function
= read_buffer_timer
;
376 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
377 buf
->read_timer
.data
= (unsigned long)buf
;
379 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
380 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
382 add_timer(&buf
->read_timer
);
383 buf
->read_timer_enabled
= 1;
387 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
389 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
391 struct channel
*chan
= buf
->backend
.chan
;
392 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
394 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
395 || !chan
->read_timer_interval
396 || !buf
->read_timer_enabled
)
399 del_timer_sync(&buf
->read_timer
);
401 * do one more check to catch data that has been written in the last
404 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
405 wake_up_interruptible(&buf
->read_wait
);
406 wake_up_interruptible(&chan
->read_wait
);
408 buf
->read_timer_enabled
= 0;
411 #ifdef CONFIG_HOTPLUG_CPU
413 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
414 * @nb: notifier block
415 * @action: hotplug action to take
418 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
421 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
422 unsigned long action
,
425 unsigned int cpu
= (unsigned long)hcpu
;
426 struct channel
*chan
= container_of(nb
, struct channel
,
428 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
429 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
431 if (!chan
->cpu_hp_enable
)
434 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
437 case CPU_DOWN_FAILED
:
438 case CPU_DOWN_FAILED_FROZEN
:
440 case CPU_ONLINE_FROZEN
:
441 wake_up_interruptible(&chan
->hp_wait
);
442 lib_ring_buffer_start_switch_timer(buf
);
443 lib_ring_buffer_start_read_timer(buf
);
446 case CPU_DOWN_PREPARE
:
447 case CPU_DOWN_PREPARE_FROZEN
:
448 lib_ring_buffer_stop_switch_timer(buf
);
449 lib_ring_buffer_stop_read_timer(buf
);
453 case CPU_DEAD_FROZEN
:
455 * Performing a buffer switch on a remote CPU. Performed by
456 * the CPU responsible for doing the hotunplug after the target
457 * CPU stopped running completely. Ensures that all data
458 * from that remote CPU is flushed.
460 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
469 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
471 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
472 * that wake-up-tracing generated events are flushed before going idle (in
473 * tick_nohz). We test if the spinlock is locked to deal with the race where
474 * readers try to sample the ring buffer before we perform the switch. We let
475 * the readers retry in that case. If there is data in the buffer, the wake up
476 * is going to forbid the CPU running the reader thread from going idle.
478 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
482 struct channel
*chan
= container_of(nb
, struct channel
,
484 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
485 struct lib_ring_buffer
*buf
;
486 int cpu
= smp_processor_id();
488 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
490 * We don't support keeping the system idle with global buffers
491 * and streaming active. In order to do so, we would need to
492 * sample a non-nohz-cpumask racelessly with the nohz updates
493 * without adding synchronization overhead to nohz. Leave this
494 * use-case out for now.
499 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
501 case TICK_NOHZ_FLUSH
:
502 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
503 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
504 && chan
->read_timer_interval
505 && atomic_long_read(&buf
->active_readers
)
506 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
507 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
508 wake_up_interruptible(&buf
->read_wait
);
509 wake_up_interruptible(&chan
->read_wait
);
511 if (chan
->switch_timer_interval
)
512 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
513 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
516 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
517 lib_ring_buffer_stop_switch_timer(buf
);
518 lib_ring_buffer_stop_read_timer(buf
);
519 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
521 case TICK_NOHZ_RESTART
:
522 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
523 lib_ring_buffer_start_read_timer(buf
);
524 lib_ring_buffer_start_switch_timer(buf
);
525 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
532 void notrace
lib_ring_buffer_tick_nohz_flush(void)
534 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
538 void notrace
lib_ring_buffer_tick_nohz_stop(void)
540 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
544 void notrace
lib_ring_buffer_tick_nohz_restart(void)
546 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
549 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
554 static void channel_unregister_notifiers(struct channel
*chan
)
556 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
559 channel_iterator_unregister_notifiers(chan
);
560 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
563 * Remove the nohz notifier first, so we are certain we stop
566 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
567 &chan
->tick_nohz_notifier
);
569 * ring_buffer_nohz_lock will not be needed below, because
570 * we just removed the notifiers, which were the only source of
573 #endif /* CONFIG_NO_HZ */
574 #ifdef CONFIG_HOTPLUG_CPU
576 chan
->cpu_hp_enable
= 0;
577 for_each_online_cpu(cpu
) {
578 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
580 lib_ring_buffer_stop_switch_timer(buf
);
581 lib_ring_buffer_stop_read_timer(buf
);
584 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
586 for_each_possible_cpu(cpu
) {
587 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
589 lib_ring_buffer_stop_switch_timer(buf
);
590 lib_ring_buffer_stop_read_timer(buf
);
594 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
596 lib_ring_buffer_stop_switch_timer(buf
);
597 lib_ring_buffer_stop_read_timer(buf
);
599 channel_backend_unregister_notifiers(&chan
->backend
);
602 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
604 if (!buf
->quiescent
) {
605 buf
->quiescent
= true;
606 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
610 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
612 buf
->quiescent
= false;
615 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
618 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
620 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
622 for_each_channel_cpu(cpu
, chan
) {
623 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
626 lib_ring_buffer_set_quiescent(buf
);
630 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
632 lib_ring_buffer_set_quiescent(buf
);
635 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
637 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
640 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
642 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
644 for_each_channel_cpu(cpu
, chan
) {
645 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
648 lib_ring_buffer_clear_quiescent(buf
);
652 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
654 lib_ring_buffer_clear_quiescent(buf
);
657 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
659 static void channel_free(struct channel
*chan
)
661 if (chan
->backend
.release_priv_ops
) {
662 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
664 channel_iterator_free(chan
);
665 channel_backend_free(&chan
->backend
);
670 * channel_create - Create channel.
671 * @config: ring buffer instance configuration
672 * @name: name of the channel
673 * @priv: ring buffer client private data
674 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
675 * address mapping. It is used only by RING_BUFFER_STATIC
676 * configuration. It can be set to NULL for other backends.
677 * @subbuf_size: subbuffer size
678 * @num_subbuf: number of subbuffers
679 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
680 * padding to let readers get those sub-buffers.
681 * Used for live streaming.
682 * @read_timer_interval: Time interval (in us) to wake up pending readers.
685 * Returns NULL on failure.
687 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
688 const char *name
, void *priv
, void *buf_addr
,
690 size_t num_subbuf
, unsigned int switch_timer_interval
,
691 unsigned int read_timer_interval
)
694 struct channel
*chan
;
696 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
697 read_timer_interval
))
700 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
704 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
705 subbuf_size
, num_subbuf
);
709 ret
= channel_iterator_init(chan
);
711 goto error_free_backend
;
713 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
714 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
715 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
716 kref_init(&chan
->ref
);
717 init_waitqueue_head(&chan
->read_wait
);
718 init_waitqueue_head(&chan
->hp_wait
);
720 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
721 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
722 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
723 chan
->tick_nohz_notifier
.notifier_call
=
724 ring_buffer_tick_nohz_callback
;
725 chan
->tick_nohz_notifier
.priority
= ~0U;
726 atomic_notifier_chain_register(&tick_nohz_notifier
,
727 &chan
->tick_nohz_notifier
);
728 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
731 * In case of non-hotplug cpu, if the ring-buffer is allocated
732 * in early initcall, it will not be notified of secondary cpus.
733 * In that off case, we need to allocate for all possible cpus.
735 #ifdef CONFIG_HOTPLUG_CPU
736 chan
->cpu_hp_notifier
.notifier_call
=
737 lib_ring_buffer_cpu_hp_callback
;
738 chan
->cpu_hp_notifier
.priority
= 6;
739 register_cpu_notifier(&chan
->cpu_hp_notifier
);
742 for_each_online_cpu(cpu
) {
743 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
745 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
746 lib_ring_buffer_start_switch_timer(buf
);
747 lib_ring_buffer_start_read_timer(buf
);
748 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
750 chan
->cpu_hp_enable
= 1;
753 for_each_possible_cpu(cpu
) {
754 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
756 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
757 lib_ring_buffer_start_switch_timer(buf
);
758 lib_ring_buffer_start_read_timer(buf
);
759 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
763 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
765 lib_ring_buffer_start_switch_timer(buf
);
766 lib_ring_buffer_start_read_timer(buf
);
772 channel_backend_free(&chan
->backend
);
777 EXPORT_SYMBOL_GPL(channel_create
);
780 void channel_release(struct kref
*kref
)
782 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
787 * channel_destroy - Finalize, wait for q.s. and destroy channel.
788 * @chan: channel to destroy
791 * Call "destroy" callback, finalize channels, and then decrement the
792 * channel reference count. Note that when readers have completed data
793 * consumption of finalized channels, get_subbuf() will return -ENODATA.
794 * They should release their handle at that point. Returns the private
797 void *channel_destroy(struct channel
*chan
)
800 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
803 channel_unregister_notifiers(chan
);
805 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
807 * No need to hold cpu hotplug, because all notifiers have been
810 for_each_channel_cpu(cpu
, chan
) {
811 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
814 if (config
->cb
.buffer_finalize
)
815 config
->cb
.buffer_finalize(buf
,
818 if (buf
->backend
.allocated
)
819 lib_ring_buffer_set_quiescent(buf
);
821 * Perform flush before writing to finalized.
824 ACCESS_ONCE(buf
->finalized
) = 1;
825 wake_up_interruptible(&buf
->read_wait
);
828 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
830 if (config
->cb
.buffer_finalize
)
831 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
832 if (buf
->backend
.allocated
)
833 lib_ring_buffer_set_quiescent(buf
);
835 * Perform flush before writing to finalized.
838 ACCESS_ONCE(buf
->finalized
) = 1;
839 wake_up_interruptible(&buf
->read_wait
);
841 ACCESS_ONCE(chan
->finalized
) = 1;
842 wake_up_interruptible(&chan
->hp_wait
);
843 wake_up_interruptible(&chan
->read_wait
);
844 priv
= chan
->backend
.priv
;
845 kref_put(&chan
->ref
, channel_release
);
848 EXPORT_SYMBOL_GPL(channel_destroy
);
850 struct lib_ring_buffer
*channel_get_ring_buffer(
851 const struct lib_ring_buffer_config
*config
,
852 struct channel
*chan
, int cpu
)
854 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
855 return chan
->backend
.buf
;
857 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
859 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
861 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
863 struct channel
*chan
= buf
->backend
.chan
;
865 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
867 if (!lttng_kref_get(&chan
->ref
)) {
868 atomic_long_dec(&buf
->active_readers
);
871 lttng_smp_mb__after_atomic();
874 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
876 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
878 struct channel
*chan
= buf
->backend
.chan
;
880 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
881 lttng_smp_mb__before_atomic();
882 atomic_long_dec(&buf
->active_readers
);
883 kref_put(&chan
->ref
, channel_release
);
885 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
888 * Promote compiler barrier to a smp_mb().
889 * For the specific ring buffer case, this IPI call should be removed if the
890 * architecture does not reorder writes. This should eventually be provided by
891 * a separate architecture-specific infrastructure.
893 static void remote_mb(void *info
)
899 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
901 * @consumed: consumed count indicating the position where to read
902 * @produced: produced count, indicates position when to stop reading
904 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
905 * data to read at consumed position, or 0 if the get operation succeeds.
906 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
909 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
910 unsigned long *consumed
, unsigned long *produced
)
912 struct channel
*chan
= buf
->backend
.chan
;
913 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
914 unsigned long consumed_cur
, write_offset
;
918 finalized
= ACCESS_ONCE(buf
->finalized
);
920 * Read finalized before counters.
923 consumed_cur
= atomic_long_read(&buf
->consumed
);
925 * No need to issue a memory barrier between consumed count read and
926 * write offset read, because consumed count can only change
927 * concurrently in overwrite mode, and we keep a sequence counter
928 * identifier derived from the write offset to check we are getting
929 * the same sub-buffer we are expecting (the sub-buffers are atomically
930 * "tagged" upon writes, tags are checked upon read).
932 write_offset
= v_read(config
, &buf
->offset
);
935 * Check that we are not about to read the same subbuffer in
936 * which the writer head is.
938 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
942 *consumed
= consumed_cur
;
943 *produced
= subbuf_trunc(write_offset
, chan
);
949 * The memory barriers __wait_event()/wake_up_interruptible() take care
950 * of "raw_spin_is_locked" memory ordering.
954 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
959 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
962 * lib_ring_buffer_put_snapshot - move consumed counter forward
964 * Should only be called from consumer context.
966 * @consumed_new: new consumed count value
968 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
969 unsigned long consumed_new
)
971 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
972 struct channel
*chan
= bufb
->chan
;
973 unsigned long consumed
;
975 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
978 * Only push the consumed value forward.
979 * If the consumed cmpxchg fails, this is because we have been pushed by
980 * the writer in flight recorder mode.
982 consumed
= atomic_long_read(&buf
->consumed
);
983 while ((long) consumed
- (long) consumed_new
< 0)
984 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
986 /* Wake-up the metadata producer */
987 wake_up_interruptible(&buf
->write_wait
);
989 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
992 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
994 * @consumed: consumed count indicating the position where to read
996 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
997 * data to read at consumed position, or 0 if the get operation succeeds.
998 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1000 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1001 unsigned long consumed
)
1003 struct channel
*chan
= buf
->backend
.chan
;
1004 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1005 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1009 if (buf
->get_subbuf
) {
1011 * Reader is trying to get a subbuffer twice.
1013 CHAN_WARN_ON(chan
, 1);
1017 finalized
= ACCESS_ONCE(buf
->finalized
);
1019 * Read finalized before counters.
1022 consumed_cur
= atomic_long_read(&buf
->consumed
);
1023 consumed_idx
= subbuf_index(consumed
, chan
);
1024 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1026 * Make sure we read the commit count before reading the buffer
1027 * data and the write offset. Correct consumed offset ordering
1028 * wrt commit count is insured by the use of cmpxchg to update
1029 * the consumed offset.
1030 * smp_call_function_single can fail if the remote CPU is offline,
1031 * this is OK because then there is no wmb to execute there.
1032 * If our thread is executing on the same CPU as the on the buffers
1033 * belongs to, we don't have to synchronize it at all. If we are
1034 * migrated, the scheduler will take care of the memory barriers.
1035 * Normally, smp_call_function_single() should ensure program order when
1036 * executing the remote function, which implies that it surrounds the
1037 * function execution with :
1048 * However, smp_call_function_single() does not seem to clearly execute
1049 * such barriers. It depends on spinlock semantic to provide the barrier
1050 * before executing the IPI and, when busy-looping, csd_lock_wait only
1051 * executes smp_mb() when it has to wait for the other CPU.
1053 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1054 * required ourself, even if duplicated. It has no performance impact
1057 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1058 * read and write vs write. They do not ensure core synchronization. We
1059 * really have to ensure total order between the 3 barriers running on
1062 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1063 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1064 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1065 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1066 /* Total order with IPI handler smp_mb() */
1068 smp_call_function_single(buf
->backend
.cpu
,
1069 remote_mb
, NULL
, 1);
1070 /* Total order with IPI handler smp_mb() */
1074 /* Total order with IPI handler smp_mb() */
1076 smp_call_function(remote_mb
, NULL
, 1);
1077 /* Total order with IPI handler smp_mb() */
1082 * Local rmb to match the remote wmb to read the commit count
1083 * before the buffer data and the write offset.
1088 write_offset
= v_read(config
, &buf
->offset
);
1091 * Check that the buffer we are getting is after or at consumed_cur
1094 if ((long) subbuf_trunc(consumed
, chan
)
1095 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1099 * Check that the subbuffer we are trying to consume has been
1100 * already fully committed.
1102 if (((commit_count
- chan
->backend
.subbuf_size
)
1103 & chan
->commit_count_mask
)
1104 - (buf_trunc(consumed
, chan
)
1105 >> chan
->backend
.num_subbuf_order
)
1110 * Check that we are not about to read the same subbuffer in
1111 * which the writer head is.
1113 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1118 * Failure to get the subbuffer causes a busy-loop retry without going
1119 * to a wait queue. These are caused by short-lived race windows where
1120 * the writer is getting access to a subbuffer we were trying to get
1121 * access to. Also checks that the "consumed" buffer count we are
1122 * looking for matches the one contained in the subbuffer id.
1124 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1125 consumed_idx
, buf_trunc_val(consumed
, chan
));
1128 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1130 buf
->get_subbuf_consumed
= consumed
;
1131 buf
->get_subbuf
= 1;
1137 * The memory barriers __wait_event()/wake_up_interruptible() take care
1138 * of "raw_spin_is_locked" memory ordering.
1142 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1147 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1150 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1153 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1155 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1156 struct channel
*chan
= bufb
->chan
;
1157 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1158 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1160 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1162 if (!buf
->get_subbuf
) {
1164 * Reader puts a subbuffer it did not get.
1166 CHAN_WARN_ON(chan
, 1);
1169 consumed
= buf
->get_subbuf_consumed
;
1170 buf
->get_subbuf
= 0;
1173 * Clear the records_unread counter. (overruns counter)
1174 * Can still be non-zero if a file reader simply grabbed the data
1175 * without using iterators.
1176 * Can be below zero if an iterator is used on a snapshot more than
1179 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1180 v_add(config
, v_read(config
,
1181 &bufb
->array
[read_sb_bindex
]->records_unread
),
1182 &bufb
->records_read
);
1183 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1184 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1185 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1186 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1189 * Exchange the reader subbuffer with the one we put in its place in the
1190 * writer subbuffer table. Expect the original consumed count. If
1191 * update_read_sb_index fails, this is because the writer updated the
1192 * subbuffer concurrently. We should therefore keep the subbuffer we
1193 * currently have: it has become invalid to try reading this sub-buffer
1194 * consumed count value anyway.
1196 consumed_idx
= subbuf_index(consumed
, chan
);
1197 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1198 consumed_idx
, buf_trunc_val(consumed
, chan
));
1200 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1201 * if the writer concurrently updated it.
1204 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1207 * cons_offset is an iterator on all subbuffer offsets between the reader
1208 * position and the writer position. (inclusive)
1211 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1212 struct channel
*chan
,
1213 unsigned long cons_offset
,
1216 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1217 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1219 cons_idx
= subbuf_index(cons_offset
, chan
);
1220 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1221 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1223 if (subbuf_offset(commit_count
, chan
) != 0)
1225 "ring buffer %s, cpu %d: "
1226 "commit count in subbuffer %lu,\n"
1227 "expecting multiples of %lu bytes\n"
1228 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1229 chan
->backend
.name
, cpu
, cons_idx
,
1230 chan
->backend
.subbuf_size
,
1231 commit_count
, commit_count_sb
);
1233 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1234 chan
->backend
.name
, cpu
, commit_count
);
1238 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1239 struct channel
*chan
,
1240 void *priv
, int cpu
)
1242 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1243 unsigned long write_offset
, cons_offset
;
1246 * No need to order commit_count, write_offset and cons_offset reads
1247 * because we execute at teardown when no more writer nor reader
1248 * references are left.
1250 write_offset
= v_read(config
, &buf
->offset
);
1251 cons_offset
= atomic_long_read(&buf
->consumed
);
1252 if (write_offset
!= cons_offset
)
1254 "ring buffer %s, cpu %d: "
1255 "non-consumed data\n"
1256 " [ %lu bytes written, %lu bytes read ]\n",
1257 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1259 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1260 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1263 cons_offset
= subbuf_align(cons_offset
, chan
))
1264 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1269 void lib_ring_buffer_print_errors(struct channel
*chan
,
1270 struct lib_ring_buffer
*buf
, int cpu
)
1272 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1273 void *priv
= chan
->backend
.priv
;
1275 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1276 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1277 "%lu records overrun\n",
1279 v_read(config
, &buf
->records_count
),
1280 v_read(config
, &buf
->records_overrun
));
1282 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1283 "%lu records overrun\n",
1284 chan
->backend
.name
, cpu
,
1285 v_read(config
, &buf
->records_count
),
1286 v_read(config
, &buf
->records_overrun
));
1288 if (v_read(config
, &buf
->records_lost_full
)
1289 || v_read(config
, &buf
->records_lost_wrap
)
1290 || v_read(config
, &buf
->records_lost_big
))
1292 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1293 " [ %lu buffer full, %lu nest buffer wrap-around, "
1294 "%lu event too big ]\n",
1295 chan
->backend
.name
, cpu
,
1296 v_read(config
, &buf
->records_lost_full
),
1297 v_read(config
, &buf
->records_lost_wrap
),
1298 v_read(config
, &buf
->records_lost_big
));
1300 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1304 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1306 * Only executed by SWITCH_FLUSH, which can be issued while tracing is active
1307 * or at buffer finalization (destroy).
1310 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1311 struct channel
*chan
,
1312 struct switch_offsets
*offsets
,
1315 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1316 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1317 unsigned long commit_count
;
1319 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1322 * Order all writes to buffer before the commit count update that will
1323 * determine that the subbuffer is full.
1325 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1327 * Must write slot data before incrementing commit count. This
1328 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1334 v_add(config
, config
->cb
.subbuffer_header_size(),
1335 &buf
->commit_hot
[oldidx
].cc
);
1336 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1337 /* Check if the written buffer has to be delivered */
1338 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1339 commit_count
, oldidx
, tsc
);
1340 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1341 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1346 * lib_ring_buffer_switch_old_end: switch old subbuffer
1348 * Note : offset_old should never be 0 here. It is ok, because we never perform
1349 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1350 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1354 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1355 struct channel
*chan
,
1356 struct switch_offsets
*offsets
,
1359 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1360 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1361 unsigned long commit_count
, padding_size
, data_size
;
1363 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1364 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1365 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1368 * Order all writes to buffer before the commit count update that will
1369 * determine that the subbuffer is full.
1371 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1373 * Must write slot data before incrementing commit count. This
1374 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1380 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1381 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1382 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1383 commit_count
, oldidx
, tsc
);
1384 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1385 offsets
->old
+ padding_size
, commit_count
);
1389 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1391 * This code can be executed unordered : writers may already have written to the
1392 * sub-buffer before this code gets executed, caution. The commit makes sure
1393 * that this code is executed before the deliver of this sub-buffer.
1396 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1397 struct channel
*chan
,
1398 struct switch_offsets
*offsets
,
1401 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1402 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1403 unsigned long commit_count
;
1405 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1408 * Order all writes to buffer before the commit count update that will
1409 * determine that the subbuffer is full.
1411 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1413 * Must write slot data before incrementing commit count. This
1414 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1420 v_add(config
, config
->cb
.subbuffer_header_size(),
1421 &buf
->commit_hot
[beginidx
].cc
);
1422 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1423 /* Check if the written buffer has to be delivered */
1424 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1425 commit_count
, beginidx
, tsc
);
1426 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1427 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1432 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1434 * Calls subbuffer_set_data_size() to set the data size of the current
1435 * sub-buffer. We do not need to perform check_deliver nor commit here,
1436 * since this task will be done by the "commit" of the event for which
1437 * we are currently doing the space reservation.
1440 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1441 struct channel
*chan
,
1442 struct switch_offsets
*offsets
,
1445 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1446 unsigned long endidx
, data_size
;
1448 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1449 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1450 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1456 * !0 if execution must be aborted.
1459 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1460 struct lib_ring_buffer
*buf
,
1461 struct channel
*chan
,
1462 struct switch_offsets
*offsets
,
1465 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1466 unsigned long off
, reserve_commit_diff
;
1468 offsets
->begin
= v_read(config
, &buf
->offset
);
1469 offsets
->old
= offsets
->begin
;
1470 offsets
->switch_old_start
= 0;
1471 off
= subbuf_offset(offsets
->begin
, chan
);
1473 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1476 * Ensure we flush the header of an empty subbuffer when doing the
1477 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1478 * total data gathering duration even if there were no records saved
1479 * after the last buffer switch.
1480 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1481 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1482 * subbuffer header as appropriate.
1483 * The next record that reserves space will be responsible for
1484 * populating the following subbuffer header. We choose not to populate
1485 * the next subbuffer header here because we want to be able to use
1486 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1487 * buffer flush, which must guarantee that all the buffer content
1488 * (records and header timestamps) are visible to the reader. This is
1489 * required for quiescence guarantees for the fusion merge.
1491 if (mode
!= SWITCH_FLUSH
&& !off
)
1492 return -1; /* we do not have to switch : buffer is empty */
1494 if (unlikely(off
== 0)) {
1495 unsigned long sb_index
, commit_count
;
1498 * We are performing a SWITCH_FLUSH. There may be concurrent
1499 * writes into the buffer if e.g. invoked while performing a
1500 * snapshot on an active trace.
1502 * If the client does not save any header information (sub-buffer
1503 * header size == 0), don't switch empty subbuffer on finalize,
1504 * because it is invalid to deliver a completely empty
1507 if (!config
->cb
.subbuffer_header_size())
1510 /* Test new buffer integrity */
1511 sb_index
= subbuf_index(offsets
->begin
, chan
);
1512 commit_count
= v_read(config
,
1513 &buf
->commit_cold
[sb_index
].cc_sb
);
1514 reserve_commit_diff
=
1515 (buf_trunc(offsets
->begin
, chan
)
1516 >> chan
->backend
.num_subbuf_order
)
1517 - (commit_count
& chan
->commit_count_mask
);
1518 if (likely(reserve_commit_diff
== 0)) {
1519 /* Next subbuffer not being written to. */
1520 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1521 subbuf_trunc(offsets
->begin
, chan
)
1522 - subbuf_trunc((unsigned long)
1523 atomic_long_read(&buf
->consumed
), chan
)
1524 >= chan
->backend
.buf_size
)) {
1526 * We do not overwrite non consumed buffers
1527 * and we are full : don't switch.
1532 * Next subbuffer not being written to, and we
1533 * are either in overwrite mode or the buffer is
1534 * not full. It's safe to write in this new
1540 * Next subbuffer reserve offset does not match the
1541 * commit offset. Don't perform switch in
1542 * producer-consumer and overwrite mode. Caused by
1543 * either a writer OOPS or too many nested writes over a
1544 * reserve/commit pair.
1550 * Need to write the subbuffer start header on finalize.
1552 offsets
->switch_old_start
= 1;
1554 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1555 /* Note: old points to the next subbuf at offset 0 */
1556 offsets
->end
= offsets
->begin
;
1561 * Force a sub-buffer switch. This operation is completely reentrant : can be
1562 * called while tracing is active with absolutely no lock held.
1564 * Note, however, that as a v_cmpxchg is used for some atomic
1565 * operations, this function must be called from the CPU which owns the buffer
1566 * for a ACTIVE flush.
1568 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1570 struct channel
*chan
= buf
->backend
.chan
;
1571 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1572 struct switch_offsets offsets
;
1573 unsigned long oldidx
;
1579 * Perform retryable operations.
1582 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1584 return; /* Switch not needed */
1585 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1589 * Atomically update last_tsc. This update races against concurrent
1590 * atomic updates, but the race will always cause supplementary full TSC
1591 * records, never the opposite (missing a full TSC record when it would
1594 save_last_tsc(config
, buf
, tsc
);
1597 * Push the reader if necessary
1599 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1601 oldidx
= subbuf_index(offsets
.old
, chan
);
1602 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1605 * May need to populate header start on SWITCH_FLUSH.
1607 if (offsets
.switch_old_start
) {
1608 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1609 offsets
.old
+= config
->cb
.subbuffer_header_size();
1613 * Switch old subbuffer.
1615 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1617 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1619 struct switch_param
{
1620 struct lib_ring_buffer
*buf
;
1621 enum switch_mode mode
;
1624 static void remote_switch(void *info
)
1626 struct switch_param
*param
= info
;
1627 struct lib_ring_buffer
*buf
= param
->buf
;
1629 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1632 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1633 enum switch_mode mode
)
1635 struct channel
*chan
= buf
->backend
.chan
;
1636 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1638 struct switch_param param
;
1641 * With global synchronization we don't need to use the IPI scheme.
1643 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1644 lib_ring_buffer_switch_slow(buf
, mode
);
1649 * Taking lock on CPU hotplug to ensure two things: first, that the
1650 * target cpu is not taken concurrently offline while we are within
1651 * smp_call_function_single() (I don't trust that get_cpu() on the
1652 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1653 * confirmed)). Secondly, if it happens that the CPU is not online, our
1654 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1655 * CPU hotplug handlers, which can also perform a remote subbuffer
1661 ret
= smp_call_function_single(buf
->backend
.cpu
,
1662 remote_switch
, ¶m
, 1);
1664 /* Remote CPU is offline, do it ourself. */
1665 lib_ring_buffer_switch_slow(buf
, mode
);
1670 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1672 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1674 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1676 /* Switch sub-buffer even if current sub-buffer is empty. */
1677 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1679 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1681 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1686 * -ENOSPC if event size is too large for packet.
1687 * -ENOBUFS if there is currently not enough space in buffer for the event.
1688 * -EIO if data cannot be written into the buffer for any other reason.
1691 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1692 struct channel
*chan
,
1693 struct switch_offsets
*offsets
,
1694 struct lib_ring_buffer_ctx
*ctx
)
1696 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1697 unsigned long reserve_commit_diff
, offset_cmp
;
1700 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1701 offsets
->old
= offsets
->begin
;
1702 offsets
->switch_new_start
= 0;
1703 offsets
->switch_new_end
= 0;
1704 offsets
->switch_old_end
= 0;
1705 offsets
->pre_header_padding
= 0;
1707 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1708 if ((int64_t) ctx
->tsc
== -EIO
)
1711 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1712 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1714 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1715 offsets
->switch_new_start
= 1; /* For offsets->begin */
1717 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1719 &offsets
->pre_header_padding
,
1722 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1725 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1726 offsets
->size
> chan
->backend
.subbuf_size
)) {
1727 offsets
->switch_old_end
= 1; /* For offsets->old */
1728 offsets
->switch_new_start
= 1; /* For offsets->begin */
1731 if (unlikely(offsets
->switch_new_start
)) {
1732 unsigned long sb_index
, commit_count
;
1735 * We are typically not filling the previous buffer completely.
1737 if (likely(offsets
->switch_old_end
))
1738 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1739 offsets
->begin
= offsets
->begin
1740 + config
->cb
.subbuffer_header_size();
1741 /* Test new buffer integrity */
1742 sb_index
= subbuf_index(offsets
->begin
, chan
);
1744 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1745 * lib_ring_buffer_check_deliver() has the matching
1746 * memory barriers required around commit_cold cc_sb
1747 * updates to ensure reserve and commit counter updates
1748 * are not seen reordered when updated by another CPU.
1751 commit_count
= v_read(config
,
1752 &buf
->commit_cold
[sb_index
].cc_sb
);
1753 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1755 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1757 * The reserve counter have been concurrently updated
1758 * while we read the commit counter. This means the
1759 * commit counter we read might not match buf->offset
1760 * due to concurrent update. We therefore need to retry.
1764 reserve_commit_diff
=
1765 (buf_trunc(offsets
->begin
, chan
)
1766 >> chan
->backend
.num_subbuf_order
)
1767 - (commit_count
& chan
->commit_count_mask
);
1768 if (likely(reserve_commit_diff
== 0)) {
1769 /* Next subbuffer not being written to. */
1770 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1771 subbuf_trunc(offsets
->begin
, chan
)
1772 - subbuf_trunc((unsigned long)
1773 atomic_long_read(&buf
->consumed
), chan
)
1774 >= chan
->backend
.buf_size
)) {
1776 * We do not overwrite non consumed buffers
1777 * and we are full : record is lost.
1779 v_inc(config
, &buf
->records_lost_full
);
1783 * Next subbuffer not being written to, and we
1784 * are either in overwrite mode or the buffer is
1785 * not full. It's safe to write in this new
1791 * Next subbuffer reserve offset does not match the
1792 * commit offset, and this did not involve update to the
1793 * reserve counter. Drop record in producer-consumer and
1794 * overwrite mode. Caused by either a writer OOPS or
1795 * too many nested writes over a reserve/commit pair.
1797 v_inc(config
, &buf
->records_lost_wrap
);
1801 config
->cb
.record_header_size(config
, chan
,
1803 &offsets
->pre_header_padding
,
1806 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1809 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1810 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1812 * Record too big for subbuffers, report error, don't
1813 * complete the sub-buffer switch.
1815 v_inc(config
, &buf
->records_lost_big
);
1819 * We just made a successful buffer switch and the
1820 * record fits in the new subbuffer. Let's write.
1825 * Record fits in the current buffer and we are not on a switch
1826 * boundary. It's safe to write.
1829 offsets
->end
= offsets
->begin
+ offsets
->size
;
1831 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1833 * The offset_end will fall at the very beginning of the next
1836 offsets
->switch_new_end
= 1; /* For offsets->begin */
1842 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1843 * @ctx: ring buffer context.
1845 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1846 * -EIO for other errors, else returns 0.
1847 * It will take care of sub-buffer switching.
1849 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1851 struct channel
*chan
= ctx
->chan
;
1852 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1853 struct lib_ring_buffer
*buf
;
1854 struct switch_offsets offsets
;
1857 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1858 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1860 buf
= chan
->backend
.buf
;
1866 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1870 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1875 * Atomically update last_tsc. This update races against concurrent
1876 * atomic updates, but the race will always cause supplementary full TSC
1877 * records, never the opposite (missing a full TSC record when it would
1880 save_last_tsc(config
, buf
, ctx
->tsc
);
1883 * Push the reader if necessary
1885 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1888 * Clear noref flag for this subbuffer.
1890 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1891 subbuf_index(offsets
.end
- 1, chan
));
1894 * Switch old subbuffer if needed.
1896 if (unlikely(offsets
.switch_old_end
)) {
1897 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1898 subbuf_index(offsets
.old
- 1, chan
));
1899 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1903 * Populate new subbuffer.
1905 if (unlikely(offsets
.switch_new_start
))
1906 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1908 if (unlikely(offsets
.switch_new_end
))
1909 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1911 ctx
->slot_size
= offsets
.size
;
1912 ctx
->pre_offset
= offsets
.begin
;
1913 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1916 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
1918 int __init
init_lib_ring_buffer_frontend(void)
1922 for_each_possible_cpu(cpu
)
1923 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
1927 module_init(init_lib_ring_buffer_frontend
);
1929 void __exit
exit_lib_ring_buffer_frontend(void)
1933 module_exit(exit_lib_ring_buffer_frontend
);