mm/pagemap: recognize uffd-wp bit for shmem/hugetlbfs
[linux-block.git] / include / linux / userfaultfd_k.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
932b18e0
AA
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>
55adf4de 17#include <linux/mm.h>
1db9dbc2
PX
18#include <linux/swap.h>
19#include <linux/swapops.h>
55adf4de 20#include <asm-generic/pgtable_uffd.h>
932b18e0 21
7677f7fd
AR
22/* The set of all possible UFFD-related VM flags. */
23#define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)
24
932b18e0
AA
25/*
26 * CAREFUL: Check include/uapi/asm-generic/fcntl.h when defining
27 * new flags, since they might collide with O_* ones. We want
28 * to re-use O_* flags that couldn't possibly have a meaning
29 * from userfaultfd, in order to leave a free define-space for
30 * shared O_* flags.
31 */
32#define UFFD_CLOEXEC O_CLOEXEC
33#define UFFD_NONBLOCK O_NONBLOCK
34
35#define UFFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
36#define UFFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS)
37
cefdca0a
PX
38extern int sysctl_unprivileged_userfaultfd;
39
2b740303 40extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason);
932b18e0 41
f6191471
AR
42/*
43 * The mode of operation for __mcopy_atomic and its helpers.
44 *
45 * This is almost an implementation detail (mcopy_atomic below doesn't take this
46 * as a parameter), but it's exposed here because memory-kind-specific
47 * implementations (e.g. hugetlbfs) need to know the mode of operation.
48 */
49enum mcopy_atomic_mode {
50 /* A normal copy_from_user into the destination range. */
51 MCOPY_ATOMIC_NORMAL,
52 /* Don't copy; map the destination range to the zero page. */
53 MCOPY_ATOMIC_ZEROPAGE,
54 /* Just install pte(s) with the existing page(s) in the page cache. */
55 MCOPY_ATOMIC_CONTINUE,
56};
57
7d64ae3a
AR
58extern int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
59 struct vm_area_struct *dst_vma,
60 unsigned long dst_addr, struct page *page,
61 bool newly_allocated, bool wp_copy);
62
c1a4de99 63extern ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
df2cc96e 64 unsigned long src_start, unsigned long len,
a759a909 65 atomic_t *mmap_changing, __u64 mode);
c1a4de99
AA
66extern ssize_t mfill_zeropage(struct mm_struct *dst_mm,
67 unsigned long dst_start,
df2cc96e 68 unsigned long len,
a759a909 69 atomic_t *mmap_changing);
f6191471 70extern ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long dst_start,
a759a909 71 unsigned long len, atomic_t *mmap_changing);
ffd05793
SL
72extern int mwriteprotect_range(struct mm_struct *dst_mm,
73 unsigned long start, unsigned long len,
a759a909 74 bool enable_wp, atomic_t *mmap_changing);
c1a4de99 75
932b18e0
AA
76/* mm helpers */
77static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
78 struct vm_userfaultfd_ctx vm_ctx)
79{
80 return vma->vm_userfaultfd_ctx.ctx == vm_ctx.ctx;
81}
82
c1991e07 83/*
0d9cadab
AR
84 * Never enable huge pmd sharing on some uffd registered vmas:
85 *
86 * - VM_UFFD_WP VMAs, because write protect information is per pgtable entry.
87 *
88 * - VM_UFFD_MINOR VMAs, because otherwise we would never get minor faults for
89 * VMAs which share huge pmds. (If you have two mappings to the same
90 * underlying pages, and fault in the non-UFFD-registered one with a write,
91 * with huge pmd sharing this would *also* setup the second UFFD-registered
92 * mapping, and we'd not get minor faults.)
c1991e07
PX
93 */
94static inline bool uffd_disable_huge_pmd_share(struct vm_area_struct *vma)
95{
0d9cadab 96 return vma->vm_flags & (VM_UFFD_WP | VM_UFFD_MINOR);
c1991e07
PX
97}
98
9c28a205
PX
99/*
100 * Don't do fault around for either WP or MINOR registered uffd range. For
101 * MINOR registered range, fault around will be a total disaster and ptes can
102 * be installed without notifications; for WP it should mostly be fine as long
103 * as the fault around checks for pte_none() before the installation, however
104 * to be super safe we just forbid it.
105 */
106static inline bool uffd_disable_fault_around(struct vm_area_struct *vma)
107{
108 return vma->vm_flags & (VM_UFFD_WP | VM_UFFD_MINOR);
109}
110
932b18e0
AA
111static inline bool userfaultfd_missing(struct vm_area_struct *vma)
112{
113 return vma->vm_flags & VM_UFFD_MISSING;
114}
115
1df319e0
SL
116static inline bool userfaultfd_wp(struct vm_area_struct *vma)
117{
118 return vma->vm_flags & VM_UFFD_WP;
119}
120
7677f7fd
AR
121static inline bool userfaultfd_minor(struct vm_area_struct *vma)
122{
123 return vma->vm_flags & VM_UFFD_MINOR;
124}
125
55adf4de
AA
126static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
127 pte_t pte)
128{
129 return userfaultfd_wp(vma) && pte_uffd_wp(pte);
130}
131
132static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
133 pmd_t pmd)
134{
135 return userfaultfd_wp(vma) && pmd_uffd_wp(pmd);
136}
137
932b18e0
AA
138static inline bool userfaultfd_armed(struct vm_area_struct *vma)
139{
7677f7fd 140 return vma->vm_flags & __VM_UFFD_FLAGS;
932b18e0
AA
141}
142
893e26e6
PE
143extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *);
144extern void dup_userfaultfd_complete(struct list_head *);
145
72f87654
PE
146extern void mremap_userfaultfd_prep(struct vm_area_struct *,
147 struct vm_userfaultfd_ctx *);
90794bf1 148extern void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *,
72f87654
PE
149 unsigned long from, unsigned long to,
150 unsigned long len);
151
70ccb92f 152extern bool userfaultfd_remove(struct vm_area_struct *vma,
d811914d
MR
153 unsigned long start,
154 unsigned long end);
05ce7724 155
897ab3e0
MR
156extern int userfaultfd_unmap_prep(struct vm_area_struct *vma,
157 unsigned long start, unsigned long end,
158 struct list_head *uf);
159extern void userfaultfd_unmap_complete(struct mm_struct *mm,
160 struct list_head *uf);
161
932b18e0
AA
162#else /* CONFIG_USERFAULTFD */
163
164/* mm helpers */
2b740303
SJ
165static inline vm_fault_t handle_userfault(struct vm_fault *vmf,
166 unsigned long reason)
932b18e0
AA
167{
168 return VM_FAULT_SIGBUS;
169}
170
171static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
172 struct vm_userfaultfd_ctx vm_ctx)
173{
174 return true;
175}
176
177static inline bool userfaultfd_missing(struct vm_area_struct *vma)
178{
179 return false;
180}
181
1df319e0
SL
182static inline bool userfaultfd_wp(struct vm_area_struct *vma)
183{
184 return false;
185}
186
7677f7fd
AR
187static inline bool userfaultfd_minor(struct vm_area_struct *vma)
188{
189 return false;
190}
191
55adf4de
AA
192static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma,
193 pte_t pte)
194{
195 return false;
196}
197
198static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma,
199 pmd_t pmd)
200{
201 return false;
202}
203
204
932b18e0
AA
205static inline bool userfaultfd_armed(struct vm_area_struct *vma)
206{
207 return false;
208}
209
893e26e6
PE
210static inline int dup_userfaultfd(struct vm_area_struct *vma,
211 struct list_head *l)
212{
213 return 0;
214}
215
216static inline void dup_userfaultfd_complete(struct list_head *l)
217{
218}
219
72f87654
PE
220static inline void mremap_userfaultfd_prep(struct vm_area_struct *vma,
221 struct vm_userfaultfd_ctx *ctx)
222{
223}
224
90794bf1 225static inline void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *ctx,
72f87654
PE
226 unsigned long from,
227 unsigned long to,
228 unsigned long len)
229{
230}
05ce7724 231
70ccb92f 232static inline bool userfaultfd_remove(struct vm_area_struct *vma,
d811914d
MR
233 unsigned long start,
234 unsigned long end)
05ce7724 235{
70ccb92f 236 return true;
05ce7724 237}
897ab3e0
MR
238
239static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma,
240 unsigned long start, unsigned long end,
241 struct list_head *uf)
242{
243 return 0;
244}
245
246static inline void userfaultfd_unmap_complete(struct mm_struct *mm,
247 struct list_head *uf)
248{
249}
ca49ca71 250
9c28a205
PX
251static inline bool uffd_disable_fault_around(struct vm_area_struct *vma)
252{
253 return false;
254}
255
932b18e0
AA
256#endif /* CONFIG_USERFAULTFD */
257
1db9dbc2
PX
258static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry)
259{
260#ifdef CONFIG_PTE_MARKER_UFFD_WP
261 return is_pte_marker_entry(entry) &&
262 (pte_marker_get(entry) & PTE_MARKER_UFFD_WP);
263#else
264 return false;
265#endif
266}
267
268static inline bool pte_marker_uffd_wp(pte_t pte)
269{
270#ifdef CONFIG_PTE_MARKER_UFFD_WP
271 swp_entry_t entry;
272
273 if (!is_swap_pte(pte))
274 return false;
275
276 entry = pte_to_swp_entry(pte);
277
278 return pte_marker_entry_uffd_wp(entry);
279#else
280 return false;
281#endif
282}
283
284/*
285 * Returns true if this is a swap pte and was uffd-wp wr-protected in either
286 * forms (pte marker or a normal swap pte), false otherwise.
287 */
288static inline bool pte_swp_uffd_wp_any(pte_t pte)
289{
290#ifdef CONFIG_PTE_MARKER_UFFD_WP
291 if (!is_swap_pte(pte))
292 return false;
293
294 if (pte_swp_uffd_wp(pte))
295 return true;
296
297 if (pte_marker_uffd_wp(pte))
298 return true;
299#endif
300 return false;
301}
302
932b18e0 303#endif /* _LINUX_USERFAULTFD_K_H */