blob: cec550c8468f484a3f14d6a6b3e5dcc2a09ea70f [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 Kravetz369cd212017-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 Kravetz369cd212017-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 Kravetz369cd212017-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 Kravetz369cd212017-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 Kravetz369cd212017-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 Kravetz369cd212017-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)))) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800486 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700487 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
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800617 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800618 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
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800907 wake_up_poll(&ctx->fd_wqh, EPOLLHUP);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700908 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:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800952 return EPOLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700953 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)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800959 return EPOLLERR;
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))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800973 ret = EPOLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800974 else if (waitqueue_active(&ctx->event_wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800975 ret = EPOLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800976
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700977 return ret;
978 default:
Andrea Arcangeli84749012017-02-22 15:42:12 -0800979 WARN_ON_ONCE(1);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800980 return EPOLLERR;
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;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800991
Eric Biggers284cd242018-01-31 16:19:48 -0800992 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new,
993 O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS));
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800994 if (fd < 0)
995 return fd;
996
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800997 msg->arg.reserved.reserved1 = 0;
998 msg->arg.fork.ufd = fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800999 return 0;
1000}
1001
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001002static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001003 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001004{
1005 ssize_t ret;
1006 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001007 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001008 /*
1009 * Handling fork event requires sleeping operations, so
1010 * we drop the event_wqh lock, then do these ops, then
1011 * lock it back and wake up the waiter. While the lock is
1012 * dropped the ewq may go away so we keep track of it
1013 * carefully.
1014 */
1015 LIST_HEAD(fork_event);
1016 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001017
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001018 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001019 spin_lock(&ctx->fd_wqh.lock);
1020 __add_wait_queue(&ctx->fd_wqh, &wait);
1021 for (;;) {
1022 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001023 spin_lock(&ctx->fault_pending_wqh.lock);
1024 uwq = find_userfault(ctx);
1025 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001026 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001027 * Use a seqcount to repeat the lockless check
1028 * in wake_userfault() to avoid missing
1029 * wakeups because during the refile both
1030 * waitqueue could become empty if this is the
1031 * only userfault.
1032 */
1033 write_seqcount_begin(&ctx->refile_seq);
1034
1035 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001036 * The fault_pending_wqh.lock prevents the uwq
1037 * to disappear from under us.
1038 *
1039 * Refile this userfault from
1040 * fault_pending_wqh to fault_wqh, it's not
1041 * pending anymore after we read it.
1042 *
1043 * Use list_del() by hand (as
1044 * userfaultfd_wake_function also uses
1045 * list_del_init() by hand) to be sure nobody
1046 * changes __remove_wait_queue() to use
1047 * list_del_init() in turn breaking the
1048 * !list_empty_careful() check in
Ingo Molnar2055da92017-06-20 12:06:46 +02001049 * handle_userfault(). The uwq->wq.head list
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001050 * must never be empty at any time during the
1051 * refile, or the waitqueue could disappear
1052 * from under us. The "wait_queue_head_t"
1053 * parameter of __remove_wait_queue() is unused
1054 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001055 */
Ingo Molnar2055da92017-06-20 12:06:46 +02001056 list_del(&uwq->wq.entry);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001057 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
1058
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001059 write_seqcount_end(&ctx->refile_seq);
1060
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001061 /* careful to always initialize msg if ret == 0 */
1062 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001063 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001064 ret = 0;
1065 break;
1066 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001067 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001068
1069 spin_lock(&ctx->event_wqh.lock);
1070 uwq = find_userfault_evt(ctx);
1071 if (uwq) {
1072 *msg = uwq->msg;
1073
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001074 if (uwq->msg.event == UFFD_EVENT_FORK) {
1075 fork_nctx = (struct userfaultfd_ctx *)
1076 (unsigned long)
1077 uwq->msg.arg.reserved.reserved1;
Ingo Molnar2055da92017-06-20 12:06:46 +02001078 list_move(&uwq->wq.entry, &fork_event);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001079 /*
1080 * fork_nctx can be freed as soon as
1081 * we drop the lock, unless we take a
1082 * reference on it.
1083 */
1084 userfaultfd_ctx_get(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001085 spin_unlock(&ctx->event_wqh.lock);
1086 ret = 0;
1087 break;
1088 }
1089
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001090 userfaultfd_event_complete(ctx, uwq);
1091 spin_unlock(&ctx->event_wqh.lock);
1092 ret = 0;
1093 break;
1094 }
1095 spin_unlock(&ctx->event_wqh.lock);
1096
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001097 if (signal_pending(current)) {
1098 ret = -ERESTARTSYS;
1099 break;
1100 }
1101 if (no_wait) {
1102 ret = -EAGAIN;
1103 break;
1104 }
1105 spin_unlock(&ctx->fd_wqh.lock);
1106 schedule();
1107 spin_lock(&ctx->fd_wqh.lock);
1108 }
1109 __remove_wait_queue(&ctx->fd_wqh, &wait);
1110 __set_current_state(TASK_RUNNING);
1111 spin_unlock(&ctx->fd_wqh.lock);
1112
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001113 if (!ret && msg->event == UFFD_EVENT_FORK) {
1114 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001115 spin_lock(&ctx->event_wqh.lock);
1116 if (!list_empty(&fork_event)) {
1117 /*
1118 * The fork thread didn't abort, so we can
1119 * drop the temporary refcount.
1120 */
1121 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001122
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001123 uwq = list_first_entry(&fork_event,
1124 typeof(*uwq),
1125 wq.entry);
1126 /*
1127 * If fork_event list wasn't empty and in turn
1128 * the event wasn't already released by fork
1129 * (the event is allocated on fork kernel
1130 * stack), put the event back to its place in
1131 * the event_wq. fork_event head will be freed
1132 * as soon as we return so the event cannot
1133 * stay queued there no matter the current
1134 * "ret" value.
1135 */
1136 list_del(&uwq->wq.entry);
1137 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1138
1139 /*
1140 * Leave the event in the waitqueue and report
1141 * error to userland if we failed to resolve
1142 * the userfault fork.
1143 */
1144 if (likely(!ret))
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001145 userfaultfd_event_complete(ctx, uwq);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001146 } else {
1147 /*
1148 * Here the fork thread aborted and the
1149 * refcount from the fork thread on fork_nctx
1150 * has already been released. We still hold
1151 * the reference we took before releasing the
1152 * lock above. If resolve_userfault_fork
1153 * failed we've to drop it because the
1154 * fork_nctx has to be freed in such case. If
1155 * it succeeded we'll hold it because the new
1156 * uffd references it.
1157 */
1158 if (ret)
1159 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001160 }
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001161 spin_unlock(&ctx->event_wqh.lock);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001162 }
1163
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001164 return ret;
1165}
1166
1167static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1168 size_t count, loff_t *ppos)
1169{
1170 struct userfaultfd_ctx *ctx = file->private_data;
1171 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001172 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001173 int no_wait = file->f_flags & O_NONBLOCK;
1174
1175 if (ctx->state == UFFD_STATE_WAIT_API)
1176 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001177
1178 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001179 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001180 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001181 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001182 if (_ret < 0)
1183 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001184 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001185 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001186 ret += sizeof(msg);
1187 buf += sizeof(msg);
1188 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001189 /*
1190 * Allow to read more than one fault at time but only
1191 * block if waiting for the very first one.
1192 */
1193 no_wait = O_NONBLOCK;
1194 }
1195}
1196
1197static void __wake_userfault(struct userfaultfd_ctx *ctx,
1198 struct userfaultfd_wake_range *range)
1199{
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001200 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001201 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001202 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001203 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001204 range);
1205 if (waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001206 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001207 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001208}
1209
1210static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1211 struct userfaultfd_wake_range *range)
1212{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001213 unsigned seq;
1214 bool need_wakeup;
1215
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001216 /*
1217 * To be sure waitqueue_active() is not reordered by the CPU
1218 * before the pagetable update, use an explicit SMP memory
1219 * barrier here. PT lock release or up_read(mmap_sem) still
1220 * have release semantics that can allow the
1221 * waitqueue_active() to be reordered before the pte update.
1222 */
1223 smp_mb();
1224
1225 /*
1226 * Use waitqueue_active because it's very frequent to
1227 * change the address space atomically even if there are no
1228 * userfaults yet. So we take the spinlock only when we're
1229 * sure we've userfaults to wake.
1230 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001231 do {
1232 seq = read_seqcount_begin(&ctx->refile_seq);
1233 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1234 waitqueue_active(&ctx->fault_wqh);
1235 cond_resched();
1236 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1237 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001238 __wake_userfault(ctx, range);
1239}
1240
1241static __always_inline int validate_range(struct mm_struct *mm,
1242 __u64 start, __u64 len)
1243{
1244 __u64 task_size = mm->task_size;
1245
1246 if (start & ~PAGE_MASK)
1247 return -EINVAL;
1248 if (len & ~PAGE_MASK)
1249 return -EINVAL;
1250 if (!len)
1251 return -EINVAL;
1252 if (start < mmap_min_addr)
1253 return -EINVAL;
1254 if (start >= task_size)
1255 return -EINVAL;
1256 if (len > task_size - start)
1257 return -EINVAL;
1258 return 0;
1259}
1260
Mike Rapoportba6907d2017-02-22 15:43:22 -08001261static inline bool vma_can_userfault(struct vm_area_struct *vma)
1262{
Mike Rapoportcac67322017-02-22 15:43:40 -08001263 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1264 vma_is_shmem(vma);
Mike Rapoportba6907d2017-02-22 15:43:22 -08001265}
1266
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001267static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1268 unsigned long arg)
1269{
1270 struct mm_struct *mm = ctx->mm;
1271 struct vm_area_struct *vma, *prev, *cur;
1272 int ret;
1273 struct uffdio_register uffdio_register;
1274 struct uffdio_register __user *user_uffdio_register;
1275 unsigned long vm_flags, new_flags;
1276 bool found;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001277 bool basic_ioctls;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001278 unsigned long start, end, vma_end;
1279
1280 user_uffdio_register = (struct uffdio_register __user *) arg;
1281
1282 ret = -EFAULT;
1283 if (copy_from_user(&uffdio_register, user_uffdio_register,
1284 sizeof(uffdio_register)-sizeof(__u64)))
1285 goto out;
1286
1287 ret = -EINVAL;
1288 if (!uffdio_register.mode)
1289 goto out;
1290 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1291 UFFDIO_REGISTER_MODE_WP))
1292 goto out;
1293 vm_flags = 0;
1294 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1295 vm_flags |= VM_UFFD_MISSING;
1296 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1297 vm_flags |= VM_UFFD_WP;
1298 /*
1299 * FIXME: remove the below error constraint by
1300 * implementing the wprotect tracking mode.
1301 */
1302 ret = -EINVAL;
1303 goto out;
1304 }
1305
1306 ret = validate_range(mm, uffdio_register.range.start,
1307 uffdio_register.range.len);
1308 if (ret)
1309 goto out;
1310
1311 start = uffdio_register.range.start;
1312 end = start + uffdio_register.range.len;
1313
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001314 ret = -ENOMEM;
1315 if (!mmget_not_zero(mm))
1316 goto out;
1317
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001318 down_write(&mm->mmap_sem);
1319 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001320 if (!vma)
1321 goto out_unlock;
1322
1323 /* check that there's at least one vma in the range */
1324 ret = -EINVAL;
1325 if (vma->vm_start >= end)
1326 goto out_unlock;
1327
1328 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001329 * If the first vma contains huge pages, make sure start address
1330 * is aligned to huge page size.
1331 */
1332 if (is_vm_hugetlb_page(vma)) {
1333 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1334
1335 if (start & (vma_hpagesize - 1))
1336 goto out_unlock;
1337 }
1338
1339 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001340 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001341 */
1342 found = false;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001343 basic_ioctls = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001344 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1345 cond_resched();
1346
1347 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1348 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1349
1350 /* check not compatible vmas */
1351 ret = -EINVAL;
Mike Rapoportba6907d2017-02-22 15:43:22 -08001352 if (!vma_can_userfault(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001353 goto out_unlock;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001354 /*
1355 * If this vma contains ending address, and huge pages
1356 * check alignment.
1357 */
1358 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1359 end > cur->vm_start) {
1360 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1361
1362 ret = -EINVAL;
1363
1364 if (end & (vma_hpagesize - 1))
1365 goto out_unlock;
1366 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001367
1368 /*
1369 * Check that this vma isn't already owned by a
1370 * different userfaultfd. We can't allow more than one
1371 * userfaultfd to own a single vma simultaneously or we
1372 * wouldn't know which one to deliver the userfaults to.
1373 */
1374 ret = -EBUSY;
1375 if (cur->vm_userfaultfd_ctx.ctx &&
1376 cur->vm_userfaultfd_ctx.ctx != ctx)
1377 goto out_unlock;
1378
Mike Kravetzcab350a2017-02-22 15:43:04 -08001379 /*
1380 * Note vmas containing huge pages
1381 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001382 if (is_vm_hugetlb_page(cur))
1383 basic_ioctls = true;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001384
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001385 found = true;
1386 }
1387 BUG_ON(!found);
1388
1389 if (vma->vm_start < start)
1390 prev = vma;
1391
1392 ret = 0;
1393 do {
1394 cond_resched();
1395
Mike Rapoportba6907d2017-02-22 15:43:22 -08001396 BUG_ON(!vma_can_userfault(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001397 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1398 vma->vm_userfaultfd_ctx.ctx != ctx);
1399
1400 /*
1401 * Nothing to do: this vma is already registered into this
1402 * userfaultfd and with the right tracking mode too.
1403 */
1404 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1405 (vma->vm_flags & vm_flags) == vm_flags)
1406 goto skip;
1407
1408 if (vma->vm_start > start)
1409 start = vma->vm_start;
1410 vma_end = min(end, vma->vm_end);
1411
1412 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
1413 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1414 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1415 vma_policy(vma),
1416 ((struct vm_userfaultfd_ctx){ ctx }));
1417 if (prev) {
1418 vma = prev;
1419 goto next;
1420 }
1421 if (vma->vm_start < start) {
1422 ret = split_vma(mm, vma, start, 1);
1423 if (ret)
1424 break;
1425 }
1426 if (vma->vm_end > end) {
1427 ret = split_vma(mm, vma, end, 0);
1428 if (ret)
1429 break;
1430 }
1431 next:
1432 /*
1433 * In the vma_merge() successful mprotect-like case 8:
1434 * the next vma was merged into the current one and
1435 * the current one has not been updated yet.
1436 */
1437 vma->vm_flags = new_flags;
1438 vma->vm_userfaultfd_ctx.ctx = ctx;
1439
1440 skip:
1441 prev = vma;
1442 start = vma->vm_end;
1443 vma = vma->vm_next;
1444 } while (vma && vma->vm_start < end);
1445out_unlock:
1446 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001447 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001448 if (!ret) {
1449 /*
1450 * Now that we scanned all vmas we can already tell
1451 * userland which ioctls methods are guaranteed to
1452 * succeed on this range.
1453 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001454 if (put_user(basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
Mike Kravetzcab350a2017-02-22 15:43:04 -08001455 UFFD_API_RANGE_IOCTLS,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001456 &user_uffdio_register->ioctls))
1457 ret = -EFAULT;
1458 }
1459out:
1460 return ret;
1461}
1462
1463static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1464 unsigned long arg)
1465{
1466 struct mm_struct *mm = ctx->mm;
1467 struct vm_area_struct *vma, *prev, *cur;
1468 int ret;
1469 struct uffdio_range uffdio_unregister;
1470 unsigned long new_flags;
1471 bool found;
1472 unsigned long start, end, vma_end;
1473 const void __user *buf = (void __user *)arg;
1474
1475 ret = -EFAULT;
1476 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1477 goto out;
1478
1479 ret = validate_range(mm, uffdio_unregister.start,
1480 uffdio_unregister.len);
1481 if (ret)
1482 goto out;
1483
1484 start = uffdio_unregister.start;
1485 end = start + uffdio_unregister.len;
1486
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001487 ret = -ENOMEM;
1488 if (!mmget_not_zero(mm))
1489 goto out;
1490
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001491 down_write(&mm->mmap_sem);
1492 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001493 if (!vma)
1494 goto out_unlock;
1495
1496 /* check that there's at least one vma in the range */
1497 ret = -EINVAL;
1498 if (vma->vm_start >= end)
1499 goto out_unlock;
1500
1501 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001502 * If the first vma contains huge pages, make sure start address
1503 * is aligned to huge page size.
1504 */
1505 if (is_vm_hugetlb_page(vma)) {
1506 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1507
1508 if (start & (vma_hpagesize - 1))
1509 goto out_unlock;
1510 }
1511
1512 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001513 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001514 */
1515 found = false;
1516 ret = -EINVAL;
1517 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1518 cond_resched();
1519
1520 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1521 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1522
1523 /*
1524 * Check not compatible vmas, not strictly required
1525 * here as not compatible vmas cannot have an
1526 * userfaultfd_ctx registered on them, but this
1527 * provides for more strict behavior to notice
1528 * unregistration errors.
1529 */
Mike Rapoportba6907d2017-02-22 15:43:22 -08001530 if (!vma_can_userfault(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001531 goto out_unlock;
1532
1533 found = true;
1534 }
1535 BUG_ON(!found);
1536
1537 if (vma->vm_start < start)
1538 prev = vma;
1539
1540 ret = 0;
1541 do {
1542 cond_resched();
1543
Mike Rapoportba6907d2017-02-22 15:43:22 -08001544 BUG_ON(!vma_can_userfault(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001545
1546 /*
1547 * Nothing to do: this vma is already registered into this
1548 * userfaultfd and with the right tracking mode too.
1549 */
1550 if (!vma->vm_userfaultfd_ctx.ctx)
1551 goto skip;
1552
1553 if (vma->vm_start > start)
1554 start = vma->vm_start;
1555 vma_end = min(end, vma->vm_end);
1556
Andrea Arcangeli09fa5292017-02-22 15:42:46 -08001557 if (userfaultfd_missing(vma)) {
1558 /*
1559 * Wake any concurrent pending userfault while
1560 * we unregister, so they will not hang
1561 * permanently and it avoids userland to call
1562 * UFFDIO_WAKE explicitly.
1563 */
1564 struct userfaultfd_wake_range range;
1565 range.start = start;
1566 range.len = vma_end - start;
1567 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1568 }
1569
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001570 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1571 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1572 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1573 vma_policy(vma),
1574 NULL_VM_UFFD_CTX);
1575 if (prev) {
1576 vma = prev;
1577 goto next;
1578 }
1579 if (vma->vm_start < start) {
1580 ret = split_vma(mm, vma, start, 1);
1581 if (ret)
1582 break;
1583 }
1584 if (vma->vm_end > end) {
1585 ret = split_vma(mm, vma, end, 0);
1586 if (ret)
1587 break;
1588 }
1589 next:
1590 /*
1591 * In the vma_merge() successful mprotect-like case 8:
1592 * the next vma was merged into the current one and
1593 * the current one has not been updated yet.
1594 */
1595 vma->vm_flags = new_flags;
1596 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1597
1598 skip:
1599 prev = vma;
1600 start = vma->vm_end;
1601 vma = vma->vm_next;
1602 } while (vma && vma->vm_start < end);
1603out_unlock:
1604 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001605 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001606out:
1607 return ret;
1608}
1609
1610/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001611 * userfaultfd_wake may be used in combination with the
1612 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001613 */
1614static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1615 unsigned long arg)
1616{
1617 int ret;
1618 struct uffdio_range uffdio_wake;
1619 struct userfaultfd_wake_range range;
1620 const void __user *buf = (void __user *)arg;
1621
1622 ret = -EFAULT;
1623 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1624 goto out;
1625
1626 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1627 if (ret)
1628 goto out;
1629
1630 range.start = uffdio_wake.start;
1631 range.len = uffdio_wake.len;
1632
1633 /*
1634 * len == 0 means wake all and we don't want to wake all here,
1635 * so check it again to be sure.
1636 */
1637 VM_BUG_ON(!range.len);
1638
1639 wake_userfault(ctx, &range);
1640 ret = 0;
1641
1642out:
1643 return ret;
1644}
1645
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001646static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1647 unsigned long arg)
1648{
1649 __s64 ret;
1650 struct uffdio_copy uffdio_copy;
1651 struct uffdio_copy __user *user_uffdio_copy;
1652 struct userfaultfd_wake_range range;
1653
1654 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1655
1656 ret = -EFAULT;
1657 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1658 /* don't copy "copy" last field */
1659 sizeof(uffdio_copy)-sizeof(__s64)))
1660 goto out;
1661
1662 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1663 if (ret)
1664 goto out;
1665 /*
1666 * double check for wraparound just in case. copy_from_user()
1667 * will later check uffdio_copy.src + uffdio_copy.len to fit
1668 * in the userland range.
1669 */
1670 ret = -EINVAL;
1671 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1672 goto out;
1673 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1674 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001675 if (mmget_not_zero(ctx->mm)) {
1676 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1677 uffdio_copy.len);
1678 mmput(ctx->mm);
Mike Rapoport96333182017-02-24 14:58:31 -08001679 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001680 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001681 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001682 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1683 return -EFAULT;
1684 if (ret < 0)
1685 goto out;
1686 BUG_ON(!ret);
1687 /* len == 0 would wake all */
1688 range.len = ret;
1689 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1690 range.start = uffdio_copy.dst;
1691 wake_userfault(ctx, &range);
1692 }
1693 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1694out:
1695 return ret;
1696}
1697
1698static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1699 unsigned long arg)
1700{
1701 __s64 ret;
1702 struct uffdio_zeropage uffdio_zeropage;
1703 struct uffdio_zeropage __user *user_uffdio_zeropage;
1704 struct userfaultfd_wake_range range;
1705
1706 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1707
1708 ret = -EFAULT;
1709 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1710 /* don't copy "zeropage" last field */
1711 sizeof(uffdio_zeropage)-sizeof(__s64)))
1712 goto out;
1713
1714 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1715 uffdio_zeropage.range.len);
1716 if (ret)
1717 goto out;
1718 ret = -EINVAL;
1719 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1720 goto out;
1721
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001722 if (mmget_not_zero(ctx->mm)) {
1723 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1724 uffdio_zeropage.range.len);
1725 mmput(ctx->mm);
Mike Rapoport9d95aa42017-08-02 13:32:15 -07001726 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001727 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001728 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001729 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1730 return -EFAULT;
1731 if (ret < 0)
1732 goto out;
1733 /* len == 0 would wake all */
1734 BUG_ON(!ret);
1735 range.len = ret;
1736 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1737 range.start = uffdio_zeropage.range.start;
1738 wake_userfault(ctx, &range);
1739 }
1740 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1741out:
1742 return ret;
1743}
1744
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001745static inline unsigned int uffd_ctx_features(__u64 user_features)
1746{
1747 /*
1748 * For the current set of features the bits just coincide
1749 */
1750 return (unsigned int)user_features;
1751}
1752
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001753/*
1754 * userland asks for a certain API version and we return which bits
1755 * and ioctl commands are implemented in this kernel for such API
1756 * version or -EINVAL if unknown.
1757 */
1758static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1759 unsigned long arg)
1760{
1761 struct uffdio_api uffdio_api;
1762 void __user *buf = (void __user *)arg;
1763 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001764 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001765
1766 ret = -EINVAL;
1767 if (ctx->state != UFFD_STATE_WAIT_API)
1768 goto out;
1769 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001770 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001771 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001772 features = uffdio_api.features;
1773 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001774 memset(&uffdio_api, 0, sizeof(uffdio_api));
1775 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1776 goto out;
1777 ret = -EINVAL;
1778 goto out;
1779 }
Andrea Arcangeli65603142017-02-22 15:42:24 -08001780 /* report all available features and ioctls to userland */
1781 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001782 uffdio_api.ioctls = UFFD_API_IOCTLS;
1783 ret = -EFAULT;
1784 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1785 goto out;
1786 ctx->state = UFFD_STATE_RUNNING;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001787 /* only enable the requested features for this uffd context */
1788 ctx->features = uffd_ctx_features(features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001789 ret = 0;
1790out:
1791 return ret;
1792}
1793
1794static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1795 unsigned long arg)
1796{
1797 int ret = -EINVAL;
1798 struct userfaultfd_ctx *ctx = file->private_data;
1799
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001800 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1801 return -EINVAL;
1802
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001803 switch(cmd) {
1804 case UFFDIO_API:
1805 ret = userfaultfd_api(ctx, arg);
1806 break;
1807 case UFFDIO_REGISTER:
1808 ret = userfaultfd_register(ctx, arg);
1809 break;
1810 case UFFDIO_UNREGISTER:
1811 ret = userfaultfd_unregister(ctx, arg);
1812 break;
1813 case UFFDIO_WAKE:
1814 ret = userfaultfd_wake(ctx, arg);
1815 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001816 case UFFDIO_COPY:
1817 ret = userfaultfd_copy(ctx, arg);
1818 break;
1819 case UFFDIO_ZEROPAGE:
1820 ret = userfaultfd_zeropage(ctx, arg);
1821 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001822 }
1823 return ret;
1824}
1825
1826#ifdef CONFIG_PROC_FS
1827static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1828{
1829 struct userfaultfd_ctx *ctx = f->private_data;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001830 wait_queue_entry_t *wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001831 struct userfaultfd_wait_queue *uwq;
1832 unsigned long pending = 0, total = 0;
1833
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001834 spin_lock(&ctx->fault_pending_wqh.lock);
Ingo Molnar2055da92017-06-20 12:06:46 +02001835 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001836 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001837 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001838 total++;
1839 }
Ingo Molnar2055da92017-06-20 12:06:46 +02001840 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001841 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1842 total++;
1843 }
1844 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001845
1846 /*
1847 * If more protocols will be added, there will be all shown
1848 * separated by a space. Like this:
1849 * protocols: aa:... bb:...
1850 */
1851 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Mike Rapoport045098e2017-04-07 16:04:42 -07001852 pending, total, UFFD_API, ctx->features,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001853 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1854}
1855#endif
1856
1857static const struct file_operations userfaultfd_fops = {
1858#ifdef CONFIG_PROC_FS
1859 .show_fdinfo = userfaultfd_show_fdinfo,
1860#endif
1861 .release = userfaultfd_release,
1862 .poll = userfaultfd_poll,
1863 .read = userfaultfd_read,
1864 .unlocked_ioctl = userfaultfd_ioctl,
1865 .compat_ioctl = userfaultfd_ioctl,
1866 .llseek = noop_llseek,
1867};
1868
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001869static void init_once_userfaultfd_ctx(void *mem)
1870{
1871 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1872
1873 init_waitqueue_head(&ctx->fault_pending_wqh);
1874 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001875 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001876 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001877 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001878}
1879
Eric Biggers284cd242018-01-31 16:19:48 -08001880SYSCALL_DEFINE1(userfaultfd, int, flags)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001881{
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001882 struct userfaultfd_ctx *ctx;
Eric Biggers284cd242018-01-31 16:19:48 -08001883 int fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001884
1885 BUG_ON(!current->mm);
1886
1887 /* Check the UFFD_* constants for consistency. */
1888 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1889 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1890
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001891 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
Eric Biggers284cd242018-01-31 16:19:48 -08001892 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001893
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001894 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001895 if (!ctx)
Eric Biggers284cd242018-01-31 16:19:48 -08001896 return -ENOMEM;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001897
1898 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001899 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001900 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001901 ctx->state = UFFD_STATE_WAIT_API;
1902 ctx->released = false;
1903 ctx->mm = current->mm;
1904 /* prevent the mm struct to be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001905 mmgrab(ctx->mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001906
Eric Biggers284cd242018-01-31 16:19:48 -08001907 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
1908 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1909 if (fd < 0) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001910 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001911 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001912 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001913 return fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001914}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001915
1916static int __init userfaultfd_init(void)
1917{
1918 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1919 sizeof(struct userfaultfd_ctx),
1920 0,
1921 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1922 init_once_userfaultfd_ctx);
1923 return 0;
1924}
1925__initcall(userfaultfd_init);