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