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 <linux/swap.h>
19 #include <linux/leafops.h>
20 #include <asm-generic/pgtable_uffd.h>
21 #include <linux/hugetlb_inline.h>
22
23 /* The set of all possible UFFD-related VM flags. */
24 #define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)
25
26 #define __VMA_UFFD_FLAGS mk_vma_flags(VMA_UFFD_MISSING_BIT, VMA_UFFD_WP_BIT, \
27 VMA_UFFD_MINOR_BIT)
28
29 /*
30 * CAREFUL: Check include/uapi/asm-generic/fcntl.h when defining
31 * new flags, since they might collide with O_* ones. We want
32 * to re-use O_* flags that couldn't possibly have a meaning
33 * from userfaultfd, in order to leave a free define-space for
34 * shared O_* flags.
35 */
36 #define UFFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
37
38 /*
39 * Start with fault_pending_wqh and fault_wqh so they're more likely
40 * to be in the same cacheline.
41 *
42 * Locking order:
43 * fd_wqh.lock
44 * fault_pending_wqh.lock
45 * fault_wqh.lock
46 * event_wqh.lock
47 *
48 * To avoid deadlocks, IRQs must be disabled when taking any of the above locks,
49 * since fd_wqh.lock is taken by aio_poll() while it's holding a lock that's
50 * also taken in IRQ context.
51 */
52 struct userfaultfd_ctx {
53 /* waitqueue head for the pending (i.e. not read) userfaults */
54 wait_queue_head_t fault_pending_wqh;
55 /* waitqueue head for the userfaults */
56 wait_queue_head_t fault_wqh;
57 /* waitqueue head for the pseudo fd to wakeup poll/read */
58 wait_queue_head_t fd_wqh;
59 /* waitqueue head for events */
60 wait_queue_head_t event_wqh;
61 /* a refile sequence protected by fault_pending_wqh lock */
62 seqcount_spinlock_t refile_seq;
63 /* pseudo fd refcounting */
64 refcount_t refcount;
65 /* userfaultfd syscall flags */
66 unsigned int flags;
67 /* features requested from the userspace */
68 unsigned int features;
69 /* released */
70 bool released;
71 /*
72 * Prevents userfaultfd operations (fill/move/wp) from happening while
73 * some non-cooperative event(s) is taking place. Increments are done
74 * in write-mode. Whereas, userfaultfd operations, which includes
75 * reading mmap_changing, is done under read-mode.
76 */
77 struct rw_semaphore map_changing_lock;
78 /* memory mappings are changing because of non-cooperative event */
79 atomic_t mmap_changing;
80 /* mm with one ore more vmas attached to this userfaultfd_ctx */
81 struct mm_struct *mm;
82 };
83
84 extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason);
85
86 /* A combined operation mode + behavior flags. */
87 typedef unsigned int __bitwise uffd_flags_t;
88
89 /* Mutually exclusive modes of operation. */
90 enum mfill_atomic_mode {
91 MFILL_ATOMIC_COPY,
92 MFILL_ATOMIC_ZEROPAGE,
93 MFILL_ATOMIC_CONTINUE,
94 MFILL_ATOMIC_POISON,
95 NR_MFILL_ATOMIC_MODES,
96 };
97
98 #define MFILL_ATOMIC_MODE_BITS (const_ilog2(NR_MFILL_ATOMIC_MODES - 1) + 1)
99 #define MFILL_ATOMIC_BIT(nr) BIT(MFILL_ATOMIC_MODE_BITS + (nr))
100 #define MFILL_ATOMIC_FLAG(nr) ((__force uffd_flags_t) MFILL_ATOMIC_BIT(nr))
101 #define MFILL_ATOMIC_MODE_MASK ((__force uffd_flags_t) (MFILL_ATOMIC_BIT(0) - 1))
102
uffd_flags_mode_is(uffd_flags_t flags,enum mfill_atomic_mode expected)103 static inline bool uffd_flags_mode_is(uffd_flags_t flags, enum mfill_atomic_mode expected)
104 {
105 return (flags & MFILL_ATOMIC_MODE_MASK) == ((__force uffd_flags_t) expected);
106 }
107
uffd_flags_set_mode(uffd_flags_t flags,enum mfill_atomic_mode mode)108 static inline uffd_flags_t uffd_flags_set_mode(uffd_flags_t flags, enum mfill_atomic_mode mode)
109 {
110 flags &= ~MFILL_ATOMIC_MODE_MASK;
111 return flags | ((__force uffd_flags_t) mode);
112 }
113
114 /* Flags controlling behavior. These behavior changes are mode-independent. */
115 #define MFILL_ATOMIC_WP MFILL_ATOMIC_FLAG(0)
116
117 extern int mfill_atomic_install_pte(pmd_t *dst_pmd,
118 struct vm_area_struct *dst_vma,
119 unsigned long dst_addr, struct page *page,
120 bool newly_allocated, uffd_flags_t flags);
121
122 extern ssize_t mfill_atomic_copy(struct userfaultfd_ctx *ctx, unsigned long dst_start,
123 unsigned long src_start, unsigned long len,
124 uffd_flags_t flags);
125 extern ssize_t mfill_atomic_zeropage(struct userfaultfd_ctx *ctx,
126 unsigned long dst_start,
127 unsigned long len);
128 extern ssize_t mfill_atomic_continue(struct userfaultfd_ctx *ctx, unsigned long dst_start,
129 unsigned long len, uffd_flags_t flags);
130 extern ssize_t mfill_atomic_poison(struct userfaultfd_ctx *ctx, unsigned long start,
131 unsigned long len, uffd_flags_t flags);
132 extern int mwriteprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
133 unsigned long len, bool enable_wp);
134 extern long uffd_wp_range(struct vm_area_struct *vma,
135 unsigned long start, unsigned long len, bool enable_wp);
136
137 /* move_pages */
138 void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2);
139 void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2);
140 ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
141 unsigned long src_start, unsigned long len, __u64 flags);
142 int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval,
143 struct vm_area_struct *dst_vma,
144 struct vm_area_struct *src_vma,
145 unsigned long dst_addr, unsigned long src_addr);
146
147 /* mm helpers */
is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct * vma,struct vm_userfaultfd_ctx vm_ctx)148 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
149 struct vm_userfaultfd_ctx vm_ctx)
150 {
151 return vma->vm_userfaultfd_ctx.ctx == vm_ctx.ctx;
152 }
153
154 /*
155 * Never enable huge pmd sharing on some uffd registered vmas:
156 *
157 * - VM_UFFD_WP VMAs, because write protect information is per pgtable entry.
158 *
159 * - VM_UFFD_MINOR VMAs, because otherwise we would never get minor faults for
160 * VMAs which share huge pmds. (If you have two mappings to the same
161 * underlying pages, and fault in the non-UFFD-registered one with a write,
162 * with huge pmd sharing this would *also* setup the second UFFD-registered
163 * mapping, and we'd not get minor faults.)
164 */
uffd_disable_huge_pmd_share(struct vm_area_struct * vma)165 static inline bool uffd_disable_huge_pmd_share(struct vm_area_struct *vma)
166 {
167 return vma->vm_flags & (VM_UFFD_WP | VM_UFFD_MINOR);
168 }
169
170 /*
171 * Don't do fault around for either WP or MINOR registered uffd range. For
172 * MINOR registered range, fault around will be a total disaster and ptes can
173 * be installed without notifications; for WP it should mostly be fine as long
174 * as the fault around checks for pte_none() before the installation, however
175 * to be super safe we just forbid it.
176 */
uffd_disable_fault_around(struct vm_area_struct * vma)177 static inline bool uffd_disable_fault_around(struct vm_area_struct *vma)
178 {
179 return vma->vm_flags & (VM_UFFD_WP | VM_UFFD_MINOR);
180 }
181
userfaultfd_missing(struct vm_area_struct * vma)182 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
183 {
184 return vma->vm_flags & VM_UFFD_MISSING;
185 }
186
userfaultfd_wp(struct vm_area_struct * vma)187 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
188 {
189 return vma->vm_flags & VM_UFFD_WP;
190 }
191
userfaultfd_minor(struct vm_area_struct * vma)192 static inline bool userfaultfd_minor(struct vm_area_struct *vma)
193 {
194 return vma->vm_flags & VM_UFFD_MINOR;
195 }
196
userfaultfd_pte_wp(struct vm_area_struct * vma,pte_t pte)197 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
198 pte_t pte)
199 {
200 return userfaultfd_wp(vma) && pte_uffd_wp(pte);
201 }
202
userfaultfd_huge_pmd_wp(struct vm_area_struct * vma,pmd_t pmd)203 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
204 pmd_t pmd)
205 {
206 return userfaultfd_wp(vma) && pmd_uffd_wp(pmd);
207 }
208
userfaultfd_armed(struct vm_area_struct * vma)209 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
210 {
211 return vma->vm_flags & __VM_UFFD_FLAGS;
212 }
213
vma_can_userfault(struct vm_area_struct * vma,vm_flags_t vm_flags,bool wp_async)214 static inline bool vma_can_userfault(struct vm_area_struct *vma,
215 vm_flags_t vm_flags,
216 bool wp_async)
217 {
218 vm_flags &= __VM_UFFD_FLAGS;
219
220 if (vma->vm_flags & VM_DROPPABLE)
221 return false;
222
223 if ((vm_flags & VM_UFFD_MINOR) &&
224 (!is_vm_hugetlb_page(vma) && !vma_is_shmem(vma)))
225 return false;
226
227 /*
228 * If wp async enabled, and WP is the only mode enabled, allow any
229 * memory type.
230 */
231 if (wp_async && (vm_flags == VM_UFFD_WP))
232 return true;
233
234 /*
235 * If user requested uffd-wp but not enabled pte markers for
236 * uffd-wp, then shmem & hugetlbfs are not supported but only
237 * anonymous.
238 */
239 if (!uffd_supports_wp_marker() && (vm_flags & VM_UFFD_WP) &&
240 !vma_is_anonymous(vma))
241 return false;
242
243 /* By default, allow any of anon|shmem|hugetlb */
244 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
245 vma_is_shmem(vma);
246 }
247
vma_has_uffd_without_event_remap(struct vm_area_struct * vma)248 static inline bool vma_has_uffd_without_event_remap(struct vm_area_struct *vma)
249 {
250 struct userfaultfd_ctx *uffd_ctx = vma->vm_userfaultfd_ctx.ctx;
251
252 return uffd_ctx && (uffd_ctx->features & UFFD_FEATURE_EVENT_REMAP) == 0;
253 }
254
255 extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *);
256 extern void dup_userfaultfd_complete(struct list_head *);
257 void dup_userfaultfd_fail(struct list_head *);
258
259 extern void mremap_userfaultfd_prep(struct vm_area_struct *,
260 struct vm_userfaultfd_ctx *);
261 extern void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *,
262 unsigned long from, unsigned long to,
263 unsigned long len);
264 void mremap_userfaultfd_fail(struct vm_userfaultfd_ctx *);
265
266 extern bool userfaultfd_remove(struct vm_area_struct *vma,
267 unsigned long start,
268 unsigned long end);
269
270 extern int userfaultfd_unmap_prep(struct vm_area_struct *vma,
271 unsigned long start, unsigned long end, struct list_head *uf);
272 extern void userfaultfd_unmap_complete(struct mm_struct *mm,
273 struct list_head *uf);
274 extern bool userfaultfd_wp_unpopulated(struct vm_area_struct *vma);
275 extern bool userfaultfd_wp_async(struct vm_area_struct *vma);
276
277 void userfaultfd_reset_ctx(struct vm_area_struct *vma);
278
279 struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
280 struct vm_area_struct *prev,
281 struct vm_area_struct *vma,
282 unsigned long start,
283 unsigned long end);
284
285 int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
286 struct vm_area_struct *vma,
287 vm_flags_t vm_flags,
288 unsigned long start, unsigned long end,
289 bool wp_async);
290
291 void userfaultfd_release_new(struct userfaultfd_ctx *ctx);
292
293 void userfaultfd_release_all(struct mm_struct *mm,
294 struct userfaultfd_ctx *ctx);
295
userfaultfd_wp_use_markers(struct vm_area_struct * vma)296 static inline bool userfaultfd_wp_use_markers(struct vm_area_struct *vma)
297 {
298 /* Only wr-protect mode uses pte markers */
299 if (!userfaultfd_wp(vma))
300 return false;
301
302 /* File-based uffd-wp always need markers */
303 if (!vma_is_anonymous(vma))
304 return true;
305
306 /*
307 * Anonymous uffd-wp only needs the markers if WP_UNPOPULATED
308 * enabled (to apply markers on zero pages).
309 */
310 return userfaultfd_wp_unpopulated(vma);
311 }
312
313 /*
314 * Returns true if this is a swap pte and was uffd-wp wr-protected in either
315 * forms (pte marker or a normal swap pte), false otherwise.
316 */
pte_swp_uffd_wp_any(pte_t pte)317 static inline bool pte_swp_uffd_wp_any(pte_t pte)
318 {
319 if (!uffd_supports_wp_marker())
320 return false;
321
322 if (pte_present(pte))
323 return false;
324
325 if (pte_swp_uffd_wp(pte))
326 return true;
327
328 if (pte_is_uffd_wp_marker(pte))
329 return true;
330
331 return false;
332 }
333 #else /* CONFIG_USERFAULTFD */
334
335 /* mm helpers */
handle_userfault(struct vm_fault * vmf,unsigned long reason)336 static inline vm_fault_t handle_userfault(struct vm_fault *vmf,
337 unsigned long reason)
338 {
339 return VM_FAULT_SIGBUS;
340 }
341
uffd_wp_range(struct vm_area_struct * vma,unsigned long start,unsigned long len,bool enable_wp)342 static inline long uffd_wp_range(struct vm_area_struct *vma,
343 unsigned long start, unsigned long len,
344 bool enable_wp)
345 {
346 return false;
347 }
348
is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct * vma,struct vm_userfaultfd_ctx vm_ctx)349 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
350 struct vm_userfaultfd_ctx vm_ctx)
351 {
352 return true;
353 }
354
userfaultfd_missing(struct vm_area_struct * vma)355 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
356 {
357 return false;
358 }
359
userfaultfd_wp(struct vm_area_struct * vma)360 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
361 {
362 return false;
363 }
364
userfaultfd_minor(struct vm_area_struct * vma)365 static inline bool userfaultfd_minor(struct vm_area_struct *vma)
366 {
367 return false;
368 }
369
userfaultfd_pte_wp(struct vm_area_struct * vma,pte_t pte)370 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
371 pte_t pte)
372 {
373 return false;
374 }
375
userfaultfd_huge_pmd_wp(struct vm_area_struct * vma,pmd_t pmd)376 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
377 pmd_t pmd)
378 {
379 return false;
380 }
381
382
userfaultfd_armed(struct vm_area_struct * vma)383 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
384 {
385 return false;
386 }
387
dup_userfaultfd(struct vm_area_struct * vma,struct list_head * l)388 static inline int dup_userfaultfd(struct vm_area_struct *vma,
389 struct list_head *l)
390 {
391 return 0;
392 }
393
dup_userfaultfd_complete(struct list_head * l)394 static inline void dup_userfaultfd_complete(struct list_head *l)
395 {
396 }
397
dup_userfaultfd_fail(struct list_head * l)398 static inline void dup_userfaultfd_fail(struct list_head *l)
399 {
400 }
401
mremap_userfaultfd_prep(struct vm_area_struct * vma,struct vm_userfaultfd_ctx * ctx)402 static inline void mremap_userfaultfd_prep(struct vm_area_struct *vma,
403 struct vm_userfaultfd_ctx *ctx)
404 {
405 }
406
mremap_userfaultfd_complete(struct vm_userfaultfd_ctx * ctx,unsigned long from,unsigned long to,unsigned long len)407 static inline void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *ctx,
408 unsigned long from,
409 unsigned long to,
410 unsigned long len)
411 {
412 }
413
mremap_userfaultfd_fail(struct vm_userfaultfd_ctx * ctx)414 static inline void mremap_userfaultfd_fail(struct vm_userfaultfd_ctx *ctx)
415 {
416 }
417
userfaultfd_remove(struct vm_area_struct * vma,unsigned long start,unsigned long end)418 static inline bool userfaultfd_remove(struct vm_area_struct *vma,
419 unsigned long start,
420 unsigned long end)
421 {
422 return true;
423 }
424
userfaultfd_unmap_prep(struct vm_area_struct * vma,unsigned long start,unsigned long end,struct list_head * uf)425 static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma,
426 unsigned long start, unsigned long end,
427 struct list_head *uf)
428 {
429 return 0;
430 }
431
userfaultfd_unmap_complete(struct mm_struct * mm,struct list_head * uf)432 static inline void userfaultfd_unmap_complete(struct mm_struct *mm,
433 struct list_head *uf)
434 {
435 }
436
uffd_disable_fault_around(struct vm_area_struct * vma)437 static inline bool uffd_disable_fault_around(struct vm_area_struct *vma)
438 {
439 return false;
440 }
441
userfaultfd_wp_unpopulated(struct vm_area_struct * vma)442 static inline bool userfaultfd_wp_unpopulated(struct vm_area_struct *vma)
443 {
444 return false;
445 }
446
userfaultfd_wp_async(struct vm_area_struct * vma)447 static inline bool userfaultfd_wp_async(struct vm_area_struct *vma)
448 {
449 return false;
450 }
451
vma_has_uffd_without_event_remap(struct vm_area_struct * vma)452 static inline bool vma_has_uffd_without_event_remap(struct vm_area_struct *vma)
453 {
454 return false;
455 }
456
userfaultfd_wp_use_markers(struct vm_area_struct * vma)457 static inline bool userfaultfd_wp_use_markers(struct vm_area_struct *vma)
458 {
459 return false;
460 }
461
462 /*
463 * Returns true if this is a swap pte and was uffd-wp wr-protected in either
464 * forms (pte marker or a normal swap pte), false otherwise.
465 */
pte_swp_uffd_wp_any(pte_t pte)466 static inline bool pte_swp_uffd_wp_any(pte_t pte)
467 {
468 return false;
469 }
470 #endif /* CONFIG_USERFAULTFD */
471 #endif /* _LINUX_USERFAULTFD_K_H */
472