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