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