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"
65 * Internal structure representing offsets to use at a sub-buffer switch.
67 struct switch_offsets
{
68 unsigned long begin
, end
, old
;
69 size_t pre_header_padding
, size
;
70 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
81 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
82 #endif /* CONFIG_NO_HZ */
84 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
86 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
87 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
90 void lib_ring_buffer_print_errors(struct channel
*chan
,
91 struct lib_ring_buffer
*buf
, int cpu
);
94 * Must be called under cpu hotplug protection.
96 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
98 struct channel
*chan
= buf
->backend
.chan
;
100 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
101 kfree(buf
->commit_hot
);
102 kfree(buf
->commit_cold
);
104 lib_ring_buffer_backend_free(&buf
->backend
);
108 * lib_ring_buffer_reset - Reset ring buffer to initial values.
111 * Effectively empty the ring buffer. Should be called when the buffer is not
112 * used for writing. The ring buffer can be opened for reading, but the reader
113 * should not be using the iterator concurrently with reset. The previous
114 * current iterator record is reset.
116 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
118 struct channel
*chan
= buf
->backend
.chan
;
119 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
123 * Reset iterator first. It will put the subbuffer if it currently holds
126 lib_ring_buffer_iterator_reset(buf
);
127 v_set(config
, &buf
->offset
, 0);
128 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
129 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
130 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
131 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
133 atomic_long_set(&buf
->consumed
, 0);
134 atomic_set(&buf
->record_disabled
, 0);
135 v_set(config
, &buf
->last_tsc
, 0);
136 lib_ring_buffer_backend_reset(&buf
->backend
);
137 /* Don't reset number of active readers */
138 v_set(config
, &buf
->records_lost_full
, 0);
139 v_set(config
, &buf
->records_lost_wrap
, 0);
140 v_set(config
, &buf
->records_lost_big
, 0);
141 v_set(config
, &buf
->records_count
, 0);
142 v_set(config
, &buf
->records_overrun
, 0);
145 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
148 * channel_reset - Reset channel to initial values.
151 * Effectively empty the channel. Should be called when the channel is not used
152 * for writing. The channel can be opened for reading, but the reader should not
153 * be using the iterator concurrently with reset. The previous current iterator
156 void channel_reset(struct channel
*chan
)
159 * Reset iterators first. Will put the subbuffer if held for reading.
161 channel_iterator_reset(chan
);
162 atomic_set(&chan
->record_disabled
, 0);
163 /* Don't reset commit_count_mask, still valid */
164 channel_backend_reset(&chan
->backend
);
165 /* Don't reset switch/read timer interval */
166 /* Don't reset notifiers and notifier enable bits */
167 /* Don't reset reader reference count */
169 EXPORT_SYMBOL_GPL(channel_reset
);
172 * Must be called under cpu hotplug protection.
174 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
175 struct channel_backend
*chanb
, int cpu
)
177 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
178 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
179 void *priv
= chanb
->priv
;
180 size_t subbuf_header_size
;
184 /* Test for cpu hotplug */
185 if (buf
->backend
.allocated
)
189 * Paranoia: per cpu dynamic allocation is not officially documented as
190 * zeroing the memory, so let's do it here too, just in case.
192 memset(buf
, 0, sizeof(*buf
));
194 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
199 kzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
200 * chan
->backend
.num_subbuf
,
201 1 << INTERNODE_CACHE_SHIFT
),
202 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
203 if (!buf
->commit_hot
) {
209 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
210 * chan
->backend
.num_subbuf
,
211 1 << INTERNODE_CACHE_SHIFT
),
212 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
213 if (!buf
->commit_cold
) {
218 init_waitqueue_head(&buf
->read_wait
);
219 init_waitqueue_head(&buf
->write_wait
);
220 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
223 * Write the subbuffer header for first subbuffer so we know the total
224 * duration of data gathering.
226 subbuf_header_size
= config
->cb
.subbuffer_header_size();
227 v_set(config
, &buf
->offset
, subbuf_header_size
);
228 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
229 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
230 config
->cb
.buffer_begin(buf
, tsc
, 0);
231 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
233 if (config
->cb
.buffer_create
) {
234 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
240 * Ensure the buffer is ready before setting it to allocated and setting
242 * Used for cpu hotplug vs cpumask iteration.
245 buf
->backend
.allocated
= 1;
247 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
248 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
249 chan
->backend
.cpumask
));
250 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
257 kfree(buf
->commit_cold
);
259 kfree(buf
->commit_hot
);
261 lib_ring_buffer_backend_free(&buf
->backend
);
265 static void switch_buffer_timer(unsigned long data
)
267 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
268 struct channel
*chan
= buf
->backend
.chan
;
269 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
272 * Only flush buffers periodically if readers are active.
274 if (atomic_long_read(&buf
->active_readers
))
275 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
277 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
278 mod_timer_pinned(&buf
->switch_timer
,
279 jiffies
+ chan
->switch_timer_interval
);
281 mod_timer(&buf
->switch_timer
,
282 jiffies
+ chan
->switch_timer_interval
);
286 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
288 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
290 struct channel
*chan
= buf
->backend
.chan
;
291 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
293 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
295 init_timer(&buf
->switch_timer
);
296 buf
->switch_timer
.function
= switch_buffer_timer
;
297 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
298 buf
->switch_timer
.data
= (unsigned long)buf
;
299 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
300 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
302 add_timer(&buf
->switch_timer
);
303 buf
->switch_timer_enabled
= 1;
307 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
309 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
311 struct channel
*chan
= buf
->backend
.chan
;
313 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
316 del_timer_sync(&buf
->switch_timer
);
317 buf
->switch_timer_enabled
= 0;
321 * Polling timer to check the channels for data.
323 static void read_buffer_timer(unsigned long data
)
325 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
326 struct channel
*chan
= buf
->backend
.chan
;
327 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
329 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
331 if (atomic_long_read(&buf
->active_readers
)
332 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
333 wake_up_interruptible(&buf
->read_wait
);
334 wake_up_interruptible(&chan
->read_wait
);
337 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
338 mod_timer_pinned(&buf
->read_timer
,
339 jiffies
+ chan
->read_timer_interval
);
341 mod_timer(&buf
->read_timer
,
342 jiffies
+ chan
->read_timer_interval
);
346 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
348 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
350 struct channel
*chan
= buf
->backend
.chan
;
351 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
353 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
354 || !chan
->read_timer_interval
355 || buf
->read_timer_enabled
)
358 init_timer(&buf
->read_timer
);
359 buf
->read_timer
.function
= read_buffer_timer
;
360 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
361 buf
->read_timer
.data
= (unsigned long)buf
;
363 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
364 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
366 add_timer(&buf
->read_timer
);
367 buf
->read_timer_enabled
= 1;
371 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
373 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
375 struct channel
*chan
= buf
->backend
.chan
;
376 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
378 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
379 || !chan
->read_timer_interval
380 || !buf
->read_timer_enabled
)
383 del_timer_sync(&buf
->read_timer
);
385 * do one more check to catch data that has been written in the last
388 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
389 wake_up_interruptible(&buf
->read_wait
);
390 wake_up_interruptible(&chan
->read_wait
);
392 buf
->read_timer_enabled
= 0;
395 #ifdef CONFIG_HOTPLUG_CPU
397 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
398 * @nb: notifier block
399 * @action: hotplug action to take
402 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
405 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
406 unsigned long action
,
409 unsigned int cpu
= (unsigned long)hcpu
;
410 struct channel
*chan
= container_of(nb
, struct channel
,
412 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
413 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
415 if (!chan
->cpu_hp_enable
)
418 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
421 case CPU_DOWN_FAILED
:
422 case CPU_DOWN_FAILED_FROZEN
:
424 case CPU_ONLINE_FROZEN
:
425 wake_up_interruptible(&chan
->hp_wait
);
426 lib_ring_buffer_start_switch_timer(buf
);
427 lib_ring_buffer_start_read_timer(buf
);
430 case CPU_DOWN_PREPARE
:
431 case CPU_DOWN_PREPARE_FROZEN
:
432 lib_ring_buffer_stop_switch_timer(buf
);
433 lib_ring_buffer_stop_read_timer(buf
);
437 case CPU_DEAD_FROZEN
:
439 * Performing a buffer switch on a remote CPU. Performed by
440 * the CPU responsible for doing the hotunplug after the target
441 * CPU stopped running completely. Ensures that all data
442 * from that remote CPU is flushed.
444 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
453 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
455 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
456 * that wake-up-tracing generated events are flushed before going idle (in
457 * tick_nohz). We test if the spinlock is locked to deal with the race where
458 * readers try to sample the ring buffer before we perform the switch. We let
459 * the readers retry in that case. If there is data in the buffer, the wake up
460 * is going to forbid the CPU running the reader thread from going idle.
462 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
466 struct channel
*chan
= container_of(nb
, struct channel
,
468 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
469 struct lib_ring_buffer
*buf
;
470 int cpu
= smp_processor_id();
472 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
474 * We don't support keeping the system idle with global buffers
475 * and streaming active. In order to do so, we would need to
476 * sample a non-nohz-cpumask racelessly with the nohz updates
477 * without adding synchronization overhead to nohz. Leave this
478 * use-case out for now.
483 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
485 case TICK_NOHZ_FLUSH
:
486 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
487 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
488 && chan
->read_timer_interval
489 && atomic_long_read(&buf
->active_readers
)
490 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
491 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
492 wake_up_interruptible(&buf
->read_wait
);
493 wake_up_interruptible(&chan
->read_wait
);
495 if (chan
->switch_timer_interval
)
496 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
497 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
500 spin_lock(&__get_cpu_var(ring_buffer_nohz_lock
));
501 lib_ring_buffer_stop_switch_timer(buf
);
502 lib_ring_buffer_stop_read_timer(buf
);
503 spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock
));
505 case TICK_NOHZ_RESTART
:
506 spin_lock(&__get_cpu_var(ring_buffer_nohz_lock
));
507 lib_ring_buffer_start_read_timer(buf
);
508 lib_ring_buffer_start_switch_timer(buf
);
509 spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock
));
516 void notrace
lib_ring_buffer_tick_nohz_flush(void)
518 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
522 void notrace
lib_ring_buffer_tick_nohz_stop(void)
524 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
528 void notrace
lib_ring_buffer_tick_nohz_restart(void)
530 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
533 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
538 static void channel_unregister_notifiers(struct channel
*chan
)
540 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
543 channel_iterator_unregister_notifiers(chan
);
544 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
547 * Remove the nohz notifier first, so we are certain we stop
550 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
551 &chan
->tick_nohz_notifier
);
553 * ring_buffer_nohz_lock will not be needed below, because
554 * we just removed the notifiers, which were the only source of
557 #endif /* CONFIG_NO_HZ */
558 #ifdef CONFIG_HOTPLUG_CPU
560 chan
->cpu_hp_enable
= 0;
561 for_each_online_cpu(cpu
) {
562 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
564 lib_ring_buffer_stop_switch_timer(buf
);
565 lib_ring_buffer_stop_read_timer(buf
);
568 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
570 for_each_possible_cpu(cpu
) {
571 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
573 lib_ring_buffer_stop_switch_timer(buf
);
574 lib_ring_buffer_stop_read_timer(buf
);
578 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
580 lib_ring_buffer_stop_switch_timer(buf
);
581 lib_ring_buffer_stop_read_timer(buf
);
583 channel_backend_unregister_notifiers(&chan
->backend
);
586 static void channel_free(struct channel
*chan
)
588 channel_iterator_free(chan
);
589 channel_backend_free(&chan
->backend
);
594 * channel_create - Create channel.
595 * @config: ring buffer instance configuration
596 * @name: name of the channel
597 * @priv: ring buffer client private data
598 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
599 * address mapping. It is used only by RING_BUFFER_STATIC
600 * configuration. It can be set to NULL for other backends.
601 * @subbuf_size: subbuffer size
602 * @num_subbuf: number of subbuffers
603 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
604 * padding to let readers get those sub-buffers.
605 * Used for live streaming.
606 * @read_timer_interval: Time interval (in us) to wake up pending readers.
609 * Returns NULL on failure.
611 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
612 const char *name
, void *priv
, void *buf_addr
,
614 size_t num_subbuf
, unsigned int switch_timer_interval
,
615 unsigned int read_timer_interval
)
618 struct channel
*chan
;
620 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
621 read_timer_interval
))
624 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
628 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
629 subbuf_size
, num_subbuf
);
633 ret
= channel_iterator_init(chan
);
635 goto error_free_backend
;
637 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
638 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
639 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
640 kref_init(&chan
->ref
);
641 init_waitqueue_head(&chan
->read_wait
);
642 init_waitqueue_head(&chan
->hp_wait
);
644 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
645 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
646 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
647 chan
->tick_nohz_notifier
.notifier_call
=
648 ring_buffer_tick_nohz_callback
;
649 chan
->tick_nohz_notifier
.priority
= ~0U;
650 atomic_notifier_chain_register(&tick_nohz_notifier
,
651 &chan
->tick_nohz_notifier
);
652 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
655 * In case of non-hotplug cpu, if the ring-buffer is allocated
656 * in early initcall, it will not be notified of secondary cpus.
657 * In that off case, we need to allocate for all possible cpus.
659 #ifdef CONFIG_HOTPLUG_CPU
660 chan
->cpu_hp_notifier
.notifier_call
=
661 lib_ring_buffer_cpu_hp_callback
;
662 chan
->cpu_hp_notifier
.priority
= 6;
663 register_cpu_notifier(&chan
->cpu_hp_notifier
);
666 for_each_online_cpu(cpu
) {
667 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
669 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
670 lib_ring_buffer_start_switch_timer(buf
);
671 lib_ring_buffer_start_read_timer(buf
);
672 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
674 chan
->cpu_hp_enable
= 1;
677 for_each_possible_cpu(cpu
) {
678 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
680 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
681 lib_ring_buffer_start_switch_timer(buf
);
682 lib_ring_buffer_start_read_timer(buf
);
683 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
687 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
689 lib_ring_buffer_start_switch_timer(buf
);
690 lib_ring_buffer_start_read_timer(buf
);
696 channel_backend_free(&chan
->backend
);
701 EXPORT_SYMBOL_GPL(channel_create
);
704 void channel_release(struct kref
*kref
)
706 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
711 * channel_destroy - Finalize, wait for q.s. and destroy channel.
712 * @chan: channel to destroy
715 * Call "destroy" callback, finalize channels, and then decrement the
716 * channel reference count. Note that when readers have completed data
717 * consumption of finalized channels, get_subbuf() will return -ENODATA.
718 * They should release their handle at that point. Returns the private
721 void *channel_destroy(struct channel
*chan
)
724 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
727 channel_unregister_notifiers(chan
);
729 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
731 * No need to hold cpu hotplug, because all notifiers have been
734 for_each_channel_cpu(cpu
, chan
) {
735 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
738 if (config
->cb
.buffer_finalize
)
739 config
->cb
.buffer_finalize(buf
,
742 if (buf
->backend
.allocated
)
743 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
745 * Perform flush before writing to finalized.
748 ACCESS_ONCE(buf
->finalized
) = 1;
749 wake_up_interruptible(&buf
->read_wait
);
752 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
754 if (config
->cb
.buffer_finalize
)
755 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
756 if (buf
->backend
.allocated
)
757 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
759 * Perform flush before writing to finalized.
762 ACCESS_ONCE(buf
->finalized
) = 1;
763 wake_up_interruptible(&buf
->read_wait
);
765 ACCESS_ONCE(chan
->finalized
) = 1;
766 wake_up_interruptible(&chan
->hp_wait
);
767 wake_up_interruptible(&chan
->read_wait
);
768 priv
= chan
->backend
.priv
;
769 kref_put(&chan
->ref
, channel_release
);
772 EXPORT_SYMBOL_GPL(channel_destroy
);
774 struct lib_ring_buffer
*channel_get_ring_buffer(
775 const struct lib_ring_buffer_config
*config
,
776 struct channel
*chan
, int cpu
)
778 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
779 return chan
->backend
.buf
;
781 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
783 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
785 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
787 struct channel
*chan
= buf
->backend
.chan
;
789 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
791 kref_get(&chan
->ref
);
792 smp_mb__after_atomic_inc();
795 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
797 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
799 struct channel
*chan
= buf
->backend
.chan
;
801 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
802 smp_mb__before_atomic_dec();
803 atomic_long_dec(&buf
->active_readers
);
804 kref_put(&chan
->ref
, channel_release
);
806 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
809 * Promote compiler barrier to a smp_mb().
810 * For the specific ring buffer case, this IPI call should be removed if the
811 * architecture does not reorder writes. This should eventually be provided by
812 * a separate architecture-specific infrastructure.
814 static void remote_mb(void *info
)
820 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
822 * @consumed: consumed count indicating the position where to read
823 * @produced: produced count, indicates position when to stop reading
825 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
826 * data to read at consumed position, or 0 if the get operation succeeds.
827 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
830 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
831 unsigned long *consumed
, unsigned long *produced
)
833 struct channel
*chan
= buf
->backend
.chan
;
834 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
835 unsigned long consumed_cur
, write_offset
;
839 finalized
= ACCESS_ONCE(buf
->finalized
);
841 * Read finalized before counters.
844 consumed_cur
= atomic_long_read(&buf
->consumed
);
846 * No need to issue a memory barrier between consumed count read and
847 * write offset read, because consumed count can only change
848 * concurrently in overwrite mode, and we keep a sequence counter
849 * identifier derived from the write offset to check we are getting
850 * the same sub-buffer we are expecting (the sub-buffers are atomically
851 * "tagged" upon writes, tags are checked upon read).
853 write_offset
= v_read(config
, &buf
->offset
);
856 * Check that we are not about to read the same subbuffer in
857 * which the writer head is.
859 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
863 *consumed
= consumed_cur
;
864 *produced
= subbuf_trunc(write_offset
, chan
);
870 * The memory barriers __wait_event()/wake_up_interruptible() take care
871 * of "raw_spin_is_locked" memory ordering.
875 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
880 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
883 * lib_ring_buffer_put_snapshot - move consumed counter forward
885 * Should only be called from consumer context.
887 * @consumed_new: new consumed count value
889 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
890 unsigned long consumed_new
)
892 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
893 struct channel
*chan
= bufb
->chan
;
894 unsigned long consumed
;
896 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
899 * Only push the consumed value forward.
900 * If the consumed cmpxchg fails, this is because we have been pushed by
901 * the writer in flight recorder mode.
903 consumed
= atomic_long_read(&buf
->consumed
);
904 while ((long) consumed
- (long) consumed_new
< 0)
905 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
907 /* Wake-up the metadata producer */
908 wake_up_interruptible(&buf
->write_wait
);
910 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
913 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
915 * @consumed: consumed count indicating the position where to read
917 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
918 * data to read at consumed position, or 0 if the get operation succeeds.
919 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
921 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
922 unsigned long consumed
)
924 struct channel
*chan
= buf
->backend
.chan
;
925 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
926 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
931 finalized
= ACCESS_ONCE(buf
->finalized
);
933 * Read finalized before counters.
936 consumed_cur
= atomic_long_read(&buf
->consumed
);
937 consumed_idx
= subbuf_index(consumed
, chan
);
938 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
940 * Make sure we read the commit count before reading the buffer
941 * data and the write offset. Correct consumed offset ordering
942 * wrt commit count is insured by the use of cmpxchg to update
943 * the consumed offset.
944 * smp_call_function_single can fail if the remote CPU is offline,
945 * this is OK because then there is no wmb to execute there.
946 * If our thread is executing on the same CPU as the on the buffers
947 * belongs to, we don't have to synchronize it at all. If we are
948 * migrated, the scheduler will take care of the memory barriers.
949 * Normally, smp_call_function_single() should ensure program order when
950 * executing the remote function, which implies that it surrounds the
951 * function execution with :
962 * However, smp_call_function_single() does not seem to clearly execute
963 * such barriers. It depends on spinlock semantic to provide the barrier
964 * before executing the IPI and, when busy-looping, csd_lock_wait only
965 * executes smp_mb() when it has to wait for the other CPU.
967 * I don't trust this code. Therefore, let's add the smp_mb() sequence
968 * required ourself, even if duplicated. It has no performance impact
971 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
972 * read and write vs write. They do not ensure core synchronization. We
973 * really have to ensure total order between the 3 barriers running on
976 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
977 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
978 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
979 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
980 /* Total order with IPI handler smp_mb() */
982 smp_call_function_single(buf
->backend
.cpu
,
984 /* Total order with IPI handler smp_mb() */
988 /* Total order with IPI handler smp_mb() */
990 smp_call_function(remote_mb
, NULL
, 1);
991 /* Total order with IPI handler smp_mb() */
996 * Local rmb to match the remote wmb to read the commit count
997 * before the buffer data and the write offset.
1002 write_offset
= v_read(config
, &buf
->offset
);
1005 * Check that the buffer we are getting is after or at consumed_cur
1008 if ((long) subbuf_trunc(consumed
, chan
)
1009 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1013 * Check that the subbuffer we are trying to consume has been
1014 * already fully committed.
1016 if (((commit_count
- chan
->backend
.subbuf_size
)
1017 & chan
->commit_count_mask
)
1018 - (buf_trunc(consumed_cur
, chan
)
1019 >> chan
->backend
.num_subbuf_order
)
1024 * Check that we are not about to read the same subbuffer in
1025 * which the writer head is.
1027 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1032 * Failure to get the subbuffer causes a busy-loop retry without going
1033 * to a wait queue. These are caused by short-lived race windows where
1034 * the writer is getting access to a subbuffer we were trying to get
1035 * access to. Also checks that the "consumed" buffer count we are
1036 * looking for matches the one contained in the subbuffer id.
1038 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1039 consumed_idx
, buf_trunc_val(consumed
, chan
));
1042 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1044 buf
->get_subbuf_consumed
= consumed
;
1045 buf
->get_subbuf
= 1;
1051 * The memory barriers __wait_event()/wake_up_interruptible() take care
1052 * of "raw_spin_is_locked" memory ordering.
1056 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1061 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1064 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1067 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1069 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1070 struct channel
*chan
= bufb
->chan
;
1071 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1072 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1074 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1076 if (!buf
->get_subbuf
) {
1078 * Reader puts a subbuffer it did not get.
1080 CHAN_WARN_ON(chan
, 1);
1083 consumed
= buf
->get_subbuf_consumed
;
1084 buf
->get_subbuf
= 0;
1087 * Clear the records_unread counter. (overruns counter)
1088 * Can still be non-zero if a file reader simply grabbed the data
1089 * without using iterators.
1090 * Can be below zero if an iterator is used on a snapshot more than
1093 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1094 v_add(config
, v_read(config
,
1095 &bufb
->array
[read_sb_bindex
]->records_unread
),
1096 &bufb
->records_read
);
1097 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1098 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1099 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1100 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1103 * Exchange the reader subbuffer with the one we put in its place in the
1104 * writer subbuffer table. Expect the original consumed count. If
1105 * update_read_sb_index fails, this is because the writer updated the
1106 * subbuffer concurrently. We should therefore keep the subbuffer we
1107 * currently have: it has become invalid to try reading this sub-buffer
1108 * consumed count value anyway.
1110 consumed_idx
= subbuf_index(consumed
, chan
);
1111 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1112 consumed_idx
, buf_trunc_val(consumed
, chan
));
1114 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1115 * if the writer concurrently updated it.
1118 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1121 * cons_offset is an iterator on all subbuffer offsets between the reader
1122 * position and the writer position. (inclusive)
1125 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1126 struct channel
*chan
,
1127 unsigned long cons_offset
,
1130 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1131 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1133 cons_idx
= subbuf_index(cons_offset
, chan
);
1134 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1135 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1137 if (subbuf_offset(commit_count
, chan
) != 0)
1139 "ring buffer %s, cpu %d: "
1140 "commit count in subbuffer %lu,\n"
1141 "expecting multiples of %lu bytes\n"
1142 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1143 chan
->backend
.name
, cpu
, cons_idx
,
1144 chan
->backend
.subbuf_size
,
1145 commit_count
, commit_count_sb
);
1147 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1148 chan
->backend
.name
, cpu
, commit_count
);
1152 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1153 struct channel
*chan
,
1154 void *priv
, int cpu
)
1156 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1157 unsigned long write_offset
, cons_offset
;
1160 * No need to order commit_count, write_offset and cons_offset reads
1161 * because we execute at teardown when no more writer nor reader
1162 * references are left.
1164 write_offset
= v_read(config
, &buf
->offset
);
1165 cons_offset
= atomic_long_read(&buf
->consumed
);
1166 if (write_offset
!= cons_offset
)
1168 "ring buffer %s, cpu %d: "
1169 "non-consumed data\n"
1170 " [ %lu bytes written, %lu bytes read ]\n",
1171 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1173 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1174 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1177 cons_offset
= subbuf_align(cons_offset
, chan
))
1178 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1183 void lib_ring_buffer_print_errors(struct channel
*chan
,
1184 struct lib_ring_buffer
*buf
, int cpu
)
1186 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1187 void *priv
= chan
->backend
.priv
;
1189 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1190 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1191 "%lu records overrun\n",
1193 v_read(config
, &buf
->records_count
),
1194 v_read(config
, &buf
->records_overrun
));
1196 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1197 "%lu records overrun\n",
1198 chan
->backend
.name
, cpu
,
1199 v_read(config
, &buf
->records_count
),
1200 v_read(config
, &buf
->records_overrun
));
1202 if (v_read(config
, &buf
->records_lost_full
)
1203 || v_read(config
, &buf
->records_lost_wrap
)
1204 || v_read(config
, &buf
->records_lost_big
))
1206 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1207 " [ %lu buffer full, %lu nest buffer wrap-around, "
1208 "%lu event too big ]\n",
1209 chan
->backend
.name
, cpu
,
1210 v_read(config
, &buf
->records_lost_full
),
1211 v_read(config
, &buf
->records_lost_wrap
),
1212 v_read(config
, &buf
->records_lost_big
));
1214 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1218 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1220 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1223 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1224 struct channel
*chan
,
1225 struct switch_offsets
*offsets
,
1228 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1229 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1230 unsigned long commit_count
;
1232 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1235 * Order all writes to buffer before the commit count update that will
1236 * determine that the subbuffer is full.
1238 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1240 * Must write slot data before incrementing commit count. This
1241 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1247 v_add(config
, config
->cb
.subbuffer_header_size(),
1248 &buf
->commit_hot
[oldidx
].cc
);
1249 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1250 /* Check if the written buffer has to be delivered */
1251 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1252 commit_count
, oldidx
);
1253 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1254 offsets
->old
, commit_count
,
1255 config
->cb
.subbuffer_header_size());
1259 * lib_ring_buffer_switch_old_end: switch old subbuffer
1261 * Note : offset_old should never be 0 here. It is ok, because we never perform
1262 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1263 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1267 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1268 struct channel
*chan
,
1269 struct switch_offsets
*offsets
,
1272 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1273 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1274 unsigned long commit_count
, padding_size
, data_size
;
1276 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1277 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1278 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1281 * Order all writes to buffer before the commit count update that will
1282 * determine that the subbuffer is full.
1284 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1286 * Must write slot data before incrementing commit count. This
1287 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1293 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1294 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1295 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1296 commit_count
, oldidx
);
1297 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1298 offsets
->old
, commit_count
,
1303 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1305 * This code can be executed unordered : writers may already have written to the
1306 * sub-buffer before this code gets executed, caution. The commit makes sure
1307 * that this code is executed before the deliver of this sub-buffer.
1310 void lib_ring_buffer_switch_new_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 beginidx
= subbuf_index(offsets
->begin
, chan
);
1317 unsigned long commit_count
;
1319 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
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
[beginidx
].cc
);
1336 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1337 /* Check if the written buffer has to be delivered */
1338 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1339 commit_count
, beginidx
);
1340 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1341 offsets
->begin
, commit_count
,
1342 config
->cb
.subbuffer_header_size());
1346 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1348 * The only remaining threads could be the ones with pending commits. They will
1349 * have to do the deliver themselves.
1352 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1353 struct channel
*chan
,
1354 struct switch_offsets
*offsets
,
1357 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1358 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1359 unsigned long commit_count
, padding_size
, data_size
;
1361 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1362 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1363 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1366 * Order all writes to buffer before the commit count update that will
1367 * determine that the subbuffer is full.
1369 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1371 * Must write slot data before incrementing commit count. This
1372 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1378 v_add(config
, padding_size
, &buf
->commit_hot
[endidx
].cc
);
1379 commit_count
= v_read(config
, &buf
->commit_hot
[endidx
].cc
);
1380 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1381 commit_count
, endidx
);
1382 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1383 offsets
->end
, commit_count
,
1390 * !0 if execution must be aborted.
1393 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1394 struct lib_ring_buffer
*buf
,
1395 struct channel
*chan
,
1396 struct switch_offsets
*offsets
,
1399 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1402 offsets
->begin
= v_read(config
, &buf
->offset
);
1403 offsets
->old
= offsets
->begin
;
1404 offsets
->switch_old_start
= 0;
1405 off
= subbuf_offset(offsets
->begin
, chan
);
1407 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1410 * Ensure we flush the header of an empty subbuffer when doing the
1411 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1412 * total data gathering duration even if there were no records saved
1413 * after the last buffer switch.
1414 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1415 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1416 * subbuffer header as appropriate.
1417 * The next record that reserves space will be responsible for
1418 * populating the following subbuffer header. We choose not to populate
1419 * the next subbuffer header here because we want to be able to use
1420 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1421 * buffer flush, which must guarantee that all the buffer content
1422 * (records and header timestamps) are visible to the reader. This is
1423 * required for quiescence guarantees for the fusion merge.
1425 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1426 if (unlikely(off
== 0)) {
1428 * A final flush that encounters an empty
1429 * sub-buffer cannot switch buffer if a
1430 * reader is located within this sub-buffer.
1431 * Anyway, the purpose of final flushing of a
1432 * sub-buffer at offset 0 is to handle the case
1433 * of entirely empty stream.
1435 if (unlikely(subbuf_trunc(offsets
->begin
, chan
)
1436 - subbuf_trunc((unsigned long)
1437 atomic_long_read(&buf
->consumed
), chan
)
1438 >= chan
->backend
.buf_size
))
1441 * The client does not save any header information.
1442 * Don't switch empty subbuffer on finalize, because it
1443 * is invalid to deliver a completely empty subbuffer.
1445 if (!config
->cb
.subbuffer_header_size())
1448 * Need to write the subbuffer start header on finalize.
1450 offsets
->switch_old_start
= 1;
1452 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1454 return -1; /* we do not have to switch : buffer is empty */
1455 /* Note: old points to the next subbuf at offset 0 */
1456 offsets
->end
= offsets
->begin
;
1461 * Force a sub-buffer switch. This operation is completely reentrant : can be
1462 * called while tracing is active with absolutely no lock held.
1464 * Note, however, that as a v_cmpxchg is used for some atomic
1465 * operations, this function must be called from the CPU which owns the buffer
1466 * for a ACTIVE flush.
1468 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1470 struct channel
*chan
= buf
->backend
.chan
;
1471 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1472 struct switch_offsets offsets
;
1473 unsigned long oldidx
;
1479 * Perform retryable operations.
1482 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1484 return; /* Switch not needed */
1485 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1489 * Atomically update last_tsc. This update races against concurrent
1490 * atomic updates, but the race will always cause supplementary full TSC
1491 * records, never the opposite (missing a full TSC record when it would
1494 save_last_tsc(config
, buf
, tsc
);
1497 * Push the reader if necessary
1499 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1501 oldidx
= subbuf_index(offsets
.old
, chan
);
1502 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1505 * May need to populate header start on SWITCH_FLUSH.
1507 if (offsets
.switch_old_start
) {
1508 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1509 offsets
.old
+= config
->cb
.subbuffer_header_size();
1513 * Switch old subbuffer.
1515 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1517 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1519 static void remote_switch(void *info
)
1521 struct lib_ring_buffer
*buf
= info
;
1523 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1526 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1528 struct channel
*chan
= buf
->backend
.chan
;
1529 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1533 * With global synchronization we don't need to use the IPI scheme.
1535 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1536 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1541 * Taking lock on CPU hotplug to ensure two things: first, that the
1542 * target cpu is not taken concurrently offline while we are within
1543 * smp_call_function_single() (I don't trust that get_cpu() on the
1544 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1545 * confirmed)). Secondly, if it happens that the CPU is not online, our
1546 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1547 * CPU hotplug handlers, which can also perform a remote subbuffer
1551 ret
= smp_call_function_single(buf
->backend
.cpu
,
1552 remote_switch
, buf
, 1);
1554 /* Remote CPU is offline, do it ourself. */
1555 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1559 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1564 * -ENOSPC if event size is too large for packet.
1565 * -ENOBUFS if there is currently not enough space in buffer for the event.
1566 * -EIO if data cannot be written into the buffer for any other reason.
1569 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1570 struct channel
*chan
,
1571 struct switch_offsets
*offsets
,
1572 struct lib_ring_buffer_ctx
*ctx
)
1574 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1575 unsigned long reserve_commit_diff
, offset_cmp
;
1578 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1579 offsets
->old
= offsets
->begin
;
1580 offsets
->switch_new_start
= 0;
1581 offsets
->switch_new_end
= 0;
1582 offsets
->switch_old_end
= 0;
1583 offsets
->pre_header_padding
= 0;
1585 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1586 if ((int64_t) ctx
->tsc
== -EIO
)
1589 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1590 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1592 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1593 offsets
->switch_new_start
= 1; /* For offsets->begin */
1595 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1597 &offsets
->pre_header_padding
,
1600 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1603 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1604 offsets
->size
> chan
->backend
.subbuf_size
)) {
1605 offsets
->switch_old_end
= 1; /* For offsets->old */
1606 offsets
->switch_new_start
= 1; /* For offsets->begin */
1609 if (unlikely(offsets
->switch_new_start
)) {
1610 unsigned long sb_index
, commit_count
;
1613 * We are typically not filling the previous buffer completely.
1615 if (likely(offsets
->switch_old_end
))
1616 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1617 offsets
->begin
= offsets
->begin
1618 + config
->cb
.subbuffer_header_size();
1619 /* Test new buffer integrity */
1620 sb_index
= subbuf_index(offsets
->begin
, chan
);
1622 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1623 * lib_ring_buffer_check_deliver() has the matching
1624 * memory barriers required around commit_cold cc_sb
1625 * updates to ensure reserve and commit counter updates
1626 * are not seen reordered when updated by another CPU.
1629 commit_count
= v_read(config
,
1630 &buf
->commit_cold
[sb_index
].cc_sb
);
1631 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1633 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1635 * The reserve counter have been concurrently updated
1636 * while we read the commit counter. This means the
1637 * commit counter we read might not match buf->offset
1638 * due to concurrent update. We therefore need to retry.
1642 reserve_commit_diff
=
1643 (buf_trunc(offsets
->begin
, chan
)
1644 >> chan
->backend
.num_subbuf_order
)
1645 - (commit_count
& chan
->commit_count_mask
);
1646 if (likely(reserve_commit_diff
== 0)) {
1647 /* Next subbuffer not being written to. */
1648 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1649 subbuf_trunc(offsets
->begin
, chan
)
1650 - subbuf_trunc((unsigned long)
1651 atomic_long_read(&buf
->consumed
), chan
)
1652 >= chan
->backend
.buf_size
)) {
1654 * We do not overwrite non consumed buffers
1655 * and we are full : record is lost.
1657 v_inc(config
, &buf
->records_lost_full
);
1661 * Next subbuffer not being written to, and we
1662 * are either in overwrite mode or the buffer is
1663 * not full. It's safe to write in this new
1669 * Next subbuffer reserve offset does not match the
1670 * commit offset, and this did not involve update to the
1671 * reserve counter. Drop record in producer-consumer and
1672 * overwrite mode. Caused by either a writer OOPS or
1673 * too many nested writes over a reserve/commit pair.
1675 v_inc(config
, &buf
->records_lost_wrap
);
1679 config
->cb
.record_header_size(config
, chan
,
1681 &offsets
->pre_header_padding
,
1684 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1687 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1688 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1690 * Record too big for subbuffers, report error, don't
1691 * complete the sub-buffer switch.
1693 v_inc(config
, &buf
->records_lost_big
);
1697 * We just made a successful buffer switch and the
1698 * record fits in the new subbuffer. Let's write.
1703 * Record fits in the current buffer and we are not on a switch
1704 * boundary. It's safe to write.
1707 offsets
->end
= offsets
->begin
+ offsets
->size
;
1709 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1711 * The offset_end will fall at the very beginning of the next
1714 offsets
->switch_new_end
= 1; /* For offsets->begin */
1720 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1721 * @ctx: ring buffer context.
1723 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1724 * -EIO for other errors, else returns 0.
1725 * It will take care of sub-buffer switching.
1727 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1729 struct channel
*chan
= ctx
->chan
;
1730 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1731 struct lib_ring_buffer
*buf
;
1732 struct switch_offsets offsets
;
1735 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1736 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1738 buf
= chan
->backend
.buf
;
1744 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1748 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1753 * Atomically update last_tsc. This update races against concurrent
1754 * atomic updates, but the race will always cause supplementary full TSC
1755 * records, never the opposite (missing a full TSC record when it would
1758 save_last_tsc(config
, buf
, ctx
->tsc
);
1761 * Push the reader if necessary
1763 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1766 * Clear noref flag for this subbuffer.
1768 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1769 subbuf_index(offsets
.end
- 1, chan
));
1772 * Switch old subbuffer if needed.
1774 if (unlikely(offsets
.switch_old_end
)) {
1775 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1776 subbuf_index(offsets
.old
- 1, chan
));
1777 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1781 * Populate new subbuffer.
1783 if (unlikely(offsets
.switch_new_start
))
1784 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1786 if (unlikely(offsets
.switch_new_end
))
1787 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1789 ctx
->slot_size
= offsets
.size
;
1790 ctx
->pre_offset
= offsets
.begin
;
1791 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1794 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
1796 int __init
init_lib_ring_buffer_frontend(void)
1800 for_each_possible_cpu(cpu
)
1801 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
1805 module_init(init_lib_ring_buffer_frontend
);
1807 void __exit
exit_lib_ring_buffer_frontend(void)
1811 module_exit(exit_lib_ring_buffer_frontend
);