1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2015 Red Hat, Inc.
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>
21 static __always_inline
22 struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm,
23 unsigned long dst_start,
27 * Make sure that the dst range is both valid and fully within a
28 * single existing vma.
30 struct vm_area_struct *dst_vma;
32 dst_vma = find_vma(dst_mm, dst_start);
36 if (dst_start < dst_vma->vm_start ||
37 dst_start + len > dst_vma->vm_end)
41 * Check the vma is registered in uffd, this is required to
42 * enforce the VM_MAYWRITE check done at uffd registration
45 if (!dst_vma->vm_userfaultfd_ctx.ctx)
52 * Install PTEs, to map dst_addr (within dst_vma) to page.
54 * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
55 * and anon, and for both shared and private VMAs.
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)
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);
69 pgoff_t offset, max_off;
71 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
72 if (page_in_cache && !vm_shared)
74 if (writable || !page_in_cache)
75 _dst_pte = pte_mkdirty(_dst_pte);
78 _dst_pte = pte_mkuffd_wp(_dst_pte);
80 _dst_pte = pte_mkwrite(_dst_pte);
83 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
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);
91 if (unlikely(offset >= max_off))
96 if (!pte_none(*dst_pte))
100 page_add_file_rmap(page, false);
102 page_add_new_anon_rmap(page, dst_vma, dst_addr, false);
105 * Must happen after rmap, as mm_counter() checks mapping (via
106 * PageAnon()), which is set by __page_set_anon_rmap().
108 inc_mm_counter(dst_mm, mm_counter(page));
111 lru_cache_add_inactive_or_unevictable(page, dst_vma);
113 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
115 /* No need to invalidate - it was non-present before */
116 update_mmu_cache(dst_vma, dst_addr, dst_pte);
119 pte_unmap_unlock(dst_pte, ptl);
123 static int mcopy_atomic_pte(struct mm_struct *dst_mm,
125 struct vm_area_struct *dst_vma,
126 unsigned long dst_addr,
127 unsigned long src_addr,
137 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
141 page_kaddr = kmap_atomic(page);
142 ret = copy_from_user(page_kaddr,
143 (const void __user *) src_addr,
145 kunmap_atomic(page_kaddr);
147 /* fallback to copy_from_user outside mmap_lock */
151 /* don't free the page */
155 flush_dcache_page(page);
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.
166 __SetPageUptodate(page);
169 if (mem_cgroup_charge(page, dst_mm, GFP_KERNEL))
172 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
173 page, true, wp_copy);
183 static int mfill_zeropage_pte(struct mm_struct *dst_mm,
185 struct vm_area_struct *dst_vma,
186 unsigned long dst_addr)
188 pte_t _dst_pte, *dst_pte;
191 pgoff_t offset, max_off;
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);
203 if (unlikely(offset >= max_off))
207 if (!pte_none(*dst_pte))
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);
214 pte_unmap_unlock(dst_pte, ptl);
218 /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
219 static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
221 struct vm_area_struct *dst_vma,
222 unsigned long dst_addr,
225 struct inode *inode = file_inode(dst_vma->vm_file);
226 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
230 ret = shmem_getpage(inode, pgoff, &page, SGP_NOALLOC);
231 /* Our caller expects us to return -EFAULT if we failed to find page. */
241 if (PageHWPoison(page)) {
246 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
247 page, false, wp_copy);
261 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
267 pgd = pgd_offset(mm, address);
268 p4d = p4d_alloc(mm, pgd, address);
271 pud = pud_alloc(mm, p4d, address);
275 * Note that we didn't run this because the pmd was
276 * missing, the *pmd may be already established and in
277 * turn it may also be a trans_huge_pmd.
279 return pmd_alloc(mm, pud, address);
282 #ifdef CONFIG_HUGETLB_PAGE
284 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
285 * called with mmap_lock held, it will release mmap_lock before returning.
287 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
288 struct vm_area_struct *dst_vma,
289 unsigned long dst_start,
290 unsigned long src_start,
292 enum mcopy_atomic_mode mode)
294 int vm_shared = dst_vma->vm_flags & VM_SHARED;
297 unsigned long src_addr, dst_addr;
300 unsigned long vma_hpagesize;
303 struct address_space *mapping;
306 * There is no default zero huge page for all huge page sizes as
307 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
308 * by THP. Since we can not reliably insert a zero page, this
309 * feature is not supported.
311 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
312 mmap_read_unlock(dst_mm);
316 src_addr = src_start;
317 dst_addr = dst_start;
320 vma_hpagesize = vma_kernel_pagesize(dst_vma);
323 * Validate alignment based on huge page size
326 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
331 * On routine entry dst_vma is set. If we had to drop mmap_lock and
332 * retry, dst_vma will be set to NULL and we must lookup again.
336 dst_vma = find_dst_vma(dst_mm, dst_start, len);
337 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
341 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
344 vm_shared = dst_vma->vm_flags & VM_SHARED;
348 * If not shared, ensure the dst_vma has a anon_vma.
352 if (unlikely(anon_vma_prepare(dst_vma)))
356 while (src_addr < src_start + len) {
357 BUG_ON(dst_addr >= dst_start + len);
360 * Serialize via i_mmap_rwsem and hugetlb_fault_mutex.
361 * i_mmap_rwsem ensures the dst_pte remains valid even
362 * in the case of shared pmds. fault mutex prevents
363 * races with other faulting threads.
365 mapping = dst_vma->vm_file->f_mapping;
366 i_mmap_lock_read(mapping);
367 idx = linear_page_index(dst_vma, dst_addr);
368 hash = hugetlb_fault_mutex_hash(mapping, idx);
369 mutex_lock(&hugetlb_fault_mutex_table[hash]);
372 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
374 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
375 i_mmap_unlock_read(mapping);
379 if (mode != MCOPY_ATOMIC_CONTINUE &&
380 !huge_pte_none(huge_ptep_get(dst_pte))) {
382 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
383 i_mmap_unlock_read(mapping);
387 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
388 dst_addr, src_addr, mode, &page);
390 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
391 i_mmap_unlock_read(mapping);
395 if (unlikely(err == -ENOENT)) {
396 mmap_read_unlock(dst_mm);
399 err = copy_huge_page_from_user(page,
400 (const void __user *)src_addr,
401 vma_hpagesize / PAGE_SIZE,
407 mmap_read_lock(dst_mm);
415 dst_addr += vma_hpagesize;
416 src_addr += vma_hpagesize;
417 copied += vma_hpagesize;
419 if (fatal_signal_pending(current))
427 mmap_read_unlock(dst_mm);
433 BUG_ON(!copied && !err);
434 return copied ? copied : err;
436 #else /* !CONFIG_HUGETLB_PAGE */
437 /* fail at build time if gcc attempts to use this */
438 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
439 struct vm_area_struct *dst_vma,
440 unsigned long dst_start,
441 unsigned long src_start,
443 enum mcopy_atomic_mode mode);
444 #endif /* CONFIG_HUGETLB_PAGE */
446 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
448 struct vm_area_struct *dst_vma,
449 unsigned long dst_addr,
450 unsigned long src_addr,
452 enum mcopy_atomic_mode mode,
457 if (mode == MCOPY_ATOMIC_CONTINUE) {
458 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
463 * The normal page fault path for a shmem will invoke the
464 * fault, fill the hole in the file and COW it right away. The
465 * result generates plain anonymous memory. So when we are
466 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
467 * generate anonymous memory directly without actually filling
468 * the hole. For the MAP_PRIVATE case the robustness check
469 * only happens in the pagetable (to verify it's still none)
470 * and not in the radix tree.
472 if (!(dst_vma->vm_flags & VM_SHARED)) {
473 if (mode == MCOPY_ATOMIC_NORMAL)
474 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
475 dst_addr, src_addr, page,
478 err = mfill_zeropage_pte(dst_mm, dst_pmd,
481 VM_WARN_ON_ONCE(wp_copy);
482 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
484 mode != MCOPY_ATOMIC_NORMAL,
491 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
492 unsigned long dst_start,
493 unsigned long src_start,
495 enum mcopy_atomic_mode mcopy_mode,
496 atomic_t *mmap_changing,
499 struct vm_area_struct *dst_vma;
502 unsigned long src_addr, dst_addr;
508 * Sanitize the command parameters:
510 BUG_ON(dst_start & ~PAGE_MASK);
511 BUG_ON(len & ~PAGE_MASK);
513 /* Does the address range wrap, or is the span zero-sized? */
514 BUG_ON(src_start + len <= src_start);
515 BUG_ON(dst_start + len <= dst_start);
517 src_addr = src_start;
518 dst_addr = dst_start;
522 mmap_read_lock(dst_mm);
525 * If memory mappings are changing because of non-cooperative
526 * operation (e.g. mremap) running in parallel, bail out and
527 * request the user to retry later
530 if (mmap_changing && atomic_read(mmap_changing))
534 * Make sure the vma is not shared, that the dst range is
535 * both valid and fully within a single existing vma.
538 dst_vma = find_dst_vma(dst_mm, dst_start, len);
544 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
545 * it will overwrite vm_ops, so vma_is_anonymous must return false.
547 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
548 dst_vma->vm_flags & VM_SHARED))
552 * validate 'mode' now that we know the dst_vma: don't allow
553 * a wrprotect copy if the userfaultfd didn't register as WP.
555 wp_copy = mode & UFFDIO_COPY_MODE_WP;
556 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
560 * If this is a HUGETLB vma, pass off to appropriate routine
562 if (is_vm_hugetlb_page(dst_vma))
563 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
564 src_start, len, mcopy_mode);
566 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
568 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
572 * Ensure the dst_vma has a anon_vma or this page
573 * would get a NULL anon_vma when moved in the
577 if (!(dst_vma->vm_flags & VM_SHARED) &&
578 unlikely(anon_vma_prepare(dst_vma)))
581 while (src_addr < src_start + len) {
584 BUG_ON(dst_addr >= dst_start + len);
586 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
587 if (unlikely(!dst_pmd)) {
592 dst_pmdval = pmd_read_atomic(dst_pmd);
594 * If the dst_pmd is mapped as THP don't
595 * override it and just be strict.
597 if (unlikely(pmd_trans_huge(dst_pmdval))) {
601 if (unlikely(pmd_none(dst_pmdval)) &&
602 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
606 /* If an huge pmd materialized from under us fail */
607 if (unlikely(pmd_trans_huge(*dst_pmd))) {
612 BUG_ON(pmd_none(*dst_pmd));
613 BUG_ON(pmd_trans_huge(*dst_pmd));
615 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
616 src_addr, &page, mcopy_mode, wp_copy);
619 if (unlikely(err == -ENOENT)) {
622 mmap_read_unlock(dst_mm);
625 page_kaddr = kmap(page);
626 err = copy_from_user(page_kaddr,
627 (const void __user *) src_addr,
634 flush_dcache_page(page);
640 dst_addr += PAGE_SIZE;
641 src_addr += PAGE_SIZE;
644 if (fatal_signal_pending(current))
652 mmap_read_unlock(dst_mm);
658 BUG_ON(!copied && !err);
659 return copied ? copied : err;
662 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
663 unsigned long src_start, unsigned long len,
664 atomic_t *mmap_changing, __u64 mode)
666 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
667 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
670 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
671 unsigned long len, atomic_t *mmap_changing)
673 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
677 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
678 unsigned long len, atomic_t *mmap_changing)
680 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
684 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
685 unsigned long len, bool enable_wp,
686 atomic_t *mmap_changing)
688 struct vm_area_struct *dst_vma;
693 * Sanitize the command parameters:
695 BUG_ON(start & ~PAGE_MASK);
696 BUG_ON(len & ~PAGE_MASK);
698 /* Does the address range wrap, or is the span zero-sized? */
699 BUG_ON(start + len <= start);
701 mmap_read_lock(dst_mm);
704 * If memory mappings are changing because of non-cooperative
705 * operation (e.g. mremap) running in parallel, bail out and
706 * request the user to retry later
709 if (mmap_changing && atomic_read(mmap_changing))
713 dst_vma = find_dst_vma(dst_mm, start, len);
715 * Make sure the vma is not shared, that the dst range is
716 * both valid and fully within a single existing vma.
718 if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
720 if (!userfaultfd_wp(dst_vma))
722 if (!vma_is_anonymous(dst_vma))
726 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
728 newprot = vm_get_page_prot(dst_vma->vm_flags);
730 change_protection(dst_vma, start, start + len, newprot,
731 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
735 mmap_read_unlock(dst_mm);