Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
2 | * | |
7b8ea3a5 MD |
3 | * lttng-probe-user.c |
4 | * | |
5 | * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7b8ea3a5 MD |
6 | */ |
7 | ||
8 | #include <linux/uaccess.h> | |
8d90c035 | 9 | #include <linux/module.h> |
80bb2600 | 10 | #include <wrapper/uaccess.h> |
156a3977 | 11 | #include <probes/lttng-probe-user.h> |
7b8ea3a5 | 12 | |
a1ffd991 MJ |
13 | #define LTTNG_MAX_USER_STRING_LEN 1048576 /* 1MB */ |
14 | ||
7b8ea3a5 MD |
15 | /* |
16 | * Calculate string length. Include final null terminating character if there is | |
17 | * one, or ends at first fault. Disabling page faults ensures that we can safely | |
18 | * call this from pretty much any context, including those where the caller | |
19 | * holds mmap_sem, or any lock which nests in mmap_sem. | |
20 | */ | |
21 | long lttng_strlen_user_inatomic(const char *addr) | |
22 | { | |
23 | long count = 0; | |
7b8ea3a5 | 24 | |
a824008c MD |
25 | if (!addr) |
26 | return 0; | |
27 | ||
7b8ea3a5 MD |
28 | pagefault_disable(); |
29 | for (;;) { | |
30 | char v; | |
2a8b83a1 | 31 | unsigned long ret; |
7b8ea3a5 | 32 | |
80bb2600 | 33 | if (unlikely(!lttng_access_ok(VERIFY_READ, |
f127e61e MD |
34 | (__force const char __user *) addr, |
35 | sizeof(v)))) | |
36 | break; | |
7b8ea3a5 MD |
37 | ret = __copy_from_user_inatomic(&v, |
38 | (__force const char __user *)(addr), | |
39 | sizeof(v)); | |
2a8b83a1 | 40 | if (unlikely(ret > 0)) |
7b8ea3a5 MD |
41 | break; |
42 | count++; | |
a1ffd991 MJ |
43 | if (unlikely(count > LTTNG_MAX_USER_STRING_LEN)) |
44 | break; | |
7b8ea3a5 MD |
45 | if (unlikely(!v)) |
46 | break; | |
47 | addr++; | |
48 | } | |
49 | pagefault_enable(); | |
7b8ea3a5 MD |
50 | return count; |
51 | } | |
f127e61e | 52 | EXPORT_SYMBOL_GPL(lttng_strlen_user_inatomic); |