1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
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 <linux/kref.h>
44 #include <linux/percpu-defs.h>
45 #include <linux/timer.h>
46 #include <asm/cacheflush.h>
48 #include <include/ringbuffer/config.h>
49 #include <include/ringbuffer/backend.h>
50 #include <include/ringbuffer/frontend.h>
51 #include <include/ringbuffer/iterator.h>
52 #include <include/ringbuffer/nohz.h>
55 * Internal structure representing offsets to use at a sub-buffer switch.
57 struct switch_offsets
{
58 unsigned long begin
, end
, old
;
59 size_t pre_header_padding
, size
;
60 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
71 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
72 #endif /* CONFIG_NO_HZ */
74 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
76 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
77 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
80 void lib_ring_buffer_print_errors(struct channel
*chan
,
81 struct lib_ring_buffer
*buf
, int cpu
);
83 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
84 enum switch_mode mode
);
87 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
88 struct lib_ring_buffer
*buf
,
91 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
93 consumed_old
= atomic_long_read(&buf
->consumed
);
94 consumed_idx
= subbuf_index(consumed_old
, chan
);
95 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
97 * No memory barrier here, since we are only interested
98 * in a statistically correct polling result. The next poll will
99 * get the data is we are racing. The mb() that ensures correct
100 * memory order is in get_subbuf.
102 write_offset
= v_read(config
, &buf
->offset
);
105 * Check that the subbuffer we are trying to consume has been
106 * already fully committed.
109 if (((commit_count
- chan
->backend
.subbuf_size
)
110 & chan
->commit_count_mask
)
111 - (buf_trunc(consumed_old
, chan
)
112 >> chan
->backend
.num_subbuf_order
)
117 * Check that we are not about to read the same subbuffer in
118 * which the writer head is.
120 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
128 * Must be called under cpu hotplug protection.
130 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
132 struct channel
*chan
= buf
->backend
.chan
;
134 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
135 kvfree(buf
->commit_hot
);
136 kvfree(buf
->commit_cold
);
139 lib_ring_buffer_backend_free(&buf
->backend
);
143 * lib_ring_buffer_reset - Reset ring buffer to initial values.
146 * Effectively empty the ring buffer. Should be called when the buffer is not
147 * used for writing. The ring buffer can be opened for reading, but the reader
148 * should not be using the iterator concurrently with reset. The previous
149 * current iterator record is reset.
151 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
153 struct channel
*chan
= buf
->backend
.chan
;
154 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
158 * Reset iterator first. It will put the subbuffer if it currently holds
161 lib_ring_buffer_iterator_reset(buf
);
162 v_set(config
, &buf
->offset
, 0);
163 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
164 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
165 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
166 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
169 atomic_long_set(&buf
->consumed
, 0);
170 atomic_set(&buf
->record_disabled
, 0);
171 v_set(config
, &buf
->last_tsc
, 0);
172 lib_ring_buffer_backend_reset(&buf
->backend
);
173 /* Don't reset number of active readers */
174 v_set(config
, &buf
->records_lost_full
, 0);
175 v_set(config
, &buf
->records_lost_wrap
, 0);
176 v_set(config
, &buf
->records_lost_big
, 0);
177 v_set(config
, &buf
->records_count
, 0);
178 v_set(config
, &buf
->records_overrun
, 0);
181 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
184 * channel_reset - Reset channel to initial values.
187 * Effectively empty the channel. Should be called when the channel is not used
188 * for writing. The channel can be opened for reading, but the reader should not
189 * be using the iterator concurrently with reset. The previous current iterator
192 void channel_reset(struct channel
*chan
)
195 * Reset iterators first. Will put the subbuffer if held for reading.
197 channel_iterator_reset(chan
);
198 atomic_set(&chan
->record_disabled
, 0);
199 /* Don't reset commit_count_mask, still valid */
200 channel_backend_reset(&chan
->backend
);
201 /* Don't reset switch/read timer interval */
202 /* Don't reset notifiers and notifier enable bits */
203 /* Don't reset reader reference count */
205 EXPORT_SYMBOL_GPL(channel_reset
);
208 * Must be called under cpu hotplug protection.
210 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
211 struct channel_backend
*chanb
, int cpu
)
213 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
214 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
215 void *priv
= chanb
->priv
;
216 size_t subbuf_header_size
;
220 /* Test for cpu hotplug */
221 if (buf
->backend
.allocated
)
225 * Paranoia: per cpu dynamic allocation is not officially documented as
226 * zeroing the memory, so let's do it here too, just in case.
228 memset(buf
, 0, sizeof(*buf
));
230 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
235 kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
236 * chan
->backend
.num_subbuf
,
237 1 << INTERNODE_CACHE_SHIFT
),
238 GFP_KERNEL
| __GFP_NOWARN
,
239 cpu_to_node(max(cpu
, 0)));
240 if (!buf
->commit_hot
) {
246 kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
247 * chan
->backend
.num_subbuf
,
248 1 << INTERNODE_CACHE_SHIFT
),
249 GFP_KERNEL
| __GFP_NOWARN
,
250 cpu_to_node(max(cpu
, 0)));
251 if (!buf
->commit_cold
) {
257 kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
258 * chan
->backend
.num_subbuf
,
259 1 << INTERNODE_CACHE_SHIFT
),
260 GFP_KERNEL
| __GFP_NOWARN
,
261 cpu_to_node(max(cpu
, 0)));
264 goto free_commit_cold
;
267 init_waitqueue_head(&buf
->read_wait
);
268 init_waitqueue_head(&buf
->write_wait
);
269 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
272 * Write the subbuffer header for first subbuffer so we know the total
273 * duration of data gathering.
275 subbuf_header_size
= config
->cb
.subbuffer_header_size();
276 v_set(config
, &buf
->offset
, subbuf_header_size
);
277 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
278 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
279 config
->cb
.buffer_begin(buf
, tsc
, 0);
280 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
282 if (config
->cb
.buffer_create
) {
283 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
289 * Ensure the buffer is ready before setting it to allocated and setting
291 * Used for cpu hotplug vs cpumask iteration.
294 buf
->backend
.allocated
= 1;
296 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
297 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
298 chan
->backend
.cpumask
));
299 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
307 kvfree(buf
->commit_cold
);
309 kvfree(buf
->commit_hot
);
311 lib_ring_buffer_backend_free(&buf
->backend
);
315 static void switch_buffer_timer(struct timer_list
*t
)
317 struct lib_ring_buffer
*buf
= from_timer(buf
, t
, switch_timer
);
318 struct channel
*chan
= buf
->backend
.chan
;
321 * Only flush buffers periodically if readers are active.
323 if (atomic_long_read(&buf
->active_readers
))
324 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
326 mod_timer(&buf
->switch_timer
,
327 jiffies
+ chan
->switch_timer_interval
);
331 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
333 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
335 struct channel
*chan
= buf
->backend
.chan
;
336 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
337 unsigned int flags
= 0;
339 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
342 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
343 flags
= TIMER_PINNED
;
345 timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
);
346 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
348 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
349 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
351 add_timer(&buf
->switch_timer
);
353 buf
->switch_timer_enabled
= 1;
357 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
359 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
361 struct channel
*chan
= buf
->backend
.chan
;
363 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
366 del_timer_sync(&buf
->switch_timer
);
367 buf
->switch_timer_enabled
= 0;
371 * Polling timer to check the channels for data.
373 static void read_buffer_timer(struct timer_list
*t
)
375 struct lib_ring_buffer
*buf
= from_timer(buf
, t
, read_timer
);
376 struct channel
*chan
= buf
->backend
.chan
;
377 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
379 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
381 if (atomic_long_read(&buf
->active_readers
)
382 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
383 wake_up_interruptible(&buf
->read_wait
);
384 wake_up_interruptible(&chan
->read_wait
);
387 mod_timer(&buf
->read_timer
,
388 jiffies
+ chan
->read_timer_interval
);
392 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
394 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
396 struct channel
*chan
= buf
->backend
.chan
;
397 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
398 unsigned int flags
= 0;
400 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
401 || !chan
->read_timer_interval
402 || buf
->read_timer_enabled
)
405 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
406 flags
= TIMER_PINNED
;
408 timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
);
409 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
411 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
412 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
414 add_timer(&buf
->read_timer
);
416 buf
->read_timer_enabled
= 1;
420 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
422 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
424 struct channel
*chan
= buf
->backend
.chan
;
425 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
427 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
428 || !chan
->read_timer_interval
429 || !buf
->read_timer_enabled
)
432 del_timer_sync(&buf
->read_timer
);
434 * do one more check to catch data that has been written in the last
437 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
438 wake_up_interruptible(&buf
->read_wait
);
439 wake_up_interruptible(&chan
->read_wait
);
441 buf
->read_timer_enabled
= 0;
444 enum cpuhp_state lttng_rb_hp_prepare
;
445 enum cpuhp_state lttng_rb_hp_online
;
447 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
449 lttng_rb_hp_prepare
= val
;
451 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
453 void lttng_rb_set_hp_online(enum cpuhp_state val
)
455 lttng_rb_hp_online
= val
;
457 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
459 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
460 struct lttng_cpuhp_node
*node
)
462 struct channel
*chan
= container_of(node
, struct channel
,
464 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
465 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
467 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
470 * Performing a buffer switch on a remote CPU. Performed by
471 * the CPU responsible for doing the hotunplug after the target
472 * CPU stopped running completely. Ensures that all data
473 * from that remote CPU is flushed.
475 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
478 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
480 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
481 struct lttng_cpuhp_node
*node
)
483 struct channel
*chan
= container_of(node
, struct channel
,
485 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
486 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
488 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
490 wake_up_interruptible(&chan
->hp_wait
);
491 lib_ring_buffer_start_switch_timer(buf
);
492 lib_ring_buffer_start_read_timer(buf
);
495 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
497 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
498 struct lttng_cpuhp_node
*node
)
500 struct channel
*chan
= container_of(node
, struct channel
,
502 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
503 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
505 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
507 lib_ring_buffer_stop_switch_timer(buf
);
508 lib_ring_buffer_stop_read_timer(buf
);
511 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
513 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
515 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
516 * that wake-up-tracing generated events are flushed before going idle (in
517 * tick_nohz). We test if the spinlock is locked to deal with the race where
518 * readers try to sample the ring buffer before we perform the switch. We let
519 * the readers retry in that case. If there is data in the buffer, the wake up
520 * is going to forbid the CPU running the reader thread from going idle.
522 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
526 struct channel
*chan
= container_of(nb
, struct channel
,
528 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
529 struct lib_ring_buffer
*buf
;
530 int cpu
= smp_processor_id();
532 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
534 * We don't support keeping the system idle with global buffers
535 * and streaming active. In order to do so, we would need to
536 * sample a non-nohz-cpumask racelessly with the nohz updates
537 * without adding synchronization overhead to nohz. Leave this
538 * use-case out for now.
543 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
545 case TICK_NOHZ_FLUSH
:
546 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
547 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
548 && chan
->read_timer_interval
549 && atomic_long_read(&buf
->active_readers
)
550 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
551 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
552 wake_up_interruptible(&buf
->read_wait
);
553 wake_up_interruptible(&chan
->read_wait
);
555 if (chan
->switch_timer_interval
)
556 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
557 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
560 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
561 lib_ring_buffer_stop_switch_timer(buf
);
562 lib_ring_buffer_stop_read_timer(buf
);
563 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
565 case TICK_NOHZ_RESTART
:
566 spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock
));
567 lib_ring_buffer_start_read_timer(buf
);
568 lib_ring_buffer_start_switch_timer(buf
);
569 spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock
));
576 void notrace
lib_ring_buffer_tick_nohz_flush(void)
578 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
582 void notrace
lib_ring_buffer_tick_nohz_stop(void)
584 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
588 void notrace
lib_ring_buffer_tick_nohz_restart(void)
590 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
593 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
598 static void channel_unregister_notifiers(struct channel
*chan
)
600 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
602 channel_iterator_unregister_notifiers(chan
);
603 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
606 * Remove the nohz notifier first, so we are certain we stop
609 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
610 &chan
->tick_nohz_notifier
);
612 * ring_buffer_nohz_lock will not be needed below, because
613 * we just removed the notifiers, which were the only source of
616 #endif /* CONFIG_NO_HZ */
620 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
621 &chan
->cpuhp_online
.node
);
623 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
624 &chan
->cpuhp_prepare
.node
);
628 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
630 lib_ring_buffer_stop_switch_timer(buf
);
631 lib_ring_buffer_stop_read_timer(buf
);
633 channel_backend_unregister_notifiers(&chan
->backend
);
636 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
638 if (!buf
->quiescent
) {
639 buf
->quiescent
= true;
640 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
644 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
646 buf
->quiescent
= false;
649 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
652 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
654 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
656 for_each_channel_cpu(cpu
, chan
) {
657 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
660 lib_ring_buffer_set_quiescent(buf
);
664 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
666 lib_ring_buffer_set_quiescent(buf
);
669 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
671 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
674 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
676 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
678 for_each_channel_cpu(cpu
, chan
) {
679 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
682 lib_ring_buffer_clear_quiescent(buf
);
686 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
688 lib_ring_buffer_clear_quiescent(buf
);
691 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
693 static void channel_free(struct channel
*chan
)
695 if (chan
->backend
.release_priv_ops
) {
696 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
698 channel_iterator_free(chan
);
699 channel_backend_free(&chan
->backend
);
704 * channel_create - Create channel.
705 * @config: ring buffer instance configuration
706 * @name: name of the channel
707 * @priv: ring buffer client private data
708 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
709 * address mapping. It is used only by RING_BUFFER_STATIC
710 * configuration. It can be set to NULL for other backends.
711 * @subbuf_size: subbuffer size
712 * @num_subbuf: number of subbuffers
713 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
714 * padding to let readers get those sub-buffers.
715 * Used for live streaming.
716 * @read_timer_interval: Time interval (in us) to wake up pending readers.
719 * Returns NULL on failure.
721 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
722 const char *name
, void *priv
, void *buf_addr
,
724 size_t num_subbuf
, unsigned int switch_timer_interval
,
725 unsigned int read_timer_interval
)
728 struct channel
*chan
;
730 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
731 read_timer_interval
))
734 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
738 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
739 subbuf_size
, num_subbuf
);
743 ret
= channel_iterator_init(chan
);
745 goto error_free_backend
;
747 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
748 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
749 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
750 kref_init(&chan
->ref
);
751 init_waitqueue_head(&chan
->read_wait
);
752 init_waitqueue_head(&chan
->hp_wait
);
754 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
755 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
756 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
757 &chan
->cpuhp_prepare
.node
);
759 goto cpuhp_prepare_error
;
761 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
762 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
763 &chan
->cpuhp_online
.node
);
765 goto cpuhp_online_error
;
767 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
768 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
769 chan
->tick_nohz_notifier
.notifier_call
=
770 ring_buffer_tick_nohz_callback
;
771 chan
->tick_nohz_notifier
.priority
= ~0U;
772 atomic_notifier_chain_register(&tick_nohz_notifier
,
773 &chan
->tick_nohz_notifier
);
774 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
777 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
779 lib_ring_buffer_start_switch_timer(buf
);
780 lib_ring_buffer_start_read_timer(buf
);
786 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
787 &chan
->cpuhp_prepare
.node
);
791 channel_backend_free(&chan
->backend
);
796 EXPORT_SYMBOL_GPL(channel_create
);
799 void channel_release(struct kref
*kref
)
801 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
806 * channel_destroy - Finalize, wait for q.s. and destroy channel.
807 * @chan: channel to destroy
810 * Call "destroy" callback, finalize channels, and then decrement the
811 * channel reference count. Note that when readers have completed data
812 * consumption of finalized channels, get_subbuf() will return -ENODATA.
813 * They should release their handle at that point. Returns the private
816 void *channel_destroy(struct channel
*chan
)
819 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
822 channel_unregister_notifiers(chan
);
824 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
826 * No need to hold cpu hotplug, because all notifiers have been
829 for_each_channel_cpu(cpu
, chan
) {
830 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
833 if (config
->cb
.buffer_finalize
)
834 config
->cb
.buffer_finalize(buf
,
838 * Perform flush before writing to finalized.
841 WRITE_ONCE(buf
->finalized
, 1);
842 wake_up_interruptible(&buf
->read_wait
);
845 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
847 if (config
->cb
.buffer_finalize
)
848 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
850 * Perform flush before writing to finalized.
853 WRITE_ONCE(buf
->finalized
, 1);
854 wake_up_interruptible(&buf
->read_wait
);
856 WRITE_ONCE(chan
->finalized
, 1);
857 wake_up_interruptible(&chan
->hp_wait
);
858 wake_up_interruptible(&chan
->read_wait
);
859 priv
= chan
->backend
.priv
;
860 kref_put(&chan
->ref
, channel_release
);
863 EXPORT_SYMBOL_GPL(channel_destroy
);
865 struct lib_ring_buffer
*channel_get_ring_buffer(
866 const struct lib_ring_buffer_config
*config
,
867 struct channel
*chan
, int cpu
)
869 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
870 return chan
->backend
.buf
;
872 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
874 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
876 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
878 struct channel
*chan
= buf
->backend
.chan
;
880 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
882 kref_get(&chan
->ref
);
883 smp_mb__after_atomic();
886 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
888 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
890 struct channel
*chan
= buf
->backend
.chan
;
892 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
893 smp_mb__before_atomic();
894 atomic_long_dec(&buf
->active_readers
);
895 kref_put(&chan
->ref
, channel_release
);
897 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
900 * Promote compiler barrier to a smp_mb().
901 * For the specific ring buffer case, this IPI call should be removed if the
902 * architecture does not reorder writes. This should eventually be provided by
903 * a separate architecture-specific infrastructure.
905 static void remote_mb(void *info
)
911 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
913 * @consumed: consumed count indicating the position where to read
914 * @produced: produced count, indicates position when to stop reading
916 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
917 * data to read at consumed position, or 0 if the get operation succeeds.
918 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
921 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
922 unsigned long *consumed
, unsigned long *produced
)
924 struct channel
*chan
= buf
->backend
.chan
;
925 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
926 unsigned long consumed_cur
, write_offset
;
930 finalized
= READ_ONCE(buf
->finalized
);
932 * Read finalized before counters.
935 consumed_cur
= atomic_long_read(&buf
->consumed
);
937 * No need to issue a memory barrier between consumed count read and
938 * write offset read, because consumed count can only change
939 * concurrently in overwrite mode, and we keep a sequence counter
940 * identifier derived from the write offset to check we are getting
941 * the same sub-buffer we are expecting (the sub-buffers are atomically
942 * "tagged" upon writes, tags are checked upon read).
944 write_offset
= v_read(config
, &buf
->offset
);
947 * Check that we are not about to read the same subbuffer in
948 * which the writer head is.
950 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
954 *consumed
= consumed_cur
;
955 *produced
= subbuf_trunc(write_offset
, chan
);
961 * The memory barriers __wait_event()/wake_up_interruptible() take care
962 * of "raw_spin_is_locked" memory ordering.
966 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
971 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
974 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
975 * are saved regardless of whether the consumed and produced positions are
976 * in the same subbuffer.
978 * @consumed: consumed byte count indicating the last position read
979 * @produced: produced byte count indicating the last position written
981 * This function is meant to provide information on the exact producer and
982 * consumer positions without regard for the "snapshot" feature.
984 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
985 unsigned long *consumed
, unsigned long *produced
)
987 struct channel
*chan
= buf
->backend
.chan
;
988 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
991 *consumed
= atomic_long_read(&buf
->consumed
);
993 * No need to issue a memory barrier between consumed count read and
994 * write offset read, because consumed count can only change
995 * concurrently in overwrite mode, and we keep a sequence counter
996 * identifier derived from the write offset to check we are getting
997 * the same sub-buffer we are expecting (the sub-buffers are atomically
998 * "tagged" upon writes, tags are checked upon read).
1000 *produced
= v_read(config
, &buf
->offset
);
1005 * lib_ring_buffer_put_snapshot - move consumed counter forward
1007 * Should only be called from consumer context.
1009 * @consumed_new: new consumed count value
1011 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1012 unsigned long consumed_new
)
1014 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1015 struct channel
*chan
= bufb
->chan
;
1016 unsigned long consumed
;
1018 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1021 * Only push the consumed value forward.
1022 * If the consumed cmpxchg fails, this is because we have been pushed by
1023 * the writer in flight recorder mode.
1025 consumed
= atomic_long_read(&buf
->consumed
);
1026 while ((long) consumed
- (long) consumed_new
< 0)
1027 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1029 /* Wake-up the metadata producer */
1030 wake_up_interruptible(&buf
->write_wait
);
1032 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1034 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1035 static void lib_ring_buffer_flush_read_subbuf_dcache(
1036 const struct lib_ring_buffer_config
*config
,
1037 struct channel
*chan
,
1038 struct lib_ring_buffer
*buf
)
1040 struct lib_ring_buffer_backend_pages
*pages
;
1041 unsigned long sb_bindex
, id
, i
, nr_pages
;
1043 if (config
->output
!= RING_BUFFER_MMAP
)
1047 * Architectures with caches aliased on virtual addresses may
1048 * use different cache lines for the linear mapping vs
1049 * user-space memory mapping. Given that the ring buffer is
1050 * based on the kernel linear mapping, aligning it with the
1051 * user-space mapping is not straightforward, and would require
1052 * extra TLB entries. Therefore, simply flush the dcache for the
1053 * entire sub-buffer before reading it.
1055 id
= buf
->backend
.buf_rsb
.id
;
1056 sb_bindex
= subbuffer_id_get_index(config
, id
);
1057 pages
= buf
->backend
.array
[sb_bindex
];
1058 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1059 for (i
= 0; i
< nr_pages
; i
++) {
1060 struct lib_ring_buffer_backend_page
*backend_page
;
1062 backend_page
= &pages
->p
[i
];
1063 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1067 static void lib_ring_buffer_flush_read_subbuf_dcache(
1068 const struct lib_ring_buffer_config
*config
,
1069 struct channel
*chan
,
1070 struct lib_ring_buffer
*buf
)
1076 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1078 * @consumed: consumed count indicating the position where to read
1080 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1081 * data to read at consumed position, or 0 if the get operation succeeds.
1082 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1084 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1085 unsigned long consumed
)
1087 struct channel
*chan
= buf
->backend
.chan
;
1088 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1089 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1093 if (buf
->get_subbuf
) {
1095 * Reader is trying to get a subbuffer twice.
1097 CHAN_WARN_ON(chan
, 1);
1101 finalized
= READ_ONCE(buf
->finalized
);
1103 * Read finalized before counters.
1106 consumed_cur
= atomic_long_read(&buf
->consumed
);
1107 consumed_idx
= subbuf_index(consumed
, chan
);
1108 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1110 * Make sure we read the commit count before reading the buffer
1111 * data and the write offset. Correct consumed offset ordering
1112 * wrt commit count is insured by the use of cmpxchg to update
1113 * the consumed offset.
1114 * smp_call_function_single can fail if the remote CPU is offline,
1115 * this is OK because then there is no wmb to execute there.
1116 * If our thread is executing on the same CPU as the on the buffers
1117 * belongs to, we don't have to synchronize it at all. If we are
1118 * migrated, the scheduler will take care of the memory barriers.
1119 * Normally, smp_call_function_single() should ensure program order when
1120 * executing the remote function, which implies that it surrounds the
1121 * function execution with :
1132 * However, smp_call_function_single() does not seem to clearly execute
1133 * such barriers. It depends on spinlock semantic to provide the barrier
1134 * before executing the IPI and, when busy-looping, csd_lock_wait only
1135 * executes smp_mb() when it has to wait for the other CPU.
1137 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1138 * required ourself, even if duplicated. It has no performance impact
1141 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1142 * read and write vs write. They do not ensure core synchronization. We
1143 * really have to ensure total order between the 3 barriers running on
1146 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1147 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1148 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1149 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1150 /* Total order with IPI handler smp_mb() */
1152 smp_call_function_single(buf
->backend
.cpu
,
1153 remote_mb
, NULL
, 1);
1154 /* Total order with IPI handler smp_mb() */
1158 /* Total order with IPI handler smp_mb() */
1160 smp_call_function(remote_mb
, NULL
, 1);
1161 /* Total order with IPI handler smp_mb() */
1166 * Local rmb to match the remote wmb to read the commit count
1167 * before the buffer data and the write offset.
1172 write_offset
= v_read(config
, &buf
->offset
);
1175 * Check that the buffer we are getting is after or at consumed_cur
1178 if ((long) subbuf_trunc(consumed
, chan
)
1179 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1183 * Check that the subbuffer we are trying to consume has been
1184 * already fully committed.
1186 if (((commit_count
- chan
->backend
.subbuf_size
)
1187 & chan
->commit_count_mask
)
1188 - (buf_trunc(consumed
, chan
)
1189 >> chan
->backend
.num_subbuf_order
)
1194 * Check that we are not about to read the same subbuffer in
1195 * which the writer head is.
1197 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1202 * Failure to get the subbuffer causes a busy-loop retry without going
1203 * to a wait queue. These are caused by short-lived race windows where
1204 * the writer is getting access to a subbuffer we were trying to get
1205 * access to. Also checks that the "consumed" buffer count we are
1206 * looking for matches the one contained in the subbuffer id.
1208 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1209 consumed_idx
, buf_trunc_val(consumed
, chan
));
1212 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1214 buf
->get_subbuf_consumed
= consumed
;
1215 buf
->get_subbuf
= 1;
1217 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1223 * The memory barriers __wait_event()/wake_up_interruptible() take care
1224 * of "raw_spin_is_locked" memory ordering.
1228 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1233 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1236 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1239 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1241 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1242 struct channel
*chan
= bufb
->chan
;
1243 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1244 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1246 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1248 if (!buf
->get_subbuf
) {
1250 * Reader puts a subbuffer it did not get.
1252 CHAN_WARN_ON(chan
, 1);
1255 consumed
= buf
->get_subbuf_consumed
;
1256 buf
->get_subbuf
= 0;
1259 * Clear the records_unread counter. (overruns counter)
1260 * Can still be non-zero if a file reader simply grabbed the data
1261 * without using iterators.
1262 * Can be below zero if an iterator is used on a snapshot more than
1265 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1266 v_add(config
, v_read(config
,
1267 &bufb
->array
[read_sb_bindex
]->records_unread
),
1268 &bufb
->records_read
);
1269 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1270 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1271 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1272 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1275 * Exchange the reader subbuffer with the one we put in its place in the
1276 * writer subbuffer table. Expect the original consumed count. If
1277 * update_read_sb_index fails, this is because the writer updated the
1278 * subbuffer concurrently. We should therefore keep the subbuffer we
1279 * currently have: it has become invalid to try reading this sub-buffer
1280 * consumed count value anyway.
1282 consumed_idx
= subbuf_index(consumed
, chan
);
1283 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1284 consumed_idx
, buf_trunc_val(consumed
, chan
));
1286 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1287 * if the writer concurrently updated it.
1290 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1293 * cons_offset is an iterator on all subbuffer offsets between the reader
1294 * position and the writer position. (inclusive)
1297 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1298 struct channel
*chan
,
1299 unsigned long cons_offset
,
1302 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1303 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1305 cons_idx
= subbuf_index(cons_offset
, chan
);
1306 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1307 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1309 if (subbuf_offset(commit_count
, chan
) != 0)
1311 "ring buffer %s, cpu %d: "
1312 "commit count in subbuffer %lu,\n"
1313 "expecting multiples of %lu bytes\n"
1314 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1315 chan
->backend
.name
, cpu
, cons_idx
,
1316 chan
->backend
.subbuf_size
,
1317 commit_count
, commit_count_sb
);
1319 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1320 chan
->backend
.name
, cpu
, commit_count
);
1324 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1325 struct channel
*chan
,
1326 void *priv
, int cpu
)
1328 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1329 unsigned long write_offset
, cons_offset
;
1332 * No need to order commit_count, write_offset and cons_offset reads
1333 * because we execute at teardown when no more writer nor reader
1334 * references are left.
1336 write_offset
= v_read(config
, &buf
->offset
);
1337 cons_offset
= atomic_long_read(&buf
->consumed
);
1338 if (write_offset
!= cons_offset
)
1340 "ring buffer %s, cpu %d: "
1341 "non-consumed data\n"
1342 " [ %lu bytes written, %lu bytes read ]\n",
1343 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1345 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1346 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1349 cons_offset
= subbuf_align(cons_offset
, chan
))
1350 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1354 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1356 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1357 struct lib_ring_buffer
*buf
,
1360 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1362 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1363 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1364 "%lu records overrun\n",
1366 v_read(config
, &buf
->records_count
),
1367 v_read(config
, &buf
->records_overrun
));
1369 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1370 "%lu records overrun\n",
1371 chan
->backend
.name
, cpu
,
1372 v_read(config
, &buf
->records_count
),
1373 v_read(config
, &buf
->records_overrun
));
1378 void lib_ring_buffer_print_records_count(struct channel
*chan
,
1379 struct lib_ring_buffer
*buf
,
1386 void lib_ring_buffer_print_errors(struct channel
*chan
,
1387 struct lib_ring_buffer
*buf
, int cpu
)
1389 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1390 void *priv
= chan
->backend
.priv
;
1392 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1393 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1394 if (v_read(config
, &buf
->records_lost_full
)
1395 || v_read(config
, &buf
->records_lost_wrap
)
1396 || v_read(config
, &buf
->records_lost_big
))
1398 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1399 " [ %lu buffer full, %lu nest buffer wrap-around, "
1400 "%lu event too big ]\n",
1401 chan
->backend
.name
, cpu
,
1402 v_read(config
, &buf
->records_lost_full
),
1403 v_read(config
, &buf
->records_lost_wrap
),
1404 v_read(config
, &buf
->records_lost_big
));
1406 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1410 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1412 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1415 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1416 struct channel
*chan
,
1417 struct switch_offsets
*offsets
,
1420 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1421 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1422 unsigned long commit_count
;
1423 struct commit_counters_hot
*cc_hot
;
1425 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1428 * Order all writes to buffer before the commit count update that will
1429 * determine that the subbuffer is full.
1431 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1433 * Must write slot data before incrementing commit count. This
1434 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1440 cc_hot
= &buf
->commit_hot
[oldidx
];
1441 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1442 commit_count
= v_read(config
, &cc_hot
->cc
);
1443 /* Check if the written buffer has to be delivered */
1444 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1445 commit_count
, oldidx
, tsc
);
1446 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1447 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1448 commit_count
, cc_hot
);
1452 * lib_ring_buffer_switch_old_end: switch old subbuffer
1454 * Note : offset_old should never be 0 here. It is ok, because we never perform
1455 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1456 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1460 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1461 struct channel
*chan
,
1462 struct switch_offsets
*offsets
,
1465 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1466 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1467 unsigned long commit_count
, padding_size
, data_size
;
1468 struct commit_counters_hot
*cc_hot
;
1471 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1472 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1473 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1475 ts_end
= &buf
->ts_end
[oldidx
];
1477 * This is the last space reservation in that sub-buffer before
1478 * it gets delivered. This provides exclusive access to write to
1479 * this sub-buffer's ts_end. There are also no concurrent
1480 * readers of that ts_end because delivery of that sub-buffer is
1481 * postponed until the commit counter is incremented for the
1482 * current space reservation.
1487 * Order all writes to buffer and store to ts_end before the commit
1488 * count update that will determine that the subbuffer is full.
1490 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1492 * Must write slot data before incrementing commit count. This
1493 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1499 cc_hot
= &buf
->commit_hot
[oldidx
];
1500 v_add(config
, padding_size
, &cc_hot
->cc
);
1501 commit_count
= v_read(config
, &cc_hot
->cc
);
1502 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1503 commit_count
, oldidx
, tsc
);
1504 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1505 offsets
->old
+ padding_size
, commit_count
,
1510 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1512 * This code can be executed unordered : writers may already have written to the
1513 * sub-buffer before this code gets executed, caution. The commit makes sure
1514 * that this code is executed before the deliver of this sub-buffer.
1517 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1518 struct channel
*chan
,
1519 struct switch_offsets
*offsets
,
1522 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1523 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1524 unsigned long commit_count
;
1525 struct commit_counters_hot
*cc_hot
;
1527 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1530 * Order all writes to buffer before the commit count update that will
1531 * determine that the subbuffer is full.
1533 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1535 * Must write slot data before incrementing commit count. This
1536 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1542 cc_hot
= &buf
->commit_hot
[beginidx
];
1543 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1544 commit_count
= v_read(config
, &cc_hot
->cc
);
1545 /* Check if the written buffer has to be delivered */
1546 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1547 commit_count
, beginidx
, tsc
);
1548 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1549 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1550 commit_count
, cc_hot
);
1554 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1556 * Calls subbuffer_set_data_size() to set the data size of the current
1557 * sub-buffer. We do not need to perform check_deliver nor commit here,
1558 * since this task will be done by the "commit" of the event for which
1559 * we are currently doing the space reservation.
1562 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1563 struct channel
*chan
,
1564 struct switch_offsets
*offsets
,
1567 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1568 unsigned long endidx
, data_size
;
1571 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1572 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1573 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1574 ts_end
= &buf
->ts_end
[endidx
];
1576 * This is the last space reservation in that sub-buffer before
1577 * it gets delivered. This provides exclusive access to write to
1578 * this sub-buffer's ts_end. There are also no concurrent
1579 * readers of that ts_end because delivery of that sub-buffer is
1580 * postponed until the commit counter is incremented for the
1581 * current space reservation.
1589 * !0 if execution must be aborted.
1592 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1593 struct lib_ring_buffer
*buf
,
1594 struct channel
*chan
,
1595 struct switch_offsets
*offsets
,
1598 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1599 unsigned long off
, reserve_commit_diff
;
1601 offsets
->begin
= v_read(config
, &buf
->offset
);
1602 offsets
->old
= offsets
->begin
;
1603 offsets
->switch_old_start
= 0;
1604 off
= subbuf_offset(offsets
->begin
, chan
);
1606 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1609 * Ensure we flush the header of an empty subbuffer when doing the
1610 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1611 * total data gathering duration even if there were no records saved
1612 * after the last buffer switch.
1613 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1614 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1615 * subbuffer header as appropriate.
1616 * The next record that reserves space will be responsible for
1617 * populating the following subbuffer header. We choose not to populate
1618 * the next subbuffer header here because we want to be able to use
1619 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1620 * buffer flush, which must guarantee that all the buffer content
1621 * (records and header timestamps) are visible to the reader. This is
1622 * required for quiescence guarantees for the fusion merge.
1624 if (mode
!= SWITCH_FLUSH
&& !off
)
1625 return -1; /* we do not have to switch : buffer is empty */
1627 if (unlikely(off
== 0)) {
1628 unsigned long sb_index
, commit_count
;
1631 * We are performing a SWITCH_FLUSH. At this stage, there are no
1632 * concurrent writes into the buffer.
1634 * The client does not save any header information. Don't
1635 * switch empty subbuffer on finalize, because it is invalid to
1636 * deliver a completely empty subbuffer.
1638 if (!config
->cb
.subbuffer_header_size())
1641 /* Test new buffer integrity */
1642 sb_index
= subbuf_index(offsets
->begin
, chan
);
1643 commit_count
= v_read(config
,
1644 &buf
->commit_cold
[sb_index
].cc_sb
);
1645 reserve_commit_diff
=
1646 (buf_trunc(offsets
->begin
, chan
)
1647 >> chan
->backend
.num_subbuf_order
)
1648 - (commit_count
& chan
->commit_count_mask
);
1649 if (likely(reserve_commit_diff
== 0)) {
1650 /* Next subbuffer not being written to. */
1651 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1652 subbuf_trunc(offsets
->begin
, chan
)
1653 - subbuf_trunc((unsigned long)
1654 atomic_long_read(&buf
->consumed
), chan
)
1655 >= chan
->backend
.buf_size
)) {
1657 * We do not overwrite non consumed buffers
1658 * and we are full : don't switch.
1663 * Next subbuffer not being written to, and we
1664 * are either in overwrite mode or the buffer is
1665 * not full. It's safe to write in this new
1671 * Next subbuffer reserve offset does not match the
1672 * commit offset. Don't perform switch in
1673 * producer-consumer and overwrite mode. Caused by
1674 * either a writer OOPS or too many nested writes over a
1675 * reserve/commit pair.
1681 * Need to write the subbuffer start header on finalize.
1683 offsets
->switch_old_start
= 1;
1685 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1686 /* Note: old points to the next subbuf at offset 0 */
1687 offsets
->end
= offsets
->begin
;
1692 * Force a sub-buffer switch. This operation is completely reentrant : can be
1693 * called while tracing is active with absolutely no lock held.
1695 * Note, however, that as a v_cmpxchg is used for some atomic
1696 * operations, this function must be called from the CPU which owns the buffer
1697 * for a ACTIVE flush.
1699 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1701 struct channel
*chan
= buf
->backend
.chan
;
1702 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1703 struct switch_offsets offsets
;
1704 unsigned long oldidx
;
1710 * Perform retryable operations.
1713 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1715 return; /* Switch not needed */
1716 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1720 * Atomically update last_tsc. This update races against concurrent
1721 * atomic updates, but the race will always cause supplementary full TSC
1722 * records, never the opposite (missing a full TSC record when it would
1725 save_last_tsc(config
, buf
, tsc
);
1728 * Push the reader if necessary
1730 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1732 oldidx
= subbuf_index(offsets
.old
, chan
);
1733 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1736 * May need to populate header start on SWITCH_FLUSH.
1738 if (offsets
.switch_old_start
) {
1739 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1740 offsets
.old
+= config
->cb
.subbuffer_header_size();
1744 * Switch old subbuffer.
1746 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1748 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1750 struct switch_param
{
1751 struct lib_ring_buffer
*buf
;
1752 enum switch_mode mode
;
1755 static void remote_switch(void *info
)
1757 struct switch_param
*param
= info
;
1758 struct lib_ring_buffer
*buf
= param
->buf
;
1760 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1763 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1764 enum switch_mode mode
)
1766 struct channel
*chan
= buf
->backend
.chan
;
1767 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1769 struct switch_param param
;
1772 * With global synchronization we don't need to use the IPI scheme.
1774 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1775 lib_ring_buffer_switch_slow(buf
, mode
);
1780 * Disabling preemption ensures two things: first, that the
1781 * target cpu is not taken concurrently offline while we are within
1782 * smp_call_function_single(). Secondly, if it happens that the
1783 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1784 * needs to be protected from CPU hotplug handlers, which can
1785 * also perform a remote subbuffer switch.
1790 ret
= smp_call_function_single(buf
->backend
.cpu
,
1791 remote_switch
, ¶m
, 1);
1793 /* Remote CPU is offline, do it ourself. */
1794 lib_ring_buffer_switch_slow(buf
, mode
);
1799 /* Switch sub-buffer if current sub-buffer is non-empty. */
1800 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1802 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1804 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1806 /* Switch sub-buffer even if current sub-buffer is empty. */
1807 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1809 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1811 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1813 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
)
1815 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1816 struct channel
*chan
= bufb
->chan
;
1818 lib_ring_buffer_switch_remote(buf
);
1819 lib_ring_buffer_clear_reader(buf
, chan
);
1821 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
1826 * -ENOSPC if event size is too large for packet.
1827 * -ENOBUFS if there is currently not enough space in buffer for the event.
1828 * -EIO if data cannot be written into the buffer for any other reason.
1831 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1832 struct channel
*chan
,
1833 struct switch_offsets
*offsets
,
1834 struct lib_ring_buffer_ctx
*ctx
,
1837 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1838 unsigned long reserve_commit_diff
, offset_cmp
;
1841 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1842 offsets
->old
= offsets
->begin
;
1843 offsets
->switch_new_start
= 0;
1844 offsets
->switch_new_end
= 0;
1845 offsets
->switch_old_end
= 0;
1846 offsets
->pre_header_padding
= 0;
1848 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1849 if ((int64_t) ctx
->tsc
== -EIO
)
1852 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1853 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1855 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1856 offsets
->switch_new_start
= 1; /* For offsets->begin */
1858 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1860 &offsets
->pre_header_padding
,
1863 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1866 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1867 offsets
->size
> chan
->backend
.subbuf_size
)) {
1868 offsets
->switch_old_end
= 1; /* For offsets->old */
1869 offsets
->switch_new_start
= 1; /* For offsets->begin */
1872 if (unlikely(offsets
->switch_new_start
)) {
1873 unsigned long sb_index
, commit_count
;
1876 * We are typically not filling the previous buffer completely.
1878 if (likely(offsets
->switch_old_end
))
1879 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1880 offsets
->begin
= offsets
->begin
1881 + config
->cb
.subbuffer_header_size();
1882 /* Test new buffer integrity */
1883 sb_index
= subbuf_index(offsets
->begin
, chan
);
1885 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1886 * lib_ring_buffer_check_deliver() has the matching
1887 * memory barriers required around commit_cold cc_sb
1888 * updates to ensure reserve and commit counter updates
1889 * are not seen reordered when updated by another CPU.
1892 commit_count
= v_read(config
,
1893 &buf
->commit_cold
[sb_index
].cc_sb
);
1894 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1896 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1898 * The reserve counter have been concurrently updated
1899 * while we read the commit counter. This means the
1900 * commit counter we read might not match buf->offset
1901 * due to concurrent update. We therefore need to retry.
1905 reserve_commit_diff
=
1906 (buf_trunc(offsets
->begin
, chan
)
1907 >> chan
->backend
.num_subbuf_order
)
1908 - (commit_count
& chan
->commit_count_mask
);
1909 if (likely(reserve_commit_diff
== 0)) {
1910 /* Next subbuffer not being written to. */
1911 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1912 subbuf_trunc(offsets
->begin
, chan
)
1913 - subbuf_trunc((unsigned long)
1914 atomic_long_read(&buf
->consumed
), chan
)
1915 >= chan
->backend
.buf_size
)) {
1917 * We do not overwrite non consumed buffers
1918 * and we are full : record is lost.
1920 v_inc(config
, &buf
->records_lost_full
);
1924 * Next subbuffer not being written to, and we
1925 * are either in overwrite mode or the buffer is
1926 * not full. It's safe to write in this new
1932 * Next subbuffer reserve offset does not match the
1933 * commit offset, and this did not involve update to the
1934 * reserve counter. Drop record in producer-consumer and
1935 * overwrite mode. Caused by either a writer OOPS or
1936 * too many nested writes over a reserve/commit pair.
1938 v_inc(config
, &buf
->records_lost_wrap
);
1942 config
->cb
.record_header_size(config
, chan
,
1944 &offsets
->pre_header_padding
,
1947 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1950 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1951 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1953 * Record too big for subbuffers, report error, don't
1954 * complete the sub-buffer switch.
1956 v_inc(config
, &buf
->records_lost_big
);
1960 * We just made a successful buffer switch and the
1961 * record fits in the new subbuffer. Let's write.
1966 * Record fits in the current buffer and we are not on a switch
1967 * boundary. It's safe to write.
1970 offsets
->end
= offsets
->begin
+ offsets
->size
;
1972 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1974 * The offset_end will fall at the very beginning of the next
1977 offsets
->switch_new_end
= 1; /* For offsets->begin */
1982 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
1984 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1986 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1987 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1989 return chan
->backend
.buf
;
1992 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
1994 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1995 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
1997 v_inc(config
, &buf
->records_lost_big
);
1999 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2002 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2003 * @ctx: ring buffer context.
2005 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2006 * -EIO for other errors, else returns 0.
2007 * It will take care of sub-buffer switching.
2009 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
,
2012 struct channel
*chan
= ctx
->chan
;
2013 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2014 struct lib_ring_buffer
*buf
;
2015 struct switch_offsets offsets
;
2018 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2022 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2026 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2031 * Atomically update last_tsc. This update races against concurrent
2032 * atomic updates, but the race will always cause supplementary full TSC
2033 * records, never the opposite (missing a full TSC record when it would
2036 save_last_tsc(config
, buf
, ctx
->tsc
);
2039 * Push the reader if necessary
2041 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2044 * Clear noref flag for this subbuffer.
2046 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2047 subbuf_index(offsets
.end
- 1, chan
));
2050 * Switch old subbuffer if needed.
2052 if (unlikely(offsets
.switch_old_end
)) {
2053 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2054 subbuf_index(offsets
.old
- 1, chan
));
2055 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2059 * Populate new subbuffer.
2061 if (unlikely(offsets
.switch_new_start
))
2062 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2064 if (unlikely(offsets
.switch_new_end
))
2065 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2067 ctx
->slot_size
= offsets
.size
;
2068 ctx
->pre_offset
= offsets
.begin
;
2069 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2072 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2075 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2076 struct lib_ring_buffer
*buf
,
2077 unsigned long commit_count
,
2080 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2081 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2085 * The ring buffer can count events recorded and overwritten per buffer,
2086 * but it is disabled by default due to its performance overhead.
2088 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2090 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2091 struct lib_ring_buffer
*buf
,
2094 v_add(config
, subbuffer_get_records_count(config
,
2095 &buf
->backend
, idx
),
2096 &buf
->records_count
);
2097 v_add(config
, subbuffer_count_records_overrun(config
,
2098 &buf
->backend
, idx
),
2099 &buf
->records_overrun
);
2101 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2103 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2104 struct lib_ring_buffer
*buf
,
2108 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2111 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2112 struct lib_ring_buffer
*buf
,
2113 struct channel
*chan
,
2114 unsigned long offset
,
2115 unsigned long commit_count
,
2119 unsigned long old_commit_count
= commit_count
2120 - chan
->backend
.subbuf_size
;
2123 * If we succeeded at updating cc_sb below, we are the subbuffer
2124 * writer delivering the subbuffer. Deals with concurrent
2125 * updates of the "cc" value without adding a add_return atomic
2126 * operation to the fast path.
2128 * We are doing the delivery in two steps:
2129 * - First, we cmpxchg() cc_sb to the new value
2130 * old_commit_count + 1. This ensures that we are the only
2131 * subbuffer user successfully filling the subbuffer, but we
2132 * do _not_ set the cc_sb value to "commit_count" yet.
2133 * Therefore, other writers that would wrap around the ring
2134 * buffer and try to start writing to our subbuffer would
2135 * have to drop records, because it would appear as
2137 * We therefore have exclusive access to the subbuffer control
2138 * structures. This mutual exclusion with other writers is
2139 * crucially important to perform record overruns count in
2140 * flight recorder mode locklessly.
2141 * - When we are ready to release the subbuffer (either for
2142 * reading or for overrun by other writers), we simply set the
2143 * cc_sb value to "commit_count" and perform delivery.
2145 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2146 * This guarantees that old_commit_count + 1 != commit_count.
2150 * Order prior updates to reserve count prior to the
2151 * commit_cold cc_sb update.
2154 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2155 old_commit_count
, old_commit_count
+ 1)
2156 == old_commit_count
)) {
2160 * Start of exclusive subbuffer access. We are
2161 * guaranteed to be the last writer in this subbuffer
2162 * and any other writer trying to access this subbuffer
2163 * in this state is required to drop records.
2165 * We can read the ts_end for the current sub-buffer
2166 * which has been saved by the very last space
2167 * reservation for the current sub-buffer.
2169 * Order increment of commit counter before reading ts_end.
2172 ts_end
= &buf
->ts_end
[idx
];
2173 deliver_count_events(config
, buf
, idx
);
2174 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2175 lib_ring_buffer_get_data_size(config
,
2180 * Increment the packet counter while we have exclusive
2183 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2186 * Set noref flag and offset for this subbuffer id.
2187 * Contains a memory barrier that ensures counter stores
2188 * are ordered before set noref and offset.
2190 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2191 buf_trunc_val(offset
, chan
));
2194 * Order set_noref and record counter updates before the
2195 * end of subbuffer exclusive access. Orders with
2196 * respect to writers coming into the subbuffer after
2197 * wrap around, and also order wrt concurrent readers.
2200 /* End of exclusive subbuffer access */
2201 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2204 * Order later updates to reserve count after
2205 * the commit_cold cc_sb update.
2208 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2212 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2214 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2215 && atomic_long_read(&buf
->active_readers
)
2216 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2217 wake_up_interruptible(&buf
->read_wait
);
2218 wake_up_interruptible(&chan
->read_wait
);
2223 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2225 int __init
init_lib_ring_buffer_frontend(void)
2229 for_each_possible_cpu(cpu
)
2230 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2234 module_init(init_lib_ring_buffer_frontend
);
2236 void __exit
exit_lib_ring_buffer_frontend(void)
2240 module_exit(exit_lib_ring_buffer_frontend
);