1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
5 * Ring buffer backend (API).
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
10 * the reader in flight recorder mode.
13 #ifndef _LIB_RING_BUFFER_BACKEND_H
14 #define _LIB_RING_BUFFER_BACKEND_H
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>
24 #include <wrapper/uaccess.h>
25 #include <lttng/probe-user.h>
27 /* Internal helpers */
28 #include <ringbuffer/backend_internal.h>
29 #include <ringbuffer/frontend_internal.h>
31 /* Ring buffer backend API */
33 /* Ring buffer backend access (read/write) */
35 extern size_t lib_ring_buffer_read(struct lttng_kernel_ring_buffer_backend
*bufb
,
36 size_t offset
, void *dest
, size_t len
);
38 extern int __lib_ring_buffer_copy_to_user(struct lttng_kernel_ring_buffer_backend
*bufb
,
39 size_t offset
, void __user
*dest
,
42 extern int lib_ring_buffer_read_cstr(struct lttng_kernel_ring_buffer_backend
*bufb
,
43 size_t offset
, void *dest
, size_t len
);
45 extern unsigned long *
46 lib_ring_buffer_read_get_pfn(struct lttng_kernel_ring_buffer_backend
*bufb
, size_t offset
,
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.
56 lib_ring_buffer_offset_address(struct lttng_kernel_ring_buffer_backend
*bufb
,
59 lib_ring_buffer_read_offset_address(struct lttng_kernel_ring_buffer_backend
*bufb
,
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
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.
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
)
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
;
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
),
98 _lib_ring_buffer_write(bufb
, offset
, src
, len
);
99 ctx
->priv
.buf_offset
+= len
;
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
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
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
)
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
;
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
),
137 _lib_ring_buffer_memset(bufb
, offset
, c
, len
);
138 ctx
->priv
.buf_offset
+= len
;
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.
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
)
152 for (count
= 0; count
< len
; count
++) {
156 * Only read source character once, in case it is
157 * modified concurrently.
159 c
= LTTNG_READ_ONCE(src
[count
]);
162 lib_ring_buffer_do_copy(config
, &dest
[count
], &c
, 1);
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.
173 * This function deals with userspace pointers, it should never be called
174 * directly without having the src pointer checked with access_ok()
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
)
183 for (count
= 0; count
< len
; count
++) {
187 ret
= __copy_from_user_inatomic(&c
, src
+ count
, 1);
190 lib_ring_buffer_do_copy(config
, &dest
[count
], &c
, 1);
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
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.
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
)
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
;
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
)) {
232 count
= lib_ring_buffer_do_strcpy(config
,
233 backend_pages
->p
[index
].virt
234 + (offset
& ~PAGE_MASK
),
238 if (unlikely(count
< len
- 1)) {
239 size_t pad_len
= len
- 1 - count
;
241 lib_ring_buffer_do_memset(backend_pages
->p
[index
].virt
242 + (offset
& ~PAGE_MASK
),
247 lib_ring_buffer_do_memset(backend_pages
->p
[index
].virt
248 + (offset
& ~PAGE_MASK
),
251 _lib_ring_buffer_strcpy(bufb
, offset
, src
, len
, pad
);
253 ctx
->priv
.buf_offset
+= len
;
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
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').
269 * The length of the pascal strings in the ring buffer is explicit: it
270 * is either the array or sequence length.
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
));
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
)
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
;
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
)) {
298 count
= lib_ring_buffer_do_strcpy(config
,
299 backend_pages
->p
[index
].virt
300 + (offset
& ~PAGE_MASK
),
304 if (unlikely(count
< len
)) {
305 size_t pad_len
= len
- count
;
307 lib_ring_buffer_do_memset(backend_pages
->p
[index
].virt
308 + (offset
& ~PAGE_MASK
),
313 _lib_ring_buffer_pstrcpy(bufb
, offset
, src
, len
, pad
);
315 ctx
->priv
.buf_offset
+= len
;
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
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.
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
)
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
;
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
);
351 if (unlikely(!lttng_access_ok(VERIFY_READ
, src
, len
)))
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
),
359 if (unlikely(ret
> 0)) {
361 goto fill_buffer_enable_pf
;
364 _lib_ring_buffer_copy_from_user_inatomic(bufb
, offset
, src
, len
);
367 ctx
->priv
.buf_offset
+= len
;
371 fill_buffer_enable_pf
:
375 * In the error path we call the slow path version to avoid
376 * the pollution of static inline code.
378 _lib_ring_buffer_memset(bufb
, offset
, 0, len
);
379 ctx
->priv
.buf_offset
+= len
;
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
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
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
)
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
;
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
);
419 if (unlikely(!lttng_access_ok(VERIFY_READ
, src
, len
)))
423 if (likely(bytes_left_in_page
== len
)) {
426 count
= lib_ring_buffer_do_strcpy_from_user_inatomic(config
,
427 backend_pages
->p
[index
].virt
428 + (offset
& ~PAGE_MASK
),
432 if (unlikely(count
< len
- 1)) {
433 size_t pad_len
= len
- 1 - count
;
435 lib_ring_buffer_do_memset(backend_pages
->p
[index
].virt
436 + (offset
& ~PAGE_MASK
),
441 lib_ring_buffer_do_memset(backend_pages
->p
[index
].virt
442 + (offset
& ~PAGE_MASK
),
445 _lib_ring_buffer_strcpy_from_user_inatomic(bufb
, offset
, src
,
449 ctx
->priv
.buf_offset
+= len
;
455 * In the error path we call the slow path version to avoid
456 * the pollution of static inline code.
458 _lib_ring_buffer_memset(bufb
, offset
, pad
, len
- 1);
460 _lib_ring_buffer_memset(bufb
, offset
, '\0', 1);
461 ctx
->priv
.buf_offset
+= len
;
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
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').
477 * The length of the pascal strings in the ring buffer is explicit: it
478 * is either the array or sequence length.
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
));
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
)
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
;
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
);
504 if (unlikely(!lttng_access_ok(VERIFY_READ
, src
, len
)))
508 if (likely(bytes_left_in_page
== len
)) {
511 count
= lib_ring_buffer_do_strcpy_from_user_inatomic(config
,
512 backend_pages
->p
[index
].virt
513 + (offset
& ~PAGE_MASK
),
517 if (unlikely(count
< len
)) {
518 size_t pad_len
= len
- count
;
520 lib_ring_buffer_do_memset(backend_pages
->p
[index
].virt
521 + (offset
& ~PAGE_MASK
),
526 _lib_ring_buffer_pstrcpy_from_user_inatomic(bufb
, offset
, src
, len
, pad
);
528 ctx
->priv
.buf_offset
+= len
;
535 * In the error path we call the slow path version to avoid
536 * the pollution of static inline code.
538 _lib_ring_buffer_memset(bufb
, offset
, pad
, len
);
539 ctx
->priv
.buf_offset
+= len
;
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
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
)
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
;
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
);
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
);
569 return records_unread
;
573 * We use __copy_from_user_inatomic to copy userspace data after
574 * checking with access_ok() and disabling page faults.
576 * Return 0 if OK, nonzero on error.
579 unsigned long lib_ring_buffer_copy_from_user_check_nofault(void *dest
,
580 const void __user
*src
,
583 return lttng_copy_from_user_check_nofault(dest
, src
, len
);
586 #endif /* _LIB_RING_BUFFER_BACKEND_H */