blob: 23290cc93a2473a804a52b7e7eac48616ff87429 [file] [log] [blame]
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07001#ifndef __LINUX_UACCESS_H__
2#define __LINUX_UACCESS_H__
3
Peter Zijlstraa8663742006-12-06 20:32:20 -08004#include <linux/preempt.h>
David Hildenbrand8bcbde52015-05-11 17:52:06 +02005#include <linux/sched.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07006#include <asm/uaccess.h>
7
David Hildenbrand8bcbde52015-05-11 17:52:06 +02008static __always_inline void pagefault_disabled_inc(void)
9{
10 current->pagefault_disabled++;
11}
12
13static __always_inline void pagefault_disabled_dec(void)
14{
15 current->pagefault_disabled--;
16 WARN_ON(current->pagefault_disabled < 0);
17}
18
Peter Zijlstraa8663742006-12-06 20:32:20 -080019/*
David Hildenbrand8bcbde52015-05-11 17:52:06 +020020 * These routines enable/disable the pagefault handler. If disabled, it will
21 * not take any locks and go straight to the fixup table.
Peter Zijlstraa8663742006-12-06 20:32:20 -080022 *
David Hildenbrand8bcbde52015-05-11 17:52:06 +020023 * 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 Zijlstraa8663742006-12-06 20:32:20 -080030 */
31static inline void pagefault_disable(void)
32{
Peter Zijlstrabdb43802013-09-10 12:15:23 +020033 preempt_count_inc();
David Hildenbrand8bcbde52015-05-11 17:52:06 +020034 pagefault_disabled_inc();
Peter Zijlstraa8663742006-12-06 20:32:20 -080035 /*
36 * make sure to have issued the store before a pagefault
37 * can hit.
38 */
39 barrier();
40}
41
42static 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 Hildenbrand8bcbde52015-05-11 17:52:06 +020049 pagefault_disabled_dec();
50#ifndef CONFIG_PREEMPT
Peter Zijlstrabdb43802013-09-10 12:15:23 +020051 preempt_count_dec();
Peter Zijlstra62b94a02013-11-20 16:52:19 +010052#else
53 preempt_enable();
54#endif
Peter Zijlstraa8663742006-12-06 20:32:20 -080055}
56
David Hildenbrand8bcbde52015-05-11 17:52:06 +020057/*
58 * Is the pagefault handler disabled? If so, user access methods will not sleep.
59 */
60#define pagefault_disabled() (current->pagefault_disabled != 0)
61
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070062#ifndef ARCH_HAS_NOCACHE_UACCESS
63
64static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
65 const void __user *from, unsigned long n)
66{
67 return __copy_from_user_inatomic(to, from, n);
68}
69
70static inline unsigned long __copy_from_user_nocache(void *to,
71 const void __user *from, unsigned long n)
72{
73 return __copy_from_user(to, from, n);
74}
75
76#endif /* ARCH_HAS_NOCACHE_UACCESS */
77
Andrew Morton1b79e552006-09-27 01:51:14 -070078/**
79 * probe_kernel_address(): safely attempt to read from a location
80 * @addr: address to read from - its type is type typeof(retval)*
81 * @retval: read into this variable
82 *
83 * Safely read from address @addr into variable @revtal. If a kernel fault
84 * happens, handle that and return -EFAULT.
85 * We ensure that the __get_user() is executed in atomic context so that
86 * do_page_fault() doesn't attempt to take mmap_sem. This makes
87 * probe_kernel_address() suitable for use within regions where the caller
88 * already holds mmap_sem, or other locks which nest inside mmap_sem.
Andrew Morton20aa7b22006-12-06 20:36:40 -080089 * This must be a macro because __get_user() needs to know the types of the
90 * args.
91 *
92 * We don't include enough header files to be able to do the set_fs(). We
93 * require that the probe_kernel_address() caller will do that.
Andrew Morton1b79e552006-09-27 01:51:14 -070094 */
95#define probe_kernel_address(addr, retval) \
96 ({ \
97 long ret; \
Andrew Morton20aa7b22006-12-06 20:36:40 -080098 mm_segment_t old_fs = get_fs(); \
Andrew Morton1b79e552006-09-27 01:51:14 -070099 \
Andrew Morton20aa7b22006-12-06 20:36:40 -0800100 set_fs(KERNEL_DS); \
Peter Zijlstraa8663742006-12-06 20:32:20 -0800101 pagefault_disable(); \
Hiroshi Shimamotofb71e452008-09-15 18:04:26 -0700102 ret = __copy_from_user_inatomic(&(retval), (__force typeof(retval) __user *)(addr), sizeof(retval)); \
Peter Zijlstraa8663742006-12-06 20:32:20 -0800103 pagefault_enable(); \
Andrew Morton20aa7b22006-12-06 20:36:40 -0800104 set_fs(old_fs); \
Andrew Morton1b79e552006-09-27 01:51:14 -0700105 ret; \
106 })
107
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200108/*
109 * probe_kernel_read(): safely attempt to read from a location
110 * @dst: pointer to the buffer that shall take the data
111 * @src: address to read from
112 * @size: size of the data chunk
113 *
114 * Safely read from address @src to the buffer at @dst. If a kernel fault
115 * happens, handle that and return -EFAULT.
116 */
Steven Rostedtf29c5042011-05-19 14:35:33 -0400117extern long probe_kernel_read(void *dst, const void *src, size_t size);
118extern long __probe_kernel_read(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200119
120/*
121 * probe_kernel_write(): safely attempt to write to a location
122 * @dst: address to write to
123 * @src: pointer to the data that shall be written
124 * @size: size of the data chunk
125 *
126 * Safely write to address @dst from the buffer at @src. If a kernel fault
127 * happens, handle that and return -EFAULT.
128 */
Steven Rostedtf29c5042011-05-19 14:35:33 -0400129extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
130extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200131
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700132#endif /* __LINUX_UACCESS_H__ */