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>
57 #include <asm/cacheflush.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>
64 #include <wrapper/atomic.h>
65 #include <wrapper/kref.h>
66 #include <wrapper/percpu-defs.h>
67 #include <wrapper/timer.h>
68 #include <wrapper/vmalloc.h>
71 * Internal structure representing offsets to use at a sub-buffer switch.
73 struct switch_offsets
{
74 unsigned long begin
, end
, old
;
75 size_t pre_header_padding
, size
;
76 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
87 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
88 #endif /* CONFIG_NO_HZ */
90 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
92 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
93 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
96 void lib_ring_buffer_print_errors(struct channel
*chan
,
97 struct lib_ring_buffer
*buf
, int cpu
);
99 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
100 enum switch_mode mode
);
103 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
104 struct lib_ring_buffer
*buf
,
105 struct channel
*chan
)
107 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
109 consumed_old
= atomic_long_read(&buf
->consumed
);
110 consumed_idx
= subbuf_index(consumed_old
, chan
);
111 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
113 * No memory barrier here, since we are only interested
114 * in a statistically correct polling result. The next poll will
115 * get the data is we are racing. The mb() that ensures correct
116 * memory order is in get_subbuf.
118 write_offset
= v_read(config
, &buf
->offset
);
121 * Check that the subbuffer we are trying to consume has been
122 * already fully committed.
125 if (((commit_count
- chan
->backend
.subbuf_size
)
126 & chan
->commit_count_mask
)
127 - (buf_trunc(consumed_old
, chan
)
128 >> chan
->backend
.num_subbuf_order
)
133 * Check that we are not about to read the same subbuffer in
134 * which the writer head is.
136 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
144 * Must be called under cpu hotplug protection.
146 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
148 struct channel
*chan
= buf
->backend
.chan
;
150 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
151 lttng_kvfree(buf
->commit_hot
);
152 lttng_kvfree(buf
->commit_cold
);
153 lttng_kvfree(buf
->ts_end
);
155 lib_ring_buffer_backend_free(&buf
->backend
);
159 * lib_ring_buffer_reset - Reset ring buffer to initial values.
162 * Effectively empty the ring buffer. Should be called when the buffer is not
163 * used for writing. The ring buffer can be opened for reading, but the reader
164 * should not be using the iterator concurrently with reset. The previous
165 * current iterator record is reset.
167 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
169 struct channel
*chan
= buf
->backend
.chan
;
170 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
174 * Reset iterator first. It will put the subbuffer if it currently holds
177 lib_ring_buffer_iterator_reset(buf
);
178 v_set(config
, &buf
->offset
, 0);
179 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
180 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
181 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
182 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
185 atomic_long_set(&buf
->consumed
, 0);
186 atomic_set(&buf
->record_disabled
, 0);
187 v_set(config
, &buf
->last_tsc
, 0);
188 lib_ring_buffer_backend_reset(&buf
->backend
);
189 /* Don't reset number of active readers */
190 v_set(config
, &buf
->records_lost_full
, 0);
191 v_set(config
, &buf
->records_lost_wrap
, 0);
192 v_set(config
, &buf
->records_lost_big
, 0);
193 v_set(config
, &buf
->records_count
, 0);
194 v_set(config
, &buf
->records_overrun
, 0);
197 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
200 * channel_reset - Reset channel to initial values.
203 * Effectively empty the channel. Should be called when the channel is not used
204 * for writing. The channel can be opened for reading, but the reader should not
205 * be using the iterator concurrently with reset. The previous current iterator
208 void channel_reset(struct channel
*chan
)
211 * Reset iterators first. Will put the subbuffer if held for reading.
213 channel_iterator_reset(chan
);
214 atomic_set(&chan
->record_disabled
, 0);
215 /* Don't reset commit_count_mask, still valid */
216 channel_backend_reset(&chan
->backend
);
217 /* Don't reset switch/read timer interval */
218 /* Don't reset notifiers and notifier enable bits */
219 /* Don't reset reader reference count */
221 EXPORT_SYMBOL_GPL(channel_reset
);
224 * Must be called under cpu hotplug protection.
226 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
227 struct channel_backend
*chanb
, int cpu
)
229 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
230 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
231 void *priv
= chanb
->priv
;
232 size_t subbuf_header_size
;
236 /* Test for cpu hotplug */
237 if (buf
->backend
.allocated
)
241 * Paranoia: per cpu dynamic allocation is not officially documented as
242 * zeroing the memory, so let's do it here too, just in case.
244 memset(buf
, 0, sizeof(*buf
));
246 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
251 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
252 * chan
->backend
.num_subbuf
,
253 1 << INTERNODE_CACHE_SHIFT
),
254 GFP_KERNEL
| __GFP_NOWARN
,
255 cpu_to_node(max(cpu
, 0)));
256 if (!buf
->commit_hot
) {
262 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
263 * chan
->backend
.num_subbuf
,
264 1 << INTERNODE_CACHE_SHIFT
),
265 GFP_KERNEL
| __GFP_NOWARN
,
266 cpu_to_node(max(cpu
, 0)));
267 if (!buf
->commit_cold
) {
273 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
274 * chan
->backend
.num_subbuf
,
275 1 << INTERNODE_CACHE_SHIFT
),
276 GFP_KERNEL
| __GFP_NOWARN
,
277 cpu_to_node(max(cpu
, 0)));
280 goto free_commit_cold
;
283 init_waitqueue_head(&buf
->read_wait
);
284 init_waitqueue_head(&buf
->write_wait
);
285 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
288 * Write the subbuffer header for first subbuffer so we know the total
289 * duration of data gathering.
291 subbuf_header_size
= config
->cb
.subbuffer_header_size();
292 v_set(config
, &buf
->offset
, subbuf_header_size
);
293 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
294 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
295 config
->cb
.buffer_begin(buf
, tsc
, 0);
296 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
298 if (config
->cb
.buffer_create
) {
299 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
305 * Ensure the buffer is ready before setting it to allocated and setting
307 * Used for cpu hotplug vs cpumask iteration.
310 buf
->backend
.allocated
= 1;
312 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
313 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
314 chan
->backend
.cpumask
));
315 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
322 lttng_kvfree(buf
->ts_end
);
324 lttng_kvfree(buf
->commit_cold
);
326 lttng_kvfree(buf
->commit_hot
);
328 lib_ring_buffer_backend_free(&buf
->backend
);
332 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
334 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, switch_timer
);
335 struct channel
*chan
= buf
->backend
.chan
;
336 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
339 * Only flush buffers periodically if readers are active.
341 if (atomic_long_read(&buf
->active_readers
))
342 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
344 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
345 lttng_mod_timer_pinned(&buf
->switch_timer
,
346 jiffies
+ chan
->switch_timer_interval
);
348 mod_timer(&buf
->switch_timer
,
349 jiffies
+ chan
->switch_timer_interval
);
353 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
355 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
357 struct channel
*chan
= buf
->backend
.chan
;
358 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
359 unsigned int flags
= 0;
361 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
364 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
365 flags
= LTTNG_TIMER_PINNED
;
367 lttng_timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
, buf
);
368 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
370 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
371 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
373 add_timer(&buf
->switch_timer
);
375 buf
->switch_timer_enabled
= 1;
379 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
381 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
383 struct channel
*chan
= buf
->backend
.chan
;
385 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
388 del_timer_sync(&buf
->switch_timer
);
389 buf
->switch_timer_enabled
= 0;
393 * Polling timer to check the channels for data.
395 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
397 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, read_timer
);
398 struct channel
*chan
= buf
->backend
.chan
;
399 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
401 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
403 if (atomic_long_read(&buf
->active_readers
)
404 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
405 wake_up_interruptible(&buf
->read_wait
);
406 wake_up_interruptible(&chan
->read_wait
);
409 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
410 lttng_mod_timer_pinned(&buf
->read_timer
,
411 jiffies
+ chan
->read_timer_interval
);
413 mod_timer(&buf
->read_timer
,
414 jiffies
+ chan
->read_timer_interval
);
418 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
420 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
422 struct channel
*chan
= buf
->backend
.chan
;
423 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
426 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
427 || !chan
->read_timer_interval
428 || buf
->read_timer_enabled
)
431 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
432 flags
= LTTNG_TIMER_PINNED
;
434 lttng_timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
, buf
);
435 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
437 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
438 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
440 add_timer(&buf
->read_timer
);
442 buf
->read_timer_enabled
= 1;
446 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
448 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
450 struct channel
*chan
= buf
->backend
.chan
;
451 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
453 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
454 || !chan
->read_timer_interval
455 || !buf
->read_timer_enabled
)
458 del_timer_sync(&buf
->read_timer
);
460 * do one more check to catch data that has been written in the last
463 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
464 wake_up_interruptible(&buf
->read_wait
);
465 wake_up_interruptible(&chan
->read_wait
);
467 buf
->read_timer_enabled
= 0;
470 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
472 enum cpuhp_state lttng_rb_hp_prepare
;
473 enum cpuhp_state lttng_rb_hp_online
;
475 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
477 lttng_rb_hp_prepare
= val
;
479 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
481 void lttng_rb_set_hp_online(enum cpuhp_state val
)
483 lttng_rb_hp_online
= val
;
485 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
487 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
488 struct lttng_cpuhp_node
*node
)
490 struct channel
*chan
= container_of(node
, struct channel
,
492 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
493 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
495 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
498 * Performing a buffer switch on a remote CPU. Performed by
499 * the CPU responsible for doing the hotunplug after the target
500 * CPU stopped running completely. Ensures that all data
501 * from that remote CPU is flushed.
503 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
506 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
508 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
509 struct lttng_cpuhp_node
*node
)
511 struct channel
*chan
= container_of(node
, struct channel
,
513 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
514 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
516 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
518 wake_up_interruptible(&chan
->hp_wait
);
519 lib_ring_buffer_start_switch_timer(buf
);
520 lib_ring_buffer_start_read_timer(buf
);
523 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
525 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
526 struct lttng_cpuhp_node
*node
)
528 struct channel
*chan
= container_of(node
, struct channel
,
530 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
531 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
533 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
535 lib_ring_buffer_stop_switch_timer(buf
);
536 lib_ring_buffer_stop_read_timer(buf
);
539 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
541 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
543 #ifdef CONFIG_HOTPLUG_CPU
546 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
547 * @nb: notifier block
548 * @action: hotplug action to take
551 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
554 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
555 unsigned long action
,
558 unsigned int cpu
= (unsigned long)hcpu
;
559 struct channel
*chan
= container_of(nb
, struct channel
,
561 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
562 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
564 if (!chan
->cpu_hp_enable
)
567 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
570 case CPU_DOWN_FAILED
:
571 case CPU_DOWN_FAILED_FROZEN
:
573 case CPU_ONLINE_FROZEN
:
574 wake_up_interruptible(&chan
->hp_wait
);
575 lib_ring_buffer_start_switch_timer(buf
);
576 lib_ring_buffer_start_read_timer(buf
);
579 case CPU_DOWN_PREPARE
:
580 case CPU_DOWN_PREPARE_FROZEN
:
581 lib_ring_buffer_stop_switch_timer(buf
);
582 lib_ring_buffer_stop_read_timer(buf
);
586 case CPU_DEAD_FROZEN
:
588 * Performing a buffer switch on a remote CPU. Performed by
589 * the CPU responsible for doing the hotunplug after the target
590 * CPU stopped running completely. Ensures that all data
591 * from that remote CPU is flushed.
593 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
603 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
605 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
607 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
608 * that wake-up-tracing generated events are flushed before going idle (in
609 * tick_nohz). We test if the spinlock is locked to deal with the race where
610 * readers try to sample the ring buffer before we perform the switch. We let
611 * the readers retry in that case. If there is data in the buffer, the wake up
612 * is going to forbid the CPU running the reader thread from going idle.
614 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
618 struct channel
*chan
= container_of(nb
, struct channel
,
620 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
621 struct lib_ring_buffer
*buf
;
622 int cpu
= smp_processor_id();
624 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
626 * We don't support keeping the system idle with global buffers
627 * and streaming active. In order to do so, we would need to
628 * sample a non-nohz-cpumask racelessly with the nohz updates
629 * without adding synchronization overhead to nohz. Leave this
630 * use-case out for now.
635 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
637 case TICK_NOHZ_FLUSH
:
638 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
639 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
640 && chan
->read_timer_interval
641 && atomic_long_read(&buf
->active_readers
)
642 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
643 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
644 wake_up_interruptible(&buf
->read_wait
);
645 wake_up_interruptible(&chan
->read_wait
);
647 if (chan
->switch_timer_interval
)
648 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
649 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
652 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
653 lib_ring_buffer_stop_switch_timer(buf
);
654 lib_ring_buffer_stop_read_timer(buf
);
655 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
657 case TICK_NOHZ_RESTART
:
658 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
659 lib_ring_buffer_start_read_timer(buf
);
660 lib_ring_buffer_start_switch_timer(buf
);
661 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
668 void notrace
lib_ring_buffer_tick_nohz_flush(void)
670 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
674 void notrace
lib_ring_buffer_tick_nohz_stop(void)
676 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
680 void notrace
lib_ring_buffer_tick_nohz_restart(void)
682 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
685 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
690 static void channel_unregister_notifiers(struct channel
*chan
)
692 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
694 channel_iterator_unregister_notifiers(chan
);
695 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
698 * Remove the nohz notifier first, so we are certain we stop
701 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
702 &chan
->tick_nohz_notifier
);
704 * ring_buffer_nohz_lock will not be needed below, because
705 * we just removed the notifiers, which were the only source of
708 #endif /* CONFIG_NO_HZ */
709 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
713 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
714 &chan
->cpuhp_online
.node
);
716 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
717 &chan
->cpuhp_prepare
.node
);
720 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
724 #ifdef CONFIG_HOTPLUG_CPU
726 chan
->cpu_hp_enable
= 0;
727 for_each_online_cpu(cpu
) {
728 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
730 lib_ring_buffer_stop_switch_timer(buf
);
731 lib_ring_buffer_stop_read_timer(buf
);
734 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
736 for_each_possible_cpu(cpu
) {
737 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
739 lib_ring_buffer_stop_switch_timer(buf
);
740 lib_ring_buffer_stop_read_timer(buf
);
744 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
746 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
748 lib_ring_buffer_stop_switch_timer(buf
);
749 lib_ring_buffer_stop_read_timer(buf
);
751 channel_backend_unregister_notifiers(&chan
->backend
);
754 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
756 if (!buf
->quiescent
) {
757 buf
->quiescent
= true;
758 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
762 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
764 buf
->quiescent
= false;
767 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
770 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
772 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
774 for_each_channel_cpu(cpu
, chan
) {
775 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
778 lib_ring_buffer_set_quiescent(buf
);
782 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
784 lib_ring_buffer_set_quiescent(buf
);
787 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
789 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
792 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
794 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
796 for_each_channel_cpu(cpu
, chan
) {
797 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
800 lib_ring_buffer_clear_quiescent(buf
);
804 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
806 lib_ring_buffer_clear_quiescent(buf
);
809 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
811 static void channel_free(struct channel
*chan
)
813 if (chan
->backend
.release_priv_ops
) {
814 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
816 channel_iterator_free(chan
);
817 channel_backend_free(&chan
->backend
);
822 * channel_create - Create channel.
823 * @config: ring buffer instance configuration
824 * @name: name of the channel
825 * @priv: ring buffer client private data
826 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
827 * address mapping. It is used only by RING_BUFFER_STATIC
828 * configuration. It can be set to NULL for other backends.
829 * @subbuf_size: subbuffer size
830 * @num_subbuf: number of subbuffers
831 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
832 * padding to let readers get those sub-buffers.
833 * Used for live streaming.
834 * @read_timer_interval: Time interval (in us) to wake up pending readers.
837 * Returns NULL on failure.
839 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
840 const char *name
, void *priv
, void *buf_addr
,
842 size_t num_subbuf
, unsigned int switch_timer_interval
,
843 unsigned int read_timer_interval
)
846 struct channel
*chan
;
848 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
849 read_timer_interval
))
852 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
856 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
857 subbuf_size
, num_subbuf
);
861 ret
= channel_iterator_init(chan
);
863 goto error_free_backend
;
865 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
866 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
867 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
868 kref_init(&chan
->ref
);
869 init_waitqueue_head(&chan
->read_wait
);
870 init_waitqueue_head(&chan
->hp_wait
);
872 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
873 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
874 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
875 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
876 &chan
->cpuhp_prepare
.node
);
878 goto cpuhp_prepare_error
;
880 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
881 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
882 &chan
->cpuhp_online
.node
);
884 goto cpuhp_online_error
;
885 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
889 * In case of non-hotplug cpu, if the ring-buffer is allocated
890 * in early initcall, it will not be notified of secondary cpus.
891 * In that off case, we need to allocate for all possible cpus.
893 #ifdef CONFIG_HOTPLUG_CPU
894 chan
->cpu_hp_notifier
.notifier_call
=
895 lib_ring_buffer_cpu_hp_callback
;
896 chan
->cpu_hp_notifier
.priority
= 6;
897 register_cpu_notifier(&chan
->cpu_hp_notifier
);
900 for_each_online_cpu(cpu
) {
901 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
903 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
904 lib_ring_buffer_start_switch_timer(buf
);
905 lib_ring_buffer_start_read_timer(buf
);
906 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
908 chan
->cpu_hp_enable
= 1;
911 for_each_possible_cpu(cpu
) {
912 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
914 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
915 lib_ring_buffer_start_switch_timer(buf
);
916 lib_ring_buffer_start_read_timer(buf
);
917 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
921 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
923 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
924 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
925 chan
->tick_nohz_notifier
.notifier_call
=
926 ring_buffer_tick_nohz_callback
;
927 chan
->tick_nohz_notifier
.priority
= ~0U;
928 atomic_notifier_chain_register(&tick_nohz_notifier
,
929 &chan
->tick_nohz_notifier
);
930 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
933 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
935 lib_ring_buffer_start_switch_timer(buf
);
936 lib_ring_buffer_start_read_timer(buf
);
941 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
943 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
944 &chan
->cpuhp_prepare
.node
);
947 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
949 channel_backend_free(&chan
->backend
);
954 EXPORT_SYMBOL_GPL(channel_create
);
957 void channel_release(struct kref
*kref
)
959 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
964 * channel_destroy - Finalize, wait for q.s. and destroy channel.
965 * @chan: channel to destroy
968 * Call "destroy" callback, finalize channels, and then decrement the
969 * channel reference count. Note that when readers have completed data
970 * consumption of finalized channels, get_subbuf() will return -ENODATA.
971 * They should release their handle at that point. Returns the private
974 void *channel_destroy(struct channel
*chan
)
977 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
980 channel_unregister_notifiers(chan
);
982 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
984 * No need to hold cpu hotplug, because all notifiers have been
987 for_each_channel_cpu(cpu
, chan
) {
988 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
991 if (config
->cb
.buffer_finalize
)
992 config
->cb
.buffer_finalize(buf
,
995 if (buf
->backend
.allocated
)
996 lib_ring_buffer_set_quiescent(buf
);
998 * Perform flush before writing to finalized.
1001 WRITE_ONCE(buf
->finalized
, 1);
1002 wake_up_interruptible(&buf
->read_wait
);
1005 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
1007 if (config
->cb
.buffer_finalize
)
1008 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
1009 if (buf
->backend
.allocated
)
1010 lib_ring_buffer_set_quiescent(buf
);
1012 * Perform flush before writing to finalized.
1015 WRITE_ONCE(buf
->finalized
, 1);
1016 wake_up_interruptible(&buf
->read_wait
);
1018 WRITE_ONCE(chan
->finalized
, 1);
1019 wake_up_interruptible(&chan
->hp_wait
);
1020 wake_up_interruptible(&chan
->read_wait
);
1021 priv
= chan
->backend
.priv
;
1022 kref_put(&chan
->ref
, channel_release
);
1025 EXPORT_SYMBOL_GPL(channel_destroy
);
1027 struct lib_ring_buffer
*channel_get_ring_buffer(
1028 const struct lib_ring_buffer_config
*config
,
1029 struct channel
*chan
, int cpu
)
1031 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1032 return chan
->backend
.buf
;
1034 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1036 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1038 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1040 struct channel
*chan
= buf
->backend
.chan
;
1042 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1044 if (!lttng_kref_get(&chan
->ref
)) {
1045 atomic_long_dec(&buf
->active_readers
);
1048 lttng_smp_mb__after_atomic();
1051 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1053 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1055 struct channel
*chan
= buf
->backend
.chan
;
1057 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1058 lttng_smp_mb__before_atomic();
1059 atomic_long_dec(&buf
->active_readers
);
1060 kref_put(&chan
->ref
, channel_release
);
1062 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1065 * Promote compiler barrier to a smp_mb().
1066 * For the specific ring buffer case, this IPI call should be removed if the
1067 * architecture does not reorder writes. This should eventually be provided by
1068 * a separate architecture-specific infrastructure.
1070 static void remote_mb(void *info
)
1076 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1078 * @consumed: consumed count indicating the position where to read
1079 * @produced: produced count, indicates position when to stop reading
1081 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1082 * data to read at consumed position, or 0 if the get operation succeeds.
1083 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1086 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1087 unsigned long *consumed
, unsigned long *produced
)
1089 struct channel
*chan
= buf
->backend
.chan
;
1090 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1091 unsigned long consumed_cur
, write_offset
;
1095 finalized
= READ_ONCE(buf
->finalized
);
1097 * Read finalized before counters.
1100 consumed_cur
= atomic_long_read(&buf
->consumed
);
1102 * No need to issue a memory barrier between consumed count read and
1103 * write offset read, because consumed count can only change
1104 * concurrently in overwrite mode, and we keep a sequence counter
1105 * identifier derived from the write offset to check we are getting
1106 * the same sub-buffer we are expecting (the sub-buffers are atomically
1107 * "tagged" upon writes, tags are checked upon read).
1109 write_offset
= v_read(config
, &buf
->offset
);
1112 * Check that we are not about to read the same subbuffer in
1113 * which the writer head is.
1115 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1119 *consumed
= consumed_cur
;
1120 *produced
= subbuf_trunc(write_offset
, chan
);
1126 * The memory barriers __wait_event()/wake_up_interruptible() take care
1127 * of "raw_spin_is_locked" memory ordering.
1131 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1136 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1139 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1140 * are saved regardless of whether the consumed and produced positions are
1141 * in the same subbuffer.
1143 * @consumed: consumed byte count indicating the last position read
1144 * @produced: produced byte count indicating the last position written
1146 * This function is meant to provide information on the exact producer and
1147 * consumer positions without regard for the "snapshot" feature.
1149 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1150 unsigned long *consumed
, unsigned long *produced
)
1152 struct channel
*chan
= buf
->backend
.chan
;
1153 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1156 *consumed
= atomic_long_read(&buf
->consumed
);
1158 * No need to issue a memory barrier between consumed count read and
1159 * write offset read, because consumed count can only change
1160 * concurrently in overwrite mode, and we keep a sequence counter
1161 * identifier derived from the write offset to check we are getting
1162 * the same sub-buffer we are expecting (the sub-buffers are atomically
1163 * "tagged" upon writes, tags are checked upon read).
1165 *produced
= v_read(config
, &buf
->offset
);
1170 * lib_ring_buffer_put_snapshot - move consumed counter forward
1172 * Should only be called from consumer context.
1174 * @consumed_new: new consumed count value
1176 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1177 unsigned long consumed_new
)
1179 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1180 struct channel
*chan
= bufb
->chan
;
1181 unsigned long consumed
;
1183 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1186 * Only push the consumed value forward.
1187 * If the consumed cmpxchg fails, this is because we have been pushed by
1188 * the writer in flight recorder mode.
1190 consumed
= atomic_long_read(&buf
->consumed
);
1191 while ((long) consumed
- (long) consumed_new
< 0)
1192 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1194 /* Wake-up the metadata producer */
1195 wake_up_interruptible(&buf
->write_wait
);
1197 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1199 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1200 static void lib_ring_buffer_flush_read_subbuf_dcache(
1201 const struct lib_ring_buffer_config
*config
,
1202 struct channel
*chan
,
1203 struct lib_ring_buffer
*buf
)
1205 struct lib_ring_buffer_backend_pages
*pages
;
1206 unsigned long sb_bindex
, id
, i
, nr_pages
;
1208 if (config
->output
!= RING_BUFFER_MMAP
)
1212 * Architectures with caches aliased on virtual addresses may
1213 * use different cache lines for the linear mapping vs
1214 * user-space memory mapping. Given that the ring buffer is
1215 * based on the kernel linear mapping, aligning it with the
1216 * user-space mapping is not straightforward, and would require
1217 * extra TLB entries. Therefore, simply flush the dcache for the
1218 * entire sub-buffer before reading it.
1220 id
= buf
->backend
.buf_rsb
.id
;
1221 sb_bindex
= subbuffer_id_get_index(config
, id
);
1222 pages
= buf
->backend
.array
[sb_bindex
];
1223 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1224 for (i
= 0; i
< nr_pages
; i
++) {
1225 struct lib_ring_buffer_backend_page
*backend_page
;
1227 backend_page
= &pages
->p
[i
];
1228 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1232 static void lib_ring_buffer_flush_read_subbuf_dcache(
1233 const struct lib_ring_buffer_config
*config
,
1234 struct channel
*chan
,
1235 struct lib_ring_buffer
*buf
)
1241 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1243 * @consumed: consumed count indicating the position where to read
1245 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1246 * data to read at consumed position, or 0 if the get operation succeeds.
1247 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1249 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1250 unsigned long consumed
)
1252 struct channel
*chan
= buf
->backend
.chan
;
1253 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1254 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1258 if (buf
->get_subbuf
) {
1260 * Reader is trying to get a subbuffer twice.
1262 CHAN_WARN_ON(chan
, 1);
1266 finalized
= READ_ONCE(buf
->finalized
);
1268 * Read finalized before counters.
1271 consumed_cur
= atomic_long_read(&buf
->consumed
);
1272 consumed_idx
= subbuf_index(consumed
, chan
);
1273 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1275 * Make sure we read the commit count before reading the buffer
1276 * data and the write offset. Correct consumed offset ordering
1277 * wrt commit count is insured by the use of cmpxchg to update
1278 * the consumed offset.
1279 * smp_call_function_single can fail if the remote CPU is offline,
1280 * this is OK because then there is no wmb to execute there.
1281 * If our thread is executing on the same CPU as the on the buffers
1282 * belongs to, we don't have to synchronize it at all. If we are
1283 * migrated, the scheduler will take care of the memory barriers.
1284 * Normally, smp_call_function_single() should ensure program order when
1285 * executing the remote function, which implies that it surrounds the
1286 * function execution with :
1297 * However, smp_call_function_single() does not seem to clearly execute
1298 * such barriers. It depends on spinlock semantic to provide the barrier
1299 * before executing the IPI and, when busy-looping, csd_lock_wait only
1300 * executes smp_mb() when it has to wait for the other CPU.
1302 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1303 * required ourself, even if duplicated. It has no performance impact
1306 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1307 * read and write vs write. They do not ensure core synchronization. We
1308 * really have to ensure total order between the 3 barriers running on
1311 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1312 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1313 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1314 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1315 /* Total order with IPI handler smp_mb() */
1317 smp_call_function_single(buf
->backend
.cpu
,
1318 remote_mb
, NULL
, 1);
1319 /* Total order with IPI handler smp_mb() */
1323 /* Total order with IPI handler smp_mb() */
1325 smp_call_function(remote_mb
, NULL
, 1);
1326 /* Total order with IPI handler smp_mb() */
1331 * Local rmb to match the remote wmb to read the commit count
1332 * before the buffer data and the write offset.
1337 write_offset
= v_read(config
, &buf
->offset
);
1340 * Check that the buffer we are getting is after or at consumed_cur
1343 if ((long) subbuf_trunc(consumed
, chan
)
1344 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1348 * Check that the subbuffer we are trying to consume has been
1349 * already fully committed.
1351 if (((commit_count
- chan
->backend
.subbuf_size
)
1352 & chan
->commit_count_mask
)
1353 - (buf_trunc(consumed
, chan
)
1354 >> chan
->backend
.num_subbuf_order
)
1359 * Check that we are not about to read the same subbuffer in
1360 * which the writer head is.
1362 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1367 * Failure to get the subbuffer causes a busy-loop retry without going
1368 * to a wait queue. These are caused by short-lived race windows where
1369 * the writer is getting access to a subbuffer we were trying to get
1370 * access to. Also checks that the "consumed" buffer count we are
1371 * looking for matches the one contained in the subbuffer id.
1373 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1374 consumed_idx
, buf_trunc_val(consumed
, chan
));
1377 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1379 buf
->get_subbuf_consumed
= consumed
;
1380 buf
->get_subbuf
= 1;
1382 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1388 * The memory barriers __wait_event()/wake_up_interruptible() take care
1389 * of "raw_spin_is_locked" memory ordering.
1393 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1398 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1401 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1404 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1406 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1407 struct channel
*chan
= bufb
->chan
;
1408 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1409 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1411 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1413 if (!buf
->get_subbuf
) {
1415 * Reader puts a subbuffer it did not get.
1417 CHAN_WARN_ON(chan
, 1);
1420 consumed
= buf
->get_subbuf_consumed
;
1421 buf
->get_subbuf
= 0;
1424 * Clear the records_unread counter. (overruns counter)
1425 * Can still be non-zero if a file reader simply grabbed the data
1426 * without using iterators.
1427 * Can be below zero if an iterator is used on a snapshot more than
1430 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1431 v_add(config
, v_read(config
,
1432 &bufb
->array
[read_sb_bindex
]->records_unread
),
1433 &bufb
->records_read
);
1434 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1435 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1436 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1437 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1440 * Exchange the reader subbuffer with the one we put in its place in the
1441 * writer subbuffer table. Expect the original consumed count. If
1442 * update_read_sb_index fails, this is because the writer updated the
1443 * subbuffer concurrently. We should therefore keep the subbuffer we
1444 * currently have: it has become invalid to try reading this sub-buffer
1445 * consumed count value anyway.
1447 consumed_idx
= subbuf_index(consumed
, chan
);
1448 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1449 consumed_idx
, buf_trunc_val(consumed
, chan
));
1451 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1452 * if the writer concurrently updated it.
1455 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1458 * cons_offset is an iterator on all subbuffer offsets between the reader
1459 * position and the writer position. (inclusive)
1462 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1463 struct channel
*chan
,
1464 unsigned long cons_offset
,
1467 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1468 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1470 cons_idx
= subbuf_index(cons_offset
, chan
);
1471 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1472 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1474 if (subbuf_offset(commit_count
, chan
) != 0)
1476 "ring buffer %s, cpu %d: "
1477 "commit count in subbuffer %lu,\n"
1478 "expecting multiples of %lu bytes\n"
1479 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1480 chan
->backend
.name
, cpu
, cons_idx
,
1481 chan
->backend
.subbuf_size
,
1482 commit_count
, commit_count_sb
);
1484 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1485 chan
->backend
.name
, cpu
, commit_count
);
1489 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1490 struct channel
*chan
,
1491 void *priv
, int cpu
)
1493 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1494 unsigned long write_offset
, cons_offset
;
1497 * No need to order commit_count, write_offset and cons_offset reads
1498 * because we execute at teardown when no more writer nor reader
1499 * references are left.
1501 write_offset
= v_read(config
, &buf
->offset
);
1502 cons_offset
= atomic_long_read(&buf
->consumed
);
1503 if (write_offset
!= cons_offset
)
1505 "ring buffer %s, cpu %d: "
1506 "non-consumed data\n"
1507 " [ %lu bytes written, %lu bytes read ]\n",
1508 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1510 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1511 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1514 cons_offset
= subbuf_align(cons_offset
, chan
))
1515 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1520 void lib_ring_buffer_print_errors(struct channel
*chan
,
1521 struct lib_ring_buffer
*buf
, int cpu
)
1523 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1524 void *priv
= chan
->backend
.priv
;
1526 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1527 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1528 "%lu records overrun\n",
1530 v_read(config
, &buf
->records_count
),
1531 v_read(config
, &buf
->records_overrun
));
1533 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1534 "%lu records overrun\n",
1535 chan
->backend
.name
, cpu
,
1536 v_read(config
, &buf
->records_count
),
1537 v_read(config
, &buf
->records_overrun
));
1539 if (v_read(config
, &buf
->records_lost_full
)
1540 || v_read(config
, &buf
->records_lost_wrap
)
1541 || v_read(config
, &buf
->records_lost_big
))
1543 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1544 " [ %lu buffer full, %lu nest buffer wrap-around, "
1545 "%lu event too big ]\n",
1546 chan
->backend
.name
, cpu
,
1547 v_read(config
, &buf
->records_lost_full
),
1548 v_read(config
, &buf
->records_lost_wrap
),
1549 v_read(config
, &buf
->records_lost_big
));
1551 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1555 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1557 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1560 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1561 struct channel
*chan
,
1562 struct switch_offsets
*offsets
,
1565 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1566 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1567 unsigned long commit_count
;
1568 struct commit_counters_hot
*cc_hot
;
1570 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1573 * Order all writes to buffer before the commit count update that will
1574 * determine that the subbuffer is full.
1576 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1578 * Must write slot data before incrementing commit count. This
1579 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1585 cc_hot
= &buf
->commit_hot
[oldidx
];
1586 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1587 commit_count
= v_read(config
, &cc_hot
->cc
);
1588 /* Check if the written buffer has to be delivered */
1589 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1590 commit_count
, oldidx
, tsc
);
1591 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1592 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1593 commit_count
, cc_hot
);
1597 * lib_ring_buffer_switch_old_end: switch old subbuffer
1599 * Note : offset_old should never be 0 here. It is ok, because we never perform
1600 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1601 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1605 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1606 struct channel
*chan
,
1607 struct switch_offsets
*offsets
,
1610 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1611 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1612 unsigned long commit_count
, padding_size
, data_size
;
1613 struct commit_counters_hot
*cc_hot
;
1616 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1617 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1618 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1620 ts_end
= &buf
->ts_end
[oldidx
];
1622 * This is the last space reservation in that sub-buffer before
1623 * it gets delivered. This provides exclusive access to write to
1624 * this sub-buffer's ts_end. There are also no concurrent
1625 * readers of that ts_end because delivery of that sub-buffer is
1626 * postponed until the commit counter is incremented for the
1627 * current space reservation.
1632 * Order all writes to buffer and store to ts_end before the commit
1633 * count update that will determine that the subbuffer is full.
1635 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1637 * Must write slot data before incrementing commit count. This
1638 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1644 cc_hot
= &buf
->commit_hot
[oldidx
];
1645 v_add(config
, padding_size
, &cc_hot
->cc
);
1646 commit_count
= v_read(config
, &cc_hot
->cc
);
1647 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1648 commit_count
, oldidx
, tsc
);
1649 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1650 offsets
->old
+ padding_size
, commit_count
,
1655 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1657 * This code can be executed unordered : writers may already have written to the
1658 * sub-buffer before this code gets executed, caution. The commit makes sure
1659 * that this code is executed before the deliver of this sub-buffer.
1662 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1663 struct channel
*chan
,
1664 struct switch_offsets
*offsets
,
1667 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1668 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1669 unsigned long commit_count
;
1670 struct commit_counters_hot
*cc_hot
;
1672 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1675 * Order all writes to buffer before the commit count update that will
1676 * determine that the subbuffer is full.
1678 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1680 * Must write slot data before incrementing commit count. This
1681 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1687 cc_hot
= &buf
->commit_hot
[beginidx
];
1688 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1689 commit_count
= v_read(config
, &cc_hot
->cc
);
1690 /* Check if the written buffer has to be delivered */
1691 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1692 commit_count
, beginidx
, tsc
);
1693 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1694 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1695 commit_count
, cc_hot
);
1699 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1701 * Calls subbuffer_set_data_size() to set the data size of the current
1702 * sub-buffer. We do not need to perform check_deliver nor commit here,
1703 * since this task will be done by the "commit" of the event for which
1704 * we are currently doing the space reservation.
1707 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1708 struct channel
*chan
,
1709 struct switch_offsets
*offsets
,
1712 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1713 unsigned long endidx
, data_size
;
1716 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1717 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1718 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1719 ts_end
= &buf
->ts_end
[endidx
];
1721 * This is the last space reservation in that sub-buffer before
1722 * it gets delivered. This provides exclusive access to write to
1723 * this sub-buffer's ts_end. There are also no concurrent
1724 * readers of that ts_end because delivery of that sub-buffer is
1725 * postponed until the commit counter is incremented for the
1726 * current space reservation.
1734 * !0 if execution must be aborted.
1737 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1738 struct lib_ring_buffer
*buf
,
1739 struct channel
*chan
,
1740 struct switch_offsets
*offsets
,
1743 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1744 unsigned long off
, reserve_commit_diff
;
1746 offsets
->begin
= v_read(config
, &buf
->offset
);
1747 offsets
->old
= offsets
->begin
;
1748 offsets
->switch_old_start
= 0;
1749 off
= subbuf_offset(offsets
->begin
, chan
);
1751 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1754 * Ensure we flush the header of an empty subbuffer when doing the
1755 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1756 * total data gathering duration even if there were no records saved
1757 * after the last buffer switch.
1758 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1759 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1760 * subbuffer header as appropriate.
1761 * The next record that reserves space will be responsible for
1762 * populating the following subbuffer header. We choose not to populate
1763 * the next subbuffer header here because we want to be able to use
1764 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1765 * buffer flush, which must guarantee that all the buffer content
1766 * (records and header timestamps) are visible to the reader. This is
1767 * required for quiescence guarantees for the fusion merge.
1769 if (mode
!= SWITCH_FLUSH
&& !off
)
1770 return -1; /* we do not have to switch : buffer is empty */
1772 if (unlikely(off
== 0)) {
1773 unsigned long sb_index
, commit_count
;
1776 * We are performing a SWITCH_FLUSH. At this stage, there are no
1777 * concurrent writes into the buffer.
1779 * The client does not save any header information. Don't
1780 * switch empty subbuffer on finalize, because it is invalid to
1781 * deliver a completely empty subbuffer.
1783 if (!config
->cb
.subbuffer_header_size())
1786 /* Test new buffer integrity */
1787 sb_index
= subbuf_index(offsets
->begin
, chan
);
1788 commit_count
= v_read(config
,
1789 &buf
->commit_cold
[sb_index
].cc_sb
);
1790 reserve_commit_diff
=
1791 (buf_trunc(offsets
->begin
, chan
)
1792 >> chan
->backend
.num_subbuf_order
)
1793 - (commit_count
& chan
->commit_count_mask
);
1794 if (likely(reserve_commit_diff
== 0)) {
1795 /* Next subbuffer not being written to. */
1796 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1797 subbuf_trunc(offsets
->begin
, chan
)
1798 - subbuf_trunc((unsigned long)
1799 atomic_long_read(&buf
->consumed
), chan
)
1800 >= chan
->backend
.buf_size
)) {
1802 * We do not overwrite non consumed buffers
1803 * and we are full : don't switch.
1808 * Next subbuffer not being written to, and we
1809 * are either in overwrite mode or the buffer is
1810 * not full. It's safe to write in this new
1816 * Next subbuffer reserve offset does not match the
1817 * commit offset. Don't perform switch in
1818 * producer-consumer and overwrite mode. Caused by
1819 * either a writer OOPS or too many nested writes over a
1820 * reserve/commit pair.
1826 * Need to write the subbuffer start header on finalize.
1828 offsets
->switch_old_start
= 1;
1830 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1831 /* Note: old points to the next subbuf at offset 0 */
1832 offsets
->end
= offsets
->begin
;
1837 * Force a sub-buffer switch. This operation is completely reentrant : can be
1838 * called while tracing is active with absolutely no lock held.
1840 * Note, however, that as a v_cmpxchg is used for some atomic
1841 * operations, this function must be called from the CPU which owns the buffer
1842 * for a ACTIVE flush.
1844 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1846 struct channel
*chan
= buf
->backend
.chan
;
1847 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1848 struct switch_offsets offsets
;
1849 unsigned long oldidx
;
1855 * Perform retryable operations.
1858 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1860 return; /* Switch not needed */
1861 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1865 * Atomically update last_tsc. This update races against concurrent
1866 * atomic updates, but the race will always cause supplementary full TSC
1867 * records, never the opposite (missing a full TSC record when it would
1870 save_last_tsc(config
, buf
, tsc
);
1873 * Push the reader if necessary
1875 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1877 oldidx
= subbuf_index(offsets
.old
, chan
);
1878 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1881 * May need to populate header start on SWITCH_FLUSH.
1883 if (offsets
.switch_old_start
) {
1884 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1885 offsets
.old
+= config
->cb
.subbuffer_header_size();
1889 * Switch old subbuffer.
1891 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1893 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1895 struct switch_param
{
1896 struct lib_ring_buffer
*buf
;
1897 enum switch_mode mode
;
1900 static void remote_switch(void *info
)
1902 struct switch_param
*param
= info
;
1903 struct lib_ring_buffer
*buf
= param
->buf
;
1905 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1908 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1909 enum switch_mode mode
)
1911 struct channel
*chan
= buf
->backend
.chan
;
1912 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1914 struct switch_param param
;
1917 * With global synchronization we don't need to use the IPI scheme.
1919 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1920 lib_ring_buffer_switch_slow(buf
, mode
);
1925 * Disabling preemption ensures two things: first, that the
1926 * target cpu is not taken concurrently offline while we are within
1927 * smp_call_function_single(). Secondly, if it happens that the
1928 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1929 * needs to be protected from CPU hotplug handlers, which can
1930 * also perform a remote subbuffer switch.
1935 ret
= smp_call_function_single(buf
->backend
.cpu
,
1936 remote_switch
, ¶m
, 1);
1938 /* Remote CPU is offline, do it ourself. */
1939 lib_ring_buffer_switch_slow(buf
, mode
);
1944 /* Switch sub-buffer if current sub-buffer is non-empty. */
1945 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1947 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1949 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1951 /* Switch sub-buffer even if current sub-buffer is empty. */
1952 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1954 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1956 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1961 * -ENOSPC if event size is too large for packet.
1962 * -ENOBUFS if there is currently not enough space in buffer for the event.
1963 * -EIO if data cannot be written into the buffer for any other reason.
1966 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1967 struct channel
*chan
,
1968 struct switch_offsets
*offsets
,
1969 struct lib_ring_buffer_ctx
*ctx
)
1971 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1972 unsigned long reserve_commit_diff
, offset_cmp
;
1975 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1976 offsets
->old
= offsets
->begin
;
1977 offsets
->switch_new_start
= 0;
1978 offsets
->switch_new_end
= 0;
1979 offsets
->switch_old_end
= 0;
1980 offsets
->pre_header_padding
= 0;
1982 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1983 if ((int64_t) ctx
->tsc
== -EIO
)
1986 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1987 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1989 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1990 offsets
->switch_new_start
= 1; /* For offsets->begin */
1992 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1994 &offsets
->pre_header_padding
,
1997 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2000 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2001 offsets
->size
> chan
->backend
.subbuf_size
)) {
2002 offsets
->switch_old_end
= 1; /* For offsets->old */
2003 offsets
->switch_new_start
= 1; /* For offsets->begin */
2006 if (unlikely(offsets
->switch_new_start
)) {
2007 unsigned long sb_index
, commit_count
;
2010 * We are typically not filling the previous buffer completely.
2012 if (likely(offsets
->switch_old_end
))
2013 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2014 offsets
->begin
= offsets
->begin
2015 + config
->cb
.subbuffer_header_size();
2016 /* Test new buffer integrity */
2017 sb_index
= subbuf_index(offsets
->begin
, chan
);
2019 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2020 * lib_ring_buffer_check_deliver() has the matching
2021 * memory barriers required around commit_cold cc_sb
2022 * updates to ensure reserve and commit counter updates
2023 * are not seen reordered when updated by another CPU.
2026 commit_count
= v_read(config
,
2027 &buf
->commit_cold
[sb_index
].cc_sb
);
2028 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2030 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2032 * The reserve counter have been concurrently updated
2033 * while we read the commit counter. This means the
2034 * commit counter we read might not match buf->offset
2035 * due to concurrent update. We therefore need to retry.
2039 reserve_commit_diff
=
2040 (buf_trunc(offsets
->begin
, chan
)
2041 >> chan
->backend
.num_subbuf_order
)
2042 - (commit_count
& chan
->commit_count_mask
);
2043 if (likely(reserve_commit_diff
== 0)) {
2044 /* Next subbuffer not being written to. */
2045 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2046 subbuf_trunc(offsets
->begin
, chan
)
2047 - subbuf_trunc((unsigned long)
2048 atomic_long_read(&buf
->consumed
), chan
)
2049 >= chan
->backend
.buf_size
)) {
2051 * We do not overwrite non consumed buffers
2052 * and we are full : record is lost.
2054 v_inc(config
, &buf
->records_lost_full
);
2058 * Next subbuffer not being written to, and we
2059 * are either in overwrite mode or the buffer is
2060 * not full. It's safe to write in this new
2066 * Next subbuffer reserve offset does not match the
2067 * commit offset, and this did not involve update to the
2068 * reserve counter. Drop record in producer-consumer and
2069 * overwrite mode. Caused by either a writer OOPS or
2070 * too many nested writes over a reserve/commit pair.
2072 v_inc(config
, &buf
->records_lost_wrap
);
2076 config
->cb
.record_header_size(config
, chan
,
2078 &offsets
->pre_header_padding
,
2081 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2084 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2085 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2087 * Record too big for subbuffers, report error, don't
2088 * complete the sub-buffer switch.
2090 v_inc(config
, &buf
->records_lost_big
);
2094 * We just made a successful buffer switch and the
2095 * record fits in the new subbuffer. Let's write.
2100 * Record fits in the current buffer and we are not on a switch
2101 * boundary. It's safe to write.
2104 offsets
->end
= offsets
->begin
+ offsets
->size
;
2106 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2108 * The offset_end will fall at the very beginning of the next
2111 offsets
->switch_new_end
= 1; /* For offsets->begin */
2116 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2118 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2120 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2121 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2123 return chan
->backend
.buf
;
2126 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2128 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2129 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2131 v_inc(config
, &buf
->records_lost_big
);
2133 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2136 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2137 * @ctx: ring buffer context.
2139 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2140 * -EIO for other errors, else returns 0.
2141 * It will take care of sub-buffer switching.
2143 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
2145 struct channel
*chan
= ctx
->chan
;
2146 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2147 struct lib_ring_buffer
*buf
;
2148 struct switch_offsets offsets
;
2151 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2155 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2159 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2164 * Atomically update last_tsc. This update races against concurrent
2165 * atomic updates, but the race will always cause supplementary full TSC
2166 * records, never the opposite (missing a full TSC record when it would
2169 save_last_tsc(config
, buf
, ctx
->tsc
);
2172 * Push the reader if necessary
2174 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2177 * Clear noref flag for this subbuffer.
2179 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2180 subbuf_index(offsets
.end
- 1, chan
));
2183 * Switch old subbuffer if needed.
2185 if (unlikely(offsets
.switch_old_end
)) {
2186 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2187 subbuf_index(offsets
.old
- 1, chan
));
2188 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2192 * Populate new subbuffer.
2194 if (unlikely(offsets
.switch_new_start
))
2195 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2197 if (unlikely(offsets
.switch_new_end
))
2198 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2200 ctx
->slot_size
= offsets
.size
;
2201 ctx
->pre_offset
= offsets
.begin
;
2202 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2205 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2208 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2209 struct lib_ring_buffer
*buf
,
2210 unsigned long commit_count
,
2213 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2214 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2218 * The ring buffer can count events recorded and overwritten per buffer,
2219 * but it is disabled by default due to its performance overhead.
2221 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2223 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2224 struct lib_ring_buffer
*buf
,
2227 v_add(config
, subbuffer_get_records_count(config
,
2228 &buf
->backend
, idx
),
2229 &buf
->records_count
);
2230 v_add(config
, subbuffer_count_records_overrun(config
,
2231 &buf
->backend
, idx
),
2232 &buf
->records_overrun
);
2234 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2236 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2237 struct lib_ring_buffer
*buf
,
2241 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2244 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2245 struct lib_ring_buffer
*buf
,
2246 struct channel
*chan
,
2247 unsigned long offset
,
2248 unsigned long commit_count
,
2252 unsigned long old_commit_count
= commit_count
2253 - chan
->backend
.subbuf_size
;
2256 * If we succeeded at updating cc_sb below, we are the subbuffer
2257 * writer delivering the subbuffer. Deals with concurrent
2258 * updates of the "cc" value without adding a add_return atomic
2259 * operation to the fast path.
2261 * We are doing the delivery in two steps:
2262 * - First, we cmpxchg() cc_sb to the new value
2263 * old_commit_count + 1. This ensures that we are the only
2264 * subbuffer user successfully filling the subbuffer, but we
2265 * do _not_ set the cc_sb value to "commit_count" yet.
2266 * Therefore, other writers that would wrap around the ring
2267 * buffer and try to start writing to our subbuffer would
2268 * have to drop records, because it would appear as
2270 * We therefore have exclusive access to the subbuffer control
2271 * structures. This mutual exclusion with other writers is
2272 * crucially important to perform record overruns count in
2273 * flight recorder mode locklessly.
2274 * - When we are ready to release the subbuffer (either for
2275 * reading or for overrun by other writers), we simply set the
2276 * cc_sb value to "commit_count" and perform delivery.
2278 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2279 * This guarantees that old_commit_count + 1 != commit_count.
2283 * Order prior updates to reserve count prior to the
2284 * commit_cold cc_sb update.
2287 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2288 old_commit_count
, old_commit_count
+ 1)
2289 == old_commit_count
)) {
2293 * Start of exclusive subbuffer access. We are
2294 * guaranteed to be the last writer in this subbuffer
2295 * and any other writer trying to access this subbuffer
2296 * in this state is required to drop records.
2298 * We can read the ts_end for the current sub-buffer
2299 * which has been saved by the very last space
2300 * reservation for the current sub-buffer.
2302 * Order increment of commit counter before reading ts_end.
2305 ts_end
= &buf
->ts_end
[idx
];
2306 deliver_count_events(config
, buf
, idx
);
2307 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2308 lib_ring_buffer_get_data_size(config
,
2313 * Increment the packet counter while we have exclusive
2316 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2319 * Set noref flag and offset for this subbuffer id.
2320 * Contains a memory barrier that ensures counter stores
2321 * are ordered before set noref and offset.
2323 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2324 buf_trunc_val(offset
, chan
));
2327 * Order set_noref and record counter updates before the
2328 * end of subbuffer exclusive access. Orders with
2329 * respect to writers coming into the subbuffer after
2330 * wrap around, and also order wrt concurrent readers.
2333 /* End of exclusive subbuffer access */
2334 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2337 * Order later updates to reserve count after
2338 * the commit_cold cc_sb update.
2341 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2345 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2347 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2348 && atomic_long_read(&buf
->active_readers
)
2349 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2350 wake_up_interruptible(&buf
->read_wait
);
2351 wake_up_interruptible(&chan
->read_wait
);
2356 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2358 int __init
init_lib_ring_buffer_frontend(void)
2362 for_each_possible_cpu(cpu
)
2363 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2367 module_init(init_lib_ring_buffer_frontend
);
2369 void __exit
exit_lib_ring_buffer_frontend(void)
2373 module_exit(exit_lib_ring_buffer_frontend
);