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 enum userfaultfd_state
{
35 struct userfaultfd_ctx
{
36 /* pseudo fd refcounting */
38 /* waitqueue head for the userfaultfd page faults */
39 wait_queue_head_t fault_wqh
;
40 /* waitqueue head for the pseudo fd to wakeup poll/read */
41 wait_queue_head_t fd_wqh
;
42 /* userfaultfd syscall flags */
45 enum userfaultfd_state state
;
48 /* mm with one ore more vmas attached to this userfaultfd_ctx */
52 struct userfaultfd_wait_queue
{
56 * Only relevant when queued in fault_wqh and only used by the
57 * read operation to avoid reading the same userfault twice.
60 struct userfaultfd_ctx
*ctx
;
63 struct userfaultfd_wake_range
{
68 static int userfaultfd_wake_function(wait_queue_t
*wq
, unsigned mode
,
69 int wake_flags
, void *key
)
71 struct userfaultfd_wake_range
*range
= key
;
73 struct userfaultfd_wait_queue
*uwq
;
74 unsigned long start
, len
;
76 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
78 /* len == 0 means wake all */
81 if (len
&& (start
> uwq
->msg
.arg
.pagefault
.address
||
82 start
+ len
<= uwq
->msg
.arg
.pagefault
.address
))
84 ret
= wake_up_state(wq
->private, mode
);
87 * Wake only once, autoremove behavior.
89 * After the effect of list_del_init is visible to the
90 * other CPUs, the waitqueue may disappear from under
91 * us, see the !list_empty_careful() in
92 * handle_userfault(). try_to_wake_up() has an
93 * implicit smp_mb__before_spinlock, and the
94 * wq->private is read before calling the extern
95 * function "wake_up_state" (which in turns calls
96 * try_to_wake_up). While the spin_lock;spin_unlock;
97 * wouldn't be enough, the smp_mb__before_spinlock is
98 * enough to avoid an explicit smp_mb() here.
100 list_del_init(&wq
->task_list
);
106 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
108 * @ctx: [in] Pointer to the userfaultfd context.
110 * Returns: In case of success, returns not zero.
112 static void userfaultfd_ctx_get(struct userfaultfd_ctx
*ctx
)
114 if (!atomic_inc_not_zero(&ctx
->refcount
))
119 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
121 * @ctx: [in] Pointer to userfaultfd context.
123 * The userfaultfd context reference must have been previously acquired either
124 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
126 static void userfaultfd_ctx_put(struct userfaultfd_ctx
*ctx
)
128 if (atomic_dec_and_test(&ctx
->refcount
)) {
129 VM_BUG_ON(spin_is_locked(&ctx
->fault_pending_wqh
.lock
));
130 VM_BUG_ON(waitqueue_active(&ctx
->fault_pending_wqh
));
131 VM_BUG_ON(spin_is_locked(&ctx
->fault_wqh
.lock
));
132 VM_BUG_ON(waitqueue_active(&ctx
->fault_wqh
));
133 VM_BUG_ON(spin_is_locked(&ctx
->fd_wqh
.lock
));
134 VM_BUG_ON(waitqueue_active(&ctx
->fd_wqh
));
140 static inline void msg_init(struct uffd_msg
*msg
)
142 BUILD_BUG_ON(sizeof(struct uffd_msg
) != 32);
144 * Must use memset to zero out the paddings or kernel data is
145 * leaked to userland.
147 memset(msg
, 0, sizeof(struct uffd_msg
));
150 static inline struct uffd_msg
userfault_msg(unsigned long address
,
152 unsigned long reason
)
156 msg
.event
= UFFD_EVENT_PAGEFAULT
;
157 msg
.arg
.pagefault
.address
= address
;
158 if (flags
& FAULT_FLAG_WRITE
)
160 * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
161 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
162 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
163 * was a read fault, otherwise if set it means it's
166 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WRITE
;
167 if (reason
& VM_UFFD_WP
)
169 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
170 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
171 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
172 * a missing fault, otherwise if set it means it's a
173 * write protect fault.
175 msg
.arg
.pagefault
.flags
|= UFFD_PAGEFAULT_FLAG_WP
;
180 * The locking rules involved in returning VM_FAULT_RETRY depending on
181 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
182 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
183 * recommendation in __lock_page_or_retry is not an understatement.
185 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
186 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
189 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
190 * set, VM_FAULT_RETRY can still be returned if and only if there are
191 * fatal_signal_pending()s, and the mmap_sem must be released before
194 int handle_userfault(struct vm_area_struct
*vma
, unsigned long address
,
195 unsigned int flags
, unsigned long reason
)
197 struct mm_struct
*mm
= vma
->vm_mm
;
198 struct userfaultfd_ctx
*ctx
;
199 struct userfaultfd_wait_queue uwq
;
202 BUG_ON(!rwsem_is_locked(&mm
->mmap_sem
));
204 ret
= VM_FAULT_SIGBUS
;
205 ctx
= vma
->vm_userfaultfd_ctx
.ctx
;
209 BUG_ON(ctx
->mm
!= mm
);
211 VM_BUG_ON(reason
& ~(VM_UFFD_MISSING
|VM_UFFD_WP
));
212 VM_BUG_ON(!(reason
& VM_UFFD_MISSING
) ^ !!(reason
& VM_UFFD_WP
));
215 * If it's already released don't get it. This avoids to loop
216 * in __get_user_pages if userfaultfd_release waits on the
217 * caller of handle_userfault to release the mmap_sem.
219 if (unlikely(ACCESS_ONCE(ctx
->released
)))
223 * Check that we can return VM_FAULT_RETRY.
225 * NOTE: it should become possible to return VM_FAULT_RETRY
226 * even if FAULT_FLAG_TRIED is set without leading to gup()
227 * -EBUSY failures, if the userfaultfd is to be extended for
228 * VM_UFFD_WP tracking and we intend to arm the userfault
229 * without first stopping userland access to the memory. For
230 * VM_UFFD_MISSING userfaults this is enough for now.
232 if (unlikely(!(flags
& FAULT_FLAG_ALLOW_RETRY
))) {
234 * Validate the invariant that nowait must allow retry
235 * to be sure not to return SIGBUS erroneously on
236 * nowait invocations.
238 BUG_ON(flags
& FAULT_FLAG_RETRY_NOWAIT
);
239 #ifdef CONFIG_DEBUG_VM
240 if (printk_ratelimit()) {
242 "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags
);
250 * Handle nowait, not much to do other than tell it to retry
253 ret
= VM_FAULT_RETRY
;
254 if (flags
& FAULT_FLAG_RETRY_NOWAIT
)
257 /* take the reference before dropping the mmap_sem */
258 userfaultfd_ctx_get(ctx
);
260 /* be gentle and immediately relinquish the mmap_sem */
261 up_read(&mm
->mmap_sem
);
263 init_waitqueue_func_entry(&uwq
.wq
, userfaultfd_wake_function
);
264 uwq
.wq
.private = current
;
265 uwq
.msg
= userfault_msg(address
, flags
, reason
);
269 spin_lock(&ctx
->fault_wqh
.lock
);
271 * After the __add_wait_queue the uwq is visible to userland
272 * through poll/read().
274 __add_wait_queue(&ctx
->fault_wqh
, &uwq
.wq
);
275 set_current_state(TASK_KILLABLE
);
276 spin_unlock(&ctx
->fault_wqh
.lock
);
278 if (likely(!ACCESS_ONCE(ctx
->released
) &&
279 !fatal_signal_pending(current
))) {
280 wake_up_poll(&ctx
->fd_wqh
, POLLIN
);
282 ret
|= VM_FAULT_MAJOR
;
285 __set_current_state(TASK_RUNNING
);
286 /* see finish_wait() comment for why list_empty_careful() */
287 if (!list_empty_careful(&uwq
.wq
.task_list
)) {
288 spin_lock(&ctx
->fault_wqh
.lock
);
289 list_del_init(&uwq
.wq
.task_list
);
290 spin_unlock(&ctx
->fault_wqh
.lock
);
294 * ctx may go away after this if the userfault pseudo fd is
297 userfaultfd_ctx_put(ctx
);
303 static int userfaultfd_release(struct inode
*inode
, struct file
*file
)
305 struct userfaultfd_ctx
*ctx
= file
->private_data
;
306 struct mm_struct
*mm
= ctx
->mm
;
307 struct vm_area_struct
*vma
, *prev
;
308 /* len == 0 means wake all */
309 struct userfaultfd_wake_range range
= { .len
= 0, };
310 unsigned long new_flags
;
312 ACCESS_ONCE(ctx
->released
) = true;
315 * Flush page faults out of all CPUs. NOTE: all page faults
316 * must be retried without returning VM_FAULT_SIGBUS if
317 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
318 * changes while handle_userfault released the mmap_sem. So
319 * it's critical that released is set to true (above), before
320 * taking the mmap_sem for writing.
322 down_write(&mm
->mmap_sem
);
324 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
326 BUG_ON(!!vma
->vm_userfaultfd_ctx
.ctx
^
327 !!(vma
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
328 if (vma
->vm_userfaultfd_ctx
.ctx
!= ctx
) {
332 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
333 prev
= vma_merge(mm
, prev
, vma
->vm_start
, vma
->vm_end
,
334 new_flags
, vma
->anon_vma
,
335 vma
->vm_file
, vma
->vm_pgoff
,
342 vma
->vm_flags
= new_flags
;
343 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
345 up_write(&mm
->mmap_sem
);
348 * After no new page faults can wait on this fault_wqh, flush
349 * the last page faults that may have been already waiting on
352 spin_lock(&ctx
->fault_wqh
.lock
);
353 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, 0, &range
);
354 spin_unlock(&ctx
->fault_wqh
.lock
);
356 wake_up_poll(&ctx
->fd_wqh
, POLLHUP
);
357 userfaultfd_ctx_put(ctx
);
361 /* fault_wqh.lock must be hold by the caller */
362 static inline unsigned int find_userfault(struct userfaultfd_ctx
*ctx
,
363 struct userfaultfd_wait_queue
**uwq
)
366 struct userfaultfd_wait_queue
*_uwq
;
367 unsigned int ret
= 0;
369 VM_BUG_ON(!spin_is_locked(&ctx
->fault_wqh
.lock
));
371 list_for_each_entry(wq
, &ctx
->fault_wqh
.task_list
, task_list
) {
372 _uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
377 * If there's at least a pending and
378 * we don't care which one it is,
379 * break immediately and leverage the
380 * efficiency of the LIFO walk.
384 * If we need to find which one was pending we
385 * keep walking until we find the first not
386 * pending one, so we read() them in FIFO order.
391 * break the loop at the first not pending
392 * one, there cannot be pending userfaults
393 * after the first not pending one, because
394 * all new pending ones are inserted at the
395 * head and we walk it in LIFO.
403 static unsigned int userfaultfd_poll(struct file
*file
, poll_table
*wait
)
405 struct userfaultfd_ctx
*ctx
= file
->private_data
;
408 poll_wait(file
, &ctx
->fd_wqh
, wait
);
410 switch (ctx
->state
) {
411 case UFFD_STATE_WAIT_API
:
413 case UFFD_STATE_RUNNING
:
415 * poll() never guarantees that read won't block.
416 * userfaults can be waken before they're read().
418 if (unlikely(!(file
->f_flags
& O_NONBLOCK
)))
420 spin_lock(&ctx
->fault_wqh
.lock
);
421 ret
= find_userfault(ctx
, NULL
);
422 spin_unlock(&ctx
->fault_wqh
.lock
);
429 static ssize_t
userfaultfd_ctx_read(struct userfaultfd_ctx
*ctx
, int no_wait
,
430 struct uffd_msg
*msg
)
433 DECLARE_WAITQUEUE(wait
, current
);
434 struct userfaultfd_wait_queue
*uwq
= NULL
;
436 /* always take the fd_wqh lock before the fault_wqh lock */
437 spin_lock(&ctx
->fd_wqh
.lock
);
438 __add_wait_queue(&ctx
->fd_wqh
, &wait
);
440 set_current_state(TASK_INTERRUPTIBLE
);
441 spin_lock(&ctx
->fault_wqh
.lock
);
442 if (find_userfault(ctx
, &uwq
)) {
444 * The fault_wqh.lock prevents the uwq to
445 * disappear from under us.
447 uwq
->pending
= false;
448 /* careful to always initialize msg if ret == 0 */
450 spin_unlock(&ctx
->fault_wqh
.lock
);
454 spin_unlock(&ctx
->fault_wqh
.lock
);
455 if (signal_pending(current
)) {
463 spin_unlock(&ctx
->fd_wqh
.lock
);
465 spin_lock(&ctx
->fd_wqh
.lock
);
467 __remove_wait_queue(&ctx
->fd_wqh
, &wait
);
468 __set_current_state(TASK_RUNNING
);
469 spin_unlock(&ctx
->fd_wqh
.lock
);
474 static ssize_t
userfaultfd_read(struct file
*file
, char __user
*buf
,
475 size_t count
, loff_t
*ppos
)
477 struct userfaultfd_ctx
*ctx
= file
->private_data
;
478 ssize_t _ret
, ret
= 0;
480 int no_wait
= file
->f_flags
& O_NONBLOCK
;
482 if (ctx
->state
== UFFD_STATE_WAIT_API
)
484 BUG_ON(ctx
->state
!= UFFD_STATE_RUNNING
);
487 if (count
< sizeof(msg
))
488 return ret
? ret
: -EINVAL
;
489 _ret
= userfaultfd_ctx_read(ctx
, no_wait
, &msg
);
491 return ret
? ret
: _ret
;
492 if (copy_to_user((__u64 __user
*) buf
, &msg
, sizeof(msg
)))
493 return ret
? ret
: -EFAULT
;
496 count
-= sizeof(msg
);
498 * Allow to read more than one fault at time but only
499 * block if waiting for the very first one.
501 no_wait
= O_NONBLOCK
;
505 static void __wake_userfault(struct userfaultfd_ctx
*ctx
,
506 struct userfaultfd_wake_range
*range
)
508 unsigned long start
, end
;
510 start
= range
->start
;
511 end
= range
->start
+ range
->len
;
513 spin_lock(&ctx
->fault_wqh
.lock
);
514 /* wake all in the range and autoremove */
515 __wake_up_locked_key(&ctx
->fault_wqh
, TASK_NORMAL
, 0, range
);
516 spin_unlock(&ctx
->fault_wqh
.lock
);
519 static __always_inline
void wake_userfault(struct userfaultfd_ctx
*ctx
,
520 struct userfaultfd_wake_range
*range
)
523 * To be sure waitqueue_active() is not reordered by the CPU
524 * before the pagetable update, use an explicit SMP memory
525 * barrier here. PT lock release or up_read(mmap_sem) still
526 * have release semantics that can allow the
527 * waitqueue_active() to be reordered before the pte update.
532 * Use waitqueue_active because it's very frequent to
533 * change the address space atomically even if there are no
534 * userfaults yet. So we take the spinlock only when we're
535 * sure we've userfaults to wake.
537 if (waitqueue_active(&ctx
->fault_wqh
))
538 __wake_userfault(ctx
, range
);
541 static __always_inline
int validate_range(struct mm_struct
*mm
,
542 __u64 start
, __u64 len
)
544 __u64 task_size
= mm
->task_size
;
546 if (start
& ~PAGE_MASK
)
548 if (len
& ~PAGE_MASK
)
552 if (start
< mmap_min_addr
)
554 if (start
>= task_size
)
556 if (len
> task_size
- start
)
561 static int userfaultfd_register(struct userfaultfd_ctx
*ctx
,
564 struct mm_struct
*mm
= ctx
->mm
;
565 struct vm_area_struct
*vma
, *prev
, *cur
;
567 struct uffdio_register uffdio_register
;
568 struct uffdio_register __user
*user_uffdio_register
;
569 unsigned long vm_flags
, new_flags
;
571 unsigned long start
, end
, vma_end
;
573 user_uffdio_register
= (struct uffdio_register __user
*) arg
;
576 if (copy_from_user(&uffdio_register
, user_uffdio_register
,
577 sizeof(uffdio_register
)-sizeof(__u64
)))
581 if (!uffdio_register
.mode
)
583 if (uffdio_register
.mode
& ~(UFFDIO_REGISTER_MODE_MISSING
|
584 UFFDIO_REGISTER_MODE_WP
))
587 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_MISSING
)
588 vm_flags
|= VM_UFFD_MISSING
;
589 if (uffdio_register
.mode
& UFFDIO_REGISTER_MODE_WP
) {
590 vm_flags
|= VM_UFFD_WP
;
592 * FIXME: remove the below error constraint by
593 * implementing the wprotect tracking mode.
599 ret
= validate_range(mm
, uffdio_register
.range
.start
,
600 uffdio_register
.range
.len
);
604 start
= uffdio_register
.range
.start
;
605 end
= start
+ uffdio_register
.range
.len
;
607 down_write(&mm
->mmap_sem
);
608 vma
= find_vma_prev(mm
, start
, &prev
);
614 /* check that there's at least one vma in the range */
616 if (vma
->vm_start
>= end
)
620 * Search for not compatible vmas.
622 * FIXME: this shall be relaxed later so that it doesn't fail
623 * on tmpfs backed vmas (in addition to the current allowance
624 * on anonymous vmas).
627 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
630 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
631 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
633 /* check not compatible vmas */
639 * Check that this vma isn't already owned by a
640 * different userfaultfd. We can't allow more than one
641 * userfaultfd to own a single vma simultaneously or we
642 * wouldn't know which one to deliver the userfaults to.
645 if (cur
->vm_userfaultfd_ctx
.ctx
&&
646 cur
->vm_userfaultfd_ctx
.ctx
!= ctx
)
653 if (vma
->vm_start
< start
)
661 BUG_ON(vma
->vm_userfaultfd_ctx
.ctx
&&
662 vma
->vm_userfaultfd_ctx
.ctx
!= ctx
);
665 * Nothing to do: this vma is already registered into this
666 * userfaultfd and with the right tracking mode too.
668 if (vma
->vm_userfaultfd_ctx
.ctx
== ctx
&&
669 (vma
->vm_flags
& vm_flags
) == vm_flags
)
672 if (vma
->vm_start
> start
)
673 start
= vma
->vm_start
;
674 vma_end
= min(end
, vma
->vm_end
);
676 new_flags
= (vma
->vm_flags
& ~vm_flags
) | vm_flags
;
677 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
678 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
680 ((struct vm_userfaultfd_ctx
){ ctx
}));
685 if (vma
->vm_start
< start
) {
686 ret
= split_vma(mm
, vma
, start
, 1);
690 if (vma
->vm_end
> end
) {
691 ret
= split_vma(mm
, vma
, end
, 0);
697 * In the vma_merge() successful mprotect-like case 8:
698 * the next vma was merged into the current one and
699 * the current one has not been updated yet.
701 vma
->vm_flags
= new_flags
;
702 vma
->vm_userfaultfd_ctx
.ctx
= ctx
;
708 } while (vma
&& vma
->vm_start
< end
);
710 up_write(&mm
->mmap_sem
);
713 * Now that we scanned all vmas we can already tell
714 * userland which ioctls methods are guaranteed to
715 * succeed on this range.
717 if (put_user(UFFD_API_RANGE_IOCTLS
,
718 &user_uffdio_register
->ioctls
))
725 static int userfaultfd_unregister(struct userfaultfd_ctx
*ctx
,
728 struct mm_struct
*mm
= ctx
->mm
;
729 struct vm_area_struct
*vma
, *prev
, *cur
;
731 struct uffdio_range uffdio_unregister
;
732 unsigned long new_flags
;
734 unsigned long start
, end
, vma_end
;
735 const void __user
*buf
= (void __user
*)arg
;
738 if (copy_from_user(&uffdio_unregister
, buf
, sizeof(uffdio_unregister
)))
741 ret
= validate_range(mm
, uffdio_unregister
.start
,
742 uffdio_unregister
.len
);
746 start
= uffdio_unregister
.start
;
747 end
= start
+ uffdio_unregister
.len
;
749 down_write(&mm
->mmap_sem
);
750 vma
= find_vma_prev(mm
, start
, &prev
);
756 /* check that there's at least one vma in the range */
758 if (vma
->vm_start
>= end
)
762 * Search for not compatible vmas.
764 * FIXME: this shall be relaxed later so that it doesn't fail
765 * on tmpfs backed vmas (in addition to the current allowance
766 * on anonymous vmas).
770 for (cur
= vma
; cur
&& cur
->vm_start
< end
; cur
= cur
->vm_next
) {
773 BUG_ON(!!cur
->vm_userfaultfd_ctx
.ctx
^
774 !!(cur
->vm_flags
& (VM_UFFD_MISSING
| VM_UFFD_WP
)));
777 * Check not compatible vmas, not strictly required
778 * here as not compatible vmas cannot have an
779 * userfaultfd_ctx registered on them, but this
780 * provides for more strict behavior to notice
781 * unregistration errors.
790 if (vma
->vm_start
< start
)
800 * Nothing to do: this vma is already registered into this
801 * userfaultfd and with the right tracking mode too.
803 if (!vma
->vm_userfaultfd_ctx
.ctx
)
806 if (vma
->vm_start
> start
)
807 start
= vma
->vm_start
;
808 vma_end
= min(end
, vma
->vm_end
);
810 new_flags
= vma
->vm_flags
& ~(VM_UFFD_MISSING
| VM_UFFD_WP
);
811 prev
= vma_merge(mm
, prev
, start
, vma_end
, new_flags
,
812 vma
->anon_vma
, vma
->vm_file
, vma
->vm_pgoff
,
819 if (vma
->vm_start
< start
) {
820 ret
= split_vma(mm
, vma
, start
, 1);
824 if (vma
->vm_end
> end
) {
825 ret
= split_vma(mm
, vma
, end
, 0);
831 * In the vma_merge() successful mprotect-like case 8:
832 * the next vma was merged into the current one and
833 * the current one has not been updated yet.
835 vma
->vm_flags
= new_flags
;
836 vma
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
842 } while (vma
&& vma
->vm_start
< end
);
844 up_write(&mm
->mmap_sem
);
850 * userfaultfd_wake is needed in case an userfault is in flight by the
851 * time a UFFDIO_COPY (or other ioctl variants) completes. The page
852 * may be well get mapped and the page fault if repeated wouldn't lead
853 * to a userfault anymore, but before scheduling in TASK_KILLABLE mode
854 * handle_userfault() doesn't recheck the pagetables and it doesn't
855 * serialize against UFFDO_COPY (or other ioctl variants). Ultimately
856 * the knowledge of which pages are mapped is left to userland who is
857 * responsible for handling the race between read() userfaults and
858 * background UFFDIO_COPY (or other ioctl variants), if done by
859 * separate concurrent threads.
861 * userfaultfd_wake may be used in combination with the
862 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
864 static int userfaultfd_wake(struct userfaultfd_ctx
*ctx
,
868 struct uffdio_range uffdio_wake
;
869 struct userfaultfd_wake_range range
;
870 const void __user
*buf
= (void __user
*)arg
;
873 if (copy_from_user(&uffdio_wake
, buf
, sizeof(uffdio_wake
)))
876 ret
= validate_range(ctx
->mm
, uffdio_wake
.start
, uffdio_wake
.len
);
880 range
.start
= uffdio_wake
.start
;
881 range
.len
= uffdio_wake
.len
;
884 * len == 0 means wake all and we don't want to wake all here,
885 * so check it again to be sure.
887 VM_BUG_ON(!range
.len
);
889 wake_userfault(ctx
, &range
);
897 * userland asks for a certain API version and we return which bits
898 * and ioctl commands are implemented in this kernel for such API
899 * version or -EINVAL if unknown.
901 static int userfaultfd_api(struct userfaultfd_ctx
*ctx
,
904 struct uffdio_api uffdio_api
;
905 void __user
*buf
= (void __user
*)arg
;
909 if (ctx
->state
!= UFFD_STATE_WAIT_API
)
912 if (copy_from_user(&uffdio_api
, buf
, sizeof(uffdio_api
)))
914 if (uffdio_api
.api
!= UFFD_API
|| uffdio_api
.features
) {
915 memset(&uffdio_api
, 0, sizeof(uffdio_api
));
916 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
921 uffdio_api
.features
= UFFD_API_FEATURES
;
922 uffdio_api
.ioctls
= UFFD_API_IOCTLS
;
924 if (copy_to_user(buf
, &uffdio_api
, sizeof(uffdio_api
)))
926 ctx
->state
= UFFD_STATE_RUNNING
;
932 static long userfaultfd_ioctl(struct file
*file
, unsigned cmd
,
936 struct userfaultfd_ctx
*ctx
= file
->private_data
;
940 ret
= userfaultfd_api(ctx
, arg
);
942 case UFFDIO_REGISTER
:
943 ret
= userfaultfd_register(ctx
, arg
);
945 case UFFDIO_UNREGISTER
:
946 ret
= userfaultfd_unregister(ctx
, arg
);
949 ret
= userfaultfd_wake(ctx
, arg
);
955 #ifdef CONFIG_PROC_FS
956 static void userfaultfd_show_fdinfo(struct seq_file
*m
, struct file
*f
)
958 struct userfaultfd_ctx
*ctx
= f
->private_data
;
960 struct userfaultfd_wait_queue
*uwq
;
961 unsigned long pending
= 0, total
= 0;
963 spin_lock(&ctx
->fault_wqh
.lock
);
964 list_for_each_entry(wq
, &ctx
->fault_wqh
.task_list
, task_list
) {
965 uwq
= container_of(wq
, struct userfaultfd_wait_queue
, wq
);
970 spin_unlock(&ctx
->fault_wqh
.lock
);
973 * If more protocols will be added, there will be all shown
974 * separated by a space. Like this:
975 * protocols: aa:... bb:...
977 seq_printf(m
, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
978 pending
, total
, UFFD_API
, UFFD_API_FEATURES
,
979 UFFD_API_IOCTLS
|UFFD_API_RANGE_IOCTLS
);
983 static const struct file_operations userfaultfd_fops
= {
984 #ifdef CONFIG_PROC_FS
985 .show_fdinfo
= userfaultfd_show_fdinfo
,
987 .release
= userfaultfd_release
,
988 .poll
= userfaultfd_poll
,
989 .read
= userfaultfd_read
,
990 .unlocked_ioctl
= userfaultfd_ioctl
,
991 .compat_ioctl
= userfaultfd_ioctl
,
992 .llseek
= noop_llseek
,
996 * userfaultfd_file_create - Creates an userfaultfd file pointer.
997 * @flags: Flags for the userfaultfd file.
999 * This function creates an userfaultfd file pointer, w/out installing
1000 * it into the fd table. This is useful when the userfaultfd file is
1001 * used during the initialization of data structures that require
1002 * extra setup after the userfaultfd creation. So the userfaultfd
1003 * creation is split into the file pointer creation phase, and the
1004 * file descriptor installation phase. In this way races with
1005 * userspace closing the newly installed file descriptor can be
1006 * avoided. Returns an userfaultfd file pointer, or a proper error
1009 static struct file
*userfaultfd_file_create(int flags
)
1012 struct userfaultfd_ctx
*ctx
;
1014 BUG_ON(!current
->mm
);
1016 /* Check the UFFD_* constants for consistency. */
1017 BUILD_BUG_ON(UFFD_CLOEXEC
!= O_CLOEXEC
);
1018 BUILD_BUG_ON(UFFD_NONBLOCK
!= O_NONBLOCK
);
1020 file
= ERR_PTR(-EINVAL
);
1021 if (flags
& ~UFFD_SHARED_FCNTL_FLAGS
)
1024 file
= ERR_PTR(-ENOMEM
);
1025 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
1029 atomic_set(&ctx
->refcount
, 1);
1030 init_waitqueue_head(&ctx
->fault_wqh
);
1031 init_waitqueue_head(&ctx
->fd_wqh
);
1033 ctx
->state
= UFFD_STATE_WAIT_API
;
1034 ctx
->released
= false;
1035 ctx
->mm
= current
->mm
;
1036 /* prevent the mm struct to be freed */
1037 atomic_inc(&ctx
->mm
->mm_users
);
1039 file
= anon_inode_getfile("[userfaultfd]", &userfaultfd_fops
, ctx
,
1040 O_RDWR
| (flags
& UFFD_SHARED_FCNTL_FLAGS
));
1047 SYSCALL_DEFINE1(userfaultfd
, int, flags
)
1052 error
= get_unused_fd_flags(flags
& UFFD_SHARED_FCNTL_FLAGS
);
1057 file
= userfaultfd_file_create(flags
);
1059 error
= PTR_ERR(file
);
1060 goto err_put_unused_fd
;
1062 fd_install(fd
, file
);