1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * fs/userfaultfd.c 4 * 5 * Copyright (C) 2007 Davide Libenzi <davide 6 * Copyright (C) 2008-2009 Red Hat, Inc. 7 * Copyright (C) 2015 Red Hat, Inc. 8 * 9 * Some part derived from fs/eventfd.c (anon 10 * mm/ksm.c (mm hashing). 11 */ 12 13 #include <linux/list.h> 14 #include <linux/hashtable.h> 15 #include <linux/sched/signal.h> 16 #include <linux/sched/mm.h> 17 #include <linux/mm.h> 18 #include <linux/mm_inline.h> 19 #include <linux/mmu_notifier.h> 20 #include <linux/poll.h> 21 #include <linux/slab.h> 22 #include <linux/seq_file.h> 23 #include <linux/file.h> 24 #include <linux/bug.h> 25 #include <linux/anon_inodes.h> 26 #include <linux/syscalls.h> 27 #include <linux/userfaultfd_k.h> 28 #include <linux/mempolicy.h> 29 #include <linux/ioctl.h> 30 #include <linux/security.h> 31 #include <linux/hugetlb.h> 32 #include <linux/swapops.h> 33 #include <linux/miscdevice.h> 34 #include <linux/uio.h> 35 36 static int sysctl_unprivileged_userfaultfd __r 37 38 #ifdef CONFIG_SYSCTL 39 static struct ctl_table vm_userfaultfd_table[] 40 { 41 .procname = "unprivilege 42 .data = &sysctl_unpr 43 .maxlen = sizeof(sysct 44 .mode = 0644, 45 .proc_handler = proc_dointve 46 .extra1 = SYSCTL_ZERO, 47 .extra2 = SYSCTL_ONE, 48 }, 49 }; 50 #endif 51 52 static struct kmem_cache *userfaultfd_ctx_cach 53 54 struct userfaultfd_fork_ctx { 55 struct userfaultfd_ctx *orig; 56 struct userfaultfd_ctx *new; 57 struct list_head list; 58 }; 59 60 struct userfaultfd_unmap_ctx { 61 struct userfaultfd_ctx *ctx; 62 unsigned long start; 63 unsigned long end; 64 struct list_head list; 65 }; 66 67 struct userfaultfd_wait_queue { 68 struct uffd_msg msg; 69 wait_queue_entry_t wq; 70 struct userfaultfd_ctx *ctx; 71 bool waken; 72 }; 73 74 struct userfaultfd_wake_range { 75 unsigned long start; 76 unsigned long len; 77 }; 78 79 /* internal indication that UFFD_API ioctl was 80 #define UFFD_FEATURE_INITIALIZED 81 82 static bool userfaultfd_is_initialized(struct 83 { 84 return ctx->features & UFFD_FEATURE_IN 85 } 86 87 static bool userfaultfd_wp_async_ctx(struct us 88 { 89 return ctx && (ctx->features & UFFD_FE 90 } 91 92 /* 93 * Whether WP_UNPOPULATED is enabled on the uf 94 * meaningful when userfaultfd_wp()==true on t 95 * anonymous. 96 */ 97 bool userfaultfd_wp_unpopulated(struct vm_area 98 { 99 struct userfaultfd_ctx *ctx = vma->vm_ 100 101 if (!ctx) 102 return false; 103 104 return ctx->features & UFFD_FEATURE_WP 105 } 106 107 static void userfaultfd_set_vm_flags(struct vm 108 vm_flags_ 109 { 110 const bool uffd_wp_changed = (vma->vm_ 111 112 vm_flags_reset(vma, flags); 113 /* 114 * For shared mappings, we want to ena 115 * userfaultfd-wp is enabled (see vma_ 116 * recalculate vma->vm_page_prot whene 117 */ 118 if ((vma->vm_flags & VM_SHARED) && uff 119 vma_set_page_prot(vma); 120 } 121 122 static int userfaultfd_wake_function(wait_queu 123 int wake_ 124 { 125 struct userfaultfd_wake_range *range = 126 int ret; 127 struct userfaultfd_wait_queue *uwq; 128 unsigned long start, len; 129 130 uwq = container_of(wq, struct userfaul 131 ret = 0; 132 /* len == 0 means wake all */ 133 start = range->start; 134 len = range->len; 135 if (len && (start > uwq->msg.arg.pagef 136 start + len <= uwq->msg.ar 137 goto out; 138 WRITE_ONCE(uwq->waken, true); 139 /* 140 * The Program-Order guarantees provid 141 * ensure uwq->waken is visible before 142 */ 143 ret = wake_up_state(wq->private, mode) 144 if (ret) { 145 /* 146 * Wake only once, autoremove 147 * 148 * After the effect of list_de 149 * CPUs, the waitqueue may dis 150 * !list_empty_careful() in ha 151 * 152 * try_to_wake_up() has an imp 153 * wq->private is read before 154 * "wake_up_state" (which in t 155 */ 156 list_del_init(&wq->entry); 157 } 158 out: 159 return ret; 160 } 161 162 /** 163 * userfaultfd_ctx_get - Acquires a reference 164 * context. 165 * @ctx: [in] Pointer to the userfaultfd conte 166 */ 167 static void userfaultfd_ctx_get(struct userfau 168 { 169 refcount_inc(&ctx->refcount); 170 } 171 172 /** 173 * userfaultfd_ctx_put - Releases a reference 174 * context. 175 * @ctx: [in] Pointer to userfaultfd context. 176 * 177 * The userfaultfd context reference must have 178 * with userfaultfd_ctx_get() or userfaultfd_c 179 */ 180 static void userfaultfd_ctx_put(struct userfau 181 { 182 if (refcount_dec_and_test(&ctx->refcou 183 VM_BUG_ON(spin_is_locked(&ctx- 184 VM_BUG_ON(waitqueue_active(&ct 185 VM_BUG_ON(spin_is_locked(&ctx- 186 VM_BUG_ON(waitqueue_active(&ct 187 VM_BUG_ON(spin_is_locked(&ctx- 188 VM_BUG_ON(waitqueue_active(&ct 189 VM_BUG_ON(spin_is_locked(&ctx- 190 VM_BUG_ON(waitqueue_active(&ct 191 mmdrop(ctx->mm); 192 kmem_cache_free(userfaultfd_ct 193 } 194 } 195 196 static inline void msg_init(struct uffd_msg *m 197 { 198 BUILD_BUG_ON(sizeof(struct uffd_msg) ! 199 /* 200 * Must use memset to zero out the pad 201 * leaked to userland. 202 */ 203 memset(msg, 0, sizeof(struct uffd_msg) 204 } 205 206 static inline struct uffd_msg userfault_msg(un 207 un 208 un 209 un 210 un 211 { 212 struct uffd_msg msg; 213 214 msg_init(&msg); 215 msg.event = UFFD_EVENT_PAGEFAULT; 216 217 msg.arg.pagefault.address = (features 218 real_addre 219 220 /* 221 * These flags indicate why the userfa 222 * - UFFD_PAGEFAULT_FLAG_WP indicates 223 * - UFFD_PAGEFAULT_FLAG_MINOR indicat 224 * - Neither of these flags being set 225 * 226 * Separately, UFFD_PAGEFAULT_FLAG_WRI 227 * fault. Otherwise, it was a read fau 228 */ 229 if (flags & FAULT_FLAG_WRITE) 230 msg.arg.pagefault.flags |= UFF 231 if (reason & VM_UFFD_WP) 232 msg.arg.pagefault.flags |= UFF 233 if (reason & VM_UFFD_MINOR) 234 msg.arg.pagefault.flags |= UFF 235 if (features & UFFD_FEATURE_THREAD_ID) 236 msg.arg.pagefault.feat.ptid = 237 return msg; 238 } 239 240 #ifdef CONFIG_HUGETLB_PAGE 241 /* 242 * Same functionality as userfaultfd_must_wait 243 * hugepmd ranges. 244 */ 245 static inline bool userfaultfd_huge_must_wait( 246 247 248 { 249 struct vm_area_struct *vma = vmf->vma; 250 pte_t *ptep, pte; 251 bool ret = true; 252 253 assert_fault_locked(vmf); 254 255 ptep = hugetlb_walk(vma, vmf->address, 256 if (!ptep) 257 goto out; 258 259 ret = false; 260 pte = huge_ptep_get(vma->vm_mm, vmf->a 261 262 /* 263 * Lockless access: we're in a wait_ev 264 * changes under us. PTE markers shou 265 * ptes here. 266 */ 267 if (huge_pte_none_mostly(pte)) 268 ret = true; 269 if (!huge_pte_write(pte) && (reason & 270 ret = true; 271 out: 272 return ret; 273 } 274 #else 275 static inline bool userfaultfd_huge_must_wait( 276 277 278 { 279 return false; /* should never get he 280 } 281 #endif /* CONFIG_HUGETLB_PAGE */ 282 283 /* 284 * Verify the pagetables are still not ok afte 285 * the fault_pending_wqh to avoid userland hav 286 * userfault that has already been resolved, i 287 * UFFDIO_COPY|ZEROPAGE are being run simultan 288 * threads. 289 */ 290 static inline bool userfaultfd_must_wait(struc 291 struc 292 unsig 293 { 294 struct mm_struct *mm = ctx->mm; 295 unsigned long address = vmf->address; 296 pgd_t *pgd; 297 p4d_t *p4d; 298 pud_t *pud; 299 pmd_t *pmd, _pmd; 300 pte_t *pte; 301 pte_t ptent; 302 bool ret = true; 303 304 assert_fault_locked(vmf); 305 306 pgd = pgd_offset(mm, address); 307 if (!pgd_present(*pgd)) 308 goto out; 309 p4d = p4d_offset(pgd, address); 310 if (!p4d_present(*p4d)) 311 goto out; 312 pud = pud_offset(p4d, address); 313 if (!pud_present(*pud)) 314 goto out; 315 pmd = pmd_offset(pud, address); 316 again: 317 _pmd = pmdp_get_lockless(pmd); 318 if (pmd_none(_pmd)) 319 goto out; 320 321 ret = false; 322 if (!pmd_present(_pmd) || pmd_devmap(_ 323 goto out; 324 325 if (pmd_trans_huge(_pmd)) { 326 if (!pmd_write(_pmd) && (reaso 327 ret = true; 328 goto out; 329 } 330 331 pte = pte_offset_map(pmd, address); 332 if (!pte) { 333 ret = true; 334 goto again; 335 } 336 /* 337 * Lockless access: we're in a wait_ev 338 * changes under us. PTE markers shou 339 * ptes here. 340 */ 341 ptent = ptep_get(pte); 342 if (pte_none_mostly(ptent)) 343 ret = true; 344 if (!pte_write(ptent) && (reason & VM_ 345 ret = true; 346 pte_unmap(pte); 347 348 out: 349 return ret; 350 } 351 352 static inline unsigned int userfaultfd_get_blo 353 { 354 if (flags & FAULT_FLAG_INTERRUPTIBLE) 355 return TASK_INTERRUPTIBLE; 356 357 if (flags & FAULT_FLAG_KILLABLE) 358 return TASK_KILLABLE; 359 360 return TASK_UNINTERRUPTIBLE; 361 } 362 363 /* 364 * The locking rules involved in returning VM_ 365 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NO 366 * FAULT_FLAG_KILLABLE are not straightforward 367 * recommendation in __lock_page_or_retry is n 368 * 369 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_ 370 * before returning VM_FAULT_RETRY only if FAU 371 * not set. 372 * 373 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_ 374 * set, VM_FAULT_RETRY can still be returned i 375 * fatal_signal_pending()s, and the mmap_lock 376 * returning it. 377 */ 378 vm_fault_t handle_userfault(struct vm_fault *v 379 { 380 struct vm_area_struct *vma = vmf->vma; 381 struct mm_struct *mm = vma->vm_mm; 382 struct userfaultfd_ctx *ctx; 383 struct userfaultfd_wait_queue uwq; 384 vm_fault_t ret = VM_FAULT_SIGBUS; 385 bool must_wait; 386 unsigned int blocking_state; 387 388 /* 389 * We don't do userfault handling for 390 * 391 * We also don't do userfault handling 392 * coredumping. hugetlbfs has the spec 393 * hugetlb_follow_page_mask() to skip 394 * FOLL_DUMP case, anon memory also ch 395 * the no_page_table() helper in follo 396 * shmem_vm_ops->fault method is invok 397 * coredumping and it ends up here. 398 */ 399 if (current->flags & (PF_EXITING|PF_DU 400 goto out; 401 402 assert_fault_locked(vmf); 403 404 ctx = vma->vm_userfaultfd_ctx.ctx; 405 if (!ctx) 406 goto out; 407 408 BUG_ON(ctx->mm != mm); 409 410 /* Any unrecognized flag is a bug. */ 411 VM_BUG_ON(reason & ~__VM_UFFD_FLAGS); 412 /* 0 or > 1 flags set is a bug; we exp 413 VM_BUG_ON(!reason || (reason & (reason 414 415 if (ctx->features & UFFD_FEATURE_SIGBU 416 goto out; 417 if (!(vmf->flags & FAULT_FLAG_USER) && 418 goto out; 419 420 /* 421 * If it's already released don't get 422 * in __get_user_pages if userfaultfd_ 423 * caller of handle_userfault to relea 424 */ 425 if (unlikely(READ_ONCE(ctx->released)) 426 /* 427 * Don't return VM_FAULT_SIGBU 428 * cooperative manager can clo 429 * last UFFDIO_COPY, without r 430 * involuntary SIGBUS if the p 431 * userfaultfd while the userf 432 * (but after the last UFFDIO_ 433 * wasn't already closed when 434 * this point, that would norm 435 * userfaultfd_must_wait retur 436 * 437 * If we were to return VM_FAU 438 * cooperative manager would b 439 * always call UFFDIO_UNREGIST 440 * close the uffd. 441 */ 442 ret = VM_FAULT_NOPAGE; 443 goto out; 444 } 445 446 /* 447 * Check that we can return VM_FAULT_R 448 * 449 * NOTE: it should become possible to 450 * even if FAULT_FLAG_TRIED is set wit 451 * -EBUSY failures, if the userfaultfd 452 * VM_UFFD_WP tracking and we intend t 453 * without first stopping userland acc 454 * VM_UFFD_MISSING userfaults this is 455 */ 456 if (unlikely(!(vmf->flags & FAULT_FLAG 457 /* 458 * Validate the invariant that 459 * to be sure not to return SI 460 * nowait invocations. 461 */ 462 BUG_ON(vmf->flags & FAULT_FLAG 463 #ifdef CONFIG_DEBUG_VM 464 if (printk_ratelimit()) { 465 printk(KERN_WARNING 466 "FAULT_FLAG_ALL 467 vmf->flags); 468 dump_stack(); 469 } 470 #endif 471 goto out; 472 } 473 474 /* 475 * Handle nowait, not much to do other 476 * and wait. 477 */ 478 ret = VM_FAULT_RETRY; 479 if (vmf->flags & FAULT_FLAG_RETRY_NOWA 480 goto out; 481 482 /* take the reference before dropping 483 userfaultfd_ctx_get(ctx); 484 485 init_waitqueue_func_entry(&uwq.wq, use 486 uwq.wq.private = current; 487 uwq.msg = userfault_msg(vmf->address, 488 reason, ctx->f 489 uwq.ctx = ctx; 490 uwq.waken = false; 491 492 blocking_state = userfaultfd_get_block 493 494 /* 495 * Take the vma lock now, in order to 496 * userfaultfd_huge_must_wait() later. 497 * (sleepable) vma lock can modify the 498 * must be before explicitly calling s 499 */ 500 if (is_vm_hugetlb_page(vma)) 501 hugetlb_vma_lock_read(vma); 502 503 spin_lock_irq(&ctx->fault_pending_wqh. 504 /* 505 * After the __add_wait_queue the uwq 506 * through poll/read(). 507 */ 508 __add_wait_queue(&ctx->fault_pending_w 509 /* 510 * The smp_mb() after __set_current_st 511 * following the spin_unlock to happen 512 * __add_wait_queue. 513 */ 514 set_current_state(blocking_state); 515 spin_unlock_irq(&ctx->fault_pending_wq 516 517 if (!is_vm_hugetlb_page(vma)) 518 must_wait = userfaultfd_must_w 519 else 520 must_wait = userfaultfd_huge_m 521 if (is_vm_hugetlb_page(vma)) 522 hugetlb_vma_unlock_read(vma); 523 release_fault_lock(vmf); 524 525 if (likely(must_wait && !READ_ONCE(ctx 526 wake_up_poll(&ctx->fd_wqh, EPO 527 schedule(); 528 } 529 530 __set_current_state(TASK_RUNNING); 531 532 /* 533 * Here we race with the list_del; lis 534 * userfaultfd_ctx_read(), however bec 535 * list_del_init() to refile across th 536 * and next pointers will never point 537 * would never let any of the two poin 538 * self. So list_empty_careful won't r 539 * pointing to self at any time during 540 * only case where list_del_init() is 541 * removal in the wake function and th 542 * and it's fine not to block on the s 543 * kernel stack can be released after 544 */ 545 if (!list_empty_careful(&uwq.wq.entry) 546 spin_lock_irq(&ctx->fault_pend 547 /* 548 * No need of list_del_init(), 549 * will be freed shortly anywa 550 */ 551 list_del(&uwq.wq.entry); 552 spin_unlock_irq(&ctx->fault_pe 553 } 554 555 /* 556 * ctx may go away after this if the u 557 * already released. 558 */ 559 userfaultfd_ctx_put(ctx); 560 561 out: 562 return ret; 563 } 564 565 static void userfaultfd_event_wait_completion( 566 567 { 568 struct userfaultfd_ctx *release_new_ct 569 570 if (WARN_ON_ONCE(current->flags & PF_E 571 goto out; 572 573 ewq->ctx = ctx; 574 init_waitqueue_entry(&ewq->wq, current 575 release_new_ctx = NULL; 576 577 spin_lock_irq(&ctx->event_wqh.lock); 578 /* 579 * After the __add_wait_queue the uwq 580 * through poll/read(). 581 */ 582 __add_wait_queue(&ctx->event_wqh, &ewq 583 for (;;) { 584 set_current_state(TASK_KILLABL 585 if (ewq->msg.event == 0) 586 break; 587 if (READ_ONCE(ctx->released) | 588 fatal_signal_pending(curre 589 /* 590 * &ewq->wq may be que 591 * __remove_wait_queue 592 * parameter. It would 593 * didn't. 594 */ 595 __remove_wait_queue(&c 596 if (ewq->msg.event == 597 struct userfau 598 599 new = (struct 600 (unsig 601 ewq->m 602 release_new_ct 603 } 604 break; 605 } 606 607 spin_unlock_irq(&ctx->event_wq 608 609 wake_up_poll(&ctx->fd_wqh, EPO 610 schedule(); 611 612 spin_lock_irq(&ctx->event_wqh. 613 } 614 __set_current_state(TASK_RUNNING); 615 spin_unlock_irq(&ctx->event_wqh.lock); 616 617 if (release_new_ctx) { 618 struct vm_area_struct *vma; 619 struct mm_struct *mm = release 620 VMA_ITERATOR(vmi, mm, 0); 621 622 /* the various vma->vm_userfau 623 mmap_write_lock(mm); 624 for_each_vma(vmi, vma) { 625 if (vma->vm_userfaultf 626 vma_start_writ 627 vma->vm_userfa 628 userfaultfd_se 629 630 } 631 } 632 mmap_write_unlock(mm); 633 634 userfaultfd_ctx_put(release_ne 635 } 636 637 /* 638 * ctx may go away after this if the u 639 * already released. 640 */ 641 out: 642 atomic_dec(&ctx->mmap_changing); 643 VM_BUG_ON(atomic_read(&ctx->mmap_chang 644 userfaultfd_ctx_put(ctx); 645 } 646 647 static void userfaultfd_event_complete(struct 648 struct 649 { 650 ewq->msg.event = 0; 651 wake_up_locked(&ctx->event_wqh); 652 __remove_wait_queue(&ctx->event_wqh, & 653 } 654 655 int dup_userfaultfd(struct vm_area_struct *vma 656 { 657 struct userfaultfd_ctx *ctx = NULL, *o 658 struct userfaultfd_fork_ctx *fctx; 659 660 octx = vma->vm_userfaultfd_ctx.ctx; 661 if (!octx) 662 return 0; 663 664 if (!(octx->features & UFFD_FEATURE_EV 665 vma_start_write(vma); 666 vma->vm_userfaultfd_ctx = NULL 667 userfaultfd_set_vm_flags(vma, 668 return 0; 669 } 670 671 list_for_each_entry(fctx, fcs, list) 672 if (fctx->orig == octx) { 673 ctx = fctx->new; 674 break; 675 } 676 677 if (!ctx) { 678 fctx = kmalloc(sizeof(*fctx), 679 if (!fctx) 680 return -ENOMEM; 681 682 ctx = kmem_cache_alloc(userfau 683 if (!ctx) { 684 kfree(fctx); 685 return -ENOMEM; 686 } 687 688 refcount_set(&ctx->refcount, 1 689 ctx->flags = octx->flags; 690 ctx->features = octx->features 691 ctx->released = false; 692 init_rwsem(&ctx->map_changing_ 693 atomic_set(&ctx->mmap_changing 694 ctx->mm = vma->vm_mm; 695 mmgrab(ctx->mm); 696 697 userfaultfd_ctx_get(octx); 698 down_write(&octx->map_changing 699 atomic_inc(&octx->mmap_changin 700 up_write(&octx->map_changing_l 701 fctx->orig = octx; 702 fctx->new = ctx; 703 list_add_tail(&fctx->list, fcs 704 } 705 706 vma->vm_userfaultfd_ctx.ctx = ctx; 707 return 0; 708 } 709 710 static void dup_fctx(struct userfaultfd_fork_c 711 { 712 struct userfaultfd_ctx *ctx = fctx->or 713 struct userfaultfd_wait_queue ewq; 714 715 msg_init(&ewq.msg); 716 717 ewq.msg.event = UFFD_EVENT_FORK; 718 ewq.msg.arg.reserved.reserved1 = (unsi 719 720 userfaultfd_event_wait_completion(ctx, 721 } 722 723 void dup_userfaultfd_complete(struct list_head 724 { 725 struct userfaultfd_fork_ctx *fctx, *n; 726 727 list_for_each_entry_safe(fctx, n, fcs, 728 dup_fctx(fctx); 729 list_del(&fctx->list); 730 kfree(fctx); 731 } 732 } 733 734 void mremap_userfaultfd_prep(struct vm_area_st 735 struct vm_userfau 736 { 737 struct userfaultfd_ctx *ctx; 738 739 ctx = vma->vm_userfaultfd_ctx.ctx; 740 741 if (!ctx) 742 return; 743 744 if (ctx->features & UFFD_FEATURE_EVENT 745 vm_ctx->ctx = ctx; 746 userfaultfd_ctx_get(ctx); 747 down_write(&ctx->map_changing_ 748 atomic_inc(&ctx->mmap_changing 749 up_write(&ctx->map_changing_lo 750 } else { 751 /* Drop uffd context if remap 752 vma_start_write(vma); 753 vma->vm_userfaultfd_ctx = NULL 754 userfaultfd_set_vm_flags(vma, 755 } 756 } 757 758 void mremap_userfaultfd_complete(struct vm_use 759 unsigned long 760 unsigned long 761 { 762 struct userfaultfd_ctx *ctx = vm_ctx-> 763 struct userfaultfd_wait_queue ewq; 764 765 if (!ctx) 766 return; 767 768 if (to & ~PAGE_MASK) { 769 userfaultfd_ctx_put(ctx); 770 return; 771 } 772 773 msg_init(&ewq.msg); 774 775 ewq.msg.event = UFFD_EVENT_REMAP; 776 ewq.msg.arg.remap.from = from; 777 ewq.msg.arg.remap.to = to; 778 ewq.msg.arg.remap.len = len; 779 780 userfaultfd_event_wait_completion(ctx, 781 } 782 783 bool userfaultfd_remove(struct vm_area_struct 784 unsigned long start, u 785 { 786 struct mm_struct *mm = vma->vm_mm; 787 struct userfaultfd_ctx *ctx; 788 struct userfaultfd_wait_queue ewq; 789 790 ctx = vma->vm_userfaultfd_ctx.ctx; 791 if (!ctx || !(ctx->features & UFFD_FEA 792 return true; 793 794 userfaultfd_ctx_get(ctx); 795 down_write(&ctx->map_changing_lock); 796 atomic_inc(&ctx->mmap_changing); 797 up_write(&ctx->map_changing_lock); 798 mmap_read_unlock(mm); 799 800 msg_init(&ewq.msg); 801 802 ewq.msg.event = UFFD_EVENT_REMOVE; 803 ewq.msg.arg.remove.start = start; 804 ewq.msg.arg.remove.end = end; 805 806 userfaultfd_event_wait_completion(ctx, 807 808 return false; 809 } 810 811 static bool has_unmap_ctx(struct userfaultfd_c 812 unsigned long start, 813 { 814 struct userfaultfd_unmap_ctx *unmap_ct 815 816 list_for_each_entry(unmap_ctx, unmaps, 817 if (unmap_ctx->ctx == ctx && u 818 unmap_ctx->end == end) 819 return true; 820 821 return false; 822 } 823 824 int userfaultfd_unmap_prep(struct vm_area_stru 825 unsigned long end, 826 { 827 struct userfaultfd_unmap_ctx *unmap_ct 828 struct userfaultfd_ctx *ctx = vma->vm_ 829 830 if (!ctx || !(ctx->features & UFFD_FEA 831 has_unmap_ctx(ctx, unmaps, start, 832 return 0; 833 834 unmap_ctx = kzalloc(sizeof(*unmap_ctx) 835 if (!unmap_ctx) 836 return -ENOMEM; 837 838 userfaultfd_ctx_get(ctx); 839 down_write(&ctx->map_changing_lock); 840 atomic_inc(&ctx->mmap_changing); 841 up_write(&ctx->map_changing_lock); 842 unmap_ctx->ctx = ctx; 843 unmap_ctx->start = start; 844 unmap_ctx->end = end; 845 list_add_tail(&unmap_ctx->list, unmaps 846 847 return 0; 848 } 849 850 void userfaultfd_unmap_complete(struct mm_stru 851 { 852 struct userfaultfd_unmap_ctx *ctx, *n; 853 struct userfaultfd_wait_queue ewq; 854 855 list_for_each_entry_safe(ctx, n, uf, l 856 msg_init(&ewq.msg); 857 858 ewq.msg.event = UFFD_EVENT_UNM 859 ewq.msg.arg.remove.start = ctx 860 ewq.msg.arg.remove.end = ctx-> 861 862 userfaultfd_event_wait_complet 863 864 list_del(&ctx->list); 865 kfree(ctx); 866 } 867 } 868 869 static int userfaultfd_release(struct inode *i 870 { 871 struct userfaultfd_ctx *ctx = file->pr 872 struct mm_struct *mm = ctx->mm; 873 struct vm_area_struct *vma, *prev; 874 /* len == 0 means wake all */ 875 struct userfaultfd_wake_range range = 876 unsigned long new_flags; 877 VMA_ITERATOR(vmi, mm, 0); 878 879 WRITE_ONCE(ctx->released, true); 880 881 if (!mmget_not_zero(mm)) 882 goto wakeup; 883 884 /* 885 * Flush page faults out of all CPUs. 886 * must be retried without returning V 887 * userfaultfd_ctx_get() succeeds but 888 * changes while handle_userfault rele 889 * it's critical that released is set 890 * taking the mmap_lock for writing. 891 */ 892 mmap_write_lock(mm); 893 prev = NULL; 894 for_each_vma(vmi, vma) { 895 cond_resched(); 896 BUG_ON(!!vma->vm_userfaultfd_c 897 !!(vma->vm_flags & __VM 898 if (vma->vm_userfaultfd_ctx.ct 899 prev = vma; 900 continue; 901 } 902 /* Reset ptes for the whole vm 903 if (userfaultfd_wp(vma)) 904 uffd_wp_range(vma, vma 905 vma->vm_ 906 new_flags = vma->vm_flags & ~_ 907 vma = vma_modify_flags_uffd(&v 908 vm 909 NU 910 911 vma_start_write(vma); 912 userfaultfd_set_vm_flags(vma, 913 vma->vm_userfaultfd_ctx = NULL 914 915 prev = vma; 916 } 917 mmap_write_unlock(mm); 918 mmput(mm); 919 wakeup: 920 /* 921 * After no new page faults can wait o 922 * the last page faults that may have 923 * the fault_*wqh. 924 */ 925 spin_lock_irq(&ctx->fault_pending_wqh. 926 __wake_up_locked_key(&ctx->fault_pendi 927 __wake_up(&ctx->fault_wqh, TASK_NORMAL 928 spin_unlock_irq(&ctx->fault_pending_wq 929 930 /* Flush pending events that may still 931 wake_up_all(&ctx->event_wqh); 932 933 wake_up_poll(&ctx->fd_wqh, EPOLLHUP); 934 userfaultfd_ctx_put(ctx); 935 return 0; 936 } 937 938 /* fault_pending_wqh.lock must be hold by the 939 static inline struct userfaultfd_wait_queue *f 940 wait_queue_head_t *wqh) 941 { 942 wait_queue_entry_t *wq; 943 struct userfaultfd_wait_queue *uwq; 944 945 lockdep_assert_held(&wqh->lock); 946 947 uwq = NULL; 948 if (!waitqueue_active(wqh)) 949 goto out; 950 /* walk in reverse to provide FIFO beh 951 wq = list_last_entry(&wqh->head, typeo 952 uwq = container_of(wq, struct userfaul 953 out: 954 return uwq; 955 } 956 957 static inline struct userfaultfd_wait_queue *f 958 struct userfaultfd_ctx *ctx) 959 { 960 return find_userfault_in(&ctx->fault_p 961 } 962 963 static inline struct userfaultfd_wait_queue *f 964 struct userfaultfd_ctx *ctx) 965 { 966 return find_userfault_in(&ctx->event_w 967 } 968 969 static __poll_t userfaultfd_poll(struct file * 970 { 971 struct userfaultfd_ctx *ctx = file->pr 972 __poll_t ret; 973 974 poll_wait(file, &ctx->fd_wqh, wait); 975 976 if (!userfaultfd_is_initialized(ctx)) 977 return EPOLLERR; 978 979 /* 980 * poll() never guarantees that read w 981 * userfaults can be waken before they 982 */ 983 if (unlikely(!(file->f_flags & O_NONBL 984 return EPOLLERR; 985 /* 986 * lockless access to see if there are 987 * __pollwait last action is the add_w 988 * the spin_unlock would allow the wai 989 * pass above the actual list_add insi 990 * add_wait_queue critical section. So 991 * memory barrier to serialize the lis 992 * add_wait_queue() with the waitqueue 993 * below. 994 */ 995 ret = 0; 996 smp_mb(); 997 if (waitqueue_active(&ctx->fault_pendi 998 ret = EPOLLIN; 999 else if (waitqueue_active(&ctx->event_ 1000 ret = EPOLLIN; 1001 1002 return ret; 1003 } 1004 1005 static const struct file_operations userfault 1006 1007 static int resolve_userfault_fork(struct user 1008 struct inod 1009 struct uffd 1010 { 1011 int fd; 1012 1013 fd = anon_inode_create_getfd("[userfa 1014 O_RDONLY | (new->flag 1015 if (fd < 0) 1016 return fd; 1017 1018 msg->arg.reserved.reserved1 = 0; 1019 msg->arg.fork.ufd = fd; 1020 return 0; 1021 } 1022 1023 static ssize_t userfaultfd_ctx_read(struct us 1024 struct uf 1025 { 1026 ssize_t ret; 1027 DECLARE_WAITQUEUE(wait, current); 1028 struct userfaultfd_wait_queue *uwq; 1029 /* 1030 * Handling fork event requires sleep 1031 * we drop the event_wqh lock, then d 1032 * lock it back and wake up the waite 1033 * dropped the ewq may go away so we 1034 * carefully. 1035 */ 1036 LIST_HEAD(fork_event); 1037 struct userfaultfd_ctx *fork_nctx = N 1038 1039 /* always take the fd_wqh lock before 1040 spin_lock_irq(&ctx->fd_wqh.lock); 1041 __add_wait_queue(&ctx->fd_wqh, &wait) 1042 for (;;) { 1043 set_current_state(TASK_INTERR 1044 spin_lock(&ctx->fault_pending 1045 uwq = find_userfault(ctx); 1046 if (uwq) { 1047 /* 1048 * Use a seqcount to 1049 * in wake_userfault( 1050 * wakeups because du 1051 * waitqueue could be 1052 * only userfault. 1053 */ 1054 write_seqcount_begin( 1055 1056 /* 1057 * The fault_pending_ 1058 * to disappear from 1059 * 1060 * Refile this userfa 1061 * fault_pending_wqh 1062 * pending anymore af 1063 * 1064 * Use list_del() by 1065 * userfaultfd_wake_f 1066 * list_del_init() by 1067 * changes __remove_w 1068 * list_del_init() in 1069 * !list_empty_carefu 1070 * handle_userfault() 1071 * must never be empt 1072 * refile, or the wai 1073 * from under us. The 1074 * parameter of __rem 1075 * anyway. 1076 */ 1077 list_del(&uwq->wq.ent 1078 add_wait_queue(&ctx-> 1079 1080 write_seqcount_end(&c 1081 1082 /* careful to always 1083 *msg = uwq->msg; 1084 spin_unlock(&ctx->fau 1085 ret = 0; 1086 break; 1087 } 1088 spin_unlock(&ctx->fault_pendi 1089 1090 spin_lock(&ctx->event_wqh.loc 1091 uwq = find_userfault_evt(ctx) 1092 if (uwq) { 1093 *msg = uwq->msg; 1094 1095 if (uwq->msg.event == 1096 fork_nctx = ( 1097 (unsi 1098 uwq-> 1099 list_move(&uw 1100 /* 1101 * fork_nctx 1102 * we drop th 1103 * reference 1104 */ 1105 userfaultfd_c 1106 spin_unlock(& 1107 ret = 0; 1108 break; 1109 } 1110 1111 userfaultfd_event_com 1112 spin_unlock(&ctx->eve 1113 ret = 0; 1114 break; 1115 } 1116 spin_unlock(&ctx->event_wqh.l 1117 1118 if (signal_pending(current)) 1119 ret = -ERESTARTSYS; 1120 break; 1121 } 1122 if (no_wait) { 1123 ret = -EAGAIN; 1124 break; 1125 } 1126 spin_unlock_irq(&ctx->fd_wqh. 1127 schedule(); 1128 spin_lock_irq(&ctx->fd_wqh.lo 1129 } 1130 __remove_wait_queue(&ctx->fd_wqh, &wa 1131 __set_current_state(TASK_RUNNING); 1132 spin_unlock_irq(&ctx->fd_wqh.lock); 1133 1134 if (!ret && msg->event == UFFD_EVENT_ 1135 ret = resolve_userfault_fork( 1136 spin_lock_irq(&ctx->event_wqh 1137 if (!list_empty(&fork_event)) 1138 /* 1139 * The fork thread di 1140 * drop the temporary 1141 */ 1142 userfaultfd_ctx_put(f 1143 1144 uwq = list_first_entr 1145 1146 1147 /* 1148 * If fork_event list 1149 * the event wasn't a 1150 * (the event is allo 1151 * stack), put the ev 1152 * the event_wq. fork 1153 * as soon as we retu 1154 * stay queued there 1155 * "ret" value. 1156 */ 1157 list_del(&uwq->wq.ent 1158 __add_wait_queue(&ctx 1159 1160 /* 1161 * Leave the event in 1162 * error to userland 1163 * the userfault fork 1164 */ 1165 if (likely(!ret)) 1166 userfaultfd_e 1167 } else { 1168 /* 1169 * Here the fork thre 1170 * refcount from the 1171 * has already been r 1172 * the reference we t 1173 * lock above. If res 1174 * failed we've to dr 1175 * fork_nctx has to b 1176 * it succeeded we'll 1177 * uffd references it 1178 */ 1179 if (ret) 1180 userfaultfd_c 1181 } 1182 spin_unlock_irq(&ctx->event_w 1183 } 1184 1185 return ret; 1186 } 1187 1188 static ssize_t userfaultfd_read_iter(struct k 1189 { 1190 struct file *file = iocb->ki_filp; 1191 struct userfaultfd_ctx *ctx = file->p 1192 ssize_t _ret, ret = 0; 1193 struct uffd_msg msg; 1194 struct inode *inode = file_inode(file 1195 bool no_wait; 1196 1197 if (!userfaultfd_is_initialized(ctx)) 1198 return -EINVAL; 1199 1200 no_wait = file->f_flags & O_NONBLOCK 1201 for (;;) { 1202 if (iov_iter_count(to) < size 1203 return ret ? ret : -E 1204 _ret = userfaultfd_ctx_read(c 1205 if (_ret < 0) 1206 return ret ? ret : _r 1207 _ret = !copy_to_iter_full(&ms 1208 if (_ret) 1209 return ret ? ret : -E 1210 ret += sizeof(msg); 1211 /* 1212 * Allow to read more than on 1213 * block if waiting for the v 1214 */ 1215 no_wait = true; 1216 } 1217 } 1218 1219 static void __wake_userfault(struct userfault 1220 struct userfault 1221 { 1222 spin_lock_irq(&ctx->fault_pending_wqh 1223 /* wake all in the range and autoremo 1224 if (waitqueue_active(&ctx->fault_pend 1225 __wake_up_locked_key(&ctx->fa 1226 range); 1227 if (waitqueue_active(&ctx->fault_wqh) 1228 __wake_up(&ctx->fault_wqh, TA 1229 spin_unlock_irq(&ctx->fault_pending_w 1230 } 1231 1232 static __always_inline void wake_userfault(st 1233 st 1234 { 1235 unsigned seq; 1236 bool need_wakeup; 1237 1238 /* 1239 * To be sure waitqueue_active() is n 1240 * before the pagetable update, use a 1241 * barrier here. PT lock release or m 1242 * have release semantics that can al 1243 * waitqueue_active() to be reordered 1244 */ 1245 smp_mb(); 1246 1247 /* 1248 * Use waitqueue_active because it's 1249 * change the address space atomicall 1250 * userfaults yet. So we take the spi 1251 * sure we've userfaults to wake. 1252 */ 1253 do { 1254 seq = read_seqcount_begin(&ct 1255 need_wakeup = waitqueue_activ 1256 waitqueue_active(&ctx 1257 cond_resched(); 1258 } while (read_seqcount_retry(&ctx->re 1259 if (need_wakeup) 1260 __wake_userfault(ctx, range); 1261 } 1262 1263 static __always_inline int validate_unaligned 1264 struct mm_struct *mm, __u64 start, __ 1265 { 1266 __u64 task_size = mm->task_size; 1267 1268 if (len & ~PAGE_MASK) 1269 return -EINVAL; 1270 if (!len) 1271 return -EINVAL; 1272 if (start < mmap_min_addr) 1273 return -EINVAL; 1274 if (start >= task_size) 1275 return -EINVAL; 1276 if (len > task_size - start) 1277 return -EINVAL; 1278 if (start + len <= start) 1279 return -EINVAL; 1280 return 0; 1281 } 1282 1283 static __always_inline int validate_range(str 1284 __u 1285 { 1286 if (start & ~PAGE_MASK) 1287 return -EINVAL; 1288 1289 return validate_unaligned_range(mm, s 1290 } 1291 1292 static int userfaultfd_register(struct userfa 1293 unsigned long 1294 { 1295 struct mm_struct *mm = ctx->mm; 1296 struct vm_area_struct *vma, *prev, *c 1297 int ret; 1298 struct uffdio_register uffdio_registe 1299 struct uffdio_register __user *user_u 1300 unsigned long vm_flags, new_flags; 1301 bool found; 1302 bool basic_ioctls; 1303 unsigned long start, end, vma_end; 1304 struct vma_iterator vmi; 1305 bool wp_async = userfaultfd_wp_async_ 1306 1307 user_uffdio_register = (struct uffdio 1308 1309 ret = -EFAULT; 1310 if (copy_from_user(&uffdio_register, 1311 sizeof(uffdio_regi 1312 goto out; 1313 1314 ret = -EINVAL; 1315 if (!uffdio_register.mode) 1316 goto out; 1317 if (uffdio_register.mode & ~UFFD_API_ 1318 goto out; 1319 vm_flags = 0; 1320 if (uffdio_register.mode & UFFDIO_REG 1321 vm_flags |= VM_UFFD_MISSING; 1322 if (uffdio_register.mode & UFFDIO_REG 1323 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP 1324 goto out; 1325 #endif 1326 vm_flags |= VM_UFFD_WP; 1327 } 1328 if (uffdio_register.mode & UFFDIO_REG 1329 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 1330 goto out; 1331 #endif 1332 vm_flags |= VM_UFFD_MINOR; 1333 } 1334 1335 ret = validate_range(mm, uffdio_regis 1336 uffdio_register. 1337 if (ret) 1338 goto out; 1339 1340 start = uffdio_register.range.start; 1341 end = start + uffdio_register.range.l 1342 1343 ret = -ENOMEM; 1344 if (!mmget_not_zero(mm)) 1345 goto out; 1346 1347 ret = -EINVAL; 1348 mmap_write_lock(mm); 1349 vma_iter_init(&vmi, mm, start); 1350 vma = vma_find(&vmi, end); 1351 if (!vma) 1352 goto out_unlock; 1353 1354 /* 1355 * If the first vma contains huge pag 1356 * is aligned to huge page size. 1357 */ 1358 if (is_vm_hugetlb_page(vma)) { 1359 unsigned long vma_hpagesize = 1360 1361 if (start & (vma_hpagesize - 1362 goto out_unlock; 1363 } 1364 1365 /* 1366 * Search for not compatible vmas. 1367 */ 1368 found = false; 1369 basic_ioctls = false; 1370 cur = vma; 1371 do { 1372 cond_resched(); 1373 1374 BUG_ON(!!cur->vm_userfaultfd_ 1375 !!(cur->vm_flags & __V 1376 1377 /* check not compatible vmas 1378 ret = -EINVAL; 1379 if (!vma_can_userfault(cur, v 1380 goto out_unlock; 1381 1382 /* 1383 * UFFDIO_COPY will fill file 1384 * PROT_WRITE. This check enf 1385 * MAP_SHARED, the process ha 1386 * file. If VM_MAYWRITE is se 1387 * MAP_SHARED vma: there is n 1388 * F_WRITE_SEAL can be taken 1389 */ 1390 ret = -EPERM; 1391 if (unlikely(!(cur->vm_flags 1392 goto out_unlock; 1393 1394 /* 1395 * If this vma contains endin 1396 * check alignment. 1397 */ 1398 if (is_vm_hugetlb_page(cur) & 1399 end > cur->vm_start) { 1400 unsigned long vma_hpa 1401 1402 ret = -EINVAL; 1403 1404 if (end & (vma_hpages 1405 goto out_unlo 1406 } 1407 if ((vm_flags & VM_UFFD_WP) & 1408 goto out_unlock; 1409 1410 /* 1411 * Check that this vma isn't 1412 * different userfaultfd. We 1413 * userfaultfd to own a singl 1414 * wouldn't know which one to 1415 */ 1416 ret = -EBUSY; 1417 if (cur->vm_userfaultfd_ctx.c 1418 cur->vm_userfaultfd_ctx.c 1419 goto out_unlock; 1420 1421 /* 1422 * Note vmas containing huge 1423 */ 1424 if (is_vm_hugetlb_page(cur)) 1425 basic_ioctls = true; 1426 1427 found = true; 1428 } for_each_vma_range(vmi, cur, end); 1429 BUG_ON(!found); 1430 1431 vma_iter_set(&vmi, start); 1432 prev = vma_prev(&vmi); 1433 if (vma->vm_start < start) 1434 prev = vma; 1435 1436 ret = 0; 1437 for_each_vma_range(vmi, vma, end) { 1438 cond_resched(); 1439 1440 BUG_ON(!vma_can_userfault(vma 1441 BUG_ON(vma->vm_userfaultfd_ct 1442 vma->vm_userfaultfd_ct 1443 WARN_ON(!(vma->vm_flags & VM_ 1444 1445 /* 1446 * Nothing to do: this vma is 1447 * userfaultfd and with the r 1448 */ 1449 if (vma->vm_userfaultfd_ctx.c 1450 (vma->vm_flags & vm_flags 1451 goto skip; 1452 1453 if (vma->vm_start > start) 1454 start = vma->vm_start 1455 vma_end = min(end, vma->vm_en 1456 1457 new_flags = (vma->vm_flags & 1458 vma = vma_modify_flags_uffd(& 1459 n 1460 ( 1461 if (IS_ERR(vma)) { 1462 ret = PTR_ERR(vma); 1463 break; 1464 } 1465 1466 /* 1467 * In the vma_merge() success 1468 * the next vma was merged in 1469 * the current one has not be 1470 */ 1471 vma_start_write(vma); 1472 userfaultfd_set_vm_flags(vma, 1473 vma->vm_userfaultfd_ctx.ctx = 1474 1475 if (is_vm_hugetlb_page(vma) & 1476 hugetlb_unshare_all_p 1477 1478 skip: 1479 prev = vma; 1480 start = vma->vm_end; 1481 } 1482 1483 out_unlock: 1484 mmap_write_unlock(mm); 1485 mmput(mm); 1486 if (!ret) { 1487 __u64 ioctls_out; 1488 1489 ioctls_out = basic_ioctls ? U 1490 UFFD_API_RANGE_IOCTLS; 1491 1492 /* 1493 * Declare the WP ioctl only 1494 * specified and all checks p 1495 */ 1496 if (!(uffdio_register.mode & 1497 ioctls_out &= ~((__u6 1498 1499 /* CONTINUE ioctl is only sup 1500 if (!(uffdio_register.mode & 1501 ioctls_out &= ~((__u6 1502 1503 /* 1504 * Now that we scanned all vm 1505 * userland which ioctls meth 1506 * succeed on this range. 1507 */ 1508 if (put_user(ioctls_out, &use 1509 ret = -EFAULT; 1510 } 1511 out: 1512 return ret; 1513 } 1514 1515 static int userfaultfd_unregister(struct user 1516 unsigned lo 1517 { 1518 struct mm_struct *mm = ctx->mm; 1519 struct vm_area_struct *vma, *prev, *c 1520 int ret; 1521 struct uffdio_range uffdio_unregister 1522 unsigned long new_flags; 1523 bool found; 1524 unsigned long start, end, vma_end; 1525 const void __user *buf = (void __user 1526 struct vma_iterator vmi; 1527 bool wp_async = userfaultfd_wp_async_ 1528 1529 ret = -EFAULT; 1530 if (copy_from_user(&uffdio_unregister 1531 goto out; 1532 1533 ret = validate_range(mm, uffdio_unreg 1534 uffdio_unregiste 1535 if (ret) 1536 goto out; 1537 1538 start = uffdio_unregister.start; 1539 end = start + uffdio_unregister.len; 1540 1541 ret = -ENOMEM; 1542 if (!mmget_not_zero(mm)) 1543 goto out; 1544 1545 mmap_write_lock(mm); 1546 ret = -EINVAL; 1547 vma_iter_init(&vmi, mm, start); 1548 vma = vma_find(&vmi, end); 1549 if (!vma) 1550 goto out_unlock; 1551 1552 /* 1553 * If the first vma contains huge pag 1554 * is aligned to huge page size. 1555 */ 1556 if (is_vm_hugetlb_page(vma)) { 1557 unsigned long vma_hpagesize = 1558 1559 if (start & (vma_hpagesize - 1560 goto out_unlock; 1561 } 1562 1563 /* 1564 * Search for not compatible vmas. 1565 */ 1566 found = false; 1567 cur = vma; 1568 do { 1569 cond_resched(); 1570 1571 BUG_ON(!!cur->vm_userfaultfd_ 1572 !!(cur->vm_flags & __V 1573 1574 /* 1575 * Check not compatible vmas, 1576 * here as not compatible vma 1577 * userfaultfd_ctx registered 1578 * provides for more strict b 1579 * unregistration errors. 1580 */ 1581 if (!vma_can_userfault(cur, c 1582 goto out_unlock; 1583 1584 found = true; 1585 } for_each_vma_range(vmi, cur, end); 1586 BUG_ON(!found); 1587 1588 vma_iter_set(&vmi, start); 1589 prev = vma_prev(&vmi); 1590 if (vma->vm_start < start) 1591 prev = vma; 1592 1593 ret = 0; 1594 for_each_vma_range(vmi, vma, end) { 1595 cond_resched(); 1596 1597 BUG_ON(!vma_can_userfault(vma 1598 1599 /* 1600 * Nothing to do: this vma is 1601 * userfaultfd and with the r 1602 */ 1603 if (!vma->vm_userfaultfd_ctx. 1604 goto skip; 1605 1606 WARN_ON(!(vma->vm_flags & VM_ 1607 1608 if (vma->vm_start > start) 1609 start = vma->vm_start 1610 vma_end = min(end, vma->vm_en 1611 1612 if (userfaultfd_missing(vma)) 1613 /* 1614 * Wake any concurren 1615 * we unregister, so 1616 * permanently and it 1617 * UFFDIO_WAKE explic 1618 */ 1619 struct userfaultfd_wa 1620 range.start = start; 1621 range.len = vma_end - 1622 wake_userfault(vma->v 1623 } 1624 1625 /* Reset ptes for the whole v 1626 if (userfaultfd_wp(vma)) 1627 uffd_wp_range(vma, st 1628 1629 new_flags = vma->vm_flags & ~ 1630 vma = vma_modify_flags_uffd(& 1631 n 1632 if (IS_ERR(vma)) { 1633 ret = PTR_ERR(vma); 1634 break; 1635 } 1636 1637 /* 1638 * In the vma_merge() success 1639 * the next vma was merged in 1640 * the current one has not be 1641 */ 1642 vma_start_write(vma); 1643 userfaultfd_set_vm_flags(vma, 1644 vma->vm_userfaultfd_ctx = NUL 1645 1646 skip: 1647 prev = vma; 1648 start = vma->vm_end; 1649 } 1650 1651 out_unlock: 1652 mmap_write_unlock(mm); 1653 mmput(mm); 1654 out: 1655 return ret; 1656 } 1657 1658 /* 1659 * userfaultfd_wake may be used in combinatio 1660 * UFFDIO_*_MODE_DONTWAKE to wakeup userfault 1661 */ 1662 static int userfaultfd_wake(struct userfaultf 1663 unsigned long arg 1664 { 1665 int ret; 1666 struct uffdio_range uffdio_wake; 1667 struct userfaultfd_wake_range range; 1668 const void __user *buf = (void __user 1669 1670 ret = -EFAULT; 1671 if (copy_from_user(&uffdio_wake, buf, 1672 goto out; 1673 1674 ret = validate_range(ctx->mm, uffdio_ 1675 if (ret) 1676 goto out; 1677 1678 range.start = uffdio_wake.start; 1679 range.len = uffdio_wake.len; 1680 1681 /* 1682 * len == 0 means wake all and we don 1683 * so check it again to be sure. 1684 */ 1685 VM_BUG_ON(!range.len); 1686 1687 wake_userfault(ctx, &range); 1688 ret = 0; 1689 1690 out: 1691 return ret; 1692 } 1693 1694 static int userfaultfd_copy(struct userfaultf 1695 unsigned long arg 1696 { 1697 __s64 ret; 1698 struct uffdio_copy uffdio_copy; 1699 struct uffdio_copy __user *user_uffdi 1700 struct userfaultfd_wake_range range; 1701 uffd_flags_t flags = 0; 1702 1703 user_uffdio_copy = (struct uffdio_cop 1704 1705 ret = -EAGAIN; 1706 if (atomic_read(&ctx->mmap_changing)) 1707 goto out; 1708 1709 ret = -EFAULT; 1710 if (copy_from_user(&uffdio_copy, user 1711 /* don't copy "cop 1712 sizeof(uffdio_copy 1713 goto out; 1714 1715 ret = validate_unaligned_range(ctx->m 1716 uffdio 1717 if (ret) 1718 goto out; 1719 ret = validate_range(ctx->mm, uffdio_ 1720 if (ret) 1721 goto out; 1722 1723 ret = -EINVAL; 1724 if (uffdio_copy.mode & ~(UFFDIO_COPY_ 1725 goto out; 1726 if (uffdio_copy.mode & UFFDIO_COPY_MO 1727 flags |= MFILL_ATOMIC_WP; 1728 if (mmget_not_zero(ctx->mm)) { 1729 ret = mfill_atomic_copy(ctx, 1730 uffdi 1731 mmput(ctx->mm); 1732 } else { 1733 return -ESRCH; 1734 } 1735 if (unlikely(put_user(ret, &user_uffd 1736 return -EFAULT; 1737 if (ret < 0) 1738 goto out; 1739 BUG_ON(!ret); 1740 /* len == 0 would wake all */ 1741 range.len = ret; 1742 if (!(uffdio_copy.mode & UFFDIO_COPY_ 1743 range.start = uffdio_copy.dst 1744 wake_userfault(ctx, &range); 1745 } 1746 ret = range.len == uffdio_copy.len ? 1747 out: 1748 return ret; 1749 } 1750 1751 static int userfaultfd_zeropage(struct userfa 1752 unsigned long 1753 { 1754 __s64 ret; 1755 struct uffdio_zeropage uffdio_zeropag 1756 struct uffdio_zeropage __user *user_u 1757 struct userfaultfd_wake_range range; 1758 1759 user_uffdio_zeropage = (struct uffdio 1760 1761 ret = -EAGAIN; 1762 if (atomic_read(&ctx->mmap_changing)) 1763 goto out; 1764 1765 ret = -EFAULT; 1766 if (copy_from_user(&uffdio_zeropage, 1767 /* don't copy "zer 1768 sizeof(uffdio_zero 1769 goto out; 1770 1771 ret = validate_range(ctx->mm, uffdio_ 1772 uffdio_zeropage. 1773 if (ret) 1774 goto out; 1775 ret = -EINVAL; 1776 if (uffdio_zeropage.mode & ~UFFDIO_ZE 1777 goto out; 1778 1779 if (mmget_not_zero(ctx->mm)) { 1780 ret = mfill_atomic_zeropage(c 1781 uf 1782 mmput(ctx->mm); 1783 } else { 1784 return -ESRCH; 1785 } 1786 if (unlikely(put_user(ret, &user_uffd 1787 return -EFAULT; 1788 if (ret < 0) 1789 goto out; 1790 /* len == 0 would wake all */ 1791 BUG_ON(!ret); 1792 range.len = ret; 1793 if (!(uffdio_zeropage.mode & UFFDIO_Z 1794 range.start = uffdio_zeropage 1795 wake_userfault(ctx, &range); 1796 } 1797 ret = range.len == uffdio_zeropage.ra 1798 out: 1799 return ret; 1800 } 1801 1802 static int userfaultfd_writeprotect(struct us 1803 unsigned 1804 { 1805 int ret; 1806 struct uffdio_writeprotect uffdio_wp; 1807 struct uffdio_writeprotect __user *us 1808 struct userfaultfd_wake_range range; 1809 bool mode_wp, mode_dontwake; 1810 1811 if (atomic_read(&ctx->mmap_changing)) 1812 return -EAGAIN; 1813 1814 user_uffdio_wp = (struct uffdio_write 1815 1816 if (copy_from_user(&uffdio_wp, user_u 1817 sizeof(struct uffd 1818 return -EFAULT; 1819 1820 ret = validate_range(ctx->mm, uffdio_ 1821 uffdio_wp.range. 1822 if (ret) 1823 return ret; 1824 1825 if (uffdio_wp.mode & ~(UFFDIO_WRITEPR 1826 UFFDIO_WRITEPR 1827 return -EINVAL; 1828 1829 mode_wp = uffdio_wp.mode & UFFDIO_WRI 1830 mode_dontwake = uffdio_wp.mode & UFFD 1831 1832 if (mode_wp && mode_dontwake) 1833 return -EINVAL; 1834 1835 if (mmget_not_zero(ctx->mm)) { 1836 ret = mwriteprotect_range(ctx 1837 uff 1838 mmput(ctx->mm); 1839 } else { 1840 return -ESRCH; 1841 } 1842 1843 if (ret) 1844 return ret; 1845 1846 if (!mode_wp && !mode_dontwake) { 1847 range.start = uffdio_wp.range 1848 range.len = uffdio_wp.range.l 1849 wake_userfault(ctx, &range); 1850 } 1851 return ret; 1852 } 1853 1854 static int userfaultfd_continue(struct userfa 1855 { 1856 __s64 ret; 1857 struct uffdio_continue uffdio_continu 1858 struct uffdio_continue __user *user_u 1859 struct userfaultfd_wake_range range; 1860 uffd_flags_t flags = 0; 1861 1862 user_uffdio_continue = (struct uffdio 1863 1864 ret = -EAGAIN; 1865 if (atomic_read(&ctx->mmap_changing)) 1866 goto out; 1867 1868 ret = -EFAULT; 1869 if (copy_from_user(&uffdio_continue, 1870 /* don't copy the 1871 sizeof(uffdio_cont 1872 goto out; 1873 1874 ret = validate_range(ctx->mm, uffdio_ 1875 uffdio_continue. 1876 if (ret) 1877 goto out; 1878 1879 ret = -EINVAL; 1880 if (uffdio_continue.mode & ~(UFFDIO_C 1881 UFFDIO_C 1882 goto out; 1883 if (uffdio_continue.mode & UFFDIO_CON 1884 flags |= MFILL_ATOMIC_WP; 1885 1886 if (mmget_not_zero(ctx->mm)) { 1887 ret = mfill_atomic_continue(c 1888 u 1889 mmput(ctx->mm); 1890 } else { 1891 return -ESRCH; 1892 } 1893 1894 if (unlikely(put_user(ret, &user_uffd 1895 return -EFAULT; 1896 if (ret < 0) 1897 goto out; 1898 1899 /* len == 0 would wake all */ 1900 BUG_ON(!ret); 1901 range.len = ret; 1902 if (!(uffdio_continue.mode & UFFDIO_C 1903 range.start = uffdio_continue 1904 wake_userfault(ctx, &range); 1905 } 1906 ret = range.len == uffdio_continue.ra 1907 1908 out: 1909 return ret; 1910 } 1911 1912 static inline int userfaultfd_poison(struct u 1913 { 1914 __s64 ret; 1915 struct uffdio_poison uffdio_poison; 1916 struct uffdio_poison __user *user_uff 1917 struct userfaultfd_wake_range range; 1918 1919 user_uffdio_poison = (struct uffdio_p 1920 1921 ret = -EAGAIN; 1922 if (atomic_read(&ctx->mmap_changing)) 1923 goto out; 1924 1925 ret = -EFAULT; 1926 if (copy_from_user(&uffdio_poison, us 1927 /* don't copy the 1928 sizeof(uffdio_pois 1929 goto out; 1930 1931 ret = validate_range(ctx->mm, uffdio_ 1932 uffdio_poison.ra 1933 if (ret) 1934 goto out; 1935 1936 ret = -EINVAL; 1937 if (uffdio_poison.mode & ~UFFDIO_POIS 1938 goto out; 1939 1940 if (mmget_not_zero(ctx->mm)) { 1941 ret = mfill_atomic_poison(ctx 1942 uff 1943 mmput(ctx->mm); 1944 } else { 1945 return -ESRCH; 1946 } 1947 1948 if (unlikely(put_user(ret, &user_uffd 1949 return -EFAULT; 1950 if (ret < 0) 1951 goto out; 1952 1953 /* len == 0 would wake all */ 1954 BUG_ON(!ret); 1955 range.len = ret; 1956 if (!(uffdio_poison.mode & UFFDIO_POI 1957 range.start = uffdio_poison.r 1958 wake_userfault(ctx, &range); 1959 } 1960 ret = range.len == uffdio_poison.rang 1961 1962 out: 1963 return ret; 1964 } 1965 1966 bool userfaultfd_wp_async(struct vm_area_stru 1967 { 1968 return userfaultfd_wp_async_ctx(vma-> 1969 } 1970 1971 static inline unsigned int uffd_ctx_features( 1972 { 1973 /* 1974 * For the current set of features th 1975 * UFFD_FEATURE_INITIALIZED to mark t 1976 */ 1977 return (unsigned int)user_features | 1978 } 1979 1980 static int userfaultfd_move(struct userfaultf 1981 unsigned long arg 1982 { 1983 __s64 ret; 1984 struct uffdio_move uffdio_move; 1985 struct uffdio_move __user *user_uffdi 1986 struct userfaultfd_wake_range range; 1987 struct mm_struct *mm = ctx->mm; 1988 1989 user_uffdio_move = (struct uffdio_mov 1990 1991 if (atomic_read(&ctx->mmap_changing)) 1992 return -EAGAIN; 1993 1994 if (copy_from_user(&uffdio_move, user 1995 /* don't copy "mov 1996 sizeof(uffdio_move 1997 return -EFAULT; 1998 1999 /* Do not allow cross-mm moves. */ 2000 if (mm != current->mm) 2001 return -EINVAL; 2002 2003 ret = validate_range(mm, uffdio_move. 2004 if (ret) 2005 return ret; 2006 2007 ret = validate_range(mm, uffdio_move. 2008 if (ret) 2009 return ret; 2010 2011 if (uffdio_move.mode & ~(UFFDIO_MOVE_ 2012 UFFDIO_MOVE 2013 return -EINVAL; 2014 2015 if (mmget_not_zero(mm)) { 2016 ret = move_pages(ctx, uffdio_ 2017 uffdio_move. 2018 mmput(mm); 2019 } else { 2020 return -ESRCH; 2021 } 2022 2023 if (unlikely(put_user(ret, &user_uffd 2024 return -EFAULT; 2025 if (ret < 0) 2026 goto out; 2027 2028 /* len == 0 would wake all */ 2029 VM_WARN_ON(!ret); 2030 range.len = ret; 2031 if (!(uffdio_move.mode & UFFDIO_MOVE_ 2032 range.start = uffdio_move.dst 2033 wake_userfault(ctx, &range); 2034 } 2035 ret = range.len == uffdio_move.len ? 2036 2037 out: 2038 return ret; 2039 } 2040 2041 /* 2042 * userland asks for a certain API version an 2043 * and ioctl commands are implemented in this 2044 * version or -EINVAL if unknown. 2045 */ 2046 static int userfaultfd_api(struct userfaultfd 2047 unsigned long arg) 2048 { 2049 struct uffdio_api uffdio_api; 2050 void __user *buf = (void __user *)arg 2051 unsigned int ctx_features; 2052 int ret; 2053 __u64 features; 2054 2055 ret = -EFAULT; 2056 if (copy_from_user(&uffdio_api, buf, 2057 goto out; 2058 features = uffdio_api.features; 2059 ret = -EINVAL; 2060 if (uffdio_api.api != UFFD_API) 2061 goto err_out; 2062 ret = -EPERM; 2063 if ((features & UFFD_FEATURE_EVENT_FO 2064 goto err_out; 2065 2066 /* WP_ASYNC relies on WP_UNPOPULATED, 2067 if (features & UFFD_FEATURE_WP_ASYNC) 2068 features |= UFFD_FEATURE_WP_U 2069 2070 /* report all available features and 2071 uffdio_api.features = UFFD_API_FEATUR 2072 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 2073 uffdio_api.features &= 2074 ~(UFFD_FEATURE_MINOR_HUGETLBF 2075 #endif 2076 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP 2077 uffdio_api.features &= ~UFFD_FEATURE_ 2078 #endif 2079 #ifndef CONFIG_PTE_MARKER_UFFD_WP 2080 uffdio_api.features &= ~UFFD_FEATURE_ 2081 uffdio_api.features &= ~UFFD_FEATURE_ 2082 uffdio_api.features &= ~UFFD_FEATURE_ 2083 #endif 2084 2085 ret = -EINVAL; 2086 if (features & ~uffdio_api.features) 2087 goto err_out; 2088 2089 uffdio_api.ioctls = UFFD_API_IOCTLS; 2090 ret = -EFAULT; 2091 if (copy_to_user(buf, &uffdio_api, si 2092 goto out; 2093 2094 /* only enable the requested features 2095 ctx_features = uffd_ctx_features(feat 2096 ret = -EINVAL; 2097 if (cmpxchg(&ctx->features, 0, ctx_fe 2098 goto err_out; 2099 2100 ret = 0; 2101 out: 2102 return ret; 2103 err_out: 2104 memset(&uffdio_api, 0, sizeof(uffdio_ 2105 if (copy_to_user(buf, &uffdio_api, si 2106 ret = -EFAULT; 2107 goto out; 2108 } 2109 2110 static long userfaultfd_ioctl(struct file *fi 2111 unsigned long a 2112 { 2113 int ret = -EINVAL; 2114 struct userfaultfd_ctx *ctx = file->p 2115 2116 if (cmd != UFFDIO_API && !userfaultfd 2117 return -EINVAL; 2118 2119 switch(cmd) { 2120 case UFFDIO_API: 2121 ret = userfaultfd_api(ctx, ar 2122 break; 2123 case UFFDIO_REGISTER: 2124 ret = userfaultfd_register(ct 2125 break; 2126 case UFFDIO_UNREGISTER: 2127 ret = userfaultfd_unregister( 2128 break; 2129 case UFFDIO_WAKE: 2130 ret = userfaultfd_wake(ctx, a 2131 break; 2132 case UFFDIO_COPY: 2133 ret = userfaultfd_copy(ctx, a 2134 break; 2135 case UFFDIO_ZEROPAGE: 2136 ret = userfaultfd_zeropage(ct 2137 break; 2138 case UFFDIO_MOVE: 2139 ret = userfaultfd_move(ctx, a 2140 break; 2141 case UFFDIO_WRITEPROTECT: 2142 ret = userfaultfd_writeprotec 2143 break; 2144 case UFFDIO_CONTINUE: 2145 ret = userfaultfd_continue(ct 2146 break; 2147 case UFFDIO_POISON: 2148 ret = userfaultfd_poison(ctx, 2149 break; 2150 } 2151 return ret; 2152 } 2153 2154 #ifdef CONFIG_PROC_FS 2155 static void userfaultfd_show_fdinfo(struct se 2156 { 2157 struct userfaultfd_ctx *ctx = f->priv 2158 wait_queue_entry_t *wq; 2159 unsigned long pending = 0, total = 0; 2160 2161 spin_lock_irq(&ctx->fault_pending_wqh 2162 list_for_each_entry(wq, &ctx->fault_p 2163 pending++; 2164 total++; 2165 } 2166 list_for_each_entry(wq, &ctx->fault_w 2167 total++; 2168 } 2169 spin_unlock_irq(&ctx->fault_pending_w 2170 2171 /* 2172 * If more protocols will be added, t 2173 * separated by a space. Like this: 2174 * protocols: aa:... bb:... 2175 */ 2176 seq_printf(m, "pending:\t%lu\ntotal:\ 2177 pending, total, UFFD_API, 2178 UFFD_API_IOCTLS|UFFD_API_R 2179 } 2180 #endif 2181 2182 static const struct file_operations userfault 2183 #ifdef CONFIG_PROC_FS 2184 .show_fdinfo = userfaultfd_show_fd 2185 #endif 2186 .release = userfaultfd_release 2187 .poll = userfaultfd_poll, 2188 .read_iter = userfaultfd_read_it 2189 .unlocked_ioctl = userfaultfd_ioctl, 2190 .compat_ioctl = compat_ptr_ioctl, 2191 .llseek = noop_llseek, 2192 }; 2193 2194 static void init_once_userfaultfd_ctx(void *m 2195 { 2196 struct userfaultfd_ctx *ctx = (struct 2197 2198 init_waitqueue_head(&ctx->fault_pendi 2199 init_waitqueue_head(&ctx->fault_wqh); 2200 init_waitqueue_head(&ctx->event_wqh); 2201 init_waitqueue_head(&ctx->fd_wqh); 2202 seqcount_spinlock_init(&ctx->refile_s 2203 } 2204 2205 static int new_userfaultfd(int flags) 2206 { 2207 struct userfaultfd_ctx *ctx; 2208 struct file *file; 2209 int fd; 2210 2211 BUG_ON(!current->mm); 2212 2213 /* Check the UFFD_* constants for con 2214 BUILD_BUG_ON(UFFD_USER_MODE_ONLY & UF 2215 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXE 2216 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBL 2217 2218 if (flags & ~(UFFD_SHARED_FCNTL_FLAGS 2219 return -EINVAL; 2220 2221 ctx = kmem_cache_alloc(userfaultfd_ct 2222 if (!ctx) 2223 return -ENOMEM; 2224 2225 refcount_set(&ctx->refcount, 1); 2226 ctx->flags = flags; 2227 ctx->features = 0; 2228 ctx->released = false; 2229 init_rwsem(&ctx->map_changing_lock); 2230 atomic_set(&ctx->mmap_changing, 0); 2231 ctx->mm = current->mm; 2232 2233 fd = get_unused_fd_flags(flags & UFFD 2234 if (fd < 0) 2235 goto err_out; 2236 2237 /* Create a new inode so that the LSM 2238 file = anon_inode_create_getfile("[us 2239 O_RDONLY | (flags & U 2240 if (IS_ERR(file)) { 2241 put_unused_fd(fd); 2242 fd = PTR_ERR(file); 2243 goto err_out; 2244 } 2245 /* prevent the mm struct to be freed 2246 mmgrab(ctx->mm); 2247 file->f_mode |= FMODE_NOWAIT; 2248 fd_install(fd, file); 2249 return fd; 2250 err_out: 2251 kmem_cache_free(userfaultfd_ctx_cache 2252 return fd; 2253 } 2254 2255 static inline bool userfaultfd_syscall_allowe 2256 { 2257 /* Userspace-only page faults are alw 2258 if (flags & UFFD_USER_MODE_ONLY) 2259 return true; 2260 2261 /* 2262 * The user is requesting a userfault 2263 * Privileged users are always allowe 2264 */ 2265 if (capable(CAP_SYS_PTRACE)) 2266 return true; 2267 2268 /* Otherwise, access to kernel fault 2269 return sysctl_unprivileged_userfaultf 2270 } 2271 2272 SYSCALL_DEFINE1(userfaultfd, int, flags) 2273 { 2274 if (!userfaultfd_syscall_allowed(flag 2275 return -EPERM; 2276 2277 return new_userfaultfd(flags); 2278 } 2279 2280 static long userfaultfd_dev_ioctl(struct file 2281 { 2282 if (cmd != USERFAULTFD_IOC_NEW) 2283 return -EINVAL; 2284 2285 return new_userfaultfd(flags); 2286 } 2287 2288 static const struct file_operations userfault 2289 .unlocked_ioctl = userfaultfd_dev_ioc 2290 .compat_ioctl = userfaultfd_dev_ioctl 2291 .owner = THIS_MODULE, 2292 .llseek = noop_llseek, 2293 }; 2294 2295 static struct miscdevice userfaultfd_misc = { 2296 .minor = MISC_DYNAMIC_MINOR, 2297 .name = "userfaultfd", 2298 .fops = &userfaultfd_dev_fops 2299 }; 2300 2301 static int __init userfaultfd_init(void) 2302 { 2303 int ret; 2304 2305 ret = misc_register(&userfaultfd_misc 2306 if (ret) 2307 return ret; 2308 2309 userfaultfd_ctx_cachep = kmem_cache_c 2310 2311 2312 2313 2314 #ifdef CONFIG_SYSCTL 2315 register_sysctl_init("vm", vm_userfau 2316 #endif 2317 return 0; 2318 } 2319 __initcall(userfaultfd_init); 2320
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.