Revert "Fix: don't perform extra flush on metadata channel"
[lttng-modules.git] / lib / ringbuffer / ring_buffer_frontend.c
CommitLineData
f3bc08c5
MD
1/*
2 * ring_buffer_frontend.c
3 *
886d51a3
MD
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
f3bc08c5
MD
20 *
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
23 *
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
27 *
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
32 *
33 * Author:
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
35 *
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
40 * And from K42 :
41 * Bob Wisniewski <bob@watson.ibm.com>
42 *
43 * Buffer reader semantic :
44 *
45 * - get_subbuf_size
46 * while buffer is not finalized and empty
47 * - get_subbuf
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
51 * - put_subbuf
f3bc08c5
MD
52 */
53
54#include <linux/delay.h>
55#include <linux/module.h>
56#include <linux/percpu.h>
57
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
815aa903
MD
454#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
455
456enum cpuhp_state lttng_rb_hp_prepare;
457enum cpuhp_state lttng_rb_hp_online;
458
459void lttng_rb_set_hp_prepare(enum cpuhp_state val)
460{
461 lttng_rb_hp_prepare = val;
462}
463EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare);
464
465void lttng_rb_set_hp_online(enum cpuhp_state val)
466{
467 lttng_rb_hp_online = val;
468}
469EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online);
470
471int lttng_cpuhp_rb_frontend_dead(unsigned int cpu,
472 struct lttng_cpuhp_node *node)
473{
474 struct channel *chan = container_of(node, struct channel,
475 cpuhp_prepare);
476 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
477 const struct lib_ring_buffer_config *config = &chan->backend.config;
478
479 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
480
481 /*
482 * Performing a buffer switch on a remote CPU. Performed by
483 * the CPU responsible for doing the hotunplug after the target
484 * CPU stopped running completely. Ensures that all data
485 * from that remote CPU is flushed.
486 */
487 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
488 return 0;
489}
490EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead);
491
492int lttng_cpuhp_rb_frontend_online(unsigned int cpu,
493 struct lttng_cpuhp_node *node)
494{
495 struct channel *chan = container_of(node, struct channel,
496 cpuhp_online);
497 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
498 const struct lib_ring_buffer_config *config = &chan->backend.config;
499
500 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
501
502 wake_up_interruptible(&chan->hp_wait);
503 lib_ring_buffer_start_switch_timer(buf);
504 lib_ring_buffer_start_read_timer(buf);
505 return 0;
506}
507EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online);
508
509int lttng_cpuhp_rb_frontend_offline(unsigned int cpu,
510 struct lttng_cpuhp_node *node)
511{
512 struct channel *chan = container_of(node, struct channel,
513 cpuhp_online);
514 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
515 const struct lib_ring_buffer_config *config = &chan->backend.config;
516
517 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
518
519 lib_ring_buffer_stop_switch_timer(buf);
520 lib_ring_buffer_stop_read_timer(buf);
521 return 0;
522}
523EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline);
524
525#else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
526
f3bc08c5 527#ifdef CONFIG_HOTPLUG_CPU
815aa903 528
f3bc08c5
MD
529/**
530 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
531 * @nb: notifier block
532 * @action: hotplug action to take
533 * @hcpu: CPU number
534 *
535 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
536 */
537static
e8f071d5 538int lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb,
f3bc08c5
MD
539 unsigned long action,
540 void *hcpu)
541{
542 unsigned int cpu = (unsigned long)hcpu;
543 struct channel *chan = container_of(nb, struct channel,
544 cpu_hp_notifier);
545 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
5a8fd222 546 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
547
548 if (!chan->cpu_hp_enable)
549 return NOTIFY_DONE;
550
551 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
552
553 switch (action) {
554 case CPU_DOWN_FAILED:
555 case CPU_DOWN_FAILED_FROZEN:
556 case CPU_ONLINE:
557 case CPU_ONLINE_FROZEN:
24cedcfe 558 wake_up_interruptible(&chan->hp_wait);
f3bc08c5
MD
559 lib_ring_buffer_start_switch_timer(buf);
560 lib_ring_buffer_start_read_timer(buf);
561 return NOTIFY_OK;
562
563 case CPU_DOWN_PREPARE:
564 case CPU_DOWN_PREPARE_FROZEN:
565 lib_ring_buffer_stop_switch_timer(buf);
566 lib_ring_buffer_stop_read_timer(buf);
567 return NOTIFY_OK;
568
569 case CPU_DEAD:
570 case CPU_DEAD_FROZEN:
571 /*
572 * Performing a buffer switch on a remote CPU. Performed by
573 * the CPU responsible for doing the hotunplug after the target
574 * CPU stopped running completely. Ensures that all data
575 * from that remote CPU is flushed.
576 */
577 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
578 return NOTIFY_OK;
579
580 default:
581 return NOTIFY_DONE;
582 }
583}
815aa903 584
f3bc08c5
MD
585#endif
586
815aa903
MD
587#endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
588
23b908b0 589#if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
f3bc08c5
MD
590/*
591 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
592 * that wake-up-tracing generated events are flushed before going idle (in
593 * tick_nohz). We test if the spinlock is locked to deal with the race where
594 * readers try to sample the ring buffer before we perform the switch. We let
595 * the readers retry in that case. If there is data in the buffer, the wake up
596 * is going to forbid the CPU running the reader thread from going idle.
597 */
598static int notrace ring_buffer_tick_nohz_callback(struct notifier_block *nb,
599 unsigned long val,
600 void *data)
601{
602 struct channel *chan = container_of(nb, struct channel,
603 tick_nohz_notifier);
5a8fd222 604 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
605 struct lib_ring_buffer *buf;
606 int cpu = smp_processor_id();
607
608 if (config->alloc != RING_BUFFER_ALLOC_PER_CPU) {
609 /*
610 * We don't support keeping the system idle with global buffers
611 * and streaming active. In order to do so, we would need to
612 * sample a non-nohz-cpumask racelessly with the nohz updates
613 * without adding synchronization overhead to nohz. Leave this
614 * use-case out for now.
615 */
616 return 0;
617 }
618
619 buf = channel_get_ring_buffer(config, chan, cpu);
620 switch (val) {
621 case TICK_NOHZ_FLUSH:
622 raw_spin_lock(&buf->raw_tick_nohz_spinlock);
623 if (config->wakeup == RING_BUFFER_WAKEUP_BY_TIMER
624 && chan->read_timer_interval
625 && atomic_long_read(&buf->active_readers)
626 && (lib_ring_buffer_poll_deliver(config, buf, chan)
627 || lib_ring_buffer_pending_data(config, buf, chan))) {
628 wake_up_interruptible(&buf->read_wait);
629 wake_up_interruptible(&chan->read_wait);
630 }
631 if (chan->switch_timer_interval)
632 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
633 raw_spin_unlock(&buf->raw_tick_nohz_spinlock);
634 break;
635 case TICK_NOHZ_STOP:
e6b06d7d 636 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock));
f3bc08c5
MD
637 lib_ring_buffer_stop_switch_timer(buf);
638 lib_ring_buffer_stop_read_timer(buf);
e6b06d7d 639 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock));
f3bc08c5
MD
640 break;
641 case TICK_NOHZ_RESTART:
e6b06d7d 642 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock));
f3bc08c5
MD
643 lib_ring_buffer_start_read_timer(buf);
644 lib_ring_buffer_start_switch_timer(buf);
e6b06d7d 645 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock));
f3bc08c5
MD
646 break;
647 }
648
649 return 0;
650}
651
652void notrace lib_ring_buffer_tick_nohz_flush(void)
653{
654 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_FLUSH,
655 NULL);
656}
657
658void notrace lib_ring_buffer_tick_nohz_stop(void)
659{
660 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_STOP,
661 NULL);
662}
663
664void notrace lib_ring_buffer_tick_nohz_restart(void)
665{
666 atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_RESTART,
667 NULL);
668}
23b908b0 669#endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
f3bc08c5
MD
670
671/*
672 * Holds CPU hotplug.
673 */
674static void channel_unregister_notifiers(struct channel *chan)
675{
5a8fd222 676 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
677
678 channel_iterator_unregister_notifiers(chan);
679 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
680#ifdef CONFIG_NO_HZ
681 /*
682 * Remove the nohz notifier first, so we are certain we stop
683 * the timers.
684 */
685 atomic_notifier_chain_unregister(&tick_nohz_notifier,
686 &chan->tick_nohz_notifier);
687 /*
688 * ring_buffer_nohz_lock will not be needed below, because
689 * we just removed the notifiers, which were the only source of
690 * concurrency.
691 */
692#endif /* CONFIG_NO_HZ */
815aa903
MD
693#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
694 {
695 int ret;
696
697 ret = cpuhp_state_remove_instance(lttng_rb_hp_online,
698 &chan->cpuhp_online.node);
699 WARN_ON(ret);
700 ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare,
701 &chan->cpuhp_prepare.node);
702 WARN_ON(ret);
f3bc08c5 703 }
815aa903
MD
704#else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
705 {
706 int cpu;
707
708#ifdef CONFIG_HOTPLUG_CPU
709 get_online_cpus();
710 chan->cpu_hp_enable = 0;
711 for_each_online_cpu(cpu) {
712 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
713 cpu);
714 lib_ring_buffer_stop_switch_timer(buf);
715 lib_ring_buffer_stop_read_timer(buf);
716 }
717 put_online_cpus();
718 unregister_cpu_notifier(&chan->cpu_hp_notifier);
f3bc08c5 719#else
815aa903
MD
720 for_each_possible_cpu(cpu) {
721 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
722 cpu);
723 lib_ring_buffer_stop_switch_timer(buf);
724 lib_ring_buffer_stop_read_timer(buf);
725 }
f3bc08c5 726#endif
815aa903
MD
727 }
728#endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
f3bc08c5
MD
729 } else {
730 struct lib_ring_buffer *buf = chan->backend.buf;
731
732 lib_ring_buffer_stop_switch_timer(buf);
733 lib_ring_buffer_stop_read_timer(buf);
734 }
735 channel_backend_unregister_notifiers(&chan->backend);
736}
737
64af2437
MD
738static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer *buf)
739{
740 if (!buf->quiescent) {
741 buf->quiescent = true;
742 _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH);
743 }
744}
745
746static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer *buf)
747{
748 buf->quiescent = false;
749}
750
751void lib_ring_buffer_set_quiescent_channel(struct channel *chan)
752{
753 int cpu;
754 const struct lib_ring_buffer_config *config = &chan->backend.config;
755
756 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
757 get_online_cpus();
758 for_each_channel_cpu(cpu, chan) {
759 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
760 cpu);
761
762 lib_ring_buffer_set_quiescent(buf);
763 }
764 put_online_cpus();
765 } else {
766 struct lib_ring_buffer *buf = chan->backend.buf;
767
768 lib_ring_buffer_set_quiescent(buf);
769 }
770}
771EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel);
772
773void lib_ring_buffer_clear_quiescent_channel(struct channel *chan)
774{
775 int cpu;
776 const struct lib_ring_buffer_config *config = &chan->backend.config;
777
778 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
779 get_online_cpus();
780 for_each_channel_cpu(cpu, chan) {
781 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
782 cpu);
783
784 lib_ring_buffer_clear_quiescent(buf);
785 }
786 put_online_cpus();
787 } else {
788 struct lib_ring_buffer *buf = chan->backend.buf;
789
790 lib_ring_buffer_clear_quiescent(buf);
791 }
792}
793EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel);
794
f3bc08c5
MD
795static void channel_free(struct channel *chan)
796{
dd5a0db3
MD
797 if (chan->backend.release_priv_ops) {
798 chan->backend.release_priv_ops(chan->backend.priv_ops);
799 }
f3bc08c5
MD
800 channel_iterator_free(chan);
801 channel_backend_free(&chan->backend);
802 kfree(chan);
803}
804
805/**
806 * channel_create - Create channel.
807 * @config: ring buffer instance configuration
808 * @name: name of the channel
809 * @priv: ring buffer client private data
810 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
811 * address mapping. It is used only by RING_BUFFER_STATIC
812 * configuration. It can be set to NULL for other backends.
813 * @subbuf_size: subbuffer size
814 * @num_subbuf: number of subbuffers
815 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
816 * padding to let readers get those sub-buffers.
817 * Used for live streaming.
818 * @read_timer_interval: Time interval (in us) to wake up pending readers.
819 *
820 * Holds cpu hotplug.
821 * Returns NULL on failure.
822 */
823struct channel *channel_create(const struct lib_ring_buffer_config *config,
824 const char *name, void *priv, void *buf_addr,
825 size_t subbuf_size,
826 size_t num_subbuf, unsigned int switch_timer_interval,
827 unsigned int read_timer_interval)
828{
815aa903 829 int ret;
f3bc08c5
MD
830 struct channel *chan;
831
832 if (lib_ring_buffer_check_config(config, switch_timer_interval,
833 read_timer_interval))
834 return NULL;
835
836 chan = kzalloc(sizeof(struct channel), GFP_KERNEL);
837 if (!chan)
838 return NULL;
839
840 ret = channel_backend_init(&chan->backend, name, config, priv,
841 subbuf_size, num_subbuf);
842 if (ret)
843 goto error;
844
845 ret = channel_iterator_init(chan);
846 if (ret)
847 goto error_free_backend;
848
849 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
850 chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
851 chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
f40270ad 852 kref_init(&chan->ref);
f3bc08c5 853 init_waitqueue_head(&chan->read_wait);
24cedcfe 854 init_waitqueue_head(&chan->hp_wait);
f3bc08c5
MD
855
856 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
815aa903
MD
857#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
858 chan->cpuhp_prepare.component = LTTNG_RING_BUFFER_FRONTEND;
859 ret = cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare,
860 &chan->cpuhp_prepare.node);
861 if (ret)
862 goto cpuhp_prepare_error;
863
864 chan->cpuhp_online.component = LTTNG_RING_BUFFER_FRONTEND;
865 ret = cpuhp_state_add_instance(lttng_rb_hp_online,
866 &chan->cpuhp_online.node);
867 if (ret)
868 goto cpuhp_online_error;
869#else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
870 {
871 int cpu;
872 /*
873 * In case of non-hotplug cpu, if the ring-buffer is allocated
874 * in early initcall, it will not be notified of secondary cpus.
875 * In that off case, we need to allocate for all possible cpus.
876 */
877#ifdef CONFIG_HOTPLUG_CPU
878 chan->cpu_hp_notifier.notifier_call =
879 lib_ring_buffer_cpu_hp_callback;
880 chan->cpu_hp_notifier.priority = 6;
881 register_cpu_notifier(&chan->cpu_hp_notifier);
882
883 get_online_cpus();
884 for_each_online_cpu(cpu) {
885 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
886 cpu);
887 spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu));
888 lib_ring_buffer_start_switch_timer(buf);
889 lib_ring_buffer_start_read_timer(buf);
890 spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu));
891 }
892 chan->cpu_hp_enable = 1;
893 put_online_cpus();
894#else
895 for_each_possible_cpu(cpu) {
896 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
897 cpu);
898 spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu));
899 lib_ring_buffer_start_switch_timer(buf);
900 lib_ring_buffer_start_read_timer(buf);
901 spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu));
902 }
903#endif
904 }
905#endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
906
23b908b0 907#if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
f3bc08c5
MD
908 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
909 chan->tick_nohz_notifier.notifier_call =
910 ring_buffer_tick_nohz_callback;
911 chan->tick_nohz_notifier.priority = ~0U;
912 atomic_notifier_chain_register(&tick_nohz_notifier,
913 &chan->tick_nohz_notifier);
23b908b0 914#endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
f3bc08c5 915
f3bc08c5
MD
916 } else {
917 struct lib_ring_buffer *buf = chan->backend.buf;
918
919 lib_ring_buffer_start_switch_timer(buf);
920 lib_ring_buffer_start_read_timer(buf);
921 }
922
923 return chan;
924
815aa903
MD
925#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
926cpuhp_online_error:
927 ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare,
928 &chan->cpuhp_prepare.node);
929 WARN_ON(ret);
930cpuhp_prepare_error:
931#endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
f3bc08c5
MD
932error_free_backend:
933 channel_backend_free(&chan->backend);
934error:
935 kfree(chan);
936 return NULL;
937}
938EXPORT_SYMBOL_GPL(channel_create);
939
f40270ad
MD
940static
941void channel_release(struct kref *kref)
942{
943 struct channel *chan = container_of(kref, struct channel, ref);
944 channel_free(chan);
945}
946
f3bc08c5
MD
947/**
948 * channel_destroy - Finalize, wait for q.s. and destroy channel.
949 * @chan: channel to destroy
950 *
951 * Holds cpu hotplug.
9a0df743
MD
952 * Call "destroy" callback, finalize channels, and then decrement the
953 * channel reference count. Note that when readers have completed data
954 * consumption of finalized channels, get_subbuf() will return -ENODATA.
955 * They should release their handle at that point. Returns the private
956 * data pointer.
f3bc08c5
MD
957 */
958void *channel_destroy(struct channel *chan)
959{
960 int cpu;
5a8fd222 961 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
962 void *priv;
963
964 channel_unregister_notifiers(chan);
965
966 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
967 /*
968 * No need to hold cpu hotplug, because all notifiers have been
969 * unregistered.
970 */
971 for_each_channel_cpu(cpu, chan) {
972 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
973 cpu);
974
975 if (config->cb.buffer_finalize)
976 config->cb.buffer_finalize(buf,
977 chan->backend.priv,
978 cpu);
979 if (buf->backend.allocated)
64af2437 980 lib_ring_buffer_set_quiescent(buf);
f3bc08c5
MD
981 /*
982 * Perform flush before writing to finalized.
983 */
984 smp_wmb();
985 ACCESS_ONCE(buf->finalized) = 1;
986 wake_up_interruptible(&buf->read_wait);
987 }
988 } else {
989 struct lib_ring_buffer *buf = chan->backend.buf;
990
991 if (config->cb.buffer_finalize)
992 config->cb.buffer_finalize(buf, chan->backend.priv, -1);
993 if (buf->backend.allocated)
64af2437 994 lib_ring_buffer_set_quiescent(buf);
f3bc08c5
MD
995 /*
996 * Perform flush before writing to finalized.
997 */
998 smp_wmb();
999 ACCESS_ONCE(buf->finalized) = 1;
1000 wake_up_interruptible(&buf->read_wait);
1001 }
24cedcfe
MD
1002 ACCESS_ONCE(chan->finalized) = 1;
1003 wake_up_interruptible(&chan->hp_wait);
f3bc08c5 1004 wake_up_interruptible(&chan->read_wait);
f3bc08c5 1005 priv = chan->backend.priv;
ba1d61bc 1006 kref_put(&chan->ref, channel_release);
f3bc08c5
MD
1007 return priv;
1008}
1009EXPORT_SYMBOL_GPL(channel_destroy);
1010
1011struct lib_ring_buffer *channel_get_ring_buffer(
1012 const struct lib_ring_buffer_config *config,
1013 struct channel *chan, int cpu)
1014{
1015 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL)
1016 return chan->backend.buf;
1017 else
1018 return per_cpu_ptr(chan->backend.buf, cpu);
1019}
1020EXPORT_SYMBOL_GPL(channel_get_ring_buffer);
1021
1022int lib_ring_buffer_open_read(struct lib_ring_buffer *buf)
1023{
1024 struct channel *chan = buf->backend.chan;
1025
1026 if (!atomic_long_add_unless(&buf->active_readers, 1, 1))
1027 return -EBUSY;
9c1f4643
MD
1028 if (!lttng_kref_get(&chan->ref)) {
1029 atomic_long_dec(&buf->active_readers);
1030 return -EOVERFLOW;
1031 }
505fb410 1032 lttng_smp_mb__after_atomic();
f3bc08c5
MD
1033 return 0;
1034}
1035EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read);
1036
1037void lib_ring_buffer_release_read(struct lib_ring_buffer *buf)
1038{
1039 struct channel *chan = buf->backend.chan;
1040
1041 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
505fb410 1042 lttng_smp_mb__before_atomic();
f3bc08c5 1043 atomic_long_dec(&buf->active_readers);
f40270ad 1044 kref_put(&chan->ref, channel_release);
f3bc08c5
MD
1045}
1046EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read);
1047
1048/*
1049 * Promote compiler barrier to a smp_mb().
1050 * For the specific ring buffer case, this IPI call should be removed if the
1051 * architecture does not reorder writes. This should eventually be provided by
1052 * a separate architecture-specific infrastructure.
1053 */
1054static void remote_mb(void *info)
1055{
1056 smp_mb();
1057}
1058
1059/**
1060 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1061 * @buf: ring buffer
1062 * @consumed: consumed count indicating the position where to read
1063 * @produced: produced count, indicates position when to stop reading
1064 *
1065 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1066 * data to read at consumed position, or 0 if the get operation succeeds.
1067 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1068 */
1069
1070int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf,
1071 unsigned long *consumed, unsigned long *produced)
1072{
1073 struct channel *chan = buf->backend.chan;
5a8fd222 1074 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1075 unsigned long consumed_cur, write_offset;
1076 int finalized;
1077
1e8d53a9
MD
1078 /*
1079 * First, ensure we perform a "final" flush onto the stream. This will
1080 * ensure we create a packet of padding if we encounter an empty
1081 * packet. This ensures the time-stamps right before the snapshot is
1082 * used as end of packet timestamp.
1083 */
1084 if (!buf->quiescent)
1085 _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH);
1086
f3bc08c5
MD
1087retry:
1088 finalized = ACCESS_ONCE(buf->finalized);
1089 /*
1090 * Read finalized before counters.
1091 */
1092 smp_rmb();
1093 consumed_cur = atomic_long_read(&buf->consumed);
1094 /*
1095 * No need to issue a memory barrier between consumed count read and
1096 * write offset read, because consumed count can only change
1097 * concurrently in overwrite mode, and we keep a sequence counter
1098 * identifier derived from the write offset to check we are getting
1099 * the same sub-buffer we are expecting (the sub-buffers are atomically
1100 * "tagged" upon writes, tags are checked upon read).
1101 */
1102 write_offset = v_read(config, &buf->offset);
1103
1104 /*
1105 * Check that we are not about to read the same subbuffer in
1106 * which the writer head is.
1107 */
1108 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1109 == 0)
1110 goto nodata;
1111
1112 *consumed = consumed_cur;
1113 *produced = subbuf_trunc(write_offset, chan);
1114
1115 return 0;
1116
1117nodata:
1118 /*
1119 * The memory barriers __wait_event()/wake_up_interruptible() take care
1120 * of "raw_spin_is_locked" memory ordering.
1121 */
1122 if (finalized)
1123 return -ENODATA;
1124 else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1125 goto retry;
1126 else
1127 return -EAGAIN;
1128}
1129EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot);
1130
1131/**
1132 * lib_ring_buffer_put_snapshot - move consumed counter forward
71c1d843
MD
1133 *
1134 * Should only be called from consumer context.
f3bc08c5
MD
1135 * @buf: ring buffer
1136 * @consumed_new: new consumed count value
1137 */
1138void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf,
1139 unsigned long consumed_new)
1140{
1141 struct lib_ring_buffer_backend *bufb = &buf->backend;
1142 struct channel *chan = bufb->chan;
1143 unsigned long consumed;
1144
1145 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
1146
1147 /*
1148 * Only push the consumed value forward.
1149 * If the consumed cmpxchg fails, this is because we have been pushed by
1150 * the writer in flight recorder mode.
1151 */
1152 consumed = atomic_long_read(&buf->consumed);
1153 while ((long) consumed - (long) consumed_new < 0)
1154 consumed = atomic_long_cmpxchg(&buf->consumed, consumed,
1155 consumed_new);
71c1d843
MD
1156 /* Wake-up the metadata producer */
1157 wake_up_interruptible(&buf->write_wait);
f3bc08c5
MD
1158}
1159EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer);
1160
1161/**
1162 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1163 * @buf: ring buffer
1164 * @consumed: consumed count indicating the position where to read
1165 *
1166 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1167 * data to read at consumed position, or 0 if the get operation succeeds.
1168 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
1169 */
1170int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf,
1171 unsigned long consumed)
1172{
1173 struct channel *chan = buf->backend.chan;
5a8fd222 1174 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1175 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
1176 int ret;
1177 int finalized;
1178
8202b8a0
MD
1179 if (buf->get_subbuf) {
1180 /*
1181 * Reader is trying to get a subbuffer twice.
1182 */
1183 CHAN_WARN_ON(chan, 1);
1184 return -EBUSY;
1185 }
f3bc08c5
MD
1186retry:
1187 finalized = ACCESS_ONCE(buf->finalized);
1188 /*
1189 * Read finalized before counters.
1190 */
1191 smp_rmb();
1192 consumed_cur = atomic_long_read(&buf->consumed);
1193 consumed_idx = subbuf_index(consumed, chan);
1194 commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb);
1195 /*
1196 * Make sure we read the commit count before reading the buffer
1197 * data and the write offset. Correct consumed offset ordering
1198 * wrt commit count is insured by the use of cmpxchg to update
1199 * the consumed offset.
1200 * smp_call_function_single can fail if the remote CPU is offline,
1201 * this is OK because then there is no wmb to execute there.
1202 * If our thread is executing on the same CPU as the on the buffers
1203 * belongs to, we don't have to synchronize it at all. If we are
1204 * migrated, the scheduler will take care of the memory barriers.
1205 * Normally, smp_call_function_single() should ensure program order when
1206 * executing the remote function, which implies that it surrounds the
1207 * function execution with :
1208 * smp_mb()
1209 * send IPI
1210 * csd_lock_wait
1211 * recv IPI
1212 * smp_mb()
1213 * exec. function
1214 * smp_mb()
1215 * csd unlock
1216 * smp_mb()
1217 *
1218 * However, smp_call_function_single() does not seem to clearly execute
1219 * such barriers. It depends on spinlock semantic to provide the barrier
1220 * before executing the IPI and, when busy-looping, csd_lock_wait only
1221 * executes smp_mb() when it has to wait for the other CPU.
1222 *
1223 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1224 * required ourself, even if duplicated. It has no performance impact
1225 * anyway.
1226 *
1227 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1228 * read and write vs write. They do not ensure core synchronization. We
1229 * really have to ensure total order between the 3 barriers running on
1230 * the 2 CPUs.
1231 */
1232 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1233 if (config->sync == RING_BUFFER_SYNC_PER_CPU
1234 && config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
1235 if (raw_smp_processor_id() != buf->backend.cpu) {
1236 /* Total order with IPI handler smp_mb() */
1237 smp_mb();
1238 smp_call_function_single(buf->backend.cpu,
1239 remote_mb, NULL, 1);
1240 /* Total order with IPI handler smp_mb() */
1241 smp_mb();
1242 }
1243 } else {
1244 /* Total order with IPI handler smp_mb() */
1245 smp_mb();
1246 smp_call_function(remote_mb, NULL, 1);
1247 /* Total order with IPI handler smp_mb() */
1248 smp_mb();
1249 }
1250 } else {
1251 /*
1252 * Local rmb to match the remote wmb to read the commit count
1253 * before the buffer data and the write offset.
1254 */
1255 smp_rmb();
1256 }
1257
1258 write_offset = v_read(config, &buf->offset);
1259
1260 /*
1261 * Check that the buffer we are getting is after or at consumed_cur
1262 * position.
1263 */
1264 if ((long) subbuf_trunc(consumed, chan)
1265 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1266 goto nodata;
1267
1268 /*
1269 * Check that the subbuffer we are trying to consume has been
1270 * already fully committed.
1271 */
1272 if (((commit_count - chan->backend.subbuf_size)
1273 & chan->commit_count_mask)
c9b3b5e2 1274 - (buf_trunc(consumed, chan)
f3bc08c5
MD
1275 >> chan->backend.num_subbuf_order)
1276 != 0)
1277 goto nodata;
1278
1279 /*
1280 * Check that we are not about to read the same subbuffer in
1281 * which the writer head is.
1282 */
c9b3b5e2 1283 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan)
f3bc08c5
MD
1284 == 0)
1285 goto nodata;
1286
1287 /*
1288 * Failure to get the subbuffer causes a busy-loop retry without going
1289 * to a wait queue. These are caused by short-lived race windows where
1290 * the writer is getting access to a subbuffer we were trying to get
1291 * access to. Also checks that the "consumed" buffer count we are
1292 * looking for matches the one contained in the subbuffer id.
1293 */
1294 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1295 consumed_idx, buf_trunc_val(consumed, chan));
1296 if (ret)
1297 goto retry;
1298 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1299
1300 buf->get_subbuf_consumed = consumed;
1301 buf->get_subbuf = 1;
1302
1303 return 0;
1304
1305nodata:
1306 /*
1307 * The memory barriers __wait_event()/wake_up_interruptible() take care
1308 * of "raw_spin_is_locked" memory ordering.
1309 */
1310 if (finalized)
1311 return -ENODATA;
1312 else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1313 goto retry;
1314 else
1315 return -EAGAIN;
1316}
1317EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf);
1318
1319/**
1320 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1321 * @buf: ring buffer
1322 */
1323void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf)
1324{
1325 struct lib_ring_buffer_backend *bufb = &buf->backend;
1326 struct channel *chan = bufb->chan;
5a8fd222 1327 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1328 unsigned long read_sb_bindex, consumed_idx, consumed;
1329
1330 CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1);
1331
1332 if (!buf->get_subbuf) {
1333 /*
1334 * Reader puts a subbuffer it did not get.
1335 */
1336 CHAN_WARN_ON(chan, 1);
1337 return;
1338 }
1339 consumed = buf->get_subbuf_consumed;
1340 buf->get_subbuf = 0;
1341
1342 /*
1343 * Clear the records_unread counter. (overruns counter)
1344 * Can still be non-zero if a file reader simply grabbed the data
1345 * without using iterators.
1346 * Can be below zero if an iterator is used on a snapshot more than
1347 * once.
1348 */
1349 read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1350 v_add(config, v_read(config,
1351 &bufb->array[read_sb_bindex]->records_unread),
1352 &bufb->records_read);
1353 v_set(config, &bufb->array[read_sb_bindex]->records_unread, 0);
1354 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1355 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1356 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1357
1358 /*
1359 * Exchange the reader subbuffer with the one we put in its place in the
1360 * writer subbuffer table. Expect the original consumed count. If
1361 * update_read_sb_index fails, this is because the writer updated the
1362 * subbuffer concurrently. We should therefore keep the subbuffer we
1363 * currently have: it has become invalid to try reading this sub-buffer
1364 * consumed count value anyway.
1365 */
1366 consumed_idx = subbuf_index(consumed, chan);
1367 update_read_sb_index(config, &buf->backend, &chan->backend,
1368 consumed_idx, buf_trunc_val(consumed, chan));
1369 /*
1370 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1371 * if the writer concurrently updated it.
1372 */
1373}
1374EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf);
1375
1376/*
1377 * cons_offset is an iterator on all subbuffer offsets between the reader
1378 * position and the writer position. (inclusive)
1379 */
1380static
1381void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf,
1382 struct channel *chan,
1383 unsigned long cons_offset,
1384 int cpu)
1385{
5a8fd222 1386 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1387 unsigned long cons_idx, commit_count, commit_count_sb;
1388
1389 cons_idx = subbuf_index(cons_offset, chan);
1390 commit_count = v_read(config, &buf->commit_hot[cons_idx].cc);
1391 commit_count_sb = v_read(config, &buf->commit_cold[cons_idx].cc_sb);
1392
1393 if (subbuf_offset(commit_count, chan) != 0)
1394 printk(KERN_WARNING
1395 "ring buffer %s, cpu %d: "
1396 "commit count in subbuffer %lu,\n"
1397 "expecting multiples of %lu bytes\n"
1398 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1399 chan->backend.name, cpu, cons_idx,
1400 chan->backend.subbuf_size,
1401 commit_count, commit_count_sb);
1402
1403 printk(KERN_DEBUG "ring buffer: %s, cpu %d: %lu bytes committed\n",
1404 chan->backend.name, cpu, commit_count);
1405}
1406
1407static
1408void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf,
1409 struct channel *chan,
1410 void *priv, int cpu)
1411{
5a8fd222 1412 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1413 unsigned long write_offset, cons_offset;
1414
f3bc08c5
MD
1415 /*
1416 * No need to order commit_count, write_offset and cons_offset reads
1417 * because we execute at teardown when no more writer nor reader
1418 * references are left.
1419 */
1420 write_offset = v_read(config, &buf->offset);
1421 cons_offset = atomic_long_read(&buf->consumed);
1422 if (write_offset != cons_offset)
05aad775 1423 printk(KERN_DEBUG
f3bc08c5
MD
1424 "ring buffer %s, cpu %d: "
1425 "non-consumed data\n"
1426 " [ %lu bytes written, %lu bytes read ]\n",
1427 chan->backend.name, cpu, write_offset, cons_offset);
1428
1429 for (cons_offset = atomic_long_read(&buf->consumed);
1430 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1431 chan)
1432 - cons_offset) > 0;
1433 cons_offset = subbuf_align(cons_offset, chan))
1434 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1435 cpu);
1436}
1437
1438static
1439void lib_ring_buffer_print_errors(struct channel *chan,
1440 struct lib_ring_buffer *buf, int cpu)
1441{
5a8fd222 1442 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1443 void *priv = chan->backend.priv;
1444
ec01ec93
MD
1445 if (!strcmp(chan->backend.name, "relay-metadata")) {
1446 printk(KERN_DEBUG "ring buffer %s: %lu records written, "
1447 "%lu records overrun\n",
1448 chan->backend.name,
1449 v_read(config, &buf->records_count),
1450 v_read(config, &buf->records_overrun));
1451 } else {
1452 printk(KERN_DEBUG "ring buffer %s, cpu %d: %lu records written, "
1453 "%lu records overrun\n",
1454 chan->backend.name, cpu,
1455 v_read(config, &buf->records_count),
1456 v_read(config, &buf->records_overrun));
1457
1458 if (v_read(config, &buf->records_lost_full)
1459 || v_read(config, &buf->records_lost_wrap)
1460 || v_read(config, &buf->records_lost_big))
1461 printk(KERN_WARNING
1462 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1463 " [ %lu buffer full, %lu nest buffer wrap-around, "
1464 "%lu event too big ]\n",
1465 chan->backend.name, cpu,
1466 v_read(config, &buf->records_lost_full),
1467 v_read(config, &buf->records_lost_wrap),
1468 v_read(config, &buf->records_lost_big));
1469 }
f3bc08c5
MD
1470 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu);
1471}
1472
1473/*
1474 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1475 *
dc5cd570
MD
1476 * Only executed by SWITCH_FLUSH, which can be issued while tracing is active
1477 * or at buffer finalization (destroy).
f3bc08c5
MD
1478 */
1479static
1480void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf,
1481 struct channel *chan,
1482 struct switch_offsets *offsets,
1483 u64 tsc)
1484{
5a8fd222 1485 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1486 unsigned long oldidx = subbuf_index(offsets->old, chan);
1487 unsigned long commit_count;
fbe84fd7 1488 struct commit_counters_hot *cc_hot;
f3bc08c5
MD
1489
1490 config->cb.buffer_begin(buf, tsc, oldidx);
1491
1492 /*
1493 * Order all writes to buffer before the commit count update that will
1494 * determine that the subbuffer is full.
1495 */
1496 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1497 /*
1498 * Must write slot data before incrementing commit count. This
1499 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1500 * by get_subbuf().
1501 */
1502 barrier();
1503 } else
1504 smp_wmb();
fbe84fd7
MD
1505 cc_hot = &buf->commit_hot[oldidx];
1506 v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc);
1507 commit_count = v_read(config, &cc_hot->cc);
f3bc08c5
MD
1508 /* Check if the written buffer has to be delivered */
1509 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
635e457c 1510 commit_count, oldidx, tsc);
8ec496cf 1511 lib_ring_buffer_write_commit_counter(config, buf, chan,
7915e163 1512 offsets->old + config->cb.subbuffer_header_size(),
fbe84fd7 1513 commit_count, cc_hot);
f3bc08c5
MD
1514}
1515
1516/*
1517 * lib_ring_buffer_switch_old_end: switch old subbuffer
1518 *
1519 * Note : offset_old should never be 0 here. It is ok, because we never perform
1520 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1521 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1522 * subbuffer.
1523 */
1524static
1525void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf,
1526 struct channel *chan,
1527 struct switch_offsets *offsets,
1528 u64 tsc)
1529{
5a8fd222 1530 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1531 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1532 unsigned long commit_count, padding_size, data_size;
fbe84fd7 1533 struct commit_counters_hot *cc_hot;
f3bc08c5
MD
1534
1535 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1536 padding_size = chan->backend.subbuf_size - data_size;
1537 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size);
1538
1539 /*
1540 * Order all writes to buffer before the commit count update that will
1541 * determine that the subbuffer is full.
1542 */
1543 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1544 /*
1545 * Must write slot data before incrementing commit count. This
1546 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1547 * by get_subbuf().
1548 */
1549 barrier();
1550 } else
1551 smp_wmb();
fbe84fd7
MD
1552 cc_hot = &buf->commit_hot[oldidx];
1553 v_add(config, padding_size, &cc_hot->cc);
1554 commit_count = v_read(config, &cc_hot->cc);
f3bc08c5 1555 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
635e457c 1556 commit_count, oldidx, tsc);
8ec496cf
MD
1557 lib_ring_buffer_write_commit_counter(config, buf, chan,
1558 offsets->old + padding_size, commit_count,
fbe84fd7 1559 cc_hot);
f3bc08c5
MD
1560}
1561
1562/*
1563 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1564 *
1565 * This code can be executed unordered : writers may already have written to the
1566 * sub-buffer before this code gets executed, caution. The commit makes sure
1567 * that this code is executed before the deliver of this sub-buffer.
1568 */
1569static
1570void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf,
1571 struct channel *chan,
1572 struct switch_offsets *offsets,
1573 u64 tsc)
1574{
5a8fd222 1575 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1576 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1577 unsigned long commit_count;
fbe84fd7 1578 struct commit_counters_hot *cc_hot;
f3bc08c5
MD
1579
1580 config->cb.buffer_begin(buf, tsc, beginidx);
1581
1582 /*
1583 * Order all writes to buffer before the commit count update that will
1584 * determine that the subbuffer is full.
1585 */
1586 if (config->ipi == RING_BUFFER_IPI_BARRIER) {
1587 /*
1588 * Must write slot data before incrementing commit count. This
1589 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1590 * by get_subbuf().
1591 */
1592 barrier();
1593 } else
1594 smp_wmb();
fbe84fd7
MD
1595 cc_hot = &buf->commit_hot[beginidx];
1596 v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc);
1597 commit_count = v_read(config, &cc_hot->cc);
f3bc08c5
MD
1598 /* Check if the written buffer has to be delivered */
1599 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
635e457c 1600 commit_count, beginidx, tsc);
8ec496cf 1601 lib_ring_buffer_write_commit_counter(config, buf, chan,
7915e163 1602 offsets->begin + config->cb.subbuffer_header_size(),
fbe84fd7 1603 commit_count, cc_hot);
f3bc08c5
MD
1604}
1605
f5ea5800
MD
1606/*
1607 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1608 *
768b05c9
MD
1609 * Calls subbuffer_set_data_size() to set the data size of the current
1610 * sub-buffer. We do not need to perform check_deliver nor commit here,
1611 * since this task will be done by the "commit" of the event for which
1612 * we are currently doing the space reservation.
f5ea5800
MD
1613 */
1614static
1615void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf,
1616 struct channel *chan,
1617 struct switch_offsets *offsets,
1618 u64 tsc)
1619{
1620 const struct lib_ring_buffer_config *config = &chan->backend.config;
768b05c9 1621 unsigned long endidx, data_size;
f5ea5800 1622
768b05c9 1623 endidx = subbuf_index(offsets->end - 1, chan);
f5ea5800 1624 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
f5ea5800 1625 subbuffer_set_data_size(config, &buf->backend, endidx, data_size);
f5ea5800
MD
1626}
1627
f3bc08c5
MD
1628/*
1629 * Returns :
1630 * 0 if ok
1631 * !0 if execution must be aborted.
1632 */
1633static
1634int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
1635 struct lib_ring_buffer *buf,
1636 struct channel *chan,
1637 struct switch_offsets *offsets,
1638 u64 *tsc)
1639{
5a8fd222 1640 const struct lib_ring_buffer_config *config = &chan->backend.config;
5334a2c5 1641 unsigned long off, reserve_commit_diff;
f3bc08c5
MD
1642
1643 offsets->begin = v_read(config, &buf->offset);
1644 offsets->old = offsets->begin;
1645 offsets->switch_old_start = 0;
1646 off = subbuf_offset(offsets->begin, chan);
1647
1648 *tsc = config->cb.ring_buffer_clock_read(chan);
1649
1650 /*
1651 * Ensure we flush the header of an empty subbuffer when doing the
1652 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1653 * total data gathering duration even if there were no records saved
1654 * after the last buffer switch.
1655 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1656 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1657 * subbuffer header as appropriate.
1658 * The next record that reserves space will be responsible for
1659 * populating the following subbuffer header. We choose not to populate
1660 * the next subbuffer header here because we want to be able to use
1661 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1662 * buffer flush, which must guarantee that all the buffer content
1663 * (records and header timestamps) are visible to the reader. This is
1664 * required for quiescence guarantees for the fusion merge.
1665 */
5334a2c5
MD
1666 if (mode != SWITCH_FLUSH && !off)
1667 return -1; /* we do not have to switch : buffer is empty */
1668
1669 if (unlikely(off == 0)) {
1670 unsigned long sb_index, commit_count;
1671
1672 /*
dc5cd570
MD
1673 * We are performing a SWITCH_FLUSH. There may be concurrent
1674 * writes into the buffer if e.g. invoked while performing a
1675 * snapshot on an active trace.
5334a2c5 1676 *
dc5cd570
MD
1677 * If the client does not save any header information (sub-buffer
1678 * header size == 0), don't switch empty subbuffer on finalize,
1679 * because it is invalid to deliver a completely empty
1680 * subbuffer.
5334a2c5
MD
1681 */
1682 if (!config->cb.subbuffer_header_size())
1683 return -1;
1684
1685 /* Test new buffer integrity */
1686 sb_index = subbuf_index(offsets->begin, chan);
1687 commit_count = v_read(config,
1688 &buf->commit_cold[sb_index].cc_sb);
1689 reserve_commit_diff =
1690 (buf_trunc(offsets->begin, chan)
1691 >> chan->backend.num_subbuf_order)
1692 - (commit_count & chan->commit_count_mask);
1693 if (likely(reserve_commit_diff == 0)) {
1694 /* Next subbuffer not being written to. */
1695 if (unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1696 subbuf_trunc(offsets->begin, chan)
1697 - subbuf_trunc((unsigned long)
1698 atomic_long_read(&buf->consumed), chan)
1699 >= chan->backend.buf_size)) {
1700 /*
1701 * We do not overwrite non consumed buffers
1702 * and we are full : don't switch.
1703 */
f3bc08c5 1704 return -1;
5334a2c5
MD
1705 } else {
1706 /*
1707 * Next subbuffer not being written to, and we
1708 * are either in overwrite mode or the buffer is
1709 * not full. It's safe to write in this new
1710 * subbuffer.
1711 */
1712 }
1713 } else {
f3bc08c5 1714 /*
5334a2c5
MD
1715 * Next subbuffer reserve offset does not match the
1716 * commit offset. Don't perform switch in
1717 * producer-consumer and overwrite mode. Caused by
1718 * either a writer OOPS or too many nested writes over a
1719 * reserve/commit pair.
f3bc08c5 1720 */
5334a2c5 1721 return -1;
f3bc08c5 1722 }
5334a2c5
MD
1723
1724 /*
1725 * Need to write the subbuffer start header on finalize.
1726 */
1727 offsets->switch_old_start = 1;
1728 }
1729 offsets->begin = subbuf_align(offsets->begin, chan);
f3bc08c5
MD
1730 /* Note: old points to the next subbuf at offset 0 */
1731 offsets->end = offsets->begin;
1732 return 0;
1733}
1734
1735/*
1736 * Force a sub-buffer switch. This operation is completely reentrant : can be
1737 * called while tracing is active with absolutely no lock held.
1738 *
1739 * Note, however, that as a v_cmpxchg is used for some atomic
1740 * operations, this function must be called from the CPU which owns the buffer
1741 * for a ACTIVE flush.
1742 */
1743void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode)
1744{
1745 struct channel *chan = buf->backend.chan;
5a8fd222 1746 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
1747 struct switch_offsets offsets;
1748 unsigned long oldidx;
1749 u64 tsc;
1750
1751 offsets.size = 0;
1752
1753 /*
1754 * Perform retryable operations.
1755 */
1756 do {
1757 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
1758 &tsc))
1759 return; /* Switch not needed */
1760 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
1761 != offsets.old);
1762
1763 /*
1764 * Atomically update last_tsc. This update races against concurrent
1765 * atomic updates, but the race will always cause supplementary full TSC
1766 * records, never the opposite (missing a full TSC record when it would
1767 * be needed).
1768 */
1769 save_last_tsc(config, buf, tsc);
1770
1771 /*
1772 * Push the reader if necessary
1773 */
1774 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
1775
1776 oldidx = subbuf_index(offsets.old, chan);
1777 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx);
1778
1779 /*
1780 * May need to populate header start on SWITCH_FLUSH.
1781 */
1782 if (offsets.switch_old_start) {
1783 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc);
1784 offsets.old += config->cb.subbuffer_header_size();
1785 }
1786
1787 /*
1788 * Switch old subbuffer.
1789 */
1790 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc);
1791}
1792EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow);
1793
9dded431
MD
1794struct switch_param {
1795 struct lib_ring_buffer *buf;
1796 enum switch_mode mode;
1797};
1798
5e391252
MD
1799static void remote_switch(void *info)
1800{
9dded431
MD
1801 struct switch_param *param = info;
1802 struct lib_ring_buffer *buf = param->buf;
5e391252 1803
9dded431 1804 lib_ring_buffer_switch_slow(buf, param->mode);
5e391252
MD
1805}
1806
64af2437
MD
1807static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf,
1808 enum switch_mode mode)
5e391252
MD
1809{
1810 struct channel *chan = buf->backend.chan;
1811 const struct lib_ring_buffer_config *config = &chan->backend.config;
1812 int ret;
9dded431 1813 struct switch_param param;
5e391252
MD
1814
1815 /*
1816 * With global synchronization we don't need to use the IPI scheme.
1817 */
1818 if (config->sync == RING_BUFFER_SYNC_GLOBAL) {
64af2437 1819 lib_ring_buffer_switch_slow(buf, mode);
5e391252
MD
1820 return;
1821 }
1822
1823 /*
1824 * Taking lock on CPU hotplug to ensure two things: first, that the
1825 * target cpu is not taken concurrently offline while we are within
1826 * smp_call_function_single() (I don't trust that get_cpu() on the
1827 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1828 * confirmed)). Secondly, if it happens that the CPU is not online, our
1829 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1830 * CPU hotplug handlers, which can also perform a remote subbuffer
1831 * switch.
1832 */
1833 get_online_cpus();
9dded431
MD
1834 param.buf = buf;
1835 param.mode = mode;
5e391252 1836 ret = smp_call_function_single(buf->backend.cpu,
9dded431 1837 remote_switch, &param, 1);
5e391252
MD
1838 if (ret) {
1839 /* Remote CPU is offline, do it ourself. */
64af2437 1840 lib_ring_buffer_switch_slow(buf, mode);
5e391252
MD
1841 }
1842 put_online_cpus();
1843}
64af2437
MD
1844
1845void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf)
1846{
1847 _lib_ring_buffer_switch_remote(buf, SWITCH_ACTIVE);
1848}
5e391252
MD
1849EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote);
1850
f3bc08c5
MD
1851/*
1852 * Returns :
1853 * 0 if ok
97ca2c54
MD
1854 * -ENOSPC if event size is too large for packet.
1855 * -ENOBUFS if there is currently not enough space in buffer for the event.
1856 * -EIO if data cannot be written into the buffer for any other reason.
f3bc08c5
MD
1857 */
1858static
1859int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf,
1860 struct channel *chan,
1861 struct switch_offsets *offsets,
1862 struct lib_ring_buffer_ctx *ctx)
1863{
5a8fd222 1864 const struct lib_ring_buffer_config *config = &chan->backend.config;
0fdec686 1865 unsigned long reserve_commit_diff, offset_cmp;
f3bc08c5 1866
0fdec686
MD
1867retry:
1868 offsets->begin = offset_cmp = v_read(config, &buf->offset);
f3bc08c5
MD
1869 offsets->old = offsets->begin;
1870 offsets->switch_new_start = 0;
f5ea5800 1871 offsets->switch_new_end = 0;
f3bc08c5
MD
1872 offsets->switch_old_end = 0;
1873 offsets->pre_header_padding = 0;
1874
1875 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
97ca2c54
MD
1876 if ((int64_t) ctx->tsc == -EIO)
1877 return -EIO;
f3bc08c5
MD
1878
1879 if (last_tsc_overflow(config, buf, ctx->tsc))
64c796d8 1880 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
f3bc08c5
MD
1881
1882 if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
1883 offsets->switch_new_start = 1; /* For offsets->begin */
1884 } else {
1885 offsets->size = config->cb.record_header_size(config, chan,
1886 offsets->begin,
f3bc08c5 1887 &offsets->pre_header_padding,
64c796d8 1888 ctx);
f3bc08c5
MD
1889 offsets->size +=
1890 lib_ring_buffer_align(offsets->begin + offsets->size,
1891 ctx->largest_align)
1892 + ctx->data_size;
1893 if (unlikely(subbuf_offset(offsets->begin, chan) +
1894 offsets->size > chan->backend.subbuf_size)) {
1895 offsets->switch_old_end = 1; /* For offsets->old */
1896 offsets->switch_new_start = 1; /* For offsets->begin */
1897 }
1898 }
1899 if (unlikely(offsets->switch_new_start)) {
0fdec686 1900 unsigned long sb_index, commit_count;
f3bc08c5
MD
1901
1902 /*
1903 * We are typically not filling the previous buffer completely.
1904 */
1905 if (likely(offsets->switch_old_end))
1906 offsets->begin = subbuf_align(offsets->begin, chan);
1907 offsets->begin = offsets->begin
1908 + config->cb.subbuffer_header_size();
1909 /* Test new buffer integrity */
1910 sb_index = subbuf_index(offsets->begin, chan);
0fdec686
MD
1911 /*
1912 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1913 * lib_ring_buffer_check_deliver() has the matching
1914 * memory barriers required around commit_cold cc_sb
1915 * updates to ensure reserve and commit counter updates
1916 * are not seen reordered when updated by another CPU.
1917 */
1918 smp_rmb();
1919 commit_count = v_read(config,
1920 &buf->commit_cold[sb_index].cc_sb);
1921 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1922 smp_rmb();
1923 if (unlikely(offset_cmp != v_read(config, &buf->offset))) {
1924 /*
1925 * The reserve counter have been concurrently updated
1926 * while we read the commit counter. This means the
1927 * commit counter we read might not match buf->offset
1928 * due to concurrent update. We therefore need to retry.
1929 */
1930 goto retry;
1931 }
f3bc08c5
MD
1932 reserve_commit_diff =
1933 (buf_trunc(offsets->begin, chan)
1934 >> chan->backend.num_subbuf_order)
0fdec686 1935 - (commit_count & chan->commit_count_mask);
f3bc08c5
MD
1936 if (likely(reserve_commit_diff == 0)) {
1937 /* Next subbuffer not being written to. */
1938 if (unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1939 subbuf_trunc(offsets->begin, chan)
1940 - subbuf_trunc((unsigned long)
1941 atomic_long_read(&buf->consumed), chan)
1942 >= chan->backend.buf_size)) {
1943 /*
1944 * We do not overwrite non consumed buffers
1945 * and we are full : record is lost.
1946 */
1947 v_inc(config, &buf->records_lost_full);
97ca2c54 1948 return -ENOBUFS;
f3bc08c5
MD
1949 } else {
1950 /*
1951 * Next subbuffer not being written to, and we
1952 * are either in overwrite mode or the buffer is
1953 * not full. It's safe to write in this new
1954 * subbuffer.
1955 */
1956 }
1957 } else {
1958 /*
1959 * Next subbuffer reserve offset does not match the
0fdec686
MD
1960 * commit offset, and this did not involve update to the
1961 * reserve counter. Drop record in producer-consumer and
1962 * overwrite mode. Caused by either a writer OOPS or
1963 * too many nested writes over a reserve/commit pair.
f3bc08c5
MD
1964 */
1965 v_inc(config, &buf->records_lost_wrap);
97ca2c54 1966 return -EIO;
f3bc08c5
MD
1967 }
1968 offsets->size =
1969 config->cb.record_header_size(config, chan,
1970 offsets->begin,
f3bc08c5 1971 &offsets->pre_header_padding,
64c796d8 1972 ctx);
f3bc08c5
MD
1973 offsets->size +=
1974 lib_ring_buffer_align(offsets->begin + offsets->size,
1975 ctx->largest_align)
1976 + ctx->data_size;
1977 if (unlikely(subbuf_offset(offsets->begin, chan)
1978 + offsets->size > chan->backend.subbuf_size)) {
1979 /*
1980 * Record too big for subbuffers, report error, don't
1981 * complete the sub-buffer switch.
1982 */
1983 v_inc(config, &buf->records_lost_big);
97ca2c54 1984 return -ENOSPC;
f3bc08c5
MD
1985 } else {
1986 /*
1987 * We just made a successful buffer switch and the
1988 * record fits in the new subbuffer. Let's write.
1989 */
1990 }
1991 } else {
1992 /*
1993 * Record fits in the current buffer and we are not on a switch
1994 * boundary. It's safe to write.
1995 */
1996 }
1997 offsets->end = offsets->begin + offsets->size;
f5ea5800
MD
1998
1999 if (unlikely(subbuf_offset(offsets->end, chan) == 0)) {
2000 /*
2001 * The offset_end will fall at the very beginning of the next
2002 * subbuffer.
2003 */
2004 offsets->switch_new_end = 1; /* For offsets->begin */
2005 }
f3bc08c5
MD
2006 return 0;
2007}
2008
d7e74017
MD
2009static struct lib_ring_buffer *get_current_buf(struct channel *chan, int cpu)
2010{
2011 const struct lib_ring_buffer_config *config = &chan->backend.config;
2012
2013 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
2014 return per_cpu_ptr(chan->backend.buf, cpu);
2015 else
2016 return chan->backend.buf;
2017}
2018
2019void lib_ring_buffer_lost_event_too_big(struct channel *chan)
2020{
2021 const struct lib_ring_buffer_config *config = &chan->backend.config;
2022 struct lib_ring_buffer *buf = get_current_buf(chan, smp_processor_id());
2023
2024 v_inc(config, &buf->records_lost_big);
2025}
2026EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big);
2027
f3bc08c5
MD
2028/**
2029 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2030 * @ctx: ring buffer context.
2031 *
97ca2c54
MD
2032 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2033 * -EIO for other errors, else returns 0.
f3bc08c5
MD
2034 * It will take care of sub-buffer switching.
2035 */
2036int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx)
2037{
2038 struct channel *chan = ctx->chan;
5a8fd222 2039 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5
MD
2040 struct lib_ring_buffer *buf;
2041 struct switch_offsets offsets;
c099397a 2042 int ret;
f3bc08c5 2043
d7e74017 2044 ctx->buf = buf = get_current_buf(chan, ctx->cpu);
f3bc08c5
MD
2045 offsets.size = 0;
2046
2047 do {
97ca2c54
MD
2048 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
2049 ctx);
2050 if (unlikely(ret))
2051 return ret;
f3bc08c5
MD
2052 } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
2053 offsets.end)
2054 != offsets.old));
2055
2056 /*
2057 * Atomically update last_tsc. This update races against concurrent
2058 * atomic updates, but the race will always cause supplementary full TSC
2059 * records, never the opposite (missing a full TSC record when it would
2060 * be needed).
2061 */
2062 save_last_tsc(config, buf, ctx->tsc);
2063
2064 /*
2065 * Push the reader if necessary
2066 */
2067 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
2068
2069 /*
2070 * Clear noref flag for this subbuffer.
2071 */
2072 lib_ring_buffer_clear_noref(config, &buf->backend,
2073 subbuf_index(offsets.end - 1, chan));
2074
2075 /*
2076 * Switch old subbuffer if needed.
2077 */
2078 if (unlikely(offsets.switch_old_end)) {
2079 lib_ring_buffer_clear_noref(config, &buf->backend,
2080 subbuf_index(offsets.old - 1, chan));
2081 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc);
2082 }
2083
2084 /*
2085 * Populate new subbuffer.
2086 */
2087 if (unlikely(offsets.switch_new_start))
2088 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc);
2089
f5ea5800
MD
2090 if (unlikely(offsets.switch_new_end))
2091 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc);
2092
f3bc08c5
MD
2093 ctx->slot_size = offsets.size;
2094 ctx->pre_offset = offsets.begin;
2095 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
2096 return 0;
2097}
2098EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow);
6fb8de4b 2099
aece661f
MD
2100static
2101void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config *config,
2102 struct lib_ring_buffer *buf,
2103 unsigned long commit_count,
2104 unsigned long idx)
2105{
2106 if (config->oops == RING_BUFFER_OOPS_CONSISTENCY)
2107 v_set(config, &buf->commit_hot[idx].seq, commit_count);
2108}
2109
25337cb5
MD
2110/*
2111 * The ring buffer can count events recorded and overwritten per buffer,
2112 * but it is disabled by default due to its performance overhead.
2113 */
2114#ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2115static
2116void deliver_count_events(const struct lib_ring_buffer_config *config,
2117 struct lib_ring_buffer *buf,
2118 unsigned long idx)
2119{
2120 v_add(config, subbuffer_get_records_count(config,
2121 &buf->backend, idx),
2122 &buf->records_count);
2123 v_add(config, subbuffer_count_records_overrun(config,
2124 &buf->backend, idx),
2125 &buf->records_overrun);
2126}
2127#else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2128static
2129void deliver_count_events(const struct lib_ring_buffer_config *config,
2130 struct lib_ring_buffer *buf,
2131 unsigned long idx)
2132{
2133}
2134#endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2135
2136
aece661f
MD
2137void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config *config,
2138 struct lib_ring_buffer *buf,
2139 struct channel *chan,
2140 unsigned long offset,
2141 unsigned long commit_count,
2142 unsigned long idx,
2143 u64 tsc)
2144{
2145 unsigned long old_commit_count = commit_count
2146 - chan->backend.subbuf_size;
2147
2148 /*
2149 * If we succeeded at updating cc_sb below, we are the subbuffer
2150 * writer delivering the subbuffer. Deals with concurrent
2151 * updates of the "cc" value without adding a add_return atomic
2152 * operation to the fast path.
2153 *
2154 * We are doing the delivery in two steps:
2155 * - First, we cmpxchg() cc_sb to the new value
2156 * old_commit_count + 1. This ensures that we are the only
2157 * subbuffer user successfully filling the subbuffer, but we
2158 * do _not_ set the cc_sb value to "commit_count" yet.
2159 * Therefore, other writers that would wrap around the ring
2160 * buffer and try to start writing to our subbuffer would
2161 * have to drop records, because it would appear as
2162 * non-filled.
2163 * We therefore have exclusive access to the subbuffer control
2164 * structures. This mutual exclusion with other writers is
2165 * crucially important to perform record overruns count in
2166 * flight recorder mode locklessly.
2167 * - When we are ready to release the subbuffer (either for
2168 * reading or for overrun by other writers), we simply set the
2169 * cc_sb value to "commit_count" and perform delivery.
2170 *
2171 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2172 * This guarantees that old_commit_count + 1 != commit_count.
2173 */
2174
2175 /*
2176 * Order prior updates to reserve count prior to the
2177 * commit_cold cc_sb update.
2178 */
2179 smp_wmb();
2180 if (likely(v_cmpxchg(config, &buf->commit_cold[idx].cc_sb,
2181 old_commit_count, old_commit_count + 1)
2182 == old_commit_count)) {
2183 /*
2184 * Start of exclusive subbuffer access. We are
2185 * guaranteed to be the last writer in this subbuffer
2186 * and any other writer trying to access this subbuffer
2187 * in this state is required to drop records.
2188 */
25337cb5 2189 deliver_count_events(config, buf, idx);
aece661f
MD
2190 config->cb.buffer_end(buf, tsc, idx,
2191 lib_ring_buffer_get_data_size(config,
2192 buf,
2193 idx));
2194
2195 /*
2196 * Increment the packet counter while we have exclusive
2197 * access.
2198 */
2199 subbuffer_inc_packet_count(config, &buf->backend, idx);
2200
2201 /*
2202 * Set noref flag and offset for this subbuffer id.
2203 * Contains a memory barrier that ensures counter stores
2204 * are ordered before set noref and offset.
2205 */
2206 lib_ring_buffer_set_noref_offset(config, &buf->backend, idx,
2207 buf_trunc_val(offset, chan));
2208
2209 /*
2210 * Order set_noref and record counter updates before the
2211 * end of subbuffer exclusive access. Orders with
2212 * respect to writers coming into the subbuffer after
2213 * wrap around, and also order wrt concurrent readers.
2214 */
2215 smp_mb();
2216 /* End of exclusive subbuffer access */
2217 v_set(config, &buf->commit_cold[idx].cc_sb,
2218 commit_count);
2219 /*
2220 * Order later updates to reserve count after
2221 * the commit_cold cc_sb update.
2222 */
2223 smp_wmb();
2224 lib_ring_buffer_vmcore_check_deliver(config, buf,
2225 commit_count, idx);
2226
2227 /*
2228 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2229 */
2230 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER
2231 && atomic_long_read(&buf->active_readers)
2232 && lib_ring_buffer_poll_deliver(config, buf, chan)) {
2233 wake_up_interruptible(&buf->read_wait);
2234 wake_up_interruptible(&chan->read_wait);
2235 }
2236
2237 }
2238}
2239EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow);
2240
02a766bb 2241int __init init_lib_ring_buffer_frontend(void)
6fb8de4b
MD
2242{
2243 int cpu;
2244
2245 for_each_possible_cpu(cpu)
2246 spin_lock_init(&per_cpu(ring_buffer_nohz_lock, cpu));
02a766bb 2247 return 0;
6fb8de4b 2248}
02a766bb
MD
2249
2250module_init(init_lib_ring_buffer_frontend);
1a5db82d
MD
2251
2252void __exit exit_lib_ring_buffer_frontend(void)
2253{
2254}
2255
2256module_exit(exit_lib_ring_buffer_frontend);
This page took 0.129886 seconds and 4 git commands to generate.