Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | #ifndef _LINUX_RING_BUFFER_FRONTEND_API_H |
2 | #define _LINUX_RING_BUFFER_FRONTEND_API_H | |
3 | ||
4 | /* | |
5 | * linux/ringbuffer/frontend_api.h | |
6 | * | |
7 | * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
9 | * Ring Buffer Library Synchronization Header (buffer write API). | |
10 | * | |
11 | * Author: | |
12 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
13 | * | |
14 | * See ring_buffer_frontend.c for more information on wait-free algorithms. | |
15 | * See linux/ringbuffer/frontend.h for channel allocation and read-side API. | |
16 | * | |
17 | * Dual LGPL v2.1/GPL v2 license. | |
18 | */ | |
19 | ||
4931a13e | 20 | #include "frontend.h" |
4318ae1b | 21 | #include "lttng/core.h" |
9f3fdbc6 MD |
22 | #include <urcu-bp.h> |
23 | #include <urcu/compiler.h> | |
852c2936 MD |
24 | |
25 | /** | |
26 | * lib_ring_buffer_get_cpu - Precedes ring buffer reserve/commit. | |
27 | * | |
9f3fdbc6 MD |
28 | * Grabs RCU read-side lock and keeps a ring buffer nesting count as |
29 | * supplementary safety net to ensure tracer client code will never | |
30 | * trigger an endless recursion. Returns the processor ID on success, | |
31 | * -EPERM on failure (nesting count too high). | |
852c2936 MD |
32 | * |
33 | * asm volatile and "memory" clobber prevent the compiler from moving | |
34 | * instructions out of the ring buffer nesting count. This is required to ensure | |
35 | * that probe side-effects which can cause recursion (e.g. unforeseen traps, | |
36 | * divisions by 0, ...) are triggered within the incremented nesting count | |
37 | * section. | |
38 | */ | |
39 | static inline | |
4cfec15c | 40 | int lib_ring_buffer_get_cpu(const struct lttng_ust_lib_ring_buffer_config *config) |
852c2936 MD |
41 | { |
42 | int cpu, nesting; | |
43 | ||
9f3fdbc6 | 44 | rcu_read_lock(); |
2b2d6ff7 | 45 | cpu = lttng_ust_get_cpu(); |
b9b858b8 | 46 | nesting = ++lib_ring_buffer_nesting; /* TLS */ |
9f3fdbc6 | 47 | cmm_barrier(); |
852c2936 | 48 | |
b5a3dfa5 | 49 | if (caa_unlikely(nesting > 4)) { |
852c2936 | 50 | WARN_ON_ONCE(1); |
b9b858b8 | 51 | lib_ring_buffer_nesting--; /* TLS */ |
9f3fdbc6 | 52 | rcu_read_unlock(); |
852c2936 MD |
53 | return -EPERM; |
54 | } else | |
55 | return cpu; | |
56 | } | |
57 | ||
58 | /** | |
59 | * lib_ring_buffer_put_cpu - Follows ring buffer reserve/commit. | |
60 | */ | |
61 | static inline | |
4cfec15c | 62 | void lib_ring_buffer_put_cpu(const struct lttng_ust_lib_ring_buffer_config *config) |
852c2936 | 63 | { |
9f3fdbc6 | 64 | cmm_barrier(); |
b9b858b8 | 65 | lib_ring_buffer_nesting--; /* TLS */ |
9f3fdbc6 | 66 | rcu_read_unlock(); |
852c2936 MD |
67 | } |
68 | ||
69 | /* | |
70 | * lib_ring_buffer_try_reserve is called by lib_ring_buffer_reserve(). It is not | |
71 | * part of the API per se. | |
72 | * | |
73 | * returns 0 if reserve ok, or 1 if the slow path must be taken. | |
74 | */ | |
75 | static inline | |
4cfec15c MD |
76 | int lib_ring_buffer_try_reserve(const struct lttng_ust_lib_ring_buffer_config *config, |
77 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
852c2936 MD |
78 | unsigned long *o_begin, unsigned long *o_end, |
79 | unsigned long *o_old, size_t *before_hdr_pad) | |
80 | { | |
81 | struct channel *chan = ctx->chan; | |
4cfec15c | 82 | struct lttng_ust_lib_ring_buffer *buf = ctx->buf; |
852c2936 MD |
83 | *o_begin = v_read(config, &buf->offset); |
84 | *o_old = *o_begin; | |
85 | ||
86 | ctx->tsc = lib_ring_buffer_clock_read(chan); | |
87 | if ((int64_t) ctx->tsc == -EIO) | |
88 | return 1; | |
89 | ||
90 | /* | |
91 | * Prefetch cacheline for read because we have to read the previous | |
92 | * commit counter to increment it and commit seq value to compare it to | |
93 | * the commit counter. | |
94 | */ | |
9f3fdbc6 | 95 | //prefetch(&buf->commit_hot[subbuf_index(*o_begin, chan)]); |
852c2936 MD |
96 | |
97 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
98 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
99 | ||
b5a3dfa5 | 100 | if (caa_unlikely(subbuf_offset(*o_begin, chan) == 0)) |
852c2936 MD |
101 | return 1; |
102 | ||
103 | ctx->slot_size = record_header_size(config, chan, *o_begin, | |
104 | before_hdr_pad, ctx); | |
105 | ctx->slot_size += | |
106 | lib_ring_buffer_align(*o_begin + ctx->slot_size, | |
107 | ctx->largest_align) + ctx->data_size; | |
b5a3dfa5 | 108 | if (caa_unlikely((subbuf_offset(*o_begin, chan) + ctx->slot_size) |
852c2936 MD |
109 | > chan->backend.subbuf_size)) |
110 | return 1; | |
111 | ||
112 | /* | |
113 | * Record fits in the current buffer and we are not on a switch | |
114 | * boundary. It's safe to write. | |
115 | */ | |
116 | *o_end = *o_begin + ctx->slot_size; | |
117 | ||
b5a3dfa5 | 118 | if (caa_unlikely((subbuf_offset(*o_end, chan)) == 0)) |
852c2936 MD |
119 | /* |
120 | * The offset_end will fall at the very beginning of the next | |
121 | * subbuffer. | |
122 | */ | |
123 | return 1; | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | /** | |
129 | * lib_ring_buffer_reserve - Reserve space in a ring buffer. | |
130 | * @config: ring buffer instance configuration. | |
131 | * @ctx: ring buffer context. (input and output) Must be already initialized. | |
132 | * | |
133 | * Atomic wait-free slot reservation. The reserved space starts at the context | |
134 | * "pre_offset". Its length is "slot_size". The associated time-stamp is "tsc". | |
135 | * | |
136 | * Return : | |
137 | * 0 on success. | |
138 | * -EAGAIN if channel is disabled. | |
139 | * -ENOSPC if event size is too large for packet. | |
140 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
141 | * -EIO if data cannot be written into the buffer for any other reason. | |
142 | */ | |
143 | ||
144 | static inline | |
4cfec15c MD |
145 | int lib_ring_buffer_reserve(const struct lttng_ust_lib_ring_buffer_config *config, |
146 | struct lttng_ust_lib_ring_buffer_ctx *ctx) | |
852c2936 MD |
147 | { |
148 | struct channel *chan = ctx->chan; | |
38fae1d3 | 149 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c | 150 | struct lttng_ust_lib_ring_buffer *buf; |
852c2936 MD |
151 | unsigned long o_begin, o_end, o_old; |
152 | size_t before_hdr_pad = 0; | |
153 | ||
9f3fdbc6 | 154 | if (uatomic_read(&chan->record_disabled)) |
852c2936 MD |
155 | return -EAGAIN; |
156 | ||
157 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 158 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 159 | else |
1d498196 | 160 | buf = shmp(handle, chan->backend.buf[0].shmp); |
9f3fdbc6 | 161 | if (uatomic_read(&buf->record_disabled)) |
852c2936 MD |
162 | return -EAGAIN; |
163 | ctx->buf = buf; | |
164 | ||
165 | /* | |
166 | * Perform retryable operations. | |
167 | */ | |
b5a3dfa5 | 168 | if (caa_unlikely(lib_ring_buffer_try_reserve(config, ctx, &o_begin, |
852c2936 MD |
169 | &o_end, &o_old, &before_hdr_pad))) |
170 | goto slow_path; | |
171 | ||
b5a3dfa5 | 172 | if (caa_unlikely(v_cmpxchg(config, &ctx->buf->offset, o_old, o_end) |
852c2936 MD |
173 | != o_old)) |
174 | goto slow_path; | |
175 | ||
176 | /* | |
177 | * Atomically update last_tsc. This update races against concurrent | |
178 | * atomic updates, but the race will always cause supplementary full TSC | |
179 | * record headers, never the opposite (missing a full TSC record header | |
180 | * when it would be needed). | |
181 | */ | |
182 | save_last_tsc(config, ctx->buf, ctx->tsc); | |
183 | ||
184 | /* | |
185 | * Push the reader if necessary | |
186 | */ | |
187 | lib_ring_buffer_reserve_push_reader(ctx->buf, chan, o_end - 1); | |
188 | ||
189 | /* | |
190 | * Clear noref flag for this subbuffer. | |
191 | */ | |
192 | lib_ring_buffer_clear_noref(config, &ctx->buf->backend, | |
1d498196 | 193 | subbuf_index(o_end - 1, chan), handle); |
852c2936 MD |
194 | |
195 | ctx->pre_offset = o_begin; | |
196 | ctx->buf_offset = o_begin + before_hdr_pad; | |
197 | return 0; | |
198 | slow_path: | |
199 | return lib_ring_buffer_reserve_slow(ctx); | |
200 | } | |
201 | ||
202 | /** | |
203 | * lib_ring_buffer_switch - Perform a sub-buffer switch for a per-cpu buffer. | |
204 | * @config: ring buffer instance configuration. | |
205 | * @buf: buffer | |
206 | * @mode: buffer switch mode (SWITCH_ACTIVE or SWITCH_FLUSH) | |
207 | * | |
208 | * This operation is completely reentrant : can be called while tracing is | |
209 | * active with absolutely no lock held. | |
210 | * | |
211 | * Note, however, that as a v_cmpxchg is used for some atomic operations and | |
212 | * requires to be executed locally for per-CPU buffers, this function must be | |
213 | * called from the CPU which owns the buffer for a ACTIVE flush, with preemption | |
214 | * disabled, for RING_BUFFER_SYNC_PER_CPU configuration. | |
215 | */ | |
216 | static inline | |
4cfec15c MD |
217 | void lib_ring_buffer_switch(const struct lttng_ust_lib_ring_buffer_config *config, |
218 | struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, | |
38fae1d3 | 219 | struct lttng_ust_shm_handle *handle) |
852c2936 | 220 | { |
1d498196 | 221 | lib_ring_buffer_switch_slow(buf, mode, handle); |
852c2936 MD |
222 | } |
223 | ||
224 | /* See ring_buffer_frontend_api.h for lib_ring_buffer_reserve(). */ | |
225 | ||
226 | /** | |
227 | * lib_ring_buffer_commit - Commit an record. | |
228 | * @config: ring buffer instance configuration. | |
229 | * @ctx: ring buffer context. (input arguments only) | |
230 | * | |
231 | * Atomic unordered slot commit. Increments the commit count in the | |
232 | * specified sub-buffer, and delivers it if necessary. | |
233 | */ | |
234 | static inline | |
4cfec15c MD |
235 | void lib_ring_buffer_commit(const struct lttng_ust_lib_ring_buffer_config *config, |
236 | const struct lttng_ust_lib_ring_buffer_ctx *ctx) | |
852c2936 MD |
237 | { |
238 | struct channel *chan = ctx->chan; | |
38fae1d3 | 239 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c | 240 | struct lttng_ust_lib_ring_buffer *buf = ctx->buf; |
852c2936 MD |
241 | unsigned long offset_end = ctx->buf_offset; |
242 | unsigned long endidx = subbuf_index(offset_end - 1, chan); | |
243 | unsigned long commit_count; | |
244 | ||
245 | /* | |
246 | * Must count record before incrementing the commit count. | |
247 | */ | |
1d498196 | 248 | subbuffer_count_record(config, &buf->backend, endidx, handle); |
852c2936 MD |
249 | |
250 | /* | |
251 | * Order all writes to buffer before the commit count update that will | |
252 | * determine that the subbuffer is full. | |
253 | */ | |
9f3fdbc6 | 254 | cmm_smp_wmb(); |
852c2936 | 255 | |
4746ae29 | 256 | v_add(config, ctx->slot_size, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
852c2936 MD |
257 | |
258 | /* | |
259 | * commit count read can race with concurrent OOO commit count updates. | |
260 | * This is only needed for lib_ring_buffer_check_deliver (for | |
261 | * non-polling delivery only) and for | |
262 | * lib_ring_buffer_write_commit_counter. The race can only cause the | |
263 | * counter to be read with the same value more than once, which could | |
264 | * cause : | |
265 | * - Multiple delivery for the same sub-buffer (which is handled | |
266 | * gracefully by the reader code) if the value is for a full | |
267 | * sub-buffer. It's important that we can never miss a sub-buffer | |
268 | * delivery. Re-reading the value after the v_add ensures this. | |
269 | * - Reading a commit_count with a higher value that what was actually | |
270 | * added to it for the lib_ring_buffer_write_commit_counter call | |
271 | * (again caused by a concurrent committer). It does not matter, | |
272 | * because this function is interested in the fact that the commit | |
273 | * count reaches back the reserve offset for a specific sub-buffer, | |
274 | * which is completely independent of the order. | |
275 | */ | |
4746ae29 | 276 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
852c2936 MD |
277 | |
278 | lib_ring_buffer_check_deliver(config, buf, chan, offset_end - 1, | |
1d498196 | 279 | commit_count, endidx, handle); |
852c2936 MD |
280 | /* |
281 | * Update used size at each commit. It's needed only for extracting | |
282 | * ring_buffer buffers from vmcore, after crash. | |
283 | */ | |
284 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, | |
285 | ctx->buf_offset, commit_count, | |
1d498196 | 286 | ctx->slot_size, handle); |
852c2936 MD |
287 | } |
288 | ||
289 | /** | |
290 | * lib_ring_buffer_try_discard_reserve - Try discarding a record. | |
291 | * @config: ring buffer instance configuration. | |
292 | * @ctx: ring buffer context. (input arguments only) | |
293 | * | |
294 | * Only succeeds if no other record has been written after the record to | |
295 | * discard. If discard fails, the record must be committed to the buffer. | |
296 | * | |
297 | * Returns 0 upon success, -EPERM if the record cannot be discarded. | |
298 | */ | |
299 | static inline | |
4cfec15c MD |
300 | int lib_ring_buffer_try_discard_reserve(const struct lttng_ust_lib_ring_buffer_config *config, |
301 | const struct lttng_ust_lib_ring_buffer_ctx *ctx) | |
852c2936 | 302 | { |
4cfec15c | 303 | struct lttng_ust_lib_ring_buffer *buf = ctx->buf; |
852c2936 MD |
304 | unsigned long end_offset = ctx->pre_offset + ctx->slot_size; |
305 | ||
306 | /* | |
307 | * We need to ensure that if the cmpxchg succeeds and discards the | |
308 | * record, the next record will record a full TSC, because it cannot | |
309 | * rely on the last_tsc associated with the discarded record to detect | |
310 | * overflows. The only way to ensure this is to set the last_tsc to 0 | |
311 | * (assuming no 64-bit TSC overflow), which forces to write a 64-bit | |
312 | * timestamp in the next record. | |
313 | * | |
314 | * Note: if discard fails, we must leave the TSC in the record header. | |
315 | * It is needed to keep track of TSC overflows for the following | |
316 | * records. | |
317 | */ | |
318 | save_last_tsc(config, buf, 0ULL); | |
319 | ||
b5a3dfa5 | 320 | if (caa_likely(v_cmpxchg(config, &buf->offset, end_offset, ctx->pre_offset) |
852c2936 MD |
321 | != end_offset)) |
322 | return -EPERM; | |
323 | else | |
324 | return 0; | |
325 | } | |
326 | ||
327 | static inline | |
4cfec15c | 328 | void channel_record_disable(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
329 | struct channel *chan) |
330 | { | |
9f3fdbc6 | 331 | uatomic_inc(&chan->record_disabled); |
852c2936 MD |
332 | } |
333 | ||
334 | static inline | |
4cfec15c | 335 | void channel_record_enable(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
336 | struct channel *chan) |
337 | { | |
9f3fdbc6 | 338 | uatomic_dec(&chan->record_disabled); |
852c2936 MD |
339 | } |
340 | ||
341 | static inline | |
4cfec15c MD |
342 | void lib_ring_buffer_record_disable(const struct lttng_ust_lib_ring_buffer_config *config, |
343 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 344 | { |
9f3fdbc6 | 345 | uatomic_inc(&buf->record_disabled); |
852c2936 MD |
346 | } |
347 | ||
348 | static inline | |
4cfec15c MD |
349 | void lib_ring_buffer_record_enable(const struct lttng_ust_lib_ring_buffer_config *config, |
350 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 351 | { |
9f3fdbc6 | 352 | uatomic_dec(&buf->record_disabled); |
852c2936 MD |
353 | } |
354 | ||
355 | #endif /* _LINUX_RING_BUFFER_FRONTEND_API_H */ |