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 {
53 unsigned long address;
56 struct userfaultfd_ctx *ctx;
59 struct userfaultfd_wake_range {
64 static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
65 int wake_flags, void *key)
67 struct userfaultfd_wake_range *range = key;
69 struct userfaultfd_wait_queue *uwq;
70 unsigned long start, len;
72 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
74 /* don't wake the pending ones to avoid reads to block */
75 if (uwq->pending && !ACCESS_ONCE(uwq->ctx->released))
77 /* len == 0 means wake all */
80 if (len && (start > uwq->address || start + len <= uwq->address))
82 ret = wake_up_state(wq->private, mode);
85 * Wake only once, autoremove behavior.
87 * After the effect of list_del_init is visible to the
88 * other CPUs, the waitqueue may disappear from under
89 * us, see the !list_empty_careful() in
90 * handle_userfault(). try_to_wake_up() has an
91 * implicit smp_mb__before_spinlock, and the
92 * wq->private is read before calling the extern
93 * function "wake_up_state" (which in turns calls
94 * try_to_wake_up). While the spin_lock;spin_unlock;
95 * wouldn't be enough, the smp_mb__before_spinlock is
96 * enough to avoid an explicit smp_mb() here.
98 list_del_init(&wq->task_list);
104 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
106 * @ctx: [in] Pointer to the userfaultfd context.
108 * Returns: In case of success, returns not zero.
110 static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
112 if (!atomic_inc_not_zero(&ctx->refcount))
117 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
119 * @ctx: [in] Pointer to userfaultfd context.
121 * The userfaultfd context reference must have been previously acquired either
122 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
124 static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
126 if (atomic_dec_and_test(&ctx->refcount)) {
127 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
128 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
129 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
130 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
131 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
132 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
138 static inline unsigned long userfault_address(unsigned long address,
140 unsigned long reason)
142 BUILD_BUG_ON(PAGE_SHIFT < UFFD_BITS);
143 address &= PAGE_MASK;
144 if (flags & FAULT_FLAG_WRITE)
146 * Encode "write" fault information in the LSB of the
147 * address read by userland, without depending on
148 * FAULT_FLAG_WRITE kernel internal value.
150 address |= UFFD_BIT_WRITE;
151 if (reason & VM_UFFD_WP)
153 * Encode "reason" fault information as bit number 1
154 * in the address read by userland. If bit number 1 is
155 * clear it means the reason is a VM_FAULT_MISSING
158 address |= UFFD_BIT_WP;
163 * The locking rules involved in returning VM_FAULT_RETRY depending on
164 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
165 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
166 * recommendation in __lock_page_or_retry is not an understatement.
168 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
169 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
172 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
173 * set, VM_FAULT_RETRY can still be returned if and only if there are
174 * fatal_signal_pending()s, and the mmap_sem must be released before
177 int handle_userfault(struct vm_area_struct *vma, unsigned long address,
178 unsigned int flags, unsigned long reason)
180 struct mm_struct *mm = vma->vm_mm;
181 struct userfaultfd_ctx *ctx;
182 struct userfaultfd_wait_queue uwq;
184 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
186 ctx = vma->vm_userfaultfd_ctx.ctx;
188 return VM_FAULT_SIGBUS;
190 BUG_ON(ctx->mm != mm);
192 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
193 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
196 * If it's already released don't get it. This avoids to loop
197 * in __get_user_pages if userfaultfd_release waits on the
198 * caller of handle_userfault to release the mmap_sem.
200 if (unlikely(ACCESS_ONCE(ctx->released)))
201 return VM_FAULT_SIGBUS;
204 * Check that we can return VM_FAULT_RETRY.
206 * NOTE: it should become possible to return VM_FAULT_RETRY
207 * even if FAULT_FLAG_TRIED is set without leading to gup()
208 * -EBUSY failures, if the userfaultfd is to be extended for
209 * VM_UFFD_WP tracking and we intend to arm the userfault
210 * without first stopping userland access to the memory. For
211 * VM_UFFD_MISSING userfaults this is enough for now.
213 if (unlikely(!(flags & FAULT_FLAG_ALLOW_RETRY))) {
215 * Validate the invariant that nowait must allow retry
216 * to be sure not to return SIGBUS erroneously on
217 * nowait invocations.
219 BUG_ON(flags & FAULT_FLAG_RETRY_NOWAIT);
220 #ifdef CONFIG_DEBUG_VM
221 if (printk_ratelimit()) {
223 "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags);
227 return VM_FAULT_SIGBUS;
231 * Handle nowait, not much to do other than tell it to retry
234 if (flags & FAULT_FLAG_RETRY_NOWAIT)
235 return VM_FAULT_RETRY;
237 /* take the reference before dropping the mmap_sem */
238 userfaultfd_ctx_get(ctx);
240 /* be gentle and immediately relinquish the mmap_sem */
241 up_read(&mm->mmap_sem);
243 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
244 uwq.wq.private = current;
245 uwq.address = userfault_address(address, flags, reason);
249 spin_lock(&ctx->fault_wqh.lock);
251 * After the __add_wait_queue the uwq is visible to userland
252 * through poll/read().
254 __add_wait_queue(&ctx->fault_wqh, &uwq.wq);
256 set_current_state(TASK_KILLABLE);
257 if (!uwq.pending || ACCESS_ONCE(ctx->released) ||
258 fatal_signal_pending(current))
260 spin_unlock(&ctx->fault_wqh.lock);
262 wake_up_poll(&ctx->fd_wqh, POLLIN);
265 spin_lock(&ctx->fault_wqh.lock);
267 __remove_wait_queue(&ctx->fault_wqh, &uwq.wq);
268 __set_current_state(TASK_RUNNING);
269 spin_unlock(&ctx->fault_wqh.lock);
272 * ctx may go away after this if the userfault pseudo fd is
275 userfaultfd_ctx_put(ctx);
277 return VM_FAULT_RETRY;
280 static int userfaultfd_release(struct inode *inode, struct file *file)
282 struct userfaultfd_ctx *ctx = file->private_data;
283 struct mm_struct *mm = ctx->mm;
284 struct vm_area_struct *vma, *prev;
285 /* len == 0 means wake all */
286 struct userfaultfd_wake_range range = { .len = 0, };
287 unsigned long new_flags;
289 ACCESS_ONCE(ctx->released) = true;
292 * Flush page faults out of all CPUs. NOTE: all page faults
293 * must be retried without returning VM_FAULT_SIGBUS if
294 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
295 * changes while handle_userfault released the mmap_sem. So
296 * it's critical that released is set to true (above), before
297 * taking the mmap_sem for writing.
299 down_write(&mm->mmap_sem);
301 for (vma = mm->mmap; vma; vma = vma->vm_next) {
303 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
304 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
305 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
309 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
310 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
311 new_flags, vma->anon_vma,
312 vma->vm_file, vma->vm_pgoff,
319 vma->vm_flags = new_flags;
320 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
322 up_write(&mm->mmap_sem);
325 * After no new page faults can wait on this fault_wqh, flush
326 * the last page faults that may have been already waiting on
329 spin_lock(&ctx->fault_wqh.lock);
330 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, 0, &range);
331 spin_unlock(&ctx->fault_wqh.lock);
333 wake_up_poll(&ctx->fd_wqh, POLLHUP);
334 userfaultfd_ctx_put(ctx);
338 /* fault_wqh.lock must be hold by the caller */
339 static inline unsigned int find_userfault(struct userfaultfd_ctx *ctx,
340 struct userfaultfd_wait_queue **uwq)
343 struct userfaultfd_wait_queue *_uwq;
344 unsigned int ret = 0;
346 VM_BUG_ON(!spin_is_locked(&ctx->fault_wqh.lock));
348 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
349 _uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
354 * If there's at least a pending and
355 * we don't care which one it is,
356 * break immediately and leverage the
357 * efficiency of the LIFO walk.
361 * If we need to find which one was pending we
362 * keep walking until we find the first not
363 * pending one, so we read() them in FIFO order.
368 * break the loop at the first not pending
369 * one, there cannot be pending userfaults
370 * after the first not pending one, because
371 * all new pending ones are inserted at the
372 * head and we walk it in LIFO.
380 static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
382 struct userfaultfd_ctx *ctx = file->private_data;
385 poll_wait(file, &ctx->fd_wqh, wait);
387 switch (ctx->state) {
388 case UFFD_STATE_WAIT_API:
390 case UFFD_STATE_RUNNING:
391 spin_lock(&ctx->fault_wqh.lock);
392 ret = find_userfault(ctx, NULL);
393 spin_unlock(&ctx->fault_wqh.lock);
400 static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
404 DECLARE_WAITQUEUE(wait, current);
405 struct userfaultfd_wait_queue *uwq = NULL;
407 /* always take the fd_wqh lock before the fault_wqh lock */
408 spin_lock(&ctx->fd_wqh.lock);
409 __add_wait_queue(&ctx->fd_wqh, &wait);
411 set_current_state(TASK_INTERRUPTIBLE);
412 spin_lock(&ctx->fault_wqh.lock);
413 if (find_userfault(ctx, &uwq)) {
415 * The fault_wqh.lock prevents the uwq to
416 * disappear from under us.
418 uwq->pending = false;
419 /* careful to always initialize addr if ret == 0 */
420 *addr = uwq->address;
421 spin_unlock(&ctx->fault_wqh.lock);
425 spin_unlock(&ctx->fault_wqh.lock);
426 if (signal_pending(current)) {
434 spin_unlock(&ctx->fd_wqh.lock);
436 spin_lock(&ctx->fd_wqh.lock);
438 __remove_wait_queue(&ctx->fd_wqh, &wait);
439 __set_current_state(TASK_RUNNING);
440 spin_unlock(&ctx->fd_wqh.lock);
445 static ssize_t userfaultfd_read(struct file *file, char __user *buf,
446 size_t count, loff_t *ppos)
448 struct userfaultfd_ctx *ctx = file->private_data;
449 ssize_t _ret, ret = 0;
450 /* careful to always initialize addr if ret == 0 */
451 __u64 uninitialized_var(addr);
452 int no_wait = file->f_flags & O_NONBLOCK;
454 if (ctx->state == UFFD_STATE_WAIT_API)
456 BUG_ON(ctx->state != UFFD_STATE_RUNNING);
459 if (count < sizeof(addr))
460 return ret ? ret : -EINVAL;
461 _ret = userfaultfd_ctx_read(ctx, no_wait, &addr);
463 return ret ? ret : _ret;
464 if (put_user(addr, (__u64 __user *) buf))
465 return ret ? ret : -EFAULT;
468 count -= sizeof(addr);
470 * Allow to read more than one fault at time but only
471 * block if waiting for the very first one.
473 no_wait = O_NONBLOCK;
477 static void __wake_userfault(struct userfaultfd_ctx *ctx,
478 struct userfaultfd_wake_range *range)
480 unsigned long start, end;
482 start = range->start;
483 end = range->start + range->len;
485 spin_lock(&ctx->fault_wqh.lock);
486 /* wake all in the range and autoremove */
487 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, 0, range);
488 spin_unlock(&ctx->fault_wqh.lock);
491 static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
492 struct userfaultfd_wake_range *range)
495 * To be sure waitqueue_active() is not reordered by the CPU
496 * before the pagetable update, use an explicit SMP memory
497 * barrier here. PT lock release or up_read(mmap_sem) still
498 * have release semantics that can allow the
499 * waitqueue_active() to be reordered before the pte update.
504 * Use waitqueue_active because it's very frequent to
505 * change the address space atomically even if there are no
506 * userfaults yet. So we take the spinlock only when we're
507 * sure we've userfaults to wake.
509 if (waitqueue_active(&ctx->fault_wqh))
510 __wake_userfault(ctx, range);
513 static __always_inline int validate_range(struct mm_struct *mm,
514 __u64 start, __u64 len)
516 __u64 task_size = mm->task_size;
518 if (start & ~PAGE_MASK)
520 if (len & ~PAGE_MASK)
524 if (start < mmap_min_addr)
526 if (start >= task_size)
528 if (len > task_size - start)
533 static int userfaultfd_register(struct userfaultfd_ctx *ctx,
536 struct mm_struct *mm = ctx->mm;
537 struct vm_area_struct *vma, *prev, *cur;
539 struct uffdio_register uffdio_register;
540 struct uffdio_register __user *user_uffdio_register;
541 unsigned long vm_flags, new_flags;
543 unsigned long start, end, vma_end;
545 user_uffdio_register = (struct uffdio_register __user *) arg;
548 if (copy_from_user(&uffdio_register, user_uffdio_register,
549 sizeof(uffdio_register)-sizeof(__u64)))
553 if (!uffdio_register.mode)
555 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
556 UFFDIO_REGISTER_MODE_WP))
559 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
560 vm_flags |= VM_UFFD_MISSING;
561 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
562 vm_flags |= VM_UFFD_WP;
564 * FIXME: remove the below error constraint by
565 * implementing the wprotect tracking mode.
571 ret = validate_range(mm, uffdio_register.range.start,
572 uffdio_register.range.len);
576 start = uffdio_register.range.start;
577 end = start + uffdio_register.range.len;
579 down_write(&mm->mmap_sem);
580 vma = find_vma_prev(mm, start, &prev);
586 /* check that there's at least one vma in the range */
588 if (vma->vm_start >= end)
592 * Search for not compatible vmas.
594 * FIXME: this shall be relaxed later so that it doesn't fail
595 * on tmpfs backed vmas (in addition to the current allowance
596 * on anonymous vmas).
599 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
602 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
603 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
605 /* check not compatible vmas */
611 * Check that this vma isn't already owned by a
612 * different userfaultfd. We can't allow more than one
613 * userfaultfd to own a single vma simultaneously or we
614 * wouldn't know which one to deliver the userfaults to.
617 if (cur->vm_userfaultfd_ctx.ctx &&
618 cur->vm_userfaultfd_ctx.ctx != ctx)
625 if (vma->vm_start < start)
633 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
634 vma->vm_userfaultfd_ctx.ctx != ctx);
637 * Nothing to do: this vma is already registered into this
638 * userfaultfd and with the right tracking mode too.
640 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
641 (vma->vm_flags & vm_flags) == vm_flags)
644 if (vma->vm_start > start)
645 start = vma->vm_start;
646 vma_end = min(end, vma->vm_end);
648 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
649 prev = vma_merge(mm, prev, start, vma_end, new_flags,
650 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
652 ((struct vm_userfaultfd_ctx){ ctx }));
657 if (vma->vm_start < start) {
658 ret = split_vma(mm, vma, start, 1);
662 if (vma->vm_end > end) {
663 ret = split_vma(mm, vma, end, 0);
669 * In the vma_merge() successful mprotect-like case 8:
670 * the next vma was merged into the current one and
671 * the current one has not been updated yet.
673 vma->vm_flags = new_flags;
674 vma->vm_userfaultfd_ctx.ctx = ctx;
680 } while (vma && vma->vm_start < end);
682 up_write(&mm->mmap_sem);
685 * Now that we scanned all vmas we can already tell
686 * userland which ioctls methods are guaranteed to
687 * succeed on this range.
689 if (put_user(UFFD_API_RANGE_IOCTLS,
690 &user_uffdio_register->ioctls))
697 static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
700 struct mm_struct *mm = ctx->mm;
701 struct vm_area_struct *vma, *prev, *cur;
703 struct uffdio_range uffdio_unregister;
704 unsigned long new_flags;
706 unsigned long start, end, vma_end;
707 const void __user *buf = (void __user *)arg;
710 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
713 ret = validate_range(mm, uffdio_unregister.start,
714 uffdio_unregister.len);
718 start = uffdio_unregister.start;
719 end = start + uffdio_unregister.len;
721 down_write(&mm->mmap_sem);
722 vma = find_vma_prev(mm, start, &prev);
728 /* check that there's at least one vma in the range */
730 if (vma->vm_start >= end)
734 * Search for not compatible vmas.
736 * FIXME: this shall be relaxed later so that it doesn't fail
737 * on tmpfs backed vmas (in addition to the current allowance
738 * on anonymous vmas).
742 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
745 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
746 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
749 * Check not compatible vmas, not strictly required
750 * here as not compatible vmas cannot have an
751 * userfaultfd_ctx registered on them, but this
752 * provides for more strict behavior to notice
753 * unregistration errors.
762 if (vma->vm_start < start)
772 * Nothing to do: this vma is already registered into this
773 * userfaultfd and with the right tracking mode too.
775 if (!vma->vm_userfaultfd_ctx.ctx)
778 if (vma->vm_start > start)
779 start = vma->vm_start;
780 vma_end = min(end, vma->vm_end);
782 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
783 prev = vma_merge(mm, prev, start, vma_end, new_flags,
784 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
791 if (vma->vm_start < start) {
792 ret = split_vma(mm, vma, start, 1);
796 if (vma->vm_end > end) {
797 ret = split_vma(mm, vma, end, 0);
803 * In the vma_merge() successful mprotect-like case 8:
804 * the next vma was merged into the current one and
805 * the current one has not been updated yet.
807 vma->vm_flags = new_flags;
808 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
814 } while (vma && vma->vm_start < end);
816 up_write(&mm->mmap_sem);
822 * This is mostly needed to re-wakeup those userfaults that were still
823 * pending when userland wake them up the first time. We don't wake
824 * the pending one to avoid blocking reads to block, or non blocking
825 * read to return -EAGAIN, if used with POLLIN, to avoid userland
826 * doubts on why POLLIN wasn't reliable.
828 static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
832 struct uffdio_range uffdio_wake;
833 struct userfaultfd_wake_range range;
834 const void __user *buf = (void __user *)arg;
837 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
840 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
844 range.start = uffdio_wake.start;
845 range.len = uffdio_wake.len;
848 * len == 0 means wake all and we don't want to wake all here,
849 * so check it again to be sure.
851 VM_BUG_ON(!range.len);
853 wake_userfault(ctx, &range);
861 * userland asks for a certain API version and we return which bits
862 * and ioctl commands are implemented in this kernel for such API
863 * version or -EINVAL if unknown.
865 static int userfaultfd_api(struct userfaultfd_ctx *ctx,
868 struct uffdio_api uffdio_api;
869 void __user *buf = (void __user *)arg;
873 if (ctx->state != UFFD_STATE_WAIT_API)
876 if (copy_from_user(&uffdio_api, buf, sizeof(__u64)))
878 if (uffdio_api.api != UFFD_API) {
879 /* careful not to leak info, we only read the first 8 bytes */
880 memset(&uffdio_api, 0, sizeof(uffdio_api));
881 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
886 /* careful not to leak info, we only read the first 8 bytes */
887 uffdio_api.features = UFFD_API_FEATURES;
888 uffdio_api.ioctls = UFFD_API_IOCTLS;
890 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
892 ctx->state = UFFD_STATE_RUNNING;
898 static long userfaultfd_ioctl(struct file *file, unsigned cmd,
902 struct userfaultfd_ctx *ctx = file->private_data;
906 ret = userfaultfd_api(ctx, arg);
908 case UFFDIO_REGISTER:
909 ret = userfaultfd_register(ctx, arg);
911 case UFFDIO_UNREGISTER:
912 ret = userfaultfd_unregister(ctx, arg);
915 ret = userfaultfd_wake(ctx, arg);
921 #ifdef CONFIG_PROC_FS
922 static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
924 struct userfaultfd_ctx *ctx = f->private_data;
926 struct userfaultfd_wait_queue *uwq;
927 unsigned long pending = 0, total = 0;
929 spin_lock(&ctx->fault_wqh.lock);
930 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
931 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
936 spin_unlock(&ctx->fault_wqh.lock);
939 * If more protocols will be added, there will be all shown
940 * separated by a space. Like this:
941 * protocols: aa:... bb:...
943 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
944 pending, total, UFFD_API, UFFD_API_FEATURES,
945 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
949 static const struct file_operations userfaultfd_fops = {
950 #ifdef CONFIG_PROC_FS
951 .show_fdinfo = userfaultfd_show_fdinfo,
953 .release = userfaultfd_release,
954 .poll = userfaultfd_poll,
955 .read = userfaultfd_read,
956 .unlocked_ioctl = userfaultfd_ioctl,
957 .compat_ioctl = userfaultfd_ioctl,
958 .llseek = noop_llseek,
962 * userfaultfd_file_create - Creates an userfaultfd file pointer.
963 * @flags: Flags for the userfaultfd file.
965 * This function creates an userfaultfd file pointer, w/out installing
966 * it into the fd table. This is useful when the userfaultfd file is
967 * used during the initialization of data structures that require
968 * extra setup after the userfaultfd creation. So the userfaultfd
969 * creation is split into the file pointer creation phase, and the
970 * file descriptor installation phase. In this way races with
971 * userspace closing the newly installed file descriptor can be
972 * avoided. Returns an userfaultfd file pointer, or a proper error
975 static struct file *userfaultfd_file_create(int flags)
978 struct userfaultfd_ctx *ctx;
980 BUG_ON(!current->mm);
982 /* Check the UFFD_* constants for consistency. */
983 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
984 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
986 file = ERR_PTR(-EINVAL);
987 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
990 file = ERR_PTR(-ENOMEM);
991 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
995 atomic_set(&ctx->refcount, 1);
996 init_waitqueue_head(&ctx->fault_wqh);
997 init_waitqueue_head(&ctx->fd_wqh);
999 ctx->state = UFFD_STATE_WAIT_API;
1000 ctx->released = false;
1001 ctx->mm = current->mm;
1002 /* prevent the mm struct to be freed */
1003 atomic_inc(&ctx->mm->mm_users);
1005 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1006 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1013 SYSCALL_DEFINE1(userfaultfd, int, flags)
1018 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1023 file = userfaultfd_file_create(flags);
1025 error = PTR_ERR(file);
1026 goto err_put_unused_fd;
1028 fd_install(fd, file);