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