Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 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> |
3b4aafcb | 11 | #include <lttng/probe-user.h> |
7b8ea3a5 | 12 | |
eb94dcd9 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; | |
a824008c | 24 | mm_segment_t old_fs; |
7b8ea3a5 | 25 | |
a824008c MD |
26 | if (!addr) |
27 | return 0; | |
28 | ||
29 | old_fs = get_fs(); | |
7b8ea3a5 MD |
30 | set_fs(KERNEL_DS); |
31 | pagefault_disable(); | |
32 | for (;;) { | |
33 | char v; | |
2a8b83a1 | 34 | unsigned long ret; |
7b8ea3a5 | 35 | |
80bb2600 | 36 | if (unlikely(!lttng_access_ok(VERIFY_READ, |
f127e61e MD |
37 | (__force const char __user *) addr, |
38 | sizeof(v)))) | |
39 | break; | |
7b8ea3a5 MD |
40 | ret = __copy_from_user_inatomic(&v, |
41 | (__force const char __user *)(addr), | |
42 | sizeof(v)); | |
2a8b83a1 | 43 | if (unlikely(ret > 0)) |
7b8ea3a5 MD |
44 | break; |
45 | count++; | |
eb94dcd9 MJ |
46 | if (unlikely(count > LTTNG_MAX_USER_STRING_LEN)) |
47 | break; | |
7b8ea3a5 MD |
48 | if (unlikely(!v)) |
49 | break; | |
50 | addr++; | |
51 | } | |
52 | pagefault_enable(); | |
53 | set_fs(old_fs); | |
54 | return count; | |
55 | } | |
f127e61e | 56 | EXPORT_SYMBOL_GPL(lttng_strlen_user_inatomic); |