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