| 1 | /* |
| 2 | * lttng-probe-user.c |
| 3 | * |
| 4 | * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; only |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <probes/lttng-probe-user.h> |
| 24 | |
| 25 | /* |
| 26 | * Calculate string length. Include final null terminating character if there is |
| 27 | * one, or ends at first fault. Disabling page faults ensures that we can safely |
| 28 | * call this from pretty much any context, including those where the caller |
| 29 | * holds mmap_sem, or any lock which nests in mmap_sem. |
| 30 | */ |
| 31 | long lttng_strlen_user_inatomic(const char *addr) |
| 32 | { |
| 33 | long count = 0; |
| 34 | mm_segment_t old_fs; |
| 35 | |
| 36 | if (!addr) |
| 37 | return 0; |
| 38 | |
| 39 | old_fs = get_fs(); |
| 40 | set_fs(KERNEL_DS); |
| 41 | pagefault_disable(); |
| 42 | for (;;) { |
| 43 | char v; |
| 44 | unsigned long ret; |
| 45 | |
| 46 | if (unlikely(!access_ok(VERIFY_READ, |
| 47 | (__force const char __user *) addr, |
| 48 | sizeof(v)))) |
| 49 | break; |
| 50 | ret = __copy_from_user_inatomic(&v, |
| 51 | (__force const char __user *)(addr), |
| 52 | sizeof(v)); |
| 53 | if (unlikely(ret > 0)) |
| 54 | break; |
| 55 | count++; |
| 56 | if (unlikely(!v)) |
| 57 | break; |
| 58 | addr++; |
| 59 | } |
| 60 | pagefault_enable(); |
| 61 | set_fs(old_fs); |
| 62 | return count; |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(lttng_strlen_user_inatomic); |