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