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