Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_UACCESS_H__ |
| 2 | #define __LINUX_UACCESS_H__ |
| 3 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 4 | #include <linux/preempt.h> |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 5 | #include <linux/sched.h> |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 6 | #include <asm/uaccess.h> |
| 7 | |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 8 | static __always_inline void pagefault_disabled_inc(void) |
| 9 | { |
| 10 | current->pagefault_disabled++; |
| 11 | } |
| 12 | |
| 13 | static __always_inline void pagefault_disabled_dec(void) |
| 14 | { |
| 15 | current->pagefault_disabled--; |
| 16 | WARN_ON(current->pagefault_disabled < 0); |
| 17 | } |
| 18 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 19 | /* |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 20 | * These routines enable/disable the pagefault handler. If disabled, it will |
| 21 | * not take any locks and go straight to the fixup table. |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 22 | * |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 23 | * We increase the preempt and the pagefault count, to be able to distinguish |
| 24 | * whether we run in simple atomic context or in a real pagefault_disable() |
| 25 | * context. |
| 26 | * |
| 27 | * For now, after pagefault_disabled() has been called, we run in atomic |
| 28 | * context. User access methods will not sleep. |
| 29 | * |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 30 | */ |
| 31 | static inline void pagefault_disable(void) |
| 32 | { |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 33 | preempt_count_inc(); |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 34 | pagefault_disabled_inc(); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 35 | /* |
| 36 | * make sure to have issued the store before a pagefault |
| 37 | * can hit. |
| 38 | */ |
| 39 | barrier(); |
| 40 | } |
| 41 | |
| 42 | static inline void pagefault_enable(void) |
| 43 | { |
| 44 | /* |
| 45 | * make sure to issue those last loads/stores before enabling |
| 46 | * the pagefault handler again. |
| 47 | */ |
| 48 | barrier(); |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 49 | pagefault_disabled_dec(); |
| 50 | #ifndef CONFIG_PREEMPT |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 51 | preempt_count_dec(); |
Peter Zijlstra | 62b94a0 | 2013-11-20 16:52:19 +0100 | [diff] [blame] | 52 | #else |
| 53 | preempt_enable(); |
| 54 | #endif |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 55 | } |
| 56 | |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 57 | /* |
| 58 | * Is the pagefault handler disabled? If so, user access methods will not sleep. |
| 59 | */ |
| 60 | #define pagefault_disabled() (current->pagefault_disabled != 0) |
| 61 | |
David Hildenbrand | 70ffdb9 | 2015-05-11 17:52:11 +0200 | [diff] [blame^] | 62 | /* |
| 63 | * The pagefault handler is in general disabled by pagefault_disable() or |
| 64 | * when in irq context (via in_atomic()). |
| 65 | * |
| 66 | * This function should only be used by the fault handlers. Other users should |
| 67 | * stick to pagefault_disabled(). |
| 68 | * Please NEVER use preempt_disable() to disable the fault handler. With |
| 69 | * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled. |
| 70 | * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT. |
| 71 | */ |
| 72 | #define faulthandler_disabled() (pagefault_disabled() || in_atomic()) |
| 73 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 74 | #ifndef ARCH_HAS_NOCACHE_UACCESS |
| 75 | |
| 76 | static inline unsigned long __copy_from_user_inatomic_nocache(void *to, |
| 77 | const void __user *from, unsigned long n) |
| 78 | { |
| 79 | return __copy_from_user_inatomic(to, from, n); |
| 80 | } |
| 81 | |
| 82 | static inline unsigned long __copy_from_user_nocache(void *to, |
| 83 | const void __user *from, unsigned long n) |
| 84 | { |
| 85 | return __copy_from_user(to, from, n); |
| 86 | } |
| 87 | |
| 88 | #endif /* ARCH_HAS_NOCACHE_UACCESS */ |
| 89 | |
Andrew Morton | 1b79e55 | 2006-09-27 01:51:14 -0700 | [diff] [blame] | 90 | /** |
| 91 | * probe_kernel_address(): safely attempt to read from a location |
| 92 | * @addr: address to read from - its type is type typeof(retval)* |
| 93 | * @retval: read into this variable |
| 94 | * |
| 95 | * Safely read from address @addr into variable @revtal. If a kernel fault |
| 96 | * happens, handle that and return -EFAULT. |
| 97 | * We ensure that the __get_user() is executed in atomic context so that |
| 98 | * do_page_fault() doesn't attempt to take mmap_sem. This makes |
| 99 | * probe_kernel_address() suitable for use within regions where the caller |
| 100 | * already holds mmap_sem, or other locks which nest inside mmap_sem. |
Andrew Morton | 20aa7b2 | 2006-12-06 20:36:40 -0800 | [diff] [blame] | 101 | * This must be a macro because __get_user() needs to know the types of the |
| 102 | * args. |
| 103 | * |
| 104 | * We don't include enough header files to be able to do the set_fs(). We |
| 105 | * require that the probe_kernel_address() caller will do that. |
Andrew Morton | 1b79e55 | 2006-09-27 01:51:14 -0700 | [diff] [blame] | 106 | */ |
| 107 | #define probe_kernel_address(addr, retval) \ |
| 108 | ({ \ |
| 109 | long ret; \ |
Andrew Morton | 20aa7b2 | 2006-12-06 20:36:40 -0800 | [diff] [blame] | 110 | mm_segment_t old_fs = get_fs(); \ |
Andrew Morton | 1b79e55 | 2006-09-27 01:51:14 -0700 | [diff] [blame] | 111 | \ |
Andrew Morton | 20aa7b2 | 2006-12-06 20:36:40 -0800 | [diff] [blame] | 112 | set_fs(KERNEL_DS); \ |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 113 | pagefault_disable(); \ |
Hiroshi Shimamoto | fb71e45 | 2008-09-15 18:04:26 -0700 | [diff] [blame] | 114 | ret = __copy_from_user_inatomic(&(retval), (__force typeof(retval) __user *)(addr), sizeof(retval)); \ |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 115 | pagefault_enable(); \ |
Andrew Morton | 20aa7b2 | 2006-12-06 20:36:40 -0800 | [diff] [blame] | 116 | set_fs(old_fs); \ |
Andrew Morton | 1b79e55 | 2006-09-27 01:51:14 -0700 | [diff] [blame] | 117 | ret; \ |
| 118 | }) |
| 119 | |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 120 | /* |
| 121 | * probe_kernel_read(): safely attempt to read from a location |
| 122 | * @dst: pointer to the buffer that shall take the data |
| 123 | * @src: address to read from |
| 124 | * @size: size of the data chunk |
| 125 | * |
| 126 | * Safely read from address @src to the buffer at @dst. If a kernel fault |
| 127 | * happens, handle that and return -EFAULT. |
| 128 | */ |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 129 | extern long probe_kernel_read(void *dst, const void *src, size_t size); |
| 130 | extern long __probe_kernel_read(void *dst, const void *src, size_t size); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * probe_kernel_write(): safely attempt to write to a location |
| 134 | * @dst: address to write to |
| 135 | * @src: pointer to the data that shall be written |
| 136 | * @size: size of the data chunk |
| 137 | * |
| 138 | * Safely write to address @dst from the buffer at @src. If a kernel fault |
| 139 | * happens, handle that and return -EFAULT. |
| 140 | */ |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 141 | extern long notrace probe_kernel_write(void *dst, const void *src, size_t size); |
| 142 | extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 143 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 144 | #endif /* __LINUX_UACCESS_H__ */ |