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