Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1) |
2 | * | |
886d51a3 | 3 | * lib/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> |
6423140f | 25 | #include <probes/lttng-probe-user.h> |
f3bc08c5 MD |
26 | |
27 | /* Internal helpers */ | |
5671a661 MD |
28 | #include <wrapper/ringbuffer/backend_internal.h> |
29 | #include <wrapper/ringbuffer/frontend_internal.h> | |
f3bc08c5 MD |
30 | |
31 | /* Ring buffer backend API */ | |
32 | ||
33 | /* Ring buffer backend access (read/write) */ | |
34 | ||
35 | extern size_t lib_ring_buffer_read(struct lib_ring_buffer_backend *bufb, | |
36 | size_t offset, void *dest, size_t len); | |
37 | ||
38 | extern int __lib_ring_buffer_copy_to_user(struct lib_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 lib_ring_buffer_backend *bufb, | |
43 | size_t offset, void *dest, size_t len); | |
44 | ||
0112cb7b MD |
45 | extern unsigned long * |
46 | lib_ring_buffer_read_get_pfn(struct lib_ring_buffer_backend *bufb, size_t offset, | |
f3bc08c5 MD |
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 lib_ring_buffer_backend *bufb, | |
57 | size_t offset); | |
58 | extern void * | |
59 | lib_ring_buffer_read_offset_address(struct lib_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 | */ | |
8617eb9a | 74 | static inline __attribute__((always_inline)) |
f3bc08c5 MD |
75 | void lib_ring_buffer_write(const struct lib_ring_buffer_config *config, |
76 | struct lib_ring_buffer_ctx *ctx, | |
77 | const void *src, size_t len) | |
78 | { | |
79 | struct lib_ring_buffer_backend *bufb = &ctx->buf->backend; | |
80 | struct channel_backend *chanb = &ctx->chan->backend; | |
85a07c33 | 81 | size_t index, pagecpy; |
f3bc08c5 | 82 | size_t offset = ctx->buf_offset; |
85a07c33 | 83 | struct lib_ring_buffer_backend_pages *backend_pages; |
f3bc08c5 | 84 | |
61eb4c39 MD |
85 | if (unlikely(!len)) |
86 | return; | |
85a07c33 MD |
87 | backend_pages = |
88 | lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); | |
f3bc08c5 | 89 | offset &= chanb->buf_size - 1; |
f3bc08c5 MD |
90 | index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT; |
91 | pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK); | |
f3bc08c5 MD |
92 | if (likely(pagecpy == len)) |
93 | lib_ring_buffer_do_copy(config, | |
85a07c33 | 94 | backend_pages->p[index].virt |
f3bc08c5 MD |
95 | + (offset & ~PAGE_MASK), |
96 | src, len); | |
97 | else | |
98 | _lib_ring_buffer_write(bufb, offset, src, len, 0); | |
99 | ctx->buf_offset += len; | |
100 | } | |
101 | ||
4ea00e4f JD |
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 lib_ring_buffer_config *config, | |
117 | struct lib_ring_buffer_ctx *ctx, int c, size_t len) | |
118 | { | |
119 | ||
120 | struct lib_ring_buffer_backend *bufb = &ctx->buf->backend; | |
121 | struct channel_backend *chanb = &ctx->chan->backend; | |
85a07c33 | 122 | size_t index, pagecpy; |
4ea00e4f | 123 | size_t offset = ctx->buf_offset; |
85a07c33 | 124 | struct lib_ring_buffer_backend_pages *backend_pages; |
4ea00e4f | 125 | |
61eb4c39 MD |
126 | if (unlikely(!len)) |
127 | return; | |
85a07c33 MD |
128 | backend_pages = |
129 | lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); | |
4ea00e4f | 130 | offset &= chanb->buf_size - 1; |
4ea00e4f JD |
131 | index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT; |
132 | pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK); | |
4ea00e4f | 133 | if (likely(pagecpy == len)) |
85a07c33 | 134 | lib_ring_buffer_do_memset(backend_pages->p[index].virt |
4ea00e4f JD |
135 | + (offset & ~PAGE_MASK), |
136 | c, len); | |
137 | else | |
138 | _lib_ring_buffer_memset(bufb, offset, c, len, 0); | |
139 | ctx->buf_offset += len; | |
140 | } | |
141 | ||
16f78f3a MD |
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 | */ | |
d723e905 | 147 | static inline __attribute__((always_inline)) |
16f78f3a MD |
148 | size_t lib_ring_buffer_do_strcpy(const struct lib_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 | */ | |
57ccbfa6 | 160 | c = LTTNG_READ_ONCE(src[count]); |
16f78f3a MD |
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 | */ | |
d723e905 | 178 | static inline __attribute__((always_inline)) |
16f78f3a MD |
179 | size_t lib_ring_buffer_do_strcpy_from_user_inatomic(const struct lib_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 | ||
f127e61e | 188 | ret = __copy_from_user_inatomic(&c, src + count, 1); |
16f78f3a MD |
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 lib_ring_buffer_config *config, | |
214 | struct lib_ring_buffer_ctx *ctx, | |
215 | const char *src, size_t len, int pad) | |
216 | { | |
217 | struct lib_ring_buffer_backend *bufb = &ctx->buf->backend; | |
218 | struct channel_backend *chanb = &ctx->chan->backend; | |
85a07c33 | 219 | size_t index, pagecpy; |
16f78f3a | 220 | size_t offset = ctx->buf_offset; |
85a07c33 | 221 | struct lib_ring_buffer_backend_pages *backend_pages; |
16f78f3a MD |
222 | |
223 | if (unlikely(!len)) | |
224 | return; | |
85a07c33 MD |
225 | backend_pages = |
226 | lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); | |
16f78f3a | 227 | offset &= chanb->buf_size - 1; |
16f78f3a MD |
228 | index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT; |
229 | pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK); | |
16f78f3a MD |
230 | if (likely(pagecpy == len)) { |
231 | size_t count; | |
232 | ||
233 | count = lib_ring_buffer_do_strcpy(config, | |
85a07c33 | 234 | backend_pages->p[index].virt |
16f78f3a MD |
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 | ||
85a07c33 | 242 | lib_ring_buffer_do_memset(backend_pages->p[index].virt |
16f78f3a MD |
243 | + (offset & ~PAGE_MASK), |
244 | pad, pad_len); | |
245 | offset += pad_len; | |
246 | } | |
247 | /* Ending '\0' */ | |
85a07c33 | 248 | lib_ring_buffer_do_memset(backend_pages->p[index].virt |
16f78f3a MD |
249 | + (offset & ~PAGE_MASK), |
250 | '\0', 1); | |
251 | } else { | |
252 | _lib_ring_buffer_strcpy(bufb, offset, src, len, 0, pad); | |
253 | } | |
254 | ctx->buf_offset += len; | |
255 | } | |
256 | ||
4ea00e4f | 257 | /** |
7b8ea3a5 | 258 | * lib_ring_buffer_copy_from_user_inatomic - write userspace data to a buffer backend |
4ea00e4f JD |
259 | * @config : ring buffer instance configuration |
260 | * @ctx: ring buffer context. (input arguments only) | |
261 | * @src : userspace source pointer to copy from | |
262 | * @len : length of data to copy | |
263 | * | |
264 | * This function copies "len" bytes of data from a userspace pointer to a | |
265 | * buffer backend, at the current context offset. This is more or less a buffer | |
266 | * backend-specific memcpy() operation. Calls the slow path | |
7b8ea3a5 MD |
267 | * (_ring_buffer_write_from_user_inatomic) if copy is crossing a page boundary. |
268 | * Disable the page fault handler to ensure we never try to take the mmap_sem. | |
4ea00e4f | 269 | */ |
d723e905 | 270 | static inline __attribute__((always_inline)) |
7b8ea3a5 | 271 | void lib_ring_buffer_copy_from_user_inatomic(const struct lib_ring_buffer_config *config, |
4ea00e4f JD |
272 | struct lib_ring_buffer_ctx *ctx, |
273 | const void __user *src, size_t len) | |
274 | { | |
275 | struct lib_ring_buffer_backend *bufb = &ctx->buf->backend; | |
276 | struct channel_backend *chanb = &ctx->chan->backend; | |
85a07c33 | 277 | size_t index, pagecpy; |
4ea00e4f | 278 | size_t offset = ctx->buf_offset; |
85a07c33 | 279 | struct lib_ring_buffer_backend_pages *backend_pages; |
4ea00e4f JD |
280 | unsigned long ret; |
281 | ||
61eb4c39 MD |
282 | if (unlikely(!len)) |
283 | return; | |
85a07c33 MD |
284 | backend_pages = |
285 | lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); | |
4ea00e4f | 286 | offset &= chanb->buf_size - 1; |
4ea00e4f JD |
287 | index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT; |
288 | pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK); | |
4ea00e4f | 289 | |
7b8ea3a5 | 290 | pagefault_disable(); |
80bb2600 | 291 | if (unlikely(!lttng_access_ok(VERIFY_READ, src, len))) |
4ea00e4f JD |
292 | goto fill_buffer; |
293 | ||
294 | if (likely(pagecpy == len)) { | |
7b8ea3a5 | 295 | ret = lib_ring_buffer_do_copy_from_user_inatomic( |
85a07c33 | 296 | backend_pages->p[index].virt + (offset & ~PAGE_MASK), |
4ea00e4f JD |
297 | src, len); |
298 | if (unlikely(ret > 0)) { | |
d87a9f03 | 299 | /* Copy failed. */ |
4ea00e4f JD |
300 | goto fill_buffer; |
301 | } | |
302 | } else { | |
7b8ea3a5 | 303 | _lib_ring_buffer_copy_from_user_inatomic(bufb, offset, src, len, 0); |
4ea00e4f | 304 | } |
7b8ea3a5 | 305 | pagefault_enable(); |
4ea00e4f JD |
306 | ctx->buf_offset += len; |
307 | ||
308 | return; | |
309 | ||
310 | fill_buffer: | |
7b8ea3a5 | 311 | pagefault_enable(); |
4ea00e4f JD |
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); | |
8bbb8c9d | 317 | ctx->buf_offset += len; |
4ea00e4f JD |
318 | } |
319 | ||
16f78f3a MD |
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 lib_ring_buffer_config *config, | |
340 | struct lib_ring_buffer_ctx *ctx, | |
341 | const void __user *src, size_t len, int pad) | |
342 | { | |
343 | struct lib_ring_buffer_backend *bufb = &ctx->buf->backend; | |
344 | struct channel_backend *chanb = &ctx->chan->backend; | |
85a07c33 | 345 | size_t index, pagecpy; |
16f78f3a | 346 | size_t offset = ctx->buf_offset; |
85a07c33 | 347 | struct lib_ring_buffer_backend_pages *backend_pages; |
16f78f3a MD |
348 | |
349 | if (unlikely(!len)) | |
350 | return; | |
85a07c33 MD |
351 | backend_pages = |
352 | lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); | |
16f78f3a | 353 | offset &= chanb->buf_size - 1; |
16f78f3a MD |
354 | index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT; |
355 | pagecpy = min_t(size_t, len, (-offset) & ~PAGE_MASK); | |
16f78f3a | 356 | |
16f78f3a | 357 | pagefault_disable(); |
80bb2600 | 358 | if (unlikely(!lttng_access_ok(VERIFY_READ, src, len))) |
16f78f3a MD |
359 | goto fill_buffer; |
360 | ||
361 | if (likely(pagecpy == len)) { | |
362 | size_t count; | |
363 | ||
364 | count = lib_ring_buffer_do_strcpy_from_user_inatomic(config, | |
85a07c33 | 365 | backend_pages->p[index].virt |
16f78f3a MD |
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 | ||
85a07c33 | 373 | lib_ring_buffer_do_memset(backend_pages->p[index].virt |
16f78f3a MD |
374 | + (offset & ~PAGE_MASK), |
375 | pad, pad_len); | |
376 | offset += pad_len; | |
377 | } | |
378 | /* Ending '\0' */ | |
85a07c33 | 379 | lib_ring_buffer_do_memset(backend_pages->p[index].virt |
16f78f3a MD |
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(); | |
16f78f3a MD |
387 | ctx->buf_offset += len; |
388 | ||
389 | return; | |
390 | ||
391 | fill_buffer: | |
392 | pagefault_enable(); | |
16f78f3a MD |
393 | /* |
394 | * In the error path we call the slow path version to avoid | |
395 | * the pollution of static inline code. | |
396 | */ | |
397 | _lib_ring_buffer_memset(bufb, offset, pad, len - 1, 0); | |
398 | offset += len - 1; | |
399 | _lib_ring_buffer_memset(bufb, offset, '\0', 1, 0); | |
8bbb8c9d | 400 | ctx->buf_offset += len; |
16f78f3a MD |
401 | } |
402 | ||
f3bc08c5 MD |
403 | /* |
404 | * This accessor counts the number of unread records in a buffer. | |
405 | * It only provides a consistent value if no reads not writes are performed | |
406 | * concurrently. | |
407 | */ | |
408 | static inline | |
409 | unsigned long lib_ring_buffer_get_records_unread( | |
410 | const struct lib_ring_buffer_config *config, | |
411 | struct lib_ring_buffer *buf) | |
412 | { | |
413 | struct lib_ring_buffer_backend *bufb = &buf->backend; | |
414 | struct lib_ring_buffer_backend_pages *pages; | |
415 | unsigned long records_unread = 0, sb_bindex, id; | |
416 | unsigned int i; | |
417 | ||
418 | for (i = 0; i < bufb->chan->backend.num_subbuf; i++) { | |
419 | id = bufb->buf_wsb[i].id; | |
420 | sb_bindex = subbuffer_id_get_index(config, id); | |
421 | pages = bufb->array[sb_bindex]; | |
422 | records_unread += v_read(config, &pages->records_unread); | |
423 | } | |
424 | if (config->mode == RING_BUFFER_OVERWRITE) { | |
425 | id = bufb->buf_rsb.id; | |
426 | sb_bindex = subbuffer_id_get_index(config, id); | |
427 | pages = bufb->array[sb_bindex]; | |
428 | records_unread += v_read(config, &pages->records_unread); | |
429 | } | |
430 | return records_unread; | |
431 | } | |
432 | ||
3c8ebbc8 MD |
433 | /* |
434 | * We use __copy_from_user_inatomic to copy userspace data after | |
435 | * checking with access_ok() and disabling page faults. | |
436 | * | |
437 | * Return 0 if OK, nonzero on error. | |
438 | */ | |
439 | static inline | |
440 | unsigned long lib_ring_buffer_copy_from_user_check_nofault(void *dest, | |
441 | const void __user *src, | |
442 | unsigned long len) | |
443 | { | |
6423140f | 444 | return lttng_copy_from_user_check_nofault(dest, src, len); |
3c8ebbc8 MD |
445 | } |
446 | ||
886d51a3 | 447 | #endif /* _LIB_RING_BUFFER_BACKEND_H */ |