x86: some lock annotations for user copy paths
[firefly-linux-kernel-4.4.55.git] / include / asm-x86 / uaccess_32.h
1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/errno.h>
8 #include <linux/thread_info.h>
9 #include <linux/prefetch.h>
10 #include <linux/string.h>
11 #include <asm/asm.h>
12 #include <asm/page.h>
13
14 unsigned long __must_check __copy_to_user_ll
15                 (void __user *to, const void *from, unsigned long n);
16 unsigned long __must_check __copy_from_user_ll
17                 (void *to, const void __user *from, unsigned long n);
18 unsigned long __must_check __copy_from_user_ll_nozero
19                 (void *to, const void __user *from, unsigned long n);
20 unsigned long __must_check __copy_from_user_ll_nocache
21                 (void *to, const void __user *from, unsigned long n);
22 unsigned long __must_check __copy_from_user_ll_nocache_nozero
23                 (void *to, const void __user *from, unsigned long n);
24
25 /**
26  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
27  * @to:   Destination address, in user space.
28  * @from: Source address, in kernel space.
29  * @n:    Number of bytes to copy.
30  *
31  * Context: User context only.
32  *
33  * Copy data from kernel space to user space.  Caller must check
34  * the specified block with access_ok() before calling this function.
35  * The caller should also make sure he pins the user space address
36  * so that the we don't result in page fault and sleep.
37  *
38  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
39  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
40  * If a store crosses a page boundary and gets a fault, the x86 will not write
41  * anything, so this is accurate.
42  */
43
44 static __always_inline unsigned long __must_check
45 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
46 {
47         if (__builtin_constant_p(n)) {
48                 unsigned long ret;
49
50                 switch (n) {
51                 case 1:
52                         __put_user_size(*(u8 *)from, (u8 __user *)to,
53                                         1, ret, 1);
54                         return ret;
55                 case 2:
56                         __put_user_size(*(u16 *)from, (u16 __user *)to,
57                                         2, ret, 2);
58                         return ret;
59                 case 4:
60                         __put_user_size(*(u32 *)from, (u32 __user *)to,
61                                         4, ret, 4);
62                         return ret;
63                 }
64         }
65         return __copy_to_user_ll(to, from, n);
66 }
67
68 /**
69  * __copy_to_user: - Copy a block of data into user space, with less checking.
70  * @to:   Destination address, in user space.
71  * @from: Source address, in kernel space.
72  * @n:    Number of bytes to copy.
73  *
74  * Context: User context only.  This function may sleep.
75  *
76  * Copy data from kernel space to user space.  Caller must check
77  * the specified block with access_ok() before calling this function.
78  *
79  * Returns number of bytes that could not be copied.
80  * On success, this will be zero.
81  */
82 static __always_inline unsigned long __must_check
83 __copy_to_user(void __user *to, const void *from, unsigned long n)
84 {
85         might_sleep();
86         if (current->mm)
87                 might_lock_read(&current->mm->mmap_sem);
88         return __copy_to_user_inatomic(to, from, n);
89 }
90
91 static __always_inline unsigned long
92 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
93 {
94         /* Avoid zeroing the tail if the copy fails..
95          * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
96          * but as the zeroing behaviour is only significant when n is not
97          * constant, that shouldn't be a problem.
98          */
99         if (__builtin_constant_p(n)) {
100                 unsigned long ret;
101
102                 switch (n) {
103                 case 1:
104                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
105                         return ret;
106                 case 2:
107                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
108                         return ret;
109                 case 4:
110                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
111                         return ret;
112                 }
113         }
114         return __copy_from_user_ll_nozero(to, from, n);
115 }
116
117 /**
118  * __copy_from_user: - Copy a block of data from user space, with less checking.
119  * @to:   Destination address, in kernel space.
120  * @from: Source address, in user space.
121  * @n:    Number of bytes to copy.
122  *
123  * Context: User context only.  This function may sleep.
124  *
125  * Copy data from user space to kernel space.  Caller must check
126  * the specified block with access_ok() before calling this function.
127  *
128  * Returns number of bytes that could not be copied.
129  * On success, this will be zero.
130  *
131  * If some data could not be copied, this function will pad the copied
132  * data to the requested size using zero bytes.
133  *
134  * An alternate version - __copy_from_user_inatomic() - may be called from
135  * atomic context and will fail rather than sleep.  In this case the
136  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
137  * for explanation of why this is needed.
138  */
139 static __always_inline unsigned long
140 __copy_from_user(void *to, const void __user *from, unsigned long n)
141 {
142         might_sleep();
143         if (current->mm)
144                 might_lock_read(&current->mm->mmap_sem);
145         if (__builtin_constant_p(n)) {
146                 unsigned long ret;
147
148                 switch (n) {
149                 case 1:
150                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
151                         return ret;
152                 case 2:
153                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
154                         return ret;
155                 case 4:
156                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
157                         return ret;
158                 }
159         }
160         return __copy_from_user_ll(to, from, n);
161 }
162
163 static __always_inline unsigned long __copy_from_user_nocache(void *to,
164                                 const void __user *from, unsigned long n)
165 {
166         might_sleep();
167         if (current->mm)
168                 might_lock_read(&current->mm->mmap_sem);
169         if (__builtin_constant_p(n)) {
170                 unsigned long ret;
171
172                 switch (n) {
173                 case 1:
174                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
175                         return ret;
176                 case 2:
177                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
178                         return ret;
179                 case 4:
180                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
181                         return ret;
182                 }
183         }
184         return __copy_from_user_ll_nocache(to, from, n);
185 }
186
187 static __always_inline unsigned long
188 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
189                                   unsigned long n)
190 {
191        return __copy_from_user_ll_nocache_nozero(to, from, n);
192 }
193
194 unsigned long __must_check copy_to_user(void __user *to,
195                                         const void *from, unsigned long n);
196 unsigned long __must_check copy_from_user(void *to,
197                                           const void __user *from,
198                                           unsigned long n);
199 long __must_check strncpy_from_user(char *dst, const char __user *src,
200                                     long count);
201 long __must_check __strncpy_from_user(char *dst,
202                                       const char __user *src, long count);
203
204 /**
205  * strlen_user: - Get the size of a string in user space.
206  * @str: The string to measure.
207  *
208  * Context: User context only.  This function may sleep.
209  *
210  * Get the size of a NUL-terminated string in user space.
211  *
212  * Returns the size of the string INCLUDING the terminating NUL.
213  * On exception, returns 0.
214  *
215  * If there is a limit on the length of a valid string, you may wish to
216  * consider using strnlen_user() instead.
217  */
218 #define strlen_user(str) strnlen_user(str, LONG_MAX)
219
220 long strnlen_user(const char __user *str, long n);
221 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
222 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
223
224 #endif /* __i386_UACCESS_H */