20ca39a5b8fe6a4056d7764248c39bfb811cce8c
[lttng-modules.git] / include / ringbuffer / backend_internal.h
1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
2 *
3 * ringbuffer/backend_internal.h
4 *
5 * Ring buffer backend (internal helpers).
6 *
7 * Copyright (C) 2008-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #ifndef _LIB_RING_BUFFER_BACKEND_INTERNAL_H
11 #define _LIB_RING_BUFFER_BACKEND_INTERNAL_H
12
13 #include <wrapper/compiler.h>
14 #include <wrapper/inline_memcpy.h>
15 #include <ringbuffer/config.h>
16 #include <ringbuffer/backend_types.h>
17 #include <ringbuffer/frontend_types.h>
18 #include <linux/string.h>
19 #include <linux/uaccess.h>
20
21 /* Ring buffer backend API presented to the frontend */
22
23 /* Ring buffer and channel backend create/free */
24
25 int lib_ring_buffer_backend_create(struct lttng_kernel_ring_buffer_backend *bufb,
26 struct channel_backend *chan, int cpu);
27 void channel_backend_unregister_notifiers(struct channel_backend *chanb);
28 void lib_ring_buffer_backend_free(struct lttng_kernel_ring_buffer_backend *bufb);
29 int channel_backend_init(struct channel_backend *chanb,
30 const char *name,
31 const struct lttng_kernel_ring_buffer_config *config,
32 void *priv, size_t subbuf_size,
33 size_t num_subbuf);
34 void channel_backend_free(struct channel_backend *chanb);
35
36 void lib_ring_buffer_backend_reset(struct lttng_kernel_ring_buffer_backend *bufb);
37 void channel_backend_reset(struct channel_backend *chanb);
38
39 int lib_ring_buffer_backend_init(void);
40 void lib_ring_buffer_backend_exit(void);
41
42 extern void _lib_ring_buffer_write(struct lttng_kernel_ring_buffer_backend *bufb,
43 size_t offset, const void *src, size_t len,
44 size_t pagecpy);
45 extern void _lib_ring_buffer_memset(struct lttng_kernel_ring_buffer_backend *bufb,
46 size_t offset, int c, size_t len,
47 size_t pagecpy);
48 extern void _lib_ring_buffer_strcpy(struct lttng_kernel_ring_buffer_backend *bufb,
49 size_t offset, const char *src, size_t len,
50 size_t pagecpy, int pad);
51 extern void _lib_ring_buffer_pstrcpy(struct lttng_kernel_ring_buffer_backend *bufb,
52 size_t offset, const char *src, size_t len, int pad);
53 extern void _lib_ring_buffer_copy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend *bufb,
54 size_t offset, const void *src,
55 size_t len, size_t pagecpy);
56 extern void _lib_ring_buffer_strcpy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend *bufb,
57 size_t offset, const char __user *src, size_t len,
58 size_t pagecpy, int pad);
59 extern void _lib_ring_buffer_pstrcpy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend *bufb,
60 size_t offset, const char __user *src, size_t len, int pad);
61
62 /*
63 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
64 * exchanged atomically.
65 *
66 * Top half word, except lowest bit, belongs to "offset", which is used to keep
67 * to count the produced buffers. For overwrite mode, this provides the
68 * consumer with the capacity to read subbuffers in order, handling the
69 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
70 * systems) concurrently with a single execution of get_subbuf (between offset
71 * sampling and subbuffer ID exchange).
72 */
73
74 #define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
75
76 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
77 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
78 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
79 /*
80 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
81 */
82 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
83 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
84 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
85 /*
86 * In overwrite mode: lowest half of word is used for index.
87 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
88 * In producer-consumer mode: whole word used for index.
89 */
90 #define SB_ID_INDEX_SHIFT 0
91 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
92 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
93
94 /*
95 * Construct the subbuffer id from offset, index and noref. Use only the index
96 * for producer-consumer mode (offset and noref are only used in overwrite
97 * mode).
98 */
99 static inline
100 unsigned long subbuffer_id(const struct lttng_kernel_ring_buffer_config *config,
101 unsigned long offset, unsigned long noref,
102 unsigned long index)
103 {
104 if (config->mode == RING_BUFFER_OVERWRITE)
105 return (offset << SB_ID_OFFSET_SHIFT)
106 | (noref << SB_ID_NOREF_SHIFT)
107 | index;
108 else
109 return index;
110 }
111
112 /*
113 * Compare offset with the offset contained within id. Return 1 if the offset
114 * bits are identical, else 0.
115 */
116 static inline
117 int subbuffer_id_compare_offset(const struct lttng_kernel_ring_buffer_config *config,
118 unsigned long id, unsigned long offset)
119 {
120 return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT);
121 }
122
123 static inline
124 unsigned long subbuffer_id_get_index(const struct lttng_kernel_ring_buffer_config *config,
125 unsigned long id)
126 {
127 if (config->mode == RING_BUFFER_OVERWRITE)
128 return id & SB_ID_INDEX_MASK;
129 else
130 return id;
131 }
132
133 static inline
134 unsigned long subbuffer_id_is_noref(const struct lttng_kernel_ring_buffer_config *config,
135 unsigned long id)
136 {
137 if (config->mode == RING_BUFFER_OVERWRITE)
138 return !!(id & SB_ID_NOREF_MASK);
139 else
140 return 1;
141 }
142
143 /*
144 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
145 * needed.
146 */
147 static inline
148 void subbuffer_id_set_noref(const struct lttng_kernel_ring_buffer_config *config,
149 unsigned long *id)
150 {
151 if (config->mode == RING_BUFFER_OVERWRITE)
152 *id |= SB_ID_NOREF_MASK;
153 }
154
155 static inline
156 void subbuffer_id_set_noref_offset(const struct lttng_kernel_ring_buffer_config *config,
157 unsigned long *id, unsigned long offset)
158 {
159 unsigned long tmp;
160
161 if (config->mode == RING_BUFFER_OVERWRITE) {
162 tmp = *id;
163 tmp &= ~SB_ID_OFFSET_MASK;
164 tmp |= offset << SB_ID_OFFSET_SHIFT;
165 tmp |= SB_ID_NOREF_MASK;
166 /* Volatile store, read concurrently by readers. */
167 WRITE_ONCE(*id, tmp);
168 }
169 }
170
171 /* No volatile access, since already used locally */
172 static inline
173 void subbuffer_id_clear_noref(const struct lttng_kernel_ring_buffer_config *config,
174 unsigned long *id)
175 {
176 if (config->mode == RING_BUFFER_OVERWRITE)
177 *id &= ~SB_ID_NOREF_MASK;
178 }
179
180 /*
181 * For overwrite mode, cap the number of subbuffers per buffer to:
182 * 2^16 on 32-bit architectures
183 * 2^32 on 64-bit architectures
184 * This is required to fit in the index part of the ID. Return 0 on success,
185 * -EPERM on failure.
186 */
187 static inline
188 int subbuffer_id_check_index(const struct lttng_kernel_ring_buffer_config *config,
189 unsigned long num_subbuf)
190 {
191 if (config->mode == RING_BUFFER_OVERWRITE)
192 return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0;
193 else
194 return 0;
195 }
196
197 static inline
198 void lib_ring_buffer_backend_get_pages(const struct lttng_kernel_ring_buffer_config *config,
199 struct lttng_kernel_ring_buffer_ctx *ctx,
200 struct lttng_kernel_ring_buffer_backend_pages **backend_pages)
201 {
202 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
203 struct channel_backend *chanb = &ctx->priv.chan->backend;
204 size_t sbidx, offset = ctx->priv.buf_offset;
205 unsigned long sb_bindex, id;
206 struct lttng_kernel_ring_buffer_backend_pages *rpages;
207
208 offset &= chanb->buf_size - 1;
209 sbidx = offset >> chanb->subbuf_size_order;
210 id = bufb->buf_wsb[sbidx].id;
211 sb_bindex = subbuffer_id_get_index(config, id);
212 rpages = bufb->array[sb_bindex];
213 CHAN_WARN_ON(ctx->priv.chan,
214 config->mode == RING_BUFFER_OVERWRITE
215 && subbuffer_id_is_noref(config, id));
216 *backend_pages = rpages;
217 }
218
219 /* Get backend pages from cache. */
220 static inline
221 struct lttng_kernel_ring_buffer_backend_pages *
222 lib_ring_buffer_get_backend_pages_from_ctx(const struct lttng_kernel_ring_buffer_config *config,
223 struct lttng_kernel_ring_buffer_ctx *ctx)
224 {
225 return ctx->priv.backend_pages;
226 }
227
228 /*
229 * The ring buffer can count events recorded and overwritten per buffer,
230 * but it is disabled by default due to its performance overhead.
231 */
232 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
233 static inline
234 void subbuffer_count_record(const struct lttng_kernel_ring_buffer_config *config,
235 struct lttng_kernel_ring_buffer_backend *bufb,
236 unsigned long idx)
237 {
238 unsigned long sb_bindex;
239
240 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
241 v_inc(config, &bufb->array[sb_bindex]->records_commit);
242 }
243
244 /*
245 * Reader has exclusive subbuffer access for record consumption. No need to
246 * perform the decrement atomically.
247 */
248 static inline
249 void subbuffer_consume_record(const struct lttng_kernel_ring_buffer_config *config,
250 struct lttng_kernel_ring_buffer_backend *bufb)
251 {
252 unsigned long sb_bindex;
253
254 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
255 CHAN_WARN_ON(bufb->chan,
256 !v_read(config, &bufb->array[sb_bindex]->records_unread));
257 /* Non-atomic decrement protected by exclusive subbuffer access */
258 _v_dec(config, &bufb->array[sb_bindex]->records_unread);
259 v_inc(config, &bufb->records_read);
260 }
261 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
262 static inline
263 void subbuffer_count_record(const struct lttng_kernel_ring_buffer_config *config,
264 struct lttng_kernel_ring_buffer_backend *bufb,
265 unsigned long idx)
266 {
267 }
268 static inline
269 void subbuffer_consume_record(const struct lttng_kernel_ring_buffer_config *config,
270 struct lttng_kernel_ring_buffer_backend *bufb)
271 {
272 }
273 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
274
275 static inline
276 unsigned long subbuffer_get_records_count(
277 const struct lttng_kernel_ring_buffer_config *config,
278 struct lttng_kernel_ring_buffer_backend *bufb,
279 unsigned long idx)
280 {
281 unsigned long sb_bindex;
282
283 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
284 return v_read(config, &bufb->array[sb_bindex]->records_commit);
285 }
286
287 /*
288 * Must be executed at subbuffer delivery when the writer has _exclusive_
289 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
290 * lib_ring_buffer_get_records_count() must be called to get the records
291 * count before this function, because it resets the records_commit
292 * count.
293 */
294 static inline
295 unsigned long subbuffer_count_records_overrun(
296 const struct lttng_kernel_ring_buffer_config *config,
297 struct lttng_kernel_ring_buffer_backend *bufb,
298 unsigned long idx)
299 {
300 struct lttng_kernel_ring_buffer_backend_pages *pages;
301 unsigned long overruns, sb_bindex;
302
303 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
304 pages = bufb->array[sb_bindex];
305 overruns = v_read(config, &pages->records_unread);
306 v_set(config, &pages->records_unread,
307 v_read(config, &pages->records_commit));
308 v_set(config, &pages->records_commit, 0);
309
310 return overruns;
311 }
312
313 static inline
314 void subbuffer_set_data_size(const struct lttng_kernel_ring_buffer_config *config,
315 struct lttng_kernel_ring_buffer_backend *bufb,
316 unsigned long idx,
317 unsigned long data_size)
318 {
319 struct lttng_kernel_ring_buffer_backend_pages *pages;
320 unsigned long sb_bindex;
321
322 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
323 pages = bufb->array[sb_bindex];
324 pages->data_size = data_size;
325 }
326
327 static inline
328 unsigned long subbuffer_get_read_data_size(
329 const struct lttng_kernel_ring_buffer_config *config,
330 struct lttng_kernel_ring_buffer_backend *bufb)
331 {
332 struct lttng_kernel_ring_buffer_backend_pages *pages;
333 unsigned long sb_bindex;
334
335 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
336 pages = bufb->array[sb_bindex];
337 return pages->data_size;
338 }
339
340 static inline
341 unsigned long subbuffer_get_data_size(
342 const struct lttng_kernel_ring_buffer_config *config,
343 struct lttng_kernel_ring_buffer_backend *bufb,
344 unsigned long idx)
345 {
346 struct lttng_kernel_ring_buffer_backend_pages *pages;
347 unsigned long sb_bindex;
348
349 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
350 pages = bufb->array[sb_bindex];
351 return pages->data_size;
352 }
353
354 static inline
355 void subbuffer_inc_packet_count(const struct lttng_kernel_ring_buffer_config *config,
356 struct lttng_kernel_ring_buffer_backend *bufb,
357 unsigned long idx)
358 {
359 bufb->buf_cnt[idx].seq_cnt++;
360 }
361
362 /**
363 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
364 * writer.
365 */
366 static inline
367 void lib_ring_buffer_clear_noref(const struct lttng_kernel_ring_buffer_config *config,
368 struct lttng_kernel_ring_buffer_backend *bufb,
369 unsigned long idx)
370 {
371 unsigned long id, new_id;
372
373 if (config->mode != RING_BUFFER_OVERWRITE)
374 return;
375
376 /*
377 * Performing a volatile access to read the sb_pages, because we want to
378 * read a coherent version of the pointer and the associated noref flag.
379 */
380 id = LTTNG_READ_ONCE(bufb->buf_wsb[idx].id);
381 for (;;) {
382 /* This check is called on the fast path for each record. */
383 if (likely(!subbuffer_id_is_noref(config, id))) {
384 /*
385 * Store after load dependency ordering the writes to
386 * the subbuffer after load and test of the noref flag
387 * matches the memory barrier implied by the cmpxchg()
388 * in update_read_sb_index().
389 */
390 return; /* Already writing to this buffer */
391 }
392 new_id = id;
393 subbuffer_id_clear_noref(config, &new_id);
394 new_id = cmpxchg(&bufb->buf_wsb[idx].id, id, new_id);
395 if (likely(new_id == id))
396 break;
397 id = new_id;
398 }
399 }
400
401 /**
402 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
403 * called by writer.
404 */
405 static inline
406 void lib_ring_buffer_set_noref_offset(const struct lttng_kernel_ring_buffer_config *config,
407 struct lttng_kernel_ring_buffer_backend *bufb,
408 unsigned long idx, unsigned long offset)
409 {
410 if (config->mode != RING_BUFFER_OVERWRITE)
411 return;
412
413 /*
414 * Because ring_buffer_set_noref() is only called by a single thread
415 * (the one which updated the cc_sb value), there are no concurrent
416 * updates to take care of: other writers have not updated cc_sb, so
417 * they cannot set the noref flag, and concurrent readers cannot modify
418 * the pointer because the noref flag is not set yet.
419 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
420 * to the subbuffer before this set noref operation.
421 * subbuffer_set_noref() uses a volatile store to deal with concurrent
422 * readers of the noref flag.
423 */
424 CHAN_WARN_ON(bufb->chan,
425 subbuffer_id_is_noref(config, bufb->buf_wsb[idx].id));
426 /*
427 * Memory barrier that ensures counter stores are ordered before set
428 * noref and offset.
429 */
430 smp_mb();
431 subbuffer_id_set_noref_offset(config, &bufb->buf_wsb[idx].id, offset);
432 }
433
434 /**
435 * update_read_sb_index - Read-side subbuffer index update.
436 */
437 static inline
438 int update_read_sb_index(const struct lttng_kernel_ring_buffer_config *config,
439 struct lttng_kernel_ring_buffer_backend *bufb,
440 struct channel_backend *chanb,
441 unsigned long consumed_idx,
442 unsigned long consumed_count)
443 {
444 unsigned long old_id, new_id;
445
446 if (config->mode == RING_BUFFER_OVERWRITE) {
447 /*
448 * Exchange the target writer subbuffer with our own unused
449 * subbuffer. No need to use READ_ONCE() here to read the
450 * old_wpage, because the value read will be confirmed by the
451 * following cmpxchg().
452 */
453 old_id = bufb->buf_wsb[consumed_idx].id;
454 if (unlikely(!subbuffer_id_is_noref(config, old_id)))
455 return -EAGAIN;
456 /*
457 * Make sure the offset count we are expecting matches the one
458 * indicated by the writer.
459 */
460 if (unlikely(!subbuffer_id_compare_offset(config, old_id,
461 consumed_count)))
462 return -EAGAIN;
463 CHAN_WARN_ON(bufb->chan,
464 !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
465 subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
466 consumed_count);
467 new_id = cmpxchg(&bufb->buf_wsb[consumed_idx].id, old_id,
468 bufb->buf_rsb.id);
469 if (unlikely(old_id != new_id))
470 return -EAGAIN;
471 bufb->buf_rsb.id = new_id;
472 } else {
473 /* No page exchange, use the writer page directly */
474 bufb->buf_rsb.id = bufb->buf_wsb[consumed_idx].id;
475 }
476 return 0;
477 }
478
479 static inline __attribute__((always_inline))
480 void lttng_inline_memcpy(void *dest, const void *src,
481 unsigned long len)
482 {
483 switch (len) {
484 case 1:
485 *(uint8_t *) dest = *(const uint8_t *) src;
486 break;
487 case 2:
488 *(uint16_t *) dest = *(const uint16_t *) src;
489 break;
490 case 4:
491 *(uint32_t *) dest = *(const uint32_t *) src;
492 break;
493 case 8:
494 *(uint64_t *) dest = *(const uint64_t *) src;
495 break;
496 default:
497 inline_memcpy(dest, src, len);
498 }
499 }
500
501 /*
502 * Use the architecture-specific memcpy implementation for constant-sized
503 * inputs, but rely on an inline memcpy for length statically unknown.
504 * The function call to memcpy is just way too expensive for a fast path.
505 */
506 #define lib_ring_buffer_do_copy(config, dest, src, len) \
507 do { \
508 size_t __len = (len); \
509 if (__builtin_constant_p(len)) \
510 memcpy(dest, src, __len); \
511 else \
512 lttng_inline_memcpy(dest, src, __len); \
513 } while (0)
514
515 /*
516 * We use __copy_from_user_inatomic to copy userspace data since we already
517 * did the access_ok for the whole range.
518 *
519 * Return 0 if OK, nonzero on error.
520 */
521 static inline
522 unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest,
523 const void __user *src,
524 unsigned long len)
525 {
526 return __copy_from_user_inatomic(dest, src, len);
527 }
528
529 /*
530 * write len bytes to dest with c
531 */
532 static inline
533 void lib_ring_buffer_do_memset(char *dest, int c,
534 unsigned long len)
535 {
536 unsigned long i;
537
538 for (i = 0; i < len; i++)
539 dest[i] = c;
540 }
541
542 #endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */
This page took 0.038949 seconds and 3 git commands to generate.