Cleanup: clarify strcpy/strcpy_from_user local variables
[lttng-modules.git] / include / ringbuffer / backend.h
1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
2 *
3 * ringbuffer/backend.h
4 *
5 * Ring buffer backend (API).
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
10 * the reader in flight recorder mode.
11 */
12
13 #ifndef _LIB_RING_BUFFER_BACKEND_H
14 #define _LIB_RING_BUFFER_BACKEND_H
15
16 #include <linux/types.h>
17 #include <linux/sched.h>
18 #include <linux/timer.h>
19 #include <linux/wait.h>
20 #include <linux/poll.h>
21 #include <linux/list.h>
22 #include <linux/fs.h>
23 #include <linux/mm.h>
24 #include <wrapper/uaccess.h>
25
26 /* Internal helpers */
27 #include <ringbuffer/backend_internal.h>
28 #include <ringbuffer/frontend_internal.h>
29
30 /* Ring buffer backend API */
31
32 /* Ring buffer backend access (read/write) */
33
34 extern size_t lib_ring_buffer_read(struct lttng_kernel_ring_buffer_backend *bufb,
35 size_t offset, void *dest, size_t len);
36
37 extern int __lib_ring_buffer_copy_to_user(struct lttng_kernel_ring_buffer_backend *bufb,
38 size_t offset, void __user *dest,
39 size_t len);
40
41 extern int lib_ring_buffer_read_cstr(struct lttng_kernel_ring_buffer_backend *bufb,
42 size_t offset, void *dest, size_t len);
43
44 extern unsigned long *
45 lib_ring_buffer_read_get_pfn(struct lttng_kernel_ring_buffer_backend *bufb, size_t offset,
46 void ***virt);
47
48 /*
49 * Return the address where a given offset is located.
50 * Should be used to get the current subbuffer header pointer. Given we know
51 * it's never on a page boundary, it's safe to write directly to this address,
52 * as long as the write is never bigger than a page size.
53 */
54 extern void *
55 lib_ring_buffer_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
56 size_t offset);
57 extern void *
58 lib_ring_buffer_read_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
59 size_t offset);
60
61 /**
62 * lib_ring_buffer_write - write data to a buffer backend
63 * @config : ring buffer instance configuration
64 * @ctx: ring buffer context. (input arguments only)
65 * @src : source pointer to copy from
66 * @len : length of data to copy
67 *
68 * This function copies "len" bytes of data from a source pointer to a buffer
69 * backend, at the current context offset. This is more or less a buffer
70 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
71 * if copy is crossing a page boundary.
72 */
73 static inline __attribute__((always_inline))
74 void lib_ring_buffer_write(const struct lttng_kernel_ring_buffer_config *config,
75 struct lttng_kernel_ring_buffer_ctx *ctx,
76 const void *src, size_t len)
77 {
78 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
79 struct channel_backend *chanb = &ctx->priv.chan->backend;
80 size_t index, pagecpy;
81 size_t offset = ctx->priv.buf_offset;
82 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
83
84 if (unlikely(!len))
85 return;
86 backend_pages =
87 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
88 offset &= chanb->buf_size - 1;
89 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
90 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
91 if (likely(pagecpy == len))
92 lib_ring_buffer_do_copy(config,
93 backend_pages->p[index].virt
94 + (offset & ~PAGE_MASK),
95 src, len);
96 else
97 _lib_ring_buffer_write(bufb, offset, src, len, 0);
98 ctx->priv.buf_offset += len;
99 }
100
101 /**
102 * lib_ring_buffer_memset - write len bytes of c to a buffer backend
103 * @config : ring buffer instance configuration
104 * @bufb : ring buffer backend
105 * @offset : offset within the buffer
106 * @c : the byte to copy
107 * @len : number of bytes to copy
108 *
109 * This function writes "len" bytes of "c" to a buffer backend, at a specific
110 * offset. This is more or less a buffer backend-specific memset() operation.
111 * Calls the slow path (_ring_buffer_memset) if write is crossing a page
112 * boundary.
113 */
114 static inline
115 void lib_ring_buffer_memset(const struct lttng_kernel_ring_buffer_config *config,
116 struct lttng_kernel_ring_buffer_ctx *ctx, int c, size_t len)
117 {
118
119 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
120 struct channel_backend *chanb = &ctx->priv.chan->backend;
121 size_t index, pagecpy;
122 size_t offset = ctx->priv.buf_offset;
123 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
124
125 if (unlikely(!len))
126 return;
127 backend_pages =
128 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
129 offset &= chanb->buf_size - 1;
130 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
131 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
132 if (likely(pagecpy == len))
133 lib_ring_buffer_do_memset(backend_pages->p[index].virt
134 + (offset & ~PAGE_MASK),
135 c, len);
136 else
137 _lib_ring_buffer_memset(bufb, offset, c, len, 0);
138 ctx->priv.buf_offset += len;
139 }
140
141 /*
142 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
143 * terminating character is found in @src. Returns the number of bytes
144 * copied. Does *not* terminate @dest with NULL terminating character.
145 */
146 static inline __attribute__((always_inline))
147 size_t lib_ring_buffer_do_strcpy(const struct lttng_kernel_ring_buffer_config *config,
148 char *dest, const char *src, size_t len)
149 {
150 size_t count;
151
152 for (count = 0; count < len; count++) {
153 char c;
154
155 /*
156 * Only read source character once, in case it is
157 * modified concurrently.
158 */
159 c = LTTNG_READ_ONCE(src[count]);
160 if (!c)
161 break;
162 lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
163 }
164 return count;
165 }
166
167 /*
168 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
169 * terminating character is found in @src, or when a fault occurs.
170 * Returns the number of bytes copied. Does *not* terminate @dest with
171 * NULL terminating character.
172 *
173 * This function deals with userspace pointers, it should never be called
174 * directly without having the src pointer checked with access_ok()
175 * previously.
176 */
177 static inline __attribute__((always_inline))
178 size_t lib_ring_buffer_do_strcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
179 char *dest, const char __user *src, size_t len)
180 {
181 size_t count;
182
183 for (count = 0; count < len; count++) {
184 int ret;
185 char c;
186
187 ret = __copy_from_user_inatomic(&c, src + count, 1);
188 if (ret || !c)
189 break;
190 lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
191 }
192 return count;
193 }
194
195 /**
196 * lib_ring_buffer_strcpy - write string data to a buffer backend
197 * @config : ring buffer instance configuration
198 * @ctx: ring buffer context. (input arguments only)
199 * @src : source pointer to copy from
200 * @len : length of data to copy
201 * @pad : character to use for padding
202 *
203 * This function copies @len - 1 bytes of string data from a source
204 * pointer to a buffer backend, followed by a terminating '\0'
205 * character, at the current context offset. This is more or less a
206 * buffer backend-specific strncpy() operation. If a terminating '\0'
207 * character is found in @src before @len - 1 characters are copied, pad
208 * the buffer with @pad characters (e.g. '#'). Calls the slow path
209 * (_ring_buffer_strcpy) if copy is crossing a page boundary.
210 */
211 static inline
212 void lib_ring_buffer_strcpy(const struct lttng_kernel_ring_buffer_config *config,
213 struct lttng_kernel_ring_buffer_ctx *ctx,
214 const char *src, size_t len, int pad)
215 {
216 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
217 struct channel_backend *chanb = &ctx->priv.chan->backend;
218 size_t index, bytes_left_in_page;
219 size_t offset = ctx->priv.buf_offset;
220 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
221
222 if (unlikely(!len))
223 return;
224 backend_pages =
225 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
226 offset &= chanb->buf_size - 1;
227 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
228 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
229 if (likely(bytes_left_in_page == len)) {
230 size_t count;
231
232 count = lib_ring_buffer_do_strcpy(config,
233 backend_pages->p[index].virt
234 + (offset & ~PAGE_MASK),
235 src, len - 1);
236 offset += count;
237 /* Padding */
238 if (unlikely(count < len - 1)) {
239 size_t pad_len = len - 1 - count;
240
241 lib_ring_buffer_do_memset(backend_pages->p[index].virt
242 + (offset & ~PAGE_MASK),
243 pad, pad_len);
244 offset += pad_len;
245 }
246 /* Ending '\0' */
247 lib_ring_buffer_do_memset(backend_pages->p[index].virt
248 + (offset & ~PAGE_MASK),
249 '\0', 1);
250 } else {
251 _lib_ring_buffer_strcpy(bufb, offset, src, len, pad);
252 }
253 ctx->priv.buf_offset += len;
254 }
255
256 /**
257 * lib_ring_buffer_pstrcpy - write kernel C-string (input) to a buffer backend P-string
258 * @config : ring buffer instance configuration
259 * @ctx: ring buffer context. (input arguments only)
260 * @src : source pointer to copy from
261 * @len : length of data to copy
262 * @pad : character to use for padding
263 *
264 * This function copies up to @len bytes of data from a source pointer
265 * to a Pascal String into the buffer backend. If a terminating '\0'
266 * character is found in @src before @len characters are copied, pad the
267 * buffer with @pad characters (e.g. '\0').
268 *
269 * The length of the pascal strings in the ring buffer is explicit: it
270 * is either the array or sequence length.
271 */
272 static inline
273 void lib_ring_buffer_pstrcpy(const struct lttng_kernel_ring_buffer_config *config,
274 struct lttng_kernel_ring_buffer_ctx *ctx,
275 const char *src, size_t len, char pad)
276 __attribute__((always_inline));
277 static inline
278 void lib_ring_buffer_pstrcpy(const struct lttng_kernel_ring_buffer_config *config,
279 struct lttng_kernel_ring_buffer_ctx *ctx,
280 const char *src, size_t len, char pad)
281 {
282 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
283 struct channel_backend *chanb = &ctx->priv.chan->backend;
284 size_t index, bytes_left_in_page;
285 size_t offset = ctx->priv.buf_offset;
286 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
287
288 if (unlikely(!len))
289 return;
290 backend_pages =
291 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
292 offset &= chanb->buf_size - 1;
293 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
294 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
295 if (likely(bytes_left_in_page == len)) {
296 size_t count;
297
298 count = lib_ring_buffer_do_strcpy(config,
299 backend_pages->p[index].virt
300 + (offset & ~PAGE_MASK),
301 src, len);
302 offset += count;
303 /* Padding */
304 if (unlikely(count < len)) {
305 size_t pad_len = len - count;
306
307 lib_ring_buffer_do_memset(backend_pages->p[index].virt
308 + (offset & ~PAGE_MASK),
309 pad, pad_len);
310 offset += pad_len;
311 }
312 } else {
313 _lib_ring_buffer_pstrcpy(bufb, offset, src, len, pad);
314 }
315 ctx->priv.buf_offset += len;
316 }
317
318 /**
319 * lib_ring_buffer_copy_from_user_inatomic - write userspace data to a buffer backend
320 * @config : ring buffer instance configuration
321 * @ctx: ring buffer context. (input arguments only)
322 * @src : userspace source pointer to copy from
323 * @len : length of data to copy
324 *
325 * This function copies "len" bytes of data from a userspace pointer to a
326 * buffer backend, at the current context offset. This is more or less a buffer
327 * backend-specific memcpy() operation. Calls the slow path
328 * (_ring_buffer_write_from_user_inatomic) if copy is crossing a page boundary.
329 * Disable the page fault handler to ensure we never try to take the mmap_sem.
330 */
331 static inline __attribute__((always_inline))
332 void lib_ring_buffer_copy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
333 struct lttng_kernel_ring_buffer_ctx *ctx,
334 const void __user *src, size_t len)
335 {
336 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
337 struct channel_backend *chanb = &ctx->priv.chan->backend;
338 size_t index, pagecpy;
339 size_t offset = ctx->priv.buf_offset;
340 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
341 unsigned long ret;
342
343 if (unlikely(!len))
344 return;
345 backend_pages =
346 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
347 offset &= chanb->buf_size - 1;
348 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
349 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
350
351 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
352 goto fill_buffer;
353
354 pagefault_disable();
355 if (likely(pagecpy == len)) {
356 ret = lib_ring_buffer_do_copy_from_user_inatomic(
357 backend_pages->p[index].virt + (offset & ~PAGE_MASK),
358 src, len);
359 if (unlikely(ret > 0)) {
360 /* Copy failed. */
361 goto fill_buffer_enable_pf;
362 }
363 } else {
364 _lib_ring_buffer_copy_from_user_inatomic(bufb, offset, src, len, 0);
365 }
366 pagefault_enable();
367 ctx->priv.buf_offset += len;
368
369 return;
370
371 fill_buffer_enable_pf:
372 pagefault_enable();
373 fill_buffer:
374 /*
375 * In the error path we call the slow path version to avoid
376 * the pollution of static inline code.
377 */
378 _lib_ring_buffer_memset(bufb, offset, 0, len, 0);
379 ctx->priv.buf_offset += len;
380 }
381
382 /**
383 * lib_ring_buffer_strcpy_from_user_inatomic - write userspace string data to a buffer backend
384 * @config : ring buffer instance configuration
385 * @ctx: ring buffer context (input arguments only)
386 * @src : userspace source pointer to copy from
387 * @len : length of data to copy
388 * @pad : character to use for padding
389 *
390 * This function copies @len - 1 bytes of string data from a userspace
391 * source pointer to a buffer backend, followed by a terminating '\0'
392 * character, at the current context offset. This is more or less a
393 * buffer backend-specific strncpy() operation. If a terminating '\0'
394 * character is found in @src before @len - 1 characters are copied, pad
395 * the buffer with @pad characters (e.g. '#'). Calls the slow path
396 * (_ring_buffer_strcpy_from_user_inatomic) if copy is crossing a page
397 * boundary. Disable the page fault handler to ensure we never try to
398 * take the mmap_sem.
399 */
400 static inline
401 void lib_ring_buffer_strcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
402 struct lttng_kernel_ring_buffer_ctx *ctx,
403 const void __user *src, size_t len, int pad)
404 {
405 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
406 struct channel_backend *chanb = &ctx->priv.chan->backend;
407 size_t index, bytes_left_in_page;
408 size_t offset = ctx->priv.buf_offset;
409 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
410
411 if (unlikely(!len))
412 return;
413 backend_pages =
414 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
415 offset &= chanb->buf_size - 1;
416 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
417 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
418
419 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
420 goto fill_buffer;
421
422 pagefault_disable();
423 if (likely(bytes_left_in_page == len)) {
424 size_t count;
425
426 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
427 backend_pages->p[index].virt
428 + (offset & ~PAGE_MASK),
429 src, len - 1);
430 offset += count;
431 /* Padding */
432 if (unlikely(count < len - 1)) {
433 size_t pad_len = len - 1 - count;
434
435 lib_ring_buffer_do_memset(backend_pages->p[index].virt
436 + (offset & ~PAGE_MASK),
437 pad, pad_len);
438 offset += pad_len;
439 }
440 /* Ending '\0' */
441 lib_ring_buffer_do_memset(backend_pages->p[index].virt
442 + (offset & ~PAGE_MASK),
443 '\0', 1);
444 } else {
445 _lib_ring_buffer_strcpy_from_user_inatomic(bufb, offset, src,
446 len, pad);
447 }
448 pagefault_enable();
449 ctx->priv.buf_offset += len;
450
451 return;
452
453 fill_buffer:
454 /*
455 * In the error path we call the slow path version to avoid
456 * the pollution of static inline code.
457 */
458 _lib_ring_buffer_memset(bufb, offset, pad, len - 1, 0);
459 offset += len - 1;
460 _lib_ring_buffer_memset(bufb, offset, '\0', 1, 0);
461 ctx->priv.buf_offset += len;
462 }
463
464 /**
465 * lib_ring_buffer_pstrcpy_from_user_inatomic - write user-space C-string (input) to a buffer backend P-string
466 * @config : ring buffer instance configuration
467 * @ctx: ring buffer context. (input arguments only)
468 * @src : source pointer to copy from
469 * @len : length of data to copy
470 * @pad : character to use for padding
471 *
472 * This function copies up to @len bytes of data from a source pointer
473 * to a Pascal String into the buffer backend. If a terminating '\0'
474 * character is found in @src before @len characters are copied, pad the
475 * buffer with @pad characters (e.g. '\0').
476 *
477 * The length of the pascal strings in the ring buffer is explicit: it
478 * is either the array or sequence length.
479 */
480 static inline
481 void lib_ring_buffer_pstrcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
482 struct lttng_kernel_ring_buffer_ctx *ctx,
483 const char __user *src, size_t len, char pad)
484 __attribute__((always_inline));
485 static inline
486 void lib_ring_buffer_pstrcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
487 struct lttng_kernel_ring_buffer_ctx *ctx,
488 const char __user *src, size_t len, char pad)
489 {
490 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
491 struct channel_backend *chanb = &ctx->priv.chan->backend;
492 size_t index, bytes_left_in_page;
493 size_t offset = ctx->priv.buf_offset;
494 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
495
496 if (unlikely(!len))
497 return;
498 backend_pages =
499 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
500 offset &= chanb->buf_size - 1;
501 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
502 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
503
504 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
505 goto fill_buffer;
506
507 pagefault_disable();
508 if (likely(bytes_left_in_page == len)) {
509 size_t count;
510
511 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
512 backend_pages->p[index].virt
513 + (offset & ~PAGE_MASK),
514 src, len);
515 offset += count;
516 /* Padding */
517 if (unlikely(count < len)) {
518 size_t pad_len = len - count;
519
520 lib_ring_buffer_do_memset(backend_pages->p[index].virt
521 + (offset & ~PAGE_MASK),
522 pad, pad_len);
523 offset += pad_len;
524 }
525 } else {
526 _lib_ring_buffer_pstrcpy_from_user_inatomic(bufb, offset, src, len, pad);
527 }
528 ctx->priv.buf_offset += len;
529 pagefault_enable();
530
531 return;
532
533 fill_buffer:
534 /*
535 * In the error path we call the slow path version to avoid
536 * the pollution of static inline code.
537 */
538 _lib_ring_buffer_memset(bufb, offset, pad, len, 0);
539 ctx->priv.buf_offset += len;
540 }
541
542 /*
543 * This accessor counts the number of unread records in a buffer.
544 * It only provides a consistent value if no reads not writes are performed
545 * concurrently.
546 */
547 static inline
548 unsigned long lib_ring_buffer_get_records_unread(
549 const struct lttng_kernel_ring_buffer_config *config,
550 struct lttng_kernel_ring_buffer *buf)
551 {
552 struct lttng_kernel_ring_buffer_backend *bufb = &buf->backend;
553 struct lttng_kernel_ring_buffer_backend_pages *pages;
554 unsigned long records_unread = 0, sb_bindex, id;
555 unsigned int i;
556
557 for (i = 0; i < bufb->chan->backend.num_subbuf; i++) {
558 id = bufb->buf_wsb[i].id;
559 sb_bindex = subbuffer_id_get_index(config, id);
560 pages = bufb->array[sb_bindex];
561 records_unread += v_read(config, &pages->records_unread);
562 }
563 if (config->mode == RING_BUFFER_OVERWRITE) {
564 id = bufb->buf_rsb.id;
565 sb_bindex = subbuffer_id_get_index(config, id);
566 pages = bufb->array[sb_bindex];
567 records_unread += v_read(config, &pages->records_unread);
568 }
569 return records_unread;
570 }
571
572 /*
573 * We use __copy_from_user_inatomic to copy userspace data after
574 * checking with access_ok() and disabling page faults.
575 *
576 * Return 0 if OK, nonzero on error.
577 */
578 static inline
579 unsigned long lib_ring_buffer_copy_from_user_check_nofault(void *dest,
580 const void __user *src,
581 unsigned long len)
582 {
583 unsigned long ret;
584
585 if (!lttng_access_ok(VERIFY_READ, src, len))
586 return 1;
587 pagefault_disable();
588 ret = __copy_from_user_inatomic(dest, src, len);
589 pagefault_enable();
590 return ret;
591 }
592
593 #endif /* _LIB_RING_BUFFER_BACKEND_H */
This page took 0.040737 seconds and 4 git commands to generate.