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