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