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