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