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