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 /* pseudo fd refcounting */
50 /* userfaultfd syscall flags */
53 enum userfaultfd_state state
;
56 /* mm with one ore more vmas attached to this userfaultfd_ctx */
60 struct userfaultfd_wait_queue
{
63 struct userfaultfd_ctx
*ctx
;
66 struct userfaultfd_wake_range
{
71 static int userfaultfd_wake_function(wait_queue_t
*wq
, unsigned mode
,
72 int wake_flags
, void *key
)
74 struct userfaultfd_wake_range
*range
= key
;
76 struct userfaultfd_wait_queue
*uwq
;
77 unsigned long start
, len
;
79 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
81 /* len == 0 means wake all */
84 if (len
&& (start
> uwq
->msg
.arg
.pagefault
.address
||
85 start
+ len
<= uwq
->msg
.arg
.pagefault
.address
))
87 ret
= wake_up_state(wq
->private, mode
);
90 * Wake only once, autoremove behavior.
92 * After the effect of list_del_init is visible to the
93 * other CPUs, the waitqueue may disappear from under
94 * us, see the !list_empty_careful() in
95 * handle_userfault(). try_to_wake_up() has an
96 * implicit smp_mb__before_spinlock, and the
97 * wq->private is read before calling the extern
98 * function "wake_up_state" (which in turns calls
99 * try_to_wake_up). While the spin_lock;spin_unlock;
100 * wouldn't be enough, the smp_mb__before_spinlock is
101 * enough to avoid an explicit smp_mb() here.
103 list_del_init(&wq
->task_list
);
109 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
111 * @ctx: [in] Pointer to the userfaultfd context.
113 * Returns: In case of success, returns not zero.
115 static void userfaultfd_ctx_get(struct userfaultfd_ctx
*ctx
)
117 if (!atomic_inc_not_zero(&ctx
->refcount
))
122 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
124 * @ctx: [in] Pointer to userfaultfd context.
126 * The userfaultfd context reference must have been previously acquired either
127 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
129 static void userfaultfd_ctx_put(struct userfaultfd_ctx
*ctx
)
131 if (atomic_dec_and_test(&ctx
->refcount
)) {
132 VM_BUG_ON(spin_is_locked(&ctx
->fault_pending_wqh
.lock
));
133 VM_BUG_ON(waitqueue_active(&ctx
->fault_pending_wqh
));
134 VM_BUG_ON(spin_is_locked(&ctx
->fault_wqh
.lock
));
135 VM_BUG_ON(waitqueue_active(&ctx
->fault_wqh
));
136 VM_BUG_ON(spin_is_locked(&ctx
->fd_wqh
.lock
));
137 VM_BUG_ON(waitqueue_active(&ctx
->fd_wqh
));
139 kmem_cache_free(userfaultfd_ctx_cachep
, ctx
);
143 static inline void msg_init(struct uffd_msg
*msg
)
145 BUILD_BUG_ON(sizeof(struct uffd_msg
) != 32);
147 * Must use memset to zero out the paddings or kernel data is
148 * leaked to userland.
150 memset(msg
, 0, sizeof(struct uffd_msg
));
153 static inline struct uffd_msg
userfault_msg(unsigned long address
,
155 unsigned long reason
)
159 msg
.event
= UFFD_EVENT_PAGEFAULT
;
160 msg
.arg
.pagefault
.address
= address
;
161 if (flags
& FAULT_FLAG_WRITE
)
163 * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
164 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
165 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
166 * was a read fault, otherwise if set it means it's
169 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WRITE
;
170 if (reason
& VM_UFFD_WP
)
172 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
173 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
174 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
175 * a missing fault, otherwise if set it means it's a
176 * write protect fault.
178 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WP
;
183 * Verify the pagetables are still not ok after having reigstered into
184 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
185 * userfault that has already been resolved, if userfaultfd_read and
186 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
189 static inline bool userfaultfd_must_wait(struct userfaultfd_ctx
*ctx
,
190 unsigned long address
,
192 unsigned long reason
)
194 struct mm_struct
*mm
= ctx
->mm
;
201 VM_BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
203 pgd
= pgd_offset(mm
, address
);
204 if (!pgd_present(*pgd
))
206 pud
= pud_offset(pgd
, address
);
207 if (!pud_present(*pud
))
209 pmd
= pmd_offset(pud
, address
);
211 * READ_ONCE must function as a barrier with narrower scope
212 * and it must be equivalent to:
213 * _pmd = *pmd; barrier();
215 * This is to deal with the instability (as in
216 * pmd_trans_unstable) of the pmd.
218 _pmd
= READ_ONCE(*pmd
);
219 if (!pmd_present(_pmd
))
223 if (pmd_trans_huge(_pmd
))
227 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
228 * and use the standard pte_offset_map() instead of parsing _pmd.
230 pte
= pte_offset_map(pmd
, address
);
232 * Lockless access: we're in a wait_event so it's ok if it
244 * The locking rules involved in returning VM_FAULT_RETRY depending on
245 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
246 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
247 * recommendation in __lock_page_or_retry is not an understatement.
249 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
250 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
253 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
254 * set, VM_FAULT_RETRY can still be returned if and only if there are
255 * fatal_signal_pending()s, and the mmap_sem must be released before
258 int handle_userfault(struct vm_area_struct
*vma
, unsigned long address
,
259 unsigned int flags
, unsigned long reason
)
261 struct mm_struct
*mm
= vma
->vm_mm
;
262 struct userfaultfd_ctx
*ctx
;
263 struct userfaultfd_wait_queue uwq
;
265 bool must_wait
, return_to_userland
;
267 BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
269 ret
= VM_FAULT_SIGBUS
;
270 ctx
= vma
->vm_userfaultfd_ctx
.ctx
;
274 BUG_ON(ctx
->mm
!= mm
);
276 VM_BUG_ON(reason
& ~(VM_UFFD_MISSING
|VM_UFFD_WP
));
277 VM_BUG_ON(!(reason
& VM_UFFD_MISSING
) ^ !!(reason
& VM_UFFD_WP
));
280 * If it's already released don't get it. This avoids to loop
281 * in __get_user_pages if userfaultfd_release waits on the
282 * caller of handle_userfault to release the mmap_sem.
284 if (unlikely(ACCESS_ONCE(ctx
->released
)))
288 * Check that we can return VM_FAULT_RETRY.
290 * NOTE: it should become possible to return VM_FAULT_RETRY
291 * even if FAULT_FLAG_TRIED is set without leading to gup()
292 * -EBUSY failures, if the userfaultfd is to be extended for
293 * VM_UFFD_WP tracking and we intend to arm the userfault
294 * without first stopping userland access to the memory. For
295 * VM_UFFD_MISSING userfaults this is enough for now.
297 if (unlikely(!(flags
& FAULT_FLAG_ALLOW_RETRY
))) {
299 * Validate the invariant that nowait must allow retry
300 * to be sure not to return SIGBUS erroneously on
301 * nowait invocations.
303 BUG_ON(flags
& FAULT_FLAG_RETRY_NOWAIT
);
304 #ifdef CONFIG_DEBUG_VM
305 if (printk_ratelimit()) {
307 "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags
);
315 * Handle nowait, not much to do other than tell it to retry
318 ret
= VM_FAULT_RETRY
;
319 if (flags
& FAULT_FLAG_RETRY_NOWAIT
)
322 /* take the reference before dropping the mmap_sem */
323 userfaultfd_ctx_get(ctx
);
325 init_waitqueue_func_entry(&uwq
.wq
, userfaultfd_wake_function
);
326 uwq
.wq
.private = current
;
327 uwq
.msg
= userfault_msg(address
, flags
, reason
);
330 return_to_userland
= (flags
& (FAULT_FLAG_USER
|FAULT_FLAG_KILLABLE
)) ==
331 (FAULT_FLAG_USER
|FAULT_FLAG_KILLABLE
);
333 spin_lock(&ctx
->fault_pending_wqh
.lock
);
335 * After the __add_wait_queue the uwq is visible to userland
336 * through poll/read().
338 __add_wait_queue(&ctx
->fault_pending_wqh
, &uwq
.wq
);
340 * The smp_mb() after __set_current_state prevents the reads
341 * following the spin_unlock to happen before the list_add in
344 set_current_state(return_to_userland
? TASK_INTERRUPTIBLE
:
346 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
348 must_wait
= userfaultfd_must_wait(ctx
, address
, flags
, reason
);
349 up_read(&mm
->mmap_sem
);
351 if (likely(must_wait
&& !ACCESS_ONCE(ctx
->released
) &&
352 (return_to_userland
? !signal_pending(current
) :
353 !fatal_signal_pending(current
)))) {
354 wake_up_poll(&ctx
->fd_wqh
, POLLIN
);
356 ret
|= VM_FAULT_MAJOR
;
359 __set_current_state(TASK_RUNNING
);
361 if (return_to_userland
) {
362 if (signal_pending(current
) &&
363 !fatal_signal_pending(current
)) {
365 * If we got a SIGSTOP or SIGCONT and this is
366 * a normal userland page fault, just let
367 * userland return so the signal will be
368 * handled and gdb debugging works. The page
369 * fault code immediately after we return from
370 * this function is going to release the
371 * mmap_sem and it's not depending on it
372 * (unlike gup would if we were not to return
375 * If a fatal signal is pending we still take
376 * the streamlined VM_FAULT_RETRY failure path
377 * and there's no need to retake the mmap_sem
380 down_read(&mm
->mmap_sem
);
386 * Here we race with the list_del; list_add in
387 * userfaultfd_ctx_read(), however because we don't ever run
388 * list_del_init() to refile across the two lists, the prev
389 * and next pointers will never point to self. list_add also
390 * would never let any of the two pointers to point to
391 * self. So list_empty_careful won't risk to see both pointers
392 * pointing to self at any time during the list refile. The
393 * only case where list_del_init() is called is the full
394 * removal in the wake function and there we don't re-list_add
395 * and it's fine not to block on the spinlock. The uwq on this
396 * kernel stack can be released after the list_del_init.
398 if (!list_empty_careful(&uwq
.wq
.task_list
)) {
399 spin_lock(&ctx
->fault_pending_wqh
.lock
);
401 * No need of list_del_init(), the uwq on the stack
402 * will be freed shortly anyway.
404 list_del(&uwq
.wq
.task_list
);
405 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
409 * ctx may go away after this if the userfault pseudo fd is
412 userfaultfd_ctx_put(ctx
);
418 static int userfaultfd_release(struct inode
*inode
, struct file
*file
)
420 struct userfaultfd_ctx
*ctx
= file
->private_data
;
421 struct mm_struct
*mm
= ctx
->mm
;
422 struct vm_area_struct
*vma
, *prev
;
423 /* len == 0 means wake all */
424 struct userfaultfd_wake_range range
= { .len
= 0, };
425 unsigned long new_flags
;
427 ACCESS_ONCE(ctx
->released
) = true;
430 * Flush page faults out of all CPUs. NOTE: all page faults
431 * must be retried without returning VM_FAULT_SIGBUS if
432 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
433 * changes while handle_userfault released the mmap_sem. So
434 * it's critical that released is set to true (above), before
435 * taking the mmap_sem for writing.
437 down_write(&mm
->mmap_sem
);
439 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
441 BUG_ON(!!vma
->vm_userfaultfd_ctx
.ctx
^
442 !!(vma
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
443 if (vma
->vm_userfaultfd_ctx
.ctx
!= ctx
) {
447 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
448 prev
= vma_merge(mm
, prev
, vma
->vm_start
, vma
->vm_end
,
449 new_flags
, vma
->anon_vma
,
450 vma
->vm_file
, vma
->vm_pgoff
,
457 vma
->vm_flags
= new_flags
;
458 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
460 up_write(&mm
->mmap_sem
);
463 * After no new page faults can wait on this fault_*wqh, flush
464 * the last page faults that may have been already waiting on
467 spin_lock(&ctx
->fault_pending_wqh
.lock
);
468 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
, 0, &range
);
469 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, 0, &range
);
470 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
472 wake_up_poll(&ctx
->fd_wqh
, POLLHUP
);
473 userfaultfd_ctx_put(ctx
);
477 /* fault_pending_wqh.lock must be hold by the caller */
478 static inline struct userfaultfd_wait_queue
*find_userfault(
479 struct userfaultfd_ctx
*ctx
)
482 struct userfaultfd_wait_queue
*uwq
;
484 VM_BUG_ON(!spin_is_locked(&ctx
->fault_pending_wqh
.lock
));
487 if (!waitqueue_active(&ctx
->fault_pending_wqh
))
489 /* walk in reverse to provide FIFO behavior to read userfaults */
490 wq
= list_last_entry(&ctx
->fault_pending_wqh
.task_list
,
491 typeof(*wq
), task_list
);
492 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
497 static unsigned int userfaultfd_poll(struct file
*file
, poll_table
*wait
)
499 struct userfaultfd_ctx
*ctx
= file
->private_data
;
502 poll_wait(file
, &ctx
->fd_wqh
, wait
);
504 switch (ctx
->state
) {
505 case UFFD_STATE_WAIT_API
:
507 case UFFD_STATE_RUNNING
:
509 * poll() never guarantees that read won't block.
510 * userfaults can be waken before they're read().
512 if (unlikely(!(file
->f_flags
& O_NONBLOCK
)))
515 * lockless access to see if there are pending faults
516 * __pollwait last action is the add_wait_queue but
517 * the spin_unlock would allow the waitqueue_active to
518 * pass above the actual list_add inside
519 * add_wait_queue critical section. So use a full
520 * memory barrier to serialize the list_add write of
521 * add_wait_queue() with the waitqueue_active read
526 if (waitqueue_active(&ctx
->fault_pending_wqh
))
534 static ssize_t
userfaultfd_ctx_read(struct userfaultfd_ctx
*ctx
, int no_wait
,
535 struct uffd_msg
*msg
)
538 DECLARE_WAITQUEUE(wait
, current
);
539 struct userfaultfd_wait_queue
*uwq
;
541 /* always take the fd_wqh lock before the fault_pending_wqh lock */
542 spin_lock(&ctx
->fd_wqh
.lock
);
543 __add_wait_queue(&ctx
->fd_wqh
, &wait
);
545 set_current_state(TASK_INTERRUPTIBLE
);
546 spin_lock(&ctx
->fault_pending_wqh
.lock
);
547 uwq
= find_userfault(ctx
);
550 * The fault_pending_wqh.lock prevents the uwq
551 * to disappear from under us.
553 * Refile this userfault from
554 * fault_pending_wqh to fault_wqh, it's not
555 * pending anymore after we read it.
557 * Use list_del() by hand (as
558 * userfaultfd_wake_function also uses
559 * list_del_init() by hand) to be sure nobody
560 * changes __remove_wait_queue() to use
561 * list_del_init() in turn breaking the
562 * !list_empty_careful() check in
563 * handle_userfault(). The uwq->wq.task_list
564 * must never be empty at any time during the
565 * refile, or the waitqueue could disappear
566 * from under us. The "wait_queue_head_t"
567 * parameter of __remove_wait_queue() is unused
570 list_del(&uwq
->wq
.task_list
);
571 __add_wait_queue(&ctx
->fault_wqh
, &uwq
->wq
);
573 /* careful to always initialize msg if ret == 0 */
575 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
579 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
580 if (signal_pending(current
)) {
588 spin_unlock(&ctx
->fd_wqh
.lock
);
590 spin_lock(&ctx
->fd_wqh
.lock
);
592 __remove_wait_queue(&ctx
->fd_wqh
, &wait
);
593 __set_current_state(TASK_RUNNING
);
594 spin_unlock(&ctx
->fd_wqh
.lock
);
599 static ssize_t
userfaultfd_read(struct file
*file
, char __user
*buf
,
600 size_t count
, loff_t
*ppos
)
602 struct userfaultfd_ctx
*ctx
= file
->private_data
;
603 ssize_t _ret
, ret
= 0;
605 int no_wait
= file
->f_flags
& O_NONBLOCK
;
607 if (ctx
->state
== UFFD_STATE_WAIT_API
)
611 if (count
< sizeof(msg
))
612 return ret
? ret
: -EINVAL
;
613 _ret
= userfaultfd_ctx_read(ctx
, no_wait
, &msg
);
615 return ret
? ret
: _ret
;
616 if (copy_to_user((__u64 __user
*) buf
, &msg
, sizeof(msg
)))
617 return ret
? ret
: -EFAULT
;
620 count
-= sizeof(msg
);
622 * Allow to read more than one fault at time but only
623 * block if waiting for the very first one.
625 no_wait
= O_NONBLOCK
;
629 static void __wake_userfault(struct userfaultfd_ctx
*ctx
,
630 struct userfaultfd_wake_range
*range
)
632 unsigned long start
, end
;
634 start
= range
->start
;
635 end
= range
->start
+ range
->len
;
637 spin_lock(&ctx
->fault_pending_wqh
.lock
);
638 /* wake all in the range and autoremove */
639 if (waitqueue_active(&ctx
->fault_pending_wqh
))
640 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
, 0,
642 if (waitqueue_active(&ctx
->fault_wqh
))
643 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, 0, range
);
644 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
647 static __always_inline
void wake_userfault(struct userfaultfd_ctx
*ctx
,
648 struct userfaultfd_wake_range
*range
)
651 * To be sure waitqueue_active() is not reordered by the CPU
652 * before the pagetable update, use an explicit SMP memory
653 * barrier here. PT lock release or up_read(mmap_sem) still
654 * have release semantics that can allow the
655 * waitqueue_active() to be reordered before the pte update.
660 * Use waitqueue_active because it's very frequent to
661 * change the address space atomically even if there are no
662 * userfaults yet. So we take the spinlock only when we're
663 * sure we've userfaults to wake.
665 if (waitqueue_active(&ctx
->fault_pending_wqh
) ||
666 waitqueue_active(&ctx
->fault_wqh
))
667 __wake_userfault(ctx
, range
);
670 static __always_inline
int validate_range(struct mm_struct
*mm
,
671 __u64 start
, __u64 len
)
673 __u64 task_size
= mm
->task_size
;
675 if (start
& ~PAGE_MASK
)
677 if (len
& ~PAGE_MASK
)
681 if (start
< mmap_min_addr
)
683 if (start
>= task_size
)
685 if (len
> task_size
- start
)
690 static int userfaultfd_register(struct userfaultfd_ctx
*ctx
,
693 struct mm_struct
*mm
= ctx
->mm
;
694 struct vm_area_struct
*vma
, *prev
, *cur
;
696 struct uffdio_register uffdio_register
;
697 struct uffdio_register __user
*user_uffdio_register
;
698 unsigned long vm_flags
, new_flags
;
700 unsigned long start
, end
, vma_end
;
702 user_uffdio_register
= (struct uffdio_register __user
*) arg
;
705 if (copy_from_user(&uffdio_register
, user_uffdio_register
,
706 sizeof(uffdio_register
)-sizeof(__u64
)))
710 if (!uffdio_register
.mode
)
712 if (uffdio_register
.mode
& ~(UFFDIO_REGISTER_MODE_MISSING
|
713 UFFDIO_REGISTER_MODE_WP
))
716 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_MISSING
)
717 vm_flags
|= VM_UFFD_MISSING
;
718 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_WP
) {
719 vm_flags
|= VM_UFFD_WP
;
721 * FIXME: remove the below error constraint by
722 * implementing the wprotect tracking mode.
728 ret
= validate_range(mm
, uffdio_register
.range
.start
,
729 uffdio_register
.range
.len
);
733 start
= uffdio_register
.range
.start
;
734 end
= start
+ uffdio_register
.range
.len
;
736 down_write(&mm
->mmap_sem
);
737 vma
= find_vma_prev(mm
, start
, &prev
);
743 /* check that there's at least one vma in the range */
745 if (vma
->vm_start
>= end
)
749 * Search for not compatible vmas.
751 * FIXME: this shall be relaxed later so that it doesn't fail
752 * on tmpfs backed vmas (in addition to the current allowance
753 * on anonymous vmas).
756 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
759 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
760 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
762 /* check not compatible vmas */
768 * Check that this vma isn't already owned by a
769 * different userfaultfd. We can't allow more than one
770 * userfaultfd to own a single vma simultaneously or we
771 * wouldn't know which one to deliver the userfaults to.
774 if (cur
->vm_userfaultfd_ctx
.ctx
&&
775 cur
->vm_userfaultfd_ctx
.ctx
!= ctx
)
782 if (vma
->vm_start
< start
)
790 BUG_ON(vma
->vm_userfaultfd_ctx
.ctx
&&
791 vma
->vm_userfaultfd_ctx
.ctx
!= ctx
);
794 * Nothing to do: this vma is already registered into this
795 * userfaultfd and with the right tracking mode too.
797 if (vma
->vm_userfaultfd_ctx
.ctx
== ctx
&&
798 (vma
->vm_flags
& vm_flags
) == vm_flags
)
801 if (vma
->vm_start
> start
)
802 start
= vma
->vm_start
;
803 vma_end
= min(end
, vma
->vm_end
);
805 new_flags
= (vma
->vm_flags
& ~vm_flags
) | vm_flags
;
806 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
807 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
809 ((struct vm_userfaultfd_ctx
){ ctx
}));
814 if (vma
->vm_start
< start
) {
815 ret
= split_vma(mm
, vma
, start
, 1);
819 if (vma
->vm_end
> end
) {
820 ret
= split_vma(mm
, vma
, end
, 0);
826 * In the vma_merge() successful mprotect-like case 8:
827 * the next vma was merged into the current one and
828 * the current one has not been updated yet.
830 vma
->vm_flags
= new_flags
;
831 vma
->vm_userfaultfd_ctx
.ctx
= ctx
;
837 } while (vma
&& vma
->vm_start
< end
);
839 up_write(&mm
->mmap_sem
);
842 * Now that we scanned all vmas we can already tell
843 * userland which ioctls methods are guaranteed to
844 * succeed on this range.
846 if (put_user(UFFD_API_RANGE_IOCTLS
,
847 &user_uffdio_register
->ioctls
))
854 static int userfaultfd_unregister(struct userfaultfd_ctx
*ctx
,
857 struct mm_struct
*mm
= ctx
->mm
;
858 struct vm_area_struct
*vma
, *prev
, *cur
;
860 struct uffdio_range uffdio_unregister
;
861 unsigned long new_flags
;
863 unsigned long start
, end
, vma_end
;
864 const void __user
*buf
= (void __user
*)arg
;
867 if (copy_from_user(&uffdio_unregister
, buf
, sizeof(uffdio_unregister
)))
870 ret
= validate_range(mm
, uffdio_unregister
.start
,
871 uffdio_unregister
.len
);
875 start
= uffdio_unregister
.start
;
876 end
= start
+ uffdio_unregister
.len
;
878 down_write(&mm
->mmap_sem
);
879 vma
= find_vma_prev(mm
, start
, &prev
);
885 /* check that there's at least one vma in the range */
887 if (vma
->vm_start
>= end
)
891 * Search for not compatible vmas.
893 * FIXME: this shall be relaxed later so that it doesn't fail
894 * on tmpfs backed vmas (in addition to the current allowance
895 * on anonymous vmas).
899 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
902 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
903 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
906 * Check not compatible vmas, not strictly required
907 * here as not compatible vmas cannot have an
908 * userfaultfd_ctx registered on them, but this
909 * provides for more strict behavior to notice
910 * unregistration errors.
919 if (vma
->vm_start
< start
)
929 * Nothing to do: this vma is already registered into this
930 * userfaultfd and with the right tracking mode too.
932 if (!vma
->vm_userfaultfd_ctx
.ctx
)
935 if (vma
->vm_start
> start
)
936 start
= vma
->vm_start
;
937 vma_end
= min(end
, vma
->vm_end
);
939 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
940 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
941 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
948 if (vma
->vm_start
< start
) {
949 ret
= split_vma(mm
, vma
, start
, 1);
953 if (vma
->vm_end
> end
) {
954 ret
= split_vma(mm
, vma
, end
, 0);
960 * In the vma_merge() successful mprotect-like case 8:
961 * the next vma was merged into the current one and
962 * the current one has not been updated yet.
964 vma
->vm_flags
= new_flags
;
965 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
971 } while (vma
&& vma
->vm_start
< end
);
973 up_write(&mm
->mmap_sem
);
979 * userfaultfd_wake may be used in combination with the
980 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
982 static int userfaultfd_wake(struct userfaultfd_ctx
*ctx
,
986 struct uffdio_range uffdio_wake
;
987 struct userfaultfd_wake_range range
;
988 const void __user
*buf
= (void __user
*)arg
;
991 if (copy_from_user(&uffdio_wake
, buf
, sizeof(uffdio_wake
)))
994 ret
= validate_range(ctx
->mm
, uffdio_wake
.start
, uffdio_wake
.len
);
998 range
.start
= uffdio_wake
.start
;
999 range
.len
= uffdio_wake
.len
;
1002 * len == 0 means wake all and we don't want to wake all here,
1003 * so check it again to be sure.
1005 VM_BUG_ON(!range
.len
);
1007 wake_userfault(ctx
, &range
);
1014 static int userfaultfd_copy(struct userfaultfd_ctx
*ctx
,
1018 struct uffdio_copy uffdio_copy
;
1019 struct uffdio_copy __user
*user_uffdio_copy
;
1020 struct userfaultfd_wake_range range
;
1022 user_uffdio_copy
= (struct uffdio_copy __user
*) arg
;
1025 if (copy_from_user(&uffdio_copy
, user_uffdio_copy
,
1026 /* don't copy "copy" last field */
1027 sizeof(uffdio_copy
)-sizeof(__s64
)))
1030 ret
= validate_range(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.len
);
1034 * double check for wraparound just in case. copy_from_user()
1035 * will later check uffdio_copy.src + uffdio_copy.len to fit
1036 * in the userland range.
1039 if (uffdio_copy
.src
+ uffdio_copy
.len
<= uffdio_copy
.src
)
1041 if (uffdio_copy
.mode
& ~UFFDIO_COPY_MODE_DONTWAKE
)
1044 ret
= mcopy_atomic(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.src
,
1046 if (unlikely(put_user(ret
, &user_uffdio_copy
->copy
)))
1051 /* len == 0 would wake all */
1053 if (!(uffdio_copy
.mode
& UFFDIO_COPY_MODE_DONTWAKE
)) {
1054 range
.start
= uffdio_copy
.dst
;
1055 wake_userfault(ctx
, &range
);
1057 ret
= range
.len
== uffdio_copy
.len
? 0 : -EAGAIN
;
1062 static int userfaultfd_zeropage(struct userfaultfd_ctx
*ctx
,
1066 struct uffdio_zeropage uffdio_zeropage
;
1067 struct uffdio_zeropage __user
*user_uffdio_zeropage
;
1068 struct userfaultfd_wake_range range
;
1070 user_uffdio_zeropage
= (struct uffdio_zeropage __user
*) arg
;
1073 if (copy_from_user(&uffdio_zeropage
, user_uffdio_zeropage
,
1074 /* don't copy "zeropage" last field */
1075 sizeof(uffdio_zeropage
)-sizeof(__s64
)))
1078 ret
= validate_range(ctx
->mm
, uffdio_zeropage
.range
.start
,
1079 uffdio_zeropage
.range
.len
);
1083 if (uffdio_zeropage
.mode
& ~UFFDIO_ZEROPAGE_MODE_DONTWAKE
)
1086 ret
= mfill_zeropage(ctx
->mm
, uffdio_zeropage
.range
.start
,
1087 uffdio_zeropage
.range
.len
);
1088 if (unlikely(put_user(ret
, &user_uffdio_zeropage
->zeropage
)))
1092 /* len == 0 would wake all */
1095 if (!(uffdio_zeropage
.mode
& UFFDIO_ZEROPAGE_MODE_DONTWAKE
)) {
1096 range
.start
= uffdio_zeropage
.range
.start
;
1097 wake_userfault(ctx
, &range
);
1099 ret
= range
.len
== uffdio_zeropage
.range
.len
? 0 : -EAGAIN
;
1105 * userland asks for a certain API version and we return which bits
1106 * and ioctl commands are implemented in this kernel for such API
1107 * version or -EINVAL if unknown.
1109 static int userfaultfd_api(struct userfaultfd_ctx
*ctx
,
1112 struct uffdio_api uffdio_api
;
1113 void __user
*buf
= (void __user
*)arg
;
1117 if (ctx
->state
!= UFFD_STATE_WAIT_API
)
1120 if (copy_from_user(&uffdio_api
, buf
, sizeof(uffdio_api
)))
1122 if (uffdio_api
.api
!= UFFD_API
|| uffdio_api
.features
) {
1123 memset(&uffdio_api
, 0, sizeof(uffdio_api
));
1124 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1129 uffdio_api
.features
= UFFD_API_FEATURES
;
1130 uffdio_api
.ioctls
= UFFD_API_IOCTLS
;
1132 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1134 ctx
->state
= UFFD_STATE_RUNNING
;
1140 static long userfaultfd_ioctl(struct file
*file
, unsigned cmd
,
1144 struct userfaultfd_ctx
*ctx
= file
->private_data
;
1146 if (cmd
!= UFFDIO_API
&& ctx
->state
== UFFD_STATE_WAIT_API
)
1151 ret
= userfaultfd_api(ctx
, arg
);
1153 case UFFDIO_REGISTER
:
1154 ret
= userfaultfd_register(ctx
, arg
);
1156 case UFFDIO_UNREGISTER
:
1157 ret
= userfaultfd_unregister(ctx
, arg
);
1160 ret
= userfaultfd_wake(ctx
, arg
);
1163 ret
= userfaultfd_copy(ctx
, arg
);
1165 case UFFDIO_ZEROPAGE
:
1166 ret
= userfaultfd_zeropage(ctx
, arg
);
1172 #ifdef CONFIG_PROC_FS
1173 static void userfaultfd_show_fdinfo(struct seq_file
*m
, struct file
*f
)
1175 struct userfaultfd_ctx
*ctx
= f
->private_data
;
1177 struct userfaultfd_wait_queue
*uwq
;
1178 unsigned long pending
= 0, total
= 0;
1180 spin_lock(&ctx
->fault_pending_wqh
.lock
);
1181 list_for_each_entry(wq
, &ctx
->fault_pending_wqh
.task_list
, task_list
) {
1182 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1186 list_for_each_entry(wq
, &ctx
->fault_wqh
.task_list
, task_list
) {
1187 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1190 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
1193 * If more protocols will be added, there will be all shown
1194 * separated by a space. Like this:
1195 * protocols: aa:... bb:...
1197 seq_printf(m
, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
1198 pending
, total
, UFFD_API
, UFFD_API_FEATURES
,
1199 UFFD_API_IOCTLS
|UFFD_API_RANGE_IOCTLS
);
1203 static const struct file_operations userfaultfd_fops
= {
1204 #ifdef CONFIG_PROC_FS
1205 .show_fdinfo
= userfaultfd_show_fdinfo
,
1207 .release
= userfaultfd_release
,
1208 .poll
= userfaultfd_poll
,
1209 .read
= userfaultfd_read
,
1210 .unlocked_ioctl
= userfaultfd_ioctl
,
1211 .compat_ioctl
= userfaultfd_ioctl
,
1212 .llseek
= noop_llseek
,
1215 static void init_once_userfaultfd_ctx(void *mem
)
1217 struct userfaultfd_ctx
*ctx
= (struct userfaultfd_ctx
*) mem
;
1219 init_waitqueue_head(&ctx
->fault_pending_wqh
);
1220 init_waitqueue_head(&ctx
->fault_wqh
);
1221 init_waitqueue_head(&ctx
->fd_wqh
);
1225 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1226 * @flags: Flags for the userfaultfd file.
1228 * This function creates an userfaultfd file pointer, w/out installing
1229 * it into the fd table. This is useful when the userfaultfd file is
1230 * used during the initialization of data structures that require
1231 * extra setup after the userfaultfd creation. So the userfaultfd
1232 * creation is split into the file pointer creation phase, and the
1233 * file descriptor installation phase. In this way races with
1234 * userspace closing the newly installed file descriptor can be
1235 * avoided. Returns an userfaultfd file pointer, or a proper error
1238 static struct file
*userfaultfd_file_create(int flags
)
1241 struct userfaultfd_ctx
*ctx
;
1243 BUG_ON(!current
->mm
);
1245 /* Check the UFFD_* constants for consistency. */
1246 BUILD_BUG_ON(UFFD_CLOEXEC
!= O_CLOEXEC
);
1247 BUILD_BUG_ON(UFFD_NONBLOCK
!= O_NONBLOCK
);
1249 file
= ERR_PTR(-EINVAL
);
1250 if (flags
& ~UFFD_SHARED_FCNTL_FLAGS
)
1253 file
= ERR_PTR(-ENOMEM
);
1254 ctx
= kmem_cache_alloc(userfaultfd_ctx_cachep
, GFP_KERNEL
);
1258 atomic_set(&ctx
->refcount
, 1);
1260 ctx
->state
= UFFD_STATE_WAIT_API
;
1261 ctx
->released
= false;
1262 ctx
->mm
= current
->mm
;
1263 /* prevent the mm struct to be freed */
1264 atomic_inc(&ctx
->mm
->mm_users
);
1266 file
= anon_inode_getfile("[userfaultfd]", &userfaultfd_fops
, ctx
,
1267 O_RDWR
| (flags
& UFFD_SHARED_FCNTL_FLAGS
));
1269 kmem_cache_free(userfaultfd_ctx_cachep
, ctx
);
1274 SYSCALL_DEFINE1(userfaultfd
, int, flags
)
1279 error
= get_unused_fd_flags(flags
& UFFD_SHARED_FCNTL_FLAGS
);
1284 file
= userfaultfd_file_create(flags
);
1286 error
= PTR_ERR(file
);
1287 goto err_put_unused_fd
;
1289 fd_install(fd
, file
);
1299 static int __init
userfaultfd_init(void)
1301 userfaultfd_ctx_cachep
= kmem_cache_create("userfaultfd_ctx_cache",
1302 sizeof(struct userfaultfd_ctx
),
1304 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
1305 init_once_userfaultfd_ctx
);
1308 __initcall(userfaultfd_init
);