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