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