6a90161839c76903f28bb4c5a5e4eede152dfc2c
[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
120 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
121 struct channel_backend *chanb = &ctx->priv.chan->backend;
122 size_t index, bytes_left_in_page;
123 size_t offset = ctx->priv.buf_offset;
124 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
125
126 if (unlikely(!len))
127 return;
128 backend_pages =
129 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
130 offset &= chanb->buf_size - 1;
131 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
132 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
133 if (likely(bytes_left_in_page == len))
134 lib_ring_buffer_do_memset(backend_pages->p[index].virt
135 + (offset & ~PAGE_MASK),
136 c, len);
137 else
138 _lib_ring_buffer_memset(bufb, offset, c, len);
139 ctx->priv.buf_offset += len;
140 }
141
142 /*
143 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
144 * terminating character is found in @src. Returns the number of bytes
145 * copied. Does *not* terminate @dest with NULL terminating character.
146 */
147 static inline __attribute__((always_inline))
148 size_t lib_ring_buffer_do_strcpy(const struct lttng_kernel_ring_buffer_config *config,
149 char *dest, const char *src, size_t len)
150 {
151 size_t count;
152
153 for (count = 0; count < len; count++) {
154 char c;
155
156 /*
157 * Only read source character once, in case it is
158 * modified concurrently.
159 */
160 c = LTTNG_READ_ONCE(src[count]);
161 if (!c)
162 break;
163 lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
164 }
165 return count;
166 }
167
168 /*
169 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
170 * terminating character is found in @src, or when a fault occurs.
171 * Returns the number of bytes copied. Does *not* terminate @dest with
172 * NULL terminating character.
173 *
174 * This function deals with userspace pointers, it should never be called
175 * directly without having the src pointer checked with access_ok()
176 * previously.
177 */
178 static inline __attribute__((always_inline))
179 size_t lib_ring_buffer_do_strcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
180 char *dest, const char __user *src, size_t len)
181 {
182 size_t count;
183
184 for (count = 0; count < len; count++) {
185 int ret;
186 char c;
187
188 ret = __copy_from_user_inatomic(&c, src + count, 1);
189 if (ret || !c)
190 break;
191 lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
192 }
193 return count;
194 }
195
196 /**
197 * lib_ring_buffer_strcpy - write string data to a buffer backend
198 * @config : ring buffer instance configuration
199 * @ctx: ring buffer context. (input arguments only)
200 * @src : source pointer to copy from
201 * @len : length of data to copy
202 * @pad : character to use for padding
203 *
204 * This function copies @len - 1 bytes of string data from a source
205 * pointer to a buffer backend, followed by a terminating '\0'
206 * character, at the current context offset. This is more or less a
207 * buffer backend-specific strncpy() operation. If a terminating '\0'
208 * character is found in @src before @len - 1 characters are copied, pad
209 * the buffer with @pad characters (e.g. '#'). Calls the slow path
210 * (_ring_buffer_strcpy) if copy is crossing a page boundary.
211 */
212 static inline
213 void lib_ring_buffer_strcpy(const struct lttng_kernel_ring_buffer_config *config,
214 struct lttng_kernel_ring_buffer_ctx *ctx,
215 const char *src, size_t len, int pad)
216 {
217 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
218 struct channel_backend *chanb = &ctx->priv.chan->backend;
219 size_t index, bytes_left_in_page;
220 size_t offset = ctx->priv.buf_offset;
221 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
222
223 if (unlikely(!len))
224 return;
225 backend_pages =
226 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
227 offset &= chanb->buf_size - 1;
228 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
229 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
230 if (likely(bytes_left_in_page == len)) {
231 size_t count;
232
233 count = lib_ring_buffer_do_strcpy(config,
234 backend_pages->p[index].virt
235 + (offset & ~PAGE_MASK),
236 src, len - 1);
237 offset += count;
238 /* Padding */
239 if (unlikely(count < len - 1)) {
240 size_t pad_len = len - 1 - count;
241
242 lib_ring_buffer_do_memset(backend_pages->p[index].virt
243 + (offset & ~PAGE_MASK),
244 pad, pad_len);
245 offset += pad_len;
246 }
247 /* Ending '\0' */
248 lib_ring_buffer_do_memset(backend_pages->p[index].virt
249 + (offset & ~PAGE_MASK),
250 '\0', 1);
251 } else {
252 _lib_ring_buffer_strcpy(bufb, offset, src, len, pad);
253 }
254 ctx->priv.buf_offset += len;
255 }
256
257 /**
258 * lib_ring_buffer_pstrcpy - write kernel C-string (input) to a buffer backend P-string
259 * @config : ring buffer instance configuration
260 * @ctx: ring buffer context. (input arguments only)
261 * @src : source pointer to copy from
262 * @len : length of data to copy
263 * @pad : character to use for padding
264 *
265 * This function copies up to @len bytes of data from a source pointer
266 * to a Pascal String into the buffer backend. If a terminating '\0'
267 * character is found in @src before @len characters are copied, pad the
268 * buffer with @pad characters (e.g. '\0').
269 *
270 * The length of the pascal strings in the ring buffer is explicit: it
271 * is either the array or sequence length.
272 */
273 static inline
274 void lib_ring_buffer_pstrcpy(const struct lttng_kernel_ring_buffer_config *config,
275 struct lttng_kernel_ring_buffer_ctx *ctx,
276 const char *src, size_t len, char pad)
277 __attribute__((always_inline));
278 static inline
279 void lib_ring_buffer_pstrcpy(const struct lttng_kernel_ring_buffer_config *config,
280 struct lttng_kernel_ring_buffer_ctx *ctx,
281 const char *src, size_t len, char pad)
282 {
283 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
284 struct channel_backend *chanb = &ctx->priv.chan->backend;
285 size_t index, bytes_left_in_page;
286 size_t offset = ctx->priv.buf_offset;
287 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
288
289 if (unlikely(!len))
290 return;
291 backend_pages =
292 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
293 offset &= chanb->buf_size - 1;
294 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
295 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
296 if (likely(bytes_left_in_page == len)) {
297 size_t count;
298
299 count = lib_ring_buffer_do_strcpy(config,
300 backend_pages->p[index].virt
301 + (offset & ~PAGE_MASK),
302 src, len);
303 offset += count;
304 /* Padding */
305 if (unlikely(count < len)) {
306 size_t pad_len = len - count;
307
308 lib_ring_buffer_do_memset(backend_pages->p[index].virt
309 + (offset & ~PAGE_MASK),
310 pad, pad_len);
311 offset += pad_len;
312 }
313 } else {
314 _lib_ring_buffer_pstrcpy(bufb, offset, src, len, pad);
315 }
316 ctx->priv.buf_offset += len;
317 }
318
319 /**
320 * lib_ring_buffer_copy_from_user_inatomic - write userspace data to a buffer backend
321 * @config : ring buffer instance configuration
322 * @ctx: ring buffer context. (input arguments only)
323 * @src : userspace source pointer to copy from
324 * @len : length of data to copy
325 *
326 * This function copies "len" bytes of data from a userspace pointer to a
327 * buffer backend, at the current context offset. This is more or less a buffer
328 * backend-specific memcpy() operation. Calls the slow path
329 * (_ring_buffer_write_from_user_inatomic) if copy is crossing a page boundary.
330 * Disable the page fault handler to ensure we never try to take the mmap_sem.
331 */
332 static inline __attribute__((always_inline))
333 void lib_ring_buffer_copy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
334 struct lttng_kernel_ring_buffer_ctx *ctx,
335 const void __user *src, size_t len)
336 {
337 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
338 struct channel_backend *chanb = &ctx->priv.chan->backend;
339 size_t index, bytes_left_in_page;
340 size_t offset = ctx->priv.buf_offset;
341 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
342 unsigned long ret;
343
344 if (unlikely(!len))
345 return;
346 backend_pages =
347 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
348 offset &= chanb->buf_size - 1;
349 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
350 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
351
352 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
353 goto fill_buffer;
354
355 pagefault_disable();
356 if (likely(bytes_left_in_page == len)) {
357 ret = lib_ring_buffer_do_copy_from_user_inatomic(
358 backend_pages->p[index].virt + (offset & ~PAGE_MASK),
359 src, len);
360 if (unlikely(ret > 0)) {
361 /* Copy failed. */
362 goto fill_buffer_enable_pf;
363 }
364 } else {
365 _lib_ring_buffer_copy_from_user_inatomic(bufb, offset, src, len);
366 }
367 pagefault_enable();
368 ctx->priv.buf_offset += len;
369
370 return;
371
372 fill_buffer_enable_pf:
373 pagefault_enable();
374 fill_buffer:
375 /*
376 * In the error path we call the slow path version to avoid
377 * the pollution of static inline code.
378 */
379 _lib_ring_buffer_memset(bufb, offset, 0, len);
380 ctx->priv.buf_offset += len;
381 }
382
383 /**
384 * lib_ring_buffer_strcpy_from_user_inatomic - write userspace string data to a buffer backend
385 * @config : ring buffer instance configuration
386 * @ctx: ring buffer context (input arguments only)
387 * @src : userspace source pointer to copy from
388 * @len : length of data to copy
389 * @pad : character to use for padding
390 *
391 * This function copies @len - 1 bytes of string data from a userspace
392 * source pointer to a buffer backend, followed by a terminating '\0'
393 * character, at the current context offset. This is more or less a
394 * buffer backend-specific strncpy() operation. If a terminating '\0'
395 * character is found in @src before @len - 1 characters are copied, pad
396 * the buffer with @pad characters (e.g. '#'). Calls the slow path
397 * (_ring_buffer_strcpy_from_user_inatomic) if copy is crossing a page
398 * boundary. Disable the page fault handler to ensure we never try to
399 * take the mmap_sem.
400 */
401 static inline
402 void lib_ring_buffer_strcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
403 struct lttng_kernel_ring_buffer_ctx *ctx,
404 const void __user *src, size_t len, int pad)
405 {
406 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
407 struct channel_backend *chanb = &ctx->priv.chan->backend;
408 size_t index, bytes_left_in_page;
409 size_t offset = ctx->priv.buf_offset;
410 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
411
412 if (unlikely(!len))
413 return;
414 backend_pages =
415 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
416 offset &= chanb->buf_size - 1;
417 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
418 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
419
420 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
421 goto fill_buffer;
422
423 pagefault_disable();
424 if (likely(bytes_left_in_page == len)) {
425 size_t count;
426
427 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
428 backend_pages->p[index].virt
429 + (offset & ~PAGE_MASK),
430 src, len - 1);
431 offset += count;
432 /* Padding */
433 if (unlikely(count < len - 1)) {
434 size_t pad_len = len - 1 - count;
435
436 lib_ring_buffer_do_memset(backend_pages->p[index].virt
437 + (offset & ~PAGE_MASK),
438 pad, pad_len);
439 offset += pad_len;
440 }
441 /* Ending '\0' */
442 lib_ring_buffer_do_memset(backend_pages->p[index].virt
443 + (offset & ~PAGE_MASK),
444 '\0', 1);
445 } else {
446 _lib_ring_buffer_strcpy_from_user_inatomic(bufb, offset, src,
447 len, pad);
448 }
449 pagefault_enable();
450 ctx->priv.buf_offset += len;
451
452 return;
453
454 fill_buffer:
455 /*
456 * In the error path we call the slow path version to avoid
457 * the pollution of static inline code.
458 */
459 _lib_ring_buffer_memset(bufb, offset, pad, len - 1);
460 offset += len - 1;
461 _lib_ring_buffer_memset(bufb, offset, '\0', 1);
462 ctx->priv.buf_offset += len;
463 }
464
465 /**
466 * lib_ring_buffer_pstrcpy_from_user_inatomic - write user-space C-string (input) to a buffer backend P-string
467 * @config : ring buffer instance configuration
468 * @ctx: ring buffer context. (input arguments only)
469 * @src : source pointer to copy from
470 * @len : length of data to copy
471 * @pad : character to use for padding
472 *
473 * This function copies up to @len bytes of data from a source pointer
474 * to a Pascal String into the buffer backend. If a terminating '\0'
475 * character is found in @src before @len characters are copied, pad the
476 * buffer with @pad characters (e.g. '\0').
477 *
478 * The length of the pascal strings in the ring buffer is explicit: it
479 * is either the array or sequence length.
480 */
481 static inline
482 void lib_ring_buffer_pstrcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
483 struct lttng_kernel_ring_buffer_ctx *ctx,
484 const char __user *src, size_t len, char pad)
485 __attribute__((always_inline));
486 static inline
487 void lib_ring_buffer_pstrcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
488 struct lttng_kernel_ring_buffer_ctx *ctx,
489 const char __user *src, size_t len, char pad)
490 {
491 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
492 struct channel_backend *chanb = &ctx->priv.chan->backend;
493 size_t index, bytes_left_in_page;
494 size_t offset = ctx->priv.buf_offset;
495 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
496
497 if (unlikely(!len))
498 return;
499 backend_pages =
500 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
501 offset &= chanb->buf_size - 1;
502 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
503 bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
504
505 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
506 goto fill_buffer;
507
508 pagefault_disable();
509 if (likely(bytes_left_in_page == len)) {
510 size_t count;
511
512 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
513 backend_pages->p[index].virt
514 + (offset & ~PAGE_MASK),
515 src, len);
516 offset += count;
517 /* Padding */
518 if (unlikely(count < len)) {
519 size_t pad_len = len - count;
520
521 lib_ring_buffer_do_memset(backend_pages->p[index].virt
522 + (offset & ~PAGE_MASK),
523 pad, pad_len);
524 offset += pad_len;
525 }
526 } else {
527 _lib_ring_buffer_pstrcpy_from_user_inatomic(bufb, offset, src, len, pad);
528 }
529 ctx->priv.buf_offset += len;
530 pagefault_enable();
531
532 return;
533
534 fill_buffer:
535 /*
536 * In the error path we call the slow path version to avoid
537 * the pollution of static inline code.
538 */
539 _lib_ring_buffer_memset(bufb, offset, pad, len);
540 ctx->priv.buf_offset += len;
541 }
542
543 /*
544 * This accessor counts the number of unread records in a buffer.
545 * It only provides a consistent value if no reads not writes are performed
546 * concurrently.
547 */
548 static inline
549 unsigned long lib_ring_buffer_get_records_unread(
550 const struct lttng_kernel_ring_buffer_config *config,
551 struct lttng_kernel_ring_buffer *buf)
552 {
553 struct lttng_kernel_ring_buffer_backend *bufb = &buf->backend;
554 struct lttng_kernel_ring_buffer_backend_pages *pages;
555 unsigned long records_unread = 0, sb_bindex, id;
556 unsigned int i;
557
558 for (i = 0; i < bufb->chan->backend.num_subbuf; i++) {
559 id = bufb->buf_wsb[i].id;
560 sb_bindex = subbuffer_id_get_index(config, id);
561 pages = bufb->array[sb_bindex];
562 records_unread += v_read(config, &pages->records_unread);
563 }
564 if (config->mode == RING_BUFFER_OVERWRITE) {
565 id = bufb->buf_rsb.id;
566 sb_bindex = subbuffer_id_get_index(config, id);
567 pages = bufb->array[sb_bindex];
568 records_unread += v_read(config, &pages->records_unread);
569 }
570 return records_unread;
571 }
572
573 /*
574 * We use __copy_from_user_inatomic to copy userspace data after
575 * checking with access_ok() and disabling page faults.
576 *
577 * Return 0 if OK, nonzero on error.
578 */
579 static inline
580 unsigned long lib_ring_buffer_copy_from_user_check_nofault(void *dest,
581 const void __user *src,
582 unsigned long len)
583 {
584 return lttng_copy_from_user_check_nofault(dest, src, len);
585 }
586
587 #endif /* _LIB_RING_BUFFER_BACKEND_H */
This page took 0.040168 seconds and 3 git commands to generate.