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