Clean-up: rb: backend.h: remove extra newline
[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 #include <lttng/probe-user.h>
26
27 /* Internal helpers */
28 #include <ringbuffer/backend_internal.h>
29 #include <ringbuffer/frontend_internal.h>
30
31 /* Ring buffer backend API */
32
33 /* Ring buffer backend access (read/write) */
34
35 extern size_t lib_ring_buffer_read(struct lttng_kernel_ring_buffer_backend *bufb,
36 size_t offset, void *dest, size_t len);
37
38 extern int __lib_ring_buffer_copy_to_user(struct lttng_kernel_ring_buffer_backend *bufb,
39 size_t offset, void __user *dest,
40 size_t len);
41
42 extern int lib_ring_buffer_read_cstr(struct lttng_kernel_ring_buffer_backend *bufb,
43 size_t offset, void *dest, size_t len);
44
45 extern unsigned long *
46 lib_ring_buffer_read_get_pfn(struct lttng_kernel_ring_buffer_backend *bufb, size_t offset,
47 void ***virt);
48
49 /*
50 * Return the address where a given offset is located.
51 * Should be used to get the current subbuffer header pointer. Given we know
52 * it's never on a page boundary, it's safe to write directly to this address,
53 * as long as the write is never bigger than a page size.
54 */
55 extern void *
56 lib_ring_buffer_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
57 size_t offset);
58 extern void *
59 lib_ring_buffer_read_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
60 size_t offset);
61
62 /**
63 * lib_ring_buffer_write - write data to a buffer backend
64 * @config : ring buffer instance configuration
65 * @ctx: ring buffer context. (input arguments only)
66 * @src : source pointer to copy from
67 * @len : length of data to copy
68 *
69 * This function copies "len" bytes of data from a source pointer to a buffer
70 * backend, at the current context offset. This is more or less a buffer
71 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
72 * if copy is crossing a page boundary.
73 */
74 static inline __attribute__((always_inline))
75 void lib_ring_buffer_write(const struct lttng_kernel_ring_buffer_config *config,
76 struct lttng_kernel_ring_buffer_ctx *ctx,
77 const void *src, size_t len)
78 {
79 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
80 struct channel_backend *chanb = &ctx->priv.chan->backend;
81 size_t index, bytes_left_in_page;
82 size_t offset = ctx->priv.buf_offset;
83 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
84
85 if (unlikely(!len))
86 return;
87 backend_pages =
88 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
89 offset &= chanb->buf_size - 1;
90 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
91 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
92 if (likely(bytes_left_in_page == len))
93 lib_ring_buffer_do_copy(config,
94 backend_pages->p[index].virt
95 + (offset & ~PAGE_MASK),
96 src, len);
97 else
98 _lib_ring_buffer_write(bufb, offset, src, len);
99 ctx->priv.buf_offset += len;
100 }
101
102 /**
103 * lib_ring_buffer_memset - write len bytes of c to a buffer backend
104 * @config : ring buffer instance configuration
105 * @bufb : ring buffer backend
106 * @offset : offset within the buffer
107 * @c : the byte to copy
108 * @len : number of bytes to copy
109 *
110 * This function writes "len" bytes of "c" to a buffer backend, at a specific
111 * offset. This is more or less a buffer backend-specific memset() operation.
112 * Calls the slow path (_ring_buffer_memset) if write is crossing a page
113 * boundary.
114 */
115 static inline
116 void lib_ring_buffer_memset(const struct lttng_kernel_ring_buffer_config *config,
117 struct lttng_kernel_ring_buffer_ctx *ctx, int c, size_t len)
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, bytes_left_in_page;
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 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
132 if (likely(bytes_left_in_page == 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);
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, bytes_left_in_page;
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 bytes_left_in_page = 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(bytes_left_in_page == 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);
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);
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);
459 offset += len - 1;
460 _lib_ring_buffer_memset(bufb, offset, '\0', 1);
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);
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 return lttng_copy_from_user_check_nofault(dest, src, len);
584 }
585
586 #endif /* _LIB_RING_BUFFER_BACKEND_H */
This page took 0.063331 seconds and 4 git commands to generate.