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 "../../lttng-tracer-core.h"
59 #include "../../wrapper/ringbuffer/config.h"
60 #include "../../wrapper/ringbuffer/backend.h"
61 #include "../../wrapper/ringbuffer/frontend.h"
62 #include "../../wrapper/ringbuffer/iterator.h"
63 #include "../../wrapper/ringbuffer/nohz.h"
66 * Internal structure representing offsets to use at a sub-buffer switch.
68 struct switch_offsets
{
69 unsigned long begin
, end
, old
;
70 size_t pre_header_padding
, size
;
71 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
82 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
83 #endif /* CONFIG_NO_HZ */
85 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
87 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
88 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
91 void lib_ring_buffer_print_errors(struct channel
*chan
,
92 struct lib_ring_buffer
*buf
, int cpu
);
95 * Must be called under cpu hotplug protection.
97 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
99 struct channel
*chan
= buf
->backend
.chan
;
101 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
102 kfree(buf
->commit_hot
);
103 kfree(buf
->commit_cold
);
105 lib_ring_buffer_backend_free(&buf
->backend
);
109 * lib_ring_buffer_reset - Reset ring buffer to initial values.
112 * Effectively empty the ring buffer. Should be called when the buffer is not
113 * used for writing. The ring buffer can be opened for reading, but the reader
114 * should not be using the iterator concurrently with reset. The previous
115 * current iterator record is reset.
117 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
119 struct channel
*chan
= buf
->backend
.chan
;
120 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
124 * Reset iterator first. It will put the subbuffer if it currently holds
127 lib_ring_buffer_iterator_reset(buf
);
128 v_set(config
, &buf
->offset
, 0);
129 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
130 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
131 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
132 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
134 atomic_long_set(&buf
->consumed
, 0);
135 atomic_set(&buf
->record_disabled
, 0);
136 v_set(config
, &buf
->last_tsc
, 0);
137 lib_ring_buffer_backend_reset(&buf
->backend
);
138 /* Don't reset number of active readers */
139 v_set(config
, &buf
->records_lost_full
, 0);
140 v_set(config
, &buf
->records_lost_wrap
, 0);
141 v_set(config
, &buf
->records_lost_big
, 0);
142 v_set(config
, &buf
->records_count
, 0);
143 v_set(config
, &buf
->records_overrun
, 0);
146 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
149 * channel_reset - Reset channel to initial values.
152 * Effectively empty the channel. Should be called when the channel is not used
153 * for writing. The channel can be opened for reading, but the reader should not
154 * be using the iterator concurrently with reset. The previous current iterator
157 void channel_reset(struct channel
*chan
)
160 * Reset iterators first. Will put the subbuffer if held for reading.
162 channel_iterator_reset(chan
);
163 atomic_set(&chan
->record_disabled
, 0);
164 /* Don't reset commit_count_mask, still valid */
165 channel_backend_reset(&chan
->backend
);
166 /* Don't reset switch/read timer interval */
167 /* Don't reset notifiers and notifier enable bits */
168 /* Don't reset reader reference count */
170 EXPORT_SYMBOL_GPL(channel_reset
);
173 * Must be called under cpu hotplug protection.
175 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
176 struct channel_backend
*chanb
, int cpu
)
178 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
179 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
180 void *priv
= chanb
->priv
;
181 size_t subbuf_header_size
;
185 /* Test for cpu hotplug */
186 if (buf
->backend
.allocated
)
190 * Paranoia: per cpu dynamic allocation is not officially documented as
191 * zeroing the memory, so let's do it here too, just in case.
193 memset(buf
, 0, sizeof(*buf
));
195 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
200 kzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
201 * chan
->backend
.num_subbuf
,
202 1 << INTERNODE_CACHE_SHIFT
),
203 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
204 if (!buf
->commit_hot
) {
210 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
211 * chan
->backend
.num_subbuf
,
212 1 << INTERNODE_CACHE_SHIFT
),
213 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
214 if (!buf
->commit_cold
) {
219 init_waitqueue_head(&buf
->read_wait
);
220 init_waitqueue_head(&buf
->write_wait
);
221 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
224 * Write the subbuffer header for first subbuffer so we know the total
225 * duration of data gathering.
227 subbuf_header_size
= config
->cb
.subbuffer_header_size();
228 v_set(config
, &buf
->offset
, subbuf_header_size
);
229 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
230 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
231 config
->cb
.buffer_begin(buf
, tsc
, 0);
232 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
234 if (config
->cb
.buffer_create
) {
235 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
241 * Ensure the buffer is ready before setting it to allocated and setting
243 * Used for cpu hotplug vs cpumask iteration.
246 buf
->backend
.allocated
= 1;
248 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
249 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
250 chan
->backend
.cpumask
));
251 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
258 kfree(buf
->commit_cold
);
260 kfree(buf
->commit_hot
);
262 lib_ring_buffer_backend_free(&buf
->backend
);
266 static void switch_buffer_timer(unsigned long data
)
268 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
269 struct channel
*chan
= buf
->backend
.chan
;
270 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
273 * Only flush buffers periodically if readers are active.
275 if (atomic_long_read(&buf
->active_readers
))
276 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
278 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
279 mod_timer_pinned(&buf
->switch_timer
,
280 jiffies
+ chan
->switch_timer_interval
);
282 mod_timer(&buf
->switch_timer
,
283 jiffies
+ chan
->switch_timer_interval
);
287 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
289 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
291 struct channel
*chan
= buf
->backend
.chan
;
292 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
294 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
296 init_timer(&buf
->switch_timer
);
297 buf
->switch_timer
.function
= switch_buffer_timer
;
298 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
299 buf
->switch_timer
.data
= (unsigned long)buf
;
300 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
301 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
303 add_timer(&buf
->switch_timer
);
304 buf
->switch_timer_enabled
= 1;
308 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
310 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
312 struct channel
*chan
= buf
->backend
.chan
;
314 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
317 del_timer_sync(&buf
->switch_timer
);
318 buf
->switch_timer_enabled
= 0;
322 * Polling timer to check the channels for data.
324 static void read_buffer_timer(unsigned long data
)
326 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
327 struct channel
*chan
= buf
->backend
.chan
;
328 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
330 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
332 if (atomic_long_read(&buf
->active_readers
)
333 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
334 wake_up_interruptible(&buf
->read_wait
);
335 wake_up_interruptible(&chan
->read_wait
);
338 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
339 mod_timer_pinned(&buf
->read_timer
,
340 jiffies
+ chan
->read_timer_interval
);
342 mod_timer(&buf
->read_timer
,
343 jiffies
+ chan
->read_timer_interval
);
347 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
349 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
351 struct channel
*chan
= buf
->backend
.chan
;
352 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
354 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
355 || !chan
->read_timer_interval
356 || buf
->read_timer_enabled
)
359 init_timer(&buf
->read_timer
);
360 buf
->read_timer
.function
= read_buffer_timer
;
361 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
362 buf
->read_timer
.data
= (unsigned long)buf
;
364 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
365 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
367 add_timer(&buf
->read_timer
);
368 buf
->read_timer_enabled
= 1;
372 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
374 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
376 struct channel
*chan
= buf
->backend
.chan
;
377 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
379 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
380 || !chan
->read_timer_interval
381 || !buf
->read_timer_enabled
)
384 del_timer_sync(&buf
->read_timer
);
386 * do one more check to catch data that has been written in the last
389 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
390 wake_up_interruptible(&buf
->read_wait
);
391 wake_up_interruptible(&chan
->read_wait
);
393 buf
->read_timer_enabled
= 0;
396 #ifdef CONFIG_HOTPLUG_CPU
398 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
399 * @nb: notifier block
400 * @action: hotplug action to take
403 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
406 int __cpuinit
lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
407 unsigned long action
,
410 unsigned int cpu
= (unsigned long)hcpu
;
411 struct channel
*chan
= container_of(nb
, struct channel
,
413 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
414 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
416 if (!chan
->cpu_hp_enable
)
419 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
422 case CPU_DOWN_FAILED
:
423 case CPU_DOWN_FAILED_FROZEN
:
425 case CPU_ONLINE_FROZEN
:
426 wake_up_interruptible(&chan
->hp_wait
);
427 lib_ring_buffer_start_switch_timer(buf
);
428 lib_ring_buffer_start_read_timer(buf
);
431 case CPU_DOWN_PREPARE
:
432 case CPU_DOWN_PREPARE_FROZEN
:
433 lib_ring_buffer_stop_switch_timer(buf
);
434 lib_ring_buffer_stop_read_timer(buf
);
438 case CPU_DEAD_FROZEN
:
440 * Performing a buffer switch on a remote CPU. Performed by
441 * the CPU responsible for doing the hotunplug after the target
442 * CPU stopped running completely. Ensures that all data
443 * from that remote CPU is flushed.
445 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
454 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
456 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
457 * that wake-up-tracing generated events are flushed before going idle (in
458 * tick_nohz). We test if the spinlock is locked to deal with the race where
459 * readers try to sample the ring buffer before we perform the switch. We let
460 * the readers retry in that case. If there is data in the buffer, the wake up
461 * is going to forbid the CPU running the reader thread from going idle.
463 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
467 struct channel
*chan
= container_of(nb
, struct channel
,
469 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
470 struct lib_ring_buffer
*buf
;
471 int cpu
= smp_processor_id();
473 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
475 * We don't support keeping the system idle with global buffers
476 * and streaming active. In order to do so, we would need to
477 * sample a non-nohz-cpumask racelessly with the nohz updates
478 * without adding synchronization overhead to nohz. Leave this
479 * use-case out for now.
484 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
486 case TICK_NOHZ_FLUSH
:
487 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
488 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
489 && chan
->read_timer_interval
490 && atomic_long_read(&buf
->active_readers
)
491 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
492 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
493 wake_up_interruptible(&buf
->read_wait
);
494 wake_up_interruptible(&chan
->read_wait
);
496 if (chan
->switch_timer_interval
)
497 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
498 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
501 spin_lock(&__get_cpu_var(ring_buffer_nohz_lock
));
502 lib_ring_buffer_stop_switch_timer(buf
);
503 lib_ring_buffer_stop_read_timer(buf
);
504 spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock
));
506 case TICK_NOHZ_RESTART
:
507 spin_lock(&__get_cpu_var(ring_buffer_nohz_lock
));
508 lib_ring_buffer_start_read_timer(buf
);
509 lib_ring_buffer_start_switch_timer(buf
);
510 spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock
));
517 void notrace
lib_ring_buffer_tick_nohz_flush(void)
519 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
523 void notrace
lib_ring_buffer_tick_nohz_stop(void)
525 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
529 void notrace
lib_ring_buffer_tick_nohz_restart(void)
531 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
534 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
539 static void channel_unregister_notifiers(struct channel
*chan
)
541 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
544 channel_iterator_unregister_notifiers(chan
);
545 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
548 * Remove the nohz notifier first, so we are certain we stop
551 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
552 &chan
->tick_nohz_notifier
);
554 * ring_buffer_nohz_lock will not be needed below, because
555 * we just removed the notifiers, which were the only source of
558 #endif /* CONFIG_NO_HZ */
559 #ifdef CONFIG_HOTPLUG_CPU
561 chan
->cpu_hp_enable
= 0;
562 for_each_online_cpu(cpu
) {
563 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
565 lib_ring_buffer_stop_switch_timer(buf
);
566 lib_ring_buffer_stop_read_timer(buf
);
569 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
571 for_each_possible_cpu(cpu
) {
572 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
574 lib_ring_buffer_stop_switch_timer(buf
);
575 lib_ring_buffer_stop_read_timer(buf
);
579 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
581 lib_ring_buffer_stop_switch_timer(buf
);
582 lib_ring_buffer_stop_read_timer(buf
);
584 channel_backend_unregister_notifiers(&chan
->backend
);
587 static void channel_free(struct channel
*chan
)
589 channel_iterator_free(chan
);
590 channel_backend_free(&chan
->backend
);
595 * channel_create - Create channel.
596 * @config: ring buffer instance configuration
597 * @name: name of the channel
598 * @priv: ring buffer client private data
599 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
600 * address mapping. It is used only by RING_BUFFER_STATIC
601 * configuration. It can be set to NULL for other backends.
602 * @subbuf_size: subbuffer size
603 * @num_subbuf: number of subbuffers
604 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
605 * padding to let readers get those sub-buffers.
606 * Used for live streaming.
607 * @read_timer_interval: Time interval (in us) to wake up pending readers.
610 * Returns NULL on failure.
612 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
613 const char *name
, void *priv
, void *buf_addr
,
615 size_t num_subbuf
, unsigned int switch_timer_interval
,
616 unsigned int read_timer_interval
)
619 struct channel
*chan
;
621 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
622 read_timer_interval
))
625 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
629 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
630 subbuf_size
, num_subbuf
);
634 ret
= channel_iterator_init(chan
);
636 goto error_free_backend
;
638 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
639 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
640 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
641 kref_init(&chan
->ref
);
642 init_waitqueue_head(&chan
->read_wait
);
643 init_waitqueue_head(&chan
->hp_wait
);
645 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
646 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
647 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
648 chan
->tick_nohz_notifier
.notifier_call
=
649 ring_buffer_tick_nohz_callback
;
650 chan
->tick_nohz_notifier
.priority
= ~0U;
651 atomic_notifier_chain_register(&tick_nohz_notifier
,
652 &chan
->tick_nohz_notifier
);
653 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
656 * In case of non-hotplug cpu, if the ring-buffer is allocated
657 * in early initcall, it will not be notified of secondary cpus.
658 * In that off case, we need to allocate for all possible cpus.
660 #ifdef CONFIG_HOTPLUG_CPU
661 chan
->cpu_hp_notifier
.notifier_call
=
662 lib_ring_buffer_cpu_hp_callback
;
663 chan
->cpu_hp_notifier
.priority
= 6;
664 register_cpu_notifier(&chan
->cpu_hp_notifier
);
667 for_each_online_cpu(cpu
) {
668 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
670 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
671 lib_ring_buffer_start_switch_timer(buf
);
672 lib_ring_buffer_start_read_timer(buf
);
673 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
675 chan
->cpu_hp_enable
= 1;
678 for_each_possible_cpu(cpu
) {
679 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
681 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
682 lib_ring_buffer_start_switch_timer(buf
);
683 lib_ring_buffer_start_read_timer(buf
);
684 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
688 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
690 lib_ring_buffer_start_switch_timer(buf
);
691 lib_ring_buffer_start_read_timer(buf
);
697 channel_backend_free(&chan
->backend
);
702 EXPORT_SYMBOL_GPL(channel_create
);
705 void channel_release(struct kref
*kref
)
707 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
712 * channel_destroy - Finalize, wait for q.s. and destroy channel.
713 * @chan: channel to destroy
716 * Call "destroy" callback, finalize channels, and then decrement the
717 * channel reference count. Note that when readers have completed data
718 * consumption of finalized channels, get_subbuf() will return -ENODATA.
719 * They should release their handle at that point. Returns the private
722 void *channel_destroy(struct channel
*chan
)
725 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
728 channel_unregister_notifiers(chan
);
730 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
732 * No need to hold cpu hotplug, because all notifiers have been
735 for_each_channel_cpu(cpu
, chan
) {
736 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
739 if (config
->cb
.buffer_finalize
)
740 config
->cb
.buffer_finalize(buf
,
743 if (buf
->backend
.allocated
)
744 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
746 * Perform flush before writing to finalized.
749 ACCESS_ONCE(buf
->finalized
) = 1;
750 wake_up_interruptible(&buf
->read_wait
);
753 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
755 if (config
->cb
.buffer_finalize
)
756 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
757 if (buf
->backend
.allocated
)
758 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
760 * Perform flush before writing to finalized.
763 ACCESS_ONCE(buf
->finalized
) = 1;
764 wake_up_interruptible(&buf
->read_wait
);
766 ACCESS_ONCE(chan
->finalized
) = 1;
767 wake_up_interruptible(&chan
->hp_wait
);
768 wake_up_interruptible(&chan
->read_wait
);
769 priv
= chan
->backend
.priv
;
770 kref_put(&chan
->ref
, channel_release
);
773 EXPORT_SYMBOL_GPL(channel_destroy
);
775 struct lib_ring_buffer
*channel_get_ring_buffer(
776 const struct lib_ring_buffer_config
*config
,
777 struct channel
*chan
, int cpu
)
779 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
780 return chan
->backend
.buf
;
782 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
784 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
786 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
788 struct channel
*chan
= buf
->backend
.chan
;
790 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
792 kref_get(&chan
->ref
);
793 smp_mb__after_atomic_inc();
796 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
798 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
800 struct channel
*chan
= buf
->backend
.chan
;
802 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
803 smp_mb__before_atomic_dec();
804 atomic_long_dec(&buf
->active_readers
);
805 kref_put(&chan
->ref
, channel_release
);
807 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
810 * Promote compiler barrier to a smp_mb().
811 * For the specific ring buffer case, this IPI call should be removed if the
812 * architecture does not reorder writes. This should eventually be provided by
813 * a separate architecture-specific infrastructure.
815 static void remote_mb(void *info
)
821 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
823 * @consumed: consumed count indicating the position where to read
824 * @produced: produced count, indicates position when to stop reading
826 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
827 * data to read at consumed position, or 0 if the get operation succeeds.
828 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
831 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
832 unsigned long *consumed
, unsigned long *produced
)
834 struct channel
*chan
= buf
->backend
.chan
;
835 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
836 unsigned long consumed_cur
, write_offset
;
840 finalized
= ACCESS_ONCE(buf
->finalized
);
842 * Read finalized before counters.
845 consumed_cur
= atomic_long_read(&buf
->consumed
);
847 * No need to issue a memory barrier between consumed count read and
848 * write offset read, because consumed count can only change
849 * concurrently in overwrite mode, and we keep a sequence counter
850 * identifier derived from the write offset to check we are getting
851 * the same sub-buffer we are expecting (the sub-buffers are atomically
852 * "tagged" upon writes, tags are checked upon read).
854 write_offset
= v_read(config
, &buf
->offset
);
857 * Check that we are not about to read the same subbuffer in
858 * which the writer head is.
860 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
864 *consumed
= consumed_cur
;
865 *produced
= subbuf_trunc(write_offset
, chan
);
871 * The memory barriers __wait_event()/wake_up_interruptible() take care
872 * of "raw_spin_is_locked" memory ordering.
876 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
881 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
884 * lib_ring_buffer_put_snapshot - move consumed counter forward
886 * Should only be called from consumer context.
888 * @consumed_new: new consumed count value
890 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
891 unsigned long consumed_new
)
893 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
894 struct channel
*chan
= bufb
->chan
;
895 unsigned long consumed
;
897 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
900 * Only push the consumed value forward.
901 * If the consumed cmpxchg fails, this is because we have been pushed by
902 * the writer in flight recorder mode.
904 consumed
= atomic_long_read(&buf
->consumed
);
905 while ((long) consumed
- (long) consumed_new
< 0)
906 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
908 /* Wake-up the metadata producer */
909 wake_up_interruptible(&buf
->write_wait
);
911 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
914 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
916 * @consumed: consumed count indicating the position where to read
918 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
919 * data to read at consumed position, or 0 if the get operation succeeds.
920 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
922 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
923 unsigned long consumed
)
925 struct channel
*chan
= buf
->backend
.chan
;
926 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
927 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
932 finalized
= ACCESS_ONCE(buf
->finalized
);
934 * Read finalized before counters.
937 consumed_cur
= atomic_long_read(&buf
->consumed
);
938 consumed_idx
= subbuf_index(consumed
, chan
);
939 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
941 * Make sure we read the commit count before reading the buffer
942 * data and the write offset. Correct consumed offset ordering
943 * wrt commit count is insured by the use of cmpxchg to update
944 * the consumed offset.
945 * smp_call_function_single can fail if the remote CPU is offline,
946 * this is OK because then there is no wmb to execute there.
947 * If our thread is executing on the same CPU as the on the buffers
948 * belongs to, we don't have to synchronize it at all. If we are
949 * migrated, the scheduler will take care of the memory barriers.
950 * Normally, smp_call_function_single() should ensure program order when
951 * executing the remote function, which implies that it surrounds the
952 * function execution with :
963 * However, smp_call_function_single() does not seem to clearly execute
964 * such barriers. It depends on spinlock semantic to provide the barrier
965 * before executing the IPI and, when busy-looping, csd_lock_wait only
966 * executes smp_mb() when it has to wait for the other CPU.
968 * I don't trust this code. Therefore, let's add the smp_mb() sequence
969 * required ourself, even if duplicated. It has no performance impact
972 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
973 * read and write vs write. They do not ensure core synchronization. We
974 * really have to ensure total order between the 3 barriers running on
977 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
978 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
979 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
980 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
981 /* Total order with IPI handler smp_mb() */
983 smp_call_function_single(buf
->backend
.cpu
,
985 /* Total order with IPI handler smp_mb() */
989 /* Total order with IPI handler smp_mb() */
991 smp_call_function(remote_mb
, NULL
, 1);
992 /* Total order with IPI handler smp_mb() */
997 * Local rmb to match the remote wmb to read the commit count
998 * before the buffer data and the write offset.
1003 write_offset
= v_read(config
, &buf
->offset
);
1006 * Check that the buffer we are getting is after or at consumed_cur
1009 if ((long) subbuf_trunc(consumed
, chan
)
1010 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1014 * Check that the subbuffer we are trying to consume has been
1015 * already fully committed.
1017 if (((commit_count
- chan
->backend
.subbuf_size
)
1018 & chan
->commit_count_mask
)
1019 - (buf_trunc(consumed_cur
, chan
)
1020 >> chan
->backend
.num_subbuf_order
)
1025 * Check that we are not about to read the same subbuffer in
1026 * which the writer head is.
1028 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1033 * Failure to get the subbuffer causes a busy-loop retry without going
1034 * to a wait queue. These are caused by short-lived race windows where
1035 * the writer is getting access to a subbuffer we were trying to get
1036 * access to. Also checks that the "consumed" buffer count we are
1037 * looking for matches the one contained in the subbuffer id.
1039 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1040 consumed_idx
, buf_trunc_val(consumed
, chan
));
1043 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1045 buf
->get_subbuf_consumed
= consumed
;
1046 buf
->get_subbuf
= 1;
1052 * The memory barriers __wait_event()/wake_up_interruptible() take care
1053 * of "raw_spin_is_locked" memory ordering.
1057 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1062 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1065 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1068 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1070 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1071 struct channel
*chan
= bufb
->chan
;
1072 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1073 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1075 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1077 if (!buf
->get_subbuf
) {
1079 * Reader puts a subbuffer it did not get.
1081 CHAN_WARN_ON(chan
, 1);
1084 consumed
= buf
->get_subbuf_consumed
;
1085 buf
->get_subbuf
= 0;
1088 * Clear the records_unread counter. (overruns counter)
1089 * Can still be non-zero if a file reader simply grabbed the data
1090 * without using iterators.
1091 * Can be below zero if an iterator is used on a snapshot more than
1094 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1095 v_add(config
, v_read(config
,
1096 &bufb
->array
[read_sb_bindex
]->records_unread
),
1097 &bufb
->records_read
);
1098 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1099 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1100 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1101 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1104 * Exchange the reader subbuffer with the one we put in its place in the
1105 * writer subbuffer table. Expect the original consumed count. If
1106 * update_read_sb_index fails, this is because the writer updated the
1107 * subbuffer concurrently. We should therefore keep the subbuffer we
1108 * currently have: it has become invalid to try reading this sub-buffer
1109 * consumed count value anyway.
1111 consumed_idx
= subbuf_index(consumed
, chan
);
1112 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1113 consumed_idx
, buf_trunc_val(consumed
, chan
));
1115 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1116 * if the writer concurrently updated it.
1119 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1122 * cons_offset is an iterator on all subbuffer offsets between the reader
1123 * position and the writer position. (inclusive)
1126 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1127 struct channel
*chan
,
1128 unsigned long cons_offset
,
1131 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1132 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1134 cons_idx
= subbuf_index(cons_offset
, chan
);
1135 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1136 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1138 if (subbuf_offset(commit_count
, chan
) != 0)
1140 "ring buffer %s, cpu %d: "
1141 "commit count in subbuffer %lu,\n"
1142 "expecting multiples of %lu bytes\n"
1143 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1144 chan
->backend
.name
, cpu
, cons_idx
,
1145 chan
->backend
.subbuf_size
,
1146 commit_count
, commit_count_sb
);
1148 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1149 chan
->backend
.name
, cpu
, commit_count
);
1153 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1154 struct channel
*chan
,
1155 void *priv
, int cpu
)
1157 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1158 unsigned long write_offset
, cons_offset
;
1161 * No need to order commit_count, write_offset and cons_offset reads
1162 * because we execute at teardown when no more writer nor reader
1163 * references are left.
1165 write_offset
= v_read(config
, &buf
->offset
);
1166 cons_offset
= atomic_long_read(&buf
->consumed
);
1167 if (write_offset
!= cons_offset
)
1169 "ring buffer %s, cpu %d: "
1170 "non-consumed data\n"
1171 " [ %lu bytes written, %lu bytes read ]\n",
1172 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1174 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1175 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1178 cons_offset
= subbuf_align(cons_offset
, chan
))
1179 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1184 void lib_ring_buffer_print_errors(struct channel
*chan
,
1185 struct lib_ring_buffer
*buf
, int cpu
)
1187 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1188 void *priv
= chan
->backend
.priv
;
1190 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1191 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1192 "%lu records overrun\n",
1194 v_read(config
, &buf
->records_count
),
1195 v_read(config
, &buf
->records_overrun
));
1197 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1198 "%lu records overrun\n",
1199 chan
->backend
.name
, cpu
,
1200 v_read(config
, &buf
->records_count
),
1201 v_read(config
, &buf
->records_overrun
));
1203 if (v_read(config
, &buf
->records_lost_full
)
1204 || v_read(config
, &buf
->records_lost_wrap
)
1205 || v_read(config
, &buf
->records_lost_big
))
1207 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1208 " [ %lu buffer full, %lu nest buffer wrap-around, "
1209 "%lu event too big ]\n",
1210 chan
->backend
.name
, cpu
,
1211 v_read(config
, &buf
->records_lost_full
),
1212 v_read(config
, &buf
->records_lost_wrap
),
1213 v_read(config
, &buf
->records_lost_big
));
1215 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1219 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1221 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1224 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1225 struct channel
*chan
,
1226 struct switch_offsets
*offsets
,
1229 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1230 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1231 unsigned long commit_count
;
1233 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1236 * Order all writes to buffer before the commit count update that will
1237 * determine that the subbuffer is full.
1239 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1241 * Must write slot data before incrementing commit count. This
1242 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1248 v_add(config
, config
->cb
.subbuffer_header_size(),
1249 &buf
->commit_hot
[oldidx
].cc
);
1250 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1251 /* Check if the written buffer has to be delivered */
1252 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1253 commit_count
, oldidx
);
1254 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1255 offsets
->old
, commit_count
,
1256 config
->cb
.subbuffer_header_size());
1260 * lib_ring_buffer_switch_old_end: switch old subbuffer
1262 * Note : offset_old should never be 0 here. It is ok, because we never perform
1263 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1264 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1268 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1269 struct channel
*chan
,
1270 struct switch_offsets
*offsets
,
1273 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1274 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1275 unsigned long commit_count
, padding_size
, data_size
;
1277 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1278 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1279 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1282 * Order all writes to buffer before the commit count update that will
1283 * determine that the subbuffer is full.
1285 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1287 * Must write slot data before incrementing commit count. This
1288 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1294 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1295 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1296 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1297 commit_count
, oldidx
);
1298 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1299 offsets
->old
, commit_count
,
1304 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1306 * This code can be executed unordered : writers may already have written to the
1307 * sub-buffer before this code gets executed, caution. The commit makes sure
1308 * that this code is executed before the deliver of this sub-buffer.
1311 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1312 struct channel
*chan
,
1313 struct switch_offsets
*offsets
,
1316 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1317 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1318 unsigned long commit_count
;
1320 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1323 * Order all writes to buffer before the commit count update that will
1324 * determine that the subbuffer is full.
1326 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1328 * Must write slot data before incrementing commit count. This
1329 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1335 v_add(config
, config
->cb
.subbuffer_header_size(),
1336 &buf
->commit_hot
[beginidx
].cc
);
1337 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1338 /* Check if the written buffer has to be delivered */
1339 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1340 commit_count
, beginidx
);
1341 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1342 offsets
->begin
, commit_count
,
1343 config
->cb
.subbuffer_header_size());
1347 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1349 * The only remaining threads could be the ones with pending commits. They will
1350 * have to do the deliver themselves.
1353 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1354 struct channel
*chan
,
1355 struct switch_offsets
*offsets
,
1358 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1359 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1360 unsigned long commit_count
, padding_size
, data_size
;
1362 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1363 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1364 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1367 * Order all writes to buffer before the commit count update that will
1368 * determine that the subbuffer is full.
1370 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1372 * Must write slot data before incrementing commit count. This
1373 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1379 v_add(config
, padding_size
, &buf
->commit_hot
[endidx
].cc
);
1380 commit_count
= v_read(config
, &buf
->commit_hot
[endidx
].cc
);
1381 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1382 commit_count
, endidx
);
1383 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1384 offsets
->end
, commit_count
,
1391 * !0 if execution must be aborted.
1394 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1395 struct lib_ring_buffer
*buf
,
1396 struct channel
*chan
,
1397 struct switch_offsets
*offsets
,
1400 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1403 offsets
->begin
= v_read(config
, &buf
->offset
);
1404 offsets
->old
= offsets
->begin
;
1405 offsets
->switch_old_start
= 0;
1406 off
= subbuf_offset(offsets
->begin
, chan
);
1408 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1411 * Ensure we flush the header of an empty subbuffer when doing the
1412 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1413 * total data gathering duration even if there were no records saved
1414 * after the last buffer switch.
1415 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1416 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1417 * subbuffer header as appropriate.
1418 * The next record that reserves space will be responsible for
1419 * populating the following subbuffer header. We choose not to populate
1420 * the next subbuffer header here because we want to be able to use
1421 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1422 * buffer flush, which must guarantee that all the buffer content
1423 * (records and header timestamps) are visible to the reader. This is
1424 * required for quiescence guarantees for the fusion merge.
1426 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1427 if (unlikely(off
== 0)) {
1429 * The client does not save any header information.
1430 * Don't switch empty subbuffer on finalize, because it
1431 * is invalid to deliver a completely empty subbuffer.
1433 if (!config
->cb
.subbuffer_header_size())
1436 * Need to write the subbuffer start header on finalize.
1438 offsets
->switch_old_start
= 1;
1440 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1442 return -1; /* we do not have to switch : buffer is empty */
1443 /* Note: old points to the next subbuf at offset 0 */
1444 offsets
->end
= offsets
->begin
;
1449 * Force a sub-buffer switch. This operation is completely reentrant : can be
1450 * called while tracing is active with absolutely no lock held.
1452 * Note, however, that as a v_cmpxchg is used for some atomic
1453 * operations, this function must be called from the CPU which owns the buffer
1454 * for a ACTIVE flush.
1456 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1458 struct channel
*chan
= buf
->backend
.chan
;
1459 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1460 struct switch_offsets offsets
;
1461 unsigned long oldidx
;
1467 * Perform retryable operations.
1470 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1472 return; /* Switch not needed */
1473 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1477 * Atomically update last_tsc. This update races against concurrent
1478 * atomic updates, but the race will always cause supplementary full TSC
1479 * records, never the opposite (missing a full TSC record when it would
1482 save_last_tsc(config
, buf
, tsc
);
1485 * Push the reader if necessary
1487 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1489 oldidx
= subbuf_index(offsets
.old
, chan
);
1490 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1493 * May need to populate header start on SWITCH_FLUSH.
1495 if (offsets
.switch_old_start
) {
1496 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1497 offsets
.old
+= config
->cb
.subbuffer_header_size();
1501 * Switch old subbuffer.
1503 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1505 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1510 * -ENOSPC if event size is too large for packet.
1511 * -ENOBUFS if there is currently not enough space in buffer for the event.
1512 * -EIO if data cannot be written into the buffer for any other reason.
1515 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1516 struct channel
*chan
,
1517 struct switch_offsets
*offsets
,
1518 struct lib_ring_buffer_ctx
*ctx
)
1520 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1521 unsigned long reserve_commit_diff
;
1523 offsets
->begin
= v_read(config
, &buf
->offset
);
1524 offsets
->old
= offsets
->begin
;
1525 offsets
->switch_new_start
= 0;
1526 offsets
->switch_new_end
= 0;
1527 offsets
->switch_old_end
= 0;
1528 offsets
->pre_header_padding
= 0;
1530 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1531 if ((int64_t) ctx
->tsc
== -EIO
)
1534 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1535 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1537 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1538 offsets
->switch_new_start
= 1; /* For offsets->begin */
1540 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1542 &offsets
->pre_header_padding
,
1545 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1548 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1549 offsets
->size
> chan
->backend
.subbuf_size
)) {
1550 offsets
->switch_old_end
= 1; /* For offsets->old */
1551 offsets
->switch_new_start
= 1; /* For offsets->begin */
1554 if (unlikely(offsets
->switch_new_start
)) {
1555 unsigned long sb_index
;
1558 * We are typically not filling the previous buffer completely.
1560 if (likely(offsets
->switch_old_end
))
1561 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1562 offsets
->begin
= offsets
->begin
1563 + config
->cb
.subbuffer_header_size();
1564 /* Test new buffer integrity */
1565 sb_index
= subbuf_index(offsets
->begin
, chan
);
1566 reserve_commit_diff
=
1567 (buf_trunc(offsets
->begin
, chan
)
1568 >> chan
->backend
.num_subbuf_order
)
1569 - ((unsigned long) v_read(config
,
1570 &buf
->commit_cold
[sb_index
].cc_sb
)
1571 & chan
->commit_count_mask
);
1572 if (likely(reserve_commit_diff
== 0)) {
1573 /* Next subbuffer not being written to. */
1574 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1575 subbuf_trunc(offsets
->begin
, chan
)
1576 - subbuf_trunc((unsigned long)
1577 atomic_long_read(&buf
->consumed
), chan
)
1578 >= chan
->backend
.buf_size
)) {
1580 * We do not overwrite non consumed buffers
1581 * and we are full : record is lost.
1583 v_inc(config
, &buf
->records_lost_full
);
1587 * Next subbuffer not being written to, and we
1588 * are either in overwrite mode or the buffer is
1589 * not full. It's safe to write in this new
1595 * Next subbuffer reserve offset does not match the
1596 * commit offset. Drop record in producer-consumer and
1597 * overwrite mode. Caused by either a writer OOPS or too
1598 * many nested writes over a reserve/commit pair.
1600 v_inc(config
, &buf
->records_lost_wrap
);
1604 config
->cb
.record_header_size(config
, chan
,
1606 &offsets
->pre_header_padding
,
1609 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1612 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1613 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1615 * Record too big for subbuffers, report error, don't
1616 * complete the sub-buffer switch.
1618 v_inc(config
, &buf
->records_lost_big
);
1622 * We just made a successful buffer switch and the
1623 * record fits in the new subbuffer. Let's write.
1628 * Record fits in the current buffer and we are not on a switch
1629 * boundary. It's safe to write.
1632 offsets
->end
= offsets
->begin
+ offsets
->size
;
1634 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1636 * The offset_end will fall at the very beginning of the next
1639 offsets
->switch_new_end
= 1; /* For offsets->begin */
1645 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1646 * @ctx: ring buffer context.
1648 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1649 * -EIO for other errors, else returns 0.
1650 * It will take care of sub-buffer switching.
1652 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1654 struct channel
*chan
= ctx
->chan
;
1655 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1656 struct lib_ring_buffer
*buf
;
1657 struct switch_offsets offsets
;
1660 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1661 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1663 buf
= chan
->backend
.buf
;
1669 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1673 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1678 * Atomically update last_tsc. This update races against concurrent
1679 * atomic updates, but the race will always cause supplementary full TSC
1680 * records, never the opposite (missing a full TSC record when it would
1683 save_last_tsc(config
, buf
, ctx
->tsc
);
1686 * Push the reader if necessary
1688 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1691 * Clear noref flag for this subbuffer.
1693 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1694 subbuf_index(offsets
.end
- 1, chan
));
1697 * Switch old subbuffer if needed.
1699 if (unlikely(offsets
.switch_old_end
)) {
1700 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1701 subbuf_index(offsets
.old
- 1, chan
));
1702 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1706 * Populate new subbuffer.
1708 if (unlikely(offsets
.switch_new_start
))
1709 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1711 if (unlikely(offsets
.switch_new_end
))
1712 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1714 ctx
->slot_size
= offsets
.size
;
1715 ctx
->pre_offset
= offsets
.begin
;
1716 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1719 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
1721 int __init
init_lib_ring_buffer_frontend(void)
1725 for_each_possible_cpu(cpu
)
1726 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
1730 module_init(init_lib_ring_buffer_frontend
);
1732 void __exit
exit_lib_ring_buffer_frontend(void)
1736 module_exit(exit_lib_ring_buffer_frontend
);