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