Fix ring_buffer_frontend.c: missing include lttng-tracer-core.h
[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
58 #include "../../lttng-tracer-core.h"
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
65 /*
66 * Internal structure representing offsets to use at a sub-buffer switch.
67 */
68 struct switch_offsets {
69 unsigned long begin, end, old;
70 size_t pre_header_padding, size;
71 unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1,
72 switch_old_end:1;
73 };
74
75 #ifdef CONFIG_NO_HZ
76 enum tick_nohz_val {
77 TICK_NOHZ_STOP,
78 TICK_NOHZ_FLUSH,
79 TICK_NOHZ_RESTART,
80 };
81
82 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier);
83 #endif /* CONFIG_NO_HZ */
84
85 static DEFINE_PER_CPU(spinlock_t, ring_buffer_nohz_lock);
86
87 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting);
88 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting);
89
90 static
91 void lib_ring_buffer_print_errors(struct channel *chan,
92 struct lib_ring_buffer *buf, int cpu);
93
94 /*
95 * Must be called under cpu hotplug protection.
96 */
97 void lib_ring_buffer_free(struct lib_ring_buffer *buf)
98 {
99 struct channel *chan = buf->backend.chan;
100
101 lib_ring_buffer_print_errors(chan, buf, buf->backend.cpu);
102 kfree(buf->commit_hot);
103 kfree(buf->commit_cold);
104
105 lib_ring_buffer_backend_free(&buf->backend);
106 }
107
108 /**
109 * lib_ring_buffer_reset - Reset ring buffer to initial values.
110 * @buf: Ring buffer.
111 *
112 * Effectively empty the ring buffer. Should be called when the buffer is not
113 * used for writing. The ring buffer can be opened for reading, but the reader
114 * should not be using the iterator concurrently with reset. The previous
115 * current iterator record is reset.
116 */
117 void lib_ring_buffer_reset(struct lib_ring_buffer *buf)
118 {
119 struct channel *chan = buf->backend.chan;
120 const struct lib_ring_buffer_config *config = &chan->backend.config;
121 unsigned int i;
122
123 /*
124 * Reset iterator first. It will put the subbuffer if it currently holds
125 * it.
126 */
127 lib_ring_buffer_iterator_reset(buf);
128 v_set(config, &buf->offset, 0);
129 for (i = 0; i < chan->backend.num_subbuf; i++) {
130 v_set(config, &buf->commit_hot[i].cc, 0);
131 v_set(config, &buf->commit_hot[i].seq, 0);
132 v_set(config, &buf->commit_cold[i].cc_sb, 0);
133 }
134 atomic_long_set(&buf->consumed, 0);
135 atomic_set(&buf->record_disabled, 0);
136 v_set(config, &buf->last_tsc, 0);
137 lib_ring_buffer_backend_reset(&buf->backend);
138 /* Don't reset number of active readers */
139 v_set(config, &buf->records_lost_full, 0);
140 v_set(config, &buf->records_lost_wrap, 0);
141 v_set(config, &buf->records_lost_big, 0);
142 v_set(config, &buf->records_count, 0);
143 v_set(config, &buf->records_overrun, 0);
144 buf->finalized = 0;
145 }
146 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset);
147
148 /**
149 * channel_reset - Reset channel to initial values.
150 * @chan: Channel.
151 *
152 * Effectively empty the channel. Should be called when the channel is not used
153 * for writing. The channel can be opened for reading, but the reader should not
154 * be using the iterator concurrently with reset. The previous current iterator
155 * record is reset.
156 */
157 void channel_reset(struct channel *chan)
158 {
159 /*
160 * Reset iterators first. Will put the subbuffer if held for reading.
161 */
162 channel_iterator_reset(chan);
163 atomic_set(&chan->record_disabled, 0);
164 /* Don't reset commit_count_mask, still valid */
165 channel_backend_reset(&chan->backend);
166 /* Don't reset switch/read timer interval */
167 /* Don't reset notifiers and notifier enable bits */
168 /* Don't reset reader reference count */
169 }
170 EXPORT_SYMBOL_GPL(channel_reset);
171
172 /*
173 * Must be called under cpu hotplug protection.
174 */
175 int lib_ring_buffer_create(struct lib_ring_buffer *buf,
176 struct channel_backend *chanb, int cpu)
177 {
178 const struct lib_ring_buffer_config *config = &chanb->config;
179 struct channel *chan = container_of(chanb, struct channel, backend);
180 void *priv = chanb->priv;
181 size_t subbuf_header_size;
182 u64 tsc;
183 int ret;
184
185 /* Test for cpu hotplug */
186 if (buf->backend.allocated)
187 return 0;
188
189 /*
190 * Paranoia: per cpu dynamic allocation is not officially documented as
191 * zeroing the memory, so let's do it here too, just in case.
192 */
193 memset(buf, 0, sizeof(*buf));
194
195 ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, cpu);
196 if (ret)
197 return ret;
198
199 buf->commit_hot =
200 kzalloc_node(ALIGN(sizeof(*buf->commit_hot)
201 * chan->backend.num_subbuf,
202 1 << INTERNODE_CACHE_SHIFT),
203 GFP_KERNEL, cpu_to_node(max(cpu, 0)));
204 if (!buf->commit_hot) {
205 ret = -ENOMEM;
206 goto free_chanbuf;
207 }
208
209 buf->commit_cold =
210 kzalloc_node(ALIGN(sizeof(*buf->commit_cold)
211 * chan->backend.num_subbuf,
212 1 << INTERNODE_CACHE_SHIFT),
213 GFP_KERNEL, cpu_to_node(max(cpu, 0)));
214 if (!buf->commit_cold) {
215 ret = -ENOMEM;
216 goto free_commit;
217 }
218
219 init_waitqueue_head(&buf->read_wait);
220 init_waitqueue_head(&buf->write_wait);
221 raw_spin_lock_init(&buf->raw_tick_nohz_spinlock);
222
223 /*
224 * Write the subbuffer header for first subbuffer so we know the total
225 * duration of data gathering.
226 */
227 subbuf_header_size = config->cb.subbuffer_header_size();
228 v_set(config, &buf->offset, subbuf_header_size);
229 subbuffer_id_clear_noref(config, &buf->backend.buf_wsb[0].id);
230 tsc = config->cb.ring_buffer_clock_read(buf->backend.chan);
231 config->cb.buffer_begin(buf, tsc, 0);
232 v_add(config, subbuf_header_size, &buf->commit_hot[0].cc);
233
234 if (config->cb.buffer_create) {
235 ret = config->cb.buffer_create(buf, priv, cpu, chanb->name);
236 if (ret)
237 goto free_init;
238 }
239
240 /*
241 * Ensure the buffer is ready before setting it to allocated and setting
242 * the cpumask.
243 * Used for cpu hotplug vs cpumask iteration.
244 */
245 smp_wmb();
246 buf->backend.allocated = 1;
247
248 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
249 CHAN_WARN_ON(chan, cpumask_test_cpu(cpu,
250 chan->backend.cpumask));
251 cpumask_set_cpu(cpu, chan->backend.cpumask);
252 }
253
254 return 0;
255
256 /* Error handling */
257 free_init:
258 kfree(buf->commit_cold);
259 free_commit:
260 kfree(buf->commit_hot);
261 free_chanbuf:
262 lib_ring_buffer_backend_free(&buf->backend);
263 return ret;
264 }
265
266 static void switch_buffer_timer(unsigned long data)
267 {
268 struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data;
269 struct channel *chan = buf->backend.chan;
270 const struct lib_ring_buffer_config *config = &chan->backend.config;
271
272 /*
273 * Only flush buffers periodically if readers are active.
274 */
275 if (atomic_long_read(&buf->active_readers))
276 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
277
278 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
279 mod_timer_pinned(&buf->switch_timer,
280 jiffies + chan->switch_timer_interval);
281 else
282 mod_timer(&buf->switch_timer,
283 jiffies + chan->switch_timer_interval);
284 }
285
286 /*
287 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
288 */
289 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer *buf)
290 {
291 struct channel *chan = buf->backend.chan;
292 const struct lib_ring_buffer_config *config = &chan->backend.config;
293
294 if (!chan->switch_timer_interval || buf->switch_timer_enabled)
295 return;
296 init_timer(&buf->switch_timer);
297 buf->switch_timer.function = switch_buffer_timer;
298 buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
299 buf->switch_timer.data = (unsigned long)buf;
300 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
301 add_timer_on(&buf->switch_timer, buf->backend.cpu);
302 else
303 add_timer(&buf->switch_timer);
304 buf->switch_timer_enabled = 1;
305 }
306
307 /*
308 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
309 */
310 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer *buf)
311 {
312 struct channel *chan = buf->backend.chan;
313
314 if (!chan->switch_timer_interval || !buf->switch_timer_enabled)
315 return;
316
317 del_timer_sync(&buf->switch_timer);
318 buf->switch_timer_enabled = 0;
319 }
320
321 /*
322 * Polling timer to check the channels for data.
323 */
324 static void read_buffer_timer(unsigned long data)
325 {
326 struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data;
327 struct channel *chan = buf->backend.chan;
328 const struct lib_ring_buffer_config *config = &chan->backend.config;
329
330 CHAN_WARN_ON(chan, !buf->backend.allocated);
331
332 if (atomic_long_read(&buf->active_readers)
333 && lib_ring_buffer_poll_deliver(config, buf, chan)) {
334 wake_up_interruptible(&buf->read_wait);
335 wake_up_interruptible(&chan->read_wait);
336 }
337
338 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
339 mod_timer_pinned(&buf->read_timer,
340 jiffies + chan->read_timer_interval);
341 else
342 mod_timer(&buf->read_timer,
343 jiffies + chan->read_timer_interval);
344 }
345
346 /*
347 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
348 */
349 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer *buf)
350 {
351 struct channel *chan = buf->backend.chan;
352 const struct lib_ring_buffer_config *config = &chan->backend.config;
353
354 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
355 || !chan->read_timer_interval
356 || buf->read_timer_enabled)
357 return;
358
359 init_timer(&buf->read_timer);
360 buf->read_timer.function = read_buffer_timer;
361 buf->read_timer.expires = jiffies + chan->read_timer_interval;
362 buf->read_timer.data = (unsigned long)buf;
363
364 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
365 add_timer_on(&buf->read_timer, buf->backend.cpu);
366 else
367 add_timer(&buf->read_timer);
368 buf->read_timer_enabled = 1;
369 }
370
371 /*
372 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
373 */
374 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer *buf)
375 {
376 struct channel *chan = buf->backend.chan;
377 const struct lib_ring_buffer_config *config = &chan->backend.config;
378
379 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
380 || !chan->read_timer_interval
381 || !buf->read_timer_enabled)
382 return;
383
384 del_timer_sync(&buf->read_timer);
385 /*
386 * do one more check to catch data that has been written in the last
387 * timer period.
388 */
389 if (lib_ring_buffer_poll_deliver(config, buf, chan)) {
390 wake_up_interruptible(&buf->read_wait);
391 wake_up_interruptible(&chan->read_wait);
392 }
393 buf->read_timer_enabled = 0;
394 }
395
396 #ifdef CONFIG_HOTPLUG_CPU
397 /**
398 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
399 * @nb: notifier block
400 * @action: hotplug action to take
401 * @hcpu: CPU number
402 *
403 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
404 */
405 static
406 int __cpuinit lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb,
407 unsigned long action,
408 void *hcpu)
409 {
410 unsigned int cpu = (unsigned long)hcpu;
411 struct channel *chan = container_of(nb, struct channel,
412 cpu_hp_notifier);
413 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
414 const struct lib_ring_buffer_config *config = &chan->backend.config;
415
416 if (!chan->cpu_hp_enable)
417 return NOTIFY_DONE;
418
419 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
420
421 switch (action) {
422 case CPU_DOWN_FAILED:
423 case CPU_DOWN_FAILED_FROZEN:
424 case CPU_ONLINE:
425 case CPU_ONLINE_FROZEN:
426 wake_up_interruptible(&chan->hp_wait);
427 lib_ring_buffer_start_switch_timer(buf);
428 lib_ring_buffer_start_read_timer(buf);
429 return NOTIFY_OK;
430
431 case CPU_DOWN_PREPARE:
432 case CPU_DOWN_PREPARE_FROZEN:
433 lib_ring_buffer_stop_switch_timer(buf);
434 lib_ring_buffer_stop_read_timer(buf);
435 return NOTIFY_OK;
436
437 case CPU_DEAD:
438 case CPU_DEAD_FROZEN:
439 /*
440 * Performing a buffer switch on a remote CPU. Performed by
441 * the CPU responsible for doing the hotunplug after the target
442 * CPU stopped running completely. Ensures that all data
443 * from that remote CPU is flushed.
444 */
445 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
446 return NOTIFY_OK;
447
448 default:
449 return NOTIFY_DONE;
450 }
451 }
452 #endif
453
454 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
455 /*
456 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
457 * that wake-up-tracing generated events are flushed before going idle (in
458 * tick_nohz). We test if the spinlock is locked to deal with the race where
459 * readers try to sample the ring buffer before we perform the switch. We let
460 * the readers retry in that case. If there is data in the buffer, the wake up
461 * is going to forbid the CPU running the reader thread from going idle.
462 */
463 static int notrace ring_buffer_tick_nohz_callback(struct notifier_block *nb,
464 unsigned long val,
465 void *data)
466 {
467 struct channel *chan = container_of(nb, struct channel,
468 tick_nohz_notifier);
469 const struct lib_ring_buffer_config *config = &chan->backend.config;
470 struct lib_ring_buffer *buf;
471 int cpu = smp_processor_id();
472
473 if (config->alloc != RING_BUFFER_ALLOC_PER_CPU) {
474 /*
475 * We don't support keeping the system idle with global buffers
476 * and streaming active. In order to do so, we would need to
477 * sample a non-nohz-cpumask racelessly with the nohz updates
478 * without adding synchronization overhead to nohz. Leave this
479 * use-case out for now.
480 */
481 return 0;
482 }
483
484 buf = channel_get_ring_buffer(config, chan, cpu);
485 switch (val) {
486 case TICK_NOHZ_FLUSH:
487 raw_spin_lock(&buf->raw_tick_nohz_spinlock);
488 if (config->wakeup == RING_BUFFER_WAKEUP_BY_TIMER
489 && chan->read_timer_interval
490 && atomic_long_read(&buf->active_readers)
491 && (lib_ring_buffer_poll_deliver(config, buf, chan)
492 || lib_ring_buffer_pending_data(config, buf, chan))) {
493 wake_up_interruptible(&buf->read_wait);
494 wake_up_interruptible(&chan->read_wait);
495 }
496 if (chan->switch_timer_interval)
497 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
498 raw_spin_unlock(&buf->raw_tick_nohz_spinlock);
499 break;
500 case TICK_NOHZ_STOP:
501 spin_lock(&__get_cpu_var(ring_buffer_nohz_lock));
502 lib_ring_buffer_stop_switch_timer(buf);
503 lib_ring_buffer_stop_read_timer(buf);
504 spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock));
505 break;
506 case TICK_NOHZ_RESTART:
507 spin_lock(&__get_cpu_var(ring_buffer_nohz_lock));
508 lib_ring_buffer_start_read_timer(buf);
509 lib_ring_buffer_start_switch_timer(buf);
510 spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock));
511 break;
512 }
513
514 return 0;
515 }
516
517 void notrace lib_ring_buffer_tick_nohz_flush(void)
518 {
519 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_FLUSH,
520 NULL);
521 }
522
523 void notrace lib_ring_buffer_tick_nohz_stop(void)
524 {
525 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_STOP,
526 NULL);
527 }
528
529 void notrace lib_ring_buffer_tick_nohz_restart(void)
530 {
531 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_RESTART,
532 NULL);
533 }
534 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
535
536 /*
537 * Holds CPU hotplug.
538 */
539 static void channel_unregister_notifiers(struct channel *chan)
540 {
541 const struct lib_ring_buffer_config *config = &chan->backend.config;
542 int cpu;
543
544 channel_iterator_unregister_notifiers(chan);
545 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
546 #ifdef CONFIG_NO_HZ
547 /*
548 * Remove the nohz notifier first, so we are certain we stop
549 * the timers.
550 */
551 atomic_notifier_chain_unregister(&tick_nohz_notifier,
552 &chan->tick_nohz_notifier);
553 /*
554 * ring_buffer_nohz_lock will not be needed below, because
555 * we just removed the notifiers, which were the only source of
556 * concurrency.
557 */
558 #endif /* CONFIG_NO_HZ */
559 #ifdef CONFIG_HOTPLUG_CPU
560 get_online_cpus();
561 chan->cpu_hp_enable = 0;
562 for_each_online_cpu(cpu) {
563 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
564 cpu);
565 lib_ring_buffer_stop_switch_timer(buf);
566 lib_ring_buffer_stop_read_timer(buf);
567 }
568 put_online_cpus();
569 unregister_cpu_notifier(&chan->cpu_hp_notifier);
570 #else
571 for_each_possible_cpu(cpu) {
572 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
573 cpu);
574 lib_ring_buffer_stop_switch_timer(buf);
575 lib_ring_buffer_stop_read_timer(buf);
576 }
577 #endif
578 } else {
579 struct lib_ring_buffer *buf = chan->backend.buf;
580
581 lib_ring_buffer_stop_switch_timer(buf);
582 lib_ring_buffer_stop_read_timer(buf);
583 }
584 channel_backend_unregister_notifiers(&chan->backend);
585 }
586
587 static void channel_free(struct channel *chan)
588 {
589 channel_iterator_free(chan);
590 channel_backend_free(&chan->backend);
591 kfree(chan);
592 }
593
594 /**
595 * channel_create - Create channel.
596 * @config: ring buffer instance configuration
597 * @name: name of the channel
598 * @priv: ring buffer client private data
599 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
600 * address mapping. It is used only by RING_BUFFER_STATIC
601 * configuration. It can be set to NULL for other backends.
602 * @subbuf_size: subbuffer size
603 * @num_subbuf: number of subbuffers
604 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
605 * padding to let readers get those sub-buffers.
606 * Used for live streaming.
607 * @read_timer_interval: Time interval (in us) to wake up pending readers.
608 *
609 * Holds cpu hotplug.
610 * Returns NULL on failure.
611 */
612 struct channel *channel_create(const struct lib_ring_buffer_config *config,
613 const char *name, void *priv, void *buf_addr,
614 size_t subbuf_size,
615 size_t num_subbuf, unsigned int switch_timer_interval,
616 unsigned int read_timer_interval)
617 {
618 int ret, cpu;
619 struct channel *chan;
620
621 if (lib_ring_buffer_check_config(config, switch_timer_interval,
622 read_timer_interval))
623 return NULL;
624
625 chan = kzalloc(sizeof(struct channel), GFP_KERNEL);
626 if (!chan)
627 return NULL;
628
629 ret = channel_backend_init(&chan->backend, name, config, priv,
630 subbuf_size, num_subbuf);
631 if (ret)
632 goto error;
633
634 ret = channel_iterator_init(chan);
635 if (ret)
636 goto error_free_backend;
637
638 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
639 chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
640 chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
641 kref_init(&chan->ref);
642 init_waitqueue_head(&chan->read_wait);
643 init_waitqueue_head(&chan->hp_wait);
644
645 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
646 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
647 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
648 chan->tick_nohz_notifier.notifier_call =
649 ring_buffer_tick_nohz_callback;
650 chan->tick_nohz_notifier.priority = ~0U;
651 atomic_notifier_chain_register(&tick_nohz_notifier,
652 &chan->tick_nohz_notifier);
653 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
654
655 /*
656 * In case of non-hotplug cpu, if the ring-buffer is allocated
657 * in early initcall, it will not be notified of secondary cpus.
658 * In that off case, we need to allocate for all possible cpus.
659 */
660 #ifdef CONFIG_HOTPLUG_CPU
661 chan->cpu_hp_notifier.notifier_call =
662 lib_ring_buffer_cpu_hp_callback;
663 chan->cpu_hp_notifier.priority = 6;
664 register_cpu_notifier(&chan->cpu_hp_notifier);
665
666 get_online_cpus();
667 for_each_online_cpu(cpu) {
668 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
669 cpu);
670 spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu));
671 lib_ring_buffer_start_switch_timer(buf);
672 lib_ring_buffer_start_read_timer(buf);
673 spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu));
674 }
675 chan->cpu_hp_enable = 1;
676 put_online_cpus();
677 #else
678 for_each_possible_cpu(cpu) {
679 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
680 cpu);
681 spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu));
682 lib_ring_buffer_start_switch_timer(buf);
683 lib_ring_buffer_start_read_timer(buf);
684 spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu));
685 }
686 #endif
687 } else {
688 struct lib_ring_buffer *buf = chan->backend.buf;
689
690 lib_ring_buffer_start_switch_timer(buf);
691 lib_ring_buffer_start_read_timer(buf);
692 }
693
694 return chan;
695
696 error_free_backend:
697 channel_backend_free(&chan->backend);
698 error:
699 kfree(chan);
700 return NULL;
701 }
702 EXPORT_SYMBOL_GPL(channel_create);
703
704 static
705 void channel_release(struct kref *kref)
706 {
707 struct channel *chan = container_of(kref, struct channel, ref);
708 channel_free(chan);
709 }
710
711 /**
712 * channel_destroy - Finalize, wait for q.s. and destroy channel.
713 * @chan: channel to destroy
714 *
715 * Holds cpu hotplug.
716 * Call "destroy" callback, finalize channels, and then decrement the
717 * channel reference count. Note that when readers have completed data
718 * consumption of finalized channels, get_subbuf() will return -ENODATA.
719 * They should release their handle at that point. Returns the private
720 * data pointer.
721 */
722 void *channel_destroy(struct channel *chan)
723 {
724 int cpu;
725 const struct lib_ring_buffer_config *config = &chan->backend.config;
726 void *priv;
727
728 channel_unregister_notifiers(chan);
729
730 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
731 /*
732 * No need to hold cpu hotplug, because all notifiers have been
733 * unregistered.
734 */
735 for_each_channel_cpu(cpu, chan) {
736 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
737 cpu);
738
739 if (config->cb.buffer_finalize)
740 config->cb.buffer_finalize(buf,
741 chan->backend.priv,
742 cpu);
743 if (buf->backend.allocated)
744 lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH);
745 /*
746 * Perform flush before writing to finalized.
747 */
748 smp_wmb();
749 ACCESS_ONCE(buf->finalized) = 1;
750 wake_up_interruptible(&buf->read_wait);
751 }
752 } else {
753 struct lib_ring_buffer *buf = chan->backend.buf;
754
755 if (config->cb.buffer_finalize)
756 config->cb.buffer_finalize(buf, chan->backend.priv, -1);
757 if (buf->backend.allocated)
758 lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH);
759 /*
760 * Perform flush before writing to finalized.
761 */
762 smp_wmb();
763 ACCESS_ONCE(buf->finalized) = 1;
764 wake_up_interruptible(&buf->read_wait);
765 }
766 ACCESS_ONCE(chan->finalized) = 1;
767 wake_up_interruptible(&chan->hp_wait);
768 wake_up_interruptible(&chan->read_wait);
769 priv = chan->backend.priv;
770 kref_put(&chan->ref, channel_release);
771 return priv;
772 }
773 EXPORT_SYMBOL_GPL(channel_destroy);
774
775 struct lib_ring_buffer *channel_get_ring_buffer(
776 const struct lib_ring_buffer_config *config,
777 struct channel *chan, int cpu)
778 {
779 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL)
780 return chan->backend.buf;
781 else
782 return per_cpu_ptr(chan->backend.buf, cpu);
783 }
784 EXPORT_SYMBOL_GPL(channel_get_ring_buffer);
785
786 int lib_ring_buffer_open_read(struct lib_ring_buffer *buf)
787 {
788 struct channel *chan = buf->backend.chan;
789
790 if (!atomic_long_add_unless(&buf->active_readers, 1, 1))
791 return -EBUSY;
792 kref_get(&chan->ref);
793 smp_mb__after_atomic_inc();
794 return 0;
795 }
796 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read);
797
798 void lib_ring_buffer_release_read(struct lib_ring_buffer *buf)
799 {
800 struct channel *chan = buf->backend.chan;
801
802 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
803 smp_mb__before_atomic_dec();
804 atomic_long_dec(&buf->active_readers);
805 kref_put(&chan->ref, channel_release);
806 }
807 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read);
808
809 /*
810 * Promote compiler barrier to a smp_mb().
811 * For the specific ring buffer case, this IPI call should be removed if the
812 * architecture does not reorder writes. This should eventually be provided by
813 * a separate architecture-specific infrastructure.
814 */
815 static void remote_mb(void *info)
816 {
817 smp_mb();
818 }
819
820 /**
821 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
822 * @buf: ring buffer
823 * @consumed: consumed count indicating the position where to read
824 * @produced: produced count, indicates position when to stop reading
825 *
826 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
827 * data to read at consumed position, or 0 if the get operation succeeds.
828 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
829 */
830
831 int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf,
832 unsigned long *consumed, unsigned long *produced)
833 {
834 struct channel *chan = buf->backend.chan;
835 const struct lib_ring_buffer_config *config = &chan->backend.config;
836 unsigned long consumed_cur, write_offset;
837 int finalized;
838
839 retry:
840 finalized = ACCESS_ONCE(buf->finalized);
841 /*
842 * Read finalized before counters.
843 */
844 smp_rmb();
845 consumed_cur = atomic_long_read(&buf->consumed);
846 /*
847 * No need to issue a memory barrier between consumed count read and
848 * write offset read, because consumed count can only change
849 * concurrently in overwrite mode, and we keep a sequence counter
850 * identifier derived from the write offset to check we are getting
851 * the same sub-buffer we are expecting (the sub-buffers are atomically
852 * "tagged" upon writes, tags are checked upon read).
853 */
854 write_offset = v_read(config, &buf->offset);
855
856 /*
857 * Check that we are not about to read the same subbuffer in
858 * which the writer head is.
859 */
860 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
861 == 0)
862 goto nodata;
863
864 *consumed = consumed_cur;
865 *produced = subbuf_trunc(write_offset, chan);
866
867 return 0;
868
869 nodata:
870 /*
871 * The memory barriers __wait_event()/wake_up_interruptible() take care
872 * of "raw_spin_is_locked" memory ordering.
873 */
874 if (finalized)
875 return -ENODATA;
876 else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
877 goto retry;
878 else
879 return -EAGAIN;
880 }
881 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot);
882
883 /**
884 * lib_ring_buffer_put_snapshot - move consumed counter forward
885 *
886 * Should only be called from consumer context.
887 * @buf: ring buffer
888 * @consumed_new: new consumed count value
889 */
890 void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf,
891 unsigned long consumed_new)
892 {
893 struct lib_ring_buffer_backend *bufb = &buf->backend;
894 struct channel *chan = bufb->chan;
895 unsigned long consumed;
896
897 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
898
899 /*
900 * Only push the consumed value forward.
901 * If the consumed cmpxchg fails, this is because we have been pushed by
902 * the writer in flight recorder mode.
903 */
904 consumed = atomic_long_read(&buf->consumed);
905 while ((long) consumed - (long) consumed_new < 0)
906 consumed = atomic_long_cmpxchg(&buf->consumed, consumed,
907 consumed_new);
908 /* Wake-up the metadata producer */
909 wake_up_interruptible(&buf->write_wait);
910 }
911 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer);
912
913 /**
914 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
915 * @buf: ring buffer
916 * @consumed: consumed count indicating the position where to read
917 *
918 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
919 * data to read at consumed position, or 0 if the get operation succeeds.
920 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
921 */
922 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf,
923 unsigned long consumed)
924 {
925 struct channel *chan = buf->backend.chan;
926 const struct lib_ring_buffer_config *config = &chan->backend.config;
927 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
928 int ret;
929 int finalized;
930
931 retry:
932 finalized = ACCESS_ONCE(buf->finalized);
933 /*
934 * Read finalized before counters.
935 */
936 smp_rmb();
937 consumed_cur = atomic_long_read(&buf->consumed);
938 consumed_idx = subbuf_index(consumed, chan);
939 commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb);
940 /*
941 * Make sure we read the commit count before reading the buffer
942 * data and the write offset. Correct consumed offset ordering
943 * wrt commit count is insured by the use of cmpxchg to update
944 * the consumed offset.
945 * smp_call_function_single can fail if the remote CPU is offline,
946 * this is OK because then there is no wmb to execute there.
947 * If our thread is executing on the same CPU as the on the buffers
948 * belongs to, we don't have to synchronize it at all. If we are
949 * migrated, the scheduler will take care of the memory barriers.
950 * Normally, smp_call_function_single() should ensure program order when
951 * executing the remote function, which implies that it surrounds the
952 * function execution with :
953 * smp_mb()
954 * send IPI
955 * csd_lock_wait
956 * recv IPI
957 * smp_mb()
958 * exec. function
959 * smp_mb()
960 * csd unlock
961 * smp_mb()
962 *
963 * However, smp_call_function_single() does not seem to clearly execute
964 * such barriers. It depends on spinlock semantic to provide the barrier
965 * before executing the IPI and, when busy-looping, csd_lock_wait only
966 * executes smp_mb() when it has to wait for the other CPU.
967 *
968 * I don't trust this code. Therefore, let's add the smp_mb() sequence
969 * required ourself, even if duplicated. It has no performance impact
970 * anyway.
971 *
972 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
973 * read and write vs write. They do not ensure core synchronization. We
974 * really have to ensure total order between the 3 barriers running on
975 * the 2 CPUs.
976 */
977 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
978 if (config->sync == RING_BUFFER_SYNC_PER_CPU
979 && config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
980 if (raw_smp_processor_id() != buf->backend.cpu) {
981 /* Total order with IPI handler smp_mb() */
982 smp_mb();
983 smp_call_function_single(buf->backend.cpu,
984 remote_mb, NULL, 1);
985 /* Total order with IPI handler smp_mb() */
986 smp_mb();
987 }
988 } else {
989 /* Total order with IPI handler smp_mb() */
990 smp_mb();
991 smp_call_function(remote_mb, NULL, 1);
992 /* Total order with IPI handler smp_mb() */
993 smp_mb();
994 }
995 } else {
996 /*
997 * Local rmb to match the remote wmb to read the commit count
998 * before the buffer data and the write offset.
999 */
1000 smp_rmb();
1001 }
1002
1003 write_offset = v_read(config, &buf->offset);
1004
1005 /*
1006 * Check that the buffer we are getting is after or at consumed_cur
1007 * position.
1008 */
1009 if ((long) subbuf_trunc(consumed, chan)
1010 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1011 goto nodata;
1012
1013 /*
1014 * Check that the subbuffer we are trying to consume has been
1015 * already fully committed.
1016 */
1017 if (((commit_count - chan->backend.subbuf_size)
1018 & chan->commit_count_mask)
1019 - (buf_trunc(consumed_cur, chan)
1020 >> chan->backend.num_subbuf_order)
1021 != 0)
1022 goto nodata;
1023
1024 /*
1025 * Check that we are not about to read the same subbuffer in
1026 * which the writer head is.
1027 */
1028 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1029 == 0)
1030 goto nodata;
1031
1032 /*
1033 * Failure to get the subbuffer causes a busy-loop retry without going
1034 * to a wait queue. These are caused by short-lived race windows where
1035 * the writer is getting access to a subbuffer we were trying to get
1036 * access to. Also checks that the "consumed" buffer count we are
1037 * looking for matches the one contained in the subbuffer id.
1038 */
1039 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1040 consumed_idx, buf_trunc_val(consumed, chan));
1041 if (ret)
1042 goto retry;
1043 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1044
1045 buf->get_subbuf_consumed = consumed;
1046 buf->get_subbuf = 1;
1047
1048 return 0;
1049
1050 nodata:
1051 /*
1052 * The memory barriers __wait_event()/wake_up_interruptible() take care
1053 * of "raw_spin_is_locked" memory ordering.
1054 */
1055 if (finalized)
1056 return -ENODATA;
1057 else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1058 goto retry;
1059 else
1060 return -EAGAIN;
1061 }
1062 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf);
1063
1064 /**
1065 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1066 * @buf: ring buffer
1067 */
1068 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf)
1069 {
1070 struct lib_ring_buffer_backend *bufb = &buf->backend;
1071 struct channel *chan = bufb->chan;
1072 const struct lib_ring_buffer_config *config = &chan->backend.config;
1073 unsigned long read_sb_bindex, consumed_idx, consumed;
1074
1075 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
1076
1077 if (!buf->get_subbuf) {
1078 /*
1079 * Reader puts a subbuffer it did not get.
1080 */
1081 CHAN_WARN_ON(chan, 1);
1082 return;
1083 }
1084 consumed = buf->get_subbuf_consumed;
1085 buf->get_subbuf = 0;
1086
1087 /*
1088 * Clear the records_unread counter. (overruns counter)
1089 * Can still be non-zero if a file reader simply grabbed the data
1090 * without using iterators.
1091 * Can be below zero if an iterator is used on a snapshot more than
1092 * once.
1093 */
1094 read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1095 v_add(config, v_read(config,
1096 &bufb->array[read_sb_bindex]->records_unread),
1097 &bufb->records_read);
1098 v_set(config, &bufb->array[read_sb_bindex]->records_unread, 0);
1099 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1100 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1101 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1102
1103 /*
1104 * Exchange the reader subbuffer with the one we put in its place in the
1105 * writer subbuffer table. Expect the original consumed count. If
1106 * update_read_sb_index fails, this is because the writer updated the
1107 * subbuffer concurrently. We should therefore keep the subbuffer we
1108 * currently have: it has become invalid to try reading this sub-buffer
1109 * consumed count value anyway.
1110 */
1111 consumed_idx = subbuf_index(consumed, chan);
1112 update_read_sb_index(config, &buf->backend, &chan->backend,
1113 consumed_idx, buf_trunc_val(consumed, chan));
1114 /*
1115 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1116 * if the writer concurrently updated it.
1117 */
1118 }
1119 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf);
1120
1121 /*
1122 * cons_offset is an iterator on all subbuffer offsets between the reader
1123 * position and the writer position. (inclusive)
1124 */
1125 static
1126 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf,
1127 struct channel *chan,
1128 unsigned long cons_offset,
1129 int cpu)
1130 {
1131 const struct lib_ring_buffer_config *config = &chan->backend.config;
1132 unsigned long cons_idx, commit_count, commit_count_sb;
1133
1134 cons_idx = subbuf_index(cons_offset, chan);
1135 commit_count = v_read(config, &buf->commit_hot[cons_idx].cc);
1136 commit_count_sb = v_read(config, &buf->commit_cold[cons_idx].cc_sb);
1137
1138 if (subbuf_offset(commit_count, chan) != 0)
1139 printk(KERN_WARNING
1140 "ring buffer %s, cpu %d: "
1141 "commit count in subbuffer %lu,\n"
1142 "expecting multiples of %lu bytes\n"
1143 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1144 chan->backend.name, cpu, cons_idx,
1145 chan->backend.subbuf_size,
1146 commit_count, commit_count_sb);
1147
1148 printk(KERN_DEBUG "ring buffer: %s, cpu %d: %lu bytes committed\n",
1149 chan->backend.name, cpu, commit_count);
1150 }
1151
1152 static
1153 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf,
1154 struct channel *chan,
1155 void *priv, int cpu)
1156 {
1157 const struct lib_ring_buffer_config *config = &chan->backend.config;
1158 unsigned long write_offset, cons_offset;
1159
1160 /*
1161 * No need to order commit_count, write_offset and cons_offset reads
1162 * because we execute at teardown when no more writer nor reader
1163 * references are left.
1164 */
1165 write_offset = v_read(config, &buf->offset);
1166 cons_offset = atomic_long_read(&buf->consumed);
1167 if (write_offset != cons_offset)
1168 printk(KERN_DEBUG
1169 "ring buffer %s, cpu %d: "
1170 "non-consumed data\n"
1171 " [ %lu bytes written, %lu bytes read ]\n",
1172 chan->backend.name, cpu, write_offset, cons_offset);
1173
1174 for (cons_offset = atomic_long_read(&buf->consumed);
1175 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1176 chan)
1177 - cons_offset) > 0;
1178 cons_offset = subbuf_align(cons_offset, chan))
1179 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1180 cpu);
1181 }
1182
1183 static
1184 void lib_ring_buffer_print_errors(struct channel *chan,
1185 struct lib_ring_buffer *buf, int cpu)
1186 {
1187 const struct lib_ring_buffer_config *config = &chan->backend.config;
1188 void *priv = chan->backend.priv;
1189
1190 if (!strcmp(chan->backend.name, "relay-metadata")) {
1191 printk(KERN_DEBUG "ring buffer %s: %lu records written, "
1192 "%lu records overrun\n",
1193 chan->backend.name,
1194 v_read(config, &buf->records_count),
1195 v_read(config, &buf->records_overrun));
1196 } else {
1197 printk(KERN_DEBUG "ring buffer %s, cpu %d: %lu records written, "
1198 "%lu records overrun\n",
1199 chan->backend.name, cpu,
1200 v_read(config, &buf->records_count),
1201 v_read(config, &buf->records_overrun));
1202
1203 if (v_read(config, &buf->records_lost_full)
1204 || v_read(config, &buf->records_lost_wrap)
1205 || v_read(config, &buf->records_lost_big))
1206 printk(KERN_WARNING
1207 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1208 " [ %lu buffer full, %lu nest buffer wrap-around, "
1209 "%lu event too big ]\n",
1210 chan->backend.name, cpu,
1211 v_read(config, &buf->records_lost_full),
1212 v_read(config, &buf->records_lost_wrap),
1213 v_read(config, &buf->records_lost_big));
1214 }
1215 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu);
1216 }
1217
1218 /*
1219 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1220 *
1221 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1222 */
1223 static
1224 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf,
1225 struct channel *chan,
1226 struct switch_offsets *offsets,
1227 u64 tsc)
1228 {
1229 const struct lib_ring_buffer_config *config = &chan->backend.config;
1230 unsigned long oldidx = subbuf_index(offsets->old, chan);
1231 unsigned long commit_count;
1232
1233 config->cb.buffer_begin(buf, tsc, oldidx);
1234
1235 /*
1236 * Order all writes to buffer before the commit count update that will
1237 * determine that the subbuffer is full.
1238 */
1239 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1240 /*
1241 * Must write slot data before incrementing commit count. This
1242 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1243 * by get_subbuf().
1244 */
1245 barrier();
1246 } else
1247 smp_wmb();
1248 v_add(config, config->cb.subbuffer_header_size(),
1249 &buf->commit_hot[oldidx].cc);
1250 commit_count = v_read(config, &buf->commit_hot[oldidx].cc);
1251 /* Check if the written buffer has to be delivered */
1252 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1253 commit_count, oldidx);
1254 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1255 offsets->old, commit_count,
1256 config->cb.subbuffer_header_size());
1257 }
1258
1259 /*
1260 * lib_ring_buffer_switch_old_end: switch old subbuffer
1261 *
1262 * Note : offset_old should never be 0 here. It is ok, because we never perform
1263 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1264 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1265 * subbuffer.
1266 */
1267 static
1268 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf,
1269 struct channel *chan,
1270 struct switch_offsets *offsets,
1271 u64 tsc)
1272 {
1273 const struct lib_ring_buffer_config *config = &chan->backend.config;
1274 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1275 unsigned long commit_count, padding_size, data_size;
1276
1277 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1278 padding_size = chan->backend.subbuf_size - data_size;
1279 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size);
1280
1281 /*
1282 * Order all writes to buffer before the commit count update that will
1283 * determine that the subbuffer is full.
1284 */
1285 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1286 /*
1287 * Must write slot data before incrementing commit count. This
1288 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1289 * by get_subbuf().
1290 */
1291 barrier();
1292 } else
1293 smp_wmb();
1294 v_add(config, padding_size, &buf->commit_hot[oldidx].cc);
1295 commit_count = v_read(config, &buf->commit_hot[oldidx].cc);
1296 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1297 commit_count, oldidx);
1298 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1299 offsets->old, commit_count,
1300 padding_size);
1301 }
1302
1303 /*
1304 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1305 *
1306 * This code can be executed unordered : writers may already have written to the
1307 * sub-buffer before this code gets executed, caution. The commit makes sure
1308 * that this code is executed before the deliver of this sub-buffer.
1309 */
1310 static
1311 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf,
1312 struct channel *chan,
1313 struct switch_offsets *offsets,
1314 u64 tsc)
1315 {
1316 const struct lib_ring_buffer_config *config = &chan->backend.config;
1317 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1318 unsigned long commit_count;
1319
1320 config->cb.buffer_begin(buf, tsc, beginidx);
1321
1322 /*
1323 * Order all writes to buffer before the commit count update that will
1324 * determine that the subbuffer is full.
1325 */
1326 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1327 /*
1328 * Must write slot data before incrementing commit count. This
1329 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1330 * by get_subbuf().
1331 */
1332 barrier();
1333 } else
1334 smp_wmb();
1335 v_add(config, config->cb.subbuffer_header_size(),
1336 &buf->commit_hot[beginidx].cc);
1337 commit_count = v_read(config, &buf->commit_hot[beginidx].cc);
1338 /* Check if the written buffer has to be delivered */
1339 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1340 commit_count, beginidx);
1341 lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx,
1342 offsets->begin, commit_count,
1343 config->cb.subbuffer_header_size());
1344 }
1345
1346 /*
1347 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1348 *
1349 * The only remaining threads could be the ones with pending commits. They will
1350 * have to do the deliver themselves.
1351 */
1352 static
1353 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf,
1354 struct channel *chan,
1355 struct switch_offsets *offsets,
1356 u64 tsc)
1357 {
1358 const struct lib_ring_buffer_config *config = &chan->backend.config;
1359 unsigned long endidx = subbuf_index(offsets->end - 1, chan);
1360 unsigned long commit_count, padding_size, data_size;
1361
1362 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1363 padding_size = chan->backend.subbuf_size - data_size;
1364 subbuffer_set_data_size(config, &buf->backend, endidx, data_size);
1365
1366 /*
1367 * Order all writes to buffer before the commit count update that will
1368 * determine that the subbuffer is full.
1369 */
1370 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1371 /*
1372 * Must write slot data before incrementing commit count. This
1373 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1374 * by get_subbuf().
1375 */
1376 barrier();
1377 } else
1378 smp_wmb();
1379 v_add(config, padding_size, &buf->commit_hot[endidx].cc);
1380 commit_count = v_read(config, &buf->commit_hot[endidx].cc);
1381 lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1,
1382 commit_count, endidx);
1383 lib_ring_buffer_write_commit_counter(config, buf, chan, endidx,
1384 offsets->end, commit_count,
1385 padding_size);
1386 }
1387
1388 /*
1389 * Returns :
1390 * 0 if ok
1391 * !0 if execution must be aborted.
1392 */
1393 static
1394 int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
1395 struct lib_ring_buffer *buf,
1396 struct channel *chan,
1397 struct switch_offsets *offsets,
1398 u64 *tsc)
1399 {
1400 const struct lib_ring_buffer_config *config = &chan->backend.config;
1401 unsigned long off;
1402
1403 offsets->begin = v_read(config, &buf->offset);
1404 offsets->old = offsets->begin;
1405 offsets->switch_old_start = 0;
1406 off = subbuf_offset(offsets->begin, chan);
1407
1408 *tsc = config->cb.ring_buffer_clock_read(chan);
1409
1410 /*
1411 * Ensure we flush the header of an empty subbuffer when doing the
1412 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1413 * total data gathering duration even if there were no records saved
1414 * after the last buffer switch.
1415 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1416 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1417 * subbuffer header as appropriate.
1418 * The next record that reserves space will be responsible for
1419 * populating the following subbuffer header. We choose not to populate
1420 * the next subbuffer header here because we want to be able to use
1421 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1422 * buffer flush, which must guarantee that all the buffer content
1423 * (records and header timestamps) are visible to the reader. This is
1424 * required for quiescence guarantees for the fusion merge.
1425 */
1426 if (mode == SWITCH_FLUSH || off > 0) {
1427 if (unlikely(off == 0)) {
1428 /*
1429 * The client does not save any header information.
1430 * Don't switch empty subbuffer on finalize, because it
1431 * is invalid to deliver a completely empty subbuffer.
1432 */
1433 if (!config->cb.subbuffer_header_size())
1434 return -1;
1435 /*
1436 * Need to write the subbuffer start header on finalize.
1437 */
1438 offsets->switch_old_start = 1;
1439 }
1440 offsets->begin = subbuf_align(offsets->begin, chan);
1441 } else
1442 return -1; /* we do not have to switch : buffer is empty */
1443 /* Note: old points to the next subbuf at offset 0 */
1444 offsets->end = offsets->begin;
1445 return 0;
1446 }
1447
1448 /*
1449 * Force a sub-buffer switch. This operation is completely reentrant : can be
1450 * called while tracing is active with absolutely no lock held.
1451 *
1452 * Note, however, that as a v_cmpxchg is used for some atomic
1453 * operations, this function must be called from the CPU which owns the buffer
1454 * for a ACTIVE flush.
1455 */
1456 void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode)
1457 {
1458 struct channel *chan = buf->backend.chan;
1459 const struct lib_ring_buffer_config *config = &chan->backend.config;
1460 struct switch_offsets offsets;
1461 unsigned long oldidx;
1462 u64 tsc;
1463
1464 offsets.size = 0;
1465
1466 /*
1467 * Perform retryable operations.
1468 */
1469 do {
1470 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
1471 &tsc))
1472 return; /* Switch not needed */
1473 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
1474 != offsets.old);
1475
1476 /*
1477 * Atomically update last_tsc. This update races against concurrent
1478 * atomic updates, but the race will always cause supplementary full TSC
1479 * records, never the opposite (missing a full TSC record when it would
1480 * be needed).
1481 */
1482 save_last_tsc(config, buf, tsc);
1483
1484 /*
1485 * Push the reader if necessary
1486 */
1487 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
1488
1489 oldidx = subbuf_index(offsets.old, chan);
1490 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx);
1491
1492 /*
1493 * May need to populate header start on SWITCH_FLUSH.
1494 */
1495 if (offsets.switch_old_start) {
1496 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc);
1497 offsets.old += config->cb.subbuffer_header_size();
1498 }
1499
1500 /*
1501 * Switch old subbuffer.
1502 */
1503 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc);
1504 }
1505 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow);
1506
1507 /*
1508 * Returns :
1509 * 0 if ok
1510 * -ENOSPC if event size is too large for packet.
1511 * -ENOBUFS if there is currently not enough space in buffer for the event.
1512 * -EIO if data cannot be written into the buffer for any other reason.
1513 */
1514 static
1515 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf,
1516 struct channel *chan,
1517 struct switch_offsets *offsets,
1518 struct lib_ring_buffer_ctx *ctx)
1519 {
1520 const struct lib_ring_buffer_config *config = &chan->backend.config;
1521 unsigned long reserve_commit_diff;
1522
1523 offsets->begin = v_read(config, &buf->offset);
1524 offsets->old = offsets->begin;
1525 offsets->switch_new_start = 0;
1526 offsets->switch_new_end = 0;
1527 offsets->switch_old_end = 0;
1528 offsets->pre_header_padding = 0;
1529
1530 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
1531 if ((int64_t) ctx->tsc == -EIO)
1532 return -EIO;
1533
1534 if (last_tsc_overflow(config, buf, ctx->tsc))
1535 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
1536
1537 if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
1538 offsets->switch_new_start = 1; /* For offsets->begin */
1539 } else {
1540 offsets->size = config->cb.record_header_size(config, chan,
1541 offsets->begin,
1542 &offsets->pre_header_padding,
1543 ctx);
1544 offsets->size +=
1545 lib_ring_buffer_align(offsets->begin + offsets->size,
1546 ctx->largest_align)
1547 + ctx->data_size;
1548 if (unlikely(subbuf_offset(offsets->begin, chan) +
1549 offsets->size > chan->backend.subbuf_size)) {
1550 offsets->switch_old_end = 1; /* For offsets->old */
1551 offsets->switch_new_start = 1; /* For offsets->begin */
1552 }
1553 }
1554 if (unlikely(offsets->switch_new_start)) {
1555 unsigned long sb_index;
1556
1557 /*
1558 * We are typically not filling the previous buffer completely.
1559 */
1560 if (likely(offsets->switch_old_end))
1561 offsets->begin = subbuf_align(offsets->begin, chan);
1562 offsets->begin = offsets->begin
1563 + config->cb.subbuffer_header_size();
1564 /* Test new buffer integrity */
1565 sb_index = subbuf_index(offsets->begin, chan);
1566 reserve_commit_diff =
1567 (buf_trunc(offsets->begin, chan)
1568 >> chan->backend.num_subbuf_order)
1569 - ((unsigned long) v_read(config,
1570 &buf->commit_cold[sb_index].cc_sb)
1571 & chan->commit_count_mask);
1572 if (likely(reserve_commit_diff == 0)) {
1573 /* Next subbuffer not being written to. */
1574 if (unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1575 subbuf_trunc(offsets->begin, chan)
1576 - subbuf_trunc((unsigned long)
1577 atomic_long_read(&buf->consumed), chan)
1578 >= chan->backend.buf_size)) {
1579 /*
1580 * We do not overwrite non consumed buffers
1581 * and we are full : record is lost.
1582 */
1583 v_inc(config, &buf->records_lost_full);
1584 return -ENOBUFS;
1585 } else {
1586 /*
1587 * Next subbuffer not being written to, and we
1588 * are either in overwrite mode or the buffer is
1589 * not full. It's safe to write in this new
1590 * subbuffer.
1591 */
1592 }
1593 } else {
1594 /*
1595 * Next subbuffer reserve offset does not match the
1596 * commit offset. Drop record in producer-consumer and
1597 * overwrite mode. Caused by either a writer OOPS or too
1598 * many nested writes over a reserve/commit pair.
1599 */
1600 v_inc(config, &buf->records_lost_wrap);
1601 return -EIO;
1602 }
1603 offsets->size =
1604 config->cb.record_header_size(config, chan,
1605 offsets->begin,
1606 &offsets->pre_header_padding,
1607 ctx);
1608 offsets->size +=
1609 lib_ring_buffer_align(offsets->begin + offsets->size,
1610 ctx->largest_align)
1611 + ctx->data_size;
1612 if (unlikely(subbuf_offset(offsets->begin, chan)
1613 + offsets->size > chan->backend.subbuf_size)) {
1614 /*
1615 * Record too big for subbuffers, report error, don't
1616 * complete the sub-buffer switch.
1617 */
1618 v_inc(config, &buf->records_lost_big);
1619 return -ENOSPC;
1620 } else {
1621 /*
1622 * We just made a successful buffer switch and the
1623 * record fits in the new subbuffer. Let's write.
1624 */
1625 }
1626 } else {
1627 /*
1628 * Record fits in the current buffer and we are not on a switch
1629 * boundary. It's safe to write.
1630 */
1631 }
1632 offsets->end = offsets->begin + offsets->size;
1633
1634 if (unlikely(subbuf_offset(offsets->end, chan) == 0)) {
1635 /*
1636 * The offset_end will fall at the very beginning of the next
1637 * subbuffer.
1638 */
1639 offsets->switch_new_end = 1; /* For offsets->begin */
1640 }
1641 return 0;
1642 }
1643
1644 /**
1645 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1646 * @ctx: ring buffer context.
1647 *
1648 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1649 * -EIO for other errors, else returns 0.
1650 * It will take care of sub-buffer switching.
1651 */
1652 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx)
1653 {
1654 struct channel *chan = ctx->chan;
1655 const struct lib_ring_buffer_config *config = &chan->backend.config;
1656 struct lib_ring_buffer *buf;
1657 struct switch_offsets offsets;
1658 int ret;
1659
1660 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
1661 buf = per_cpu_ptr(chan->backend.buf, ctx->cpu);
1662 else
1663 buf = chan->backend.buf;
1664 ctx->buf = buf;
1665
1666 offsets.size = 0;
1667
1668 do {
1669 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
1670 ctx);
1671 if (unlikely(ret))
1672 return ret;
1673 } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
1674 offsets.end)
1675 != offsets.old));
1676
1677 /*
1678 * Atomically update last_tsc. This update races against concurrent
1679 * atomic updates, but the race will always cause supplementary full TSC
1680 * records, never the opposite (missing a full TSC record when it would
1681 * be needed).
1682 */
1683 save_last_tsc(config, buf, ctx->tsc);
1684
1685 /*
1686 * Push the reader if necessary
1687 */
1688 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
1689
1690 /*
1691 * Clear noref flag for this subbuffer.
1692 */
1693 lib_ring_buffer_clear_noref(config, &buf->backend,
1694 subbuf_index(offsets.end - 1, chan));
1695
1696 /*
1697 * Switch old subbuffer if needed.
1698 */
1699 if (unlikely(offsets.switch_old_end)) {
1700 lib_ring_buffer_clear_noref(config, &buf->backend,
1701 subbuf_index(offsets.old - 1, chan));
1702 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc);
1703 }
1704
1705 /*
1706 * Populate new subbuffer.
1707 */
1708 if (unlikely(offsets.switch_new_start))
1709 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc);
1710
1711 if (unlikely(offsets.switch_new_end))
1712 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc);
1713
1714 ctx->slot_size = offsets.size;
1715 ctx->pre_offset = offsets.begin;
1716 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
1717 return 0;
1718 }
1719 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow);
1720
1721 int __init init_lib_ring_buffer_frontend(void)
1722 {
1723 int cpu;
1724
1725 for_each_possible_cpu(cpu)
1726 spin_lock_init(&per_cpu(ring_buffer_nohz_lock, cpu));
1727 return 0;
1728 }
1729
1730 module_init(init_lib_ring_buffer_frontend);
1731
1732 void __exit exit_lib_ring_buffer_frontend(void)
1733 {
1734 }
1735
1736 module_exit(exit_lib_ring_buffer_frontend);
This page took 0.09627 seconds and 5 git commands to generate.