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