1 /* SPDX-License-Identifier: GPL-2.0 */
3 * include/linux/userfaultfd_k.h
5 * Copyright (C) 2015 Red Hat, Inc.
9 #ifndef _LINUX_USERFAULTFD_K_H
10 #define _LINUX_USERFAULTFD_K_H
12 #ifdef CONFIG_USERFAULTFD
14 #include <linux/userfaultfd.h> /* linux/include/uapi/linux/userfaultfd.h */
16 #include <linux/fcntl.h>
18 #include <linux/swap.h>
19 #include <linux/swapops.h>
20 #include <asm-generic/pgtable_uffd.h>
21 #include <linux/hugetlb_inline.h>
23 /* The set of all possible UFFD-related VM flags. */
24 #define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)
27 * CAREFUL: Check include/uapi/asm-generic/fcntl.h when defining
28 * new flags, since they might collide with O_* ones. We want
29 * to re-use O_* flags that couldn't possibly have a meaning
30 * from userfaultfd, in order to leave a free define-space for
33 #define UFFD_CLOEXEC O_CLOEXEC
34 #define UFFD_NONBLOCK O_NONBLOCK
36 #define UFFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
37 #define UFFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS)
39 extern int sysctl_unprivileged_userfaultfd;
41 extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason);
44 * The mode of operation for __mcopy_atomic and its helpers.
46 * This is almost an implementation detail (mcopy_atomic below doesn't take this
47 * as a parameter), but it's exposed here because memory-kind-specific
48 * implementations (e.g. hugetlbfs) need to know the mode of operation.
50 enum mcopy_atomic_mode {
51 /* A normal copy_from_user into the destination range. */
53 /* Don't copy; map the destination range to the zero page. */
54 MCOPY_ATOMIC_ZEROPAGE,
55 /* Just install pte(s) with the existing page(s) in the page cache. */
56 MCOPY_ATOMIC_CONTINUE,
59 extern int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
60 struct vm_area_struct *dst_vma,
61 unsigned long dst_addr, struct page *page,
62 bool newly_allocated, bool wp_copy);
64 extern ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
65 unsigned long src_start, unsigned long len,
66 atomic_t *mmap_changing, __u64 mode);
67 extern ssize_t mfill_zeropage(struct mm_struct *dst_mm,
68 unsigned long dst_start,
70 atomic_t *mmap_changing);
71 extern ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long dst_start,
72 unsigned long len, atomic_t *mmap_changing);
73 extern int mwriteprotect_range(struct mm_struct *dst_mm,
74 unsigned long start, unsigned long len,
75 bool enable_wp, atomic_t *mmap_changing);
78 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
79 struct vm_userfaultfd_ctx vm_ctx)
81 return vma->vm_userfaultfd_ctx.ctx == vm_ctx.ctx;
85 * Never enable huge pmd sharing on some uffd registered vmas:
87 * - VM_UFFD_WP VMAs, because write protect information is per pgtable entry.
89 * - VM_UFFD_MINOR VMAs, because otherwise we would never get minor faults for
90 * VMAs which share huge pmds. (If you have two mappings to the same
91 * underlying pages, and fault in the non-UFFD-registered one with a write,
92 * with huge pmd sharing this would *also* setup the second UFFD-registered
93 * mapping, and we'd not get minor faults.)
95 static inline bool uffd_disable_huge_pmd_share(struct vm_area_struct *vma)
97 return vma->vm_flags & (VM_UFFD_WP | VM_UFFD_MINOR);
101 * Don't do fault around for either WP or MINOR registered uffd range. For
102 * MINOR registered range, fault around will be a total disaster and ptes can
103 * be installed without notifications; for WP it should mostly be fine as long
104 * as the fault around checks for pte_none() before the installation, however
105 * to be super safe we just forbid it.
107 static inline bool uffd_disable_fault_around(struct vm_area_struct *vma)
109 return vma->vm_flags & (VM_UFFD_WP | VM_UFFD_MINOR);
112 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
114 return vma->vm_flags & VM_UFFD_MISSING;
117 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
119 return vma->vm_flags & VM_UFFD_WP;
122 static inline bool userfaultfd_minor(struct vm_area_struct *vma)
124 return vma->vm_flags & VM_UFFD_MINOR;
127 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
130 return userfaultfd_wp(vma) && pte_uffd_wp(pte);
133 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
136 return userfaultfd_wp(vma) && pmd_uffd_wp(pmd);
139 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
141 return vma->vm_flags & __VM_UFFD_FLAGS;
144 static inline bool vma_can_userfault(struct vm_area_struct *vma,
145 unsigned long vm_flags)
147 if (vm_flags & VM_UFFD_MINOR)
148 return is_vm_hugetlb_page(vma) || vma_is_shmem(vma);
150 #ifndef CONFIG_PTE_MARKER_UFFD_WP
152 * If user requested uffd-wp but not enabled pte markers for
153 * uffd-wp, then shmem & hugetlbfs are not supported but only
156 if ((vm_flags & VM_UFFD_WP) && !vma_is_anonymous(vma))
159 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
163 extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *);
164 extern void dup_userfaultfd_complete(struct list_head *);
166 extern void mremap_userfaultfd_prep(struct vm_area_struct *,
167 struct vm_userfaultfd_ctx *);
168 extern void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *,
169 unsigned long from, unsigned long to,
172 extern bool userfaultfd_remove(struct vm_area_struct *vma,
176 extern int userfaultfd_unmap_prep(struct vm_area_struct *vma,
177 unsigned long start, unsigned long end,
178 struct list_head *uf);
179 extern void userfaultfd_unmap_complete(struct mm_struct *mm,
180 struct list_head *uf);
182 #else /* CONFIG_USERFAULTFD */
185 static inline vm_fault_t handle_userfault(struct vm_fault *vmf,
186 unsigned long reason)
188 return VM_FAULT_SIGBUS;
191 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
192 struct vm_userfaultfd_ctx vm_ctx)
197 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
202 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
207 static inline bool userfaultfd_minor(struct vm_area_struct *vma)
212 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
218 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
225 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
230 static inline int dup_userfaultfd(struct vm_area_struct *vma,
236 static inline void dup_userfaultfd_complete(struct list_head *l)
240 static inline void mremap_userfaultfd_prep(struct vm_area_struct *vma,
241 struct vm_userfaultfd_ctx *ctx)
245 static inline void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *ctx,
252 static inline bool userfaultfd_remove(struct vm_area_struct *vma,
259 static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma,
260 unsigned long start, unsigned long end,
261 struct list_head *uf)
266 static inline void userfaultfd_unmap_complete(struct mm_struct *mm,
267 struct list_head *uf)
271 static inline bool uffd_disable_fault_around(struct vm_area_struct *vma)
276 #endif /* CONFIG_USERFAULTFD */
278 static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry)
280 #ifdef CONFIG_PTE_MARKER_UFFD_WP
281 return is_pte_marker_entry(entry) &&
282 (pte_marker_get(entry) & PTE_MARKER_UFFD_WP);
288 static inline bool pte_marker_uffd_wp(pte_t pte)
290 #ifdef CONFIG_PTE_MARKER_UFFD_WP
293 if (!is_swap_pte(pte))
296 entry = pte_to_swp_entry(pte);
298 return pte_marker_entry_uffd_wp(entry);
305 * Returns true if this is a swap pte and was uffd-wp wr-protected in either
306 * forms (pte marker or a normal swap pte), false otherwise.
308 static inline bool pte_swp_uffd_wp_any(pte_t pte)
310 #ifdef CONFIG_PTE_MARKER_UFFD_WP
311 if (!is_swap_pte(pte))
314 if (pte_swp_uffd_wp(pte))
317 if (pte_marker_uffd_wp(pte))
323 #endif /* _LINUX_USERFAULTFD_K_H */