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