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