Rename struct lib_ring_buffer_ctx to struct lttng_kernel_ring_buffer_ctx
[lttng-modules.git] / include / ringbuffer / backend.h
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
9f36eaed 2 *
24591303 3 * ringbuffer/backend.h
f3bc08c5
MD
4 *
5 * Ring buffer backend (API).
6 *
886d51a3
MD
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
f3bc08c5
MD
9 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
10 * the reader in flight recorder mode.
11 */
12
9f36eaed
MJ
13#ifndef _LIB_RING_BUFFER_BACKEND_H
14#define _LIB_RING_BUFFER_BACKEND_H
15
f3bc08c5
MD
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>
80bb2600 24#include <wrapper/uaccess.h>
f3bc08c5
MD
25
26/* Internal helpers */
24591303
MD
27#include <ringbuffer/backend_internal.h>
28#include <ringbuffer/frontend_internal.h>
f3bc08c5
MD
29
30/* Ring buffer backend API */
31
32/* Ring buffer backend access (read/write) */
33
34extern size_t lib_ring_buffer_read(struct lib_ring_buffer_backend *bufb,
35 size_t offset, void *dest, size_t len);
36
37extern int __lib_ring_buffer_copy_to_user(struct lib_ring_buffer_backend *bufb,
38 size_t offset, void __user *dest,
39 size_t len);
40
41extern int lib_ring_buffer_read_cstr(struct lib_ring_buffer_backend *bufb,
42 size_t offset, void *dest, size_t len);
43
0112cb7b
MD
44extern unsigned long *
45lib_ring_buffer_read_get_pfn(struct lib_ring_buffer_backend *bufb, size_t offset,
f3bc08c5
MD
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 */
54extern void *
55lib_ring_buffer_offset_address(struct lib_ring_buffer_backend *bufb,
56 size_t offset);
57extern void *
58lib_ring_buffer_read_offset_address(struct lib_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 */
8617eb9a 73static inline __attribute__((always_inline))
f3bc08c5 74void lib_ring_buffer_write(const struct lib_ring_buffer_config *config,
8a57ec02 75 struct lttng_kernel_ring_buffer_ctx *ctx,
f3bc08c5
MD
76 const void *src, size_t len)
77{
b1199bd3
MD
78 struct lib_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
79 struct channel_backend *chanb = &ctx->priv.chan->backend;
85a07c33 80 size_t index, pagecpy;
b1199bd3 81 size_t offset = ctx->priv.buf_offset;
85a07c33 82 struct lib_ring_buffer_backend_pages *backend_pages;
f3bc08c5 83
61eb4c39
MD
84 if (unlikely(!len))
85 return;
85a07c33
MD
86 backend_pages =
87 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
f3bc08c5 88 offset &= chanb->buf_size - 1;
f3bc08c5
MD
89 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
90 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
f3bc08c5
MD
91 if (likely(pagecpy == len))
92 lib_ring_buffer_do_copy(config,
85a07c33 93 backend_pages->p[index].virt
f3bc08c5
MD
94 + (offset & ~PAGE_MASK),
95 src, len);
96 else
97 _lib_ring_buffer_write(bufb, offset, src, len, 0);
b1199bd3 98 ctx->priv.buf_offset += len;
f3bc08c5
MD
99}
100
4ea00e4f
JD
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 */
114static inline
115void lib_ring_buffer_memset(const struct lib_ring_buffer_config *config,
8a57ec02 116 struct lttng_kernel_ring_buffer_ctx *ctx, int c, size_t len)
4ea00e4f
JD
117{
118
b1199bd3
MD
119 struct lib_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
120 struct channel_backend *chanb = &ctx->priv.chan->backend;
85a07c33 121 size_t index, pagecpy;
b1199bd3 122 size_t offset = ctx->priv.buf_offset;
85a07c33 123 struct lib_ring_buffer_backend_pages *backend_pages;
4ea00e4f 124
61eb4c39
MD
125 if (unlikely(!len))
126 return;
85a07c33
MD
127 backend_pages =
128 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
4ea00e4f 129 offset &= chanb->buf_size - 1;
4ea00e4f
JD
130 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
131 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
4ea00e4f 132 if (likely(pagecpy == len))
85a07c33 133 lib_ring_buffer_do_memset(backend_pages->p[index].virt
4ea00e4f
JD
134 + (offset & ~PAGE_MASK),
135 c, len);
136 else
137 _lib_ring_buffer_memset(bufb, offset, c, len, 0);
b1199bd3 138 ctx->priv.buf_offset += len;
4ea00e4f
JD
139}
140
16f78f3a
MD
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 */
d723e905 146static inline __attribute__((always_inline))
16f78f3a
MD
147size_t lib_ring_buffer_do_strcpy(const struct lib_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 */
585e5dcc 159 c = LTTNG_READ_ONCE(src[count]);
16f78f3a
MD
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 */
d723e905 177static inline __attribute__((always_inline))
16f78f3a
MD
178size_t lib_ring_buffer_do_strcpy_from_user_inatomic(const struct lib_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
f127e61e 187 ret = __copy_from_user_inatomic(&c, src + count, 1);
16f78f3a
MD
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 */
211static inline
212void lib_ring_buffer_strcpy(const struct lib_ring_buffer_config *config,
8a57ec02 213 struct lttng_kernel_ring_buffer_ctx *ctx,
16f78f3a
MD
214 const char *src, size_t len, int pad)
215{
b1199bd3
MD
216 struct lib_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
217 struct channel_backend *chanb = &ctx->priv.chan->backend;
85a07c33 218 size_t index, pagecpy;
b1199bd3 219 size_t offset = ctx->priv.buf_offset;
85a07c33 220 struct lib_ring_buffer_backend_pages *backend_pages;
16f78f3a
MD
221
222 if (unlikely(!len))
223 return;
85a07c33
MD
224 backend_pages =
225 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
16f78f3a 226 offset &= chanb->buf_size - 1;
16f78f3a
MD
227 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
228 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
16f78f3a
MD
229 if (likely(pagecpy == len)) {
230 size_t count;
231
232 count = lib_ring_buffer_do_strcpy(config,
85a07c33 233 backend_pages->p[index].virt
16f78f3a
MD
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
85a07c33 241 lib_ring_buffer_do_memset(backend_pages->p[index].virt
16f78f3a
MD
242 + (offset & ~PAGE_MASK),
243 pad, pad_len);
244 offset += pad_len;
245 }
246 /* Ending '\0' */
85a07c33 247 lib_ring_buffer_do_memset(backend_pages->p[index].virt
16f78f3a
MD
248 + (offset & ~PAGE_MASK),
249 '\0', 1);
250 } else {
251 _lib_ring_buffer_strcpy(bufb, offset, src, len, 0, pad);
252 }
b1199bd3 253 ctx->priv.buf_offset += len;
16f78f3a
MD
254}
255
4ea00e4f 256/**
7b8ea3a5 257 * lib_ring_buffer_copy_from_user_inatomic - write userspace data to a buffer backend
4ea00e4f
JD
258 * @config : ring buffer instance configuration
259 * @ctx: ring buffer context. (input arguments only)
260 * @src : userspace source pointer to copy from
261 * @len : length of data to copy
262 *
263 * This function copies "len" bytes of data from a userspace pointer to a
264 * buffer backend, at the current context offset. This is more or less a buffer
265 * backend-specific memcpy() operation. Calls the slow path
7b8ea3a5
MD
266 * (_ring_buffer_write_from_user_inatomic) if copy is crossing a page boundary.
267 * Disable the page fault handler to ensure we never try to take the mmap_sem.
4ea00e4f 268 */
d723e905 269static inline __attribute__((always_inline))
7b8ea3a5 270void lib_ring_buffer_copy_from_user_inatomic(const struct lib_ring_buffer_config *config,
8a57ec02 271 struct lttng_kernel_ring_buffer_ctx *ctx,
4ea00e4f
JD
272 const void __user *src, size_t len)
273{
b1199bd3
MD
274 struct lib_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
275 struct channel_backend *chanb = &ctx->priv.chan->backend;
85a07c33 276 size_t index, pagecpy;
b1199bd3 277 size_t offset = ctx->priv.buf_offset;
85a07c33 278 struct lib_ring_buffer_backend_pages *backend_pages;
4ea00e4f
JD
279 unsigned long ret;
280
61eb4c39
MD
281 if (unlikely(!len))
282 return;
85a07c33
MD
283 backend_pages =
284 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
4ea00e4f 285 offset &= chanb->buf_size - 1;
4ea00e4f
JD
286 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
287 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
4ea00e4f 288
7b8ea3a5 289 pagefault_disable();
80bb2600 290 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
4ea00e4f
JD
291 goto fill_buffer;
292
293 if (likely(pagecpy == len)) {
7b8ea3a5 294 ret = lib_ring_buffer_do_copy_from_user_inatomic(
85a07c33 295 backend_pages->p[index].virt + (offset & ~PAGE_MASK),
4ea00e4f
JD
296 src, len);
297 if (unlikely(ret > 0)) {
d87a9f03 298 /* Copy failed. */
4ea00e4f
JD
299 goto fill_buffer;
300 }
301 } else {
7b8ea3a5 302 _lib_ring_buffer_copy_from_user_inatomic(bufb, offset, src, len, 0);
4ea00e4f 303 }
7b8ea3a5 304 pagefault_enable();
b1199bd3 305 ctx->priv.buf_offset += len;
4ea00e4f
JD
306
307 return;
308
309fill_buffer:
7b8ea3a5 310 pagefault_enable();
4ea00e4f
JD
311 /*
312 * In the error path we call the slow path version to avoid
313 * the pollution of static inline code.
314 */
315 _lib_ring_buffer_memset(bufb, offset, 0, len, 0);
316}
317
16f78f3a
MD
318/**
319 * lib_ring_buffer_strcpy_from_user_inatomic - write userspace string 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 * @pad : character to use for padding
325 *
326 * This function copies @len - 1 bytes of string data from a userspace
327 * source pointer to a buffer backend, followed by a terminating '\0'
328 * character, at the current context offset. This is more or less a
329 * buffer backend-specific strncpy() operation. If a terminating '\0'
330 * character is found in @src before @len - 1 characters are copied, pad
331 * the buffer with @pad characters (e.g. '#'). Calls the slow path
332 * (_ring_buffer_strcpy_from_user_inatomic) if copy is crossing a page
333 * boundary. Disable the page fault handler to ensure we never try to
334 * take the mmap_sem.
335 */
336static inline
337void lib_ring_buffer_strcpy_from_user_inatomic(const struct lib_ring_buffer_config *config,
8a57ec02 338 struct lttng_kernel_ring_buffer_ctx *ctx,
16f78f3a
MD
339 const void __user *src, size_t len, int pad)
340{
b1199bd3
MD
341 struct lib_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
342 struct channel_backend *chanb = &ctx->priv.chan->backend;
85a07c33 343 size_t index, pagecpy;
b1199bd3 344 size_t offset = ctx->priv.buf_offset;
85a07c33 345 struct lib_ring_buffer_backend_pages *backend_pages;
16f78f3a
MD
346
347 if (unlikely(!len))
348 return;
85a07c33
MD
349 backend_pages =
350 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
16f78f3a 351 offset &= chanb->buf_size - 1;
16f78f3a
MD
352 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
353 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
16f78f3a 354
16f78f3a 355 pagefault_disable();
80bb2600 356 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
16f78f3a
MD
357 goto fill_buffer;
358
359 if (likely(pagecpy == len)) {
360 size_t count;
361
362 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
85a07c33 363 backend_pages->p[index].virt
16f78f3a
MD
364 + (offset & ~PAGE_MASK),
365 src, len - 1);
366 offset += count;
367 /* Padding */
368 if (unlikely(count < len - 1)) {
369 size_t pad_len = len - 1 - count;
370
85a07c33 371 lib_ring_buffer_do_memset(backend_pages->p[index].virt
16f78f3a
MD
372 + (offset & ~PAGE_MASK),
373 pad, pad_len);
374 offset += pad_len;
375 }
376 /* Ending '\0' */
85a07c33 377 lib_ring_buffer_do_memset(backend_pages->p[index].virt
16f78f3a
MD
378 + (offset & ~PAGE_MASK),
379 '\0', 1);
380 } else {
381 _lib_ring_buffer_strcpy_from_user_inatomic(bufb, offset, src,
382 len, 0, pad);
383 }
384 pagefault_enable();
b1199bd3 385 ctx->priv.buf_offset += len;
16f78f3a
MD
386
387 return;
388
389fill_buffer:
390 pagefault_enable();
16f78f3a
MD
391 /*
392 * In the error path we call the slow path version to avoid
393 * the pollution of static inline code.
394 */
395 _lib_ring_buffer_memset(bufb, offset, pad, len - 1, 0);
396 offset += len - 1;
397 _lib_ring_buffer_memset(bufb, offset, '\0', 1, 0);
398}
399
f3bc08c5
MD
400/*
401 * This accessor counts the number of unread records in a buffer.
402 * It only provides a consistent value if no reads not writes are performed
403 * concurrently.
404 */
405static inline
406unsigned long lib_ring_buffer_get_records_unread(
407 const struct lib_ring_buffer_config *config,
408 struct lib_ring_buffer *buf)
409{
410 struct lib_ring_buffer_backend *bufb = &buf->backend;
411 struct lib_ring_buffer_backend_pages *pages;
412 unsigned long records_unread = 0, sb_bindex, id;
413 unsigned int i;
414
415 for (i = 0; i < bufb->chan->backend.num_subbuf; i++) {
416 id = bufb->buf_wsb[i].id;
417 sb_bindex = subbuffer_id_get_index(config, id);
418 pages = bufb->array[sb_bindex];
419 records_unread += v_read(config, &pages->records_unread);
420 }
421 if (config->mode == RING_BUFFER_OVERWRITE) {
422 id = bufb->buf_rsb.id;
423 sb_bindex = subbuffer_id_get_index(config, id);
424 pages = bufb->array[sb_bindex];
425 records_unread += v_read(config, &pages->records_unread);
426 }
427 return records_unread;
428}
429
3c8ebbc8
MD
430/*
431 * We use __copy_from_user_inatomic to copy userspace data after
432 * checking with access_ok() and disabling page faults.
433 *
434 * Return 0 if OK, nonzero on error.
435 */
436static inline
437unsigned long lib_ring_buffer_copy_from_user_check_nofault(void *dest,
438 const void __user *src,
439 unsigned long len)
440{
441 unsigned long ret;
3c8ebbc8 442
80bb2600 443 if (!lttng_access_ok(VERIFY_READ, src, len))
3c8ebbc8 444 return 1;
3c8ebbc8
MD
445 pagefault_disable();
446 ret = __copy_from_user_inatomic(dest, src, len);
447 pagefault_enable();
3c8ebbc8
MD
448 return ret;
449}
450
886d51a3 451#endif /* _LIB_RING_BUFFER_BACKEND_H */
This page took 0.06453 seconds and 4 git commands to generate.