blob: 69c867c01d524e88a6b7b7f8a9e7b46b8158c4e1 [file] [log] [blame]
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001/*
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
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070030static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
31
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070032enum userfaultfd_state {
33 UFFD_STATE_WAIT_API,
34 UFFD_STATE_RUNNING,
35};
36
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070037/*
38 * Start with fault_pending_wqh and fault_wqh so they're more likely
39 * to be in the same cacheline.
40 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070041struct userfaultfd_ctx {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -070042 /* waitqueue head for the pending (i.e. not read) userfaults */
43 wait_queue_head_t fault_pending_wqh;
44 /* waitqueue head for the userfaults */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070045 wait_queue_head_t fault_wqh;
46 /* waitqueue head for the pseudo fd to wakeup poll/read */
47 wait_queue_head_t fd_wqh;
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -070048 /* a refile sequence protected by fault_pending_wqh lock */
49 struct seqcount refile_seq;
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070050 /* pseudo fd refcounting */
51 atomic_t refcount;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070052 /* userfaultfd syscall flags */
53 unsigned int flags;
54 /* state machine */
55 enum userfaultfd_state state;
56 /* released */
57 bool released;
58 /* mm with one ore more vmas attached to this userfaultfd_ctx */
59 struct mm_struct *mm;
60};
61
62struct userfaultfd_wait_queue {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070063 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070064 wait_queue_t wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070065 struct userfaultfd_ctx *ctx;
66};
67
68struct userfaultfd_wake_range {
69 unsigned long start;
70 unsigned long len;
71};
72
73static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
74 int wake_flags, void *key)
75{
76 struct userfaultfd_wake_range *range = key;
77 int ret;
78 struct userfaultfd_wait_queue *uwq;
79 unsigned long start, len;
80
81 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
82 ret = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070083 /* len == 0 means wake all */
84 start = range->start;
85 len = range->len;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070086 if (len && (start > uwq->msg.arg.pagefault.address ||
87 start + len <= uwq->msg.arg.pagefault.address))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070088 goto out;
89 ret = wake_up_state(wq->private, mode);
90 if (ret)
91 /*
92 * Wake only once, autoremove behavior.
93 *
94 * After the effect of list_del_init is visible to the
95 * other CPUs, the waitqueue may disappear from under
96 * us, see the !list_empty_careful() in
97 * handle_userfault(). try_to_wake_up() has an
98 * implicit smp_mb__before_spinlock, and the
99 * wq->private is read before calling the extern
100 * function "wake_up_state" (which in turns calls
101 * try_to_wake_up). While the spin_lock;spin_unlock;
102 * wouldn't be enough, the smp_mb__before_spinlock is
103 * enough to avoid an explicit smp_mb() here.
104 */
105 list_del_init(&wq->task_list);
106out:
107 return ret;
108}
109
110/**
111 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
112 * context.
113 * @ctx: [in] Pointer to the userfaultfd context.
114 *
115 * Returns: In case of success, returns not zero.
116 */
117static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
118{
119 if (!atomic_inc_not_zero(&ctx->refcount))
120 BUG();
121}
122
123/**
124 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
125 * context.
126 * @ctx: [in] Pointer to userfaultfd context.
127 *
128 * The userfaultfd context reference must have been previously acquired either
129 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
130 */
131static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
132{
133 if (atomic_dec_and_test(&ctx->refcount)) {
134 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
135 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
136 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
137 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
138 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
139 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700140 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700141 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700142 }
143}
144
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700145static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700146{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700147 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
148 /*
149 * Must use memset to zero out the paddings or kernel data is
150 * leaked to userland.
151 */
152 memset(msg, 0, sizeof(struct uffd_msg));
153}
154
155static inline struct uffd_msg userfault_msg(unsigned long address,
156 unsigned int flags,
157 unsigned long reason)
158{
159 struct uffd_msg msg;
160 msg_init(&msg);
161 msg.event = UFFD_EVENT_PAGEFAULT;
162 msg.arg.pagefault.address = address;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700163 if (flags & FAULT_FLAG_WRITE)
164 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700165 * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
166 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
167 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
168 * was a read fault, otherwise if set it means it's
169 * a write fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700170 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700171 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700172 if (reason & VM_UFFD_WP)
173 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700174 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
175 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
176 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
177 * a missing fault, otherwise if set it means it's a
178 * write protect fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700179 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700180 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
181 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700182}
183
184/*
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700185 * Verify the pagetables are still not ok after having reigstered into
186 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
187 * userfault that has already been resolved, if userfaultfd_read and
188 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
189 * threads.
190 */
191static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
192 unsigned long address,
193 unsigned long flags,
194 unsigned long reason)
195{
196 struct mm_struct *mm = ctx->mm;
197 pgd_t *pgd;
198 pud_t *pud;
199 pmd_t *pmd, _pmd;
200 pte_t *pte;
201 bool ret = true;
202
203 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
204
205 pgd = pgd_offset(mm, address);
206 if (!pgd_present(*pgd))
207 goto out;
208 pud = pud_offset(pgd, address);
209 if (!pud_present(*pud))
210 goto out;
211 pmd = pmd_offset(pud, address);
212 /*
213 * READ_ONCE must function as a barrier with narrower scope
214 * and it must be equivalent to:
215 * _pmd = *pmd; barrier();
216 *
217 * This is to deal with the instability (as in
218 * pmd_trans_unstable) of the pmd.
219 */
220 _pmd = READ_ONCE(*pmd);
221 if (!pmd_present(_pmd))
222 goto out;
223
224 ret = false;
225 if (pmd_trans_huge(_pmd))
226 goto out;
227
228 /*
229 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
230 * and use the standard pte_offset_map() instead of parsing _pmd.
231 */
232 pte = pte_offset_map(pmd, address);
233 /*
234 * Lockless access: we're in a wait_event so it's ok if it
235 * changes under us.
236 */
237 if (pte_none(*pte))
238 ret = true;
239 pte_unmap(pte);
240
241out:
242 return ret;
243}
244
245/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700246 * The locking rules involved in returning VM_FAULT_RETRY depending on
247 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
248 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
249 * recommendation in __lock_page_or_retry is not an understatement.
250 *
251 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
252 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
253 * not set.
254 *
255 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
256 * set, VM_FAULT_RETRY can still be returned if and only if there are
257 * fatal_signal_pending()s, and the mmap_sem must be released before
258 * returning it.
259 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700260int handle_userfault(struct fault_env *fe, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700261{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700262 struct mm_struct *mm = fe->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700263 struct userfaultfd_ctx *ctx;
264 struct userfaultfd_wait_queue uwq;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700265 int ret;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700266 bool must_wait, return_to_userland;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700267
268 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
269
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700270 ret = VM_FAULT_SIGBUS;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700271 ctx = fe->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700272 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700273 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700274
275 BUG_ON(ctx->mm != mm);
276
277 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
278 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
279
280 /*
281 * If it's already released don't get it. This avoids to loop
282 * in __get_user_pages if userfaultfd_release waits on the
283 * caller of handle_userfault to release the mmap_sem.
284 */
285 if (unlikely(ACCESS_ONCE(ctx->released)))
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700286 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700287
288 /*
Linus Torvalds39680f52016-03-01 11:56:22 -0800289 * We don't do userfault handling for the final child pid update.
290 */
291 if (current->flags & PF_EXITING)
292 goto out;
293
294 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700295 * Check that we can return VM_FAULT_RETRY.
296 *
297 * NOTE: it should become possible to return VM_FAULT_RETRY
298 * even if FAULT_FLAG_TRIED is set without leading to gup()
299 * -EBUSY failures, if the userfaultfd is to be extended for
300 * VM_UFFD_WP tracking and we intend to arm the userfault
301 * without first stopping userland access to the memory. For
302 * VM_UFFD_MISSING userfaults this is enough for now.
303 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700304 if (unlikely(!(fe->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700305 /*
306 * Validate the invariant that nowait must allow retry
307 * to be sure not to return SIGBUS erroneously on
308 * nowait invocations.
309 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700310 BUG_ON(fe->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700311#ifdef CONFIG_DEBUG_VM
312 if (printk_ratelimit()) {
313 printk(KERN_WARNING
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700314 "FAULT_FLAG_ALLOW_RETRY missing %x\n", fe->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700315 dump_stack();
316 }
317#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700318 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700319 }
320
321 /*
322 * Handle nowait, not much to do other than tell it to retry
323 * and wait.
324 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700325 ret = VM_FAULT_RETRY;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700326 if (fe->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700327 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700328
329 /* take the reference before dropping the mmap_sem */
330 userfaultfd_ctx_get(ctx);
331
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700332 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
333 uwq.wq.private = current;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700334 uwq.msg = userfault_msg(fe->address, fe->flags, reason);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700335 uwq.ctx = ctx;
336
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700337 return_to_userland =
338 (fe->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700339 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
340
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700341 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700342 /*
343 * After the __add_wait_queue the uwq is visible to userland
344 * through poll/read().
345 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700346 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
347 /*
348 * The smp_mb() after __set_current_state prevents the reads
349 * following the spin_unlock to happen before the list_add in
350 * __add_wait_queue.
351 */
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700352 set_current_state(return_to_userland ? TASK_INTERRUPTIBLE :
353 TASK_KILLABLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700354 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700355
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700356 must_wait = userfaultfd_must_wait(ctx, fe->address, fe->flags, reason);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700357 up_read(&mm->mmap_sem);
358
359 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700360 (return_to_userland ? !signal_pending(current) :
361 !fatal_signal_pending(current)))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700362 wake_up_poll(&ctx->fd_wqh, POLLIN);
363 schedule();
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700364 ret |= VM_FAULT_MAJOR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700365 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700366
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700367 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700368
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700369 if (return_to_userland) {
370 if (signal_pending(current) &&
371 !fatal_signal_pending(current)) {
372 /*
373 * If we got a SIGSTOP or SIGCONT and this is
374 * a normal userland page fault, just let
375 * userland return so the signal will be
376 * handled and gdb debugging works. The page
377 * fault code immediately after we return from
378 * this function is going to release the
379 * mmap_sem and it's not depending on it
380 * (unlike gup would if we were not to return
381 * VM_FAULT_RETRY).
382 *
383 * If a fatal signal is pending we still take
384 * the streamlined VM_FAULT_RETRY failure path
385 * and there's no need to retake the mmap_sem
386 * in such case.
387 */
388 down_read(&mm->mmap_sem);
389 ret = 0;
390 }
391 }
392
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700393 /*
394 * Here we race with the list_del; list_add in
395 * userfaultfd_ctx_read(), however because we don't ever run
396 * list_del_init() to refile across the two lists, the prev
397 * and next pointers will never point to self. list_add also
398 * would never let any of the two pointers to point to
399 * self. So list_empty_careful won't risk to see both pointers
400 * pointing to self at any time during the list refile. The
401 * only case where list_del_init() is called is the full
402 * removal in the wake function and there we don't re-list_add
403 * and it's fine not to block on the spinlock. The uwq on this
404 * kernel stack can be released after the list_del_init.
405 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700406 if (!list_empty_careful(&uwq.wq.task_list)) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700407 spin_lock(&ctx->fault_pending_wqh.lock);
408 /*
409 * No need of list_del_init(), the uwq on the stack
410 * will be freed shortly anyway.
411 */
412 list_del(&uwq.wq.task_list);
413 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700414 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700415
416 /*
417 * ctx may go away after this if the userfault pseudo fd is
418 * already released.
419 */
420 userfaultfd_ctx_put(ctx);
421
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700422out:
423 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700424}
425
426static int userfaultfd_release(struct inode *inode, struct file *file)
427{
428 struct userfaultfd_ctx *ctx = file->private_data;
429 struct mm_struct *mm = ctx->mm;
430 struct vm_area_struct *vma, *prev;
431 /* len == 0 means wake all */
432 struct userfaultfd_wake_range range = { .len = 0, };
433 unsigned long new_flags;
434
435 ACCESS_ONCE(ctx->released) = true;
436
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700437 if (!mmget_not_zero(mm))
438 goto wakeup;
439
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700440 /*
441 * Flush page faults out of all CPUs. NOTE: all page faults
442 * must be retried without returning VM_FAULT_SIGBUS if
443 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
444 * changes while handle_userfault released the mmap_sem. So
445 * it's critical that released is set to true (above), before
446 * taking the mmap_sem for writing.
447 */
448 down_write(&mm->mmap_sem);
449 prev = NULL;
450 for (vma = mm->mmap; vma; vma = vma->vm_next) {
451 cond_resched();
452 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
453 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
454 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
455 prev = vma;
456 continue;
457 }
458 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
459 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
460 new_flags, vma->anon_vma,
461 vma->vm_file, vma->vm_pgoff,
462 vma_policy(vma),
Colin Cross3e4578f2015-10-27 16:42:08 -0700463 NULL_VM_UFFD_CTX,
464 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700465 if (prev)
466 vma = prev;
467 else
468 prev = vma;
469 vma->vm_flags = new_flags;
470 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
471 }
472 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700473 mmput(mm);
474wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700475 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700476 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700477 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700478 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700479 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700480 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700481 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
482 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700483 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700484
485 wake_up_poll(&ctx->fd_wqh, POLLHUP);
486 userfaultfd_ctx_put(ctx);
487 return 0;
488}
489
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700490/* fault_pending_wqh.lock must be hold by the caller */
491static inline struct userfaultfd_wait_queue *find_userfault(
492 struct userfaultfd_ctx *ctx)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700493{
494 wait_queue_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700495 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700496
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700497 VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700498
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700499 uwq = NULL;
500 if (!waitqueue_active(&ctx->fault_pending_wqh))
501 goto out;
502 /* walk in reverse to provide FIFO behavior to read userfaults */
503 wq = list_last_entry(&ctx->fault_pending_wqh.task_list,
504 typeof(*wq), task_list);
505 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
506out:
507 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700508}
509
510static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
511{
512 struct userfaultfd_ctx *ctx = file->private_data;
513 unsigned int ret;
514
515 poll_wait(file, &ctx->fd_wqh, wait);
516
517 switch (ctx->state) {
518 case UFFD_STATE_WAIT_API:
519 return POLLERR;
520 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700521 /*
522 * poll() never guarantees that read won't block.
523 * userfaults can be waken before they're read().
524 */
525 if (unlikely(!(file->f_flags & O_NONBLOCK)))
526 return POLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700527 /*
528 * lockless access to see if there are pending faults
529 * __pollwait last action is the add_wait_queue but
530 * the spin_unlock would allow the waitqueue_active to
531 * pass above the actual list_add inside
532 * add_wait_queue critical section. So use a full
533 * memory barrier to serialize the list_add write of
534 * add_wait_queue() with the waitqueue_active read
535 * below.
536 */
537 ret = 0;
538 smp_mb();
539 if (waitqueue_active(&ctx->fault_pending_wqh))
540 ret = POLLIN;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700541 return ret;
542 default:
543 BUG();
544 }
545}
546
547static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700548 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700549{
550 ssize_t ret;
551 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700552 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700553
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700554 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700555 spin_lock(&ctx->fd_wqh.lock);
556 __add_wait_queue(&ctx->fd_wqh, &wait);
557 for (;;) {
558 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700559 spin_lock(&ctx->fault_pending_wqh.lock);
560 uwq = find_userfault(ctx);
561 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700562 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700563 * Use a seqcount to repeat the lockless check
564 * in wake_userfault() to avoid missing
565 * wakeups because during the refile both
566 * waitqueue could become empty if this is the
567 * only userfault.
568 */
569 write_seqcount_begin(&ctx->refile_seq);
570
571 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700572 * The fault_pending_wqh.lock prevents the uwq
573 * to disappear from under us.
574 *
575 * Refile this userfault from
576 * fault_pending_wqh to fault_wqh, it's not
577 * pending anymore after we read it.
578 *
579 * Use list_del() by hand (as
580 * userfaultfd_wake_function also uses
581 * list_del_init() by hand) to be sure nobody
582 * changes __remove_wait_queue() to use
583 * list_del_init() in turn breaking the
584 * !list_empty_careful() check in
585 * handle_userfault(). The uwq->wq.task_list
586 * must never be empty at any time during the
587 * refile, or the waitqueue could disappear
588 * from under us. The "wait_queue_head_t"
589 * parameter of __remove_wait_queue() is unused
590 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700591 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700592 list_del(&uwq->wq.task_list);
593 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
594
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700595 write_seqcount_end(&ctx->refile_seq);
596
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700597 /* careful to always initialize msg if ret == 0 */
598 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700599 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700600 ret = 0;
601 break;
602 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700603 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700604 if (signal_pending(current)) {
605 ret = -ERESTARTSYS;
606 break;
607 }
608 if (no_wait) {
609 ret = -EAGAIN;
610 break;
611 }
612 spin_unlock(&ctx->fd_wqh.lock);
613 schedule();
614 spin_lock(&ctx->fd_wqh.lock);
615 }
616 __remove_wait_queue(&ctx->fd_wqh, &wait);
617 __set_current_state(TASK_RUNNING);
618 spin_unlock(&ctx->fd_wqh.lock);
619
620 return ret;
621}
622
623static ssize_t userfaultfd_read(struct file *file, char __user *buf,
624 size_t count, loff_t *ppos)
625{
626 struct userfaultfd_ctx *ctx = file->private_data;
627 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700628 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700629 int no_wait = file->f_flags & O_NONBLOCK;
630
631 if (ctx->state == UFFD_STATE_WAIT_API)
632 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700633
634 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700635 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700636 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700637 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700638 if (_ret < 0)
639 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700640 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700641 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700642 ret += sizeof(msg);
643 buf += sizeof(msg);
644 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700645 /*
646 * Allow to read more than one fault at time but only
647 * block if waiting for the very first one.
648 */
649 no_wait = O_NONBLOCK;
650 }
651}
652
653static void __wake_userfault(struct userfaultfd_ctx *ctx,
654 struct userfaultfd_wake_range *range)
655{
656 unsigned long start, end;
657
658 start = range->start;
659 end = range->start + range->len;
660
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700661 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700662 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700663 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700664 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700665 range);
666 if (waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700667 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700668 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700669}
670
671static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
672 struct userfaultfd_wake_range *range)
673{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700674 unsigned seq;
675 bool need_wakeup;
676
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700677 /*
678 * To be sure waitqueue_active() is not reordered by the CPU
679 * before the pagetable update, use an explicit SMP memory
680 * barrier here. PT lock release or up_read(mmap_sem) still
681 * have release semantics that can allow the
682 * waitqueue_active() to be reordered before the pte update.
683 */
684 smp_mb();
685
686 /*
687 * Use waitqueue_active because it's very frequent to
688 * change the address space atomically even if there are no
689 * userfaults yet. So we take the spinlock only when we're
690 * sure we've userfaults to wake.
691 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700692 do {
693 seq = read_seqcount_begin(&ctx->refile_seq);
694 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
695 waitqueue_active(&ctx->fault_wqh);
696 cond_resched();
697 } while (read_seqcount_retry(&ctx->refile_seq, seq));
698 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700699 __wake_userfault(ctx, range);
700}
701
702static __always_inline int validate_range(struct mm_struct *mm,
703 __u64 start, __u64 len)
704{
705 __u64 task_size = mm->task_size;
706
707 if (start & ~PAGE_MASK)
708 return -EINVAL;
709 if (len & ~PAGE_MASK)
710 return -EINVAL;
711 if (!len)
712 return -EINVAL;
713 if (start < mmap_min_addr)
714 return -EINVAL;
715 if (start >= task_size)
716 return -EINVAL;
717 if (len > task_size - start)
718 return -EINVAL;
719 return 0;
720}
721
722static int userfaultfd_register(struct userfaultfd_ctx *ctx,
723 unsigned long arg)
724{
725 struct mm_struct *mm = ctx->mm;
726 struct vm_area_struct *vma, *prev, *cur;
727 int ret;
728 struct uffdio_register uffdio_register;
729 struct uffdio_register __user *user_uffdio_register;
730 unsigned long vm_flags, new_flags;
731 bool found;
732 unsigned long start, end, vma_end;
733
734 user_uffdio_register = (struct uffdio_register __user *) arg;
735
736 ret = -EFAULT;
737 if (copy_from_user(&uffdio_register, user_uffdio_register,
738 sizeof(uffdio_register)-sizeof(__u64)))
739 goto out;
740
741 ret = -EINVAL;
742 if (!uffdio_register.mode)
743 goto out;
744 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
745 UFFDIO_REGISTER_MODE_WP))
746 goto out;
747 vm_flags = 0;
748 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
749 vm_flags |= VM_UFFD_MISSING;
750 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
751 vm_flags |= VM_UFFD_WP;
752 /*
753 * FIXME: remove the below error constraint by
754 * implementing the wprotect tracking mode.
755 */
756 ret = -EINVAL;
757 goto out;
758 }
759
760 ret = validate_range(mm, uffdio_register.range.start,
761 uffdio_register.range.len);
762 if (ret)
763 goto out;
764
765 start = uffdio_register.range.start;
766 end = start + uffdio_register.range.len;
767
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700768 ret = -ENOMEM;
769 if (!mmget_not_zero(mm))
770 goto out;
771
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700772 down_write(&mm->mmap_sem);
773 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700774 if (!vma)
775 goto out_unlock;
776
777 /* check that there's at least one vma in the range */
778 ret = -EINVAL;
779 if (vma->vm_start >= end)
780 goto out_unlock;
781
782 /*
783 * Search for not compatible vmas.
784 *
785 * FIXME: this shall be relaxed later so that it doesn't fail
786 * on tmpfs backed vmas (in addition to the current allowance
787 * on anonymous vmas).
788 */
789 found = false;
790 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
791 cond_resched();
792
793 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
794 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
795
796 /* check not compatible vmas */
797 ret = -EINVAL;
798 if (cur->vm_ops)
799 goto out_unlock;
800
801 /*
802 * Check that this vma isn't already owned by a
803 * different userfaultfd. We can't allow more than one
804 * userfaultfd to own a single vma simultaneously or we
805 * wouldn't know which one to deliver the userfaults to.
806 */
807 ret = -EBUSY;
808 if (cur->vm_userfaultfd_ctx.ctx &&
809 cur->vm_userfaultfd_ctx.ctx != ctx)
810 goto out_unlock;
811
812 found = true;
813 }
814 BUG_ON(!found);
815
816 if (vma->vm_start < start)
817 prev = vma;
818
819 ret = 0;
820 do {
821 cond_resched();
822
823 BUG_ON(vma->vm_ops);
824 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
825 vma->vm_userfaultfd_ctx.ctx != ctx);
826
827 /*
828 * Nothing to do: this vma is already registered into this
829 * userfaultfd and with the right tracking mode too.
830 */
831 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
832 (vma->vm_flags & vm_flags) == vm_flags)
833 goto skip;
834
835 if (vma->vm_start > start)
836 start = vma->vm_start;
837 vma_end = min(end, vma->vm_end);
838
839 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
840 prev = vma_merge(mm, prev, start, vma_end, new_flags,
841 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
842 vma_policy(vma),
Colin Cross3e4578f2015-10-27 16:42:08 -0700843 ((struct vm_userfaultfd_ctx){ ctx }),
844 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700845 if (prev) {
846 vma = prev;
847 goto next;
848 }
849 if (vma->vm_start < start) {
850 ret = split_vma(mm, vma, start, 1);
851 if (ret)
852 break;
853 }
854 if (vma->vm_end > end) {
855 ret = split_vma(mm, vma, end, 0);
856 if (ret)
857 break;
858 }
859 next:
860 /*
861 * In the vma_merge() successful mprotect-like case 8:
862 * the next vma was merged into the current one and
863 * the current one has not been updated yet.
864 */
865 vma->vm_flags = new_flags;
866 vma->vm_userfaultfd_ctx.ctx = ctx;
867
868 skip:
869 prev = vma;
870 start = vma->vm_end;
871 vma = vma->vm_next;
872 } while (vma && vma->vm_start < end);
873out_unlock:
874 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700875 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700876 if (!ret) {
877 /*
878 * Now that we scanned all vmas we can already tell
879 * userland which ioctls methods are guaranteed to
880 * succeed on this range.
881 */
882 if (put_user(UFFD_API_RANGE_IOCTLS,
883 &user_uffdio_register->ioctls))
884 ret = -EFAULT;
885 }
886out:
887 return ret;
888}
889
890static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
891 unsigned long arg)
892{
893 struct mm_struct *mm = ctx->mm;
894 struct vm_area_struct *vma, *prev, *cur;
895 int ret;
896 struct uffdio_range uffdio_unregister;
897 unsigned long new_flags;
898 bool found;
899 unsigned long start, end, vma_end;
900 const void __user *buf = (void __user *)arg;
901
902 ret = -EFAULT;
903 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
904 goto out;
905
906 ret = validate_range(mm, uffdio_unregister.start,
907 uffdio_unregister.len);
908 if (ret)
909 goto out;
910
911 start = uffdio_unregister.start;
912 end = start + uffdio_unregister.len;
913
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700914 ret = -ENOMEM;
915 if (!mmget_not_zero(mm))
916 goto out;
917
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700918 down_write(&mm->mmap_sem);
919 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700920 if (!vma)
921 goto out_unlock;
922
923 /* check that there's at least one vma in the range */
924 ret = -EINVAL;
925 if (vma->vm_start >= end)
926 goto out_unlock;
927
928 /*
929 * Search for not compatible vmas.
930 *
931 * FIXME: this shall be relaxed later so that it doesn't fail
932 * on tmpfs backed vmas (in addition to the current allowance
933 * on anonymous vmas).
934 */
935 found = false;
936 ret = -EINVAL;
937 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
938 cond_resched();
939
940 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
941 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
942
943 /*
944 * Check not compatible vmas, not strictly required
945 * here as not compatible vmas cannot have an
946 * userfaultfd_ctx registered on them, but this
947 * provides for more strict behavior to notice
948 * unregistration errors.
949 */
950 if (cur->vm_ops)
951 goto out_unlock;
952
953 found = true;
954 }
955 BUG_ON(!found);
956
957 if (vma->vm_start < start)
958 prev = vma;
959
960 ret = 0;
961 do {
962 cond_resched();
963
964 BUG_ON(vma->vm_ops);
965
966 /*
967 * Nothing to do: this vma is already registered into this
968 * userfaultfd and with the right tracking mode too.
969 */
970 if (!vma->vm_userfaultfd_ctx.ctx)
971 goto skip;
972
973 if (vma->vm_start > start)
974 start = vma->vm_start;
975 vma_end = min(end, vma->vm_end);
976
977 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
978 prev = vma_merge(mm, prev, start, vma_end, new_flags,
979 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
980 vma_policy(vma),
Colin Cross3e4578f2015-10-27 16:42:08 -0700981 NULL_VM_UFFD_CTX,
982 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700983 if (prev) {
984 vma = prev;
985 goto next;
986 }
987 if (vma->vm_start < start) {
988 ret = split_vma(mm, vma, start, 1);
989 if (ret)
990 break;
991 }
992 if (vma->vm_end > end) {
993 ret = split_vma(mm, vma, end, 0);
994 if (ret)
995 break;
996 }
997 next:
998 /*
999 * In the vma_merge() successful mprotect-like case 8:
1000 * the next vma was merged into the current one and
1001 * the current one has not been updated yet.
1002 */
1003 vma->vm_flags = new_flags;
1004 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1005
1006 skip:
1007 prev = vma;
1008 start = vma->vm_end;
1009 vma = vma->vm_next;
1010 } while (vma && vma->vm_start < end);
1011out_unlock:
1012 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001013 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001014out:
1015 return ret;
1016}
1017
1018/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001019 * userfaultfd_wake may be used in combination with the
1020 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001021 */
1022static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1023 unsigned long arg)
1024{
1025 int ret;
1026 struct uffdio_range uffdio_wake;
1027 struct userfaultfd_wake_range range;
1028 const void __user *buf = (void __user *)arg;
1029
1030 ret = -EFAULT;
1031 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1032 goto out;
1033
1034 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1035 if (ret)
1036 goto out;
1037
1038 range.start = uffdio_wake.start;
1039 range.len = uffdio_wake.len;
1040
1041 /*
1042 * len == 0 means wake all and we don't want to wake all here,
1043 * so check it again to be sure.
1044 */
1045 VM_BUG_ON(!range.len);
1046
1047 wake_userfault(ctx, &range);
1048 ret = 0;
1049
1050out:
1051 return ret;
1052}
1053
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001054static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1055 unsigned long arg)
1056{
1057 __s64 ret;
1058 struct uffdio_copy uffdio_copy;
1059 struct uffdio_copy __user *user_uffdio_copy;
1060 struct userfaultfd_wake_range range;
1061
1062 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1063
1064 ret = -EFAULT;
1065 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1066 /* don't copy "copy" last field */
1067 sizeof(uffdio_copy)-sizeof(__s64)))
1068 goto out;
1069
1070 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1071 if (ret)
1072 goto out;
1073 /*
1074 * double check for wraparound just in case. copy_from_user()
1075 * will later check uffdio_copy.src + uffdio_copy.len to fit
1076 * in the userland range.
1077 */
1078 ret = -EINVAL;
1079 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1080 goto out;
1081 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1082 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001083 if (mmget_not_zero(ctx->mm)) {
1084 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1085 uffdio_copy.len);
1086 mmput(ctx->mm);
1087 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001088 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1089 return -EFAULT;
1090 if (ret < 0)
1091 goto out;
1092 BUG_ON(!ret);
1093 /* len == 0 would wake all */
1094 range.len = ret;
1095 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1096 range.start = uffdio_copy.dst;
1097 wake_userfault(ctx, &range);
1098 }
1099 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1100out:
1101 return ret;
1102}
1103
1104static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1105 unsigned long arg)
1106{
1107 __s64 ret;
1108 struct uffdio_zeropage uffdio_zeropage;
1109 struct uffdio_zeropage __user *user_uffdio_zeropage;
1110 struct userfaultfd_wake_range range;
1111
1112 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1113
1114 ret = -EFAULT;
1115 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1116 /* don't copy "zeropage" last field */
1117 sizeof(uffdio_zeropage)-sizeof(__s64)))
1118 goto out;
1119
1120 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1121 uffdio_zeropage.range.len);
1122 if (ret)
1123 goto out;
1124 ret = -EINVAL;
1125 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1126 goto out;
1127
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001128 if (mmget_not_zero(ctx->mm)) {
1129 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1130 uffdio_zeropage.range.len);
1131 mmput(ctx->mm);
1132 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001133 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1134 return -EFAULT;
1135 if (ret < 0)
1136 goto out;
1137 /* len == 0 would wake all */
1138 BUG_ON(!ret);
1139 range.len = ret;
1140 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1141 range.start = uffdio_zeropage.range.start;
1142 wake_userfault(ctx, &range);
1143 }
1144 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1145out:
1146 return ret;
1147}
1148
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001149/*
1150 * userland asks for a certain API version and we return which bits
1151 * and ioctl commands are implemented in this kernel for such API
1152 * version or -EINVAL if unknown.
1153 */
1154static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1155 unsigned long arg)
1156{
1157 struct uffdio_api uffdio_api;
1158 void __user *buf = (void __user *)arg;
1159 int ret;
1160
1161 ret = -EINVAL;
1162 if (ctx->state != UFFD_STATE_WAIT_API)
1163 goto out;
1164 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001165 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001166 goto out;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001167 if (uffdio_api.api != UFFD_API || uffdio_api.features) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001168 memset(&uffdio_api, 0, sizeof(uffdio_api));
1169 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1170 goto out;
1171 ret = -EINVAL;
1172 goto out;
1173 }
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001174 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001175 uffdio_api.ioctls = UFFD_API_IOCTLS;
1176 ret = -EFAULT;
1177 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1178 goto out;
1179 ctx->state = UFFD_STATE_RUNNING;
1180 ret = 0;
1181out:
1182 return ret;
1183}
1184
1185static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1186 unsigned long arg)
1187{
1188 int ret = -EINVAL;
1189 struct userfaultfd_ctx *ctx = file->private_data;
1190
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001191 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1192 return -EINVAL;
1193
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001194 switch(cmd) {
1195 case UFFDIO_API:
1196 ret = userfaultfd_api(ctx, arg);
1197 break;
1198 case UFFDIO_REGISTER:
1199 ret = userfaultfd_register(ctx, arg);
1200 break;
1201 case UFFDIO_UNREGISTER:
1202 ret = userfaultfd_unregister(ctx, arg);
1203 break;
1204 case UFFDIO_WAKE:
1205 ret = userfaultfd_wake(ctx, arg);
1206 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001207 case UFFDIO_COPY:
1208 ret = userfaultfd_copy(ctx, arg);
1209 break;
1210 case UFFDIO_ZEROPAGE:
1211 ret = userfaultfd_zeropage(ctx, arg);
1212 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001213 }
1214 return ret;
1215}
1216
1217#ifdef CONFIG_PROC_FS
1218static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1219{
1220 struct userfaultfd_ctx *ctx = f->private_data;
1221 wait_queue_t *wq;
1222 struct userfaultfd_wait_queue *uwq;
1223 unsigned long pending = 0, total = 0;
1224
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001225 spin_lock(&ctx->fault_pending_wqh.lock);
1226 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001227 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001228 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001229 total++;
1230 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001231 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1232 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1233 total++;
1234 }
1235 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001236
1237 /*
1238 * If more protocols will be added, there will be all shown
1239 * separated by a space. Like this:
1240 * protocols: aa:... bb:...
1241 */
1242 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001243 pending, total, UFFD_API, UFFD_API_FEATURES,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001244 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1245}
1246#endif
1247
1248static const struct file_operations userfaultfd_fops = {
1249#ifdef CONFIG_PROC_FS
1250 .show_fdinfo = userfaultfd_show_fdinfo,
1251#endif
1252 .release = userfaultfd_release,
1253 .poll = userfaultfd_poll,
1254 .read = userfaultfd_read,
1255 .unlocked_ioctl = userfaultfd_ioctl,
1256 .compat_ioctl = userfaultfd_ioctl,
1257 .llseek = noop_llseek,
1258};
1259
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001260static void init_once_userfaultfd_ctx(void *mem)
1261{
1262 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1263
1264 init_waitqueue_head(&ctx->fault_pending_wqh);
1265 init_waitqueue_head(&ctx->fault_wqh);
1266 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001267 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001268}
1269
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001270/**
1271 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1272 * @flags: Flags for the userfaultfd file.
1273 *
1274 * This function creates an userfaultfd file pointer, w/out installing
1275 * it into the fd table. This is useful when the userfaultfd file is
1276 * used during the initialization of data structures that require
1277 * extra setup after the userfaultfd creation. So the userfaultfd
1278 * creation is split into the file pointer creation phase, and the
1279 * file descriptor installation phase. In this way races with
1280 * userspace closing the newly installed file descriptor can be
1281 * avoided. Returns an userfaultfd file pointer, or a proper error
1282 * pointer.
1283 */
1284static struct file *userfaultfd_file_create(int flags)
1285{
1286 struct file *file;
1287 struct userfaultfd_ctx *ctx;
1288
1289 BUG_ON(!current->mm);
1290
1291 /* Check the UFFD_* constants for consistency. */
1292 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1293 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1294
1295 file = ERR_PTR(-EINVAL);
1296 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1297 goto out;
1298
1299 file = ERR_PTR(-ENOMEM);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001300 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001301 if (!ctx)
1302 goto out;
1303
1304 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001305 ctx->flags = flags;
1306 ctx->state = UFFD_STATE_WAIT_API;
1307 ctx->released = false;
1308 ctx->mm = current->mm;
1309 /* prevent the mm struct to be freed */
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001310 atomic_inc(&ctx->mm->mm_count);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001311
1312 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1313 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
Eric Biggersc03e9462015-09-17 16:01:54 -07001314 if (IS_ERR(file)) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001315 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001316 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001317 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001318out:
1319 return file;
1320}
1321
1322SYSCALL_DEFINE1(userfaultfd, int, flags)
1323{
1324 int fd, error;
1325 struct file *file;
1326
1327 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1328 if (error < 0)
1329 return error;
1330 fd = error;
1331
1332 file = userfaultfd_file_create(flags);
1333 if (IS_ERR(file)) {
1334 error = PTR_ERR(file);
1335 goto err_put_unused_fd;
1336 }
1337 fd_install(fd, file);
1338
1339 return fd;
1340
1341err_put_unused_fd:
1342 put_unused_fd(fd);
1343
1344 return error;
1345}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001346
1347static int __init userfaultfd_init(void)
1348{
1349 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1350 sizeof(struct userfaultfd_ctx),
1351 0,
1352 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1353 init_once_userfaultfd_ctx);
1354 return 0;
1355}
1356__initcall(userfaultfd_init);