blob: 68a62457e6858a27c486202c8442ae5774956175 [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));
140 mmput(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 */
260int handle_userfault(struct vm_area_struct *vma, unsigned long address,
261 unsigned int flags, unsigned long reason)
262{
263 struct mm_struct *mm = vma->vm_mm;
264 struct userfaultfd_ctx *ctx;
265 struct userfaultfd_wait_queue uwq;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700266 int ret;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700267 bool must_wait, return_to_userland;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700268
269 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
270
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700271 ret = VM_FAULT_SIGBUS;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700272 ctx = vma->vm_userfaultfd_ctx.ctx;
273 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700274 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700275
276 BUG_ON(ctx->mm != mm);
277
278 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
279 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
280
281 /*
282 * If it's already released don't get it. This avoids to loop
283 * in __get_user_pages if userfaultfd_release waits on the
284 * caller of handle_userfault to release the mmap_sem.
285 */
286 if (unlikely(ACCESS_ONCE(ctx->released)))
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700287 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700288
289 /*
290 * Check that we can return VM_FAULT_RETRY.
291 *
292 * NOTE: it should become possible to return VM_FAULT_RETRY
293 * even if FAULT_FLAG_TRIED is set without leading to gup()
294 * -EBUSY failures, if the userfaultfd is to be extended for
295 * VM_UFFD_WP tracking and we intend to arm the userfault
296 * without first stopping userland access to the memory. For
297 * VM_UFFD_MISSING userfaults this is enough for now.
298 */
299 if (unlikely(!(flags & FAULT_FLAG_ALLOW_RETRY))) {
300 /*
301 * Validate the invariant that nowait must allow retry
302 * to be sure not to return SIGBUS erroneously on
303 * nowait invocations.
304 */
305 BUG_ON(flags & FAULT_FLAG_RETRY_NOWAIT);
306#ifdef CONFIG_DEBUG_VM
307 if (printk_ratelimit()) {
308 printk(KERN_WARNING
309 "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags);
310 dump_stack();
311 }
312#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700313 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700314 }
315
316 /*
317 * Handle nowait, not much to do other than tell it to retry
318 * and wait.
319 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700320 ret = VM_FAULT_RETRY;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700321 if (flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700322 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700323
324 /* take the reference before dropping the mmap_sem */
325 userfaultfd_ctx_get(ctx);
326
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700327 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
328 uwq.wq.private = current;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700329 uwq.msg = userfault_msg(address, flags, reason);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700330 uwq.ctx = ctx;
331
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700332 return_to_userland = (flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
333 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
334
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700335 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700336 /*
337 * After the __add_wait_queue the uwq is visible to userland
338 * through poll/read().
339 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700340 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
341 /*
342 * The smp_mb() after __set_current_state prevents the reads
343 * following the spin_unlock to happen before the list_add in
344 * __add_wait_queue.
345 */
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700346 set_current_state(return_to_userland ? TASK_INTERRUPTIBLE :
347 TASK_KILLABLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700348 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700349
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700350 must_wait = userfaultfd_must_wait(ctx, address, flags, reason);
351 up_read(&mm->mmap_sem);
352
353 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700354 (return_to_userland ? !signal_pending(current) :
355 !fatal_signal_pending(current)))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700356 wake_up_poll(&ctx->fd_wqh, POLLIN);
357 schedule();
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700358 ret |= VM_FAULT_MAJOR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700359 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700360
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700361 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700362
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700363 if (return_to_userland) {
364 if (signal_pending(current) &&
365 !fatal_signal_pending(current)) {
366 /*
367 * If we got a SIGSTOP or SIGCONT and this is
368 * a normal userland page fault, just let
369 * userland return so the signal will be
370 * handled and gdb debugging works. The page
371 * fault code immediately after we return from
372 * this function is going to release the
373 * mmap_sem and it's not depending on it
374 * (unlike gup would if we were not to return
375 * VM_FAULT_RETRY).
376 *
377 * If a fatal signal is pending we still take
378 * the streamlined VM_FAULT_RETRY failure path
379 * and there's no need to retake the mmap_sem
380 * in such case.
381 */
382 down_read(&mm->mmap_sem);
383 ret = 0;
384 }
385 }
386
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700387 /*
388 * Here we race with the list_del; list_add in
389 * userfaultfd_ctx_read(), however because we don't ever run
390 * list_del_init() to refile across the two lists, the prev
391 * and next pointers will never point to self. list_add also
392 * would never let any of the two pointers to point to
393 * self. So list_empty_careful won't risk to see both pointers
394 * pointing to self at any time during the list refile. The
395 * only case where list_del_init() is called is the full
396 * removal in the wake function and there we don't re-list_add
397 * and it's fine not to block on the spinlock. The uwq on this
398 * kernel stack can be released after the list_del_init.
399 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700400 if (!list_empty_careful(&uwq.wq.task_list)) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700401 spin_lock(&ctx->fault_pending_wqh.lock);
402 /*
403 * No need of list_del_init(), the uwq on the stack
404 * will be freed shortly anyway.
405 */
406 list_del(&uwq.wq.task_list);
407 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700408 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700409
410 /*
411 * ctx may go away after this if the userfault pseudo fd is
412 * already released.
413 */
414 userfaultfd_ctx_put(ctx);
415
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700416out:
417 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700418}
419
420static int userfaultfd_release(struct inode *inode, struct file *file)
421{
422 struct userfaultfd_ctx *ctx = file->private_data;
423 struct mm_struct *mm = ctx->mm;
424 struct vm_area_struct *vma, *prev;
425 /* len == 0 means wake all */
426 struct userfaultfd_wake_range range = { .len = 0, };
427 unsigned long new_flags;
428
429 ACCESS_ONCE(ctx->released) = true;
430
431 /*
432 * Flush page faults out of all CPUs. NOTE: all page faults
433 * must be retried without returning VM_FAULT_SIGBUS if
434 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
435 * changes while handle_userfault released the mmap_sem. So
436 * it's critical that released is set to true (above), before
437 * taking the mmap_sem for writing.
438 */
439 down_write(&mm->mmap_sem);
440 prev = NULL;
441 for (vma = mm->mmap; vma; vma = vma->vm_next) {
442 cond_resched();
443 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
444 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
445 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
446 prev = vma;
447 continue;
448 }
449 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
450 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
451 new_flags, vma->anon_vma,
452 vma->vm_file, vma->vm_pgoff,
453 vma_policy(vma),
Dmitry Shmidtca259b22015-12-28 10:53:45 -0800454 NULL_VM_UFFD_CTX,
455 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700456 if (prev)
457 vma = prev;
458 else
459 prev = vma;
460 vma->vm_flags = new_flags;
461 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
462 }
463 up_write(&mm->mmap_sem);
464
465 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700466 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700467 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700468 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700469 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700470 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700471 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
472 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700473 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700474
475 wake_up_poll(&ctx->fd_wqh, POLLHUP);
476 userfaultfd_ctx_put(ctx);
477 return 0;
478}
479
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700480/* fault_pending_wqh.lock must be hold by the caller */
481static inline struct userfaultfd_wait_queue *find_userfault(
482 struct userfaultfd_ctx *ctx)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700483{
484 wait_queue_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700485 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700486
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700487 VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700488
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700489 uwq = NULL;
490 if (!waitqueue_active(&ctx->fault_pending_wqh))
491 goto out;
492 /* walk in reverse to provide FIFO behavior to read userfaults */
493 wq = list_last_entry(&ctx->fault_pending_wqh.task_list,
494 typeof(*wq), task_list);
495 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
496out:
497 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700498}
499
500static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
501{
502 struct userfaultfd_ctx *ctx = file->private_data;
503 unsigned int ret;
504
505 poll_wait(file, &ctx->fd_wqh, wait);
506
507 switch (ctx->state) {
508 case UFFD_STATE_WAIT_API:
509 return POLLERR;
510 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700511 /*
512 * poll() never guarantees that read won't block.
513 * userfaults can be waken before they're read().
514 */
515 if (unlikely(!(file->f_flags & O_NONBLOCK)))
516 return POLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700517 /*
518 * lockless access to see if there are pending faults
519 * __pollwait last action is the add_wait_queue but
520 * the spin_unlock would allow the waitqueue_active to
521 * pass above the actual list_add inside
522 * add_wait_queue critical section. So use a full
523 * memory barrier to serialize the list_add write of
524 * add_wait_queue() with the waitqueue_active read
525 * below.
526 */
527 ret = 0;
528 smp_mb();
529 if (waitqueue_active(&ctx->fault_pending_wqh))
530 ret = POLLIN;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700531 return ret;
532 default:
533 BUG();
534 }
535}
536
537static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700538 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700539{
540 ssize_t ret;
541 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700542 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700543
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700544 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700545 spin_lock(&ctx->fd_wqh.lock);
546 __add_wait_queue(&ctx->fd_wqh, &wait);
547 for (;;) {
548 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700549 spin_lock(&ctx->fault_pending_wqh.lock);
550 uwq = find_userfault(ctx);
551 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700552 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700553 * Use a seqcount to repeat the lockless check
554 * in wake_userfault() to avoid missing
555 * wakeups because during the refile both
556 * waitqueue could become empty if this is the
557 * only userfault.
558 */
559 write_seqcount_begin(&ctx->refile_seq);
560
561 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700562 * The fault_pending_wqh.lock prevents the uwq
563 * to disappear from under us.
564 *
565 * Refile this userfault from
566 * fault_pending_wqh to fault_wqh, it's not
567 * pending anymore after we read it.
568 *
569 * Use list_del() by hand (as
570 * userfaultfd_wake_function also uses
571 * list_del_init() by hand) to be sure nobody
572 * changes __remove_wait_queue() to use
573 * list_del_init() in turn breaking the
574 * !list_empty_careful() check in
575 * handle_userfault(). The uwq->wq.task_list
576 * must never be empty at any time during the
577 * refile, or the waitqueue could disappear
578 * from under us. The "wait_queue_head_t"
579 * parameter of __remove_wait_queue() is unused
580 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700581 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700582 list_del(&uwq->wq.task_list);
583 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
584
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700585 write_seqcount_end(&ctx->refile_seq);
586
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700587 /* careful to always initialize msg if ret == 0 */
588 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700589 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700590 ret = 0;
591 break;
592 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700593 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700594 if (signal_pending(current)) {
595 ret = -ERESTARTSYS;
596 break;
597 }
598 if (no_wait) {
599 ret = -EAGAIN;
600 break;
601 }
602 spin_unlock(&ctx->fd_wqh.lock);
603 schedule();
604 spin_lock(&ctx->fd_wqh.lock);
605 }
606 __remove_wait_queue(&ctx->fd_wqh, &wait);
607 __set_current_state(TASK_RUNNING);
608 spin_unlock(&ctx->fd_wqh.lock);
609
610 return ret;
611}
612
613static ssize_t userfaultfd_read(struct file *file, char __user *buf,
614 size_t count, loff_t *ppos)
615{
616 struct userfaultfd_ctx *ctx = file->private_data;
617 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700618 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700619 int no_wait = file->f_flags & O_NONBLOCK;
620
621 if (ctx->state == UFFD_STATE_WAIT_API)
622 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700623
624 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700625 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700626 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700627 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700628 if (_ret < 0)
629 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700630 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700631 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700632 ret += sizeof(msg);
633 buf += sizeof(msg);
634 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700635 /*
636 * Allow to read more than one fault at time but only
637 * block if waiting for the very first one.
638 */
639 no_wait = O_NONBLOCK;
640 }
641}
642
643static void __wake_userfault(struct userfaultfd_ctx *ctx,
644 struct userfaultfd_wake_range *range)
645{
646 unsigned long start, end;
647
648 start = range->start;
649 end = range->start + range->len;
650
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700651 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700652 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700653 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700654 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700655 range);
656 if (waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700657 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700658 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700659}
660
661static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
662 struct userfaultfd_wake_range *range)
663{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700664 unsigned seq;
665 bool need_wakeup;
666
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700667 /*
668 * To be sure waitqueue_active() is not reordered by the CPU
669 * before the pagetable update, use an explicit SMP memory
670 * barrier here. PT lock release or up_read(mmap_sem) still
671 * have release semantics that can allow the
672 * waitqueue_active() to be reordered before the pte update.
673 */
674 smp_mb();
675
676 /*
677 * Use waitqueue_active because it's very frequent to
678 * change the address space atomically even if there are no
679 * userfaults yet. So we take the spinlock only when we're
680 * sure we've userfaults to wake.
681 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700682 do {
683 seq = read_seqcount_begin(&ctx->refile_seq);
684 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
685 waitqueue_active(&ctx->fault_wqh);
686 cond_resched();
687 } while (read_seqcount_retry(&ctx->refile_seq, seq));
688 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700689 __wake_userfault(ctx, range);
690}
691
692static __always_inline int validate_range(struct mm_struct *mm,
693 __u64 start, __u64 len)
694{
695 __u64 task_size = mm->task_size;
696
697 if (start & ~PAGE_MASK)
698 return -EINVAL;
699 if (len & ~PAGE_MASK)
700 return -EINVAL;
701 if (!len)
702 return -EINVAL;
703 if (start < mmap_min_addr)
704 return -EINVAL;
705 if (start >= task_size)
706 return -EINVAL;
707 if (len > task_size - start)
708 return -EINVAL;
709 return 0;
710}
711
712static int userfaultfd_register(struct userfaultfd_ctx *ctx,
713 unsigned long arg)
714{
715 struct mm_struct *mm = ctx->mm;
716 struct vm_area_struct *vma, *prev, *cur;
717 int ret;
718 struct uffdio_register uffdio_register;
719 struct uffdio_register __user *user_uffdio_register;
720 unsigned long vm_flags, new_flags;
721 bool found;
722 unsigned long start, end, vma_end;
723
724 user_uffdio_register = (struct uffdio_register __user *) arg;
725
726 ret = -EFAULT;
727 if (copy_from_user(&uffdio_register, user_uffdio_register,
728 sizeof(uffdio_register)-sizeof(__u64)))
729 goto out;
730
731 ret = -EINVAL;
732 if (!uffdio_register.mode)
733 goto out;
734 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
735 UFFDIO_REGISTER_MODE_WP))
736 goto out;
737 vm_flags = 0;
738 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
739 vm_flags |= VM_UFFD_MISSING;
740 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
741 vm_flags |= VM_UFFD_WP;
742 /*
743 * FIXME: remove the below error constraint by
744 * implementing the wprotect tracking mode.
745 */
746 ret = -EINVAL;
747 goto out;
748 }
749
750 ret = validate_range(mm, uffdio_register.range.start,
751 uffdio_register.range.len);
752 if (ret)
753 goto out;
754
755 start = uffdio_register.range.start;
756 end = start + uffdio_register.range.len;
757
758 down_write(&mm->mmap_sem);
759 vma = find_vma_prev(mm, start, &prev);
760
761 ret = -ENOMEM;
762 if (!vma)
763 goto out_unlock;
764
765 /* check that there's at least one vma in the range */
766 ret = -EINVAL;
767 if (vma->vm_start >= end)
768 goto out_unlock;
769
770 /*
771 * Search for not compatible vmas.
772 *
773 * FIXME: this shall be relaxed later so that it doesn't fail
774 * on tmpfs backed vmas (in addition to the current allowance
775 * on anonymous vmas).
776 */
777 found = false;
778 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
779 cond_resched();
780
781 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
782 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
783
784 /* check not compatible vmas */
785 ret = -EINVAL;
786 if (cur->vm_ops)
787 goto out_unlock;
788
789 /*
790 * Check that this vma isn't already owned by a
791 * different userfaultfd. We can't allow more than one
792 * userfaultfd to own a single vma simultaneously or we
793 * wouldn't know which one to deliver the userfaults to.
794 */
795 ret = -EBUSY;
796 if (cur->vm_userfaultfd_ctx.ctx &&
797 cur->vm_userfaultfd_ctx.ctx != ctx)
798 goto out_unlock;
799
800 found = true;
801 }
802 BUG_ON(!found);
803
804 if (vma->vm_start < start)
805 prev = vma;
806
807 ret = 0;
808 do {
809 cond_resched();
810
811 BUG_ON(vma->vm_ops);
812 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
813 vma->vm_userfaultfd_ctx.ctx != ctx);
814
815 /*
816 * Nothing to do: this vma is already registered into this
817 * userfaultfd and with the right tracking mode too.
818 */
819 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
820 (vma->vm_flags & vm_flags) == vm_flags)
821 goto skip;
822
823 if (vma->vm_start > start)
824 start = vma->vm_start;
825 vma_end = min(end, vma->vm_end);
826
827 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
828 prev = vma_merge(mm, prev, start, vma_end, new_flags,
829 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
830 vma_policy(vma),
Dmitry Shmidtca259b22015-12-28 10:53:45 -0800831 ((struct vm_userfaultfd_ctx){ ctx }),
832 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700833 if (prev) {
834 vma = prev;
835 goto next;
836 }
837 if (vma->vm_start < start) {
838 ret = split_vma(mm, vma, start, 1);
839 if (ret)
840 break;
841 }
842 if (vma->vm_end > end) {
843 ret = split_vma(mm, vma, end, 0);
844 if (ret)
845 break;
846 }
847 next:
848 /*
849 * In the vma_merge() successful mprotect-like case 8:
850 * the next vma was merged into the current one and
851 * the current one has not been updated yet.
852 */
853 vma->vm_flags = new_flags;
854 vma->vm_userfaultfd_ctx.ctx = ctx;
855
856 skip:
857 prev = vma;
858 start = vma->vm_end;
859 vma = vma->vm_next;
860 } while (vma && vma->vm_start < end);
861out_unlock:
862 up_write(&mm->mmap_sem);
863 if (!ret) {
864 /*
865 * Now that we scanned all vmas we can already tell
866 * userland which ioctls methods are guaranteed to
867 * succeed on this range.
868 */
869 if (put_user(UFFD_API_RANGE_IOCTLS,
870 &user_uffdio_register->ioctls))
871 ret = -EFAULT;
872 }
873out:
874 return ret;
875}
876
877static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
878 unsigned long arg)
879{
880 struct mm_struct *mm = ctx->mm;
881 struct vm_area_struct *vma, *prev, *cur;
882 int ret;
883 struct uffdio_range uffdio_unregister;
884 unsigned long new_flags;
885 bool found;
886 unsigned long start, end, vma_end;
887 const void __user *buf = (void __user *)arg;
888
889 ret = -EFAULT;
890 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
891 goto out;
892
893 ret = validate_range(mm, uffdio_unregister.start,
894 uffdio_unregister.len);
895 if (ret)
896 goto out;
897
898 start = uffdio_unregister.start;
899 end = start + uffdio_unregister.len;
900
901 down_write(&mm->mmap_sem);
902 vma = find_vma_prev(mm, start, &prev);
903
904 ret = -ENOMEM;
905 if (!vma)
906 goto out_unlock;
907
908 /* check that there's at least one vma in the range */
909 ret = -EINVAL;
910 if (vma->vm_start >= end)
911 goto out_unlock;
912
913 /*
914 * Search for not compatible vmas.
915 *
916 * FIXME: this shall be relaxed later so that it doesn't fail
917 * on tmpfs backed vmas (in addition to the current allowance
918 * on anonymous vmas).
919 */
920 found = false;
921 ret = -EINVAL;
922 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
923 cond_resched();
924
925 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
926 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
927
928 /*
929 * Check not compatible vmas, not strictly required
930 * here as not compatible vmas cannot have an
931 * userfaultfd_ctx registered on them, but this
932 * provides for more strict behavior to notice
933 * unregistration errors.
934 */
935 if (cur->vm_ops)
936 goto out_unlock;
937
938 found = true;
939 }
940 BUG_ON(!found);
941
942 if (vma->vm_start < start)
943 prev = vma;
944
945 ret = 0;
946 do {
947 cond_resched();
948
949 BUG_ON(vma->vm_ops);
950
951 /*
952 * Nothing to do: this vma is already registered into this
953 * userfaultfd and with the right tracking mode too.
954 */
955 if (!vma->vm_userfaultfd_ctx.ctx)
956 goto skip;
957
958 if (vma->vm_start > start)
959 start = vma->vm_start;
960 vma_end = min(end, vma->vm_end);
961
962 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
963 prev = vma_merge(mm, prev, start, vma_end, new_flags,
964 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
965 vma_policy(vma),
Dmitry Shmidtca259b22015-12-28 10:53:45 -0800966 NULL_VM_UFFD_CTX,
967 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700968 if (prev) {
969 vma = prev;
970 goto next;
971 }
972 if (vma->vm_start < start) {
973 ret = split_vma(mm, vma, start, 1);
974 if (ret)
975 break;
976 }
977 if (vma->vm_end > end) {
978 ret = split_vma(mm, vma, end, 0);
979 if (ret)
980 break;
981 }
982 next:
983 /*
984 * In the vma_merge() successful mprotect-like case 8:
985 * the next vma was merged into the current one and
986 * the current one has not been updated yet.
987 */
988 vma->vm_flags = new_flags;
989 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
990
991 skip:
992 prev = vma;
993 start = vma->vm_end;
994 vma = vma->vm_next;
995 } while (vma && vma->vm_start < end);
996out_unlock:
997 up_write(&mm->mmap_sem);
998out:
999 return ret;
1000}
1001
1002/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001003 * userfaultfd_wake may be used in combination with the
1004 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001005 */
1006static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1007 unsigned long arg)
1008{
1009 int ret;
1010 struct uffdio_range uffdio_wake;
1011 struct userfaultfd_wake_range range;
1012 const void __user *buf = (void __user *)arg;
1013
1014 ret = -EFAULT;
1015 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1016 goto out;
1017
1018 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1019 if (ret)
1020 goto out;
1021
1022 range.start = uffdio_wake.start;
1023 range.len = uffdio_wake.len;
1024
1025 /*
1026 * len == 0 means wake all and we don't want to wake all here,
1027 * so check it again to be sure.
1028 */
1029 VM_BUG_ON(!range.len);
1030
1031 wake_userfault(ctx, &range);
1032 ret = 0;
1033
1034out:
1035 return ret;
1036}
1037
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001038static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1039 unsigned long arg)
1040{
1041 __s64 ret;
1042 struct uffdio_copy uffdio_copy;
1043 struct uffdio_copy __user *user_uffdio_copy;
1044 struct userfaultfd_wake_range range;
1045
1046 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1047
1048 ret = -EFAULT;
1049 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1050 /* don't copy "copy" last field */
1051 sizeof(uffdio_copy)-sizeof(__s64)))
1052 goto out;
1053
1054 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1055 if (ret)
1056 goto out;
1057 /*
1058 * double check for wraparound just in case. copy_from_user()
1059 * will later check uffdio_copy.src + uffdio_copy.len to fit
1060 * in the userland range.
1061 */
1062 ret = -EINVAL;
1063 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1064 goto out;
1065 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1066 goto out;
1067
1068 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1069 uffdio_copy.len);
1070 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1071 return -EFAULT;
1072 if (ret < 0)
1073 goto out;
1074 BUG_ON(!ret);
1075 /* len == 0 would wake all */
1076 range.len = ret;
1077 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1078 range.start = uffdio_copy.dst;
1079 wake_userfault(ctx, &range);
1080 }
1081 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1082out:
1083 return ret;
1084}
1085
1086static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1087 unsigned long arg)
1088{
1089 __s64 ret;
1090 struct uffdio_zeropage uffdio_zeropage;
1091 struct uffdio_zeropage __user *user_uffdio_zeropage;
1092 struct userfaultfd_wake_range range;
1093
1094 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1095
1096 ret = -EFAULT;
1097 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1098 /* don't copy "zeropage" last field */
1099 sizeof(uffdio_zeropage)-sizeof(__s64)))
1100 goto out;
1101
1102 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1103 uffdio_zeropage.range.len);
1104 if (ret)
1105 goto out;
1106 ret = -EINVAL;
1107 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1108 goto out;
1109
1110 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1111 uffdio_zeropage.range.len);
1112 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1113 return -EFAULT;
1114 if (ret < 0)
1115 goto out;
1116 /* len == 0 would wake all */
1117 BUG_ON(!ret);
1118 range.len = ret;
1119 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1120 range.start = uffdio_zeropage.range.start;
1121 wake_userfault(ctx, &range);
1122 }
1123 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1124out:
1125 return ret;
1126}
1127
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001128/*
1129 * userland asks for a certain API version and we return which bits
1130 * and ioctl commands are implemented in this kernel for such API
1131 * version or -EINVAL if unknown.
1132 */
1133static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1134 unsigned long arg)
1135{
1136 struct uffdio_api uffdio_api;
1137 void __user *buf = (void __user *)arg;
1138 int ret;
1139
1140 ret = -EINVAL;
1141 if (ctx->state != UFFD_STATE_WAIT_API)
1142 goto out;
1143 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001144 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001145 goto out;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001146 if (uffdio_api.api != UFFD_API || uffdio_api.features) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001147 memset(&uffdio_api, 0, sizeof(uffdio_api));
1148 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1149 goto out;
1150 ret = -EINVAL;
1151 goto out;
1152 }
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001153 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001154 uffdio_api.ioctls = UFFD_API_IOCTLS;
1155 ret = -EFAULT;
1156 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1157 goto out;
1158 ctx->state = UFFD_STATE_RUNNING;
1159 ret = 0;
1160out:
1161 return ret;
1162}
1163
1164static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1165 unsigned long arg)
1166{
1167 int ret = -EINVAL;
1168 struct userfaultfd_ctx *ctx = file->private_data;
1169
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001170 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1171 return -EINVAL;
1172
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001173 switch(cmd) {
1174 case UFFDIO_API:
1175 ret = userfaultfd_api(ctx, arg);
1176 break;
1177 case UFFDIO_REGISTER:
1178 ret = userfaultfd_register(ctx, arg);
1179 break;
1180 case UFFDIO_UNREGISTER:
1181 ret = userfaultfd_unregister(ctx, arg);
1182 break;
1183 case UFFDIO_WAKE:
1184 ret = userfaultfd_wake(ctx, arg);
1185 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001186 case UFFDIO_COPY:
1187 ret = userfaultfd_copy(ctx, arg);
1188 break;
1189 case UFFDIO_ZEROPAGE:
1190 ret = userfaultfd_zeropage(ctx, arg);
1191 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001192 }
1193 return ret;
1194}
1195
1196#ifdef CONFIG_PROC_FS
1197static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1198{
1199 struct userfaultfd_ctx *ctx = f->private_data;
1200 wait_queue_t *wq;
1201 struct userfaultfd_wait_queue *uwq;
1202 unsigned long pending = 0, total = 0;
1203
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001204 spin_lock(&ctx->fault_pending_wqh.lock);
1205 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001206 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001207 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001208 total++;
1209 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001210 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1211 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1212 total++;
1213 }
1214 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001215
1216 /*
1217 * If more protocols will be added, there will be all shown
1218 * separated by a space. Like this:
1219 * protocols: aa:... bb:...
1220 */
1221 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001222 pending, total, UFFD_API, UFFD_API_FEATURES,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001223 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1224}
1225#endif
1226
1227static const struct file_operations userfaultfd_fops = {
1228#ifdef CONFIG_PROC_FS
1229 .show_fdinfo = userfaultfd_show_fdinfo,
1230#endif
1231 .release = userfaultfd_release,
1232 .poll = userfaultfd_poll,
1233 .read = userfaultfd_read,
1234 .unlocked_ioctl = userfaultfd_ioctl,
1235 .compat_ioctl = userfaultfd_ioctl,
1236 .llseek = noop_llseek,
1237};
1238
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001239static void init_once_userfaultfd_ctx(void *mem)
1240{
1241 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1242
1243 init_waitqueue_head(&ctx->fault_pending_wqh);
1244 init_waitqueue_head(&ctx->fault_wqh);
1245 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001246 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001247}
1248
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001249/**
1250 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1251 * @flags: Flags for the userfaultfd file.
1252 *
1253 * This function creates an userfaultfd file pointer, w/out installing
1254 * it into the fd table. This is useful when the userfaultfd file is
1255 * used during the initialization of data structures that require
1256 * extra setup after the userfaultfd creation. So the userfaultfd
1257 * creation is split into the file pointer creation phase, and the
1258 * file descriptor installation phase. In this way races with
1259 * userspace closing the newly installed file descriptor can be
1260 * avoided. Returns an userfaultfd file pointer, or a proper error
1261 * pointer.
1262 */
1263static struct file *userfaultfd_file_create(int flags)
1264{
1265 struct file *file;
1266 struct userfaultfd_ctx *ctx;
1267
1268 BUG_ON(!current->mm);
1269
1270 /* Check the UFFD_* constants for consistency. */
1271 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1272 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1273
1274 file = ERR_PTR(-EINVAL);
1275 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1276 goto out;
1277
1278 file = ERR_PTR(-ENOMEM);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001279 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001280 if (!ctx)
1281 goto out;
1282
1283 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001284 ctx->flags = flags;
1285 ctx->state = UFFD_STATE_WAIT_API;
1286 ctx->released = false;
1287 ctx->mm = current->mm;
1288 /* prevent the mm struct to be freed */
1289 atomic_inc(&ctx->mm->mm_users);
1290
1291 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1292 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
Eric Biggersc03e9462015-09-17 16:01:54 -07001293 if (IS_ERR(file)) {
1294 mmput(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001295 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001296 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001297out:
1298 return file;
1299}
1300
1301SYSCALL_DEFINE1(userfaultfd, int, flags)
1302{
1303 int fd, error;
1304 struct file *file;
1305
1306 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1307 if (error < 0)
1308 return error;
1309 fd = error;
1310
1311 file = userfaultfd_file_create(flags);
1312 if (IS_ERR(file)) {
1313 error = PTR_ERR(file);
1314 goto err_put_unused_fd;
1315 }
1316 fd_install(fd, file);
1317
1318 return fd;
1319
1320err_put_unused_fd:
1321 put_unused_fd(fd);
1322
1323 return error;
1324}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001325
1326static int __init userfaultfd_init(void)
1327{
1328 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1329 sizeof(struct userfaultfd_ctx),
1330 0,
1331 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1332 init_once_userfaultfd_ctx);
1333 return 0;
1334}
1335__initcall(userfaultfd_init);