blob: 5f11678907d5d90427f089a3c0e649c4ee2444d1 [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;
580 BUG_ON(ctx->state != UFFD_STATE_RUNNING);
581
582 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700583 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700584 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700585 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700586 if (_ret < 0)
587 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700588 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700589 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700590 ret += sizeof(msg);
591 buf += sizeof(msg);
592 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700593 /*
594 * Allow to read more than one fault at time but only
595 * block if waiting for the very first one.
596 */
597 no_wait = O_NONBLOCK;
598 }
599}
600
601static void __wake_userfault(struct userfaultfd_ctx *ctx,
602 struct userfaultfd_wake_range *range)
603{
604 unsigned long start, end;
605
606 start = range->start;
607 end = range->start + range->len;
608
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700609 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700610 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700611 if (waitqueue_active(&ctx->fault_pending_wqh))
612 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, 0,
613 range);
614 if (waitqueue_active(&ctx->fault_wqh))
615 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, 0, range);
616 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700617}
618
619static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
620 struct userfaultfd_wake_range *range)
621{
622 /*
623 * To be sure waitqueue_active() is not reordered by the CPU
624 * before the pagetable update, use an explicit SMP memory
625 * barrier here. PT lock release or up_read(mmap_sem) still
626 * have release semantics that can allow the
627 * waitqueue_active() to be reordered before the pte update.
628 */
629 smp_mb();
630
631 /*
632 * Use waitqueue_active because it's very frequent to
633 * change the address space atomically even if there are no
634 * userfaults yet. So we take the spinlock only when we're
635 * sure we've userfaults to wake.
636 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700637 if (waitqueue_active(&ctx->fault_pending_wqh) ||
638 waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700639 __wake_userfault(ctx, range);
640}
641
642static __always_inline int validate_range(struct mm_struct *mm,
643 __u64 start, __u64 len)
644{
645 __u64 task_size = mm->task_size;
646
647 if (start & ~PAGE_MASK)
648 return -EINVAL;
649 if (len & ~PAGE_MASK)
650 return -EINVAL;
651 if (!len)
652 return -EINVAL;
653 if (start < mmap_min_addr)
654 return -EINVAL;
655 if (start >= task_size)
656 return -EINVAL;
657 if (len > task_size - start)
658 return -EINVAL;
659 return 0;
660}
661
662static int userfaultfd_register(struct userfaultfd_ctx *ctx,
663 unsigned long arg)
664{
665 struct mm_struct *mm = ctx->mm;
666 struct vm_area_struct *vma, *prev, *cur;
667 int ret;
668 struct uffdio_register uffdio_register;
669 struct uffdio_register __user *user_uffdio_register;
670 unsigned long vm_flags, new_flags;
671 bool found;
672 unsigned long start, end, vma_end;
673
674 user_uffdio_register = (struct uffdio_register __user *) arg;
675
676 ret = -EFAULT;
677 if (copy_from_user(&uffdio_register, user_uffdio_register,
678 sizeof(uffdio_register)-sizeof(__u64)))
679 goto out;
680
681 ret = -EINVAL;
682 if (!uffdio_register.mode)
683 goto out;
684 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
685 UFFDIO_REGISTER_MODE_WP))
686 goto out;
687 vm_flags = 0;
688 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
689 vm_flags |= VM_UFFD_MISSING;
690 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
691 vm_flags |= VM_UFFD_WP;
692 /*
693 * FIXME: remove the below error constraint by
694 * implementing the wprotect tracking mode.
695 */
696 ret = -EINVAL;
697 goto out;
698 }
699
700 ret = validate_range(mm, uffdio_register.range.start,
701 uffdio_register.range.len);
702 if (ret)
703 goto out;
704
705 start = uffdio_register.range.start;
706 end = start + uffdio_register.range.len;
707
708 down_write(&mm->mmap_sem);
709 vma = find_vma_prev(mm, start, &prev);
710
711 ret = -ENOMEM;
712 if (!vma)
713 goto out_unlock;
714
715 /* check that there's at least one vma in the range */
716 ret = -EINVAL;
717 if (vma->vm_start >= end)
718 goto out_unlock;
719
720 /*
721 * Search for not compatible vmas.
722 *
723 * FIXME: this shall be relaxed later so that it doesn't fail
724 * on tmpfs backed vmas (in addition to the current allowance
725 * on anonymous vmas).
726 */
727 found = false;
728 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
729 cond_resched();
730
731 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
732 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
733
734 /* check not compatible vmas */
735 ret = -EINVAL;
736 if (cur->vm_ops)
737 goto out_unlock;
738
739 /*
740 * Check that this vma isn't already owned by a
741 * different userfaultfd. We can't allow more than one
742 * userfaultfd to own a single vma simultaneously or we
743 * wouldn't know which one to deliver the userfaults to.
744 */
745 ret = -EBUSY;
746 if (cur->vm_userfaultfd_ctx.ctx &&
747 cur->vm_userfaultfd_ctx.ctx != ctx)
748 goto out_unlock;
749
750 found = true;
751 }
752 BUG_ON(!found);
753
754 if (vma->vm_start < start)
755 prev = vma;
756
757 ret = 0;
758 do {
759 cond_resched();
760
761 BUG_ON(vma->vm_ops);
762 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
763 vma->vm_userfaultfd_ctx.ctx != ctx);
764
765 /*
766 * Nothing to do: this vma is already registered into this
767 * userfaultfd and with the right tracking mode too.
768 */
769 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
770 (vma->vm_flags & vm_flags) == vm_flags)
771 goto skip;
772
773 if (vma->vm_start > start)
774 start = vma->vm_start;
775 vma_end = min(end, vma->vm_end);
776
777 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
778 prev = vma_merge(mm, prev, start, vma_end, new_flags,
779 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
780 vma_policy(vma),
781 ((struct vm_userfaultfd_ctx){ ctx }));
782 if (prev) {
783 vma = prev;
784 goto next;
785 }
786 if (vma->vm_start < start) {
787 ret = split_vma(mm, vma, start, 1);
788 if (ret)
789 break;
790 }
791 if (vma->vm_end > end) {
792 ret = split_vma(mm, vma, end, 0);
793 if (ret)
794 break;
795 }
796 next:
797 /*
798 * In the vma_merge() successful mprotect-like case 8:
799 * the next vma was merged into the current one and
800 * the current one has not been updated yet.
801 */
802 vma->vm_flags = new_flags;
803 vma->vm_userfaultfd_ctx.ctx = ctx;
804
805 skip:
806 prev = vma;
807 start = vma->vm_end;
808 vma = vma->vm_next;
809 } while (vma && vma->vm_start < end);
810out_unlock:
811 up_write(&mm->mmap_sem);
812 if (!ret) {
813 /*
814 * Now that we scanned all vmas we can already tell
815 * userland which ioctls methods are guaranteed to
816 * succeed on this range.
817 */
818 if (put_user(UFFD_API_RANGE_IOCTLS,
819 &user_uffdio_register->ioctls))
820 ret = -EFAULT;
821 }
822out:
823 return ret;
824}
825
826static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
827 unsigned long arg)
828{
829 struct mm_struct *mm = ctx->mm;
830 struct vm_area_struct *vma, *prev, *cur;
831 int ret;
832 struct uffdio_range uffdio_unregister;
833 unsigned long new_flags;
834 bool found;
835 unsigned long start, end, vma_end;
836 const void __user *buf = (void __user *)arg;
837
838 ret = -EFAULT;
839 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
840 goto out;
841
842 ret = validate_range(mm, uffdio_unregister.start,
843 uffdio_unregister.len);
844 if (ret)
845 goto out;
846
847 start = uffdio_unregister.start;
848 end = start + uffdio_unregister.len;
849
850 down_write(&mm->mmap_sem);
851 vma = find_vma_prev(mm, start, &prev);
852
853 ret = -ENOMEM;
854 if (!vma)
855 goto out_unlock;
856
857 /* check that there's at least one vma in the range */
858 ret = -EINVAL;
859 if (vma->vm_start >= end)
860 goto out_unlock;
861
862 /*
863 * Search for not compatible vmas.
864 *
865 * FIXME: this shall be relaxed later so that it doesn't fail
866 * on tmpfs backed vmas (in addition to the current allowance
867 * on anonymous vmas).
868 */
869 found = false;
870 ret = -EINVAL;
871 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
872 cond_resched();
873
874 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
875 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
876
877 /*
878 * Check not compatible vmas, not strictly required
879 * here as not compatible vmas cannot have an
880 * userfaultfd_ctx registered on them, but this
881 * provides for more strict behavior to notice
882 * unregistration errors.
883 */
884 if (cur->vm_ops)
885 goto out_unlock;
886
887 found = true;
888 }
889 BUG_ON(!found);
890
891 if (vma->vm_start < start)
892 prev = vma;
893
894 ret = 0;
895 do {
896 cond_resched();
897
898 BUG_ON(vma->vm_ops);
899
900 /*
901 * Nothing to do: this vma is already registered into this
902 * userfaultfd and with the right tracking mode too.
903 */
904 if (!vma->vm_userfaultfd_ctx.ctx)
905 goto skip;
906
907 if (vma->vm_start > start)
908 start = vma->vm_start;
909 vma_end = min(end, vma->vm_end);
910
911 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
912 prev = vma_merge(mm, prev, start, vma_end, new_flags,
913 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
914 vma_policy(vma),
915 NULL_VM_UFFD_CTX);
916 if (prev) {
917 vma = prev;
918 goto next;
919 }
920 if (vma->vm_start < start) {
921 ret = split_vma(mm, vma, start, 1);
922 if (ret)
923 break;
924 }
925 if (vma->vm_end > end) {
926 ret = split_vma(mm, vma, end, 0);
927 if (ret)
928 break;
929 }
930 next:
931 /*
932 * In the vma_merge() successful mprotect-like case 8:
933 * the next vma was merged into the current one and
934 * the current one has not been updated yet.
935 */
936 vma->vm_flags = new_flags;
937 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
938
939 skip:
940 prev = vma;
941 start = vma->vm_end;
942 vma = vma->vm_next;
943 } while (vma && vma->vm_start < end);
944out_unlock:
945 up_write(&mm->mmap_sem);
946out:
947 return ret;
948}
949
950/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700951 * userfaultfd_wake may be used in combination with the
952 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700953 */
954static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
955 unsigned long arg)
956{
957 int ret;
958 struct uffdio_range uffdio_wake;
959 struct userfaultfd_wake_range range;
960 const void __user *buf = (void __user *)arg;
961
962 ret = -EFAULT;
963 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
964 goto out;
965
966 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
967 if (ret)
968 goto out;
969
970 range.start = uffdio_wake.start;
971 range.len = uffdio_wake.len;
972
973 /*
974 * len == 0 means wake all and we don't want to wake all here,
975 * so check it again to be sure.
976 */
977 VM_BUG_ON(!range.len);
978
979 wake_userfault(ctx, &range);
980 ret = 0;
981
982out:
983 return ret;
984}
985
Andrea Arcangeliad465cae2015-09-04 15:47:11 -0700986static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
987 unsigned long arg)
988{
989 __s64 ret;
990 struct uffdio_copy uffdio_copy;
991 struct uffdio_copy __user *user_uffdio_copy;
992 struct userfaultfd_wake_range range;
993
994 user_uffdio_copy = (struct uffdio_copy __user *) arg;
995
996 ret = -EFAULT;
997 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
998 /* don't copy "copy" last field */
999 sizeof(uffdio_copy)-sizeof(__s64)))
1000 goto out;
1001
1002 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1003 if (ret)
1004 goto out;
1005 /*
1006 * double check for wraparound just in case. copy_from_user()
1007 * will later check uffdio_copy.src + uffdio_copy.len to fit
1008 * in the userland range.
1009 */
1010 ret = -EINVAL;
1011 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1012 goto out;
1013 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1014 goto out;
1015
1016 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1017 uffdio_copy.len);
1018 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1019 return -EFAULT;
1020 if (ret < 0)
1021 goto out;
1022 BUG_ON(!ret);
1023 /* len == 0 would wake all */
1024 range.len = ret;
1025 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1026 range.start = uffdio_copy.dst;
1027 wake_userfault(ctx, &range);
1028 }
1029 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1030out:
1031 return ret;
1032}
1033
1034static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1035 unsigned long arg)
1036{
1037 __s64 ret;
1038 struct uffdio_zeropage uffdio_zeropage;
1039 struct uffdio_zeropage __user *user_uffdio_zeropage;
1040 struct userfaultfd_wake_range range;
1041
1042 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1043
1044 ret = -EFAULT;
1045 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1046 /* don't copy "zeropage" last field */
1047 sizeof(uffdio_zeropage)-sizeof(__s64)))
1048 goto out;
1049
1050 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1051 uffdio_zeropage.range.len);
1052 if (ret)
1053 goto out;
1054 ret = -EINVAL;
1055 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1056 goto out;
1057
1058 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1059 uffdio_zeropage.range.len);
1060 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1061 return -EFAULT;
1062 if (ret < 0)
1063 goto out;
1064 /* len == 0 would wake all */
1065 BUG_ON(!ret);
1066 range.len = ret;
1067 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1068 range.start = uffdio_zeropage.range.start;
1069 wake_userfault(ctx, &range);
1070 }
1071 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1072out:
1073 return ret;
1074}
1075
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001076/*
1077 * userland asks for a certain API version and we return which bits
1078 * and ioctl commands are implemented in this kernel for such API
1079 * version or -EINVAL if unknown.
1080 */
1081static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1082 unsigned long arg)
1083{
1084 struct uffdio_api uffdio_api;
1085 void __user *buf = (void __user *)arg;
1086 int ret;
1087
1088 ret = -EINVAL;
1089 if (ctx->state != UFFD_STATE_WAIT_API)
1090 goto out;
1091 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001092 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001093 goto out;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001094 if (uffdio_api.api != UFFD_API || uffdio_api.features) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001095 memset(&uffdio_api, 0, sizeof(uffdio_api));
1096 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1097 goto out;
1098 ret = -EINVAL;
1099 goto out;
1100 }
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001101 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001102 uffdio_api.ioctls = UFFD_API_IOCTLS;
1103 ret = -EFAULT;
1104 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1105 goto out;
1106 ctx->state = UFFD_STATE_RUNNING;
1107 ret = 0;
1108out:
1109 return ret;
1110}
1111
1112static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1113 unsigned long arg)
1114{
1115 int ret = -EINVAL;
1116 struct userfaultfd_ctx *ctx = file->private_data;
1117
1118 switch(cmd) {
1119 case UFFDIO_API:
1120 ret = userfaultfd_api(ctx, arg);
1121 break;
1122 case UFFDIO_REGISTER:
1123 ret = userfaultfd_register(ctx, arg);
1124 break;
1125 case UFFDIO_UNREGISTER:
1126 ret = userfaultfd_unregister(ctx, arg);
1127 break;
1128 case UFFDIO_WAKE:
1129 ret = userfaultfd_wake(ctx, arg);
1130 break;
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001131 case UFFDIO_COPY:
1132 ret = userfaultfd_copy(ctx, arg);
1133 break;
1134 case UFFDIO_ZEROPAGE:
1135 ret = userfaultfd_zeropage(ctx, arg);
1136 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001137 }
1138 return ret;
1139}
1140
1141#ifdef CONFIG_PROC_FS
1142static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1143{
1144 struct userfaultfd_ctx *ctx = f->private_data;
1145 wait_queue_t *wq;
1146 struct userfaultfd_wait_queue *uwq;
1147 unsigned long pending = 0, total = 0;
1148
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001149 spin_lock(&ctx->fault_pending_wqh.lock);
1150 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001151 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001152 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001153 total++;
1154 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001155 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1156 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1157 total++;
1158 }
1159 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001160
1161 /*
1162 * If more protocols will be added, there will be all shown
1163 * separated by a space. Like this:
1164 * protocols: aa:... bb:...
1165 */
1166 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001167 pending, total, UFFD_API, UFFD_API_FEATURES,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001168 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1169}
1170#endif
1171
1172static const struct file_operations userfaultfd_fops = {
1173#ifdef CONFIG_PROC_FS
1174 .show_fdinfo = userfaultfd_show_fdinfo,
1175#endif
1176 .release = userfaultfd_release,
1177 .poll = userfaultfd_poll,
1178 .read = userfaultfd_read,
1179 .unlocked_ioctl = userfaultfd_ioctl,
1180 .compat_ioctl = userfaultfd_ioctl,
1181 .llseek = noop_llseek,
1182};
1183
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001184static void init_once_userfaultfd_ctx(void *mem)
1185{
1186 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1187
1188 init_waitqueue_head(&ctx->fault_pending_wqh);
1189 init_waitqueue_head(&ctx->fault_wqh);
1190 init_waitqueue_head(&ctx->fd_wqh);
1191}
1192
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001193/**
1194 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1195 * @flags: Flags for the userfaultfd file.
1196 *
1197 * This function creates an userfaultfd file pointer, w/out installing
1198 * it into the fd table. This is useful when the userfaultfd file is
1199 * used during the initialization of data structures that require
1200 * extra setup after the userfaultfd creation. So the userfaultfd
1201 * creation is split into the file pointer creation phase, and the
1202 * file descriptor installation phase. In this way races with
1203 * userspace closing the newly installed file descriptor can be
1204 * avoided. Returns an userfaultfd file pointer, or a proper error
1205 * pointer.
1206 */
1207static struct file *userfaultfd_file_create(int flags)
1208{
1209 struct file *file;
1210 struct userfaultfd_ctx *ctx;
1211
1212 BUG_ON(!current->mm);
1213
1214 /* Check the UFFD_* constants for consistency. */
1215 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1216 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1217
1218 file = ERR_PTR(-EINVAL);
1219 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1220 goto out;
1221
1222 file = ERR_PTR(-ENOMEM);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001223 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001224 if (!ctx)
1225 goto out;
1226
1227 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001228 ctx->flags = flags;
1229 ctx->state = UFFD_STATE_WAIT_API;
1230 ctx->released = false;
1231 ctx->mm = current->mm;
1232 /* prevent the mm struct to be freed */
1233 atomic_inc(&ctx->mm->mm_users);
1234
1235 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1236 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1237 if (IS_ERR(file))
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001238 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001239out:
1240 return file;
1241}
1242
1243SYSCALL_DEFINE1(userfaultfd, int, flags)
1244{
1245 int fd, error;
1246 struct file *file;
1247
1248 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1249 if (error < 0)
1250 return error;
1251 fd = error;
1252
1253 file = userfaultfd_file_create(flags);
1254 if (IS_ERR(file)) {
1255 error = PTR_ERR(file);
1256 goto err_put_unused_fd;
1257 }
1258 fd_install(fd, file);
1259
1260 return fd;
1261
1262err_put_unused_fd:
1263 put_unused_fd(fd);
1264
1265 return error;
1266}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001267
1268static int __init userfaultfd_init(void)
1269{
1270 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1271 sizeof(struct userfaultfd_ctx),
1272 0,
1273 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1274 init_once_userfaultfd_ctx);
1275 return 0;
1276}
1277__initcall(userfaultfd_init);