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