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/hashtable.h>
16 #include <linux/sched.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/seq_file.h>
21 #include <linux/file.h>
22 #include <linux/bug.h>
23 #include <linux/anon_inodes.h>
24 #include <linux/syscalls.h>
25 #include <linux/userfaultfd_k.h>
26 #include <linux/mempolicy.h>
27 #include <linux/ioctl.h>
28 #include <linux/security.h>
30 static struct kmem_cache
*userfaultfd_ctx_cachep __read_mostly
;
32 enum userfaultfd_state
{
38 * Start with fault_pending_wqh and fault_wqh so they're more likely
39 * to be in the same cacheline.
41 struct userfaultfd_ctx
{
42 /* waitqueue head for the pending (i.e. not read) userfaults */
43 wait_queue_head_t fault_pending_wqh
;
44 /* waitqueue head for the userfaults */
45 wait_queue_head_t fault_wqh
;
46 /* waitqueue head for the pseudo fd to wakeup poll/read */
47 wait_queue_head_t fd_wqh
;
48 /* a refile sequence protected by fault_pending_wqh lock */
49 struct seqcount refile_seq
;
50 /* pseudo fd refcounting */
52 /* userfaultfd syscall flags */
55 enum userfaultfd_state state
;
58 /* mm with one ore more vmas attached to this userfaultfd_ctx */
62 struct userfaultfd_wait_queue
{
65 struct userfaultfd_ctx
*ctx
;
68 struct userfaultfd_wake_range
{
73 static int userfaultfd_wake_function(wait_queue_t
*wq
, unsigned mode
,
74 int wake_flags
, void *key
)
76 struct userfaultfd_wake_range
*range
= key
;
78 struct userfaultfd_wait_queue
*uwq
;
79 unsigned long start
, len
;
81 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
83 /* len == 0 means wake all */
86 if (len
&& (start
> uwq
->msg
.arg
.pagefault
.address
||
87 start
+ len
<= uwq
->msg
.arg
.pagefault
.address
))
89 ret
= wake_up_state(wq
->private, mode
);
92 * Wake only once, autoremove behavior.
94 * After the effect of list_del_init is visible to the
95 * other CPUs, the waitqueue may disappear from under
96 * us, see the !list_empty_careful() in
97 * handle_userfault(). try_to_wake_up() has an
98 * implicit smp_mb__before_spinlock, and the
99 * wq->private is read before calling the extern
100 * function "wake_up_state" (which in turns calls
101 * try_to_wake_up). While the spin_lock;spin_unlock;
102 * wouldn't be enough, the smp_mb__before_spinlock is
103 * enough to avoid an explicit smp_mb() here.
105 list_del_init(&wq
->task_list
);
111 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
113 * @ctx: [in] Pointer to the userfaultfd context.
115 * Returns: In case of success, returns not zero.
117 static void userfaultfd_ctx_get(struct userfaultfd_ctx
*ctx
)
119 if (!atomic_inc_not_zero(&ctx
->refcount
))
124 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
126 * @ctx: [in] Pointer to userfaultfd context.
128 * The userfaultfd context reference must have been previously acquired either
129 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
131 static void userfaultfd_ctx_put(struct userfaultfd_ctx
*ctx
)
133 if (atomic_dec_and_test(&ctx
->refcount
)) {
134 VM_BUG_ON(spin_is_locked(&ctx
->fault_pending_wqh
.lock
));
135 VM_BUG_ON(waitqueue_active(&ctx
->fault_pending_wqh
));
136 VM_BUG_ON(spin_is_locked(&ctx
->fault_wqh
.lock
));
137 VM_BUG_ON(waitqueue_active(&ctx
->fault_wqh
));
138 VM_BUG_ON(spin_is_locked(&ctx
->fd_wqh
.lock
));
139 VM_BUG_ON(waitqueue_active(&ctx
->fd_wqh
));
141 kmem_cache_free(userfaultfd_ctx_cachep
, ctx
);
145 static inline void msg_init(struct uffd_msg
*msg
)
147 BUILD_BUG_ON(sizeof(struct uffd_msg
) != 32);
149 * Must use memset to zero out the paddings or kernel data is
150 * leaked to userland.
152 memset(msg
, 0, sizeof(struct uffd_msg
));
155 static inline struct uffd_msg
userfault_msg(unsigned long address
,
157 unsigned long reason
)
161 msg
.event
= UFFD_EVENT_PAGEFAULT
;
162 msg
.arg
.pagefault
.address
= address
;
163 if (flags
& FAULT_FLAG_WRITE
)
165 * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
166 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
167 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
168 * was a read fault, otherwise if set it means it's
171 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WRITE
;
172 if (reason
& VM_UFFD_WP
)
174 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
175 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
176 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
177 * a missing fault, otherwise if set it means it's a
178 * write protect fault.
180 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WP
;
185 * Verify the pagetables are still not ok after having reigstered into
186 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
187 * userfault that has already been resolved, if userfaultfd_read and
188 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
191 static inline bool userfaultfd_must_wait(struct userfaultfd_ctx
*ctx
,
192 unsigned long address
,
194 unsigned long reason
)
196 struct mm_struct
*mm
= ctx
->mm
;
203 VM_BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
205 pgd
= pgd_offset(mm
, address
);
206 if (!pgd_present(*pgd
))
208 pud
= pud_offset(pgd
, address
);
209 if (!pud_present(*pud
))
211 pmd
= pmd_offset(pud
, address
);
213 * READ_ONCE must function as a barrier with narrower scope
214 * and it must be equivalent to:
215 * _pmd = *pmd; barrier();
217 * This is to deal with the instability (as in
218 * pmd_trans_unstable) of the pmd.
220 _pmd
= READ_ONCE(*pmd
);
221 if (!pmd_present(_pmd
))
225 if (pmd_trans_huge(_pmd
))
229 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
230 * and use the standard pte_offset_map() instead of parsing _pmd.
232 pte
= pte_offset_map(pmd
, address
);
234 * Lockless access: we're in a wait_event so it's ok if it
246 * The locking rules involved in returning VM_FAULT_RETRY depending on
247 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
248 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
249 * recommendation in __lock_page_or_retry is not an understatement.
251 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
252 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
255 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
256 * set, VM_FAULT_RETRY can still be returned if and only if there are
257 * fatal_signal_pending()s, and the mmap_sem must be released before
260 int handle_userfault(struct vm_area_struct
*vma
, unsigned long address
,
261 unsigned int flags
, unsigned long reason
)
263 struct mm_struct
*mm
= vma
->vm_mm
;
264 struct userfaultfd_ctx
*ctx
;
265 struct userfaultfd_wait_queue uwq
;
267 bool must_wait
, return_to_userland
;
269 BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
271 ret
= VM_FAULT_SIGBUS
;
272 ctx
= vma
->vm_userfaultfd_ctx
.ctx
;
276 BUG_ON(ctx
->mm
!= mm
);
278 VM_BUG_ON(reason
& ~(VM_UFFD_MISSING
|VM_UFFD_WP
));
279 VM_BUG_ON(!(reason
& VM_UFFD_MISSING
) ^ !!(reason
& VM_UFFD_WP
));
282 * If it's already released don't get it. This avoids to loop
283 * in __get_user_pages if userfaultfd_release waits on the
284 * caller of handle_userfault to release the mmap_sem.
286 if (unlikely(ACCESS_ONCE(ctx
->released
)))
290 * We don't do userfault handling for the final child pid update.
292 if (current
->flags
& PF_EXITING
)
296 * Check that we can return VM_FAULT_RETRY.
298 * NOTE: it should become possible to return VM_FAULT_RETRY
299 * even if FAULT_FLAG_TRIED is set without leading to gup()
300 * -EBUSY failures, if the userfaultfd is to be extended for
301 * VM_UFFD_WP tracking and we intend to arm the userfault
302 * without first stopping userland access to the memory. For
303 * VM_UFFD_MISSING userfaults this is enough for now.
305 if (unlikely(!(flags
& FAULT_FLAG_ALLOW_RETRY
))) {
307 * Validate the invariant that nowait must allow retry
308 * to be sure not to return SIGBUS erroneously on
309 * nowait invocations.
311 BUG_ON(flags
& FAULT_FLAG_RETRY_NOWAIT
);
312 #ifdef CONFIG_DEBUG_VM
313 if (printk_ratelimit()) {
315 "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags
);
323 * Handle nowait, not much to do other than tell it to retry
326 ret
= VM_FAULT_RETRY
;
327 if (flags
& FAULT_FLAG_RETRY_NOWAIT
)
330 /* take the reference before dropping the mmap_sem */
331 userfaultfd_ctx_get(ctx
);
333 init_waitqueue_func_entry(&uwq
.wq
, userfaultfd_wake_function
);
334 uwq
.wq
.private = current
;
335 uwq
.msg
= userfault_msg(address
, flags
, reason
);
338 return_to_userland
= (flags
& (FAULT_FLAG_USER
|FAULT_FLAG_KILLABLE
)) ==
339 (FAULT_FLAG_USER
|FAULT_FLAG_KILLABLE
);
341 spin_lock(&ctx
->fault_pending_wqh
.lock
);
343 * After the __add_wait_queue the uwq is visible to userland
344 * through poll/read().
346 __add_wait_queue(&ctx
->fault_pending_wqh
, &uwq
.wq
);
348 * The smp_mb() after __set_current_state prevents the reads
349 * following the spin_unlock to happen before the list_add in
352 set_current_state(return_to_userland
? TASK_INTERRUPTIBLE
:
354 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
356 must_wait
= userfaultfd_must_wait(ctx
, address
, flags
, reason
);
357 up_read(&mm
->mmap_sem
);
359 if (likely(must_wait
&& !ACCESS_ONCE(ctx
->released
) &&
360 (return_to_userland
? !signal_pending(current
) :
361 !fatal_signal_pending(current
)))) {
362 wake_up_poll(&ctx
->fd_wqh
, POLLIN
);
364 ret
|= VM_FAULT_MAJOR
;
367 __set_current_state(TASK_RUNNING
);
369 if (return_to_userland
) {
370 if (signal_pending(current
) &&
371 !fatal_signal_pending(current
)) {
373 * If we got a SIGSTOP or SIGCONT and this is
374 * a normal userland page fault, just let
375 * userland return so the signal will be
376 * handled and gdb debugging works. The page
377 * fault code immediately after we return from
378 * this function is going to release the
379 * mmap_sem and it's not depending on it
380 * (unlike gup would if we were not to return
383 * If a fatal signal is pending we still take
384 * the streamlined VM_FAULT_RETRY failure path
385 * and there's no need to retake the mmap_sem
388 down_read(&mm
->mmap_sem
);
394 * Here we race with the list_del; list_add in
395 * userfaultfd_ctx_read(), however because we don't ever run
396 * list_del_init() to refile across the two lists, the prev
397 * and next pointers will never point to self. list_add also
398 * would never let any of the two pointers to point to
399 * self. So list_empty_careful won't risk to see both pointers
400 * pointing to self at any time during the list refile. The
401 * only case where list_del_init() is called is the full
402 * removal in the wake function and there we don't re-list_add
403 * and it's fine not to block on the spinlock. The uwq on this
404 * kernel stack can be released after the list_del_init.
406 if (!list_empty_careful(&uwq
.wq
.task_list
)) {
407 spin_lock(&ctx
->fault_pending_wqh
.lock
);
409 * No need of list_del_init(), the uwq on the stack
410 * will be freed shortly anyway.
412 list_del(&uwq
.wq
.task_list
);
413 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
417 * ctx may go away after this if the userfault pseudo fd is
420 userfaultfd_ctx_put(ctx
);
426 static int userfaultfd_release(struct inode
*inode
, struct file
*file
)
428 struct userfaultfd_ctx
*ctx
= file
->private_data
;
429 struct mm_struct
*mm
= ctx
->mm
;
430 struct vm_area_struct
*vma
, *prev
;
431 /* len == 0 means wake all */
432 struct userfaultfd_wake_range range
= { .len
= 0, };
433 unsigned long new_flags
;
435 ACCESS_ONCE(ctx
->released
) = true;
438 * Flush page faults out of all CPUs. NOTE: all page faults
439 * must be retried without returning VM_FAULT_SIGBUS if
440 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
441 * changes while handle_userfault released the mmap_sem. So
442 * it's critical that released is set to true (above), before
443 * taking the mmap_sem for writing.
445 down_write(&mm
->mmap_sem
);
447 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
449 BUG_ON(!!vma
->vm_userfaultfd_ctx
.ctx
^
450 !!(vma
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
451 if (vma
->vm_userfaultfd_ctx
.ctx
!= ctx
) {
455 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
456 prev
= vma_merge(mm
, prev
, vma
->vm_start
, vma
->vm_end
,
457 new_flags
, vma
->anon_vma
,
458 vma
->vm_file
, vma
->vm_pgoff
,
465 vma
->vm_flags
= new_flags
;
466 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
468 up_write(&mm
->mmap_sem
);
471 * After no new page faults can wait on this fault_*wqh, flush
472 * the last page faults that may have been already waiting on
475 spin_lock(&ctx
->fault_pending_wqh
.lock
);
476 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
, &range
);
477 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, &range
);
478 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
480 wake_up_poll(&ctx
->fd_wqh
, POLLHUP
);
481 userfaultfd_ctx_put(ctx
);
485 /* fault_pending_wqh.lock must be hold by the caller */
486 static inline struct userfaultfd_wait_queue
*find_userfault(
487 struct userfaultfd_ctx
*ctx
)
490 struct userfaultfd_wait_queue
*uwq
;
492 VM_BUG_ON(!spin_is_locked(&ctx
->fault_pending_wqh
.lock
));
495 if (!waitqueue_active(&ctx
->fault_pending_wqh
))
497 /* walk in reverse to provide FIFO behavior to read userfaults */
498 wq
= list_last_entry(&ctx
->fault_pending_wqh
.task_list
,
499 typeof(*wq
), task_list
);
500 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
505 static unsigned int userfaultfd_poll(struct file
*file
, poll_table
*wait
)
507 struct userfaultfd_ctx
*ctx
= file
->private_data
;
510 poll_wait(file
, &ctx
->fd_wqh
, wait
);
512 switch (ctx
->state
) {
513 case UFFD_STATE_WAIT_API
:
515 case UFFD_STATE_RUNNING
:
517 * poll() never guarantees that read won't block.
518 * userfaults can be waken before they're read().
520 if (unlikely(!(file
->f_flags
& O_NONBLOCK
)))
523 * lockless access to see if there are pending faults
524 * __pollwait last action is the add_wait_queue but
525 * the spin_unlock would allow the waitqueue_active to
526 * pass above the actual list_add inside
527 * add_wait_queue critical section. So use a full
528 * memory barrier to serialize the list_add write of
529 * add_wait_queue() with the waitqueue_active read
534 if (waitqueue_active(&ctx
->fault_pending_wqh
))
542 static ssize_t
userfaultfd_ctx_read(struct userfaultfd_ctx
*ctx
, int no_wait
,
543 struct uffd_msg
*msg
)
546 DECLARE_WAITQUEUE(wait
, current
);
547 struct userfaultfd_wait_queue
*uwq
;
549 /* always take the fd_wqh lock before the fault_pending_wqh lock */
550 spin_lock(&ctx
->fd_wqh
.lock
);
551 __add_wait_queue(&ctx
->fd_wqh
, &wait
);
553 set_current_state(TASK_INTERRUPTIBLE
);
554 spin_lock(&ctx
->fault_pending_wqh
.lock
);
555 uwq
= find_userfault(ctx
);
558 * Use a seqcount to repeat the lockless check
559 * in wake_userfault() to avoid missing
560 * wakeups because during the refile both
561 * waitqueue could become empty if this is the
564 write_seqcount_begin(&ctx
->refile_seq
);
567 * The fault_pending_wqh.lock prevents the uwq
568 * to disappear from under us.
570 * Refile this userfault from
571 * fault_pending_wqh to fault_wqh, it's not
572 * pending anymore after we read it.
574 * Use list_del() by hand (as
575 * userfaultfd_wake_function also uses
576 * list_del_init() by hand) to be sure nobody
577 * changes __remove_wait_queue() to use
578 * list_del_init() in turn breaking the
579 * !list_empty_careful() check in
580 * handle_userfault(). The uwq->wq.task_list
581 * must never be empty at any time during the
582 * refile, or the waitqueue could disappear
583 * from under us. The "wait_queue_head_t"
584 * parameter of __remove_wait_queue() is unused
587 list_del(&uwq
->wq
.task_list
);
588 __add_wait_queue(&ctx
->fault_wqh
, &uwq
->wq
);
590 write_seqcount_end(&ctx
->refile_seq
);
592 /* careful to always initialize msg if ret == 0 */
594 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
598 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
599 if (signal_pending(current
)) {
607 spin_unlock(&ctx
->fd_wqh
.lock
);
609 spin_lock(&ctx
->fd_wqh
.lock
);
611 __remove_wait_queue(&ctx
->fd_wqh
, &wait
);
612 __set_current_state(TASK_RUNNING
);
613 spin_unlock(&ctx
->fd_wqh
.lock
);
618 static ssize_t
userfaultfd_read(struct file
*file
, char __user
*buf
,
619 size_t count
, loff_t
*ppos
)
621 struct userfaultfd_ctx
*ctx
= file
->private_data
;
622 ssize_t _ret
, ret
= 0;
624 int no_wait
= file
->f_flags
& O_NONBLOCK
;
626 if (ctx
->state
== UFFD_STATE_WAIT_API
)
630 if (count
< sizeof(msg
))
631 return ret
? ret
: -EINVAL
;
632 _ret
= userfaultfd_ctx_read(ctx
, no_wait
, &msg
);
634 return ret
? ret
: _ret
;
635 if (copy_to_user((__u64 __user
*) buf
, &msg
, sizeof(msg
)))
636 return ret
? ret
: -EFAULT
;
639 count
-= sizeof(msg
);
641 * Allow to read more than one fault at time but only
642 * block if waiting for the very first one.
644 no_wait
= O_NONBLOCK
;
648 static void __wake_userfault(struct userfaultfd_ctx
*ctx
,
649 struct userfaultfd_wake_range
*range
)
651 unsigned long start
, end
;
653 start
= range
->start
;
654 end
= range
->start
+ range
->len
;
656 spin_lock(&ctx
->fault_pending_wqh
.lock
);
657 /* wake all in the range and autoremove */
658 if (waitqueue_active(&ctx
->fault_pending_wqh
))
659 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
,
661 if (waitqueue_active(&ctx
->fault_wqh
))
662 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, range
);
663 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
666 static __always_inline
void wake_userfault(struct userfaultfd_ctx
*ctx
,
667 struct userfaultfd_wake_range
*range
)
673 * To be sure waitqueue_active() is not reordered by the CPU
674 * before the pagetable update, use an explicit SMP memory
675 * barrier here. PT lock release or up_read(mmap_sem) still
676 * have release semantics that can allow the
677 * waitqueue_active() to be reordered before the pte update.
682 * Use waitqueue_active because it's very frequent to
683 * change the address space atomically even if there are no
684 * userfaults yet. So we take the spinlock only when we're
685 * sure we've userfaults to wake.
688 seq
= read_seqcount_begin(&ctx
->refile_seq
);
689 need_wakeup
= waitqueue_active(&ctx
->fault_pending_wqh
) ||
690 waitqueue_active(&ctx
->fault_wqh
);
692 } while (read_seqcount_retry(&ctx
->refile_seq
, seq
));
694 __wake_userfault(ctx
, range
);
697 static __always_inline
int validate_range(struct mm_struct
*mm
,
698 __u64 start
, __u64 len
)
700 __u64 task_size
= mm
->task_size
;
702 if (start
& ~PAGE_MASK
)
704 if (len
& ~PAGE_MASK
)
708 if (start
< mmap_min_addr
)
710 if (start
>= task_size
)
712 if (len
> task_size
- start
)
717 static int userfaultfd_register(struct userfaultfd_ctx
*ctx
,
720 struct mm_struct
*mm
= ctx
->mm
;
721 struct vm_area_struct
*vma
, *prev
, *cur
;
723 struct uffdio_register uffdio_register
;
724 struct uffdio_register __user
*user_uffdio_register
;
725 unsigned long vm_flags
, new_flags
;
727 unsigned long start
, end
, vma_end
;
729 user_uffdio_register
= (struct uffdio_register __user
*) arg
;
732 if (copy_from_user(&uffdio_register
, user_uffdio_register
,
733 sizeof(uffdio_register
)-sizeof(__u64
)))
737 if (!uffdio_register
.mode
)
739 if (uffdio_register
.mode
& ~(UFFDIO_REGISTER_MODE_MISSING
|
740 UFFDIO_REGISTER_MODE_WP
))
743 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_MISSING
)
744 vm_flags
|= VM_UFFD_MISSING
;
745 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_WP
) {
746 vm_flags
|= VM_UFFD_WP
;
748 * FIXME: remove the below error constraint by
749 * implementing the wprotect tracking mode.
755 ret
= validate_range(mm
, uffdio_register
.range
.start
,
756 uffdio_register
.range
.len
);
760 start
= uffdio_register
.range
.start
;
761 end
= start
+ uffdio_register
.range
.len
;
763 down_write(&mm
->mmap_sem
);
764 vma
= find_vma_prev(mm
, start
, &prev
);
770 /* check that there's at least one vma in the range */
772 if (vma
->vm_start
>= end
)
776 * Search for not compatible vmas.
778 * FIXME: this shall be relaxed later so that it doesn't fail
779 * on tmpfs backed vmas (in addition to the current allowance
780 * on anonymous vmas).
783 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
786 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
787 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
789 /* check not compatible vmas */
795 * Check that this vma isn't already owned by a
796 * different userfaultfd. We can't allow more than one
797 * userfaultfd to own a single vma simultaneously or we
798 * wouldn't know which one to deliver the userfaults to.
801 if (cur
->vm_userfaultfd_ctx
.ctx
&&
802 cur
->vm_userfaultfd_ctx
.ctx
!= ctx
)
809 if (vma
->vm_start
< start
)
817 BUG_ON(vma
->vm_userfaultfd_ctx
.ctx
&&
818 vma
->vm_userfaultfd_ctx
.ctx
!= ctx
);
821 * Nothing to do: this vma is already registered into this
822 * userfaultfd and with the right tracking mode too.
824 if (vma
->vm_userfaultfd_ctx
.ctx
== ctx
&&
825 (vma
->vm_flags
& vm_flags
) == vm_flags
)
828 if (vma
->vm_start
> start
)
829 start
= vma
->vm_start
;
830 vma_end
= min(end
, vma
->vm_end
);
832 new_flags
= (vma
->vm_flags
& ~vm_flags
) | vm_flags
;
833 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
834 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
836 ((struct vm_userfaultfd_ctx
){ ctx
}));
841 if (vma
->vm_start
< start
) {
842 ret
= split_vma(mm
, vma
, start
, 1);
846 if (vma
->vm_end
> end
) {
847 ret
= split_vma(mm
, vma
, end
, 0);
853 * In the vma_merge() successful mprotect-like case 8:
854 * the next vma was merged into the current one and
855 * the current one has not been updated yet.
857 vma
->vm_flags
= new_flags
;
858 vma
->vm_userfaultfd_ctx
.ctx
= ctx
;
864 } while (vma
&& vma
->vm_start
< end
);
866 up_write(&mm
->mmap_sem
);
869 * Now that we scanned all vmas we can already tell
870 * userland which ioctls methods are guaranteed to
871 * succeed on this range.
873 if (put_user(UFFD_API_RANGE_IOCTLS
,
874 &user_uffdio_register
->ioctls
))
881 static int userfaultfd_unregister(struct userfaultfd_ctx
*ctx
,
884 struct mm_struct
*mm
= ctx
->mm
;
885 struct vm_area_struct
*vma
, *prev
, *cur
;
887 struct uffdio_range uffdio_unregister
;
888 unsigned long new_flags
;
890 unsigned long start
, end
, vma_end
;
891 const void __user
*buf
= (void __user
*)arg
;
894 if (copy_from_user(&uffdio_unregister
, buf
, sizeof(uffdio_unregister
)))
897 ret
= validate_range(mm
, uffdio_unregister
.start
,
898 uffdio_unregister
.len
);
902 start
= uffdio_unregister
.start
;
903 end
= start
+ uffdio_unregister
.len
;
905 down_write(&mm
->mmap_sem
);
906 vma
= find_vma_prev(mm
, start
, &prev
);
912 /* check that there's at least one vma in the range */
914 if (vma
->vm_start
>= end
)
918 * Search for not compatible vmas.
920 * FIXME: this shall be relaxed later so that it doesn't fail
921 * on tmpfs backed vmas (in addition to the current allowance
922 * on anonymous vmas).
926 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
929 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
930 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
933 * Check not compatible vmas, not strictly required
934 * here as not compatible vmas cannot have an
935 * userfaultfd_ctx registered on them, but this
936 * provides for more strict behavior to notice
937 * unregistration errors.
946 if (vma
->vm_start
< start
)
956 * Nothing to do: this vma is already registered into this
957 * userfaultfd and with the right tracking mode too.
959 if (!vma
->vm_userfaultfd_ctx
.ctx
)
962 if (vma
->vm_start
> start
)
963 start
= vma
->vm_start
;
964 vma_end
= min(end
, vma
->vm_end
);
966 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
967 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
968 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
975 if (vma
->vm_start
< start
) {
976 ret
= split_vma(mm
, vma
, start
, 1);
980 if (vma
->vm_end
> end
) {
981 ret
= split_vma(mm
, vma
, end
, 0);
987 * In the vma_merge() successful mprotect-like case 8:
988 * the next vma was merged into the current one and
989 * the current one has not been updated yet.
991 vma
->vm_flags
= new_flags
;
992 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
998 } while (vma
&& vma
->vm_start
< end
);
1000 up_write(&mm
->mmap_sem
);
1006 * userfaultfd_wake may be used in combination with the
1007 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
1009 static int userfaultfd_wake(struct userfaultfd_ctx
*ctx
,
1013 struct uffdio_range uffdio_wake
;
1014 struct userfaultfd_wake_range range
;
1015 const void __user
*buf
= (void __user
*)arg
;
1018 if (copy_from_user(&uffdio_wake
, buf
, sizeof(uffdio_wake
)))
1021 ret
= validate_range(ctx
->mm
, uffdio_wake
.start
, uffdio_wake
.len
);
1025 range
.start
= uffdio_wake
.start
;
1026 range
.len
= uffdio_wake
.len
;
1029 * len == 0 means wake all and we don't want to wake all here,
1030 * so check it again to be sure.
1032 VM_BUG_ON(!range
.len
);
1034 wake_userfault(ctx
, &range
);
1041 static int userfaultfd_copy(struct userfaultfd_ctx
*ctx
,
1045 struct uffdio_copy uffdio_copy
;
1046 struct uffdio_copy __user
*user_uffdio_copy
;
1047 struct userfaultfd_wake_range range
;
1049 user_uffdio_copy
= (struct uffdio_copy __user
*) arg
;
1052 if (copy_from_user(&uffdio_copy
, user_uffdio_copy
,
1053 /* don't copy "copy" last field */
1054 sizeof(uffdio_copy
)-sizeof(__s64
)))
1057 ret
= validate_range(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.len
);
1061 * double check for wraparound just in case. copy_from_user()
1062 * will later check uffdio_copy.src + uffdio_copy.len to fit
1063 * in the userland range.
1066 if (uffdio_copy
.src
+ uffdio_copy
.len
<= uffdio_copy
.src
)
1068 if (uffdio_copy
.mode
& ~UFFDIO_COPY_MODE_DONTWAKE
)
1071 ret
= mcopy_atomic(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.src
,
1073 if (unlikely(put_user(ret
, &user_uffdio_copy
->copy
)))
1078 /* len == 0 would wake all */
1080 if (!(uffdio_copy
.mode
& UFFDIO_COPY_MODE_DONTWAKE
)) {
1081 range
.start
= uffdio_copy
.dst
;
1082 wake_userfault(ctx
, &range
);
1084 ret
= range
.len
== uffdio_copy
.len
? 0 : -EAGAIN
;
1089 static int userfaultfd_zeropage(struct userfaultfd_ctx
*ctx
,
1093 struct uffdio_zeropage uffdio_zeropage
;
1094 struct uffdio_zeropage __user
*user_uffdio_zeropage
;
1095 struct userfaultfd_wake_range range
;
1097 user_uffdio_zeropage
= (struct uffdio_zeropage __user
*) arg
;
1100 if (copy_from_user(&uffdio_zeropage
, user_uffdio_zeropage
,
1101 /* don't copy "zeropage" last field */
1102 sizeof(uffdio_zeropage
)-sizeof(__s64
)))
1105 ret
= validate_range(ctx
->mm
, uffdio_zeropage
.range
.start
,
1106 uffdio_zeropage
.range
.len
);
1110 if (uffdio_zeropage
.mode
& ~UFFDIO_ZEROPAGE_MODE_DONTWAKE
)
1113 ret
= mfill_zeropage(ctx
->mm
, uffdio_zeropage
.range
.start
,
1114 uffdio_zeropage
.range
.len
);
1115 if (unlikely(put_user(ret
, &user_uffdio_zeropage
->zeropage
)))
1119 /* len == 0 would wake all */
1122 if (!(uffdio_zeropage
.mode
& UFFDIO_ZEROPAGE_MODE_DONTWAKE
)) {
1123 range
.start
= uffdio_zeropage
.range
.start
;
1124 wake_userfault(ctx
, &range
);
1126 ret
= range
.len
== uffdio_zeropage
.range
.len
? 0 : -EAGAIN
;
1132 * userland asks for a certain API version and we return which bits
1133 * and ioctl commands are implemented in this kernel for such API
1134 * version or -EINVAL if unknown.
1136 static int userfaultfd_api(struct userfaultfd_ctx
*ctx
,
1139 struct uffdio_api uffdio_api
;
1140 void __user
*buf
= (void __user
*)arg
;
1144 if (ctx
->state
!= UFFD_STATE_WAIT_API
)
1147 if (copy_from_user(&uffdio_api
, buf
, sizeof(uffdio_api
)))
1149 if (uffdio_api
.api
!= UFFD_API
|| uffdio_api
.features
) {
1150 memset(&uffdio_api
, 0, sizeof(uffdio_api
));
1151 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1156 uffdio_api
.features
= UFFD_API_FEATURES
;
1157 uffdio_api
.ioctls
= UFFD_API_IOCTLS
;
1159 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1161 ctx
->state
= UFFD_STATE_RUNNING
;
1167 static long userfaultfd_ioctl(struct file
*file
, unsigned cmd
,
1171 struct userfaultfd_ctx
*ctx
= file
->private_data
;
1173 if (cmd
!= UFFDIO_API
&& ctx
->state
== UFFD_STATE_WAIT_API
)
1178 ret
= userfaultfd_api(ctx
, arg
);
1180 case UFFDIO_REGISTER
:
1181 ret
= userfaultfd_register(ctx
, arg
);
1183 case UFFDIO_UNREGISTER
:
1184 ret
= userfaultfd_unregister(ctx
, arg
);
1187 ret
= userfaultfd_wake(ctx
, arg
);
1190 ret
= userfaultfd_copy(ctx
, arg
);
1192 case UFFDIO_ZEROPAGE
:
1193 ret
= userfaultfd_zeropage(ctx
, arg
);
1199 #ifdef CONFIG_PROC_FS
1200 static void userfaultfd_show_fdinfo(struct seq_file
*m
, struct file
*f
)
1202 struct userfaultfd_ctx
*ctx
= f
->private_data
;
1204 struct userfaultfd_wait_queue
*uwq
;
1205 unsigned long pending
= 0, total
= 0;
1207 spin_lock(&ctx
->fault_pending_wqh
.lock
);
1208 list_for_each_entry(wq
, &ctx
->fault_pending_wqh
.task_list
, task_list
) {
1209 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1213 list_for_each_entry(wq
, &ctx
->fault_wqh
.task_list
, task_list
) {
1214 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1217 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
1220 * If more protocols will be added, there will be all shown
1221 * separated by a space. Like this:
1222 * protocols: aa:... bb:...
1224 seq_printf(m
, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
1225 pending
, total
, UFFD_API
, UFFD_API_FEATURES
,
1226 UFFD_API_IOCTLS
|UFFD_API_RANGE_IOCTLS
);
1230 static const struct file_operations userfaultfd_fops
= {
1231 #ifdef CONFIG_PROC_FS
1232 .show_fdinfo
= userfaultfd_show_fdinfo
,
1234 .release
= userfaultfd_release
,
1235 .poll
= userfaultfd_poll
,
1236 .read
= userfaultfd_read
,
1237 .unlocked_ioctl
= userfaultfd_ioctl
,
1238 .compat_ioctl
= userfaultfd_ioctl
,
1239 .llseek
= noop_llseek
,
1242 static void init_once_userfaultfd_ctx(void *mem
)
1244 struct userfaultfd_ctx
*ctx
= (struct userfaultfd_ctx
*) mem
;
1246 init_waitqueue_head(&ctx
->fault_pending_wqh
);
1247 init_waitqueue_head(&ctx
->fault_wqh
);
1248 init_waitqueue_head(&ctx
->fd_wqh
);
1249 seqcount_init(&ctx
->refile_seq
);
1253 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1254 * @flags: Flags for the userfaultfd file.
1256 * This function creates an userfaultfd file pointer, w/out installing
1257 * it into the fd table. This is useful when the userfaultfd file is
1258 * used during the initialization of data structures that require
1259 * extra setup after the userfaultfd creation. So the userfaultfd
1260 * creation is split into the file pointer creation phase, and the
1261 * file descriptor installation phase. In this way races with
1262 * userspace closing the newly installed file descriptor can be
1263 * avoided. Returns an userfaultfd file pointer, or a proper error
1266 static struct file
*userfaultfd_file_create(int flags
)
1269 struct userfaultfd_ctx
*ctx
;
1271 BUG_ON(!current
->mm
);
1273 /* Check the UFFD_* constants for consistency. */
1274 BUILD_BUG_ON(UFFD_CLOEXEC
!= O_CLOEXEC
);
1275 BUILD_BUG_ON(UFFD_NONBLOCK
!= O_NONBLOCK
);
1277 file
= ERR_PTR(-EINVAL
);
1278 if (flags
& ~UFFD_SHARED_FCNTL_FLAGS
)
1281 file
= ERR_PTR(-ENOMEM
);
1282 ctx
= kmem_cache_alloc(userfaultfd_ctx_cachep
, GFP_KERNEL
);
1286 atomic_set(&ctx
->refcount
, 1);
1288 ctx
->state
= UFFD_STATE_WAIT_API
;
1289 ctx
->released
= false;
1290 ctx
->mm
= current
->mm
;
1291 /* prevent the mm struct to be freed */
1292 atomic_inc(&ctx
->mm
->mm_users
);
1294 file
= anon_inode_getfile("[userfaultfd]", &userfaultfd_fops
, ctx
,
1295 O_RDWR
| (flags
& UFFD_SHARED_FCNTL_FLAGS
));
1298 kmem_cache_free(userfaultfd_ctx_cachep
, ctx
);
1304 SYSCALL_DEFINE1(userfaultfd
, int, flags
)
1309 error
= get_unused_fd_flags(flags
& UFFD_SHARED_FCNTL_FLAGS
);
1314 file
= userfaultfd_file_create(flags
);
1316 error
= PTR_ERR(file
);
1317 goto err_put_unused_fd
;
1319 fd_install(fd
, file
);
1329 static int __init
userfaultfd_init(void)
1331 userfaultfd_ctx_cachep
= kmem_cache_create("userfaultfd_ctx_cache",
1332 sizeof(struct userfaultfd_ctx
),
1334 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
1335 init_once_userfaultfd_ctx
);
1338 __initcall(userfaultfd_init
);