blob: 2407249998c326e178087f65eb1500954e32abf1 [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
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080015#include <linux/list.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070016#include <linux/hashtable.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010018#include <linux/sched/mm.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070019#include <linux/mm.h>
20#include <linux/poll.h>
21#include <linux/slab.h>
22#include <linux/seq_file.h>
23#include <linux/file.h>
24#include <linux/bug.h>
25#include <linux/anon_inodes.h>
26#include <linux/syscalls.h>
27#include <linux/userfaultfd_k.h>
28#include <linux/mempolicy.h>
29#include <linux/ioctl.h>
30#include <linux/security.h>
Mike Kravetzcab350a2017-02-22 15:43:04 -080031#include <linux/hugetlb.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070032
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070033static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
34
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070035enum userfaultfd_state {
36 UFFD_STATE_WAIT_API,
37 UFFD_STATE_RUNNING,
38};
39
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070040/*
41 * Start with fault_pending_wqh and fault_wqh so they're more likely
42 * to be in the same cacheline.
43 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070044struct userfaultfd_ctx {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -070045 /* waitqueue head for the pending (i.e. not read) userfaults */
46 wait_queue_head_t fault_pending_wqh;
47 /* waitqueue head for the userfaults */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070048 wait_queue_head_t fault_wqh;
49 /* waitqueue head for the pseudo fd to wakeup poll/read */
50 wait_queue_head_t fd_wqh;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080051 /* waitqueue head for events */
52 wait_queue_head_t event_wqh;
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -070053 /* a refile sequence protected by fault_pending_wqh lock */
54 struct seqcount refile_seq;
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070055 /* pseudo fd refcounting */
56 atomic_t refcount;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070057 /* userfaultfd syscall flags */
58 unsigned int flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080059 /* features requested from the userspace */
60 unsigned int features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070061 /* state machine */
62 enum userfaultfd_state state;
63 /* released */
64 bool released;
65 /* mm with one ore more vmas attached to this userfaultfd_ctx */
66 struct mm_struct *mm;
67};
68
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080069struct userfaultfd_fork_ctx {
70 struct userfaultfd_ctx *orig;
71 struct userfaultfd_ctx *new;
72 struct list_head list;
73};
74
Mike Rapoport897ab3e2017-02-24 14:58:22 -080075struct userfaultfd_unmap_ctx {
76 struct userfaultfd_ctx *ctx;
77 unsigned long start;
78 unsigned long end;
79 struct list_head list;
80};
81
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070082struct userfaultfd_wait_queue {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070083 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070084 wait_queue_t wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070085 struct userfaultfd_ctx *ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -080086 bool waken;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070087};
88
89struct userfaultfd_wake_range {
90 unsigned long start;
91 unsigned long len;
92};
93
94static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
95 int wake_flags, void *key)
96{
97 struct userfaultfd_wake_range *range = key;
98 int ret;
99 struct userfaultfd_wait_queue *uwq;
100 unsigned long start, len;
101
102 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
103 ret = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700104 /* len == 0 means wake all */
105 start = range->start;
106 len = range->len;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700107 if (len && (start > uwq->msg.arg.pagefault.address ||
108 start + len <= uwq->msg.arg.pagefault.address))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700109 goto out;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800110 WRITE_ONCE(uwq->waken, true);
111 /*
112 * The implicit smp_mb__before_spinlock in try_to_wake_up()
113 * renders uwq->waken visible to other CPUs before the task is
114 * waken.
115 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700116 ret = wake_up_state(wq->private, mode);
117 if (ret)
118 /*
119 * Wake only once, autoremove behavior.
120 *
121 * After the effect of list_del_init is visible to the
122 * other CPUs, the waitqueue may disappear from under
123 * us, see the !list_empty_careful() in
124 * handle_userfault(). try_to_wake_up() has an
125 * implicit smp_mb__before_spinlock, and the
126 * wq->private is read before calling the extern
127 * function "wake_up_state" (which in turns calls
128 * try_to_wake_up). While the spin_lock;spin_unlock;
129 * wouldn't be enough, the smp_mb__before_spinlock is
130 * enough to avoid an explicit smp_mb() here.
131 */
132 list_del_init(&wq->task_list);
133out:
134 return ret;
135}
136
137/**
138 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
139 * context.
140 * @ctx: [in] Pointer to the userfaultfd context.
141 *
142 * Returns: In case of success, returns not zero.
143 */
144static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
145{
146 if (!atomic_inc_not_zero(&ctx->refcount))
147 BUG();
148}
149
150/**
151 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
152 * context.
153 * @ctx: [in] Pointer to userfaultfd context.
154 *
155 * The userfaultfd context reference must have been previously acquired either
156 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
157 */
158static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
159{
160 if (atomic_dec_and_test(&ctx->refcount)) {
161 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
162 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
163 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
164 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800165 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
166 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700167 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
168 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700169 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700170 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700171 }
172}
173
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700174static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700175{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700176 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
177 /*
178 * Must use memset to zero out the paddings or kernel data is
179 * leaked to userland.
180 */
181 memset(msg, 0, sizeof(struct uffd_msg));
182}
183
184static inline struct uffd_msg userfault_msg(unsigned long address,
185 unsigned int flags,
186 unsigned long reason)
187{
188 struct uffd_msg msg;
189 msg_init(&msg);
190 msg.event = UFFD_EVENT_PAGEFAULT;
191 msg.arg.pagefault.address = address;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700192 if (flags & FAULT_FLAG_WRITE)
193 /*
Andrea Arcangelia4605a62017-02-22 15:42:09 -0800194 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700195 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
196 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
197 * was a read fault, otherwise if set it means it's
198 * a write fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700199 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700200 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700201 if (reason & VM_UFFD_WP)
202 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700203 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
204 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
205 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
206 * a missing fault, otherwise if set it means it's a
207 * write protect fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700208 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700209 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
210 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700211}
212
Mike Kravetz369cd212017-02-22 15:43:10 -0800213#ifdef CONFIG_HUGETLB_PAGE
214/*
215 * Same functionality as userfaultfd_must_wait below with modifications for
216 * hugepmd ranges.
217 */
218static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
219 unsigned long address,
220 unsigned long flags,
221 unsigned long reason)
222{
223 struct mm_struct *mm = ctx->mm;
224 pte_t *pte;
225 bool ret = true;
226
227 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
228
229 pte = huge_pte_offset(mm, address);
230 if (!pte)
231 goto out;
232
233 ret = false;
234
235 /*
236 * Lockless access: we're in a wait_event so it's ok if it
237 * changes under us.
238 */
239 if (huge_pte_none(*pte))
240 ret = true;
241 if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
242 ret = true;
243out:
244 return ret;
245}
246#else
247static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
248 unsigned long address,
249 unsigned long flags,
250 unsigned long reason)
251{
252 return false; /* should never get here */
253}
254#endif /* CONFIG_HUGETLB_PAGE */
255
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700256/*
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700257 * Verify the pagetables are still not ok after having reigstered into
258 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
259 * userfault that has already been resolved, if userfaultfd_read and
260 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
261 * threads.
262 */
263static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
264 unsigned long address,
265 unsigned long flags,
266 unsigned long reason)
267{
268 struct mm_struct *mm = ctx->mm;
269 pgd_t *pgd;
270 pud_t *pud;
271 pmd_t *pmd, _pmd;
272 pte_t *pte;
273 bool ret = true;
274
275 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
276
277 pgd = pgd_offset(mm, address);
278 if (!pgd_present(*pgd))
279 goto out;
280 pud = pud_offset(pgd, address);
281 if (!pud_present(*pud))
282 goto out;
283 pmd = pmd_offset(pud, address);
284 /*
285 * READ_ONCE must function as a barrier with narrower scope
286 * and it must be equivalent to:
287 * _pmd = *pmd; barrier();
288 *
289 * This is to deal with the instability (as in
290 * pmd_trans_unstable) of the pmd.
291 */
292 _pmd = READ_ONCE(*pmd);
293 if (!pmd_present(_pmd))
294 goto out;
295
296 ret = false;
297 if (pmd_trans_huge(_pmd))
298 goto out;
299
300 /*
301 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
302 * and use the standard pte_offset_map() instead of parsing _pmd.
303 */
304 pte = pte_offset_map(pmd, address);
305 /*
306 * Lockless access: we're in a wait_event so it's ok if it
307 * changes under us.
308 */
309 if (pte_none(*pte))
310 ret = true;
311 pte_unmap(pte);
312
313out:
314 return ret;
315}
316
317/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700318 * The locking rules involved in returning VM_FAULT_RETRY depending on
319 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
320 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
321 * recommendation in __lock_page_or_retry is not an understatement.
322 *
323 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
324 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
325 * not set.
326 *
327 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
328 * set, VM_FAULT_RETRY can still be returned if and only if there are
329 * fatal_signal_pending()s, and the mmap_sem must be released before
330 * returning it.
331 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800332int handle_userfault(struct vm_fault *vmf, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700333{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800334 struct mm_struct *mm = vmf->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700335 struct userfaultfd_ctx *ctx;
336 struct userfaultfd_wait_queue uwq;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700337 int ret;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700338 bool must_wait, return_to_userland;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800339 long blocking_state;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700340
341 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
342
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700343 ret = VM_FAULT_SIGBUS;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800344 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700345 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700346 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700347
348 BUG_ON(ctx->mm != mm);
349
350 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
351 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
352
353 /*
354 * If it's already released don't get it. This avoids to loop
355 * in __get_user_pages if userfaultfd_release waits on the
356 * caller of handle_userfault to release the mmap_sem.
357 */
358 if (unlikely(ACCESS_ONCE(ctx->released)))
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700359 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700360
361 /*
Linus Torvalds39680f52016-03-01 11:56:22 -0800362 * We don't do userfault handling for the final child pid update.
363 */
364 if (current->flags & PF_EXITING)
365 goto out;
366
367 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700368 * Check that we can return VM_FAULT_RETRY.
369 *
370 * NOTE: it should become possible to return VM_FAULT_RETRY
371 * even if FAULT_FLAG_TRIED is set without leading to gup()
372 * -EBUSY failures, if the userfaultfd is to be extended for
373 * VM_UFFD_WP tracking and we intend to arm the userfault
374 * without first stopping userland access to the memory. For
375 * VM_UFFD_MISSING userfaults this is enough for now.
376 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800377 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700378 /*
379 * Validate the invariant that nowait must allow retry
380 * to be sure not to return SIGBUS erroneously on
381 * nowait invocations.
382 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800383 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700384#ifdef CONFIG_DEBUG_VM
385 if (printk_ratelimit()) {
386 printk(KERN_WARNING
Jan Kara82b0f8c2016-12-14 15:06:58 -0800387 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
388 vmf->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700389 dump_stack();
390 }
391#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700392 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700393 }
394
395 /*
396 * Handle nowait, not much to do other than tell it to retry
397 * and wait.
398 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700399 ret = VM_FAULT_RETRY;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800400 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700401 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700402
403 /* take the reference before dropping the mmap_sem */
404 userfaultfd_ctx_get(ctx);
405
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700406 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
407 uwq.wq.private = current;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800408 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700409 uwq.ctx = ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800410 uwq.waken = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700411
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700412 return_to_userland =
Jan Kara82b0f8c2016-12-14 15:06:58 -0800413 (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700414 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800415 blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
416 TASK_KILLABLE;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700417
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700418 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700419 /*
420 * After the __add_wait_queue the uwq is visible to userland
421 * through poll/read().
422 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700423 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
424 /*
425 * The smp_mb() after __set_current_state prevents the reads
426 * following the spin_unlock to happen before the list_add in
427 * __add_wait_queue.
428 */
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800429 set_current_state(blocking_state);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700430 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700431
Mike Kravetz369cd212017-02-22 15:43:10 -0800432 if (!is_vm_hugetlb_page(vmf->vma))
433 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
434 reason);
435 else
436 must_wait = userfaultfd_huge_must_wait(ctx, vmf->address,
437 vmf->flags, reason);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700438 up_read(&mm->mmap_sem);
439
440 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700441 (return_to_userland ? !signal_pending(current) :
442 !fatal_signal_pending(current)))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700443 wake_up_poll(&ctx->fd_wqh, POLLIN);
444 schedule();
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700445 ret |= VM_FAULT_MAJOR;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800446
447 /*
448 * False wakeups can orginate even from rwsem before
449 * up_read() however userfaults will wait either for a
450 * targeted wakeup on the specific uwq waitqueue from
451 * wake_userfault() or for signals or for uffd
452 * release.
453 */
454 while (!READ_ONCE(uwq.waken)) {
455 /*
456 * This needs the full smp_store_mb()
457 * guarantee as the state write must be
458 * visible to other CPUs before reading
459 * uwq.waken from other CPUs.
460 */
461 set_current_state(blocking_state);
462 if (READ_ONCE(uwq.waken) ||
463 READ_ONCE(ctx->released) ||
464 (return_to_userland ? signal_pending(current) :
465 fatal_signal_pending(current)))
466 break;
467 schedule();
468 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700469 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700470
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700471 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700472
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700473 if (return_to_userland) {
474 if (signal_pending(current) &&
475 !fatal_signal_pending(current)) {
476 /*
477 * If we got a SIGSTOP or SIGCONT and this is
478 * a normal userland page fault, just let
479 * userland return so the signal will be
480 * handled and gdb debugging works. The page
481 * fault code immediately after we return from
482 * this function is going to release the
483 * mmap_sem and it's not depending on it
484 * (unlike gup would if we were not to return
485 * VM_FAULT_RETRY).
486 *
487 * If a fatal signal is pending we still take
488 * the streamlined VM_FAULT_RETRY failure path
489 * and there's no need to retake the mmap_sem
490 * in such case.
491 */
492 down_read(&mm->mmap_sem);
Andrea Arcangeli6bbc4a42017-03-09 16:16:28 -0800493 ret = VM_FAULT_NOPAGE;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700494 }
495 }
496
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700497 /*
498 * Here we race with the list_del; list_add in
499 * userfaultfd_ctx_read(), however because we don't ever run
500 * list_del_init() to refile across the two lists, the prev
501 * and next pointers will never point to self. list_add also
502 * would never let any of the two pointers to point to
503 * self. So list_empty_careful won't risk to see both pointers
504 * pointing to self at any time during the list refile. The
505 * only case where list_del_init() is called is the full
506 * removal in the wake function and there we don't re-list_add
507 * and it's fine not to block on the spinlock. The uwq on this
508 * kernel stack can be released after the list_del_init.
509 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700510 if (!list_empty_careful(&uwq.wq.task_list)) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700511 spin_lock(&ctx->fault_pending_wqh.lock);
512 /*
513 * No need of list_del_init(), the uwq on the stack
514 * will be freed shortly anyway.
515 */
516 list_del(&uwq.wq.task_list);
517 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700518 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700519
520 /*
521 * ctx may go away after this if the userfault pseudo fd is
522 * already released.
523 */
524 userfaultfd_ctx_put(ctx);
525
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700526out:
527 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700528}
529
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800530static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
531 struct userfaultfd_wait_queue *ewq)
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800532{
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800533 if (WARN_ON_ONCE(current->flags & PF_EXITING))
534 goto out;
535
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800536 ewq->ctx = ctx;
537 init_waitqueue_entry(&ewq->wq, current);
538
539 spin_lock(&ctx->event_wqh.lock);
540 /*
541 * After the __add_wait_queue the uwq is visible to userland
542 * through poll/read().
543 */
544 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
545 for (;;) {
546 set_current_state(TASK_KILLABLE);
547 if (ewq->msg.event == 0)
548 break;
549 if (ACCESS_ONCE(ctx->released) ||
550 fatal_signal_pending(current)) {
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800551 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800552 if (ewq->msg.event == UFFD_EVENT_FORK) {
553 struct userfaultfd_ctx *new;
554
555 new = (struct userfaultfd_ctx *)
556 (unsigned long)
557 ewq->msg.arg.reserved.reserved1;
558
559 userfaultfd_ctx_put(new);
560 }
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800561 break;
562 }
563
564 spin_unlock(&ctx->event_wqh.lock);
565
566 wake_up_poll(&ctx->fd_wqh, POLLIN);
567 schedule();
568
569 spin_lock(&ctx->event_wqh.lock);
570 }
571 __set_current_state(TASK_RUNNING);
572 spin_unlock(&ctx->event_wqh.lock);
573
574 /*
575 * ctx may go away after this if the userfault pseudo fd is
576 * already released.
577 */
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800578out:
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800579 userfaultfd_ctx_put(ctx);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800580}
581
582static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
583 struct userfaultfd_wait_queue *ewq)
584{
585 ewq->msg.event = 0;
586 wake_up_locked(&ctx->event_wqh);
587 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
588}
589
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800590int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
591{
592 struct userfaultfd_ctx *ctx = NULL, *octx;
593 struct userfaultfd_fork_ctx *fctx;
594
595 octx = vma->vm_userfaultfd_ctx.ctx;
596 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
597 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
598 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
599 return 0;
600 }
601
602 list_for_each_entry(fctx, fcs, list)
603 if (fctx->orig == octx) {
604 ctx = fctx->new;
605 break;
606 }
607
608 if (!ctx) {
609 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
610 if (!fctx)
611 return -ENOMEM;
612
613 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
614 if (!ctx) {
615 kfree(fctx);
616 return -ENOMEM;
617 }
618
619 atomic_set(&ctx->refcount, 1);
620 ctx->flags = octx->flags;
621 ctx->state = UFFD_STATE_RUNNING;
622 ctx->features = octx->features;
623 ctx->released = false;
624 ctx->mm = vma->vm_mm;
Mike Rapoportd3aadc82017-02-22 15:42:30 -0800625 atomic_inc(&ctx->mm->mm_count);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800626
627 userfaultfd_ctx_get(octx);
628 fctx->orig = octx;
629 fctx->new = ctx;
630 list_add_tail(&fctx->list, fcs);
631 }
632
633 vma->vm_userfaultfd_ctx.ctx = ctx;
634 return 0;
635}
636
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800637static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800638{
639 struct userfaultfd_ctx *ctx = fctx->orig;
640 struct userfaultfd_wait_queue ewq;
641
642 msg_init(&ewq.msg);
643
644 ewq.msg.event = UFFD_EVENT_FORK;
645 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
646
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800647 userfaultfd_event_wait_completion(ctx, &ewq);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800648}
649
650void dup_userfaultfd_complete(struct list_head *fcs)
651{
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800652 struct userfaultfd_fork_ctx *fctx, *n;
653
654 list_for_each_entry_safe(fctx, n, fcs, list) {
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800655 dup_fctx(fctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800656 list_del(&fctx->list);
657 kfree(fctx);
658 }
659}
660
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800661void mremap_userfaultfd_prep(struct vm_area_struct *vma,
662 struct vm_userfaultfd_ctx *vm_ctx)
663{
664 struct userfaultfd_ctx *ctx;
665
666 ctx = vma->vm_userfaultfd_ctx.ctx;
667 if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
668 vm_ctx->ctx = ctx;
669 userfaultfd_ctx_get(ctx);
670 }
671}
672
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800673void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800674 unsigned long from, unsigned long to,
675 unsigned long len)
676{
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800677 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800678 struct userfaultfd_wait_queue ewq;
679
680 if (!ctx)
681 return;
682
683 if (to & ~PAGE_MASK) {
684 userfaultfd_ctx_put(ctx);
685 return;
686 }
687
688 msg_init(&ewq.msg);
689
690 ewq.msg.event = UFFD_EVENT_REMAP;
691 ewq.msg.arg.remap.from = from;
692 ewq.msg.arg.remap.to = to;
693 ewq.msg.arg.remap.len = len;
694
695 userfaultfd_event_wait_completion(ctx, &ewq);
696}
697
Mike Rapoportd8119142017-02-24 14:56:02 -0800698void userfaultfd_remove(struct vm_area_struct *vma,
699 struct vm_area_struct **prev,
700 unsigned long start, unsigned long end)
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800701{
702 struct mm_struct *mm = vma->vm_mm;
703 struct userfaultfd_ctx *ctx;
704 struct userfaultfd_wait_queue ewq;
705
706 ctx = vma->vm_userfaultfd_ctx.ctx;
Mike Rapoportd8119142017-02-24 14:56:02 -0800707 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800708 return;
709
710 userfaultfd_ctx_get(ctx);
711 up_read(&mm->mmap_sem);
712
713 *prev = NULL; /* We wait for ACK w/o the mmap semaphore */
714
715 msg_init(&ewq.msg);
716
Mike Rapoportd8119142017-02-24 14:56:02 -0800717 ewq.msg.event = UFFD_EVENT_REMOVE;
718 ewq.msg.arg.remove.start = start;
719 ewq.msg.arg.remove.end = end;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800720
721 userfaultfd_event_wait_completion(ctx, &ewq);
722
723 down_read(&mm->mmap_sem);
724}
725
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800726static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
727 unsigned long start, unsigned long end)
728{
729 struct userfaultfd_unmap_ctx *unmap_ctx;
730
731 list_for_each_entry(unmap_ctx, unmaps, list)
732 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
733 unmap_ctx->end == end)
734 return true;
735
736 return false;
737}
738
739int userfaultfd_unmap_prep(struct vm_area_struct *vma,
740 unsigned long start, unsigned long end,
741 struct list_head *unmaps)
742{
743 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
744 struct userfaultfd_unmap_ctx *unmap_ctx;
745 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
746
747 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
748 has_unmap_ctx(ctx, unmaps, start, end))
749 continue;
750
751 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
752 if (!unmap_ctx)
753 return -ENOMEM;
754
755 userfaultfd_ctx_get(ctx);
756 unmap_ctx->ctx = ctx;
757 unmap_ctx->start = start;
758 unmap_ctx->end = end;
759 list_add_tail(&unmap_ctx->list, unmaps);
760 }
761
762 return 0;
763}
764
765void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
766{
767 struct userfaultfd_unmap_ctx *ctx, *n;
768 struct userfaultfd_wait_queue ewq;
769
770 list_for_each_entry_safe(ctx, n, uf, list) {
771 msg_init(&ewq.msg);
772
773 ewq.msg.event = UFFD_EVENT_UNMAP;
774 ewq.msg.arg.remove.start = ctx->start;
775 ewq.msg.arg.remove.end = ctx->end;
776
777 userfaultfd_event_wait_completion(ctx->ctx, &ewq);
778
779 list_del(&ctx->list);
780 kfree(ctx);
781 }
782}
783
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700784static int userfaultfd_release(struct inode *inode, struct file *file)
785{
786 struct userfaultfd_ctx *ctx = file->private_data;
787 struct mm_struct *mm = ctx->mm;
788 struct vm_area_struct *vma, *prev;
789 /* len == 0 means wake all */
790 struct userfaultfd_wake_range range = { .len = 0, };
791 unsigned long new_flags;
792
793 ACCESS_ONCE(ctx->released) = true;
794
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700795 if (!mmget_not_zero(mm))
796 goto wakeup;
797
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700798 /*
799 * Flush page faults out of all CPUs. NOTE: all page faults
800 * must be retried without returning VM_FAULT_SIGBUS if
801 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
802 * changes while handle_userfault released the mmap_sem. So
803 * it's critical that released is set to true (above), before
804 * taking the mmap_sem for writing.
805 */
806 down_write(&mm->mmap_sem);
807 prev = NULL;
808 for (vma = mm->mmap; vma; vma = vma->vm_next) {
809 cond_resched();
810 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
811 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
812 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
813 prev = vma;
814 continue;
815 }
816 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
817 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
818 new_flags, vma->anon_vma,
819 vma->vm_file, vma->vm_pgoff,
820 vma_policy(vma),
821 NULL_VM_UFFD_CTX);
822 if (prev)
823 vma = prev;
824 else
825 prev = vma;
826 vma->vm_flags = new_flags;
827 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
828 }
829 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700830 mmput(mm);
831wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700832 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700833 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700834 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700835 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700836 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700837 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700838 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
839 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700840 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700841
842 wake_up_poll(&ctx->fd_wqh, POLLHUP);
843 userfaultfd_ctx_put(ctx);
844 return 0;
845}
846
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700847/* fault_pending_wqh.lock must be hold by the caller */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800848static inline struct userfaultfd_wait_queue *find_userfault_in(
849 wait_queue_head_t *wqh)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700850{
851 wait_queue_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700852 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700853
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800854 VM_BUG_ON(!spin_is_locked(&wqh->lock));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700855
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700856 uwq = NULL;
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800857 if (!waitqueue_active(wqh))
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700858 goto out;
859 /* walk in reverse to provide FIFO behavior to read userfaults */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800860 wq = list_last_entry(&wqh->task_list, typeof(*wq), task_list);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700861 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
862out:
863 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700864}
865
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800866static inline struct userfaultfd_wait_queue *find_userfault(
867 struct userfaultfd_ctx *ctx)
868{
869 return find_userfault_in(&ctx->fault_pending_wqh);
870}
871
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800872static inline struct userfaultfd_wait_queue *find_userfault_evt(
873 struct userfaultfd_ctx *ctx)
874{
875 return find_userfault_in(&ctx->event_wqh);
876}
877
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700878static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
879{
880 struct userfaultfd_ctx *ctx = file->private_data;
881 unsigned int ret;
882
883 poll_wait(file, &ctx->fd_wqh, wait);
884
885 switch (ctx->state) {
886 case UFFD_STATE_WAIT_API:
887 return POLLERR;
888 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700889 /*
890 * poll() never guarantees that read won't block.
891 * userfaults can be waken before they're read().
892 */
893 if (unlikely(!(file->f_flags & O_NONBLOCK)))
894 return POLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700895 /*
896 * lockless access to see if there are pending faults
897 * __pollwait last action is the add_wait_queue but
898 * the spin_unlock would allow the waitqueue_active to
899 * pass above the actual list_add inside
900 * add_wait_queue critical section. So use a full
901 * memory barrier to serialize the list_add write of
902 * add_wait_queue() with the waitqueue_active read
903 * below.
904 */
905 ret = 0;
906 smp_mb();
907 if (waitqueue_active(&ctx->fault_pending_wqh))
908 ret = POLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800909 else if (waitqueue_active(&ctx->event_wqh))
910 ret = POLLIN;
911
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700912 return ret;
913 default:
Andrea Arcangeli84749012017-02-22 15:42:12 -0800914 WARN_ON_ONCE(1);
915 return POLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700916 }
917}
918
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800919static const struct file_operations userfaultfd_fops;
920
921static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
922 struct userfaultfd_ctx *new,
923 struct uffd_msg *msg)
924{
925 int fd;
926 struct file *file;
927 unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
928
929 fd = get_unused_fd_flags(flags);
930 if (fd < 0)
931 return fd;
932
933 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
934 O_RDWR | flags);
935 if (IS_ERR(file)) {
936 put_unused_fd(fd);
937 return PTR_ERR(file);
938 }
939
940 fd_install(fd, file);
941 msg->arg.reserved.reserved1 = 0;
942 msg->arg.fork.ufd = fd;
943
944 return 0;
945}
946
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700947static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700948 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700949{
950 ssize_t ret;
951 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700952 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800953 /*
954 * Handling fork event requires sleeping operations, so
955 * we drop the event_wqh lock, then do these ops, then
956 * lock it back and wake up the waiter. While the lock is
957 * dropped the ewq may go away so we keep track of it
958 * carefully.
959 */
960 LIST_HEAD(fork_event);
961 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700962
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700963 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700964 spin_lock(&ctx->fd_wqh.lock);
965 __add_wait_queue(&ctx->fd_wqh, &wait);
966 for (;;) {
967 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700968 spin_lock(&ctx->fault_pending_wqh.lock);
969 uwq = find_userfault(ctx);
970 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700971 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700972 * Use a seqcount to repeat the lockless check
973 * in wake_userfault() to avoid missing
974 * wakeups because during the refile both
975 * waitqueue could become empty if this is the
976 * only userfault.
977 */
978 write_seqcount_begin(&ctx->refile_seq);
979
980 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700981 * The fault_pending_wqh.lock prevents the uwq
982 * to disappear from under us.
983 *
984 * Refile this userfault from
985 * fault_pending_wqh to fault_wqh, it's not
986 * pending anymore after we read it.
987 *
988 * Use list_del() by hand (as
989 * userfaultfd_wake_function also uses
990 * list_del_init() by hand) to be sure nobody
991 * changes __remove_wait_queue() to use
992 * list_del_init() in turn breaking the
993 * !list_empty_careful() check in
994 * handle_userfault(). The uwq->wq.task_list
995 * must never be empty at any time during the
996 * refile, or the waitqueue could disappear
997 * from under us. The "wait_queue_head_t"
998 * parameter of __remove_wait_queue() is unused
999 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001000 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001001 list_del(&uwq->wq.task_list);
1002 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
1003
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001004 write_seqcount_end(&ctx->refile_seq);
1005
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001006 /* careful to always initialize msg if ret == 0 */
1007 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001008 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001009 ret = 0;
1010 break;
1011 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001012 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001013
1014 spin_lock(&ctx->event_wqh.lock);
1015 uwq = find_userfault_evt(ctx);
1016 if (uwq) {
1017 *msg = uwq->msg;
1018
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001019 if (uwq->msg.event == UFFD_EVENT_FORK) {
1020 fork_nctx = (struct userfaultfd_ctx *)
1021 (unsigned long)
1022 uwq->msg.arg.reserved.reserved1;
1023 list_move(&uwq->wq.task_list, &fork_event);
1024 spin_unlock(&ctx->event_wqh.lock);
1025 ret = 0;
1026 break;
1027 }
1028
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001029 userfaultfd_event_complete(ctx, uwq);
1030 spin_unlock(&ctx->event_wqh.lock);
1031 ret = 0;
1032 break;
1033 }
1034 spin_unlock(&ctx->event_wqh.lock);
1035
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001036 if (signal_pending(current)) {
1037 ret = -ERESTARTSYS;
1038 break;
1039 }
1040 if (no_wait) {
1041 ret = -EAGAIN;
1042 break;
1043 }
1044 spin_unlock(&ctx->fd_wqh.lock);
1045 schedule();
1046 spin_lock(&ctx->fd_wqh.lock);
1047 }
1048 __remove_wait_queue(&ctx->fd_wqh, &wait);
1049 __set_current_state(TASK_RUNNING);
1050 spin_unlock(&ctx->fd_wqh.lock);
1051
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001052 if (!ret && msg->event == UFFD_EVENT_FORK) {
1053 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
1054
1055 if (!ret) {
1056 spin_lock(&ctx->event_wqh.lock);
1057 if (!list_empty(&fork_event)) {
1058 uwq = list_first_entry(&fork_event,
1059 typeof(*uwq),
1060 wq.task_list);
1061 list_del(&uwq->wq.task_list);
1062 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1063 userfaultfd_event_complete(ctx, uwq);
1064 }
1065 spin_unlock(&ctx->event_wqh.lock);
1066 }
1067 }
1068
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001069 return ret;
1070}
1071
1072static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1073 size_t count, loff_t *ppos)
1074{
1075 struct userfaultfd_ctx *ctx = file->private_data;
1076 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001077 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001078 int no_wait = file->f_flags & O_NONBLOCK;
1079
1080 if (ctx->state == UFFD_STATE_WAIT_API)
1081 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001082
1083 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001084 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001085 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001086 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001087 if (_ret < 0)
1088 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001089 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001090 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001091 ret += sizeof(msg);
1092 buf += sizeof(msg);
1093 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001094 /*
1095 * Allow to read more than one fault at time but only
1096 * block if waiting for the very first one.
1097 */
1098 no_wait = O_NONBLOCK;
1099 }
1100}
1101
1102static void __wake_userfault(struct userfaultfd_ctx *ctx,
1103 struct userfaultfd_wake_range *range)
1104{
1105 unsigned long start, end;
1106
1107 start = range->start;
1108 end = range->start + range->len;
1109
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001110 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001111 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001112 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001113 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001114 range);
1115 if (waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001116 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001117 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001118}
1119
1120static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1121 struct userfaultfd_wake_range *range)
1122{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001123 unsigned seq;
1124 bool need_wakeup;
1125
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001126 /*
1127 * To be sure waitqueue_active() is not reordered by the CPU
1128 * before the pagetable update, use an explicit SMP memory
1129 * barrier here. PT lock release or up_read(mmap_sem) still
1130 * have release semantics that can allow the
1131 * waitqueue_active() to be reordered before the pte update.
1132 */
1133 smp_mb();
1134
1135 /*
1136 * Use waitqueue_active because it's very frequent to
1137 * change the address space atomically even if there are no
1138 * userfaults yet. So we take the spinlock only when we're
1139 * sure we've userfaults to wake.
1140 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001141 do {
1142 seq = read_seqcount_begin(&ctx->refile_seq);
1143 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1144 waitqueue_active(&ctx->fault_wqh);
1145 cond_resched();
1146 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1147 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001148 __wake_userfault(ctx, range);
1149}
1150
1151static __always_inline int validate_range(struct mm_struct *mm,
1152 __u64 start, __u64 len)
1153{
1154 __u64 task_size = mm->task_size;
1155
1156 if (start & ~PAGE_MASK)
1157 return -EINVAL;
1158 if (len & ~PAGE_MASK)
1159 return -EINVAL;
1160 if (!len)
1161 return -EINVAL;
1162 if (start < mmap_min_addr)
1163 return -EINVAL;
1164 if (start >= task_size)
1165 return -EINVAL;
1166 if (len > task_size - start)
1167 return -EINVAL;
1168 return 0;
1169}
1170
Mike Rapoportba6907d2017-02-22 15:43:22 -08001171static inline bool vma_can_userfault(struct vm_area_struct *vma)
1172{
Mike Rapoportcac67322017-02-22 15:43:40 -08001173 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1174 vma_is_shmem(vma);
Mike Rapoportba6907d2017-02-22 15:43:22 -08001175}
1176
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001177static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1178 unsigned long arg)
1179{
1180 struct mm_struct *mm = ctx->mm;
1181 struct vm_area_struct *vma, *prev, *cur;
1182 int ret;
1183 struct uffdio_register uffdio_register;
1184 struct uffdio_register __user *user_uffdio_register;
1185 unsigned long vm_flags, new_flags;
1186 bool found;
Mike Rapoportcac67322017-02-22 15:43:40 -08001187 bool non_anon_pages;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001188 unsigned long start, end, vma_end;
1189
1190 user_uffdio_register = (struct uffdio_register __user *) arg;
1191
1192 ret = -EFAULT;
1193 if (copy_from_user(&uffdio_register, user_uffdio_register,
1194 sizeof(uffdio_register)-sizeof(__u64)))
1195 goto out;
1196
1197 ret = -EINVAL;
1198 if (!uffdio_register.mode)
1199 goto out;
1200 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1201 UFFDIO_REGISTER_MODE_WP))
1202 goto out;
1203 vm_flags = 0;
1204 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1205 vm_flags |= VM_UFFD_MISSING;
1206 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1207 vm_flags |= VM_UFFD_WP;
1208 /*
1209 * FIXME: remove the below error constraint by
1210 * implementing the wprotect tracking mode.
1211 */
1212 ret = -EINVAL;
1213 goto out;
1214 }
1215
1216 ret = validate_range(mm, uffdio_register.range.start,
1217 uffdio_register.range.len);
1218 if (ret)
1219 goto out;
1220
1221 start = uffdio_register.range.start;
1222 end = start + uffdio_register.range.len;
1223
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001224 ret = -ENOMEM;
1225 if (!mmget_not_zero(mm))
1226 goto out;
1227
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001228 down_write(&mm->mmap_sem);
1229 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001230 if (!vma)
1231 goto out_unlock;
1232
1233 /* check that there's at least one vma in the range */
1234 ret = -EINVAL;
1235 if (vma->vm_start >= end)
1236 goto out_unlock;
1237
1238 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001239 * If the first vma contains huge pages, make sure start address
1240 * is aligned to huge page size.
1241 */
1242 if (is_vm_hugetlb_page(vma)) {
1243 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1244
1245 if (start & (vma_hpagesize - 1))
1246 goto out_unlock;
1247 }
1248
1249 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001250 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001251 */
1252 found = false;
Mike Rapoportcac67322017-02-22 15:43:40 -08001253 non_anon_pages = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001254 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1255 cond_resched();
1256
1257 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1258 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1259
1260 /* check not compatible vmas */
1261 ret = -EINVAL;
Mike Rapoportba6907d2017-02-22 15:43:22 -08001262 if (!vma_can_userfault(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001263 goto out_unlock;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001264 /*
1265 * If this vma contains ending address, and huge pages
1266 * check alignment.
1267 */
1268 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1269 end > cur->vm_start) {
1270 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1271
1272 ret = -EINVAL;
1273
1274 if (end & (vma_hpagesize - 1))
1275 goto out_unlock;
1276 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001277
1278 /*
1279 * Check that this vma isn't already owned by a
1280 * different userfaultfd. We can't allow more than one
1281 * userfaultfd to own a single vma simultaneously or we
1282 * wouldn't know which one to deliver the userfaults to.
1283 */
1284 ret = -EBUSY;
1285 if (cur->vm_userfaultfd_ctx.ctx &&
1286 cur->vm_userfaultfd_ctx.ctx != ctx)
1287 goto out_unlock;
1288
Mike Kravetzcab350a2017-02-22 15:43:04 -08001289 /*
1290 * Note vmas containing huge pages
1291 */
Mike Rapoportcac67322017-02-22 15:43:40 -08001292 if (is_vm_hugetlb_page(cur) || vma_is_shmem(cur))
1293 non_anon_pages = true;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001294
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001295 found = true;
1296 }
1297 BUG_ON(!found);
1298
1299 if (vma->vm_start < start)
1300 prev = vma;
1301
1302 ret = 0;
1303 do {
1304 cond_resched();
1305
Mike Rapoportba6907d2017-02-22 15:43:22 -08001306 BUG_ON(!vma_can_userfault(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001307 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1308 vma->vm_userfaultfd_ctx.ctx != ctx);
1309
1310 /*
1311 * Nothing to do: this vma is already registered into this
1312 * userfaultfd and with the right tracking mode too.
1313 */
1314 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1315 (vma->vm_flags & vm_flags) == vm_flags)
1316 goto skip;
1317
1318 if (vma->vm_start > start)
1319 start = vma->vm_start;
1320 vma_end = min(end, vma->vm_end);
1321
1322 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
1323 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1324 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1325 vma_policy(vma),
1326 ((struct vm_userfaultfd_ctx){ ctx }));
1327 if (prev) {
1328 vma = prev;
1329 goto next;
1330 }
1331 if (vma->vm_start < start) {
1332 ret = split_vma(mm, vma, start, 1);
1333 if (ret)
1334 break;
1335 }
1336 if (vma->vm_end > end) {
1337 ret = split_vma(mm, vma, end, 0);
1338 if (ret)
1339 break;
1340 }
1341 next:
1342 /*
1343 * In the vma_merge() successful mprotect-like case 8:
1344 * the next vma was merged into the current one and
1345 * the current one has not been updated yet.
1346 */
1347 vma->vm_flags = new_flags;
1348 vma->vm_userfaultfd_ctx.ctx = ctx;
1349
1350 skip:
1351 prev = vma;
1352 start = vma->vm_end;
1353 vma = vma->vm_next;
1354 } while (vma && vma->vm_start < end);
1355out_unlock:
1356 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001357 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001358 if (!ret) {
1359 /*
1360 * Now that we scanned all vmas we can already tell
1361 * userland which ioctls methods are guaranteed to
1362 * succeed on this range.
1363 */
Mike Rapoportcac67322017-02-22 15:43:40 -08001364 if (put_user(non_anon_pages ? UFFD_API_RANGE_IOCTLS_BASIC :
Mike Kravetzcab350a2017-02-22 15:43:04 -08001365 UFFD_API_RANGE_IOCTLS,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001366 &user_uffdio_register->ioctls))
1367 ret = -EFAULT;
1368 }
1369out:
1370 return ret;
1371}
1372
1373static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1374 unsigned long arg)
1375{
1376 struct mm_struct *mm = ctx->mm;
1377 struct vm_area_struct *vma, *prev, *cur;
1378 int ret;
1379 struct uffdio_range uffdio_unregister;
1380 unsigned long new_flags;
1381 bool found;
1382 unsigned long start, end, vma_end;
1383 const void __user *buf = (void __user *)arg;
1384
1385 ret = -EFAULT;
1386 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1387 goto out;
1388
1389 ret = validate_range(mm, uffdio_unregister.start,
1390 uffdio_unregister.len);
1391 if (ret)
1392 goto out;
1393
1394 start = uffdio_unregister.start;
1395 end = start + uffdio_unregister.len;
1396
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001397 ret = -ENOMEM;
1398 if (!mmget_not_zero(mm))
1399 goto out;
1400
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001401 down_write(&mm->mmap_sem);
1402 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001403 if (!vma)
1404 goto out_unlock;
1405
1406 /* check that there's at least one vma in the range */
1407 ret = -EINVAL;
1408 if (vma->vm_start >= end)
1409 goto out_unlock;
1410
1411 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001412 * If the first vma contains huge pages, make sure start address
1413 * is aligned to huge page size.
1414 */
1415 if (is_vm_hugetlb_page(vma)) {
1416 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1417
1418 if (start & (vma_hpagesize - 1))
1419 goto out_unlock;
1420 }
1421
1422 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001423 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001424 */
1425 found = false;
1426 ret = -EINVAL;
1427 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1428 cond_resched();
1429
1430 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1431 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1432
1433 /*
1434 * Check not compatible vmas, not strictly required
1435 * here as not compatible vmas cannot have an
1436 * userfaultfd_ctx registered on them, but this
1437 * provides for more strict behavior to notice
1438 * unregistration errors.
1439 */
Mike Rapoportba6907d2017-02-22 15:43:22 -08001440 if (!vma_can_userfault(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001441 goto out_unlock;
1442
1443 found = true;
1444 }
1445 BUG_ON(!found);
1446
1447 if (vma->vm_start < start)
1448 prev = vma;
1449
1450 ret = 0;
1451 do {
1452 cond_resched();
1453
Mike Rapoportba6907d2017-02-22 15:43:22 -08001454 BUG_ON(!vma_can_userfault(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001455
1456 /*
1457 * Nothing to do: this vma is already registered into this
1458 * userfaultfd and with the right tracking mode too.
1459 */
1460 if (!vma->vm_userfaultfd_ctx.ctx)
1461 goto skip;
1462
1463 if (vma->vm_start > start)
1464 start = vma->vm_start;
1465 vma_end = min(end, vma->vm_end);
1466
Andrea Arcangeli09fa5292017-02-22 15:42:46 -08001467 if (userfaultfd_missing(vma)) {
1468 /*
1469 * Wake any concurrent pending userfault while
1470 * we unregister, so they will not hang
1471 * permanently and it avoids userland to call
1472 * UFFDIO_WAKE explicitly.
1473 */
1474 struct userfaultfd_wake_range range;
1475 range.start = start;
1476 range.len = vma_end - start;
1477 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1478 }
1479
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001480 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1481 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1482 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1483 vma_policy(vma),
1484 NULL_VM_UFFD_CTX);
1485 if (prev) {
1486 vma = prev;
1487 goto next;
1488 }
1489 if (vma->vm_start < start) {
1490 ret = split_vma(mm, vma, start, 1);
1491 if (ret)
1492 break;
1493 }
1494 if (vma->vm_end > end) {
1495 ret = split_vma(mm, vma, end, 0);
1496 if (ret)
1497 break;
1498 }
1499 next:
1500 /*
1501 * In the vma_merge() successful mprotect-like case 8:
1502 * the next vma was merged into the current one and
1503 * the current one has not been updated yet.
1504 */
1505 vma->vm_flags = new_flags;
1506 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1507
1508 skip:
1509 prev = vma;
1510 start = vma->vm_end;
1511 vma = vma->vm_next;
1512 } while (vma && vma->vm_start < end);
1513out_unlock:
1514 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001515 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001516out:
1517 return ret;
1518}
1519
1520/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001521 * userfaultfd_wake may be used in combination with the
1522 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001523 */
1524static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1525 unsigned long arg)
1526{
1527 int ret;
1528 struct uffdio_range uffdio_wake;
1529 struct userfaultfd_wake_range range;
1530 const void __user *buf = (void __user *)arg;
1531
1532 ret = -EFAULT;
1533 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1534 goto out;
1535
1536 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1537 if (ret)
1538 goto out;
1539
1540 range.start = uffdio_wake.start;
1541 range.len = uffdio_wake.len;
1542
1543 /*
1544 * len == 0 means wake all and we don't want to wake all here,
1545 * so check it again to be sure.
1546 */
1547 VM_BUG_ON(!range.len);
1548
1549 wake_userfault(ctx, &range);
1550 ret = 0;
1551
1552out:
1553 return ret;
1554}
1555
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001556static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1557 unsigned long arg)
1558{
1559 __s64 ret;
1560 struct uffdio_copy uffdio_copy;
1561 struct uffdio_copy __user *user_uffdio_copy;
1562 struct userfaultfd_wake_range range;
1563
1564 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1565
1566 ret = -EFAULT;
1567 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1568 /* don't copy "copy" last field */
1569 sizeof(uffdio_copy)-sizeof(__s64)))
1570 goto out;
1571
1572 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1573 if (ret)
1574 goto out;
1575 /*
1576 * double check for wraparound just in case. copy_from_user()
1577 * will later check uffdio_copy.src + uffdio_copy.len to fit
1578 * in the userland range.
1579 */
1580 ret = -EINVAL;
1581 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1582 goto out;
1583 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1584 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001585 if (mmget_not_zero(ctx->mm)) {
1586 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1587 uffdio_copy.len);
1588 mmput(ctx->mm);
Mike Rapoport96333182017-02-24 14:58:31 -08001589 } else {
1590 return -ENOSPC;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001591 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001592 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1593 return -EFAULT;
1594 if (ret < 0)
1595 goto out;
1596 BUG_ON(!ret);
1597 /* len == 0 would wake all */
1598 range.len = ret;
1599 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1600 range.start = uffdio_copy.dst;
1601 wake_userfault(ctx, &range);
1602 }
1603 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1604out:
1605 return ret;
1606}
1607
1608static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1609 unsigned long arg)
1610{
1611 __s64 ret;
1612 struct uffdio_zeropage uffdio_zeropage;
1613 struct uffdio_zeropage __user *user_uffdio_zeropage;
1614 struct userfaultfd_wake_range range;
1615
1616 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1617
1618 ret = -EFAULT;
1619 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1620 /* don't copy "zeropage" last field */
1621 sizeof(uffdio_zeropage)-sizeof(__s64)))
1622 goto out;
1623
1624 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1625 uffdio_zeropage.range.len);
1626 if (ret)
1627 goto out;
1628 ret = -EINVAL;
1629 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1630 goto out;
1631
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001632 if (mmget_not_zero(ctx->mm)) {
1633 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1634 uffdio_zeropage.range.len);
1635 mmput(ctx->mm);
1636 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001637 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1638 return -EFAULT;
1639 if (ret < 0)
1640 goto out;
1641 /* len == 0 would wake all */
1642 BUG_ON(!ret);
1643 range.len = ret;
1644 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1645 range.start = uffdio_zeropage.range.start;
1646 wake_userfault(ctx, &range);
1647 }
1648 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1649out:
1650 return ret;
1651}
1652
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001653static inline unsigned int uffd_ctx_features(__u64 user_features)
1654{
1655 /*
1656 * For the current set of features the bits just coincide
1657 */
1658 return (unsigned int)user_features;
1659}
1660
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001661/*
1662 * userland asks for a certain API version and we return which bits
1663 * and ioctl commands are implemented in this kernel for such API
1664 * version or -EINVAL if unknown.
1665 */
1666static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1667 unsigned long arg)
1668{
1669 struct uffdio_api uffdio_api;
1670 void __user *buf = (void __user *)arg;
1671 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001672 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001673
1674 ret = -EINVAL;
1675 if (ctx->state != UFFD_STATE_WAIT_API)
1676 goto out;
1677 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001678 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001679 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001680 features = uffdio_api.features;
1681 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001682 memset(&uffdio_api, 0, sizeof(uffdio_api));
1683 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1684 goto out;
1685 ret = -EINVAL;
1686 goto out;
1687 }
Andrea Arcangeli65603142017-02-22 15:42:24 -08001688 /* report all available features and ioctls to userland */
1689 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001690 uffdio_api.ioctls = UFFD_API_IOCTLS;
1691 ret = -EFAULT;
1692 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1693 goto out;
1694 ctx->state = UFFD_STATE_RUNNING;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001695 /* only enable the requested features for this uffd context */
1696 ctx->features = uffd_ctx_features(features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001697 ret = 0;
1698out:
1699 return ret;
1700}
1701
1702static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1703 unsigned long arg)
1704{
1705 int ret = -EINVAL;
1706 struct userfaultfd_ctx *ctx = file->private_data;
1707
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001708 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1709 return -EINVAL;
1710
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001711 switch(cmd) {
1712 case UFFDIO_API:
1713 ret = userfaultfd_api(ctx, arg);
1714 break;
1715 case UFFDIO_REGISTER:
1716 ret = userfaultfd_register(ctx, arg);
1717 break;
1718 case UFFDIO_UNREGISTER:
1719 ret = userfaultfd_unregister(ctx, arg);
1720 break;
1721 case UFFDIO_WAKE:
1722 ret = userfaultfd_wake(ctx, arg);
1723 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001724 case UFFDIO_COPY:
1725 ret = userfaultfd_copy(ctx, arg);
1726 break;
1727 case UFFDIO_ZEROPAGE:
1728 ret = userfaultfd_zeropage(ctx, arg);
1729 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001730 }
1731 return ret;
1732}
1733
1734#ifdef CONFIG_PROC_FS
1735static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1736{
1737 struct userfaultfd_ctx *ctx = f->private_data;
1738 wait_queue_t *wq;
1739 struct userfaultfd_wait_queue *uwq;
1740 unsigned long pending = 0, total = 0;
1741
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001742 spin_lock(&ctx->fault_pending_wqh.lock);
1743 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001744 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001745 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001746 total++;
1747 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001748 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1749 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1750 total++;
1751 }
1752 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001753
1754 /*
1755 * If more protocols will be added, there will be all shown
1756 * separated by a space. Like this:
1757 * protocols: aa:... bb:...
1758 */
1759 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001760 pending, total, UFFD_API, UFFD_API_FEATURES,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001761 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1762}
1763#endif
1764
1765static const struct file_operations userfaultfd_fops = {
1766#ifdef CONFIG_PROC_FS
1767 .show_fdinfo = userfaultfd_show_fdinfo,
1768#endif
1769 .release = userfaultfd_release,
1770 .poll = userfaultfd_poll,
1771 .read = userfaultfd_read,
1772 .unlocked_ioctl = userfaultfd_ioctl,
1773 .compat_ioctl = userfaultfd_ioctl,
1774 .llseek = noop_llseek,
1775};
1776
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001777static void init_once_userfaultfd_ctx(void *mem)
1778{
1779 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1780
1781 init_waitqueue_head(&ctx->fault_pending_wqh);
1782 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001783 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001784 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001785 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001786}
1787
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001788/**
Masahiro Yamada9332ef92017-02-27 14:28:47 -08001789 * userfaultfd_file_create - Creates a userfaultfd file pointer.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001790 * @flags: Flags for the userfaultfd file.
1791 *
Masahiro Yamada9332ef92017-02-27 14:28:47 -08001792 * This function creates a userfaultfd file pointer, w/out installing
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001793 * it into the fd table. This is useful when the userfaultfd file is
1794 * used during the initialization of data structures that require
1795 * extra setup after the userfaultfd creation. So the userfaultfd
1796 * creation is split into the file pointer creation phase, and the
1797 * file descriptor installation phase. In this way races with
1798 * userspace closing the newly installed file descriptor can be
Masahiro Yamada9332ef92017-02-27 14:28:47 -08001799 * avoided. Returns a userfaultfd file pointer, or a proper error
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001800 * pointer.
1801 */
1802static struct file *userfaultfd_file_create(int flags)
1803{
1804 struct file *file;
1805 struct userfaultfd_ctx *ctx;
1806
1807 BUG_ON(!current->mm);
1808
1809 /* Check the UFFD_* constants for consistency. */
1810 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1811 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1812
1813 file = ERR_PTR(-EINVAL);
1814 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1815 goto out;
1816
1817 file = ERR_PTR(-ENOMEM);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001818 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001819 if (!ctx)
1820 goto out;
1821
1822 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001823 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001824 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001825 ctx->state = UFFD_STATE_WAIT_API;
1826 ctx->released = false;
1827 ctx->mm = current->mm;
1828 /* prevent the mm struct to be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001829 mmgrab(ctx->mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001830
1831 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1832 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
Eric Biggersc03e9462015-09-17 16:01:54 -07001833 if (IS_ERR(file)) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001834 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001835 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001836 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001837out:
1838 return file;
1839}
1840
1841SYSCALL_DEFINE1(userfaultfd, int, flags)
1842{
1843 int fd, error;
1844 struct file *file;
1845
1846 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1847 if (error < 0)
1848 return error;
1849 fd = error;
1850
1851 file = userfaultfd_file_create(flags);
1852 if (IS_ERR(file)) {
1853 error = PTR_ERR(file);
1854 goto err_put_unused_fd;
1855 }
1856 fd_install(fd, file);
1857
1858 return fd;
1859
1860err_put_unused_fd:
1861 put_unused_fd(fd);
1862
1863 return error;
1864}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001865
1866static int __init userfaultfd_init(void)
1867{
1868 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1869 sizeof(struct userfaultfd_ctx),
1870 0,
1871 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1872 init_once_userfaultfd_ctx);
1873 return 0;
1874}
1875__initcall(userfaultfd_init);