• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (!rcu_access_pointer(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(page);
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,bool * mmap_changing,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 					      bool *mmap_changing,
285 					      enum mcopy_atomic_mode mode)
286 {
287 	int vm_alloc_shared = dst_vma->vm_flags & VM_SHARED;
288 	int vm_shared = dst_vma->vm_flags & VM_SHARED;
289 	ssize_t err;
290 	pte_t *dst_pte;
291 	unsigned long src_addr, dst_addr;
292 	long copied;
293 	struct page *page;
294 	unsigned long vma_hpagesize;
295 	pgoff_t idx;
296 	u32 hash;
297 	struct address_space *mapping;
298 
299 	/*
300 	 * There is no default zero huge page for all huge page sizes as
301 	 * supported by hugetlb.  A PMD_SIZE huge pages may exist as used
302 	 * by THP.  Since we can not reliably insert a zero page, this
303 	 * feature is not supported.
304 	 */
305 	if (mode == MCOPY_ATOMIC_ZEROPAGE) {
306 		mmap_read_unlock(dst_mm);
307 		return -EINVAL;
308 	}
309 
310 	src_addr = src_start;
311 	dst_addr = dst_start;
312 	copied = 0;
313 	page = NULL;
314 	vma_hpagesize = vma_kernel_pagesize(dst_vma);
315 
316 	/*
317 	 * Validate alignment based on huge page size
318 	 */
319 	err = -EINVAL;
320 	if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
321 		goto out_unlock;
322 
323 retry:
324 	/*
325 	 * On routine entry dst_vma is set.  If we had to drop mmap_lock and
326 	 * retry, dst_vma will be set to NULL and we must lookup again.
327 	 */
328 	if (!dst_vma) {
329 		err = -ENOENT;
330 		dst_vma = find_dst_vma(dst_mm, dst_start, len);
331 		if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
332 			goto out_unlock;
333 
334 		err = -EINVAL;
335 		if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
336 			goto out_unlock;
337 
338 		vm_shared = dst_vma->vm_flags & VM_SHARED;
339 	}
340 
341 	/*
342 	 * If not shared, ensure the dst_vma has a anon_vma.
343 	 */
344 	err = -ENOMEM;
345 	if (!vm_shared) {
346 		if (unlikely(anon_vma_prepare(dst_vma)))
347 			goto out_unlock;
348 	}
349 
350 	while (src_addr < src_start + len) {
351 		BUG_ON(dst_addr >= dst_start + len);
352 
353 		/*
354 		 * Serialize via i_mmap_rwsem and hugetlb_fault_mutex.
355 		 * i_mmap_rwsem ensures the dst_pte remains valid even
356 		 * in the case of shared pmds.  fault mutex prevents
357 		 * races with other faulting threads.
358 		 */
359 		mapping = dst_vma->vm_file->f_mapping;
360 		i_mmap_lock_read(mapping);
361 		idx = linear_page_index(dst_vma, dst_addr);
362 		hash = hugetlb_fault_mutex_hash(mapping, idx);
363 		mutex_lock(&hugetlb_fault_mutex_table[hash]);
364 
365 		err = -ENOMEM;
366 		dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
367 		if (!dst_pte) {
368 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
369 			i_mmap_unlock_read(mapping);
370 			goto out_unlock;
371 		}
372 
373 		if (mode != MCOPY_ATOMIC_CONTINUE &&
374 		    !huge_pte_none(huge_ptep_get(dst_pte))) {
375 			err = -EEXIST;
376 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
377 			i_mmap_unlock_read(mapping);
378 			goto out_unlock;
379 		}
380 
381 		err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
382 					       dst_addr, src_addr, mode, &page);
383 
384 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
385 		i_mmap_unlock_read(mapping);
386 		vm_alloc_shared = vm_shared;
387 
388 		cond_resched();
389 
390 		if (unlikely(err == -ENOENT)) {
391 			mmap_read_unlock(dst_mm);
392 			BUG_ON(!page);
393 
394 			err = copy_huge_page_from_user(page,
395 						(const void __user *)src_addr,
396 						vma_hpagesize / PAGE_SIZE,
397 						true);
398 			if (unlikely(err)) {
399 				err = -EFAULT;
400 				goto out;
401 			}
402 			mmap_read_lock(dst_mm);
403 			/*
404 			 * If memory mappings are changing because of non-cooperative
405 			 * operation (e.g. mremap) running in parallel, bail out and
406 			 * request the user to retry later
407 			 */
408 			if (mmap_changing && READ_ONCE(*mmap_changing)) {
409 				err = -EAGAIN;
410 				break;
411 			}
412 
413 			dst_vma = NULL;
414 			goto retry;
415 		} else
416 			BUG_ON(page);
417 
418 		if (!err) {
419 			dst_addr += vma_hpagesize;
420 			src_addr += vma_hpagesize;
421 			copied += vma_hpagesize;
422 
423 			if (fatal_signal_pending(current))
424 				err = -EINTR;
425 		}
426 		if (err)
427 			break;
428 	}
429 
430 out_unlock:
431 	mmap_read_unlock(dst_mm);
432 out:
433 	if (page) {
434 		/*
435 		 * We encountered an error and are about to free a newly
436 		 * allocated huge page.
437 		 *
438 		 * Reservation handling is very subtle, and is different for
439 		 * private and shared mappings.  See the routine
440 		 * restore_reserve_on_error for details.  Unfortunately, we
441 		 * can not call restore_reserve_on_error now as it would
442 		 * require holding mmap_lock.
443 		 *
444 		 * If a reservation for the page existed in the reservation
445 		 * map of a private mapping, the map was modified to indicate
446 		 * the reservation was consumed when the page was allocated.
447 		 * We clear the PagePrivate flag now so that the global
448 		 * reserve count will not be incremented in free_huge_page.
449 		 * The reservation map will still indicate the reservation
450 		 * was consumed and possibly prevent later page allocation.
451 		 * This is better than leaking a global reservation.  If no
452 		 * reservation existed, it is still safe to clear PagePrivate
453 		 * as no adjustments to reservation counts were made during
454 		 * allocation.
455 		 *
456 		 * The reservation map for shared mappings indicates which
457 		 * pages have reservations.  When a huge page is allocated
458 		 * for an address with a reservation, no change is made to
459 		 * the reserve map.  In this case PagePrivate will be set
460 		 * to indicate that the global reservation count should be
461 		 * incremented when the page is freed.  This is the desired
462 		 * behavior.  However, when a huge page is allocated for an
463 		 * address without a reservation a reservation entry is added
464 		 * to the reservation map, and PagePrivate will not be set.
465 		 * When the page is freed, the global reserve count will NOT
466 		 * be incremented and it will appear as though we have leaked
467 		 * reserved page.  In this case, set PagePrivate so that the
468 		 * global reserve count will be incremented to match the
469 		 * reservation map entry which was created.
470 		 *
471 		 * Note that vm_alloc_shared is based on the flags of the vma
472 		 * for which the page was originally allocated.  dst_vma could
473 		 * be different or NULL on error.
474 		 */
475 		if (vm_alloc_shared)
476 			SetPagePrivate(page);
477 		else
478 			ClearPagePrivate(page);
479 		put_page(page);
480 	}
481 	BUG_ON(copied < 0);
482 	BUG_ON(err > 0);
483 	BUG_ON(!copied && !err);
484 	return copied ? copied : err;
485 }
486 #else /* !CONFIG_HUGETLB_PAGE */
487 /* fail at build time if gcc attempts to use this */
488 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
489 				      struct vm_area_struct *dst_vma,
490 				      unsigned long dst_start,
491 				      unsigned long src_start,
492 				      unsigned long len,
493 				      bool *mmap_changing,
494 				      enum mcopy_atomic_mode mode);
495 #endif /* CONFIG_HUGETLB_PAGE */
496 
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)497 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
498 						pmd_t *dst_pmd,
499 						struct vm_area_struct *dst_vma,
500 						unsigned long dst_addr,
501 						unsigned long src_addr,
502 						struct page **page,
503 						enum mcopy_atomic_mode mode,
504 						bool wp_copy)
505 {
506 	ssize_t err;
507 
508 	if (mode == MCOPY_ATOMIC_CONTINUE) {
509 		return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
510 					    wp_copy);
511 	}
512 
513 	/*
514 	 * The normal page fault path for a shmem will invoke the
515 	 * fault, fill the hole in the file and COW it right away. The
516 	 * result generates plain anonymous memory. So when we are
517 	 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
518 	 * generate anonymous memory directly without actually filling
519 	 * the hole. For the MAP_PRIVATE case the robustness check
520 	 * only happens in the pagetable (to verify it's still none)
521 	 * and not in the radix tree.
522 	 */
523 	if (!(dst_vma->vm_flags & VM_SHARED)) {
524 		if (mode == MCOPY_ATOMIC_NORMAL)
525 			err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
526 					       dst_addr, src_addr, page,
527 					       wp_copy);
528 		else
529 			err = mfill_zeropage_pte(dst_mm, dst_pmd,
530 						 dst_vma, dst_addr);
531 	} else {
532 		VM_WARN_ON_ONCE(wp_copy);
533 		err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
534 					     dst_addr, src_addr,
535 					     mode != MCOPY_ATOMIC_NORMAL,
536 					     page);
537 	}
538 
539 	return err;
540 }
541 
__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)542 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
543 					      unsigned long dst_start,
544 					      unsigned long src_start,
545 					      unsigned long len,
546 					      enum mcopy_atomic_mode mcopy_mode,
547 					      bool *mmap_changing,
548 					      __u64 mode)
549 {
550 	struct vm_area_struct *dst_vma;
551 	ssize_t err;
552 	pmd_t *dst_pmd;
553 	unsigned long src_addr, dst_addr;
554 	long copied;
555 	struct page *page;
556 	bool wp_copy;
557 
558 	/*
559 	 * Sanitize the command parameters:
560 	 */
561 	BUG_ON(dst_start & ~PAGE_MASK);
562 	BUG_ON(len & ~PAGE_MASK);
563 
564 	/* Does the address range wrap, or is the span zero-sized? */
565 	BUG_ON(src_start + len <= src_start);
566 	BUG_ON(dst_start + len <= dst_start);
567 
568 	src_addr = src_start;
569 	dst_addr = dst_start;
570 	copied = 0;
571 	page = NULL;
572 retry:
573 	err = -EAGAIN;
574 	if (mode & UFFDIO_MODE_MMAP_TRYLOCK) {
575 		if (!mmap_read_trylock(dst_mm))
576 			goto out;
577 	} else {
578 		mmap_read_lock(dst_mm);
579 	}
580 
581 	/*
582 	 * If memory mappings are changing because of non-cooperative
583 	 * operation (e.g. mremap) running in parallel, bail out and
584 	 * request the user to retry later
585 	 */
586 	if (mmap_changing && READ_ONCE(*mmap_changing))
587 		goto out_unlock;
588 
589 	/*
590 	 * Make sure the vma is not shared, that the dst range is
591 	 * both valid and fully within a single existing vma.
592 	 */
593 	err = -ENOENT;
594 	dst_vma = find_dst_vma(dst_mm, dst_start, len);
595 	if (!dst_vma)
596 		goto out_unlock;
597 
598 	err = -EINVAL;
599 	/*
600 	 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
601 	 * it will overwrite vm_ops, so vma_is_anonymous must return false.
602 	 */
603 	if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
604 	    dst_vma->vm_flags & VM_SHARED))
605 		goto out_unlock;
606 
607 	/*
608 	 * validate 'mode' now that we know the dst_vma: don't allow
609 	 * a wrprotect copy if the userfaultfd didn't register as WP.
610 	 */
611 	wp_copy = mode & UFFDIO_COPY_MODE_WP;
612 	if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
613 		goto out_unlock;
614 
615 	/*
616 	 * If this is a HUGETLB vma, pass off to appropriate routine
617 	 */
618 	if (is_vm_hugetlb_page(dst_vma))
619 		return  __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
620 					       src_start, len, mmap_changing,
621 					       mcopy_mode);
622 
623 	if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
624 		goto out_unlock;
625 	if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
626 		goto out_unlock;
627 
628 	/*
629 	 * Ensure the dst_vma has a anon_vma or this page
630 	 * would get a NULL anon_vma when moved in the
631 	 * dst_vma.
632 	 */
633 	err = -ENOMEM;
634 	if (!(dst_vma->vm_flags & VM_SHARED) &&
635 	    unlikely(anon_vma_prepare(dst_vma)))
636 		goto out_unlock;
637 
638 	while (src_addr < src_start + len) {
639 		pmd_t dst_pmdval;
640 
641 		BUG_ON(dst_addr >= dst_start + len);
642 
643 		dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
644 		if (unlikely(!dst_pmd)) {
645 			err = -ENOMEM;
646 			break;
647 		}
648 
649 		dst_pmdval = pmd_read_atomic(dst_pmd);
650 		/*
651 		 * If the dst_pmd is mapped as THP don't
652 		 * override it and just be strict.
653 		 */
654 		if (unlikely(pmd_trans_huge(dst_pmdval))) {
655 			err = -EEXIST;
656 			break;
657 		}
658 		if (unlikely(pmd_none(dst_pmdval)) &&
659 		    unlikely(__pte_alloc(dst_mm, dst_pmd))) {
660 			err = -ENOMEM;
661 			break;
662 		}
663 		/* If an huge pmd materialized from under us fail */
664 		if (unlikely(pmd_trans_huge(*dst_pmd))) {
665 			err = -EFAULT;
666 			break;
667 		}
668 
669 		BUG_ON(pmd_none(*dst_pmd));
670 		BUG_ON(pmd_trans_huge(*dst_pmd));
671 
672 		err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
673 				       src_addr, &page, mcopy_mode, wp_copy);
674 		cond_resched();
675 
676 		if (unlikely(err == -ENOENT)) {
677 			void *page_kaddr;
678 
679 			/*
680 			 * Return early due to mmap_lock contention only after
681 			 * some pages are copied to ensure that jank sensitive
682 			 * threads don't keep retrying for progress-critical
683 			 * pages.
684 			 */
685 			if (copied && mmap_lock_is_contended(dst_mm))
686 				break;
687 
688 			mmap_read_unlock(dst_mm);
689 			BUG_ON(!page);
690 
691 			page_kaddr = kmap(page);
692 			err = copy_from_user(page_kaddr,
693 					     (const void __user *) src_addr,
694 					     PAGE_SIZE);
695 			kunmap(page);
696 			if (unlikely(err)) {
697 				err = -EFAULT;
698 				goto out;
699 			}
700 			flush_dcache_page(page);
701 			goto retry;
702 		} else
703 			BUG_ON(page);
704 
705 		if (!err) {
706 			dst_addr += PAGE_SIZE;
707 			src_addr += PAGE_SIZE;
708 			copied += PAGE_SIZE;
709 
710 			if (fatal_signal_pending(current))
711 				err = -EINTR;
712 
713 			if (mmap_lock_is_contended(dst_mm))
714 				err = -EAGAIN;
715 		}
716 		if (err)
717 			break;
718 	}
719 
720 out_unlock:
721 	mmap_read_unlock(dst_mm);
722 out:
723 	if (page)
724 		put_page(page);
725 	BUG_ON(copied < 0);
726 	BUG_ON(err > 0);
727 	BUG_ON(!copied && !err);
728 	return copied ? copied : err;
729 }
730 
mcopy_atomic(struct mm_struct * dst_mm,unsigned long dst_start,unsigned long src_start,unsigned long len,bool * mmap_changing,__u64 mode)731 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
732 		     unsigned long src_start, unsigned long len,
733 		     bool *mmap_changing, __u64 mode)
734 {
735 	return __mcopy_atomic(dst_mm, dst_start, src_start, len,
736 			      MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
737 }
738 
mfill_zeropage(struct mm_struct * dst_mm,unsigned long start,unsigned long len,bool * mmap_changing,__u64 mode)739 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
740 		       unsigned long len, bool *mmap_changing, __u64 mode)
741 {
742 	return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
743 			      mmap_changing, mode);
744 }
745 
mcopy_continue(struct mm_struct * dst_mm,unsigned long start,unsigned long len,bool * mmap_changing)746 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
747 		       unsigned long len, bool *mmap_changing)
748 {
749 	return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
750 			      mmap_changing, 0);
751 }
752 
mwriteprotect_range(struct mm_struct * dst_mm,unsigned long start,unsigned long len,bool enable_wp,bool * mmap_changing)753 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
754 			unsigned long len, bool enable_wp, bool *mmap_changing)
755 {
756 	struct vm_area_struct *dst_vma;
757 	pgprot_t newprot;
758 	int err;
759 
760 	/*
761 	 * Sanitize the command parameters:
762 	 */
763 	BUG_ON(start & ~PAGE_MASK);
764 	BUG_ON(len & ~PAGE_MASK);
765 
766 	/* Does the address range wrap, or is the span zero-sized? */
767 	BUG_ON(start + len <= start);
768 
769 	mmap_read_lock(dst_mm);
770 
771 	/*
772 	 * If memory mappings are changing because of non-cooperative
773 	 * operation (e.g. mremap) running in parallel, bail out and
774 	 * request the user to retry later
775 	 */
776 	err = -EAGAIN;
777 	if (mmap_changing && READ_ONCE(*mmap_changing))
778 		goto out_unlock;
779 
780 	err = -ENOENT;
781 	dst_vma = find_dst_vma(dst_mm, start, len);
782 	/*
783 	 * Make sure the vma is not shared, that the dst range is
784 	 * both valid and fully within a single existing vma.
785 	 */
786 	if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
787 		goto out_unlock;
788 	if (!userfaultfd_wp(dst_vma))
789 		goto out_unlock;
790 	if (!vma_is_anonymous(dst_vma))
791 		goto out_unlock;
792 
793 	if (enable_wp)
794 		newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
795 	else
796 		newprot = vm_get_page_prot(dst_vma->vm_flags);
797 
798 	change_protection(dst_vma, start, start + len, newprot,
799 			  enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
800 
801 	err = 0;
802 out_unlock:
803 	mmap_read_unlock(dst_mm);
804 	return err;
805 }
806