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 int mcopy_atomic_pte(struct mm_struct *dst_mm,
23 struct vm_area_struct *dst_vma,
24 unsigned long dst_addr,
25 unsigned long src_addr,
28 struct mem_cgroup *memcg;
29 pte_t _dst_pte, *dst_pte;
34 pgoff_t offset, max_off;
39 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
43 page_kaddr = kmap_atomic(page);
44 ret = copy_from_user(page_kaddr,
45 (const void __user *) src_addr,
47 kunmap_atomic(page_kaddr);
49 /* fallback to copy_from_user outside mmap_sem */
53 /* don't free the page */
62 * The memory barrier inside __SetPageUptodate makes sure that
63 * preceeding stores to the page contents become visible before
64 * the set_pte_at() write.
66 __SetPageUptodate(page);
69 if (mem_cgroup_try_charge(page, dst_mm, GFP_KERNEL, &memcg, false))
72 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
73 if (dst_vma->vm_flags & VM_WRITE)
74 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
76 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
77 if (dst_vma->vm_file) {
78 /* the shmem MAP_PRIVATE case requires checking the i_size */
79 inode = dst_vma->vm_file->f_inode;
80 offset = linear_page_index(dst_vma, dst_addr);
81 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
83 if (unlikely(offset >= max_off))
84 goto out_release_uncharge_unlock;
87 if (!pte_none(*dst_pte))
88 goto out_release_uncharge_unlock;
90 inc_mm_counter(dst_mm, MM_ANONPAGES);
91 page_add_new_anon_rmap(page, dst_vma, dst_addr, false);
92 mem_cgroup_commit_charge(page, memcg, false, false);
93 lru_cache_add_active_or_unevictable(page, dst_vma);
95 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
97 /* No need to invalidate - it was non-present before */
98 update_mmu_cache(dst_vma, dst_addr, dst_pte);
100 pte_unmap_unlock(dst_pte, ptl);
104 out_release_uncharge_unlock:
105 pte_unmap_unlock(dst_pte, ptl);
106 mem_cgroup_cancel_charge(page, memcg, false);
112 static int mfill_zeropage_pte(struct mm_struct *dst_mm,
114 struct vm_area_struct *dst_vma,
115 unsigned long dst_addr)
117 pte_t _dst_pte, *dst_pte;
120 pgoff_t offset, max_off;
123 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
124 dst_vma->vm_page_prot));
125 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
126 if (dst_vma->vm_file) {
127 /* the shmem MAP_PRIVATE case requires checking the i_size */
128 inode = dst_vma->vm_file->f_inode;
129 offset = linear_page_index(dst_vma, dst_addr);
130 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
132 if (unlikely(offset >= max_off))
136 if (!pte_none(*dst_pte))
138 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
139 /* No need to invalidate - it was non-present before */
140 update_mmu_cache(dst_vma, dst_addr, dst_pte);
143 pte_unmap_unlock(dst_pte, ptl);
147 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
153 pgd = pgd_offset(mm, address);
154 p4d = p4d_alloc(mm, pgd, address);
157 pud = pud_alloc(mm, p4d, address);
161 * Note that we didn't run this because the pmd was
162 * missing, the *pmd may be already established and in
163 * turn it may also be a trans_huge_pmd.
165 return pmd_alloc(mm, pud, address);
168 #ifdef CONFIG_HUGETLB_PAGE
170 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
171 * called with mmap_sem held, it will release mmap_sem before returning.
173 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
174 struct vm_area_struct *dst_vma,
175 unsigned long dst_start,
176 unsigned long src_start,
180 int vm_alloc_shared = dst_vma->vm_flags & VM_SHARED;
181 int vm_shared = dst_vma->vm_flags & VM_SHARED;
184 unsigned long src_addr, dst_addr;
188 unsigned long vma_hpagesize;
191 struct address_space *mapping;
194 * There is no default zero huge page for all huge page sizes as
195 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
196 * by THP. Since we can not reliably insert a zero page, this
197 * feature is not supported.
200 up_read(&dst_mm->mmap_sem);
204 src_addr = src_start;
205 dst_addr = dst_start;
208 vma_hpagesize = vma_kernel_pagesize(dst_vma);
211 * Validate alignment based on huge page size
214 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
219 * On routine entry dst_vma is set. If we had to drop mmap_sem and
220 * retry, dst_vma will be set to NULL and we must lookup again.
224 dst_vma = find_vma(dst_mm, dst_start);
225 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
228 * Check the vma is registered in uffd, this is
229 * required to enforce the VM_MAYWRITE check done at
230 * uffd registration time.
232 if (!dst_vma->vm_userfaultfd_ctx.ctx)
235 if (dst_start < dst_vma->vm_start ||
236 dst_start + len > dst_vma->vm_end)
240 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
243 vm_shared = dst_vma->vm_flags & VM_SHARED;
246 if (WARN_ON(dst_addr & (vma_hpagesize - 1) ||
247 (len - copied) & (vma_hpagesize - 1)))
251 * If not shared, ensure the dst_vma has a anon_vma.
255 if (unlikely(anon_vma_prepare(dst_vma)))
259 h = hstate_vma(dst_vma);
261 while (src_addr < src_start + len) {
264 BUG_ON(dst_addr >= dst_start + len);
265 VM_BUG_ON(dst_addr & ~huge_page_mask(h));
268 * Serialize via hugetlb_fault_mutex
270 idx = linear_page_index(dst_vma, dst_addr);
271 mapping = dst_vma->vm_file->f_mapping;
272 hash = hugetlb_fault_mutex_hash(h, mapping, idx, dst_addr);
273 mutex_lock(&hugetlb_fault_mutex_table[hash]);
276 dst_pte = huge_pte_alloc(dst_mm, dst_addr, huge_page_size(h));
278 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
283 dst_pteval = huge_ptep_get(dst_pte);
284 if (!huge_pte_none(dst_pteval)) {
285 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
289 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
290 dst_addr, src_addr, &page);
292 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
293 vm_alloc_shared = vm_shared;
297 if (unlikely(err == -ENOENT)) {
298 up_read(&dst_mm->mmap_sem);
301 err = copy_huge_page_from_user(page,
302 (const void __user *)src_addr,
303 pages_per_huge_page(h), true);
308 down_read(&dst_mm->mmap_sem);
316 dst_addr += vma_hpagesize;
317 src_addr += vma_hpagesize;
318 copied += vma_hpagesize;
320 if (fatal_signal_pending(current))
328 up_read(&dst_mm->mmap_sem);
332 * We encountered an error and are about to free a newly
333 * allocated huge page.
335 * Reservation handling is very subtle, and is different for
336 * private and shared mappings. See the routine
337 * restore_reserve_on_error for details. Unfortunately, we
338 * can not call restore_reserve_on_error now as it would
339 * require holding mmap_sem.
341 * If a reservation for the page existed in the reservation
342 * map of a private mapping, the map was modified to indicate
343 * the reservation was consumed when the page was allocated.
344 * We clear the PagePrivate flag now so that the global
345 * reserve count will not be incremented in free_huge_page.
346 * The reservation map will still indicate the reservation
347 * was consumed and possibly prevent later page allocation.
348 * This is better than leaking a global reservation. If no
349 * reservation existed, it is still safe to clear PagePrivate
350 * as no adjustments to reservation counts were made during
353 * The reservation map for shared mappings indicates which
354 * pages have reservations. When a huge page is allocated
355 * for an address with a reservation, no change is made to
356 * the reserve map. In this case PagePrivate will be set
357 * to indicate that the global reservation count should be
358 * incremented when the page is freed. This is the desired
359 * behavior. However, when a huge page is allocated for an
360 * address without a reservation a reservation entry is added
361 * to the reservation map, and PagePrivate will not be set.
362 * When the page is freed, the global reserve count will NOT
363 * be incremented and it will appear as though we have leaked
364 * reserved page. In this case, set PagePrivate so that the
365 * global reserve count will be incremented to match the
366 * reservation map entry which was created.
368 * Note that vm_alloc_shared is based on the flags of the vma
369 * for which the page was originally allocated. dst_vma could
370 * be different or NULL on error.
373 SetPagePrivate(page);
375 ClearPagePrivate(page);
380 BUG_ON(!copied && !err);
381 return copied ? copied : err;
383 #else /* !CONFIG_HUGETLB_PAGE */
384 /* fail at build time if gcc attempts to use this */
385 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
386 struct vm_area_struct *dst_vma,
387 unsigned long dst_start,
388 unsigned long src_start,
391 #endif /* CONFIG_HUGETLB_PAGE */
393 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
395 struct vm_area_struct *dst_vma,
396 unsigned long dst_addr,
397 unsigned long src_addr,
404 * The normal page fault path for a shmem will invoke the
405 * fault, fill the hole in the file and COW it right away. The
406 * result generates plain anonymous memory. So when we are
407 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
408 * generate anonymous memory directly without actually filling
409 * the hole. For the MAP_PRIVATE case the robustness check
410 * only happens in the pagetable (to verify it's still none)
411 * and not in the radix tree.
413 if (!(dst_vma->vm_flags & VM_SHARED)) {
415 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
416 dst_addr, src_addr, page);
418 err = mfill_zeropage_pte(dst_mm, dst_pmd,
422 err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd,
426 err = shmem_mfill_zeropage_pte(dst_mm, dst_pmd,
433 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
434 unsigned long dst_start,
435 unsigned long src_start,
440 struct vm_area_struct *dst_vma;
443 unsigned long src_addr, dst_addr;
448 * Sanitize the command parameters:
450 BUG_ON(dst_start & ~PAGE_MASK);
451 BUG_ON(len & ~PAGE_MASK);
453 /* Does the address range wrap, or is the span zero-sized? */
454 BUG_ON(src_start + len <= src_start);
455 BUG_ON(dst_start + len <= dst_start);
457 src_addr = src_start;
458 dst_addr = dst_start;
462 down_read(&dst_mm->mmap_sem);
465 * If memory mappings are changing because of non-cooperative
466 * operation (e.g. mremap) running in parallel, bail out and
467 * request the user to retry later
470 if (mmap_changing && READ_ONCE(*mmap_changing))
474 * Make sure the vma is not shared, that the dst range is
475 * both valid and fully within a single existing vma.
478 dst_vma = find_vma(dst_mm, dst_start);
482 * Check the vma is registered in uffd, this is required to
483 * enforce the VM_MAYWRITE check done at uffd registration
486 if (!dst_vma->vm_userfaultfd_ctx.ctx)
489 if (dst_start < dst_vma->vm_start ||
490 dst_start + len > dst_vma->vm_end)
495 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
496 * it will overwrite vm_ops, so vma_is_anonymous must return false.
498 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
499 dst_vma->vm_flags & VM_SHARED))
503 * If this is a HUGETLB vma, pass off to appropriate routine
505 if (is_vm_hugetlb_page(dst_vma))
506 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
507 src_start, len, zeropage);
509 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
513 * Ensure the dst_vma has a anon_vma or this page
514 * would get a NULL anon_vma when moved in the
518 if (!(dst_vma->vm_flags & VM_SHARED) &&
519 unlikely(anon_vma_prepare(dst_vma)))
522 while (src_addr < src_start + len) {
525 BUG_ON(dst_addr >= dst_start + len);
527 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
528 if (unlikely(!dst_pmd)) {
533 dst_pmdval = pmd_read_atomic(dst_pmd);
535 * If the dst_pmd is mapped as THP don't
536 * override it and just be strict.
538 if (unlikely(pmd_trans_huge(dst_pmdval))) {
542 if (unlikely(pmd_none(dst_pmdval)) &&
543 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
547 /* If an huge pmd materialized from under us fail */
548 if (unlikely(pmd_trans_huge(*dst_pmd))) {
553 BUG_ON(pmd_none(*dst_pmd));
554 BUG_ON(pmd_trans_huge(*dst_pmd));
556 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
557 src_addr, &page, zeropage);
560 if (unlikely(err == -ENOENT)) {
563 up_read(&dst_mm->mmap_sem);
566 page_kaddr = kmap(page);
567 err = copy_from_user(page_kaddr,
568 (const void __user *) src_addr,
580 dst_addr += PAGE_SIZE;
581 src_addr += PAGE_SIZE;
584 if (fatal_signal_pending(current))
592 up_read(&dst_mm->mmap_sem);
598 BUG_ON(!copied && !err);
599 return copied ? copied : err;
602 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
603 unsigned long src_start, unsigned long len,
606 return __mcopy_atomic(dst_mm, dst_start, src_start, len, false,
610 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
611 unsigned long len, bool *mmap_changing)
613 return __mcopy_atomic(dst_mm, start, 0, len, true, mmap_changing);