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>
21 static __always_inline
22 struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm,
23 unsigned long dst_start,
27 * Make sure that the dst range is both valid and fully within a
28 * single existing vma.
30 struct vm_area_struct *dst_vma;
32 dst_vma = find_vma(dst_mm, dst_start);
36 if (dst_start < dst_vma->vm_start ||
37 dst_start + len > dst_vma->vm_end)
41 * Check the vma is registered in uffd, this is required to
42 * enforce the VM_MAYWRITE check done at uffd registration
45 if (!dst_vma->vm_userfaultfd_ctx.ctx)
52 * Install PTEs, to map dst_addr (within dst_vma) to page.
54 * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
55 * and anon, and for both shared and private VMAs.
57 int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
58 struct vm_area_struct *dst_vma,
59 unsigned long dst_addr, struct page *page,
60 bool newly_allocated, bool wp_copy)
63 pte_t _dst_pte, *dst_pte;
64 bool writable = dst_vma->vm_flags & VM_WRITE;
65 bool vm_shared = dst_vma->vm_flags & VM_SHARED;
66 bool page_in_cache = page->mapping;
69 pgoff_t offset, max_off;
71 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
72 _dst_pte = pte_mkdirty(_dst_pte);
73 if (page_in_cache && !vm_shared)
77 * Always mark a PTE as write-protected when needed, regardless of
78 * VM_WRITE, which the user might change.
81 _dst_pte = pte_mkuffd_wp(_dst_pte);
83 _dst_pte = pte_mkwrite(_dst_pte);
85 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
87 if (vma_is_shmem(dst_vma)) {
88 /* serialize against truncate with the page table lock */
89 inode = dst_vma->vm_file->f_inode;
90 offset = linear_page_index(dst_vma, dst_addr);
91 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
93 if (unlikely(offset >= max_off))
98 if (!pte_none(*dst_pte))
102 /* Usually, cache pages are already added to LRU */
105 page_add_file_rmap(page, dst_vma, false);
107 page_add_new_anon_rmap(page, dst_vma, dst_addr, false);
108 lru_cache_add_inactive_or_unevictable(page, 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 mcopy_atomic_pte(struct mm_struct *dst_mm,
129 struct vm_area_struct *dst_vma,
130 unsigned long dst_addr,
131 unsigned long src_addr,
141 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
145 page_kaddr = kmap_atomic(page);
146 ret = copy_from_user(page_kaddr,
147 (const void __user *) src_addr,
149 kunmap_atomic(page_kaddr);
151 /* fallback to copy_from_user outside mmap_lock */
155 /* don't free the page */
159 flush_dcache_page(page);
166 * The memory barrier inside __SetPageUptodate makes sure that
167 * preceding stores to the page contents become visible before
168 * the set_pte_at() write.
170 __SetPageUptodate(page);
173 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL))
176 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
177 page, true, wp_copy);
187 static int mfill_zeropage_pte(struct mm_struct *dst_mm,
189 struct vm_area_struct *dst_vma,
190 unsigned long dst_addr)
192 pte_t _dst_pte, *dst_pte;
195 pgoff_t offset, max_off;
198 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
199 dst_vma->vm_page_prot));
200 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
201 if (dst_vma->vm_file) {
202 /* the shmem MAP_PRIVATE case requires checking the i_size */
203 inode = dst_vma->vm_file->f_inode;
204 offset = linear_page_index(dst_vma, dst_addr);
205 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
207 if (unlikely(offset >= max_off))
211 if (!pte_none(*dst_pte))
213 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
214 /* No need to invalidate - it was non-present before */
215 update_mmu_cache(dst_vma, dst_addr, dst_pte);
218 pte_unmap_unlock(dst_pte, ptl);
222 /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
223 static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
225 struct vm_area_struct *dst_vma,
226 unsigned long dst_addr,
229 struct inode *inode = file_inode(dst_vma->vm_file);
230 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
234 ret = shmem_getpage(inode, pgoff, &page, SGP_READ);
242 if (PageHWPoison(page)) {
247 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
248 page, false, wp_copy);
262 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
268 pgd = pgd_offset(mm, address);
269 p4d = p4d_alloc(mm, pgd, address);
272 pud = pud_alloc(mm, p4d, address);
276 * Note that we didn't run this because the pmd was
277 * missing, the *pmd may be already established and in
278 * turn it may also be a trans_huge_pmd.
280 return pmd_alloc(mm, pud, address);
283 #ifdef CONFIG_HUGETLB_PAGE
285 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
286 * called with mmap_lock held, it will release mmap_lock before returning.
288 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
289 struct vm_area_struct *dst_vma,
290 unsigned long dst_start,
291 unsigned long src_start,
293 enum mcopy_atomic_mode mode)
295 int vm_shared = dst_vma->vm_flags & VM_SHARED;
298 unsigned long src_addr, dst_addr;
301 unsigned long vma_hpagesize;
304 struct address_space *mapping;
307 * There is no default zero huge page for all huge page sizes as
308 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
309 * by THP. Since we can not reliably insert a zero page, this
310 * feature is not supported.
312 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
313 mmap_read_unlock(dst_mm);
317 src_addr = src_start;
318 dst_addr = dst_start;
321 vma_hpagesize = vma_kernel_pagesize(dst_vma);
324 * Validate alignment based on huge page size
327 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
332 * On routine entry dst_vma is set. If we had to drop mmap_lock and
333 * retry, dst_vma will be set to NULL and we must lookup again.
337 dst_vma = find_dst_vma(dst_mm, dst_start, len);
338 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
342 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
345 vm_shared = dst_vma->vm_flags & VM_SHARED;
349 * If not shared, ensure the dst_vma has a anon_vma.
353 if (unlikely(anon_vma_prepare(dst_vma)))
357 while (src_addr < src_start + len) {
358 BUG_ON(dst_addr >= dst_start + len);
361 * Serialize via i_mmap_rwsem and hugetlb_fault_mutex.
362 * i_mmap_rwsem ensures the dst_pte remains valid even
363 * in the case of shared pmds. fault mutex prevents
364 * races with other faulting threads.
366 mapping = dst_vma->vm_file->f_mapping;
367 i_mmap_lock_read(mapping);
368 idx = linear_page_index(dst_vma, dst_addr);
369 hash = hugetlb_fault_mutex_hash(mapping, idx);
370 mutex_lock(&hugetlb_fault_mutex_table[hash]);
373 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
375 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
376 i_mmap_unlock_read(mapping);
380 if (mode != MCOPY_ATOMIC_CONTINUE &&
381 !huge_pte_none(huge_ptep_get(dst_pte))) {
383 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
384 i_mmap_unlock_read(mapping);
388 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
389 dst_addr, src_addr, mode, &page);
391 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
392 i_mmap_unlock_read(mapping);
396 if (unlikely(err == -ENOENT)) {
397 mmap_read_unlock(dst_mm);
400 err = copy_huge_page_from_user(page,
401 (const void __user *)src_addr,
402 vma_hpagesize / PAGE_SIZE,
408 mmap_read_lock(dst_mm);
416 dst_addr += vma_hpagesize;
417 src_addr += vma_hpagesize;
418 copied += vma_hpagesize;
420 if (fatal_signal_pending(current))
428 mmap_read_unlock(dst_mm);
434 BUG_ON(!copied && !err);
435 return copied ? copied : err;
437 #else /* !CONFIG_HUGETLB_PAGE */
438 /* fail at build time if gcc attempts to use this */
439 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
440 struct vm_area_struct *dst_vma,
441 unsigned long dst_start,
442 unsigned long src_start,
444 enum mcopy_atomic_mode mode);
445 #endif /* CONFIG_HUGETLB_PAGE */
447 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
449 struct vm_area_struct *dst_vma,
450 unsigned long dst_addr,
451 unsigned long src_addr,
453 enum mcopy_atomic_mode mode,
458 if (mode == MCOPY_ATOMIC_CONTINUE) {
459 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
464 * The normal page fault path for a shmem will invoke the
465 * fault, fill the hole in the file and COW it right away. The
466 * result generates plain anonymous memory. So when we are
467 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
468 * generate anonymous memory directly without actually filling
469 * the hole. For the MAP_PRIVATE case the robustness check
470 * only happens in the pagetable (to verify it's still none)
471 * and not in the radix tree.
473 if (!(dst_vma->vm_flags & VM_SHARED)) {
474 if (mode == MCOPY_ATOMIC_NORMAL)
475 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
476 dst_addr, src_addr, page,
479 err = mfill_zeropage_pte(dst_mm, dst_pmd,
482 VM_WARN_ON_ONCE(wp_copy);
483 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
485 mode != MCOPY_ATOMIC_NORMAL,
492 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
493 unsigned long dst_start,
494 unsigned long src_start,
496 enum mcopy_atomic_mode mcopy_mode,
497 atomic_t *mmap_changing,
500 struct vm_area_struct *dst_vma;
503 unsigned long src_addr, dst_addr;
509 * Sanitize the command parameters:
511 BUG_ON(dst_start & ~PAGE_MASK);
512 BUG_ON(len & ~PAGE_MASK);
514 /* Does the address range wrap, or is the span zero-sized? */
515 BUG_ON(src_start + len <= src_start);
516 BUG_ON(dst_start + len <= dst_start);
518 src_addr = src_start;
519 dst_addr = dst_start;
523 mmap_read_lock(dst_mm);
526 * If memory mappings are changing because of non-cooperative
527 * operation (e.g. mremap) running in parallel, bail out and
528 * request the user to retry later
531 if (mmap_changing && atomic_read(mmap_changing))
535 * Make sure the vma is not shared, that the dst range is
536 * both valid and fully within a single existing vma.
539 dst_vma = find_dst_vma(dst_mm, dst_start, len);
545 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
546 * it will overwrite vm_ops, so vma_is_anonymous must return false.
548 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
549 dst_vma->vm_flags & VM_SHARED))
553 * validate 'mode' now that we know the dst_vma: don't allow
554 * a wrprotect copy if the userfaultfd didn't register as WP.
556 wp_copy = mode & UFFDIO_COPY_MODE_WP;
557 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
561 * If this is a HUGETLB vma, pass off to appropriate routine
563 if (is_vm_hugetlb_page(dst_vma))
564 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
565 src_start, len, mcopy_mode);
567 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
569 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
573 * Ensure the dst_vma has a anon_vma or this page
574 * would get a NULL anon_vma when moved in the
578 if (!(dst_vma->vm_flags & VM_SHARED) &&
579 unlikely(anon_vma_prepare(dst_vma)))
582 while (src_addr < src_start + len) {
585 BUG_ON(dst_addr >= dst_start + len);
587 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
588 if (unlikely(!dst_pmd)) {
593 dst_pmdval = pmd_read_atomic(dst_pmd);
595 * If the dst_pmd is mapped as THP don't
596 * override it and just be strict.
598 if (unlikely(pmd_trans_huge(dst_pmdval))) {
602 if (unlikely(pmd_none(dst_pmdval)) &&
603 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
607 /* If an huge pmd materialized from under us fail */
608 if (unlikely(pmd_trans_huge(*dst_pmd))) {
613 BUG_ON(pmd_none(*dst_pmd));
614 BUG_ON(pmd_trans_huge(*dst_pmd));
616 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
617 src_addr, &page, mcopy_mode, wp_copy);
620 if (unlikely(err == -ENOENT)) {
623 mmap_read_unlock(dst_mm);
626 page_kaddr = kmap(page);
627 err = copy_from_user(page_kaddr,
628 (const void __user *) src_addr,
635 flush_dcache_page(page);
641 dst_addr += PAGE_SIZE;
642 src_addr += PAGE_SIZE;
645 if (fatal_signal_pending(current))
653 mmap_read_unlock(dst_mm);
659 BUG_ON(!copied && !err);
660 return copied ? copied : err;
663 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
664 unsigned long src_start, unsigned long len,
665 atomic_t *mmap_changing, __u64 mode)
667 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
668 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
671 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
672 unsigned long len, atomic_t *mmap_changing)
674 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
678 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
679 unsigned long len, atomic_t *mmap_changing)
681 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
685 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
686 unsigned long len, bool enable_wp,
687 atomic_t *mmap_changing)
689 struct vm_area_struct *dst_vma;
694 * Sanitize the command parameters:
696 BUG_ON(start & ~PAGE_MASK);
697 BUG_ON(len & ~PAGE_MASK);
699 /* Does the address range wrap, or is the span zero-sized? */
700 BUG_ON(start + len <= start);
702 mmap_read_lock(dst_mm);
705 * If memory mappings are changing because of non-cooperative
706 * operation (e.g. mremap) running in parallel, bail out and
707 * request the user to retry later
710 if (mmap_changing && atomic_read(mmap_changing))
714 dst_vma = find_dst_vma(dst_mm, start, len);
716 * Make sure the vma is not shared, that the dst range is
717 * both valid and fully within a single existing vma.
719 if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
721 if (!userfaultfd_wp(dst_vma))
723 if (!vma_is_anonymous(dst_vma))
727 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
729 newprot = vm_get_page_prot(dst_vma->vm_flags);
731 change_protection(dst_vma, start, start + len, newprot,
732 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
736 mmap_read_unlock(dst_mm);