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