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 <asm/cacheflush.h>
45 #include <ringbuffer/config.h>
46 #include <ringbuffer/backend.h>
47 #include <ringbuffer/frontend.h>
48 #include <ringbuffer/iterator.h>
49 #include <ringbuffer/nohz.h>
50 #include <wrapper/atomic.h>
51 #include <wrapper/cpu.h>
52 #include <wrapper/kref.h>
53 #include <wrapper/percpu-defs.h>
54 #include <wrapper/timer.h>
55 #include <wrapper/vmalloc.h>
58 * Internal structure representing offsets to use at a sub-buffer switch.
60 struct switch_offsets
{
61 unsigned long begin
, end
, old
;
62 size_t pre_header_padding
, size
;
63 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
74 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
75 #endif /* CONFIG_NO_HZ */
77 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
79 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
80 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
83 void lib_ring_buffer_print_errors(struct lttng_kernel_ring_buffer_channel
*chan
,
84 struct lttng_kernel_ring_buffer
*buf
, int cpu
);
86 void _lib_ring_buffer_switch_remote(struct lttng_kernel_ring_buffer
*buf
,
87 enum switch_mode mode
);
90 int lib_ring_buffer_poll_deliver(const struct lttng_kernel_ring_buffer_config
*config
,
91 struct lttng_kernel_ring_buffer
*buf
,
92 struct lttng_kernel_ring_buffer_channel
*chan
)
94 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
96 consumed_old
= atomic_long_read(&buf
->consumed
);
97 consumed_idx
= subbuf_index(consumed_old
, chan
);
98 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
100 * No memory barrier here, since we are only interested
101 * in a statistically correct polling result. The next poll will
102 * get the data is we are racing. The mb() that ensures correct
103 * memory order is in get_subbuf.
105 write_offset
= v_read(config
, &buf
->offset
);
108 * Check that the subbuffer we are trying to consume has been
109 * already fully committed.
112 if (((commit_count
- chan
->backend
.subbuf_size
)
113 & chan
->commit_count_mask
)
114 - (buf_trunc(consumed_old
, chan
)
115 >> chan
->backend
.num_subbuf_order
)
120 * Check that we are not about to read the same subbuffer in
121 * which the writer head is.
123 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
131 * Must be called under cpu hotplug protection.
133 void lib_ring_buffer_free(struct lttng_kernel_ring_buffer
*buf
)
135 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
137 irq_work_sync(&buf
->wakeup_pending
);
139 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
140 lttng_kvfree(buf
->commit_hot
);
141 lttng_kvfree(buf
->commit_cold
);
142 lttng_kvfree(buf
->ts_end
);
144 lib_ring_buffer_backend_free(&buf
->backend
);
148 * lib_ring_buffer_reset - Reset ring buffer to initial values.
151 * Effectively empty the ring buffer. Should be called when the buffer is not
152 * used for writing. The ring buffer can be opened for reading, but the reader
153 * should not be using the iterator concurrently with reset. The previous
154 * current iterator record is reset.
156 void lib_ring_buffer_reset(struct lttng_kernel_ring_buffer
*buf
)
158 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
159 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
163 * Reset iterator first. It will put the subbuffer if it currently holds
166 lib_ring_buffer_iterator_reset(buf
);
167 v_set(config
, &buf
->offset
, 0);
168 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
169 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
170 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
171 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
174 atomic_long_set(&buf
->consumed
, 0);
175 atomic_set(&buf
->record_disabled
, 0);
176 v_set(config
, &buf
->last_tsc
, 0);
177 lib_ring_buffer_backend_reset(&buf
->backend
);
178 /* Don't reset number of active readers */
179 v_set(config
, &buf
->records_lost_full
, 0);
180 v_set(config
, &buf
->records_lost_wrap
, 0);
181 v_set(config
, &buf
->records_lost_big
, 0);
182 v_set(config
, &buf
->records_count
, 0);
183 v_set(config
, &buf
->records_overrun
, 0);
186 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
189 * channel_reset - Reset channel to initial values.
192 * Effectively empty the channel. Should be called when the channel is not used
193 * for writing. The channel can be opened for reading, but the reader should not
194 * be using the iterator concurrently with reset. The previous current iterator
197 void channel_reset(struct lttng_kernel_ring_buffer_channel
*chan
)
200 * Reset iterators first. Will put the subbuffer if held for reading.
202 channel_iterator_reset(chan
);
203 atomic_set(&chan
->record_disabled
, 0);
204 /* Don't reset commit_count_mask, still valid */
205 channel_backend_reset(&chan
->backend
);
206 /* Don't reset switch/read timer interval */
207 /* Don't reset notifiers and notifier enable bits */
208 /* Don't reset reader reference count */
210 EXPORT_SYMBOL_GPL(channel_reset
);
212 static void lib_ring_buffer_pending_wakeup_buf(struct irq_work
*entry
)
214 struct lttng_kernel_ring_buffer
*buf
= container_of(entry
, struct lttng_kernel_ring_buffer
,
216 wake_up_interruptible(&buf
->read_wait
);
219 static void lib_ring_buffer_pending_wakeup_chan(struct irq_work
*entry
)
221 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(entry
, struct lttng_kernel_ring_buffer_channel
, wakeup_pending
);
222 wake_up_interruptible(&chan
->read_wait
);
226 * Must be called under cpu hotplug protection.
228 int lib_ring_buffer_create(struct lttng_kernel_ring_buffer
*buf
,
229 struct channel_backend
*chanb
, int cpu
)
231 const struct lttng_kernel_ring_buffer_config
*config
= &chanb
->config
;
232 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(chanb
, struct lttng_kernel_ring_buffer_channel
, backend
);
233 void *priv
= chanb
->priv
;
234 size_t subbuf_header_size
;
238 /* Test for cpu hotplug */
239 if (buf
->backend
.allocated
)
243 * Paranoia: per cpu dynamic allocation is not officially documented as
244 * zeroing the memory, so let's do it here too, just in case.
246 memset(buf
, 0, sizeof(*buf
));
248 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
253 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
254 * chan
->backend
.num_subbuf
,
255 1 << INTERNODE_CACHE_SHIFT
),
256 GFP_KERNEL
| __GFP_NOWARN
,
257 cpu_to_node(max(cpu
, 0)));
258 if (!buf
->commit_hot
) {
264 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
265 * chan
->backend
.num_subbuf
,
266 1 << INTERNODE_CACHE_SHIFT
),
267 GFP_KERNEL
| __GFP_NOWARN
,
268 cpu_to_node(max(cpu
, 0)));
269 if (!buf
->commit_cold
) {
275 lttng_kvzalloc_node(ALIGN(sizeof(*buf
->ts_end
)
276 * chan
->backend
.num_subbuf
,
277 1 << INTERNODE_CACHE_SHIFT
),
278 GFP_KERNEL
| __GFP_NOWARN
,
279 cpu_to_node(max(cpu
, 0)));
282 goto free_commit_cold
;
285 init_waitqueue_head(&buf
->read_wait
);
286 init_waitqueue_head(&buf
->write_wait
);
287 init_irq_work(&buf
->wakeup_pending
, lib_ring_buffer_pending_wakeup_buf
);
288 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
291 * Write the subbuffer header for first subbuffer so we know the total
292 * duration of data gathering.
294 subbuf_header_size
= config
->cb
.subbuffer_header_size();
295 v_set(config
, &buf
->offset
, subbuf_header_size
);
296 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
297 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
298 config
->cb
.buffer_begin(buf
, tsc
, 0);
299 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
301 if (config
->cb
.buffer_create
) {
302 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
308 * Ensure the buffer is ready before setting it to allocated and setting
310 * Used for cpu hotplug vs cpumask iteration.
313 buf
->backend
.allocated
= 1;
315 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
316 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
317 chan
->backend
.cpumask
));
318 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
325 lttng_kvfree(buf
->ts_end
);
327 lttng_kvfree(buf
->commit_cold
);
329 lttng_kvfree(buf
->commit_hot
);
331 lib_ring_buffer_backend_free(&buf
->backend
);
335 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
337 struct lttng_kernel_ring_buffer
*buf
= lttng_from_timer(buf
, t
, switch_timer
);
338 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
339 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
342 * Only flush buffers periodically if readers are active.
344 if (atomic_long_read(&buf
->active_readers
))
345 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
347 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
348 lttng_mod_timer_pinned(&buf
->switch_timer
,
349 jiffies
+ chan
->switch_timer_interval
);
351 mod_timer(&buf
->switch_timer
,
352 jiffies
+ chan
->switch_timer_interval
);
356 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
358 static void lib_ring_buffer_start_switch_timer(struct lttng_kernel_ring_buffer
*buf
)
360 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
361 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
362 unsigned int flags
= 0;
364 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
367 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
368 flags
= LTTNG_TIMER_PINNED
;
370 lttng_timer_setup(&buf
->switch_timer
, switch_buffer_timer
, flags
, buf
);
371 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
373 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
374 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
376 add_timer(&buf
->switch_timer
);
378 buf
->switch_timer_enabled
= 1;
382 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
384 static void lib_ring_buffer_stop_switch_timer(struct lttng_kernel_ring_buffer
*buf
)
386 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
388 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
391 del_timer_sync(&buf
->switch_timer
);
392 buf
->switch_timer_enabled
= 0;
396 * Polling timer to check the channels for data.
398 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t
)
400 struct lttng_kernel_ring_buffer
*buf
= lttng_from_timer(buf
, t
, read_timer
);
401 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
402 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
404 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
406 if (atomic_long_read(&buf
->active_readers
)
407 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
408 wake_up_interruptible(&buf
->read_wait
);
409 wake_up_interruptible(&chan
->read_wait
);
412 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
413 lttng_mod_timer_pinned(&buf
->read_timer
,
414 jiffies
+ chan
->read_timer_interval
);
416 mod_timer(&buf
->read_timer
,
417 jiffies
+ chan
->read_timer_interval
);
421 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
423 static void lib_ring_buffer_start_read_timer(struct lttng_kernel_ring_buffer
*buf
)
425 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
426 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
427 unsigned int flags
= 0;
429 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
430 || !chan
->read_timer_interval
431 || buf
->read_timer_enabled
)
434 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
435 flags
= LTTNG_TIMER_PINNED
;
437 lttng_timer_setup(&buf
->read_timer
, read_buffer_timer
, flags
, buf
);
438 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
440 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
441 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
443 add_timer(&buf
->read_timer
);
445 buf
->read_timer_enabled
= 1;
449 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
451 static void lib_ring_buffer_stop_read_timer(struct lttng_kernel_ring_buffer
*buf
)
453 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
454 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
456 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
457 || !chan
->read_timer_interval
458 || !buf
->read_timer_enabled
)
461 del_timer_sync(&buf
->read_timer
);
463 * do one more check to catch data that has been written in the last
466 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
467 wake_up_interruptible(&buf
->read_wait
);
468 wake_up_interruptible(&chan
->read_wait
);
470 buf
->read_timer_enabled
= 0;
473 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
475 enum cpuhp_state lttng_rb_hp_prepare
;
476 enum cpuhp_state lttng_rb_hp_online
;
478 void lttng_rb_set_hp_prepare(enum cpuhp_state val
)
480 lttng_rb_hp_prepare
= val
;
482 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare
);
484 void lttng_rb_set_hp_online(enum cpuhp_state val
)
486 lttng_rb_hp_online
= val
;
488 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online
);
490 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu
,
491 struct lttng_cpuhp_node
*node
)
493 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(node
, struct lttng_kernel_ring_buffer_channel
,
495 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
496 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
498 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
501 * Performing a buffer switch on a remote CPU. Performed by
502 * the CPU responsible for doing the hotunplug after the target
503 * CPU stopped running completely. Ensures that all data
504 * from that remote CPU is flushed.
506 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
509 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead
);
511 int lttng_cpuhp_rb_frontend_online(unsigned int cpu
,
512 struct lttng_cpuhp_node
*node
)
514 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(node
, struct lttng_kernel_ring_buffer_channel
,
516 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
517 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
519 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
521 wake_up_interruptible(&chan
->hp_wait
);
522 lib_ring_buffer_start_switch_timer(buf
);
523 lib_ring_buffer_start_read_timer(buf
);
526 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online
);
528 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu
,
529 struct lttng_cpuhp_node
*node
)
531 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(node
, struct lttng_kernel_ring_buffer_channel
,
533 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
534 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
536 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
538 lib_ring_buffer_stop_switch_timer(buf
);
539 lib_ring_buffer_stop_read_timer(buf
);
542 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline
);
544 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
546 #ifdef CONFIG_HOTPLUG_CPU
549 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
550 * @nb: notifier block
551 * @action: hotplug action to take
554 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
557 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
558 unsigned long action
,
561 unsigned int cpu
= (unsigned long)hcpu
;
562 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(nb
, struct lttng_kernel_ring_buffer_channel
,
564 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
565 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
567 if (!chan
->cpu_hp_enable
)
570 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
573 case CPU_DOWN_FAILED
:
574 case CPU_DOWN_FAILED_FROZEN
:
576 case CPU_ONLINE_FROZEN
:
577 wake_up_interruptible(&chan
->hp_wait
);
578 lib_ring_buffer_start_switch_timer(buf
);
579 lib_ring_buffer_start_read_timer(buf
);
582 case CPU_DOWN_PREPARE
:
583 case CPU_DOWN_PREPARE_FROZEN
:
584 lib_ring_buffer_stop_switch_timer(buf
);
585 lib_ring_buffer_stop_read_timer(buf
);
589 case CPU_DEAD_FROZEN
:
591 * Performing a buffer switch on a remote CPU. Performed by
592 * the CPU responsible for doing the hotunplug after the target
593 * CPU stopped running completely. Ensures that all data
594 * from that remote CPU is flushed.
596 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
606 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
608 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
610 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
611 * that wake-up-tracing generated events are flushed before going idle (in
612 * tick_nohz). We test if the spinlock is locked to deal with the race where
613 * readers try to sample the ring buffer before we perform the switch. We let
614 * the readers retry in that case. If there is data in the buffer, the wake up
615 * is going to forbid the CPU running the reader thread from going idle.
617 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
621 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(nb
, struct lttng_kernel_ring_buffer_channel
,
623 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
624 struct lttng_kernel_ring_buffer
*buf
;
625 int cpu
= smp_processor_id();
627 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
629 * We don't support keeping the system idle with global buffers
630 * and streaming active. In order to do so, we would need to
631 * sample a non-nohz-cpumask racelessly with the nohz updates
632 * without adding synchronization overhead to nohz. Leave this
633 * use-case out for now.
638 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
640 case TICK_NOHZ_FLUSH
:
641 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
642 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
643 && chan
->read_timer_interval
644 && atomic_long_read(&buf
->active_readers
)
645 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
646 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
647 wake_up_interruptible(&buf
->read_wait
);
648 wake_up_interruptible(&chan
->read_wait
);
650 if (chan
->switch_timer_interval
)
651 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
652 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
655 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
656 lib_ring_buffer_stop_switch_timer(buf
);
657 lib_ring_buffer_stop_read_timer(buf
);
658 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
660 case TICK_NOHZ_RESTART
:
661 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
662 lib_ring_buffer_start_read_timer(buf
);
663 lib_ring_buffer_start_switch_timer(buf
);
664 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
671 void notrace
lib_ring_buffer_tick_nohz_flush(void)
673 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
677 void notrace
lib_ring_buffer_tick_nohz_stop(void)
679 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
683 void notrace
lib_ring_buffer_tick_nohz_restart(void)
685 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
688 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
693 static void channel_unregister_notifiers(struct lttng_kernel_ring_buffer_channel
*chan
)
695 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
697 channel_iterator_unregister_notifiers(chan
);
698 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
701 * Remove the nohz notifier first, so we are certain we stop
704 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
705 &chan
->tick_nohz_notifier
);
707 * ring_buffer_nohz_lock will not be needed below, because
708 * we just removed the notifiers, which were the only source of
711 #endif /* CONFIG_NO_HZ */
712 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
716 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
717 &chan
->cpuhp_online
.node
);
719 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
720 &chan
->cpuhp_prepare
.node
);
723 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
727 #ifdef CONFIG_HOTPLUG_CPU
728 lttng_cpus_read_lock();
729 chan
->cpu_hp_enable
= 0;
730 for_each_online_cpu(cpu
) {
731 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
733 lib_ring_buffer_stop_switch_timer(buf
);
734 lib_ring_buffer_stop_read_timer(buf
);
736 lttng_cpus_read_unlock();
737 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
739 for_each_possible_cpu(cpu
) {
740 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
742 lib_ring_buffer_stop_switch_timer(buf
);
743 lib_ring_buffer_stop_read_timer(buf
);
747 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
749 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
751 lib_ring_buffer_stop_switch_timer(buf
);
752 lib_ring_buffer_stop_read_timer(buf
);
754 channel_backend_unregister_notifiers(&chan
->backend
);
757 static void lib_ring_buffer_set_quiescent(struct lttng_kernel_ring_buffer
*buf
)
759 if (!buf
->quiescent
) {
760 buf
->quiescent
= true;
761 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
765 static void lib_ring_buffer_clear_quiescent(struct lttng_kernel_ring_buffer
*buf
)
767 buf
->quiescent
= false;
770 void lib_ring_buffer_set_quiescent_channel(struct lttng_kernel_ring_buffer_channel
*chan
)
773 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
775 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
776 lttng_cpus_read_lock();
777 for_each_channel_cpu(cpu
, chan
) {
778 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
781 lib_ring_buffer_set_quiescent(buf
);
783 lttng_cpus_read_unlock();
785 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
787 lib_ring_buffer_set_quiescent(buf
);
790 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
792 void lib_ring_buffer_clear_quiescent_channel(struct lttng_kernel_ring_buffer_channel
*chan
)
795 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
797 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
798 lttng_cpus_read_lock();
799 for_each_channel_cpu(cpu
, chan
) {
800 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
803 lib_ring_buffer_clear_quiescent(buf
);
805 lttng_cpus_read_unlock();
807 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
809 lib_ring_buffer_clear_quiescent(buf
);
812 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
814 static void channel_free(struct lttng_kernel_ring_buffer_channel
*chan
)
816 if (chan
->backend
.release_priv_ops
) {
817 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
819 channel_iterator_free(chan
);
820 channel_backend_free(&chan
->backend
);
825 * channel_create - Create channel.
826 * @config: ring buffer instance configuration
827 * @name: name of the channel
828 * @priv: ring buffer client private data
829 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
830 * address mapping. It is used only by RING_BUFFER_STATIC
831 * configuration. It can be set to NULL for other backends.
832 * @subbuf_size: subbuffer size
833 * @num_subbuf: number of subbuffers
834 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
835 * padding to let readers get those sub-buffers.
836 * Used for live streaming.
837 * @read_timer_interval: Time interval (in us) to wake up pending readers.
840 * Returns NULL on failure.
842 struct lttng_kernel_ring_buffer_channel
*channel_create(const struct lttng_kernel_ring_buffer_config
*config
,
843 const char *name
, void *priv
, void *buf_addr
,
845 size_t num_subbuf
, unsigned int switch_timer_interval
,
846 unsigned int read_timer_interval
)
849 struct lttng_kernel_ring_buffer_channel
*chan
;
851 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
852 read_timer_interval
))
855 chan
= kzalloc(sizeof(struct lttng_kernel_ring_buffer_channel
), GFP_KERNEL
);
859 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
860 subbuf_size
, num_subbuf
);
864 ret
= channel_iterator_init(chan
);
866 goto error_free_backend
;
868 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
869 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
870 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
871 kref_init(&chan
->ref
);
872 init_waitqueue_head(&chan
->read_wait
);
873 init_waitqueue_head(&chan
->hp_wait
);
874 init_irq_work(&chan
->wakeup_pending
, lib_ring_buffer_pending_wakeup_chan
);
876 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
877 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
878 chan
->cpuhp_prepare
.component
= LTTNG_RING_BUFFER_FRONTEND
;
879 ret
= cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare
,
880 &chan
->cpuhp_prepare
.node
);
882 goto cpuhp_prepare_error
;
884 chan
->cpuhp_online
.component
= LTTNG_RING_BUFFER_FRONTEND
;
885 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
886 &chan
->cpuhp_online
.node
);
888 goto cpuhp_online_error
;
889 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
893 * In case of non-hotplug cpu, if the ring-buffer is allocated
894 * in early initcall, it will not be notified of secondary cpus.
895 * In that off case, we need to allocate for all possible cpus.
897 #ifdef CONFIG_HOTPLUG_CPU
898 chan
->cpu_hp_notifier
.notifier_call
=
899 lib_ring_buffer_cpu_hp_callback
;
900 chan
->cpu_hp_notifier
.priority
= 6;
901 register_cpu_notifier(&chan
->cpu_hp_notifier
);
903 lttng_cpus_read_lock();
904 for_each_online_cpu(cpu
) {
905 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
907 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
908 lib_ring_buffer_start_switch_timer(buf
);
909 lib_ring_buffer_start_read_timer(buf
);
910 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
912 chan
->cpu_hp_enable
= 1;
913 lttng_cpus_read_unlock();
915 for_each_possible_cpu(cpu
) {
916 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
918 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
919 lib_ring_buffer_start_switch_timer(buf
);
920 lib_ring_buffer_start_read_timer(buf
);
921 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
925 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
927 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
928 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
929 chan
->tick_nohz_notifier
.notifier_call
=
930 ring_buffer_tick_nohz_callback
;
931 chan
->tick_nohz_notifier
.priority
= ~0U;
932 atomic_notifier_chain_register(&tick_nohz_notifier
,
933 &chan
->tick_nohz_notifier
);
934 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
937 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
939 lib_ring_buffer_start_switch_timer(buf
);
940 lib_ring_buffer_start_read_timer(buf
);
945 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
947 ret
= cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare
,
948 &chan
->cpuhp_prepare
.node
);
951 #endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
953 channel_backend_free(&chan
->backend
);
958 EXPORT_SYMBOL_GPL(channel_create
);
961 void channel_release(struct kref
*kref
)
963 struct lttng_kernel_ring_buffer_channel
*chan
= container_of(kref
, struct lttng_kernel_ring_buffer_channel
, ref
);
968 * channel_destroy - Finalize, wait for q.s. and destroy channel.
969 * @chan: channel to destroy
972 * Call "destroy" callback, finalize channels, and then decrement the
973 * channel reference count. Note that when readers have completed data
974 * consumption of finalized channels, get_subbuf() will return -ENODATA.
975 * They should release their handle at that point. Returns the private
978 void *channel_destroy(struct lttng_kernel_ring_buffer_channel
*chan
)
981 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
984 irq_work_sync(&chan
->wakeup_pending
);
986 channel_unregister_notifiers(chan
);
988 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
990 * No need to hold cpu hotplug, because all notifiers have been
993 for_each_channel_cpu(cpu
, chan
) {
994 struct lttng_kernel_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
997 if (config
->cb
.buffer_finalize
)
998 config
->cb
.buffer_finalize(buf
,
1002 * Perform flush before writing to finalized.
1005 WRITE_ONCE(buf
->finalized
, 1);
1006 wake_up_interruptible(&buf
->read_wait
);
1009 struct lttng_kernel_ring_buffer
*buf
= chan
->backend
.buf
;
1011 if (config
->cb
.buffer_finalize
)
1012 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
1014 * Perform flush before writing to finalized.
1017 WRITE_ONCE(buf
->finalized
, 1);
1018 wake_up_interruptible(&buf
->read_wait
);
1020 WRITE_ONCE(chan
->finalized
, 1);
1021 wake_up_interruptible(&chan
->hp_wait
);
1022 wake_up_interruptible(&chan
->read_wait
);
1023 priv
= chan
->backend
.priv
;
1024 kref_put(&chan
->ref
, channel_release
);
1027 EXPORT_SYMBOL_GPL(channel_destroy
);
1029 struct lttng_kernel_ring_buffer
*channel_get_ring_buffer(
1030 const struct lttng_kernel_ring_buffer_config
*config
,
1031 struct lttng_kernel_ring_buffer_channel
*chan
, int cpu
)
1033 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
1034 return chan
->backend
.buf
;
1036 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
1038 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
1040 int lib_ring_buffer_open_read(struct lttng_kernel_ring_buffer
*buf
)
1042 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1044 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
1046 if (!lttng_kref_get(&chan
->ref
)) {
1047 atomic_long_dec(&buf
->active_readers
);
1050 lttng_smp_mb__after_atomic();
1053 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
1055 void lib_ring_buffer_release_read(struct lttng_kernel_ring_buffer
*buf
)
1057 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1059 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1060 lttng_smp_mb__before_atomic();
1061 atomic_long_dec(&buf
->active_readers
);
1062 kref_put(&chan
->ref
, channel_release
);
1064 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
1067 * Promote compiler barrier to a smp_mb().
1068 * For the specific ring buffer case, this IPI call should be removed if the
1069 * architecture does not reorder writes. This should eventually be provided by
1070 * a separate architecture-specific infrastructure.
1072 static void remote_mb(void *info
)
1078 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1080 * @consumed: consumed count indicating the position where to read
1081 * @produced: produced count, indicates position when to stop reading
1083 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1084 * data to read at consumed position, or 0 if the get operation succeeds.
1085 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1088 int lib_ring_buffer_snapshot(struct lttng_kernel_ring_buffer
*buf
,
1089 unsigned long *consumed
, unsigned long *produced
)
1091 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1092 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1093 unsigned long consumed_cur
, write_offset
;
1097 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1099 * Read finalized before counters.
1102 consumed_cur
= atomic_long_read(&buf
->consumed
);
1104 * No need to issue a memory barrier between consumed count read and
1105 * write offset read, because consumed count can only change
1106 * concurrently in overwrite mode, and we keep a sequence counter
1107 * identifier derived from the write offset to check we are getting
1108 * the same sub-buffer we are expecting (the sub-buffers are atomically
1109 * "tagged" upon writes, tags are checked upon read).
1111 write_offset
= v_read(config
, &buf
->offset
);
1114 * Check that we are not about to read the same subbuffer in
1115 * which the writer head is.
1117 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1121 *consumed
= consumed_cur
;
1122 *produced
= subbuf_trunc(write_offset
, chan
);
1128 * The memory barriers __wait_event()/wake_up_interruptible() take care
1129 * of "raw_spin_is_locked" memory ordering.
1133 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1138 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
1141 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1142 * are saved regardless of whether the consumed and produced positions are
1143 * in the same subbuffer.
1145 * @consumed: consumed byte count indicating the last position read
1146 * @produced: produced byte count indicating the last position written
1148 * This function is meant to provide information on the exact producer and
1149 * consumer positions without regard for the "snapshot" feature.
1151 int lib_ring_buffer_snapshot_sample_positions(struct lttng_kernel_ring_buffer
*buf
,
1152 unsigned long *consumed
, unsigned long *produced
)
1154 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1155 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1158 *consumed
= atomic_long_read(&buf
->consumed
);
1160 * No need to issue a memory barrier between consumed count read and
1161 * write offset read, because consumed count can only change
1162 * concurrently in overwrite mode, and we keep a sequence counter
1163 * identifier derived from the write offset to check we are getting
1164 * the same sub-buffer we are expecting (the sub-buffers are atomically
1165 * "tagged" upon writes, tags are checked upon read).
1167 *produced
= v_read(config
, &buf
->offset
);
1172 * lib_ring_buffer_put_snapshot - move consumed counter forward
1174 * Should only be called from consumer context.
1176 * @consumed_new: new consumed count value
1178 void lib_ring_buffer_move_consumer(struct lttng_kernel_ring_buffer
*buf
,
1179 unsigned long consumed_new
)
1181 struct lttng_kernel_ring_buffer_backend
*bufb
= &buf
->backend
;
1182 struct lttng_kernel_ring_buffer_channel
*chan
= bufb
->chan
;
1183 unsigned long consumed
;
1185 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1188 * Only push the consumed value forward.
1189 * If the consumed cmpxchg fails, this is because we have been pushed by
1190 * the writer in flight recorder mode.
1192 consumed
= atomic_long_read(&buf
->consumed
);
1193 while ((long) consumed
- (long) consumed_new
< 0)
1194 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
1196 /* Wake-up the metadata producer */
1197 wake_up_interruptible(&buf
->write_wait
);
1199 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
1201 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1202 static void lib_ring_buffer_flush_read_subbuf_dcache(
1203 const struct lttng_kernel_ring_buffer_config
*config
,
1204 struct lttng_kernel_ring_buffer_channel
*chan
,
1205 struct lttng_kernel_ring_buffer
*buf
)
1207 struct lttng_kernel_ring_buffer_backend_pages
*pages
;
1208 unsigned long sb_bindex
, id
, i
, nr_pages
;
1210 if (config
->output
!= RING_BUFFER_MMAP
)
1214 * Architectures with caches aliased on virtual addresses may
1215 * use different cache lines for the linear mapping vs
1216 * user-space memory mapping. Given that the ring buffer is
1217 * based on the kernel linear mapping, aligning it with the
1218 * user-space mapping is not straightforward, and would require
1219 * extra TLB entries. Therefore, simply flush the dcache for the
1220 * entire sub-buffer before reading it.
1222 id
= buf
->backend
.buf_rsb
.id
;
1223 sb_bindex
= subbuffer_id_get_index(config
, id
);
1224 pages
= buf
->backend
.array
[sb_bindex
];
1225 nr_pages
= buf
->backend
.num_pages_per_subbuf
;
1226 for (i
= 0; i
< nr_pages
; i
++) {
1227 struct lttng_kernel_ring_buffer_backend_page
*backend_page
;
1229 backend_page
= &pages
->p
[i
];
1230 flush_dcache_page(pfn_to_page(backend_page
->pfn
));
1234 static void lib_ring_buffer_flush_read_subbuf_dcache(
1235 const struct lttng_kernel_ring_buffer_config
*config
,
1236 struct lttng_kernel_ring_buffer_channel
*chan
,
1237 struct lttng_kernel_ring_buffer
*buf
)
1243 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1245 * @consumed: consumed count indicating the position where to read
1247 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1248 * data to read at consumed position, or 0 if the get operation succeeds.
1249 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1251 int lib_ring_buffer_get_subbuf(struct lttng_kernel_ring_buffer
*buf
,
1252 unsigned long consumed
)
1254 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1255 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1256 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1260 if (buf
->get_subbuf
) {
1262 * Reader is trying to get a subbuffer twice.
1264 CHAN_WARN_ON(chan
, 1);
1268 finalized
= LTTNG_READ_ONCE(buf
->finalized
);
1270 * Read finalized before counters.
1273 consumed_cur
= atomic_long_read(&buf
->consumed
);
1274 consumed_idx
= subbuf_index(consumed
, chan
);
1275 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1277 * Make sure we read the commit count before reading the buffer
1278 * data and the write offset. Correct consumed offset ordering
1279 * wrt commit count is insured by the use of cmpxchg to update
1280 * the consumed offset.
1281 * smp_call_function_single can fail if the remote CPU is offline,
1282 * this is OK because then there is no wmb to execute there.
1283 * If our thread is executing on the same CPU as the on the buffers
1284 * belongs to, we don't have to synchronize it at all. If we are
1285 * migrated, the scheduler will take care of the memory barriers.
1286 * Normally, smp_call_function_single() should ensure program order when
1287 * executing the remote function, which implies that it surrounds the
1288 * function execution with :
1299 * However, smp_call_function_single() does not seem to clearly execute
1300 * such barriers. It depends on spinlock semantic to provide the barrier
1301 * before executing the IPI and, when busy-looping, csd_lock_wait only
1302 * executes smp_mb() when it has to wait for the other CPU.
1304 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1305 * required ourself, even if duplicated. It has no performance impact
1308 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1309 * read and write vs write. They do not ensure core synchronization. We
1310 * really have to ensure total order between the 3 barriers running on
1313 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1314 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1315 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1316 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1317 /* Total order with IPI handler smp_mb() */
1319 smp_call_function_single(buf
->backend
.cpu
,
1320 remote_mb
, NULL
, 1);
1321 /* Total order with IPI handler smp_mb() */
1325 /* Total order with IPI handler smp_mb() */
1327 smp_call_function(remote_mb
, NULL
, 1);
1328 /* Total order with IPI handler smp_mb() */
1333 * Local rmb to match the remote wmb to read the commit count
1334 * before the buffer data and the write offset.
1339 write_offset
= v_read(config
, &buf
->offset
);
1342 * Check that the buffer we are getting is after or at consumed_cur
1345 if ((long) subbuf_trunc(consumed
, chan
)
1346 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1350 * Check that the subbuffer we are trying to consume has been
1351 * already fully committed.
1353 if (((commit_count
- chan
->backend
.subbuf_size
)
1354 & chan
->commit_count_mask
)
1355 - (buf_trunc(consumed
, chan
)
1356 >> chan
->backend
.num_subbuf_order
)
1361 * Check that we are not about to read the same subbuffer in
1362 * which the writer head is.
1364 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1369 * Failure to get the subbuffer causes a busy-loop retry without going
1370 * to a wait queue. These are caused by short-lived race windows where
1371 * the writer is getting access to a subbuffer we were trying to get
1372 * access to. Also checks that the "consumed" buffer count we are
1373 * looking for matches the one contained in the subbuffer id.
1375 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1376 consumed_idx
, buf_trunc_val(consumed
, chan
));
1379 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1381 buf
->get_subbuf_consumed
= consumed
;
1382 buf
->get_subbuf
= 1;
1384 lib_ring_buffer_flush_read_subbuf_dcache(config
, chan
, buf
);
1390 * The memory barriers __wait_event()/wake_up_interruptible() take care
1391 * of "raw_spin_is_locked" memory ordering.
1395 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1400 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1403 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1406 void lib_ring_buffer_put_subbuf(struct lttng_kernel_ring_buffer
*buf
)
1408 struct lttng_kernel_ring_buffer_backend
*bufb
= &buf
->backend
;
1409 struct lttng_kernel_ring_buffer_channel
*chan
= bufb
->chan
;
1410 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1411 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1413 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1415 if (!buf
->get_subbuf
) {
1417 * Reader puts a subbuffer it did not get.
1419 CHAN_WARN_ON(chan
, 1);
1422 consumed
= buf
->get_subbuf_consumed
;
1423 buf
->get_subbuf
= 0;
1426 * Clear the records_unread counter. (overruns counter)
1427 * Can still be non-zero if a file reader simply grabbed the data
1428 * without using iterators.
1429 * Can be below zero if an iterator is used on a snapshot more than
1432 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1433 v_add(config
, v_read(config
,
1434 &bufb
->array
[read_sb_bindex
]->records_unread
),
1435 &bufb
->records_read
);
1436 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1437 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1438 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1439 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1442 * Exchange the reader subbuffer with the one we put in its place in the
1443 * writer subbuffer table. Expect the original consumed count. If
1444 * update_read_sb_index fails, this is because the writer updated the
1445 * subbuffer concurrently. We should therefore keep the subbuffer we
1446 * currently have: it has become invalid to try reading this sub-buffer
1447 * consumed count value anyway.
1449 consumed_idx
= subbuf_index(consumed
, chan
);
1450 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1451 consumed_idx
, buf_trunc_val(consumed
, chan
));
1453 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1454 * if the writer concurrently updated it.
1457 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1460 * cons_offset is an iterator on all subbuffer offsets between the reader
1461 * position and the writer position. (inclusive)
1464 void lib_ring_buffer_print_subbuffer_errors(struct lttng_kernel_ring_buffer
*buf
,
1465 struct lttng_kernel_ring_buffer_channel
*chan
,
1466 unsigned long cons_offset
,
1469 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1470 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1472 cons_idx
= subbuf_index(cons_offset
, chan
);
1473 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1474 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1476 if (subbuf_offset(commit_count
, chan
) != 0)
1478 "LTTng: ring buffer %s, cpu %d: "
1479 "commit count in subbuffer %lu,\n"
1480 "expecting multiples of %lu bytes\n"
1481 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1482 chan
->backend
.name
, cpu
, cons_idx
,
1483 chan
->backend
.subbuf_size
,
1484 commit_count
, commit_count_sb
);
1486 printk(KERN_DEBUG
"LTTng: ring buffer: %s, cpu %d: %lu bytes committed\n",
1487 chan
->backend
.name
, cpu
, commit_count
);
1491 void lib_ring_buffer_print_buffer_errors(struct lttng_kernel_ring_buffer
*buf
,
1492 struct lttng_kernel_ring_buffer_channel
*chan
,
1493 void *priv
, int cpu
)
1495 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1496 unsigned long write_offset
, cons_offset
;
1499 * No need to order commit_count, write_offset and cons_offset reads
1500 * because we execute at teardown when no more writer nor reader
1501 * references are left.
1503 write_offset
= v_read(config
, &buf
->offset
);
1504 cons_offset
= atomic_long_read(&buf
->consumed
);
1505 if (write_offset
!= cons_offset
)
1507 "LTTng: ring buffer %s, cpu %d: "
1508 "non-consumed data\n"
1509 " [ %lu bytes written, %lu bytes read ]\n",
1510 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1512 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1513 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1516 cons_offset
= subbuf_align(cons_offset
, chan
))
1517 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1521 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1523 void lib_ring_buffer_print_records_count(struct lttng_kernel_ring_buffer_channel
*chan
,
1524 struct lttng_kernel_ring_buffer
*buf
,
1527 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1529 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1530 printk(KERN_DEBUG
"LTTng: ring buffer %s: %lu records written, "
1531 "%lu records overrun\n",
1533 v_read(config
, &buf
->records_count
),
1534 v_read(config
, &buf
->records_overrun
));
1536 printk(KERN_DEBUG
"LTTng: ring buffer %s, cpu %d: %lu records written, "
1537 "%lu records overrun\n",
1538 chan
->backend
.name
, cpu
,
1539 v_read(config
, &buf
->records_count
),
1540 v_read(config
, &buf
->records_overrun
));
1545 void lib_ring_buffer_print_records_count(struct lttng_kernel_ring_buffer_channel
*chan
,
1546 struct lttng_kernel_ring_buffer
*buf
,
1553 void lib_ring_buffer_print_errors(struct lttng_kernel_ring_buffer_channel
*chan
,
1554 struct lttng_kernel_ring_buffer
*buf
, int cpu
)
1556 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1557 void *priv
= chan
->backend
.priv
;
1559 lib_ring_buffer_print_records_count(chan
, buf
, cpu
);
1560 if (strcmp(chan
->backend
.name
, "relay-metadata")) {
1561 if (v_read(config
, &buf
->records_lost_full
)
1562 || v_read(config
, &buf
->records_lost_wrap
)
1563 || v_read(config
, &buf
->records_lost_big
))
1565 "LTTng: ring buffer %s, cpu %d: records were lost. Caused by:\n"
1566 " [ %lu buffer full, %lu nest buffer wrap-around, "
1567 "%lu event too big ]\n",
1568 chan
->backend
.name
, cpu
,
1569 v_read(config
, &buf
->records_lost_full
),
1570 v_read(config
, &buf
->records_lost_wrap
),
1571 v_read(config
, &buf
->records_lost_big
));
1573 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1577 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1579 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1582 void lib_ring_buffer_switch_old_start(struct lttng_kernel_ring_buffer
*buf
,
1583 struct lttng_kernel_ring_buffer_channel
*chan
,
1584 struct switch_offsets
*offsets
,
1585 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1587 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1588 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1589 unsigned long commit_count
;
1590 struct commit_counters_hot
*cc_hot
;
1592 config
->cb
.buffer_begin(buf
, ctx
->priv
.tsc
, oldidx
);
1595 * Order all writes to buffer before the commit count update that will
1596 * determine that the subbuffer is full.
1598 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1600 * Must write slot data before incrementing commit count. This
1601 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1607 cc_hot
= &buf
->commit_hot
[oldidx
];
1608 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1609 commit_count
= v_read(config
, &cc_hot
->cc
);
1610 /* Check if the written buffer has to be delivered */
1611 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1612 commit_count
, oldidx
, ctx
);
1613 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1614 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1615 commit_count
, cc_hot
);
1619 * lib_ring_buffer_switch_old_end: switch old subbuffer
1621 * Note : offset_old should never be 0 here. It is ok, because we never perform
1622 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1623 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1627 void lib_ring_buffer_switch_old_end(struct lttng_kernel_ring_buffer
*buf
,
1628 struct lttng_kernel_ring_buffer_channel
*chan
,
1629 struct switch_offsets
*offsets
,
1630 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1632 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1633 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1634 unsigned long commit_count
, padding_size
, data_size
;
1635 struct commit_counters_hot
*cc_hot
;
1638 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1639 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1640 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1642 ts_end
= &buf
->ts_end
[oldidx
];
1644 * This is the last space reservation in that sub-buffer before
1645 * it gets delivered. This provides exclusive access to write to
1646 * this sub-buffer's ts_end. There are also no concurrent
1647 * readers of that ts_end because delivery of that sub-buffer is
1648 * postponed until the commit counter is incremented for the
1649 * current space reservation.
1651 *ts_end
= ctx
->priv
.tsc
;
1654 * Order all writes to buffer and store to ts_end before the commit
1655 * count update that will determine that the subbuffer is full.
1657 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1659 * Must write slot data before incrementing commit count. This
1660 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1666 cc_hot
= &buf
->commit_hot
[oldidx
];
1667 v_add(config
, padding_size
, &cc_hot
->cc
);
1668 commit_count
= v_read(config
, &cc_hot
->cc
);
1669 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1670 commit_count
, oldidx
, ctx
);
1671 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1672 offsets
->old
+ padding_size
, commit_count
,
1677 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1679 * This code can be executed unordered : writers may already have written to the
1680 * sub-buffer before this code gets executed, caution. The commit makes sure
1681 * that this code is executed before the deliver of this sub-buffer.
1684 void lib_ring_buffer_switch_new_start(struct lttng_kernel_ring_buffer
*buf
,
1685 struct lttng_kernel_ring_buffer_channel
*chan
,
1686 struct switch_offsets
*offsets
,
1687 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1689 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1690 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1691 unsigned long commit_count
;
1692 struct commit_counters_hot
*cc_hot
;
1694 config
->cb
.buffer_begin(buf
, ctx
->priv
.tsc
, beginidx
);
1697 * Order all writes to buffer before the commit count update that will
1698 * determine that the subbuffer is full.
1700 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1702 * Must write slot data before incrementing commit count. This
1703 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1709 cc_hot
= &buf
->commit_hot
[beginidx
];
1710 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1711 commit_count
= v_read(config
, &cc_hot
->cc
);
1712 /* Check if the written buffer has to be delivered */
1713 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1714 commit_count
, beginidx
, ctx
);
1715 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1716 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1717 commit_count
, cc_hot
);
1721 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1723 * Calls subbuffer_set_data_size() to set the data size of the current
1724 * sub-buffer. We do not need to perform check_deliver nor commit here,
1725 * since this task will be done by the "commit" of the event for which
1726 * we are currently doing the space reservation.
1729 void lib_ring_buffer_switch_new_end(struct lttng_kernel_ring_buffer
*buf
,
1730 struct lttng_kernel_ring_buffer_channel
*chan
,
1731 struct switch_offsets
*offsets
,
1732 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
1734 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1735 unsigned long endidx
, data_size
;
1738 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1739 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1740 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1741 ts_end
= &buf
->ts_end
[endidx
];
1743 * This is the last space reservation in that sub-buffer before
1744 * it gets delivered. This provides exclusive access to write to
1745 * this sub-buffer's ts_end. There are also no concurrent
1746 * readers of that ts_end because delivery of that sub-buffer is
1747 * postponed until the commit counter is incremented for the
1748 * current space reservation.
1750 *ts_end
= ctx
->priv
.tsc
;
1756 * !0 if execution must be aborted.
1759 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1760 struct lttng_kernel_ring_buffer
*buf
,
1761 struct lttng_kernel_ring_buffer_channel
*chan
,
1762 struct switch_offsets
*offsets
,
1763 struct lttng_kernel_ring_buffer_ctx
*ctx
)
1765 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1766 unsigned long off
, reserve_commit_diff
;
1768 offsets
->begin
= v_read(config
, &buf
->offset
);
1769 offsets
->old
= offsets
->begin
;
1770 offsets
->switch_old_start
= 0;
1771 off
= subbuf_offset(offsets
->begin
, chan
);
1773 ctx
->priv
.tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1776 * Ensure we flush the header of an empty subbuffer when doing the
1777 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1778 * total data gathering duration even if there were no records saved
1779 * after the last buffer switch.
1780 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1781 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1782 * subbuffer header as appropriate.
1783 * The next record that reserves space will be responsible for
1784 * populating the following subbuffer header. We choose not to populate
1785 * the next subbuffer header here because we want to be able to use
1786 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1787 * buffer flush, which must guarantee that all the buffer content
1788 * (records and header timestamps) are visible to the reader. This is
1789 * required for quiescence guarantees for the fusion merge.
1791 if (mode
!= SWITCH_FLUSH
&& !off
)
1792 return -1; /* we do not have to switch : buffer is empty */
1794 if (unlikely(off
== 0)) {
1795 unsigned long sb_index
, commit_count
;
1798 * We are performing a SWITCH_FLUSH. At this stage, there are no
1799 * concurrent writes into the buffer.
1801 * The client does not save any header information. Don't
1802 * switch empty subbuffer on finalize, because it is invalid to
1803 * deliver a completely empty subbuffer.
1805 if (!config
->cb
.subbuffer_header_size())
1808 /* Test new buffer integrity */
1809 sb_index
= subbuf_index(offsets
->begin
, chan
);
1810 commit_count
= v_read(config
,
1811 &buf
->commit_cold
[sb_index
].cc_sb
);
1812 reserve_commit_diff
=
1813 (buf_trunc(offsets
->begin
, chan
)
1814 >> chan
->backend
.num_subbuf_order
)
1815 - (commit_count
& chan
->commit_count_mask
);
1816 if (likely(reserve_commit_diff
== 0)) {
1817 /* Next subbuffer not being written to. */
1818 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1819 subbuf_trunc(offsets
->begin
, chan
)
1820 - subbuf_trunc((unsigned long)
1821 atomic_long_read(&buf
->consumed
), chan
)
1822 >= chan
->backend
.buf_size
)) {
1824 * We do not overwrite non consumed buffers
1825 * and we are full : don't switch.
1830 * Next subbuffer not being written to, and we
1831 * are either in overwrite mode or the buffer is
1832 * not full. It's safe to write in this new
1838 * Next subbuffer reserve offset does not match the
1839 * commit offset. Don't perform switch in
1840 * producer-consumer and overwrite mode. Caused by
1841 * either a writer OOPS or too many nested writes over a
1842 * reserve/commit pair.
1848 * Need to write the subbuffer start header on finalize.
1850 offsets
->switch_old_start
= 1;
1852 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1853 /* Note: old points to the next subbuf at offset 0 */
1854 offsets
->end
= offsets
->begin
;
1856 * Populate the records lost counters prior to performing a
1857 * sub-buffer switch.
1859 ctx
->priv
.records_lost_full
= v_read(config
, &buf
->records_lost_full
);
1860 ctx
->priv
.records_lost_wrap
= v_read(config
, &buf
->records_lost_wrap
);
1861 ctx
->priv
.records_lost_big
= v_read(config
, &buf
->records_lost_big
);
1866 * Force a sub-buffer switch. This operation is completely reentrant : can be
1867 * called while tracing is active with absolutely no lock held.
1869 * Note, however, that as a v_cmpxchg is used for some atomic
1870 * operations, this function must be called from the CPU which owns the buffer
1871 * for a ACTIVE flush.
1873 void lib_ring_buffer_switch_slow(struct lttng_kernel_ring_buffer
*buf
, enum switch_mode mode
)
1875 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1876 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1877 struct lttng_kernel_ring_buffer_ctx ctx
;
1878 struct switch_offsets offsets
;
1879 unsigned long oldidx
;
1884 * Perform retryable operations.
1887 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1889 return; /* Switch not needed */
1890 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1894 * Atomically update last_tsc. This update races against concurrent
1895 * atomic updates, but the race will always cause supplementary full TSC
1896 * records, never the opposite (missing a full TSC record when it would
1899 save_last_tsc(config
, buf
, ctx
.priv
.tsc
);
1902 * Push the reader if necessary
1904 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1906 oldidx
= subbuf_index(offsets
.old
, chan
);
1907 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1910 * May need to populate header start on SWITCH_FLUSH.
1912 if (offsets
.switch_old_start
) {
1913 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, &ctx
);
1914 offsets
.old
+= config
->cb
.subbuffer_header_size();
1918 * Switch old subbuffer.
1920 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, &ctx
);
1922 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1924 struct switch_param
{
1925 struct lttng_kernel_ring_buffer
*buf
;
1926 enum switch_mode mode
;
1929 static void remote_switch(void *info
)
1931 struct switch_param
*param
= info
;
1932 struct lttng_kernel_ring_buffer
*buf
= param
->buf
;
1934 lib_ring_buffer_switch_slow(buf
, param
->mode
);
1937 static void _lib_ring_buffer_switch_remote(struct lttng_kernel_ring_buffer
*buf
,
1938 enum switch_mode mode
)
1940 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
1941 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
1943 struct switch_param param
;
1946 * With global synchronization we don't need to use the IPI scheme.
1948 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1949 lib_ring_buffer_switch_slow(buf
, mode
);
1954 * Disabling preemption ensures two things: first, that the
1955 * target cpu is not taken concurrently offline while we are within
1956 * smp_call_function_single(). Secondly, if it happens that the
1957 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1958 * needs to be protected from CPU hotplug handlers, which can
1959 * also perform a remote subbuffer switch.
1964 ret
= smp_call_function_single(buf
->backend
.cpu
,
1965 remote_switch
, ¶m
, 1);
1967 /* Remote CPU is offline, do it ourself. */
1968 lib_ring_buffer_switch_slow(buf
, mode
);
1973 /* Switch sub-buffer if current sub-buffer is non-empty. */
1974 void lib_ring_buffer_switch_remote(struct lttng_kernel_ring_buffer
*buf
)
1976 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1978 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1980 /* Switch sub-buffer even if current sub-buffer is empty. */
1981 void lib_ring_buffer_switch_remote_empty(struct lttng_kernel_ring_buffer
*buf
)
1983 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1985 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1987 void lib_ring_buffer_clear(struct lttng_kernel_ring_buffer
*buf
)
1989 struct lttng_kernel_ring_buffer_backend
*bufb
= &buf
->backend
;
1990 struct lttng_kernel_ring_buffer_channel
*chan
= bufb
->chan
;
1992 lib_ring_buffer_switch_remote(buf
);
1993 lib_ring_buffer_clear_reader(buf
, chan
);
1995 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear
);
2000 * -ENOSPC if event size is too large for packet.
2001 * -ENOBUFS if there is currently not enough space in buffer for the event.
2002 * -EIO if data cannot be written into the buffer for any other reason.
2005 int lib_ring_buffer_try_reserve_slow(struct lttng_kernel_ring_buffer
*buf
,
2006 struct lttng_kernel_ring_buffer_channel
*chan
,
2007 struct switch_offsets
*offsets
,
2008 struct lttng_kernel_ring_buffer_ctx
*ctx
,
2011 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2012 unsigned long reserve_commit_diff
, offset_cmp
;
2015 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2016 offsets
->old
= offsets
->begin
;
2017 offsets
->switch_new_start
= 0;
2018 offsets
->switch_new_end
= 0;
2019 offsets
->switch_old_end
= 0;
2020 offsets
->pre_header_padding
= 0;
2022 ctx
->priv
.tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2023 if ((int64_t) ctx
->priv
.tsc
== -EIO
)
2026 if (last_tsc_overflow(config
, buf
, ctx
->priv
.tsc
))
2027 ctx
->priv
.rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2029 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->priv
.chan
) == 0)) {
2030 offsets
->switch_new_start
= 1; /* For offsets->begin */
2032 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2034 &offsets
->pre_header_padding
,
2037 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2040 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
2041 offsets
->size
> chan
->backend
.subbuf_size
)) {
2042 offsets
->switch_old_end
= 1; /* For offsets->old */
2043 offsets
->switch_new_start
= 1; /* For offsets->begin */
2046 if (unlikely(offsets
->switch_new_start
)) {
2047 unsigned long sb_index
, commit_count
;
2050 * We are typically not filling the previous buffer completely.
2052 if (likely(offsets
->switch_old_end
))
2053 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2054 offsets
->begin
= offsets
->begin
2055 + config
->cb
.subbuffer_header_size();
2056 /* Test new buffer integrity */
2057 sb_index
= subbuf_index(offsets
->begin
, chan
);
2059 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2060 * lib_ring_buffer_check_deliver() has the matching
2061 * memory barriers required around commit_cold cc_sb
2062 * updates to ensure reserve and commit counter updates
2063 * are not seen reordered when updated by another CPU.
2066 commit_count
= v_read(config
,
2067 &buf
->commit_cold
[sb_index
].cc_sb
);
2068 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2070 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2072 * The reserve counter have been concurrently updated
2073 * while we read the commit counter. This means the
2074 * commit counter we read might not match buf->offset
2075 * due to concurrent update. We therefore need to retry.
2079 reserve_commit_diff
=
2080 (buf_trunc(offsets
->begin
, chan
)
2081 >> chan
->backend
.num_subbuf_order
)
2082 - (commit_count
& chan
->commit_count_mask
);
2083 if (likely(reserve_commit_diff
== 0)) {
2084 /* Next subbuffer not being written to. */
2085 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2086 subbuf_trunc(offsets
->begin
, chan
)
2087 - subbuf_trunc((unsigned long)
2088 atomic_long_read(&buf
->consumed
), chan
)
2089 >= chan
->backend
.buf_size
)) {
2091 * We do not overwrite non consumed buffers
2092 * and we are full : record is lost.
2094 v_inc(config
, &buf
->records_lost_full
);
2098 * Next subbuffer not being written to, and we
2099 * are either in overwrite mode or the buffer is
2100 * not full. It's safe to write in this new
2106 * Next subbuffer reserve offset does not match the
2107 * commit offset, and this did not involve update to the
2108 * reserve counter. Drop record in producer-consumer and
2109 * overwrite mode. Caused by either a writer OOPS or
2110 * too many nested writes over a reserve/commit pair.
2112 v_inc(config
, &buf
->records_lost_wrap
);
2116 config
->cb
.record_header_size(config
, chan
,
2118 &offsets
->pre_header_padding
,
2121 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2124 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
2125 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2127 * Record too big for subbuffers, report error, don't
2128 * complete the sub-buffer switch.
2130 v_inc(config
, &buf
->records_lost_big
);
2134 * We just made a successful buffer switch and the
2135 * record fits in the new subbuffer. Let's write.
2140 * Record fits in the current buffer and we are not on a switch
2141 * boundary. It's safe to write.
2144 offsets
->end
= offsets
->begin
+ offsets
->size
;
2146 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2148 * The offset_end will fall at the very beginning of the next
2151 offsets
->switch_new_end
= 1; /* For offsets->begin */
2154 * Populate the records lost counters when the space reservation
2155 * may cause a sub-buffer switch.
2157 if (offsets
->switch_new_end
|| offsets
->switch_old_end
) {
2158 ctx
->priv
.records_lost_full
= v_read(config
, &buf
->records_lost_full
);
2159 ctx
->priv
.records_lost_wrap
= v_read(config
, &buf
->records_lost_wrap
);
2160 ctx
->priv
.records_lost_big
= v_read(config
, &buf
->records_lost_big
);
2165 static struct lttng_kernel_ring_buffer
*get_current_buf(struct lttng_kernel_ring_buffer_channel
*chan
, int cpu
)
2167 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2169 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2170 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
2172 return chan
->backend
.buf
;
2175 void lib_ring_buffer_lost_event_too_big(struct lttng_kernel_ring_buffer_channel
*chan
)
2177 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2178 struct lttng_kernel_ring_buffer
*buf
= get_current_buf(chan
, smp_processor_id());
2180 v_inc(config
, &buf
->records_lost_big
);
2182 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big
);
2185 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2186 * @ctx: ring buffer context.
2188 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2189 * -EIO for other errors, else returns 0.
2190 * It will take care of sub-buffer switching.
2192 int lib_ring_buffer_reserve_slow(struct lttng_kernel_ring_buffer_ctx
*ctx
,
2195 struct lttng_kernel_ring_buffer_channel
*chan
= ctx
->priv
.chan
;
2196 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
2197 struct lttng_kernel_ring_buffer
*buf
;
2198 struct switch_offsets offsets
;
2201 ctx
->priv
.buf
= buf
= get_current_buf(chan
, ctx
->priv
.reserve_cpu
);
2205 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2209 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2214 * Atomically update last_tsc. This update races against concurrent
2215 * atomic updates, but the race will always cause supplementary full TSC
2216 * records, never the opposite (missing a full TSC record when it would
2219 save_last_tsc(config
, buf
, ctx
->priv
.tsc
);
2222 * Push the reader if necessary
2224 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2227 * Clear noref flag for this subbuffer.
2229 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2230 subbuf_index(offsets
.end
- 1, chan
));
2233 * Switch old subbuffer if needed.
2235 if (unlikely(offsets
.switch_old_end
)) {
2236 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2237 subbuf_index(offsets
.old
- 1, chan
));
2238 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
);
2242 * Populate new subbuffer.
2244 if (unlikely(offsets
.switch_new_start
))
2245 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
);
2247 if (unlikely(offsets
.switch_new_end
))
2248 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
);
2250 ctx
->priv
.slot_size
= offsets
.size
;
2251 ctx
->priv
.pre_offset
= offsets
.begin
;
2252 ctx
->priv
.buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2255 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
2258 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_kernel_ring_buffer_config
*config
,
2259 struct lttng_kernel_ring_buffer
*buf
,
2260 unsigned long commit_count
,
2263 if (config
->oops
== RING_BUFFER_OOPS_CONSISTENCY
)
2264 v_set(config
, &buf
->commit_hot
[idx
].seq
, commit_count
);
2268 * The ring buffer can count events recorded and overwritten per buffer,
2269 * but it is disabled by default due to its performance overhead.
2271 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2273 void deliver_count_events(const struct lttng_kernel_ring_buffer_config
*config
,
2274 struct lttng_kernel_ring_buffer
*buf
,
2277 v_add(config
, subbuffer_get_records_count(config
,
2278 &buf
->backend
, idx
),
2279 &buf
->records_count
);
2280 v_add(config
, subbuffer_count_records_overrun(config
,
2281 &buf
->backend
, idx
),
2282 &buf
->records_overrun
);
2284 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2286 void deliver_count_events(const struct lttng_kernel_ring_buffer_config
*config
,
2287 struct lttng_kernel_ring_buffer
*buf
,
2291 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2294 void lib_ring_buffer_check_deliver_slow(const struct lttng_kernel_ring_buffer_config
*config
,
2295 struct lttng_kernel_ring_buffer
*buf
,
2296 struct lttng_kernel_ring_buffer_channel
*chan
,
2297 unsigned long offset
,
2298 unsigned long commit_count
,
2300 const struct lttng_kernel_ring_buffer_ctx
*ctx
)
2302 unsigned long old_commit_count
= commit_count
2303 - chan
->backend
.subbuf_size
;
2306 * If we succeeded at updating cc_sb below, we are the subbuffer
2307 * writer delivering the subbuffer. Deals with concurrent
2308 * updates of the "cc" value without adding a add_return atomic
2309 * operation to the fast path.
2311 * We are doing the delivery in two steps:
2312 * - First, we cmpxchg() cc_sb to the new value
2313 * old_commit_count + 1. This ensures that we are the only
2314 * subbuffer user successfully filling the subbuffer, but we
2315 * do _not_ set the cc_sb value to "commit_count" yet.
2316 * Therefore, other writers that would wrap around the ring
2317 * buffer and try to start writing to our subbuffer would
2318 * have to drop records, because it would appear as
2320 * We therefore have exclusive access to the subbuffer control
2321 * structures. This mutual exclusion with other writers is
2322 * crucially important to perform record overruns count in
2323 * flight recorder mode locklessly.
2324 * - When we are ready to release the subbuffer (either for
2325 * reading or for overrun by other writers), we simply set the
2326 * cc_sb value to "commit_count" and perform delivery.
2328 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2329 * This guarantees that old_commit_count + 1 != commit_count.
2333 * Order prior updates to reserve count prior to the
2334 * commit_cold cc_sb update.
2337 if (likely(v_cmpxchg(config
, &buf
->commit_cold
[idx
].cc_sb
,
2338 old_commit_count
, old_commit_count
+ 1)
2339 == old_commit_count
)) {
2343 * Start of exclusive subbuffer access. We are
2344 * guaranteed to be the last writer in this subbuffer
2345 * and any other writer trying to access this subbuffer
2346 * in this state is required to drop records.
2348 * We can read the ts_end for the current sub-buffer
2349 * which has been saved by the very last space
2350 * reservation for the current sub-buffer.
2352 * Order increment of commit counter before reading ts_end.
2355 ts_end
= &buf
->ts_end
[idx
];
2356 deliver_count_events(config
, buf
, idx
);
2357 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2358 lib_ring_buffer_get_data_size(config
,
2363 * Increment the packet counter while we have exclusive
2366 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
);
2369 * Set noref flag and offset for this subbuffer id.
2370 * Contains a memory barrier that ensures counter stores
2371 * are ordered before set noref and offset.
2373 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2374 buf_trunc_val(offset
, chan
));
2377 * Order set_noref and record counter updates before the
2378 * end of subbuffer exclusive access. Orders with
2379 * respect to writers coming into the subbuffer after
2380 * wrap around, and also order wrt concurrent readers.
2383 /* End of exclusive subbuffer access */
2384 v_set(config
, &buf
->commit_cold
[idx
].cc_sb
,
2387 * Order later updates to reserve count after
2388 * the commit_cold cc_sb update.
2391 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2395 * RING_BUFFER_WAKEUP_BY_WRITER uses an irq_work to issue
2398 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2399 && atomic_long_read(&buf
->active_readers
)
2400 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
2401 irq_work_queue(&buf
->wakeup_pending
);
2402 irq_work_queue(&chan
->wakeup_pending
);
2407 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow
);
2409 int __init
init_lib_ring_buffer_frontend(void)
2413 for_each_possible_cpu(cpu
)
2414 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
2418 module_init(init_lib_ring_buffer_frontend
);
2420 void __exit
exit_lib_ring_buffer_frontend(void)
2424 module_exit(exit_lib_ring_buffer_frontend
);