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