blob: 9442423979c1c6ccf58af38e0e1030bb2ad43d26 [file] [log] [blame]
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07001#ifndef __LINUX_UACCESS_H__
2#define __LINUX_UACCESS_H__
3
David Hildenbrand8bcbde52015-05-11 17:52:06 +02004#include <linux/sched.h>
Al Viro2cf6ba52017-03-20 21:08:07 -04005
6#define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
7
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07008#include <asm/uaccess.h>
9
David Hildenbrand8bcbde52015-05-11 17:52:06 +020010static __always_inline void pagefault_disabled_inc(void)
11{
12 current->pagefault_disabled++;
13}
14
15static __always_inline void pagefault_disabled_dec(void)
16{
17 current->pagefault_disabled--;
18 WARN_ON(current->pagefault_disabled < 0);
19}
20
Peter Zijlstraa8663742006-12-06 20:32:20 -080021/*
David Hildenbrand8bcbde52015-05-11 17:52:06 +020022 * These routines enable/disable the pagefault handler. If disabled, it will
23 * not take any locks and go straight to the fixup table.
Peter Zijlstraa8663742006-12-06 20:32:20 -080024 *
David Hildenbrand8222dbe2015-05-11 17:52:20 +020025 * User access methods will not sleep when called from a pagefault_disabled()
26 * environment.
Peter Zijlstraa8663742006-12-06 20:32:20 -080027 */
28static inline void pagefault_disable(void)
29{
David Hildenbrand8bcbde52015-05-11 17:52:06 +020030 pagefault_disabled_inc();
Peter Zijlstraa8663742006-12-06 20:32:20 -080031 /*
32 * make sure to have issued the store before a pagefault
33 * can hit.
34 */
35 barrier();
36}
37
38static inline void pagefault_enable(void)
39{
40 /*
41 * make sure to issue those last loads/stores before enabling
42 * the pagefault handler again.
43 */
44 barrier();
David Hildenbrand8bcbde52015-05-11 17:52:06 +020045 pagefault_disabled_dec();
Peter Zijlstraa8663742006-12-06 20:32:20 -080046}
47
David Hildenbrand8bcbde52015-05-11 17:52:06 +020048/*
49 * Is the pagefault handler disabled? If so, user access methods will not sleep.
50 */
51#define pagefault_disabled() (current->pagefault_disabled != 0)
52
David Hildenbrand70ffdb92015-05-11 17:52:11 +020053/*
54 * The pagefault handler is in general disabled by pagefault_disable() or
55 * when in irq context (via in_atomic()).
56 *
57 * This function should only be used by the fault handlers. Other users should
58 * stick to pagefault_disabled().
59 * Please NEVER use preempt_disable() to disable the fault handler. With
60 * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
61 * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
62 */
63#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
64
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070065#ifndef ARCH_HAS_NOCACHE_UACCESS
66
67static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
68 const void __user *from, unsigned long n)
69{
70 return __copy_from_user_inatomic(to, from, n);
71}
72
73static inline unsigned long __copy_from_user_nocache(void *to,
74 const void __user *from, unsigned long n)
75{
76 return __copy_from_user(to, from, n);
77}
78
79#endif /* ARCH_HAS_NOCACHE_UACCESS */
80
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020081/*
82 * probe_kernel_read(): safely attempt to read from a location
83 * @dst: pointer to the buffer that shall take the data
84 * @src: address to read from
85 * @size: size of the data chunk
86 *
87 * Safely read from address @src to the buffer at @dst. If a kernel fault
88 * happens, handle that and return -EFAULT.
89 */
Steven Rostedtf29c5042011-05-19 14:35:33 -040090extern long probe_kernel_read(void *dst, const void *src, size_t size);
91extern long __probe_kernel_read(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020092
93/*
94 * probe_kernel_write(): safely attempt to write to a location
95 * @dst: address to write to
96 * @src: pointer to the data that shall be written
97 * @size: size of the data chunk
98 *
99 * Safely write to address @dst from the buffer at @src. If a kernel fault
100 * happens, handle that and return -EFAULT.
101 */
Steven Rostedtf29c5042011-05-19 14:35:33 -0400102extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
103extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200104
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700105extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
106
Andrew Morton0ab32b62015-11-05 18:46:03 -0800107/**
108 * probe_kernel_address(): safely attempt to read from a location
109 * @addr: address to read from
110 * @retval: read into this variable
111 *
112 * Returns 0 on success, or -EFAULT.
113 */
114#define probe_kernel_address(addr, retval) \
115 probe_kernel_read(&retval, addr, sizeof(retval))
116
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800117#ifndef user_access_begin
118#define user_access_begin() do { } while (0)
119#define user_access_end() do { } while (0)
Linus Torvalds1bd44032016-08-08 13:02:01 -0700120#define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
121#define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800122#endif
123
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700124#endif /* __LINUX_UACCESS_H__ */