f939787837c6fbb46ab501fcf75ff6c7d81c586a
[lttng-modules.git] / lib / ringbuffer / ring_buffer_frontend.c
1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
2 *
3 * ring_buffer_frontend.c
4 *
5 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
8 * recorder (overwrite) modes. See thesis:
9 *
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
13 *
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"
18 *
19 * Author:
20 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
21 *
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>
26 * And from K42 :
27 * Bob Wisniewski <bob@watson.ibm.com>
28 *
29 * Buffer reader semantic :
30 *
31 * - get_subbuf_size
32 * while buffer is not finalized and empty
33 * - get_subbuf
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
37 * - put_subbuf
38 */
39
40 #include <linux/delay.h>
41 #include <linux/module.h>
42 #include <linux/percpu.h>
43 #include <asm/cacheflush.h>
44
45 #include <wrapper/ringbuffer/config.h>
46 #include <wrapper/ringbuffer/backend.h>
47 #include <wrapper/ringbuffer/frontend.h>
48 #include <wrapper/ringbuffer/iterator.h>
49 #include <wrapper/ringbuffer/nohz.h>
50 #include <wrapper/atomic.h>
51 #include <wrapper/kref.h>
52 #include <wrapper/percpu-defs.h>
53 #include <wrapper/timer.h>
54
55 /*
56 * Internal structure representing offsets to use at a sub-buffer switch.
57 */
58 struct switch_offsets {
59 unsigned long begin, end, old;
60 size_t pre_header_padding, size;
61 unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1,
62 switch_old_end:1;
63 };
64
65 #ifdef CONFIG_NO_HZ
66 enum tick_nohz_val {
67 TICK_NOHZ_STOP,
68 TICK_NOHZ_FLUSH,
69 TICK_NOHZ_RESTART,
70 };
71
72 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier);
73 #endif /* CONFIG_NO_HZ */
74
75 static DEFINE_PER_CPU(spinlock_t, ring_buffer_nohz_lock);
76
77 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting);
78 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting);
79
80 static
81 void lib_ring_buffer_print_errors(struct channel *chan,
82 struct lib_ring_buffer *buf, int cpu);
83 static
84 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf,
85 enum switch_mode mode);
86
87 static
88 int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config *config,
89 struct lib_ring_buffer *buf,
90 struct channel *chan)
91 {
92 unsigned long consumed_old, consumed_idx, commit_count, write_offset;
93
94 consumed_old = atomic_long_read(&buf->consumed);
95 consumed_idx = subbuf_index(consumed_old, chan);
96 commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb);
97 /*
98 * No memory barrier here, since we are only interested
99 * in a statistically correct polling result. The next poll will
100 * get the data is we are racing. The mb() that ensures correct
101 * memory order is in get_subbuf.
102 */
103 write_offset = v_read(config, &buf->offset);
104
105 /*
106 * Check that the subbuffer we are trying to consume has been
107 * already fully committed.
108 */
109
110 if (((commit_count - chan->backend.subbuf_size)
111 & chan->commit_count_mask)
112 - (buf_trunc(consumed_old, chan)
113 >> chan->backend.num_subbuf_order)
114 != 0)
115 return 0;
116
117 /*
118 * Check that we are not about to read the same subbuffer in
119 * which the writer head is.
120 */
121 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_old, chan)
122 == 0)
123 return 0;
124
125 return 1;
126 }
127
128 /*
129 * Must be called under cpu hotplug protection.
130 */
131 void lib_ring_buffer_free(struct lib_ring_buffer *buf)
132 {
133 struct channel *chan = buf->backend.chan;
134
135 lib_ring_buffer_print_errors(chan, buf, buf->backend.cpu);
136 kvfree(buf->commit_hot);
137 kvfree(buf->commit_cold);
138 kvfree(buf->ts_end);
139
140 lib_ring_buffer_backend_free(&buf->backend);
141 }
142
143 /**
144 * lib_ring_buffer_reset - Reset ring buffer to initial values.
145 * @buf: Ring buffer.
146 *
147 * Effectively empty the ring buffer. Should be called when the buffer is not
148 * used for writing. The ring buffer can be opened for reading, but the reader
149 * should not be using the iterator concurrently with reset. The previous
150 * current iterator record is reset.
151 */
152 void lib_ring_buffer_reset(struct lib_ring_buffer *buf)
153 {
154 struct channel *chan = buf->backend.chan;
155 const struct lib_ring_buffer_config *config = &chan->backend.config;
156 unsigned int i;
157
158 /*
159 * Reset iterator first. It will put the subbuffer if it currently holds
160 * it.
161 */
162 lib_ring_buffer_iterator_reset(buf);
163 v_set(config, &buf->offset, 0);
164 for (i = 0; i < chan->backend.num_subbuf; i++) {
165 v_set(config, &buf->commit_hot[i].cc, 0);
166 v_set(config, &buf->commit_hot[i].seq, 0);
167 v_set(config, &buf->commit_cold[i].cc_sb, 0);
168 buf->ts_end[i] = 0;
169 }
170 atomic_long_set(&buf->consumed, 0);
171 atomic_set(&buf->record_disabled, 0);
172 v_set(config, &buf->last_tsc, 0);
173 lib_ring_buffer_backend_reset(&buf->backend);
174 /* Don't reset number of active readers */
175 v_set(config, &buf->records_lost_full, 0);
176 v_set(config, &buf->records_lost_wrap, 0);
177 v_set(config, &buf->records_lost_big, 0);
178 v_set(config, &buf->records_count, 0);
179 v_set(config, &buf->records_overrun, 0);
180 buf->finalized = 0;
181 }
182 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset);
183
184 /**
185 * channel_reset - Reset channel to initial values.
186 * @chan: Channel.
187 *
188 * Effectively empty the channel. Should be called when the channel is not used
189 * for writing. The channel can be opened for reading, but the reader should not
190 * be using the iterator concurrently with reset. The previous current iterator
191 * record is reset.
192 */
193 void channel_reset(struct channel *chan)
194 {
195 /*
196 * Reset iterators first. Will put the subbuffer if held for reading.
197 */
198 channel_iterator_reset(chan);
199 atomic_set(&chan->record_disabled, 0);
200 /* Don't reset commit_count_mask, still valid */
201 channel_backend_reset(&chan->backend);
202 /* Don't reset switch/read timer interval */
203 /* Don't reset notifiers and notifier enable bits */
204 /* Don't reset reader reference count */
205 }
206 EXPORT_SYMBOL_GPL(channel_reset);
207
208 /*
209 * Must be called under cpu hotplug protection.
210 */
211 int lib_ring_buffer_create(struct lib_ring_buffer *buf,
212 struct channel_backend *chanb, int cpu)
213 {
214 const struct lib_ring_buffer_config *config = &chanb->config;
215 struct channel *chan = container_of(chanb, struct channel, backend);
216 void *priv = chanb->priv;
217 size_t subbuf_header_size;
218 u64 tsc;
219 int ret;
220
221 /* Test for cpu hotplug */
222 if (buf->backend.allocated)
223 return 0;
224
225 /*
226 * Paranoia: per cpu dynamic allocation is not officially documented as
227 * zeroing the memory, so let's do it here too, just in case.
228 */
229 memset(buf, 0, sizeof(*buf));
230
231 ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, cpu);
232 if (ret)
233 return ret;
234
235 buf->commit_hot =
236 kvzalloc_node(ALIGN(sizeof(*buf->commit_hot)
237 * chan->backend.num_subbuf,
238 1 << INTERNODE_CACHE_SHIFT),
239 GFP_KERNEL | __GFP_NOWARN,
240 cpu_to_node(max(cpu, 0)));
241 if (!buf->commit_hot) {
242 ret = -ENOMEM;
243 goto free_chanbuf;
244 }
245
246 buf->commit_cold =
247 kvzalloc_node(ALIGN(sizeof(*buf->commit_cold)
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_cold) {
253 ret = -ENOMEM;
254 goto free_commit;
255 }
256
257 buf->ts_end =
258 kvzalloc_node(ALIGN(sizeof(*buf->ts_end)
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->ts_end) {
264 ret = -ENOMEM;
265 goto free_commit_cold;
266 }
267
268 init_waitqueue_head(&buf->read_wait);
269 init_waitqueue_head(&buf->write_wait);
270 raw_spin_lock_init(&buf->raw_tick_nohz_spinlock);
271
272 /*
273 * Write the subbuffer header for first subbuffer so we know the total
274 * duration of data gathering.
275 */
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);
282
283 if (config->cb.buffer_create) {
284 ret = config->cb.buffer_create(buf, priv, cpu, chanb->name);
285 if (ret)
286 goto free_init;
287 }
288
289 /*
290 * Ensure the buffer is ready before setting it to allocated and setting
291 * the cpumask.
292 * Used for cpu hotplug vs cpumask iteration.
293 */
294 smp_wmb();
295 buf->backend.allocated = 1;
296
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);
301 }
302 return 0;
303
304 /* Error handling */
305 free_init:
306 kvfree(buf->ts_end);
307 free_commit_cold:
308 kvfree(buf->commit_cold);
309 free_commit:
310 kvfree(buf->commit_hot);
311 free_chanbuf:
312 lib_ring_buffer_backend_free(&buf->backend);
313 return ret;
314 }
315
316 static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t)
317 {
318 struct lib_ring_buffer *buf = lttng_from_timer(buf, t, switch_timer);
319 struct channel *chan = buf->backend.chan;
320 const struct lib_ring_buffer_config *config = &chan->backend.config;
321
322 /*
323 * Only flush buffers periodically if readers are active.
324 */
325 if (atomic_long_read(&buf->active_readers))
326 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
327
328 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
329 lttng_mod_timer_pinned(&buf->switch_timer,
330 jiffies + chan->switch_timer_interval);
331 else
332 mod_timer(&buf->switch_timer,
333 jiffies + chan->switch_timer_interval);
334 }
335
336 /*
337 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
338 */
339 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer *buf)
340 {
341 struct channel *chan = buf->backend.chan;
342 const struct lib_ring_buffer_config *config = &chan->backend.config;
343 unsigned int flags = 0;
344
345 if (!chan->switch_timer_interval || buf->switch_timer_enabled)
346 return;
347
348 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
349 flags = LTTNG_TIMER_PINNED;
350
351 lttng_timer_setup(&buf->switch_timer, switch_buffer_timer, flags, buf);
352 buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
353
354 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
355 add_timer_on(&buf->switch_timer, buf->backend.cpu);
356 else
357 add_timer(&buf->switch_timer);
358
359 buf->switch_timer_enabled = 1;
360 }
361
362 /*
363 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
364 */
365 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer *buf)
366 {
367 struct channel *chan = buf->backend.chan;
368
369 if (!chan->switch_timer_interval || !buf->switch_timer_enabled)
370 return;
371
372 del_timer_sync(&buf->switch_timer);
373 buf->switch_timer_enabled = 0;
374 }
375
376 /*
377 * Polling timer to check the channels for data.
378 */
379 static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t)
380 {
381 struct lib_ring_buffer *buf = lttng_from_timer(buf, t, read_timer);
382 struct channel *chan = buf->backend.chan;
383 const struct lib_ring_buffer_config *config = &chan->backend.config;
384
385 CHAN_WARN_ON(chan, !buf->backend.allocated);
386
387 if (atomic_long_read(&buf->active_readers)
388 && lib_ring_buffer_poll_deliver(config, buf, chan)) {
389 wake_up_interruptible(&buf->read_wait);
390 wake_up_interruptible(&chan->read_wait);
391 }
392
393 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
394 lttng_mod_timer_pinned(&buf->read_timer,
395 jiffies + chan->read_timer_interval);
396 else
397 mod_timer(&buf->read_timer,
398 jiffies + chan->read_timer_interval);
399 }
400
401 /*
402 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
403 */
404 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer *buf)
405 {
406 struct channel *chan = buf->backend.chan;
407 const struct lib_ring_buffer_config *config = &chan->backend.config;
408 unsigned int flags = 0;
409
410 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
411 || !chan->read_timer_interval
412 || buf->read_timer_enabled)
413 return;
414
415 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
416 flags = LTTNG_TIMER_PINNED;
417
418 lttng_timer_setup(&buf->read_timer, read_buffer_timer, flags, buf);
419 buf->read_timer.expires = jiffies + chan->read_timer_interval;
420
421 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
422 add_timer_on(&buf->read_timer, buf->backend.cpu);
423 else
424 add_timer(&buf->read_timer);
425
426 buf->read_timer_enabled = 1;
427 }
428
429 /*
430 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
431 */
432 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer *buf)
433 {
434 struct channel *chan = buf->backend.chan;
435 const struct lib_ring_buffer_config *config = &chan->backend.config;
436
437 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
438 || !chan->read_timer_interval
439 || !buf->read_timer_enabled)
440 return;
441
442 del_timer_sync(&buf->read_timer);
443 /*
444 * do one more check to catch data that has been written in the last
445 * timer period.
446 */
447 if (lib_ring_buffer_poll_deliver(config, buf, chan)) {
448 wake_up_interruptible(&buf->read_wait);
449 wake_up_interruptible(&chan->read_wait);
450 }
451 buf->read_timer_enabled = 0;
452 }
453
454 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
455
456 enum cpuhp_state lttng_rb_hp_prepare;
457 enum cpuhp_state lttng_rb_hp_online;
458
459 void lttng_rb_set_hp_prepare(enum cpuhp_state val)
460 {
461 lttng_rb_hp_prepare = val;
462 }
463 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare);
464
465 void lttng_rb_set_hp_online(enum cpuhp_state val)
466 {
467 lttng_rb_hp_online = val;
468 }
469 EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online);
470
471 int lttng_cpuhp_rb_frontend_dead(unsigned int cpu,
472 struct lttng_cpuhp_node *node)
473 {
474 struct channel *chan = container_of(node, struct channel,
475 cpuhp_prepare);
476 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
477 const struct lib_ring_buffer_config *config = &chan->backend.config;
478
479 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
480
481 /*
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.
486 */
487 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
488 return 0;
489 }
490 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead);
491
492 int lttng_cpuhp_rb_frontend_online(unsigned int cpu,
493 struct lttng_cpuhp_node *node)
494 {
495 struct channel *chan = container_of(node, struct channel,
496 cpuhp_online);
497 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
498 const struct lib_ring_buffer_config *config = &chan->backend.config;
499
500 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
501
502 wake_up_interruptible(&chan->hp_wait);
503 lib_ring_buffer_start_switch_timer(buf);
504 lib_ring_buffer_start_read_timer(buf);
505 return 0;
506 }
507 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online);
508
509 int lttng_cpuhp_rb_frontend_offline(unsigned int cpu,
510 struct lttng_cpuhp_node *node)
511 {
512 struct channel *chan = container_of(node, struct channel,
513 cpuhp_online);
514 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
515 const struct lib_ring_buffer_config *config = &chan->backend.config;
516
517 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
518
519 lib_ring_buffer_stop_switch_timer(buf);
520 lib_ring_buffer_stop_read_timer(buf);
521 return 0;
522 }
523 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline);
524
525 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
526
527 #ifdef CONFIG_HOTPLUG_CPU
528
529 /**
530 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
531 * @nb: notifier block
532 * @action: hotplug action to take
533 * @hcpu: CPU number
534 *
535 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
536 */
537 static
538 int lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb,
539 unsigned long action,
540 void *hcpu)
541 {
542 unsigned int cpu = (unsigned long)hcpu;
543 struct channel *chan = container_of(nb, struct channel,
544 cpu_hp_notifier);
545 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
546 const struct lib_ring_buffer_config *config = &chan->backend.config;
547
548 if (!chan->cpu_hp_enable)
549 return NOTIFY_DONE;
550
551 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
552
553 switch (action) {
554 case CPU_DOWN_FAILED:
555 case CPU_DOWN_FAILED_FROZEN:
556 case CPU_ONLINE:
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);
561 return NOTIFY_OK;
562
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);
567 return NOTIFY_OK;
568
569 case CPU_DEAD:
570 case CPU_DEAD_FROZEN:
571 /*
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.
576 */
577 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
578 return NOTIFY_OK;
579
580 default:
581 return NOTIFY_DONE;
582 }
583 }
584
585 #endif
586
587 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
588
589 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
590 /*
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.
597 */
598 static int notrace ring_buffer_tick_nohz_callback(struct notifier_block *nb,
599 unsigned long val,
600 void *data)
601 {
602 struct channel *chan = container_of(nb, struct channel,
603 tick_nohz_notifier);
604 const struct lib_ring_buffer_config *config = &chan->backend.config;
605 struct lib_ring_buffer *buf;
606 int cpu = smp_processor_id();
607
608 if (config->alloc != RING_BUFFER_ALLOC_PER_CPU) {
609 /*
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.
615 */
616 return 0;
617 }
618
619 buf = channel_get_ring_buffer(config, chan, cpu);
620 switch (val) {
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);
630 }
631 if (chan->switch_timer_interval)
632 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
633 raw_spin_unlock(&buf->raw_tick_nohz_spinlock);
634 break;
635 case TICK_NOHZ_STOP:
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));
640 break;
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));
646 break;
647 }
648
649 return 0;
650 }
651
652 void notrace lib_ring_buffer_tick_nohz_flush(void)
653 {
654 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_FLUSH,
655 NULL);
656 }
657
658 void notrace lib_ring_buffer_tick_nohz_stop(void)
659 {
660 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_STOP,
661 NULL);
662 }
663
664 void notrace lib_ring_buffer_tick_nohz_restart(void)
665 {
666 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_RESTART,
667 NULL);
668 }
669 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
670
671 /*
672 * Holds CPU hotplug.
673 */
674 static void channel_unregister_notifiers(struct channel *chan)
675 {
676 const struct lib_ring_buffer_config *config = &chan->backend.config;
677
678 channel_iterator_unregister_notifiers(chan);
679 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
680 #ifdef CONFIG_NO_HZ
681 /*
682 * Remove the nohz notifier first, so we are certain we stop
683 * the timers.
684 */
685 atomic_notifier_chain_unregister(&tick_nohz_notifier,
686 &chan->tick_nohz_notifier);
687 /*
688 * ring_buffer_nohz_lock will not be needed below, because
689 * we just removed the notifiers, which were the only source of
690 * concurrency.
691 */
692 #endif /* CONFIG_NO_HZ */
693 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
694 {
695 int ret;
696
697 ret = cpuhp_state_remove_instance(lttng_rb_hp_online,
698 &chan->cpuhp_online.node);
699 WARN_ON(ret);
700 ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare,
701 &chan->cpuhp_prepare.node);
702 WARN_ON(ret);
703 }
704 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
705 {
706 int cpu;
707
708 #ifdef CONFIG_HOTPLUG_CPU
709 get_online_cpus();
710 chan->cpu_hp_enable = 0;
711 for_each_online_cpu(cpu) {
712 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
713 cpu);
714 lib_ring_buffer_stop_switch_timer(buf);
715 lib_ring_buffer_stop_read_timer(buf);
716 }
717 put_online_cpus();
718 unregister_cpu_notifier(&chan->cpu_hp_notifier);
719 #else
720 for_each_possible_cpu(cpu) {
721 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
722 cpu);
723 lib_ring_buffer_stop_switch_timer(buf);
724 lib_ring_buffer_stop_read_timer(buf);
725 }
726 #endif
727 }
728 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
729 } else {
730 struct lib_ring_buffer *buf = chan->backend.buf;
731
732 lib_ring_buffer_stop_switch_timer(buf);
733 lib_ring_buffer_stop_read_timer(buf);
734 }
735 channel_backend_unregister_notifiers(&chan->backend);
736 }
737
738 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer *buf)
739 {
740 if (!buf->quiescent) {
741 buf->quiescent = true;
742 _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH);
743 }
744 }
745
746 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer *buf)
747 {
748 buf->quiescent = false;
749 }
750
751 void lib_ring_buffer_set_quiescent_channel(struct channel *chan)
752 {
753 int cpu;
754 const struct lib_ring_buffer_config *config = &chan->backend.config;
755
756 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
757 get_online_cpus();
758 for_each_channel_cpu(cpu, chan) {
759 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
760 cpu);
761
762 lib_ring_buffer_set_quiescent(buf);
763 }
764 put_online_cpus();
765 } else {
766 struct lib_ring_buffer *buf = chan->backend.buf;
767
768 lib_ring_buffer_set_quiescent(buf);
769 }
770 }
771 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel);
772
773 void lib_ring_buffer_clear_quiescent_channel(struct channel *chan)
774 {
775 int cpu;
776 const struct lib_ring_buffer_config *config = &chan->backend.config;
777
778 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
779 get_online_cpus();
780 for_each_channel_cpu(cpu, chan) {
781 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
782 cpu);
783
784 lib_ring_buffer_clear_quiescent(buf);
785 }
786 put_online_cpus();
787 } else {
788 struct lib_ring_buffer *buf = chan->backend.buf;
789
790 lib_ring_buffer_clear_quiescent(buf);
791 }
792 }
793 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel);
794
795 static void channel_free(struct channel *chan)
796 {
797 if (chan->backend.release_priv_ops) {
798 chan->backend.release_priv_ops(chan->backend.priv_ops);
799 }
800 channel_iterator_free(chan);
801 channel_backend_free(&chan->backend);
802 kfree(chan);
803 }
804
805 /**
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.
819 *
820 * Holds cpu hotplug.
821 * Returns NULL on failure.
822 */
823 struct channel *channel_create(const struct lib_ring_buffer_config *config,
824 const char *name, void *priv, void *buf_addr,
825 size_t subbuf_size,
826 size_t num_subbuf, unsigned int switch_timer_interval,
827 unsigned int read_timer_interval)
828 {
829 int ret;
830 struct channel *chan;
831
832 if (lib_ring_buffer_check_config(config, switch_timer_interval,
833 read_timer_interval))
834 return NULL;
835
836 chan = kzalloc(sizeof(struct channel), GFP_KERNEL);
837 if (!chan)
838 return NULL;
839
840 ret = channel_backend_init(&chan->backend, name, config, priv,
841 subbuf_size, num_subbuf);
842 if (ret)
843 goto error;
844
845 ret = channel_iterator_init(chan);
846 if (ret)
847 goto error_free_backend;
848
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);
855
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);
861 if (ret)
862 goto cpuhp_prepare_error;
863
864 chan->cpuhp_online.component = LTTNG_RING_BUFFER_FRONTEND;
865 ret = cpuhp_state_add_instance(lttng_rb_hp_online,
866 &chan->cpuhp_online.node);
867 if (ret)
868 goto cpuhp_online_error;
869 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
870 {
871 int cpu;
872 /*
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.
876 */
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);
882
883 get_online_cpus();
884 for_each_online_cpu(cpu) {
885 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
886 cpu);
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));
891 }
892 chan->cpu_hp_enable = 1;
893 put_online_cpus();
894 #else
895 for_each_possible_cpu(cpu) {
896 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
897 cpu);
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));
902 }
903 #endif
904 }
905 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
906
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) */
915
916 } else {
917 struct lib_ring_buffer *buf = chan->backend.buf;
918
919 lib_ring_buffer_start_switch_timer(buf);
920 lib_ring_buffer_start_read_timer(buf);
921 }
922
923 return chan;
924
925 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
926 cpuhp_online_error:
927 ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare,
928 &chan->cpuhp_prepare.node);
929 WARN_ON(ret);
930 cpuhp_prepare_error:
931 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
932 error_free_backend:
933 channel_backend_free(&chan->backend);
934 error:
935 kfree(chan);
936 return NULL;
937 }
938 EXPORT_SYMBOL_GPL(channel_create);
939
940 static
941 void channel_release(struct kref *kref)
942 {
943 struct channel *chan = container_of(kref, struct channel, ref);
944 channel_free(chan);
945 }
946
947 /**
948 * channel_destroy - Finalize, wait for q.s. and destroy channel.
949 * @chan: channel to destroy
950 *
951 * Holds cpu hotplug.
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
956 * data pointer.
957 */
958 void *channel_destroy(struct channel *chan)
959 {
960 int cpu;
961 const struct lib_ring_buffer_config *config = &chan->backend.config;
962 void *priv;
963
964 channel_unregister_notifiers(chan);
965
966 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
967 /*
968 * No need to hold cpu hotplug, because all notifiers have been
969 * unregistered.
970 */
971 for_each_channel_cpu(cpu, chan) {
972 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
973 cpu);
974
975 if (config->cb.buffer_finalize)
976 config->cb.buffer_finalize(buf,
977 chan->backend.priv,
978 cpu);
979 /*
980 * Perform flush before writing to finalized.
981 */
982 smp_wmb();
983 WRITE_ONCE(buf->finalized, 1);
984 wake_up_interruptible(&buf->read_wait);
985 }
986 } else {
987 struct lib_ring_buffer *buf = chan->backend.buf;
988
989 if (config->cb.buffer_finalize)
990 config->cb.buffer_finalize(buf, chan->backend.priv, -1);
991 /*
992 * Perform flush before writing to finalized.
993 */
994 smp_wmb();
995 WRITE_ONCE(buf->finalized, 1);
996 wake_up_interruptible(&buf->read_wait);
997 }
998 WRITE_ONCE(chan->finalized, 1);
999 wake_up_interruptible(&chan->hp_wait);
1000 wake_up_interruptible(&chan->read_wait);
1001 priv = chan->backend.priv;
1002 kref_put(&chan->ref, channel_release);
1003 return priv;
1004 }
1005 EXPORT_SYMBOL_GPL(channel_destroy);
1006
1007 struct lib_ring_buffer *channel_get_ring_buffer(
1008 const struct lib_ring_buffer_config *config,
1009 struct channel *chan, int cpu)
1010 {
1011 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL)
1012 return chan->backend.buf;
1013 else
1014 return per_cpu_ptr(chan->backend.buf, cpu);
1015 }
1016 EXPORT_SYMBOL_GPL(channel_get_ring_buffer);
1017
1018 int lib_ring_buffer_open_read(struct lib_ring_buffer *buf)
1019 {
1020 struct channel *chan = buf->backend.chan;
1021
1022 if (!atomic_long_add_unless(&buf->active_readers, 1, 1))
1023 return -EBUSY;
1024 if (!lttng_kref_get(&chan->ref)) {
1025 atomic_long_dec(&buf->active_readers);
1026 return -EOVERFLOW;
1027 }
1028 lttng_smp_mb__after_atomic();
1029 return 0;
1030 }
1031 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read);
1032
1033 void lib_ring_buffer_release_read(struct lib_ring_buffer *buf)
1034 {
1035 struct channel *chan = buf->backend.chan;
1036
1037 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
1038 lttng_smp_mb__before_atomic();
1039 atomic_long_dec(&buf->active_readers);
1040 kref_put(&chan->ref, channel_release);
1041 }
1042 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read);
1043
1044 /*
1045 * Promote compiler barrier to a smp_mb().
1046 * For the specific ring buffer case, this IPI call should be removed if the
1047 * architecture does not reorder writes. This should eventually be provided by
1048 * a separate architecture-specific infrastructure.
1049 */
1050 static void remote_mb(void *info)
1051 {
1052 smp_mb();
1053 }
1054
1055 /**
1056 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1057 * @buf: ring buffer
1058 * @consumed: consumed count indicating the position where to read
1059 * @produced: produced count, indicates position when to stop reading
1060 *
1061 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1062 * data to read at consumed position, or 0 if the get operation succeeds.
1063 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1064 */
1065
1066 int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf,
1067 unsigned long *consumed, unsigned long *produced)
1068 {
1069 struct channel *chan = buf->backend.chan;
1070 const struct lib_ring_buffer_config *config = &chan->backend.config;
1071 unsigned long consumed_cur, write_offset;
1072 int finalized;
1073
1074 retry:
1075 finalized = READ_ONCE(buf->finalized);
1076 /*
1077 * Read finalized before counters.
1078 */
1079 smp_rmb();
1080 consumed_cur = atomic_long_read(&buf->consumed);
1081 /*
1082 * No need to issue a memory barrier between consumed count read and
1083 * write offset read, because consumed count can only change
1084 * concurrently in overwrite mode, and we keep a sequence counter
1085 * identifier derived from the write offset to check we are getting
1086 * the same sub-buffer we are expecting (the sub-buffers are atomically
1087 * "tagged" upon writes, tags are checked upon read).
1088 */
1089 write_offset = v_read(config, &buf->offset);
1090
1091 /*
1092 * Check that we are not about to read the same subbuffer in
1093 * which the writer head is.
1094 */
1095 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1096 == 0)
1097 goto nodata;
1098
1099 *consumed = consumed_cur;
1100 *produced = subbuf_trunc(write_offset, chan);
1101
1102 return 0;
1103
1104 nodata:
1105 /*
1106 * The memory barriers __wait_event()/wake_up_interruptible() take care
1107 * of "raw_spin_is_locked" memory ordering.
1108 */
1109 if (finalized)
1110 return -ENODATA;
1111 else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1112 goto retry;
1113 else
1114 return -EAGAIN;
1115 }
1116 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot);
1117
1118 /**
1119 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1120 * are saved regardless of whether the consumed and produced positions are
1121 * in the same subbuffer.
1122 * @buf: ring buffer
1123 * @consumed: consumed byte count indicating the last position read
1124 * @produced: produced byte count indicating the last position written
1125 *
1126 * This function is meant to provide information on the exact producer and
1127 * consumer positions without regard for the "snapshot" feature.
1128 */
1129 int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer *buf,
1130 unsigned long *consumed, unsigned long *produced)
1131 {
1132 struct channel *chan = buf->backend.chan;
1133 const struct lib_ring_buffer_config *config = &chan->backend.config;
1134
1135 smp_rmb();
1136 *consumed = atomic_long_read(&buf->consumed);
1137 /*
1138 * No need to issue a memory barrier between consumed count read and
1139 * write offset read, because consumed count can only change
1140 * concurrently in overwrite mode, and we keep a sequence counter
1141 * identifier derived from the write offset to check we are getting
1142 * the same sub-buffer we are expecting (the sub-buffers are atomically
1143 * "tagged" upon writes, tags are checked upon read).
1144 */
1145 *produced = v_read(config, &buf->offset);
1146 return 0;
1147 }
1148
1149 /**
1150 * lib_ring_buffer_put_snapshot - move consumed counter forward
1151 *
1152 * Should only be called from consumer context.
1153 * @buf: ring buffer
1154 * @consumed_new: new consumed count value
1155 */
1156 void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf,
1157 unsigned long consumed_new)
1158 {
1159 struct lib_ring_buffer_backend *bufb = &buf->backend;
1160 struct channel *chan = bufb->chan;
1161 unsigned long consumed;
1162
1163 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
1164
1165 /*
1166 * Only push the consumed value forward.
1167 * If the consumed cmpxchg fails, this is because we have been pushed by
1168 * the writer in flight recorder mode.
1169 */
1170 consumed = atomic_long_read(&buf->consumed);
1171 while ((long) consumed - (long) consumed_new < 0)
1172 consumed = atomic_long_cmpxchg(&buf->consumed, consumed,
1173 consumed_new);
1174 /* Wake-up the metadata producer */
1175 wake_up_interruptible(&buf->write_wait);
1176 }
1177 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer);
1178
1179 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1180 static void lib_ring_buffer_flush_read_subbuf_dcache(
1181 const struct lib_ring_buffer_config *config,
1182 struct channel *chan,
1183 struct lib_ring_buffer *buf)
1184 {
1185 struct lib_ring_buffer_backend_pages *pages;
1186 unsigned long sb_bindex, id, i, nr_pages;
1187
1188 if (config->output != RING_BUFFER_MMAP)
1189 return;
1190
1191 /*
1192 * Architectures with caches aliased on virtual addresses may
1193 * use different cache lines for the linear mapping vs
1194 * user-space memory mapping. Given that the ring buffer is
1195 * based on the kernel linear mapping, aligning it with the
1196 * user-space mapping is not straightforward, and would require
1197 * extra TLB entries. Therefore, simply flush the dcache for the
1198 * entire sub-buffer before reading it.
1199 */
1200 id = buf->backend.buf_rsb.id;
1201 sb_bindex = subbuffer_id_get_index(config, id);
1202 pages = buf->backend.array[sb_bindex];
1203 nr_pages = buf->backend.num_pages_per_subbuf;
1204 for (i = 0; i < nr_pages; i++) {
1205 struct lib_ring_buffer_backend_page *backend_page;
1206
1207 backend_page = &pages->p[i];
1208 flush_dcache_page(pfn_to_page(backend_page->pfn));
1209 }
1210 }
1211 #else
1212 static void lib_ring_buffer_flush_read_subbuf_dcache(
1213 const struct lib_ring_buffer_config *config,
1214 struct channel *chan,
1215 struct lib_ring_buffer *buf)
1216 {
1217 }
1218 #endif
1219
1220 /**
1221 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1222 * @buf: ring buffer
1223 * @consumed: consumed count indicating the position where to read
1224 *
1225 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1226 * data to read at consumed position, or 0 if the get operation succeeds.
1227 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1228 */
1229 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf,
1230 unsigned long consumed)
1231 {
1232 struct channel *chan = buf->backend.chan;
1233 const struct lib_ring_buffer_config *config = &chan->backend.config;
1234 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
1235 int ret;
1236 int finalized;
1237
1238 if (buf->get_subbuf) {
1239 /*
1240 * Reader is trying to get a subbuffer twice.
1241 */
1242 CHAN_WARN_ON(chan, 1);
1243 return -EBUSY;
1244 }
1245 retry:
1246 finalized = READ_ONCE(buf->finalized);
1247 /*
1248 * Read finalized before counters.
1249 */
1250 smp_rmb();
1251 consumed_cur = atomic_long_read(&buf->consumed);
1252 consumed_idx = subbuf_index(consumed, chan);
1253 commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb);
1254 /*
1255 * Make sure we read the commit count before reading the buffer
1256 * data and the write offset. Correct consumed offset ordering
1257 * wrt commit count is insured by the use of cmpxchg to update
1258 * the consumed offset.
1259 * smp_call_function_single can fail if the remote CPU is offline,
1260 * this is OK because then there is no wmb to execute there.
1261 * If our thread is executing on the same CPU as the on the buffers
1262 * belongs to, we don't have to synchronize it at all. If we are
1263 * migrated, the scheduler will take care of the memory barriers.
1264 * Normally, smp_call_function_single() should ensure program order when
1265 * executing the remote function, which implies that it surrounds the
1266 * function execution with :
1267 * smp_mb()
1268 * send IPI
1269 * csd_lock_wait
1270 * recv IPI
1271 * smp_mb()
1272 * exec. function
1273 * smp_mb()
1274 * csd unlock
1275 * smp_mb()
1276 *
1277 * However, smp_call_function_single() does not seem to clearly execute
1278 * such barriers. It depends on spinlock semantic to provide the barrier
1279 * before executing the IPI and, when busy-looping, csd_lock_wait only
1280 * executes smp_mb() when it has to wait for the other CPU.
1281 *
1282 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1283 * required ourself, even if duplicated. It has no performance impact
1284 * anyway.
1285 *
1286 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1287 * read and write vs write. They do not ensure core synchronization. We
1288 * really have to ensure total order between the 3 barriers running on
1289 * the 2 CPUs.
1290 */
1291 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1292 if (config->sync == RING_BUFFER_SYNC_PER_CPU
1293 && config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
1294 if (raw_smp_processor_id() != buf->backend.cpu) {
1295 /* Total order with IPI handler smp_mb() */
1296 smp_mb();
1297 smp_call_function_single(buf->backend.cpu,
1298 remote_mb, NULL, 1);
1299 /* Total order with IPI handler smp_mb() */
1300 smp_mb();
1301 }
1302 } else {
1303 /* Total order with IPI handler smp_mb() */
1304 smp_mb();
1305 smp_call_function(remote_mb, NULL, 1);
1306 /* Total order with IPI handler smp_mb() */
1307 smp_mb();
1308 }
1309 } else {
1310 /*
1311 * Local rmb to match the remote wmb to read the commit count
1312 * before the buffer data and the write offset.
1313 */
1314 smp_rmb();
1315 }
1316
1317 write_offset = v_read(config, &buf->offset);
1318
1319 /*
1320 * Check that the buffer we are getting is after or at consumed_cur
1321 * position.
1322 */
1323 if ((long) subbuf_trunc(consumed, chan)
1324 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1325 goto nodata;
1326
1327 /*
1328 * Check that the subbuffer we are trying to consume has been
1329 * already fully committed.
1330 */
1331 if (((commit_count - chan->backend.subbuf_size)
1332 & chan->commit_count_mask)
1333 - (buf_trunc(consumed, chan)
1334 >> chan->backend.num_subbuf_order)
1335 != 0)
1336 goto nodata;
1337
1338 /*
1339 * Check that we are not about to read the same subbuffer in
1340 * which the writer head is.
1341 */
1342 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan)
1343 == 0)
1344 goto nodata;
1345
1346 /*
1347 * Failure to get the subbuffer causes a busy-loop retry without going
1348 * to a wait queue. These are caused by short-lived race windows where
1349 * the writer is getting access to a subbuffer we were trying to get
1350 * access to. Also checks that the "consumed" buffer count we are
1351 * looking for matches the one contained in the subbuffer id.
1352 */
1353 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1354 consumed_idx, buf_trunc_val(consumed, chan));
1355 if (ret)
1356 goto retry;
1357 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1358
1359 buf->get_subbuf_consumed = consumed;
1360 buf->get_subbuf = 1;
1361
1362 lib_ring_buffer_flush_read_subbuf_dcache(config, chan, buf);
1363
1364 return 0;
1365
1366 nodata:
1367 /*
1368 * The memory barriers __wait_event()/wake_up_interruptible() take care
1369 * of "raw_spin_is_locked" memory ordering.
1370 */
1371 if (finalized)
1372 return -ENODATA;
1373 else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1374 goto retry;
1375 else
1376 return -EAGAIN;
1377 }
1378 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf);
1379
1380 /**
1381 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1382 * @buf: ring buffer
1383 */
1384 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf)
1385 {
1386 struct lib_ring_buffer_backend *bufb = &buf->backend;
1387 struct channel *chan = bufb->chan;
1388 const struct lib_ring_buffer_config *config = &chan->backend.config;
1389 unsigned long read_sb_bindex, consumed_idx, consumed;
1390
1391 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
1392
1393 if (!buf->get_subbuf) {
1394 /*
1395 * Reader puts a subbuffer it did not get.
1396 */
1397 CHAN_WARN_ON(chan, 1);
1398 return;
1399 }
1400 consumed = buf->get_subbuf_consumed;
1401 buf->get_subbuf = 0;
1402
1403 /*
1404 * Clear the records_unread counter. (overruns counter)
1405 * Can still be non-zero if a file reader simply grabbed the data
1406 * without using iterators.
1407 * Can be below zero if an iterator is used on a snapshot more than
1408 * once.
1409 */
1410 read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1411 v_add(config, v_read(config,
1412 &bufb->array[read_sb_bindex]->records_unread),
1413 &bufb->records_read);
1414 v_set(config, &bufb->array[read_sb_bindex]->records_unread, 0);
1415 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1416 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1417 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1418
1419 /*
1420 * Exchange the reader subbuffer with the one we put in its place in the
1421 * writer subbuffer table. Expect the original consumed count. If
1422 * update_read_sb_index fails, this is because the writer updated the
1423 * subbuffer concurrently. We should therefore keep the subbuffer we
1424 * currently have: it has become invalid to try reading this sub-buffer
1425 * consumed count value anyway.
1426 */
1427 consumed_idx = subbuf_index(consumed, chan);
1428 update_read_sb_index(config, &buf->backend, &chan->backend,
1429 consumed_idx, buf_trunc_val(consumed, chan));
1430 /*
1431 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1432 * if the writer concurrently updated it.
1433 */
1434 }
1435 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf);
1436
1437 /*
1438 * cons_offset is an iterator on all subbuffer offsets between the reader
1439 * position and the writer position. (inclusive)
1440 */
1441 static
1442 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf,
1443 struct channel *chan,
1444 unsigned long cons_offset,
1445 int cpu)
1446 {
1447 const struct lib_ring_buffer_config *config = &chan->backend.config;
1448 unsigned long cons_idx, commit_count, commit_count_sb;
1449
1450 cons_idx = subbuf_index(cons_offset, chan);
1451 commit_count = v_read(config, &buf->commit_hot[cons_idx].cc);
1452 commit_count_sb = v_read(config, &buf->commit_cold[cons_idx].cc_sb);
1453
1454 if (subbuf_offset(commit_count, chan) != 0)
1455 printk(KERN_WARNING
1456 "ring buffer %s, cpu %d: "
1457 "commit count in subbuffer %lu,\n"
1458 "expecting multiples of %lu bytes\n"
1459 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1460 chan->backend.name, cpu, cons_idx,
1461 chan->backend.subbuf_size,
1462 commit_count, commit_count_sb);
1463
1464 printk(KERN_DEBUG "ring buffer: %s, cpu %d: %lu bytes committed\n",
1465 chan->backend.name, cpu, commit_count);
1466 }
1467
1468 static
1469 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf,
1470 struct channel *chan,
1471 void *priv, int cpu)
1472 {
1473 const struct lib_ring_buffer_config *config = &chan->backend.config;
1474 unsigned long write_offset, cons_offset;
1475
1476 /*
1477 * No need to order commit_count, write_offset and cons_offset reads
1478 * because we execute at teardown when no more writer nor reader
1479 * references are left.
1480 */
1481 write_offset = v_read(config, &buf->offset);
1482 cons_offset = atomic_long_read(&buf->consumed);
1483 if (write_offset != cons_offset)
1484 printk(KERN_DEBUG
1485 "ring buffer %s, cpu %d: "
1486 "non-consumed data\n"
1487 " [ %lu bytes written, %lu bytes read ]\n",
1488 chan->backend.name, cpu, write_offset, cons_offset);
1489
1490 for (cons_offset = atomic_long_read(&buf->consumed);
1491 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1492 chan)
1493 - cons_offset) > 0;
1494 cons_offset = subbuf_align(cons_offset, chan))
1495 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1496 cpu);
1497 }
1498
1499 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
1500 static
1501 void lib_ring_buffer_print_records_count(struct channel *chan,
1502 struct lib_ring_buffer *buf,
1503 int cpu)
1504 {
1505 const struct lib_ring_buffer_config *config = &chan->backend.config;
1506
1507 if (!strcmp(chan->backend.name, "relay-metadata")) {
1508 printk(KERN_DEBUG "ring buffer %s: %lu records written, "
1509 "%lu records overrun\n",
1510 chan->backend.name,
1511 v_read(config, &buf->records_count),
1512 v_read(config, &buf->records_overrun));
1513 } else {
1514 printk(KERN_DEBUG "ring buffer %s, cpu %d: %lu records written, "
1515 "%lu records overrun\n",
1516 chan->backend.name, cpu,
1517 v_read(config, &buf->records_count),
1518 v_read(config, &buf->records_overrun));
1519 }
1520 }
1521 #else
1522 static
1523 void lib_ring_buffer_print_records_count(struct channel *chan,
1524 struct lib_ring_buffer *buf,
1525 int cpu)
1526 {
1527 }
1528 #endif
1529
1530 static
1531 void lib_ring_buffer_print_errors(struct channel *chan,
1532 struct lib_ring_buffer *buf, int cpu)
1533 {
1534 const struct lib_ring_buffer_config *config = &chan->backend.config;
1535 void *priv = chan->backend.priv;
1536
1537 lib_ring_buffer_print_records_count(chan, buf, cpu);
1538 if (strcmp(chan->backend.name, "relay-metadata")) {
1539 if (v_read(config, &buf->records_lost_full)
1540 || v_read(config, &buf->records_lost_wrap)
1541 || v_read(config, &buf->records_lost_big))
1542 printk(KERN_WARNING
1543 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1544 " [ %lu buffer full, %lu nest buffer wrap-around, "
1545 "%lu event too big ]\n",
1546 chan->backend.name, cpu,
1547 v_read(config, &buf->records_lost_full),
1548 v_read(config, &buf->records_lost_wrap),
1549 v_read(config, &buf->records_lost_big));
1550 }
1551 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu);
1552 }
1553
1554 /*
1555 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1556 *
1557 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1558 */
1559 static
1560 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf,
1561 struct channel *chan,
1562 struct switch_offsets *offsets,
1563 u64 tsc)
1564 {
1565 const struct lib_ring_buffer_config *config = &chan->backend.config;
1566 unsigned long oldidx = subbuf_index(offsets->old, chan);
1567 unsigned long commit_count;
1568 struct commit_counters_hot *cc_hot;
1569
1570 config->cb.buffer_begin(buf, tsc, oldidx);
1571
1572 /*
1573 * Order all writes to buffer before the commit count update that will
1574 * determine that the subbuffer is full.
1575 */
1576 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1577 /*
1578 * Must write slot data before incrementing commit count. This
1579 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1580 * by get_subbuf().
1581 */
1582 barrier();
1583 } else
1584 smp_wmb();
1585 cc_hot = &buf->commit_hot[oldidx];
1586 v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc);
1587 commit_count = v_read(config, &cc_hot->cc);
1588 /* Check if the written buffer has to be delivered */
1589 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1590 commit_count, oldidx, tsc);
1591 lib_ring_buffer_write_commit_counter(config, buf, chan,
1592 offsets->old + config->cb.subbuffer_header_size(),
1593 commit_count, cc_hot);
1594 }
1595
1596 /*
1597 * lib_ring_buffer_switch_old_end: switch old subbuffer
1598 *
1599 * Note : offset_old should never be 0 here. It is ok, because we never perform
1600 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1601 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1602 * subbuffer.
1603 */
1604 static
1605 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf,
1606 struct channel *chan,
1607 struct switch_offsets *offsets,
1608 u64 tsc)
1609 {
1610 const struct lib_ring_buffer_config *config = &chan->backend.config;
1611 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1612 unsigned long commit_count, padding_size, data_size;
1613 struct commit_counters_hot *cc_hot;
1614 u64 *ts_end;
1615
1616 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1617 padding_size = chan->backend.subbuf_size - data_size;
1618 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size);
1619
1620 ts_end = &buf->ts_end[oldidx];
1621 /*
1622 * This is the last space reservation in that sub-buffer before
1623 * it gets delivered. This provides exclusive access to write to
1624 * this sub-buffer's ts_end. There are also no concurrent
1625 * readers of that ts_end because delivery of that sub-buffer is
1626 * postponed until the commit counter is incremented for the
1627 * current space reservation.
1628 */
1629 *ts_end = tsc;
1630
1631 /*
1632 * Order all writes to buffer and store to ts_end before the commit
1633 * count update that will determine that the subbuffer is full.
1634 */
1635 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1636 /*
1637 * Must write slot data before incrementing commit count. This
1638 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1639 * by get_subbuf().
1640 */
1641 barrier();
1642 } else
1643 smp_wmb();
1644 cc_hot = &buf->commit_hot[oldidx];
1645 v_add(config, padding_size, &cc_hot->cc);
1646 commit_count = v_read(config, &cc_hot->cc);
1647 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1648 commit_count, oldidx, tsc);
1649 lib_ring_buffer_write_commit_counter(config, buf, chan,
1650 offsets->old + padding_size, commit_count,
1651 cc_hot);
1652 }
1653
1654 /*
1655 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1656 *
1657 * This code can be executed unordered : writers may already have written to the
1658 * sub-buffer before this code gets executed, caution. The commit makes sure
1659 * that this code is executed before the deliver of this sub-buffer.
1660 */
1661 static
1662 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf,
1663 struct channel *chan,
1664 struct switch_offsets *offsets,
1665 u64 tsc)
1666 {
1667 const struct lib_ring_buffer_config *config = &chan->backend.config;
1668 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1669 unsigned long commit_count;
1670 struct commit_counters_hot *cc_hot;
1671
1672 config->cb.buffer_begin(buf, tsc, beginidx);
1673
1674 /*
1675 * Order all writes to buffer before the commit count update that will
1676 * determine that the subbuffer is full.
1677 */
1678 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1679 /*
1680 * Must write slot data before incrementing commit count. This
1681 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1682 * by get_subbuf().
1683 */
1684 barrier();
1685 } else
1686 smp_wmb();
1687 cc_hot = &buf->commit_hot[beginidx];
1688 v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc);
1689 commit_count = v_read(config, &cc_hot->cc);
1690 /* Check if the written buffer has to be delivered */
1691 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1692 commit_count, beginidx, tsc);
1693 lib_ring_buffer_write_commit_counter(config, buf, chan,
1694 offsets->begin + config->cb.subbuffer_header_size(),
1695 commit_count, cc_hot);
1696 }
1697
1698 /*
1699 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1700 *
1701 * Calls subbuffer_set_data_size() to set the data size of the current
1702 * sub-buffer. We do not need to perform check_deliver nor commit here,
1703 * since this task will be done by the "commit" of the event for which
1704 * we are currently doing the space reservation.
1705 */
1706 static
1707 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf,
1708 struct channel *chan,
1709 struct switch_offsets *offsets,
1710 u64 tsc)
1711 {
1712 const struct lib_ring_buffer_config *config = &chan->backend.config;
1713 unsigned long endidx, data_size;
1714 u64 *ts_end;
1715
1716 endidx = subbuf_index(offsets->end - 1, chan);
1717 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1718 subbuffer_set_data_size(config, &buf->backend, endidx, data_size);
1719 ts_end = &buf->ts_end[endidx];
1720 /*
1721 * This is the last space reservation in that sub-buffer before
1722 * it gets delivered. This provides exclusive access to write to
1723 * this sub-buffer's ts_end. There are also no concurrent
1724 * readers of that ts_end because delivery of that sub-buffer is
1725 * postponed until the commit counter is incremented for the
1726 * current space reservation.
1727 */
1728 *ts_end = tsc;
1729 }
1730
1731 /*
1732 * Returns :
1733 * 0 if ok
1734 * !0 if execution must be aborted.
1735 */
1736 static
1737 int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
1738 struct lib_ring_buffer *buf,
1739 struct channel *chan,
1740 struct switch_offsets *offsets,
1741 u64 *tsc)
1742 {
1743 const struct lib_ring_buffer_config *config = &chan->backend.config;
1744 unsigned long off, reserve_commit_diff;
1745
1746 offsets->begin = v_read(config, &buf->offset);
1747 offsets->old = offsets->begin;
1748 offsets->switch_old_start = 0;
1749 off = subbuf_offset(offsets->begin, chan);
1750
1751 *tsc = config->cb.ring_buffer_clock_read(chan);
1752
1753 /*
1754 * Ensure we flush the header of an empty subbuffer when doing the
1755 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1756 * total data gathering duration even if there were no records saved
1757 * after the last buffer switch.
1758 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1759 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1760 * subbuffer header as appropriate.
1761 * The next record that reserves space will be responsible for
1762 * populating the following subbuffer header. We choose not to populate
1763 * the next subbuffer header here because we want to be able to use
1764 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1765 * buffer flush, which must guarantee that all the buffer content
1766 * (records and header timestamps) are visible to the reader. This is
1767 * required for quiescence guarantees for the fusion merge.
1768 */
1769 if (mode != SWITCH_FLUSH && !off)
1770 return -1; /* we do not have to switch : buffer is empty */
1771
1772 if (unlikely(off == 0)) {
1773 unsigned long sb_index, commit_count;
1774
1775 /*
1776 * We are performing a SWITCH_FLUSH. At this stage, there are no
1777 * concurrent writes into the buffer.
1778 *
1779 * The client does not save any header information. Don't
1780 * switch empty subbuffer on finalize, because it is invalid to
1781 * deliver a completely empty subbuffer.
1782 */
1783 if (!config->cb.subbuffer_header_size())
1784 return -1;
1785
1786 /* Test new buffer integrity */
1787 sb_index = subbuf_index(offsets->begin, chan);
1788 commit_count = v_read(config,
1789 &buf->commit_cold[sb_index].cc_sb);
1790 reserve_commit_diff =
1791 (buf_trunc(offsets->begin, chan)
1792 >> chan->backend.num_subbuf_order)
1793 - (commit_count & chan->commit_count_mask);
1794 if (likely(reserve_commit_diff == 0)) {
1795 /* Next subbuffer not being written to. */
1796 if (unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1797 subbuf_trunc(offsets->begin, chan)
1798 - subbuf_trunc((unsigned long)
1799 atomic_long_read(&buf->consumed), chan)
1800 >= chan->backend.buf_size)) {
1801 /*
1802 * We do not overwrite non consumed buffers
1803 * and we are full : don't switch.
1804 */
1805 return -1;
1806 } else {
1807 /*
1808 * Next subbuffer not being written to, and we
1809 * are either in overwrite mode or the buffer is
1810 * not full. It's safe to write in this new
1811 * subbuffer.
1812 */
1813 }
1814 } else {
1815 /*
1816 * Next subbuffer reserve offset does not match the
1817 * commit offset. Don't perform switch in
1818 * producer-consumer and overwrite mode. Caused by
1819 * either a writer OOPS or too many nested writes over a
1820 * reserve/commit pair.
1821 */
1822 return -1;
1823 }
1824
1825 /*
1826 * Need to write the subbuffer start header on finalize.
1827 */
1828 offsets->switch_old_start = 1;
1829 }
1830 offsets->begin = subbuf_align(offsets->begin, chan);
1831 /* Note: old points to the next subbuf at offset 0 */
1832 offsets->end = offsets->begin;
1833 return 0;
1834 }
1835
1836 /*
1837 * Force a sub-buffer switch. This operation is completely reentrant : can be
1838 * called while tracing is active with absolutely no lock held.
1839 *
1840 * Note, however, that as a v_cmpxchg is used for some atomic
1841 * operations, this function must be called from the CPU which owns the buffer
1842 * for a ACTIVE flush.
1843 */
1844 void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode)
1845 {
1846 struct channel *chan = buf->backend.chan;
1847 const struct lib_ring_buffer_config *config = &chan->backend.config;
1848 struct switch_offsets offsets;
1849 unsigned long oldidx;
1850 u64 tsc;
1851
1852 offsets.size = 0;
1853
1854 /*
1855 * Perform retryable operations.
1856 */
1857 do {
1858 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
1859 &tsc))
1860 return; /* Switch not needed */
1861 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
1862 != offsets.old);
1863
1864 /*
1865 * Atomically update last_tsc. This update races against concurrent
1866 * atomic updates, but the race will always cause supplementary full TSC
1867 * records, never the opposite (missing a full TSC record when it would
1868 * be needed).
1869 */
1870 save_last_tsc(config, buf, tsc);
1871
1872 /*
1873 * Push the reader if necessary
1874 */
1875 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
1876
1877 oldidx = subbuf_index(offsets.old, chan);
1878 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx);
1879
1880 /*
1881 * May need to populate header start on SWITCH_FLUSH.
1882 */
1883 if (offsets.switch_old_start) {
1884 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc);
1885 offsets.old += config->cb.subbuffer_header_size();
1886 }
1887
1888 /*
1889 * Switch old subbuffer.
1890 */
1891 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc);
1892 }
1893 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow);
1894
1895 struct switch_param {
1896 struct lib_ring_buffer *buf;
1897 enum switch_mode mode;
1898 };
1899
1900 static void remote_switch(void *info)
1901 {
1902 struct switch_param *param = info;
1903 struct lib_ring_buffer *buf = param->buf;
1904
1905 lib_ring_buffer_switch_slow(buf, param->mode);
1906 }
1907
1908 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf,
1909 enum switch_mode mode)
1910 {
1911 struct channel *chan = buf->backend.chan;
1912 const struct lib_ring_buffer_config *config = &chan->backend.config;
1913 int ret;
1914 struct switch_param param;
1915
1916 /*
1917 * With global synchronization we don't need to use the IPI scheme.
1918 */
1919 if (config->sync == RING_BUFFER_SYNC_GLOBAL) {
1920 lib_ring_buffer_switch_slow(buf, mode);
1921 return;
1922 }
1923
1924 /*
1925 * Disabling preemption ensures two things: first, that the
1926 * target cpu is not taken concurrently offline while we are within
1927 * smp_call_function_single(). Secondly, if it happens that the
1928 * CPU is not online, our own call to lib_ring_buffer_switch_slow()
1929 * needs to be protected from CPU hotplug handlers, which can
1930 * also perform a remote subbuffer switch.
1931 */
1932 preempt_disable();
1933 param.buf = buf;
1934 param.mode = mode;
1935 ret = smp_call_function_single(buf->backend.cpu,
1936 remote_switch, &param, 1);
1937 if (ret) {
1938 /* Remote CPU is offline, do it ourself. */
1939 lib_ring_buffer_switch_slow(buf, mode);
1940 }
1941 preempt_enable();
1942 }
1943
1944 /* Switch sub-buffer if current sub-buffer is non-empty. */
1945 void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf)
1946 {
1947 _lib_ring_buffer_switch_remote(buf, SWITCH_ACTIVE);
1948 }
1949 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote);
1950
1951 /* Switch sub-buffer even if current sub-buffer is empty. */
1952 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer *buf)
1953 {
1954 _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH);
1955 }
1956 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty);
1957
1958 void lib_ring_buffer_clear(struct lib_ring_buffer *buf)
1959 {
1960 struct lib_ring_buffer_backend *bufb = &buf->backend;
1961 struct channel *chan = bufb->chan;
1962
1963 lib_ring_buffer_switch_remote(buf);
1964 lib_ring_buffer_clear_reader(buf, chan);
1965 }
1966 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear);
1967
1968 /*
1969 * Returns :
1970 * 0 if ok
1971 * -ENOSPC if event size is too large for packet.
1972 * -ENOBUFS if there is currently not enough space in buffer for the event.
1973 * -EIO if data cannot be written into the buffer for any other reason.
1974 */
1975 static
1976 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf,
1977 struct channel *chan,
1978 struct switch_offsets *offsets,
1979 struct lib_ring_buffer_ctx *ctx,
1980 void *client_ctx)
1981 {
1982 const struct lib_ring_buffer_config *config = &chan->backend.config;
1983 unsigned long reserve_commit_diff, offset_cmp;
1984
1985 retry:
1986 offsets->begin = offset_cmp = v_read(config, &buf->offset);
1987 offsets->old = offsets->begin;
1988 offsets->switch_new_start = 0;
1989 offsets->switch_new_end = 0;
1990 offsets->switch_old_end = 0;
1991 offsets->pre_header_padding = 0;
1992
1993 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
1994 if ((int64_t) ctx->tsc == -EIO)
1995 return -EIO;
1996
1997 if (last_tsc_overflow(config, buf, ctx->tsc))
1998 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
1999
2000 if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
2001 offsets->switch_new_start = 1; /* For offsets->begin */
2002 } else {
2003 offsets->size = config->cb.record_header_size(config, chan,
2004 offsets->begin,
2005 &offsets->pre_header_padding,
2006 ctx, client_ctx);
2007 offsets->size +=
2008 lib_ring_buffer_align(offsets->begin + offsets->size,
2009 ctx->largest_align)
2010 + ctx->data_size;
2011 if (unlikely(subbuf_offset(offsets->begin, chan) +
2012 offsets->size > chan->backend.subbuf_size)) {
2013 offsets->switch_old_end = 1; /* For offsets->old */
2014 offsets->switch_new_start = 1; /* For offsets->begin */
2015 }
2016 }
2017 if (unlikely(offsets->switch_new_start)) {
2018 unsigned long sb_index, commit_count;
2019
2020 /*
2021 * We are typically not filling the previous buffer completely.
2022 */
2023 if (likely(offsets->switch_old_end))
2024 offsets->begin = subbuf_align(offsets->begin, chan);
2025 offsets->begin = offsets->begin
2026 + config->cb.subbuffer_header_size();
2027 /* Test new buffer integrity */
2028 sb_index = subbuf_index(offsets->begin, chan);
2029 /*
2030 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2031 * lib_ring_buffer_check_deliver() has the matching
2032 * memory barriers required around commit_cold cc_sb
2033 * updates to ensure reserve and commit counter updates
2034 * are not seen reordered when updated by another CPU.
2035 */
2036 smp_rmb();
2037 commit_count = v_read(config,
2038 &buf->commit_cold[sb_index].cc_sb);
2039 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2040 smp_rmb();
2041 if (unlikely(offset_cmp != v_read(config, &buf->offset))) {
2042 /*
2043 * The reserve counter have been concurrently updated
2044 * while we read the commit counter. This means the
2045 * commit counter we read might not match buf->offset
2046 * due to concurrent update. We therefore need to retry.
2047 */
2048 goto retry;
2049 }
2050 reserve_commit_diff =
2051 (buf_trunc(offsets->begin, chan)
2052 >> chan->backend.num_subbuf_order)
2053 - (commit_count & chan->commit_count_mask);
2054 if (likely(reserve_commit_diff == 0)) {
2055 /* Next subbuffer not being written to. */
2056 if (unlikely(config->mode != RING_BUFFER_OVERWRITE &&
2057 subbuf_trunc(offsets->begin, chan)
2058 - subbuf_trunc((unsigned long)
2059 atomic_long_read(&buf->consumed), chan)
2060 >= chan->backend.buf_size)) {
2061 /*
2062 * We do not overwrite non consumed buffers
2063 * and we are full : record is lost.
2064 */
2065 v_inc(config, &buf->records_lost_full);
2066 return -ENOBUFS;
2067 } else {
2068 /*
2069 * Next subbuffer not being written to, and we
2070 * are either in overwrite mode or the buffer is
2071 * not full. It's safe to write in this new
2072 * subbuffer.
2073 */
2074 }
2075 } else {
2076 /*
2077 * Next subbuffer reserve offset does not match the
2078 * commit offset, and this did not involve update to the
2079 * reserve counter. Drop record in producer-consumer and
2080 * overwrite mode. Caused by either a writer OOPS or
2081 * too many nested writes over a reserve/commit pair.
2082 */
2083 v_inc(config, &buf->records_lost_wrap);
2084 return -EIO;
2085 }
2086 offsets->size =
2087 config->cb.record_header_size(config, chan,
2088 offsets->begin,
2089 &offsets->pre_header_padding,
2090 ctx, client_ctx);
2091 offsets->size +=
2092 lib_ring_buffer_align(offsets->begin + offsets->size,
2093 ctx->largest_align)
2094 + ctx->data_size;
2095 if (unlikely(subbuf_offset(offsets->begin, chan)
2096 + offsets->size > chan->backend.subbuf_size)) {
2097 /*
2098 * Record too big for subbuffers, report error, don't
2099 * complete the sub-buffer switch.
2100 */
2101 v_inc(config, &buf->records_lost_big);
2102 return -ENOSPC;
2103 } else {
2104 /*
2105 * We just made a successful buffer switch and the
2106 * record fits in the new subbuffer. Let's write.
2107 */
2108 }
2109 } else {
2110 /*
2111 * Record fits in the current buffer and we are not on a switch
2112 * boundary. It's safe to write.
2113 */
2114 }
2115 offsets->end = offsets->begin + offsets->size;
2116
2117 if (unlikely(subbuf_offset(offsets->end, chan) == 0)) {
2118 /*
2119 * The offset_end will fall at the very beginning of the next
2120 * subbuffer.
2121 */
2122 offsets->switch_new_end = 1; /* For offsets->begin */
2123 }
2124 return 0;
2125 }
2126
2127 static struct lib_ring_buffer *get_current_buf(struct channel *chan, int cpu)
2128 {
2129 const struct lib_ring_buffer_config *config = &chan->backend.config;
2130
2131 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
2132 return per_cpu_ptr(chan->backend.buf, cpu);
2133 else
2134 return chan->backend.buf;
2135 }
2136
2137 void lib_ring_buffer_lost_event_too_big(struct channel *chan)
2138 {
2139 const struct lib_ring_buffer_config *config = &chan->backend.config;
2140 struct lib_ring_buffer *buf = get_current_buf(chan, smp_processor_id());
2141
2142 v_inc(config, &buf->records_lost_big);
2143 }
2144 EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big);
2145
2146 /**
2147 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2148 * @ctx: ring buffer context.
2149 *
2150 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2151 * -EIO for other errors, else returns 0.
2152 * It will take care of sub-buffer switching.
2153 */
2154 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx,
2155 void *client_ctx)
2156 {
2157 struct channel *chan = ctx->chan;
2158 const struct lib_ring_buffer_config *config = &chan->backend.config;
2159 struct lib_ring_buffer *buf;
2160 struct switch_offsets offsets;
2161 int ret;
2162
2163 ctx->buf = buf = get_current_buf(chan, ctx->cpu);
2164 offsets.size = 0;
2165
2166 do {
2167 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
2168 ctx, client_ctx);
2169 if (unlikely(ret))
2170 return ret;
2171 } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
2172 offsets.end)
2173 != offsets.old));
2174
2175 /*
2176 * Atomically update last_tsc. This update races against concurrent
2177 * atomic updates, but the race will always cause supplementary full TSC
2178 * records, never the opposite (missing a full TSC record when it would
2179 * be needed).
2180 */
2181 save_last_tsc(config, buf, ctx->tsc);
2182
2183 /*
2184 * Push the reader if necessary
2185 */
2186 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
2187
2188 /*
2189 * Clear noref flag for this subbuffer.
2190 */
2191 lib_ring_buffer_clear_noref(config, &buf->backend,
2192 subbuf_index(offsets.end - 1, chan));
2193
2194 /*
2195 * Switch old subbuffer if needed.
2196 */
2197 if (unlikely(offsets.switch_old_end)) {
2198 lib_ring_buffer_clear_noref(config, &buf->backend,
2199 subbuf_index(offsets.old - 1, chan));
2200 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc);
2201 }
2202
2203 /*
2204 * Populate new subbuffer.
2205 */
2206 if (unlikely(offsets.switch_new_start))
2207 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc);
2208
2209 if (unlikely(offsets.switch_new_end))
2210 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc);
2211
2212 ctx->slot_size = offsets.size;
2213 ctx->pre_offset = offsets.begin;
2214 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
2215 return 0;
2216 }
2217 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow);
2218
2219 static
2220 void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config *config,
2221 struct lib_ring_buffer *buf,
2222 unsigned long commit_count,
2223 unsigned long idx)
2224 {
2225 if (config->oops == RING_BUFFER_OOPS_CONSISTENCY)
2226 v_set(config, &buf->commit_hot[idx].seq, commit_count);
2227 }
2228
2229 /*
2230 * The ring buffer can count events recorded and overwritten per buffer,
2231 * but it is disabled by default due to its performance overhead.
2232 */
2233 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2234 static
2235 void deliver_count_events(const struct lib_ring_buffer_config *config,
2236 struct lib_ring_buffer *buf,
2237 unsigned long idx)
2238 {
2239 v_add(config, subbuffer_get_records_count(config,
2240 &buf->backend, idx),
2241 &buf->records_count);
2242 v_add(config, subbuffer_count_records_overrun(config,
2243 &buf->backend, idx),
2244 &buf->records_overrun);
2245 }
2246 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2247 static
2248 void deliver_count_events(const struct lib_ring_buffer_config *config,
2249 struct lib_ring_buffer *buf,
2250 unsigned long idx)
2251 {
2252 }
2253 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2254
2255
2256 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config *config,
2257 struct lib_ring_buffer *buf,
2258 struct channel *chan,
2259 unsigned long offset,
2260 unsigned long commit_count,
2261 unsigned long idx,
2262 u64 tsc)
2263 {
2264 unsigned long old_commit_count = commit_count
2265 - chan->backend.subbuf_size;
2266
2267 /*
2268 * If we succeeded at updating cc_sb below, we are the subbuffer
2269 * writer delivering the subbuffer. Deals with concurrent
2270 * updates of the "cc" value without adding a add_return atomic
2271 * operation to the fast path.
2272 *
2273 * We are doing the delivery in two steps:
2274 * - First, we cmpxchg() cc_sb to the new value
2275 * old_commit_count + 1. This ensures that we are the only
2276 * subbuffer user successfully filling the subbuffer, but we
2277 * do _not_ set the cc_sb value to "commit_count" yet.
2278 * Therefore, other writers that would wrap around the ring
2279 * buffer and try to start writing to our subbuffer would
2280 * have to drop records, because it would appear as
2281 * non-filled.
2282 * We therefore have exclusive access to the subbuffer control
2283 * structures. This mutual exclusion with other writers is
2284 * crucially important to perform record overruns count in
2285 * flight recorder mode locklessly.
2286 * - When we are ready to release the subbuffer (either for
2287 * reading or for overrun by other writers), we simply set the
2288 * cc_sb value to "commit_count" and perform delivery.
2289 *
2290 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2291 * This guarantees that old_commit_count + 1 != commit_count.
2292 */
2293
2294 /*
2295 * Order prior updates to reserve count prior to the
2296 * commit_cold cc_sb update.
2297 */
2298 smp_wmb();
2299 if (likely(v_cmpxchg(config, &buf->commit_cold[idx].cc_sb,
2300 old_commit_count, old_commit_count + 1)
2301 == old_commit_count)) {
2302 u64 *ts_end;
2303
2304 /*
2305 * Start of exclusive subbuffer access. We are
2306 * guaranteed to be the last writer in this subbuffer
2307 * and any other writer trying to access this subbuffer
2308 * in this state is required to drop records.
2309 *
2310 * We can read the ts_end for the current sub-buffer
2311 * which has been saved by the very last space
2312 * reservation for the current sub-buffer.
2313 *
2314 * Order increment of commit counter before reading ts_end.
2315 */
2316 smp_mb();
2317 ts_end = &buf->ts_end[idx];
2318 deliver_count_events(config, buf, idx);
2319 config->cb.buffer_end(buf, *ts_end, idx,
2320 lib_ring_buffer_get_data_size(config,
2321 buf,
2322 idx));
2323
2324 /*
2325 * Increment the packet counter while we have exclusive
2326 * access.
2327 */
2328 subbuffer_inc_packet_count(config, &buf->backend, idx);
2329
2330 /*
2331 * Set noref flag and offset for this subbuffer id.
2332 * Contains a memory barrier that ensures counter stores
2333 * are ordered before set noref and offset.
2334 */
2335 lib_ring_buffer_set_noref_offset(config, &buf->backend, idx,
2336 buf_trunc_val(offset, chan));
2337
2338 /*
2339 * Order set_noref and record counter updates before the
2340 * end of subbuffer exclusive access. Orders with
2341 * respect to writers coming into the subbuffer after
2342 * wrap around, and also order wrt concurrent readers.
2343 */
2344 smp_mb();
2345 /* End of exclusive subbuffer access */
2346 v_set(config, &buf->commit_cold[idx].cc_sb,
2347 commit_count);
2348 /*
2349 * Order later updates to reserve count after
2350 * the commit_cold cc_sb update.
2351 */
2352 smp_wmb();
2353 lib_ring_buffer_vmcore_check_deliver(config, buf,
2354 commit_count, idx);
2355
2356 /*
2357 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2358 */
2359 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER
2360 && atomic_long_read(&buf->active_readers)
2361 && lib_ring_buffer_poll_deliver(config, buf, chan)) {
2362 wake_up_interruptible(&buf->read_wait);
2363 wake_up_interruptible(&chan->read_wait);
2364 }
2365
2366 }
2367 }
2368 EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow);
2369
2370 int __init init_lib_ring_buffer_frontend(void)
2371 {
2372 int cpu;
2373
2374 for_each_possible_cpu(cpu)
2375 spin_lock_init(&per_cpu(ring_buffer_nohz_lock, cpu));
2376 return 0;
2377 }
2378
2379 module_init(init_lib_ring_buffer_frontend);
2380
2381 void __exit exit_lib_ring_buffer_frontend(void)
2382 {
2383 }
2384
2385 module_exit(exit_lib_ring_buffer_frontend);
This page took 0.098622 seconds and 3 git commands to generate.