blob: ea9008254df46f3acb935876468b0c0e9c4e8df5 [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>
17#include <linux/sched.h>
18#include <linux/mm.h>
19#include <linux/poll.h>
20#include <linux/slab.h>
21#include <linux/seq_file.h>
22#include <linux/file.h>
23#include <linux/bug.h>
24#include <linux/anon_inodes.h>
25#include <linux/syscalls.h>
26#include <linux/userfaultfd_k.h>
27#include <linux/mempolicy.h>
28#include <linux/ioctl.h>
29#include <linux/security.h>
30
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070031static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
32
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070033enum userfaultfd_state {
34 UFFD_STATE_WAIT_API,
35 UFFD_STATE_RUNNING,
36};
37
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070038/*
39 * Start with fault_pending_wqh and fault_wqh so they're more likely
40 * to be in the same cacheline.
41 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070042struct userfaultfd_ctx {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -070043 /* waitqueue head for the pending (i.e. not read) userfaults */
44 wait_queue_head_t fault_pending_wqh;
45 /* waitqueue head for the userfaults */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070046 wait_queue_head_t fault_wqh;
47 /* waitqueue head for the pseudo fd to wakeup poll/read */
48 wait_queue_head_t fd_wqh;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080049 /* waitqueue head for events */
50 wait_queue_head_t event_wqh;
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -070051 /* a refile sequence protected by fault_pending_wqh lock */
52 struct seqcount refile_seq;
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070053 /* pseudo fd refcounting */
54 atomic_t refcount;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070055 /* userfaultfd syscall flags */
56 unsigned int flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080057 /* features requested from the userspace */
58 unsigned int features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070059 /* state machine */
60 enum userfaultfd_state state;
61 /* released */
62 bool released;
63 /* mm with one ore more vmas attached to this userfaultfd_ctx */
64 struct mm_struct *mm;
65};
66
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080067struct userfaultfd_fork_ctx {
68 struct userfaultfd_ctx *orig;
69 struct userfaultfd_ctx *new;
70 struct list_head list;
71};
72
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070073struct userfaultfd_wait_queue {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070074 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070075 wait_queue_t wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070076 struct userfaultfd_ctx *ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -080077 bool waken;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070078};
79
80struct userfaultfd_wake_range {
81 unsigned long start;
82 unsigned long len;
83};
84
85static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
86 int wake_flags, void *key)
87{
88 struct userfaultfd_wake_range *range = key;
89 int ret;
90 struct userfaultfd_wait_queue *uwq;
91 unsigned long start, len;
92
93 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
94 ret = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070095 /* len == 0 means wake all */
96 start = range->start;
97 len = range->len;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070098 if (len && (start > uwq->msg.arg.pagefault.address ||
99 start + len <= uwq->msg.arg.pagefault.address))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700100 goto out;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800101 WRITE_ONCE(uwq->waken, true);
102 /*
103 * The implicit smp_mb__before_spinlock in try_to_wake_up()
104 * renders uwq->waken visible to other CPUs before the task is
105 * waken.
106 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700107 ret = wake_up_state(wq->private, mode);
108 if (ret)
109 /*
110 * Wake only once, autoremove behavior.
111 *
112 * After the effect of list_del_init is visible to the
113 * other CPUs, the waitqueue may disappear from under
114 * us, see the !list_empty_careful() in
115 * handle_userfault(). try_to_wake_up() has an
116 * implicit smp_mb__before_spinlock, and the
117 * wq->private is read before calling the extern
118 * function "wake_up_state" (which in turns calls
119 * try_to_wake_up). While the spin_lock;spin_unlock;
120 * wouldn't be enough, the smp_mb__before_spinlock is
121 * enough to avoid an explicit smp_mb() here.
122 */
123 list_del_init(&wq->task_list);
124out:
125 return ret;
126}
127
128/**
129 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
130 * context.
131 * @ctx: [in] Pointer to the userfaultfd context.
132 *
133 * Returns: In case of success, returns not zero.
134 */
135static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
136{
137 if (!atomic_inc_not_zero(&ctx->refcount))
138 BUG();
139}
140
141/**
142 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
143 * context.
144 * @ctx: [in] Pointer to userfaultfd context.
145 *
146 * The userfaultfd context reference must have been previously acquired either
147 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
148 */
149static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
150{
151 if (atomic_dec_and_test(&ctx->refcount)) {
152 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
153 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
154 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
155 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800156 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
157 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700158 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
159 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700160 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700161 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700162 }
163}
164
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700165static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700166{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700167 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
168 /*
169 * Must use memset to zero out the paddings or kernel data is
170 * leaked to userland.
171 */
172 memset(msg, 0, sizeof(struct uffd_msg));
173}
174
175static inline struct uffd_msg userfault_msg(unsigned long address,
176 unsigned int flags,
177 unsigned long reason)
178{
179 struct uffd_msg msg;
180 msg_init(&msg);
181 msg.event = UFFD_EVENT_PAGEFAULT;
182 msg.arg.pagefault.address = address;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700183 if (flags & FAULT_FLAG_WRITE)
184 /*
Andrea Arcangelia4605a62017-02-22 15:42:09 -0800185 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700186 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
187 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
188 * was a read fault, otherwise if set it means it's
189 * a write fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700190 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700191 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700192 if (reason & VM_UFFD_WP)
193 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700194 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
195 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
196 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
197 * a missing fault, otherwise if set it means it's a
198 * write protect fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700199 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700200 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
201 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700202}
203
204/*
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700205 * Verify the pagetables are still not ok after having reigstered into
206 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
207 * userfault that has already been resolved, if userfaultfd_read and
208 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
209 * threads.
210 */
211static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
212 unsigned long address,
213 unsigned long flags,
214 unsigned long reason)
215{
216 struct mm_struct *mm = ctx->mm;
217 pgd_t *pgd;
218 pud_t *pud;
219 pmd_t *pmd, _pmd;
220 pte_t *pte;
221 bool ret = true;
222
223 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
224
225 pgd = pgd_offset(mm, address);
226 if (!pgd_present(*pgd))
227 goto out;
228 pud = pud_offset(pgd, address);
229 if (!pud_present(*pud))
230 goto out;
231 pmd = pmd_offset(pud, address);
232 /*
233 * READ_ONCE must function as a barrier with narrower scope
234 * and it must be equivalent to:
235 * _pmd = *pmd; barrier();
236 *
237 * This is to deal with the instability (as in
238 * pmd_trans_unstable) of the pmd.
239 */
240 _pmd = READ_ONCE(*pmd);
241 if (!pmd_present(_pmd))
242 goto out;
243
244 ret = false;
245 if (pmd_trans_huge(_pmd))
246 goto out;
247
248 /*
249 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
250 * and use the standard pte_offset_map() instead of parsing _pmd.
251 */
252 pte = pte_offset_map(pmd, address);
253 /*
254 * Lockless access: we're in a wait_event so it's ok if it
255 * changes under us.
256 */
257 if (pte_none(*pte))
258 ret = true;
259 pte_unmap(pte);
260
261out:
262 return ret;
263}
264
265/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700266 * The locking rules involved in returning VM_FAULT_RETRY depending on
267 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
268 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
269 * recommendation in __lock_page_or_retry is not an understatement.
270 *
271 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
272 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
273 * not set.
274 *
275 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
276 * set, VM_FAULT_RETRY can still be returned if and only if there are
277 * fatal_signal_pending()s, and the mmap_sem must be released before
278 * returning it.
279 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800280int handle_userfault(struct vm_fault *vmf, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700281{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800282 struct mm_struct *mm = vmf->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700283 struct userfaultfd_ctx *ctx;
284 struct userfaultfd_wait_queue uwq;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700285 int ret;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700286 bool must_wait, return_to_userland;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800287 long blocking_state;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700288
289 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
290
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700291 ret = VM_FAULT_SIGBUS;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800292 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700293 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700294 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700295
296 BUG_ON(ctx->mm != mm);
297
298 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
299 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
300
301 /*
302 * If it's already released don't get it. This avoids to loop
303 * in __get_user_pages if userfaultfd_release waits on the
304 * caller of handle_userfault to release the mmap_sem.
305 */
306 if (unlikely(ACCESS_ONCE(ctx->released)))
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700307 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700308
309 /*
Linus Torvalds39680f52016-03-01 11:56:22 -0800310 * We don't do userfault handling for the final child pid update.
311 */
312 if (current->flags & PF_EXITING)
313 goto out;
314
315 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700316 * Check that we can return VM_FAULT_RETRY.
317 *
318 * NOTE: it should become possible to return VM_FAULT_RETRY
319 * even if FAULT_FLAG_TRIED is set without leading to gup()
320 * -EBUSY failures, if the userfaultfd is to be extended for
321 * VM_UFFD_WP tracking and we intend to arm the userfault
322 * without first stopping userland access to the memory. For
323 * VM_UFFD_MISSING userfaults this is enough for now.
324 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800325 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700326 /*
327 * Validate the invariant that nowait must allow retry
328 * to be sure not to return SIGBUS erroneously on
329 * nowait invocations.
330 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800331 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700332#ifdef CONFIG_DEBUG_VM
333 if (printk_ratelimit()) {
334 printk(KERN_WARNING
Jan Kara82b0f8c2016-12-14 15:06:58 -0800335 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
336 vmf->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700337 dump_stack();
338 }
339#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700340 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700341 }
342
343 /*
344 * Handle nowait, not much to do other than tell it to retry
345 * and wait.
346 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700347 ret = VM_FAULT_RETRY;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800348 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700349 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700350
351 /* take the reference before dropping the mmap_sem */
352 userfaultfd_ctx_get(ctx);
353
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700354 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
355 uwq.wq.private = current;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800356 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700357 uwq.ctx = ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800358 uwq.waken = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700359
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700360 return_to_userland =
Jan Kara82b0f8c2016-12-14 15:06:58 -0800361 (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700362 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800363 blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
364 TASK_KILLABLE;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700365
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700366 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700367 /*
368 * After the __add_wait_queue the uwq is visible to userland
369 * through poll/read().
370 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700371 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
372 /*
373 * The smp_mb() after __set_current_state prevents the reads
374 * following the spin_unlock to happen before the list_add in
375 * __add_wait_queue.
376 */
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800377 set_current_state(blocking_state);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700378 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700379
Jan Kara82b0f8c2016-12-14 15:06:58 -0800380 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
381 reason);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700382 up_read(&mm->mmap_sem);
383
384 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700385 (return_to_userland ? !signal_pending(current) :
386 !fatal_signal_pending(current)))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700387 wake_up_poll(&ctx->fd_wqh, POLLIN);
388 schedule();
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700389 ret |= VM_FAULT_MAJOR;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800390
391 /*
392 * False wakeups can orginate even from rwsem before
393 * up_read() however userfaults will wait either for a
394 * targeted wakeup on the specific uwq waitqueue from
395 * wake_userfault() or for signals or for uffd
396 * release.
397 */
398 while (!READ_ONCE(uwq.waken)) {
399 /*
400 * This needs the full smp_store_mb()
401 * guarantee as the state write must be
402 * visible to other CPUs before reading
403 * uwq.waken from other CPUs.
404 */
405 set_current_state(blocking_state);
406 if (READ_ONCE(uwq.waken) ||
407 READ_ONCE(ctx->released) ||
408 (return_to_userland ? signal_pending(current) :
409 fatal_signal_pending(current)))
410 break;
411 schedule();
412 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700413 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700414
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700415 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700416
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700417 if (return_to_userland) {
418 if (signal_pending(current) &&
419 !fatal_signal_pending(current)) {
420 /*
421 * If we got a SIGSTOP or SIGCONT and this is
422 * a normal userland page fault, just let
423 * userland return so the signal will be
424 * handled and gdb debugging works. The page
425 * fault code immediately after we return from
426 * this function is going to release the
427 * mmap_sem and it's not depending on it
428 * (unlike gup would if we were not to return
429 * VM_FAULT_RETRY).
430 *
431 * If a fatal signal is pending we still take
432 * the streamlined VM_FAULT_RETRY failure path
433 * and there's no need to retake the mmap_sem
434 * in such case.
435 */
436 down_read(&mm->mmap_sem);
437 ret = 0;
438 }
439 }
440
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700441 /*
442 * Here we race with the list_del; list_add in
443 * userfaultfd_ctx_read(), however because we don't ever run
444 * list_del_init() to refile across the two lists, the prev
445 * and next pointers will never point to self. list_add also
446 * would never let any of the two pointers to point to
447 * self. So list_empty_careful won't risk to see both pointers
448 * pointing to self at any time during the list refile. The
449 * only case where list_del_init() is called is the full
450 * removal in the wake function and there we don't re-list_add
451 * and it's fine not to block on the spinlock. The uwq on this
452 * kernel stack can be released after the list_del_init.
453 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700454 if (!list_empty_careful(&uwq.wq.task_list)) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700455 spin_lock(&ctx->fault_pending_wqh.lock);
456 /*
457 * No need of list_del_init(), the uwq on the stack
458 * will be freed shortly anyway.
459 */
460 list_del(&uwq.wq.task_list);
461 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700462 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700463
464 /*
465 * ctx may go away after this if the userfault pseudo fd is
466 * already released.
467 */
468 userfaultfd_ctx_put(ctx);
469
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700470out:
471 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700472}
473
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800474static int userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
475 struct userfaultfd_wait_queue *ewq)
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800476{
477 int ret = 0;
478
479 ewq->ctx = ctx;
480 init_waitqueue_entry(&ewq->wq, current);
481
482 spin_lock(&ctx->event_wqh.lock);
483 /*
484 * After the __add_wait_queue the uwq is visible to userland
485 * through poll/read().
486 */
487 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
488 for (;;) {
489 set_current_state(TASK_KILLABLE);
490 if (ewq->msg.event == 0)
491 break;
492 if (ACCESS_ONCE(ctx->released) ||
493 fatal_signal_pending(current)) {
494 ret = -1;
495 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
496 break;
497 }
498
499 spin_unlock(&ctx->event_wqh.lock);
500
501 wake_up_poll(&ctx->fd_wqh, POLLIN);
502 schedule();
503
504 spin_lock(&ctx->event_wqh.lock);
505 }
506 __set_current_state(TASK_RUNNING);
507 spin_unlock(&ctx->event_wqh.lock);
508
509 /*
510 * ctx may go away after this if the userfault pseudo fd is
511 * already released.
512 */
513
514 userfaultfd_ctx_put(ctx);
515 return ret;
516}
517
518static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
519 struct userfaultfd_wait_queue *ewq)
520{
521 ewq->msg.event = 0;
522 wake_up_locked(&ctx->event_wqh);
523 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
524}
525
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800526int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
527{
528 struct userfaultfd_ctx *ctx = NULL, *octx;
529 struct userfaultfd_fork_ctx *fctx;
530
531 octx = vma->vm_userfaultfd_ctx.ctx;
532 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
533 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
534 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
535 return 0;
536 }
537
538 list_for_each_entry(fctx, fcs, list)
539 if (fctx->orig == octx) {
540 ctx = fctx->new;
541 break;
542 }
543
544 if (!ctx) {
545 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
546 if (!fctx)
547 return -ENOMEM;
548
549 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
550 if (!ctx) {
551 kfree(fctx);
552 return -ENOMEM;
553 }
554
555 atomic_set(&ctx->refcount, 1);
556 ctx->flags = octx->flags;
557 ctx->state = UFFD_STATE_RUNNING;
558 ctx->features = octx->features;
559 ctx->released = false;
560 ctx->mm = vma->vm_mm;
Mike Rapoportd3aadc82017-02-22 15:42:30 -0800561 atomic_inc(&ctx->mm->mm_count);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800562
563 userfaultfd_ctx_get(octx);
564 fctx->orig = octx;
565 fctx->new = ctx;
566 list_add_tail(&fctx->list, fcs);
567 }
568
569 vma->vm_userfaultfd_ctx.ctx = ctx;
570 return 0;
571}
572
573static int dup_fctx(struct userfaultfd_fork_ctx *fctx)
574{
575 struct userfaultfd_ctx *ctx = fctx->orig;
576 struct userfaultfd_wait_queue ewq;
577
578 msg_init(&ewq.msg);
579
580 ewq.msg.event = UFFD_EVENT_FORK;
581 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
582
583 return userfaultfd_event_wait_completion(ctx, &ewq);
584}
585
586void dup_userfaultfd_complete(struct list_head *fcs)
587{
588 int ret = 0;
589 struct userfaultfd_fork_ctx *fctx, *n;
590
591 list_for_each_entry_safe(fctx, n, fcs, list) {
592 if (!ret)
593 ret = dup_fctx(fctx);
594 list_del(&fctx->list);
595 kfree(fctx);
596 }
597}
598
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800599void mremap_userfaultfd_prep(struct vm_area_struct *vma,
600 struct vm_userfaultfd_ctx *vm_ctx)
601{
602 struct userfaultfd_ctx *ctx;
603
604 ctx = vma->vm_userfaultfd_ctx.ctx;
605 if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
606 vm_ctx->ctx = ctx;
607 userfaultfd_ctx_get(ctx);
608 }
609}
610
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800611void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800612 unsigned long from, unsigned long to,
613 unsigned long len)
614{
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800615 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800616 struct userfaultfd_wait_queue ewq;
617
618 if (!ctx)
619 return;
620
621 if (to & ~PAGE_MASK) {
622 userfaultfd_ctx_put(ctx);
623 return;
624 }
625
626 msg_init(&ewq.msg);
627
628 ewq.msg.event = UFFD_EVENT_REMAP;
629 ewq.msg.arg.remap.from = from;
630 ewq.msg.arg.remap.to = to;
631 ewq.msg.arg.remap.len = len;
632
633 userfaultfd_event_wait_completion(ctx, &ewq);
634}
635
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800636void madvise_userfault_dontneed(struct vm_area_struct *vma,
637 struct vm_area_struct **prev,
638 unsigned long start, unsigned long end)
639{
640 struct mm_struct *mm = vma->vm_mm;
641 struct userfaultfd_ctx *ctx;
642 struct userfaultfd_wait_queue ewq;
643
644 ctx = vma->vm_userfaultfd_ctx.ctx;
645 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_MADVDONTNEED))
646 return;
647
648 userfaultfd_ctx_get(ctx);
649 up_read(&mm->mmap_sem);
650
651 *prev = NULL; /* We wait for ACK w/o the mmap semaphore */
652
653 msg_init(&ewq.msg);
654
655 ewq.msg.event = UFFD_EVENT_MADVDONTNEED;
656 ewq.msg.arg.madv_dn.start = start;
657 ewq.msg.arg.madv_dn.end = end;
658
659 userfaultfd_event_wait_completion(ctx, &ewq);
660
661 down_read(&mm->mmap_sem);
662}
663
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700664static int userfaultfd_release(struct inode *inode, struct file *file)
665{
666 struct userfaultfd_ctx *ctx = file->private_data;
667 struct mm_struct *mm = ctx->mm;
668 struct vm_area_struct *vma, *prev;
669 /* len == 0 means wake all */
670 struct userfaultfd_wake_range range = { .len = 0, };
671 unsigned long new_flags;
672
673 ACCESS_ONCE(ctx->released) = true;
674
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700675 if (!mmget_not_zero(mm))
676 goto wakeup;
677
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700678 /*
679 * Flush page faults out of all CPUs. NOTE: all page faults
680 * must be retried without returning VM_FAULT_SIGBUS if
681 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
682 * changes while handle_userfault released the mmap_sem. So
683 * it's critical that released is set to true (above), before
684 * taking the mmap_sem for writing.
685 */
686 down_write(&mm->mmap_sem);
687 prev = NULL;
688 for (vma = mm->mmap; vma; vma = vma->vm_next) {
689 cond_resched();
690 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
691 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
692 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
693 prev = vma;
694 continue;
695 }
696 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
697 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
698 new_flags, vma->anon_vma,
699 vma->vm_file, vma->vm_pgoff,
700 vma_policy(vma),
701 NULL_VM_UFFD_CTX);
702 if (prev)
703 vma = prev;
704 else
705 prev = vma;
706 vma->vm_flags = new_flags;
707 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
708 }
709 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700710 mmput(mm);
711wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700712 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700713 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700714 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700715 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700716 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700717 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700718 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
719 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700720 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700721
722 wake_up_poll(&ctx->fd_wqh, POLLHUP);
723 userfaultfd_ctx_put(ctx);
724 return 0;
725}
726
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700727/* fault_pending_wqh.lock must be hold by the caller */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800728static inline struct userfaultfd_wait_queue *find_userfault_in(
729 wait_queue_head_t *wqh)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700730{
731 wait_queue_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700732 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700733
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800734 VM_BUG_ON(!spin_is_locked(&wqh->lock));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700735
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700736 uwq = NULL;
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800737 if (!waitqueue_active(wqh))
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700738 goto out;
739 /* walk in reverse to provide FIFO behavior to read userfaults */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800740 wq = list_last_entry(&wqh->task_list, typeof(*wq), task_list);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700741 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
742out:
743 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700744}
745
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800746static inline struct userfaultfd_wait_queue *find_userfault(
747 struct userfaultfd_ctx *ctx)
748{
749 return find_userfault_in(&ctx->fault_pending_wqh);
750}
751
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800752static inline struct userfaultfd_wait_queue *find_userfault_evt(
753 struct userfaultfd_ctx *ctx)
754{
755 return find_userfault_in(&ctx->event_wqh);
756}
757
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700758static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
759{
760 struct userfaultfd_ctx *ctx = file->private_data;
761 unsigned int ret;
762
763 poll_wait(file, &ctx->fd_wqh, wait);
764
765 switch (ctx->state) {
766 case UFFD_STATE_WAIT_API:
767 return POLLERR;
768 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700769 /*
770 * poll() never guarantees that read won't block.
771 * userfaults can be waken before they're read().
772 */
773 if (unlikely(!(file->f_flags & O_NONBLOCK)))
774 return POLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700775 /*
776 * lockless access to see if there are pending faults
777 * __pollwait last action is the add_wait_queue but
778 * the spin_unlock would allow the waitqueue_active to
779 * pass above the actual list_add inside
780 * add_wait_queue critical section. So use a full
781 * memory barrier to serialize the list_add write of
782 * add_wait_queue() with the waitqueue_active read
783 * below.
784 */
785 ret = 0;
786 smp_mb();
787 if (waitqueue_active(&ctx->fault_pending_wqh))
788 ret = POLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800789 else if (waitqueue_active(&ctx->event_wqh))
790 ret = POLLIN;
791
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700792 return ret;
793 default:
Andrea Arcangeli84749012017-02-22 15:42:12 -0800794 WARN_ON_ONCE(1);
795 return POLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700796 }
797}
798
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800799static const struct file_operations userfaultfd_fops;
800
801static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
802 struct userfaultfd_ctx *new,
803 struct uffd_msg *msg)
804{
805 int fd;
806 struct file *file;
807 unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
808
809 fd = get_unused_fd_flags(flags);
810 if (fd < 0)
811 return fd;
812
813 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
814 O_RDWR | flags);
815 if (IS_ERR(file)) {
816 put_unused_fd(fd);
817 return PTR_ERR(file);
818 }
819
820 fd_install(fd, file);
821 msg->arg.reserved.reserved1 = 0;
822 msg->arg.fork.ufd = fd;
823
824 return 0;
825}
826
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700827static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700828 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700829{
830 ssize_t ret;
831 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700832 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800833 /*
834 * Handling fork event requires sleeping operations, so
835 * we drop the event_wqh lock, then do these ops, then
836 * lock it back and wake up the waiter. While the lock is
837 * dropped the ewq may go away so we keep track of it
838 * carefully.
839 */
840 LIST_HEAD(fork_event);
841 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700842
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700843 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700844 spin_lock(&ctx->fd_wqh.lock);
845 __add_wait_queue(&ctx->fd_wqh, &wait);
846 for (;;) {
847 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700848 spin_lock(&ctx->fault_pending_wqh.lock);
849 uwq = find_userfault(ctx);
850 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700851 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700852 * Use a seqcount to repeat the lockless check
853 * in wake_userfault() to avoid missing
854 * wakeups because during the refile both
855 * waitqueue could become empty if this is the
856 * only userfault.
857 */
858 write_seqcount_begin(&ctx->refile_seq);
859
860 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700861 * The fault_pending_wqh.lock prevents the uwq
862 * to disappear from under us.
863 *
864 * Refile this userfault from
865 * fault_pending_wqh to fault_wqh, it's not
866 * pending anymore after we read it.
867 *
868 * Use list_del() by hand (as
869 * userfaultfd_wake_function also uses
870 * list_del_init() by hand) to be sure nobody
871 * changes __remove_wait_queue() to use
872 * list_del_init() in turn breaking the
873 * !list_empty_careful() check in
874 * handle_userfault(). The uwq->wq.task_list
875 * must never be empty at any time during the
876 * refile, or the waitqueue could disappear
877 * from under us. The "wait_queue_head_t"
878 * parameter of __remove_wait_queue() is unused
879 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700880 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700881 list_del(&uwq->wq.task_list);
882 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
883
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700884 write_seqcount_end(&ctx->refile_seq);
885
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700886 /* careful to always initialize msg if ret == 0 */
887 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700888 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700889 ret = 0;
890 break;
891 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700892 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800893
894 spin_lock(&ctx->event_wqh.lock);
895 uwq = find_userfault_evt(ctx);
896 if (uwq) {
897 *msg = uwq->msg;
898
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800899 if (uwq->msg.event == UFFD_EVENT_FORK) {
900 fork_nctx = (struct userfaultfd_ctx *)
901 (unsigned long)
902 uwq->msg.arg.reserved.reserved1;
903 list_move(&uwq->wq.task_list, &fork_event);
904 spin_unlock(&ctx->event_wqh.lock);
905 ret = 0;
906 break;
907 }
908
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800909 userfaultfd_event_complete(ctx, uwq);
910 spin_unlock(&ctx->event_wqh.lock);
911 ret = 0;
912 break;
913 }
914 spin_unlock(&ctx->event_wqh.lock);
915
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700916 if (signal_pending(current)) {
917 ret = -ERESTARTSYS;
918 break;
919 }
920 if (no_wait) {
921 ret = -EAGAIN;
922 break;
923 }
924 spin_unlock(&ctx->fd_wqh.lock);
925 schedule();
926 spin_lock(&ctx->fd_wqh.lock);
927 }
928 __remove_wait_queue(&ctx->fd_wqh, &wait);
929 __set_current_state(TASK_RUNNING);
930 spin_unlock(&ctx->fd_wqh.lock);
931
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800932 if (!ret && msg->event == UFFD_EVENT_FORK) {
933 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
934
935 if (!ret) {
936 spin_lock(&ctx->event_wqh.lock);
937 if (!list_empty(&fork_event)) {
938 uwq = list_first_entry(&fork_event,
939 typeof(*uwq),
940 wq.task_list);
941 list_del(&uwq->wq.task_list);
942 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
943 userfaultfd_event_complete(ctx, uwq);
944 }
945 spin_unlock(&ctx->event_wqh.lock);
946 }
947 }
948
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700949 return ret;
950}
951
952static ssize_t userfaultfd_read(struct file *file, char __user *buf,
953 size_t count, loff_t *ppos)
954{
955 struct userfaultfd_ctx *ctx = file->private_data;
956 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700957 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700958 int no_wait = file->f_flags & O_NONBLOCK;
959
960 if (ctx->state == UFFD_STATE_WAIT_API)
961 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700962
963 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700964 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700965 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700966 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700967 if (_ret < 0)
968 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700969 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700970 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700971 ret += sizeof(msg);
972 buf += sizeof(msg);
973 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700974 /*
975 * Allow to read more than one fault at time but only
976 * block if waiting for the very first one.
977 */
978 no_wait = O_NONBLOCK;
979 }
980}
981
982static void __wake_userfault(struct userfaultfd_ctx *ctx,
983 struct userfaultfd_wake_range *range)
984{
985 unsigned long start, end;
986
987 start = range->start;
988 end = range->start + range->len;
989
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700990 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700991 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700992 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700993 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700994 range);
995 if (waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700996 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700997 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700998}
999
1000static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1001 struct userfaultfd_wake_range *range)
1002{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001003 unsigned seq;
1004 bool need_wakeup;
1005
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001006 /*
1007 * To be sure waitqueue_active() is not reordered by the CPU
1008 * before the pagetable update, use an explicit SMP memory
1009 * barrier here. PT lock release or up_read(mmap_sem) still
1010 * have release semantics that can allow the
1011 * waitqueue_active() to be reordered before the pte update.
1012 */
1013 smp_mb();
1014
1015 /*
1016 * Use waitqueue_active because it's very frequent to
1017 * change the address space atomically even if there are no
1018 * userfaults yet. So we take the spinlock only when we're
1019 * sure we've userfaults to wake.
1020 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001021 do {
1022 seq = read_seqcount_begin(&ctx->refile_seq);
1023 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1024 waitqueue_active(&ctx->fault_wqh);
1025 cond_resched();
1026 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1027 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001028 __wake_userfault(ctx, range);
1029}
1030
1031static __always_inline int validate_range(struct mm_struct *mm,
1032 __u64 start, __u64 len)
1033{
1034 __u64 task_size = mm->task_size;
1035
1036 if (start & ~PAGE_MASK)
1037 return -EINVAL;
1038 if (len & ~PAGE_MASK)
1039 return -EINVAL;
1040 if (!len)
1041 return -EINVAL;
1042 if (start < mmap_min_addr)
1043 return -EINVAL;
1044 if (start >= task_size)
1045 return -EINVAL;
1046 if (len > task_size - start)
1047 return -EINVAL;
1048 return 0;
1049}
1050
1051static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1052 unsigned long arg)
1053{
1054 struct mm_struct *mm = ctx->mm;
1055 struct vm_area_struct *vma, *prev, *cur;
1056 int ret;
1057 struct uffdio_register uffdio_register;
1058 struct uffdio_register __user *user_uffdio_register;
1059 unsigned long vm_flags, new_flags;
1060 bool found;
1061 unsigned long start, end, vma_end;
1062
1063 user_uffdio_register = (struct uffdio_register __user *) arg;
1064
1065 ret = -EFAULT;
1066 if (copy_from_user(&uffdio_register, user_uffdio_register,
1067 sizeof(uffdio_register)-sizeof(__u64)))
1068 goto out;
1069
1070 ret = -EINVAL;
1071 if (!uffdio_register.mode)
1072 goto out;
1073 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1074 UFFDIO_REGISTER_MODE_WP))
1075 goto out;
1076 vm_flags = 0;
1077 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1078 vm_flags |= VM_UFFD_MISSING;
1079 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1080 vm_flags |= VM_UFFD_WP;
1081 /*
1082 * FIXME: remove the below error constraint by
1083 * implementing the wprotect tracking mode.
1084 */
1085 ret = -EINVAL;
1086 goto out;
1087 }
1088
1089 ret = validate_range(mm, uffdio_register.range.start,
1090 uffdio_register.range.len);
1091 if (ret)
1092 goto out;
1093
1094 start = uffdio_register.range.start;
1095 end = start + uffdio_register.range.len;
1096
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001097 ret = -ENOMEM;
1098 if (!mmget_not_zero(mm))
1099 goto out;
1100
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001101 down_write(&mm->mmap_sem);
1102 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001103 if (!vma)
1104 goto out_unlock;
1105
1106 /* check that there's at least one vma in the range */
1107 ret = -EINVAL;
1108 if (vma->vm_start >= end)
1109 goto out_unlock;
1110
1111 /*
1112 * Search for not compatible vmas.
1113 *
1114 * FIXME: this shall be relaxed later so that it doesn't fail
1115 * on tmpfs backed vmas (in addition to the current allowance
1116 * on anonymous vmas).
1117 */
1118 found = false;
1119 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1120 cond_resched();
1121
1122 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1123 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1124
1125 /* check not compatible vmas */
1126 ret = -EINVAL;
Andrea Arcangelia94720bf2017-02-22 15:42:15 -08001127 if (!vma_is_anonymous(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001128 goto out_unlock;
1129
1130 /*
1131 * Check that this vma isn't already owned by a
1132 * different userfaultfd. We can't allow more than one
1133 * userfaultfd to own a single vma simultaneously or we
1134 * wouldn't know which one to deliver the userfaults to.
1135 */
1136 ret = -EBUSY;
1137 if (cur->vm_userfaultfd_ctx.ctx &&
1138 cur->vm_userfaultfd_ctx.ctx != ctx)
1139 goto out_unlock;
1140
1141 found = true;
1142 }
1143 BUG_ON(!found);
1144
1145 if (vma->vm_start < start)
1146 prev = vma;
1147
1148 ret = 0;
1149 do {
1150 cond_resched();
1151
Andrea Arcangelia94720bf2017-02-22 15:42:15 -08001152 BUG_ON(!vma_is_anonymous(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001153 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1154 vma->vm_userfaultfd_ctx.ctx != ctx);
1155
1156 /*
1157 * Nothing to do: this vma is already registered into this
1158 * userfaultfd and with the right tracking mode too.
1159 */
1160 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1161 (vma->vm_flags & vm_flags) == vm_flags)
1162 goto skip;
1163
1164 if (vma->vm_start > start)
1165 start = vma->vm_start;
1166 vma_end = min(end, vma->vm_end);
1167
1168 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
1169 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1170 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1171 vma_policy(vma),
1172 ((struct vm_userfaultfd_ctx){ ctx }));
1173 if (prev) {
1174 vma = prev;
1175 goto next;
1176 }
1177 if (vma->vm_start < start) {
1178 ret = split_vma(mm, vma, start, 1);
1179 if (ret)
1180 break;
1181 }
1182 if (vma->vm_end > end) {
1183 ret = split_vma(mm, vma, end, 0);
1184 if (ret)
1185 break;
1186 }
1187 next:
1188 /*
1189 * In the vma_merge() successful mprotect-like case 8:
1190 * the next vma was merged into the current one and
1191 * the current one has not been updated yet.
1192 */
1193 vma->vm_flags = new_flags;
1194 vma->vm_userfaultfd_ctx.ctx = ctx;
1195
1196 skip:
1197 prev = vma;
1198 start = vma->vm_end;
1199 vma = vma->vm_next;
1200 } while (vma && vma->vm_start < end);
1201out_unlock:
1202 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001203 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001204 if (!ret) {
1205 /*
1206 * Now that we scanned all vmas we can already tell
1207 * userland which ioctls methods are guaranteed to
1208 * succeed on this range.
1209 */
1210 if (put_user(UFFD_API_RANGE_IOCTLS,
1211 &user_uffdio_register->ioctls))
1212 ret = -EFAULT;
1213 }
1214out:
1215 return ret;
1216}
1217
1218static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1219 unsigned long arg)
1220{
1221 struct mm_struct *mm = ctx->mm;
1222 struct vm_area_struct *vma, *prev, *cur;
1223 int ret;
1224 struct uffdio_range uffdio_unregister;
1225 unsigned long new_flags;
1226 bool found;
1227 unsigned long start, end, vma_end;
1228 const void __user *buf = (void __user *)arg;
1229
1230 ret = -EFAULT;
1231 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1232 goto out;
1233
1234 ret = validate_range(mm, uffdio_unregister.start,
1235 uffdio_unregister.len);
1236 if (ret)
1237 goto out;
1238
1239 start = uffdio_unregister.start;
1240 end = start + uffdio_unregister.len;
1241
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001242 ret = -ENOMEM;
1243 if (!mmget_not_zero(mm))
1244 goto out;
1245
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001246 down_write(&mm->mmap_sem);
1247 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001248 if (!vma)
1249 goto out_unlock;
1250
1251 /* check that there's at least one vma in the range */
1252 ret = -EINVAL;
1253 if (vma->vm_start >= end)
1254 goto out_unlock;
1255
1256 /*
1257 * Search for not compatible vmas.
1258 *
1259 * FIXME: this shall be relaxed later so that it doesn't fail
1260 * on tmpfs backed vmas (in addition to the current allowance
1261 * on anonymous vmas).
1262 */
1263 found = false;
1264 ret = -EINVAL;
1265 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1266 cond_resched();
1267
1268 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1269 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1270
1271 /*
1272 * Check not compatible vmas, not strictly required
1273 * here as not compatible vmas cannot have an
1274 * userfaultfd_ctx registered on them, but this
1275 * provides for more strict behavior to notice
1276 * unregistration errors.
1277 */
Andrea Arcangelia94720bf2017-02-22 15:42:15 -08001278 if (!vma_is_anonymous(cur))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001279 goto out_unlock;
1280
1281 found = true;
1282 }
1283 BUG_ON(!found);
1284
1285 if (vma->vm_start < start)
1286 prev = vma;
1287
1288 ret = 0;
1289 do {
1290 cond_resched();
1291
Andrea Arcangelia94720bf2017-02-22 15:42:15 -08001292 BUG_ON(!vma_is_anonymous(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001293
1294 /*
1295 * Nothing to do: this vma is already registered into this
1296 * userfaultfd and with the right tracking mode too.
1297 */
1298 if (!vma->vm_userfaultfd_ctx.ctx)
1299 goto skip;
1300
1301 if (vma->vm_start > start)
1302 start = vma->vm_start;
1303 vma_end = min(end, vma->vm_end);
1304
1305 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1306 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1307 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1308 vma_policy(vma),
1309 NULL_VM_UFFD_CTX);
1310 if (prev) {
1311 vma = prev;
1312 goto next;
1313 }
1314 if (vma->vm_start < start) {
1315 ret = split_vma(mm, vma, start, 1);
1316 if (ret)
1317 break;
1318 }
1319 if (vma->vm_end > end) {
1320 ret = split_vma(mm, vma, end, 0);
1321 if (ret)
1322 break;
1323 }
1324 next:
1325 /*
1326 * In the vma_merge() successful mprotect-like case 8:
1327 * the next vma was merged into the current one and
1328 * the current one has not been updated yet.
1329 */
1330 vma->vm_flags = new_flags;
1331 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1332
1333 skip:
1334 prev = vma;
1335 start = vma->vm_end;
1336 vma = vma->vm_next;
1337 } while (vma && vma->vm_start < end);
1338out_unlock:
1339 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001340 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001341out:
1342 return ret;
1343}
1344
1345/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001346 * userfaultfd_wake may be used in combination with the
1347 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001348 */
1349static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1350 unsigned long arg)
1351{
1352 int ret;
1353 struct uffdio_range uffdio_wake;
1354 struct userfaultfd_wake_range range;
1355 const void __user *buf = (void __user *)arg;
1356
1357 ret = -EFAULT;
1358 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1359 goto out;
1360
1361 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1362 if (ret)
1363 goto out;
1364
1365 range.start = uffdio_wake.start;
1366 range.len = uffdio_wake.len;
1367
1368 /*
1369 * len == 0 means wake all and we don't want to wake all here,
1370 * so check it again to be sure.
1371 */
1372 VM_BUG_ON(!range.len);
1373
1374 wake_userfault(ctx, &range);
1375 ret = 0;
1376
1377out:
1378 return ret;
1379}
1380
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001381static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1382 unsigned long arg)
1383{
1384 __s64 ret;
1385 struct uffdio_copy uffdio_copy;
1386 struct uffdio_copy __user *user_uffdio_copy;
1387 struct userfaultfd_wake_range range;
1388
1389 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1390
1391 ret = -EFAULT;
1392 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1393 /* don't copy "copy" last field */
1394 sizeof(uffdio_copy)-sizeof(__s64)))
1395 goto out;
1396
1397 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1398 if (ret)
1399 goto out;
1400 /*
1401 * double check for wraparound just in case. copy_from_user()
1402 * will later check uffdio_copy.src + uffdio_copy.len to fit
1403 * in the userland range.
1404 */
1405 ret = -EINVAL;
1406 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1407 goto out;
1408 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1409 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001410 if (mmget_not_zero(ctx->mm)) {
1411 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1412 uffdio_copy.len);
1413 mmput(ctx->mm);
1414 }
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001415 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1416 return -EFAULT;
1417 if (ret < 0)
1418 goto out;
1419 BUG_ON(!ret);
1420 /* len == 0 would wake all */
1421 range.len = ret;
1422 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1423 range.start = uffdio_copy.dst;
1424 wake_userfault(ctx, &range);
1425 }
1426 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1427out:
1428 return ret;
1429}
1430
1431static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1432 unsigned long arg)
1433{
1434 __s64 ret;
1435 struct uffdio_zeropage uffdio_zeropage;
1436 struct uffdio_zeropage __user *user_uffdio_zeropage;
1437 struct userfaultfd_wake_range range;
1438
1439 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1440
1441 ret = -EFAULT;
1442 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1443 /* don't copy "zeropage" last field */
1444 sizeof(uffdio_zeropage)-sizeof(__s64)))
1445 goto out;
1446
1447 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1448 uffdio_zeropage.range.len);
1449 if (ret)
1450 goto out;
1451 ret = -EINVAL;
1452 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1453 goto out;
1454
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001455 if (mmget_not_zero(ctx->mm)) {
1456 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1457 uffdio_zeropage.range.len);
1458 mmput(ctx->mm);
1459 }
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001460 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1461 return -EFAULT;
1462 if (ret < 0)
1463 goto out;
1464 /* len == 0 would wake all */
1465 BUG_ON(!ret);
1466 range.len = ret;
1467 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1468 range.start = uffdio_zeropage.range.start;
1469 wake_userfault(ctx, &range);
1470 }
1471 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1472out:
1473 return ret;
1474}
1475
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001476static inline unsigned int uffd_ctx_features(__u64 user_features)
1477{
1478 /*
1479 * For the current set of features the bits just coincide
1480 */
1481 return (unsigned int)user_features;
1482}
1483
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001484/*
1485 * userland asks for a certain API version and we return which bits
1486 * and ioctl commands are implemented in this kernel for such API
1487 * version or -EINVAL if unknown.
1488 */
1489static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1490 unsigned long arg)
1491{
1492 struct uffdio_api uffdio_api;
1493 void __user *buf = (void __user *)arg;
1494 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001495 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001496
1497 ret = -EINVAL;
1498 if (ctx->state != UFFD_STATE_WAIT_API)
1499 goto out;
1500 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001501 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001502 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001503 features = uffdio_api.features;
1504 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001505 memset(&uffdio_api, 0, sizeof(uffdio_api));
1506 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1507 goto out;
1508 ret = -EINVAL;
1509 goto out;
1510 }
Andrea Arcangeli65603142017-02-22 15:42:24 -08001511 /* report all available features and ioctls to userland */
1512 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001513 uffdio_api.ioctls = UFFD_API_IOCTLS;
1514 ret = -EFAULT;
1515 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1516 goto out;
1517 ctx->state = UFFD_STATE_RUNNING;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001518 /* only enable the requested features for this uffd context */
1519 ctx->features = uffd_ctx_features(features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001520 ret = 0;
1521out:
1522 return ret;
1523}
1524
1525static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1526 unsigned long arg)
1527{
1528 int ret = -EINVAL;
1529 struct userfaultfd_ctx *ctx = file->private_data;
1530
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001531 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1532 return -EINVAL;
1533
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001534 switch(cmd) {
1535 case UFFDIO_API:
1536 ret = userfaultfd_api(ctx, arg);
1537 break;
1538 case UFFDIO_REGISTER:
1539 ret = userfaultfd_register(ctx, arg);
1540 break;
1541 case UFFDIO_UNREGISTER:
1542 ret = userfaultfd_unregister(ctx, arg);
1543 break;
1544 case UFFDIO_WAKE:
1545 ret = userfaultfd_wake(ctx, arg);
1546 break;
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001547 case UFFDIO_COPY:
1548 ret = userfaultfd_copy(ctx, arg);
1549 break;
1550 case UFFDIO_ZEROPAGE:
1551 ret = userfaultfd_zeropage(ctx, arg);
1552 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001553 }
1554 return ret;
1555}
1556
1557#ifdef CONFIG_PROC_FS
1558static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1559{
1560 struct userfaultfd_ctx *ctx = f->private_data;
1561 wait_queue_t *wq;
1562 struct userfaultfd_wait_queue *uwq;
1563 unsigned long pending = 0, total = 0;
1564
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001565 spin_lock(&ctx->fault_pending_wqh.lock);
1566 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001567 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001568 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001569 total++;
1570 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001571 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1572 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1573 total++;
1574 }
1575 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001576
1577 /*
1578 * If more protocols will be added, there will be all shown
1579 * separated by a space. Like this:
1580 * protocols: aa:... bb:...
1581 */
1582 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001583 pending, total, UFFD_API, UFFD_API_FEATURES,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001584 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1585}
1586#endif
1587
1588static const struct file_operations userfaultfd_fops = {
1589#ifdef CONFIG_PROC_FS
1590 .show_fdinfo = userfaultfd_show_fdinfo,
1591#endif
1592 .release = userfaultfd_release,
1593 .poll = userfaultfd_poll,
1594 .read = userfaultfd_read,
1595 .unlocked_ioctl = userfaultfd_ioctl,
1596 .compat_ioctl = userfaultfd_ioctl,
1597 .llseek = noop_llseek,
1598};
1599
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001600static void init_once_userfaultfd_ctx(void *mem)
1601{
1602 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1603
1604 init_waitqueue_head(&ctx->fault_pending_wqh);
1605 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001606 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001607 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001608 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001609}
1610
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001611/**
1612 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1613 * @flags: Flags for the userfaultfd file.
1614 *
1615 * This function creates an userfaultfd file pointer, w/out installing
1616 * it into the fd table. This is useful when the userfaultfd file is
1617 * used during the initialization of data structures that require
1618 * extra setup after the userfaultfd creation. So the userfaultfd
1619 * creation is split into the file pointer creation phase, and the
1620 * file descriptor installation phase. In this way races with
1621 * userspace closing the newly installed file descriptor can be
1622 * avoided. Returns an userfaultfd file pointer, or a proper error
1623 * pointer.
1624 */
1625static struct file *userfaultfd_file_create(int flags)
1626{
1627 struct file *file;
1628 struct userfaultfd_ctx *ctx;
1629
1630 BUG_ON(!current->mm);
1631
1632 /* Check the UFFD_* constants for consistency. */
1633 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1634 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1635
1636 file = ERR_PTR(-EINVAL);
1637 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1638 goto out;
1639
1640 file = ERR_PTR(-ENOMEM);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001641 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001642 if (!ctx)
1643 goto out;
1644
1645 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001646 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001647 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001648 ctx->state = UFFD_STATE_WAIT_API;
1649 ctx->released = false;
1650 ctx->mm = current->mm;
1651 /* prevent the mm struct to be freed */
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001652 atomic_inc(&ctx->mm->mm_count);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001653
1654 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1655 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
Eric Biggersc03e9462015-09-17 16:01:54 -07001656 if (IS_ERR(file)) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001657 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001658 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001659 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001660out:
1661 return file;
1662}
1663
1664SYSCALL_DEFINE1(userfaultfd, int, flags)
1665{
1666 int fd, error;
1667 struct file *file;
1668
1669 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1670 if (error < 0)
1671 return error;
1672 fd = error;
1673
1674 file = userfaultfd_file_create(flags);
1675 if (IS_ERR(file)) {
1676 error = PTR_ERR(file);
1677 goto err_put_unused_fd;
1678 }
1679 fd_install(fd, file);
1680
1681 return fd;
1682
1683err_put_unused_fd:
1684 put_unused_fd(fd);
1685
1686 return error;
1687}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001688
1689static int __init userfaultfd_init(void)
1690{
1691 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1692 sizeof(struct userfaultfd_ctx),
1693 0,
1694 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1695 init_once_userfaultfd_ctx);
1696 return 0;
1697}
1698__initcall(userfaultfd_init);