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);
34 if (!range_in_vma(dst_vma, dst_start, dst_start + len))
38 * Check the vma is registered in uffd, this is required to
39 * enforce the VM_MAYWRITE check done at uffd registration
42 if (!dst_vma->vm_userfaultfd_ctx.ctx)
49 * Install PTEs, to map dst_addr (within dst_vma) to page.
51 * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
52 * and anon, and for both shared and private VMAs.
54 int mfill_atomic_install_pte(pmd_t *dst_pmd,
55 struct vm_area_struct *dst_vma,
56 unsigned long dst_addr, struct page *page,
57 bool newly_allocated, uffd_flags_t flags)
60 struct mm_struct *dst_mm = dst_vma->vm_mm;
61 pte_t _dst_pte, *dst_pte;
62 bool writable = dst_vma->vm_flags & VM_WRITE;
63 bool vm_shared = dst_vma->vm_flags & VM_SHARED;
64 bool page_in_cache = page_mapping(page);
68 pgoff_t offset, max_off;
70 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
71 _dst_pte = pte_mkdirty(_dst_pte);
72 if (page_in_cache && !vm_shared)
75 _dst_pte = pte_mkwrite(_dst_pte);
76 if (flags & MFILL_ATOMIC_WP)
77 _dst_pte = pte_mkuffd_wp(_dst_pte);
79 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
81 if (vma_is_shmem(dst_vma)) {
82 /* serialize against truncate with the page table lock */
83 inode = dst_vma->vm_file->f_inode;
84 offset = linear_page_index(dst_vma, dst_addr);
85 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
87 if (unlikely(offset >= max_off))
93 * We allow to overwrite a pte marker: consider when both MISSING|WP
94 * registered, we firstly wr-protect a none pte which has no page cache
95 * page backing it, then access the page.
97 if (!pte_none_mostly(*dst_pte))
100 folio = page_folio(page);
102 /* Usually, cache pages are already added to LRU */
104 folio_add_lru(folio);
105 page_add_file_rmap(page, dst_vma, false);
107 page_add_new_anon_rmap(page, dst_vma, dst_addr);
108 folio_add_lru_vma(folio, dst_vma);
112 * Must happen after rmap, as mm_counter() checks mapping (via
113 * PageAnon()), which is set by __page_set_anon_rmap().
115 inc_mm_counter(dst_mm, mm_counter(page));
117 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
119 /* No need to invalidate - it was non-present before */
120 update_mmu_cache(dst_vma, dst_addr, dst_pte);
123 pte_unmap_unlock(dst_pte, ptl);
127 static int mfill_atomic_pte_copy(pmd_t *dst_pmd,
128 struct vm_area_struct *dst_vma,
129 unsigned long dst_addr,
130 unsigned long src_addr,
132 struct folio **foliop)
140 folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, dst_vma,
145 kaddr = kmap_local_folio(folio, 0);
147 * The read mmap_lock is held here. Despite the
148 * mmap_lock being read recursive a deadlock is still
149 * possible if a writer has taken a lock. For example:
151 * process A thread 1 takes read lock on own mmap_lock
152 * process A thread 2 calls mmap, blocks taking write lock
153 * process B thread 1 takes page fault, read lock on own mmap lock
154 * process B thread 2 calls mmap, blocks taking write lock
155 * process A thread 1 blocks taking read lock on process B
156 * process B thread 1 blocks taking read lock on process A
158 * Disable page faults to prevent potential deadlock
159 * and retry the copy outside the mmap_lock.
162 ret = copy_from_user(kaddr, (const void __user *) src_addr,
167 /* fallback to copy_from_user outside mmap_lock */
171 /* don't free the page */
175 flush_dcache_folio(folio);
182 * The memory barrier inside __folio_mark_uptodate makes sure that
183 * preceding stores to the page contents become visible before
184 * the set_pte_at() write.
186 __folio_mark_uptodate(folio);
189 if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL))
192 ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
193 &folio->page, true, flags);
203 static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd,
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_vma->vm_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_vma->vm_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 mfill_atomic_pte_continue(pmd_t *dst_pmd,
239 struct vm_area_struct *dst_vma,
240 unsigned long dst_addr,
243 struct inode *inode = file_inode(dst_vma->vm_file);
244 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
249 ret = shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC);
250 /* Our caller expects us to return -EFAULT if we failed to find folio */
260 page = folio_file_page(folio, pgoff);
261 if (PageHWPoison(page)) {
266 ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
281 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
287 pgd = pgd_offset(mm, address);
288 p4d = p4d_alloc(mm, pgd, address);
291 pud = pud_alloc(mm, p4d, address);
295 * Note that we didn't run this because the pmd was
296 * missing, the *pmd may be already established and in
297 * turn it may also be a trans_huge_pmd.
299 return pmd_alloc(mm, pud, address);
302 #ifdef CONFIG_HUGETLB_PAGE
304 * mfill_atomic processing for HUGETLB vmas. Note that this routine is
305 * called with mmap_lock held, it will release mmap_lock before returning.
307 static __always_inline ssize_t mfill_atomic_hugetlb(
308 struct vm_area_struct *dst_vma,
309 unsigned long dst_start,
310 unsigned long src_start,
314 struct mm_struct *dst_mm = dst_vma->vm_mm;
315 int vm_shared = dst_vma->vm_flags & VM_SHARED;
318 unsigned long src_addr, dst_addr;
321 unsigned long vma_hpagesize;
324 struct address_space *mapping;
327 * There is no default zero huge page for all huge page sizes as
328 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
329 * by THP. Since we can not reliably insert a zero page, this
330 * feature is not supported.
332 if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE)) {
333 mmap_read_unlock(dst_mm);
337 src_addr = src_start;
338 dst_addr = dst_start;
341 vma_hpagesize = vma_kernel_pagesize(dst_vma);
344 * Validate alignment based on huge page size
347 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
352 * On routine entry dst_vma is set. If we had to drop mmap_lock and
353 * retry, dst_vma will be set to NULL and we must lookup again.
357 dst_vma = find_dst_vma(dst_mm, dst_start, len);
358 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
362 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
365 vm_shared = dst_vma->vm_flags & VM_SHARED;
369 * If not shared, ensure the dst_vma has a anon_vma.
373 if (unlikely(anon_vma_prepare(dst_vma)))
377 while (src_addr < src_start + len) {
378 BUG_ON(dst_addr >= dst_start + len);
381 * Serialize via vma_lock and hugetlb_fault_mutex.
382 * vma_lock ensures the dst_pte remains valid even
383 * in the case of shared pmds. fault mutex prevents
384 * races with other faulting threads.
386 idx = linear_page_index(dst_vma, dst_addr);
387 mapping = dst_vma->vm_file->f_mapping;
388 hash = hugetlb_fault_mutex_hash(mapping, idx);
389 mutex_lock(&hugetlb_fault_mutex_table[hash]);
390 hugetlb_vma_lock_read(dst_vma);
393 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
395 hugetlb_vma_unlock_read(dst_vma);
396 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
400 if (!uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE) &&
401 !huge_pte_none_mostly(huge_ptep_get(dst_pte))) {
403 hugetlb_vma_unlock_read(dst_vma);
404 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
408 err = hugetlb_mfill_atomic_pte(dst_pte, dst_vma, dst_addr,
409 src_addr, flags, &folio);
411 hugetlb_vma_unlock_read(dst_vma);
412 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
416 if (unlikely(err == -ENOENT)) {
417 mmap_read_unlock(dst_mm);
420 err = copy_folio_from_user(folio,
421 (const void __user *)src_addr, true);
426 mmap_read_lock(dst_mm);
434 dst_addr += vma_hpagesize;
435 src_addr += vma_hpagesize;
436 copied += vma_hpagesize;
438 if (fatal_signal_pending(current))
446 mmap_read_unlock(dst_mm);
452 BUG_ON(!copied && !err);
453 return copied ? copied : err;
455 #else /* !CONFIG_HUGETLB_PAGE */
456 /* fail at build time if gcc attempts to use this */
457 extern ssize_t mfill_atomic_hugetlb(struct vm_area_struct *dst_vma,
458 unsigned long dst_start,
459 unsigned long src_start,
462 #endif /* CONFIG_HUGETLB_PAGE */
464 static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd,
465 struct vm_area_struct *dst_vma,
466 unsigned long dst_addr,
467 unsigned long src_addr,
469 struct folio **foliop)
473 if (uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) {
474 return mfill_atomic_pte_continue(dst_pmd, dst_vma,
479 * The normal page fault path for a shmem will invoke the
480 * fault, fill the hole in the file and COW it right away. The
481 * result generates plain anonymous memory. So when we are
482 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
483 * generate anonymous memory directly without actually filling
484 * the hole. For the MAP_PRIVATE case the robustness check
485 * only happens in the pagetable (to verify it's still none)
486 * and not in the radix tree.
488 if (!(dst_vma->vm_flags & VM_SHARED)) {
489 if (uffd_flags_mode_is(flags, MFILL_ATOMIC_COPY))
490 err = mfill_atomic_pte_copy(dst_pmd, dst_vma,
494 err = mfill_atomic_pte_zeropage(dst_pmd,
497 err = shmem_mfill_atomic_pte(dst_pmd, dst_vma,
505 static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm,
506 unsigned long dst_start,
507 unsigned long src_start,
509 atomic_t *mmap_changing,
512 struct vm_area_struct *dst_vma;
515 unsigned long src_addr, dst_addr;
520 * Sanitize the command parameters:
522 BUG_ON(dst_start & ~PAGE_MASK);
523 BUG_ON(len & ~PAGE_MASK);
525 /* Does the address range wrap, or is the span zero-sized? */
526 BUG_ON(src_start + len <= src_start);
527 BUG_ON(dst_start + len <= dst_start);
529 src_addr = src_start;
530 dst_addr = dst_start;
534 mmap_read_lock(dst_mm);
537 * If memory mappings are changing because of non-cooperative
538 * operation (e.g. mremap) running in parallel, bail out and
539 * request the user to retry later
542 if (mmap_changing && atomic_read(mmap_changing))
546 * Make sure the vma is not shared, that the dst range is
547 * both valid and fully within a single existing vma.
550 dst_vma = find_dst_vma(dst_mm, dst_start, len);
556 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
557 * it will overwrite vm_ops, so vma_is_anonymous must return false.
559 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
560 dst_vma->vm_flags & VM_SHARED))
564 * validate 'mode' now that we know the dst_vma: don't allow
565 * a wrprotect copy if the userfaultfd didn't register as WP.
567 if ((flags & MFILL_ATOMIC_WP) && !(dst_vma->vm_flags & VM_UFFD_WP))
571 * If this is a HUGETLB vma, pass off to appropriate routine
573 if (is_vm_hugetlb_page(dst_vma))
574 return mfill_atomic_hugetlb(dst_vma, dst_start,
575 src_start, len, flags);
577 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
579 if (!vma_is_shmem(dst_vma) &&
580 uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE))
584 * Ensure the dst_vma has a anon_vma or this page
585 * would get a NULL anon_vma when moved in the
589 if (!(dst_vma->vm_flags & VM_SHARED) &&
590 unlikely(anon_vma_prepare(dst_vma)))
593 while (src_addr < src_start + len) {
596 BUG_ON(dst_addr >= dst_start + len);
598 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
599 if (unlikely(!dst_pmd)) {
604 dst_pmdval = pmdp_get_lockless(dst_pmd);
606 * If the dst_pmd is mapped as THP don't
607 * override it and just be strict.
609 if (unlikely(pmd_trans_huge(dst_pmdval))) {
613 if (unlikely(pmd_none(dst_pmdval)) &&
614 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
618 /* If an huge pmd materialized from under us fail */
619 if (unlikely(pmd_trans_huge(*dst_pmd))) {
624 BUG_ON(pmd_none(*dst_pmd));
625 BUG_ON(pmd_trans_huge(*dst_pmd));
627 err = mfill_atomic_pte(dst_pmd, dst_vma, dst_addr,
628 src_addr, flags, &folio);
631 if (unlikely(err == -ENOENT)) {
634 mmap_read_unlock(dst_mm);
637 kaddr = kmap_local_folio(folio, 0);
638 err = copy_from_user(kaddr,
639 (const void __user *) src_addr,
646 flush_dcache_folio(folio);
652 dst_addr += PAGE_SIZE;
653 src_addr += PAGE_SIZE;
656 if (fatal_signal_pending(current))
664 mmap_read_unlock(dst_mm);
670 BUG_ON(!copied && !err);
671 return copied ? copied : err;
674 ssize_t mfill_atomic_copy(struct mm_struct *dst_mm, unsigned long dst_start,
675 unsigned long src_start, unsigned long len,
676 atomic_t *mmap_changing, uffd_flags_t flags)
678 return mfill_atomic(dst_mm, dst_start, src_start, len, mmap_changing,
679 uffd_flags_set_mode(flags, MFILL_ATOMIC_COPY));
682 ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, unsigned long start,
683 unsigned long len, atomic_t *mmap_changing)
685 return mfill_atomic(dst_mm, start, 0, len, mmap_changing,
686 uffd_flags_set_mode(0, MFILL_ATOMIC_ZEROPAGE));
689 ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long start,
690 unsigned long len, atomic_t *mmap_changing,
693 return mfill_atomic(dst_mm, start, 0, len, mmap_changing,
694 uffd_flags_set_mode(flags, MFILL_ATOMIC_CONTINUE));
697 long uffd_wp_range(struct vm_area_struct *dst_vma,
698 unsigned long start, unsigned long len, bool enable_wp)
700 unsigned int mm_cp_flags;
701 struct mmu_gather tlb;
704 VM_WARN_ONCE(start < dst_vma->vm_start || start + len > dst_vma->vm_end,
705 "The address range exceeds VMA boundary.\n");
707 mm_cp_flags = MM_CP_UFFD_WP;
709 mm_cp_flags = MM_CP_UFFD_WP_RESOLVE;
712 * vma->vm_page_prot already reflects that uffd-wp is enabled for this
713 * VMA (see userfaultfd_set_vm_flags()) and that all PTEs are supposed
714 * to be write-protected as default whenever protection changes.
715 * Try upgrading write permissions manually.
717 if (!enable_wp && vma_wants_manual_pte_write_upgrade(dst_vma))
718 mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
719 tlb_gather_mmu(&tlb, dst_vma->vm_mm);
720 ret = change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags);
721 tlb_finish_mmu(&tlb);
726 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
727 unsigned long len, bool enable_wp,
728 atomic_t *mmap_changing)
730 unsigned long end = start + len;
731 unsigned long _start, _end;
732 struct vm_area_struct *dst_vma;
733 unsigned long page_mask;
735 VMA_ITERATOR(vmi, dst_mm, start);
738 * Sanitize the command parameters:
740 BUG_ON(start & ~PAGE_MASK);
741 BUG_ON(len & ~PAGE_MASK);
743 /* Does the address range wrap, or is the span zero-sized? */
744 BUG_ON(start + len <= start);
746 mmap_read_lock(dst_mm);
749 * If memory mappings are changing because of non-cooperative
750 * operation (e.g. mremap) running in parallel, bail out and
751 * request the user to retry later
754 if (mmap_changing && atomic_read(mmap_changing))
758 for_each_vma_range(vmi, dst_vma, end) {
760 if (!userfaultfd_wp(dst_vma)) {
765 if (is_vm_hugetlb_page(dst_vma)) {
767 page_mask = vma_kernel_pagesize(dst_vma) - 1;
768 if ((start & page_mask) || (len & page_mask))
772 _start = max(dst_vma->vm_start, start);
773 _end = min(dst_vma->vm_end, end);
775 err = uffd_wp_range(dst_vma, _start, _end - _start, enable_wp);
777 /* Return 0 on success, <0 on failures */
783 mmap_read_unlock(dst_mm);