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