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