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