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)
78 _dst_pte = pte_mkwrite(_dst_pte);
80 _dst_pte = pte_mkuffd_wp(_dst_pte);
82 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
84 if (vma_is_shmem(dst_vma)) {
85 /* serialize against truncate with the page table lock */
86 inode = dst_vma->vm_file->f_inode;
87 offset = linear_page_index(dst_vma, dst_addr);
88 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
90 if (unlikely(offset >= max_off))
96 * We allow to overwrite a pte marker: consider when both MISSING|WP
97 * registered, we firstly wr-protect a none pte which has no page cache
98 * page backing it, then access the page.
100 if (!pte_none_mostly(*dst_pte))
103 folio = page_folio(page);
105 /* Usually, cache pages are already added to LRU */
107 folio_add_lru(folio);
108 page_add_file_rmap(page, dst_vma, false);
110 page_add_new_anon_rmap(page, dst_vma, dst_addr);
111 folio_add_lru_vma(folio, dst_vma);
115 * Must happen after rmap, as mm_counter() checks mapping (via
116 * PageAnon()), which is set by __page_set_anon_rmap().
118 inc_mm_counter(dst_mm, mm_counter(page));
120 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
122 /* No need to invalidate - it was non-present before */
123 update_mmu_cache(dst_vma, dst_addr, dst_pte);
126 pte_unmap_unlock(dst_pte, ptl);
130 static int mcopy_atomic_pte(struct mm_struct *dst_mm,
132 struct vm_area_struct *dst_vma,
133 unsigned long dst_addr,
134 unsigned long src_addr,
144 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
148 page_kaddr = kmap_local_page(page);
150 * The read mmap_lock is held here. Despite the
151 * mmap_lock being read recursive a deadlock is still
152 * possible if a writer has taken a lock. For example:
154 * process A thread 1 takes read lock on own mmap_lock
155 * process A thread 2 calls mmap, blocks taking write lock
156 * process B thread 1 takes page fault, read lock on own mmap lock
157 * process B thread 2 calls mmap, blocks taking write lock
158 * process A thread 1 blocks taking read lock on process B
159 * process B thread 1 blocks taking read lock on process A
161 * Disable page faults to prevent potential deadlock
162 * and retry the copy outside the mmap_lock.
165 ret = copy_from_user(page_kaddr,
166 (const void __user *) src_addr,
169 kunmap_local(page_kaddr);
171 /* fallback to copy_from_user outside mmap_lock */
175 /* don't free the page */
179 flush_dcache_page(page);
186 * The memory barrier inside __SetPageUptodate makes sure that
187 * preceding stores to the page contents become visible before
188 * the set_pte_at() write.
190 __SetPageUptodate(page);
193 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL))
196 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
197 page, true, wp_copy);
207 static int mfill_zeropage_pte(struct mm_struct *dst_mm,
209 struct vm_area_struct *dst_vma,
210 unsigned long dst_addr)
212 pte_t _dst_pte, *dst_pte;
215 pgoff_t offset, max_off;
218 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
219 dst_vma->vm_page_prot));
220 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
221 if (dst_vma->vm_file) {
222 /* the shmem MAP_PRIVATE case requires checking the i_size */
223 inode = dst_vma->vm_file->f_inode;
224 offset = linear_page_index(dst_vma, dst_addr);
225 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
227 if (unlikely(offset >= max_off))
231 if (!pte_none(*dst_pte))
233 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
234 /* No need to invalidate - it was non-present before */
235 update_mmu_cache(dst_vma, dst_addr, dst_pte);
238 pte_unmap_unlock(dst_pte, ptl);
242 /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
243 static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
245 struct vm_area_struct *dst_vma,
246 unsigned long dst_addr,
249 struct inode *inode = file_inode(dst_vma->vm_file);
250 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
255 ret = shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC);
256 /* Our caller expects us to return -EFAULT if we failed to find folio */
266 page = folio_file_page(folio, pgoff);
267 if (PageHWPoison(page)) {
272 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
273 page, false, wp_copy);
287 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
293 pgd = pgd_offset(mm, address);
294 p4d = p4d_alloc(mm, pgd, address);
297 pud = pud_alloc(mm, p4d, address);
301 * Note that we didn't run this because the pmd was
302 * missing, the *pmd may be already established and in
303 * turn it may also be a trans_huge_pmd.
305 return pmd_alloc(mm, pud, address);
308 #ifdef CONFIG_HUGETLB_PAGE
310 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
311 * called with mmap_lock held, it will release mmap_lock before returning.
313 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
314 struct vm_area_struct *dst_vma,
315 unsigned long dst_start,
316 unsigned long src_start,
318 enum mcopy_atomic_mode mode,
321 int vm_shared = dst_vma->vm_flags & VM_SHARED;
324 unsigned long src_addr, dst_addr;
327 unsigned long vma_hpagesize;
330 struct address_space *mapping;
333 * There is no default zero huge page for all huge page sizes as
334 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
335 * by THP. Since we can not reliably insert a zero page, this
336 * feature is not supported.
338 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
339 mmap_read_unlock(dst_mm);
343 src_addr = src_start;
344 dst_addr = dst_start;
347 vma_hpagesize = vma_kernel_pagesize(dst_vma);
350 * Validate alignment based on huge page size
353 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
358 * On routine entry dst_vma is set. If we had to drop mmap_lock and
359 * retry, dst_vma will be set to NULL and we must lookup again.
363 dst_vma = find_dst_vma(dst_mm, dst_start, len);
364 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
368 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
371 vm_shared = dst_vma->vm_flags & VM_SHARED;
375 * If not shared, ensure the dst_vma has a anon_vma.
379 if (unlikely(anon_vma_prepare(dst_vma)))
383 while (src_addr < src_start + len) {
384 BUG_ON(dst_addr >= dst_start + len);
387 * Serialize via vma_lock and hugetlb_fault_mutex.
388 * vma_lock ensures the dst_pte remains valid even
389 * in the case of shared pmds. fault mutex prevents
390 * races with other faulting threads.
392 idx = linear_page_index(dst_vma, dst_addr);
393 mapping = dst_vma->vm_file->f_mapping;
394 hash = hugetlb_fault_mutex_hash(mapping, idx);
395 mutex_lock(&hugetlb_fault_mutex_table[hash]);
396 hugetlb_vma_lock_read(dst_vma);
399 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
401 hugetlb_vma_unlock_read(dst_vma);
402 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
406 if (mode != MCOPY_ATOMIC_CONTINUE &&
407 !huge_pte_none_mostly(huge_ptep_get(dst_pte))) {
409 hugetlb_vma_unlock_read(dst_vma);
410 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
414 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
415 dst_addr, src_addr, mode, &page,
418 hugetlb_vma_unlock_read(dst_vma);
419 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
423 if (unlikely(err == -ENOENT)) {
424 mmap_read_unlock(dst_mm);
427 err = copy_huge_page_from_user(page,
428 (const void __user *)src_addr,
429 vma_hpagesize / PAGE_SIZE,
435 mmap_read_lock(dst_mm);
443 dst_addr += vma_hpagesize;
444 src_addr += vma_hpagesize;
445 copied += vma_hpagesize;
447 if (fatal_signal_pending(current))
455 mmap_read_unlock(dst_mm);
461 BUG_ON(!copied && !err);
462 return copied ? copied : err;
464 #else /* !CONFIG_HUGETLB_PAGE */
465 /* fail at build time if gcc attempts to use this */
466 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
467 struct vm_area_struct *dst_vma,
468 unsigned long dst_start,
469 unsigned long src_start,
471 enum mcopy_atomic_mode mode,
473 #endif /* CONFIG_HUGETLB_PAGE */
475 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
477 struct vm_area_struct *dst_vma,
478 unsigned long dst_addr,
479 unsigned long src_addr,
481 enum mcopy_atomic_mode mode,
486 if (mode == MCOPY_ATOMIC_CONTINUE) {
487 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
492 * The normal page fault path for a shmem will invoke the
493 * fault, fill the hole in the file and COW it right away. The
494 * result generates plain anonymous memory. So when we are
495 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
496 * generate anonymous memory directly without actually filling
497 * the hole. For the MAP_PRIVATE case the robustness check
498 * only happens in the pagetable (to verify it's still none)
499 * and not in the radix tree.
501 if (!(dst_vma->vm_flags & VM_SHARED)) {
502 if (mode == MCOPY_ATOMIC_NORMAL)
503 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
504 dst_addr, src_addr, page,
507 err = mfill_zeropage_pte(dst_mm, dst_pmd,
510 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
512 mode != MCOPY_ATOMIC_NORMAL,
519 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
520 unsigned long dst_start,
521 unsigned long src_start,
523 enum mcopy_atomic_mode mcopy_mode,
524 atomic_t *mmap_changing,
527 struct vm_area_struct *dst_vma;
530 unsigned long src_addr, dst_addr;
536 * Sanitize the command parameters:
538 BUG_ON(dst_start & ~PAGE_MASK);
539 BUG_ON(len & ~PAGE_MASK);
541 /* Does the address range wrap, or is the span zero-sized? */
542 BUG_ON(src_start + len <= src_start);
543 BUG_ON(dst_start + len <= dst_start);
545 src_addr = src_start;
546 dst_addr = dst_start;
550 mmap_read_lock(dst_mm);
553 * If memory mappings are changing because of non-cooperative
554 * operation (e.g. mremap) running in parallel, bail out and
555 * request the user to retry later
558 if (mmap_changing && atomic_read(mmap_changing))
562 * Make sure the vma is not shared, that the dst range is
563 * both valid and fully within a single existing vma.
566 dst_vma = find_dst_vma(dst_mm, dst_start, len);
572 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
573 * it will overwrite vm_ops, so vma_is_anonymous must return false.
575 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
576 dst_vma->vm_flags & VM_SHARED))
580 * validate 'mode' now that we know the dst_vma: don't allow
581 * a wrprotect copy if the userfaultfd didn't register as WP.
583 wp_copy = mode & UFFDIO_COPY_MODE_WP;
584 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
588 * If this is a HUGETLB vma, pass off to appropriate routine
590 if (is_vm_hugetlb_page(dst_vma))
591 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
592 src_start, len, mcopy_mode,
595 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
597 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
601 * Ensure the dst_vma has a anon_vma or this page
602 * would get a NULL anon_vma when moved in the
606 if (!(dst_vma->vm_flags & VM_SHARED) &&
607 unlikely(anon_vma_prepare(dst_vma)))
610 while (src_addr < src_start + len) {
613 BUG_ON(dst_addr >= dst_start + len);
615 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
616 if (unlikely(!dst_pmd)) {
621 dst_pmdval = pmdp_get_lockless(dst_pmd);
623 * If the dst_pmd is mapped as THP don't
624 * override it and just be strict.
626 if (unlikely(pmd_trans_huge(dst_pmdval))) {
630 if (unlikely(pmd_none(dst_pmdval)) &&
631 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
635 /* If an huge pmd materialized from under us fail */
636 if (unlikely(pmd_trans_huge(*dst_pmd))) {
641 BUG_ON(pmd_none(*dst_pmd));
642 BUG_ON(pmd_trans_huge(*dst_pmd));
644 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
645 src_addr, &page, mcopy_mode, wp_copy);
648 if (unlikely(err == -ENOENT)) {
651 mmap_read_unlock(dst_mm);
654 page_kaddr = kmap_local_page(page);
655 err = copy_from_user(page_kaddr,
656 (const void __user *) src_addr,
658 kunmap_local(page_kaddr);
663 flush_dcache_page(page);
669 dst_addr += PAGE_SIZE;
670 src_addr += PAGE_SIZE;
673 if (fatal_signal_pending(current))
681 mmap_read_unlock(dst_mm);
687 BUG_ON(!copied && !err);
688 return copied ? copied : err;
691 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
692 unsigned long src_start, unsigned long len,
693 atomic_t *mmap_changing, __u64 mode)
695 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
696 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
699 ssize_t mfill_zeropage(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_ZEROPAGE,
706 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
707 unsigned long len, atomic_t *mmap_changing)
709 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
713 long uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
714 unsigned long start, unsigned long len, bool enable_wp)
716 unsigned int mm_cp_flags;
717 struct mmu_gather tlb;
721 mm_cp_flags = MM_CP_UFFD_WP;
723 mm_cp_flags = MM_CP_UFFD_WP_RESOLVE;
726 * vma->vm_page_prot already reflects that uffd-wp is enabled for this
727 * VMA (see userfaultfd_set_vm_flags()) and that all PTEs are supposed
728 * to be write-protected as default whenever protection changes.
729 * Try upgrading write permissions manually.
731 if (!enable_wp && vma_wants_manual_pte_write_upgrade(dst_vma))
732 mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
733 tlb_gather_mmu(&tlb, dst_mm);
734 ret = change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags);
735 tlb_finish_mmu(&tlb);
740 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
741 unsigned long len, bool enable_wp,
742 atomic_t *mmap_changing)
744 struct vm_area_struct *dst_vma;
745 unsigned long page_mask;
749 * Sanitize the command parameters:
751 BUG_ON(start & ~PAGE_MASK);
752 BUG_ON(len & ~PAGE_MASK);
754 /* Does the address range wrap, or is the span zero-sized? */
755 BUG_ON(start + len <= start);
757 mmap_read_lock(dst_mm);
760 * If memory mappings are changing because of non-cooperative
761 * operation (e.g. mremap) running in parallel, bail out and
762 * request the user to retry later
765 if (mmap_changing && atomic_read(mmap_changing))
769 dst_vma = find_dst_vma(dst_mm, start, len);
773 if (!userfaultfd_wp(dst_vma))
775 if (!vma_can_userfault(dst_vma, dst_vma->vm_flags))
778 if (is_vm_hugetlb_page(dst_vma)) {
780 page_mask = vma_kernel_pagesize(dst_vma) - 1;
781 if ((start & page_mask) || (len & page_mask))
785 err = uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp);
787 /* Return 0 on success, <0 on failures */
792 mmap_read_unlock(dst_mm);