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