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>
22 static __always_inline
23 struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm,
24 unsigned long dst_start,
28 * Make sure that the dst range is both valid and fully within a
29 * single existing vma.
31 struct vm_area_struct *dst_vma;
33 dst_vma = find_vma(dst_mm, dst_start);
37 if (dst_start < dst_vma->vm_start ||
38 dst_start + len > dst_vma->vm_end)
42 * Check the vma is registered in uffd, this is required to
43 * enforce the VM_MAYWRITE check done at uffd registration
46 if (!dst_vma->vm_userfaultfd_ctx.ctx)
53 * Install PTEs, to map dst_addr (within dst_vma) to page.
55 * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
56 * and anon, and for both shared and private VMAs.
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)
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;
70 pgoff_t offset, max_off;
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)
78 * Always mark a PTE as write-protected when needed, regardless of
79 * VM_WRITE, which the user might change.
82 _dst_pte = pte_mkuffd_wp(_dst_pte);
87 _dst_pte = pte_mkwrite(_dst_pte);
90 * We need this to make sure write bit removed; as mk_pte()
91 * could return a pte with write bit set.
93 _dst_pte = pte_wrprotect(_dst_pte);
95 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
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);
103 if (unlikely(offset >= max_off))
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.
113 if (!pte_none_mostly(*dst_pte))
117 /* Usually, cache pages are already added to LRU */
120 page_add_file_rmap(page, dst_vma, false);
122 page_add_new_anon_rmap(page, dst_vma, dst_addr);
123 lru_cache_add_inactive_or_unevictable(page, dst_vma);
127 * Must happen after rmap, as mm_counter() checks mapping (via
128 * PageAnon()), which is set by __page_set_anon_rmap().
130 inc_mm_counter(dst_mm, mm_counter(page));
132 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
134 /* No need to invalidate - it was non-present before */
135 update_mmu_cache(dst_vma, dst_addr, dst_pte);
138 pte_unmap_unlock(dst_pte, ptl);
142 static int mcopy_atomic_pte(struct mm_struct *dst_mm,
144 struct vm_area_struct *dst_vma,
145 unsigned long dst_addr,
146 unsigned long src_addr,
156 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
160 page_kaddr = kmap_atomic(page);
161 ret = copy_from_user(page_kaddr,
162 (const void __user *) src_addr,
164 kunmap_atomic(page_kaddr);
166 /* fallback to copy_from_user outside mmap_lock */
170 /* don't free the page */
174 flush_dcache_page(page);
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.
185 __SetPageUptodate(page);
188 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL))
191 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
192 page, true, wp_copy);
202 static int mfill_zeropage_pte(struct mm_struct *dst_mm,
204 struct vm_area_struct *dst_vma,
205 unsigned long dst_addr)
207 pte_t _dst_pte, *dst_pte;
210 pgoff_t offset, max_off;
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);
222 if (unlikely(offset >= max_off))
226 if (!pte_none(*dst_pte))
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);
233 pte_unmap_unlock(dst_pte, ptl);
237 /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
238 static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
240 struct vm_area_struct *dst_vma,
241 unsigned long dst_addr,
244 struct inode *inode = file_inode(dst_vma->vm_file);
245 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
249 ret = shmem_getpage(inode, pgoff, &page, SGP_NOALLOC);
250 /* Our caller expects us to return -EFAULT if we failed to find page. */
260 if (PageHWPoison(page)) {
265 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
266 page, false, wp_copy);
280 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
286 pgd = pgd_offset(mm, address);
287 p4d = p4d_alloc(mm, pgd, address);
290 pud = pud_alloc(mm, p4d, address);
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.
298 return pmd_alloc(mm, pud, address);
301 #ifdef CONFIG_HUGETLB_PAGE
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.
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,
311 enum mcopy_atomic_mode mode,
314 int vm_shared = dst_vma->vm_flags & VM_SHARED;
317 unsigned long src_addr, dst_addr;
320 unsigned long vma_hpagesize;
323 struct address_space *mapping;
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.
331 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
332 mmap_read_unlock(dst_mm);
336 src_addr = src_start;
337 dst_addr = dst_start;
340 vma_hpagesize = vma_kernel_pagesize(dst_vma);
343 * Validate alignment based on huge page size
346 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
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.
356 dst_vma = find_dst_vma(dst_mm, dst_start, len);
357 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
361 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
364 vm_shared = dst_vma->vm_flags & VM_SHARED;
368 * If not shared, ensure the dst_vma has a anon_vma.
372 if (unlikely(anon_vma_prepare(dst_vma)))
376 while (src_addr < src_start + len) {
377 BUG_ON(dst_addr >= dst_start + len);
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.
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]);
392 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
394 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
395 i_mmap_unlock_read(mapping);
399 if (mode != MCOPY_ATOMIC_CONTINUE &&
400 !huge_pte_none_mostly(huge_ptep_get(dst_pte))) {
402 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
403 i_mmap_unlock_read(mapping);
407 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
408 dst_addr, src_addr, mode, &page,
411 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
412 i_mmap_unlock_read(mapping);
416 if (unlikely(err == -ENOENT)) {
417 mmap_read_unlock(dst_mm);
420 err = copy_huge_page_from_user(page,
421 (const void __user *)src_addr,
422 vma_hpagesize / PAGE_SIZE,
428 mmap_read_lock(dst_mm);
436 dst_addr += vma_hpagesize;
437 src_addr += vma_hpagesize;
438 copied += vma_hpagesize;
440 if (fatal_signal_pending(current))
448 mmap_read_unlock(dst_mm);
454 BUG_ON(!copied && !err);
455 return copied ? copied : err;
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,
464 enum mcopy_atomic_mode mode,
466 #endif /* CONFIG_HUGETLB_PAGE */
468 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
470 struct vm_area_struct *dst_vma,
471 unsigned long dst_addr,
472 unsigned long src_addr,
474 enum mcopy_atomic_mode mode,
479 if (mode == MCOPY_ATOMIC_CONTINUE) {
480 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
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.
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,
500 err = mfill_zeropage_pte(dst_mm, dst_pmd,
503 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
505 mode != MCOPY_ATOMIC_NORMAL,
512 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
513 unsigned long dst_start,
514 unsigned long src_start,
516 enum mcopy_atomic_mode mcopy_mode,
517 atomic_t *mmap_changing,
520 struct vm_area_struct *dst_vma;
523 unsigned long src_addr, dst_addr;
529 * Sanitize the command parameters:
531 BUG_ON(dst_start & ~PAGE_MASK);
532 BUG_ON(len & ~PAGE_MASK);
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);
538 src_addr = src_start;
539 dst_addr = dst_start;
543 mmap_read_lock(dst_mm);
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
551 if (mmap_changing && atomic_read(mmap_changing))
555 * Make sure the vma is not shared, that the dst range is
556 * both valid and fully within a single existing vma.
559 dst_vma = find_dst_vma(dst_mm, dst_start, len);
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.
568 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
569 dst_vma->vm_flags & VM_SHARED))
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.
576 wp_copy = mode & UFFDIO_COPY_MODE_WP;
577 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
581 * If this is a HUGETLB vma, pass off to appropriate routine
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,
588 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
590 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
594 * Ensure the dst_vma has a anon_vma or this page
595 * would get a NULL anon_vma when moved in the
599 if (!(dst_vma->vm_flags & VM_SHARED) &&
600 unlikely(anon_vma_prepare(dst_vma)))
603 while (src_addr < src_start + len) {
606 BUG_ON(dst_addr >= dst_start + len);
608 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
609 if (unlikely(!dst_pmd)) {
614 dst_pmdval = pmd_read_atomic(dst_pmd);
616 * If the dst_pmd is mapped as THP don't
617 * override it and just be strict.
619 if (unlikely(pmd_trans_huge(dst_pmdval))) {
623 if (unlikely(pmd_none(dst_pmdval)) &&
624 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
628 /* If an huge pmd materialized from under us fail */
629 if (unlikely(pmd_trans_huge(*dst_pmd))) {
634 BUG_ON(pmd_none(*dst_pmd));
635 BUG_ON(pmd_trans_huge(*dst_pmd));
637 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
638 src_addr, &page, mcopy_mode, wp_copy);
641 if (unlikely(err == -ENOENT)) {
644 mmap_read_unlock(dst_mm);
647 page_kaddr = kmap(page);
648 err = copy_from_user(page_kaddr,
649 (const void __user *) src_addr,
656 flush_dcache_page(page);
662 dst_addr += PAGE_SIZE;
663 src_addr += PAGE_SIZE;
666 if (fatal_signal_pending(current))
674 mmap_read_unlock(dst_mm);
680 BUG_ON(!copied && !err);
681 return copied ? copied : err;
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)
688 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
689 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
692 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
693 unsigned long len, atomic_t *mmap_changing)
695 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
699 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
700 unsigned long len, atomic_t *mmap_changing)
702 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
706 void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
707 unsigned long start, unsigned long len, bool enable_wp)
709 struct mmu_gather tlb;
713 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
715 newprot = vm_get_page_prot(dst_vma->vm_flags);
717 tlb_gather_mmu(&tlb, dst_mm);
718 change_protection(&tlb, dst_vma, start, start + len, newprot,
719 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
720 tlb_finish_mmu(&tlb);
723 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
724 unsigned long len, bool enable_wp,
725 atomic_t *mmap_changing)
727 struct vm_area_struct *dst_vma;
728 unsigned long page_mask;
732 * Sanitize the command parameters:
734 BUG_ON(start & ~PAGE_MASK);
735 BUG_ON(len & ~PAGE_MASK);
737 /* Does the address range wrap, or is the span zero-sized? */
738 BUG_ON(start + len <= start);
740 mmap_read_lock(dst_mm);
743 * If memory mappings are changing because of non-cooperative
744 * operation (e.g. mremap) running in parallel, bail out and
745 * request the user to retry later
748 if (mmap_changing && atomic_read(mmap_changing))
752 dst_vma = find_dst_vma(dst_mm, start, len);
756 if (!userfaultfd_wp(dst_vma))
758 if (!vma_can_userfault(dst_vma, dst_vma->vm_flags))
761 if (is_vm_hugetlb_page(dst_vma)) {
763 page_mask = vma_kernel_pagesize(dst_vma) - 1;
764 if ((start & page_mask) || (len & page_mask))
768 uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp);
772 mmap_read_unlock(dst_mm);