MAINTAINERS: Include PCI bindings in host bridge entry
[linux-2.6-block.git] / mm / userfaultfd.c
CommitLineData
20c8ccb1 1// SPDX-License-Identifier: GPL-2.0-only
c1a4de99
AA
2/*
3 * mm/userfaultfd.c
4 *
5 * Copyright (C) 2015 Red Hat, Inc.
c1a4de99
AA
6 */
7
8#include <linux/mm.h>
174cd4b1 9#include <linux/sched/signal.h>
c1a4de99
AA
10#include <linux/pagemap.h>
11#include <linux/rmap.h>
12#include <linux/swap.h>
13#include <linux/swapops.h>
14#include <linux/userfaultfd_k.h>
15#include <linux/mmu_notifier.h>
60d4d2d2 16#include <linux/hugetlb.h>
26071ced 17#include <linux/shmem_fs.h>
c1a4de99 18#include <asm/tlbflush.h>
4a18419f 19#include <asm/tlb.h>
c1a4de99
AA
20#include "internal.h"
21
643aa36e
WY
22static __always_inline
23struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm,
24 unsigned long dst_start,
25 unsigned long len)
26{
27 /*
28 * Make sure that the dst range is both valid and fully within a
29 * single existing vma.
30 */
31 struct vm_area_struct *dst_vma;
32
33 dst_vma = find_vma(dst_mm, dst_start);
34 if (!dst_vma)
35 return NULL;
36
37 if (dst_start < dst_vma->vm_start ||
38 dst_start + len > dst_vma->vm_end)
39 return NULL;
40
41 /*
42 * Check the vma is registered in uffd, this is required to
43 * enforce the VM_MAYWRITE check done at uffd registration
44 * time.
45 */
46 if (!dst_vma->vm_userfaultfd_ctx.ctx)
47 return NULL;
48
49 return dst_vma;
50}
51
15313257
AR
52/*
53 * Install PTEs, to map dst_addr (within dst_vma) to page.
54 *
7d64ae3a
AR
55 * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
56 * and anon, and for both shared and private VMAs.
15313257 57 */
7d64ae3a
AR
58int 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)
15313257
AR
62{
63 int ret;
64 pte_t _dst_pte, *dst_pte;
65 bool writable = dst_vma->vm_flags & VM_WRITE;
66 bool vm_shared = dst_vma->vm_flags & VM_SHARED;
67 bool page_in_cache = page->mapping;
68 spinlock_t *ptl;
69 struct inode *inode;
70 pgoff_t offset, max_off;
71
72 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
9ae0f87d 73 _dst_pte = pte_mkdirty(_dst_pte);
15313257
AR
74 if (page_in_cache && !vm_shared)
75 writable = false;
0e88904c
NA
76
77 /*
78 * Always mark a PTE as write-protected when needed, regardless of
79 * VM_WRITE, which the user might change.
80 */
8ee79edf 81 if (wp_copy) {
0e88904c 82 _dst_pte = pte_mkuffd_wp(_dst_pte);
8ee79edf
PX
83 writable = false;
84 }
85
86 if (writable)
0e88904c 87 _dst_pte = pte_mkwrite(_dst_pte);
8ee79edf
PX
88 else
89 /*
90 * We need this to make sure write bit removed; as mk_pte()
91 * could return a pte with write bit set.
92 */
93 _dst_pte = pte_wrprotect(_dst_pte);
15313257
AR
94
95 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
96
97 if (vma_is_shmem(dst_vma)) {
98 /* serialize against truncate with the page table lock */
99 inode = dst_vma->vm_file->f_inode;
100 offset = linear_page_index(dst_vma, dst_addr);
101 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
102 ret = -EFAULT;
103 if (unlikely(offset >= max_off))
104 goto out_unlock;
105 }
106
107 ret = -EEXIST;
8ee79edf
PX
108 /*
109 * We allow to overwrite a pte marker: consider when both MISSING|WP
110 * registered, we firstly wr-protect a none pte which has no page cache
111 * page backing it, then access the page.
112 */
113 if (!pte_none_mostly(*dst_pte))
15313257
AR
114 goto out_unlock;
115
cea86fe2
HD
116 if (page_in_cache) {
117 /* Usually, cache pages are already added to LRU */
118 if (newly_allocated)
119 lru_cache_add(page);
120 page_add_file_rmap(page, dst_vma, false);
121 } else {
40f2bbf7 122 page_add_new_anon_rmap(page, dst_vma, dst_addr);
cea86fe2
HD
123 lru_cache_add_inactive_or_unevictable(page, dst_vma);
124 }
15313257
AR
125
126 /*
127 * Must happen after rmap, as mm_counter() checks mapping (via
128 * PageAnon()), which is set by __page_set_anon_rmap().
129 */
130 inc_mm_counter(dst_mm, mm_counter(page));
131
15313257
AR
132 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
133
134 /* No need to invalidate - it was non-present before */
135 update_mmu_cache(dst_vma, dst_addr, dst_pte);
136 ret = 0;
137out_unlock:
138 pte_unmap_unlock(dst_pte, ptl);
139 return ret;
140}
141
c1a4de99
AA
142static int mcopy_atomic_pte(struct mm_struct *dst_mm,
143 pmd_t *dst_pmd,
144 struct vm_area_struct *dst_vma,
145 unsigned long dst_addr,
b6ebaedb 146 unsigned long src_addr,
72981e0e
AA
147 struct page **pagep,
148 bool wp_copy)
c1a4de99 149{
c1a4de99
AA
150 void *page_kaddr;
151 int ret;
b6ebaedb 152 struct page *page;
c1a4de99 153
b6ebaedb
AA
154 if (!*pagep) {
155 ret = -ENOMEM;
156 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
157 if (!page)
158 goto out;
159
160 page_kaddr = kmap_atomic(page);
161 ret = copy_from_user(page_kaddr,
162 (const void __user *) src_addr,
163 PAGE_SIZE);
164 kunmap_atomic(page_kaddr);
165
c1e8d7c6 166 /* fallback to copy_from_user outside mmap_lock */
b6ebaedb 167 if (unlikely(ret)) {
9e368259 168 ret = -ENOENT;
b6ebaedb
AA
169 *pagep = page;
170 /* don't free the page */
171 goto out;
172 }
7c25a0b8
MS
173
174 flush_dcache_page(page);
b6ebaedb
AA
175 } else {
176 page = *pagep;
177 *pagep = NULL;
178 }
c1a4de99
AA
179
180 /*
181 * The memory barrier inside __SetPageUptodate makes sure that
f4f5329d 182 * preceding stores to the page contents become visible before
c1a4de99
AA
183 * the set_pte_at() write.
184 */
185 __SetPageUptodate(page);
186
187 ret = -ENOMEM;
8f425e4e 188 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL))
c1a4de99
AA
189 goto out_release;
190
15313257
AR
191 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
192 page, true, wp_copy);
193 if (ret)
194 goto out_release;
c1a4de99
AA
195out:
196 return ret;
c1a4de99 197out_release:
09cbfeaf 198 put_page(page);
c1a4de99 199 goto out;
c1a4de99
AA
200}
201
202static int mfill_zeropage_pte(struct mm_struct *dst_mm,
203 pmd_t *dst_pmd,
204 struct vm_area_struct *dst_vma,
205 unsigned long dst_addr)
206{
207 pte_t _dst_pte, *dst_pte;
208 spinlock_t *ptl;
209 int ret;
e2a50c1f
AA
210 pgoff_t offset, max_off;
211 struct inode *inode;
c1a4de99
AA
212
213 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
214 dst_vma->vm_page_prot));
c1a4de99 215 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
e2a50c1f
AA
216 if (dst_vma->vm_file) {
217 /* the shmem MAP_PRIVATE case requires checking the i_size */
218 inode = dst_vma->vm_file->f_inode;
219 offset = linear_page_index(dst_vma, dst_addr);
220 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
221 ret = -EFAULT;
222 if (unlikely(offset >= max_off))
223 goto out_unlock;
224 }
225 ret = -EEXIST;
c1a4de99
AA
226 if (!pte_none(*dst_pte))
227 goto out_unlock;
228 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
229 /* No need to invalidate - it was non-present before */
230 update_mmu_cache(dst_vma, dst_addr, dst_pte);
231 ret = 0;
232out_unlock:
233 pte_unmap_unlock(dst_pte, ptl);
234 return ret;
235}
236
15313257
AR
237/* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
238static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
239 pmd_t *dst_pmd,
240 struct vm_area_struct *dst_vma,
241 unsigned long dst_addr,
242 bool wp_copy)
243{
244 struct inode *inode = file_inode(dst_vma->vm_file);
245 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
12acf4fb 246 struct folio *folio;
15313257
AR
247 struct page *page;
248 int ret;
249
12acf4fb
MWO
250 ret = shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC);
251 /* Our caller expects us to return -EFAULT if we failed to find folio */
73f37dbc
AR
252 if (ret == -ENOENT)
253 ret = -EFAULT;
15313257
AR
254 if (ret)
255 goto out;
12acf4fb 256 if (!folio) {
15313257
AR
257 ret = -EFAULT;
258 goto out;
259 }
260
12acf4fb 261 page = folio_file_page(folio, pgoff);
a7605426
YS
262 if (PageHWPoison(page)) {
263 ret = -EIO;
264 goto out_release;
265 }
266
15313257
AR
267 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
268 page, false, wp_copy);
269 if (ret)
270 goto out_release;
271
12acf4fb 272 folio_unlock(folio);
15313257
AR
273 ret = 0;
274out:
275 return ret;
276out_release:
12acf4fb
MWO
277 folio_unlock(folio);
278 folio_put(folio);
15313257
AR
279 goto out;
280}
281
c1a4de99
AA
282static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
283{
284 pgd_t *pgd;
c2febafc 285 p4d_t *p4d;
c1a4de99 286 pud_t *pud;
c1a4de99
AA
287
288 pgd = pgd_offset(mm, address);
c2febafc
KS
289 p4d = p4d_alloc(mm, pgd, address);
290 if (!p4d)
291 return NULL;
292 pud = pud_alloc(mm, p4d, address);
293 if (!pud)
294 return NULL;
295 /*
296 * Note that we didn't run this because the pmd was
297 * missing, the *pmd may be already established and in
298 * turn it may also be a trans_huge_pmd.
299 */
300 return pmd_alloc(mm, pud, address);
c1a4de99
AA
301}
302
60d4d2d2
MK
303#ifdef CONFIG_HUGETLB_PAGE
304/*
305 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
c1e8d7c6 306 * called with mmap_lock held, it will release mmap_lock before returning.
60d4d2d2
MK
307 */
308static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
309 struct vm_area_struct *dst_vma,
310 unsigned long dst_start,
311 unsigned long src_start,
312 unsigned long len,
6041c691
PX
313 enum mcopy_atomic_mode mode,
314 bool wp_copy)
60d4d2d2 315{
1c9e8def 316 int vm_shared = dst_vma->vm_flags & VM_SHARED;
60d4d2d2
MK
317 ssize_t err;
318 pte_t *dst_pte;
319 unsigned long src_addr, dst_addr;
320 long copied;
321 struct page *page;
60d4d2d2
MK
322 unsigned long vma_hpagesize;
323 pgoff_t idx;
324 u32 hash;
325 struct address_space *mapping;
326
327 /*
328 * There is no default zero huge page for all huge page sizes as
329 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
330 * by THP. Since we can not reliably insert a zero page, this
331 * feature is not supported.
332 */
f6191471 333 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
d8ed45c5 334 mmap_read_unlock(dst_mm);
60d4d2d2
MK
335 return -EINVAL;
336 }
337
338 src_addr = src_start;
339 dst_addr = dst_start;
340 copied = 0;
341 page = NULL;
342 vma_hpagesize = vma_kernel_pagesize(dst_vma);
343
344 /*
345 * Validate alignment based on huge page size
346 */
347 err = -EINVAL;
348 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
349 goto out_unlock;
350
351retry:
352 /*
c1e8d7c6 353 * On routine entry dst_vma is set. If we had to drop mmap_lock and
60d4d2d2
MK
354 * retry, dst_vma will be set to NULL and we must lookup again.
355 */
356 if (!dst_vma) {
27d02568 357 err = -ENOENT;
643aa36e 358 dst_vma = find_dst_vma(dst_mm, dst_start, len);
60d4d2d2
MK
359 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
360 goto out_unlock;
1c9e8def 361
27d02568
MR
362 err = -EINVAL;
363 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
364 goto out_unlock;
365
1c9e8def 366 vm_shared = dst_vma->vm_flags & VM_SHARED;
60d4d2d2
MK
367 }
368
60d4d2d2 369 /*
1c9e8def 370 * If not shared, ensure the dst_vma has a anon_vma.
60d4d2d2
MK
371 */
372 err = -ENOMEM;
1c9e8def
MK
373 if (!vm_shared) {
374 if (unlikely(anon_vma_prepare(dst_vma)))
375 goto out_unlock;
376 }
60d4d2d2 377
60d4d2d2 378 while (src_addr < src_start + len) {
60d4d2d2 379 BUG_ON(dst_addr >= dst_start + len);
60d4d2d2
MK
380
381 /*
40549ba8
MK
382 * Serialize via vma_lock and hugetlb_fault_mutex.
383 * vma_lock ensures the dst_pte remains valid even
384 * in the case of shared pmds. fault mutex prevents
385 * races with other faulting threads.
60d4d2d2 386 */
c0d0381a 387 idx = linear_page_index(dst_vma, dst_addr);
3a47c54f 388 mapping = dst_vma->vm_file->f_mapping;
188b04a7 389 hash = hugetlb_fault_mutex_hash(mapping, idx);
60d4d2d2 390 mutex_lock(&hugetlb_fault_mutex_table[hash]);
40549ba8 391 hugetlb_vma_lock_read(dst_vma);
60d4d2d2
MK
392
393 err = -ENOMEM;
aec44e0f 394 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
60d4d2d2 395 if (!dst_pte) {
40549ba8 396 hugetlb_vma_unlock_read(dst_vma);
60d4d2d2
MK
397 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
398 goto out_unlock;
399 }
400
f6191471 401 if (mode != MCOPY_ATOMIC_CONTINUE &&
6041c691 402 !huge_pte_none_mostly(huge_ptep_get(dst_pte))) {
f6191471 403 err = -EEXIST;
40549ba8 404 hugetlb_vma_unlock_read(dst_vma);
60d4d2d2
MK
405 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
406 goto out_unlock;
407 }
408
409 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
6041c691
PX
410 dst_addr, src_addr, mode, &page,
411 wp_copy);
60d4d2d2 412
40549ba8 413 hugetlb_vma_unlock_read(dst_vma);
60d4d2d2
MK
414 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
415
416 cond_resched();
417
9e368259 418 if (unlikely(err == -ENOENT)) {
d8ed45c5 419 mmap_read_unlock(dst_mm);
60d4d2d2
MK
420 BUG_ON(!page);
421
422 err = copy_huge_page_from_user(page,
423 (const void __user *)src_addr,
4fb07ee6
WY
424 vma_hpagesize / PAGE_SIZE,
425 true);
60d4d2d2
MK
426 if (unlikely(err)) {
427 err = -EFAULT;
428 goto out;
429 }
d8ed45c5 430 mmap_read_lock(dst_mm);
60d4d2d2
MK
431
432 dst_vma = NULL;
433 goto retry;
434 } else
435 BUG_ON(page);
436
437 if (!err) {
438 dst_addr += vma_hpagesize;
439 src_addr += vma_hpagesize;
440 copied += vma_hpagesize;
441
442 if (fatal_signal_pending(current))
443 err = -EINTR;
444 }
445 if (err)
446 break;
447 }
448
449out_unlock:
d8ed45c5 450 mmap_read_unlock(dst_mm);
60d4d2d2 451out:
8cc5fcbb 452 if (page)
60d4d2d2
MK
453 put_page(page);
454 BUG_ON(copied < 0);
455 BUG_ON(err > 0);
456 BUG_ON(!copied && !err);
457 return copied ? copied : err;
458}
459#else /* !CONFIG_HUGETLB_PAGE */
460/* fail at build time if gcc attempts to use this */
461extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
462 struct vm_area_struct *dst_vma,
463 unsigned long dst_start,
464 unsigned long src_start,
465 unsigned long len,
6041c691
PX
466 enum mcopy_atomic_mode mode,
467 bool wp_copy);
60d4d2d2
MK
468#endif /* CONFIG_HUGETLB_PAGE */
469
3217d3c7
MR
470static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
471 pmd_t *dst_pmd,
472 struct vm_area_struct *dst_vma,
473 unsigned long dst_addr,
474 unsigned long src_addr,
475 struct page **page,
15313257 476 enum mcopy_atomic_mode mode,
72981e0e 477 bool wp_copy)
3217d3c7
MR
478{
479 ssize_t err;
480
15313257
AR
481 if (mode == MCOPY_ATOMIC_CONTINUE) {
482 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
483 wp_copy);
484 }
485
5b51072e
AA
486 /*
487 * The normal page fault path for a shmem will invoke the
488 * fault, fill the hole in the file and COW it right away. The
489 * result generates plain anonymous memory. So when we are
490 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
491 * generate anonymous memory directly without actually filling
492 * the hole. For the MAP_PRIVATE case the robustness check
493 * only happens in the pagetable (to verify it's still none)
494 * and not in the radix tree.
495 */
496 if (!(dst_vma->vm_flags & VM_SHARED)) {
15313257 497 if (mode == MCOPY_ATOMIC_NORMAL)
3217d3c7 498 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
72981e0e
AA
499 dst_addr, src_addr, page,
500 wp_copy);
3217d3c7
MR
501 else
502 err = mfill_zeropage_pte(dst_mm, dst_pmd,
503 dst_vma, dst_addr);
504 } else {
3460f6e5 505 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
15313257
AR
506 dst_addr, src_addr,
507 mode != MCOPY_ATOMIC_NORMAL,
8ee79edf 508 wp_copy, page);
3217d3c7
MR
509 }
510
511 return err;
512}
513
c1a4de99
AA
514static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
515 unsigned long dst_start,
516 unsigned long src_start,
517 unsigned long len,
f6191471 518 enum mcopy_atomic_mode mcopy_mode,
a759a909 519 atomic_t *mmap_changing,
72981e0e 520 __u64 mode)
c1a4de99
AA
521{
522 struct vm_area_struct *dst_vma;
523 ssize_t err;
524 pmd_t *dst_pmd;
525 unsigned long src_addr, dst_addr;
b6ebaedb
AA
526 long copied;
527 struct page *page;
72981e0e 528 bool wp_copy;
c1a4de99
AA
529
530 /*
531 * Sanitize the command parameters:
532 */
533 BUG_ON(dst_start & ~PAGE_MASK);
534 BUG_ON(len & ~PAGE_MASK);
535
536 /* Does the address range wrap, or is the span zero-sized? */
537 BUG_ON(src_start + len <= src_start);
538 BUG_ON(dst_start + len <= dst_start);
539
b6ebaedb
AA
540 src_addr = src_start;
541 dst_addr = dst_start;
542 copied = 0;
543 page = NULL;
544retry:
d8ed45c5 545 mmap_read_lock(dst_mm);
c1a4de99 546
df2cc96e
MR
547 /*
548 * If memory mappings are changing because of non-cooperative
549 * operation (e.g. mremap) running in parallel, bail out and
550 * request the user to retry later
551 */
552 err = -EAGAIN;
a759a909 553 if (mmap_changing && atomic_read(mmap_changing))
df2cc96e
MR
554 goto out_unlock;
555
c1a4de99
AA
556 /*
557 * Make sure the vma is not shared, that the dst range is
558 * both valid and fully within a single existing vma.
559 */
27d02568 560 err = -ENOENT;
643aa36e 561 dst_vma = find_dst_vma(dst_mm, dst_start, len);
26071ced
MR
562 if (!dst_vma)
563 goto out_unlock;
c1a4de99 564
27d02568
MR
565 err = -EINVAL;
566 /*
567 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
568 * it will overwrite vm_ops, so vma_is_anonymous must return false.
569 */
570 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
571 dst_vma->vm_flags & VM_SHARED))
572 goto out_unlock;
573
72981e0e
AA
574 /*
575 * validate 'mode' now that we know the dst_vma: don't allow
576 * a wrprotect copy if the userfaultfd didn't register as WP.
577 */
578 wp_copy = mode & UFFDIO_COPY_MODE_WP;
579 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
580 goto out_unlock;
581
60d4d2d2
MK
582 /*
583 * If this is a HUGETLB vma, pass off to appropriate routine
584 */
585 if (is_vm_hugetlb_page(dst_vma))
586 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
6041c691
PX
587 src_start, len, mcopy_mode,
588 wp_copy);
60d4d2d2 589
26071ced 590 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
b6ebaedb 591 goto out_unlock;
15313257 592 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
f6191471 593 goto out_unlock;
c1a4de99
AA
594
595 /*
596 * Ensure the dst_vma has a anon_vma or this page
597 * would get a NULL anon_vma when moved in the
598 * dst_vma.
599 */
600 err = -ENOMEM;
5b51072e
AA
601 if (!(dst_vma->vm_flags & VM_SHARED) &&
602 unlikely(anon_vma_prepare(dst_vma)))
b6ebaedb 603 goto out_unlock;
c1a4de99 604
b6ebaedb 605 while (src_addr < src_start + len) {
c1a4de99 606 pmd_t dst_pmdval;
b6ebaedb 607
c1a4de99 608 BUG_ON(dst_addr >= dst_start + len);
b6ebaedb 609
c1a4de99
AA
610 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
611 if (unlikely(!dst_pmd)) {
612 err = -ENOMEM;
613 break;
614 }
615
616 dst_pmdval = pmd_read_atomic(dst_pmd);
617 /*
618 * If the dst_pmd is mapped as THP don't
619 * override it and just be strict.
620 */
621 if (unlikely(pmd_trans_huge(dst_pmdval))) {
622 err = -EEXIST;
623 break;
624 }
625 if (unlikely(pmd_none(dst_pmdval)) &&
4cf58924 626 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
c1a4de99
AA
627 err = -ENOMEM;
628 break;
629 }
630 /* If an huge pmd materialized from under us fail */
631 if (unlikely(pmd_trans_huge(*dst_pmd))) {
632 err = -EFAULT;
633 break;
634 }
635
636 BUG_ON(pmd_none(*dst_pmd));
637 BUG_ON(pmd_trans_huge(*dst_pmd));
638
3217d3c7 639 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
15313257 640 src_addr, &page, mcopy_mode, wp_copy);
c1a4de99
AA
641 cond_resched();
642
9e368259 643 if (unlikely(err == -ENOENT)) {
b6ebaedb
AA
644 void *page_kaddr;
645
d8ed45c5 646 mmap_read_unlock(dst_mm);
b6ebaedb
AA
647 BUG_ON(!page);
648
649 page_kaddr = kmap(page);
650 err = copy_from_user(page_kaddr,
651 (const void __user *) src_addr,
652 PAGE_SIZE);
653 kunmap(page);
654 if (unlikely(err)) {
655 err = -EFAULT;
656 goto out;
657 }
7c25a0b8 658 flush_dcache_page(page);
b6ebaedb
AA
659 goto retry;
660 } else
661 BUG_ON(page);
662
c1a4de99
AA
663 if (!err) {
664 dst_addr += PAGE_SIZE;
665 src_addr += PAGE_SIZE;
666 copied += PAGE_SIZE;
667
668 if (fatal_signal_pending(current))
669 err = -EINTR;
670 }
671 if (err)
672 break;
673 }
674
b6ebaedb 675out_unlock:
d8ed45c5 676 mmap_read_unlock(dst_mm);
b6ebaedb
AA
677out:
678 if (page)
09cbfeaf 679 put_page(page);
c1a4de99
AA
680 BUG_ON(copied < 0);
681 BUG_ON(err > 0);
682 BUG_ON(!copied && !err);
683 return copied ? copied : err;
684}
685
686ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
df2cc96e 687 unsigned long src_start, unsigned long len,
a759a909 688 atomic_t *mmap_changing, __u64 mode)
c1a4de99 689{
f6191471
AR
690 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
691 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
c1a4de99
AA
692}
693
694ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
a759a909 695 unsigned long len, atomic_t *mmap_changing)
c1a4de99 696{
f6191471
AR
697 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
698 mmap_changing, 0);
699}
700
701ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
a759a909 702 unsigned long len, atomic_t *mmap_changing)
f6191471
AR
703{
704 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
705 mmap_changing, 0);
c1a4de99 706}
ffd05793 707
f369b07c
PX
708void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
709 unsigned long start, unsigned long len, bool enable_wp)
710{
711 struct mmu_gather tlb;
712 pgprot_t newprot;
713
714 if (enable_wp)
715 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
716 else
717 newprot = vm_get_page_prot(dst_vma->vm_flags);
718
719 tlb_gather_mmu(&tlb, dst_mm);
720 change_protection(&tlb, dst_vma, start, start + len, newprot,
721 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
722 tlb_finish_mmu(&tlb);
723}
724
ffd05793 725int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
a759a909
NA
726 unsigned long len, bool enable_wp,
727 atomic_t *mmap_changing)
ffd05793
SL
728{
729 struct vm_area_struct *dst_vma;
5a90d5a1 730 unsigned long page_mask;
ffd05793
SL
731 int err;
732
733 /*
734 * Sanitize the command parameters:
735 */
736 BUG_ON(start & ~PAGE_MASK);
737 BUG_ON(len & ~PAGE_MASK);
738
739 /* Does the address range wrap, or is the span zero-sized? */
740 BUG_ON(start + len <= start);
741
d8ed45c5 742 mmap_read_lock(dst_mm);
ffd05793
SL
743
744 /*
745 * If memory mappings are changing because of non-cooperative
746 * operation (e.g. mremap) running in parallel, bail out and
747 * request the user to retry later
748 */
749 err = -EAGAIN;
a759a909 750 if (mmap_changing && atomic_read(mmap_changing))
ffd05793
SL
751 goto out_unlock;
752
753 err = -ENOENT;
754 dst_vma = find_dst_vma(dst_mm, start, len);
b1f9e876
PX
755
756 if (!dst_vma)
ffd05793
SL
757 goto out_unlock;
758 if (!userfaultfd_wp(dst_vma))
759 goto out_unlock;
b1f9e876 760 if (!vma_can_userfault(dst_vma, dst_vma->vm_flags))
ffd05793
SL
761 goto out_unlock;
762
5a90d5a1
PX
763 if (is_vm_hugetlb_page(dst_vma)) {
764 err = -EINVAL;
765 page_mask = vma_kernel_pagesize(dst_vma) - 1;
766 if ((start & page_mask) || (len & page_mask))
767 goto out_unlock;
768 }
769
f369b07c 770 uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp);
ffd05793
SL
771
772 err = 0;
773out_unlock:
d8ed45c5 774 mmap_read_unlock(dst_mm);
ffd05793
SL
775 return err;
776}