userfaultfd: non-cooperative: fix fork fctx->new memleak
[linux-2.6-block.git] / fs / userfaultfd.c
CommitLineData
86039bd3
AA
1/*
2 * fs/userfaultfd.c
3 *
4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
5 * Copyright (C) 2008-2009 Red Hat, Inc.
6 * Copyright (C) 2015 Red Hat, Inc.
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2. See
9 * the COPYING file in the top-level directory.
10 *
11 * Some part derived from fs/eventfd.c (anon inode setup) and
12 * mm/ksm.c (mm hashing).
13 */
14
9cd75c3c 15#include <linux/list.h>
86039bd3 16#include <linux/hashtable.h>
174cd4b1 17#include <linux/sched/signal.h>
6e84f315 18#include <linux/sched/mm.h>
86039bd3
AA
19#include <linux/mm.h>
20#include <linux/poll.h>
21#include <linux/slab.h>
22#include <linux/seq_file.h>
23#include <linux/file.h>
24#include <linux/bug.h>
25#include <linux/anon_inodes.h>
26#include <linux/syscalls.h>
27#include <linux/userfaultfd_k.h>
28#include <linux/mempolicy.h>
29#include <linux/ioctl.h>
30#include <linux/security.h>
cab350af 31#include <linux/hugetlb.h>
86039bd3 32
3004ec9c
AA
33static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
34
86039bd3
AA
35enum userfaultfd_state {
36 UFFD_STATE_WAIT_API,
37 UFFD_STATE_RUNNING,
38};
39
3004ec9c
AA
40/*
41 * Start with fault_pending_wqh and fault_wqh so they're more likely
42 * to be in the same cacheline.
43 */
86039bd3 44struct userfaultfd_ctx {
15b726ef
AA
45 /* waitqueue head for the pending (i.e. not read) userfaults */
46 wait_queue_head_t fault_pending_wqh;
47 /* waitqueue head for the userfaults */
86039bd3
AA
48 wait_queue_head_t fault_wqh;
49 /* waitqueue head for the pseudo fd to wakeup poll/read */
50 wait_queue_head_t fd_wqh;
9cd75c3c
PE
51 /* waitqueue head for events */
52 wait_queue_head_t event_wqh;
2c5b7e1b
AA
53 /* a refile sequence protected by fault_pending_wqh lock */
54 struct seqcount refile_seq;
3004ec9c
AA
55 /* pseudo fd refcounting */
56 atomic_t refcount;
86039bd3
AA
57 /* userfaultfd syscall flags */
58 unsigned int flags;
9cd75c3c
PE
59 /* features requested from the userspace */
60 unsigned int features;
86039bd3
AA
61 /* state machine */
62 enum userfaultfd_state state;
63 /* released */
64 bool released;
65 /* mm with one ore more vmas attached to this userfaultfd_ctx */
66 struct mm_struct *mm;
67};
68
893e26e6
PE
69struct userfaultfd_fork_ctx {
70 struct userfaultfd_ctx *orig;
71 struct userfaultfd_ctx *new;
72 struct list_head list;
73};
74
897ab3e0
MR
75struct userfaultfd_unmap_ctx {
76 struct userfaultfd_ctx *ctx;
77 unsigned long start;
78 unsigned long end;
79 struct list_head list;
80};
81
86039bd3 82struct userfaultfd_wait_queue {
a9b85f94 83 struct uffd_msg msg;
86039bd3 84 wait_queue_t wq;
86039bd3 85 struct userfaultfd_ctx *ctx;
15a77c6f 86 bool waken;
86039bd3
AA
87};
88
89struct userfaultfd_wake_range {
90 unsigned long start;
91 unsigned long len;
92};
93
94static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
95 int wake_flags, void *key)
96{
97 struct userfaultfd_wake_range *range = key;
98 int ret;
99 struct userfaultfd_wait_queue *uwq;
100 unsigned long start, len;
101
102 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
103 ret = 0;
86039bd3
AA
104 /* len == 0 means wake all */
105 start = range->start;
106 len = range->len;
a9b85f94
AA
107 if (len && (start > uwq->msg.arg.pagefault.address ||
108 start + len <= uwq->msg.arg.pagefault.address))
86039bd3 109 goto out;
15a77c6f
AA
110 WRITE_ONCE(uwq->waken, true);
111 /*
112 * The implicit smp_mb__before_spinlock in try_to_wake_up()
113 * renders uwq->waken visible to other CPUs before the task is
114 * waken.
115 */
86039bd3
AA
116 ret = wake_up_state(wq->private, mode);
117 if (ret)
118 /*
119 * Wake only once, autoremove behavior.
120 *
121 * After the effect of list_del_init is visible to the
122 * other CPUs, the waitqueue may disappear from under
123 * us, see the !list_empty_careful() in
124 * handle_userfault(). try_to_wake_up() has an
125 * implicit smp_mb__before_spinlock, and the
126 * wq->private is read before calling the extern
127 * function "wake_up_state" (which in turns calls
128 * try_to_wake_up). While the spin_lock;spin_unlock;
129 * wouldn't be enough, the smp_mb__before_spinlock is
130 * enough to avoid an explicit smp_mb() here.
131 */
132 list_del_init(&wq->task_list);
133out:
134 return ret;
135}
136
137/**
138 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
139 * context.
140 * @ctx: [in] Pointer to the userfaultfd context.
141 *
142 * Returns: In case of success, returns not zero.
143 */
144static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
145{
146 if (!atomic_inc_not_zero(&ctx->refcount))
147 BUG();
148}
149
150/**
151 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
152 * context.
153 * @ctx: [in] Pointer to userfaultfd context.
154 *
155 * The userfaultfd context reference must have been previously acquired either
156 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
157 */
158static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
159{
160 if (atomic_dec_and_test(&ctx->refcount)) {
161 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
162 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
163 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
164 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
9cd75c3c
PE
165 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
166 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
86039bd3
AA
167 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
168 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
d2005e3f 169 mmdrop(ctx->mm);
3004ec9c 170 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
86039bd3
AA
171 }
172}
173
a9b85f94 174static inline void msg_init(struct uffd_msg *msg)
86039bd3 175{
a9b85f94
AA
176 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
177 /*
178 * Must use memset to zero out the paddings or kernel data is
179 * leaked to userland.
180 */
181 memset(msg, 0, sizeof(struct uffd_msg));
182}
183
184static inline struct uffd_msg userfault_msg(unsigned long address,
185 unsigned int flags,
186 unsigned long reason)
187{
188 struct uffd_msg msg;
189 msg_init(&msg);
190 msg.event = UFFD_EVENT_PAGEFAULT;
191 msg.arg.pagefault.address = address;
86039bd3
AA
192 if (flags & FAULT_FLAG_WRITE)
193 /*
a4605a61 194 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
a9b85f94
AA
195 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
196 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
197 * was a read fault, otherwise if set it means it's
198 * a write fault.
86039bd3 199 */
a9b85f94 200 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
86039bd3
AA
201 if (reason & VM_UFFD_WP)
202 /*
a9b85f94
AA
203 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
204 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
205 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
206 * a missing fault, otherwise if set it means it's a
207 * write protect fault.
86039bd3 208 */
a9b85f94
AA
209 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
210 return msg;
86039bd3
AA
211}
212
369cd212
MK
213#ifdef CONFIG_HUGETLB_PAGE
214/*
215 * Same functionality as userfaultfd_must_wait below with modifications for
216 * hugepmd ranges.
217 */
218static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
219 unsigned long address,
220 unsigned long flags,
221 unsigned long reason)
222{
223 struct mm_struct *mm = ctx->mm;
224 pte_t *pte;
225 bool ret = true;
226
227 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
228
229 pte = huge_pte_offset(mm, address);
230 if (!pte)
231 goto out;
232
233 ret = false;
234
235 /*
236 * Lockless access: we're in a wait_event so it's ok if it
237 * changes under us.
238 */
239 if (huge_pte_none(*pte))
240 ret = true;
241 if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
242 ret = true;
243out:
244 return ret;
245}
246#else
247static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
248 unsigned long address,
249 unsigned long flags,
250 unsigned long reason)
251{
252 return false; /* should never get here */
253}
254#endif /* CONFIG_HUGETLB_PAGE */
255
8d2afd96
AA
256/*
257 * Verify the pagetables are still not ok after having reigstered into
258 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
259 * userfault that has already been resolved, if userfaultfd_read and
260 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
261 * threads.
262 */
263static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
264 unsigned long address,
265 unsigned long flags,
266 unsigned long reason)
267{
268 struct mm_struct *mm = ctx->mm;
269 pgd_t *pgd;
270 pud_t *pud;
271 pmd_t *pmd, _pmd;
272 pte_t *pte;
273 bool ret = true;
274
275 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
276
277 pgd = pgd_offset(mm, address);
278 if (!pgd_present(*pgd))
279 goto out;
280 pud = pud_offset(pgd, address);
281 if (!pud_present(*pud))
282 goto out;
283 pmd = pmd_offset(pud, address);
284 /*
285 * READ_ONCE must function as a barrier with narrower scope
286 * and it must be equivalent to:
287 * _pmd = *pmd; barrier();
288 *
289 * This is to deal with the instability (as in
290 * pmd_trans_unstable) of the pmd.
291 */
292 _pmd = READ_ONCE(*pmd);
293 if (!pmd_present(_pmd))
294 goto out;
295
296 ret = false;
297 if (pmd_trans_huge(_pmd))
298 goto out;
299
300 /*
301 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
302 * and use the standard pte_offset_map() instead of parsing _pmd.
303 */
304 pte = pte_offset_map(pmd, address);
305 /*
306 * Lockless access: we're in a wait_event so it's ok if it
307 * changes under us.
308 */
309 if (pte_none(*pte))
310 ret = true;
311 pte_unmap(pte);
312
313out:
314 return ret;
315}
316
86039bd3
AA
317/*
318 * The locking rules involved in returning VM_FAULT_RETRY depending on
319 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
320 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
321 * recommendation in __lock_page_or_retry is not an understatement.
322 *
323 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
324 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
325 * not set.
326 *
327 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
328 * set, VM_FAULT_RETRY can still be returned if and only if there are
329 * fatal_signal_pending()s, and the mmap_sem must be released before
330 * returning it.
331 */
82b0f8c3 332int handle_userfault(struct vm_fault *vmf, unsigned long reason)
86039bd3 333{
82b0f8c3 334 struct mm_struct *mm = vmf->vma->vm_mm;
86039bd3
AA
335 struct userfaultfd_ctx *ctx;
336 struct userfaultfd_wait_queue uwq;
ba85c702 337 int ret;
dfa37dc3 338 bool must_wait, return_to_userland;
15a77c6f 339 long blocking_state;
86039bd3
AA
340
341 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
342
ba85c702 343 ret = VM_FAULT_SIGBUS;
82b0f8c3 344 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
86039bd3 345 if (!ctx)
ba85c702 346 goto out;
86039bd3
AA
347
348 BUG_ON(ctx->mm != mm);
349
350 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
351 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
352
353 /*
354 * If it's already released don't get it. This avoids to loop
355 * in __get_user_pages if userfaultfd_release waits on the
356 * caller of handle_userfault to release the mmap_sem.
357 */
358 if (unlikely(ACCESS_ONCE(ctx->released)))
ba85c702 359 goto out;
86039bd3 360
39680f50
LT
361 /*
362 * We don't do userfault handling for the final child pid update.
363 */
364 if (current->flags & PF_EXITING)
365 goto out;
366
86039bd3
AA
367 /*
368 * Check that we can return VM_FAULT_RETRY.
369 *
370 * NOTE: it should become possible to return VM_FAULT_RETRY
371 * even if FAULT_FLAG_TRIED is set without leading to gup()
372 * -EBUSY failures, if the userfaultfd is to be extended for
373 * VM_UFFD_WP tracking and we intend to arm the userfault
374 * without first stopping userland access to the memory. For
375 * VM_UFFD_MISSING userfaults this is enough for now.
376 */
82b0f8c3 377 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
86039bd3
AA
378 /*
379 * Validate the invariant that nowait must allow retry
380 * to be sure not to return SIGBUS erroneously on
381 * nowait invocations.
382 */
82b0f8c3 383 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
86039bd3
AA
384#ifdef CONFIG_DEBUG_VM
385 if (printk_ratelimit()) {
386 printk(KERN_WARNING
82b0f8c3
JK
387 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
388 vmf->flags);
86039bd3
AA
389 dump_stack();
390 }
391#endif
ba85c702 392 goto out;
86039bd3
AA
393 }
394
395 /*
396 * Handle nowait, not much to do other than tell it to retry
397 * and wait.
398 */
ba85c702 399 ret = VM_FAULT_RETRY;
82b0f8c3 400 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
ba85c702 401 goto out;
86039bd3
AA
402
403 /* take the reference before dropping the mmap_sem */
404 userfaultfd_ctx_get(ctx);
405
86039bd3
AA
406 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
407 uwq.wq.private = current;
82b0f8c3 408 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
86039bd3 409 uwq.ctx = ctx;
15a77c6f 410 uwq.waken = false;
86039bd3 411
bae473a4 412 return_to_userland =
82b0f8c3 413 (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
dfa37dc3 414 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
15a77c6f
AA
415 blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
416 TASK_KILLABLE;
dfa37dc3 417
15b726ef 418 spin_lock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
419 /*
420 * After the __add_wait_queue the uwq is visible to userland
421 * through poll/read().
422 */
15b726ef
AA
423 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
424 /*
425 * The smp_mb() after __set_current_state prevents the reads
426 * following the spin_unlock to happen before the list_add in
427 * __add_wait_queue.
428 */
15a77c6f 429 set_current_state(blocking_state);
15b726ef 430 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3 431
369cd212
MK
432 if (!is_vm_hugetlb_page(vmf->vma))
433 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
434 reason);
435 else
436 must_wait = userfaultfd_huge_must_wait(ctx, vmf->address,
437 vmf->flags, reason);
8d2afd96
AA
438 up_read(&mm->mmap_sem);
439
440 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
dfa37dc3
AA
441 (return_to_userland ? !signal_pending(current) :
442 !fatal_signal_pending(current)))) {
86039bd3
AA
443 wake_up_poll(&ctx->fd_wqh, POLLIN);
444 schedule();
ba85c702 445 ret |= VM_FAULT_MAJOR;
15a77c6f
AA
446
447 /*
448 * False wakeups can orginate even from rwsem before
449 * up_read() however userfaults will wait either for a
450 * targeted wakeup on the specific uwq waitqueue from
451 * wake_userfault() or for signals or for uffd
452 * release.
453 */
454 while (!READ_ONCE(uwq.waken)) {
455 /*
456 * This needs the full smp_store_mb()
457 * guarantee as the state write must be
458 * visible to other CPUs before reading
459 * uwq.waken from other CPUs.
460 */
461 set_current_state(blocking_state);
462 if (READ_ONCE(uwq.waken) ||
463 READ_ONCE(ctx->released) ||
464 (return_to_userland ? signal_pending(current) :
465 fatal_signal_pending(current)))
466 break;
467 schedule();
468 }
ba85c702 469 }
86039bd3 470
ba85c702 471 __set_current_state(TASK_RUNNING);
15b726ef 472
dfa37dc3
AA
473 if (return_to_userland) {
474 if (signal_pending(current) &&
475 !fatal_signal_pending(current)) {
476 /*
477 * If we got a SIGSTOP or SIGCONT and this is
478 * a normal userland page fault, just let
479 * userland return so the signal will be
480 * handled and gdb debugging works. The page
481 * fault code immediately after we return from
482 * this function is going to release the
483 * mmap_sem and it's not depending on it
484 * (unlike gup would if we were not to return
485 * VM_FAULT_RETRY).
486 *
487 * If a fatal signal is pending we still take
488 * the streamlined VM_FAULT_RETRY failure path
489 * and there's no need to retake the mmap_sem
490 * in such case.
491 */
492 down_read(&mm->mmap_sem);
6bbc4a41 493 ret = VM_FAULT_NOPAGE;
dfa37dc3
AA
494 }
495 }
496
15b726ef
AA
497 /*
498 * Here we race with the list_del; list_add in
499 * userfaultfd_ctx_read(), however because we don't ever run
500 * list_del_init() to refile across the two lists, the prev
501 * and next pointers will never point to self. list_add also
502 * would never let any of the two pointers to point to
503 * self. So list_empty_careful won't risk to see both pointers
504 * pointing to self at any time during the list refile. The
505 * only case where list_del_init() is called is the full
506 * removal in the wake function and there we don't re-list_add
507 * and it's fine not to block on the spinlock. The uwq on this
508 * kernel stack can be released after the list_del_init.
509 */
ba85c702 510 if (!list_empty_careful(&uwq.wq.task_list)) {
15b726ef
AA
511 spin_lock(&ctx->fault_pending_wqh.lock);
512 /*
513 * No need of list_del_init(), the uwq on the stack
514 * will be freed shortly anyway.
515 */
516 list_del(&uwq.wq.task_list);
517 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3 518 }
86039bd3
AA
519
520 /*
521 * ctx may go away after this if the userfault pseudo fd is
522 * already released.
523 */
524 userfaultfd_ctx_put(ctx);
525
ba85c702
AA
526out:
527 return ret;
86039bd3
AA
528}
529
8c9e7bb7
AA
530static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
531 struct userfaultfd_wait_queue *ewq)
9cd75c3c 532{
9a69a829
AA
533 if (WARN_ON_ONCE(current->flags & PF_EXITING))
534 goto out;
9cd75c3c
PE
535
536 ewq->ctx = ctx;
537 init_waitqueue_entry(&ewq->wq, current);
538
539 spin_lock(&ctx->event_wqh.lock);
540 /*
541 * After the __add_wait_queue the uwq is visible to userland
542 * through poll/read().
543 */
544 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
545 for (;;) {
546 set_current_state(TASK_KILLABLE);
547 if (ewq->msg.event == 0)
548 break;
549 if (ACCESS_ONCE(ctx->released) ||
550 fatal_signal_pending(current)) {
9cd75c3c 551 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
7eb76d45
MR
552 if (ewq->msg.event == UFFD_EVENT_FORK) {
553 struct userfaultfd_ctx *new;
554
555 new = (struct userfaultfd_ctx *)
556 (unsigned long)
557 ewq->msg.arg.reserved.reserved1;
558
559 userfaultfd_ctx_put(new);
560 }
9cd75c3c
PE
561 break;
562 }
563
564 spin_unlock(&ctx->event_wqh.lock);
565
566 wake_up_poll(&ctx->fd_wqh, POLLIN);
567 schedule();
568
569 spin_lock(&ctx->event_wqh.lock);
570 }
571 __set_current_state(TASK_RUNNING);
572 spin_unlock(&ctx->event_wqh.lock);
573
574 /*
575 * ctx may go away after this if the userfault pseudo fd is
576 * already released.
577 */
9a69a829 578out:
9cd75c3c 579 userfaultfd_ctx_put(ctx);
9cd75c3c
PE
580}
581
582static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
583 struct userfaultfd_wait_queue *ewq)
584{
585 ewq->msg.event = 0;
586 wake_up_locked(&ctx->event_wqh);
587 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
588}
589
893e26e6
PE
590int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
591{
592 struct userfaultfd_ctx *ctx = NULL, *octx;
593 struct userfaultfd_fork_ctx *fctx;
594
595 octx = vma->vm_userfaultfd_ctx.ctx;
596 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
597 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
598 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
599 return 0;
600 }
601
602 list_for_each_entry(fctx, fcs, list)
603 if (fctx->orig == octx) {
604 ctx = fctx->new;
605 break;
606 }
607
608 if (!ctx) {
609 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
610 if (!fctx)
611 return -ENOMEM;
612
613 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
614 if (!ctx) {
615 kfree(fctx);
616 return -ENOMEM;
617 }
618
619 atomic_set(&ctx->refcount, 1);
620 ctx->flags = octx->flags;
621 ctx->state = UFFD_STATE_RUNNING;
622 ctx->features = octx->features;
623 ctx->released = false;
624 ctx->mm = vma->vm_mm;
d3aadc8e 625 atomic_inc(&ctx->mm->mm_count);
893e26e6
PE
626
627 userfaultfd_ctx_get(octx);
628 fctx->orig = octx;
629 fctx->new = ctx;
630 list_add_tail(&fctx->list, fcs);
631 }
632
633 vma->vm_userfaultfd_ctx.ctx = ctx;
634 return 0;
635}
636
8c9e7bb7 637static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
893e26e6
PE
638{
639 struct userfaultfd_ctx *ctx = fctx->orig;
640 struct userfaultfd_wait_queue ewq;
641
642 msg_init(&ewq.msg);
643
644 ewq.msg.event = UFFD_EVENT_FORK;
645 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
646
8c9e7bb7 647 userfaultfd_event_wait_completion(ctx, &ewq);
893e26e6
PE
648}
649
650void dup_userfaultfd_complete(struct list_head *fcs)
651{
893e26e6
PE
652 struct userfaultfd_fork_ctx *fctx, *n;
653
654 list_for_each_entry_safe(fctx, n, fcs, list) {
8c9e7bb7 655 dup_fctx(fctx);
893e26e6
PE
656 list_del(&fctx->list);
657 kfree(fctx);
658 }
659}
660
72f87654
PE
661void mremap_userfaultfd_prep(struct vm_area_struct *vma,
662 struct vm_userfaultfd_ctx *vm_ctx)
663{
664 struct userfaultfd_ctx *ctx;
665
666 ctx = vma->vm_userfaultfd_ctx.ctx;
667 if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
668 vm_ctx->ctx = ctx;
669 userfaultfd_ctx_get(ctx);
670 }
671}
672
90794bf1 673void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
72f87654
PE
674 unsigned long from, unsigned long to,
675 unsigned long len)
676{
90794bf1 677 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
72f87654
PE
678 struct userfaultfd_wait_queue ewq;
679
680 if (!ctx)
681 return;
682
683 if (to & ~PAGE_MASK) {
684 userfaultfd_ctx_put(ctx);
685 return;
686 }
687
688 msg_init(&ewq.msg);
689
690 ewq.msg.event = UFFD_EVENT_REMAP;
691 ewq.msg.arg.remap.from = from;
692 ewq.msg.arg.remap.to = to;
693 ewq.msg.arg.remap.len = len;
694
695 userfaultfd_event_wait_completion(ctx, &ewq);
696}
697
d811914d
MR
698void userfaultfd_remove(struct vm_area_struct *vma,
699 struct vm_area_struct **prev,
700 unsigned long start, unsigned long end)
05ce7724
PE
701{
702 struct mm_struct *mm = vma->vm_mm;
703 struct userfaultfd_ctx *ctx;
704 struct userfaultfd_wait_queue ewq;
705
706 ctx = vma->vm_userfaultfd_ctx.ctx;
d811914d 707 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
05ce7724
PE
708 return;
709
710 userfaultfd_ctx_get(ctx);
711 up_read(&mm->mmap_sem);
712
713 *prev = NULL; /* We wait for ACK w/o the mmap semaphore */
714
715 msg_init(&ewq.msg);
716
d811914d
MR
717 ewq.msg.event = UFFD_EVENT_REMOVE;
718 ewq.msg.arg.remove.start = start;
719 ewq.msg.arg.remove.end = end;
05ce7724
PE
720
721 userfaultfd_event_wait_completion(ctx, &ewq);
722
723 down_read(&mm->mmap_sem);
724}
725
897ab3e0
MR
726static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
727 unsigned long start, unsigned long end)
728{
729 struct userfaultfd_unmap_ctx *unmap_ctx;
730
731 list_for_each_entry(unmap_ctx, unmaps, list)
732 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
733 unmap_ctx->end == end)
734 return true;
735
736 return false;
737}
738
739int userfaultfd_unmap_prep(struct vm_area_struct *vma,
740 unsigned long start, unsigned long end,
741 struct list_head *unmaps)
742{
743 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
744 struct userfaultfd_unmap_ctx *unmap_ctx;
745 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
746
747 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
748 has_unmap_ctx(ctx, unmaps, start, end))
749 continue;
750
751 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
752 if (!unmap_ctx)
753 return -ENOMEM;
754
755 userfaultfd_ctx_get(ctx);
756 unmap_ctx->ctx = ctx;
757 unmap_ctx->start = start;
758 unmap_ctx->end = end;
759 list_add_tail(&unmap_ctx->list, unmaps);
760 }
761
762 return 0;
763}
764
765void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
766{
767 struct userfaultfd_unmap_ctx *ctx, *n;
768 struct userfaultfd_wait_queue ewq;
769
770 list_for_each_entry_safe(ctx, n, uf, list) {
771 msg_init(&ewq.msg);
772
773 ewq.msg.event = UFFD_EVENT_UNMAP;
774 ewq.msg.arg.remove.start = ctx->start;
775 ewq.msg.arg.remove.end = ctx->end;
776
777 userfaultfd_event_wait_completion(ctx->ctx, &ewq);
778
779 list_del(&ctx->list);
780 kfree(ctx);
781 }
782}
783
86039bd3
AA
784static int userfaultfd_release(struct inode *inode, struct file *file)
785{
786 struct userfaultfd_ctx *ctx = file->private_data;
787 struct mm_struct *mm = ctx->mm;
788 struct vm_area_struct *vma, *prev;
789 /* len == 0 means wake all */
790 struct userfaultfd_wake_range range = { .len = 0, };
791 unsigned long new_flags;
792
793 ACCESS_ONCE(ctx->released) = true;
794
d2005e3f
ON
795 if (!mmget_not_zero(mm))
796 goto wakeup;
797
86039bd3
AA
798 /*
799 * Flush page faults out of all CPUs. NOTE: all page faults
800 * must be retried without returning VM_FAULT_SIGBUS if
801 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
802 * changes while handle_userfault released the mmap_sem. So
803 * it's critical that released is set to true (above), before
804 * taking the mmap_sem for writing.
805 */
806 down_write(&mm->mmap_sem);
807 prev = NULL;
808 for (vma = mm->mmap; vma; vma = vma->vm_next) {
809 cond_resched();
810 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
811 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
812 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
813 prev = vma;
814 continue;
815 }
816 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
817 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
818 new_flags, vma->anon_vma,
819 vma->vm_file, vma->vm_pgoff,
820 vma_policy(vma),
821 NULL_VM_UFFD_CTX);
822 if (prev)
823 vma = prev;
824 else
825 prev = vma;
826 vma->vm_flags = new_flags;
827 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
828 }
829 up_write(&mm->mmap_sem);
d2005e3f
ON
830 mmput(mm);
831wakeup:
86039bd3 832 /*
15b726ef 833 * After no new page faults can wait on this fault_*wqh, flush
86039bd3 834 * the last page faults that may have been already waiting on
15b726ef 835 * the fault_*wqh.
86039bd3 836 */
15b726ef 837 spin_lock(&ctx->fault_pending_wqh.lock);
ac5be6b4
AA
838 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
839 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
15b726ef 840 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
841
842 wake_up_poll(&ctx->fd_wqh, POLLHUP);
843 userfaultfd_ctx_put(ctx);
844 return 0;
845}
846
15b726ef 847/* fault_pending_wqh.lock must be hold by the caller */
6dcc27fd
PE
848static inline struct userfaultfd_wait_queue *find_userfault_in(
849 wait_queue_head_t *wqh)
86039bd3
AA
850{
851 wait_queue_t *wq;
15b726ef 852 struct userfaultfd_wait_queue *uwq;
86039bd3 853
6dcc27fd 854 VM_BUG_ON(!spin_is_locked(&wqh->lock));
86039bd3 855
15b726ef 856 uwq = NULL;
6dcc27fd 857 if (!waitqueue_active(wqh))
15b726ef
AA
858 goto out;
859 /* walk in reverse to provide FIFO behavior to read userfaults */
6dcc27fd 860 wq = list_last_entry(&wqh->task_list, typeof(*wq), task_list);
15b726ef
AA
861 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
862out:
863 return uwq;
86039bd3 864}
6dcc27fd
PE
865
866static inline struct userfaultfd_wait_queue *find_userfault(
867 struct userfaultfd_ctx *ctx)
868{
869 return find_userfault_in(&ctx->fault_pending_wqh);
870}
86039bd3 871
9cd75c3c
PE
872static inline struct userfaultfd_wait_queue *find_userfault_evt(
873 struct userfaultfd_ctx *ctx)
874{
875 return find_userfault_in(&ctx->event_wqh);
876}
877
86039bd3
AA
878static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
879{
880 struct userfaultfd_ctx *ctx = file->private_data;
881 unsigned int ret;
882
883 poll_wait(file, &ctx->fd_wqh, wait);
884
885 switch (ctx->state) {
886 case UFFD_STATE_WAIT_API:
887 return POLLERR;
888 case UFFD_STATE_RUNNING:
ba85c702
AA
889 /*
890 * poll() never guarantees that read won't block.
891 * userfaults can be waken before they're read().
892 */
893 if (unlikely(!(file->f_flags & O_NONBLOCK)))
894 return POLLERR;
15b726ef
AA
895 /*
896 * lockless access to see if there are pending faults
897 * __pollwait last action is the add_wait_queue but
898 * the spin_unlock would allow the waitqueue_active to
899 * pass above the actual list_add inside
900 * add_wait_queue critical section. So use a full
901 * memory barrier to serialize the list_add write of
902 * add_wait_queue() with the waitqueue_active read
903 * below.
904 */
905 ret = 0;
906 smp_mb();
907 if (waitqueue_active(&ctx->fault_pending_wqh))
908 ret = POLLIN;
9cd75c3c
PE
909 else if (waitqueue_active(&ctx->event_wqh))
910 ret = POLLIN;
911
86039bd3
AA
912 return ret;
913 default:
8474901a
AA
914 WARN_ON_ONCE(1);
915 return POLLERR;
86039bd3
AA
916 }
917}
918
893e26e6
PE
919static const struct file_operations userfaultfd_fops;
920
921static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
922 struct userfaultfd_ctx *new,
923 struct uffd_msg *msg)
924{
925 int fd;
926 struct file *file;
927 unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
928
929 fd = get_unused_fd_flags(flags);
930 if (fd < 0)
931 return fd;
932
933 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
934 O_RDWR | flags);
935 if (IS_ERR(file)) {
936 put_unused_fd(fd);
937 return PTR_ERR(file);
938 }
939
940 fd_install(fd, file);
941 msg->arg.reserved.reserved1 = 0;
942 msg->arg.fork.ufd = fd;
943
944 return 0;
945}
946
86039bd3 947static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
a9b85f94 948 struct uffd_msg *msg)
86039bd3
AA
949{
950 ssize_t ret;
951 DECLARE_WAITQUEUE(wait, current);
15b726ef 952 struct userfaultfd_wait_queue *uwq;
893e26e6
PE
953 /*
954 * Handling fork event requires sleeping operations, so
955 * we drop the event_wqh lock, then do these ops, then
956 * lock it back and wake up the waiter. While the lock is
957 * dropped the ewq may go away so we keep track of it
958 * carefully.
959 */
960 LIST_HEAD(fork_event);
961 struct userfaultfd_ctx *fork_nctx = NULL;
86039bd3 962
15b726ef 963 /* always take the fd_wqh lock before the fault_pending_wqh lock */
86039bd3
AA
964 spin_lock(&ctx->fd_wqh.lock);
965 __add_wait_queue(&ctx->fd_wqh, &wait);
966 for (;;) {
967 set_current_state(TASK_INTERRUPTIBLE);
15b726ef
AA
968 spin_lock(&ctx->fault_pending_wqh.lock);
969 uwq = find_userfault(ctx);
970 if (uwq) {
2c5b7e1b
AA
971 /*
972 * Use a seqcount to repeat the lockless check
973 * in wake_userfault() to avoid missing
974 * wakeups because during the refile both
975 * waitqueue could become empty if this is the
976 * only userfault.
977 */
978 write_seqcount_begin(&ctx->refile_seq);
979
86039bd3 980 /*
15b726ef
AA
981 * The fault_pending_wqh.lock prevents the uwq
982 * to disappear from under us.
983 *
984 * Refile this userfault from
985 * fault_pending_wqh to fault_wqh, it's not
986 * pending anymore after we read it.
987 *
988 * Use list_del() by hand (as
989 * userfaultfd_wake_function also uses
990 * list_del_init() by hand) to be sure nobody
991 * changes __remove_wait_queue() to use
992 * list_del_init() in turn breaking the
993 * !list_empty_careful() check in
994 * handle_userfault(). The uwq->wq.task_list
995 * must never be empty at any time during the
996 * refile, or the waitqueue could disappear
997 * from under us. The "wait_queue_head_t"
998 * parameter of __remove_wait_queue() is unused
999 * anyway.
86039bd3 1000 */
15b726ef
AA
1001 list_del(&uwq->wq.task_list);
1002 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
1003
2c5b7e1b
AA
1004 write_seqcount_end(&ctx->refile_seq);
1005
a9b85f94
AA
1006 /* careful to always initialize msg if ret == 0 */
1007 *msg = uwq->msg;
15b726ef 1008 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
1009 ret = 0;
1010 break;
1011 }
15b726ef 1012 spin_unlock(&ctx->fault_pending_wqh.lock);
9cd75c3c
PE
1013
1014 spin_lock(&ctx->event_wqh.lock);
1015 uwq = find_userfault_evt(ctx);
1016 if (uwq) {
1017 *msg = uwq->msg;
1018
893e26e6
PE
1019 if (uwq->msg.event == UFFD_EVENT_FORK) {
1020 fork_nctx = (struct userfaultfd_ctx *)
1021 (unsigned long)
1022 uwq->msg.arg.reserved.reserved1;
1023 list_move(&uwq->wq.task_list, &fork_event);
1024 spin_unlock(&ctx->event_wqh.lock);
1025 ret = 0;
1026 break;
1027 }
1028
9cd75c3c
PE
1029 userfaultfd_event_complete(ctx, uwq);
1030 spin_unlock(&ctx->event_wqh.lock);
1031 ret = 0;
1032 break;
1033 }
1034 spin_unlock(&ctx->event_wqh.lock);
1035
86039bd3
AA
1036 if (signal_pending(current)) {
1037 ret = -ERESTARTSYS;
1038 break;
1039 }
1040 if (no_wait) {
1041 ret = -EAGAIN;
1042 break;
1043 }
1044 spin_unlock(&ctx->fd_wqh.lock);
1045 schedule();
1046 spin_lock(&ctx->fd_wqh.lock);
1047 }
1048 __remove_wait_queue(&ctx->fd_wqh, &wait);
1049 __set_current_state(TASK_RUNNING);
1050 spin_unlock(&ctx->fd_wqh.lock);
1051
893e26e6
PE
1052 if (!ret && msg->event == UFFD_EVENT_FORK) {
1053 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
1054
1055 if (!ret) {
1056 spin_lock(&ctx->event_wqh.lock);
1057 if (!list_empty(&fork_event)) {
1058 uwq = list_first_entry(&fork_event,
1059 typeof(*uwq),
1060 wq.task_list);
1061 list_del(&uwq->wq.task_list);
1062 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1063 userfaultfd_event_complete(ctx, uwq);
1064 }
1065 spin_unlock(&ctx->event_wqh.lock);
1066 }
1067 }
1068
86039bd3
AA
1069 return ret;
1070}
1071
1072static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1073 size_t count, loff_t *ppos)
1074{
1075 struct userfaultfd_ctx *ctx = file->private_data;
1076 ssize_t _ret, ret = 0;
a9b85f94 1077 struct uffd_msg msg;
86039bd3
AA
1078 int no_wait = file->f_flags & O_NONBLOCK;
1079
1080 if (ctx->state == UFFD_STATE_WAIT_API)
1081 return -EINVAL;
86039bd3
AA
1082
1083 for (;;) {
a9b85f94 1084 if (count < sizeof(msg))
86039bd3 1085 return ret ? ret : -EINVAL;
a9b85f94 1086 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
86039bd3
AA
1087 if (_ret < 0)
1088 return ret ? ret : _ret;
a9b85f94 1089 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
86039bd3 1090 return ret ? ret : -EFAULT;
a9b85f94
AA
1091 ret += sizeof(msg);
1092 buf += sizeof(msg);
1093 count -= sizeof(msg);
86039bd3
AA
1094 /*
1095 * Allow to read more than one fault at time but only
1096 * block if waiting for the very first one.
1097 */
1098 no_wait = O_NONBLOCK;
1099 }
1100}
1101
1102static void __wake_userfault(struct userfaultfd_ctx *ctx,
1103 struct userfaultfd_wake_range *range)
1104{
1105 unsigned long start, end;
1106
1107 start = range->start;
1108 end = range->start + range->len;
1109
15b726ef 1110 spin_lock(&ctx->fault_pending_wqh.lock);
86039bd3 1111 /* wake all in the range and autoremove */
15b726ef 1112 if (waitqueue_active(&ctx->fault_pending_wqh))
ac5be6b4 1113 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
15b726ef
AA
1114 range);
1115 if (waitqueue_active(&ctx->fault_wqh))
ac5be6b4 1116 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
15b726ef 1117 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
1118}
1119
1120static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1121 struct userfaultfd_wake_range *range)
1122{
2c5b7e1b
AA
1123 unsigned seq;
1124 bool need_wakeup;
1125
86039bd3
AA
1126 /*
1127 * To be sure waitqueue_active() is not reordered by the CPU
1128 * before the pagetable update, use an explicit SMP memory
1129 * barrier here. PT lock release or up_read(mmap_sem) still
1130 * have release semantics that can allow the
1131 * waitqueue_active() to be reordered before the pte update.
1132 */
1133 smp_mb();
1134
1135 /*
1136 * Use waitqueue_active because it's very frequent to
1137 * change the address space atomically even if there are no
1138 * userfaults yet. So we take the spinlock only when we're
1139 * sure we've userfaults to wake.
1140 */
2c5b7e1b
AA
1141 do {
1142 seq = read_seqcount_begin(&ctx->refile_seq);
1143 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1144 waitqueue_active(&ctx->fault_wqh);
1145 cond_resched();
1146 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1147 if (need_wakeup)
86039bd3
AA
1148 __wake_userfault(ctx, range);
1149}
1150
1151static __always_inline int validate_range(struct mm_struct *mm,
1152 __u64 start, __u64 len)
1153{
1154 __u64 task_size = mm->task_size;
1155
1156 if (start & ~PAGE_MASK)
1157 return -EINVAL;
1158 if (len & ~PAGE_MASK)
1159 return -EINVAL;
1160 if (!len)
1161 return -EINVAL;
1162 if (start < mmap_min_addr)
1163 return -EINVAL;
1164 if (start >= task_size)
1165 return -EINVAL;
1166 if (len > task_size - start)
1167 return -EINVAL;
1168 return 0;
1169}
1170
ba6907db
MR
1171static inline bool vma_can_userfault(struct vm_area_struct *vma)
1172{
cac67329
MR
1173 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1174 vma_is_shmem(vma);
ba6907db
MR
1175}
1176
86039bd3
AA
1177static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1178 unsigned long arg)
1179{
1180 struct mm_struct *mm = ctx->mm;
1181 struct vm_area_struct *vma, *prev, *cur;
1182 int ret;
1183 struct uffdio_register uffdio_register;
1184 struct uffdio_register __user *user_uffdio_register;
1185 unsigned long vm_flags, new_flags;
1186 bool found;
cac67329 1187 bool non_anon_pages;
86039bd3
AA
1188 unsigned long start, end, vma_end;
1189
1190 user_uffdio_register = (struct uffdio_register __user *) arg;
1191
1192 ret = -EFAULT;
1193 if (copy_from_user(&uffdio_register, user_uffdio_register,
1194 sizeof(uffdio_register)-sizeof(__u64)))
1195 goto out;
1196
1197 ret = -EINVAL;
1198 if (!uffdio_register.mode)
1199 goto out;
1200 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1201 UFFDIO_REGISTER_MODE_WP))
1202 goto out;
1203 vm_flags = 0;
1204 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1205 vm_flags |= VM_UFFD_MISSING;
1206 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1207 vm_flags |= VM_UFFD_WP;
1208 /*
1209 * FIXME: remove the below error constraint by
1210 * implementing the wprotect tracking mode.
1211 */
1212 ret = -EINVAL;
1213 goto out;
1214 }
1215
1216 ret = validate_range(mm, uffdio_register.range.start,
1217 uffdio_register.range.len);
1218 if (ret)
1219 goto out;
1220
1221 start = uffdio_register.range.start;
1222 end = start + uffdio_register.range.len;
1223
d2005e3f
ON
1224 ret = -ENOMEM;
1225 if (!mmget_not_zero(mm))
1226 goto out;
1227
86039bd3
AA
1228 down_write(&mm->mmap_sem);
1229 vma = find_vma_prev(mm, start, &prev);
86039bd3
AA
1230 if (!vma)
1231 goto out_unlock;
1232
1233 /* check that there's at least one vma in the range */
1234 ret = -EINVAL;
1235 if (vma->vm_start >= end)
1236 goto out_unlock;
1237
cab350af
MK
1238 /*
1239 * If the first vma contains huge pages, make sure start address
1240 * is aligned to huge page size.
1241 */
1242 if (is_vm_hugetlb_page(vma)) {
1243 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1244
1245 if (start & (vma_hpagesize - 1))
1246 goto out_unlock;
1247 }
1248
86039bd3
AA
1249 /*
1250 * Search for not compatible vmas.
86039bd3
AA
1251 */
1252 found = false;
cac67329 1253 non_anon_pages = false;
86039bd3
AA
1254 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1255 cond_resched();
1256
1257 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1258 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1259
1260 /* check not compatible vmas */
1261 ret = -EINVAL;
ba6907db 1262 if (!vma_can_userfault(cur))
86039bd3 1263 goto out_unlock;
cab350af
MK
1264 /*
1265 * If this vma contains ending address, and huge pages
1266 * check alignment.
1267 */
1268 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1269 end > cur->vm_start) {
1270 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1271
1272 ret = -EINVAL;
1273
1274 if (end & (vma_hpagesize - 1))
1275 goto out_unlock;
1276 }
86039bd3
AA
1277
1278 /*
1279 * Check that this vma isn't already owned by a
1280 * different userfaultfd. We can't allow more than one
1281 * userfaultfd to own a single vma simultaneously or we
1282 * wouldn't know which one to deliver the userfaults to.
1283 */
1284 ret = -EBUSY;
1285 if (cur->vm_userfaultfd_ctx.ctx &&
1286 cur->vm_userfaultfd_ctx.ctx != ctx)
1287 goto out_unlock;
1288
cab350af
MK
1289 /*
1290 * Note vmas containing huge pages
1291 */
cac67329
MR
1292 if (is_vm_hugetlb_page(cur) || vma_is_shmem(cur))
1293 non_anon_pages = true;
cab350af 1294
86039bd3
AA
1295 found = true;
1296 }
1297 BUG_ON(!found);
1298
1299 if (vma->vm_start < start)
1300 prev = vma;
1301
1302 ret = 0;
1303 do {
1304 cond_resched();
1305
ba6907db 1306 BUG_ON(!vma_can_userfault(vma));
86039bd3
AA
1307 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1308 vma->vm_userfaultfd_ctx.ctx != ctx);
1309
1310 /*
1311 * Nothing to do: this vma is already registered into this
1312 * userfaultfd and with the right tracking mode too.
1313 */
1314 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1315 (vma->vm_flags & vm_flags) == vm_flags)
1316 goto skip;
1317
1318 if (vma->vm_start > start)
1319 start = vma->vm_start;
1320 vma_end = min(end, vma->vm_end);
1321
1322 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
1323 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1324 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1325 vma_policy(vma),
1326 ((struct vm_userfaultfd_ctx){ ctx }));
1327 if (prev) {
1328 vma = prev;
1329 goto next;
1330 }
1331 if (vma->vm_start < start) {
1332 ret = split_vma(mm, vma, start, 1);
1333 if (ret)
1334 break;
1335 }
1336 if (vma->vm_end > end) {
1337 ret = split_vma(mm, vma, end, 0);
1338 if (ret)
1339 break;
1340 }
1341 next:
1342 /*
1343 * In the vma_merge() successful mprotect-like case 8:
1344 * the next vma was merged into the current one and
1345 * the current one has not been updated yet.
1346 */
1347 vma->vm_flags = new_flags;
1348 vma->vm_userfaultfd_ctx.ctx = ctx;
1349
1350 skip:
1351 prev = vma;
1352 start = vma->vm_end;
1353 vma = vma->vm_next;
1354 } while (vma && vma->vm_start < end);
1355out_unlock:
1356 up_write(&mm->mmap_sem);
d2005e3f 1357 mmput(mm);
86039bd3
AA
1358 if (!ret) {
1359 /*
1360 * Now that we scanned all vmas we can already tell
1361 * userland which ioctls methods are guaranteed to
1362 * succeed on this range.
1363 */
cac67329 1364 if (put_user(non_anon_pages ? UFFD_API_RANGE_IOCTLS_BASIC :
cab350af 1365 UFFD_API_RANGE_IOCTLS,
86039bd3
AA
1366 &user_uffdio_register->ioctls))
1367 ret = -EFAULT;
1368 }
1369out:
1370 return ret;
1371}
1372
1373static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1374 unsigned long arg)
1375{
1376 struct mm_struct *mm = ctx->mm;
1377 struct vm_area_struct *vma, *prev, *cur;
1378 int ret;
1379 struct uffdio_range uffdio_unregister;
1380 unsigned long new_flags;
1381 bool found;
1382 unsigned long start, end, vma_end;
1383 const void __user *buf = (void __user *)arg;
1384
1385 ret = -EFAULT;
1386 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1387 goto out;
1388
1389 ret = validate_range(mm, uffdio_unregister.start,
1390 uffdio_unregister.len);
1391 if (ret)
1392 goto out;
1393
1394 start = uffdio_unregister.start;
1395 end = start + uffdio_unregister.len;
1396
d2005e3f
ON
1397 ret = -ENOMEM;
1398 if (!mmget_not_zero(mm))
1399 goto out;
1400
86039bd3
AA
1401 down_write(&mm->mmap_sem);
1402 vma = find_vma_prev(mm, start, &prev);
86039bd3
AA
1403 if (!vma)
1404 goto out_unlock;
1405
1406 /* check that there's at least one vma in the range */
1407 ret = -EINVAL;
1408 if (vma->vm_start >= end)
1409 goto out_unlock;
1410
cab350af
MK
1411 /*
1412 * If the first vma contains huge pages, make sure start address
1413 * is aligned to huge page size.
1414 */
1415 if (is_vm_hugetlb_page(vma)) {
1416 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1417
1418 if (start & (vma_hpagesize - 1))
1419 goto out_unlock;
1420 }
1421
86039bd3
AA
1422 /*
1423 * Search for not compatible vmas.
86039bd3
AA
1424 */
1425 found = false;
1426 ret = -EINVAL;
1427 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1428 cond_resched();
1429
1430 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1431 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1432
1433 /*
1434 * Check not compatible vmas, not strictly required
1435 * here as not compatible vmas cannot have an
1436 * userfaultfd_ctx registered on them, but this
1437 * provides for more strict behavior to notice
1438 * unregistration errors.
1439 */
ba6907db 1440 if (!vma_can_userfault(cur))
86039bd3
AA
1441 goto out_unlock;
1442
1443 found = true;
1444 }
1445 BUG_ON(!found);
1446
1447 if (vma->vm_start < start)
1448 prev = vma;
1449
1450 ret = 0;
1451 do {
1452 cond_resched();
1453
ba6907db 1454 BUG_ON(!vma_can_userfault(vma));
86039bd3
AA
1455
1456 /*
1457 * Nothing to do: this vma is already registered into this
1458 * userfaultfd and with the right tracking mode too.
1459 */
1460 if (!vma->vm_userfaultfd_ctx.ctx)
1461 goto skip;
1462
1463 if (vma->vm_start > start)
1464 start = vma->vm_start;
1465 vma_end = min(end, vma->vm_end);
1466
09fa5296
AA
1467 if (userfaultfd_missing(vma)) {
1468 /*
1469 * Wake any concurrent pending userfault while
1470 * we unregister, so they will not hang
1471 * permanently and it avoids userland to call
1472 * UFFDIO_WAKE explicitly.
1473 */
1474 struct userfaultfd_wake_range range;
1475 range.start = start;
1476 range.len = vma_end - start;
1477 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1478 }
1479
86039bd3
AA
1480 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1481 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1482 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1483 vma_policy(vma),
1484 NULL_VM_UFFD_CTX);
1485 if (prev) {
1486 vma = prev;
1487 goto next;
1488 }
1489 if (vma->vm_start < start) {
1490 ret = split_vma(mm, vma, start, 1);
1491 if (ret)
1492 break;
1493 }
1494 if (vma->vm_end > end) {
1495 ret = split_vma(mm, vma, end, 0);
1496 if (ret)
1497 break;
1498 }
1499 next:
1500 /*
1501 * In the vma_merge() successful mprotect-like case 8:
1502 * the next vma was merged into the current one and
1503 * the current one has not been updated yet.
1504 */
1505 vma->vm_flags = new_flags;
1506 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1507
1508 skip:
1509 prev = vma;
1510 start = vma->vm_end;
1511 vma = vma->vm_next;
1512 } while (vma && vma->vm_start < end);
1513out_unlock:
1514 up_write(&mm->mmap_sem);
d2005e3f 1515 mmput(mm);
86039bd3
AA
1516out:
1517 return ret;
1518}
1519
1520/*
ba85c702
AA
1521 * userfaultfd_wake may be used in combination with the
1522 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
86039bd3
AA
1523 */
1524static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1525 unsigned long arg)
1526{
1527 int ret;
1528 struct uffdio_range uffdio_wake;
1529 struct userfaultfd_wake_range range;
1530 const void __user *buf = (void __user *)arg;
1531
1532 ret = -EFAULT;
1533 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1534 goto out;
1535
1536 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1537 if (ret)
1538 goto out;
1539
1540 range.start = uffdio_wake.start;
1541 range.len = uffdio_wake.len;
1542
1543 /*
1544 * len == 0 means wake all and we don't want to wake all here,
1545 * so check it again to be sure.
1546 */
1547 VM_BUG_ON(!range.len);
1548
1549 wake_userfault(ctx, &range);
1550 ret = 0;
1551
1552out:
1553 return ret;
1554}
1555
ad465cae
AA
1556static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1557 unsigned long arg)
1558{
1559 __s64 ret;
1560 struct uffdio_copy uffdio_copy;
1561 struct uffdio_copy __user *user_uffdio_copy;
1562 struct userfaultfd_wake_range range;
1563
1564 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1565
1566 ret = -EFAULT;
1567 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1568 /* don't copy "copy" last field */
1569 sizeof(uffdio_copy)-sizeof(__s64)))
1570 goto out;
1571
1572 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1573 if (ret)
1574 goto out;
1575 /*
1576 * double check for wraparound just in case. copy_from_user()
1577 * will later check uffdio_copy.src + uffdio_copy.len to fit
1578 * in the userland range.
1579 */
1580 ret = -EINVAL;
1581 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1582 goto out;
1583 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1584 goto out;
d2005e3f
ON
1585 if (mmget_not_zero(ctx->mm)) {
1586 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1587 uffdio_copy.len);
1588 mmput(ctx->mm);
96333187
MR
1589 } else {
1590 return -ENOSPC;
d2005e3f 1591 }
ad465cae
AA
1592 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1593 return -EFAULT;
1594 if (ret < 0)
1595 goto out;
1596 BUG_ON(!ret);
1597 /* len == 0 would wake all */
1598 range.len = ret;
1599 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1600 range.start = uffdio_copy.dst;
1601 wake_userfault(ctx, &range);
1602 }
1603 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1604out:
1605 return ret;
1606}
1607
1608static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1609 unsigned long arg)
1610{
1611 __s64 ret;
1612 struct uffdio_zeropage uffdio_zeropage;
1613 struct uffdio_zeropage __user *user_uffdio_zeropage;
1614 struct userfaultfd_wake_range range;
1615
1616 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1617
1618 ret = -EFAULT;
1619 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1620 /* don't copy "zeropage" last field */
1621 sizeof(uffdio_zeropage)-sizeof(__s64)))
1622 goto out;
1623
1624 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1625 uffdio_zeropage.range.len);
1626 if (ret)
1627 goto out;
1628 ret = -EINVAL;
1629 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1630 goto out;
1631
d2005e3f
ON
1632 if (mmget_not_zero(ctx->mm)) {
1633 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1634 uffdio_zeropage.range.len);
1635 mmput(ctx->mm);
1636 }
ad465cae
AA
1637 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1638 return -EFAULT;
1639 if (ret < 0)
1640 goto out;
1641 /* len == 0 would wake all */
1642 BUG_ON(!ret);
1643 range.len = ret;
1644 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1645 range.start = uffdio_zeropage.range.start;
1646 wake_userfault(ctx, &range);
1647 }
1648 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1649out:
1650 return ret;
1651}
1652
9cd75c3c
PE
1653static inline unsigned int uffd_ctx_features(__u64 user_features)
1654{
1655 /*
1656 * For the current set of features the bits just coincide
1657 */
1658 return (unsigned int)user_features;
1659}
1660
86039bd3
AA
1661/*
1662 * userland asks for a certain API version and we return which bits
1663 * and ioctl commands are implemented in this kernel for such API
1664 * version or -EINVAL if unknown.
1665 */
1666static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1667 unsigned long arg)
1668{
1669 struct uffdio_api uffdio_api;
1670 void __user *buf = (void __user *)arg;
1671 int ret;
65603144 1672 __u64 features;
86039bd3
AA
1673
1674 ret = -EINVAL;
1675 if (ctx->state != UFFD_STATE_WAIT_API)
1676 goto out;
1677 ret = -EFAULT;
a9b85f94 1678 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
86039bd3 1679 goto out;
65603144
AA
1680 features = uffdio_api.features;
1681 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
86039bd3
AA
1682 memset(&uffdio_api, 0, sizeof(uffdio_api));
1683 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1684 goto out;
1685 ret = -EINVAL;
1686 goto out;
1687 }
65603144
AA
1688 /* report all available features and ioctls to userland */
1689 uffdio_api.features = UFFD_API_FEATURES;
86039bd3
AA
1690 uffdio_api.ioctls = UFFD_API_IOCTLS;
1691 ret = -EFAULT;
1692 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1693 goto out;
1694 ctx->state = UFFD_STATE_RUNNING;
65603144
AA
1695 /* only enable the requested features for this uffd context */
1696 ctx->features = uffd_ctx_features(features);
86039bd3
AA
1697 ret = 0;
1698out:
1699 return ret;
1700}
1701
1702static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1703 unsigned long arg)
1704{
1705 int ret = -EINVAL;
1706 struct userfaultfd_ctx *ctx = file->private_data;
1707
e6485a47
AA
1708 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1709 return -EINVAL;
1710
86039bd3
AA
1711 switch(cmd) {
1712 case UFFDIO_API:
1713 ret = userfaultfd_api(ctx, arg);
1714 break;
1715 case UFFDIO_REGISTER:
1716 ret = userfaultfd_register(ctx, arg);
1717 break;
1718 case UFFDIO_UNREGISTER:
1719 ret = userfaultfd_unregister(ctx, arg);
1720 break;
1721 case UFFDIO_WAKE:
1722 ret = userfaultfd_wake(ctx, arg);
1723 break;
ad465cae
AA
1724 case UFFDIO_COPY:
1725 ret = userfaultfd_copy(ctx, arg);
1726 break;
1727 case UFFDIO_ZEROPAGE:
1728 ret = userfaultfd_zeropage(ctx, arg);
1729 break;
86039bd3
AA
1730 }
1731 return ret;
1732}
1733
1734#ifdef CONFIG_PROC_FS
1735static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1736{
1737 struct userfaultfd_ctx *ctx = f->private_data;
1738 wait_queue_t *wq;
1739 struct userfaultfd_wait_queue *uwq;
1740 unsigned long pending = 0, total = 0;
1741
15b726ef
AA
1742 spin_lock(&ctx->fault_pending_wqh.lock);
1743 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
1744 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1745 pending++;
1746 total++;
1747 }
86039bd3
AA
1748 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1749 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
86039bd3
AA
1750 total++;
1751 }
15b726ef 1752 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
1753
1754 /*
1755 * If more protocols will be added, there will be all shown
1756 * separated by a space. Like this:
1757 * protocols: aa:... bb:...
1758 */
1759 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
3f602d27 1760 pending, total, UFFD_API, UFFD_API_FEATURES,
86039bd3
AA
1761 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1762}
1763#endif
1764
1765static const struct file_operations userfaultfd_fops = {
1766#ifdef CONFIG_PROC_FS
1767 .show_fdinfo = userfaultfd_show_fdinfo,
1768#endif
1769 .release = userfaultfd_release,
1770 .poll = userfaultfd_poll,
1771 .read = userfaultfd_read,
1772 .unlocked_ioctl = userfaultfd_ioctl,
1773 .compat_ioctl = userfaultfd_ioctl,
1774 .llseek = noop_llseek,
1775};
1776
3004ec9c
AA
1777static void init_once_userfaultfd_ctx(void *mem)
1778{
1779 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1780
1781 init_waitqueue_head(&ctx->fault_pending_wqh);
1782 init_waitqueue_head(&ctx->fault_wqh);
9cd75c3c 1783 init_waitqueue_head(&ctx->event_wqh);
3004ec9c 1784 init_waitqueue_head(&ctx->fd_wqh);
2c5b7e1b 1785 seqcount_init(&ctx->refile_seq);
3004ec9c
AA
1786}
1787
86039bd3 1788/**
9332ef9d 1789 * userfaultfd_file_create - Creates a userfaultfd file pointer.
86039bd3
AA
1790 * @flags: Flags for the userfaultfd file.
1791 *
9332ef9d 1792 * This function creates a userfaultfd file pointer, w/out installing
86039bd3
AA
1793 * it into the fd table. This is useful when the userfaultfd file is
1794 * used during the initialization of data structures that require
1795 * extra setup after the userfaultfd creation. So the userfaultfd
1796 * creation is split into the file pointer creation phase, and the
1797 * file descriptor installation phase. In this way races with
1798 * userspace closing the newly installed file descriptor can be
9332ef9d 1799 * avoided. Returns a userfaultfd file pointer, or a proper error
86039bd3
AA
1800 * pointer.
1801 */
1802static struct file *userfaultfd_file_create(int flags)
1803{
1804 struct file *file;
1805 struct userfaultfd_ctx *ctx;
1806
1807 BUG_ON(!current->mm);
1808
1809 /* Check the UFFD_* constants for consistency. */
1810 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1811 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1812
1813 file = ERR_PTR(-EINVAL);
1814 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1815 goto out;
1816
1817 file = ERR_PTR(-ENOMEM);
3004ec9c 1818 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
86039bd3
AA
1819 if (!ctx)
1820 goto out;
1821
1822 atomic_set(&ctx->refcount, 1);
86039bd3 1823 ctx->flags = flags;
9cd75c3c 1824 ctx->features = 0;
86039bd3
AA
1825 ctx->state = UFFD_STATE_WAIT_API;
1826 ctx->released = false;
1827 ctx->mm = current->mm;
1828 /* prevent the mm struct to be freed */
f1f10076 1829 mmgrab(ctx->mm);
86039bd3
AA
1830
1831 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1832 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
c03e946f 1833 if (IS_ERR(file)) {
d2005e3f 1834 mmdrop(ctx->mm);
3004ec9c 1835 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
c03e946f 1836 }
86039bd3
AA
1837out:
1838 return file;
1839}
1840
1841SYSCALL_DEFINE1(userfaultfd, int, flags)
1842{
1843 int fd, error;
1844 struct file *file;
1845
1846 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1847 if (error < 0)
1848 return error;
1849 fd = error;
1850
1851 file = userfaultfd_file_create(flags);
1852 if (IS_ERR(file)) {
1853 error = PTR_ERR(file);
1854 goto err_put_unused_fd;
1855 }
1856 fd_install(fd, file);
1857
1858 return fd;
1859
1860err_put_unused_fd:
1861 put_unused_fd(fd);
1862
1863 return error;
1864}
3004ec9c
AA
1865
1866static int __init userfaultfd_init(void)
1867{
1868 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1869 sizeof(struct userfaultfd_ctx),
1870 0,
1871 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1872 init_once_userfaultfd_ctx);
1873 return 0;
1874}
1875__initcall(userfaultfd_init);