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