1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * include/linux/userfaultfd_k.h
4 *
5 * Copyright (C) 2015 Red Hat, Inc.
6 *
7 */
8
9 #ifndef _LINUX_USERFAULTFD_K_H
10 #define _LINUX_USERFAULTFD_K_H
11
12 #ifdef CONFIG_USERFAULTFD
13
14 #include <linux/userfaultfd.h> /* linux/include/uapi/linux/userfaultfd.h */
15
16 #include <linux/fcntl.h>
17 #include <linux/mm.h>
18 #include <asm-generic/pgtable_uffd.h>
19
20 /* The set of all possible UFFD-related VM flags. */
21 #define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)
22
23 /*
24 * CAREFUL: Check include/uapi/asm-generic/fcntl.h when defining
25 * new flags, since they might collide with O_* ones. We want
26 * to re-use O_* flags that couldn't possibly have a meaning
27 * from userfaultfd, in order to leave a free define-space for
28 * shared O_* flags.
29 */
30 #define UFFD_CLOEXEC O_CLOEXEC
31 #define UFFD_NONBLOCK O_NONBLOCK
32
33 #define UFFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
34 #define UFFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS)
35
36 static_assert(UFFDIO_ZEROPAGE_MODE_MMAP_TRYLOCK == UFFDIO_COPY_MODE_MMAP_TRYLOCK);
37 #define UFFDIO_MODE_MMAP_TRYLOCK UFFDIO_COPY_MODE_MMAP_TRYLOCK
38
39 extern int sysctl_unprivileged_userfaultfd;
40
41 extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason);
42 #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
43 extern bool userfaultfd_using_sigbus(struct vm_area_struct *vma);
44 #endif
45
46 /*
47 * The mode of operation for __mcopy_atomic and its helpers.
48 *
49 * This is almost an implementation detail (mcopy_atomic below doesn't take this
50 * as a parameter), but it's exposed here because memory-kind-specific
51 * implementations (e.g. hugetlbfs) need to know the mode of operation.
52 */
53 enum mcopy_atomic_mode {
54 /* A normal copy_from_user into the destination range. */
55 MCOPY_ATOMIC_NORMAL,
56 /* Don't copy; map the destination range to the zero page. */
57 MCOPY_ATOMIC_ZEROPAGE,
58 /* Just install pte(s) with the existing page(s) in the page cache. */
59 MCOPY_ATOMIC_CONTINUE,
60 };
61
62 extern int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
63 struct vm_area_struct *dst_vma,
64 unsigned long dst_addr, struct page *page,
65 bool newly_allocated, bool wp_copy);
66
67 extern ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
68 unsigned long src_start, unsigned long len,
69 atomic_t *mmap_changing, __u64 mode);
70 extern ssize_t mfill_zeropage(struct mm_struct *dst_mm,
71 unsigned long dst_start, unsigned long len,
72 atomic_t *mmap_changing, __u64 mode);
73 extern ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long dst_start,
74 unsigned long len, atomic_t *mmap_changing);
75 extern int mwriteprotect_range(struct mm_struct *dst_mm,
76 unsigned long start, unsigned long len,
77 bool enable_wp, atomic_t *mmap_changing);
78
79 /* mm helpers */
is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct * vma,struct vm_userfaultfd_ctx vm_ctx)80 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
81 struct vm_userfaultfd_ctx vm_ctx)
82 {
83 return rcu_access_pointer(vma->vm_userfaultfd_ctx.ctx) == vm_ctx.ctx;
84 }
85
86 /*
87 * Never enable huge pmd sharing on some uffd registered vmas:
88 *
89 * - VM_UFFD_WP VMAs, because write protect information is per pgtable entry.
90 *
91 * - VM_UFFD_MINOR VMAs, because otherwise we would never get minor faults for
92 * VMAs which share huge pmds. (If you have two mappings to the same
93 * underlying pages, and fault in the non-UFFD-registered one with a write,
94 * with huge pmd sharing this would *also* setup the second UFFD-registered
95 * mapping, and we'd not get minor faults.)
96 */
uffd_disable_huge_pmd_share(struct vm_area_struct * vma)97 static inline bool uffd_disable_huge_pmd_share(struct vm_area_struct *vma)
98 {
99 return vma->vm_flags & (VM_UFFD_WP | VM_UFFD_MINOR);
100 }
101
userfaultfd_missing(struct vm_area_struct * vma)102 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
103 {
104 return vma->vm_flags & VM_UFFD_MISSING;
105 }
106
userfaultfd_wp(struct vm_area_struct * vma)107 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
108 {
109 return vma->vm_flags & VM_UFFD_WP;
110 }
111
userfaultfd_minor(struct vm_area_struct * vma)112 static inline bool userfaultfd_minor(struct vm_area_struct *vma)
113 {
114 return vma->vm_flags & VM_UFFD_MINOR;
115 }
116
userfaultfd_pte_wp(struct vm_area_struct * vma,pte_t pte)117 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
118 pte_t pte)
119 {
120 return userfaultfd_wp(vma) && pte_uffd_wp(pte);
121 }
122
userfaultfd_huge_pmd_wp(struct vm_area_struct * vma,pmd_t pmd)123 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
124 pmd_t pmd)
125 {
126 return userfaultfd_wp(vma) && pmd_uffd_wp(pmd);
127 }
128
userfaultfd_armed(struct vm_area_struct * vma)129 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
130 {
131 return vma->vm_flags & __VM_UFFD_FLAGS;
132 }
133
134 extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *);
135 extern void dup_userfaultfd_complete(struct list_head *);
136
137 extern void mremap_userfaultfd_prep(struct vm_area_struct *,
138 struct vm_userfaultfd_ctx *);
139 extern void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *,
140 unsigned long from, unsigned long to,
141 unsigned long len);
142
143 extern bool userfaultfd_remove(struct vm_area_struct *vma,
144 unsigned long start,
145 unsigned long end);
146
147 extern int userfaultfd_unmap_prep(struct vm_area_struct *vma,
148 unsigned long start, unsigned long end,
149 struct list_head *uf);
150 extern void userfaultfd_unmap_complete(struct mm_struct *mm,
151 struct list_head *uf);
152
153 #else /* CONFIG_USERFAULTFD */
154
155 /* mm helpers */
handle_userfault(struct vm_fault * vmf,unsigned long reason)156 static inline vm_fault_t handle_userfault(struct vm_fault *vmf,
157 unsigned long reason)
158 {
159 return VM_FAULT_SIGBUS;
160 }
161
162 #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
userfaultfd_using_sigbus(struct vm_area_struct * vma)163 static inline bool userfaultfd_using_sigbus(struct vm_area_struct *vma)
164 {
165 return false;
166 }
167 #endif
168
is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct * vma,struct vm_userfaultfd_ctx vm_ctx)169 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
170 struct vm_userfaultfd_ctx vm_ctx)
171 {
172 return true;
173 }
174
userfaultfd_missing(struct vm_area_struct * vma)175 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
176 {
177 return false;
178 }
179
userfaultfd_wp(struct vm_area_struct * vma)180 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
181 {
182 return false;
183 }
184
userfaultfd_minor(struct vm_area_struct * vma)185 static inline bool userfaultfd_minor(struct vm_area_struct *vma)
186 {
187 return false;
188 }
189
userfaultfd_pte_wp(struct vm_area_struct * vma,pte_t pte)190 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
191 pte_t pte)
192 {
193 return false;
194 }
195
userfaultfd_huge_pmd_wp(struct vm_area_struct * vma,pmd_t pmd)196 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
197 pmd_t pmd)
198 {
199 return false;
200 }
201
202
userfaultfd_armed(struct vm_area_struct * vma)203 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
204 {
205 return false;
206 }
207
dup_userfaultfd(struct vm_area_struct * vma,struct list_head * l)208 static inline int dup_userfaultfd(struct vm_area_struct *vma,
209 struct list_head *l)
210 {
211 return 0;
212 }
213
dup_userfaultfd_complete(struct list_head * l)214 static inline void dup_userfaultfd_complete(struct list_head *l)
215 {
216 }
217
mremap_userfaultfd_prep(struct vm_area_struct * vma,struct vm_userfaultfd_ctx * ctx)218 static inline void mremap_userfaultfd_prep(struct vm_area_struct *vma,
219 struct vm_userfaultfd_ctx *ctx)
220 {
221 }
222
mremap_userfaultfd_complete(struct vm_userfaultfd_ctx * ctx,unsigned long from,unsigned long to,unsigned long len)223 static inline void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *ctx,
224 unsigned long from,
225 unsigned long to,
226 unsigned long len)
227 {
228 }
229
userfaultfd_remove(struct vm_area_struct * vma,unsigned long start,unsigned long end)230 static inline bool userfaultfd_remove(struct vm_area_struct *vma,
231 unsigned long start,
232 unsigned long end)
233 {
234 return true;
235 }
236
userfaultfd_unmap_prep(struct vm_area_struct * vma,unsigned long start,unsigned long end,struct list_head * uf)237 static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma,
238 unsigned long start, unsigned long end,
239 struct list_head *uf)
240 {
241 return 0;
242 }
243
userfaultfd_unmap_complete(struct mm_struct * mm,struct list_head * uf)244 static inline void userfaultfd_unmap_complete(struct mm_struct *mm,
245 struct list_head *uf)
246 {
247 }
248
249 #endif /* CONFIG_USERFAULTFD */
250
251 #endif /* _LINUX_USERFAULTFD_K_H */
252