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);
71 pgoff_t offset, max_off;
73 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
74 _dst_pte = pte_mkdirty(_dst_pte);
75 if (page_in_cache && !vm_shared)
79 * Always mark a PTE as write-protected when needed, regardless of
80 * VM_WRITE, which the user might change.
83 _dst_pte = pte_mkuffd_wp(_dst_pte);
88 _dst_pte = pte_mkwrite(_dst_pte);
91 * We need this to make sure write bit removed; as mk_pte()
92 * could return a pte with write bit set.
94 _dst_pte = pte_wrprotect(_dst_pte);
96 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
98 if (vma_is_shmem(dst_vma)) {
99 /* serialize against truncate with the page table lock */
100 inode = dst_vma->vm_file->f_inode;
101 offset = linear_page_index(dst_vma, dst_addr);
102 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
104 if (unlikely(offset >= max_off))
110 * We allow to overwrite a pte marker: consider when both MISSING|WP
111 * registered, we firstly wr-protect a none pte which has no page cache
112 * page backing it, then access the page.
114 if (!pte_none_mostly(*dst_pte))
117 folio = page_folio(page);
119 /* Usually, cache pages are already added to LRU */
121 folio_add_lru(folio);
122 page_add_file_rmap(page, dst_vma, false);
124 page_add_new_anon_rmap(page, dst_vma, dst_addr);
125 folio_add_lru_vma(folio, dst_vma);
129 * Must happen after rmap, as mm_counter() checks mapping (via
130 * PageAnon()), which is set by __page_set_anon_rmap().
132 inc_mm_counter(dst_mm, mm_counter(page));
134 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
136 /* No need to invalidate - it was non-present before */
137 update_mmu_cache(dst_vma, dst_addr, dst_pte);
140 pte_unmap_unlock(dst_pte, ptl);
144 static int mcopy_atomic_pte(struct mm_struct *dst_mm,
146 struct vm_area_struct *dst_vma,
147 unsigned long dst_addr,
148 unsigned long src_addr,
158 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
162 page_kaddr = kmap_local_page(page);
164 * The read mmap_lock is held here. Despite the
165 * mmap_lock being read recursive a deadlock is still
166 * possible if a writer has taken a lock. For example:
168 * process A thread 1 takes read lock on own mmap_lock
169 * process A thread 2 calls mmap, blocks taking write lock
170 * process B thread 1 takes page fault, read lock on own mmap lock
171 * process B thread 2 calls mmap, blocks taking write lock
172 * process A thread 1 blocks taking read lock on process B
173 * process B thread 1 blocks taking read lock on process A
175 * Disable page faults to prevent potential deadlock
176 * and retry the copy outside the mmap_lock.
179 ret = copy_from_user(page_kaddr,
180 (const void __user *) src_addr,
183 kunmap_local(page_kaddr);
185 /* fallback to copy_from_user outside mmap_lock */
189 /* don't free the page */
193 flush_dcache_page(page);
200 * The memory barrier inside __SetPageUptodate makes sure that
201 * preceding stores to the page contents become visible before
202 * the set_pte_at() write.
204 __SetPageUptodate(page);
207 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL))
210 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
211 page, true, wp_copy);
221 static int mfill_zeropage_pte(struct mm_struct *dst_mm,
223 struct vm_area_struct *dst_vma,
224 unsigned long dst_addr)
226 pte_t _dst_pte, *dst_pte;
229 pgoff_t offset, max_off;
232 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
233 dst_vma->vm_page_prot));
234 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
235 if (dst_vma->vm_file) {
236 /* the shmem MAP_PRIVATE case requires checking the i_size */
237 inode = dst_vma->vm_file->f_inode;
238 offset = linear_page_index(dst_vma, dst_addr);
239 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
241 if (unlikely(offset >= max_off))
245 if (!pte_none(*dst_pte))
247 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
248 /* No need to invalidate - it was non-present before */
249 update_mmu_cache(dst_vma, dst_addr, dst_pte);
252 pte_unmap_unlock(dst_pte, ptl);
256 /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
257 static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
259 struct vm_area_struct *dst_vma,
260 unsigned long dst_addr,
263 struct inode *inode = file_inode(dst_vma->vm_file);
264 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
269 ret = shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC);
270 /* Our caller expects us to return -EFAULT if we failed to find folio */
280 page = folio_file_page(folio, pgoff);
281 if (PageHWPoison(page)) {
286 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
287 page, false, wp_copy);
301 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
307 pgd = pgd_offset(mm, address);
308 p4d = p4d_alloc(mm, pgd, address);
311 pud = pud_alloc(mm, p4d, address);
315 * Note that we didn't run this because the pmd was
316 * missing, the *pmd may be already established and in
317 * turn it may also be a trans_huge_pmd.
319 return pmd_alloc(mm, pud, address);
322 #ifdef CONFIG_HUGETLB_PAGE
324 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
325 * called with mmap_lock held, it will release mmap_lock before returning.
327 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
328 struct vm_area_struct *dst_vma,
329 unsigned long dst_start,
330 unsigned long src_start,
332 enum mcopy_atomic_mode mode,
335 int vm_shared = dst_vma->vm_flags & VM_SHARED;
338 unsigned long src_addr, dst_addr;
341 unsigned long vma_hpagesize;
344 struct address_space *mapping;
347 * There is no default zero huge page for all huge page sizes as
348 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
349 * by THP. Since we can not reliably insert a zero page, this
350 * feature is not supported.
352 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
353 mmap_read_unlock(dst_mm);
357 src_addr = src_start;
358 dst_addr = dst_start;
361 vma_hpagesize = vma_kernel_pagesize(dst_vma);
364 * Validate alignment based on huge page size
367 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
372 * On routine entry dst_vma is set. If we had to drop mmap_lock and
373 * retry, dst_vma will be set to NULL and we must lookup again.
377 dst_vma = find_dst_vma(dst_mm, dst_start, len);
378 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
382 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
385 vm_shared = dst_vma->vm_flags & VM_SHARED;
389 * If not shared, ensure the dst_vma has a anon_vma.
393 if (unlikely(anon_vma_prepare(dst_vma)))
397 while (src_addr < src_start + len) {
398 BUG_ON(dst_addr >= dst_start + len);
401 * Serialize via vma_lock and hugetlb_fault_mutex.
402 * vma_lock ensures the dst_pte remains valid even
403 * in the case of shared pmds. fault mutex prevents
404 * races with other faulting threads.
406 idx = linear_page_index(dst_vma, dst_addr);
407 mapping = dst_vma->vm_file->f_mapping;
408 hash = hugetlb_fault_mutex_hash(mapping, idx);
409 mutex_lock(&hugetlb_fault_mutex_table[hash]);
410 hugetlb_vma_lock_read(dst_vma);
413 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
415 hugetlb_vma_unlock_read(dst_vma);
416 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
420 if (mode != MCOPY_ATOMIC_CONTINUE &&
421 !huge_pte_none_mostly(huge_ptep_get(dst_pte))) {
423 hugetlb_vma_unlock_read(dst_vma);
424 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
428 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
429 dst_addr, src_addr, mode, &page,
432 hugetlb_vma_unlock_read(dst_vma);
433 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
437 if (unlikely(err == -ENOENT)) {
438 mmap_read_unlock(dst_mm);
441 err = copy_huge_page_from_user(page,
442 (const void __user *)src_addr,
443 vma_hpagesize / PAGE_SIZE,
449 mmap_read_lock(dst_mm);
457 dst_addr += vma_hpagesize;
458 src_addr += vma_hpagesize;
459 copied += vma_hpagesize;
461 if (fatal_signal_pending(current))
469 mmap_read_unlock(dst_mm);
475 BUG_ON(!copied && !err);
476 return copied ? copied : err;
478 #else /* !CONFIG_HUGETLB_PAGE */
479 /* fail at build time if gcc attempts to use this */
480 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
481 struct vm_area_struct *dst_vma,
482 unsigned long dst_start,
483 unsigned long src_start,
485 enum mcopy_atomic_mode mode,
487 #endif /* CONFIG_HUGETLB_PAGE */
489 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
491 struct vm_area_struct *dst_vma,
492 unsigned long dst_addr,
493 unsigned long src_addr,
495 enum mcopy_atomic_mode mode,
500 if (mode == MCOPY_ATOMIC_CONTINUE) {
501 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
506 * The normal page fault path for a shmem will invoke the
507 * fault, fill the hole in the file and COW it right away. The
508 * result generates plain anonymous memory. So when we are
509 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
510 * generate anonymous memory directly without actually filling
511 * the hole. For the MAP_PRIVATE case the robustness check
512 * only happens in the pagetable (to verify it's still none)
513 * and not in the radix tree.
515 if (!(dst_vma->vm_flags & VM_SHARED)) {
516 if (mode == MCOPY_ATOMIC_NORMAL)
517 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
518 dst_addr, src_addr, page,
521 err = mfill_zeropage_pte(dst_mm, dst_pmd,
524 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
526 mode != MCOPY_ATOMIC_NORMAL,
533 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
534 unsigned long dst_start,
535 unsigned long src_start,
537 enum mcopy_atomic_mode mcopy_mode,
538 atomic_t *mmap_changing,
541 struct vm_area_struct *dst_vma;
544 unsigned long src_addr, dst_addr;
550 * Sanitize the command parameters:
552 BUG_ON(dst_start & ~PAGE_MASK);
553 BUG_ON(len & ~PAGE_MASK);
555 /* Does the address range wrap, or is the span zero-sized? */
556 BUG_ON(src_start + len <= src_start);
557 BUG_ON(dst_start + len <= dst_start);
559 src_addr = src_start;
560 dst_addr = dst_start;
564 mmap_read_lock(dst_mm);
567 * If memory mappings are changing because of non-cooperative
568 * operation (e.g. mremap) running in parallel, bail out and
569 * request the user to retry later
572 if (mmap_changing && atomic_read(mmap_changing))
576 * Make sure the vma is not shared, that the dst range is
577 * both valid and fully within a single existing vma.
580 dst_vma = find_dst_vma(dst_mm, dst_start, len);
586 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
587 * it will overwrite vm_ops, so vma_is_anonymous must return false.
589 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
590 dst_vma->vm_flags & VM_SHARED))
594 * validate 'mode' now that we know the dst_vma: don't allow
595 * a wrprotect copy if the userfaultfd didn't register as WP.
597 wp_copy = mode & UFFDIO_COPY_MODE_WP;
598 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
602 * If this is a HUGETLB vma, pass off to appropriate routine
604 if (is_vm_hugetlb_page(dst_vma))
605 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
606 src_start, len, mcopy_mode,
609 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
611 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
615 * Ensure the dst_vma has a anon_vma or this page
616 * would get a NULL anon_vma when moved in the
620 if (!(dst_vma->vm_flags & VM_SHARED) &&
621 unlikely(anon_vma_prepare(dst_vma)))
624 while (src_addr < src_start + len) {
627 BUG_ON(dst_addr >= dst_start + len);
629 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
630 if (unlikely(!dst_pmd)) {
635 dst_pmdval = pmdp_get_lockless(dst_pmd);
637 * If the dst_pmd is mapped as THP don't
638 * override it and just be strict.
640 if (unlikely(pmd_trans_huge(dst_pmdval))) {
644 if (unlikely(pmd_none(dst_pmdval)) &&
645 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
649 /* If an huge pmd materialized from under us fail */
650 if (unlikely(pmd_trans_huge(*dst_pmd))) {
655 BUG_ON(pmd_none(*dst_pmd));
656 BUG_ON(pmd_trans_huge(*dst_pmd));
658 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
659 src_addr, &page, mcopy_mode, wp_copy);
662 if (unlikely(err == -ENOENT)) {
665 mmap_read_unlock(dst_mm);
668 page_kaddr = kmap_local_page(page);
669 err = copy_from_user(page_kaddr,
670 (const void __user *) src_addr,
672 kunmap_local(page_kaddr);
677 flush_dcache_page(page);
683 dst_addr += PAGE_SIZE;
684 src_addr += PAGE_SIZE;
687 if (fatal_signal_pending(current))
695 mmap_read_unlock(dst_mm);
701 BUG_ON(!copied && !err);
702 return copied ? copied : err;
705 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
706 unsigned long src_start, unsigned long len,
707 atomic_t *mmap_changing, __u64 mode)
709 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
710 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
713 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
714 unsigned long len, atomic_t *mmap_changing)
716 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
720 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
721 unsigned long len, atomic_t *mmap_changing)
723 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
727 void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
728 unsigned long start, unsigned long len, bool enable_wp)
730 struct mmu_gather tlb;
734 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
736 newprot = vm_get_page_prot(dst_vma->vm_flags);
738 tlb_gather_mmu(&tlb, dst_mm);
739 change_protection(&tlb, dst_vma, start, start + len, newprot,
740 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
741 tlb_finish_mmu(&tlb);
744 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
745 unsigned long len, bool enable_wp,
746 atomic_t *mmap_changing)
748 struct vm_area_struct *dst_vma;
749 unsigned long page_mask;
753 * Sanitize the command parameters:
755 BUG_ON(start & ~PAGE_MASK);
756 BUG_ON(len & ~PAGE_MASK);
758 /* Does the address range wrap, or is the span zero-sized? */
759 BUG_ON(start + len <= start);
761 mmap_read_lock(dst_mm);
764 * If memory mappings are changing because of non-cooperative
765 * operation (e.g. mremap) running in parallel, bail out and
766 * request the user to retry later
769 if (mmap_changing && atomic_read(mmap_changing))
773 dst_vma = find_dst_vma(dst_mm, start, len);
777 if (!userfaultfd_wp(dst_vma))
779 if (!vma_can_userfault(dst_vma, dst_vma->vm_flags))
782 if (is_vm_hugetlb_page(dst_vma)) {
784 page_mask = vma_kernel_pagesize(dst_vma) - 1;
785 if ((start & page_mask) || (len & page_mask))
789 uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp);
793 mmap_read_unlock(dst_mm);