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(page);
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_local_page(page);
162 * The read mmap_lock is held here. Despite the
163 * mmap_lock being read recursive a deadlock is still
164 * possible if a writer has taken a lock. For example:
166 * process A thread 1 takes read lock on own mmap_lock
167 * process A thread 2 calls mmap, blocks taking write lock
168 * process B thread 1 takes page fault, read lock on own mmap lock
169 * process B thread 2 calls mmap, blocks taking write lock
170 * process A thread 1 blocks taking read lock on process B
171 * process B thread 1 blocks taking read lock on process A
173 * Disable page faults to prevent potential deadlock
174 * and retry the copy outside the mmap_lock.
177 ret = copy_from_user(page_kaddr,
178 (const void __user *) src_addr,
181 kunmap_local(page_kaddr);
183 /* fallback to copy_from_user outside mmap_lock */
187 /* don't free the page */
191 flush_dcache_page(page);
198 * The memory barrier inside __SetPageUptodate makes sure that
199 * preceding stores to the page contents become visible before
200 * the set_pte_at() write.
202 __SetPageUptodate(page);
205 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL))
208 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
209 page, true, wp_copy);
219 static int mfill_zeropage_pte(struct mm_struct *dst_mm,
221 struct vm_area_struct *dst_vma,
222 unsigned long dst_addr)
224 pte_t _dst_pte, *dst_pte;
227 pgoff_t offset, max_off;
230 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
231 dst_vma->vm_page_prot));
232 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
233 if (dst_vma->vm_file) {
234 /* the shmem MAP_PRIVATE case requires checking the i_size */
235 inode = dst_vma->vm_file->f_inode;
236 offset = linear_page_index(dst_vma, dst_addr);
237 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
239 if (unlikely(offset >= max_off))
243 if (!pte_none(*dst_pte))
245 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
246 /* No need to invalidate - it was non-present before */
247 update_mmu_cache(dst_vma, dst_addr, dst_pte);
250 pte_unmap_unlock(dst_pte, ptl);
254 /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
255 static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
257 struct vm_area_struct *dst_vma,
258 unsigned long dst_addr,
261 struct inode *inode = file_inode(dst_vma->vm_file);
262 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
267 ret = shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC);
268 /* Our caller expects us to return -EFAULT if we failed to find folio */
278 page = folio_file_page(folio, pgoff);
279 if (PageHWPoison(page)) {
284 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
285 page, false, wp_copy);
299 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
305 pgd = pgd_offset(mm, address);
306 p4d = p4d_alloc(mm, pgd, address);
309 pud = pud_alloc(mm, p4d, address);
313 * Note that we didn't run this because the pmd was
314 * missing, the *pmd may be already established and in
315 * turn it may also be a trans_huge_pmd.
317 return pmd_alloc(mm, pud, address);
320 #ifdef CONFIG_HUGETLB_PAGE
322 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
323 * called with mmap_lock held, it will release mmap_lock before returning.
325 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
326 struct vm_area_struct *dst_vma,
327 unsigned long dst_start,
328 unsigned long src_start,
330 enum mcopy_atomic_mode mode,
333 int vm_shared = dst_vma->vm_flags & VM_SHARED;
336 unsigned long src_addr, dst_addr;
339 unsigned long vma_hpagesize;
342 struct address_space *mapping;
345 * There is no default zero huge page for all huge page sizes as
346 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
347 * by THP. Since we can not reliably insert a zero page, this
348 * feature is not supported.
350 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
351 mmap_read_unlock(dst_mm);
355 src_addr = src_start;
356 dst_addr = dst_start;
359 vma_hpagesize = vma_kernel_pagesize(dst_vma);
362 * Validate alignment based on huge page size
365 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
370 * On routine entry dst_vma is set. If we had to drop mmap_lock and
371 * retry, dst_vma will be set to NULL and we must lookup again.
375 dst_vma = find_dst_vma(dst_mm, dst_start, len);
376 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
380 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
383 vm_shared = dst_vma->vm_flags & VM_SHARED;
387 * If not shared, ensure the dst_vma has a anon_vma.
391 if (unlikely(anon_vma_prepare(dst_vma)))
395 while (src_addr < src_start + len) {
396 BUG_ON(dst_addr >= dst_start + len);
399 * Serialize via vma_lock and hugetlb_fault_mutex.
400 * vma_lock ensures the dst_pte remains valid even
401 * in the case of shared pmds. fault mutex prevents
402 * races with other faulting threads.
404 idx = linear_page_index(dst_vma, dst_addr);
405 mapping = dst_vma->vm_file->f_mapping;
406 hash = hugetlb_fault_mutex_hash(mapping, idx);
407 mutex_lock(&hugetlb_fault_mutex_table[hash]);
408 hugetlb_vma_lock_read(dst_vma);
411 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
413 hugetlb_vma_unlock_read(dst_vma);
414 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
418 if (mode != MCOPY_ATOMIC_CONTINUE &&
419 !huge_pte_none_mostly(huge_ptep_get(dst_pte))) {
421 hugetlb_vma_unlock_read(dst_vma);
422 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
426 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
427 dst_addr, src_addr, mode, &page,
430 hugetlb_vma_unlock_read(dst_vma);
431 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
435 if (unlikely(err == -ENOENT)) {
436 mmap_read_unlock(dst_mm);
439 err = copy_huge_page_from_user(page,
440 (const void __user *)src_addr,
441 vma_hpagesize / PAGE_SIZE,
447 mmap_read_lock(dst_mm);
455 dst_addr += vma_hpagesize;
456 src_addr += vma_hpagesize;
457 copied += vma_hpagesize;
459 if (fatal_signal_pending(current))
467 mmap_read_unlock(dst_mm);
473 BUG_ON(!copied && !err);
474 return copied ? copied : err;
476 #else /* !CONFIG_HUGETLB_PAGE */
477 /* fail at build time if gcc attempts to use this */
478 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
479 struct vm_area_struct *dst_vma,
480 unsigned long dst_start,
481 unsigned long src_start,
483 enum mcopy_atomic_mode mode,
485 #endif /* CONFIG_HUGETLB_PAGE */
487 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
489 struct vm_area_struct *dst_vma,
490 unsigned long dst_addr,
491 unsigned long src_addr,
493 enum mcopy_atomic_mode mode,
498 if (mode == MCOPY_ATOMIC_CONTINUE) {
499 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
504 * The normal page fault path for a shmem will invoke the
505 * fault, fill the hole in the file and COW it right away. The
506 * result generates plain anonymous memory. So when we are
507 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
508 * generate anonymous memory directly without actually filling
509 * the hole. For the MAP_PRIVATE case the robustness check
510 * only happens in the pagetable (to verify it's still none)
511 * and not in the radix tree.
513 if (!(dst_vma->vm_flags & VM_SHARED)) {
514 if (mode == MCOPY_ATOMIC_NORMAL)
515 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
516 dst_addr, src_addr, page,
519 err = mfill_zeropage_pte(dst_mm, dst_pmd,
522 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
524 mode != MCOPY_ATOMIC_NORMAL,
531 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
532 unsigned long dst_start,
533 unsigned long src_start,
535 enum mcopy_atomic_mode mcopy_mode,
536 atomic_t *mmap_changing,
539 struct vm_area_struct *dst_vma;
542 unsigned long src_addr, dst_addr;
548 * Sanitize the command parameters:
550 BUG_ON(dst_start & ~PAGE_MASK);
551 BUG_ON(len & ~PAGE_MASK);
553 /* Does the address range wrap, or is the span zero-sized? */
554 BUG_ON(src_start + len <= src_start);
555 BUG_ON(dst_start + len <= dst_start);
557 src_addr = src_start;
558 dst_addr = dst_start;
562 mmap_read_lock(dst_mm);
565 * If memory mappings are changing because of non-cooperative
566 * operation (e.g. mremap) running in parallel, bail out and
567 * request the user to retry later
570 if (mmap_changing && atomic_read(mmap_changing))
574 * Make sure the vma is not shared, that the dst range is
575 * both valid and fully within a single existing vma.
578 dst_vma = find_dst_vma(dst_mm, dst_start, len);
584 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
585 * it will overwrite vm_ops, so vma_is_anonymous must return false.
587 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
588 dst_vma->vm_flags & VM_SHARED))
592 * validate 'mode' now that we know the dst_vma: don't allow
593 * a wrprotect copy if the userfaultfd didn't register as WP.
595 wp_copy = mode & UFFDIO_COPY_MODE_WP;
596 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
600 * If this is a HUGETLB vma, pass off to appropriate routine
602 if (is_vm_hugetlb_page(dst_vma))
603 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
604 src_start, len, mcopy_mode,
607 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
609 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
613 * Ensure the dst_vma has a anon_vma or this page
614 * would get a NULL anon_vma when moved in the
618 if (!(dst_vma->vm_flags & VM_SHARED) &&
619 unlikely(anon_vma_prepare(dst_vma)))
622 while (src_addr < src_start + len) {
625 BUG_ON(dst_addr >= dst_start + len);
627 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
628 if (unlikely(!dst_pmd)) {
633 dst_pmdval = pmd_read_atomic(dst_pmd);
635 * If the dst_pmd is mapped as THP don't
636 * override it and just be strict.
638 if (unlikely(pmd_trans_huge(dst_pmdval))) {
642 if (unlikely(pmd_none(dst_pmdval)) &&
643 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
647 /* If an huge pmd materialized from under us fail */
648 if (unlikely(pmd_trans_huge(*dst_pmd))) {
653 BUG_ON(pmd_none(*dst_pmd));
654 BUG_ON(pmd_trans_huge(*dst_pmd));
656 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
657 src_addr, &page, mcopy_mode, wp_copy);
660 if (unlikely(err == -ENOENT)) {
663 mmap_read_unlock(dst_mm);
666 page_kaddr = kmap_local_page(page);
667 err = copy_from_user(page_kaddr,
668 (const void __user *) src_addr,
670 kunmap_local(page_kaddr);
675 flush_dcache_page(page);
681 dst_addr += PAGE_SIZE;
682 src_addr += PAGE_SIZE;
685 if (fatal_signal_pending(current))
693 mmap_read_unlock(dst_mm);
699 BUG_ON(!copied && !err);
700 return copied ? copied : err;
703 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
704 unsigned long src_start, unsigned long len,
705 atomic_t *mmap_changing, __u64 mode)
707 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
708 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
711 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
712 unsigned long len, atomic_t *mmap_changing)
714 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
718 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
719 unsigned long len, atomic_t *mmap_changing)
721 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
725 void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
726 unsigned long start, unsigned long len, bool enable_wp)
728 struct mmu_gather tlb;
732 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
734 newprot = vm_get_page_prot(dst_vma->vm_flags);
736 tlb_gather_mmu(&tlb, dst_mm);
737 change_protection(&tlb, dst_vma, start, start + len, newprot,
738 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
739 tlb_finish_mmu(&tlb);
742 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
743 unsigned long len, bool enable_wp,
744 atomic_t *mmap_changing)
746 struct vm_area_struct *dst_vma;
747 unsigned long page_mask;
751 * Sanitize the command parameters:
753 BUG_ON(start & ~PAGE_MASK);
754 BUG_ON(len & ~PAGE_MASK);
756 /* Does the address range wrap, or is the span zero-sized? */
757 BUG_ON(start + len <= start);
759 mmap_read_lock(dst_mm);
762 * If memory mappings are changing because of non-cooperative
763 * operation (e.g. mremap) running in parallel, bail out and
764 * request the user to retry later
767 if (mmap_changing && atomic_read(mmap_changing))
771 dst_vma = find_dst_vma(dst_mm, start, len);
775 if (!userfaultfd_wp(dst_vma))
777 if (!vma_can_userfault(dst_vma, dst_vma->vm_flags))
780 if (is_vm_hugetlb_page(dst_vma)) {
782 page_mask = vma_kernel_pagesize(dst_vma) - 1;
783 if ((start & page_mask) || (len & page_mask))
787 uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp);
791 mmap_read_unlock(dst_mm);