Cleanup: disable page fault after access_ok
[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, pagecpy;
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 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
229 if (likely(pagecpy == 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, 0, pad);
252 }
253 ctx->priv.buf_offset += len;
254 }
255
256 /**
257 * lib_ring_buffer_copy_from_user_inatomic - write userspace data to a buffer backend
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
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.
268 */
269 static inline __attribute__((always_inline))
270 void lib_ring_buffer_copy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
271 struct lttng_kernel_ring_buffer_ctx *ctx,
272 const void __user *src, size_t len)
273 {
274 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
275 struct channel_backend *chanb = &ctx->priv.chan->backend;
276 size_t index, pagecpy;
277 size_t offset = ctx->priv.buf_offset;
278 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
279 unsigned long ret;
280
281 if (unlikely(!len))
282 return;
283 backend_pages =
284 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
285 offset &= chanb->buf_size - 1;
286 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
287 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
288
289 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
290 goto fill_buffer;
291
292 pagefault_disable();
293 if (likely(pagecpy == len)) {
294 ret = lib_ring_buffer_do_copy_from_user_inatomic(
295 backend_pages->p[index].virt + (offset & ~PAGE_MASK),
296 src, len);
297 if (unlikely(ret > 0)) {
298 /* Copy failed. */
299 goto fill_buffer_enable_pf;
300 }
301 } else {
302 _lib_ring_buffer_copy_from_user_inatomic(bufb, offset, src, len, 0);
303 }
304 pagefault_enable();
305 ctx->priv.buf_offset += len;
306
307 return;
308
309 fill_buffer_enable_pf:
310 pagefault_enable();
311 fill_buffer:
312 /*
313 * In the error path we call the slow path version to avoid
314 * the pollution of static inline code.
315 */
316 _lib_ring_buffer_memset(bufb, offset, 0, len, 0);
317 ctx->priv.buf_offset += len;
318 }
319
320 /**
321 * lib_ring_buffer_strcpy_from_user_inatomic - write userspace string data to a buffer backend
322 * @config : ring buffer instance configuration
323 * @ctx: ring buffer context (input arguments only)
324 * @src : userspace source pointer to copy from
325 * @len : length of data to copy
326 * @pad : character to use for padding
327 *
328 * This function copies @len - 1 bytes of string data from a userspace
329 * source pointer to a buffer backend, followed by a terminating '\0'
330 * character, at the current context offset. This is more or less a
331 * buffer backend-specific strncpy() operation. If a terminating '\0'
332 * character is found in @src before @len - 1 characters are copied, pad
333 * the buffer with @pad characters (e.g. '#'). Calls the slow path
334 * (_ring_buffer_strcpy_from_user_inatomic) if copy is crossing a page
335 * boundary. Disable the page fault handler to ensure we never try to
336 * take the mmap_sem.
337 */
338 static inline
339 void lib_ring_buffer_strcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
340 struct lttng_kernel_ring_buffer_ctx *ctx,
341 const void __user *src, size_t len, int pad)
342 {
343 struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
344 struct channel_backend *chanb = &ctx->priv.chan->backend;
345 size_t index, pagecpy;
346 size_t offset = ctx->priv.buf_offset;
347 struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
348
349 if (unlikely(!len))
350 return;
351 backend_pages =
352 lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
353 offset &= chanb->buf_size - 1;
354 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
355 pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK);
356
357 if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
358 goto fill_buffer;
359
360 pagefault_disable();
361 if (likely(pagecpy == len)) {
362 size_t count;
363
364 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
365 backend_pages->p[index].virt
366 + (offset & ~PAGE_MASK),
367 src, len - 1);
368 offset += count;
369 /* Padding */
370 if (unlikely(count < len - 1)) {
371 size_t pad_len = len - 1 - count;
372
373 lib_ring_buffer_do_memset(backend_pages->p[index].virt
374 + (offset & ~PAGE_MASK),
375 pad, pad_len);
376 offset += pad_len;
377 }
378 /* Ending '\0' */
379 lib_ring_buffer_do_memset(backend_pages->p[index].virt
380 + (offset & ~PAGE_MASK),
381 '\0', 1);
382 } else {
383 _lib_ring_buffer_strcpy_from_user_inatomic(bufb, offset, src,
384 len, 0, pad);
385 }
386 pagefault_enable();
387 ctx->priv.buf_offset += len;
388
389 return;
390
391 fill_buffer:
392 /*
393 * In the error path we call the slow path version to avoid
394 * the pollution of static inline code.
395 */
396 _lib_ring_buffer_memset(bufb, offset, pad, len - 1, 0);
397 offset += len - 1;
398 _lib_ring_buffer_memset(bufb, offset, '\0', 1, 0);
399 ctx->priv.buf_offset += len;
400 }
401
402 /*
403 * This accessor counts the number of unread records in a buffer.
404 * It only provides a consistent value if no reads not writes are performed
405 * concurrently.
406 */
407 static inline
408 unsigned long lib_ring_buffer_get_records_unread(
409 const struct lttng_kernel_ring_buffer_config *config,
410 struct lttng_kernel_ring_buffer *buf)
411 {
412 struct lttng_kernel_ring_buffer_backend *bufb = &buf->backend;
413 struct lttng_kernel_ring_buffer_backend_pages *pages;
414 unsigned long records_unread = 0, sb_bindex, id;
415 unsigned int i;
416
417 for (i = 0; i < bufb->chan->backend.num_subbuf; i++) {
418 id = bufb->buf_wsb[i].id;
419 sb_bindex = subbuffer_id_get_index(config, id);
420 pages = bufb->array[sb_bindex];
421 records_unread += v_read(config, &pages->records_unread);
422 }
423 if (config->mode == RING_BUFFER_OVERWRITE) {
424 id = bufb->buf_rsb.id;
425 sb_bindex = subbuffer_id_get_index(config, id);
426 pages = bufb->array[sb_bindex];
427 records_unread += v_read(config, &pages->records_unread);
428 }
429 return records_unread;
430 }
431
432 /*
433 * We use __copy_from_user_inatomic to copy userspace data after
434 * checking with access_ok() and disabling page faults.
435 *
436 * Return 0 if OK, nonzero on error.
437 */
438 static inline
439 unsigned long lib_ring_buffer_copy_from_user_check_nofault(void *dest,
440 const void __user *src,
441 unsigned long len)
442 {
443 unsigned long ret;
444
445 if (!lttng_access_ok(VERIFY_READ, src, len))
446 return 1;
447 pagefault_disable();
448 ret = __copy_from_user_inatomic(dest, src, len);
449 pagefault_enable();
450 return ret;
451 }
452
453 #endif /* _LIB_RING_BUFFER_BACKEND_H */
This page took 0.038987 seconds and 4 git commands to generate.