4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
5 * Copyright (C) 2008-2009 Red Hat, Inc.
6 * Copyright (C) 2015 Red Hat, Inc.
8 * This work is licensed under the terms of the GNU GPL, version 2. See
9 * the COPYING file in the top-level directory.
11 * Some part derived from fs/eventfd.c (anon inode setup) and
12 * mm/ksm.c (mm hashing).
15 #include <linux/list.h>
16 #include <linux/hashtable.h>
17 #include <linux/sched.h>
19 #include <linux/poll.h>
20 #include <linux/slab.h>
21 #include <linux/seq_file.h>
22 #include <linux/file.h>
23 #include <linux/bug.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/syscalls.h>
26 #include <linux/userfaultfd_k.h>
27 #include <linux/mempolicy.h>
28 #include <linux/ioctl.h>
29 #include <linux/security.h>
30 #include <linux/hugetlb.h>
32 static struct kmem_cache
*userfaultfd_ctx_cachep __read_mostly
;
34 enum userfaultfd_state
{
40 * Start with fault_pending_wqh and fault_wqh so they're more likely
41 * to be in the same cacheline.
43 struct userfaultfd_ctx
{
44 /* waitqueue head for the pending (i.e. not read) userfaults */
45 wait_queue_head_t fault_pending_wqh
;
46 /* waitqueue head for the userfaults */
47 wait_queue_head_t fault_wqh
;
48 /* waitqueue head for the pseudo fd to wakeup poll/read */
49 wait_queue_head_t fd_wqh
;
50 /* waitqueue head for events */
51 wait_queue_head_t event_wqh
;
52 /* a refile sequence protected by fault_pending_wqh lock */
53 struct seqcount refile_seq
;
54 /* pseudo fd refcounting */
56 /* userfaultfd syscall flags */
58 /* features requested from the userspace */
59 unsigned int features
;
61 enum userfaultfd_state state
;
64 /* mm with one ore more vmas attached to this userfaultfd_ctx */
68 struct userfaultfd_fork_ctx
{
69 struct userfaultfd_ctx
*orig
;
70 struct userfaultfd_ctx
*new;
71 struct list_head list
;
74 struct userfaultfd_unmap_ctx
{
75 struct userfaultfd_ctx
*ctx
;
78 struct list_head list
;
81 struct userfaultfd_wait_queue
{
84 struct userfaultfd_ctx
*ctx
;
88 struct userfaultfd_wake_range
{
93 static int userfaultfd_wake_function(wait_queue_t
*wq
, unsigned mode
,
94 int wake_flags
, void *key
)
96 struct userfaultfd_wake_range
*range
= key
;
98 struct userfaultfd_wait_queue
*uwq
;
99 unsigned long start
, len
;
101 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
103 /* len == 0 means wake all */
104 start
= range
->start
;
106 if (len
&& (start
> uwq
->msg
.arg
.pagefault
.address
||
107 start
+ len
<= uwq
->msg
.arg
.pagefault
.address
))
109 WRITE_ONCE(uwq
->waken
, true);
111 * The implicit smp_mb__before_spinlock in try_to_wake_up()
112 * renders uwq->waken visible to other CPUs before the task is
115 ret
= wake_up_state(wq
->private, mode
);
118 * Wake only once, autoremove behavior.
120 * After the effect of list_del_init is visible to the
121 * other CPUs, the waitqueue may disappear from under
122 * us, see the !list_empty_careful() in
123 * handle_userfault(). try_to_wake_up() has an
124 * implicit smp_mb__before_spinlock, and the
125 * wq->private is read before calling the extern
126 * function "wake_up_state" (which in turns calls
127 * try_to_wake_up). While the spin_lock;spin_unlock;
128 * wouldn't be enough, the smp_mb__before_spinlock is
129 * enough to avoid an explicit smp_mb() here.
131 list_del_init(&wq
->task_list
);
137 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
139 * @ctx: [in] Pointer to the userfaultfd context.
141 * Returns: In case of success, returns not zero.
143 static void userfaultfd_ctx_get(struct userfaultfd_ctx
*ctx
)
145 if (!atomic_inc_not_zero(&ctx
->refcount
))
150 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
152 * @ctx: [in] Pointer to userfaultfd context.
154 * The userfaultfd context reference must have been previously acquired either
155 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
157 static void userfaultfd_ctx_put(struct userfaultfd_ctx
*ctx
)
159 if (atomic_dec_and_test(&ctx
->refcount
)) {
160 VM_BUG_ON(spin_is_locked(&ctx
->fault_pending_wqh
.lock
));
161 VM_BUG_ON(waitqueue_active(&ctx
->fault_pending_wqh
));
162 VM_BUG_ON(spin_is_locked(&ctx
->fault_wqh
.lock
));
163 VM_BUG_ON(waitqueue_active(&ctx
->fault_wqh
));
164 VM_BUG_ON(spin_is_locked(&ctx
->event_wqh
.lock
));
165 VM_BUG_ON(waitqueue_active(&ctx
->event_wqh
));
166 VM_BUG_ON(spin_is_locked(&ctx
->fd_wqh
.lock
));
167 VM_BUG_ON(waitqueue_active(&ctx
->fd_wqh
));
169 kmem_cache_free(userfaultfd_ctx_cachep
, ctx
);
173 static inline void msg_init(struct uffd_msg
*msg
)
175 BUILD_BUG_ON(sizeof(struct uffd_msg
) != 32);
177 * Must use memset to zero out the paddings or kernel data is
178 * leaked to userland.
180 memset(msg
, 0, sizeof(struct uffd_msg
));
183 static inline struct uffd_msg
userfault_msg(unsigned long address
,
185 unsigned long reason
)
189 msg
.event
= UFFD_EVENT_PAGEFAULT
;
190 msg
.arg
.pagefault
.address
= address
;
191 if (flags
& FAULT_FLAG_WRITE
)
193 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
194 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
195 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
196 * was a read fault, otherwise if set it means it's
199 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WRITE
;
200 if (reason
& VM_UFFD_WP
)
202 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
203 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
204 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
205 * a missing fault, otherwise if set it means it's a
206 * write protect fault.
208 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WP
;
212 #ifdef CONFIG_HUGETLB_PAGE
214 * Same functionality as userfaultfd_must_wait below with modifications for
217 static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx
*ctx
,
218 unsigned long address
,
220 unsigned long reason
)
222 struct mm_struct
*mm
= ctx
->mm
;
226 VM_BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
228 pte
= huge_pte_offset(mm
, address
);
235 * Lockless access: we're in a wait_event so it's ok if it
238 if (huge_pte_none(*pte
))
240 if (!huge_pte_write(*pte
) && (reason
& VM_UFFD_WP
))
246 static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx
*ctx
,
247 unsigned long address
,
249 unsigned long reason
)
251 return false; /* should never get here */
253 #endif /* CONFIG_HUGETLB_PAGE */
256 * Verify the pagetables are still not ok after having reigstered into
257 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
258 * userfault that has already been resolved, if userfaultfd_read and
259 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
262 static inline bool userfaultfd_must_wait(struct userfaultfd_ctx
*ctx
,
263 unsigned long address
,
265 unsigned long reason
)
267 struct mm_struct
*mm
= ctx
->mm
;
274 VM_BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
276 pgd
= pgd_offset(mm
, address
);
277 if (!pgd_present(*pgd
))
279 pud
= pud_offset(pgd
, address
);
280 if (!pud_present(*pud
))
282 pmd
= pmd_offset(pud
, address
);
284 * READ_ONCE must function as a barrier with narrower scope
285 * and it must be equivalent to:
286 * _pmd = *pmd; barrier();
288 * This is to deal with the instability (as in
289 * pmd_trans_unstable) of the pmd.
291 _pmd
= READ_ONCE(*pmd
);
292 if (!pmd_present(_pmd
))
296 if (pmd_trans_huge(_pmd
))
300 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
301 * and use the standard pte_offset_map() instead of parsing _pmd.
303 pte
= pte_offset_map(pmd
, address
);
305 * Lockless access: we're in a wait_event so it's ok if it
317 * The locking rules involved in returning VM_FAULT_RETRY depending on
318 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
319 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
320 * recommendation in __lock_page_or_retry is not an understatement.
322 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
323 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
326 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
327 * set, VM_FAULT_RETRY can still be returned if and only if there are
328 * fatal_signal_pending()s, and the mmap_sem must be released before
331 int handle_userfault(struct vm_fault
*vmf
, unsigned long reason
)
333 struct mm_struct
*mm
= vmf
->vma
->vm_mm
;
334 struct userfaultfd_ctx
*ctx
;
335 struct userfaultfd_wait_queue uwq
;
337 bool must_wait
, return_to_userland
;
340 BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
342 ret
= VM_FAULT_SIGBUS
;
343 ctx
= vmf
->vma
->vm_userfaultfd_ctx
.ctx
;
347 BUG_ON(ctx
->mm
!= mm
);
349 VM_BUG_ON(reason
& ~(VM_UFFD_MISSING
|VM_UFFD_WP
));
350 VM_BUG_ON(!(reason
& VM_UFFD_MISSING
) ^ !!(reason
& VM_UFFD_WP
));
353 * If it's already released don't get it. This avoids to loop
354 * in __get_user_pages if userfaultfd_release waits on the
355 * caller of handle_userfault to release the mmap_sem.
357 if (unlikely(ACCESS_ONCE(ctx
->released
)))
361 * We don't do userfault handling for the final child pid update.
363 if (current
->flags
& PF_EXITING
)
367 * Check that we can return VM_FAULT_RETRY.
369 * NOTE: it should become possible to return VM_FAULT_RETRY
370 * even if FAULT_FLAG_TRIED is set without leading to gup()
371 * -EBUSY failures, if the userfaultfd is to be extended for
372 * VM_UFFD_WP tracking and we intend to arm the userfault
373 * without first stopping userland access to the memory. For
374 * VM_UFFD_MISSING userfaults this is enough for now.
376 if (unlikely(!(vmf
->flags
& FAULT_FLAG_ALLOW_RETRY
))) {
378 * Validate the invariant that nowait must allow retry
379 * to be sure not to return SIGBUS erroneously on
380 * nowait invocations.
382 BUG_ON(vmf
->flags
& FAULT_FLAG_RETRY_NOWAIT
);
383 #ifdef CONFIG_DEBUG_VM
384 if (printk_ratelimit()) {
386 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
395 * Handle nowait, not much to do other than tell it to retry
398 ret
= VM_FAULT_RETRY
;
399 if (vmf
->flags
& FAULT_FLAG_RETRY_NOWAIT
)
402 /* take the reference before dropping the mmap_sem */
403 userfaultfd_ctx_get(ctx
);
405 init_waitqueue_func_entry(&uwq
.wq
, userfaultfd_wake_function
);
406 uwq
.wq
.private = current
;
407 uwq
.msg
= userfault_msg(vmf
->address
, vmf
->flags
, reason
);
412 (vmf
->flags
& (FAULT_FLAG_USER
|FAULT_FLAG_KILLABLE
)) ==
413 (FAULT_FLAG_USER
|FAULT_FLAG_KILLABLE
);
414 blocking_state
= return_to_userland
? TASK_INTERRUPTIBLE
:
417 spin_lock(&ctx
->fault_pending_wqh
.lock
);
419 * After the __add_wait_queue the uwq is visible to userland
420 * through poll/read().
422 __add_wait_queue(&ctx
->fault_pending_wqh
, &uwq
.wq
);
424 * The smp_mb() after __set_current_state prevents the reads
425 * following the spin_unlock to happen before the list_add in
428 set_current_state(blocking_state
);
429 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
431 if (!is_vm_hugetlb_page(vmf
->vma
))
432 must_wait
= userfaultfd_must_wait(ctx
, vmf
->address
, vmf
->flags
,
435 must_wait
= userfaultfd_huge_must_wait(ctx
, vmf
->address
,
437 up_read(&mm
->mmap_sem
);
439 if (likely(must_wait
&& !ACCESS_ONCE(ctx
->released
) &&
440 (return_to_userland
? !signal_pending(current
) :
441 !fatal_signal_pending(current
)))) {
442 wake_up_poll(&ctx
->fd_wqh
, POLLIN
);
444 ret
|= VM_FAULT_MAJOR
;
447 * False wakeups can orginate even from rwsem before
448 * up_read() however userfaults will wait either for a
449 * targeted wakeup on the specific uwq waitqueue from
450 * wake_userfault() or for signals or for uffd
453 while (!READ_ONCE(uwq
.waken
)) {
455 * This needs the full smp_store_mb()
456 * guarantee as the state write must be
457 * visible to other CPUs before reading
458 * uwq.waken from other CPUs.
460 set_current_state(blocking_state
);
461 if (READ_ONCE(uwq
.waken
) ||
462 READ_ONCE(ctx
->released
) ||
463 (return_to_userland
? signal_pending(current
) :
464 fatal_signal_pending(current
)))
470 __set_current_state(TASK_RUNNING
);
472 if (return_to_userland
) {
473 if (signal_pending(current
) &&
474 !fatal_signal_pending(current
)) {
476 * If we got a SIGSTOP or SIGCONT and this is
477 * a normal userland page fault, just let
478 * userland return so the signal will be
479 * handled and gdb debugging works. The page
480 * fault code immediately after we return from
481 * this function is going to release the
482 * mmap_sem and it's not depending on it
483 * (unlike gup would if we were not to return
486 * If a fatal signal is pending we still take
487 * the streamlined VM_FAULT_RETRY failure path
488 * and there's no need to retake the mmap_sem
491 down_read(&mm
->mmap_sem
);
497 * Here we race with the list_del; list_add in
498 * userfaultfd_ctx_read(), however because we don't ever run
499 * list_del_init() to refile across the two lists, the prev
500 * and next pointers will never point to self. list_add also
501 * would never let any of the two pointers to point to
502 * self. So list_empty_careful won't risk to see both pointers
503 * pointing to self at any time during the list refile. The
504 * only case where list_del_init() is called is the full
505 * removal in the wake function and there we don't re-list_add
506 * and it's fine not to block on the spinlock. The uwq on this
507 * kernel stack can be released after the list_del_init.
509 if (!list_empty_careful(&uwq
.wq
.task_list
)) {
510 spin_lock(&ctx
->fault_pending_wqh
.lock
);
512 * No need of list_del_init(), the uwq on the stack
513 * will be freed shortly anyway.
515 list_del(&uwq
.wq
.task_list
);
516 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
520 * ctx may go away after this if the userfault pseudo fd is
523 userfaultfd_ctx_put(ctx
);
529 static int userfaultfd_event_wait_completion(struct userfaultfd_ctx
*ctx
,
530 struct userfaultfd_wait_queue
*ewq
)
535 init_waitqueue_entry(&ewq
->wq
, current
);
537 spin_lock(&ctx
->event_wqh
.lock
);
539 * After the __add_wait_queue the uwq is visible to userland
540 * through poll/read().
542 __add_wait_queue(&ctx
->event_wqh
, &ewq
->wq
);
544 set_current_state(TASK_KILLABLE
);
545 if (ewq
->msg
.event
== 0)
547 if (ACCESS_ONCE(ctx
->released
) ||
548 fatal_signal_pending(current
)) {
550 __remove_wait_queue(&ctx
->event_wqh
, &ewq
->wq
);
554 spin_unlock(&ctx
->event_wqh
.lock
);
556 wake_up_poll(&ctx
->fd_wqh
, POLLIN
);
559 spin_lock(&ctx
->event_wqh
.lock
);
561 __set_current_state(TASK_RUNNING
);
562 spin_unlock(&ctx
->event_wqh
.lock
);
565 * ctx may go away after this if the userfault pseudo fd is
569 userfaultfd_ctx_put(ctx
);
573 static void userfaultfd_event_complete(struct userfaultfd_ctx
*ctx
,
574 struct userfaultfd_wait_queue
*ewq
)
577 wake_up_locked(&ctx
->event_wqh
);
578 __remove_wait_queue(&ctx
->event_wqh
, &ewq
->wq
);
581 int dup_userfaultfd(struct vm_area_struct
*vma
, struct list_head
*fcs
)
583 struct userfaultfd_ctx
*ctx
= NULL
, *octx
;
584 struct userfaultfd_fork_ctx
*fctx
;
586 octx
= vma
->vm_userfaultfd_ctx
.ctx
;
587 if (!octx
|| !(octx
->features
& UFFD_FEATURE_EVENT_FORK
)) {
588 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
589 vma
->vm_flags
&= ~(VM_UFFD_WP
| VM_UFFD_MISSING
);
593 list_for_each_entry(fctx
, fcs
, list
)
594 if (fctx
->orig
== octx
) {
600 fctx
= kmalloc(sizeof(*fctx
), GFP_KERNEL
);
604 ctx
= kmem_cache_alloc(userfaultfd_ctx_cachep
, GFP_KERNEL
);
610 atomic_set(&ctx
->refcount
, 1);
611 ctx
->flags
= octx
->flags
;
612 ctx
->state
= UFFD_STATE_RUNNING
;
613 ctx
->features
= octx
->features
;
614 ctx
->released
= false;
615 ctx
->mm
= vma
->vm_mm
;
616 atomic_inc(&ctx
->mm
->mm_count
);
618 userfaultfd_ctx_get(octx
);
621 list_add_tail(&fctx
->list
, fcs
);
624 vma
->vm_userfaultfd_ctx
.ctx
= ctx
;
628 static int dup_fctx(struct userfaultfd_fork_ctx
*fctx
)
630 struct userfaultfd_ctx
*ctx
= fctx
->orig
;
631 struct userfaultfd_wait_queue ewq
;
635 ewq
.msg
.event
= UFFD_EVENT_FORK
;
636 ewq
.msg
.arg
.reserved
.reserved1
= (unsigned long)fctx
->new;
638 return userfaultfd_event_wait_completion(ctx
, &ewq
);
641 void dup_userfaultfd_complete(struct list_head
*fcs
)
644 struct userfaultfd_fork_ctx
*fctx
, *n
;
646 list_for_each_entry_safe(fctx
, n
, fcs
, list
) {
648 ret
= dup_fctx(fctx
);
649 list_del(&fctx
->list
);
654 void mremap_userfaultfd_prep(struct vm_area_struct
*vma
,
655 struct vm_userfaultfd_ctx
*vm_ctx
)
657 struct userfaultfd_ctx
*ctx
;
659 ctx
= vma
->vm_userfaultfd_ctx
.ctx
;
660 if (ctx
&& (ctx
->features
& UFFD_FEATURE_EVENT_REMAP
)) {
662 userfaultfd_ctx_get(ctx
);
666 void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx
*vm_ctx
,
667 unsigned long from
, unsigned long to
,
670 struct userfaultfd_ctx
*ctx
= vm_ctx
->ctx
;
671 struct userfaultfd_wait_queue ewq
;
676 if (to
& ~PAGE_MASK
) {
677 userfaultfd_ctx_put(ctx
);
683 ewq
.msg
.event
= UFFD_EVENT_REMAP
;
684 ewq
.msg
.arg
.remap
.from
= from
;
685 ewq
.msg
.arg
.remap
.to
= to
;
686 ewq
.msg
.arg
.remap
.len
= len
;
688 userfaultfd_event_wait_completion(ctx
, &ewq
);
691 void userfaultfd_remove(struct vm_area_struct
*vma
,
692 struct vm_area_struct
**prev
,
693 unsigned long start
, unsigned long end
)
695 struct mm_struct
*mm
= vma
->vm_mm
;
696 struct userfaultfd_ctx
*ctx
;
697 struct userfaultfd_wait_queue ewq
;
699 ctx
= vma
->vm_userfaultfd_ctx
.ctx
;
700 if (!ctx
|| !(ctx
->features
& UFFD_FEATURE_EVENT_REMOVE
))
703 userfaultfd_ctx_get(ctx
);
704 up_read(&mm
->mmap_sem
);
706 *prev
= NULL
; /* We wait for ACK w/o the mmap semaphore */
710 ewq
.msg
.event
= UFFD_EVENT_REMOVE
;
711 ewq
.msg
.arg
.remove
.start
= start
;
712 ewq
.msg
.arg
.remove
.end
= end
;
714 userfaultfd_event_wait_completion(ctx
, &ewq
);
716 down_read(&mm
->mmap_sem
);
719 static bool has_unmap_ctx(struct userfaultfd_ctx
*ctx
, struct list_head
*unmaps
,
720 unsigned long start
, unsigned long end
)
722 struct userfaultfd_unmap_ctx
*unmap_ctx
;
724 list_for_each_entry(unmap_ctx
, unmaps
, list
)
725 if (unmap_ctx
->ctx
== ctx
&& unmap_ctx
->start
== start
&&
726 unmap_ctx
->end
== end
)
732 int userfaultfd_unmap_prep(struct vm_area_struct
*vma
,
733 unsigned long start
, unsigned long end
,
734 struct list_head
*unmaps
)
736 for ( ; vma
&& vma
->vm_start
< end
; vma
= vma
->vm_next
) {
737 struct userfaultfd_unmap_ctx
*unmap_ctx
;
738 struct userfaultfd_ctx
*ctx
= vma
->vm_userfaultfd_ctx
.ctx
;
740 if (!ctx
|| !(ctx
->features
& UFFD_FEATURE_EVENT_UNMAP
) ||
741 has_unmap_ctx(ctx
, unmaps
, start
, end
))
744 unmap_ctx
= kzalloc(sizeof(*unmap_ctx
), GFP_KERNEL
);
748 userfaultfd_ctx_get(ctx
);
749 unmap_ctx
->ctx
= ctx
;
750 unmap_ctx
->start
= start
;
751 unmap_ctx
->end
= end
;
752 list_add_tail(&unmap_ctx
->list
, unmaps
);
758 void userfaultfd_unmap_complete(struct mm_struct
*mm
, struct list_head
*uf
)
760 struct userfaultfd_unmap_ctx
*ctx
, *n
;
761 struct userfaultfd_wait_queue ewq
;
763 list_for_each_entry_safe(ctx
, n
, uf
, list
) {
766 ewq
.msg
.event
= UFFD_EVENT_UNMAP
;
767 ewq
.msg
.arg
.remove
.start
= ctx
->start
;
768 ewq
.msg
.arg
.remove
.end
= ctx
->end
;
770 userfaultfd_event_wait_completion(ctx
->ctx
, &ewq
);
772 list_del(&ctx
->list
);
777 void userfaultfd_exit(struct mm_struct
*mm
)
779 struct vm_area_struct
*vma
= mm
->mmap
;
782 * We can do the vma walk without locking because the caller
783 * (exit_mm) knows it now has exclusive access
786 struct userfaultfd_ctx
*ctx
= vma
->vm_userfaultfd_ctx
.ctx
;
788 if (ctx
&& (ctx
->features
& UFFD_FEATURE_EVENT_EXIT
)) {
789 struct userfaultfd_wait_queue ewq
;
791 userfaultfd_ctx_get(ctx
);
794 ewq
.msg
.event
= UFFD_EVENT_EXIT
;
796 userfaultfd_event_wait_completion(ctx
, &ewq
);
798 ctx
->features
&= ~UFFD_FEATURE_EVENT_EXIT
;
805 static int userfaultfd_release(struct inode
*inode
, struct file
*file
)
807 struct userfaultfd_ctx
*ctx
= file
->private_data
;
808 struct mm_struct
*mm
= ctx
->mm
;
809 struct vm_area_struct
*vma
, *prev
;
810 /* len == 0 means wake all */
811 struct userfaultfd_wake_range range
= { .len
= 0, };
812 unsigned long new_flags
;
814 ACCESS_ONCE(ctx
->released
) = true;
816 if (!mmget_not_zero(mm
))
820 * Flush page faults out of all CPUs. NOTE: all page faults
821 * must be retried without returning VM_FAULT_SIGBUS if
822 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
823 * changes while handle_userfault released the mmap_sem. So
824 * it's critical that released is set to true (above), before
825 * taking the mmap_sem for writing.
827 down_write(&mm
->mmap_sem
);
829 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
831 BUG_ON(!!vma
->vm_userfaultfd_ctx
.ctx
^
832 !!(vma
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
833 if (vma
->vm_userfaultfd_ctx
.ctx
!= ctx
) {
837 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
838 prev
= vma_merge(mm
, prev
, vma
->vm_start
, vma
->vm_end
,
839 new_flags
, vma
->anon_vma
,
840 vma
->vm_file
, vma
->vm_pgoff
,
847 vma
->vm_flags
= new_flags
;
848 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
850 up_write(&mm
->mmap_sem
);
854 * After no new page faults can wait on this fault_*wqh, flush
855 * the last page faults that may have been already waiting on
858 spin_lock(&ctx
->fault_pending_wqh
.lock
);
859 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
, &range
);
860 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, &range
);
861 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
863 wake_up_poll(&ctx
->fd_wqh
, POLLHUP
);
864 userfaultfd_ctx_put(ctx
);
868 /* fault_pending_wqh.lock must be hold by the caller */
869 static inline struct userfaultfd_wait_queue
*find_userfault_in(
870 wait_queue_head_t
*wqh
)
873 struct userfaultfd_wait_queue
*uwq
;
875 VM_BUG_ON(!spin_is_locked(&wqh
->lock
));
878 if (!waitqueue_active(wqh
))
880 /* walk in reverse to provide FIFO behavior to read userfaults */
881 wq
= list_last_entry(&wqh
->task_list
, typeof(*wq
), task_list
);
882 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
887 static inline struct userfaultfd_wait_queue
*find_userfault(
888 struct userfaultfd_ctx
*ctx
)
890 return find_userfault_in(&ctx
->fault_pending_wqh
);
893 static inline struct userfaultfd_wait_queue
*find_userfault_evt(
894 struct userfaultfd_ctx
*ctx
)
896 return find_userfault_in(&ctx
->event_wqh
);
899 static unsigned int userfaultfd_poll(struct file
*file
, poll_table
*wait
)
901 struct userfaultfd_ctx
*ctx
= file
->private_data
;
904 poll_wait(file
, &ctx
->fd_wqh
, wait
);
906 switch (ctx
->state
) {
907 case UFFD_STATE_WAIT_API
:
909 case UFFD_STATE_RUNNING
:
911 * poll() never guarantees that read won't block.
912 * userfaults can be waken before they're read().
914 if (unlikely(!(file
->f_flags
& O_NONBLOCK
)))
917 * lockless access to see if there are pending faults
918 * __pollwait last action is the add_wait_queue but
919 * the spin_unlock would allow the waitqueue_active to
920 * pass above the actual list_add inside
921 * add_wait_queue critical section. So use a full
922 * memory barrier to serialize the list_add write of
923 * add_wait_queue() with the waitqueue_active read
928 if (waitqueue_active(&ctx
->fault_pending_wqh
))
930 else if (waitqueue_active(&ctx
->event_wqh
))
940 static const struct file_operations userfaultfd_fops
;
942 static int resolve_userfault_fork(struct userfaultfd_ctx
*ctx
,
943 struct userfaultfd_ctx
*new,
944 struct uffd_msg
*msg
)
948 unsigned int flags
= new->flags
& UFFD_SHARED_FCNTL_FLAGS
;
950 fd
= get_unused_fd_flags(flags
);
954 file
= anon_inode_getfile("[userfaultfd]", &userfaultfd_fops
, new,
958 return PTR_ERR(file
);
961 fd_install(fd
, file
);
962 msg
->arg
.reserved
.reserved1
= 0;
963 msg
->arg
.fork
.ufd
= fd
;
968 static ssize_t
userfaultfd_ctx_read(struct userfaultfd_ctx
*ctx
, int no_wait
,
969 struct uffd_msg
*msg
)
972 DECLARE_WAITQUEUE(wait
, current
);
973 struct userfaultfd_wait_queue
*uwq
;
975 * Handling fork event requires sleeping operations, so
976 * we drop the event_wqh lock, then do these ops, then
977 * lock it back and wake up the waiter. While the lock is
978 * dropped the ewq may go away so we keep track of it
981 LIST_HEAD(fork_event
);
982 struct userfaultfd_ctx
*fork_nctx
= NULL
;
984 /* always take the fd_wqh lock before the fault_pending_wqh lock */
985 spin_lock(&ctx
->fd_wqh
.lock
);
986 __add_wait_queue(&ctx
->fd_wqh
, &wait
);
988 set_current_state(TASK_INTERRUPTIBLE
);
989 spin_lock(&ctx
->fault_pending_wqh
.lock
);
990 uwq
= find_userfault(ctx
);
993 * Use a seqcount to repeat the lockless check
994 * in wake_userfault() to avoid missing
995 * wakeups because during the refile both
996 * waitqueue could become empty if this is the
999 write_seqcount_begin(&ctx
->refile_seq
);
1002 * The fault_pending_wqh.lock prevents the uwq
1003 * to disappear from under us.
1005 * Refile this userfault from
1006 * fault_pending_wqh to fault_wqh, it's not
1007 * pending anymore after we read it.
1009 * Use list_del() by hand (as
1010 * userfaultfd_wake_function also uses
1011 * list_del_init() by hand) to be sure nobody
1012 * changes __remove_wait_queue() to use
1013 * list_del_init() in turn breaking the
1014 * !list_empty_careful() check in
1015 * handle_userfault(). The uwq->wq.task_list
1016 * must never be empty at any time during the
1017 * refile, or the waitqueue could disappear
1018 * from under us. The "wait_queue_head_t"
1019 * parameter of __remove_wait_queue() is unused
1022 list_del(&uwq
->wq
.task_list
);
1023 __add_wait_queue(&ctx
->fault_wqh
, &uwq
->wq
);
1025 write_seqcount_end(&ctx
->refile_seq
);
1027 /* careful to always initialize msg if ret == 0 */
1029 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
1033 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
1035 spin_lock(&ctx
->event_wqh
.lock
);
1036 uwq
= find_userfault_evt(ctx
);
1040 if (uwq
->msg
.event
== UFFD_EVENT_FORK
) {
1041 fork_nctx
= (struct userfaultfd_ctx
*)
1043 uwq
->msg
.arg
.reserved
.reserved1
;
1044 list_move(&uwq
->wq
.task_list
, &fork_event
);
1045 spin_unlock(&ctx
->event_wqh
.lock
);
1050 userfaultfd_event_complete(ctx
, uwq
);
1051 spin_unlock(&ctx
->event_wqh
.lock
);
1055 spin_unlock(&ctx
->event_wqh
.lock
);
1057 if (signal_pending(current
)) {
1065 spin_unlock(&ctx
->fd_wqh
.lock
);
1067 spin_lock(&ctx
->fd_wqh
.lock
);
1069 __remove_wait_queue(&ctx
->fd_wqh
, &wait
);
1070 __set_current_state(TASK_RUNNING
);
1071 spin_unlock(&ctx
->fd_wqh
.lock
);
1073 if (!ret
&& msg
->event
== UFFD_EVENT_FORK
) {
1074 ret
= resolve_userfault_fork(ctx
, fork_nctx
, msg
);
1077 spin_lock(&ctx
->event_wqh
.lock
);
1078 if (!list_empty(&fork_event
)) {
1079 uwq
= list_first_entry(&fork_event
,
1082 list_del(&uwq
->wq
.task_list
);
1083 __add_wait_queue(&ctx
->event_wqh
, &uwq
->wq
);
1084 userfaultfd_event_complete(ctx
, uwq
);
1086 spin_unlock(&ctx
->event_wqh
.lock
);
1093 static ssize_t
userfaultfd_read(struct file
*file
, char __user
*buf
,
1094 size_t count
, loff_t
*ppos
)
1096 struct userfaultfd_ctx
*ctx
= file
->private_data
;
1097 ssize_t _ret
, ret
= 0;
1098 struct uffd_msg msg
;
1099 int no_wait
= file
->f_flags
& O_NONBLOCK
;
1101 if (ctx
->state
== UFFD_STATE_WAIT_API
)
1105 if (count
< sizeof(msg
))
1106 return ret
? ret
: -EINVAL
;
1107 _ret
= userfaultfd_ctx_read(ctx
, no_wait
, &msg
);
1109 return ret
? ret
: _ret
;
1110 if (copy_to_user((__u64 __user
*) buf
, &msg
, sizeof(msg
)))
1111 return ret
? ret
: -EFAULT
;
1114 count
-= sizeof(msg
);
1116 * Allow to read more than one fault at time but only
1117 * block if waiting for the very first one.
1119 no_wait
= O_NONBLOCK
;
1123 static void __wake_userfault(struct userfaultfd_ctx
*ctx
,
1124 struct userfaultfd_wake_range
*range
)
1126 unsigned long start
, end
;
1128 start
= range
->start
;
1129 end
= range
->start
+ range
->len
;
1131 spin_lock(&ctx
->fault_pending_wqh
.lock
);
1132 /* wake all in the range and autoremove */
1133 if (waitqueue_active(&ctx
->fault_pending_wqh
))
1134 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
,
1136 if (waitqueue_active(&ctx
->fault_wqh
))
1137 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, range
);
1138 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
1141 static __always_inline
void wake_userfault(struct userfaultfd_ctx
*ctx
,
1142 struct userfaultfd_wake_range
*range
)
1148 * To be sure waitqueue_active() is not reordered by the CPU
1149 * before the pagetable update, use an explicit SMP memory
1150 * barrier here. PT lock release or up_read(mmap_sem) still
1151 * have release semantics that can allow the
1152 * waitqueue_active() to be reordered before the pte update.
1157 * Use waitqueue_active because it's very frequent to
1158 * change the address space atomically even if there are no
1159 * userfaults yet. So we take the spinlock only when we're
1160 * sure we've userfaults to wake.
1163 seq
= read_seqcount_begin(&ctx
->refile_seq
);
1164 need_wakeup
= waitqueue_active(&ctx
->fault_pending_wqh
) ||
1165 waitqueue_active(&ctx
->fault_wqh
);
1167 } while (read_seqcount_retry(&ctx
->refile_seq
, seq
));
1169 __wake_userfault(ctx
, range
);
1172 static __always_inline
int validate_range(struct mm_struct
*mm
,
1173 __u64 start
, __u64 len
)
1175 __u64 task_size
= mm
->task_size
;
1177 if (start
& ~PAGE_MASK
)
1179 if (len
& ~PAGE_MASK
)
1183 if (start
< mmap_min_addr
)
1185 if (start
>= task_size
)
1187 if (len
> task_size
- start
)
1192 static inline bool vma_can_userfault(struct vm_area_struct
*vma
)
1194 return vma_is_anonymous(vma
) || is_vm_hugetlb_page(vma
) ||
1198 static int userfaultfd_register(struct userfaultfd_ctx
*ctx
,
1201 struct mm_struct
*mm
= ctx
->mm
;
1202 struct vm_area_struct
*vma
, *prev
, *cur
;
1204 struct uffdio_register uffdio_register
;
1205 struct uffdio_register __user
*user_uffdio_register
;
1206 unsigned long vm_flags
, new_flags
;
1208 bool non_anon_pages
;
1209 unsigned long start
, end
, vma_end
;
1211 user_uffdio_register
= (struct uffdio_register __user
*) arg
;
1214 if (copy_from_user(&uffdio_register
, user_uffdio_register
,
1215 sizeof(uffdio_register
)-sizeof(__u64
)))
1219 if (!uffdio_register
.mode
)
1221 if (uffdio_register
.mode
& ~(UFFDIO_REGISTER_MODE_MISSING
|
1222 UFFDIO_REGISTER_MODE_WP
))
1225 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_MISSING
)
1226 vm_flags
|= VM_UFFD_MISSING
;
1227 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_WP
) {
1228 vm_flags
|= VM_UFFD_WP
;
1230 * FIXME: remove the below error constraint by
1231 * implementing the wprotect tracking mode.
1237 ret
= validate_range(mm
, uffdio_register
.range
.start
,
1238 uffdio_register
.range
.len
);
1242 start
= uffdio_register
.range
.start
;
1243 end
= start
+ uffdio_register
.range
.len
;
1246 if (!mmget_not_zero(mm
))
1249 down_write(&mm
->mmap_sem
);
1250 vma
= find_vma_prev(mm
, start
, &prev
);
1254 /* check that there's at least one vma in the range */
1256 if (vma
->vm_start
>= end
)
1260 * If the first vma contains huge pages, make sure start address
1261 * is aligned to huge page size.
1263 if (is_vm_hugetlb_page(vma
)) {
1264 unsigned long vma_hpagesize
= vma_kernel_pagesize(vma
);
1266 if (start
& (vma_hpagesize
- 1))
1271 * Search for not compatible vmas.
1274 non_anon_pages
= false;
1275 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
1278 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
1279 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
1281 /* check not compatible vmas */
1283 if (!vma_can_userfault(cur
))
1286 * If this vma contains ending address, and huge pages
1289 if (is_vm_hugetlb_page(cur
) && end
<= cur
->vm_end
&&
1290 end
> cur
->vm_start
) {
1291 unsigned long vma_hpagesize
= vma_kernel_pagesize(cur
);
1295 if (end
& (vma_hpagesize
- 1))
1300 * Check that this vma isn't already owned by a
1301 * different userfaultfd. We can't allow more than one
1302 * userfaultfd to own a single vma simultaneously or we
1303 * wouldn't know which one to deliver the userfaults to.
1306 if (cur
->vm_userfaultfd_ctx
.ctx
&&
1307 cur
->vm_userfaultfd_ctx
.ctx
!= ctx
)
1311 * Note vmas containing huge pages
1313 if (is_vm_hugetlb_page(cur
) || vma_is_shmem(cur
))
1314 non_anon_pages
= true;
1320 if (vma
->vm_start
< start
)
1327 BUG_ON(!vma_can_userfault(vma
));
1328 BUG_ON(vma
->vm_userfaultfd_ctx
.ctx
&&
1329 vma
->vm_userfaultfd_ctx
.ctx
!= ctx
);
1332 * Nothing to do: this vma is already registered into this
1333 * userfaultfd and with the right tracking mode too.
1335 if (vma
->vm_userfaultfd_ctx
.ctx
== ctx
&&
1336 (vma
->vm_flags
& vm_flags
) == vm_flags
)
1339 if (vma
->vm_start
> start
)
1340 start
= vma
->vm_start
;
1341 vma_end
= min(end
, vma
->vm_end
);
1343 new_flags
= (vma
->vm_flags
& ~vm_flags
) | vm_flags
;
1344 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
1345 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
1347 ((struct vm_userfaultfd_ctx
){ ctx
}));
1352 if (vma
->vm_start
< start
) {
1353 ret
= split_vma(mm
, vma
, start
, 1);
1357 if (vma
->vm_end
> end
) {
1358 ret
= split_vma(mm
, vma
, end
, 0);
1364 * In the vma_merge() successful mprotect-like case 8:
1365 * the next vma was merged into the current one and
1366 * the current one has not been updated yet.
1368 vma
->vm_flags
= new_flags
;
1369 vma
->vm_userfaultfd_ctx
.ctx
= ctx
;
1373 start
= vma
->vm_end
;
1375 } while (vma
&& vma
->vm_start
< end
);
1377 up_write(&mm
->mmap_sem
);
1381 * Now that we scanned all vmas we can already tell
1382 * userland which ioctls methods are guaranteed to
1383 * succeed on this range.
1385 if (put_user(non_anon_pages
? UFFD_API_RANGE_IOCTLS_BASIC
:
1386 UFFD_API_RANGE_IOCTLS
,
1387 &user_uffdio_register
->ioctls
))
1394 static int userfaultfd_unregister(struct userfaultfd_ctx
*ctx
,
1397 struct mm_struct
*mm
= ctx
->mm
;
1398 struct vm_area_struct
*vma
, *prev
, *cur
;
1400 struct uffdio_range uffdio_unregister
;
1401 unsigned long new_flags
;
1403 unsigned long start
, end
, vma_end
;
1404 const void __user
*buf
= (void __user
*)arg
;
1407 if (copy_from_user(&uffdio_unregister
, buf
, sizeof(uffdio_unregister
)))
1410 ret
= validate_range(mm
, uffdio_unregister
.start
,
1411 uffdio_unregister
.len
);
1415 start
= uffdio_unregister
.start
;
1416 end
= start
+ uffdio_unregister
.len
;
1419 if (!mmget_not_zero(mm
))
1422 down_write(&mm
->mmap_sem
);
1423 vma
= find_vma_prev(mm
, start
, &prev
);
1427 /* check that there's at least one vma in the range */
1429 if (vma
->vm_start
>= end
)
1433 * If the first vma contains huge pages, make sure start address
1434 * is aligned to huge page size.
1436 if (is_vm_hugetlb_page(vma
)) {
1437 unsigned long vma_hpagesize
= vma_kernel_pagesize(vma
);
1439 if (start
& (vma_hpagesize
- 1))
1444 * Search for not compatible vmas.
1448 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
1451 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
1452 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
1455 * Check not compatible vmas, not strictly required
1456 * here as not compatible vmas cannot have an
1457 * userfaultfd_ctx registered on them, but this
1458 * provides for more strict behavior to notice
1459 * unregistration errors.
1461 if (!vma_can_userfault(cur
))
1468 if (vma
->vm_start
< start
)
1475 BUG_ON(!vma_can_userfault(vma
));
1478 * Nothing to do: this vma is already registered into this
1479 * userfaultfd and with the right tracking mode too.
1481 if (!vma
->vm_userfaultfd_ctx
.ctx
)
1484 if (vma
->vm_start
> start
)
1485 start
= vma
->vm_start
;
1486 vma_end
= min(end
, vma
->vm_end
);
1488 if (userfaultfd_missing(vma
)) {
1490 * Wake any concurrent pending userfault while
1491 * we unregister, so they will not hang
1492 * permanently and it avoids userland to call
1493 * UFFDIO_WAKE explicitly.
1495 struct userfaultfd_wake_range range
;
1496 range
.start
= start
;
1497 range
.len
= vma_end
- start
;
1498 wake_userfault(vma
->vm_userfaultfd_ctx
.ctx
, &range
);
1501 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
1502 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
1503 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
1510 if (vma
->vm_start
< start
) {
1511 ret
= split_vma(mm
, vma
, start
, 1);
1515 if (vma
->vm_end
> end
) {
1516 ret
= split_vma(mm
, vma
, end
, 0);
1522 * In the vma_merge() successful mprotect-like case 8:
1523 * the next vma was merged into the current one and
1524 * the current one has not been updated yet.
1526 vma
->vm_flags
= new_flags
;
1527 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
1531 start
= vma
->vm_end
;
1533 } while (vma
&& vma
->vm_start
< end
);
1535 up_write(&mm
->mmap_sem
);
1542 * userfaultfd_wake may be used in combination with the
1543 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
1545 static int userfaultfd_wake(struct userfaultfd_ctx
*ctx
,
1549 struct uffdio_range uffdio_wake
;
1550 struct userfaultfd_wake_range range
;
1551 const void __user
*buf
= (void __user
*)arg
;
1554 if (copy_from_user(&uffdio_wake
, buf
, sizeof(uffdio_wake
)))
1557 ret
= validate_range(ctx
->mm
, uffdio_wake
.start
, uffdio_wake
.len
);
1561 range
.start
= uffdio_wake
.start
;
1562 range
.len
= uffdio_wake
.len
;
1565 * len == 0 means wake all and we don't want to wake all here,
1566 * so check it again to be sure.
1568 VM_BUG_ON(!range
.len
);
1570 wake_userfault(ctx
, &range
);
1577 static int userfaultfd_copy(struct userfaultfd_ctx
*ctx
,
1581 struct uffdio_copy uffdio_copy
;
1582 struct uffdio_copy __user
*user_uffdio_copy
;
1583 struct userfaultfd_wake_range range
;
1585 user_uffdio_copy
= (struct uffdio_copy __user
*) arg
;
1588 if (copy_from_user(&uffdio_copy
, user_uffdio_copy
,
1589 /* don't copy "copy" last field */
1590 sizeof(uffdio_copy
)-sizeof(__s64
)))
1593 ret
= validate_range(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.len
);
1597 * double check for wraparound just in case. copy_from_user()
1598 * will later check uffdio_copy.src + uffdio_copy.len to fit
1599 * in the userland range.
1602 if (uffdio_copy
.src
+ uffdio_copy
.len
<= uffdio_copy
.src
)
1604 if (uffdio_copy
.mode
& ~UFFDIO_COPY_MODE_DONTWAKE
)
1606 if (mmget_not_zero(ctx
->mm
)) {
1607 ret
= mcopy_atomic(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.src
,
1613 if (unlikely(put_user(ret
, &user_uffdio_copy
->copy
)))
1618 /* len == 0 would wake all */
1620 if (!(uffdio_copy
.mode
& UFFDIO_COPY_MODE_DONTWAKE
)) {
1621 range
.start
= uffdio_copy
.dst
;
1622 wake_userfault(ctx
, &range
);
1624 ret
= range
.len
== uffdio_copy
.len
? 0 : -EAGAIN
;
1629 static int userfaultfd_zeropage(struct userfaultfd_ctx
*ctx
,
1633 struct uffdio_zeropage uffdio_zeropage
;
1634 struct uffdio_zeropage __user
*user_uffdio_zeropage
;
1635 struct userfaultfd_wake_range range
;
1637 user_uffdio_zeropage
= (struct uffdio_zeropage __user
*) arg
;
1640 if (copy_from_user(&uffdio_zeropage
, user_uffdio_zeropage
,
1641 /* don't copy "zeropage" last field */
1642 sizeof(uffdio_zeropage
)-sizeof(__s64
)))
1645 ret
= validate_range(ctx
->mm
, uffdio_zeropage
.range
.start
,
1646 uffdio_zeropage
.range
.len
);
1650 if (uffdio_zeropage
.mode
& ~UFFDIO_ZEROPAGE_MODE_DONTWAKE
)
1653 if (mmget_not_zero(ctx
->mm
)) {
1654 ret
= mfill_zeropage(ctx
->mm
, uffdio_zeropage
.range
.start
,
1655 uffdio_zeropage
.range
.len
);
1658 if (unlikely(put_user(ret
, &user_uffdio_zeropage
->zeropage
)))
1662 /* len == 0 would wake all */
1665 if (!(uffdio_zeropage
.mode
& UFFDIO_ZEROPAGE_MODE_DONTWAKE
)) {
1666 range
.start
= uffdio_zeropage
.range
.start
;
1667 wake_userfault(ctx
, &range
);
1669 ret
= range
.len
== uffdio_zeropage
.range
.len
? 0 : -EAGAIN
;
1674 static inline unsigned int uffd_ctx_features(__u64 user_features
)
1677 * For the current set of features the bits just coincide
1679 return (unsigned int)user_features
;
1683 * userland asks for a certain API version and we return which bits
1684 * and ioctl commands are implemented in this kernel for such API
1685 * version or -EINVAL if unknown.
1687 static int userfaultfd_api(struct userfaultfd_ctx
*ctx
,
1690 struct uffdio_api uffdio_api
;
1691 void __user
*buf
= (void __user
*)arg
;
1696 if (ctx
->state
!= UFFD_STATE_WAIT_API
)
1699 if (copy_from_user(&uffdio_api
, buf
, sizeof(uffdio_api
)))
1701 features
= uffdio_api
.features
;
1702 if (uffdio_api
.api
!= UFFD_API
|| (features
& ~UFFD_API_FEATURES
)) {
1703 memset(&uffdio_api
, 0, sizeof(uffdio_api
));
1704 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1709 /* report all available features and ioctls to userland */
1710 uffdio_api
.features
= UFFD_API_FEATURES
;
1711 uffdio_api
.ioctls
= UFFD_API_IOCTLS
;
1713 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1715 ctx
->state
= UFFD_STATE_RUNNING
;
1716 /* only enable the requested features for this uffd context */
1717 ctx
->features
= uffd_ctx_features(features
);
1723 static long userfaultfd_ioctl(struct file
*file
, unsigned cmd
,
1727 struct userfaultfd_ctx
*ctx
= file
->private_data
;
1729 if (cmd
!= UFFDIO_API
&& ctx
->state
== UFFD_STATE_WAIT_API
)
1734 ret
= userfaultfd_api(ctx
, arg
);
1736 case UFFDIO_REGISTER
:
1737 ret
= userfaultfd_register(ctx
, arg
);
1739 case UFFDIO_UNREGISTER
:
1740 ret
= userfaultfd_unregister(ctx
, arg
);
1743 ret
= userfaultfd_wake(ctx
, arg
);
1746 ret
= userfaultfd_copy(ctx
, arg
);
1748 case UFFDIO_ZEROPAGE
:
1749 ret
= userfaultfd_zeropage(ctx
, arg
);
1755 #ifdef CONFIG_PROC_FS
1756 static void userfaultfd_show_fdinfo(struct seq_file
*m
, struct file
*f
)
1758 struct userfaultfd_ctx
*ctx
= f
->private_data
;
1760 struct userfaultfd_wait_queue
*uwq
;
1761 unsigned long pending
= 0, total
= 0;
1763 spin_lock(&ctx
->fault_pending_wqh
.lock
);
1764 list_for_each_entry(wq
, &ctx
->fault_pending_wqh
.task_list
, task_list
) {
1765 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1769 list_for_each_entry(wq
, &ctx
->fault_wqh
.task_list
, task_list
) {
1770 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1773 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
1776 * If more protocols will be added, there will be all shown
1777 * separated by a space. Like this:
1778 * protocols: aa:... bb:...
1780 seq_printf(m
, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
1781 pending
, total
, UFFD_API
, UFFD_API_FEATURES
,
1782 UFFD_API_IOCTLS
|UFFD_API_RANGE_IOCTLS
);
1786 static const struct file_operations userfaultfd_fops
= {
1787 #ifdef CONFIG_PROC_FS
1788 .show_fdinfo
= userfaultfd_show_fdinfo
,
1790 .release
= userfaultfd_release
,
1791 .poll
= userfaultfd_poll
,
1792 .read
= userfaultfd_read
,
1793 .unlocked_ioctl
= userfaultfd_ioctl
,
1794 .compat_ioctl
= userfaultfd_ioctl
,
1795 .llseek
= noop_llseek
,
1798 static void init_once_userfaultfd_ctx(void *mem
)
1800 struct userfaultfd_ctx
*ctx
= (struct userfaultfd_ctx
*) mem
;
1802 init_waitqueue_head(&ctx
->fault_pending_wqh
);
1803 init_waitqueue_head(&ctx
->fault_wqh
);
1804 init_waitqueue_head(&ctx
->event_wqh
);
1805 init_waitqueue_head(&ctx
->fd_wqh
);
1806 seqcount_init(&ctx
->refile_seq
);
1810 * userfaultfd_file_create - Creates a userfaultfd file pointer.
1811 * @flags: Flags for the userfaultfd file.
1813 * This function creates a userfaultfd file pointer, w/out installing
1814 * it into the fd table. This is useful when the userfaultfd file is
1815 * used during the initialization of data structures that require
1816 * extra setup after the userfaultfd creation. So the userfaultfd
1817 * creation is split into the file pointer creation phase, and the
1818 * file descriptor installation phase. In this way races with
1819 * userspace closing the newly installed file descriptor can be
1820 * avoided. Returns a userfaultfd file pointer, or a proper error
1823 static struct file
*userfaultfd_file_create(int flags
)
1826 struct userfaultfd_ctx
*ctx
;
1828 BUG_ON(!current
->mm
);
1830 /* Check the UFFD_* constants for consistency. */
1831 BUILD_BUG_ON(UFFD_CLOEXEC
!= O_CLOEXEC
);
1832 BUILD_BUG_ON(UFFD_NONBLOCK
!= O_NONBLOCK
);
1834 file
= ERR_PTR(-EINVAL
);
1835 if (flags
& ~UFFD_SHARED_FCNTL_FLAGS
)
1838 file
= ERR_PTR(-ENOMEM
);
1839 ctx
= kmem_cache_alloc(userfaultfd_ctx_cachep
, GFP_KERNEL
);
1843 atomic_set(&ctx
->refcount
, 1);
1846 ctx
->state
= UFFD_STATE_WAIT_API
;
1847 ctx
->released
= false;
1848 ctx
->mm
= current
->mm
;
1849 /* prevent the mm struct to be freed */
1852 file
= anon_inode_getfile("[userfaultfd]", &userfaultfd_fops
, ctx
,
1853 O_RDWR
| (flags
& UFFD_SHARED_FCNTL_FLAGS
));
1856 kmem_cache_free(userfaultfd_ctx_cachep
, ctx
);
1862 SYSCALL_DEFINE1(userfaultfd
, int, flags
)
1867 error
= get_unused_fd_flags(flags
& UFFD_SHARED_FCNTL_FLAGS
);
1872 file
= userfaultfd_file_create(flags
);
1874 error
= PTR_ERR(file
);
1875 goto err_put_unused_fd
;
1877 fd_install(fd
, file
);
1887 static int __init
userfaultfd_init(void)
1889 userfaultfd_ctx_cachep
= kmem_cache_create("userfaultfd_ctx_cache",
1890 sizeof(struct userfaultfd_ctx
),
1892 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
1893 init_once_userfaultfd_ctx
);
1896 __initcall(userfaultfd_init
);