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