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