1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mm/userfaultfd.c 4 * 5 * Copyright (C) 2015 Red Hat, Inc. 6 */ 7 8 #include <linux/mm.h> 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> 19 #include <asm/tlb.h> 20 #include "internal.h" 21 22 static __always_inline 23 struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, 24 unsigned long dst_start, 25 unsigned long len) 26 { 27 /* 28 * Make sure that the dst range is both valid and fully within a 29 * single existing vma. 30 */ 31 struct vm_area_struct *dst_vma; 32 33 dst_vma = find_vma(dst_mm, dst_start); 34 if (!dst_vma) 35 return NULL; 36 37 if (dst_start < dst_vma->vm_start || 38 dst_start + len > dst_vma->vm_end) 39 return NULL; 40 41 /* 42 * Check the vma is registered in uffd, this is required to 43 * enforce the VM_MAYWRITE check done at uffd registration 44 * time. 45 */ 46 if (!dst_vma->vm_userfaultfd_ctx.ctx) 47 return NULL; 48 49 return dst_vma; 50 } 51 52 /* 53 * Install PTEs, to map dst_addr (within dst_vma) to page. 54 * 55 * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem 56 * and anon, and for both shared and private VMAs. 57 */ 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) 62 { 63 int ret; 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; 68 spinlock_t *ptl; 69 struct inode *inode; 70 pgoff_t offset, max_off; 71 72 _dst_pte = mk_pte(page, dst_vma->vm_page_prot); 73 _dst_pte = pte_mkdirty(_dst_pte); 74 if (page_in_cache && !vm_shared) 75 writable = false; 76 77 /* 78 * Always mark a PTE as write-protected when needed, regardless of 79 * VM_WRITE, which the user might change. 80 */ 81 if (wp_copy) 82 _dst_pte = pte_mkuffd_wp(_dst_pte); 83 else if (writable) 84 _dst_pte = pte_mkwrite(_dst_pte); 85 86 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 87 88 if (vma_is_shmem(dst_vma)) { 89 /* serialize against truncate with the page table lock */ 90 inode = dst_vma->vm_file->f_inode; 91 offset = linear_page_index(dst_vma, dst_addr); 92 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 93 ret = -EFAULT; 94 if (unlikely(offset >= max_off)) 95 goto out_unlock; 96 } 97 98 ret = -EEXIST; 99 if (!pte_none(*dst_pte)) 100 goto out_unlock; 101 102 if (page_in_cache) { 103 /* Usually, cache pages are already added to LRU */ 104 if (newly_allocated) 105 lru_cache_add(page); 106 page_add_file_rmap(page, dst_vma, false); 107 } else { 108 page_add_new_anon_rmap(page, dst_vma, dst_addr); 109 lru_cache_add_inactive_or_unevictable(page, dst_vma); 110 } 111 112 /* 113 * Must happen after rmap, as mm_counter() checks mapping (via 114 * PageAnon()), which is set by __page_set_anon_rmap(). 115 */ 116 inc_mm_counter(dst_mm, mm_counter(page)); 117 118 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 119 120 /* No need to invalidate - it was non-present before */ 121 update_mmu_cache(dst_vma, dst_addr, dst_pte); 122 ret = 0; 123 out_unlock: 124 pte_unmap_unlock(dst_pte, ptl); 125 return ret; 126 } 127 128 static int mcopy_atomic_pte(struct mm_struct *dst_mm, 129 pmd_t *dst_pmd, 130 struct vm_area_struct *dst_vma, 131 unsigned long dst_addr, 132 unsigned long src_addr, 133 struct page **pagep, 134 bool wp_copy) 135 { 136 void *page_kaddr; 137 int ret; 138 struct page *page; 139 140 if (!*pagep) { 141 ret = -ENOMEM; 142 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr); 143 if (!page) 144 goto out; 145 146 page_kaddr = kmap_atomic(page); 147 ret = copy_from_user(page_kaddr, 148 (const void __user *) src_addr, 149 PAGE_SIZE); 150 kunmap_atomic(page_kaddr); 151 152 /* fallback to copy_from_user outside mmap_lock */ 153 if (unlikely(ret)) { 154 ret = -ENOENT; 155 *pagep = page; 156 /* don't free the page */ 157 goto out; 158 } 159 160 flush_dcache_page(page); 161 } else { 162 page = *pagep; 163 *pagep = NULL; 164 } 165 166 /* 167 * The memory barrier inside __SetPageUptodate makes sure that 168 * preceding stores to the page contents become visible before 169 * the set_pte_at() write. 170 */ 171 __SetPageUptodate(page); 172 173 ret = -ENOMEM; 174 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL)) 175 goto out_release; 176 177 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 178 page, true, wp_copy); 179 if (ret) 180 goto out_release; 181 out: 182 return ret; 183 out_release: 184 put_page(page); 185 goto out; 186 } 187 188 static int mfill_zeropage_pte(struct mm_struct *dst_mm, 189 pmd_t *dst_pmd, 190 struct vm_area_struct *dst_vma, 191 unsigned long dst_addr) 192 { 193 pte_t _dst_pte, *dst_pte; 194 spinlock_t *ptl; 195 int ret; 196 pgoff_t offset, max_off; 197 struct inode *inode; 198 199 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), 200 dst_vma->vm_page_prot)); 201 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 202 if (dst_vma->vm_file) { 203 /* the shmem MAP_PRIVATE case requires checking the i_size */ 204 inode = dst_vma->vm_file->f_inode; 205 offset = linear_page_index(dst_vma, dst_addr); 206 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 207 ret = -EFAULT; 208 if (unlikely(offset >= max_off)) 209 goto out_unlock; 210 } 211 ret = -EEXIST; 212 if (!pte_none(*dst_pte)) 213 goto out_unlock; 214 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 215 /* No need to invalidate - it was non-present before */ 216 update_mmu_cache(dst_vma, dst_addr, dst_pte); 217 ret = 0; 218 out_unlock: 219 pte_unmap_unlock(dst_pte, ptl); 220 return ret; 221 } 222 223 /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */ 224 static int mcontinue_atomic_pte(struct mm_struct *dst_mm, 225 pmd_t *dst_pmd, 226 struct vm_area_struct *dst_vma, 227 unsigned long dst_addr, 228 bool wp_copy) 229 { 230 struct inode *inode = file_inode(dst_vma->vm_file); 231 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 232 struct page *page; 233 int ret; 234 235 ret = shmem_getpage(inode, pgoff, &page, SGP_READ); 236 if (ret) 237 goto out; 238 if (!page) { 239 ret = -EFAULT; 240 goto out; 241 } 242 243 if (PageHWPoison(page)) { 244 ret = -EIO; 245 goto out_release; 246 } 247 248 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 249 page, false, wp_copy); 250 if (ret) 251 goto out_release; 252 253 unlock_page(page); 254 ret = 0; 255 out: 256 return ret; 257 out_release: 258 unlock_page(page); 259 put_page(page); 260 goto out; 261 } 262 263 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) 264 { 265 pgd_t *pgd; 266 p4d_t *p4d; 267 pud_t *pud; 268 269 pgd = pgd_offset(mm, address); 270 p4d = p4d_alloc(mm, pgd, address); 271 if (!p4d) 272 return NULL; 273 pud = pud_alloc(mm, p4d, address); 274 if (!pud) 275 return NULL; 276 /* 277 * Note that we didn't run this because the pmd was 278 * missing, the *pmd may be already established and in 279 * turn it may also be a trans_huge_pmd. 280 */ 281 return pmd_alloc(mm, pud, address); 282 } 283 284 #ifdef CONFIG_HUGETLB_PAGE 285 /* 286 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is 287 * called with mmap_lock held, it will release mmap_lock before returning. 288 */ 289 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, 290 struct vm_area_struct *dst_vma, 291 unsigned long dst_start, 292 unsigned long src_start, 293 unsigned long len, 294 enum mcopy_atomic_mode mode) 295 { 296 int vm_shared = dst_vma->vm_flags & VM_SHARED; 297 ssize_t err; 298 pte_t *dst_pte; 299 unsigned long src_addr, dst_addr; 300 long copied; 301 struct page *page; 302 unsigned long vma_hpagesize; 303 pgoff_t idx; 304 u32 hash; 305 struct address_space *mapping; 306 307 /* 308 * There is no default zero huge page for all huge page sizes as 309 * supported by hugetlb. A PMD_SIZE huge pages may exist as used 310 * by THP. Since we can not reliably insert a zero page, this 311 * feature is not supported. 312 */ 313 if (mode == MCOPY_ATOMIC_ZEROPAGE) { 314 mmap_read_unlock(dst_mm); 315 return -EINVAL; 316 } 317 318 src_addr = src_start; 319 dst_addr = dst_start; 320 copied = 0; 321 page = NULL; 322 vma_hpagesize = vma_kernel_pagesize(dst_vma); 323 324 /* 325 * Validate alignment based on huge page size 326 */ 327 err = -EINVAL; 328 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1)) 329 goto out_unlock; 330 331 retry: 332 /* 333 * On routine entry dst_vma is set. If we had to drop mmap_lock and 334 * retry, dst_vma will be set to NULL and we must lookup again. 335 */ 336 if (!dst_vma) { 337 err = -ENOENT; 338 dst_vma = find_dst_vma(dst_mm, dst_start, len); 339 if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) 340 goto out_unlock; 341 342 err = -EINVAL; 343 if (vma_hpagesize != vma_kernel_pagesize(dst_vma)) 344 goto out_unlock; 345 346 vm_shared = dst_vma->vm_flags & VM_SHARED; 347 } 348 349 /* 350 * If not shared, ensure the dst_vma has a anon_vma. 351 */ 352 err = -ENOMEM; 353 if (!vm_shared) { 354 if (unlikely(anon_vma_prepare(dst_vma))) 355 goto out_unlock; 356 } 357 358 while (src_addr < src_start + len) { 359 BUG_ON(dst_addr >= dst_start + len); 360 361 /* 362 * Serialize via i_mmap_rwsem and hugetlb_fault_mutex. 363 * i_mmap_rwsem ensures the dst_pte remains valid even 364 * in the case of shared pmds. fault mutex prevents 365 * races with other faulting threads. 366 */ 367 mapping = dst_vma->vm_file->f_mapping; 368 i_mmap_lock_read(mapping); 369 idx = linear_page_index(dst_vma, dst_addr); 370 hash = hugetlb_fault_mutex_hash(mapping, idx); 371 mutex_lock(&hugetlb_fault_mutex_table[hash]); 372 373 err = -ENOMEM; 374 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize); 375 if (!dst_pte) { 376 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 377 i_mmap_unlock_read(mapping); 378 goto out_unlock; 379 } 380 381 if (mode != MCOPY_ATOMIC_CONTINUE && 382 !huge_pte_none(huge_ptep_get(dst_pte))) { 383 err = -EEXIST; 384 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 385 i_mmap_unlock_read(mapping); 386 goto out_unlock; 387 } 388 389 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma, 390 dst_addr, src_addr, mode, &page); 391 392 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 393 i_mmap_unlock_read(mapping); 394 395 cond_resched(); 396 397 if (unlikely(err == -ENOENT)) { 398 mmap_read_unlock(dst_mm); 399 BUG_ON(!page); 400 401 err = copy_huge_page_from_user(page, 402 (const void __user *)src_addr, 403 vma_hpagesize / PAGE_SIZE, 404 true); 405 if (unlikely(err)) { 406 err = -EFAULT; 407 goto out; 408 } 409 mmap_read_lock(dst_mm); 410 411 dst_vma = NULL; 412 goto retry; 413 } else 414 BUG_ON(page); 415 416 if (!err) { 417 dst_addr += vma_hpagesize; 418 src_addr += vma_hpagesize; 419 copied += vma_hpagesize; 420 421 if (fatal_signal_pending(current)) 422 err = -EINTR; 423 } 424 if (err) 425 break; 426 } 427 428 out_unlock: 429 mmap_read_unlock(dst_mm); 430 out: 431 if (page) 432 put_page(page); 433 BUG_ON(copied < 0); 434 BUG_ON(err > 0); 435 BUG_ON(!copied && !err); 436 return copied ? copied : err; 437 } 438 #else /* !CONFIG_HUGETLB_PAGE */ 439 /* fail at build time if gcc attempts to use this */ 440 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, 441 struct vm_area_struct *dst_vma, 442 unsigned long dst_start, 443 unsigned long src_start, 444 unsigned long len, 445 enum mcopy_atomic_mode mode); 446 #endif /* CONFIG_HUGETLB_PAGE */ 447 448 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, 449 pmd_t *dst_pmd, 450 struct vm_area_struct *dst_vma, 451 unsigned long dst_addr, 452 unsigned long src_addr, 453 struct page **page, 454 enum mcopy_atomic_mode mode, 455 bool wp_copy) 456 { 457 ssize_t err; 458 459 if (mode == MCOPY_ATOMIC_CONTINUE) { 460 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 461 wp_copy); 462 } 463 464 /* 465 * The normal page fault path for a shmem will invoke the 466 * fault, fill the hole in the file and COW it right away. The 467 * result generates plain anonymous memory. So when we are 468 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll 469 * generate anonymous memory directly without actually filling 470 * the hole. For the MAP_PRIVATE case the robustness check 471 * only happens in the pagetable (to verify it's still none) 472 * and not in the radix tree. 473 */ 474 if (!(dst_vma->vm_flags & VM_SHARED)) { 475 if (mode == MCOPY_ATOMIC_NORMAL) 476 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, 477 dst_addr, src_addr, page, 478 wp_copy); 479 else 480 err = mfill_zeropage_pte(dst_mm, dst_pmd, 481 dst_vma, dst_addr); 482 } else { 483 VM_WARN_ON_ONCE(wp_copy); 484 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 485 dst_addr, src_addr, 486 mode != MCOPY_ATOMIC_NORMAL, 487 page); 488 } 489 490 return err; 491 } 492 493 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, 494 unsigned long dst_start, 495 unsigned long src_start, 496 unsigned long len, 497 enum mcopy_atomic_mode mcopy_mode, 498 atomic_t *mmap_changing, 499 __u64 mode) 500 { 501 struct vm_area_struct *dst_vma; 502 ssize_t err; 503 pmd_t *dst_pmd; 504 unsigned long src_addr, dst_addr; 505 long copied; 506 struct page *page; 507 bool wp_copy; 508 509 /* 510 * Sanitize the command parameters: 511 */ 512 BUG_ON(dst_start & ~PAGE_MASK); 513 BUG_ON(len & ~PAGE_MASK); 514 515 /* Does the address range wrap, or is the span zero-sized? */ 516 BUG_ON(src_start + len <= src_start); 517 BUG_ON(dst_start + len <= dst_start); 518 519 src_addr = src_start; 520 dst_addr = dst_start; 521 copied = 0; 522 page = NULL; 523 retry: 524 mmap_read_lock(dst_mm); 525 526 /* 527 * If memory mappings are changing because of non-cooperative 528 * operation (e.g. mremap) running in parallel, bail out and 529 * request the user to retry later 530 */ 531 err = -EAGAIN; 532 if (mmap_changing && atomic_read(mmap_changing)) 533 goto out_unlock; 534 535 /* 536 * Make sure the vma is not shared, that the dst range is 537 * both valid and fully within a single existing vma. 538 */ 539 err = -ENOENT; 540 dst_vma = find_dst_vma(dst_mm, dst_start, len); 541 if (!dst_vma) 542 goto out_unlock; 543 544 err = -EINVAL; 545 /* 546 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but 547 * it will overwrite vm_ops, so vma_is_anonymous must return false. 548 */ 549 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && 550 dst_vma->vm_flags & VM_SHARED)) 551 goto out_unlock; 552 553 /* 554 * validate 'mode' now that we know the dst_vma: don't allow 555 * a wrprotect copy if the userfaultfd didn't register as WP. 556 */ 557 wp_copy = mode & UFFDIO_COPY_MODE_WP; 558 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP)) 559 goto out_unlock; 560 561 /* 562 * If this is a HUGETLB vma, pass off to appropriate routine 563 */ 564 if (is_vm_hugetlb_page(dst_vma)) 565 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start, 566 src_start, len, mcopy_mode); 567 568 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) 569 goto out_unlock; 570 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE) 571 goto out_unlock; 572 573 /* 574 * Ensure the dst_vma has a anon_vma or this page 575 * would get a NULL anon_vma when moved in the 576 * dst_vma. 577 */ 578 err = -ENOMEM; 579 if (!(dst_vma->vm_flags & VM_SHARED) && 580 unlikely(anon_vma_prepare(dst_vma))) 581 goto out_unlock; 582 583 while (src_addr < src_start + len) { 584 pmd_t dst_pmdval; 585 586 BUG_ON(dst_addr >= dst_start + len); 587 588 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr); 589 if (unlikely(!dst_pmd)) { 590 err = -ENOMEM; 591 break; 592 } 593 594 dst_pmdval = pmd_read_atomic(dst_pmd); 595 /* 596 * If the dst_pmd is mapped as THP don't 597 * override it and just be strict. 598 */ 599 if (unlikely(pmd_trans_huge(dst_pmdval))) { 600 err = -EEXIST; 601 break; 602 } 603 if (unlikely(pmd_none(dst_pmdval)) && 604 unlikely(__pte_alloc(dst_mm, dst_pmd))) { 605 err = -ENOMEM; 606 break; 607 } 608 /* If an huge pmd materialized from under us fail */ 609 if (unlikely(pmd_trans_huge(*dst_pmd))) { 610 err = -EFAULT; 611 break; 612 } 613 614 BUG_ON(pmd_none(*dst_pmd)); 615 BUG_ON(pmd_trans_huge(*dst_pmd)); 616 617 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 618 src_addr, &page, mcopy_mode, wp_copy); 619 cond_resched(); 620 621 if (unlikely(err == -ENOENT)) { 622 void *page_kaddr; 623 624 mmap_read_unlock(dst_mm); 625 BUG_ON(!page); 626 627 page_kaddr = kmap(page); 628 err = copy_from_user(page_kaddr, 629 (const void __user *) src_addr, 630 PAGE_SIZE); 631 kunmap(page); 632 if (unlikely(err)) { 633 err = -EFAULT; 634 goto out; 635 } 636 flush_dcache_page(page); 637 goto retry; 638 } else 639 BUG_ON(page); 640 641 if (!err) { 642 dst_addr += PAGE_SIZE; 643 src_addr += PAGE_SIZE; 644 copied += PAGE_SIZE; 645 646 if (fatal_signal_pending(current)) 647 err = -EINTR; 648 } 649 if (err) 650 break; 651 } 652 653 out_unlock: 654 mmap_read_unlock(dst_mm); 655 out: 656 if (page) 657 put_page(page); 658 BUG_ON(copied < 0); 659 BUG_ON(err > 0); 660 BUG_ON(!copied && !err); 661 return copied ? copied : err; 662 } 663 664 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start, 665 unsigned long src_start, unsigned long len, 666 atomic_t *mmap_changing, __u64 mode) 667 { 668 return __mcopy_atomic(dst_mm, dst_start, src_start, len, 669 MCOPY_ATOMIC_NORMAL, mmap_changing, mode); 670 } 671 672 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start, 673 unsigned long len, atomic_t *mmap_changing) 674 { 675 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE, 676 mmap_changing, 0); 677 } 678 679 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start, 680 unsigned long len, atomic_t *mmap_changing) 681 { 682 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE, 683 mmap_changing, 0); 684 } 685 686 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, 687 unsigned long len, bool enable_wp, 688 atomic_t *mmap_changing) 689 { 690 struct vm_area_struct *dst_vma; 691 struct mmu_gather tlb; 692 pgprot_t newprot; 693 int err; 694 695 /* 696 * Sanitize the command parameters: 697 */ 698 BUG_ON(start & ~PAGE_MASK); 699 BUG_ON(len & ~PAGE_MASK); 700 701 /* Does the address range wrap, or is the span zero-sized? */ 702 BUG_ON(start + len <= start); 703 704 mmap_read_lock(dst_mm); 705 706 /* 707 * If memory mappings are changing because of non-cooperative 708 * operation (e.g. mremap) running in parallel, bail out and 709 * request the user to retry later 710 */ 711 err = -EAGAIN; 712 if (mmap_changing && atomic_read(mmap_changing)) 713 goto out_unlock; 714 715 err = -ENOENT; 716 dst_vma = find_dst_vma(dst_mm, start, len); 717 /* 718 * Make sure the vma is not shared, that the dst range is 719 * both valid and fully within a single existing vma. 720 */ 721 if (!dst_vma || (dst_vma->vm_flags & VM_SHARED)) 722 goto out_unlock; 723 if (!userfaultfd_wp(dst_vma)) 724 goto out_unlock; 725 if (!vma_is_anonymous(dst_vma)) 726 goto out_unlock; 727 728 if (enable_wp) 729 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE)); 730 else 731 newprot = vm_get_page_prot(dst_vma->vm_flags); 732 733 tlb_gather_mmu(&tlb, dst_mm); 734 change_protection(&tlb, dst_vma, start, start + len, newprot, 735 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE); 736 tlb_finish_mmu(&tlb); 737 738 err = 0; 739 out_unlock: 740 mmap_read_unlock(dst_mm); 741 return err; 742 } 743