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