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