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" |
9f3fdbc6 MD |
21 | #include "ust/core.h" |
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 | |
40 | int lib_ring_buffer_get_cpu(const struct lib_ring_buffer_config *config) | |
41 | { | |
42 | int cpu, nesting; | |
43 | ||
9f3fdbc6 MD |
44 | rcu_read_lock(); |
45 | cpu = ust_get_cpu(); | |
b9b858b8 | 46 | nesting = ++lib_ring_buffer_nesting; /* TLS */ |
9f3fdbc6 | 47 | cmm_barrier(); |
852c2936 MD |
48 | |
49 | if (unlikely(nesting > 4)) { | |
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 | |
62 | void lib_ring_buffer_put_cpu(const struct lib_ring_buffer_config *config) | |
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 | |
76 | int lib_ring_buffer_try_reserve(const struct lib_ring_buffer_config *config, | |
77 | struct lib_ring_buffer_ctx *ctx, | |
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; | |
82 | struct lib_ring_buffer *buf = ctx->buf; | |
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 | ||
100 | if (unlikely(subbuf_offset(*o_begin, chan) == 0)) | |
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; | |
108 | if (unlikely((subbuf_offset(*o_begin, chan) + ctx->slot_size) | |
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 | ||
118 | if (unlikely((subbuf_offset(*o_end, chan)) == 0)) | |
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 | |
145 | int lib_ring_buffer_reserve(const struct lib_ring_buffer_config *config, | |
146 | struct lib_ring_buffer_ctx *ctx) | |
147 | { | |
148 | struct channel *chan = ctx->chan; | |
149 | struct lib_ring_buffer *buf; | |
150 | unsigned long o_begin, o_end, o_old; | |
151 | size_t before_hdr_pad = 0; | |
152 | ||
9f3fdbc6 | 153 | if (uatomic_read(&chan->record_disabled)) |
852c2936 MD |
154 | return -EAGAIN; |
155 | ||
156 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
9f3fdbc6 | 157 | buf = &shmp(chan->backend.buf)[ctx->cpu]; |
852c2936 | 158 | else |
9f3fdbc6 MD |
159 | buf = shmp(chan->backend.buf); |
160 | if (uatomic_read(&buf->record_disabled)) | |
852c2936 MD |
161 | return -EAGAIN; |
162 | ctx->buf = buf; | |
163 | ||
164 | /* | |
165 | * Perform retryable operations. | |
166 | */ | |
167 | if (unlikely(lib_ring_buffer_try_reserve(config, ctx, &o_begin, | |
168 | &o_end, &o_old, &before_hdr_pad))) | |
169 | goto slow_path; | |
170 | ||
171 | if (unlikely(v_cmpxchg(config, &ctx->buf->offset, o_old, o_end) | |
172 | != o_old)) | |
173 | goto slow_path; | |
174 | ||
175 | /* | |
176 | * Atomically update last_tsc. This update races against concurrent | |
177 | * atomic updates, but the race will always cause supplementary full TSC | |
178 | * record headers, never the opposite (missing a full TSC record header | |
179 | * when it would be needed). | |
180 | */ | |
181 | save_last_tsc(config, ctx->buf, ctx->tsc); | |
182 | ||
183 | /* | |
184 | * Push the reader if necessary | |
185 | */ | |
186 | lib_ring_buffer_reserve_push_reader(ctx->buf, chan, o_end - 1); | |
187 | ||
188 | /* | |
189 | * Clear noref flag for this subbuffer. | |
190 | */ | |
191 | lib_ring_buffer_clear_noref(config, &ctx->buf->backend, | |
192 | subbuf_index(o_end - 1, chan)); | |
193 | ||
194 | ctx->pre_offset = o_begin; | |
195 | ctx->buf_offset = o_begin + before_hdr_pad; | |
196 | return 0; | |
197 | slow_path: | |
198 | return lib_ring_buffer_reserve_slow(ctx); | |
199 | } | |
200 | ||
201 | /** | |
202 | * lib_ring_buffer_switch - Perform a sub-buffer switch for a per-cpu buffer. | |
203 | * @config: ring buffer instance configuration. | |
204 | * @buf: buffer | |
205 | * @mode: buffer switch mode (SWITCH_ACTIVE or SWITCH_FLUSH) | |
206 | * | |
207 | * This operation is completely reentrant : can be called while tracing is | |
208 | * active with absolutely no lock held. | |
209 | * | |
210 | * Note, however, that as a v_cmpxchg is used for some atomic operations and | |
211 | * requires to be executed locally for per-CPU buffers, this function must be | |
212 | * called from the CPU which owns the buffer for a ACTIVE flush, with preemption | |
213 | * disabled, for RING_BUFFER_SYNC_PER_CPU configuration. | |
214 | */ | |
215 | static inline | |
216 | void lib_ring_buffer_switch(const struct lib_ring_buffer_config *config, | |
217 | struct lib_ring_buffer *buf, enum switch_mode mode) | |
218 | { | |
219 | lib_ring_buffer_switch_slow(buf, mode); | |
220 | } | |
221 | ||
222 | /* See ring_buffer_frontend_api.h for lib_ring_buffer_reserve(). */ | |
223 | ||
224 | /** | |
225 | * lib_ring_buffer_commit - Commit an record. | |
226 | * @config: ring buffer instance configuration. | |
227 | * @ctx: ring buffer context. (input arguments only) | |
228 | * | |
229 | * Atomic unordered slot commit. Increments the commit count in the | |
230 | * specified sub-buffer, and delivers it if necessary. | |
231 | */ | |
232 | static inline | |
233 | void lib_ring_buffer_commit(const struct lib_ring_buffer_config *config, | |
234 | const struct lib_ring_buffer_ctx *ctx) | |
235 | { | |
236 | struct channel *chan = ctx->chan; | |
237 | struct lib_ring_buffer *buf = ctx->buf; | |
238 | unsigned long offset_end = ctx->buf_offset; | |
239 | unsigned long endidx = subbuf_index(offset_end - 1, chan); | |
240 | unsigned long commit_count; | |
241 | ||
242 | /* | |
243 | * Must count record before incrementing the commit count. | |
244 | */ | |
245 | subbuffer_count_record(config, &buf->backend, endidx); | |
246 | ||
247 | /* | |
248 | * Order all writes to buffer before the commit count update that will | |
249 | * determine that the subbuffer is full. | |
250 | */ | |
9f3fdbc6 | 251 | cmm_smp_wmb(); |
852c2936 | 252 | |
9f3fdbc6 | 253 | v_add(config, ctx->slot_size, &shmp(buf->commit_hot)[endidx].cc); |
852c2936 MD |
254 | |
255 | /* | |
256 | * commit count read can race with concurrent OOO commit count updates. | |
257 | * This is only needed for lib_ring_buffer_check_deliver (for | |
258 | * non-polling delivery only) and for | |
259 | * lib_ring_buffer_write_commit_counter. The race can only cause the | |
260 | * counter to be read with the same value more than once, which could | |
261 | * cause : | |
262 | * - Multiple delivery for the same sub-buffer (which is handled | |
263 | * gracefully by the reader code) if the value is for a full | |
264 | * sub-buffer. It's important that we can never miss a sub-buffer | |
265 | * delivery. Re-reading the value after the v_add ensures this. | |
266 | * - Reading a commit_count with a higher value that what was actually | |
267 | * added to it for the lib_ring_buffer_write_commit_counter call | |
268 | * (again caused by a concurrent committer). It does not matter, | |
269 | * because this function is interested in the fact that the commit | |
270 | * count reaches back the reserve offset for a specific sub-buffer, | |
271 | * which is completely independent of the order. | |
272 | */ | |
9f3fdbc6 | 273 | commit_count = v_read(config, &shmp(buf->commit_hot)[endidx].cc); |
852c2936 MD |
274 | |
275 | lib_ring_buffer_check_deliver(config, buf, chan, offset_end - 1, | |
276 | commit_count, endidx); | |
277 | /* | |
278 | * Update used size at each commit. It's needed only for extracting | |
279 | * ring_buffer buffers from vmcore, after crash. | |
280 | */ | |
281 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, | |
282 | ctx->buf_offset, commit_count, | |
283 | ctx->slot_size); | |
284 | } | |
285 | ||
286 | /** | |
287 | * lib_ring_buffer_try_discard_reserve - Try discarding a record. | |
288 | * @config: ring buffer instance configuration. | |
289 | * @ctx: ring buffer context. (input arguments only) | |
290 | * | |
291 | * Only succeeds if no other record has been written after the record to | |
292 | * discard. If discard fails, the record must be committed to the buffer. | |
293 | * | |
294 | * Returns 0 upon success, -EPERM if the record cannot be discarded. | |
295 | */ | |
296 | static inline | |
297 | int lib_ring_buffer_try_discard_reserve(const struct lib_ring_buffer_config *config, | |
298 | const struct lib_ring_buffer_ctx *ctx) | |
299 | { | |
300 | struct lib_ring_buffer *buf = ctx->buf; | |
301 | unsigned long end_offset = ctx->pre_offset + ctx->slot_size; | |
302 | ||
303 | /* | |
304 | * We need to ensure that if the cmpxchg succeeds and discards the | |
305 | * record, the next record will record a full TSC, because it cannot | |
306 | * rely on the last_tsc associated with the discarded record to detect | |
307 | * overflows. The only way to ensure this is to set the last_tsc to 0 | |
308 | * (assuming no 64-bit TSC overflow), which forces to write a 64-bit | |
309 | * timestamp in the next record. | |
310 | * | |
311 | * Note: if discard fails, we must leave the TSC in the record header. | |
312 | * It is needed to keep track of TSC overflows for the following | |
313 | * records. | |
314 | */ | |
315 | save_last_tsc(config, buf, 0ULL); | |
316 | ||
317 | if (likely(v_cmpxchg(config, &buf->offset, end_offset, ctx->pre_offset) | |
318 | != end_offset)) | |
319 | return -EPERM; | |
320 | else | |
321 | return 0; | |
322 | } | |
323 | ||
324 | static inline | |
325 | void channel_record_disable(const struct lib_ring_buffer_config *config, | |
326 | struct channel *chan) | |
327 | { | |
9f3fdbc6 | 328 | uatomic_inc(&chan->record_disabled); |
852c2936 MD |
329 | } |
330 | ||
331 | static inline | |
332 | void channel_record_enable(const struct lib_ring_buffer_config *config, | |
333 | struct channel *chan) | |
334 | { | |
9f3fdbc6 | 335 | uatomic_dec(&chan->record_disabled); |
852c2936 MD |
336 | } |
337 | ||
338 | static inline | |
339 | void lib_ring_buffer_record_disable(const struct lib_ring_buffer_config *config, | |
340 | struct lib_ring_buffer *buf) | |
341 | { | |
9f3fdbc6 | 342 | uatomic_inc(&buf->record_disabled); |
852c2936 MD |
343 | } |
344 | ||
345 | static inline | |
346 | void lib_ring_buffer_record_enable(const struct lib_ring_buffer_config *config, | |
347 | struct lib_ring_buffer *buf) | |
348 | { | |
9f3fdbc6 | 349 | uatomic_dec(&buf->record_disabled); |
852c2936 MD |
350 | } |
351 | ||
352 | #endif /* _LINUX_RING_BUFFER_FRONTEND_API_H */ |