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