]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/userfaultfd.c
userfaultfd: propagate the full address in THP faults
[mirror_ubuntu-artful-kernel.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
15#include <linux/hashtable.h>
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/seq_file.h>
21#include <linux/file.h>
22#include <linux/bug.h>
23#include <linux/anon_inodes.h>
24#include <linux/syscalls.h>
25#include <linux/userfaultfd_k.h>
26#include <linux/mempolicy.h>
27#include <linux/ioctl.h>
28#include <linux/security.h>
29
3004ec9c
AA
30static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
31
86039bd3
AA
32enum userfaultfd_state {
33 UFFD_STATE_WAIT_API,
34 UFFD_STATE_RUNNING,
35};
36
3004ec9c
AA
37/*
38 * Start with fault_pending_wqh and fault_wqh so they're more likely
39 * to be in the same cacheline.
40 */
86039bd3 41struct userfaultfd_ctx {
15b726ef
AA
42 /* waitqueue head for the pending (i.e. not read) userfaults */
43 wait_queue_head_t fault_pending_wqh;
44 /* waitqueue head for the userfaults */
86039bd3
AA
45 wait_queue_head_t fault_wqh;
46 /* waitqueue head for the pseudo fd to wakeup poll/read */
47 wait_queue_head_t fd_wqh;
3004ec9c
AA
48 /* pseudo fd refcounting */
49 atomic_t refcount;
86039bd3
AA
50 /* userfaultfd syscall flags */
51 unsigned int flags;
52 /* state machine */
53 enum userfaultfd_state state;
54 /* released */
55 bool released;
56 /* mm with one ore more vmas attached to this userfaultfd_ctx */
57 struct mm_struct *mm;
58};
59
60struct userfaultfd_wait_queue {
a9b85f94 61 struct uffd_msg msg;
86039bd3 62 wait_queue_t wq;
86039bd3
AA
63 struct userfaultfd_ctx *ctx;
64};
65
66struct userfaultfd_wake_range {
67 unsigned long start;
68 unsigned long len;
69};
70
71static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
72 int wake_flags, void *key)
73{
74 struct userfaultfd_wake_range *range = key;
75 int ret;
76 struct userfaultfd_wait_queue *uwq;
77 unsigned long start, len;
78
79 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
80 ret = 0;
86039bd3
AA
81 /* len == 0 means wake all */
82 start = range->start;
83 len = range->len;
a9b85f94
AA
84 if (len && (start > uwq->msg.arg.pagefault.address ||
85 start + len <= uwq->msg.arg.pagefault.address))
86039bd3
AA
86 goto out;
87 ret = wake_up_state(wq->private, mode);
88 if (ret)
89 /*
90 * Wake only once, autoremove behavior.
91 *
92 * After the effect of list_del_init is visible to the
93 * other CPUs, the waitqueue may disappear from under
94 * us, see the !list_empty_careful() in
95 * handle_userfault(). try_to_wake_up() has an
96 * implicit smp_mb__before_spinlock, and the
97 * wq->private is read before calling the extern
98 * function "wake_up_state" (which in turns calls
99 * try_to_wake_up). While the spin_lock;spin_unlock;
100 * wouldn't be enough, the smp_mb__before_spinlock is
101 * enough to avoid an explicit smp_mb() here.
102 */
103 list_del_init(&wq->task_list);
104out:
105 return ret;
106}
107
108/**
109 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
110 * context.
111 * @ctx: [in] Pointer to the userfaultfd context.
112 *
113 * Returns: In case of success, returns not zero.
114 */
115static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
116{
117 if (!atomic_inc_not_zero(&ctx->refcount))
118 BUG();
119}
120
121/**
122 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
123 * context.
124 * @ctx: [in] Pointer to userfaultfd context.
125 *
126 * The userfaultfd context reference must have been previously acquired either
127 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
128 */
129static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
130{
131 if (atomic_dec_and_test(&ctx->refcount)) {
132 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
133 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
134 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
135 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
136 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
137 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
138 mmput(ctx->mm);
3004ec9c 139 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
86039bd3
AA
140 }
141}
142
a9b85f94 143static inline void msg_init(struct uffd_msg *msg)
86039bd3 144{
a9b85f94
AA
145 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
146 /*
147 * Must use memset to zero out the paddings or kernel data is
148 * leaked to userland.
149 */
150 memset(msg, 0, sizeof(struct uffd_msg));
151}
152
153static inline struct uffd_msg userfault_msg(unsigned long address,
154 unsigned int flags,
155 unsigned long reason)
156{
157 struct uffd_msg msg;
158 msg_init(&msg);
159 msg.event = UFFD_EVENT_PAGEFAULT;
160 msg.arg.pagefault.address = address;
86039bd3
AA
161 if (flags & FAULT_FLAG_WRITE)
162 /*
a9b85f94
AA
163 * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
164 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
165 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
166 * was a read fault, otherwise if set it means it's
167 * a write fault.
86039bd3 168 */
a9b85f94 169 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
86039bd3
AA
170 if (reason & VM_UFFD_WP)
171 /*
a9b85f94
AA
172 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
173 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
174 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
175 * a missing fault, otherwise if set it means it's a
176 * write protect fault.
86039bd3 177 */
a9b85f94
AA
178 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
179 return msg;
86039bd3
AA
180}
181
8d2afd96
AA
182/*
183 * Verify the pagetables are still not ok after having reigstered into
184 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
185 * userfault that has already been resolved, if userfaultfd_read and
186 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
187 * threads.
188 */
189static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
190 unsigned long address,
191 unsigned long flags,
192 unsigned long reason)
193{
194 struct mm_struct *mm = ctx->mm;
195 pgd_t *pgd;
196 pud_t *pud;
197 pmd_t *pmd, _pmd;
198 pte_t *pte;
199 bool ret = true;
200
201 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
202
203 pgd = pgd_offset(mm, address);
204 if (!pgd_present(*pgd))
205 goto out;
206 pud = pud_offset(pgd, address);
207 if (!pud_present(*pud))
208 goto out;
209 pmd = pmd_offset(pud, address);
210 /*
211 * READ_ONCE must function as a barrier with narrower scope
212 * and it must be equivalent to:
213 * _pmd = *pmd; barrier();
214 *
215 * This is to deal with the instability (as in
216 * pmd_trans_unstable) of the pmd.
217 */
218 _pmd = READ_ONCE(*pmd);
219 if (!pmd_present(_pmd))
220 goto out;
221
222 ret = false;
223 if (pmd_trans_huge(_pmd))
224 goto out;
225
226 /*
227 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
228 * and use the standard pte_offset_map() instead of parsing _pmd.
229 */
230 pte = pte_offset_map(pmd, address);
231 /*
232 * Lockless access: we're in a wait_event so it's ok if it
233 * changes under us.
234 */
235 if (pte_none(*pte))
236 ret = true;
237 pte_unmap(pte);
238
239out:
240 return ret;
241}
242
86039bd3
AA
243/*
244 * The locking rules involved in returning VM_FAULT_RETRY depending on
245 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
246 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
247 * recommendation in __lock_page_or_retry is not an understatement.
248 *
249 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
250 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
251 * not set.
252 *
253 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
254 * set, VM_FAULT_RETRY can still be returned if and only if there are
255 * fatal_signal_pending()s, and the mmap_sem must be released before
256 * returning it.
257 */
258int handle_userfault(struct vm_area_struct *vma, unsigned long address,
259 unsigned int flags, unsigned long reason)
260{
261 struct mm_struct *mm = vma->vm_mm;
262 struct userfaultfd_ctx *ctx;
263 struct userfaultfd_wait_queue uwq;
ba85c702 264 int ret;
dfa37dc3 265 bool must_wait, return_to_userland;
86039bd3
AA
266
267 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
268
ba85c702 269 ret = VM_FAULT_SIGBUS;
86039bd3
AA
270 ctx = vma->vm_userfaultfd_ctx.ctx;
271 if (!ctx)
ba85c702 272 goto out;
86039bd3
AA
273
274 BUG_ON(ctx->mm != mm);
275
276 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
277 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
278
279 /*
280 * If it's already released don't get it. This avoids to loop
281 * in __get_user_pages if userfaultfd_release waits on the
282 * caller of handle_userfault to release the mmap_sem.
283 */
284 if (unlikely(ACCESS_ONCE(ctx->released)))
ba85c702 285 goto out;
86039bd3
AA
286
287 /*
288 * Check that we can return VM_FAULT_RETRY.
289 *
290 * NOTE: it should become possible to return VM_FAULT_RETRY
291 * even if FAULT_FLAG_TRIED is set without leading to gup()
292 * -EBUSY failures, if the userfaultfd is to be extended for
293 * VM_UFFD_WP tracking and we intend to arm the userfault
294 * without first stopping userland access to the memory. For
295 * VM_UFFD_MISSING userfaults this is enough for now.
296 */
297 if (unlikely(!(flags & FAULT_FLAG_ALLOW_RETRY))) {
298 /*
299 * Validate the invariant that nowait must allow retry
300 * to be sure not to return SIGBUS erroneously on
301 * nowait invocations.
302 */
303 BUG_ON(flags & FAULT_FLAG_RETRY_NOWAIT);
304#ifdef CONFIG_DEBUG_VM
305 if (printk_ratelimit()) {
306 printk(KERN_WARNING
307 "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags);
308 dump_stack();
309 }
310#endif
ba85c702 311 goto out;
86039bd3
AA
312 }
313
314 /*
315 * Handle nowait, not much to do other than tell it to retry
316 * and wait.
317 */
ba85c702 318 ret = VM_FAULT_RETRY;
86039bd3 319 if (flags & FAULT_FLAG_RETRY_NOWAIT)
ba85c702 320 goto out;
86039bd3
AA
321
322 /* take the reference before dropping the mmap_sem */
323 userfaultfd_ctx_get(ctx);
324
86039bd3
AA
325 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
326 uwq.wq.private = current;
a9b85f94 327 uwq.msg = userfault_msg(address, flags, reason);
86039bd3
AA
328 uwq.ctx = ctx;
329
dfa37dc3
AA
330 return_to_userland = (flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
331 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
332
15b726ef 333 spin_lock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
334 /*
335 * After the __add_wait_queue the uwq is visible to userland
336 * through poll/read().
337 */
15b726ef
AA
338 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
339 /*
340 * The smp_mb() after __set_current_state prevents the reads
341 * following the spin_unlock to happen before the list_add in
342 * __add_wait_queue.
343 */
dfa37dc3
AA
344 set_current_state(return_to_userland ? TASK_INTERRUPTIBLE :
345 TASK_KILLABLE);
15b726ef 346 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3 347
8d2afd96
AA
348 must_wait = userfaultfd_must_wait(ctx, address, flags, reason);
349 up_read(&mm->mmap_sem);
350
351 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
dfa37dc3
AA
352 (return_to_userland ? !signal_pending(current) :
353 !fatal_signal_pending(current)))) {
86039bd3
AA
354 wake_up_poll(&ctx->fd_wqh, POLLIN);
355 schedule();
ba85c702
AA
356 ret |= VM_FAULT_MAJOR;
357 }
86039bd3 358
ba85c702 359 __set_current_state(TASK_RUNNING);
15b726ef 360
dfa37dc3
AA
361 if (return_to_userland) {
362 if (signal_pending(current) &&
363 !fatal_signal_pending(current)) {
364 /*
365 * If we got a SIGSTOP or SIGCONT and this is
366 * a normal userland page fault, just let
367 * userland return so the signal will be
368 * handled and gdb debugging works. The page
369 * fault code immediately after we return from
370 * this function is going to release the
371 * mmap_sem and it's not depending on it
372 * (unlike gup would if we were not to return
373 * VM_FAULT_RETRY).
374 *
375 * If a fatal signal is pending we still take
376 * the streamlined VM_FAULT_RETRY failure path
377 * and there's no need to retake the mmap_sem
378 * in such case.
379 */
380 down_read(&mm->mmap_sem);
381 ret = 0;
382 }
383 }
384
15b726ef
AA
385 /*
386 * Here we race with the list_del; list_add in
387 * userfaultfd_ctx_read(), however because we don't ever run
388 * list_del_init() to refile across the two lists, the prev
389 * and next pointers will never point to self. list_add also
390 * would never let any of the two pointers to point to
391 * self. So list_empty_careful won't risk to see both pointers
392 * pointing to self at any time during the list refile. The
393 * only case where list_del_init() is called is the full
394 * removal in the wake function and there we don't re-list_add
395 * and it's fine not to block on the spinlock. The uwq on this
396 * kernel stack can be released after the list_del_init.
397 */
ba85c702 398 if (!list_empty_careful(&uwq.wq.task_list)) {
15b726ef
AA
399 spin_lock(&ctx->fault_pending_wqh.lock);
400 /*
401 * No need of list_del_init(), the uwq on the stack
402 * will be freed shortly anyway.
403 */
404 list_del(&uwq.wq.task_list);
405 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3 406 }
86039bd3
AA
407
408 /*
409 * ctx may go away after this if the userfault pseudo fd is
410 * already released.
411 */
412 userfaultfd_ctx_put(ctx);
413
ba85c702
AA
414out:
415 return ret;
86039bd3
AA
416}
417
418static int userfaultfd_release(struct inode *inode, struct file *file)
419{
420 struct userfaultfd_ctx *ctx = file->private_data;
421 struct mm_struct *mm = ctx->mm;
422 struct vm_area_struct *vma, *prev;
423 /* len == 0 means wake all */
424 struct userfaultfd_wake_range range = { .len = 0, };
425 unsigned long new_flags;
426
427 ACCESS_ONCE(ctx->released) = true;
428
429 /*
430 * Flush page faults out of all CPUs. NOTE: all page faults
431 * must be retried without returning VM_FAULT_SIGBUS if
432 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
433 * changes while handle_userfault released the mmap_sem. So
434 * it's critical that released is set to true (above), before
435 * taking the mmap_sem for writing.
436 */
437 down_write(&mm->mmap_sem);
438 prev = NULL;
439 for (vma = mm->mmap; vma; vma = vma->vm_next) {
440 cond_resched();
441 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
442 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
443 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
444 prev = vma;
445 continue;
446 }
447 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
448 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
449 new_flags, vma->anon_vma,
450 vma->vm_file, vma->vm_pgoff,
451 vma_policy(vma),
452 NULL_VM_UFFD_CTX);
453 if (prev)
454 vma = prev;
455 else
456 prev = vma;
457 vma->vm_flags = new_flags;
458 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
459 }
460 up_write(&mm->mmap_sem);
461
462 /*
15b726ef 463 * After no new page faults can wait on this fault_*wqh, flush
86039bd3 464 * the last page faults that may have been already waiting on
15b726ef 465 * the fault_*wqh.
86039bd3 466 */
15b726ef
AA
467 spin_lock(&ctx->fault_pending_wqh.lock);
468 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, 0, &range);
86039bd3 469 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, 0, &range);
15b726ef 470 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
471
472 wake_up_poll(&ctx->fd_wqh, POLLHUP);
473 userfaultfd_ctx_put(ctx);
474 return 0;
475}
476
15b726ef
AA
477/* fault_pending_wqh.lock must be hold by the caller */
478static inline struct userfaultfd_wait_queue *find_userfault(
479 struct userfaultfd_ctx *ctx)
86039bd3
AA
480{
481 wait_queue_t *wq;
15b726ef 482 struct userfaultfd_wait_queue *uwq;
86039bd3 483
15b726ef 484 VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock));
86039bd3 485
15b726ef
AA
486 uwq = NULL;
487 if (!waitqueue_active(&ctx->fault_pending_wqh))
488 goto out;
489 /* walk in reverse to provide FIFO behavior to read userfaults */
490 wq = list_last_entry(&ctx->fault_pending_wqh.task_list,
491 typeof(*wq), task_list);
492 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
493out:
494 return uwq;
86039bd3
AA
495}
496
497static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
498{
499 struct userfaultfd_ctx *ctx = file->private_data;
500 unsigned int ret;
501
502 poll_wait(file, &ctx->fd_wqh, wait);
503
504 switch (ctx->state) {
505 case UFFD_STATE_WAIT_API:
506 return POLLERR;
507 case UFFD_STATE_RUNNING:
ba85c702
AA
508 /*
509 * poll() never guarantees that read won't block.
510 * userfaults can be waken before they're read().
511 */
512 if (unlikely(!(file->f_flags & O_NONBLOCK)))
513 return POLLERR;
15b726ef
AA
514 /*
515 * lockless access to see if there are pending faults
516 * __pollwait last action is the add_wait_queue but
517 * the spin_unlock would allow the waitqueue_active to
518 * pass above the actual list_add inside
519 * add_wait_queue critical section. So use a full
520 * memory barrier to serialize the list_add write of
521 * add_wait_queue() with the waitqueue_active read
522 * below.
523 */
524 ret = 0;
525 smp_mb();
526 if (waitqueue_active(&ctx->fault_pending_wqh))
527 ret = POLLIN;
86039bd3
AA
528 return ret;
529 default:
530 BUG();
531 }
532}
533
534static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
a9b85f94 535 struct uffd_msg *msg)
86039bd3
AA
536{
537 ssize_t ret;
538 DECLARE_WAITQUEUE(wait, current);
15b726ef 539 struct userfaultfd_wait_queue *uwq;
86039bd3 540
15b726ef 541 /* always take the fd_wqh lock before the fault_pending_wqh lock */
86039bd3
AA
542 spin_lock(&ctx->fd_wqh.lock);
543 __add_wait_queue(&ctx->fd_wqh, &wait);
544 for (;;) {
545 set_current_state(TASK_INTERRUPTIBLE);
15b726ef
AA
546 spin_lock(&ctx->fault_pending_wqh.lock);
547 uwq = find_userfault(ctx);
548 if (uwq) {
86039bd3 549 /*
15b726ef
AA
550 * The fault_pending_wqh.lock prevents the uwq
551 * to disappear from under us.
552 *
553 * Refile this userfault from
554 * fault_pending_wqh to fault_wqh, it's not
555 * pending anymore after we read it.
556 *
557 * Use list_del() by hand (as
558 * userfaultfd_wake_function also uses
559 * list_del_init() by hand) to be sure nobody
560 * changes __remove_wait_queue() to use
561 * list_del_init() in turn breaking the
562 * !list_empty_careful() check in
563 * handle_userfault(). The uwq->wq.task_list
564 * must never be empty at any time during the
565 * refile, or the waitqueue could disappear
566 * from under us. The "wait_queue_head_t"
567 * parameter of __remove_wait_queue() is unused
568 * anyway.
86039bd3 569 */
15b726ef
AA
570 list_del(&uwq->wq.task_list);
571 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
572
a9b85f94
AA
573 /* careful to always initialize msg if ret == 0 */
574 *msg = uwq->msg;
15b726ef 575 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
576 ret = 0;
577 break;
578 }
15b726ef 579 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
580 if (signal_pending(current)) {
581 ret = -ERESTARTSYS;
582 break;
583 }
584 if (no_wait) {
585 ret = -EAGAIN;
586 break;
587 }
588 spin_unlock(&ctx->fd_wqh.lock);
589 schedule();
590 spin_lock(&ctx->fd_wqh.lock);
591 }
592 __remove_wait_queue(&ctx->fd_wqh, &wait);
593 __set_current_state(TASK_RUNNING);
594 spin_unlock(&ctx->fd_wqh.lock);
595
596 return ret;
597}
598
599static ssize_t userfaultfd_read(struct file *file, char __user *buf,
600 size_t count, loff_t *ppos)
601{
602 struct userfaultfd_ctx *ctx = file->private_data;
603 ssize_t _ret, ret = 0;
a9b85f94 604 struct uffd_msg msg;
86039bd3
AA
605 int no_wait = file->f_flags & O_NONBLOCK;
606
607 if (ctx->state == UFFD_STATE_WAIT_API)
608 return -EINVAL;
86039bd3
AA
609
610 for (;;) {
a9b85f94 611 if (count < sizeof(msg))
86039bd3 612 return ret ? ret : -EINVAL;
a9b85f94 613 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
86039bd3
AA
614 if (_ret < 0)
615 return ret ? ret : _ret;
a9b85f94 616 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
86039bd3 617 return ret ? ret : -EFAULT;
a9b85f94
AA
618 ret += sizeof(msg);
619 buf += sizeof(msg);
620 count -= sizeof(msg);
86039bd3
AA
621 /*
622 * Allow to read more than one fault at time but only
623 * block if waiting for the very first one.
624 */
625 no_wait = O_NONBLOCK;
626 }
627}
628
629static void __wake_userfault(struct userfaultfd_ctx *ctx,
630 struct userfaultfd_wake_range *range)
631{
632 unsigned long start, end;
633
634 start = range->start;
635 end = range->start + range->len;
636
15b726ef 637 spin_lock(&ctx->fault_pending_wqh.lock);
86039bd3 638 /* wake all in the range and autoremove */
15b726ef
AA
639 if (waitqueue_active(&ctx->fault_pending_wqh))
640 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, 0,
641 range);
642 if (waitqueue_active(&ctx->fault_wqh))
643 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, 0, range);
644 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
645}
646
647static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
648 struct userfaultfd_wake_range *range)
649{
650 /*
651 * To be sure waitqueue_active() is not reordered by the CPU
652 * before the pagetable update, use an explicit SMP memory
653 * barrier here. PT lock release or up_read(mmap_sem) still
654 * have release semantics that can allow the
655 * waitqueue_active() to be reordered before the pte update.
656 */
657 smp_mb();
658
659 /*
660 * Use waitqueue_active because it's very frequent to
661 * change the address space atomically even if there are no
662 * userfaults yet. So we take the spinlock only when we're
663 * sure we've userfaults to wake.
664 */
15b726ef
AA
665 if (waitqueue_active(&ctx->fault_pending_wqh) ||
666 waitqueue_active(&ctx->fault_wqh))
86039bd3
AA
667 __wake_userfault(ctx, range);
668}
669
670static __always_inline int validate_range(struct mm_struct *mm,
671 __u64 start, __u64 len)
672{
673 __u64 task_size = mm->task_size;
674
675 if (start & ~PAGE_MASK)
676 return -EINVAL;
677 if (len & ~PAGE_MASK)
678 return -EINVAL;
679 if (!len)
680 return -EINVAL;
681 if (start < mmap_min_addr)
682 return -EINVAL;
683 if (start >= task_size)
684 return -EINVAL;
685 if (len > task_size - start)
686 return -EINVAL;
687 return 0;
688}
689
690static int userfaultfd_register(struct userfaultfd_ctx *ctx,
691 unsigned long arg)
692{
693 struct mm_struct *mm = ctx->mm;
694 struct vm_area_struct *vma, *prev, *cur;
695 int ret;
696 struct uffdio_register uffdio_register;
697 struct uffdio_register __user *user_uffdio_register;
698 unsigned long vm_flags, new_flags;
699 bool found;
700 unsigned long start, end, vma_end;
701
702 user_uffdio_register = (struct uffdio_register __user *) arg;
703
704 ret = -EFAULT;
705 if (copy_from_user(&uffdio_register, user_uffdio_register,
706 sizeof(uffdio_register)-sizeof(__u64)))
707 goto out;
708
709 ret = -EINVAL;
710 if (!uffdio_register.mode)
711 goto out;
712 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
713 UFFDIO_REGISTER_MODE_WP))
714 goto out;
715 vm_flags = 0;
716 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
717 vm_flags |= VM_UFFD_MISSING;
718 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
719 vm_flags |= VM_UFFD_WP;
720 /*
721 * FIXME: remove the below error constraint by
722 * implementing the wprotect tracking mode.
723 */
724 ret = -EINVAL;
725 goto out;
726 }
727
728 ret = validate_range(mm, uffdio_register.range.start,
729 uffdio_register.range.len);
730 if (ret)
731 goto out;
732
733 start = uffdio_register.range.start;
734 end = start + uffdio_register.range.len;
735
736 down_write(&mm->mmap_sem);
737 vma = find_vma_prev(mm, start, &prev);
738
739 ret = -ENOMEM;
740 if (!vma)
741 goto out_unlock;
742
743 /* check that there's at least one vma in the range */
744 ret = -EINVAL;
745 if (vma->vm_start >= end)
746 goto out_unlock;
747
748 /*
749 * Search for not compatible vmas.
750 *
751 * FIXME: this shall be relaxed later so that it doesn't fail
752 * on tmpfs backed vmas (in addition to the current allowance
753 * on anonymous vmas).
754 */
755 found = false;
756 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
757 cond_resched();
758
759 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
760 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
761
762 /* check not compatible vmas */
763 ret = -EINVAL;
764 if (cur->vm_ops)
765 goto out_unlock;
766
767 /*
768 * Check that this vma isn't already owned by a
769 * different userfaultfd. We can't allow more than one
770 * userfaultfd to own a single vma simultaneously or we
771 * wouldn't know which one to deliver the userfaults to.
772 */
773 ret = -EBUSY;
774 if (cur->vm_userfaultfd_ctx.ctx &&
775 cur->vm_userfaultfd_ctx.ctx != ctx)
776 goto out_unlock;
777
778 found = true;
779 }
780 BUG_ON(!found);
781
782 if (vma->vm_start < start)
783 prev = vma;
784
785 ret = 0;
786 do {
787 cond_resched();
788
789 BUG_ON(vma->vm_ops);
790 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
791 vma->vm_userfaultfd_ctx.ctx != ctx);
792
793 /*
794 * Nothing to do: this vma is already registered into this
795 * userfaultfd and with the right tracking mode too.
796 */
797 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
798 (vma->vm_flags & vm_flags) == vm_flags)
799 goto skip;
800
801 if (vma->vm_start > start)
802 start = vma->vm_start;
803 vma_end = min(end, vma->vm_end);
804
805 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
806 prev = vma_merge(mm, prev, start, vma_end, new_flags,
807 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
808 vma_policy(vma),
809 ((struct vm_userfaultfd_ctx){ ctx }));
810 if (prev) {
811 vma = prev;
812 goto next;
813 }
814 if (vma->vm_start < start) {
815 ret = split_vma(mm, vma, start, 1);
816 if (ret)
817 break;
818 }
819 if (vma->vm_end > end) {
820 ret = split_vma(mm, vma, end, 0);
821 if (ret)
822 break;
823 }
824 next:
825 /*
826 * In the vma_merge() successful mprotect-like case 8:
827 * the next vma was merged into the current one and
828 * the current one has not been updated yet.
829 */
830 vma->vm_flags = new_flags;
831 vma->vm_userfaultfd_ctx.ctx = ctx;
832
833 skip:
834 prev = vma;
835 start = vma->vm_end;
836 vma = vma->vm_next;
837 } while (vma && vma->vm_start < end);
838out_unlock:
839 up_write(&mm->mmap_sem);
840 if (!ret) {
841 /*
842 * Now that we scanned all vmas we can already tell
843 * userland which ioctls methods are guaranteed to
844 * succeed on this range.
845 */
846 if (put_user(UFFD_API_RANGE_IOCTLS,
847 &user_uffdio_register->ioctls))
848 ret = -EFAULT;
849 }
850out:
851 return ret;
852}
853
854static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
855 unsigned long arg)
856{
857 struct mm_struct *mm = ctx->mm;
858 struct vm_area_struct *vma, *prev, *cur;
859 int ret;
860 struct uffdio_range uffdio_unregister;
861 unsigned long new_flags;
862 bool found;
863 unsigned long start, end, vma_end;
864 const void __user *buf = (void __user *)arg;
865
866 ret = -EFAULT;
867 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
868 goto out;
869
870 ret = validate_range(mm, uffdio_unregister.start,
871 uffdio_unregister.len);
872 if (ret)
873 goto out;
874
875 start = uffdio_unregister.start;
876 end = start + uffdio_unregister.len;
877
878 down_write(&mm->mmap_sem);
879 vma = find_vma_prev(mm, start, &prev);
880
881 ret = -ENOMEM;
882 if (!vma)
883 goto out_unlock;
884
885 /* check that there's at least one vma in the range */
886 ret = -EINVAL;
887 if (vma->vm_start >= end)
888 goto out_unlock;
889
890 /*
891 * Search for not compatible vmas.
892 *
893 * FIXME: this shall be relaxed later so that it doesn't fail
894 * on tmpfs backed vmas (in addition to the current allowance
895 * on anonymous vmas).
896 */
897 found = false;
898 ret = -EINVAL;
899 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
900 cond_resched();
901
902 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
903 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
904
905 /*
906 * Check not compatible vmas, not strictly required
907 * here as not compatible vmas cannot have an
908 * userfaultfd_ctx registered on them, but this
909 * provides for more strict behavior to notice
910 * unregistration errors.
911 */
912 if (cur->vm_ops)
913 goto out_unlock;
914
915 found = true;
916 }
917 BUG_ON(!found);
918
919 if (vma->vm_start < start)
920 prev = vma;
921
922 ret = 0;
923 do {
924 cond_resched();
925
926 BUG_ON(vma->vm_ops);
927
928 /*
929 * Nothing to do: this vma is already registered into this
930 * userfaultfd and with the right tracking mode too.
931 */
932 if (!vma->vm_userfaultfd_ctx.ctx)
933 goto skip;
934
935 if (vma->vm_start > start)
936 start = vma->vm_start;
937 vma_end = min(end, vma->vm_end);
938
939 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
940 prev = vma_merge(mm, prev, start, vma_end, new_flags,
941 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
942 vma_policy(vma),
943 NULL_VM_UFFD_CTX);
944 if (prev) {
945 vma = prev;
946 goto next;
947 }
948 if (vma->vm_start < start) {
949 ret = split_vma(mm, vma, start, 1);
950 if (ret)
951 break;
952 }
953 if (vma->vm_end > end) {
954 ret = split_vma(mm, vma, end, 0);
955 if (ret)
956 break;
957 }
958 next:
959 /*
960 * In the vma_merge() successful mprotect-like case 8:
961 * the next vma was merged into the current one and
962 * the current one has not been updated yet.
963 */
964 vma->vm_flags = new_flags;
965 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
966
967 skip:
968 prev = vma;
969 start = vma->vm_end;
970 vma = vma->vm_next;
971 } while (vma && vma->vm_start < end);
972out_unlock:
973 up_write(&mm->mmap_sem);
974out:
975 return ret;
976}
977
978/*
ba85c702
AA
979 * userfaultfd_wake may be used in combination with the
980 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
86039bd3
AA
981 */
982static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
983 unsigned long arg)
984{
985 int ret;
986 struct uffdio_range uffdio_wake;
987 struct userfaultfd_wake_range range;
988 const void __user *buf = (void __user *)arg;
989
990 ret = -EFAULT;
991 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
992 goto out;
993
994 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
995 if (ret)
996 goto out;
997
998 range.start = uffdio_wake.start;
999 range.len = uffdio_wake.len;
1000
1001 /*
1002 * len == 0 means wake all and we don't want to wake all here,
1003 * so check it again to be sure.
1004 */
1005 VM_BUG_ON(!range.len);
1006
1007 wake_userfault(ctx, &range);
1008 ret = 0;
1009
1010out:
1011 return ret;
1012}
1013
ad465cae
AA
1014static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1015 unsigned long arg)
1016{
1017 __s64 ret;
1018 struct uffdio_copy uffdio_copy;
1019 struct uffdio_copy __user *user_uffdio_copy;
1020 struct userfaultfd_wake_range range;
1021
1022 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1023
1024 ret = -EFAULT;
1025 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1026 /* don't copy "copy" last field */
1027 sizeof(uffdio_copy)-sizeof(__s64)))
1028 goto out;
1029
1030 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1031 if (ret)
1032 goto out;
1033 /*
1034 * double check for wraparound just in case. copy_from_user()
1035 * will later check uffdio_copy.src + uffdio_copy.len to fit
1036 * in the userland range.
1037 */
1038 ret = -EINVAL;
1039 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1040 goto out;
1041 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1042 goto out;
1043
1044 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1045 uffdio_copy.len);
1046 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1047 return -EFAULT;
1048 if (ret < 0)
1049 goto out;
1050 BUG_ON(!ret);
1051 /* len == 0 would wake all */
1052 range.len = ret;
1053 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1054 range.start = uffdio_copy.dst;
1055 wake_userfault(ctx, &range);
1056 }
1057 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1058out:
1059 return ret;
1060}
1061
1062static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1063 unsigned long arg)
1064{
1065 __s64 ret;
1066 struct uffdio_zeropage uffdio_zeropage;
1067 struct uffdio_zeropage __user *user_uffdio_zeropage;
1068 struct userfaultfd_wake_range range;
1069
1070 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1071
1072 ret = -EFAULT;
1073 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1074 /* don't copy "zeropage" last field */
1075 sizeof(uffdio_zeropage)-sizeof(__s64)))
1076 goto out;
1077
1078 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1079 uffdio_zeropage.range.len);
1080 if (ret)
1081 goto out;
1082 ret = -EINVAL;
1083 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1084 goto out;
1085
1086 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1087 uffdio_zeropage.range.len);
1088 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1089 return -EFAULT;
1090 if (ret < 0)
1091 goto out;
1092 /* len == 0 would wake all */
1093 BUG_ON(!ret);
1094 range.len = ret;
1095 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1096 range.start = uffdio_zeropage.range.start;
1097 wake_userfault(ctx, &range);
1098 }
1099 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1100out:
1101 return ret;
1102}
1103
86039bd3
AA
1104/*
1105 * userland asks for a certain API version and we return which bits
1106 * and ioctl commands are implemented in this kernel for such API
1107 * version or -EINVAL if unknown.
1108 */
1109static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1110 unsigned long arg)
1111{
1112 struct uffdio_api uffdio_api;
1113 void __user *buf = (void __user *)arg;
1114 int ret;
1115
1116 ret = -EINVAL;
1117 if (ctx->state != UFFD_STATE_WAIT_API)
1118 goto out;
1119 ret = -EFAULT;
a9b85f94 1120 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
86039bd3 1121 goto out;
a9b85f94 1122 if (uffdio_api.api != UFFD_API || uffdio_api.features) {
86039bd3
AA
1123 memset(&uffdio_api, 0, sizeof(uffdio_api));
1124 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1125 goto out;
1126 ret = -EINVAL;
1127 goto out;
1128 }
3f602d27 1129 uffdio_api.features = UFFD_API_FEATURES;
86039bd3
AA
1130 uffdio_api.ioctls = UFFD_API_IOCTLS;
1131 ret = -EFAULT;
1132 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1133 goto out;
1134 ctx->state = UFFD_STATE_RUNNING;
1135 ret = 0;
1136out:
1137 return ret;
1138}
1139
1140static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1141 unsigned long arg)
1142{
1143 int ret = -EINVAL;
1144 struct userfaultfd_ctx *ctx = file->private_data;
1145
e6485a47
AA
1146 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1147 return -EINVAL;
1148
86039bd3
AA
1149 switch(cmd) {
1150 case UFFDIO_API:
1151 ret = userfaultfd_api(ctx, arg);
1152 break;
1153 case UFFDIO_REGISTER:
1154 ret = userfaultfd_register(ctx, arg);
1155 break;
1156 case UFFDIO_UNREGISTER:
1157 ret = userfaultfd_unregister(ctx, arg);
1158 break;
1159 case UFFDIO_WAKE:
1160 ret = userfaultfd_wake(ctx, arg);
1161 break;
ad465cae
AA
1162 case UFFDIO_COPY:
1163 ret = userfaultfd_copy(ctx, arg);
1164 break;
1165 case UFFDIO_ZEROPAGE:
1166 ret = userfaultfd_zeropage(ctx, arg);
1167 break;
86039bd3
AA
1168 }
1169 return ret;
1170}
1171
1172#ifdef CONFIG_PROC_FS
1173static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1174{
1175 struct userfaultfd_ctx *ctx = f->private_data;
1176 wait_queue_t *wq;
1177 struct userfaultfd_wait_queue *uwq;
1178 unsigned long pending = 0, total = 0;
1179
15b726ef
AA
1180 spin_lock(&ctx->fault_pending_wqh.lock);
1181 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
1182 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1183 pending++;
1184 total++;
1185 }
86039bd3
AA
1186 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1187 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
86039bd3
AA
1188 total++;
1189 }
15b726ef 1190 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
1191
1192 /*
1193 * If more protocols will be added, there will be all shown
1194 * separated by a space. Like this:
1195 * protocols: aa:... bb:...
1196 */
1197 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
3f602d27 1198 pending, total, UFFD_API, UFFD_API_FEATURES,
86039bd3
AA
1199 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1200}
1201#endif
1202
1203static const struct file_operations userfaultfd_fops = {
1204#ifdef CONFIG_PROC_FS
1205 .show_fdinfo = userfaultfd_show_fdinfo,
1206#endif
1207 .release = userfaultfd_release,
1208 .poll = userfaultfd_poll,
1209 .read = userfaultfd_read,
1210 .unlocked_ioctl = userfaultfd_ioctl,
1211 .compat_ioctl = userfaultfd_ioctl,
1212 .llseek = noop_llseek,
1213};
1214
3004ec9c
AA
1215static void init_once_userfaultfd_ctx(void *mem)
1216{
1217 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1218
1219 init_waitqueue_head(&ctx->fault_pending_wqh);
1220 init_waitqueue_head(&ctx->fault_wqh);
1221 init_waitqueue_head(&ctx->fd_wqh);
1222}
1223
86039bd3
AA
1224/**
1225 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1226 * @flags: Flags for the userfaultfd file.
1227 *
1228 * This function creates an userfaultfd file pointer, w/out installing
1229 * it into the fd table. This is useful when the userfaultfd file is
1230 * used during the initialization of data structures that require
1231 * extra setup after the userfaultfd creation. So the userfaultfd
1232 * creation is split into the file pointer creation phase, and the
1233 * file descriptor installation phase. In this way races with
1234 * userspace closing the newly installed file descriptor can be
1235 * avoided. Returns an userfaultfd file pointer, or a proper error
1236 * pointer.
1237 */
1238static struct file *userfaultfd_file_create(int flags)
1239{
1240 struct file *file;
1241 struct userfaultfd_ctx *ctx;
1242
1243 BUG_ON(!current->mm);
1244
1245 /* Check the UFFD_* constants for consistency. */
1246 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1247 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1248
1249 file = ERR_PTR(-EINVAL);
1250 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1251 goto out;
1252
1253 file = ERR_PTR(-ENOMEM);
3004ec9c 1254 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
86039bd3
AA
1255 if (!ctx)
1256 goto out;
1257
1258 atomic_set(&ctx->refcount, 1);
86039bd3
AA
1259 ctx->flags = flags;
1260 ctx->state = UFFD_STATE_WAIT_API;
1261 ctx->released = false;
1262 ctx->mm = current->mm;
1263 /* prevent the mm struct to be freed */
1264 atomic_inc(&ctx->mm->mm_users);
1265
1266 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1267 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1268 if (IS_ERR(file))
3004ec9c 1269 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
86039bd3
AA
1270out:
1271 return file;
1272}
1273
1274SYSCALL_DEFINE1(userfaultfd, int, flags)
1275{
1276 int fd, error;
1277 struct file *file;
1278
1279 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1280 if (error < 0)
1281 return error;
1282 fd = error;
1283
1284 file = userfaultfd_file_create(flags);
1285 if (IS_ERR(file)) {
1286 error = PTR_ERR(file);
1287 goto err_put_unused_fd;
1288 }
1289 fd_install(fd, file);
1290
1291 return fd;
1292
1293err_put_unused_fd:
1294 put_unused_fd(fd);
1295
1296 return error;
1297}
3004ec9c
AA
1298
1299static int __init userfaultfd_init(void)
1300{
1301 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1302 sizeof(struct userfaultfd_ctx),
1303 0,
1304 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1305 init_once_userfaultfd_ctx);
1306 return 0;
1307}
1308__initcall(userfaultfd_init);