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 <asm-generic/pgtable_uffd.h>
21 * CAREFUL: Check include/uapi/asm-generic/fcntl.h when defining
22 * new flags, since they might collide with O_* ones. We want
23 * to re-use O_* flags that couldn't possibly have a meaning
24 * from userfaultfd, in order to leave a free define-space for
27 #define UFFD_CLOEXEC O_CLOEXEC
28 #define UFFD_NONBLOCK O_NONBLOCK
30 #define UFFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
31 #define UFFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS)
33 extern int sysctl_unprivileged_userfaultfd;
35 extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason);
37 extern ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
38 unsigned long src_start, unsigned long len,
39 bool *mmap_changing, __u64 mode);
40 extern ssize_t mfill_zeropage(struct mm_struct *dst_mm,
41 unsigned long dst_start,
44 extern int mwriteprotect_range(struct mm_struct *dst_mm,
45 unsigned long start, unsigned long len,
46 bool enable_wp, bool *mmap_changing);
49 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
50 struct vm_userfaultfd_ctx vm_ctx)
52 return vma->vm_userfaultfd_ctx.ctx == vm_ctx.ctx;
55 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
57 return vma->vm_flags & VM_UFFD_MISSING;
60 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
62 return vma->vm_flags & VM_UFFD_WP;
65 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
68 return userfaultfd_wp(vma) && pte_uffd_wp(pte);
71 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
74 return userfaultfd_wp(vma) && pmd_uffd_wp(pmd);
77 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
79 return vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP);
82 extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *);
83 extern void dup_userfaultfd_complete(struct list_head *);
85 extern void mremap_userfaultfd_prep(struct vm_area_struct *,
86 struct vm_userfaultfd_ctx *);
87 extern void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *,
88 unsigned long from, unsigned long to,
91 extern bool userfaultfd_remove(struct vm_area_struct *vma,
95 extern int userfaultfd_unmap_prep(struct vm_area_struct *vma,
96 unsigned long start, unsigned long end,
97 struct list_head *uf);
98 extern void userfaultfd_unmap_complete(struct mm_struct *mm,
99 struct list_head *uf);
101 #else /* CONFIG_USERFAULTFD */
104 static inline vm_fault_t handle_userfault(struct vm_fault *vmf,
105 unsigned long reason)
107 return VM_FAULT_SIGBUS;
110 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
111 struct vm_userfaultfd_ctx vm_ctx)
116 static inline bool userfaultfd_missing(struct vm_area_struct *vma)
121 static inline bool userfaultfd_wp(struct vm_area_struct *vma)
126 static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
132 static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
139 static inline bool userfaultfd_armed(struct vm_area_struct *vma)
144 static inline int dup_userfaultfd(struct vm_area_struct *vma,
150 static inline void dup_userfaultfd_complete(struct list_head *l)
154 static inline void mremap_userfaultfd_prep(struct vm_area_struct *vma,
155 struct vm_userfaultfd_ctx *ctx)
159 static inline void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *ctx,
166 static inline bool userfaultfd_remove(struct vm_area_struct *vma,
173 static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma,
174 unsigned long start, unsigned long end,
175 struct list_head *uf)
180 static inline void userfaultfd_unmap_complete(struct mm_struct *mm,
181 struct list_head *uf)
185 #endif /* CONFIG_USERFAULTFD */
187 #endif /* _LINUX_USERFAULTFD_K_H */