blob: a9d0ddc12aced408107cd0fe384f5cc51cfffd75 [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;
Ingo Molnarac6424b2017-06-20 12:06:13 +020084 wait_queue_entry_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
Ingo Molnarac6424b2017-06-20 12:06:13 +020094static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070095 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 /*
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200112 * The Program-Order guarantees provided by the scheduler
113 * ensure uwq->waken is visible before the task is woken.
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800114 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700115 ret = wake_up_state(wq->private, mode);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200116 if (ret) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700117 /*
118 * Wake only once, autoremove behavior.
119 *
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200120 * After the effect of list_del_init is visible to the other
121 * CPUs, the waitqueue may disappear from under us, see the
122 * !list_empty_careful() in handle_userfault().
123 *
124 * try_to_wake_up() has an implicit smp_mb(), and the
125 * wq->private is read before calling the extern function
126 * "wake_up_state" (which in turns calls try_to_wake_up).
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700127 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200128 list_del_init(&wq->entry);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200129 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700130out:
131 return ret;
132}
133
134/**
135 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
136 * context.
137 * @ctx: [in] Pointer to the userfaultfd context.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700138 */
139static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
140{
141 if (!atomic_inc_not_zero(&ctx->refcount))
142 BUG();
143}
144
145/**
146 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
147 * context.
148 * @ctx: [in] Pointer to userfaultfd context.
149 *
150 * The userfaultfd context reference must have been previously acquired either
151 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
152 */
153static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
154{
155 if (atomic_dec_and_test(&ctx->refcount)) {
156 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
157 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
158 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
159 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800160 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
161 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700162 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
163 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700164 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700165 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700166 }
167}
168
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700169static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700170{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700171 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
172 /*
173 * Must use memset to zero out the paddings or kernel data is
174 * leaked to userland.
175 */
176 memset(msg, 0, sizeof(struct uffd_msg));
177}
178
179static inline struct uffd_msg userfault_msg(unsigned long address,
180 unsigned int flags,
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700181 unsigned long reason,
182 unsigned int features)
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700183{
184 struct uffd_msg msg;
185 msg_init(&msg);
186 msg.event = UFFD_EVENT_PAGEFAULT;
187 msg.arg.pagefault.address = address;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700188 if (flags & FAULT_FLAG_WRITE)
189 /*
Andrea Arcangelia4605a62017-02-22 15:42:09 -0800190 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700191 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
192 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
193 * was a read fault, otherwise if set it means it's
194 * a write fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700195 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700196 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700197 if (reason & VM_UFFD_WP)
198 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700199 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
200 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
201 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
202 * a missing fault, otherwise if set it means it's a
203 * write protect fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700204 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700205 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700206 if (features & UFFD_FEATURE_THREAD_ID)
Andrea Arcangelia36985d2017-09-06 16:23:59 -0700207 msg.arg.pagefault.feat.ptid = task_pid_vnr(current);
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700208 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700209}
210
Mike Kravetz369cd2122017-02-22 15:43:10 -0800211#ifdef CONFIG_HUGETLB_PAGE
212/*
213 * Same functionality as userfaultfd_must_wait below with modifications for
214 * hugepmd ranges.
215 */
216static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700217 struct vm_area_struct *vma,
Mike Kravetz369cd2122017-02-22 15:43:10 -0800218 unsigned long address,
219 unsigned long flags,
220 unsigned long reason)
221{
222 struct mm_struct *mm = ctx->mm;
223 pte_t *pte;
224 bool ret = true;
225
226 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
227
Punit Agrawal7868a202017-07-06 15:39:42 -0700228 pte = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
Mike Kravetz369cd2122017-02-22 15:43:10 -0800229 if (!pte)
230 goto out;
231
232 ret = false;
233
234 /*
235 * Lockless access: we're in a wait_event so it's ok if it
236 * changes under us.
237 */
238 if (huge_pte_none(*pte))
239 ret = true;
240 if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
241 ret = true;
242out:
243 return ret;
244}
245#else
246static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700247 struct vm_area_struct *vma,
Mike Kravetz369cd2122017-02-22 15:43:10 -0800248 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;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300270 p4d_t *p4d;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700271 pud_t *pud;
272 pmd_t *pmd, _pmd;
273 pte_t *pte;
274 bool ret = true;
275
276 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
277
278 pgd = pgd_offset(mm, address);
279 if (!pgd_present(*pgd))
280 goto out;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300281 p4d = p4d_offset(pgd, address);
282 if (!p4d_present(*p4d))
283 goto out;
284 pud = pud_offset(p4d, address);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700285 if (!pud_present(*pud))
286 goto out;
287 pmd = pmd_offset(pud, address);
288 /*
289 * READ_ONCE must function as a barrier with narrower scope
290 * and it must be equivalent to:
291 * _pmd = *pmd; barrier();
292 *
293 * This is to deal with the instability (as in
294 * pmd_trans_unstable) of the pmd.
295 */
296 _pmd = READ_ONCE(*pmd);
Huang Yinga365ac02018-01-31 16:17:32 -0800297 if (pmd_none(_pmd))
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700298 goto out;
299
300 ret = false;
Huang Yinga365ac02018-01-31 16:17:32 -0800301 if (!pmd_present(_pmd))
302 goto out;
303
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700304 if (pmd_trans_huge(_pmd))
305 goto out;
306
307 /*
308 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
309 * and use the standard pte_offset_map() instead of parsing _pmd.
310 */
311 pte = pte_offset_map(pmd, address);
312 /*
313 * Lockless access: we're in a wait_event so it's ok if it
314 * changes under us.
315 */
316 if (pte_none(*pte))
317 ret = true;
318 pte_unmap(pte);
319
320out:
321 return ret;
322}
323
324/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700325 * The locking rules involved in returning VM_FAULT_RETRY depending on
326 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
327 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
328 * recommendation in __lock_page_or_retry is not an understatement.
329 *
330 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
331 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
332 * not set.
333 *
334 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
335 * set, VM_FAULT_RETRY can still be returned if and only if there are
336 * fatal_signal_pending()s, and the mmap_sem must be released before
337 * returning it.
338 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800339int handle_userfault(struct vm_fault *vmf, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700340{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800341 struct mm_struct *mm = vmf->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700342 struct userfaultfd_ctx *ctx;
343 struct userfaultfd_wait_queue uwq;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700344 int ret;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700345 bool must_wait, return_to_userland;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800346 long blocking_state;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700347
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700348 ret = VM_FAULT_SIGBUS;
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700349
350 /*
351 * We don't do userfault handling for the final child pid update.
352 *
353 * We also don't do userfault handling during
354 * coredumping. hugetlbfs has the special
355 * follow_hugetlb_page() to skip missing pages in the
356 * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
357 * the no_page_table() helper in follow_page_mask(), but the
358 * shmem_vm_ops->fault method is invoked even during
359 * coredumping without mmap_sem and it ends up here.
360 */
361 if (current->flags & (PF_EXITING|PF_DUMPCORE))
362 goto out;
363
364 /*
365 * Coredumping runs without mmap_sem so we can only check that
366 * the mmap_sem is held, if PF_DUMPCORE was not set.
367 */
368 WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem));
369
Jan Kara82b0f8c2016-12-14 15:06:58 -0800370 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700371 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700372 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700373
374 BUG_ON(ctx->mm != mm);
375
376 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
377 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
378
Prakash Sangappa2d6d6f52017-09-06 16:23:39 -0700379 if (ctx->features & UFFD_FEATURE_SIGBUS)
380 goto out;
381
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700382 /*
383 * If it's already released don't get it. This avoids to loop
384 * in __get_user_pages if userfaultfd_release waits on the
385 * caller of handle_userfault to release the mmap_sem.
386 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700387 if (unlikely(READ_ONCE(ctx->released))) {
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700388 /*
389 * Don't return VM_FAULT_SIGBUS in this case, so a non
390 * cooperative manager can close the uffd after the
391 * last UFFDIO_COPY, without risking to trigger an
392 * involuntary SIGBUS if the process was starting the
393 * userfaultfd while the userfaultfd was still armed
394 * (but after the last UFFDIO_COPY). If the uffd
395 * wasn't already closed when the userfault reached
396 * this point, that would normally be solved by
397 * userfaultfd_must_wait returning 'false'.
398 *
399 * If we were to return VM_FAULT_SIGBUS here, the non
400 * cooperative manager would be instead forced to
401 * always call UFFDIO_UNREGISTER before it can safely
402 * close the uffd.
403 */
404 ret = VM_FAULT_NOPAGE;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700405 goto out;
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700406 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700407
408 /*
409 * Check that we can return VM_FAULT_RETRY.
410 *
411 * NOTE: it should become possible to return VM_FAULT_RETRY
412 * even if FAULT_FLAG_TRIED is set without leading to gup()
413 * -EBUSY failures, if the userfaultfd is to be extended for
414 * VM_UFFD_WP tracking and we intend to arm the userfault
415 * without first stopping userland access to the memory. For
416 * VM_UFFD_MISSING userfaults this is enough for now.
417 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800418 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700419 /*
420 * Validate the invariant that nowait must allow retry
421 * to be sure not to return SIGBUS erroneously on
422 * nowait invocations.
423 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800424 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700425#ifdef CONFIG_DEBUG_VM
426 if (printk_ratelimit()) {
427 printk(KERN_WARNING
Jan Kara82b0f8c2016-12-14 15:06:58 -0800428 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
429 vmf->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700430 dump_stack();
431 }
432#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700433 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700434 }
435
436 /*
437 * Handle nowait, not much to do other than tell it to retry
438 * and wait.
439 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700440 ret = VM_FAULT_RETRY;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800441 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700442 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700443
444 /* take the reference before dropping the mmap_sem */
445 userfaultfd_ctx_get(ctx);
446
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700447 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
448 uwq.wq.private = current;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700449 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason,
450 ctx->features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700451 uwq.ctx = ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800452 uwq.waken = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700453
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700454 return_to_userland =
Jan Kara82b0f8c2016-12-14 15:06:58 -0800455 (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700456 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800457 blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
458 TASK_KILLABLE;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700459
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700460 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700461 /*
462 * After the __add_wait_queue the uwq is visible to userland
463 * through poll/read().
464 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700465 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
466 /*
467 * The smp_mb() after __set_current_state prevents the reads
468 * following the spin_unlock to happen before the list_add in
469 * __add_wait_queue.
470 */
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800471 set_current_state(blocking_state);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700472 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700473
Mike Kravetz369cd2122017-02-22 15:43:10 -0800474 if (!is_vm_hugetlb_page(vmf->vma))
475 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
476 reason);
477 else
Punit Agrawal7868a202017-07-06 15:39:42 -0700478 must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma,
479 vmf->address,
Mike Kravetz369cd2122017-02-22 15:43:10 -0800480 vmf->flags, reason);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700481 up_read(&mm->mmap_sem);
482
Mark Rutland6aa7de02017-10-23 14:07:29 -0700483 if (likely(must_wait && !READ_ONCE(ctx->released) &&
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700484 (return_to_userland ? !signal_pending(current) :
485 !fatal_signal_pending(current)))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700486 wake_up_poll(&ctx->fd_wqh, POLLIN);
487 schedule();
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700488 ret |= VM_FAULT_MAJOR;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800489
490 /*
491 * False wakeups can orginate even from rwsem before
492 * up_read() however userfaults will wait either for a
493 * targeted wakeup on the specific uwq waitqueue from
494 * wake_userfault() or for signals or for uffd
495 * release.
496 */
497 while (!READ_ONCE(uwq.waken)) {
498 /*
499 * This needs the full smp_store_mb()
500 * guarantee as the state write must be
501 * visible to other CPUs before reading
502 * uwq.waken from other CPUs.
503 */
504 set_current_state(blocking_state);
505 if (READ_ONCE(uwq.waken) ||
506 READ_ONCE(ctx->released) ||
507 (return_to_userland ? signal_pending(current) :
508 fatal_signal_pending(current)))
509 break;
510 schedule();
511 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700512 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700513
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700514 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700515
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700516 if (return_to_userland) {
517 if (signal_pending(current) &&
518 !fatal_signal_pending(current)) {
519 /*
520 * If we got a SIGSTOP or SIGCONT and this is
521 * a normal userland page fault, just let
522 * userland return so the signal will be
523 * handled and gdb debugging works. The page
524 * fault code immediately after we return from
525 * this function is going to release the
526 * mmap_sem and it's not depending on it
527 * (unlike gup would if we were not to return
528 * VM_FAULT_RETRY).
529 *
530 * If a fatal signal is pending we still take
531 * the streamlined VM_FAULT_RETRY failure path
532 * and there's no need to retake the mmap_sem
533 * in such case.
534 */
535 down_read(&mm->mmap_sem);
Andrea Arcangeli6bbc4a42017-03-09 16:16:28 -0800536 ret = VM_FAULT_NOPAGE;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700537 }
538 }
539
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700540 /*
541 * Here we race with the list_del; list_add in
542 * userfaultfd_ctx_read(), however because we don't ever run
543 * list_del_init() to refile across the two lists, the prev
544 * and next pointers will never point to self. list_add also
545 * would never let any of the two pointers to point to
546 * self. So list_empty_careful won't risk to see both pointers
547 * pointing to self at any time during the list refile. The
548 * only case where list_del_init() is called is the full
549 * removal in the wake function and there we don't re-list_add
550 * and it's fine not to block on the spinlock. The uwq on this
551 * kernel stack can be released after the list_del_init.
552 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200553 if (!list_empty_careful(&uwq.wq.entry)) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700554 spin_lock(&ctx->fault_pending_wqh.lock);
555 /*
556 * No need of list_del_init(), the uwq on the stack
557 * will be freed shortly anyway.
558 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200559 list_del(&uwq.wq.entry);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700560 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700561 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700562
563 /*
564 * ctx may go away after this if the userfault pseudo fd is
565 * already released.
566 */
567 userfaultfd_ctx_put(ctx);
568
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700569out:
570 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700571}
572
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800573static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
574 struct userfaultfd_wait_queue *ewq)
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800575{
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800576 struct userfaultfd_ctx *release_new_ctx;
577
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800578 if (WARN_ON_ONCE(current->flags & PF_EXITING))
579 goto out;
580
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800581 ewq->ctx = ctx;
582 init_waitqueue_entry(&ewq->wq, current);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800583 release_new_ctx = NULL;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800584
585 spin_lock(&ctx->event_wqh.lock);
586 /*
587 * After the __add_wait_queue the uwq is visible to userland
588 * through poll/read().
589 */
590 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
591 for (;;) {
592 set_current_state(TASK_KILLABLE);
593 if (ewq->msg.event == 0)
594 break;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700595 if (READ_ONCE(ctx->released) ||
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800596 fatal_signal_pending(current)) {
Andrea Arcangeli384632e2017-10-03 16:15:38 -0700597 /*
598 * &ewq->wq may be queued in fork_event, but
599 * __remove_wait_queue ignores the head
600 * parameter. It would be a problem if it
601 * didn't.
602 */
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800603 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800604 if (ewq->msg.event == UFFD_EVENT_FORK) {
605 struct userfaultfd_ctx *new;
606
607 new = (struct userfaultfd_ctx *)
608 (unsigned long)
609 ewq->msg.arg.reserved.reserved1;
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800610 release_new_ctx = new;
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800611 }
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800612 break;
613 }
614
615 spin_unlock(&ctx->event_wqh.lock);
616
617 wake_up_poll(&ctx->fd_wqh, POLLIN);
618 schedule();
619
620 spin_lock(&ctx->event_wqh.lock);
621 }
622 __set_current_state(TASK_RUNNING);
623 spin_unlock(&ctx->event_wqh.lock);
624
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800625 if (release_new_ctx) {
626 struct vm_area_struct *vma;
627 struct mm_struct *mm = release_new_ctx->mm;
628
629 /* the various vma->vm_userfaultfd_ctx still points to it */
630 down_write(&mm->mmap_sem);
631 for (vma = mm->mmap; vma; vma = vma->vm_next)
632 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx)
633 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
634 up_write(&mm->mmap_sem);
635
636 userfaultfd_ctx_put(release_new_ctx);
637 }
638
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800639 /*
640 * ctx may go away after this if the userfault pseudo fd is
641 * already released.
642 */
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800643out:
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800644 userfaultfd_ctx_put(ctx);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800645}
646
647static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
648 struct userfaultfd_wait_queue *ewq)
649{
650 ewq->msg.event = 0;
651 wake_up_locked(&ctx->event_wqh);
652 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
653}
654
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800655int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
656{
657 struct userfaultfd_ctx *ctx = NULL, *octx;
658 struct userfaultfd_fork_ctx *fctx;
659
660 octx = vma->vm_userfaultfd_ctx.ctx;
661 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
662 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
663 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
664 return 0;
665 }
666
667 list_for_each_entry(fctx, fcs, list)
668 if (fctx->orig == octx) {
669 ctx = fctx->new;
670 break;
671 }
672
673 if (!ctx) {
674 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
675 if (!fctx)
676 return -ENOMEM;
677
678 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
679 if (!ctx) {
680 kfree(fctx);
681 return -ENOMEM;
682 }
683
684 atomic_set(&ctx->refcount, 1);
685 ctx->flags = octx->flags;
686 ctx->state = UFFD_STATE_RUNNING;
687 ctx->features = octx->features;
688 ctx->released = false;
689 ctx->mm = vma->vm_mm;
Mike Rapoport00bb31f2017-11-15 17:36:56 -0800690 mmgrab(ctx->mm);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800691
692 userfaultfd_ctx_get(octx);
693 fctx->orig = octx;
694 fctx->new = ctx;
695 list_add_tail(&fctx->list, fcs);
696 }
697
698 vma->vm_userfaultfd_ctx.ctx = ctx;
699 return 0;
700}
701
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800702static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800703{
704 struct userfaultfd_ctx *ctx = fctx->orig;
705 struct userfaultfd_wait_queue ewq;
706
707 msg_init(&ewq.msg);
708
709 ewq.msg.event = UFFD_EVENT_FORK;
710 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
711
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800712 userfaultfd_event_wait_completion(ctx, &ewq);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800713}
714
715void dup_userfaultfd_complete(struct list_head *fcs)
716{
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800717 struct userfaultfd_fork_ctx *fctx, *n;
718
719 list_for_each_entry_safe(fctx, n, fcs, list) {
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800720 dup_fctx(fctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800721 list_del(&fctx->list);
722 kfree(fctx);
723 }
724}
725
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800726void mremap_userfaultfd_prep(struct vm_area_struct *vma,
727 struct vm_userfaultfd_ctx *vm_ctx)
728{
729 struct userfaultfd_ctx *ctx;
730
731 ctx = vma->vm_userfaultfd_ctx.ctx;
732 if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
733 vm_ctx->ctx = ctx;
734 userfaultfd_ctx_get(ctx);
735 }
736}
737
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800738void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800739 unsigned long from, unsigned long to,
740 unsigned long len)
741{
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800742 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800743 struct userfaultfd_wait_queue ewq;
744
745 if (!ctx)
746 return;
747
748 if (to & ~PAGE_MASK) {
749 userfaultfd_ctx_put(ctx);
750 return;
751 }
752
753 msg_init(&ewq.msg);
754
755 ewq.msg.event = UFFD_EVENT_REMAP;
756 ewq.msg.arg.remap.from = from;
757 ewq.msg.arg.remap.to = to;
758 ewq.msg.arg.remap.len = len;
759
760 userfaultfd_event_wait_completion(ctx, &ewq);
761}
762
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800763bool userfaultfd_remove(struct vm_area_struct *vma,
Mike Rapoportd8119142017-02-24 14:56:02 -0800764 unsigned long start, unsigned long end)
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800765{
766 struct mm_struct *mm = vma->vm_mm;
767 struct userfaultfd_ctx *ctx;
768 struct userfaultfd_wait_queue ewq;
769
770 ctx = vma->vm_userfaultfd_ctx.ctx;
Mike Rapoportd8119142017-02-24 14:56:02 -0800771 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800772 return true;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800773
774 userfaultfd_ctx_get(ctx);
775 up_read(&mm->mmap_sem);
776
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800777 msg_init(&ewq.msg);
778
Mike Rapoportd8119142017-02-24 14:56:02 -0800779 ewq.msg.event = UFFD_EVENT_REMOVE;
780 ewq.msg.arg.remove.start = start;
781 ewq.msg.arg.remove.end = end;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800782
783 userfaultfd_event_wait_completion(ctx, &ewq);
784
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800785 return false;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800786}
787
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800788static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
789 unsigned long start, unsigned long end)
790{
791 struct userfaultfd_unmap_ctx *unmap_ctx;
792
793 list_for_each_entry(unmap_ctx, unmaps, list)
794 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
795 unmap_ctx->end == end)
796 return true;
797
798 return false;
799}
800
801int userfaultfd_unmap_prep(struct vm_area_struct *vma,
802 unsigned long start, unsigned long end,
803 struct list_head *unmaps)
804{
805 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
806 struct userfaultfd_unmap_ctx *unmap_ctx;
807 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
808
809 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
810 has_unmap_ctx(ctx, unmaps, start, end))
811 continue;
812
813 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
814 if (!unmap_ctx)
815 return -ENOMEM;
816
817 userfaultfd_ctx_get(ctx);
818 unmap_ctx->ctx = ctx;
819 unmap_ctx->start = start;
820 unmap_ctx->end = end;
821 list_add_tail(&unmap_ctx->list, unmaps);
822 }
823
824 return 0;
825}
826
827void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
828{
829 struct userfaultfd_unmap_ctx *ctx, *n;
830 struct userfaultfd_wait_queue ewq;
831
832 list_for_each_entry_safe(ctx, n, uf, list) {
833 msg_init(&ewq.msg);
834
835 ewq.msg.event = UFFD_EVENT_UNMAP;
836 ewq.msg.arg.remove.start = ctx->start;
837 ewq.msg.arg.remove.end = ctx->end;
838
839 userfaultfd_event_wait_completion(ctx->ctx, &ewq);
840
841 list_del(&ctx->list);
842 kfree(ctx);
843 }
844}
845
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700846static int userfaultfd_release(struct inode *inode, struct file *file)
847{
848 struct userfaultfd_ctx *ctx = file->private_data;
849 struct mm_struct *mm = ctx->mm;
850 struct vm_area_struct *vma, *prev;
851 /* len == 0 means wake all */
852 struct userfaultfd_wake_range range = { .len = 0, };
853 unsigned long new_flags;
854
Mark Rutland6aa7de02017-10-23 14:07:29 -0700855 WRITE_ONCE(ctx->released, true);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700856
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700857 if (!mmget_not_zero(mm))
858 goto wakeup;
859
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700860 /*
861 * Flush page faults out of all CPUs. NOTE: all page faults
862 * must be retried without returning VM_FAULT_SIGBUS if
863 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
864 * changes while handle_userfault released the mmap_sem. So
865 * it's critical that released is set to true (above), before
866 * taking the mmap_sem for writing.
867 */
868 down_write(&mm->mmap_sem);
869 prev = NULL;
870 for (vma = mm->mmap; vma; vma = vma->vm_next) {
871 cond_resched();
872 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
873 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
874 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
875 prev = vma;
876 continue;
877 }
878 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
879 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
880 new_flags, vma->anon_vma,
881 vma->vm_file, vma->vm_pgoff,
882 vma_policy(vma),
883 NULL_VM_UFFD_CTX);
884 if (prev)
885 vma = prev;
886 else
887 prev = vma;
888 vma->vm_flags = new_flags;
889 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
890 }
891 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700892 mmput(mm);
893wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700894 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700895 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700896 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700897 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700898 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700899 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700900 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
901 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700902 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700903
Mike Rapoport5a18b642017-08-02 13:32:24 -0700904 /* Flush pending events that may still wait on event_wqh */
905 wake_up_all(&ctx->event_wqh);
906
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700907 wake_up_poll(&ctx->fd_wqh, POLLHUP);
908 userfaultfd_ctx_put(ctx);
909 return 0;
910}
911
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700912/* fault_pending_wqh.lock must be hold by the caller */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800913static inline struct userfaultfd_wait_queue *find_userfault_in(
914 wait_queue_head_t *wqh)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700915{
Ingo Molnarac6424b2017-06-20 12:06:13 +0200916 wait_queue_entry_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700917 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700918
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800919 VM_BUG_ON(!spin_is_locked(&wqh->lock));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700920
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700921 uwq = NULL;
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800922 if (!waitqueue_active(wqh))
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700923 goto out;
924 /* walk in reverse to provide FIFO behavior to read userfaults */
Ingo Molnar2055da92017-06-20 12:06:46 +0200925 wq = list_last_entry(&wqh->head, typeof(*wq), entry);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700926 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
927out:
928 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700929}
930
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800931static inline struct userfaultfd_wait_queue *find_userfault(
932 struct userfaultfd_ctx *ctx)
933{
934 return find_userfault_in(&ctx->fault_pending_wqh);
935}
936
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800937static inline struct userfaultfd_wait_queue *find_userfault_evt(
938 struct userfaultfd_ctx *ctx)
939{
940 return find_userfault_in(&ctx->event_wqh);
941}
942
Al Viro076ccb72017-07-03 01:02:18 -0400943static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700944{
945 struct userfaultfd_ctx *ctx = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400946 __poll_t ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700947
948 poll_wait(file, &ctx->fd_wqh, wait);
949
950 switch (ctx->state) {
951 case UFFD_STATE_WAIT_API:
952 return POLLERR;
953 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700954 /*
955 * poll() never guarantees that read won't block.
956 * userfaults can be waken before they're read().
957 */
958 if (unlikely(!(file->f_flags & O_NONBLOCK)))
959 return POLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700960 /*
961 * lockless access to see if there are pending faults
962 * __pollwait last action is the add_wait_queue but
963 * the spin_unlock would allow the waitqueue_active to
964 * pass above the actual list_add inside
965 * add_wait_queue critical section. So use a full
966 * memory barrier to serialize the list_add write of
967 * add_wait_queue() with the waitqueue_active read
968 * below.
969 */
970 ret = 0;
971 smp_mb();
972 if (waitqueue_active(&ctx->fault_pending_wqh))
973 ret = POLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800974 else if (waitqueue_active(&ctx->event_wqh))
975 ret = POLLIN;
976
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700977 return ret;
978 default:
Andrea Arcangeli84749012017-02-22 15:42:12 -0800979 WARN_ON_ONCE(1);
980 return POLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700981 }
982}
983
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800984static const struct file_operations userfaultfd_fops;
985
986static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
987 struct userfaultfd_ctx *new,
988 struct uffd_msg *msg)
989{
990 int fd;
991 struct file *file;
992 unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
993
994 fd = get_unused_fd_flags(flags);
995 if (fd < 0)
996 return fd;
997
998 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
999 O_RDWR | flags);
1000 if (IS_ERR(file)) {
1001 put_unused_fd(fd);
1002 return PTR_ERR(file);
1003 }
1004
1005 fd_install(fd, file);
1006 msg->arg.reserved.reserved1 = 0;
1007 msg->arg.fork.ufd = fd;
1008
1009 return 0;
1010}
1011
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001012static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001013 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001014{
1015 ssize_t ret;
1016 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001017 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001018 /*
1019 * Handling fork event requires sleeping operations, so
1020 * we drop the event_wqh lock, then do these ops, then
1021 * lock it back and wake up the waiter. While the lock is
1022 * dropped the ewq may go away so we keep track of it
1023 * carefully.
1024 */
1025 LIST_HEAD(fork_event);
1026 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001027
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001028 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001029 spin_lock(&ctx->fd_wqh.lock);
1030 __add_wait_queue(&ctx->fd_wqh, &wait);
1031 for (;;) {
1032 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001033 spin_lock(&ctx->fault_pending_wqh.lock);
1034 uwq = find_userfault(ctx);
1035 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001036 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001037 * Use a seqcount to repeat the lockless check
1038 * in wake_userfault() to avoid missing
1039 * wakeups because during the refile both
1040 * waitqueue could become empty if this is the
1041 * only userfault.
1042 */
1043 write_seqcount_begin(&ctx->refile_seq);
1044
1045 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001046 * The fault_pending_wqh.lock prevents the uwq
1047 * to disappear from under us.
1048 *
1049 * Refile this userfault from
1050 * fault_pending_wqh to fault_wqh, it's not
1051 * pending anymore after we read it.
1052 *
1053 * Use list_del() by hand (as
1054 * userfaultfd_wake_function also uses
1055 * list_del_init() by hand) to be sure nobody
1056 * changes __remove_wait_queue() to use
1057 * list_del_init() in turn breaking the
1058 * !list_empty_careful() check in
Ingo Molnar2055da92017-06-20 12:06:46 +02001059 * handle_userfault(). The uwq->wq.head list
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001060 * must never be empty at any time during the
1061 * refile, or the waitqueue could disappear
1062 * from under us. The "wait_queue_head_t"
1063 * parameter of __remove_wait_queue() is unused
1064 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001065 */
Ingo Molnar2055da92017-06-20 12:06:46 +02001066 list_del(&uwq->wq.entry);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001067 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
1068
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001069 write_seqcount_end(&ctx->refile_seq);
1070
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001071 /* careful to always initialize msg if ret == 0 */
1072 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001073 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001074 ret = 0;
1075 break;
1076 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001077 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001078
1079 spin_lock(&ctx->event_wqh.lock);
1080 uwq = find_userfault_evt(ctx);
1081 if (uwq) {
1082 *msg = uwq->msg;
1083
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001084 if (uwq->msg.event == UFFD_EVENT_FORK) {
1085 fork_nctx = (struct userfaultfd_ctx *)
1086 (unsigned long)
1087 uwq->msg.arg.reserved.reserved1;
Ingo Molnar2055da92017-06-20 12:06:46 +02001088 list_move(&uwq->wq.entry, &fork_event);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001089 /*
1090 * fork_nctx can be freed as soon as
1091 * we drop the lock, unless we take a
1092 * reference on it.
1093 */
1094 userfaultfd_ctx_get(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001095 spin_unlock(&ctx->event_wqh.lock);
1096 ret = 0;
1097 break;
1098 }
1099
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001100 userfaultfd_event_complete(ctx, uwq);
1101 spin_unlock(&ctx->event_wqh.lock);
1102 ret = 0;
1103 break;
1104 }
1105 spin_unlock(&ctx->event_wqh.lock);
1106
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001107 if (signal_pending(current)) {
1108 ret = -ERESTARTSYS;
1109 break;
1110 }
1111 if (no_wait) {
1112 ret = -EAGAIN;
1113 break;
1114 }
1115 spin_unlock(&ctx->fd_wqh.lock);
1116 schedule();
1117 spin_lock(&ctx->fd_wqh.lock);
1118 }
1119 __remove_wait_queue(&ctx->fd_wqh, &wait);
1120 __set_current_state(TASK_RUNNING);
1121 spin_unlock(&ctx->fd_wqh.lock);
1122
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001123 if (!ret && msg->event == UFFD_EVENT_FORK) {
1124 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001125 spin_lock(&ctx->event_wqh.lock);
1126 if (!list_empty(&fork_event)) {
1127 /*
1128 * The fork thread didn't abort, so we can
1129 * drop the temporary refcount.
1130 */
1131 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001132
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001133 uwq = list_first_entry(&fork_event,
1134 typeof(*uwq),
1135 wq.entry);
1136 /*
1137 * If fork_event list wasn't empty and in turn
1138 * the event wasn't already released by fork
1139 * (the event is allocated on fork kernel
1140 * stack), put the event back to its place in
1141 * the event_wq. fork_event head will be freed
1142 * as soon as we return so the event cannot
1143 * stay queued there no matter the current
1144 * "ret" value.
1145 */
1146 list_del(&uwq->wq.entry);
1147 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1148
1149 /*
1150 * Leave the event in the waitqueue and report
1151 * error to userland if we failed to resolve
1152 * the userfault fork.
1153 */
1154 if (likely(!ret))
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001155 userfaultfd_event_complete(ctx, uwq);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001156 } else {
1157 /*
1158 * Here the fork thread aborted and the
1159 * refcount from the fork thread on fork_nctx
1160 * has already been released. We still hold
1161 * the reference we took before releasing the
1162 * lock above. If resolve_userfault_fork
1163 * failed we've to drop it because the
1164 * fork_nctx has to be freed in such case. If
1165 * it succeeded we'll hold it because the new
1166 * uffd references it.
1167 */
1168 if (ret)
1169 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001170 }
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001171 spin_unlock(&ctx->event_wqh.lock);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001172 }
1173
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001174 return ret;
1175}
1176
1177static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1178 size_t count, loff_t *ppos)
1179{
1180 struct userfaultfd_ctx *ctx = file->private_data;
1181 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001182 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001183 int no_wait = file->f_flags & O_NONBLOCK;
1184
1185 if (ctx->state == UFFD_STATE_WAIT_API)
1186 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001187
1188 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001189 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001190 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001191 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001192 if (_ret < 0)
1193 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001194 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001195 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001196 ret += sizeof(msg);
1197 buf += sizeof(msg);
1198 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001199 /*
1200 * Allow to read more than one fault at time but only
1201 * block if waiting for the very first one.
1202 */
1203 no_wait = O_NONBLOCK;
1204 }
1205}
1206
1207static void __wake_userfault(struct userfaultfd_ctx *ctx,
1208 struct userfaultfd_wake_range *range)
1209{
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001210 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001211 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001212 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001213 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001214 range);
1215 if (waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001216 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001217 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001218}
1219
1220static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1221 struct userfaultfd_wake_range *range)
1222{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001223 unsigned seq;
1224 bool need_wakeup;
1225
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001226 /*
1227 * To be sure waitqueue_active() is not reordered by the CPU
1228 * before the pagetable update, use an explicit SMP memory
1229 * barrier here. PT lock release or up_read(mmap_sem) still
1230 * have release semantics that can allow the
1231 * waitqueue_active() to be reordered before the pte update.
1232 */
1233 smp_mb();
1234
1235 /*
1236 * Use waitqueue_active because it's very frequent to
1237 * change the address space atomically even if there are no
1238 * userfaults yet. So we take the spinlock only when we're
1239 * sure we've userfaults to wake.
1240 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001241 do {
1242 seq = read_seqcount_begin(&ctx->refile_seq);
1243 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1244 waitqueue_active(&ctx->fault_wqh);
1245 cond_resched();
1246 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1247 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001248 __wake_userfault(ctx, range);
1249}
1250
1251static __always_inline int validate_range(struct mm_struct *mm,
1252 __u64 start, __u64 len)
1253{
1254 __u64 task_size = mm->task_size;
1255
1256 if (start & ~PAGE_MASK)
1257 return -EINVAL;
1258 if (len & ~PAGE_MASK)
1259 return -EINVAL;
1260 if (!len)
1261 return -EINVAL;
1262 if (start < mmap_min_addr)
1263 return -EINVAL;
1264 if (start >= task_size)
1265 return -EINVAL;
1266 if (len > task_size - start)
1267 return -EINVAL;
1268 return 0;
1269}
1270
Mike Rapoportba6907d2017-02-22 15:43:22 -08001271static inline bool vma_can_userfault(struct vm_area_struct *vma)
1272{
Mike Rapoportcac67322017-02-22 15:43:40 -08001273 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1274 vma_is_shmem(vma);
Mike Rapoportba6907d2017-02-22 15:43:22 -08001275}
1276
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001277static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1278 unsigned long arg)
1279{
1280 struct mm_struct *mm = ctx->mm;
1281 struct vm_area_struct *vma, *prev, *cur;
1282 int ret;
1283 struct uffdio_register uffdio_register;
1284 struct uffdio_register __user *user_uffdio_register;
1285 unsigned long vm_flags, new_flags;
1286 bool found;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001287 bool basic_ioctls;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001288 unsigned long start, end, vma_end;
1289
1290 user_uffdio_register = (struct uffdio_register __user *) arg;
1291
1292 ret = -EFAULT;
1293 if (copy_from_user(&uffdio_register, user_uffdio_register,
1294 sizeof(uffdio_register)-sizeof(__u64)))
1295 goto out;
1296
1297 ret = -EINVAL;
1298 if (!uffdio_register.mode)
1299 goto out;
1300 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1301 UFFDIO_REGISTER_MODE_WP))
1302 goto out;
1303 vm_flags = 0;
1304 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1305 vm_flags |= VM_UFFD_MISSING;
1306 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1307 vm_flags |= VM_UFFD_WP;
1308 /*
1309 * FIXME: remove the below error constraint by
1310 * implementing the wprotect tracking mode.
1311 */
1312 ret = -EINVAL;
1313 goto out;
1314 }
1315
1316 ret = validate_range(mm, uffdio_register.range.start,
1317 uffdio_register.range.len);
1318 if (ret)
1319 goto out;
1320
1321 start = uffdio_register.range.start;
1322 end = start + uffdio_register.range.len;
1323
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001324 ret = -ENOMEM;
1325 if (!mmget_not_zero(mm))
1326 goto out;
1327
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001328 down_write(&mm->mmap_sem);
1329 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001330 if (!vma)
1331 goto out_unlock;
1332
1333 /* check that there's at least one vma in the range */
1334 ret = -EINVAL;
1335 if (vma->vm_start >= end)
1336 goto out_unlock;
1337
1338 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001339 * If the first vma contains huge pages, make sure start address
1340 * is aligned to huge page size.
1341 */
1342 if (is_vm_hugetlb_page(vma)) {
1343 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1344
1345 if (start & (vma_hpagesize - 1))
1346 goto out_unlock;
1347 }
1348
1349 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001350 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001351 */
1352 found = false;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001353 basic_ioctls = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001354 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1355 cond_resched();
1356
1357 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1358 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1359
1360 /* check not compatible vmas */
1361 ret = -EINVAL;
Mike Rapoportba6907d2017-02-22 15:43:22 -08001362 if (!vma_can_userfault(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001363 goto out_unlock;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001364 /*
1365 * If this vma contains ending address, and huge pages
1366 * check alignment.
1367 */
1368 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1369 end > cur->vm_start) {
1370 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1371
1372 ret = -EINVAL;
1373
1374 if (end & (vma_hpagesize - 1))
1375 goto out_unlock;
1376 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001377
1378 /*
1379 * Check that this vma isn't already owned by a
1380 * different userfaultfd. We can't allow more than one
1381 * userfaultfd to own a single vma simultaneously or we
1382 * wouldn't know which one to deliver the userfaults to.
1383 */
1384 ret = -EBUSY;
1385 if (cur->vm_userfaultfd_ctx.ctx &&
1386 cur->vm_userfaultfd_ctx.ctx != ctx)
1387 goto out_unlock;
1388
Mike Kravetzcab350a2017-02-22 15:43:04 -08001389 /*
1390 * Note vmas containing huge pages
1391 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001392 if (is_vm_hugetlb_page(cur))
1393 basic_ioctls = true;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001394
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001395 found = true;
1396 }
1397 BUG_ON(!found);
1398
1399 if (vma->vm_start < start)
1400 prev = vma;
1401
1402 ret = 0;
1403 do {
1404 cond_resched();
1405
Mike Rapoportba6907d2017-02-22 15:43:22 -08001406 BUG_ON(!vma_can_userfault(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001407 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1408 vma->vm_userfaultfd_ctx.ctx != ctx);
1409
1410 /*
1411 * Nothing to do: this vma is already registered into this
1412 * userfaultfd and with the right tracking mode too.
1413 */
1414 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1415 (vma->vm_flags & vm_flags) == vm_flags)
1416 goto skip;
1417
1418 if (vma->vm_start > start)
1419 start = vma->vm_start;
1420 vma_end = min(end, vma->vm_end);
1421
1422 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
1423 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1424 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1425 vma_policy(vma),
1426 ((struct vm_userfaultfd_ctx){ ctx }));
1427 if (prev) {
1428 vma = prev;
1429 goto next;
1430 }
1431 if (vma->vm_start < start) {
1432 ret = split_vma(mm, vma, start, 1);
1433 if (ret)
1434 break;
1435 }
1436 if (vma->vm_end > end) {
1437 ret = split_vma(mm, vma, end, 0);
1438 if (ret)
1439 break;
1440 }
1441 next:
1442 /*
1443 * In the vma_merge() successful mprotect-like case 8:
1444 * the next vma was merged into the current one and
1445 * the current one has not been updated yet.
1446 */
1447 vma->vm_flags = new_flags;
1448 vma->vm_userfaultfd_ctx.ctx = ctx;
1449
1450 skip:
1451 prev = vma;
1452 start = vma->vm_end;
1453 vma = vma->vm_next;
1454 } while (vma && vma->vm_start < end);
1455out_unlock:
1456 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001457 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001458 if (!ret) {
1459 /*
1460 * Now that we scanned all vmas we can already tell
1461 * userland which ioctls methods are guaranteed to
1462 * succeed on this range.
1463 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001464 if (put_user(basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
Mike Kravetzcab350a2017-02-22 15:43:04 -08001465 UFFD_API_RANGE_IOCTLS,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001466 &user_uffdio_register->ioctls))
1467 ret = -EFAULT;
1468 }
1469out:
1470 return ret;
1471}
1472
1473static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1474 unsigned long arg)
1475{
1476 struct mm_struct *mm = ctx->mm;
1477 struct vm_area_struct *vma, *prev, *cur;
1478 int ret;
1479 struct uffdio_range uffdio_unregister;
1480 unsigned long new_flags;
1481 bool found;
1482 unsigned long start, end, vma_end;
1483 const void __user *buf = (void __user *)arg;
1484
1485 ret = -EFAULT;
1486 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1487 goto out;
1488
1489 ret = validate_range(mm, uffdio_unregister.start,
1490 uffdio_unregister.len);
1491 if (ret)
1492 goto out;
1493
1494 start = uffdio_unregister.start;
1495 end = start + uffdio_unregister.len;
1496
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001497 ret = -ENOMEM;
1498 if (!mmget_not_zero(mm))
1499 goto out;
1500
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001501 down_write(&mm->mmap_sem);
1502 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001503 if (!vma)
1504 goto out_unlock;
1505
1506 /* check that there's at least one vma in the range */
1507 ret = -EINVAL;
1508 if (vma->vm_start >= end)
1509 goto out_unlock;
1510
1511 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001512 * If the first vma contains huge pages, make sure start address
1513 * is aligned to huge page size.
1514 */
1515 if (is_vm_hugetlb_page(vma)) {
1516 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1517
1518 if (start & (vma_hpagesize - 1))
1519 goto out_unlock;
1520 }
1521
1522 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001523 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001524 */
1525 found = false;
1526 ret = -EINVAL;
1527 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1528 cond_resched();
1529
1530 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1531 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1532
1533 /*
1534 * Check not compatible vmas, not strictly required
1535 * here as not compatible vmas cannot have an
1536 * userfaultfd_ctx registered on them, but this
1537 * provides for more strict behavior to notice
1538 * unregistration errors.
1539 */
Mike Rapoportba6907d2017-02-22 15:43:22 -08001540 if (!vma_can_userfault(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001541 goto out_unlock;
1542
1543 found = true;
1544 }
1545 BUG_ON(!found);
1546
1547 if (vma->vm_start < start)
1548 prev = vma;
1549
1550 ret = 0;
1551 do {
1552 cond_resched();
1553
Mike Rapoportba6907d2017-02-22 15:43:22 -08001554 BUG_ON(!vma_can_userfault(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001555
1556 /*
1557 * Nothing to do: this vma is already registered into this
1558 * userfaultfd and with the right tracking mode too.
1559 */
1560 if (!vma->vm_userfaultfd_ctx.ctx)
1561 goto skip;
1562
1563 if (vma->vm_start > start)
1564 start = vma->vm_start;
1565 vma_end = min(end, vma->vm_end);
1566
Andrea Arcangeli09fa5292017-02-22 15:42:46 -08001567 if (userfaultfd_missing(vma)) {
1568 /*
1569 * Wake any concurrent pending userfault while
1570 * we unregister, so they will not hang
1571 * permanently and it avoids userland to call
1572 * UFFDIO_WAKE explicitly.
1573 */
1574 struct userfaultfd_wake_range range;
1575 range.start = start;
1576 range.len = vma_end - start;
1577 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1578 }
1579
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001580 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1581 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1582 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1583 vma_policy(vma),
1584 NULL_VM_UFFD_CTX);
1585 if (prev) {
1586 vma = prev;
1587 goto next;
1588 }
1589 if (vma->vm_start < start) {
1590 ret = split_vma(mm, vma, start, 1);
1591 if (ret)
1592 break;
1593 }
1594 if (vma->vm_end > end) {
1595 ret = split_vma(mm, vma, end, 0);
1596 if (ret)
1597 break;
1598 }
1599 next:
1600 /*
1601 * In the vma_merge() successful mprotect-like case 8:
1602 * the next vma was merged into the current one and
1603 * the current one has not been updated yet.
1604 */
1605 vma->vm_flags = new_flags;
1606 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1607
1608 skip:
1609 prev = vma;
1610 start = vma->vm_end;
1611 vma = vma->vm_next;
1612 } while (vma && vma->vm_start < end);
1613out_unlock:
1614 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001615 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001616out:
1617 return ret;
1618}
1619
1620/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001621 * userfaultfd_wake may be used in combination with the
1622 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001623 */
1624static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1625 unsigned long arg)
1626{
1627 int ret;
1628 struct uffdio_range uffdio_wake;
1629 struct userfaultfd_wake_range range;
1630 const void __user *buf = (void __user *)arg;
1631
1632 ret = -EFAULT;
1633 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1634 goto out;
1635
1636 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1637 if (ret)
1638 goto out;
1639
1640 range.start = uffdio_wake.start;
1641 range.len = uffdio_wake.len;
1642
1643 /*
1644 * len == 0 means wake all and we don't want to wake all here,
1645 * so check it again to be sure.
1646 */
1647 VM_BUG_ON(!range.len);
1648
1649 wake_userfault(ctx, &range);
1650 ret = 0;
1651
1652out:
1653 return ret;
1654}
1655
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001656static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1657 unsigned long arg)
1658{
1659 __s64 ret;
1660 struct uffdio_copy uffdio_copy;
1661 struct uffdio_copy __user *user_uffdio_copy;
1662 struct userfaultfd_wake_range range;
1663
1664 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1665
1666 ret = -EFAULT;
1667 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1668 /* don't copy "copy" last field */
1669 sizeof(uffdio_copy)-sizeof(__s64)))
1670 goto out;
1671
1672 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1673 if (ret)
1674 goto out;
1675 /*
1676 * double check for wraparound just in case. copy_from_user()
1677 * will later check uffdio_copy.src + uffdio_copy.len to fit
1678 * in the userland range.
1679 */
1680 ret = -EINVAL;
1681 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1682 goto out;
1683 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1684 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001685 if (mmget_not_zero(ctx->mm)) {
1686 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1687 uffdio_copy.len);
1688 mmput(ctx->mm);
Mike Rapoport96333182017-02-24 14:58:31 -08001689 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001690 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001691 }
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001692 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1693 return -EFAULT;
1694 if (ret < 0)
1695 goto out;
1696 BUG_ON(!ret);
1697 /* len == 0 would wake all */
1698 range.len = ret;
1699 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1700 range.start = uffdio_copy.dst;
1701 wake_userfault(ctx, &range);
1702 }
1703 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1704out:
1705 return ret;
1706}
1707
1708static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1709 unsigned long arg)
1710{
1711 __s64 ret;
1712 struct uffdio_zeropage uffdio_zeropage;
1713 struct uffdio_zeropage __user *user_uffdio_zeropage;
1714 struct userfaultfd_wake_range range;
1715
1716 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1717
1718 ret = -EFAULT;
1719 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1720 /* don't copy "zeropage" last field */
1721 sizeof(uffdio_zeropage)-sizeof(__s64)))
1722 goto out;
1723
1724 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1725 uffdio_zeropage.range.len);
1726 if (ret)
1727 goto out;
1728 ret = -EINVAL;
1729 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1730 goto out;
1731
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001732 if (mmget_not_zero(ctx->mm)) {
1733 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1734 uffdio_zeropage.range.len);
1735 mmput(ctx->mm);
Mike Rapoport9d95aa42017-08-02 13:32:15 -07001736 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001737 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001738 }
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001739 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1740 return -EFAULT;
1741 if (ret < 0)
1742 goto out;
1743 /* len == 0 would wake all */
1744 BUG_ON(!ret);
1745 range.len = ret;
1746 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1747 range.start = uffdio_zeropage.range.start;
1748 wake_userfault(ctx, &range);
1749 }
1750 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1751out:
1752 return ret;
1753}
1754
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001755static inline unsigned int uffd_ctx_features(__u64 user_features)
1756{
1757 /*
1758 * For the current set of features the bits just coincide
1759 */
1760 return (unsigned int)user_features;
1761}
1762
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001763/*
1764 * userland asks for a certain API version and we return which bits
1765 * and ioctl commands are implemented in this kernel for such API
1766 * version or -EINVAL if unknown.
1767 */
1768static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1769 unsigned long arg)
1770{
1771 struct uffdio_api uffdio_api;
1772 void __user *buf = (void __user *)arg;
1773 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001774 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001775
1776 ret = -EINVAL;
1777 if (ctx->state != UFFD_STATE_WAIT_API)
1778 goto out;
1779 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001780 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001781 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001782 features = uffdio_api.features;
1783 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001784 memset(&uffdio_api, 0, sizeof(uffdio_api));
1785 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1786 goto out;
1787 ret = -EINVAL;
1788 goto out;
1789 }
Andrea Arcangeli65603142017-02-22 15:42:24 -08001790 /* report all available features and ioctls to userland */
1791 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001792 uffdio_api.ioctls = UFFD_API_IOCTLS;
1793 ret = -EFAULT;
1794 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1795 goto out;
1796 ctx->state = UFFD_STATE_RUNNING;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001797 /* only enable the requested features for this uffd context */
1798 ctx->features = uffd_ctx_features(features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001799 ret = 0;
1800out:
1801 return ret;
1802}
1803
1804static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1805 unsigned long arg)
1806{
1807 int ret = -EINVAL;
1808 struct userfaultfd_ctx *ctx = file->private_data;
1809
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001810 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1811 return -EINVAL;
1812
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001813 switch(cmd) {
1814 case UFFDIO_API:
1815 ret = userfaultfd_api(ctx, arg);
1816 break;
1817 case UFFDIO_REGISTER:
1818 ret = userfaultfd_register(ctx, arg);
1819 break;
1820 case UFFDIO_UNREGISTER:
1821 ret = userfaultfd_unregister(ctx, arg);
1822 break;
1823 case UFFDIO_WAKE:
1824 ret = userfaultfd_wake(ctx, arg);
1825 break;
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001826 case UFFDIO_COPY:
1827 ret = userfaultfd_copy(ctx, arg);
1828 break;
1829 case UFFDIO_ZEROPAGE:
1830 ret = userfaultfd_zeropage(ctx, arg);
1831 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001832 }
1833 return ret;
1834}
1835
1836#ifdef CONFIG_PROC_FS
1837static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1838{
1839 struct userfaultfd_ctx *ctx = f->private_data;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001840 wait_queue_entry_t *wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001841 struct userfaultfd_wait_queue *uwq;
1842 unsigned long pending = 0, total = 0;
1843
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001844 spin_lock(&ctx->fault_pending_wqh.lock);
Ingo Molnar2055da92017-06-20 12:06:46 +02001845 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001846 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001847 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001848 total++;
1849 }
Ingo Molnar2055da92017-06-20 12:06:46 +02001850 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001851 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1852 total++;
1853 }
1854 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001855
1856 /*
1857 * If more protocols will be added, there will be all shown
1858 * separated by a space. Like this:
1859 * protocols: aa:... bb:...
1860 */
1861 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Mike Rapoport045098e2017-04-07 16:04:42 -07001862 pending, total, UFFD_API, ctx->features,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001863 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1864}
1865#endif
1866
1867static const struct file_operations userfaultfd_fops = {
1868#ifdef CONFIG_PROC_FS
1869 .show_fdinfo = userfaultfd_show_fdinfo,
1870#endif
1871 .release = userfaultfd_release,
1872 .poll = userfaultfd_poll,
1873 .read = userfaultfd_read,
1874 .unlocked_ioctl = userfaultfd_ioctl,
1875 .compat_ioctl = userfaultfd_ioctl,
1876 .llseek = noop_llseek,
1877};
1878
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001879static void init_once_userfaultfd_ctx(void *mem)
1880{
1881 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1882
1883 init_waitqueue_head(&ctx->fault_pending_wqh);
1884 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001885 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001886 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001887 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001888}
1889
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001890/**
Masahiro Yamada9332ef92017-02-27 14:28:47 -08001891 * userfaultfd_file_create - Creates a userfaultfd file pointer.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001892 * @flags: Flags for the userfaultfd file.
1893 *
Masahiro Yamada9332ef92017-02-27 14:28:47 -08001894 * This function creates a userfaultfd file pointer, w/out installing
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001895 * it into the fd table. This is useful when the userfaultfd file is
1896 * used during the initialization of data structures that require
1897 * extra setup after the userfaultfd creation. So the userfaultfd
1898 * creation is split into the file pointer creation phase, and the
1899 * file descriptor installation phase. In this way races with
1900 * userspace closing the newly installed file descriptor can be
Masahiro Yamada9332ef92017-02-27 14:28:47 -08001901 * avoided. Returns a userfaultfd file pointer, or a proper error
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001902 * pointer.
1903 */
1904static struct file *userfaultfd_file_create(int flags)
1905{
1906 struct file *file;
1907 struct userfaultfd_ctx *ctx;
1908
1909 BUG_ON(!current->mm);
1910
1911 /* Check the UFFD_* constants for consistency. */
1912 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1913 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1914
1915 file = ERR_PTR(-EINVAL);
1916 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1917 goto out;
1918
1919 file = ERR_PTR(-ENOMEM);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001920 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001921 if (!ctx)
1922 goto out;
1923
1924 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001925 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001926 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001927 ctx->state = UFFD_STATE_WAIT_API;
1928 ctx->released = false;
1929 ctx->mm = current->mm;
1930 /* prevent the mm struct to be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001931 mmgrab(ctx->mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001932
1933 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1934 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
Eric Biggersc03e9462015-09-17 16:01:54 -07001935 if (IS_ERR(file)) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001936 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001937 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001938 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001939out:
1940 return file;
1941}
1942
1943SYSCALL_DEFINE1(userfaultfd, int, flags)
1944{
1945 int fd, error;
1946 struct file *file;
1947
1948 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1949 if (error < 0)
1950 return error;
1951 fd = error;
1952
1953 file = userfaultfd_file_create(flags);
1954 if (IS_ERR(file)) {
1955 error = PTR_ERR(file);
1956 goto err_put_unused_fd;
1957 }
1958 fd_install(fd, file);
1959
1960 return fd;
1961
1962err_put_unused_fd:
1963 put_unused_fd(fd);
1964
1965 return error;
1966}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001967
1968static int __init userfaultfd_init(void)
1969{
1970 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1971 sizeof(struct userfaultfd_ctx),
1972 0,
1973 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1974 init_once_userfaultfd_ctx);
1975 return 0;
1976}
1977__initcall(userfaultfd_init);