xref: /openbmc/linux/mm/userfaultfd.c (revision cef69974)
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
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  */
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 	_dst_pte = pte_mkdirty(_dst_pte);
73 	if (page_in_cache && !vm_shared)
74 		writable = false;
75 	if (writable) {
76 		if (wp_copy)
77 			_dst_pte = pte_mkuffd_wp(_dst_pte);
78 		else
79 			_dst_pte = pte_mkwrite(_dst_pte);
80 	}
81 
82 	dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
83 
84 	if (vma_is_shmem(dst_vma)) {
85 		/* serialize against truncate with the page table lock */
86 		inode = dst_vma->vm_file->f_inode;
87 		offset = linear_page_index(dst_vma, dst_addr);
88 		max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
89 		ret = -EFAULT;
90 		if (unlikely(offset >= max_off))
91 			goto out_unlock;
92 	}
93 
94 	ret = -EEXIST;
95 	if (!pte_none(*dst_pte))
96 		goto out_unlock;
97 
98 	if (page_in_cache) {
99 		/* Usually, cache pages are already added to LRU */
100 		if (newly_allocated)
101 			lru_cache_add(page);
102 		page_add_file_rmap(page, dst_vma, false);
103 	} else {
104 		page_add_new_anon_rmap(page, dst_vma, dst_addr, false);
105 		lru_cache_add_inactive_or_unevictable(page, dst_vma);
106 	}
107 
108 	/*
109 	 * Must happen after rmap, as mm_counter() checks mapping (via
110 	 * PageAnon()), which is set by __page_set_anon_rmap().
111 	 */
112 	inc_mm_counter(dst_mm, mm_counter(page));
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 
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_folio(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 
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). */
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_READ);
232 	if (ret)
233 		goto out;
234 	if (!page) {
235 		ret = -EFAULT;
236 		goto out;
237 	}
238 
239 	if (PageHWPoison(page)) {
240 		ret = -EIO;
241 		goto out_release;
242 	}
243 
244 	ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
245 				       page, false, wp_copy);
246 	if (ret)
247 		goto out_release;
248 
249 	unlock_page(page);
250 	ret = 0;
251 out:
252 	return ret;
253 out_release:
254 	unlock_page(page);
255 	put_page(page);
256 	goto out;
257 }
258 
259 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
260 {
261 	pgd_t *pgd;
262 	p4d_t *p4d;
263 	pud_t *pud;
264 
265 	pgd = pgd_offset(mm, address);
266 	p4d = p4d_alloc(mm, pgd, address);
267 	if (!p4d)
268 		return NULL;
269 	pud = pud_alloc(mm, p4d, address);
270 	if (!pud)
271 		return NULL;
272 	/*
273 	 * Note that we didn't run this because the pmd was
274 	 * missing, the *pmd may be already established and in
275 	 * turn it may also be a trans_huge_pmd.
276 	 */
277 	return pmd_alloc(mm, pud, address);
278 }
279 
280 #ifdef CONFIG_HUGETLB_PAGE
281 /*
282  * __mcopy_atomic processing for HUGETLB vmas.  Note that this routine is
283  * called with mmap_lock held, it will release mmap_lock before returning.
284  */
285 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
286 					      struct vm_area_struct *dst_vma,
287 					      unsigned long dst_start,
288 					      unsigned long src_start,
289 					      unsigned long len,
290 					      enum mcopy_atomic_mode mode)
291 {
292 	int vm_shared = dst_vma->vm_flags & VM_SHARED;
293 	ssize_t err;
294 	pte_t *dst_pte;
295 	unsigned long src_addr, dst_addr;
296 	long copied;
297 	struct page *page;
298 	unsigned long vma_hpagesize;
299 	pgoff_t idx;
300 	u32 hash;
301 	struct address_space *mapping;
302 
303 	/*
304 	 * There is no default zero huge page for all huge page sizes as
305 	 * supported by hugetlb.  A PMD_SIZE huge pages may exist as used
306 	 * by THP.  Since we can not reliably insert a zero page, this
307 	 * feature is not supported.
308 	 */
309 	if (mode == MCOPY_ATOMIC_ZEROPAGE) {
310 		mmap_read_unlock(dst_mm);
311 		return -EINVAL;
312 	}
313 
314 	src_addr = src_start;
315 	dst_addr = dst_start;
316 	copied = 0;
317 	page = NULL;
318 	vma_hpagesize = vma_kernel_pagesize(dst_vma);
319 
320 	/*
321 	 * Validate alignment based on huge page size
322 	 */
323 	err = -EINVAL;
324 	if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
325 		goto out_unlock;
326 
327 retry:
328 	/*
329 	 * On routine entry dst_vma is set.  If we had to drop mmap_lock and
330 	 * retry, dst_vma will be set to NULL and we must lookup again.
331 	 */
332 	if (!dst_vma) {
333 		err = -ENOENT;
334 		dst_vma = find_dst_vma(dst_mm, dst_start, len);
335 		if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
336 			goto out_unlock;
337 
338 		err = -EINVAL;
339 		if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
340 			goto out_unlock;
341 
342 		vm_shared = dst_vma->vm_flags & VM_SHARED;
343 	}
344 
345 	/*
346 	 * If not shared, ensure the dst_vma has a anon_vma.
347 	 */
348 	err = -ENOMEM;
349 	if (!vm_shared) {
350 		if (unlikely(anon_vma_prepare(dst_vma)))
351 			goto out_unlock;
352 	}
353 
354 	while (src_addr < src_start + len) {
355 		BUG_ON(dst_addr >= dst_start + len);
356 
357 		/*
358 		 * Serialize via i_mmap_rwsem and hugetlb_fault_mutex.
359 		 * i_mmap_rwsem ensures the dst_pte remains valid even
360 		 * in the case of shared pmds.  fault mutex prevents
361 		 * races with other faulting threads.
362 		 */
363 		mapping = dst_vma->vm_file->f_mapping;
364 		i_mmap_lock_read(mapping);
365 		idx = linear_page_index(dst_vma, dst_addr);
366 		hash = hugetlb_fault_mutex_hash(mapping, idx);
367 		mutex_lock(&hugetlb_fault_mutex_table[hash]);
368 
369 		err = -ENOMEM;
370 		dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
371 		if (!dst_pte) {
372 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
373 			i_mmap_unlock_read(mapping);
374 			goto out_unlock;
375 		}
376 
377 		if (mode != MCOPY_ATOMIC_CONTINUE &&
378 		    !huge_pte_none(huge_ptep_get(dst_pte))) {
379 			err = -EEXIST;
380 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
381 			i_mmap_unlock_read(mapping);
382 			goto out_unlock;
383 		}
384 
385 		err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
386 					       dst_addr, src_addr, mode, &page);
387 
388 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
389 		i_mmap_unlock_read(mapping);
390 
391 		cond_resched();
392 
393 		if (unlikely(err == -ENOENT)) {
394 			mmap_read_unlock(dst_mm);
395 			BUG_ON(!page);
396 
397 			err = copy_huge_page_from_user(page,
398 						(const void __user *)src_addr,
399 						vma_hpagesize / PAGE_SIZE,
400 						true);
401 			if (unlikely(err)) {
402 				err = -EFAULT;
403 				goto out;
404 			}
405 			mmap_read_lock(dst_mm);
406 
407 			dst_vma = NULL;
408 			goto retry;
409 		} else
410 			BUG_ON(page);
411 
412 		if (!err) {
413 			dst_addr += vma_hpagesize;
414 			src_addr += vma_hpagesize;
415 			copied += vma_hpagesize;
416 
417 			if (fatal_signal_pending(current))
418 				err = -EINTR;
419 		}
420 		if (err)
421 			break;
422 	}
423 
424 out_unlock:
425 	mmap_read_unlock(dst_mm);
426 out:
427 	if (page)
428 		put_page(page);
429 	BUG_ON(copied < 0);
430 	BUG_ON(err > 0);
431 	BUG_ON(!copied && !err);
432 	return copied ? copied : err;
433 }
434 #else /* !CONFIG_HUGETLB_PAGE */
435 /* fail at build time if gcc attempts to use this */
436 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
437 				      struct vm_area_struct *dst_vma,
438 				      unsigned long dst_start,
439 				      unsigned long src_start,
440 				      unsigned long len,
441 				      enum mcopy_atomic_mode mode);
442 #endif /* CONFIG_HUGETLB_PAGE */
443 
444 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
445 						pmd_t *dst_pmd,
446 						struct vm_area_struct *dst_vma,
447 						unsigned long dst_addr,
448 						unsigned long src_addr,
449 						struct page **page,
450 						enum mcopy_atomic_mode mode,
451 						bool wp_copy)
452 {
453 	ssize_t err;
454 
455 	if (mode == MCOPY_ATOMIC_CONTINUE) {
456 		return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
457 					    wp_copy);
458 	}
459 
460 	/*
461 	 * The normal page fault path for a shmem will invoke the
462 	 * fault, fill the hole in the file and COW it right away. The
463 	 * result generates plain anonymous memory. So when we are
464 	 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
465 	 * generate anonymous memory directly without actually filling
466 	 * the hole. For the MAP_PRIVATE case the robustness check
467 	 * only happens in the pagetable (to verify it's still none)
468 	 * and not in the radix tree.
469 	 */
470 	if (!(dst_vma->vm_flags & VM_SHARED)) {
471 		if (mode == MCOPY_ATOMIC_NORMAL)
472 			err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
473 					       dst_addr, src_addr, page,
474 					       wp_copy);
475 		else
476 			err = mfill_zeropage_pte(dst_mm, dst_pmd,
477 						 dst_vma, dst_addr);
478 	} else {
479 		VM_WARN_ON_ONCE(wp_copy);
480 		err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
481 					     dst_addr, src_addr,
482 					     mode != MCOPY_ATOMIC_NORMAL,
483 					     page);
484 	}
485 
486 	return err;
487 }
488 
489 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
490 					      unsigned long dst_start,
491 					      unsigned long src_start,
492 					      unsigned long len,
493 					      enum mcopy_atomic_mode mcopy_mode,
494 					      atomic_t *mmap_changing,
495 					      __u64 mode)
496 {
497 	struct vm_area_struct *dst_vma;
498 	ssize_t err;
499 	pmd_t *dst_pmd;
500 	unsigned long src_addr, dst_addr;
501 	long copied;
502 	struct page *page;
503 	bool wp_copy;
504 
505 	/*
506 	 * Sanitize the command parameters:
507 	 */
508 	BUG_ON(dst_start & ~PAGE_MASK);
509 	BUG_ON(len & ~PAGE_MASK);
510 
511 	/* Does the address range wrap, or is the span zero-sized? */
512 	BUG_ON(src_start + len <= src_start);
513 	BUG_ON(dst_start + len <= dst_start);
514 
515 	src_addr = src_start;
516 	dst_addr = dst_start;
517 	copied = 0;
518 	page = NULL;
519 retry:
520 	mmap_read_lock(dst_mm);
521 
522 	/*
523 	 * If memory mappings are changing because of non-cooperative
524 	 * operation (e.g. mremap) running in parallel, bail out and
525 	 * request the user to retry later
526 	 */
527 	err = -EAGAIN;
528 	if (mmap_changing && atomic_read(mmap_changing))
529 		goto out_unlock;
530 
531 	/*
532 	 * Make sure the vma is not shared, that the dst range is
533 	 * both valid and fully within a single existing vma.
534 	 */
535 	err = -ENOENT;
536 	dst_vma = find_dst_vma(dst_mm, dst_start, len);
537 	if (!dst_vma)
538 		goto out_unlock;
539 
540 	err = -EINVAL;
541 	/*
542 	 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
543 	 * it will overwrite vm_ops, so vma_is_anonymous must return false.
544 	 */
545 	if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
546 	    dst_vma->vm_flags & VM_SHARED))
547 		goto out_unlock;
548 
549 	/*
550 	 * validate 'mode' now that we know the dst_vma: don't allow
551 	 * a wrprotect copy if the userfaultfd didn't register as WP.
552 	 */
553 	wp_copy = mode & UFFDIO_COPY_MODE_WP;
554 	if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
555 		goto out_unlock;
556 
557 	/*
558 	 * If this is a HUGETLB vma, pass off to appropriate routine
559 	 */
560 	if (is_vm_hugetlb_page(dst_vma))
561 		return  __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
562 						src_start, len, mcopy_mode);
563 
564 	if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
565 		goto out_unlock;
566 	if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
567 		goto out_unlock;
568 
569 	/*
570 	 * Ensure the dst_vma has a anon_vma or this page
571 	 * would get a NULL anon_vma when moved in the
572 	 * dst_vma.
573 	 */
574 	err = -ENOMEM;
575 	if (!(dst_vma->vm_flags & VM_SHARED) &&
576 	    unlikely(anon_vma_prepare(dst_vma)))
577 		goto out_unlock;
578 
579 	while (src_addr < src_start + len) {
580 		pmd_t dst_pmdval;
581 
582 		BUG_ON(dst_addr >= dst_start + len);
583 
584 		dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
585 		if (unlikely(!dst_pmd)) {
586 			err = -ENOMEM;
587 			break;
588 		}
589 
590 		dst_pmdval = pmd_read_atomic(dst_pmd);
591 		/*
592 		 * If the dst_pmd is mapped as THP don't
593 		 * override it and just be strict.
594 		 */
595 		if (unlikely(pmd_trans_huge(dst_pmdval))) {
596 			err = -EEXIST;
597 			break;
598 		}
599 		if (unlikely(pmd_none(dst_pmdval)) &&
600 		    unlikely(__pte_alloc(dst_mm, dst_pmd))) {
601 			err = -ENOMEM;
602 			break;
603 		}
604 		/* If an huge pmd materialized from under us fail */
605 		if (unlikely(pmd_trans_huge(*dst_pmd))) {
606 			err = -EFAULT;
607 			break;
608 		}
609 
610 		BUG_ON(pmd_none(*dst_pmd));
611 		BUG_ON(pmd_trans_huge(*dst_pmd));
612 
613 		err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
614 				       src_addr, &page, mcopy_mode, wp_copy);
615 		cond_resched();
616 
617 		if (unlikely(err == -ENOENT)) {
618 			void *page_kaddr;
619 
620 			mmap_read_unlock(dst_mm);
621 			BUG_ON(!page);
622 
623 			page_kaddr = kmap(page);
624 			err = copy_from_user(page_kaddr,
625 					     (const void __user *) src_addr,
626 					     PAGE_SIZE);
627 			kunmap(page);
628 			if (unlikely(err)) {
629 				err = -EFAULT;
630 				goto out;
631 			}
632 			flush_dcache_page(page);
633 			goto retry;
634 		} else
635 			BUG_ON(page);
636 
637 		if (!err) {
638 			dst_addr += PAGE_SIZE;
639 			src_addr += PAGE_SIZE;
640 			copied += PAGE_SIZE;
641 
642 			if (fatal_signal_pending(current))
643 				err = -EINTR;
644 		}
645 		if (err)
646 			break;
647 	}
648 
649 out_unlock:
650 	mmap_read_unlock(dst_mm);
651 out:
652 	if (page)
653 		put_page(page);
654 	BUG_ON(copied < 0);
655 	BUG_ON(err > 0);
656 	BUG_ON(!copied && !err);
657 	return copied ? copied : err;
658 }
659 
660 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
661 		     unsigned long src_start, unsigned long len,
662 		     atomic_t *mmap_changing, __u64 mode)
663 {
664 	return __mcopy_atomic(dst_mm, dst_start, src_start, len,
665 			      MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
666 }
667 
668 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
669 		       unsigned long len, atomic_t *mmap_changing)
670 {
671 	return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
672 			      mmap_changing, 0);
673 }
674 
675 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
676 		       unsigned long len, atomic_t *mmap_changing)
677 {
678 	return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
679 			      mmap_changing, 0);
680 }
681 
682 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
683 			unsigned long len, bool enable_wp,
684 			atomic_t *mmap_changing)
685 {
686 	struct vm_area_struct *dst_vma;
687 	pgprot_t newprot;
688 	int err;
689 
690 	/*
691 	 * Sanitize the command parameters:
692 	 */
693 	BUG_ON(start & ~PAGE_MASK);
694 	BUG_ON(len & ~PAGE_MASK);
695 
696 	/* Does the address range wrap, or is the span zero-sized? */
697 	BUG_ON(start + len <= start);
698 
699 	mmap_read_lock(dst_mm);
700 
701 	/*
702 	 * If memory mappings are changing because of non-cooperative
703 	 * operation (e.g. mremap) running in parallel, bail out and
704 	 * request the user to retry later
705 	 */
706 	err = -EAGAIN;
707 	if (mmap_changing && atomic_read(mmap_changing))
708 		goto out_unlock;
709 
710 	err = -ENOENT;
711 	dst_vma = find_dst_vma(dst_mm, start, len);
712 	/*
713 	 * Make sure the vma is not shared, that the dst range is
714 	 * both valid and fully within a single existing vma.
715 	 */
716 	if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
717 		goto out_unlock;
718 	if (!userfaultfd_wp(dst_vma))
719 		goto out_unlock;
720 	if (!vma_is_anonymous(dst_vma))
721 		goto out_unlock;
722 
723 	if (enable_wp)
724 		newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
725 	else
726 		newprot = vm_get_page_prot(dst_vma->vm_flags);
727 
728 	change_protection(dst_vma, start, start + len, newprot,
729 			  enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
730 
731 	err = 0;
732 out_unlock:
733 	mmap_read_unlock(dst_mm);
734 	return err;
735 }
736