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