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