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/list.h>
16 #include <linux/hashtable.h>
17 #include <linux/sched.h>
19 #include <linux/poll.h>
20 #include <linux/slab.h>
21 #include <linux/seq_file.h>
22 #include <linux/file.h>
23 #include <linux/bug.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/syscalls.h>
26 #include <linux/userfaultfd_k.h>
27 #include <linux/mempolicy.h>
28 #include <linux/ioctl.h>
29 #include <linux/security.h>
30 #include <linux/hugetlb.h>
32 static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
34 enum userfaultfd_state {
40 * Start with fault_pending_wqh and fault_wqh so they're more likely
41 * to be in the same cacheline.
43 struct userfaultfd_ctx {
44 /* waitqueue head for the pending (i.e. not read) userfaults */
45 wait_queue_head_t fault_pending_wqh;
46 /* waitqueue head for the userfaults */
47 wait_queue_head_t fault_wqh;
48 /* waitqueue head for the pseudo fd to wakeup poll/read */
49 wait_queue_head_t fd_wqh;
50 /* waitqueue head for events */
51 wait_queue_head_t event_wqh;
52 /* a refile sequence protected by fault_pending_wqh lock */
53 struct seqcount refile_seq;
54 /* pseudo fd refcounting */
56 /* userfaultfd syscall flags */
58 /* features requested from the userspace */
59 unsigned int features;
61 enum userfaultfd_state state;
64 /* mm with one ore more vmas attached to this userfaultfd_ctx */
68 struct userfaultfd_fork_ctx {
69 struct userfaultfd_ctx *orig;
70 struct userfaultfd_ctx *new;
71 struct list_head list;
74 struct userfaultfd_wait_queue {
77 struct userfaultfd_ctx *ctx;
81 struct userfaultfd_wake_range {
86 static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
87 int wake_flags, void *key)
89 struct userfaultfd_wake_range *range = key;
91 struct userfaultfd_wait_queue *uwq;
92 unsigned long start, len;
94 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
96 /* len == 0 means wake all */
99 if (len && (start > uwq->msg.arg.pagefault.address ||
100 start + len <= uwq->msg.arg.pagefault.address))
102 WRITE_ONCE(uwq->waken, true);
104 * The implicit smp_mb__before_spinlock in try_to_wake_up()
105 * renders uwq->waken visible to other CPUs before the task is
108 ret = wake_up_state(wq->private, mode);
111 * Wake only once, autoremove behavior.
113 * After the effect of list_del_init is visible to the
114 * other CPUs, the waitqueue may disappear from under
115 * us, see the !list_empty_careful() in
116 * handle_userfault(). try_to_wake_up() has an
117 * implicit smp_mb__before_spinlock, and the
118 * wq->private is read before calling the extern
119 * function "wake_up_state" (which in turns calls
120 * try_to_wake_up). While the spin_lock;spin_unlock;
121 * wouldn't be enough, the smp_mb__before_spinlock is
122 * enough to avoid an explicit smp_mb() here.
124 list_del_init(&wq->task_list);
130 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
132 * @ctx: [in] Pointer to the userfaultfd context.
134 * Returns: In case of success, returns not zero.
136 static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
138 if (!atomic_inc_not_zero(&ctx->refcount))
143 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
145 * @ctx: [in] Pointer to userfaultfd context.
147 * The userfaultfd context reference must have been previously acquired either
148 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
150 static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
152 if (atomic_dec_and_test(&ctx->refcount)) {
153 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
154 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
155 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
156 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
157 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
158 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
159 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
160 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
162 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
166 static inline void msg_init(struct uffd_msg *msg)
168 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
170 * Must use memset to zero out the paddings or kernel data is
171 * leaked to userland.
173 memset(msg, 0, sizeof(struct uffd_msg));
176 static inline struct uffd_msg userfault_msg(unsigned long address,
178 unsigned long reason)
182 msg.event = UFFD_EVENT_PAGEFAULT;
183 msg.arg.pagefault.address = address;
184 if (flags & FAULT_FLAG_WRITE)
186 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
187 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
188 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
189 * was a read fault, otherwise if set it means it's
192 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
193 if (reason & VM_UFFD_WP)
195 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
196 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
197 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
198 * a missing fault, otherwise if set it means it's a
199 * write protect fault.
201 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
206 * Verify the pagetables are still not ok after having reigstered into
207 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
208 * userfault that has already been resolved, if userfaultfd_read and
209 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
212 static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
213 unsigned long address,
215 unsigned long reason)
217 struct mm_struct *mm = ctx->mm;
224 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
226 pgd = pgd_offset(mm, address);
227 if (!pgd_present(*pgd))
229 pud = pud_offset(pgd, address);
230 if (!pud_present(*pud))
232 pmd = pmd_offset(pud, address);
234 * READ_ONCE must function as a barrier with narrower scope
235 * and it must be equivalent to:
236 * _pmd = *pmd; barrier();
238 * This is to deal with the instability (as in
239 * pmd_trans_unstable) of the pmd.
241 _pmd = READ_ONCE(*pmd);
242 if (!pmd_present(_pmd))
246 if (pmd_trans_huge(_pmd))
250 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
251 * and use the standard pte_offset_map() instead of parsing _pmd.
253 pte = pte_offset_map(pmd, address);
255 * Lockless access: we're in a wait_event so it's ok if it
267 * The locking rules involved in returning VM_FAULT_RETRY depending on
268 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
269 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
270 * recommendation in __lock_page_or_retry is not an understatement.
272 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
273 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
276 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
277 * set, VM_FAULT_RETRY can still be returned if and only if there are
278 * fatal_signal_pending()s, and the mmap_sem must be released before
281 int handle_userfault(struct vm_fault *vmf, unsigned long reason)
283 struct mm_struct *mm = vmf->vma->vm_mm;
284 struct userfaultfd_ctx *ctx;
285 struct userfaultfd_wait_queue uwq;
287 bool must_wait, return_to_userland;
290 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
292 ret = VM_FAULT_SIGBUS;
293 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
297 BUG_ON(ctx->mm != mm);
299 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
300 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
303 * If it's already released don't get it. This avoids to loop
304 * in __get_user_pages if userfaultfd_release waits on the
305 * caller of handle_userfault to release the mmap_sem.
307 if (unlikely(ACCESS_ONCE(ctx->released)))
311 * We don't do userfault handling for the final child pid update.
313 if (current->flags & PF_EXITING)
317 * Check that we can return VM_FAULT_RETRY.
319 * NOTE: it should become possible to return VM_FAULT_RETRY
320 * even if FAULT_FLAG_TRIED is set without leading to gup()
321 * -EBUSY failures, if the userfaultfd is to be extended for
322 * VM_UFFD_WP tracking and we intend to arm the userfault
323 * without first stopping userland access to the memory. For
324 * VM_UFFD_MISSING userfaults this is enough for now.
326 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
328 * Validate the invariant that nowait must allow retry
329 * to be sure not to return SIGBUS erroneously on
330 * nowait invocations.
332 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
333 #ifdef CONFIG_DEBUG_VM
334 if (printk_ratelimit()) {
336 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
345 * Handle nowait, not much to do other than tell it to retry
348 ret = VM_FAULT_RETRY;
349 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
352 /* take the reference before dropping the mmap_sem */
353 userfaultfd_ctx_get(ctx);
355 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
356 uwq.wq.private = current;
357 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
362 (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
363 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
364 blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
367 spin_lock(&ctx->fault_pending_wqh.lock);
369 * After the __add_wait_queue the uwq is visible to userland
370 * through poll/read().
372 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
374 * The smp_mb() after __set_current_state prevents the reads
375 * following the spin_unlock to happen before the list_add in
378 set_current_state(blocking_state);
379 spin_unlock(&ctx->fault_pending_wqh.lock);
381 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
383 up_read(&mm->mmap_sem);
385 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
386 (return_to_userland ? !signal_pending(current) :
387 !fatal_signal_pending(current)))) {
388 wake_up_poll(&ctx->fd_wqh, POLLIN);
390 ret |= VM_FAULT_MAJOR;
393 * False wakeups can orginate even from rwsem before
394 * up_read() however userfaults will wait either for a
395 * targeted wakeup on the specific uwq waitqueue from
396 * wake_userfault() or for signals or for uffd
399 while (!READ_ONCE(uwq.waken)) {
401 * This needs the full smp_store_mb()
402 * guarantee as the state write must be
403 * visible to other CPUs before reading
404 * uwq.waken from other CPUs.
406 set_current_state(blocking_state);
407 if (READ_ONCE(uwq.waken) ||
408 READ_ONCE(ctx->released) ||
409 (return_to_userland ? signal_pending(current) :
410 fatal_signal_pending(current)))
416 __set_current_state(TASK_RUNNING);
418 if (return_to_userland) {
419 if (signal_pending(current) &&
420 !fatal_signal_pending(current)) {
422 * If we got a SIGSTOP or SIGCONT and this is
423 * a normal userland page fault, just let
424 * userland return so the signal will be
425 * handled and gdb debugging works. The page
426 * fault code immediately after we return from
427 * this function is going to release the
428 * mmap_sem and it's not depending on it
429 * (unlike gup would if we were not to return
432 * If a fatal signal is pending we still take
433 * the streamlined VM_FAULT_RETRY failure path
434 * and there's no need to retake the mmap_sem
437 down_read(&mm->mmap_sem);
443 * Here we race with the list_del; list_add in
444 * userfaultfd_ctx_read(), however because we don't ever run
445 * list_del_init() to refile across the two lists, the prev
446 * and next pointers will never point to self. list_add also
447 * would never let any of the two pointers to point to
448 * self. So list_empty_careful won't risk to see both pointers
449 * pointing to self at any time during the list refile. The
450 * only case where list_del_init() is called is the full
451 * removal in the wake function and there we don't re-list_add
452 * and it's fine not to block on the spinlock. The uwq on this
453 * kernel stack can be released after the list_del_init.
455 if (!list_empty_careful(&uwq.wq.task_list)) {
456 spin_lock(&ctx->fault_pending_wqh.lock);
458 * No need of list_del_init(), the uwq on the stack
459 * will be freed shortly anyway.
461 list_del(&uwq.wq.task_list);
462 spin_unlock(&ctx->fault_pending_wqh.lock);
466 * ctx may go away after this if the userfault pseudo fd is
469 userfaultfd_ctx_put(ctx);
475 static int userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
476 struct userfaultfd_wait_queue *ewq)
481 init_waitqueue_entry(&ewq->wq, current);
483 spin_lock(&ctx->event_wqh.lock);
485 * After the __add_wait_queue the uwq is visible to userland
486 * through poll/read().
488 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
490 set_current_state(TASK_KILLABLE);
491 if (ewq->msg.event == 0)
493 if (ACCESS_ONCE(ctx->released) ||
494 fatal_signal_pending(current)) {
496 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
500 spin_unlock(&ctx->event_wqh.lock);
502 wake_up_poll(&ctx->fd_wqh, POLLIN);
505 spin_lock(&ctx->event_wqh.lock);
507 __set_current_state(TASK_RUNNING);
508 spin_unlock(&ctx->event_wqh.lock);
511 * ctx may go away after this if the userfault pseudo fd is
515 userfaultfd_ctx_put(ctx);
519 static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
520 struct userfaultfd_wait_queue *ewq)
523 wake_up_locked(&ctx->event_wqh);
524 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
527 int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
529 struct userfaultfd_ctx *ctx = NULL, *octx;
530 struct userfaultfd_fork_ctx *fctx;
532 octx = vma->vm_userfaultfd_ctx.ctx;
533 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
534 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
535 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
539 list_for_each_entry(fctx, fcs, list)
540 if (fctx->orig == octx) {
546 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
550 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
556 atomic_set(&ctx->refcount, 1);
557 ctx->flags = octx->flags;
558 ctx->state = UFFD_STATE_RUNNING;
559 ctx->features = octx->features;
560 ctx->released = false;
561 ctx->mm = vma->vm_mm;
562 atomic_inc(&ctx->mm->mm_count);
564 userfaultfd_ctx_get(octx);
567 list_add_tail(&fctx->list, fcs);
570 vma->vm_userfaultfd_ctx.ctx = ctx;
574 static int dup_fctx(struct userfaultfd_fork_ctx *fctx)
576 struct userfaultfd_ctx *ctx = fctx->orig;
577 struct userfaultfd_wait_queue ewq;
581 ewq.msg.event = UFFD_EVENT_FORK;
582 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
584 return userfaultfd_event_wait_completion(ctx, &ewq);
587 void dup_userfaultfd_complete(struct list_head *fcs)
590 struct userfaultfd_fork_ctx *fctx, *n;
592 list_for_each_entry_safe(fctx, n, fcs, list) {
594 ret = dup_fctx(fctx);
595 list_del(&fctx->list);
600 void mremap_userfaultfd_prep(struct vm_area_struct *vma,
601 struct vm_userfaultfd_ctx *vm_ctx)
603 struct userfaultfd_ctx *ctx;
605 ctx = vma->vm_userfaultfd_ctx.ctx;
606 if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
608 userfaultfd_ctx_get(ctx);
612 void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
613 unsigned long from, unsigned long to,
616 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
617 struct userfaultfd_wait_queue ewq;
622 if (to & ~PAGE_MASK) {
623 userfaultfd_ctx_put(ctx);
629 ewq.msg.event = UFFD_EVENT_REMAP;
630 ewq.msg.arg.remap.from = from;
631 ewq.msg.arg.remap.to = to;
632 ewq.msg.arg.remap.len = len;
634 userfaultfd_event_wait_completion(ctx, &ewq);
637 void madvise_userfault_dontneed(struct vm_area_struct *vma,
638 struct vm_area_struct **prev,
639 unsigned long start, unsigned long end)
641 struct mm_struct *mm = vma->vm_mm;
642 struct userfaultfd_ctx *ctx;
643 struct userfaultfd_wait_queue ewq;
645 ctx = vma->vm_userfaultfd_ctx.ctx;
646 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_MADVDONTNEED))
649 userfaultfd_ctx_get(ctx);
650 up_read(&mm->mmap_sem);
652 *prev = NULL; /* We wait for ACK w/o the mmap semaphore */
656 ewq.msg.event = UFFD_EVENT_MADVDONTNEED;
657 ewq.msg.arg.madv_dn.start = start;
658 ewq.msg.arg.madv_dn.end = end;
660 userfaultfd_event_wait_completion(ctx, &ewq);
662 down_read(&mm->mmap_sem);
665 static int userfaultfd_release(struct inode *inode, struct file *file)
667 struct userfaultfd_ctx *ctx = file->private_data;
668 struct mm_struct *mm = ctx->mm;
669 struct vm_area_struct *vma, *prev;
670 /* len == 0 means wake all */
671 struct userfaultfd_wake_range range = { .len = 0, };
672 unsigned long new_flags;
674 ACCESS_ONCE(ctx->released) = true;
676 if (!mmget_not_zero(mm))
680 * Flush page faults out of all CPUs. NOTE: all page faults
681 * must be retried without returning VM_FAULT_SIGBUS if
682 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
683 * changes while handle_userfault released the mmap_sem. So
684 * it's critical that released is set to true (above), before
685 * taking the mmap_sem for writing.
687 down_write(&mm->mmap_sem);
689 for (vma = mm->mmap; vma; vma = vma->vm_next) {
691 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
692 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
693 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
697 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
698 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
699 new_flags, vma->anon_vma,
700 vma->vm_file, vma->vm_pgoff,
707 vma->vm_flags = new_flags;
708 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
710 up_write(&mm->mmap_sem);
714 * After no new page faults can wait on this fault_*wqh, flush
715 * the last page faults that may have been already waiting on
718 spin_lock(&ctx->fault_pending_wqh.lock);
719 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
720 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
721 spin_unlock(&ctx->fault_pending_wqh.lock);
723 wake_up_poll(&ctx->fd_wqh, POLLHUP);
724 userfaultfd_ctx_put(ctx);
728 /* fault_pending_wqh.lock must be hold by the caller */
729 static inline struct userfaultfd_wait_queue *find_userfault_in(
730 wait_queue_head_t *wqh)
733 struct userfaultfd_wait_queue *uwq;
735 VM_BUG_ON(!spin_is_locked(&wqh->lock));
738 if (!waitqueue_active(wqh))
740 /* walk in reverse to provide FIFO behavior to read userfaults */
741 wq = list_last_entry(&wqh->task_list, typeof(*wq), task_list);
742 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
747 static inline struct userfaultfd_wait_queue *find_userfault(
748 struct userfaultfd_ctx *ctx)
750 return find_userfault_in(&ctx->fault_pending_wqh);
753 static inline struct userfaultfd_wait_queue *find_userfault_evt(
754 struct userfaultfd_ctx *ctx)
756 return find_userfault_in(&ctx->event_wqh);
759 static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
761 struct userfaultfd_ctx *ctx = file->private_data;
764 poll_wait(file, &ctx->fd_wqh, wait);
766 switch (ctx->state) {
767 case UFFD_STATE_WAIT_API:
769 case UFFD_STATE_RUNNING:
771 * poll() never guarantees that read won't block.
772 * userfaults can be waken before they're read().
774 if (unlikely(!(file->f_flags & O_NONBLOCK)))
777 * lockless access to see if there are pending faults
778 * __pollwait last action is the add_wait_queue but
779 * the spin_unlock would allow the waitqueue_active to
780 * pass above the actual list_add inside
781 * add_wait_queue critical section. So use a full
782 * memory barrier to serialize the list_add write of
783 * add_wait_queue() with the waitqueue_active read
788 if (waitqueue_active(&ctx->fault_pending_wqh))
790 else if (waitqueue_active(&ctx->event_wqh))
800 static const struct file_operations userfaultfd_fops;
802 static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
803 struct userfaultfd_ctx *new,
804 struct uffd_msg *msg)
808 unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
810 fd = get_unused_fd_flags(flags);
814 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
818 return PTR_ERR(file);
821 fd_install(fd, file);
822 msg->arg.reserved.reserved1 = 0;
823 msg->arg.fork.ufd = fd;
828 static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
829 struct uffd_msg *msg)
832 DECLARE_WAITQUEUE(wait, current);
833 struct userfaultfd_wait_queue *uwq;
835 * Handling fork event requires sleeping operations, so
836 * we drop the event_wqh lock, then do these ops, then
837 * lock it back and wake up the waiter. While the lock is
838 * dropped the ewq may go away so we keep track of it
841 LIST_HEAD(fork_event);
842 struct userfaultfd_ctx *fork_nctx = NULL;
844 /* always take the fd_wqh lock before the fault_pending_wqh lock */
845 spin_lock(&ctx->fd_wqh.lock);
846 __add_wait_queue(&ctx->fd_wqh, &wait);
848 set_current_state(TASK_INTERRUPTIBLE);
849 spin_lock(&ctx->fault_pending_wqh.lock);
850 uwq = find_userfault(ctx);
853 * Use a seqcount to repeat the lockless check
854 * in wake_userfault() to avoid missing
855 * wakeups because during the refile both
856 * waitqueue could become empty if this is the
859 write_seqcount_begin(&ctx->refile_seq);
862 * The fault_pending_wqh.lock prevents the uwq
863 * to disappear from under us.
865 * Refile this userfault from
866 * fault_pending_wqh to fault_wqh, it's not
867 * pending anymore after we read it.
869 * Use list_del() by hand (as
870 * userfaultfd_wake_function also uses
871 * list_del_init() by hand) to be sure nobody
872 * changes __remove_wait_queue() to use
873 * list_del_init() in turn breaking the
874 * !list_empty_careful() check in
875 * handle_userfault(). The uwq->wq.task_list
876 * must never be empty at any time during the
877 * refile, or the waitqueue could disappear
878 * from under us. The "wait_queue_head_t"
879 * parameter of __remove_wait_queue() is unused
882 list_del(&uwq->wq.task_list);
883 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
885 write_seqcount_end(&ctx->refile_seq);
887 /* careful to always initialize msg if ret == 0 */
889 spin_unlock(&ctx->fault_pending_wqh.lock);
893 spin_unlock(&ctx->fault_pending_wqh.lock);
895 spin_lock(&ctx->event_wqh.lock);
896 uwq = find_userfault_evt(ctx);
900 if (uwq->msg.event == UFFD_EVENT_FORK) {
901 fork_nctx = (struct userfaultfd_ctx *)
903 uwq->msg.arg.reserved.reserved1;
904 list_move(&uwq->wq.task_list, &fork_event);
905 spin_unlock(&ctx->event_wqh.lock);
910 userfaultfd_event_complete(ctx, uwq);
911 spin_unlock(&ctx->event_wqh.lock);
915 spin_unlock(&ctx->event_wqh.lock);
917 if (signal_pending(current)) {
925 spin_unlock(&ctx->fd_wqh.lock);
927 spin_lock(&ctx->fd_wqh.lock);
929 __remove_wait_queue(&ctx->fd_wqh, &wait);
930 __set_current_state(TASK_RUNNING);
931 spin_unlock(&ctx->fd_wqh.lock);
933 if (!ret && msg->event == UFFD_EVENT_FORK) {
934 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
937 spin_lock(&ctx->event_wqh.lock);
938 if (!list_empty(&fork_event)) {
939 uwq = list_first_entry(&fork_event,
942 list_del(&uwq->wq.task_list);
943 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
944 userfaultfd_event_complete(ctx, uwq);
946 spin_unlock(&ctx->event_wqh.lock);
953 static ssize_t userfaultfd_read(struct file *file, char __user *buf,
954 size_t count, loff_t *ppos)
956 struct userfaultfd_ctx *ctx = file->private_data;
957 ssize_t _ret, ret = 0;
959 int no_wait = file->f_flags & O_NONBLOCK;
961 if (ctx->state == UFFD_STATE_WAIT_API)
965 if (count < sizeof(msg))
966 return ret ? ret : -EINVAL;
967 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
969 return ret ? ret : _ret;
970 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
971 return ret ? ret : -EFAULT;
974 count -= sizeof(msg);
976 * Allow to read more than one fault at time but only
977 * block if waiting for the very first one.
979 no_wait = O_NONBLOCK;
983 static void __wake_userfault(struct userfaultfd_ctx *ctx,
984 struct userfaultfd_wake_range *range)
986 unsigned long start, end;
988 start = range->start;
989 end = range->start + range->len;
991 spin_lock(&ctx->fault_pending_wqh.lock);
992 /* wake all in the range and autoremove */
993 if (waitqueue_active(&ctx->fault_pending_wqh))
994 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
996 if (waitqueue_active(&ctx->fault_wqh))
997 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
998 spin_unlock(&ctx->fault_pending_wqh.lock);
1001 static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1002 struct userfaultfd_wake_range *range)
1008 * To be sure waitqueue_active() is not reordered by the CPU
1009 * before the pagetable update, use an explicit SMP memory
1010 * barrier here. PT lock release or up_read(mmap_sem) still
1011 * have release semantics that can allow the
1012 * waitqueue_active() to be reordered before the pte update.
1017 * Use waitqueue_active because it's very frequent to
1018 * change the address space atomically even if there are no
1019 * userfaults yet. So we take the spinlock only when we're
1020 * sure we've userfaults to wake.
1023 seq = read_seqcount_begin(&ctx->refile_seq);
1024 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1025 waitqueue_active(&ctx->fault_wqh);
1027 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1029 __wake_userfault(ctx, range);
1032 static __always_inline int validate_range(struct mm_struct *mm,
1033 __u64 start, __u64 len)
1035 __u64 task_size = mm->task_size;
1037 if (start & ~PAGE_MASK)
1039 if (len & ~PAGE_MASK)
1043 if (start < mmap_min_addr)
1045 if (start >= task_size)
1047 if (len > task_size - start)
1052 static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1055 struct mm_struct *mm = ctx->mm;
1056 struct vm_area_struct *vma, *prev, *cur;
1058 struct uffdio_register uffdio_register;
1059 struct uffdio_register __user *user_uffdio_register;
1060 unsigned long vm_flags, new_flags;
1063 unsigned long start, end, vma_end;
1065 user_uffdio_register = (struct uffdio_register __user *) arg;
1068 if (copy_from_user(&uffdio_register, user_uffdio_register,
1069 sizeof(uffdio_register)-sizeof(__u64)))
1073 if (!uffdio_register.mode)
1075 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1076 UFFDIO_REGISTER_MODE_WP))
1079 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1080 vm_flags |= VM_UFFD_MISSING;
1081 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1082 vm_flags |= VM_UFFD_WP;
1084 * FIXME: remove the below error constraint by
1085 * implementing the wprotect tracking mode.
1091 ret = validate_range(mm, uffdio_register.range.start,
1092 uffdio_register.range.len);
1096 start = uffdio_register.range.start;
1097 end = start + uffdio_register.range.len;
1100 if (!mmget_not_zero(mm))
1103 down_write(&mm->mmap_sem);
1104 vma = find_vma_prev(mm, start, &prev);
1108 /* check that there's at least one vma in the range */
1110 if (vma->vm_start >= end)
1114 * If the first vma contains huge pages, make sure start address
1115 * is aligned to huge page size.
1117 if (is_vm_hugetlb_page(vma)) {
1118 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1120 if (start & (vma_hpagesize - 1))
1125 * Search for not compatible vmas.
1127 * FIXME: this shall be relaxed later so that it doesn't fail
1128 * on tmpfs backed vmas (in addition to the current allowance
1129 * on anonymous vmas).
1133 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1136 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1137 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1139 /* check not compatible vmas */
1141 if (!vma_is_anonymous(cur) && !is_vm_hugetlb_page(cur))
1144 * If this vma contains ending address, and huge pages
1147 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1148 end > cur->vm_start) {
1149 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1153 if (end & (vma_hpagesize - 1))
1158 * Check that this vma isn't already owned by a
1159 * different userfaultfd. We can't allow more than one
1160 * userfaultfd to own a single vma simultaneously or we
1161 * wouldn't know which one to deliver the userfaults to.
1164 if (cur->vm_userfaultfd_ctx.ctx &&
1165 cur->vm_userfaultfd_ctx.ctx != ctx)
1169 * Note vmas containing huge pages
1171 if (is_vm_hugetlb_page(cur))
1178 if (vma->vm_start < start)
1185 BUG_ON(!vma_is_anonymous(vma) && !is_vm_hugetlb_page(vma));
1186 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1187 vma->vm_userfaultfd_ctx.ctx != ctx);
1190 * Nothing to do: this vma is already registered into this
1191 * userfaultfd and with the right tracking mode too.
1193 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1194 (vma->vm_flags & vm_flags) == vm_flags)
1197 if (vma->vm_start > start)
1198 start = vma->vm_start;
1199 vma_end = min(end, vma->vm_end);
1201 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
1202 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1203 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1205 ((struct vm_userfaultfd_ctx){ ctx }));
1210 if (vma->vm_start < start) {
1211 ret = split_vma(mm, vma, start, 1);
1215 if (vma->vm_end > end) {
1216 ret = split_vma(mm, vma, end, 0);
1222 * In the vma_merge() successful mprotect-like case 8:
1223 * the next vma was merged into the current one and
1224 * the current one has not been updated yet.
1226 vma->vm_flags = new_flags;
1227 vma->vm_userfaultfd_ctx.ctx = ctx;
1231 start = vma->vm_end;
1233 } while (vma && vma->vm_start < end);
1235 up_write(&mm->mmap_sem);
1239 * Now that we scanned all vmas we can already tell
1240 * userland which ioctls methods are guaranteed to
1241 * succeed on this range.
1243 if (put_user(huge_pages ? UFFD_API_RANGE_IOCTLS_HPAGE :
1244 UFFD_API_RANGE_IOCTLS,
1245 &user_uffdio_register->ioctls))
1252 static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1255 struct mm_struct *mm = ctx->mm;
1256 struct vm_area_struct *vma, *prev, *cur;
1258 struct uffdio_range uffdio_unregister;
1259 unsigned long new_flags;
1261 unsigned long start, end, vma_end;
1262 const void __user *buf = (void __user *)arg;
1265 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1268 ret = validate_range(mm, uffdio_unregister.start,
1269 uffdio_unregister.len);
1273 start = uffdio_unregister.start;
1274 end = start + uffdio_unregister.len;
1277 if (!mmget_not_zero(mm))
1280 down_write(&mm->mmap_sem);
1281 vma = find_vma_prev(mm, start, &prev);
1285 /* check that there's at least one vma in the range */
1287 if (vma->vm_start >= end)
1291 * If the first vma contains huge pages, make sure start address
1292 * is aligned to huge page size.
1294 if (is_vm_hugetlb_page(vma)) {
1295 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1297 if (start & (vma_hpagesize - 1))
1302 * Search for not compatible vmas.
1304 * FIXME: this shall be relaxed later so that it doesn't fail
1305 * on tmpfs backed vmas (in addition to the current allowance
1306 * on anonymous vmas).
1310 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1313 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1314 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1317 * Check not compatible vmas, not strictly required
1318 * here as not compatible vmas cannot have an
1319 * userfaultfd_ctx registered on them, but this
1320 * provides for more strict behavior to notice
1321 * unregistration errors.
1323 if (!vma_is_anonymous(cur) && !is_vm_hugetlb_page(cur))
1330 if (vma->vm_start < start)
1337 BUG_ON(!vma_is_anonymous(vma) && !is_vm_hugetlb_page(vma));
1340 * Nothing to do: this vma is already registered into this
1341 * userfaultfd and with the right tracking mode too.
1343 if (!vma->vm_userfaultfd_ctx.ctx)
1346 if (vma->vm_start > start)
1347 start = vma->vm_start;
1348 vma_end = min(end, vma->vm_end);
1350 if (userfaultfd_missing(vma)) {
1352 * Wake any concurrent pending userfault while
1353 * we unregister, so they will not hang
1354 * permanently and it avoids userland to call
1355 * UFFDIO_WAKE explicitly.
1357 struct userfaultfd_wake_range range;
1358 range.start = start;
1359 range.len = vma_end - start;
1360 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1363 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1364 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1365 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1372 if (vma->vm_start < start) {
1373 ret = split_vma(mm, vma, start, 1);
1377 if (vma->vm_end > end) {
1378 ret = split_vma(mm, vma, end, 0);
1384 * In the vma_merge() successful mprotect-like case 8:
1385 * the next vma was merged into the current one and
1386 * the current one has not been updated yet.
1388 vma->vm_flags = new_flags;
1389 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1393 start = vma->vm_end;
1395 } while (vma && vma->vm_start < end);
1397 up_write(&mm->mmap_sem);
1404 * userfaultfd_wake may be used in combination with the
1405 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
1407 static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1411 struct uffdio_range uffdio_wake;
1412 struct userfaultfd_wake_range range;
1413 const void __user *buf = (void __user *)arg;
1416 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1419 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1423 range.start = uffdio_wake.start;
1424 range.len = uffdio_wake.len;
1427 * len == 0 means wake all and we don't want to wake all here,
1428 * so check it again to be sure.
1430 VM_BUG_ON(!range.len);
1432 wake_userfault(ctx, &range);
1439 static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1443 struct uffdio_copy uffdio_copy;
1444 struct uffdio_copy __user *user_uffdio_copy;
1445 struct userfaultfd_wake_range range;
1447 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1450 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1451 /* don't copy "copy" last field */
1452 sizeof(uffdio_copy)-sizeof(__s64)))
1455 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1459 * double check for wraparound just in case. copy_from_user()
1460 * will later check uffdio_copy.src + uffdio_copy.len to fit
1461 * in the userland range.
1464 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1466 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1468 if (mmget_not_zero(ctx->mm)) {
1469 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1473 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1478 /* len == 0 would wake all */
1480 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1481 range.start = uffdio_copy.dst;
1482 wake_userfault(ctx, &range);
1484 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1489 static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1493 struct uffdio_zeropage uffdio_zeropage;
1494 struct uffdio_zeropage __user *user_uffdio_zeropage;
1495 struct userfaultfd_wake_range range;
1497 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1500 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1501 /* don't copy "zeropage" last field */
1502 sizeof(uffdio_zeropage)-sizeof(__s64)))
1505 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1506 uffdio_zeropage.range.len);
1510 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1513 if (mmget_not_zero(ctx->mm)) {
1514 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1515 uffdio_zeropage.range.len);
1518 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1522 /* len == 0 would wake all */
1525 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1526 range.start = uffdio_zeropage.range.start;
1527 wake_userfault(ctx, &range);
1529 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1534 static inline unsigned int uffd_ctx_features(__u64 user_features)
1537 * For the current set of features the bits just coincide
1539 return (unsigned int)user_features;
1543 * userland asks for a certain API version and we return which bits
1544 * and ioctl commands are implemented in this kernel for such API
1545 * version or -EINVAL if unknown.
1547 static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1550 struct uffdio_api uffdio_api;
1551 void __user *buf = (void __user *)arg;
1556 if (ctx->state != UFFD_STATE_WAIT_API)
1559 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
1561 features = uffdio_api.features;
1562 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
1563 memset(&uffdio_api, 0, sizeof(uffdio_api));
1564 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1569 /* report all available features and ioctls to userland */
1570 uffdio_api.features = UFFD_API_FEATURES;
1571 uffdio_api.ioctls = UFFD_API_IOCTLS;
1573 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1575 ctx->state = UFFD_STATE_RUNNING;
1576 /* only enable the requested features for this uffd context */
1577 ctx->features = uffd_ctx_features(features);
1583 static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1587 struct userfaultfd_ctx *ctx = file->private_data;
1589 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1594 ret = userfaultfd_api(ctx, arg);
1596 case UFFDIO_REGISTER:
1597 ret = userfaultfd_register(ctx, arg);
1599 case UFFDIO_UNREGISTER:
1600 ret = userfaultfd_unregister(ctx, arg);
1603 ret = userfaultfd_wake(ctx, arg);
1606 ret = userfaultfd_copy(ctx, arg);
1608 case UFFDIO_ZEROPAGE:
1609 ret = userfaultfd_zeropage(ctx, arg);
1615 #ifdef CONFIG_PROC_FS
1616 static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1618 struct userfaultfd_ctx *ctx = f->private_data;
1620 struct userfaultfd_wait_queue *uwq;
1621 unsigned long pending = 0, total = 0;
1623 spin_lock(&ctx->fault_pending_wqh.lock);
1624 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
1625 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1629 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1630 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1633 spin_unlock(&ctx->fault_pending_wqh.lock);
1636 * If more protocols will be added, there will be all shown
1637 * separated by a space. Like this:
1638 * protocols: aa:... bb:...
1640 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
1641 pending, total, UFFD_API, UFFD_API_FEATURES,
1642 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1646 static const struct file_operations userfaultfd_fops = {
1647 #ifdef CONFIG_PROC_FS
1648 .show_fdinfo = userfaultfd_show_fdinfo,
1650 .release = userfaultfd_release,
1651 .poll = userfaultfd_poll,
1652 .read = userfaultfd_read,
1653 .unlocked_ioctl = userfaultfd_ioctl,
1654 .compat_ioctl = userfaultfd_ioctl,
1655 .llseek = noop_llseek,
1658 static void init_once_userfaultfd_ctx(void *mem)
1660 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1662 init_waitqueue_head(&ctx->fault_pending_wqh);
1663 init_waitqueue_head(&ctx->fault_wqh);
1664 init_waitqueue_head(&ctx->event_wqh);
1665 init_waitqueue_head(&ctx->fd_wqh);
1666 seqcount_init(&ctx->refile_seq);
1670 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1671 * @flags: Flags for the userfaultfd file.
1673 * This function creates an userfaultfd file pointer, w/out installing
1674 * it into the fd table. This is useful when the userfaultfd file is
1675 * used during the initialization of data structures that require
1676 * extra setup after the userfaultfd creation. So the userfaultfd
1677 * creation is split into the file pointer creation phase, and the
1678 * file descriptor installation phase. In this way races with
1679 * userspace closing the newly installed file descriptor can be
1680 * avoided. Returns an userfaultfd file pointer, or a proper error
1683 static struct file *userfaultfd_file_create(int flags)
1686 struct userfaultfd_ctx *ctx;
1688 BUG_ON(!current->mm);
1690 /* Check the UFFD_* constants for consistency. */
1691 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1692 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1694 file = ERR_PTR(-EINVAL);
1695 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1698 file = ERR_PTR(-ENOMEM);
1699 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
1703 atomic_set(&ctx->refcount, 1);
1706 ctx->state = UFFD_STATE_WAIT_API;
1707 ctx->released = false;
1708 ctx->mm = current->mm;
1709 /* prevent the mm struct to be freed */
1710 atomic_inc(&ctx->mm->mm_count);
1712 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1713 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1716 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
1722 SYSCALL_DEFINE1(userfaultfd, int, flags)
1727 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1732 file = userfaultfd_file_create(flags);
1734 error = PTR_ERR(file);
1735 goto err_put_unused_fd;
1737 fd_install(fd, file);
1747 static int __init userfaultfd_init(void)
1749 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1750 sizeof(struct userfaultfd_ctx),
1752 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1753 init_once_userfaultfd_ctx);
1756 __initcall(userfaultfd_init);