18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * mm/userfaultfd.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Red Hat, Inc. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/mm.h> 98c2ecf20Sopenharmony_ci#include <linux/sched/signal.h> 108c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 118c2ecf20Sopenharmony_ci#include <linux/rmap.h> 128c2ecf20Sopenharmony_ci#include <linux/swap.h> 138c2ecf20Sopenharmony_ci#include <linux/swapops.h> 148c2ecf20Sopenharmony_ci#include <linux/userfaultfd_k.h> 158c2ecf20Sopenharmony_ci#include <linux/mmu_notifier.h> 168c2ecf20Sopenharmony_ci#include <linux/hugetlb.h> 178c2ecf20Sopenharmony_ci#include <linux/shmem_fs.h> 188c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 198c2ecf20Sopenharmony_ci#include "internal.h" 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic __always_inline 228c2ecf20Sopenharmony_cistruct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, 238c2ecf20Sopenharmony_ci unsigned long dst_start, 248c2ecf20Sopenharmony_ci unsigned long len) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci /* 278c2ecf20Sopenharmony_ci * Make sure that the dst range is both valid and fully within a 288c2ecf20Sopenharmony_ci * single existing vma. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci dst_vma = find_vma(dst_mm, dst_start); 338c2ecf20Sopenharmony_ci if (!dst_vma) 348c2ecf20Sopenharmony_ci return NULL; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci if (dst_start < dst_vma->vm_start || 378c2ecf20Sopenharmony_ci dst_start + len > dst_vma->vm_end) 388c2ecf20Sopenharmony_ci return NULL; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci /* 418c2ecf20Sopenharmony_ci * Check the vma is registered in uffd, this is required to 428c2ecf20Sopenharmony_ci * enforce the VM_MAYWRITE check done at uffd registration 438c2ecf20Sopenharmony_ci * time. 448c2ecf20Sopenharmony_ci */ 458c2ecf20Sopenharmony_ci if (!dst_vma->vm_userfaultfd_ctx.ctx) 468c2ecf20Sopenharmony_ci return NULL; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci return dst_vma; 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic int mcopy_atomic_pte(struct mm_struct *dst_mm, 528c2ecf20Sopenharmony_ci pmd_t *dst_pmd, 538c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma, 548c2ecf20Sopenharmony_ci unsigned long dst_addr, 558c2ecf20Sopenharmony_ci unsigned long src_addr, 568c2ecf20Sopenharmony_ci struct page **pagep, 578c2ecf20Sopenharmony_ci bool wp_copy) 588c2ecf20Sopenharmony_ci{ 598c2ecf20Sopenharmony_ci pte_t _dst_pte, *dst_pte; 608c2ecf20Sopenharmony_ci spinlock_t *ptl; 618c2ecf20Sopenharmony_ci void *page_kaddr; 628c2ecf20Sopenharmony_ci int ret; 638c2ecf20Sopenharmony_ci struct page *page; 648c2ecf20Sopenharmony_ci pgoff_t offset, max_off; 658c2ecf20Sopenharmony_ci struct inode *inode; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (!*pagep) { 688c2ecf20Sopenharmony_ci ret = -ENOMEM; 698c2ecf20Sopenharmony_ci page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr); 708c2ecf20Sopenharmony_ci if (!page) 718c2ecf20Sopenharmony_ci goto out; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci page_kaddr = kmap_atomic(page); 748c2ecf20Sopenharmony_ci ret = copy_from_user(page_kaddr, 758c2ecf20Sopenharmony_ci (const void __user *) src_addr, 768c2ecf20Sopenharmony_ci PAGE_SIZE); 778c2ecf20Sopenharmony_ci kunmap_atomic(page_kaddr); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci /* fallback to copy_from_user outside mmap_lock */ 808c2ecf20Sopenharmony_ci if (unlikely(ret)) { 818c2ecf20Sopenharmony_ci ret = -ENOENT; 828c2ecf20Sopenharmony_ci *pagep = page; 838c2ecf20Sopenharmony_ci /* don't free the page */ 848c2ecf20Sopenharmony_ci goto out; 858c2ecf20Sopenharmony_ci } 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci flush_dcache_page(page); 888c2ecf20Sopenharmony_ci } else { 898c2ecf20Sopenharmony_ci page = *pagep; 908c2ecf20Sopenharmony_ci *pagep = NULL; 918c2ecf20Sopenharmony_ci } 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci /* 948c2ecf20Sopenharmony_ci * The memory barrier inside __SetPageUptodate makes sure that 958c2ecf20Sopenharmony_ci * preceding stores to the page contents become visible before 968c2ecf20Sopenharmony_ci * the set_pte_at() write. 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_ci __SetPageUptodate(page); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci ret = -ENOMEM; 1018c2ecf20Sopenharmony_ci if (mem_cgroup_charge(page, dst_mm, GFP_KERNEL)) 1028c2ecf20Sopenharmony_ci goto out_release; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci _dst_pte = pte_mkdirty(mk_pte(page, dst_vma->vm_page_prot)); 1058c2ecf20Sopenharmony_ci if (dst_vma->vm_flags & VM_WRITE) { 1068c2ecf20Sopenharmony_ci if (wp_copy) 1078c2ecf20Sopenharmony_ci _dst_pte = pte_mkuffd_wp(_dst_pte); 1088c2ecf20Sopenharmony_ci else 1098c2ecf20Sopenharmony_ci _dst_pte = pte_mkwrite(_dst_pte); 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 1138c2ecf20Sopenharmony_ci if (dst_vma->vm_file) { 1148c2ecf20Sopenharmony_ci /* the shmem MAP_PRIVATE case requires checking the i_size */ 1158c2ecf20Sopenharmony_ci inode = dst_vma->vm_file->f_inode; 1168c2ecf20Sopenharmony_ci offset = linear_page_index(dst_vma, dst_addr); 1178c2ecf20Sopenharmony_ci max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1188c2ecf20Sopenharmony_ci ret = -EFAULT; 1198c2ecf20Sopenharmony_ci if (unlikely(offset >= max_off)) 1208c2ecf20Sopenharmony_ci goto out_release_uncharge_unlock; 1218c2ecf20Sopenharmony_ci } 1228c2ecf20Sopenharmony_ci ret = -EEXIST; 1238c2ecf20Sopenharmony_ci if (!pte_none(*dst_pte)) 1248c2ecf20Sopenharmony_ci goto out_release_uncharge_unlock; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci inc_mm_counter(dst_mm, MM_ANONPAGES); 1278c2ecf20Sopenharmony_ci page_add_new_anon_rmap(page, dst_vma, dst_addr, false); 1288c2ecf20Sopenharmony_ci lru_cache_add_inactive_or_unevictable(page, dst_vma); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci /* No need to invalidate - it was non-present before */ 1338c2ecf20Sopenharmony_ci update_mmu_cache(dst_vma, dst_addr, dst_pte); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci pte_unmap_unlock(dst_pte, ptl); 1368c2ecf20Sopenharmony_ci ret = 0; 1378c2ecf20Sopenharmony_ciout: 1388c2ecf20Sopenharmony_ci return ret; 1398c2ecf20Sopenharmony_ciout_release_uncharge_unlock: 1408c2ecf20Sopenharmony_ci pte_unmap_unlock(dst_pte, ptl); 1418c2ecf20Sopenharmony_ciout_release: 1428c2ecf20Sopenharmony_ci put_page(page); 1438c2ecf20Sopenharmony_ci goto out; 1448c2ecf20Sopenharmony_ci} 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic int mfill_zeropage_pte(struct mm_struct *dst_mm, 1478c2ecf20Sopenharmony_ci pmd_t *dst_pmd, 1488c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma, 1498c2ecf20Sopenharmony_ci unsigned long dst_addr) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci pte_t _dst_pte, *dst_pte; 1528c2ecf20Sopenharmony_ci spinlock_t *ptl; 1538c2ecf20Sopenharmony_ci int ret; 1548c2ecf20Sopenharmony_ci pgoff_t offset, max_off; 1558c2ecf20Sopenharmony_ci struct inode *inode; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), 1588c2ecf20Sopenharmony_ci dst_vma->vm_page_prot)); 1598c2ecf20Sopenharmony_ci dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 1608c2ecf20Sopenharmony_ci if (dst_vma->vm_file) { 1618c2ecf20Sopenharmony_ci /* the shmem MAP_PRIVATE case requires checking the i_size */ 1628c2ecf20Sopenharmony_ci inode = dst_vma->vm_file->f_inode; 1638c2ecf20Sopenharmony_ci offset = linear_page_index(dst_vma, dst_addr); 1648c2ecf20Sopenharmony_ci max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1658c2ecf20Sopenharmony_ci ret = -EFAULT; 1668c2ecf20Sopenharmony_ci if (unlikely(offset >= max_off)) 1678c2ecf20Sopenharmony_ci goto out_unlock; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci ret = -EEXIST; 1708c2ecf20Sopenharmony_ci if (!pte_none(*dst_pte)) 1718c2ecf20Sopenharmony_ci goto out_unlock; 1728c2ecf20Sopenharmony_ci set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 1738c2ecf20Sopenharmony_ci /* No need to invalidate - it was non-present before */ 1748c2ecf20Sopenharmony_ci update_mmu_cache(dst_vma, dst_addr, dst_pte); 1758c2ecf20Sopenharmony_ci ret = 0; 1768c2ecf20Sopenharmony_ciout_unlock: 1778c2ecf20Sopenharmony_ci pte_unmap_unlock(dst_pte, ptl); 1788c2ecf20Sopenharmony_ci return ret; 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistatic pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci pgd_t *pgd; 1848c2ecf20Sopenharmony_ci p4d_t *p4d; 1858c2ecf20Sopenharmony_ci pud_t *pud; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci pgd = pgd_offset(mm, address); 1888c2ecf20Sopenharmony_ci p4d = p4d_alloc(mm, pgd, address); 1898c2ecf20Sopenharmony_ci if (!p4d) 1908c2ecf20Sopenharmony_ci return NULL; 1918c2ecf20Sopenharmony_ci pud = pud_alloc(mm, p4d, address); 1928c2ecf20Sopenharmony_ci if (!pud) 1938c2ecf20Sopenharmony_ci return NULL; 1948c2ecf20Sopenharmony_ci /* 1958c2ecf20Sopenharmony_ci * Note that we didn't run this because the pmd was 1968c2ecf20Sopenharmony_ci * missing, the *pmd may be already established and in 1978c2ecf20Sopenharmony_ci * turn it may also be a trans_huge_pmd. 1988c2ecf20Sopenharmony_ci */ 1998c2ecf20Sopenharmony_ci return pmd_alloc(mm, pud, address); 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE 2038c2ecf20Sopenharmony_ci/* 2048c2ecf20Sopenharmony_ci * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is 2058c2ecf20Sopenharmony_ci * called with mmap_lock held, it will release mmap_lock before returning. 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_cistatic __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, 2088c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma, 2098c2ecf20Sopenharmony_ci unsigned long dst_start, 2108c2ecf20Sopenharmony_ci unsigned long src_start, 2118c2ecf20Sopenharmony_ci unsigned long len, 2128c2ecf20Sopenharmony_ci bool zeropage) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci int vm_alloc_shared = dst_vma->vm_flags & VM_SHARED; 2158c2ecf20Sopenharmony_ci int vm_shared = dst_vma->vm_flags & VM_SHARED; 2168c2ecf20Sopenharmony_ci ssize_t err; 2178c2ecf20Sopenharmony_ci pte_t *dst_pte; 2188c2ecf20Sopenharmony_ci unsigned long src_addr, dst_addr; 2198c2ecf20Sopenharmony_ci long copied; 2208c2ecf20Sopenharmony_ci struct page *page; 2218c2ecf20Sopenharmony_ci unsigned long vma_hpagesize; 2228c2ecf20Sopenharmony_ci pgoff_t idx; 2238c2ecf20Sopenharmony_ci u32 hash; 2248c2ecf20Sopenharmony_ci struct address_space *mapping; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* 2278c2ecf20Sopenharmony_ci * There is no default zero huge page for all huge page sizes as 2288c2ecf20Sopenharmony_ci * supported by hugetlb. A PMD_SIZE huge pages may exist as used 2298c2ecf20Sopenharmony_ci * by THP. Since we can not reliably insert a zero page, this 2308c2ecf20Sopenharmony_ci * feature is not supported. 2318c2ecf20Sopenharmony_ci */ 2328c2ecf20Sopenharmony_ci if (zeropage) { 2338c2ecf20Sopenharmony_ci mmap_read_unlock(dst_mm); 2348c2ecf20Sopenharmony_ci return -EINVAL; 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci src_addr = src_start; 2388c2ecf20Sopenharmony_ci dst_addr = dst_start; 2398c2ecf20Sopenharmony_ci copied = 0; 2408c2ecf20Sopenharmony_ci page = NULL; 2418c2ecf20Sopenharmony_ci vma_hpagesize = vma_kernel_pagesize(dst_vma); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci /* 2448c2ecf20Sopenharmony_ci * Validate alignment based on huge page size 2458c2ecf20Sopenharmony_ci */ 2468c2ecf20Sopenharmony_ci err = -EINVAL; 2478c2ecf20Sopenharmony_ci if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1)) 2488c2ecf20Sopenharmony_ci goto out_unlock; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ciretry: 2518c2ecf20Sopenharmony_ci /* 2528c2ecf20Sopenharmony_ci * On routine entry dst_vma is set. If we had to drop mmap_lock and 2538c2ecf20Sopenharmony_ci * retry, dst_vma will be set to NULL and we must lookup again. 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_ci if (!dst_vma) { 2568c2ecf20Sopenharmony_ci err = -ENOENT; 2578c2ecf20Sopenharmony_ci dst_vma = find_dst_vma(dst_mm, dst_start, len); 2588c2ecf20Sopenharmony_ci if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) 2598c2ecf20Sopenharmony_ci goto out_unlock; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci err = -EINVAL; 2628c2ecf20Sopenharmony_ci if (vma_hpagesize != vma_kernel_pagesize(dst_vma)) 2638c2ecf20Sopenharmony_ci goto out_unlock; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci vm_shared = dst_vma->vm_flags & VM_SHARED; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci /* 2698c2ecf20Sopenharmony_ci * If not shared, ensure the dst_vma has a anon_vma. 2708c2ecf20Sopenharmony_ci */ 2718c2ecf20Sopenharmony_ci err = -ENOMEM; 2728c2ecf20Sopenharmony_ci if (!vm_shared) { 2738c2ecf20Sopenharmony_ci if (unlikely(anon_vma_prepare(dst_vma))) 2748c2ecf20Sopenharmony_ci goto out_unlock; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci while (src_addr < src_start + len) { 2788c2ecf20Sopenharmony_ci pte_t dst_pteval; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci BUG_ON(dst_addr >= dst_start + len); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* 2838c2ecf20Sopenharmony_ci * Serialize via i_mmap_rwsem and hugetlb_fault_mutex. 2848c2ecf20Sopenharmony_ci * i_mmap_rwsem ensures the dst_pte remains valid even 2858c2ecf20Sopenharmony_ci * in the case of shared pmds. fault mutex prevents 2868c2ecf20Sopenharmony_ci * races with other faulting threads. 2878c2ecf20Sopenharmony_ci */ 2888c2ecf20Sopenharmony_ci mapping = dst_vma->vm_file->f_mapping; 2898c2ecf20Sopenharmony_ci i_mmap_lock_read(mapping); 2908c2ecf20Sopenharmony_ci idx = linear_page_index(dst_vma, dst_addr); 2918c2ecf20Sopenharmony_ci hash = hugetlb_fault_mutex_hash(mapping, idx); 2928c2ecf20Sopenharmony_ci mutex_lock(&hugetlb_fault_mutex_table[hash]); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci err = -ENOMEM; 2958c2ecf20Sopenharmony_ci dst_pte = huge_pte_alloc(dst_mm, dst_addr, vma_hpagesize); 2968c2ecf20Sopenharmony_ci if (!dst_pte) { 2978c2ecf20Sopenharmony_ci mutex_unlock(&hugetlb_fault_mutex_table[hash]); 2988c2ecf20Sopenharmony_ci i_mmap_unlock_read(mapping); 2998c2ecf20Sopenharmony_ci goto out_unlock; 3008c2ecf20Sopenharmony_ci } 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci err = -EEXIST; 3038c2ecf20Sopenharmony_ci dst_pteval = huge_ptep_get(dst_pte); 3048c2ecf20Sopenharmony_ci if (!huge_pte_none(dst_pteval)) { 3058c2ecf20Sopenharmony_ci mutex_unlock(&hugetlb_fault_mutex_table[hash]); 3068c2ecf20Sopenharmony_ci i_mmap_unlock_read(mapping); 3078c2ecf20Sopenharmony_ci goto out_unlock; 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma, 3118c2ecf20Sopenharmony_ci dst_addr, src_addr, &page); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci mutex_unlock(&hugetlb_fault_mutex_table[hash]); 3148c2ecf20Sopenharmony_ci i_mmap_unlock_read(mapping); 3158c2ecf20Sopenharmony_ci vm_alloc_shared = vm_shared; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci cond_resched(); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci if (unlikely(err == -ENOENT)) { 3208c2ecf20Sopenharmony_ci mmap_read_unlock(dst_mm); 3218c2ecf20Sopenharmony_ci BUG_ON(!page); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci err = copy_huge_page_from_user(page, 3248c2ecf20Sopenharmony_ci (const void __user *)src_addr, 3258c2ecf20Sopenharmony_ci vma_hpagesize / PAGE_SIZE, 3268c2ecf20Sopenharmony_ci true); 3278c2ecf20Sopenharmony_ci if (unlikely(err)) { 3288c2ecf20Sopenharmony_ci err = -EFAULT; 3298c2ecf20Sopenharmony_ci goto out; 3308c2ecf20Sopenharmony_ci } 3318c2ecf20Sopenharmony_ci mmap_read_lock(dst_mm); 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci dst_vma = NULL; 3348c2ecf20Sopenharmony_ci goto retry; 3358c2ecf20Sopenharmony_ci } else 3368c2ecf20Sopenharmony_ci BUG_ON(page); 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci if (!err) { 3398c2ecf20Sopenharmony_ci dst_addr += vma_hpagesize; 3408c2ecf20Sopenharmony_ci src_addr += vma_hpagesize; 3418c2ecf20Sopenharmony_ci copied += vma_hpagesize; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci if (fatal_signal_pending(current)) 3448c2ecf20Sopenharmony_ci err = -EINTR; 3458c2ecf20Sopenharmony_ci } 3468c2ecf20Sopenharmony_ci if (err) 3478c2ecf20Sopenharmony_ci break; 3488c2ecf20Sopenharmony_ci } 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ciout_unlock: 3518c2ecf20Sopenharmony_ci mmap_read_unlock(dst_mm); 3528c2ecf20Sopenharmony_ciout: 3538c2ecf20Sopenharmony_ci if (page) { 3548c2ecf20Sopenharmony_ci /* 3558c2ecf20Sopenharmony_ci * We encountered an error and are about to free a newly 3568c2ecf20Sopenharmony_ci * allocated huge page. 3578c2ecf20Sopenharmony_ci * 3588c2ecf20Sopenharmony_ci * Reservation handling is very subtle, and is different for 3598c2ecf20Sopenharmony_ci * private and shared mappings. See the routine 3608c2ecf20Sopenharmony_ci * restore_reserve_on_error for details. Unfortunately, we 3618c2ecf20Sopenharmony_ci * can not call restore_reserve_on_error now as it would 3628c2ecf20Sopenharmony_ci * require holding mmap_lock. 3638c2ecf20Sopenharmony_ci * 3648c2ecf20Sopenharmony_ci * If a reservation for the page existed in the reservation 3658c2ecf20Sopenharmony_ci * map of a private mapping, the map was modified to indicate 3668c2ecf20Sopenharmony_ci * the reservation was consumed when the page was allocated. 3678c2ecf20Sopenharmony_ci * We clear the PagePrivate flag now so that the global 3688c2ecf20Sopenharmony_ci * reserve count will not be incremented in free_huge_page. 3698c2ecf20Sopenharmony_ci * The reservation map will still indicate the reservation 3708c2ecf20Sopenharmony_ci * was consumed and possibly prevent later page allocation. 3718c2ecf20Sopenharmony_ci * This is better than leaking a global reservation. If no 3728c2ecf20Sopenharmony_ci * reservation existed, it is still safe to clear PagePrivate 3738c2ecf20Sopenharmony_ci * as no adjustments to reservation counts were made during 3748c2ecf20Sopenharmony_ci * allocation. 3758c2ecf20Sopenharmony_ci * 3768c2ecf20Sopenharmony_ci * The reservation map for shared mappings indicates which 3778c2ecf20Sopenharmony_ci * pages have reservations. When a huge page is allocated 3788c2ecf20Sopenharmony_ci * for an address with a reservation, no change is made to 3798c2ecf20Sopenharmony_ci * the reserve map. In this case PagePrivate will be set 3808c2ecf20Sopenharmony_ci * to indicate that the global reservation count should be 3818c2ecf20Sopenharmony_ci * incremented when the page is freed. This is the desired 3828c2ecf20Sopenharmony_ci * behavior. However, when a huge page is allocated for an 3838c2ecf20Sopenharmony_ci * address without a reservation a reservation entry is added 3848c2ecf20Sopenharmony_ci * to the reservation map, and PagePrivate will not be set. 3858c2ecf20Sopenharmony_ci * When the page is freed, the global reserve count will NOT 3868c2ecf20Sopenharmony_ci * be incremented and it will appear as though we have leaked 3878c2ecf20Sopenharmony_ci * reserved page. In this case, set PagePrivate so that the 3888c2ecf20Sopenharmony_ci * global reserve count will be incremented to match the 3898c2ecf20Sopenharmony_ci * reservation map entry which was created. 3908c2ecf20Sopenharmony_ci * 3918c2ecf20Sopenharmony_ci * Note that vm_alloc_shared is based on the flags of the vma 3928c2ecf20Sopenharmony_ci * for which the page was originally allocated. dst_vma could 3938c2ecf20Sopenharmony_ci * be different or NULL on error. 3948c2ecf20Sopenharmony_ci */ 3958c2ecf20Sopenharmony_ci if (vm_alloc_shared) 3968c2ecf20Sopenharmony_ci SetPagePrivate(page); 3978c2ecf20Sopenharmony_ci else 3988c2ecf20Sopenharmony_ci ClearPagePrivate(page); 3998c2ecf20Sopenharmony_ci put_page(page); 4008c2ecf20Sopenharmony_ci } 4018c2ecf20Sopenharmony_ci BUG_ON(copied < 0); 4028c2ecf20Sopenharmony_ci BUG_ON(err > 0); 4038c2ecf20Sopenharmony_ci BUG_ON(!copied && !err); 4048c2ecf20Sopenharmony_ci return copied ? copied : err; 4058c2ecf20Sopenharmony_ci} 4068c2ecf20Sopenharmony_ci#else /* !CONFIG_HUGETLB_PAGE */ 4078c2ecf20Sopenharmony_ci/* fail at build time if gcc attempts to use this */ 4088c2ecf20Sopenharmony_ciextern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, 4098c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma, 4108c2ecf20Sopenharmony_ci unsigned long dst_start, 4118c2ecf20Sopenharmony_ci unsigned long src_start, 4128c2ecf20Sopenharmony_ci unsigned long len, 4138c2ecf20Sopenharmony_ci bool zeropage); 4148c2ecf20Sopenharmony_ci#endif /* CONFIG_HUGETLB_PAGE */ 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_cistatic __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, 4178c2ecf20Sopenharmony_ci pmd_t *dst_pmd, 4188c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma, 4198c2ecf20Sopenharmony_ci unsigned long dst_addr, 4208c2ecf20Sopenharmony_ci unsigned long src_addr, 4218c2ecf20Sopenharmony_ci struct page **page, 4228c2ecf20Sopenharmony_ci bool zeropage, 4238c2ecf20Sopenharmony_ci bool wp_copy) 4248c2ecf20Sopenharmony_ci{ 4258c2ecf20Sopenharmony_ci ssize_t err; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci /* 4288c2ecf20Sopenharmony_ci * The normal page fault path for a shmem will invoke the 4298c2ecf20Sopenharmony_ci * fault, fill the hole in the file and COW it right away. The 4308c2ecf20Sopenharmony_ci * result generates plain anonymous memory. So when we are 4318c2ecf20Sopenharmony_ci * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll 4328c2ecf20Sopenharmony_ci * generate anonymous memory directly without actually filling 4338c2ecf20Sopenharmony_ci * the hole. For the MAP_PRIVATE case the robustness check 4348c2ecf20Sopenharmony_ci * only happens in the pagetable (to verify it's still none) 4358c2ecf20Sopenharmony_ci * and not in the radix tree. 4368c2ecf20Sopenharmony_ci */ 4378c2ecf20Sopenharmony_ci if (!(dst_vma->vm_flags & VM_SHARED)) { 4388c2ecf20Sopenharmony_ci if (!zeropage) 4398c2ecf20Sopenharmony_ci err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, 4408c2ecf20Sopenharmony_ci dst_addr, src_addr, page, 4418c2ecf20Sopenharmony_ci wp_copy); 4428c2ecf20Sopenharmony_ci else 4438c2ecf20Sopenharmony_ci err = mfill_zeropage_pte(dst_mm, dst_pmd, 4448c2ecf20Sopenharmony_ci dst_vma, dst_addr); 4458c2ecf20Sopenharmony_ci } else { 4468c2ecf20Sopenharmony_ci VM_WARN_ON_ONCE(wp_copy); 4478c2ecf20Sopenharmony_ci if (!zeropage) 4488c2ecf20Sopenharmony_ci err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd, 4498c2ecf20Sopenharmony_ci dst_vma, dst_addr, 4508c2ecf20Sopenharmony_ci src_addr, page); 4518c2ecf20Sopenharmony_ci else 4528c2ecf20Sopenharmony_ci err = shmem_mfill_zeropage_pte(dst_mm, dst_pmd, 4538c2ecf20Sopenharmony_ci dst_vma, dst_addr); 4548c2ecf20Sopenharmony_ci } 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci return err; 4578c2ecf20Sopenharmony_ci} 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_cistatic __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, 4608c2ecf20Sopenharmony_ci unsigned long dst_start, 4618c2ecf20Sopenharmony_ci unsigned long src_start, 4628c2ecf20Sopenharmony_ci unsigned long len, 4638c2ecf20Sopenharmony_ci bool zeropage, 4648c2ecf20Sopenharmony_ci bool *mmap_changing, 4658c2ecf20Sopenharmony_ci __u64 mode) 4668c2ecf20Sopenharmony_ci{ 4678c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma; 4688c2ecf20Sopenharmony_ci ssize_t err; 4698c2ecf20Sopenharmony_ci pmd_t *dst_pmd; 4708c2ecf20Sopenharmony_ci unsigned long src_addr, dst_addr; 4718c2ecf20Sopenharmony_ci long copied; 4728c2ecf20Sopenharmony_ci struct page *page; 4738c2ecf20Sopenharmony_ci bool wp_copy; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci /* 4768c2ecf20Sopenharmony_ci * Sanitize the command parameters: 4778c2ecf20Sopenharmony_ci */ 4788c2ecf20Sopenharmony_ci BUG_ON(dst_start & ~PAGE_MASK); 4798c2ecf20Sopenharmony_ci BUG_ON(len & ~PAGE_MASK); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci /* Does the address range wrap, or is the span zero-sized? */ 4828c2ecf20Sopenharmony_ci BUG_ON(src_start + len <= src_start); 4838c2ecf20Sopenharmony_ci BUG_ON(dst_start + len <= dst_start); 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci src_addr = src_start; 4868c2ecf20Sopenharmony_ci dst_addr = dst_start; 4878c2ecf20Sopenharmony_ci copied = 0; 4888c2ecf20Sopenharmony_ci page = NULL; 4898c2ecf20Sopenharmony_ciretry: 4908c2ecf20Sopenharmony_ci mmap_read_lock(dst_mm); 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci /* 4938c2ecf20Sopenharmony_ci * If memory mappings are changing because of non-cooperative 4948c2ecf20Sopenharmony_ci * operation (e.g. mremap) running in parallel, bail out and 4958c2ecf20Sopenharmony_ci * request the user to retry later 4968c2ecf20Sopenharmony_ci */ 4978c2ecf20Sopenharmony_ci err = -EAGAIN; 4988c2ecf20Sopenharmony_ci if (mmap_changing && READ_ONCE(*mmap_changing)) 4998c2ecf20Sopenharmony_ci goto out_unlock; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci /* 5028c2ecf20Sopenharmony_ci * Make sure the vma is not shared, that the dst range is 5038c2ecf20Sopenharmony_ci * both valid and fully within a single existing vma. 5048c2ecf20Sopenharmony_ci */ 5058c2ecf20Sopenharmony_ci err = -ENOENT; 5068c2ecf20Sopenharmony_ci dst_vma = find_dst_vma(dst_mm, dst_start, len); 5078c2ecf20Sopenharmony_ci if (!dst_vma) 5088c2ecf20Sopenharmony_ci goto out_unlock; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci err = -EINVAL; 5118c2ecf20Sopenharmony_ci /* 5128c2ecf20Sopenharmony_ci * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but 5138c2ecf20Sopenharmony_ci * it will overwrite vm_ops, so vma_is_anonymous must return false. 5148c2ecf20Sopenharmony_ci */ 5158c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && 5168c2ecf20Sopenharmony_ci dst_vma->vm_flags & VM_SHARED)) 5178c2ecf20Sopenharmony_ci goto out_unlock; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci /* 5208c2ecf20Sopenharmony_ci * validate 'mode' now that we know the dst_vma: don't allow 5218c2ecf20Sopenharmony_ci * a wrprotect copy if the userfaultfd didn't register as WP. 5228c2ecf20Sopenharmony_ci */ 5238c2ecf20Sopenharmony_ci wp_copy = mode & UFFDIO_COPY_MODE_WP; 5248c2ecf20Sopenharmony_ci if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP)) 5258c2ecf20Sopenharmony_ci goto out_unlock; 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci /* 5288c2ecf20Sopenharmony_ci * If this is a HUGETLB vma, pass off to appropriate routine 5298c2ecf20Sopenharmony_ci */ 5308c2ecf20Sopenharmony_ci if (is_vm_hugetlb_page(dst_vma)) 5318c2ecf20Sopenharmony_ci return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start, 5328c2ecf20Sopenharmony_ci src_start, len, zeropage); 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) 5358c2ecf20Sopenharmony_ci goto out_unlock; 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci /* 5388c2ecf20Sopenharmony_ci * Ensure the dst_vma has a anon_vma or this page 5398c2ecf20Sopenharmony_ci * would get a NULL anon_vma when moved in the 5408c2ecf20Sopenharmony_ci * dst_vma. 5418c2ecf20Sopenharmony_ci */ 5428c2ecf20Sopenharmony_ci err = -ENOMEM; 5438c2ecf20Sopenharmony_ci if (!(dst_vma->vm_flags & VM_SHARED) && 5448c2ecf20Sopenharmony_ci unlikely(anon_vma_prepare(dst_vma))) 5458c2ecf20Sopenharmony_ci goto out_unlock; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci while (src_addr < src_start + len) { 5488c2ecf20Sopenharmony_ci pmd_t dst_pmdval; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci BUG_ON(dst_addr >= dst_start + len); 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci dst_pmd = mm_alloc_pmd(dst_mm, dst_addr); 5538c2ecf20Sopenharmony_ci if (unlikely(!dst_pmd)) { 5548c2ecf20Sopenharmony_ci err = -ENOMEM; 5558c2ecf20Sopenharmony_ci break; 5568c2ecf20Sopenharmony_ci } 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci dst_pmdval = pmd_read_atomic(dst_pmd); 5598c2ecf20Sopenharmony_ci /* 5608c2ecf20Sopenharmony_ci * If the dst_pmd is mapped as THP don't 5618c2ecf20Sopenharmony_ci * override it and just be strict. 5628c2ecf20Sopenharmony_ci */ 5638c2ecf20Sopenharmony_ci if (unlikely(pmd_trans_huge(dst_pmdval))) { 5648c2ecf20Sopenharmony_ci err = -EEXIST; 5658c2ecf20Sopenharmony_ci break; 5668c2ecf20Sopenharmony_ci } 5678c2ecf20Sopenharmony_ci if (unlikely(pmd_none(dst_pmdval)) && 5688c2ecf20Sopenharmony_ci unlikely(__pte_alloc(dst_mm, dst_pmd))) { 5698c2ecf20Sopenharmony_ci err = -ENOMEM; 5708c2ecf20Sopenharmony_ci break; 5718c2ecf20Sopenharmony_ci } 5728c2ecf20Sopenharmony_ci /* If an huge pmd materialized from under us fail */ 5738c2ecf20Sopenharmony_ci if (unlikely(pmd_trans_huge(*dst_pmd))) { 5748c2ecf20Sopenharmony_ci err = -EFAULT; 5758c2ecf20Sopenharmony_ci break; 5768c2ecf20Sopenharmony_ci } 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci BUG_ON(pmd_none(*dst_pmd)); 5798c2ecf20Sopenharmony_ci BUG_ON(pmd_trans_huge(*dst_pmd)); 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 5828c2ecf20Sopenharmony_ci src_addr, &page, zeropage, wp_copy); 5838c2ecf20Sopenharmony_ci cond_resched(); 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci if (unlikely(err == -ENOENT)) { 5868c2ecf20Sopenharmony_ci void *page_kaddr; 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci mmap_read_unlock(dst_mm); 5898c2ecf20Sopenharmony_ci BUG_ON(!page); 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci page_kaddr = kmap(page); 5928c2ecf20Sopenharmony_ci err = copy_from_user(page_kaddr, 5938c2ecf20Sopenharmony_ci (const void __user *) src_addr, 5948c2ecf20Sopenharmony_ci PAGE_SIZE); 5958c2ecf20Sopenharmony_ci kunmap(page); 5968c2ecf20Sopenharmony_ci if (unlikely(err)) { 5978c2ecf20Sopenharmony_ci err = -EFAULT; 5988c2ecf20Sopenharmony_ci goto out; 5998c2ecf20Sopenharmony_ci } 6008c2ecf20Sopenharmony_ci flush_dcache_page(page); 6018c2ecf20Sopenharmony_ci goto retry; 6028c2ecf20Sopenharmony_ci } else 6038c2ecf20Sopenharmony_ci BUG_ON(page); 6048c2ecf20Sopenharmony_ci 6058c2ecf20Sopenharmony_ci if (!err) { 6068c2ecf20Sopenharmony_ci dst_addr += PAGE_SIZE; 6078c2ecf20Sopenharmony_ci src_addr += PAGE_SIZE; 6088c2ecf20Sopenharmony_ci copied += PAGE_SIZE; 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_ci if (fatal_signal_pending(current)) 6118c2ecf20Sopenharmony_ci err = -EINTR; 6128c2ecf20Sopenharmony_ci } 6138c2ecf20Sopenharmony_ci if (err) 6148c2ecf20Sopenharmony_ci break; 6158c2ecf20Sopenharmony_ci } 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ciout_unlock: 6188c2ecf20Sopenharmony_ci mmap_read_unlock(dst_mm); 6198c2ecf20Sopenharmony_ciout: 6208c2ecf20Sopenharmony_ci if (page) 6218c2ecf20Sopenharmony_ci put_page(page); 6228c2ecf20Sopenharmony_ci BUG_ON(copied < 0); 6238c2ecf20Sopenharmony_ci BUG_ON(err > 0); 6248c2ecf20Sopenharmony_ci BUG_ON(!copied && !err); 6258c2ecf20Sopenharmony_ci return copied ? copied : err; 6268c2ecf20Sopenharmony_ci} 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_cissize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start, 6298c2ecf20Sopenharmony_ci unsigned long src_start, unsigned long len, 6308c2ecf20Sopenharmony_ci bool *mmap_changing, __u64 mode) 6318c2ecf20Sopenharmony_ci{ 6328c2ecf20Sopenharmony_ci return __mcopy_atomic(dst_mm, dst_start, src_start, len, false, 6338c2ecf20Sopenharmony_ci mmap_changing, mode); 6348c2ecf20Sopenharmony_ci} 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_cissize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start, 6378c2ecf20Sopenharmony_ci unsigned long len, bool *mmap_changing) 6388c2ecf20Sopenharmony_ci{ 6398c2ecf20Sopenharmony_ci return __mcopy_atomic(dst_mm, start, 0, len, true, mmap_changing, 0); 6408c2ecf20Sopenharmony_ci} 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ciint mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, 6438c2ecf20Sopenharmony_ci unsigned long len, bool enable_wp, bool *mmap_changing) 6448c2ecf20Sopenharmony_ci{ 6458c2ecf20Sopenharmony_ci struct vm_area_struct *dst_vma; 6468c2ecf20Sopenharmony_ci pgprot_t newprot; 6478c2ecf20Sopenharmony_ci int err; 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci /* 6508c2ecf20Sopenharmony_ci * Sanitize the command parameters: 6518c2ecf20Sopenharmony_ci */ 6528c2ecf20Sopenharmony_ci BUG_ON(start & ~PAGE_MASK); 6538c2ecf20Sopenharmony_ci BUG_ON(len & ~PAGE_MASK); 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_ci /* Does the address range wrap, or is the span zero-sized? */ 6568c2ecf20Sopenharmony_ci BUG_ON(start + len <= start); 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_ci mmap_read_lock(dst_mm); 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci /* 6618c2ecf20Sopenharmony_ci * If memory mappings are changing because of non-cooperative 6628c2ecf20Sopenharmony_ci * operation (e.g. mremap) running in parallel, bail out and 6638c2ecf20Sopenharmony_ci * request the user to retry later 6648c2ecf20Sopenharmony_ci */ 6658c2ecf20Sopenharmony_ci err = -EAGAIN; 6668c2ecf20Sopenharmony_ci if (mmap_changing && READ_ONCE(*mmap_changing)) 6678c2ecf20Sopenharmony_ci goto out_unlock; 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci err = -ENOENT; 6708c2ecf20Sopenharmony_ci dst_vma = find_dst_vma(dst_mm, start, len); 6718c2ecf20Sopenharmony_ci /* 6728c2ecf20Sopenharmony_ci * Make sure the vma is not shared, that the dst range is 6738c2ecf20Sopenharmony_ci * both valid and fully within a single existing vma. 6748c2ecf20Sopenharmony_ci */ 6758c2ecf20Sopenharmony_ci if (!dst_vma || (dst_vma->vm_flags & VM_SHARED)) 6768c2ecf20Sopenharmony_ci goto out_unlock; 6778c2ecf20Sopenharmony_ci if (!userfaultfd_wp(dst_vma)) 6788c2ecf20Sopenharmony_ci goto out_unlock; 6798c2ecf20Sopenharmony_ci if (!vma_is_anonymous(dst_vma)) 6808c2ecf20Sopenharmony_ci goto out_unlock; 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci if (enable_wp) 6838c2ecf20Sopenharmony_ci newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE)); 6848c2ecf20Sopenharmony_ci else 6858c2ecf20Sopenharmony_ci newprot = vm_get_page_prot(dst_vma->vm_flags); 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci change_protection(dst_vma, start, start + len, newprot, 6888c2ecf20Sopenharmony_ci enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE); 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci err = 0; 6918c2ecf20Sopenharmony_ciout_unlock: 6928c2ecf20Sopenharmony_ci mmap_read_unlock(dst_mm); 6938c2ecf20Sopenharmony_ci return err; 6948c2ecf20Sopenharmony_ci} 695