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