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