1 /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
3 * ring_buffer_frontend.c
5 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
8 * recorder (overwrite) modes. See thesis:
10 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
11 * dissertation, Ecole Polytechnique de Montreal.
12 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
14 * - Algorithm presentation in Chapter 5:
15 * "Lockless Multi-Core High-Throughput Buffering".
16 * - Algorithm formal verification in Section 8.6:
17 * "Formal verification of LTTng"
20 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
22 * Inspired from LTT and RelayFS:
23 * Karim Yaghmour <karim@opersys.com>
24 * Tom Zanussi <zanussi@us.ibm.com>
25 * Bob Wisniewski <bob@watson.ibm.com>
27 * Bob Wisniewski <bob@watson.ibm.com>
29 * Buffer reader semantic :
32 * while buffer is not finalized and empty
34 * - if return value != 0, continue
35 * - splice one subbuffer worth of data to a pipe
36 * - splice the data from pipe to disk/network
40 #include <linux/delay.h>
41 #include <linux/module.h>
42 #include <linux/percpu.h>
43 #include <asm/cacheflush.h>
45 #include <wrapper/ringbuffer/config.h>
46 #include <wrapper/ringbuffer/backend.h>
47 #include <wrapper/ringbuffer/frontend.h>
48 #include <wrapper/ringbuffer/iterator.h>
49 #include <wrapper/ringbuffer/nohz.h>
50 #include <wrapper/atomic.h>
51 #include <wrapper/cpu.h>
52 #include <wrapper/kref.h>
53 #include <wrapper/percpu-defs.h>
54 #include <wrapper/timer.h>
55 #include <wrapper/vmalloc.h>
58 * Internal structure representing offsets to use at a sub-buffer switch.
60 struct switch_offsets
{
61 unsigned long begin
, end
, old
;
62 size_t pre_header_padding
, size
;
63 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
74 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
75 #endif /* CONFIG_NO_HZ */
77 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
79 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
80 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
83 void lib_ring_buffer_print_errors(struct channel
*chan
,
84 struct lib_ring_buffer
*buf
, int cpu
);
86 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
87 enum switch_mode mode
);
90 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
91 struct lib_ring_buffer
*buf
,
94 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
96 consumed_old
= atomic_long_read(&buf
->consumed
);
97 consumed_idx
= subbuf_index(consumed_old
, chan
);
98 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
100 * No memory barrier here, since we are only interested
101 * in a statistically correct polling result. The next poll will
102 * get the data is we are racing. The mb() that ensures correct
103 * memory order is in get_subbuf.
105 write_offset
= v_read(config
, &buf
->offset
);
108 * Check that the subbuffer we are trying to consume has been
109 * already fully committed.
112 if (((commit_count
- chan
->backend
.subbuf_size
)
113 & chan
->commit_count_mask
)
114 - (buf_trunc(consumed_old
, chan
)
115 >> chan
->backend
.num_subbuf_order
)
120 * Check that we are not about to read the same subbuffer in
121 * which the writer head is.
123 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
131 * Must be called under cpu hotplug protection.
133 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
135 struct channel
*chan
= buf
->backend
.chan
;
137 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
138 lttng_kvfree(buf
->commit_hot
);
139 lttng_kvfree(buf
->commit_cold
);
140 lttng_kvfree(buf
->ts_end
);
142 lib_ring_buffer_backend_free(&buf
->backend
);
146 * lib_ring_buffer_reset - Reset ring buffer to initial values.
149 * Effectively empty the ring buffer. Should be called when the buffer is not
150 * used for writing. The ring buffer can be opened for reading, but the reader
151 * should not be using the iterator concurrently with reset. The previous
152 * current iterator record is reset.
154 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
156 struct channel
*chan
= buf
->backend
.chan
;
157 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
161 * Reset iterator first. It will put the subbuffer if it currently holds
164 lib_ring_buffer_iterator_reset(buf
);
165 v_set(config
, &buf
->offset
, 0);
166 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
167 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
168 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
169 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
172 atomic_long_set(&buf
->consumed
, 0);
173 atomic_set(&buf
->record_disabled
, 0);
174 v_set(config
, &buf
->last_tsc
, 0);
175 lib_ring_buffer_backend_reset(&buf
->backend
);
176 /* Don't reset number of active readers */
177 v_set(config
, &buf
->records_lost_full
, 0);
178 v_set(config
, &buf
->records_lost_wrap
, 0);
179 v_set(config
, &buf
->records_lost_big
, 0);
180 v_set(config
, &buf
->records_count
, 0);
181 v_set(config
, &buf
->records_overrun
, 0);
184 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
187 * channel_reset - Reset channel to initial values.
190 * Effectively empty the channel. Should be called when the channel is not used
191 * for writing. The channel can be opened for reading, but the reader should not
192 * be using the iterator concurrently with reset. The previous current iterator
195 void channel_reset(struct channel
*chan
)
198 * Reset iterators first. Will put the subbuffer if held for reading.
200 channel_iterator_reset(chan
);
201 atomic_set(&chan
->record_disabled
, 0);
202 /* Don't reset commit_count_mask, still valid */
203 channel_backend_reset(&chan
->backend
);
204 /* Don't reset switch/read timer interval */
205 /* Don't reset notifiers and notifier enable bits */
206 /* Don't reset reader reference count */
208 EXPORT_SYMBOL_GPL(channel_reset
);
211 * Must be called under cpu hotplug protection.
213 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
214 struct channel_backend
*chanb
, int cpu
)
216 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
217 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
218 void *priv
= chanb
->priv
;
219 size_t subbuf_header_size
;
223 /* Test for cpu hotplug */
224 if (buf
->backend
.allocated
)
228 * Paranoia: per cpu dynamic allocation is not officially documented as
229 * zeroing the memory, so let's do it here too, just in case.
231 memset(buf
, 0, sizeof(*buf
));
233 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
238 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
239 * chan
->backend
.num_subbuf
,
240 1 << INTERNODE_CACHE_SHIFT
),
241 GFP_KERNEL
| __GFP_NOWARN
,
242 cpu_to_node(max(cpu
, 0)));
243 if (!buf
->commit_hot
) {
249 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
250 * chan
->backend
.num_subbuf
,
251 1 << INTERNODE_CACHE_SHIFT
),
252 GFP_KERNEL
| __GFP_NOWARN
,
253 cpu_to_node(max(cpu
, 0)));
254 if (!buf
->commit_cold
) {
260 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
261 * chan
->backend
.num_subbuf
,
262 1 << INTERNODE_CACHE_SHIFT
),
263 GFP_KERNEL
| __GFP_NOWARN
,
264 cpu_to_node(max(cpu
, 0)));
267 goto free_commit_cold
;
270 init_waitqueue_head(&buf
->read_wait
);
271 init_waitqueue_head(&buf
->write_wait
);
272 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
275 * Write the subbuffer header for first subbuffer so we know the total
276 * duration of data gathering.
278 subbuf_header_size
= config
->cb
.subbuffer_header_size();
279 v_set(config
, &buf
->offset
, subbuf_header_size
);
280 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
281 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
282 config
->cb
.buffer_begin(buf
, tsc
, 0);
283 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
285 if (config
->cb
.buffer_create
) {
286 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
292 * Ensure the buffer is ready before setting it to allocated and setting
294 * Used for cpu hotplug vs cpumask iteration.
297 buf
->backend
.allocated
= 1;
299 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
300 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
301 chan
->backend
.cpumask
));
302 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
309 lttng_kvfree(buf
->ts_end
);
311 lttng_kvfree(buf
->commit_cold
);
313 lttng_kvfree(buf
->commit_hot
);
315 lib_ring_buffer_backend_free(&buf
->backend
);
319 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
321 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, switch_timer
);
322 struct channel
*chan
= buf
->backend
.chan
;
323 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
326 * Only flush buffers periodically if readers are active.
328 if (atomic_long_read(&buf
->active_readers
))
329 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
331 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
332 lttng_mod_timer_pinned(&buf
->switch_timer
,
333 jiffies
+ chan
->switch_timer_interval
);
335 mod_timer(&buf
->switch_timer
,
336 jiffies
+ chan
->switch_timer_interval
);
340 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
342 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
344 struct channel
*chan
= buf
->backend
.chan
;
345 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
346 unsigned int flags
= 0;
348 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
351 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
352 flags
= LTTNG_TIMER_PINNED
;
354 lttng_timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
, buf
);
355 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
357 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
358 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
360 add_timer(&buf
->switch_timer
);
362 buf
->switch_timer_enabled
= 1;
366 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
368 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
370 struct channel
*chan
= buf
->backend
.chan
;
372 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
375 del_timer_sync(&buf
->switch_timer
);
376 buf
->switch_timer_enabled
= 0;
380 * Polling timer to check the channels for data.
382 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
384 struct lib_ring_buffer
*buf
= lttng_from_timer(buf
, t
, read_timer
);
385 struct channel
*chan
= buf
->backend
.chan
;
386 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
388 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
390 if (atomic_long_read(&buf
->active_readers
)
391 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
392 wake_up_interruptible(&buf
->read_wait
);
393 wake_up_interruptible(&chan
->read_wait
);
396 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
397 lttng_mod_timer_pinned(&buf
->read_timer
,
398 jiffies
+ chan
->read_timer_interval
);
400 mod_timer(&buf
->read_timer
,
401 jiffies
+ chan
->read_timer_interval
);
405 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
407 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
409 struct channel
*chan
= buf
->backend
.chan
;
410 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
411 unsigned int flags
= 0;
413 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
414 || !chan
->read_timer_interval
415 || buf
->read_timer_enabled
)
418 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
419 flags
= LTTNG_TIMER_PINNED
;
421 lttng_timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
, buf
);
422 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
424 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
425 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
427 add_timer(&buf
->read_timer
);
429 buf
->read_timer_enabled
= 1;
433 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
435 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
437 struct channel
*chan
= buf
->backend
.chan
;
438 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
440 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
441 || !chan
->read_timer_interval
442 || !buf
->read_timer_enabled
)
445 del_timer_sync(&buf
->read_timer
);
447 * do one more check to catch data that has been written in the last
450 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
451 wake_up_interruptible(&buf
->read_wait
);
452 wake_up_interruptible(&chan
->read_wait
);
454 buf
->read_timer_enabled
= 0;
457 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
459 enum cpuhp_state lttng_rb_hp_prepare
;
460 enum cpuhp_state lttng_rb_hp_online
;
462 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
464 lttng_rb_hp_prepare
= val
;
466 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
468 void lttng_rb_set_hp_online(enum cpuhp_state val
)
470 lttng_rb_hp_online
= val
;
472 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
474 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
475 struct lttng_cpuhp_node
*node
)
477 struct channel
*chan
= container_of(node
, struct channel
,
479 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
480 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
482 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
485 * Performing a buffer switch on a remote CPU. Performed by
486 * the CPU responsible for doing the hotunplug after the target
487 * CPU stopped running completely. Ensures that all data
488 * from that remote CPU is flushed.
490 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
493 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
495 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
496 struct lttng_cpuhp_node
*node
)
498 struct channel
*chan
= container_of(node
, struct channel
,
500 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
501 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
503 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
505 wake_up_interruptible(&chan
->hp_wait
);
506 lib_ring_buffer_start_switch_timer(buf
);
507 lib_ring_buffer_start_read_timer(buf
);
510 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
512 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
513 struct lttng_cpuhp_node
*node
)
515 struct channel
*chan
= container_of(node
, struct channel
,
517 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
518 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
520 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
522 lib_ring_buffer_stop_switch_timer(buf
);
523 lib_ring_buffer_stop_read_timer(buf
);
526 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
528 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
530 #ifdef CONFIG_HOTPLUG_CPU
533 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
534 * @nb: notifier block
535 * @action: hotplug action to take
538 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
541 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
542 unsigned long action
,
545 unsigned int cpu
= (unsigned long)hcpu
;
546 struct channel
*chan
= container_of(nb
, struct channel
,
548 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
549 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
551 if (!chan
->cpu_hp_enable
)
554 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
557 case CPU_DOWN_FAILED
:
558 case CPU_DOWN_FAILED_FROZEN
:
560 case CPU_ONLINE_FROZEN
:
561 wake_up_interruptible(&chan
->hp_wait
);
562 lib_ring_buffer_start_switch_timer(buf
);
563 lib_ring_buffer_start_read_timer(buf
);
566 case CPU_DOWN_PREPARE
:
567 case CPU_DOWN_PREPARE_FROZEN
:
568 lib_ring_buffer_stop_switch_timer(buf
);
569 lib_ring_buffer_stop_read_timer(buf
);
573 case CPU_DEAD_FROZEN
:
575 * Performing a buffer switch on a remote CPU. Performed by
576 * the CPU responsible for doing the hotunplug after the target
577 * CPU stopped running completely. Ensures that all data
578 * from that remote CPU is flushed.
580 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
590 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
592 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
594 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
595 * that wake-up-tracing generated events are flushed before going idle (in
596 * tick_nohz). We test if the spinlock is locked to deal with the race where
597 * readers try to sample the ring buffer before we perform the switch. We let
598 * the readers retry in that case. If there is data in the buffer, the wake up
599 * is going to forbid the CPU running the reader thread from going idle.
601 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
605 struct channel
*chan
= container_of(nb
, struct channel
,
607 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
608 struct lib_ring_buffer
*buf
;
609 int cpu
= smp_processor_id();
611 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
613 * We don't support keeping the system idle with global buffers
614 * and streaming active. In order to do so, we would need to
615 * sample a non-nohz-cpumask racelessly with the nohz updates
616 * without adding synchronization overhead to nohz. Leave this
617 * use-case out for now.
622 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
624 case TICK_NOHZ_FLUSH
:
625 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
626 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
627 && chan
->read_timer_interval
628 && atomic_long_read(&buf
->active_readers
)
629 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
630 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
631 wake_up_interruptible(&buf
->read_wait
);
632 wake_up_interruptible(&chan
->read_wait
);
634 if (chan
->switch_timer_interval
)
635 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
636 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
639 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
640 lib_ring_buffer_stop_switch_timer(buf
);
641 lib_ring_buffer_stop_read_timer(buf
);
642 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
644 case TICK_NOHZ_RESTART
:
645 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
646 lib_ring_buffer_start_read_timer(buf
);
647 lib_ring_buffer_start_switch_timer(buf
);
648 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
655 void notrace
lib_ring_buffer_tick_nohz_flush(void)
657 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
661 void notrace
lib_ring_buffer_tick_nohz_stop(void)
663 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
667 void notrace
lib_ring_buffer_tick_nohz_restart(void)
669 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
672 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
677 static void channel_unregister_notifiers(struct channel
*chan
)
679 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
681 channel_iterator_unregister_notifiers(chan
);
682 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
685 * Remove the nohz notifier first, so we are certain we stop
688 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
689 &chan
->tick_nohz_notifier
);
691 * ring_buffer_nohz_lock will not be needed below, because
692 * we just removed the notifiers, which were the only source of
695 #endif /* CONFIG_NO_HZ */
696 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
700 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
701 &chan
->cpuhp_online
.node
);
703 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
704 &chan
->cpuhp_prepare
.node
);
707 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
711 #ifdef CONFIG_HOTPLUG_CPU
712 lttng_cpus_read_lock();
713 chan
->cpu_hp_enable
= 0;
714 for_each_online_cpu(cpu
) {
715 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
717 lib_ring_buffer_stop_switch_timer(buf
);
718 lib_ring_buffer_stop_read_timer(buf
);
720 lttng_cpus_read_unlock();
721 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
723 for_each_possible_cpu(cpu
) {
724 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
726 lib_ring_buffer_stop_switch_timer(buf
);
727 lib_ring_buffer_stop_read_timer(buf
);
731 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
733 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
735 lib_ring_buffer_stop_switch_timer(buf
);
736 lib_ring_buffer_stop_read_timer(buf
);
738 channel_backend_unregister_notifiers(&chan
->backend
);
741 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
743 if (!buf
->quiescent
) {
744 buf
->quiescent
= true;
745 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
749 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
751 buf
->quiescent
= false;
754 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
757 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
759 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
760 lttng_cpus_read_lock();
761 for_each_channel_cpu(cpu
, chan
) {
762 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
765 lib_ring_buffer_set_quiescent(buf
);
767 lttng_cpus_read_unlock();
769 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
771 lib_ring_buffer_set_quiescent(buf
);
774 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
776 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
779 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
781 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
782 lttng_cpus_read_lock();
783 for_each_channel_cpu(cpu
, chan
) {
784 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
787 lib_ring_buffer_clear_quiescent(buf
);
789 lttng_cpus_read_unlock();
791 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
793 lib_ring_buffer_clear_quiescent(buf
);
796 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
798 static void channel_free(struct channel
*chan
)
800 if (chan
->backend
.release_priv_ops
) {
801 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
803 channel_iterator_free(chan
);
804 channel_backend_free(&chan
->backend
);
809 * channel_create - Create channel.
810 * @config: ring buffer instance configuration
811 * @name: name of the channel
812 * @priv: ring buffer client private data
813 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
814 * address mapping. It is used only by RING_BUFFER_STATIC
815 * configuration. It can be set to NULL for other backends.
816 * @subbuf_size: subbuffer size
817 * @num_subbuf: number of subbuffers
818 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
819 * padding to let readers get those sub-buffers.
820 * Used for live streaming.
821 * @read_timer_interval: Time interval (in us) to wake up pending readers.
824 * Returns NULL on failure.
826 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
827 const char *name
, void *priv
, void *buf_addr
,
829 size_t num_subbuf
, unsigned int switch_timer_interval
,
830 unsigned int read_timer_interval
)
833 struct channel
*chan
;
835 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
836 read_timer_interval
))
839 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
843 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
844 subbuf_size
, num_subbuf
);
848 ret
= channel_iterator_init(chan
);
850 goto error_free_backend
;
852 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
853 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
854 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
855 kref_init(&chan
->ref
);
856 init_waitqueue_head(&chan
->read_wait
);
857 init_waitqueue_head(&chan
->hp_wait
);
859 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
860 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
861 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
862 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
863 &chan
->cpuhp_prepare
.node
);
865 goto cpuhp_prepare_error
;
867 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
868 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
869 &chan
->cpuhp_online
.node
);
871 goto cpuhp_online_error
;
872 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
876 * In case of non-hotplug cpu, if the ring-buffer is allocated
877 * in early initcall, it will not be notified of secondary cpus.
878 * In that off case, we need to allocate for all possible cpus.
880 #ifdef CONFIG_HOTPLUG_CPU
881 chan
->cpu_hp_notifier
.notifier_call
=
882 lib_ring_buffer_cpu_hp_callback
;
883 chan
->cpu_hp_notifier
.priority
= 6;
884 register_cpu_notifier(&chan
->cpu_hp_notifier
);
886 lttng_cpus_read_lock();
887 for_each_online_cpu(cpu
) {
888 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
890 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
891 lib_ring_buffer_start_switch_timer(buf
);
892 lib_ring_buffer_start_read_timer(buf
);
893 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
895 chan
->cpu_hp_enable
= 1;
896 lttng_cpus_read_unlock();
898 for_each_possible_cpu(cpu
) {
899 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
901 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
902 lib_ring_buffer_start_switch_timer(buf
);
903 lib_ring_buffer_start_read_timer(buf
);
904 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
908 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
910 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
911 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
912 chan
->tick_nohz_notifier
.notifier_call
=
913 ring_buffer_tick_nohz_callback
;
914 chan
->tick_nohz_notifier
.priority
= ~0U;
915 atomic_notifier_chain_register(&tick_nohz_notifier
,
916 &chan
->tick_nohz_notifier
);
917 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
920 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
922 lib_ring_buffer_start_switch_timer(buf
);
923 lib_ring_buffer_start_read_timer(buf
);
928 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
930 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
931 &chan
->cpuhp_prepare
.node
);
934 #endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
936 channel_backend_free(&chan
->backend
);
941 EXPORT_SYMBOL_GPL(channel_create
);
944 void channel_release(struct kref
*kref
)
946 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
951 * channel_destroy - Finalize, wait for q.s. and destroy channel.
952 * @chan: channel to destroy
955 * Call "destroy" callback, finalize channels, and then decrement the
956 * channel reference count. Note that when readers have completed data
957 * consumption of finalized channels, get_subbuf() will return -ENODATA.
958 * They should release their handle at that point. Returns the private
961 void *channel_destroy(struct channel
*chan
)
964 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
967 channel_unregister_notifiers(chan
);
969 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
971 * No need to hold cpu hotplug, because all notifiers have been
974 for_each_channel_cpu(cpu
, chan
) {
975 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
978 if (config
->cb
.buffer_finalize
)
979 config
->cb
.buffer_finalize(buf
,
983 * Perform flush before writing to finalized.
986 WRITE_ONCE(buf
->finalized
, 1);
987 wake_up_interruptible(&buf
->read_wait
);
990 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
992 if (config
->cb
.buffer_finalize
)
993 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
995 * Perform flush before writing to finalized.
998 WRITE_ONCE(buf
->finalized
, 1);
999 wake_up_interruptible(&buf
->read_wait
);
1001 WRITE_ONCE(chan
->finalized
, 1);
1002 wake_up_interruptible(&chan
->hp_wait
);
1003 wake_up_interruptible(&chan
->read_wait
);
1004 priv
= chan
->backend
.priv
;
1005 kref_put(&chan
->ref
, channel_release
);
1008 EXPORT_SYMBOL_GPL(channel_destroy
);
1010 struct lib_ring_buffer
*channel_get_ring_buffer(
1011 const struct lib_ring_buffer_config
*config
,
1012 struct channel
*chan
, int cpu
)
1014 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1015 return chan
->backend
.buf
;
1017 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1019 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1021 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1023 struct channel
*chan
= buf
->backend
.chan
;
1025 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1027 if (!lttng_kref_get(&chan
->ref
)) {
1028 atomic_long_dec(&buf
->active_readers
);
1031 lttng_smp_mb__after_atomic();
1034 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1036 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1038 struct channel
*chan
= buf
->backend
.chan
;
1040 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1041 lttng_smp_mb__before_atomic();
1042 atomic_long_dec(&buf
->active_readers
);
1043 kref_put(&chan
->ref
, channel_release
);
1045 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1048 * Promote compiler barrier to a smp_mb().
1049 * For the specific ring buffer case, this IPI call should be removed if the
1050 * architecture does not reorder writes. This should eventually be provided by
1051 * a separate architecture-specific infrastructure.
1053 static void remote_mb(void *info
)
1059 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1061 * @consumed: consumed count indicating the position where to read
1062 * @produced: produced count, indicates position when to stop reading
1064 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1065 * data to read at consumed position, or 0 if the get operation succeeds.
1066 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1069 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1070 unsigned long *consumed
, unsigned long *produced
)
1072 struct channel
*chan
= buf
->backend
.chan
;
1073 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1074 unsigned long consumed_cur
, write_offset
;
1078 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1080 * Read finalized before counters.
1083 consumed_cur
= atomic_long_read(&buf
->consumed
);
1085 * No need to issue a memory barrier between consumed count read and
1086 * write offset read, because consumed count can only change
1087 * concurrently in overwrite mode, and we keep a sequence counter
1088 * identifier derived from the write offset to check we are getting
1089 * the same sub-buffer we are expecting (the sub-buffers are atomically
1090 * "tagged" upon writes, tags are checked upon read).
1092 write_offset
= v_read(config
, &buf
->offset
);
1095 * Check that we are not about to read the same subbuffer in
1096 * which the writer head is.
1098 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1102 *consumed
= consumed_cur
;
1103 *produced
= subbuf_trunc(write_offset
, chan
);
1109 * The memory barriers __wait_event()/wake_up_interruptible() take care
1110 * of "raw_spin_is_locked" memory ordering.
1114 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1119 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1122 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1123 * are saved regardless of whether the consumed and produced positions are
1124 * in the same subbuffer.
1126 * @consumed: consumed byte count indicating the last position read
1127 * @produced: produced byte count indicating the last position written
1129 * This function is meant to provide information on the exact producer and
1130 * consumer positions without regard for the "snapshot" feature.
1132 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1133 unsigned long *consumed
, unsigned long *produced
)
1135 struct channel
*chan
= buf
->backend
.chan
;
1136 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1139 *consumed
= atomic_long_read(&buf
->consumed
);
1141 * No need to issue a memory barrier between consumed count read and
1142 * write offset read, because consumed count can only change
1143 * concurrently in overwrite mode, and we keep a sequence counter
1144 * identifier derived from the write offset to check we are getting
1145 * the same sub-buffer we are expecting (the sub-buffers are atomically
1146 * "tagged" upon writes, tags are checked upon read).
1148 *produced
= v_read(config
, &buf
->offset
);
1153 * lib_ring_buffer_put_snapshot - move consumed counter forward
1155 * Should only be called from consumer context.
1157 * @consumed_new: new consumed count value
1159 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1160 unsigned long consumed_new
)
1162 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1163 struct channel
*chan
= bufb
->chan
;
1164 unsigned long consumed
;
1166 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1169 * Only push the consumed value forward.
1170 * If the consumed cmpxchg fails, this is because we have been pushed by
1171 * the writer in flight recorder mode.
1173 consumed
= atomic_long_read(&buf
->consumed
);
1174 while ((long) consumed
- (long) consumed_new
< 0)
1175 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1177 /* Wake-up the metadata producer */
1178 wake_up_interruptible(&buf
->write_wait
);
1180 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1182 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1183 static void lib_ring_buffer_flush_read_subbuf_dcache(
1184 const struct lib_ring_buffer_config
*config
,
1185 struct channel
*chan
,
1186 struct lib_ring_buffer
*buf
)
1188 struct lib_ring_buffer_backend_pages
*pages
;
1189 unsigned long sb_bindex
, id
, i
, nr_pages
;
1191 if (config
->output
!= RING_BUFFER_MMAP
)
1195 * Architectures with caches aliased on virtual addresses may
1196 * use different cache lines for the linear mapping vs
1197 * user-space memory mapping. Given that the ring buffer is
1198 * based on the kernel linear mapping, aligning it with the
1199 * user-space mapping is not straightforward, and would require
1200 * extra TLB entries. Therefore, simply flush the dcache for the
1201 * entire sub-buffer before reading it.
1203 id
= buf
->backend
.buf_rsb
.id
;
1204 sb_bindex
= subbuffer_id_get_index(config
, id
);
1205 pages
= buf
->backend
.array
[sb_bindex
];
1206 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1207 for (i
= 0; i
< nr_pages
; i
++) {
1208 struct lib_ring_buffer_backend_page
*backend_page
;
1210 backend_page
= &pages
->p
[i
];
1211 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1215 static void lib_ring_buffer_flush_read_subbuf_dcache(
1216 const struct lib_ring_buffer_config
*config
,
1217 struct channel
*chan
,
1218 struct lib_ring_buffer
*buf
)
1224 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1226 * @consumed: consumed count indicating the position where to read
1228 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1229 * data to read at consumed position, or 0 if the get operation succeeds.
1230 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1232 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1233 unsigned long consumed
)
1235 struct channel
*chan
= buf
->backend
.chan
;
1236 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1237 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1241 if (buf
->get_subbuf
) {
1243 * Reader is trying to get a subbuffer twice.
1245 CHAN_WARN_ON(chan
, 1);
1249 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1251 * Read finalized before counters.
1254 consumed_cur
= atomic_long_read(&buf
->consumed
);
1255 consumed_idx
= subbuf_index(consumed
, chan
);
1256 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1258 * Make sure we read the commit count before reading the buffer
1259 * data and the write offset. Correct consumed offset ordering
1260 * wrt commit count is insured by the use of cmpxchg to update
1261 * the consumed offset.
1262 * smp_call_function_single can fail if the remote CPU is offline,
1263 * this is OK because then there is no wmb to execute there.
1264 * If our thread is executing on the same CPU as the on the buffers
1265 * belongs to, we don't have to synchronize it at all. If we are
1266 * migrated, the scheduler will take care of the memory barriers.
1267 * Normally, smp_call_function_single() should ensure program order when
1268 * executing the remote function, which implies that it surrounds the
1269 * function execution with :
1280 * However, smp_call_function_single() does not seem to clearly execute
1281 * such barriers. It depends on spinlock semantic to provide the barrier
1282 * before executing the IPI and, when busy-looping, csd_lock_wait only
1283 * executes smp_mb() when it has to wait for the other CPU.
1285 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1286 * required ourself, even if duplicated. It has no performance impact
1289 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1290 * read and write vs write. They do not ensure core synchronization. We
1291 * really have to ensure total order between the 3 barriers running on
1294 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1295 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1296 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1297 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1298 /* Total order with IPI handler smp_mb() */
1300 smp_call_function_single(buf
->backend
.cpu
,
1301 remote_mb
, NULL
, 1);
1302 /* Total order with IPI handler smp_mb() */
1306 /* Total order with IPI handler smp_mb() */
1308 smp_call_function(remote_mb
, NULL
, 1);
1309 /* Total order with IPI handler smp_mb() */
1314 * Local rmb to match the remote wmb to read the commit count
1315 * before the buffer data and the write offset.
1320 write_offset
= v_read(config
, &buf
->offset
);
1323 * Check that the buffer we are getting is after or at consumed_cur
1326 if ((long) subbuf_trunc(consumed
, chan
)
1327 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1331 * Check that the subbuffer we are trying to consume has been
1332 * already fully committed.
1334 if (((commit_count
- chan
->backend
.subbuf_size
)
1335 & chan
->commit_count_mask
)
1336 - (buf_trunc(consumed
, chan
)
1337 >> chan
->backend
.num_subbuf_order
)
1342 * Check that we are not about to read the same subbuffer in
1343 * which the writer head is.
1345 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1350 * Failure to get the subbuffer causes a busy-loop retry without going
1351 * to a wait queue. These are caused by short-lived race windows where
1352 * the writer is getting access to a subbuffer we were trying to get
1353 * access to. Also checks that the "consumed" buffer count we are
1354 * looking for matches the one contained in the subbuffer id.
1356 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1357 consumed_idx
, buf_trunc_val(consumed
, chan
));
1360 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1362 buf
->get_subbuf_consumed
= consumed
;
1363 buf
->get_subbuf
= 1;
1365 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1371 * The memory barriers __wait_event()/wake_up_interruptible() take care
1372 * of "raw_spin_is_locked" memory ordering.
1376 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1381 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1384 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1387 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1389 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1390 struct channel
*chan
= bufb
->chan
;
1391 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1392 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1394 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1396 if (!buf
->get_subbuf
) {
1398 * Reader puts a subbuffer it did not get.
1400 CHAN_WARN_ON(chan
, 1);
1403 consumed
= buf
->get_subbuf_consumed
;
1404 buf
->get_subbuf
= 0;
1407 * Clear the records_unread counter. (overruns counter)
1408 * Can still be non-zero if a file reader simply grabbed the data
1409 * without using iterators.
1410 * Can be below zero if an iterator is used on a snapshot more than
1413 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1414 v_add(config
, v_read(config
,
1415 &bufb
->array
[read_sb_bindex
]->records_unread
),
1416 &bufb
->records_read
);
1417 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1418 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1419 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1420 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1423 * Exchange the reader subbuffer with the one we put in its place in the
1424 * writer subbuffer table. Expect the original consumed count. If
1425 * update_read_sb_index fails, this is because the writer updated the
1426 * subbuffer concurrently. We should therefore keep the subbuffer we
1427 * currently have: it has become invalid to try reading this sub-buffer
1428 * consumed count value anyway.
1430 consumed_idx
= subbuf_index(consumed
, chan
);
1431 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1432 consumed_idx
, buf_trunc_val(consumed
, chan
));
1434 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1435 * if the writer concurrently updated it.
1438 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1441 * cons_offset is an iterator on all subbuffer offsets between the reader
1442 * position and the writer position. (inclusive)
1445 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1446 struct channel
*chan
,
1447 unsigned long cons_offset
,
1450 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1451 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1453 cons_idx
= subbuf_index(cons_offset
, chan
);
1454 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1455 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1457 if (subbuf_offset(commit_count
, chan
) != 0)
1459 "ring buffer %s, cpu %d: "
1460 "commit count in subbuffer %lu,\n"
1461 "expecting multiples of %lu bytes\n"
1462 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1463 chan
->backend
.name
, cpu
, cons_idx
,
1464 chan
->backend
.subbuf_size
,
1465 commit_count
, commit_count_sb
);
1467 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1468 chan
->backend
.name
, cpu
, commit_count
);
1472 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1473 struct channel
*chan
,
1474 void *priv
, int cpu
)
1476 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1477 unsigned long write_offset
, cons_offset
;
1480 * No need to order commit_count, write_offset and cons_offset reads
1481 * because we execute at teardown when no more writer nor reader
1482 * references are left.
1484 write_offset
= v_read(config
, &buf
->offset
);
1485 cons_offset
= atomic_long_read(&buf
->consumed
);
1486 if (write_offset
!= cons_offset
)
1488 "ring buffer %s, cpu %d: "
1489 "non-consumed data\n"
1490 " [ %lu bytes written, %lu bytes read ]\n",
1491 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1493 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1494 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1497 cons_offset
= subbuf_align(cons_offset
, chan
))
1498 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1502 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1504 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1505 struct lib_ring_buffer
*buf
,
1508 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1510 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1511 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1512 "%lu records overrun\n",
1514 v_read(config
, &buf
->records_count
),
1515 v_read(config
, &buf
->records_overrun
));
1517 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1518 "%lu records overrun\n",
1519 chan
->backend
.name
, cpu
,
1520 v_read(config
, &buf
->records_count
),
1521 v_read(config
, &buf
->records_overrun
));
1526 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1527 struct lib_ring_buffer
*buf
,
1534 void lib_ring_buffer_print_errors(struct channel
*chan
,
1535 struct lib_ring_buffer
*buf
, int cpu
)
1537 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1538 void *priv
= chan
->backend
.priv
;
1540 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1541 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1542 if (v_read(config
, &buf
->records_lost_full
)
1543 || v_read(config
, &buf
->records_lost_wrap
)
1544 || v_read(config
, &buf
->records_lost_big
))
1546 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1547 " [ %lu buffer full, %lu nest buffer wrap-around, "
1548 "%lu event too big ]\n",
1549 chan
->backend
.name
, cpu
,
1550 v_read(config
, &buf
->records_lost_full
),
1551 v_read(config
, &buf
->records_lost_wrap
),
1552 v_read(config
, &buf
->records_lost_big
));
1554 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1558 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1560 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1563 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1564 struct channel
*chan
,
1565 struct switch_offsets
*offsets
,
1568 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1569 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1570 unsigned long commit_count
;
1571 struct commit_counters_hot
*cc_hot
;
1573 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1576 * Order all writes to buffer before the commit count update that will
1577 * determine that the subbuffer is full.
1579 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1581 * Must write slot data before incrementing commit count. This
1582 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1588 cc_hot
= &buf
->commit_hot
[oldidx
];
1589 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1590 commit_count
= v_read(config
, &cc_hot
->cc
);
1591 /* Check if the written buffer has to be delivered */
1592 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1593 commit_count
, oldidx
, tsc
);
1594 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1595 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1596 commit_count
, cc_hot
);
1600 * lib_ring_buffer_switch_old_end: switch old subbuffer
1602 * Note : offset_old should never be 0 here. It is ok, because we never perform
1603 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1604 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1608 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1609 struct channel
*chan
,
1610 struct switch_offsets
*offsets
,
1613 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1614 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1615 unsigned long commit_count
, padding_size
, data_size
;
1616 struct commit_counters_hot
*cc_hot
;
1619 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1620 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1621 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1623 ts_end
= &buf
->ts_end
[oldidx
];
1625 * This is the last space reservation in that sub-buffer before
1626 * it gets delivered. This provides exclusive access to write to
1627 * this sub-buffer's ts_end. There are also no concurrent
1628 * readers of that ts_end because delivery of that sub-buffer is
1629 * postponed until the commit counter is incremented for the
1630 * current space reservation.
1635 * Order all writes to buffer and store to ts_end before the commit
1636 * count update that will determine that the subbuffer is full.
1638 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1640 * Must write slot data before incrementing commit count. This
1641 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1647 cc_hot
= &buf
->commit_hot
[oldidx
];
1648 v_add(config
, padding_size
, &cc_hot
->cc
);
1649 commit_count
= v_read(config
, &cc_hot
->cc
);
1650 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1651 commit_count
, oldidx
, tsc
);
1652 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1653 offsets
->old
+ padding_size
, commit_count
,
1658 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1660 * This code can be executed unordered : writers may already have written to the
1661 * sub-buffer before this code gets executed, caution. The commit makes sure
1662 * that this code is executed before the deliver of this sub-buffer.
1665 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1666 struct channel
*chan
,
1667 struct switch_offsets
*offsets
,
1670 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1671 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1672 unsigned long commit_count
;
1673 struct commit_counters_hot
*cc_hot
;
1675 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1678 * Order all writes to buffer before the commit count update that will
1679 * determine that the subbuffer is full.
1681 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1683 * Must write slot data before incrementing commit count. This
1684 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1690 cc_hot
= &buf
->commit_hot
[beginidx
];
1691 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1692 commit_count
= v_read(config
, &cc_hot
->cc
);
1693 /* Check if the written buffer has to be delivered */
1694 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1695 commit_count
, beginidx
, tsc
);
1696 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1697 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1698 commit_count
, cc_hot
);
1702 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1704 * Calls subbuffer_set_data_size() to set the data size of the current
1705 * sub-buffer. We do not need to perform check_deliver nor commit here,
1706 * since this task will be done by the "commit" of the event for which
1707 * we are currently doing the space reservation.
1710 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1711 struct channel
*chan
,
1712 struct switch_offsets
*offsets
,
1715 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1716 unsigned long endidx
, data_size
;
1719 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1720 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1721 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1722 ts_end
= &buf
->ts_end
[endidx
];
1724 * This is the last space reservation in that sub-buffer before
1725 * it gets delivered. This provides exclusive access to write to
1726 * this sub-buffer's ts_end. There are also no concurrent
1727 * readers of that ts_end because delivery of that sub-buffer is
1728 * postponed until the commit counter is incremented for the
1729 * current space reservation.
1737 * !0 if execution must be aborted.
1740 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1741 struct lib_ring_buffer
*buf
,
1742 struct channel
*chan
,
1743 struct switch_offsets
*offsets
,
1746 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1747 unsigned long off
, reserve_commit_diff
;
1749 offsets
->begin
= v_read(config
, &buf
->offset
);
1750 offsets
->old
= offsets
->begin
;
1751 offsets
->switch_old_start
= 0;
1752 off
= subbuf_offset(offsets
->begin
, chan
);
1754 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1757 * Ensure we flush the header of an empty subbuffer when doing the
1758 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1759 * total data gathering duration even if there were no records saved
1760 * after the last buffer switch.
1761 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1762 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1763 * subbuffer header as appropriate.
1764 * The next record that reserves space will be responsible for
1765 * populating the following subbuffer header. We choose not to populate
1766 * the next subbuffer header here because we want to be able to use
1767 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1768 * buffer flush, which must guarantee that all the buffer content
1769 * (records and header timestamps) are visible to the reader. This is
1770 * required for quiescence guarantees for the fusion merge.
1772 if (mode
!= SWITCH_FLUSH
&& !off
)
1773 return -1; /* we do not have to switch : buffer is empty */
1775 if (unlikely(off
== 0)) {
1776 unsigned long sb_index
, commit_count
;
1779 * We are performing a SWITCH_FLUSH. At this stage, there are no
1780 * concurrent writes into the buffer.
1782 * The client does not save any header information. Don't
1783 * switch empty subbuffer on finalize, because it is invalid to
1784 * deliver a completely empty subbuffer.
1786 if (!config
->cb
.subbuffer_header_size())
1789 /* Test new buffer integrity */
1790 sb_index
= subbuf_index(offsets
->begin
, chan
);
1791 commit_count
= v_read(config
,
1792 &buf
->commit_cold
[sb_index
].cc_sb
);
1793 reserve_commit_diff
=
1794 (buf_trunc(offsets
->begin
, chan
)
1795 >> chan
->backend
.num_subbuf_order
)
1796 - (commit_count
& chan
->commit_count_mask
);
1797 if (likely(reserve_commit_diff
== 0)) {
1798 /* Next subbuffer not being written to. */
1799 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1800 subbuf_trunc(offsets
->begin
, chan
)
1801 - subbuf_trunc((unsigned long)
1802 atomic_long_read(&buf
->consumed
), chan
)
1803 >= chan
->backend
.buf_size
)) {
1805 * We do not overwrite non consumed buffers
1806 * and we are full : don't switch.
1811 * Next subbuffer not being written to, and we
1812 * are either in overwrite mode or the buffer is
1813 * not full. It's safe to write in this new
1819 * Next subbuffer reserve offset does not match the
1820 * commit offset. Don't perform switch in
1821 * producer-consumer and overwrite mode. Caused by
1822 * either a writer OOPS or too many nested writes over a
1823 * reserve/commit pair.
1829 * Need to write the subbuffer start header on finalize.
1831 offsets
->switch_old_start
= 1;
1833 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1834 /* Note: old points to the next subbuf at offset 0 */
1835 offsets
->end
= offsets
->begin
;
1840 * Force a sub-buffer switch. This operation is completely reentrant : can be
1841 * called while tracing is active with absolutely no lock held.
1843 * Note, however, that as a v_cmpxchg is used for some atomic
1844 * operations, this function must be called from the CPU which owns the buffer
1845 * for a ACTIVE flush.
1847 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1849 struct channel
*chan
= buf
->backend
.chan
;
1850 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1851 struct switch_offsets offsets
;
1852 unsigned long oldidx
;
1858 * Perform retryable operations.
1861 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1863 return; /* Switch not needed */
1864 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1868 * Atomically update last_tsc. This update races against concurrent
1869 * atomic updates, but the race will always cause supplementary full TSC
1870 * records, never the opposite (missing a full TSC record when it would
1873 save_last_tsc(config
, buf
, tsc
);
1876 * Push the reader if necessary
1878 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1880 oldidx
= subbuf_index(offsets
.old
, chan
);
1881 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1884 * May need to populate header start on SWITCH_FLUSH.
1886 if (offsets
.switch_old_start
) {
1887 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1888 offsets
.old
+= config
->cb
.subbuffer_header_size();
1892 * Switch old subbuffer.
1894 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1896 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1898 struct switch_param
{
1899 struct lib_ring_buffer
*buf
;
1900 enum switch_mode mode
;
1903 static void remote_switch(void *info
)
1905 struct switch_param
*param
= info
;
1906 struct lib_ring_buffer
*buf
= param
->buf
;
1908 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1911 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1912 enum switch_mode mode
)
1914 struct channel
*chan
= buf
->backend
.chan
;
1915 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1917 struct switch_param param
;
1920 * With global synchronization we don't need to use the IPI scheme.
1922 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1923 lib_ring_buffer_switch_slow(buf
, mode
);
1928 * Disabling preemption ensures two things: first, that the
1929 * target cpu is not taken concurrently offline while we are within
1930 * smp_call_function_single(). Secondly, if it happens that the
1931 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1932 * needs to be protected from CPU hotplug handlers, which can
1933 * also perform a remote subbuffer switch.
1938 ret
= smp_call_function_single(buf
->backend
.cpu
,
1939 remote_switch
, ¶m
, 1);
1941 /* Remote CPU is offline, do it ourself. */
1942 lib_ring_buffer_switch_slow(buf
, mode
);
1947 /* Switch sub-buffer if current sub-buffer is non-empty. */
1948 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1950 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1952 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1954 /* Switch sub-buffer even if current sub-buffer is empty. */
1955 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1957 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1959 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1961 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
)
1963 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1964 struct channel
*chan
= bufb
->chan
;
1966 lib_ring_buffer_switch_remote(buf
);
1967 lib_ring_buffer_clear_reader(buf
, chan
);
1969 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
1974 * -ENOSPC if event size is too large for packet.
1975 * -ENOBUFS if there is currently not enough space in buffer for the event.
1976 * -EIO if data cannot be written into the buffer for any other reason.
1979 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1980 struct channel
*chan
,
1981 struct switch_offsets
*offsets
,
1982 struct lib_ring_buffer_ctx
*ctx
,
1985 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1986 unsigned long reserve_commit_diff
, offset_cmp
;
1989 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1990 offsets
->old
= offsets
->begin
;
1991 offsets
->switch_new_start
= 0;
1992 offsets
->switch_new_end
= 0;
1993 offsets
->switch_old_end
= 0;
1994 offsets
->pre_header_padding
= 0;
1996 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1997 if ((int64_t) ctx
->tsc
== -EIO
)
2000 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2001 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2003 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2004 offsets
->switch_new_start
= 1; /* For offsets->begin */
2006 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2008 &offsets
->pre_header_padding
,
2011 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2014 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2015 offsets
->size
> chan
->backend
.subbuf_size
)) {
2016 offsets
->switch_old_end
= 1; /* For offsets->old */
2017 offsets
->switch_new_start
= 1; /* For offsets->begin */
2020 if (unlikely(offsets
->switch_new_start
)) {
2021 unsigned long sb_index
, commit_count
;
2024 * We are typically not filling the previous buffer completely.
2026 if (likely(offsets
->switch_old_end
))
2027 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2028 offsets
->begin
= offsets
->begin
2029 + config
->cb
.subbuffer_header_size();
2030 /* Test new buffer integrity */
2031 sb_index
= subbuf_index(offsets
->begin
, chan
);
2033 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2034 * lib_ring_buffer_check_deliver() has the matching
2035 * memory barriers required around commit_cold cc_sb
2036 * updates to ensure reserve and commit counter updates
2037 * are not seen reordered when updated by another CPU.
2040 commit_count
= v_read(config
,
2041 &buf
->commit_cold
[sb_index
].cc_sb
);
2042 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2044 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2046 * The reserve counter have been concurrently updated
2047 * while we read the commit counter. This means the
2048 * commit counter we read might not match buf->offset
2049 * due to concurrent update. We therefore need to retry.
2053 reserve_commit_diff
=
2054 (buf_trunc(offsets
->begin
, chan
)
2055 >> chan
->backend
.num_subbuf_order
)
2056 - (commit_count
& chan
->commit_count_mask
);
2057 if (likely(reserve_commit_diff
== 0)) {
2058 /* Next subbuffer not being written to. */
2059 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2060 subbuf_trunc(offsets
->begin
, chan
)
2061 - subbuf_trunc((unsigned long)
2062 atomic_long_read(&buf
->consumed
), chan
)
2063 >= chan
->backend
.buf_size
)) {
2065 * We do not overwrite non consumed buffers
2066 * and we are full : record is lost.
2068 v_inc(config
, &buf
->records_lost_full
);
2072 * Next subbuffer not being written to, and we
2073 * are either in overwrite mode or the buffer is
2074 * not full. It's safe to write in this new
2080 * Next subbuffer reserve offset does not match the
2081 * commit offset, and this did not involve update to the
2082 * reserve counter. Drop record in producer-consumer and
2083 * overwrite mode. Caused by either a writer OOPS or
2084 * too many nested writes over a reserve/commit pair.
2086 v_inc(config
, &buf
->records_lost_wrap
);
2090 config
->cb
.record_header_size(config
, chan
,
2092 &offsets
->pre_header_padding
,
2095 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2098 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2099 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2101 * Record too big for subbuffers, report error, don't
2102 * complete the sub-buffer switch.
2104 v_inc(config
, &buf
->records_lost_big
);
2108 * We just made a successful buffer switch and the
2109 * record fits in the new subbuffer. Let's write.
2114 * Record fits in the current buffer and we are not on a switch
2115 * boundary. It's safe to write.
2118 offsets
->end
= offsets
->begin
+ offsets
->size
;
2120 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2122 * The offset_end will fall at the very beginning of the next
2125 offsets
->switch_new_end
= 1; /* For offsets->begin */
2130 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2132 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2134 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2135 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2137 return chan
->backend
.buf
;
2140 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2142 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2143 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2145 v_inc(config
, &buf
->records_lost_big
);
2147 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2150 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2151 * @ctx: ring buffer context.
2153 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2154 * -EIO for other errors, else returns 0.
2155 * It will take care of sub-buffer switching.
2157 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
,
2160 struct channel
*chan
= ctx
->chan
;
2161 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2162 struct lib_ring_buffer
*buf
;
2163 struct switch_offsets offsets
;
2166 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2170 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2174 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2179 * Atomically update last_tsc. This update races against concurrent
2180 * atomic updates, but the race will always cause supplementary full TSC
2181 * records, never the opposite (missing a full TSC record when it would
2184 save_last_tsc(config
, buf
, ctx
->tsc
);
2187 * Push the reader if necessary
2189 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2192 * Clear noref flag for this subbuffer.
2194 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2195 subbuf_index(offsets
.end
- 1, chan
));
2198 * Switch old subbuffer if needed.
2200 if (unlikely(offsets
.switch_old_end
)) {
2201 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2202 subbuf_index(offsets
.old
- 1, chan
));
2203 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2207 * Populate new subbuffer.
2209 if (unlikely(offsets
.switch_new_start
))
2210 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2212 if (unlikely(offsets
.switch_new_end
))
2213 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2215 ctx
->slot_size
= offsets
.size
;
2216 ctx
->pre_offset
= offsets
.begin
;
2217 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2220 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2223 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2224 struct lib_ring_buffer
*buf
,
2225 unsigned long commit_count
,
2228 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2229 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2233 * The ring buffer can count events recorded and overwritten per buffer,
2234 * but it is disabled by default due to its performance overhead.
2236 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2238 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2239 struct lib_ring_buffer
*buf
,
2242 v_add(config
, subbuffer_get_records_count(config
,
2243 &buf
->backend
, idx
),
2244 &buf
->records_count
);
2245 v_add(config
, subbuffer_count_records_overrun(config
,
2246 &buf
->backend
, idx
),
2247 &buf
->records_overrun
);
2249 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2251 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2252 struct lib_ring_buffer
*buf
,
2256 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2259 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2260 struct lib_ring_buffer
*buf
,
2261 struct channel
*chan
,
2262 unsigned long offset
,
2263 unsigned long commit_count
,
2267 unsigned long old_commit_count
= commit_count
2268 - chan
->backend
.subbuf_size
;
2271 * If we succeeded at updating cc_sb below, we are the subbuffer
2272 * writer delivering the subbuffer. Deals with concurrent
2273 * updates of the "cc" value without adding a add_return atomic
2274 * operation to the fast path.
2276 * We are doing the delivery in two steps:
2277 * - First, we cmpxchg() cc_sb to the new value
2278 * old_commit_count + 1. This ensures that we are the only
2279 * subbuffer user successfully filling the subbuffer, but we
2280 * do _not_ set the cc_sb value to "commit_count" yet.
2281 * Therefore, other writers that would wrap around the ring
2282 * buffer and try to start writing to our subbuffer would
2283 * have to drop records, because it would appear as
2285 * We therefore have exclusive access to the subbuffer control
2286 * structures. This mutual exclusion with other writers is
2287 * crucially important to perform record overruns count in
2288 * flight recorder mode locklessly.
2289 * - When we are ready to release the subbuffer (either for
2290 * reading or for overrun by other writers), we simply set the
2291 * cc_sb value to "commit_count" and perform delivery.
2293 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2294 * This guarantees that old_commit_count + 1 != commit_count.
2298 * Order prior updates to reserve count prior to the
2299 * commit_cold cc_sb update.
2302 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2303 old_commit_count
, old_commit_count
+ 1)
2304 == old_commit_count
)) {
2308 * Start of exclusive subbuffer access. We are
2309 * guaranteed to be the last writer in this subbuffer
2310 * and any other writer trying to access this subbuffer
2311 * in this state is required to drop records.
2313 * We can read the ts_end for the current sub-buffer
2314 * which has been saved by the very last space
2315 * reservation for the current sub-buffer.
2317 * Order increment of commit counter before reading ts_end.
2320 ts_end
= &buf
->ts_end
[idx
];
2321 deliver_count_events(config
, buf
, idx
);
2322 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2323 lib_ring_buffer_get_data_size(config
,
2328 * Increment the packet counter while we have exclusive
2331 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2334 * Set noref flag and offset for this subbuffer id.
2335 * Contains a memory barrier that ensures counter stores
2336 * are ordered before set noref and offset.
2338 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2339 buf_trunc_val(offset
, chan
));
2342 * Order set_noref and record counter updates before the
2343 * end of subbuffer exclusive access. Orders with
2344 * respect to writers coming into the subbuffer after
2345 * wrap around, and also order wrt concurrent readers.
2348 /* End of exclusive subbuffer access */
2349 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2352 * Order later updates to reserve count after
2353 * the commit_cold cc_sb update.
2356 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2360 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2362 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2363 && atomic_long_read(&buf
->active_readers
)
2364 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2365 wake_up_interruptible(&buf
->read_wait
);
2366 wake_up_interruptible(&chan
->read_wait
);
2371 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2373 int __init
init_lib_ring_buffer_frontend(void)
2377 for_each_possible_cpu(cpu
)
2378 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2382 module_init(init_lib_ring_buffer_frontend
);
2384 void __exit
exit_lib_ring_buffer_frontend(void)
2388 module_exit(exit_lib_ring_buffer_frontend
);