Commit | Line | Data |
---|---|---|
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> | |
57 | ||
c075712b MD |
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> | |
47da52ac | 66 | #include <wrapper/timer.h> |
f3bc08c5 MD |
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; | |
f5ea5800 MD |
74 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, |
75 | switch_old_end:1; | |
f3bc08c5 MD |
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); | |
1b7801d6 MD |
96 | static |
97 | void _lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf, | |
98 | enum switch_mode mode); | |
f3bc08c5 MD |
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; | |
5a8fd222 | 126 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
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 | { | |
5a8fd222 | 184 | const struct lib_ring_buffer_config *config = &chanb->config; |
f3bc08c5 MD |
185 | struct channel *chan = container_of(chanb, struct channel, backend); |
186 | void *priv = chanb->priv; | |
f3bc08c5 MD |
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), | |
6e4fc6f3 MD |
209 | GFP_KERNEL | __GFP_NOWARN, |
210 | cpu_to_node(max(cpu, 0))); | |
f3bc08c5 MD |
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), | |
6e4fc6f3 MD |
220 | GFP_KERNEL | __GFP_NOWARN, |
221 | cpu_to_node(max(cpu, 0))); | |
f3bc08c5 MD |
222 | if (!buf->commit_cold) { |
223 | ret = -ENOMEM; | |
224 | goto free_commit; | |
225 | } | |
226 | ||
f3bc08c5 | 227 | init_waitqueue_head(&buf->read_wait); |
71c1d843 | 228 | init_waitqueue_head(&buf->write_wait); |
f3bc08c5 MD |
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; | |
5a8fd222 | 278 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
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) | |
47da52ac | 287 | lttng_mod_timer_pinned(&buf->switch_timer, |
f3bc08c5 MD |
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; | |
5a8fd222 | 300 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
301 | |
302 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) | |
303 | return; | |
47da52ac MJ |
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 | ||
f3bc08c5 MD |
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; | |
5a8fd222 | 341 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
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) | |
47da52ac | 352 | lttng_mod_timer_pinned(&buf->read_timer, |
f3bc08c5 MD |
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; | |
5a8fd222 | 365 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
366 | |
367 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
368 | || !chan->read_timer_interval | |
369 | || buf->read_timer_enabled) | |
370 | return; | |
371 | ||
47da52ac MJ |
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 | ||
f3bc08c5 MD |
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; | |
5a8fd222 | 394 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
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 | #ifdef CONFIG_HOTPLUG_CPU | |
414 | /** | |
415 | * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback | |
416 | * @nb: notifier block | |
417 | * @action: hotplug action to take | |
418 | * @hcpu: CPU number | |
419 | * | |
420 | * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) | |
421 | */ | |
422 | static | |
e8f071d5 | 423 | int lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb, |
f3bc08c5 MD |
424 | unsigned long action, |
425 | void *hcpu) | |
426 | { | |
427 | unsigned int cpu = (unsigned long)hcpu; | |
428 | struct channel *chan = container_of(nb, struct channel, | |
429 | cpu_hp_notifier); | |
430 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu); | |
5a8fd222 | 431 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
432 | |
433 | if (!chan->cpu_hp_enable) | |
434 | return NOTIFY_DONE; | |
435 | ||
436 | CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL); | |
437 | ||
438 | switch (action) { | |
439 | case CPU_DOWN_FAILED: | |
440 | case CPU_DOWN_FAILED_FROZEN: | |
441 | case CPU_ONLINE: | |
442 | case CPU_ONLINE_FROZEN: | |
24cedcfe | 443 | wake_up_interruptible(&chan->hp_wait); |
f3bc08c5 MD |
444 | lib_ring_buffer_start_switch_timer(buf); |
445 | lib_ring_buffer_start_read_timer(buf); | |
446 | return NOTIFY_OK; | |
447 | ||
448 | case CPU_DOWN_PREPARE: | |
449 | case CPU_DOWN_PREPARE_FROZEN: | |
450 | lib_ring_buffer_stop_switch_timer(buf); | |
451 | lib_ring_buffer_stop_read_timer(buf); | |
452 | return NOTIFY_OK; | |
453 | ||
454 | case CPU_DEAD: | |
455 | case CPU_DEAD_FROZEN: | |
456 | /* | |
457 | * Performing a buffer switch on a remote CPU. Performed by | |
458 | * the CPU responsible for doing the hotunplug after the target | |
459 | * CPU stopped running completely. Ensures that all data | |
460 | * from that remote CPU is flushed. | |
461 | */ | |
462 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); | |
463 | return NOTIFY_OK; | |
464 | ||
465 | default: | |
466 | return NOTIFY_DONE; | |
467 | } | |
468 | } | |
469 | #endif | |
470 | ||
23b908b0 | 471 | #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) |
f3bc08c5 MD |
472 | /* |
473 | * For per-cpu buffers, call the reader wakeups before switching the buffer, so | |
474 | * that wake-up-tracing generated events are flushed before going idle (in | |
475 | * tick_nohz). We test if the spinlock is locked to deal with the race where | |
476 | * readers try to sample the ring buffer before we perform the switch. We let | |
477 | * the readers retry in that case. If there is data in the buffer, the wake up | |
478 | * is going to forbid the CPU running the reader thread from going idle. | |
479 | */ | |
480 | static int notrace ring_buffer_tick_nohz_callback(struct notifier_block *nb, | |
481 | unsigned long val, | |
482 | void *data) | |
483 | { | |
484 | struct channel *chan = container_of(nb, struct channel, | |
485 | tick_nohz_notifier); | |
5a8fd222 | 486 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
487 | struct lib_ring_buffer *buf; |
488 | int cpu = smp_processor_id(); | |
489 | ||
490 | if (config->alloc != RING_BUFFER_ALLOC_PER_CPU) { | |
491 | /* | |
492 | * We don't support keeping the system idle with global buffers | |
493 | * and streaming active. In order to do so, we would need to | |
494 | * sample a non-nohz-cpumask racelessly with the nohz updates | |
495 | * without adding synchronization overhead to nohz. Leave this | |
496 | * use-case out for now. | |
497 | */ | |
498 | return 0; | |
499 | } | |
500 | ||
501 | buf = channel_get_ring_buffer(config, chan, cpu); | |
502 | switch (val) { | |
503 | case TICK_NOHZ_FLUSH: | |
504 | raw_spin_lock(&buf->raw_tick_nohz_spinlock); | |
505 | if (config->wakeup == RING_BUFFER_WAKEUP_BY_TIMER | |
506 | && chan->read_timer_interval | |
507 | && atomic_long_read(&buf->active_readers) | |
508 | && (lib_ring_buffer_poll_deliver(config, buf, chan) | |
509 | || lib_ring_buffer_pending_data(config, buf, chan))) { | |
510 | wake_up_interruptible(&buf->read_wait); | |
511 | wake_up_interruptible(&chan->read_wait); | |
512 | } | |
513 | if (chan->switch_timer_interval) | |
514 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); | |
515 | raw_spin_unlock(&buf->raw_tick_nohz_spinlock); | |
516 | break; | |
517 | case TICK_NOHZ_STOP: | |
e6b06d7d | 518 | spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
f3bc08c5 MD |
519 | lib_ring_buffer_stop_switch_timer(buf); |
520 | lib_ring_buffer_stop_read_timer(buf); | |
e6b06d7d | 521 | spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
f3bc08c5 MD |
522 | break; |
523 | case TICK_NOHZ_RESTART: | |
e6b06d7d | 524 | spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
f3bc08c5 MD |
525 | lib_ring_buffer_start_read_timer(buf); |
526 | lib_ring_buffer_start_switch_timer(buf); | |
e6b06d7d | 527 | spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
f3bc08c5 MD |
528 | break; |
529 | } | |
530 | ||
531 | return 0; | |
532 | } | |
533 | ||
534 | void notrace lib_ring_buffer_tick_nohz_flush(void) | |
535 | { | |
536 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_FLUSH, | |
537 | NULL); | |
538 | } | |
539 | ||
540 | void notrace lib_ring_buffer_tick_nohz_stop(void) | |
541 | { | |
542 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_STOP, | |
543 | NULL); | |
544 | } | |
545 | ||
546 | void notrace lib_ring_buffer_tick_nohz_restart(void) | |
547 | { | |
548 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_RESTART, | |
549 | NULL); | |
550 | } | |
23b908b0 | 551 | #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */ |
f3bc08c5 MD |
552 | |
553 | /* | |
554 | * Holds CPU hotplug. | |
555 | */ | |
556 | static void channel_unregister_notifiers(struct channel *chan) | |
557 | { | |
5a8fd222 | 558 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
559 | int cpu; |
560 | ||
561 | channel_iterator_unregister_notifiers(chan); | |
562 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
563 | #ifdef CONFIG_NO_HZ | |
564 | /* | |
565 | * Remove the nohz notifier first, so we are certain we stop | |
566 | * the timers. | |
567 | */ | |
568 | atomic_notifier_chain_unregister(&tick_nohz_notifier, | |
569 | &chan->tick_nohz_notifier); | |
570 | /* | |
571 | * ring_buffer_nohz_lock will not be needed below, because | |
572 | * we just removed the notifiers, which were the only source of | |
573 | * concurrency. | |
574 | */ | |
575 | #endif /* CONFIG_NO_HZ */ | |
576 | #ifdef CONFIG_HOTPLUG_CPU | |
577 | get_online_cpus(); | |
578 | chan->cpu_hp_enable = 0; | |
579 | for_each_online_cpu(cpu) { | |
580 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, | |
581 | cpu); | |
582 | lib_ring_buffer_stop_switch_timer(buf); | |
583 | lib_ring_buffer_stop_read_timer(buf); | |
584 | } | |
585 | put_online_cpus(); | |
586 | unregister_cpu_notifier(&chan->cpu_hp_notifier); | |
587 | #else | |
588 | for_each_possible_cpu(cpu) { | |
589 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, | |
590 | cpu); | |
591 | lib_ring_buffer_stop_switch_timer(buf); | |
592 | lib_ring_buffer_stop_read_timer(buf); | |
593 | } | |
594 | #endif | |
595 | } else { | |
596 | struct lib_ring_buffer *buf = chan->backend.buf; | |
597 | ||
598 | lib_ring_buffer_stop_switch_timer(buf); | |
599 | lib_ring_buffer_stop_read_timer(buf); | |
600 | } | |
601 | channel_backend_unregister_notifiers(&chan->backend); | |
602 | } | |
603 | ||
1b7801d6 MD |
604 | static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer *buf) |
605 | { | |
606 | if (!buf->quiescent) { | |
607 | buf->quiescent = true; | |
608 | _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH); | |
609 | } | |
610 | } | |
611 | ||
612 | static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer *buf) | |
613 | { | |
614 | buf->quiescent = false; | |
615 | } | |
616 | ||
617 | void lib_ring_buffer_set_quiescent_channel(struct channel *chan) | |
618 | { | |
619 | int cpu; | |
620 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
621 | ||
622 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
623 | get_online_cpus(); | |
624 | for_each_channel_cpu(cpu, chan) { | |
625 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, | |
626 | cpu); | |
627 | ||
628 | lib_ring_buffer_set_quiescent(buf); | |
629 | } | |
630 | put_online_cpus(); | |
631 | } else { | |
632 | struct lib_ring_buffer *buf = chan->backend.buf; | |
633 | ||
634 | lib_ring_buffer_set_quiescent(buf); | |
635 | } | |
636 | } | |
637 | EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel); | |
638 | ||
639 | void lib_ring_buffer_clear_quiescent_channel(struct channel *chan) | |
640 | { | |
641 | int cpu; | |
642 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
643 | ||
644 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
645 | get_online_cpus(); | |
646 | for_each_channel_cpu(cpu, chan) { | |
647 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, | |
648 | cpu); | |
649 | ||
650 | lib_ring_buffer_clear_quiescent(buf); | |
651 | } | |
652 | put_online_cpus(); | |
653 | } else { | |
654 | struct lib_ring_buffer *buf = chan->backend.buf; | |
655 | ||
656 | lib_ring_buffer_clear_quiescent(buf); | |
657 | } | |
658 | } | |
659 | EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel); | |
660 | ||
f3bc08c5 MD |
661 | static void channel_free(struct channel *chan) |
662 | { | |
dd5a0db3 MD |
663 | if (chan->backend.release_priv_ops) { |
664 | chan->backend.release_priv_ops(chan->backend.priv_ops); | |
665 | } | |
f3bc08c5 MD |
666 | channel_iterator_free(chan); |
667 | channel_backend_free(&chan->backend); | |
668 | kfree(chan); | |
669 | } | |
670 | ||
671 | /** | |
672 | * channel_create - Create channel. | |
673 | * @config: ring buffer instance configuration | |
674 | * @name: name of the channel | |
675 | * @priv: ring buffer client private data | |
676 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous | |
677 | * address mapping. It is used only by RING_BUFFER_STATIC | |
678 | * configuration. It can be set to NULL for other backends. | |
679 | * @subbuf_size: subbuffer size | |
680 | * @num_subbuf: number of subbuffers | |
681 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
682 | * padding to let readers get those sub-buffers. | |
683 | * Used for live streaming. | |
684 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
685 | * | |
686 | * Holds cpu hotplug. | |
687 | * Returns NULL on failure. | |
688 | */ | |
689 | struct channel *channel_create(const struct lib_ring_buffer_config *config, | |
690 | const char *name, void *priv, void *buf_addr, | |
691 | size_t subbuf_size, | |
692 | size_t num_subbuf, unsigned int switch_timer_interval, | |
693 | unsigned int read_timer_interval) | |
694 | { | |
695 | int ret, cpu; | |
696 | struct channel *chan; | |
697 | ||
698 | if (lib_ring_buffer_check_config(config, switch_timer_interval, | |
699 | read_timer_interval)) | |
700 | return NULL; | |
701 | ||
702 | chan = kzalloc(sizeof(struct channel), GFP_KERNEL); | |
703 | if (!chan) | |
704 | return NULL; | |
705 | ||
706 | ret = channel_backend_init(&chan->backend, name, config, priv, | |
707 | subbuf_size, num_subbuf); | |
708 | if (ret) | |
709 | goto error; | |
710 | ||
711 | ret = channel_iterator_init(chan); | |
712 | if (ret) | |
713 | goto error_free_backend; | |
714 | ||
715 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); | |
716 | chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); | |
717 | chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); | |
f40270ad | 718 | kref_init(&chan->ref); |
f3bc08c5 | 719 | init_waitqueue_head(&chan->read_wait); |
24cedcfe | 720 | init_waitqueue_head(&chan->hp_wait); |
f3bc08c5 MD |
721 | |
722 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
23b908b0 | 723 | #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) |
f3bc08c5 MD |
724 | /* Only benefit from NO_HZ idle with per-cpu buffers for now. */ |
725 | chan->tick_nohz_notifier.notifier_call = | |
726 | ring_buffer_tick_nohz_callback; | |
727 | chan->tick_nohz_notifier.priority = ~0U; | |
728 | atomic_notifier_chain_register(&tick_nohz_notifier, | |
729 | &chan->tick_nohz_notifier); | |
23b908b0 | 730 | #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */ |
f3bc08c5 MD |
731 | |
732 | /* | |
733 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
734 | * in early initcall, it will not be notified of secondary cpus. | |
735 | * In that off case, we need to allocate for all possible cpus. | |
736 | */ | |
737 | #ifdef CONFIG_HOTPLUG_CPU | |
738 | chan->cpu_hp_notifier.notifier_call = | |
739 | lib_ring_buffer_cpu_hp_callback; | |
740 | chan->cpu_hp_notifier.priority = 6; | |
741 | register_cpu_notifier(&chan->cpu_hp_notifier); | |
742 | ||
743 | get_online_cpus(); | |
744 | for_each_online_cpu(cpu) { | |
745 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, | |
746 | cpu); | |
747 | spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu)); | |
748 | lib_ring_buffer_start_switch_timer(buf); | |
749 | lib_ring_buffer_start_read_timer(buf); | |
750 | spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu)); | |
751 | } | |
752 | chan->cpu_hp_enable = 1; | |
753 | put_online_cpus(); | |
754 | #else | |
755 | for_each_possible_cpu(cpu) { | |
756 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, | |
757 | cpu); | |
758 | spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu)); | |
759 | lib_ring_buffer_start_switch_timer(buf); | |
760 | lib_ring_buffer_start_read_timer(buf); | |
761 | spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu)); | |
762 | } | |
763 | #endif | |
764 | } else { | |
765 | struct lib_ring_buffer *buf = chan->backend.buf; | |
766 | ||
767 | lib_ring_buffer_start_switch_timer(buf); | |
768 | lib_ring_buffer_start_read_timer(buf); | |
769 | } | |
770 | ||
771 | return chan; | |
772 | ||
773 | error_free_backend: | |
774 | channel_backend_free(&chan->backend); | |
775 | error: | |
776 | kfree(chan); | |
777 | return NULL; | |
778 | } | |
779 | EXPORT_SYMBOL_GPL(channel_create); | |
780 | ||
f40270ad MD |
781 | static |
782 | void channel_release(struct kref *kref) | |
783 | { | |
784 | struct channel *chan = container_of(kref, struct channel, ref); | |
785 | channel_free(chan); | |
786 | } | |
787 | ||
f3bc08c5 MD |
788 | /** |
789 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
790 | * @chan: channel to destroy | |
791 | * | |
792 | * Holds cpu hotplug. | |
9a0df743 MD |
793 | * Call "destroy" callback, finalize channels, and then decrement the |
794 | * channel reference count. Note that when readers have completed data | |
795 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
796 | * They should release their handle at that point. Returns the private | |
797 | * data pointer. | |
f3bc08c5 MD |
798 | */ |
799 | void *channel_destroy(struct channel *chan) | |
800 | { | |
801 | int cpu; | |
5a8fd222 | 802 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
803 | void *priv; |
804 | ||
805 | channel_unregister_notifiers(chan); | |
806 | ||
807 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
808 | /* | |
809 | * No need to hold cpu hotplug, because all notifiers have been | |
810 | * unregistered. | |
811 | */ | |
812 | for_each_channel_cpu(cpu, chan) { | |
813 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, | |
814 | cpu); | |
815 | ||
816 | if (config->cb.buffer_finalize) | |
817 | config->cb.buffer_finalize(buf, | |
818 | chan->backend.priv, | |
819 | cpu); | |
820 | if (buf->backend.allocated) | |
1b7801d6 | 821 | lib_ring_buffer_set_quiescent(buf); |
f3bc08c5 MD |
822 | /* |
823 | * Perform flush before writing to finalized. | |
824 | */ | |
825 | smp_wmb(); | |
826 | ACCESS_ONCE(buf->finalized) = 1; | |
827 | wake_up_interruptible(&buf->read_wait); | |
828 | } | |
829 | } else { | |
830 | struct lib_ring_buffer *buf = chan->backend.buf; | |
831 | ||
832 | if (config->cb.buffer_finalize) | |
833 | config->cb.buffer_finalize(buf, chan->backend.priv, -1); | |
834 | if (buf->backend.allocated) | |
1b7801d6 | 835 | lib_ring_buffer_set_quiescent(buf); |
f3bc08c5 MD |
836 | /* |
837 | * Perform flush before writing to finalized. | |
838 | */ | |
839 | smp_wmb(); | |
840 | ACCESS_ONCE(buf->finalized) = 1; | |
841 | wake_up_interruptible(&buf->read_wait); | |
842 | } | |
24cedcfe MD |
843 | ACCESS_ONCE(chan->finalized) = 1; |
844 | wake_up_interruptible(&chan->hp_wait); | |
f3bc08c5 | 845 | wake_up_interruptible(&chan->read_wait); |
f3bc08c5 | 846 | priv = chan->backend.priv; |
ba1d61bc | 847 | kref_put(&chan->ref, channel_release); |
f3bc08c5 MD |
848 | return priv; |
849 | } | |
850 | EXPORT_SYMBOL_GPL(channel_destroy); | |
851 | ||
852 | struct lib_ring_buffer *channel_get_ring_buffer( | |
853 | const struct lib_ring_buffer_config *config, | |
854 | struct channel *chan, int cpu) | |
855 | { | |
856 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) | |
857 | return chan->backend.buf; | |
858 | else | |
859 | return per_cpu_ptr(chan->backend.buf, cpu); | |
860 | } | |
861 | EXPORT_SYMBOL_GPL(channel_get_ring_buffer); | |
862 | ||
863 | int lib_ring_buffer_open_read(struct lib_ring_buffer *buf) | |
864 | { | |
865 | struct channel *chan = buf->backend.chan; | |
866 | ||
867 | if (!atomic_long_add_unless(&buf->active_readers, 1, 1)) | |
868 | return -EBUSY; | |
9c1f4643 MD |
869 | if (!lttng_kref_get(&chan->ref)) { |
870 | atomic_long_dec(&buf->active_readers); | |
871 | return -EOVERFLOW; | |
872 | } | |
505fb410 | 873 | lttng_smp_mb__after_atomic(); |
f3bc08c5 MD |
874 | return 0; |
875 | } | |
876 | EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read); | |
877 | ||
878 | void lib_ring_buffer_release_read(struct lib_ring_buffer *buf) | |
879 | { | |
880 | struct channel *chan = buf->backend.chan; | |
881 | ||
882 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); | |
505fb410 | 883 | lttng_smp_mb__before_atomic(); |
f3bc08c5 | 884 | atomic_long_dec(&buf->active_readers); |
f40270ad | 885 | kref_put(&chan->ref, channel_release); |
f3bc08c5 MD |
886 | } |
887 | EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read); | |
888 | ||
889 | /* | |
890 | * Promote compiler barrier to a smp_mb(). | |
891 | * For the specific ring buffer case, this IPI call should be removed if the | |
892 | * architecture does not reorder writes. This should eventually be provided by | |
893 | * a separate architecture-specific infrastructure. | |
894 | */ | |
895 | static void remote_mb(void *info) | |
896 | { | |
897 | smp_mb(); | |
898 | } | |
899 | ||
900 | /** | |
901 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
902 | * @buf: ring buffer | |
903 | * @consumed: consumed count indicating the position where to read | |
904 | * @produced: produced count, indicates position when to stop reading | |
905 | * | |
906 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
907 | * data to read at consumed position, or 0 if the get operation succeeds. | |
908 | * Busy-loop trying to get data if the tick_nohz sequence lock is held. | |
909 | */ | |
910 | ||
911 | int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf, | |
912 | unsigned long *consumed, unsigned long *produced) | |
913 | { | |
914 | struct channel *chan = buf->backend.chan; | |
5a8fd222 | 915 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
916 | unsigned long consumed_cur, write_offset; |
917 | int finalized; | |
918 | ||
919 | retry: | |
920 | finalized = ACCESS_ONCE(buf->finalized); | |
921 | /* | |
922 | * Read finalized before counters. | |
923 | */ | |
924 | smp_rmb(); | |
925 | consumed_cur = atomic_long_read(&buf->consumed); | |
926 | /* | |
927 | * No need to issue a memory barrier between consumed count read and | |
928 | * write offset read, because consumed count can only change | |
929 | * concurrently in overwrite mode, and we keep a sequence counter | |
930 | * identifier derived from the write offset to check we are getting | |
931 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
932 | * "tagged" upon writes, tags are checked upon read). | |
933 | */ | |
934 | write_offset = v_read(config, &buf->offset); | |
935 | ||
936 | /* | |
937 | * Check that we are not about to read the same subbuffer in | |
938 | * which the writer head is. | |
939 | */ | |
940 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
941 | == 0) | |
942 | goto nodata; | |
943 | ||
944 | *consumed = consumed_cur; | |
945 | *produced = subbuf_trunc(write_offset, chan); | |
946 | ||
947 | return 0; | |
948 | ||
949 | nodata: | |
950 | /* | |
951 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
952 | * of "raw_spin_is_locked" memory ordering. | |
953 | */ | |
954 | if (finalized) | |
955 | return -ENODATA; | |
956 | else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) | |
957 | goto retry; | |
958 | else | |
959 | return -EAGAIN; | |
960 | } | |
961 | EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot); | |
962 | ||
963 | /** | |
964 | * lib_ring_buffer_put_snapshot - move consumed counter forward | |
71c1d843 MD |
965 | * |
966 | * Should only be called from consumer context. | |
f3bc08c5 MD |
967 | * @buf: ring buffer |
968 | * @consumed_new: new consumed count value | |
969 | */ | |
970 | void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf, | |
971 | unsigned long consumed_new) | |
972 | { | |
973 | struct lib_ring_buffer_backend *bufb = &buf->backend; | |
974 | struct channel *chan = bufb->chan; | |
975 | unsigned long consumed; | |
976 | ||
977 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); | |
978 | ||
979 | /* | |
980 | * Only push the consumed value forward. | |
981 | * If the consumed cmpxchg fails, this is because we have been pushed by | |
982 | * the writer in flight recorder mode. | |
983 | */ | |
984 | consumed = atomic_long_read(&buf->consumed); | |
985 | while ((long) consumed - (long) consumed_new < 0) | |
986 | consumed = atomic_long_cmpxchg(&buf->consumed, consumed, | |
987 | consumed_new); | |
71c1d843 MD |
988 | /* Wake-up the metadata producer */ |
989 | wake_up_interruptible(&buf->write_wait); | |
f3bc08c5 MD |
990 | } |
991 | EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer); | |
992 | ||
993 | /** | |
994 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading | |
995 | * @buf: ring buffer | |
996 | * @consumed: consumed count indicating the position where to read | |
997 | * | |
998 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
999 | * data to read at consumed position, or 0 if the get operation succeeds. | |
1000 | * Busy-loop trying to get data if the tick_nohz sequence lock is held. | |
1001 | */ | |
1002 | int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf, | |
1003 | unsigned long consumed) | |
1004 | { | |
1005 | struct channel *chan = buf->backend.chan; | |
5a8fd222 | 1006 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1007 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
1008 | int ret; | |
1009 | int finalized; | |
1010 | ||
8202b8a0 MD |
1011 | if (buf->get_subbuf) { |
1012 | /* | |
1013 | * Reader is trying to get a subbuffer twice. | |
1014 | */ | |
1015 | CHAN_WARN_ON(chan, 1); | |
1016 | return -EBUSY; | |
1017 | } | |
f3bc08c5 MD |
1018 | retry: |
1019 | finalized = ACCESS_ONCE(buf->finalized); | |
1020 | /* | |
1021 | * Read finalized before counters. | |
1022 | */ | |
1023 | smp_rmb(); | |
1024 | consumed_cur = atomic_long_read(&buf->consumed); | |
1025 | consumed_idx = subbuf_index(consumed, chan); | |
1026 | commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb); | |
1027 | /* | |
1028 | * Make sure we read the commit count before reading the buffer | |
1029 | * data and the write offset. Correct consumed offset ordering | |
1030 | * wrt commit count is insured by the use of cmpxchg to update | |
1031 | * the consumed offset. | |
1032 | * smp_call_function_single can fail if the remote CPU is offline, | |
1033 | * this is OK because then there is no wmb to execute there. | |
1034 | * If our thread is executing on the same CPU as the on the buffers | |
1035 | * belongs to, we don't have to synchronize it at all. If we are | |
1036 | * migrated, the scheduler will take care of the memory barriers. | |
1037 | * Normally, smp_call_function_single() should ensure program order when | |
1038 | * executing the remote function, which implies that it surrounds the | |
1039 | * function execution with : | |
1040 | * smp_mb() | |
1041 | * send IPI | |
1042 | * csd_lock_wait | |
1043 | * recv IPI | |
1044 | * smp_mb() | |
1045 | * exec. function | |
1046 | * smp_mb() | |
1047 | * csd unlock | |
1048 | * smp_mb() | |
1049 | * | |
1050 | * However, smp_call_function_single() does not seem to clearly execute | |
1051 | * such barriers. It depends on spinlock semantic to provide the barrier | |
1052 | * before executing the IPI and, when busy-looping, csd_lock_wait only | |
1053 | * executes smp_mb() when it has to wait for the other CPU. | |
1054 | * | |
1055 | * I don't trust this code. Therefore, let's add the smp_mb() sequence | |
1056 | * required ourself, even if duplicated. It has no performance impact | |
1057 | * anyway. | |
1058 | * | |
1059 | * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs | |
1060 | * read and write vs write. They do not ensure core synchronization. We | |
1061 | * really have to ensure total order between the 3 barriers running on | |
1062 | * the 2 CPUs. | |
1063 | */ | |
1064 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { | |
1065 | if (config->sync == RING_BUFFER_SYNC_PER_CPU | |
1066 | && config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
1067 | if (raw_smp_processor_id() != buf->backend.cpu) { | |
1068 | /* Total order with IPI handler smp_mb() */ | |
1069 | smp_mb(); | |
1070 | smp_call_function_single(buf->backend.cpu, | |
1071 | remote_mb, NULL, 1); | |
1072 | /* Total order with IPI handler smp_mb() */ | |
1073 | smp_mb(); | |
1074 | } | |
1075 | } else { | |
1076 | /* Total order with IPI handler smp_mb() */ | |
1077 | smp_mb(); | |
1078 | smp_call_function(remote_mb, NULL, 1); | |
1079 | /* Total order with IPI handler smp_mb() */ | |
1080 | smp_mb(); | |
1081 | } | |
1082 | } else { | |
1083 | /* | |
1084 | * Local rmb to match the remote wmb to read the commit count | |
1085 | * before the buffer data and the write offset. | |
1086 | */ | |
1087 | smp_rmb(); | |
1088 | } | |
1089 | ||
1090 | write_offset = v_read(config, &buf->offset); | |
1091 | ||
1092 | /* | |
1093 | * Check that the buffer we are getting is after or at consumed_cur | |
1094 | * position. | |
1095 | */ | |
1096 | if ((long) subbuf_trunc(consumed, chan) | |
1097 | - (long) subbuf_trunc(consumed_cur, chan) < 0) | |
1098 | goto nodata; | |
1099 | ||
1100 | /* | |
1101 | * Check that the subbuffer we are trying to consume has been | |
1102 | * already fully committed. | |
1103 | */ | |
1104 | if (((commit_count - chan->backend.subbuf_size) | |
1105 | & chan->commit_count_mask) | |
c9b3b5e2 | 1106 | - (buf_trunc(consumed, chan) |
f3bc08c5 MD |
1107 | >> chan->backend.num_subbuf_order) |
1108 | != 0) | |
1109 | goto nodata; | |
1110 | ||
1111 | /* | |
1112 | * Check that we are not about to read the same subbuffer in | |
1113 | * which the writer head is. | |
1114 | */ | |
c9b3b5e2 | 1115 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan) |
f3bc08c5 MD |
1116 | == 0) |
1117 | goto nodata; | |
1118 | ||
1119 | /* | |
1120 | * Failure to get the subbuffer causes a busy-loop retry without going | |
1121 | * to a wait queue. These are caused by short-lived race windows where | |
1122 | * the writer is getting access to a subbuffer we were trying to get | |
1123 | * access to. Also checks that the "consumed" buffer count we are | |
1124 | * looking for matches the one contained in the subbuffer id. | |
1125 | */ | |
1126 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, | |
1127 | consumed_idx, buf_trunc_val(consumed, chan)); | |
1128 | if (ret) | |
1129 | goto retry; | |
1130 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); | |
1131 | ||
1132 | buf->get_subbuf_consumed = consumed; | |
1133 | buf->get_subbuf = 1; | |
1134 | ||
1135 | return 0; | |
1136 | ||
1137 | nodata: | |
1138 | /* | |
1139 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
1140 | * of "raw_spin_is_locked" memory ordering. | |
1141 | */ | |
1142 | if (finalized) | |
1143 | return -ENODATA; | |
1144 | else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) | |
1145 | goto retry; | |
1146 | else | |
1147 | return -EAGAIN; | |
1148 | } | |
1149 | EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf); | |
1150 | ||
1151 | /** | |
1152 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access | |
1153 | * @buf: ring buffer | |
1154 | */ | |
1155 | void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf) | |
1156 | { | |
1157 | struct lib_ring_buffer_backend *bufb = &buf->backend; | |
1158 | struct channel *chan = bufb->chan; | |
5a8fd222 | 1159 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1160 | unsigned long read_sb_bindex, consumed_idx, consumed; |
1161 | ||
1162 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); | |
1163 | ||
1164 | if (!buf->get_subbuf) { | |
1165 | /* | |
1166 | * Reader puts a subbuffer it did not get. | |
1167 | */ | |
1168 | CHAN_WARN_ON(chan, 1); | |
1169 | return; | |
1170 | } | |
1171 | consumed = buf->get_subbuf_consumed; | |
1172 | buf->get_subbuf = 0; | |
1173 | ||
1174 | /* | |
1175 | * Clear the records_unread counter. (overruns counter) | |
1176 | * Can still be non-zero if a file reader simply grabbed the data | |
1177 | * without using iterators. | |
1178 | * Can be below zero if an iterator is used on a snapshot more than | |
1179 | * once. | |
1180 | */ | |
1181 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
1182 | v_add(config, v_read(config, | |
1183 | &bufb->array[read_sb_bindex]->records_unread), | |
1184 | &bufb->records_read); | |
1185 | v_set(config, &bufb->array[read_sb_bindex]->records_unread, 0); | |
1186 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE | |
1187 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
1188 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
1189 | ||
1190 | /* | |
1191 | * Exchange the reader subbuffer with the one we put in its place in the | |
1192 | * writer subbuffer table. Expect the original consumed count. If | |
1193 | * update_read_sb_index fails, this is because the writer updated the | |
1194 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
1195 | * currently have: it has become invalid to try reading this sub-buffer | |
1196 | * consumed count value anyway. | |
1197 | */ | |
1198 | consumed_idx = subbuf_index(consumed, chan); | |
1199 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1200 | consumed_idx, buf_trunc_val(consumed, chan)); | |
1201 | /* | |
1202 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
1203 | * if the writer concurrently updated it. | |
1204 | */ | |
1205 | } | |
1206 | EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf); | |
1207 | ||
1208 | /* | |
1209 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
1210 | * position and the writer position. (inclusive) | |
1211 | */ | |
1212 | static | |
1213 | void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf, | |
1214 | struct channel *chan, | |
1215 | unsigned long cons_offset, | |
1216 | int cpu) | |
1217 | { | |
5a8fd222 | 1218 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1219 | unsigned long cons_idx, commit_count, commit_count_sb; |
1220 | ||
1221 | cons_idx = subbuf_index(cons_offset, chan); | |
1222 | commit_count = v_read(config, &buf->commit_hot[cons_idx].cc); | |
1223 | commit_count_sb = v_read(config, &buf->commit_cold[cons_idx].cc_sb); | |
1224 | ||
1225 | if (subbuf_offset(commit_count, chan) != 0) | |
1226 | printk(KERN_WARNING | |
1227 | "ring buffer %s, cpu %d: " | |
1228 | "commit count in subbuffer %lu,\n" | |
1229 | "expecting multiples of %lu bytes\n" | |
1230 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
1231 | chan->backend.name, cpu, cons_idx, | |
1232 | chan->backend.subbuf_size, | |
1233 | commit_count, commit_count_sb); | |
1234 | ||
1235 | printk(KERN_DEBUG "ring buffer: %s, cpu %d: %lu bytes committed\n", | |
1236 | chan->backend.name, cpu, commit_count); | |
1237 | } | |
1238 | ||
1239 | static | |
1240 | void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf, | |
1241 | struct channel *chan, | |
1242 | void *priv, int cpu) | |
1243 | { | |
5a8fd222 | 1244 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1245 | unsigned long write_offset, cons_offset; |
1246 | ||
f3bc08c5 MD |
1247 | /* |
1248 | * No need to order commit_count, write_offset and cons_offset reads | |
1249 | * because we execute at teardown when no more writer nor reader | |
1250 | * references are left. | |
1251 | */ | |
1252 | write_offset = v_read(config, &buf->offset); | |
1253 | cons_offset = atomic_long_read(&buf->consumed); | |
1254 | if (write_offset != cons_offset) | |
05aad775 | 1255 | printk(KERN_DEBUG |
f3bc08c5 MD |
1256 | "ring buffer %s, cpu %d: " |
1257 | "non-consumed data\n" | |
1258 | " [ %lu bytes written, %lu bytes read ]\n", | |
1259 | chan->backend.name, cpu, write_offset, cons_offset); | |
1260 | ||
1261 | for (cons_offset = atomic_long_read(&buf->consumed); | |
1262 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), | |
1263 | chan) | |
1264 | - cons_offset) > 0; | |
1265 | cons_offset = subbuf_align(cons_offset, chan)) | |
1266 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1267 | cpu); | |
1268 | } | |
1269 | ||
1270 | static | |
1271 | void lib_ring_buffer_print_errors(struct channel *chan, | |
1272 | struct lib_ring_buffer *buf, int cpu) | |
1273 | { | |
5a8fd222 | 1274 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1275 | void *priv = chan->backend.priv; |
1276 | ||
ec01ec93 MD |
1277 | if (!strcmp(chan->backend.name, "relay-metadata")) { |
1278 | printk(KERN_DEBUG "ring buffer %s: %lu records written, " | |
1279 | "%lu records overrun\n", | |
1280 | chan->backend.name, | |
1281 | v_read(config, &buf->records_count), | |
1282 | v_read(config, &buf->records_overrun)); | |
1283 | } else { | |
1284 | printk(KERN_DEBUG "ring buffer %s, cpu %d: %lu records written, " | |
1285 | "%lu records overrun\n", | |
1286 | chan->backend.name, cpu, | |
1287 | v_read(config, &buf->records_count), | |
1288 | v_read(config, &buf->records_overrun)); | |
1289 | ||
1290 | if (v_read(config, &buf->records_lost_full) | |
1291 | || v_read(config, &buf->records_lost_wrap) | |
1292 | || v_read(config, &buf->records_lost_big)) | |
1293 | printk(KERN_WARNING | |
1294 | "ring buffer %s, cpu %d: records were lost. Caused by:\n" | |
1295 | " [ %lu buffer full, %lu nest buffer wrap-around, " | |
1296 | "%lu event too big ]\n", | |
1297 | chan->backend.name, cpu, | |
1298 | v_read(config, &buf->records_lost_full), | |
1299 | v_read(config, &buf->records_lost_wrap), | |
1300 | v_read(config, &buf->records_lost_big)); | |
1301 | } | |
f3bc08c5 MD |
1302 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu); |
1303 | } | |
1304 | ||
1305 | /* | |
1306 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. | |
1307 | * | |
5bbe78ac MD |
1308 | * Only executed by SWITCH_FLUSH, which can be issued while tracing is active |
1309 | * or at buffer finalization (destroy). | |
f3bc08c5 MD |
1310 | */ |
1311 | static | |
1312 | void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf, | |
1313 | struct channel *chan, | |
1314 | struct switch_offsets *offsets, | |
1315 | u64 tsc) | |
1316 | { | |
5a8fd222 | 1317 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1318 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
1319 | unsigned long commit_count; | |
1320 | ||
1321 | config->cb.buffer_begin(buf, tsc, oldidx); | |
1322 | ||
1323 | /* | |
1324 | * Order all writes to buffer before the commit count update that will | |
1325 | * determine that the subbuffer is full. | |
1326 | */ | |
1327 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { | |
1328 | /* | |
1329 | * Must write slot data before incrementing commit count. This | |
1330 | * compiler barrier is upgraded into a smp_mb() by the IPI sent | |
1331 | * by get_subbuf(). | |
1332 | */ | |
1333 | barrier(); | |
1334 | } else | |
1335 | smp_wmb(); | |
1336 | v_add(config, config->cb.subbuffer_header_size(), | |
1337 | &buf->commit_hot[oldidx].cc); | |
1338 | commit_count = v_read(config, &buf->commit_hot[oldidx].cc); | |
1339 | /* Check if the written buffer has to be delivered */ | |
1340 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, | |
635e457c | 1341 | commit_count, oldidx, tsc); |
f3bc08c5 | 1342 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
7915e163 MD |
1343 | offsets->old + config->cb.subbuffer_header_size(), |
1344 | commit_count); | |
f3bc08c5 MD |
1345 | } |
1346 | ||
1347 | /* | |
1348 | * lib_ring_buffer_switch_old_end: switch old subbuffer | |
1349 | * | |
1350 | * Note : offset_old should never be 0 here. It is ok, because we never perform | |
1351 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller | |
1352 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty | |
1353 | * subbuffer. | |
1354 | */ | |
1355 | static | |
1356 | void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf, | |
1357 | struct channel *chan, | |
1358 | struct switch_offsets *offsets, | |
1359 | u64 tsc) | |
1360 | { | |
5a8fd222 | 1361 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1362 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
1363 | unsigned long commit_count, padding_size, data_size; | |
1364 | ||
1365 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; | |
1366 | padding_size = chan->backend.subbuf_size - data_size; | |
1367 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size); | |
1368 | ||
1369 | /* | |
1370 | * Order all writes to buffer before the commit count update that will | |
1371 | * determine that the subbuffer is full. | |
1372 | */ | |
1373 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { | |
1374 | /* | |
1375 | * Must write slot data before incrementing commit count. This | |
1376 | * compiler barrier is upgraded into a smp_mb() by the IPI sent | |
1377 | * by get_subbuf(). | |
1378 | */ | |
1379 | barrier(); | |
1380 | } else | |
1381 | smp_wmb(); | |
1382 | v_add(config, padding_size, &buf->commit_hot[oldidx].cc); | |
1383 | commit_count = v_read(config, &buf->commit_hot[oldidx].cc); | |
1384 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, | |
635e457c | 1385 | commit_count, oldidx, tsc); |
f3bc08c5 | 1386 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
7915e163 | 1387 | offsets->old + padding_size, commit_count); |
f3bc08c5 MD |
1388 | } |
1389 | ||
1390 | /* | |
1391 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. | |
1392 | * | |
1393 | * This code can be executed unordered : writers may already have written to the | |
1394 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1395 | * that this code is executed before the deliver of this sub-buffer. | |
1396 | */ | |
1397 | static | |
1398 | void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf, | |
1399 | struct channel *chan, | |
1400 | struct switch_offsets *offsets, | |
1401 | u64 tsc) | |
1402 | { | |
5a8fd222 | 1403 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1404 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
1405 | unsigned long commit_count; | |
1406 | ||
1407 | config->cb.buffer_begin(buf, tsc, beginidx); | |
1408 | ||
1409 | /* | |
1410 | * Order all writes to buffer before the commit count update that will | |
1411 | * determine that the subbuffer is full. | |
1412 | */ | |
1413 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { | |
1414 | /* | |
1415 | * Must write slot data before incrementing commit count. This | |
1416 | * compiler barrier is upgraded into a smp_mb() by the IPI sent | |
1417 | * by get_subbuf(). | |
1418 | */ | |
1419 | barrier(); | |
1420 | } else | |
1421 | smp_wmb(); | |
1422 | v_add(config, config->cb.subbuffer_header_size(), | |
1423 | &buf->commit_hot[beginidx].cc); | |
1424 | commit_count = v_read(config, &buf->commit_hot[beginidx].cc); | |
1425 | /* Check if the written buffer has to be delivered */ | |
1426 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, | |
635e457c | 1427 | commit_count, beginidx, tsc); |
f3bc08c5 | 1428 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
7915e163 MD |
1429 | offsets->begin + config->cb.subbuffer_header_size(), |
1430 | commit_count); | |
f3bc08c5 MD |
1431 | } |
1432 | ||
f5ea5800 MD |
1433 | /* |
1434 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer | |
1435 | * | |
768b05c9 MD |
1436 | * Calls subbuffer_set_data_size() to set the data size of the current |
1437 | * sub-buffer. We do not need to perform check_deliver nor commit here, | |
1438 | * since this task will be done by the "commit" of the event for which | |
1439 | * we are currently doing the space reservation. | |
f5ea5800 MD |
1440 | */ |
1441 | static | |
1442 | void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf, | |
1443 | struct channel *chan, | |
1444 | struct switch_offsets *offsets, | |
1445 | u64 tsc) | |
1446 | { | |
1447 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
768b05c9 | 1448 | unsigned long endidx, data_size; |
f5ea5800 | 1449 | |
768b05c9 | 1450 | endidx = subbuf_index(offsets->end - 1, chan); |
f5ea5800 | 1451 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; |
f5ea5800 | 1452 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size); |
f5ea5800 MD |
1453 | } |
1454 | ||
f3bc08c5 MD |
1455 | /* |
1456 | * Returns : | |
1457 | * 0 if ok | |
1458 | * !0 if execution must be aborted. | |
1459 | */ | |
1460 | static | |
1461 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, | |
1462 | struct lib_ring_buffer *buf, | |
1463 | struct channel *chan, | |
1464 | struct switch_offsets *offsets, | |
1465 | u64 *tsc) | |
1466 | { | |
5a8fd222 | 1467 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
5334a2c5 | 1468 | unsigned long off, reserve_commit_diff; |
f3bc08c5 MD |
1469 | |
1470 | offsets->begin = v_read(config, &buf->offset); | |
1471 | offsets->old = offsets->begin; | |
1472 | offsets->switch_old_start = 0; | |
1473 | off = subbuf_offset(offsets->begin, chan); | |
1474 | ||
1475 | *tsc = config->cb.ring_buffer_clock_read(chan); | |
1476 | ||
1477 | /* | |
1478 | * Ensure we flush the header of an empty subbuffer when doing the | |
1479 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the | |
1480 | * total data gathering duration even if there were no records saved | |
1481 | * after the last buffer switch. | |
1482 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. | |
1483 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of | |
1484 | * subbuffer header as appropriate. | |
1485 | * The next record that reserves space will be responsible for | |
1486 | * populating the following subbuffer header. We choose not to populate | |
1487 | * the next subbuffer header here because we want to be able to use | |
1488 | * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop | |
1489 | * buffer flush, which must guarantee that all the buffer content | |
1490 | * (records and header timestamps) are visible to the reader. This is | |
1491 | * required for quiescence guarantees for the fusion merge. | |
1492 | */ | |
5334a2c5 MD |
1493 | if (mode != SWITCH_FLUSH && !off) |
1494 | return -1; /* we do not have to switch : buffer is empty */ | |
1495 | ||
1496 | if (unlikely(off == 0)) { | |
1497 | unsigned long sb_index, commit_count; | |
1498 | ||
1499 | /* | |
5bbe78ac MD |
1500 | * We are performing a SWITCH_FLUSH. There may be concurrent |
1501 | * writes into the buffer if e.g. invoked while performing a | |
1502 | * snapshot on an active trace. | |
5334a2c5 | 1503 | * |
5bbe78ac MD |
1504 | * If the client does not save any header information (sub-buffer |
1505 | * header size == 0), don't switch empty subbuffer on finalize, | |
1506 | * because it is invalid to deliver a completely empty | |
1507 | * subbuffer. | |
5334a2c5 MD |
1508 | */ |
1509 | if (!config->cb.subbuffer_header_size()) | |
1510 | return -1; | |
1511 | ||
1512 | /* Test new buffer integrity */ | |
1513 | sb_index = subbuf_index(offsets->begin, chan); | |
1514 | commit_count = v_read(config, | |
1515 | &buf->commit_cold[sb_index].cc_sb); | |
1516 | reserve_commit_diff = | |
1517 | (buf_trunc(offsets->begin, chan) | |
1518 | >> chan->backend.num_subbuf_order) | |
1519 | - (commit_count & chan->commit_count_mask); | |
1520 | if (likely(reserve_commit_diff == 0)) { | |
1521 | /* Next subbuffer not being written to. */ | |
1522 | if (unlikely(config->mode != RING_BUFFER_OVERWRITE && | |
1523 | subbuf_trunc(offsets->begin, chan) | |
1524 | - subbuf_trunc((unsigned long) | |
1525 | atomic_long_read(&buf->consumed), chan) | |
1526 | >= chan->backend.buf_size)) { | |
1527 | /* | |
1528 | * We do not overwrite non consumed buffers | |
1529 | * and we are full : don't switch. | |
1530 | */ | |
f3bc08c5 | 1531 | return -1; |
5334a2c5 MD |
1532 | } else { |
1533 | /* | |
1534 | * Next subbuffer not being written to, and we | |
1535 | * are either in overwrite mode or the buffer is | |
1536 | * not full. It's safe to write in this new | |
1537 | * subbuffer. | |
1538 | */ | |
1539 | } | |
1540 | } else { | |
f3bc08c5 | 1541 | /* |
5334a2c5 MD |
1542 | * Next subbuffer reserve offset does not match the |
1543 | * commit offset. Don't perform switch in | |
1544 | * producer-consumer and overwrite mode. Caused by | |
1545 | * either a writer OOPS or too many nested writes over a | |
1546 | * reserve/commit pair. | |
f3bc08c5 | 1547 | */ |
5334a2c5 | 1548 | return -1; |
f3bc08c5 | 1549 | } |
5334a2c5 MD |
1550 | |
1551 | /* | |
1552 | * Need to write the subbuffer start header on finalize. | |
1553 | */ | |
1554 | offsets->switch_old_start = 1; | |
1555 | } | |
1556 | offsets->begin = subbuf_align(offsets->begin, chan); | |
f3bc08c5 MD |
1557 | /* Note: old points to the next subbuf at offset 0 */ |
1558 | offsets->end = offsets->begin; | |
1559 | return 0; | |
1560 | } | |
1561 | ||
1562 | /* | |
1563 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1564 | * called while tracing is active with absolutely no lock held. | |
1565 | * | |
1566 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1567 | * operations, this function must be called from the CPU which owns the buffer | |
1568 | * for a ACTIVE flush. | |
1569 | */ | |
1570 | void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode) | |
1571 | { | |
1572 | struct channel *chan = buf->backend.chan; | |
5a8fd222 | 1573 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1574 | struct switch_offsets offsets; |
1575 | unsigned long oldidx; | |
1576 | u64 tsc; | |
1577 | ||
1578 | offsets.size = 0; | |
1579 | ||
1580 | /* | |
1581 | * Perform retryable operations. | |
1582 | */ | |
1583 | do { | |
1584 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
1585 | &tsc)) | |
1586 | return; /* Switch not needed */ | |
1587 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1588 | != offsets.old); | |
1589 | ||
1590 | /* | |
1591 | * Atomically update last_tsc. This update races against concurrent | |
1592 | * atomic updates, but the race will always cause supplementary full TSC | |
1593 | * records, never the opposite (missing a full TSC record when it would | |
1594 | * be needed). | |
1595 | */ | |
1596 | save_last_tsc(config, buf, tsc); | |
1597 | ||
1598 | /* | |
1599 | * Push the reader if necessary | |
1600 | */ | |
1601 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1602 | ||
1603 | oldidx = subbuf_index(offsets.old, chan); | |
1604 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx); | |
1605 | ||
1606 | /* | |
1607 | * May need to populate header start on SWITCH_FLUSH. | |
1608 | */ | |
1609 | if (offsets.switch_old_start) { | |
1610 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc); | |
1611 | offsets.old += config->cb.subbuffer_header_size(); | |
1612 | } | |
1613 | ||
1614 | /* | |
1615 | * Switch old subbuffer. | |
1616 | */ | |
1617 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc); | |
1618 | } | |
1619 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow); | |
1620 | ||
9cf58910 MD |
1621 | struct switch_param { |
1622 | struct lib_ring_buffer *buf; | |
1623 | enum switch_mode mode; | |
1624 | }; | |
1625 | ||
5e391252 MD |
1626 | static void remote_switch(void *info) |
1627 | { | |
9cf58910 MD |
1628 | struct switch_param *param = info; |
1629 | struct lib_ring_buffer *buf = param->buf; | |
5e391252 | 1630 | |
9cf58910 | 1631 | lib_ring_buffer_switch_slow(buf, param->mode); |
5e391252 MD |
1632 | } |
1633 | ||
1b7801d6 MD |
1634 | static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf, |
1635 | enum switch_mode mode) | |
5e391252 MD |
1636 | { |
1637 | struct channel *chan = buf->backend.chan; | |
1638 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
1639 | int ret; | |
9cf58910 | 1640 | struct switch_param param; |
5e391252 MD |
1641 | |
1642 | /* | |
1643 | * With global synchronization we don't need to use the IPI scheme. | |
1644 | */ | |
1645 | if (config->sync == RING_BUFFER_SYNC_GLOBAL) { | |
1b7801d6 | 1646 | lib_ring_buffer_switch_slow(buf, mode); |
5e391252 MD |
1647 | return; |
1648 | } | |
1649 | ||
1650 | /* | |
1651 | * Taking lock on CPU hotplug to ensure two things: first, that the | |
1652 | * target cpu is not taken concurrently offline while we are within | |
1653 | * smp_call_function_single() (I don't trust that get_cpu() on the | |
1654 | * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be | |
1655 | * confirmed)). Secondly, if it happens that the CPU is not online, our | |
1656 | * own call to lib_ring_buffer_switch_slow() needs to be protected from | |
1657 | * CPU hotplug handlers, which can also perform a remote subbuffer | |
1658 | * switch. | |
1659 | */ | |
1660 | get_online_cpus(); | |
9cf58910 MD |
1661 | param.buf = buf; |
1662 | param.mode = mode; | |
5e391252 | 1663 | ret = smp_call_function_single(buf->backend.cpu, |
9cf58910 | 1664 | remote_switch, ¶m, 1); |
5e391252 MD |
1665 | if (ret) { |
1666 | /* Remote CPU is offline, do it ourself. */ | |
1b7801d6 | 1667 | lib_ring_buffer_switch_slow(buf, mode); |
5e391252 MD |
1668 | } |
1669 | put_online_cpus(); | |
1670 | } | |
1b7801d6 MD |
1671 | |
1672 | void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf) | |
1673 | { | |
1674 | _lib_ring_buffer_switch_remote(buf, SWITCH_ACTIVE); | |
1675 | } | |
5e391252 MD |
1676 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote); |
1677 | ||
3dc33675 MD |
1678 | /* Switch sub-buffer even if current sub-buffer is empty. */ |
1679 | void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer *buf) | |
1680 | { | |
1681 | _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH); | |
1682 | } | |
1683 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty); | |
1684 | ||
f3bc08c5 MD |
1685 | /* |
1686 | * Returns : | |
1687 | * 0 if ok | |
97ca2c54 MD |
1688 | * -ENOSPC if event size is too large for packet. |
1689 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1690 | * -EIO if data cannot be written into the buffer for any other reason. | |
f3bc08c5 MD |
1691 | */ |
1692 | static | |
1693 | int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf, | |
1694 | struct channel *chan, | |
1695 | struct switch_offsets *offsets, | |
1696 | struct lib_ring_buffer_ctx *ctx) | |
1697 | { | |
5a8fd222 | 1698 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
0fdec686 | 1699 | unsigned long reserve_commit_diff, offset_cmp; |
f3bc08c5 | 1700 | |
0fdec686 MD |
1701 | retry: |
1702 | offsets->begin = offset_cmp = v_read(config, &buf->offset); | |
f3bc08c5 MD |
1703 | offsets->old = offsets->begin; |
1704 | offsets->switch_new_start = 0; | |
f5ea5800 | 1705 | offsets->switch_new_end = 0; |
f3bc08c5 MD |
1706 | offsets->switch_old_end = 0; |
1707 | offsets->pre_header_padding = 0; | |
1708 | ||
1709 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
97ca2c54 MD |
1710 | if ((int64_t) ctx->tsc == -EIO) |
1711 | return -EIO; | |
f3bc08c5 MD |
1712 | |
1713 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
64c796d8 | 1714 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; |
f3bc08c5 MD |
1715 | |
1716 | if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { | |
1717 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1718 | } else { | |
1719 | offsets->size = config->cb.record_header_size(config, chan, | |
1720 | offsets->begin, | |
f3bc08c5 | 1721 | &offsets->pre_header_padding, |
64c796d8 | 1722 | ctx); |
f3bc08c5 MD |
1723 | offsets->size += |
1724 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1725 | ctx->largest_align) | |
1726 | + ctx->data_size; | |
1727 | if (unlikely(subbuf_offset(offsets->begin, chan) + | |
1728 | offsets->size > chan->backend.subbuf_size)) { | |
1729 | offsets->switch_old_end = 1; /* For offsets->old */ | |
1730 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1731 | } | |
1732 | } | |
1733 | if (unlikely(offsets->switch_new_start)) { | |
0fdec686 | 1734 | unsigned long sb_index, commit_count; |
f3bc08c5 MD |
1735 | |
1736 | /* | |
1737 | * We are typically not filling the previous buffer completely. | |
1738 | */ | |
1739 | if (likely(offsets->switch_old_end)) | |
1740 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1741 | offsets->begin = offsets->begin | |
1742 | + config->cb.subbuffer_header_size(); | |
1743 | /* Test new buffer integrity */ | |
1744 | sb_index = subbuf_index(offsets->begin, chan); | |
0fdec686 MD |
1745 | /* |
1746 | * Read buf->offset before buf->commit_cold[sb_index].cc_sb. | |
1747 | * lib_ring_buffer_check_deliver() has the matching | |
1748 | * memory barriers required around commit_cold cc_sb | |
1749 | * updates to ensure reserve and commit counter updates | |
1750 | * are not seen reordered when updated by another CPU. | |
1751 | */ | |
1752 | smp_rmb(); | |
1753 | commit_count = v_read(config, | |
1754 | &buf->commit_cold[sb_index].cc_sb); | |
1755 | /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */ | |
1756 | smp_rmb(); | |
1757 | if (unlikely(offset_cmp != v_read(config, &buf->offset))) { | |
1758 | /* | |
1759 | * The reserve counter have been concurrently updated | |
1760 | * while we read the commit counter. This means the | |
1761 | * commit counter we read might not match buf->offset | |
1762 | * due to concurrent update. We therefore need to retry. | |
1763 | */ | |
1764 | goto retry; | |
1765 | } | |
f3bc08c5 MD |
1766 | reserve_commit_diff = |
1767 | (buf_trunc(offsets->begin, chan) | |
1768 | >> chan->backend.num_subbuf_order) | |
0fdec686 | 1769 | - (commit_count & chan->commit_count_mask); |
f3bc08c5 MD |
1770 | if (likely(reserve_commit_diff == 0)) { |
1771 | /* Next subbuffer not being written to. */ | |
1772 | if (unlikely(config->mode != RING_BUFFER_OVERWRITE && | |
1773 | subbuf_trunc(offsets->begin, chan) | |
1774 | - subbuf_trunc((unsigned long) | |
1775 | atomic_long_read(&buf->consumed), chan) | |
1776 | >= chan->backend.buf_size)) { | |
1777 | /* | |
1778 | * We do not overwrite non consumed buffers | |
1779 | * and we are full : record is lost. | |
1780 | */ | |
1781 | v_inc(config, &buf->records_lost_full); | |
97ca2c54 | 1782 | return -ENOBUFS; |
f3bc08c5 MD |
1783 | } else { |
1784 | /* | |
1785 | * Next subbuffer not being written to, and we | |
1786 | * are either in overwrite mode or the buffer is | |
1787 | * not full. It's safe to write in this new | |
1788 | * subbuffer. | |
1789 | */ | |
1790 | } | |
1791 | } else { | |
1792 | /* | |
1793 | * Next subbuffer reserve offset does not match the | |
0fdec686 MD |
1794 | * commit offset, and this did not involve update to the |
1795 | * reserve counter. Drop record in producer-consumer and | |
1796 | * overwrite mode. Caused by either a writer OOPS or | |
1797 | * too many nested writes over a reserve/commit pair. | |
f3bc08c5 MD |
1798 | */ |
1799 | v_inc(config, &buf->records_lost_wrap); | |
97ca2c54 | 1800 | return -EIO; |
f3bc08c5 MD |
1801 | } |
1802 | offsets->size = | |
1803 | config->cb.record_header_size(config, chan, | |
1804 | offsets->begin, | |
f3bc08c5 | 1805 | &offsets->pre_header_padding, |
64c796d8 | 1806 | ctx); |
f3bc08c5 MD |
1807 | offsets->size += |
1808 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1809 | ctx->largest_align) | |
1810 | + ctx->data_size; | |
1811 | if (unlikely(subbuf_offset(offsets->begin, chan) | |
1812 | + offsets->size > chan->backend.subbuf_size)) { | |
1813 | /* | |
1814 | * Record too big for subbuffers, report error, don't | |
1815 | * complete the sub-buffer switch. | |
1816 | */ | |
1817 | v_inc(config, &buf->records_lost_big); | |
97ca2c54 | 1818 | return -ENOSPC; |
f3bc08c5 MD |
1819 | } else { |
1820 | /* | |
1821 | * We just made a successful buffer switch and the | |
1822 | * record fits in the new subbuffer. Let's write. | |
1823 | */ | |
1824 | } | |
1825 | } else { | |
1826 | /* | |
1827 | * Record fits in the current buffer and we are not on a switch | |
1828 | * boundary. It's safe to write. | |
1829 | */ | |
1830 | } | |
1831 | offsets->end = offsets->begin + offsets->size; | |
f5ea5800 MD |
1832 | |
1833 | if (unlikely(subbuf_offset(offsets->end, chan) == 0)) { | |
1834 | /* | |
1835 | * The offset_end will fall at the very beginning of the next | |
1836 | * subbuffer. | |
1837 | */ | |
1838 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
1839 | } | |
f3bc08c5 MD |
1840 | return 0; |
1841 | } | |
1842 | ||
1843 | /** | |
1844 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
1845 | * @ctx: ring buffer context. | |
1846 | * | |
97ca2c54 MD |
1847 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, |
1848 | * -EIO for other errors, else returns 0. | |
f3bc08c5 MD |
1849 | * It will take care of sub-buffer switching. |
1850 | */ | |
1851 | int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx) | |
1852 | { | |
1853 | struct channel *chan = ctx->chan; | |
5a8fd222 | 1854 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
1855 | struct lib_ring_buffer *buf; |
1856 | struct switch_offsets offsets; | |
c099397a | 1857 | int ret; |
f3bc08c5 MD |
1858 | |
1859 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1860 | buf = per_cpu_ptr(chan->backend.buf, ctx->cpu); | |
1861 | else | |
1862 | buf = chan->backend.buf; | |
1863 | ctx->buf = buf; | |
1864 | ||
1865 | offsets.size = 0; | |
1866 | ||
1867 | do { | |
97ca2c54 MD |
1868 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, |
1869 | ctx); | |
1870 | if (unlikely(ret)) | |
1871 | return ret; | |
f3bc08c5 MD |
1872 | } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
1873 | offsets.end) | |
1874 | != offsets.old)); | |
1875 | ||
1876 | /* | |
1877 | * Atomically update last_tsc. This update races against concurrent | |
1878 | * atomic updates, but the race will always cause supplementary full TSC | |
1879 | * records, never the opposite (missing a full TSC record when it would | |
1880 | * be needed). | |
1881 | */ | |
1882 | save_last_tsc(config, buf, ctx->tsc); | |
1883 | ||
1884 | /* | |
1885 | * Push the reader if necessary | |
1886 | */ | |
1887 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
1888 | ||
1889 | /* | |
1890 | * Clear noref flag for this subbuffer. | |
1891 | */ | |
1892 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1893 | subbuf_index(offsets.end - 1, chan)); | |
1894 | ||
1895 | /* | |
1896 | * Switch old subbuffer if needed. | |
1897 | */ | |
1898 | if (unlikely(offsets.switch_old_end)) { | |
1899 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1900 | subbuf_index(offsets.old - 1, chan)); | |
1901 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc); | |
1902 | } | |
1903 | ||
1904 | /* | |
1905 | * Populate new subbuffer. | |
1906 | */ | |
1907 | if (unlikely(offsets.switch_new_start)) | |
1908 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc); | |
1909 | ||
f5ea5800 MD |
1910 | if (unlikely(offsets.switch_new_end)) |
1911 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc); | |
1912 | ||
f3bc08c5 MD |
1913 | ctx->slot_size = offsets.size; |
1914 | ctx->pre_offset = offsets.begin; | |
1915 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
1916 | return 0; | |
1917 | } | |
1918 | EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow); | |
6fb8de4b | 1919 | |
02a766bb | 1920 | int __init init_lib_ring_buffer_frontend(void) |
6fb8de4b MD |
1921 | { |
1922 | int cpu; | |
1923 | ||
1924 | for_each_possible_cpu(cpu) | |
1925 | spin_lock_init(&per_cpu(ring_buffer_nohz_lock, cpu)); | |
02a766bb | 1926 | return 0; |
6fb8de4b | 1927 | } |
02a766bb MD |
1928 | |
1929 | module_init(init_lib_ring_buffer_frontend); | |
1a5db82d MD |
1930 | |
1931 | void __exit exit_lib_ring_buffer_frontend(void) | |
1932 | { | |
1933 | } | |
1934 | ||
1935 | module_exit(exit_lib_ring_buffer_frontend); |