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
;
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 spin_lock(&ctx
->fault_pending_wqh
.lock
);
332 * After the __add_wait_queue the uwq is visible to userland
333 * through poll/read().
335 __add_wait_queue(&ctx
->fault_pending_wqh
, &uwq
.wq
);
337 * The smp_mb() after __set_current_state prevents the reads
338 * following the spin_unlock to happen before the list_add in
341 set_current_state(TASK_KILLABLE
);
342 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
344 must_wait
= userfaultfd_must_wait(ctx
, address
, flags
, reason
);
345 up_read(&mm
->mmap_sem
);
347 if (likely(must_wait
&& !ACCESS_ONCE(ctx
->released
) &&
348 !fatal_signal_pending(current
))) {
349 wake_up_poll(&ctx
->fd_wqh
, POLLIN
);
351 ret
|= VM_FAULT_MAJOR
;
354 __set_current_state(TASK_RUNNING
);
357 * Here we race with the list_del; list_add in
358 * userfaultfd_ctx_read(), however because we don't ever run
359 * list_del_init() to refile across the two lists, the prev
360 * and next pointers will never point to self. list_add also
361 * would never let any of the two pointers to point to
362 * self. So list_empty_careful won't risk to see both pointers
363 * pointing to self at any time during the list refile. The
364 * only case where list_del_init() is called is the full
365 * removal in the wake function and there we don't re-list_add
366 * and it's fine not to block on the spinlock. The uwq on this
367 * kernel stack can be released after the list_del_init.
369 if (!list_empty_careful(&uwq
.wq
.task_list
)) {
370 spin_lock(&ctx
->fault_pending_wqh
.lock
);
372 * No need of list_del_init(), the uwq on the stack
373 * will be freed shortly anyway.
375 list_del(&uwq
.wq
.task_list
);
376 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
380 * ctx may go away after this if the userfault pseudo fd is
383 userfaultfd_ctx_put(ctx
);
389 static int userfaultfd_release(struct inode
*inode
, struct file
*file
)
391 struct userfaultfd_ctx
*ctx
= file
->private_data
;
392 struct mm_struct
*mm
= ctx
->mm
;
393 struct vm_area_struct
*vma
, *prev
;
394 /* len == 0 means wake all */
395 struct userfaultfd_wake_range range
= { .len
= 0, };
396 unsigned long new_flags
;
398 ACCESS_ONCE(ctx
->released
) = true;
401 * Flush page faults out of all CPUs. NOTE: all page faults
402 * must be retried without returning VM_FAULT_SIGBUS if
403 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
404 * changes while handle_userfault released the mmap_sem. So
405 * it's critical that released is set to true (above), before
406 * taking the mmap_sem for writing.
408 down_write(&mm
->mmap_sem
);
410 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
412 BUG_ON(!!vma
->vm_userfaultfd_ctx
.ctx
^
413 !!(vma
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
414 if (vma
->vm_userfaultfd_ctx
.ctx
!= ctx
) {
418 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
419 prev
= vma_merge(mm
, prev
, vma
->vm_start
, vma
->vm_end
,
420 new_flags
, vma
->anon_vma
,
421 vma
->vm_file
, vma
->vm_pgoff
,
428 vma
->vm_flags
= new_flags
;
429 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
431 up_write(&mm
->mmap_sem
);
434 * After no new page faults can wait on this fault_*wqh, flush
435 * the last page faults that may have been already waiting on
438 spin_lock(&ctx
->fault_pending_wqh
.lock
);
439 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
, 0, &range
);
440 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, 0, &range
);
441 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
443 wake_up_poll(&ctx
->fd_wqh
, POLLHUP
);
444 userfaultfd_ctx_put(ctx
);
448 /* fault_pending_wqh.lock must be hold by the caller */
449 static inline struct userfaultfd_wait_queue
*find_userfault(
450 struct userfaultfd_ctx
*ctx
)
453 struct userfaultfd_wait_queue
*uwq
;
455 VM_BUG_ON(!spin_is_locked(&ctx
->fault_pending_wqh
.lock
));
458 if (!waitqueue_active(&ctx
->fault_pending_wqh
))
460 /* walk in reverse to provide FIFO behavior to read userfaults */
461 wq
= list_last_entry(&ctx
->fault_pending_wqh
.task_list
,
462 typeof(*wq
), task_list
);
463 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
468 static unsigned int userfaultfd_poll(struct file
*file
, poll_table
*wait
)
470 struct userfaultfd_ctx
*ctx
= file
->private_data
;
473 poll_wait(file
, &ctx
->fd_wqh
, wait
);
475 switch (ctx
->state
) {
476 case UFFD_STATE_WAIT_API
:
478 case UFFD_STATE_RUNNING
:
480 * poll() never guarantees that read won't block.
481 * userfaults can be waken before they're read().
483 if (unlikely(!(file
->f_flags
& O_NONBLOCK
)))
486 * lockless access to see if there are pending faults
487 * __pollwait last action is the add_wait_queue but
488 * the spin_unlock would allow the waitqueue_active to
489 * pass above the actual list_add inside
490 * add_wait_queue critical section. So use a full
491 * memory barrier to serialize the list_add write of
492 * add_wait_queue() with the waitqueue_active read
497 if (waitqueue_active(&ctx
->fault_pending_wqh
))
505 static ssize_t
userfaultfd_ctx_read(struct userfaultfd_ctx
*ctx
, int no_wait
,
506 struct uffd_msg
*msg
)
509 DECLARE_WAITQUEUE(wait
, current
);
510 struct userfaultfd_wait_queue
*uwq
;
512 /* always take the fd_wqh lock before the fault_pending_wqh lock */
513 spin_lock(&ctx
->fd_wqh
.lock
);
514 __add_wait_queue(&ctx
->fd_wqh
, &wait
);
516 set_current_state(TASK_INTERRUPTIBLE
);
517 spin_lock(&ctx
->fault_pending_wqh
.lock
);
518 uwq
= find_userfault(ctx
);
521 * The fault_pending_wqh.lock prevents the uwq
522 * to disappear from under us.
524 * Refile this userfault from
525 * fault_pending_wqh to fault_wqh, it's not
526 * pending anymore after we read it.
528 * Use list_del() by hand (as
529 * userfaultfd_wake_function also uses
530 * list_del_init() by hand) to be sure nobody
531 * changes __remove_wait_queue() to use
532 * list_del_init() in turn breaking the
533 * !list_empty_careful() check in
534 * handle_userfault(). The uwq->wq.task_list
535 * must never be empty at any time during the
536 * refile, or the waitqueue could disappear
537 * from under us. The "wait_queue_head_t"
538 * parameter of __remove_wait_queue() is unused
541 list_del(&uwq
->wq
.task_list
);
542 __add_wait_queue(&ctx
->fault_wqh
, &uwq
->wq
);
544 /* careful to always initialize msg if ret == 0 */
546 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
550 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
551 if (signal_pending(current
)) {
559 spin_unlock(&ctx
->fd_wqh
.lock
);
561 spin_lock(&ctx
->fd_wqh
.lock
);
563 __remove_wait_queue(&ctx
->fd_wqh
, &wait
);
564 __set_current_state(TASK_RUNNING
);
565 spin_unlock(&ctx
->fd_wqh
.lock
);
570 static ssize_t
userfaultfd_read(struct file
*file
, char __user
*buf
,
571 size_t count
, loff_t
*ppos
)
573 struct userfaultfd_ctx
*ctx
= file
->private_data
;
574 ssize_t _ret
, ret
= 0;
576 int no_wait
= file
->f_flags
& O_NONBLOCK
;
578 if (ctx
->state
== UFFD_STATE_WAIT_API
)
580 BUG_ON(ctx
->state
!= UFFD_STATE_RUNNING
);
583 if (count
< sizeof(msg
))
584 return ret
? ret
: -EINVAL
;
585 _ret
= userfaultfd_ctx_read(ctx
, no_wait
, &msg
);
587 return ret
? ret
: _ret
;
588 if (copy_to_user((__u64 __user
*) buf
, &msg
, sizeof(msg
)))
589 return ret
? ret
: -EFAULT
;
592 count
-= sizeof(msg
);
594 * Allow to read more than one fault at time but only
595 * block if waiting for the very first one.
597 no_wait
= O_NONBLOCK
;
601 static void __wake_userfault(struct userfaultfd_ctx
*ctx
,
602 struct userfaultfd_wake_range
*range
)
604 unsigned long start
, end
;
606 start
= range
->start
;
607 end
= range
->start
+ range
->len
;
609 spin_lock(&ctx
->fault_pending_wqh
.lock
);
610 /* wake all in the range and autoremove */
611 if (waitqueue_active(&ctx
->fault_pending_wqh
))
612 __wake_up_locked_key(&ctx
->fault_pending_wqh
, TASK_NORMAL
, 0,
614 if (waitqueue_active(&ctx
->fault_wqh
))
615 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, 0, range
);
616 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
619 static __always_inline
void wake_userfault(struct userfaultfd_ctx
*ctx
,
620 struct userfaultfd_wake_range
*range
)
623 * To be sure waitqueue_active() is not reordered by the CPU
624 * before the pagetable update, use an explicit SMP memory
625 * barrier here. PT lock release or up_read(mmap_sem) still
626 * have release semantics that can allow the
627 * waitqueue_active() to be reordered before the pte update.
632 * Use waitqueue_active because it's very frequent to
633 * change the address space atomically even if there are no
634 * userfaults yet. So we take the spinlock only when we're
635 * sure we've userfaults to wake.
637 if (waitqueue_active(&ctx
->fault_pending_wqh
) ||
638 waitqueue_active(&ctx
->fault_wqh
))
639 __wake_userfault(ctx
, range
);
642 static __always_inline
int validate_range(struct mm_struct
*mm
,
643 __u64 start
, __u64 len
)
645 __u64 task_size
= mm
->task_size
;
647 if (start
& ~PAGE_MASK
)
649 if (len
& ~PAGE_MASK
)
653 if (start
< mmap_min_addr
)
655 if (start
>= task_size
)
657 if (len
> task_size
- start
)
662 static int userfaultfd_register(struct userfaultfd_ctx
*ctx
,
665 struct mm_struct
*mm
= ctx
->mm
;
666 struct vm_area_struct
*vma
, *prev
, *cur
;
668 struct uffdio_register uffdio_register
;
669 struct uffdio_register __user
*user_uffdio_register
;
670 unsigned long vm_flags
, new_flags
;
672 unsigned long start
, end
, vma_end
;
674 user_uffdio_register
= (struct uffdio_register __user
*) arg
;
677 if (copy_from_user(&uffdio_register
, user_uffdio_register
,
678 sizeof(uffdio_register
)-sizeof(__u64
)))
682 if (!uffdio_register
.mode
)
684 if (uffdio_register
.mode
& ~(UFFDIO_REGISTER_MODE_MISSING
|
685 UFFDIO_REGISTER_MODE_WP
))
688 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_MISSING
)
689 vm_flags
|= VM_UFFD_MISSING
;
690 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_WP
) {
691 vm_flags
|= VM_UFFD_WP
;
693 * FIXME: remove the below error constraint by
694 * implementing the wprotect tracking mode.
700 ret
= validate_range(mm
, uffdio_register
.range
.start
,
701 uffdio_register
.range
.len
);
705 start
= uffdio_register
.range
.start
;
706 end
= start
+ uffdio_register
.range
.len
;
708 down_write(&mm
->mmap_sem
);
709 vma
= find_vma_prev(mm
, start
, &prev
);
715 /* check that there's at least one vma in the range */
717 if (vma
->vm_start
>= end
)
721 * Search for not compatible vmas.
723 * FIXME: this shall be relaxed later so that it doesn't fail
724 * on tmpfs backed vmas (in addition to the current allowance
725 * on anonymous vmas).
728 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
731 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
732 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
734 /* check not compatible vmas */
740 * Check that this vma isn't already owned by a
741 * different userfaultfd. We can't allow more than one
742 * userfaultfd to own a single vma simultaneously or we
743 * wouldn't know which one to deliver the userfaults to.
746 if (cur
->vm_userfaultfd_ctx
.ctx
&&
747 cur
->vm_userfaultfd_ctx
.ctx
!= ctx
)
754 if (vma
->vm_start
< start
)
762 BUG_ON(vma
->vm_userfaultfd_ctx
.ctx
&&
763 vma
->vm_userfaultfd_ctx
.ctx
!= ctx
);
766 * Nothing to do: this vma is already registered into this
767 * userfaultfd and with the right tracking mode too.
769 if (vma
->vm_userfaultfd_ctx
.ctx
== ctx
&&
770 (vma
->vm_flags
& vm_flags
) == vm_flags
)
773 if (vma
->vm_start
> start
)
774 start
= vma
->vm_start
;
775 vma_end
= min(end
, vma
->vm_end
);
777 new_flags
= (vma
->vm_flags
& ~vm_flags
) | vm_flags
;
778 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
779 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
781 ((struct vm_userfaultfd_ctx
){ ctx
}));
786 if (vma
->vm_start
< start
) {
787 ret
= split_vma(mm
, vma
, start
, 1);
791 if (vma
->vm_end
> end
) {
792 ret
= split_vma(mm
, vma
, end
, 0);
798 * In the vma_merge() successful mprotect-like case 8:
799 * the next vma was merged into the current one and
800 * the current one has not been updated yet.
802 vma
->vm_flags
= new_flags
;
803 vma
->vm_userfaultfd_ctx
.ctx
= ctx
;
809 } while (vma
&& vma
->vm_start
< end
);
811 up_write(&mm
->mmap_sem
);
814 * Now that we scanned all vmas we can already tell
815 * userland which ioctls methods are guaranteed to
816 * succeed on this range.
818 if (put_user(UFFD_API_RANGE_IOCTLS
,
819 &user_uffdio_register
->ioctls
))
826 static int userfaultfd_unregister(struct userfaultfd_ctx
*ctx
,
829 struct mm_struct
*mm
= ctx
->mm
;
830 struct vm_area_struct
*vma
, *prev
, *cur
;
832 struct uffdio_range uffdio_unregister
;
833 unsigned long new_flags
;
835 unsigned long start
, end
, vma_end
;
836 const void __user
*buf
= (void __user
*)arg
;
839 if (copy_from_user(&uffdio_unregister
, buf
, sizeof(uffdio_unregister
)))
842 ret
= validate_range(mm
, uffdio_unregister
.start
,
843 uffdio_unregister
.len
);
847 start
= uffdio_unregister
.start
;
848 end
= start
+ uffdio_unregister
.len
;
850 down_write(&mm
->mmap_sem
);
851 vma
= find_vma_prev(mm
, start
, &prev
);
857 /* check that there's at least one vma in the range */
859 if (vma
->vm_start
>= end
)
863 * Search for not compatible vmas.
865 * FIXME: this shall be relaxed later so that it doesn't fail
866 * on tmpfs backed vmas (in addition to the current allowance
867 * on anonymous vmas).
871 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
874 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
875 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
878 * Check not compatible vmas, not strictly required
879 * here as not compatible vmas cannot have an
880 * userfaultfd_ctx registered on them, but this
881 * provides for more strict behavior to notice
882 * unregistration errors.
891 if (vma
->vm_start
< start
)
901 * Nothing to do: this vma is already registered into this
902 * userfaultfd and with the right tracking mode too.
904 if (!vma
->vm_userfaultfd_ctx
.ctx
)
907 if (vma
->vm_start
> start
)
908 start
= vma
->vm_start
;
909 vma_end
= min(end
, vma
->vm_end
);
911 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
912 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
913 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
920 if (vma
->vm_start
< start
) {
921 ret
= split_vma(mm
, vma
, start
, 1);
925 if (vma
->vm_end
> end
) {
926 ret
= split_vma(mm
, vma
, end
, 0);
932 * In the vma_merge() successful mprotect-like case 8:
933 * the next vma was merged into the current one and
934 * the current one has not been updated yet.
936 vma
->vm_flags
= new_flags
;
937 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
943 } while (vma
&& vma
->vm_start
< end
);
945 up_write(&mm
->mmap_sem
);
951 * userfaultfd_wake may be used in combination with the
952 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
954 static int userfaultfd_wake(struct userfaultfd_ctx
*ctx
,
958 struct uffdio_range uffdio_wake
;
959 struct userfaultfd_wake_range range
;
960 const void __user
*buf
= (void __user
*)arg
;
963 if (copy_from_user(&uffdio_wake
, buf
, sizeof(uffdio_wake
)))
966 ret
= validate_range(ctx
->mm
, uffdio_wake
.start
, uffdio_wake
.len
);
970 range
.start
= uffdio_wake
.start
;
971 range
.len
= uffdio_wake
.len
;
974 * len == 0 means wake all and we don't want to wake all here,
975 * so check it again to be sure.
977 VM_BUG_ON(!range
.len
);
979 wake_userfault(ctx
, &range
);
986 static int userfaultfd_copy(struct userfaultfd_ctx
*ctx
,
990 struct uffdio_copy uffdio_copy
;
991 struct uffdio_copy __user
*user_uffdio_copy
;
992 struct userfaultfd_wake_range range
;
994 user_uffdio_copy
= (struct uffdio_copy __user
*) arg
;
997 if (copy_from_user(&uffdio_copy
, user_uffdio_copy
,
998 /* don't copy "copy" last field */
999 sizeof(uffdio_copy
)-sizeof(__s64
)))
1002 ret
= validate_range(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.len
);
1006 * double check for wraparound just in case. copy_from_user()
1007 * will later check uffdio_copy.src + uffdio_copy.len to fit
1008 * in the userland range.
1011 if (uffdio_copy
.src
+ uffdio_copy
.len
<= uffdio_copy
.src
)
1013 if (uffdio_copy
.mode
& ~UFFDIO_COPY_MODE_DONTWAKE
)
1016 ret
= mcopy_atomic(ctx
->mm
, uffdio_copy
.dst
, uffdio_copy
.src
,
1018 if (unlikely(put_user(ret
, &user_uffdio_copy
->copy
)))
1023 /* len == 0 would wake all */
1025 if (!(uffdio_copy
.mode
& UFFDIO_COPY_MODE_DONTWAKE
)) {
1026 range
.start
= uffdio_copy
.dst
;
1027 wake_userfault(ctx
, &range
);
1029 ret
= range
.len
== uffdio_copy
.len
? 0 : -EAGAIN
;
1034 static int userfaultfd_zeropage(struct userfaultfd_ctx
*ctx
,
1038 struct uffdio_zeropage uffdio_zeropage
;
1039 struct uffdio_zeropage __user
*user_uffdio_zeropage
;
1040 struct userfaultfd_wake_range range
;
1042 user_uffdio_zeropage
= (struct uffdio_zeropage __user
*) arg
;
1045 if (copy_from_user(&uffdio_zeropage
, user_uffdio_zeropage
,
1046 /* don't copy "zeropage" last field */
1047 sizeof(uffdio_zeropage
)-sizeof(__s64
)))
1050 ret
= validate_range(ctx
->mm
, uffdio_zeropage
.range
.start
,
1051 uffdio_zeropage
.range
.len
);
1055 if (uffdio_zeropage
.mode
& ~UFFDIO_ZEROPAGE_MODE_DONTWAKE
)
1058 ret
= mfill_zeropage(ctx
->mm
, uffdio_zeropage
.range
.start
,
1059 uffdio_zeropage
.range
.len
);
1060 if (unlikely(put_user(ret
, &user_uffdio_zeropage
->zeropage
)))
1064 /* len == 0 would wake all */
1067 if (!(uffdio_zeropage
.mode
& UFFDIO_ZEROPAGE_MODE_DONTWAKE
)) {
1068 range
.start
= uffdio_zeropage
.range
.start
;
1069 wake_userfault(ctx
, &range
);
1071 ret
= range
.len
== uffdio_zeropage
.range
.len
? 0 : -EAGAIN
;
1077 * userland asks for a certain API version and we return which bits
1078 * and ioctl commands are implemented in this kernel for such API
1079 * version or -EINVAL if unknown.
1081 static int userfaultfd_api(struct userfaultfd_ctx
*ctx
,
1084 struct uffdio_api uffdio_api
;
1085 void __user
*buf
= (void __user
*)arg
;
1089 if (ctx
->state
!= UFFD_STATE_WAIT_API
)
1092 if (copy_from_user(&uffdio_api
, buf
, sizeof(uffdio_api
)))
1094 if (uffdio_api
.api
!= UFFD_API
|| uffdio_api
.features
) {
1095 memset(&uffdio_api
, 0, sizeof(uffdio_api
));
1096 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1101 uffdio_api
.features
= UFFD_API_FEATURES
;
1102 uffdio_api
.ioctls
= UFFD_API_IOCTLS
;
1104 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
1106 ctx
->state
= UFFD_STATE_RUNNING
;
1112 static long userfaultfd_ioctl(struct file
*file
, unsigned cmd
,
1116 struct userfaultfd_ctx
*ctx
= file
->private_data
;
1120 ret
= userfaultfd_api(ctx
, arg
);
1122 case UFFDIO_REGISTER
:
1123 ret
= userfaultfd_register(ctx
, arg
);
1125 case UFFDIO_UNREGISTER
:
1126 ret
= userfaultfd_unregister(ctx
, arg
);
1129 ret
= userfaultfd_wake(ctx
, arg
);
1132 ret
= userfaultfd_copy(ctx
, arg
);
1134 case UFFDIO_ZEROPAGE
:
1135 ret
= userfaultfd_zeropage(ctx
, arg
);
1141 #ifdef CONFIG_PROC_FS
1142 static void userfaultfd_show_fdinfo(struct seq_file
*m
, struct file
*f
)
1144 struct userfaultfd_ctx
*ctx
= f
->private_data
;
1146 struct userfaultfd_wait_queue
*uwq
;
1147 unsigned long pending
= 0, total
= 0;
1149 spin_lock(&ctx
->fault_pending_wqh
.lock
);
1150 list_for_each_entry(wq
, &ctx
->fault_pending_wqh
.task_list
, task_list
) {
1151 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1155 list_for_each_entry(wq
, &ctx
->fault_wqh
.task_list
, task_list
) {
1156 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
1159 spin_unlock(&ctx
->fault_pending_wqh
.lock
);
1162 * If more protocols will be added, there will be all shown
1163 * separated by a space. Like this:
1164 * protocols: aa:... bb:...
1166 seq_printf(m
, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
1167 pending
, total
, UFFD_API
, UFFD_API_FEATURES
,
1168 UFFD_API_IOCTLS
|UFFD_API_RANGE_IOCTLS
);
1172 static const struct file_operations userfaultfd_fops
= {
1173 #ifdef CONFIG_PROC_FS
1174 .show_fdinfo
= userfaultfd_show_fdinfo
,
1176 .release
= userfaultfd_release
,
1177 .poll
= userfaultfd_poll
,
1178 .read
= userfaultfd_read
,
1179 .unlocked_ioctl
= userfaultfd_ioctl
,
1180 .compat_ioctl
= userfaultfd_ioctl
,
1181 .llseek
= noop_llseek
,
1184 static void init_once_userfaultfd_ctx(void *mem
)
1186 struct userfaultfd_ctx
*ctx
= (struct userfaultfd_ctx
*) mem
;
1188 init_waitqueue_head(&ctx
->fault_pending_wqh
);
1189 init_waitqueue_head(&ctx
->fault_wqh
);
1190 init_waitqueue_head(&ctx
->fd_wqh
);
1194 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1195 * @flags: Flags for the userfaultfd file.
1197 * This function creates an userfaultfd file pointer, w/out installing
1198 * it into the fd table. This is useful when the userfaultfd file is
1199 * used during the initialization of data structures that require
1200 * extra setup after the userfaultfd creation. So the userfaultfd
1201 * creation is split into the file pointer creation phase, and the
1202 * file descriptor installation phase. In this way races with
1203 * userspace closing the newly installed file descriptor can be
1204 * avoided. Returns an userfaultfd file pointer, or a proper error
1207 static struct file
*userfaultfd_file_create(int flags
)
1210 struct userfaultfd_ctx
*ctx
;
1212 BUG_ON(!current
->mm
);
1214 /* Check the UFFD_* constants for consistency. */
1215 BUILD_BUG_ON(UFFD_CLOEXEC
!= O_CLOEXEC
);
1216 BUILD_BUG_ON(UFFD_NONBLOCK
!= O_NONBLOCK
);
1218 file
= ERR_PTR(-EINVAL
);
1219 if (flags
& ~UFFD_SHARED_FCNTL_FLAGS
)
1222 file
= ERR_PTR(-ENOMEM
);
1223 ctx
= kmem_cache_alloc(userfaultfd_ctx_cachep
, GFP_KERNEL
);
1227 atomic_set(&ctx
->refcount
, 1);
1229 ctx
->state
= UFFD_STATE_WAIT_API
;
1230 ctx
->released
= false;
1231 ctx
->mm
= current
->mm
;
1232 /* prevent the mm struct to be freed */
1233 atomic_inc(&ctx
->mm
->mm_users
);
1235 file
= anon_inode_getfile("[userfaultfd]", &userfaultfd_fops
, ctx
,
1236 O_RDWR
| (flags
& UFFD_SHARED_FCNTL_FLAGS
));
1238 kmem_cache_free(userfaultfd_ctx_cachep
, ctx
);
1243 SYSCALL_DEFINE1(userfaultfd
, int, flags
)
1248 error
= get_unused_fd_flags(flags
& UFFD_SHARED_FCNTL_FLAGS
);
1253 file
= userfaultfd_file_create(flags
);
1255 error
= PTR_ERR(file
);
1256 goto err_put_unused_fd
;
1258 fd_install(fd
, file
);
1268 static int __init
userfaultfd_init(void)
1270 userfaultfd_ctx_cachep
= kmem_cache_create("userfaultfd_ctx_cache",
1271 sizeof(struct userfaultfd_ctx
),
1273 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
1274 init_once_userfaultfd_ctx
);
1277 __initcall(userfaultfd_init
);