2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
54 #include <linux/delay.h>
55 #include <linux/module.h>
56 #include <linux/percpu.h>
58 #include <wrapper/ringbuffer/config.h>
59 #include <wrapper/ringbuffer/backend.h>
60 #include <wrapper/ringbuffer/frontend.h>
61 #include <wrapper/ringbuffer/iterator.h>
62 #include <wrapper/ringbuffer/nohz.h>
63 #include <wrapper/atomic.h>
64 #include <wrapper/kref.h>
65 #include <wrapper/percpu-defs.h>
66 #include <wrapper/timer.h>
69 * Internal structure representing offsets to use at a sub-buffer switch.
71 struct switch_offsets
{
72 unsigned long begin
, end
, old
;
73 size_t pre_header_padding
, size
;
74 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
85 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
86 #endif /* CONFIG_NO_HZ */
88 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
90 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
91 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
94 void lib_ring_buffer_print_errors(struct channel
*chan
,
95 struct lib_ring_buffer
*buf
, int cpu
);
97 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
98 enum switch_mode mode
);
101 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config
*config
,
102 struct lib_ring_buffer
*buf
,
103 struct channel
*chan
)
105 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
107 consumed_old
= atomic_long_read(&buf
->consumed
);
108 consumed_idx
= subbuf_index(consumed_old
, chan
);
109 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
111 * No memory barrier here, since we are only interested
112 * in a statistically correct polling result. The next poll will
113 * get the data is we are racing. The mb() that ensures correct
114 * memory order is in get_subbuf.
116 write_offset
= v_read(config
, &buf
->offset
);
119 * Check that the subbuffer we are trying to consume has been
120 * already fully committed.
123 if (((commit_count
- chan
->backend
.subbuf_size
)
124 & chan
->commit_count_mask
)
125 - (buf_trunc(consumed_old
, chan
)
126 >> chan
->backend
.num_subbuf_order
)
131 * Check that we are not about to read the same subbuffer in
132 * which the writer head is.
134 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
142 * Must be called under cpu hotplug protection.
144 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
146 struct channel
*chan
= buf
->backend
.chan
;
148 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
149 kfree(buf
->commit_hot
);
150 kfree(buf
->commit_cold
);
152 lib_ring_buffer_backend_free(&buf
->backend
);
156 * lib_ring_buffer_reset - Reset ring buffer to initial values.
159 * Effectively empty the ring buffer. Should be called when the buffer is not
160 * used for writing. The ring buffer can be opened for reading, but the reader
161 * should not be using the iterator concurrently with reset. The previous
162 * current iterator record is reset.
164 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
166 struct channel
*chan
= buf
->backend
.chan
;
167 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
171 * Reset iterator first. It will put the subbuffer if it currently holds
174 lib_ring_buffer_iterator_reset(buf
);
175 v_set(config
, &buf
->offset
, 0);
176 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
177 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
178 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
179 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
181 atomic_long_set(&buf
->consumed
, 0);
182 atomic_set(&buf
->record_disabled
, 0);
183 v_set(config
, &buf
->last_tsc
, 0);
184 lib_ring_buffer_backend_reset(&buf
->backend
);
185 /* Don't reset number of active readers */
186 v_set(config
, &buf
->records_lost_full
, 0);
187 v_set(config
, &buf
->records_lost_wrap
, 0);
188 v_set(config
, &buf
->records_lost_big
, 0);
189 v_set(config
, &buf
->records_count
, 0);
190 v_set(config
, &buf
->records_overrun
, 0);
193 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
196 * channel_reset - Reset channel to initial values.
199 * Effectively empty the channel. Should be called when the channel is not used
200 * for writing. The channel can be opened for reading, but the reader should not
201 * be using the iterator concurrently with reset. The previous current iterator
204 void channel_reset(struct channel
*chan
)
207 * Reset iterators first. Will put the subbuffer if held for reading.
209 channel_iterator_reset(chan
);
210 atomic_set(&chan
->record_disabled
, 0);
211 /* Don't reset commit_count_mask, still valid */
212 channel_backend_reset(&chan
->backend
);
213 /* Don't reset switch/read timer interval */
214 /* Don't reset notifiers and notifier enable bits */
215 /* Don't reset reader reference count */
217 EXPORT_SYMBOL_GPL(channel_reset
);
220 * Must be called under cpu hotplug protection.
222 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
223 struct channel_backend
*chanb
, int cpu
)
225 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
226 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
227 void *priv
= chanb
->priv
;
228 size_t subbuf_header_size
;
232 /* Test for cpu hotplug */
233 if (buf
->backend
.allocated
)
237 * Paranoia: per cpu dynamic allocation is not officially documented as
238 * zeroing the memory, so let's do it here too, just in case.
240 memset(buf
, 0, sizeof(*buf
));
242 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
247 kzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
248 * chan
->backend
.num_subbuf
,
249 1 << INTERNODE_CACHE_SHIFT
),
250 GFP_KERNEL
| __GFP_NOWARN
,
251 cpu_to_node(max(cpu
, 0)));
252 if (!buf
->commit_hot
) {
258 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
259 * chan
->backend
.num_subbuf
,
260 1 << INTERNODE_CACHE_SHIFT
),
261 GFP_KERNEL
| __GFP_NOWARN
,
262 cpu_to_node(max(cpu
, 0)));
263 if (!buf
->commit_cold
) {
268 init_waitqueue_head(&buf
->read_wait
);
269 init_waitqueue_head(&buf
->write_wait
);
270 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
273 * Write the subbuffer header for first subbuffer so we know the total
274 * duration of data gathering.
276 subbuf_header_size
= config
->cb
.subbuffer_header_size();
277 v_set(config
, &buf
->offset
, subbuf_header_size
);
278 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
279 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
280 config
->cb
.buffer_begin(buf
, tsc
, 0);
281 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
283 if (config
->cb
.buffer_create
) {
284 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
290 * Ensure the buffer is ready before setting it to allocated and setting
292 * Used for cpu hotplug vs cpumask iteration.
295 buf
->backend
.allocated
= 1;
297 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
298 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
299 chan
->backend
.cpumask
));
300 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
307 kfree(buf
->commit_cold
);
309 kfree(buf
->commit_hot
);
311 lib_ring_buffer_backend_free(&buf
->backend
);
315 static void switch_buffer_timer(unsigned long data
)
317 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
318 struct channel
*chan
= buf
->backend
.chan
;
319 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
322 * Only flush buffers periodically if readers are active.
324 if (atomic_long_read(&buf
->active_readers
))
325 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
327 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
328 lttng_mod_timer_pinned(&buf
->switch_timer
,
329 jiffies
+ chan
->switch_timer_interval
);
331 mod_timer(&buf
->switch_timer
,
332 jiffies
+ chan
->switch_timer_interval
);
336 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
338 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
340 struct channel
*chan
= buf
->backend
.chan
;
341 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
343 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
346 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
347 lttng_init_timer_pinned(&buf
->switch_timer
);
349 init_timer(&buf
->switch_timer
);
351 buf
->switch_timer
.function
= switch_buffer_timer
;
352 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
353 buf
->switch_timer
.data
= (unsigned long)buf
;
354 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
355 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
357 add_timer(&buf
->switch_timer
);
358 buf
->switch_timer_enabled
= 1;
362 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
364 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
366 struct channel
*chan
= buf
->backend
.chan
;
368 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
371 del_timer_sync(&buf
->switch_timer
);
372 buf
->switch_timer_enabled
= 0;
376 * Polling timer to check the channels for data.
378 static void read_buffer_timer(unsigned long data
)
380 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
381 struct channel
*chan
= buf
->backend
.chan
;
382 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
384 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
386 if (atomic_long_read(&buf
->active_readers
)
387 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
388 wake_up_interruptible(&buf
->read_wait
);
389 wake_up_interruptible(&chan
->read_wait
);
392 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
393 lttng_mod_timer_pinned(&buf
->read_timer
,
394 jiffies
+ chan
->read_timer_interval
);
396 mod_timer(&buf
->read_timer
,
397 jiffies
+ chan
->read_timer_interval
);
401 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
403 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
405 struct channel
*chan
= buf
->backend
.chan
;
406 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
408 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
409 || !chan
->read_timer_interval
410 || buf
->read_timer_enabled
)
413 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
414 lttng_init_timer_pinned(&buf
->read_timer
);
416 init_timer(&buf
->read_timer
);
418 buf
->read_timer
.function
= read_buffer_timer
;
419 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
420 buf
->read_timer
.data
= (unsigned long)buf
;
422 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
423 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
425 add_timer(&buf
->read_timer
);
426 buf
->read_timer_enabled
= 1;
430 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
432 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
434 struct channel
*chan
= buf
->backend
.chan
;
435 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
437 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
438 || !chan
->read_timer_interval
439 || !buf
->read_timer_enabled
)
442 del_timer_sync(&buf
->read_timer
);
444 * do one more check to catch data that has been written in the last
447 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
448 wake_up_interruptible(&buf
->read_wait
);
449 wake_up_interruptible(&chan
->read_wait
);
451 buf
->read_timer_enabled
= 0;
454 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
456 enum cpuhp_state lttng_rb_hp_prepare
;
457 enum cpuhp_state lttng_rb_hp_online
;
459 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
461 lttng_rb_hp_prepare
= val
;
463 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
465 void lttng_rb_set_hp_online(enum cpuhp_state val
)
467 lttng_rb_hp_online
= val
;
469 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
471 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
472 struct lttng_cpuhp_node
*node
)
474 struct channel
*chan
= container_of(node
, struct channel
,
476 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
477 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
479 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
482 * Performing a buffer switch on a remote CPU. Performed by
483 * the CPU responsible for doing the hotunplug after the target
484 * CPU stopped running completely. Ensures that all data
485 * from that remote CPU is flushed.
487 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
490 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
492 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
493 struct lttng_cpuhp_node
*node
)
495 struct channel
*chan
= container_of(node
, struct channel
,
497 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
498 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
500 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
502 wake_up_interruptible(&chan
->hp_wait
);
503 lib_ring_buffer_start_switch_timer(buf
);
504 lib_ring_buffer_start_read_timer(buf
);
507 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
509 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
510 struct lttng_cpuhp_node
*node
)
512 struct channel
*chan
= container_of(node
, struct channel
,
514 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
515 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
517 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
519 lib_ring_buffer_stop_switch_timer(buf
);
520 lib_ring_buffer_stop_read_timer(buf
);
523 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
525 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
527 #ifdef CONFIG_HOTPLUG_CPU
530 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
531 * @nb: notifier block
532 * @action: hotplug action to take
535 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
538 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
539 unsigned long action
,
542 unsigned int cpu
= (unsigned long)hcpu
;
543 struct channel
*chan
= container_of(nb
, struct channel
,
545 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
546 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
548 if (!chan
->cpu_hp_enable
)
551 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
554 case CPU_DOWN_FAILED
:
555 case CPU_DOWN_FAILED_FROZEN
:
557 case CPU_ONLINE_FROZEN
:
558 wake_up_interruptible(&chan
->hp_wait
);
559 lib_ring_buffer_start_switch_timer(buf
);
560 lib_ring_buffer_start_read_timer(buf
);
563 case CPU_DOWN_PREPARE
:
564 case CPU_DOWN_PREPARE_FROZEN
:
565 lib_ring_buffer_stop_switch_timer(buf
);
566 lib_ring_buffer_stop_read_timer(buf
);
570 case CPU_DEAD_FROZEN
:
572 * Performing a buffer switch on a remote CPU. Performed by
573 * the CPU responsible for doing the hotunplug after the target
574 * CPU stopped running completely. Ensures that all data
575 * from that remote CPU is flushed.
577 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
587 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
589 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
591 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
592 * that wake-up-tracing generated events are flushed before going idle (in
593 * tick_nohz). We test if the spinlock is locked to deal with the race where
594 * readers try to sample the ring buffer before we perform the switch. We let
595 * the readers retry in that case. If there is data in the buffer, the wake up
596 * is going to forbid the CPU running the reader thread from going idle.
598 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
602 struct channel
*chan
= container_of(nb
, struct channel
,
604 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
605 struct lib_ring_buffer
*buf
;
606 int cpu
= smp_processor_id();
608 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
610 * We don't support keeping the system idle with global buffers
611 * and streaming active. In order to do so, we would need to
612 * sample a non-nohz-cpumask racelessly with the nohz updates
613 * without adding synchronization overhead to nohz. Leave this
614 * use-case out for now.
619 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
621 case TICK_NOHZ_FLUSH
:
622 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
623 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
624 && chan
->read_timer_interval
625 && atomic_long_read(&buf
->active_readers
)
626 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
627 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
628 wake_up_interruptible(&buf
->read_wait
);
629 wake_up_interruptible(&chan
->read_wait
);
631 if (chan
->switch_timer_interval
)
632 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
633 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
636 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
637 lib_ring_buffer_stop_switch_timer(buf
);
638 lib_ring_buffer_stop_read_timer(buf
);
639 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
641 case TICK_NOHZ_RESTART
:
642 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
643 lib_ring_buffer_start_read_timer(buf
);
644 lib_ring_buffer_start_switch_timer(buf
);
645 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
652 void notrace
lib_ring_buffer_tick_nohz_flush(void)
654 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
658 void notrace
lib_ring_buffer_tick_nohz_stop(void)
660 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
664 void notrace
lib_ring_buffer_tick_nohz_restart(void)
666 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
669 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
674 static void channel_unregister_notifiers(struct channel
*chan
)
676 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
678 channel_iterator_unregister_notifiers(chan
);
679 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
682 * Remove the nohz notifier first, so we are certain we stop
685 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
686 &chan
->tick_nohz_notifier
);
688 * ring_buffer_nohz_lock will not be needed below, because
689 * we just removed the notifiers, which were the only source of
692 #endif /* CONFIG_NO_HZ */
693 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
697 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
698 &chan
->cpuhp_online
.node
);
700 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
701 &chan
->cpuhp_prepare
.node
);
704 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
708 #ifdef CONFIG_HOTPLUG_CPU
710 chan
->cpu_hp_enable
= 0;
711 for_each_online_cpu(cpu
) {
712 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
714 lib_ring_buffer_stop_switch_timer(buf
);
715 lib_ring_buffer_stop_read_timer(buf
);
718 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
720 for_each_possible_cpu(cpu
) {
721 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
723 lib_ring_buffer_stop_switch_timer(buf
);
724 lib_ring_buffer_stop_read_timer(buf
);
728 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
730 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
732 lib_ring_buffer_stop_switch_timer(buf
);
733 lib_ring_buffer_stop_read_timer(buf
);
735 channel_backend_unregister_notifiers(&chan
->backend
);
738 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
740 if (!buf
->quiescent
) {
741 buf
->quiescent
= true;
742 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
746 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
748 buf
->quiescent
= false;
751 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
754 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
756 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
758 for_each_channel_cpu(cpu
, chan
) {
759 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
762 lib_ring_buffer_set_quiescent(buf
);
766 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
768 lib_ring_buffer_set_quiescent(buf
);
771 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
773 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
776 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
778 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
780 for_each_channel_cpu(cpu
, chan
) {
781 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
784 lib_ring_buffer_clear_quiescent(buf
);
788 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
790 lib_ring_buffer_clear_quiescent(buf
);
793 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
795 static void channel_free(struct channel
*chan
)
797 if (chan
->backend
.release_priv_ops
) {
798 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
800 channel_iterator_free(chan
);
801 channel_backend_free(&chan
->backend
);
806 * channel_create - Create channel.
807 * @config: ring buffer instance configuration
808 * @name: name of the channel
809 * @priv: ring buffer client private data
810 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
811 * address mapping. It is used only by RING_BUFFER_STATIC
812 * configuration. It can be set to NULL for other backends.
813 * @subbuf_size: subbuffer size
814 * @num_subbuf: number of subbuffers
815 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
816 * padding to let readers get those sub-buffers.
817 * Used for live streaming.
818 * @read_timer_interval: Time interval (in us) to wake up pending readers.
821 * Returns NULL on failure.
823 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
824 const char *name
, void *priv
, void *buf_addr
,
826 size_t num_subbuf
, unsigned int switch_timer_interval
,
827 unsigned int read_timer_interval
)
830 struct channel
*chan
;
832 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
833 read_timer_interval
))
836 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
840 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
841 subbuf_size
, num_subbuf
);
845 ret
= channel_iterator_init(chan
);
847 goto error_free_backend
;
849 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
850 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
851 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
852 kref_init(&chan
->ref
);
853 init_waitqueue_head(&chan
->read_wait
);
854 init_waitqueue_head(&chan
->hp_wait
);
856 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
857 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
858 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
859 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
860 &chan
->cpuhp_prepare
.node
);
862 goto cpuhp_prepare_error
;
864 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
865 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
866 &chan
->cpuhp_online
.node
);
868 goto cpuhp_online_error
;
869 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
873 * In case of non-hotplug cpu, if the ring-buffer is allocated
874 * in early initcall, it will not be notified of secondary cpus.
875 * In that off case, we need to allocate for all possible cpus.
877 #ifdef CONFIG_HOTPLUG_CPU
878 chan
->cpu_hp_notifier
.notifier_call
=
879 lib_ring_buffer_cpu_hp_callback
;
880 chan
->cpu_hp_notifier
.priority
= 6;
881 register_cpu_notifier(&chan
->cpu_hp_notifier
);
884 for_each_online_cpu(cpu
) {
885 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
887 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
888 lib_ring_buffer_start_switch_timer(buf
);
889 lib_ring_buffer_start_read_timer(buf
);
890 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
892 chan
->cpu_hp_enable
= 1;
895 for_each_possible_cpu(cpu
) {
896 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
898 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
899 lib_ring_buffer_start_switch_timer(buf
);
900 lib_ring_buffer_start_read_timer(buf
);
901 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
905 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
907 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
908 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
909 chan
->tick_nohz_notifier
.notifier_call
=
910 ring_buffer_tick_nohz_callback
;
911 chan
->tick_nohz_notifier
.priority
= ~0U;
912 atomic_notifier_chain_register(&tick_nohz_notifier
,
913 &chan
->tick_nohz_notifier
);
914 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
917 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
919 lib_ring_buffer_start_switch_timer(buf
);
920 lib_ring_buffer_start_read_timer(buf
);
925 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
927 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
928 &chan
->cpuhp_prepare
.node
);
931 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
933 channel_backend_free(&chan
->backend
);
938 EXPORT_SYMBOL_GPL(channel_create
);
941 void channel_release(struct kref
*kref
)
943 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
948 * channel_destroy - Finalize, wait for q.s. and destroy channel.
949 * @chan: channel to destroy
952 * Call "destroy" callback, finalize channels, and then decrement the
953 * channel reference count. Note that when readers have completed data
954 * consumption of finalized channels, get_subbuf() will return -ENODATA.
955 * They should release their handle at that point. Returns the private
958 void *channel_destroy(struct channel
*chan
)
961 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
964 channel_unregister_notifiers(chan
);
966 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
968 * No need to hold cpu hotplug, because all notifiers have been
971 for_each_channel_cpu(cpu
, chan
) {
972 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
975 if (config
->cb
.buffer_finalize
)
976 config
->cb
.buffer_finalize(buf
,
979 if (buf
->backend
.allocated
)
980 lib_ring_buffer_set_quiescent(buf
);
982 * Perform flush before writing to finalized.
985 ACCESS_ONCE(buf
->finalized
) = 1;
986 wake_up_interruptible(&buf
->read_wait
);
989 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
991 if (config
->cb
.buffer_finalize
)
992 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
993 if (buf
->backend
.allocated
)
994 lib_ring_buffer_set_quiescent(buf
);
996 * Perform flush before writing to finalized.
999 ACCESS_ONCE(buf
->finalized
) = 1;
1000 wake_up_interruptible(&buf
->read_wait
);
1002 ACCESS_ONCE(chan
->finalized
) = 1;
1003 wake_up_interruptible(&chan
->hp_wait
);
1004 wake_up_interruptible(&chan
->read_wait
);
1005 priv
= chan
->backend
.priv
;
1006 kref_put(&chan
->ref
, channel_release
);
1009 EXPORT_SYMBOL_GPL(channel_destroy
);
1011 struct lib_ring_buffer
*channel_get_ring_buffer(
1012 const struct lib_ring_buffer_config
*config
,
1013 struct channel
*chan
, int cpu
)
1015 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1016 return chan
->backend
.buf
;
1018 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1020 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1022 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
1024 struct channel
*chan
= buf
->backend
.chan
;
1026 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1028 if (!lttng_kref_get(&chan
->ref
)) {
1029 atomic_long_dec(&buf
->active_readers
);
1032 lttng_smp_mb__after_atomic();
1035 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1037 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
1039 struct channel
*chan
= buf
->backend
.chan
;
1041 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1042 lttng_smp_mb__before_atomic();
1043 atomic_long_dec(&buf
->active_readers
);
1044 kref_put(&chan
->ref
, channel_release
);
1046 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1049 * Promote compiler barrier to a smp_mb().
1050 * For the specific ring buffer case, this IPI call should be removed if the
1051 * architecture does not reorder writes. This should eventually be provided by
1052 * a separate architecture-specific infrastructure.
1054 static void remote_mb(void *info
)
1060 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1062 * @consumed: consumed count indicating the position where to read
1063 * @produced: produced count, indicates position when to stop reading
1065 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1066 * data to read at consumed position, or 0 if the get operation succeeds.
1067 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1070 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
1071 unsigned long *consumed
, unsigned long *produced
)
1073 struct channel
*chan
= buf
->backend
.chan
;
1074 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1075 unsigned long consumed_cur
, write_offset
;
1079 * First, ensure we perform a "final" flush onto the stream. This will
1080 * ensure we create a packet of padding if we encounter an empty
1081 * packet. This ensures the time-stamps right before the snapshot is
1082 * used as end of packet timestamp.
1084 if (!buf
->quiescent
)
1085 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1088 finalized
= ACCESS_ONCE(buf
->finalized
);
1090 * Read finalized before counters.
1093 consumed_cur
= atomic_long_read(&buf
->consumed
);
1095 * No need to issue a memory barrier between consumed count read and
1096 * write offset read, because consumed count can only change
1097 * concurrently in overwrite mode, and we keep a sequence counter
1098 * identifier derived from the write offset to check we are getting
1099 * the same sub-buffer we are expecting (the sub-buffers are atomically
1100 * "tagged" upon writes, tags are checked upon read).
1102 write_offset
= v_read(config
, &buf
->offset
);
1105 * Check that we are not about to read the same subbuffer in
1106 * which the writer head is.
1108 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1112 *consumed
= consumed_cur
;
1113 *produced
= subbuf_trunc(write_offset
, chan
);
1119 * The memory barriers __wait_event()/wake_up_interruptible() take care
1120 * of "raw_spin_is_locked" memory ordering.
1124 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1129 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1132 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1133 * are saved regardless of whether the consumed and produced positions are
1134 * in the same subbuffer.
1136 * @consumed: consumed byte count indicating the last position read
1137 * @produced: produced byte count indicating the last position written
1139 * This function is meant to provide information on the exact producer and
1140 * consumer positions without regard for the "snapshot" feature.
1142 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer
*buf
,
1143 unsigned long *consumed
, unsigned long *produced
)
1145 struct channel
*chan
= buf
->backend
.chan
;
1146 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1149 *consumed
= atomic_long_read(&buf
->consumed
);
1151 * No need to issue a memory barrier between consumed count read and
1152 * write offset read, because consumed count can only change
1153 * concurrently in overwrite mode, and we keep a sequence counter
1154 * identifier derived from the write offset to check we are getting
1155 * the same sub-buffer we are expecting (the sub-buffers are atomically
1156 * "tagged" upon writes, tags are checked upon read).
1158 *produced
= v_read(config
, &buf
->offset
);
1163 * lib_ring_buffer_put_snapshot - move consumed counter forward
1165 * Should only be called from consumer context.
1167 * @consumed_new: new consumed count value
1169 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
1170 unsigned long consumed_new
)
1172 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1173 struct channel
*chan
= bufb
->chan
;
1174 unsigned long consumed
;
1176 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1179 * Only push the consumed value forward.
1180 * If the consumed cmpxchg fails, this is because we have been pushed by
1181 * the writer in flight recorder mode.
1183 consumed
= atomic_long_read(&buf
->consumed
);
1184 while ((long) consumed
- (long) consumed_new
< 0)
1185 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1187 /* Wake-up the metadata producer */
1188 wake_up_interruptible(&buf
->write_wait
);
1190 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1193 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1195 * @consumed: consumed count indicating the position where to read
1197 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1198 * data to read at consumed position, or 0 if the get operation succeeds.
1199 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1201 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1202 unsigned long consumed
)
1204 struct channel
*chan
= buf
->backend
.chan
;
1205 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1206 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1210 if (buf
->get_subbuf
) {
1212 * Reader is trying to get a subbuffer twice.
1214 CHAN_WARN_ON(chan
, 1);
1218 finalized
= ACCESS_ONCE(buf
->finalized
);
1220 * Read finalized before counters.
1223 consumed_cur
= atomic_long_read(&buf
->consumed
);
1224 consumed_idx
= subbuf_index(consumed
, chan
);
1225 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1227 * Make sure we read the commit count before reading the buffer
1228 * data and the write offset. Correct consumed offset ordering
1229 * wrt commit count is insured by the use of cmpxchg to update
1230 * the consumed offset.
1231 * smp_call_function_single can fail if the remote CPU is offline,
1232 * this is OK because then there is no wmb to execute there.
1233 * If our thread is executing on the same CPU as the on the buffers
1234 * belongs to, we don't have to synchronize it at all. If we are
1235 * migrated, the scheduler will take care of the memory barriers.
1236 * Normally, smp_call_function_single() should ensure program order when
1237 * executing the remote function, which implies that it surrounds the
1238 * function execution with :
1249 * However, smp_call_function_single() does not seem to clearly execute
1250 * such barriers. It depends on spinlock semantic to provide the barrier
1251 * before executing the IPI and, when busy-looping, csd_lock_wait only
1252 * executes smp_mb() when it has to wait for the other CPU.
1254 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1255 * required ourself, even if duplicated. It has no performance impact
1258 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1259 * read and write vs write. They do not ensure core synchronization. We
1260 * really have to ensure total order between the 3 barriers running on
1263 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1264 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1265 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1266 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1267 /* Total order with IPI handler smp_mb() */
1269 smp_call_function_single(buf
->backend
.cpu
,
1270 remote_mb
, NULL
, 1);
1271 /* Total order with IPI handler smp_mb() */
1275 /* Total order with IPI handler smp_mb() */
1277 smp_call_function(remote_mb
, NULL
, 1);
1278 /* Total order with IPI handler smp_mb() */
1283 * Local rmb to match the remote wmb to read the commit count
1284 * before the buffer data and the write offset.
1289 write_offset
= v_read(config
, &buf
->offset
);
1292 * Check that the buffer we are getting is after or at consumed_cur
1295 if ((long) subbuf_trunc(consumed
, chan
)
1296 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1300 * Check that the subbuffer we are trying to consume has been
1301 * already fully committed.
1303 if (((commit_count
- chan
->backend
.subbuf_size
)
1304 & chan
->commit_count_mask
)
1305 - (buf_trunc(consumed
, chan
)
1306 >> chan
->backend
.num_subbuf_order
)
1311 * Check that we are not about to read the same subbuffer in
1312 * which the writer head is.
1314 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1319 * Failure to get the subbuffer causes a busy-loop retry without going
1320 * to a wait queue. These are caused by short-lived race windows where
1321 * the writer is getting access to a subbuffer we were trying to get
1322 * access to. Also checks that the "consumed" buffer count we are
1323 * looking for matches the one contained in the subbuffer id.
1325 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1326 consumed_idx
, buf_trunc_val(consumed
, chan
));
1329 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1331 buf
->get_subbuf_consumed
= consumed
;
1332 buf
->get_subbuf
= 1;
1338 * The memory barriers __wait_event()/wake_up_interruptible() take care
1339 * of "raw_spin_is_locked" memory ordering.
1343 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1348 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1351 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1354 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1356 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1357 struct channel
*chan
= bufb
->chan
;
1358 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1359 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1361 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1363 if (!buf
->get_subbuf
) {
1365 * Reader puts a subbuffer it did not get.
1367 CHAN_WARN_ON(chan
, 1);
1370 consumed
= buf
->get_subbuf_consumed
;
1371 buf
->get_subbuf
= 0;
1374 * Clear the records_unread counter. (overruns counter)
1375 * Can still be non-zero if a file reader simply grabbed the data
1376 * without using iterators.
1377 * Can be below zero if an iterator is used on a snapshot more than
1380 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1381 v_add(config
, v_read(config
,
1382 &bufb
->array
[read_sb_bindex
]->records_unread
),
1383 &bufb
->records_read
);
1384 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1385 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1386 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1387 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1390 * Exchange the reader subbuffer with the one we put in its place in the
1391 * writer subbuffer table. Expect the original consumed count. If
1392 * update_read_sb_index fails, this is because the writer updated the
1393 * subbuffer concurrently. We should therefore keep the subbuffer we
1394 * currently have: it has become invalid to try reading this sub-buffer
1395 * consumed count value anyway.
1397 consumed_idx
= subbuf_index(consumed
, chan
);
1398 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1399 consumed_idx
, buf_trunc_val(consumed
, chan
));
1401 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1402 * if the writer concurrently updated it.
1405 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1408 * cons_offset is an iterator on all subbuffer offsets between the reader
1409 * position and the writer position. (inclusive)
1412 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1413 struct channel
*chan
,
1414 unsigned long cons_offset
,
1417 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1418 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1420 cons_idx
= subbuf_index(cons_offset
, chan
);
1421 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1422 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1424 if (subbuf_offset(commit_count
, chan
) != 0)
1426 "ring buffer %s, cpu %d: "
1427 "commit count in subbuffer %lu,\n"
1428 "expecting multiples of %lu bytes\n"
1429 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1430 chan
->backend
.name
, cpu
, cons_idx
,
1431 chan
->backend
.subbuf_size
,
1432 commit_count
, commit_count_sb
);
1434 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1435 chan
->backend
.name
, cpu
, commit_count
);
1439 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1440 struct channel
*chan
,
1441 void *priv
, int cpu
)
1443 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1444 unsigned long write_offset
, cons_offset
;
1447 * No need to order commit_count, write_offset and cons_offset reads
1448 * because we execute at teardown when no more writer nor reader
1449 * references are left.
1451 write_offset
= v_read(config
, &buf
->offset
);
1452 cons_offset
= atomic_long_read(&buf
->consumed
);
1453 if (write_offset
!= cons_offset
)
1455 "ring buffer %s, cpu %d: "
1456 "non-consumed data\n"
1457 " [ %lu bytes written, %lu bytes read ]\n",
1458 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1460 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1461 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1464 cons_offset
= subbuf_align(cons_offset
, chan
))
1465 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1470 void lib_ring_buffer_print_errors(struct channel
*chan
,
1471 struct lib_ring_buffer
*buf
, int cpu
)
1473 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1474 void *priv
= chan
->backend
.priv
;
1476 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1477 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1478 "%lu records overrun\n",
1480 v_read(config
, &buf
->records_count
),
1481 v_read(config
, &buf
->records_overrun
));
1483 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1484 "%lu records overrun\n",
1485 chan
->backend
.name
, cpu
,
1486 v_read(config
, &buf
->records_count
),
1487 v_read(config
, &buf
->records_overrun
));
1489 if (v_read(config
, &buf
->records_lost_full
)
1490 || v_read(config
, &buf
->records_lost_wrap
)
1491 || v_read(config
, &buf
->records_lost_big
))
1493 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1494 " [ %lu buffer full, %lu nest buffer wrap-around, "
1495 "%lu event too big ]\n",
1496 chan
->backend
.name
, cpu
,
1497 v_read(config
, &buf
->records_lost_full
),
1498 v_read(config
, &buf
->records_lost_wrap
),
1499 v_read(config
, &buf
->records_lost_big
));
1501 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1505 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1507 * Only executed by SWITCH_FLUSH, which can be issued while tracing is active
1508 * or at buffer finalization (destroy).
1511 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1512 struct channel
*chan
,
1513 struct switch_offsets
*offsets
,
1516 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1517 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1518 unsigned long commit_count
;
1519 struct commit_counters_hot
*cc_hot
;
1521 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1524 * Order all writes to buffer before the commit count update that will
1525 * determine that the subbuffer is full.
1527 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1529 * Must write slot data before incrementing commit count. This
1530 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1536 cc_hot
= &buf
->commit_hot
[oldidx
];
1537 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1538 commit_count
= v_read(config
, &cc_hot
->cc
);
1539 /* Check if the written buffer has to be delivered */
1540 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1541 commit_count
, oldidx
, tsc
);
1542 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1543 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1544 commit_count
, cc_hot
);
1548 * lib_ring_buffer_switch_old_end: switch old subbuffer
1550 * Note : offset_old should never be 0 here. It is ok, because we never perform
1551 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1552 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1556 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1557 struct channel
*chan
,
1558 struct switch_offsets
*offsets
,
1561 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1562 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1563 unsigned long commit_count
, padding_size
, data_size
;
1564 struct commit_counters_hot
*cc_hot
;
1566 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1567 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1568 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1571 * Order all writes to buffer before the commit count update that will
1572 * determine that the subbuffer is full.
1574 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1576 * Must write slot data before incrementing commit count. This
1577 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1583 cc_hot
= &buf
->commit_hot
[oldidx
];
1584 v_add(config
, padding_size
, &cc_hot
->cc
);
1585 commit_count
= v_read(config
, &cc_hot
->cc
);
1586 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1587 commit_count
, oldidx
, tsc
);
1588 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1589 offsets
->old
+ padding_size
, commit_count
,
1594 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1596 * This code can be executed unordered : writers may already have written to the
1597 * sub-buffer before this code gets executed, caution. The commit makes sure
1598 * that this code is executed before the deliver of this sub-buffer.
1601 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1602 struct channel
*chan
,
1603 struct switch_offsets
*offsets
,
1606 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1607 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1608 unsigned long commit_count
;
1609 struct commit_counters_hot
*cc_hot
;
1611 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1614 * Order all writes to buffer before the commit count update that will
1615 * determine that the subbuffer is full.
1617 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1619 * Must write slot data before incrementing commit count. This
1620 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1626 cc_hot
= &buf
->commit_hot
[beginidx
];
1627 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1628 commit_count
= v_read(config
, &cc_hot
->cc
);
1629 /* Check if the written buffer has to be delivered */
1630 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1631 commit_count
, beginidx
, tsc
);
1632 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1633 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1634 commit_count
, cc_hot
);
1638 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1640 * Calls subbuffer_set_data_size() to set the data size of the current
1641 * sub-buffer. We do not need to perform check_deliver nor commit here,
1642 * since this task will be done by the "commit" of the event for which
1643 * we are currently doing the space reservation.
1646 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1647 struct channel
*chan
,
1648 struct switch_offsets
*offsets
,
1651 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1652 unsigned long endidx
, data_size
;
1654 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1655 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1656 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1662 * !0 if execution must be aborted.
1665 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1666 struct lib_ring_buffer
*buf
,
1667 struct channel
*chan
,
1668 struct switch_offsets
*offsets
,
1671 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1672 unsigned long off
, reserve_commit_diff
;
1674 offsets
->begin
= v_read(config
, &buf
->offset
);
1675 offsets
->old
= offsets
->begin
;
1676 offsets
->switch_old_start
= 0;
1677 off
= subbuf_offset(offsets
->begin
, chan
);
1679 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1682 * Ensure we flush the header of an empty subbuffer when doing the
1683 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1684 * total data gathering duration even if there were no records saved
1685 * after the last buffer switch.
1686 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1687 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1688 * subbuffer header as appropriate.
1689 * The next record that reserves space will be responsible for
1690 * populating the following subbuffer header. We choose not to populate
1691 * the next subbuffer header here because we want to be able to use
1692 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1693 * buffer flush, which must guarantee that all the buffer content
1694 * (records and header timestamps) are visible to the reader. This is
1695 * required for quiescence guarantees for the fusion merge.
1697 if (mode
!= SWITCH_FLUSH
&& !off
)
1698 return -1; /* we do not have to switch : buffer is empty */
1700 if (unlikely(off
== 0)) {
1701 unsigned long sb_index
, commit_count
;
1704 * We are performing a SWITCH_FLUSH. There may be concurrent
1705 * writes into the buffer if e.g. invoked while performing a
1706 * snapshot on an active trace.
1708 * If the client does not save any header information (sub-buffer
1709 * header size == 0), don't switch empty subbuffer on finalize,
1710 * because it is invalid to deliver a completely empty
1713 if (!config
->cb
.subbuffer_header_size())
1716 /* Test new buffer integrity */
1717 sb_index
= subbuf_index(offsets
->begin
, chan
);
1718 commit_count
= v_read(config
,
1719 &buf
->commit_cold
[sb_index
].cc_sb
);
1720 reserve_commit_diff
=
1721 (buf_trunc(offsets
->begin
, chan
)
1722 >> chan
->backend
.num_subbuf_order
)
1723 - (commit_count
& chan
->commit_count_mask
);
1724 if (likely(reserve_commit_diff
== 0)) {
1725 /* Next subbuffer not being written to. */
1726 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1727 subbuf_trunc(offsets
->begin
, chan
)
1728 - subbuf_trunc((unsigned long)
1729 atomic_long_read(&buf
->consumed
), chan
)
1730 >= chan
->backend
.buf_size
)) {
1732 * We do not overwrite non consumed buffers
1733 * and we are full : don't switch.
1738 * Next subbuffer not being written to, and we
1739 * are either in overwrite mode or the buffer is
1740 * not full. It's safe to write in this new
1746 * Next subbuffer reserve offset does not match the
1747 * commit offset. Don't perform switch in
1748 * producer-consumer and overwrite mode. Caused by
1749 * either a writer OOPS or too many nested writes over a
1750 * reserve/commit pair.
1756 * Need to write the subbuffer start header on finalize.
1758 offsets
->switch_old_start
= 1;
1760 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1761 /* Note: old points to the next subbuf at offset 0 */
1762 offsets
->end
= offsets
->begin
;
1767 * Force a sub-buffer switch. This operation is completely reentrant : can be
1768 * called while tracing is active with absolutely no lock held.
1770 * Note, however, that as a v_cmpxchg is used for some atomic
1771 * operations, this function must be called from the CPU which owns the buffer
1772 * for a ACTIVE flush.
1774 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1776 struct channel
*chan
= buf
->backend
.chan
;
1777 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1778 struct switch_offsets offsets
;
1779 unsigned long oldidx
;
1785 * Perform retryable operations.
1788 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1790 return; /* Switch not needed */
1791 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1795 * Atomically update last_tsc. This update races against concurrent
1796 * atomic updates, but the race will always cause supplementary full TSC
1797 * records, never the opposite (missing a full TSC record when it would
1800 save_last_tsc(config
, buf
, tsc
);
1803 * Push the reader if necessary
1805 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1807 oldidx
= subbuf_index(offsets
.old
, chan
);
1808 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1811 * May need to populate header start on SWITCH_FLUSH.
1813 if (offsets
.switch_old_start
) {
1814 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1815 offsets
.old
+= config
->cb
.subbuffer_header_size();
1819 * Switch old subbuffer.
1821 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1823 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1825 struct switch_param
{
1826 struct lib_ring_buffer
*buf
;
1827 enum switch_mode mode
;
1830 static void remote_switch(void *info
)
1832 struct switch_param
*param
= info
;
1833 struct lib_ring_buffer
*buf
= param
->buf
;
1835 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1838 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1839 enum switch_mode mode
)
1841 struct channel
*chan
= buf
->backend
.chan
;
1842 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1844 struct switch_param param
;
1847 * With global synchronization we don't need to use the IPI scheme.
1849 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1850 lib_ring_buffer_switch_slow(buf
, mode
);
1855 * Taking lock on CPU hotplug to ensure two things: first, that the
1856 * target cpu is not taken concurrently offline while we are within
1857 * smp_call_function_single() (I don't trust that get_cpu() on the
1858 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1859 * confirmed)). Secondly, if it happens that the CPU is not online, our
1860 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1861 * CPU hotplug handlers, which can also perform a remote subbuffer
1867 ret
= smp_call_function_single(buf
->backend
.cpu
,
1868 remote_switch
, ¶m
, 1);
1870 /* Remote CPU is offline, do it ourself. */
1871 lib_ring_buffer_switch_slow(buf
, mode
);
1876 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1878 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1880 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1885 * -ENOSPC if event size is too large for packet.
1886 * -ENOBUFS if there is currently not enough space in buffer for the event.
1887 * -EIO if data cannot be written into the buffer for any other reason.
1890 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1891 struct channel
*chan
,
1892 struct switch_offsets
*offsets
,
1893 struct lib_ring_buffer_ctx
*ctx
)
1895 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1896 unsigned long reserve_commit_diff
, offset_cmp
;
1899 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1900 offsets
->old
= offsets
->begin
;
1901 offsets
->switch_new_start
= 0;
1902 offsets
->switch_new_end
= 0;
1903 offsets
->switch_old_end
= 0;
1904 offsets
->pre_header_padding
= 0;
1906 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1907 if ((int64_t) ctx
->tsc
== -EIO
)
1910 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1911 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1913 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1914 offsets
->switch_new_start
= 1; /* For offsets->begin */
1916 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1918 &offsets
->pre_header_padding
,
1921 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1924 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1925 offsets
->size
> chan
->backend
.subbuf_size
)) {
1926 offsets
->switch_old_end
= 1; /* For offsets->old */
1927 offsets
->switch_new_start
= 1; /* For offsets->begin */
1930 if (unlikely(offsets
->switch_new_start
)) {
1931 unsigned long sb_index
, commit_count
;
1934 * We are typically not filling the previous buffer completely.
1936 if (likely(offsets
->switch_old_end
))
1937 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1938 offsets
->begin
= offsets
->begin
1939 + config
->cb
.subbuffer_header_size();
1940 /* Test new buffer integrity */
1941 sb_index
= subbuf_index(offsets
->begin
, chan
);
1943 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1944 * lib_ring_buffer_check_deliver() has the matching
1945 * memory barriers required around commit_cold cc_sb
1946 * updates to ensure reserve and commit counter updates
1947 * are not seen reordered when updated by another CPU.
1950 commit_count
= v_read(config
,
1951 &buf
->commit_cold
[sb_index
].cc_sb
);
1952 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1954 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1956 * The reserve counter have been concurrently updated
1957 * while we read the commit counter. This means the
1958 * commit counter we read might not match buf->offset
1959 * due to concurrent update. We therefore need to retry.
1963 reserve_commit_diff
=
1964 (buf_trunc(offsets
->begin
, chan
)
1965 >> chan
->backend
.num_subbuf_order
)
1966 - (commit_count
& chan
->commit_count_mask
);
1967 if (likely(reserve_commit_diff
== 0)) {
1968 /* Next subbuffer not being written to. */
1969 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1970 subbuf_trunc(offsets
->begin
, chan
)
1971 - subbuf_trunc((unsigned long)
1972 atomic_long_read(&buf
->consumed
), chan
)
1973 >= chan
->backend
.buf_size
)) {
1975 * We do not overwrite non consumed buffers
1976 * and we are full : record is lost.
1978 v_inc(config
, &buf
->records_lost_full
);
1982 * Next subbuffer not being written to, and we
1983 * are either in overwrite mode or the buffer is
1984 * not full. It's safe to write in this new
1990 * Next subbuffer reserve offset does not match the
1991 * commit offset, and this did not involve update to the
1992 * reserve counter. Drop record in producer-consumer and
1993 * overwrite mode. Caused by either a writer OOPS or
1994 * too many nested writes over a reserve/commit pair.
1996 v_inc(config
, &buf
->records_lost_wrap
);
2000 config
->cb
.record_header_size(config
, chan
,
2002 &offsets
->pre_header_padding
,
2005 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2008 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2009 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2011 * Record too big for subbuffers, report error, don't
2012 * complete the sub-buffer switch.
2014 v_inc(config
, &buf
->records_lost_big
);
2018 * We just made a successful buffer switch and the
2019 * record fits in the new subbuffer. Let's write.
2024 * Record fits in the current buffer and we are not on a switch
2025 * boundary. It's safe to write.
2028 offsets
->end
= offsets
->begin
+ offsets
->size
;
2030 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2032 * The offset_end will fall at the very beginning of the next
2035 offsets
->switch_new_end
= 1; /* For offsets->begin */
2040 static struct lib_ring_buffer
*get_current_buf(struct channel
*chan
, int cpu
)
2042 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2044 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2045 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2047 return chan
->backend
.buf
;
2050 void lib_ring_buffer_lost_event_too_big(struct channel
*chan
)
2052 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2053 struct lib_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2055 v_inc(config
, &buf
->records_lost_big
);
2057 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2060 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2061 * @ctx: ring buffer context.
2063 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2064 * -EIO for other errors, else returns 0.
2065 * It will take care of sub-buffer switching.
2067 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
2069 struct channel
*chan
= ctx
->chan
;
2070 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2071 struct lib_ring_buffer
*buf
;
2072 struct switch_offsets offsets
;
2075 ctx
->buf
= buf
= get_current_buf(chan
, ctx
->cpu
);
2079 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2083 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2088 * Atomically update last_tsc. This update races against concurrent
2089 * atomic updates, but the race will always cause supplementary full TSC
2090 * records, never the opposite (missing a full TSC record when it would
2093 save_last_tsc(config
, buf
, ctx
->tsc
);
2096 * Push the reader if necessary
2098 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2101 * Clear noref flag for this subbuffer.
2103 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2104 subbuf_index(offsets
.end
- 1, chan
));
2107 * Switch old subbuffer if needed.
2109 if (unlikely(offsets
.switch_old_end
)) {
2110 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2111 subbuf_index(offsets
.old
- 1, chan
));
2112 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
2116 * Populate new subbuffer.
2118 if (unlikely(offsets
.switch_new_start
))
2119 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
2121 if (unlikely(offsets
.switch_new_end
))
2122 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
2124 ctx
->slot_size
= offsets
.size
;
2125 ctx
->pre_offset
= offsets
.begin
;
2126 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2129 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2132 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config
*config
,
2133 struct lib_ring_buffer
*buf
,
2134 unsigned long commit_count
,
2137 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2138 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2142 * The ring buffer can count events recorded and overwritten per buffer,
2143 * but it is disabled by default due to its performance overhead.
2145 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2147 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2148 struct lib_ring_buffer
*buf
,
2151 v_add(config
, subbuffer_get_records_count(config
,
2152 &buf
->backend
, idx
),
2153 &buf
->records_count
);
2154 v_add(config
, subbuffer_count_records_overrun(config
,
2155 &buf
->backend
, idx
),
2156 &buf
->records_overrun
);
2158 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2160 void deliver_count_events(const struct lib_ring_buffer_config
*config
,
2161 struct lib_ring_buffer
*buf
,
2165 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2168 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
2169 struct lib_ring_buffer
*buf
,
2170 struct channel
*chan
,
2171 unsigned long offset
,
2172 unsigned long commit_count
,
2176 unsigned long old_commit_count
= commit_count
2177 - chan
->backend
.subbuf_size
;
2180 * If we succeeded at updating cc_sb below, we are the subbuffer
2181 * writer delivering the subbuffer. Deals with concurrent
2182 * updates of the "cc" value without adding a add_return atomic
2183 * operation to the fast path.
2185 * We are doing the delivery in two steps:
2186 * - First, we cmpxchg() cc_sb to the new value
2187 * old_commit_count + 1. This ensures that we are the only
2188 * subbuffer user successfully filling the subbuffer, but we
2189 * do _not_ set the cc_sb value to "commit_count" yet.
2190 * Therefore, other writers that would wrap around the ring
2191 * buffer and try to start writing to our subbuffer would
2192 * have to drop records, because it would appear as
2194 * We therefore have exclusive access to the subbuffer control
2195 * structures. This mutual exclusion with other writers is
2196 * crucially important to perform record overruns count in
2197 * flight recorder mode locklessly.
2198 * - When we are ready to release the subbuffer (either for
2199 * reading or for overrun by other writers), we simply set the
2200 * cc_sb value to "commit_count" and perform delivery.
2202 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2203 * This guarantees that old_commit_count + 1 != commit_count.
2207 * Order prior updates to reserve count prior to the
2208 * commit_cold cc_sb update.
2211 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2212 old_commit_count
, old_commit_count
+ 1)
2213 == old_commit_count
)) {
2215 * Start of exclusive subbuffer access. We are
2216 * guaranteed to be the last writer in this subbuffer
2217 * and any other writer trying to access this subbuffer
2218 * in this state is required to drop records.
2220 deliver_count_events(config
, buf
, idx
);
2221 config
->cb
.buffer_end(buf
, tsc
, idx
,
2222 lib_ring_buffer_get_data_size(config
,
2227 * Increment the packet counter while we have exclusive
2230 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2233 * Set noref flag and offset for this subbuffer id.
2234 * Contains a memory barrier that ensures counter stores
2235 * are ordered before set noref and offset.
2237 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2238 buf_trunc_val(offset
, chan
));
2241 * Order set_noref and record counter updates before the
2242 * end of subbuffer exclusive access. Orders with
2243 * respect to writers coming into the subbuffer after
2244 * wrap around, and also order wrt concurrent readers.
2247 /* End of exclusive subbuffer access */
2248 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2251 * Order later updates to reserve count after
2252 * the commit_cold cc_sb update.
2255 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2259 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2261 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2262 && atomic_long_read(&buf
->active_readers
)
2263 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2264 wake_up_interruptible(&buf
->read_wait
);
2265 wake_up_interruptible(&chan
->read_wait
);
2270 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2272 int __init
init_lib_ring_buffer_frontend(void)
2276 for_each_possible_cpu(cpu
)
2277 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2281 module_init(init_lib_ring_buffer_frontend
);
2283 void __exit
exit_lib_ring_buffer_frontend(void)
2287 module_exit(exit_lib_ring_buffer_frontend
);