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