blob: 00b661d52727c2cb85bc81f4b652ea13a1fd3eea [file] [log] [blame]
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001/*
2 * fs/userfaultfd.c
3 *
4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
5 * Copyright (C) 2008-2009 Red Hat, Inc.
6 * Copyright (C) 2015 Red Hat, Inc.
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2. See
9 * the COPYING file in the top-level directory.
10 *
11 * Some part derived from fs/eventfd.c (anon inode setup) and
12 * mm/ksm.c (mm hashing).
13 */
14
15#include <linux/hashtable.h>
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/seq_file.h>
21#include <linux/file.h>
22#include <linux/bug.h>
23#include <linux/anon_inodes.h>
24#include <linux/syscalls.h>
25#include <linux/userfaultfd_k.h>
26#include <linux/mempolicy.h>
27#include <linux/ioctl.h>
28#include <linux/security.h>
29
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070030static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
31
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070032enum userfaultfd_state {
33 UFFD_STATE_WAIT_API,
34 UFFD_STATE_RUNNING,
35};
36
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070037/*
38 * Start with fault_pending_wqh and fault_wqh so they're more likely
39 * to be in the same cacheline.
40 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070041struct userfaultfd_ctx {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -070042 /* waitqueue head for the pending (i.e. not read) userfaults */
43 wait_queue_head_t fault_pending_wqh;
44 /* waitqueue head for the userfaults */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070045 wait_queue_head_t fault_wqh;
46 /* waitqueue head for the pseudo fd to wakeup poll/read */
47 wait_queue_head_t fd_wqh;
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -070048 /* a refile sequence protected by fault_pending_wqh lock */
49 struct seqcount refile_seq;
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070050 /* pseudo fd refcounting */
51 atomic_t refcount;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070052 /* userfaultfd syscall flags */
53 unsigned int flags;
54 /* state machine */
55 enum userfaultfd_state state;
56 /* released */
57 bool released;
58 /* mm with one ore more vmas attached to this userfaultfd_ctx */
59 struct mm_struct *mm;
60};
61
62struct userfaultfd_wait_queue {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070063 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070064 wait_queue_t wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070065 struct userfaultfd_ctx *ctx;
Andrea Arcangelidbd9eee2017-01-24 15:17:59 -080066 bool waken;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070067};
68
69struct userfaultfd_wake_range {
70 unsigned long start;
71 unsigned long len;
72};
73
74static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
75 int wake_flags, void *key)
76{
77 struct userfaultfd_wake_range *range = key;
78 int ret;
79 struct userfaultfd_wait_queue *uwq;
80 unsigned long start, len;
81
82 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
83 ret = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070084 /* len == 0 means wake all */
85 start = range->start;
86 len = range->len;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070087 if (len && (start > uwq->msg.arg.pagefault.address ||
88 start + len <= uwq->msg.arg.pagefault.address))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070089 goto out;
Andrea Arcangelidbd9eee2017-01-24 15:17:59 -080090 WRITE_ONCE(uwq->waken, true);
91 /*
92 * The implicit smp_mb__before_spinlock in try_to_wake_up()
93 * renders uwq->waken visible to other CPUs before the task is
94 * waken.
95 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070096 ret = wake_up_state(wq->private, mode);
97 if (ret)
98 /*
99 * Wake only once, autoremove behavior.
100 *
101 * After the effect of list_del_init is visible to the
102 * other CPUs, the waitqueue may disappear from under
103 * us, see the !list_empty_careful() in
104 * handle_userfault(). try_to_wake_up() has an
105 * implicit smp_mb__before_spinlock, and the
106 * wq->private is read before calling the extern
107 * function "wake_up_state" (which in turns calls
108 * try_to_wake_up). While the spin_lock;spin_unlock;
109 * wouldn't be enough, the smp_mb__before_spinlock is
110 * enough to avoid an explicit smp_mb() here.
111 */
112 list_del_init(&wq->task_list);
113out:
114 return ret;
115}
116
117/**
118 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
119 * context.
120 * @ctx: [in] Pointer to the userfaultfd context.
121 *
122 * Returns: In case of success, returns not zero.
123 */
124static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
125{
126 if (!atomic_inc_not_zero(&ctx->refcount))
127 BUG();
128}
129
130/**
131 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
132 * context.
133 * @ctx: [in] Pointer to userfaultfd context.
134 *
135 * The userfaultfd context reference must have been previously acquired either
136 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
137 */
138static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
139{
140 if (atomic_dec_and_test(&ctx->refcount)) {
141 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
142 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
143 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
144 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
145 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
146 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700147 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700148 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700149 }
150}
151
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700152static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700153{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700154 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
155 /*
156 * Must use memset to zero out the paddings or kernel data is
157 * leaked to userland.
158 */
159 memset(msg, 0, sizeof(struct uffd_msg));
160}
161
162static inline struct uffd_msg userfault_msg(unsigned long address,
163 unsigned int flags,
164 unsigned long reason)
165{
166 struct uffd_msg msg;
167 msg_init(&msg);
168 msg.event = UFFD_EVENT_PAGEFAULT;
169 msg.arg.pagefault.address = address;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700170 if (flags & FAULT_FLAG_WRITE)
171 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700172 * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
173 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
174 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
175 * was a read fault, otherwise if set it means it's
176 * a write fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700177 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700178 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700179 if (reason & VM_UFFD_WP)
180 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700181 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
182 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
183 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
184 * a missing fault, otherwise if set it means it's a
185 * write protect fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700186 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700187 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
188 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700189}
190
191/*
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700192 * Verify the pagetables are still not ok after having reigstered into
193 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
194 * userfault that has already been resolved, if userfaultfd_read and
195 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
196 * threads.
197 */
198static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
199 unsigned long address,
200 unsigned long flags,
201 unsigned long reason)
202{
203 struct mm_struct *mm = ctx->mm;
204 pgd_t *pgd;
205 pud_t *pud;
206 pmd_t *pmd, _pmd;
207 pte_t *pte;
208 bool ret = true;
209
210 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
211
212 pgd = pgd_offset(mm, address);
213 if (!pgd_present(*pgd))
214 goto out;
215 pud = pud_offset(pgd, address);
216 if (!pud_present(*pud))
217 goto out;
218 pmd = pmd_offset(pud, address);
219 /*
220 * READ_ONCE must function as a barrier with narrower scope
221 * and it must be equivalent to:
222 * _pmd = *pmd; barrier();
223 *
224 * This is to deal with the instability (as in
225 * pmd_trans_unstable) of the pmd.
226 */
227 _pmd = READ_ONCE(*pmd);
228 if (!pmd_present(_pmd))
229 goto out;
230
231 ret = false;
232 if (pmd_trans_huge(_pmd))
233 goto out;
234
235 /*
236 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
237 * and use the standard pte_offset_map() instead of parsing _pmd.
238 */
239 pte = pte_offset_map(pmd, address);
240 /*
241 * Lockless access: we're in a wait_event so it's ok if it
242 * changes under us.
243 */
244 if (pte_none(*pte))
245 ret = true;
246 pte_unmap(pte);
247
248out:
249 return ret;
250}
251
252/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700253 * The locking rules involved in returning VM_FAULT_RETRY depending on
254 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
255 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
256 * recommendation in __lock_page_or_retry is not an understatement.
257 *
258 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
259 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
260 * not set.
261 *
262 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
263 * set, VM_FAULT_RETRY can still be returned if and only if there are
264 * fatal_signal_pending()s, and the mmap_sem must be released before
265 * returning it.
266 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700267int handle_userfault(struct fault_env *fe, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700268{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700269 struct mm_struct *mm = fe->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700270 struct userfaultfd_ctx *ctx;
271 struct userfaultfd_wait_queue uwq;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700272 int ret;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700273 bool must_wait, return_to_userland;
Andrea Arcangelidbd9eee2017-01-24 15:17:59 -0800274 long blocking_state;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700275
276 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
277
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700278 ret = VM_FAULT_SIGBUS;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700279 ctx = fe->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700280 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700281 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700282
283 BUG_ON(ctx->mm != mm);
284
285 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
286 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
287
288 /*
289 * If it's already released don't get it. This avoids to loop
290 * in __get_user_pages if userfaultfd_release waits on the
291 * caller of handle_userfault to release the mmap_sem.
292 */
293 if (unlikely(ACCESS_ONCE(ctx->released)))
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700294 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700295
296 /*
Linus Torvalds39680f52016-03-01 11:56:22 -0800297 * We don't do userfault handling for the final child pid update.
298 */
299 if (current->flags & PF_EXITING)
300 goto out;
301
302 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700303 * Check that we can return VM_FAULT_RETRY.
304 *
305 * NOTE: it should become possible to return VM_FAULT_RETRY
306 * even if FAULT_FLAG_TRIED is set without leading to gup()
307 * -EBUSY failures, if the userfaultfd is to be extended for
308 * VM_UFFD_WP tracking and we intend to arm the userfault
309 * without first stopping userland access to the memory. For
310 * VM_UFFD_MISSING userfaults this is enough for now.
311 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700312 if (unlikely(!(fe->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700313 /*
314 * Validate the invariant that nowait must allow retry
315 * to be sure not to return SIGBUS erroneously on
316 * nowait invocations.
317 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700318 BUG_ON(fe->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700319#ifdef CONFIG_DEBUG_VM
320 if (printk_ratelimit()) {
321 printk(KERN_WARNING
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700322 "FAULT_FLAG_ALLOW_RETRY missing %x\n", fe->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700323 dump_stack();
324 }
325#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700326 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700327 }
328
329 /*
330 * Handle nowait, not much to do other than tell it to retry
331 * and wait.
332 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700333 ret = VM_FAULT_RETRY;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700334 if (fe->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700335 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700336
337 /* take the reference before dropping the mmap_sem */
338 userfaultfd_ctx_get(ctx);
339
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700340 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
341 uwq.wq.private = current;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700342 uwq.msg = userfault_msg(fe->address, fe->flags, reason);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700343 uwq.ctx = ctx;
Andrea Arcangelidbd9eee2017-01-24 15:17:59 -0800344 uwq.waken = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700345
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700346 return_to_userland =
347 (fe->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700348 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
Andrea Arcangelidbd9eee2017-01-24 15:17:59 -0800349 blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
350 TASK_KILLABLE;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700351
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700352 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700353 /*
354 * After the __add_wait_queue the uwq is visible to userland
355 * through poll/read().
356 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700357 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
358 /*
359 * The smp_mb() after __set_current_state prevents the reads
360 * following the spin_unlock to happen before the list_add in
361 * __add_wait_queue.
362 */
Andrea Arcangelidbd9eee2017-01-24 15:17:59 -0800363 set_current_state(blocking_state);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700364 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700365
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700366 must_wait = userfaultfd_must_wait(ctx, fe->address, fe->flags, reason);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700367 up_read(&mm->mmap_sem);
368
369 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700370 (return_to_userland ? !signal_pending(current) :
371 !fatal_signal_pending(current)))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700372 wake_up_poll(&ctx->fd_wqh, POLLIN);
373 schedule();
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700374 ret |= VM_FAULT_MAJOR;
Andrea Arcangelidbd9eee2017-01-24 15:17:59 -0800375
376 /*
377 * False wakeups can orginate even from rwsem before
378 * up_read() however userfaults will wait either for a
379 * targeted wakeup on the specific uwq waitqueue from
380 * wake_userfault() or for signals or for uffd
381 * release.
382 */
383 while (!READ_ONCE(uwq.waken)) {
384 /*
385 * This needs the full smp_store_mb()
386 * guarantee as the state write must be
387 * visible to other CPUs before reading
388 * uwq.waken from other CPUs.
389 */
390 set_current_state(blocking_state);
391 if (READ_ONCE(uwq.waken) ||
392 READ_ONCE(ctx->released) ||
393 (return_to_userland ? signal_pending(current) :
394 fatal_signal_pending(current)))
395 break;
396 schedule();
397 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700398 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700399
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700400 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700401
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700402 if (return_to_userland) {
403 if (signal_pending(current) &&
404 !fatal_signal_pending(current)) {
405 /*
406 * If we got a SIGSTOP or SIGCONT and this is
407 * a normal userland page fault, just let
408 * userland return so the signal will be
409 * handled and gdb debugging works. The page
410 * fault code immediately after we return from
411 * this function is going to release the
412 * mmap_sem and it's not depending on it
413 * (unlike gup would if we were not to return
414 * VM_FAULT_RETRY).
415 *
416 * If a fatal signal is pending we still take
417 * the streamlined VM_FAULT_RETRY failure path
418 * and there's no need to retake the mmap_sem
419 * in such case.
420 */
421 down_read(&mm->mmap_sem);
Andrea Arcangeli275314e2017-03-09 16:16:28 -0800422 ret = VM_FAULT_NOPAGE;
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700423 }
424 }
425
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700426 /*
427 * Here we race with the list_del; list_add in
428 * userfaultfd_ctx_read(), however because we don't ever run
429 * list_del_init() to refile across the two lists, the prev
430 * and next pointers will never point to self. list_add also
431 * would never let any of the two pointers to point to
432 * self. So list_empty_careful won't risk to see both pointers
433 * pointing to self at any time during the list refile. The
434 * only case where list_del_init() is called is the full
435 * removal in the wake function and there we don't re-list_add
436 * and it's fine not to block on the spinlock. The uwq on this
437 * kernel stack can be released after the list_del_init.
438 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700439 if (!list_empty_careful(&uwq.wq.task_list)) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700440 spin_lock(&ctx->fault_pending_wqh.lock);
441 /*
442 * No need of list_del_init(), the uwq on the stack
443 * will be freed shortly anyway.
444 */
445 list_del(&uwq.wq.task_list);
446 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700447 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700448
449 /*
450 * ctx may go away after this if the userfault pseudo fd is
451 * already released.
452 */
453 userfaultfd_ctx_put(ctx);
454
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700455out:
456 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700457}
458
459static int userfaultfd_release(struct inode *inode, struct file *file)
460{
461 struct userfaultfd_ctx *ctx = file->private_data;
462 struct mm_struct *mm = ctx->mm;
463 struct vm_area_struct *vma, *prev;
464 /* len == 0 means wake all */
465 struct userfaultfd_wake_range range = { .len = 0, };
466 unsigned long new_flags;
467
468 ACCESS_ONCE(ctx->released) = true;
469
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700470 if (!mmget_not_zero(mm))
471 goto wakeup;
472
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700473 /*
474 * Flush page faults out of all CPUs. NOTE: all page faults
475 * must be retried without returning VM_FAULT_SIGBUS if
476 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
477 * changes while handle_userfault released the mmap_sem. So
478 * it's critical that released is set to true (above), before
479 * taking the mmap_sem for writing.
480 */
481 down_write(&mm->mmap_sem);
482 prev = NULL;
483 for (vma = mm->mmap; vma; vma = vma->vm_next) {
484 cond_resched();
485 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
486 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
487 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
488 prev = vma;
489 continue;
490 }
491 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
492 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
493 new_flags, vma->anon_vma,
494 vma->vm_file, vma->vm_pgoff,
495 vma_policy(vma),
Colin Cross3e4578f2015-10-27 16:42:08 -0700496 NULL_VM_UFFD_CTX,
497 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700498 if (prev)
499 vma = prev;
500 else
501 prev = vma;
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200502 vm_write_begin(vma);
503 WRITE_ONCE(vma->vm_flags, new_flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700504 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200505 vm_write_end(vma);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700506 }
507 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700508 mmput(mm);
509wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700510 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700511 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700512 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700513 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700514 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700515 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700516 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
517 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700518 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700519
520 wake_up_poll(&ctx->fd_wqh, POLLHUP);
521 userfaultfd_ctx_put(ctx);
522 return 0;
523}
524
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700525/* fault_pending_wqh.lock must be hold by the caller */
526static inline struct userfaultfd_wait_queue *find_userfault(
527 struct userfaultfd_ctx *ctx)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700528{
529 wait_queue_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700530 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700531
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700532 VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700533
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700534 uwq = NULL;
535 if (!waitqueue_active(&ctx->fault_pending_wqh))
536 goto out;
537 /* walk in reverse to provide FIFO behavior to read userfaults */
538 wq = list_last_entry(&ctx->fault_pending_wqh.task_list,
539 typeof(*wq), task_list);
540 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
541out:
542 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700543}
544
545static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
546{
547 struct userfaultfd_ctx *ctx = file->private_data;
548 unsigned int ret;
549
550 poll_wait(file, &ctx->fd_wqh, wait);
551
552 switch (ctx->state) {
553 case UFFD_STATE_WAIT_API:
554 return POLLERR;
555 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700556 /*
557 * poll() never guarantees that read won't block.
558 * userfaults can be waken before they're read().
559 */
560 if (unlikely(!(file->f_flags & O_NONBLOCK)))
561 return POLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700562 /*
563 * lockless access to see if there are pending faults
564 * __pollwait last action is the add_wait_queue but
565 * the spin_unlock would allow the waitqueue_active to
566 * pass above the actual list_add inside
567 * add_wait_queue critical section. So use a full
568 * memory barrier to serialize the list_add write of
569 * add_wait_queue() with the waitqueue_active read
570 * below.
571 */
572 ret = 0;
573 smp_mb();
574 if (waitqueue_active(&ctx->fault_pending_wqh))
575 ret = POLLIN;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700576 return ret;
577 default:
578 BUG();
579 }
580}
581
582static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700583 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700584{
585 ssize_t ret;
586 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700587 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700588
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700589 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700590 spin_lock(&ctx->fd_wqh.lock);
591 __add_wait_queue(&ctx->fd_wqh, &wait);
592 for (;;) {
593 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700594 spin_lock(&ctx->fault_pending_wqh.lock);
595 uwq = find_userfault(ctx);
596 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700597 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700598 * Use a seqcount to repeat the lockless check
599 * in wake_userfault() to avoid missing
600 * wakeups because during the refile both
601 * waitqueue could become empty if this is the
602 * only userfault.
603 */
604 write_seqcount_begin(&ctx->refile_seq);
605
606 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700607 * The fault_pending_wqh.lock prevents the uwq
608 * to disappear from under us.
609 *
610 * Refile this userfault from
611 * fault_pending_wqh to fault_wqh, it's not
612 * pending anymore after we read it.
613 *
614 * Use list_del() by hand (as
615 * userfaultfd_wake_function also uses
616 * list_del_init() by hand) to be sure nobody
617 * changes __remove_wait_queue() to use
618 * list_del_init() in turn breaking the
619 * !list_empty_careful() check in
620 * handle_userfault(). The uwq->wq.task_list
621 * must never be empty at any time during the
622 * refile, or the waitqueue could disappear
623 * from under us. The "wait_queue_head_t"
624 * parameter of __remove_wait_queue() is unused
625 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700626 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700627 list_del(&uwq->wq.task_list);
628 __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
629
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700630 write_seqcount_end(&ctx->refile_seq);
631
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700632 /* careful to always initialize msg if ret == 0 */
633 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700634 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700635 ret = 0;
636 break;
637 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700638 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700639 if (signal_pending(current)) {
640 ret = -ERESTARTSYS;
641 break;
642 }
643 if (no_wait) {
644 ret = -EAGAIN;
645 break;
646 }
647 spin_unlock(&ctx->fd_wqh.lock);
648 schedule();
649 spin_lock(&ctx->fd_wqh.lock);
650 }
651 __remove_wait_queue(&ctx->fd_wqh, &wait);
652 __set_current_state(TASK_RUNNING);
653 spin_unlock(&ctx->fd_wqh.lock);
654
655 return ret;
656}
657
658static ssize_t userfaultfd_read(struct file *file, char __user *buf,
659 size_t count, loff_t *ppos)
660{
661 struct userfaultfd_ctx *ctx = file->private_data;
662 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700663 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700664 int no_wait = file->f_flags & O_NONBLOCK;
665
666 if (ctx->state == UFFD_STATE_WAIT_API)
667 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700668
669 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700670 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700671 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700672 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700673 if (_ret < 0)
674 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700675 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700676 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700677 ret += sizeof(msg);
678 buf += sizeof(msg);
679 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700680 /*
681 * Allow to read more than one fault at time but only
682 * block if waiting for the very first one.
683 */
684 no_wait = O_NONBLOCK;
685 }
686}
687
688static void __wake_userfault(struct userfaultfd_ctx *ctx,
689 struct userfaultfd_wake_range *range)
690{
691 unsigned long start, end;
692
693 start = range->start;
694 end = range->start + range->len;
695
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700696 spin_lock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700697 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700698 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700699 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700700 range);
701 if (waitqueue_active(&ctx->fault_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700702 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700703 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700704}
705
706static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
707 struct userfaultfd_wake_range *range)
708{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700709 unsigned seq;
710 bool need_wakeup;
711
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700712 /*
713 * To be sure waitqueue_active() is not reordered by the CPU
714 * before the pagetable update, use an explicit SMP memory
715 * barrier here. PT lock release or up_read(mmap_sem) still
716 * have release semantics that can allow the
717 * waitqueue_active() to be reordered before the pte update.
718 */
719 smp_mb();
720
721 /*
722 * Use waitqueue_active because it's very frequent to
723 * change the address space atomically even if there are no
724 * userfaults yet. So we take the spinlock only when we're
725 * sure we've userfaults to wake.
726 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -0700727 do {
728 seq = read_seqcount_begin(&ctx->refile_seq);
729 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
730 waitqueue_active(&ctx->fault_wqh);
731 cond_resched();
732 } while (read_seqcount_retry(&ctx->refile_seq, seq));
733 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700734 __wake_userfault(ctx, range);
735}
736
737static __always_inline int validate_range(struct mm_struct *mm,
738 __u64 start, __u64 len)
739{
740 __u64 task_size = mm->task_size;
741
742 if (start & ~PAGE_MASK)
743 return -EINVAL;
744 if (len & ~PAGE_MASK)
745 return -EINVAL;
746 if (!len)
747 return -EINVAL;
748 if (start < mmap_min_addr)
749 return -EINVAL;
750 if (start >= task_size)
751 return -EINVAL;
752 if (len > task_size - start)
753 return -EINVAL;
754 return 0;
755}
756
757static int userfaultfd_register(struct userfaultfd_ctx *ctx,
758 unsigned long arg)
759{
760 struct mm_struct *mm = ctx->mm;
761 struct vm_area_struct *vma, *prev, *cur;
762 int ret;
763 struct uffdio_register uffdio_register;
764 struct uffdio_register __user *user_uffdio_register;
765 unsigned long vm_flags, new_flags;
766 bool found;
767 unsigned long start, end, vma_end;
768
769 user_uffdio_register = (struct uffdio_register __user *) arg;
770
771 ret = -EFAULT;
772 if (copy_from_user(&uffdio_register, user_uffdio_register,
773 sizeof(uffdio_register)-sizeof(__u64)))
774 goto out;
775
776 ret = -EINVAL;
777 if (!uffdio_register.mode)
778 goto out;
779 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
780 UFFDIO_REGISTER_MODE_WP))
781 goto out;
782 vm_flags = 0;
783 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
784 vm_flags |= VM_UFFD_MISSING;
785 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
786 vm_flags |= VM_UFFD_WP;
787 /*
788 * FIXME: remove the below error constraint by
789 * implementing the wprotect tracking mode.
790 */
791 ret = -EINVAL;
792 goto out;
793 }
794
795 ret = validate_range(mm, uffdio_register.range.start,
796 uffdio_register.range.len);
797 if (ret)
798 goto out;
799
800 start = uffdio_register.range.start;
801 end = start + uffdio_register.range.len;
802
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700803 ret = -ENOMEM;
804 if (!mmget_not_zero(mm))
805 goto out;
806
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700807 down_write(&mm->mmap_sem);
808 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700809 if (!vma)
810 goto out_unlock;
811
812 /* check that there's at least one vma in the range */
813 ret = -EINVAL;
814 if (vma->vm_start >= end)
815 goto out_unlock;
816
817 /*
818 * Search for not compatible vmas.
819 *
820 * FIXME: this shall be relaxed later so that it doesn't fail
821 * on tmpfs backed vmas (in addition to the current allowance
822 * on anonymous vmas).
823 */
824 found = false;
825 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
826 cond_resched();
827
828 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
829 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
830
831 /* check not compatible vmas */
832 ret = -EINVAL;
833 if (cur->vm_ops)
834 goto out_unlock;
835
836 /*
837 * Check that this vma isn't already owned by a
838 * different userfaultfd. We can't allow more than one
839 * userfaultfd to own a single vma simultaneously or we
840 * wouldn't know which one to deliver the userfaults to.
841 */
842 ret = -EBUSY;
843 if (cur->vm_userfaultfd_ctx.ctx &&
844 cur->vm_userfaultfd_ctx.ctx != ctx)
845 goto out_unlock;
846
847 found = true;
848 }
849 BUG_ON(!found);
850
851 if (vma->vm_start < start)
852 prev = vma;
853
854 ret = 0;
855 do {
856 cond_resched();
857
858 BUG_ON(vma->vm_ops);
859 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
860 vma->vm_userfaultfd_ctx.ctx != ctx);
861
862 /*
863 * Nothing to do: this vma is already registered into this
864 * userfaultfd and with the right tracking mode too.
865 */
866 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
867 (vma->vm_flags & vm_flags) == vm_flags)
868 goto skip;
869
870 if (vma->vm_start > start)
871 start = vma->vm_start;
872 vma_end = min(end, vma->vm_end);
873
874 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
875 prev = vma_merge(mm, prev, start, vma_end, new_flags,
876 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
877 vma_policy(vma),
Colin Cross3e4578f2015-10-27 16:42:08 -0700878 ((struct vm_userfaultfd_ctx){ ctx }),
879 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700880 if (prev) {
881 vma = prev;
882 goto next;
883 }
884 if (vma->vm_start < start) {
885 ret = split_vma(mm, vma, start, 1);
886 if (ret)
887 break;
888 }
889 if (vma->vm_end > end) {
890 ret = split_vma(mm, vma, end, 0);
891 if (ret)
892 break;
893 }
894 next:
895 /*
896 * In the vma_merge() successful mprotect-like case 8:
897 * the next vma was merged into the current one and
898 * the current one has not been updated yet.
899 */
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200900 vm_write_begin(vma);
901 WRITE_ONCE(vma->vm_flags, new_flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700902 vma->vm_userfaultfd_ctx.ctx = ctx;
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200903 vm_write_end(vma);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700904
905 skip:
906 prev = vma;
907 start = vma->vm_end;
908 vma = vma->vm_next;
909 } while (vma && vma->vm_start < end);
910out_unlock:
911 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700912 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700913 if (!ret) {
914 /*
915 * Now that we scanned all vmas we can already tell
916 * userland which ioctls methods are guaranteed to
917 * succeed on this range.
918 */
919 if (put_user(UFFD_API_RANGE_IOCTLS,
920 &user_uffdio_register->ioctls))
921 ret = -EFAULT;
922 }
923out:
924 return ret;
925}
926
927static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
928 unsigned long arg)
929{
930 struct mm_struct *mm = ctx->mm;
931 struct vm_area_struct *vma, *prev, *cur;
932 int ret;
933 struct uffdio_range uffdio_unregister;
934 unsigned long new_flags;
935 bool found;
936 unsigned long start, end, vma_end;
937 const void __user *buf = (void __user *)arg;
938
939 ret = -EFAULT;
940 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
941 goto out;
942
943 ret = validate_range(mm, uffdio_unregister.start,
944 uffdio_unregister.len);
945 if (ret)
946 goto out;
947
948 start = uffdio_unregister.start;
949 end = start + uffdio_unregister.len;
950
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700951 ret = -ENOMEM;
952 if (!mmget_not_zero(mm))
953 goto out;
954
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700955 down_write(&mm->mmap_sem);
956 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700957 if (!vma)
958 goto out_unlock;
959
960 /* check that there's at least one vma in the range */
961 ret = -EINVAL;
962 if (vma->vm_start >= end)
963 goto out_unlock;
964
965 /*
966 * Search for not compatible vmas.
967 *
968 * FIXME: this shall be relaxed later so that it doesn't fail
969 * on tmpfs backed vmas (in addition to the current allowance
970 * on anonymous vmas).
971 */
972 found = false;
973 ret = -EINVAL;
974 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
975 cond_resched();
976
977 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
978 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
979
980 /*
981 * Check not compatible vmas, not strictly required
982 * here as not compatible vmas cannot have an
983 * userfaultfd_ctx registered on them, but this
984 * provides for more strict behavior to notice
985 * unregistration errors.
986 */
987 if (cur->vm_ops)
988 goto out_unlock;
989
990 found = true;
991 }
992 BUG_ON(!found);
993
994 if (vma->vm_start < start)
995 prev = vma;
996
997 ret = 0;
998 do {
999 cond_resched();
1000
1001 BUG_ON(vma->vm_ops);
1002
1003 /*
1004 * Nothing to do: this vma is already registered into this
1005 * userfaultfd and with the right tracking mode too.
1006 */
1007 if (!vma->vm_userfaultfd_ctx.ctx)
1008 goto skip;
1009
1010 if (vma->vm_start > start)
1011 start = vma->vm_start;
1012 vma_end = min(end, vma->vm_end);
1013
1014 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1015 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1016 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1017 vma_policy(vma),
Colin Cross3e4578f2015-10-27 16:42:08 -07001018 NULL_VM_UFFD_CTX,
1019 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001020 if (prev) {
1021 vma = prev;
1022 goto next;
1023 }
1024 if (vma->vm_start < start) {
1025 ret = split_vma(mm, vma, start, 1);
1026 if (ret)
1027 break;
1028 }
1029 if (vma->vm_end > end) {
1030 ret = split_vma(mm, vma, end, 0);
1031 if (ret)
1032 break;
1033 }
1034 next:
1035 /*
1036 * In the vma_merge() successful mprotect-like case 8:
1037 * the next vma was merged into the current one and
1038 * the current one has not been updated yet.
1039 */
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001040 vm_write_begin(vma);
1041 WRITE_ONCE(vma->vm_flags, new_flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001042 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001043 vm_write_end(vma);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001044
1045 skip:
1046 prev = vma;
1047 start = vma->vm_end;
1048 vma = vma->vm_next;
1049 } while (vma && vma->vm_start < end);
1050out_unlock:
1051 up_write(&mm->mmap_sem);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001052 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001053out:
1054 return ret;
1055}
1056
1057/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001058 * userfaultfd_wake may be used in combination with the
1059 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001060 */
1061static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1062 unsigned long arg)
1063{
1064 int ret;
1065 struct uffdio_range uffdio_wake;
1066 struct userfaultfd_wake_range range;
1067 const void __user *buf = (void __user *)arg;
1068
1069 ret = -EFAULT;
1070 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1071 goto out;
1072
1073 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1074 if (ret)
1075 goto out;
1076
1077 range.start = uffdio_wake.start;
1078 range.len = uffdio_wake.len;
1079
1080 /*
1081 * len == 0 means wake all and we don't want to wake all here,
1082 * so check it again to be sure.
1083 */
1084 VM_BUG_ON(!range.len);
1085
1086 wake_userfault(ctx, &range);
1087 ret = 0;
1088
1089out:
1090 return ret;
1091}
1092
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001093static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1094 unsigned long arg)
1095{
1096 __s64 ret;
1097 struct uffdio_copy uffdio_copy;
1098 struct uffdio_copy __user *user_uffdio_copy;
1099 struct userfaultfd_wake_range range;
1100
1101 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1102
1103 ret = -EFAULT;
1104 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1105 /* don't copy "copy" last field */
1106 sizeof(uffdio_copy)-sizeof(__s64)))
1107 goto out;
1108
1109 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1110 if (ret)
1111 goto out;
1112 /*
1113 * double check for wraparound just in case. copy_from_user()
1114 * will later check uffdio_copy.src + uffdio_copy.len to fit
1115 * in the userland range.
1116 */
1117 ret = -EINVAL;
1118 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1119 goto out;
1120 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1121 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001122 if (mmget_not_zero(ctx->mm)) {
1123 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1124 uffdio_copy.len);
1125 mmput(ctx->mm);
1126 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001127 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1128 return -EFAULT;
1129 if (ret < 0)
1130 goto out;
1131 BUG_ON(!ret);
1132 /* len == 0 would wake all */
1133 range.len = ret;
1134 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1135 range.start = uffdio_copy.dst;
1136 wake_userfault(ctx, &range);
1137 }
1138 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1139out:
1140 return ret;
1141}
1142
1143static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1144 unsigned long arg)
1145{
1146 __s64 ret;
1147 struct uffdio_zeropage uffdio_zeropage;
1148 struct uffdio_zeropage __user *user_uffdio_zeropage;
1149 struct userfaultfd_wake_range range;
1150
1151 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1152
1153 ret = -EFAULT;
1154 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1155 /* don't copy "zeropage" last field */
1156 sizeof(uffdio_zeropage)-sizeof(__s64)))
1157 goto out;
1158
1159 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1160 uffdio_zeropage.range.len);
1161 if (ret)
1162 goto out;
1163 ret = -EINVAL;
1164 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1165 goto out;
1166
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001167 if (mmget_not_zero(ctx->mm)) {
1168 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1169 uffdio_zeropage.range.len);
1170 mmput(ctx->mm);
1171 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001172 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1173 return -EFAULT;
1174 if (ret < 0)
1175 goto out;
1176 /* len == 0 would wake all */
1177 BUG_ON(!ret);
1178 range.len = ret;
1179 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1180 range.start = uffdio_zeropage.range.start;
1181 wake_userfault(ctx, &range);
1182 }
1183 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1184out:
1185 return ret;
1186}
1187
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001188/*
1189 * userland asks for a certain API version and we return which bits
1190 * and ioctl commands are implemented in this kernel for such API
1191 * version or -EINVAL if unknown.
1192 */
1193static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1194 unsigned long arg)
1195{
1196 struct uffdio_api uffdio_api;
1197 void __user *buf = (void __user *)arg;
1198 int ret;
1199
1200 ret = -EINVAL;
1201 if (ctx->state != UFFD_STATE_WAIT_API)
1202 goto out;
1203 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001204 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001205 goto out;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001206 if (uffdio_api.api != UFFD_API || uffdio_api.features) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001207 memset(&uffdio_api, 0, sizeof(uffdio_api));
1208 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1209 goto out;
1210 ret = -EINVAL;
1211 goto out;
1212 }
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001213 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001214 uffdio_api.ioctls = UFFD_API_IOCTLS;
1215 ret = -EFAULT;
1216 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1217 goto out;
1218 ctx->state = UFFD_STATE_RUNNING;
1219 ret = 0;
1220out:
1221 return ret;
1222}
1223
1224static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1225 unsigned long arg)
1226{
1227 int ret = -EINVAL;
1228 struct userfaultfd_ctx *ctx = file->private_data;
1229
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001230 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1231 return -EINVAL;
1232
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001233 switch(cmd) {
1234 case UFFDIO_API:
1235 ret = userfaultfd_api(ctx, arg);
1236 break;
1237 case UFFDIO_REGISTER:
1238 ret = userfaultfd_register(ctx, arg);
1239 break;
1240 case UFFDIO_UNREGISTER:
1241 ret = userfaultfd_unregister(ctx, arg);
1242 break;
1243 case UFFDIO_WAKE:
1244 ret = userfaultfd_wake(ctx, arg);
1245 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001246 case UFFDIO_COPY:
1247 ret = userfaultfd_copy(ctx, arg);
1248 break;
1249 case UFFDIO_ZEROPAGE:
1250 ret = userfaultfd_zeropage(ctx, arg);
1251 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001252 }
1253 return ret;
1254}
1255
1256#ifdef CONFIG_PROC_FS
1257static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1258{
1259 struct userfaultfd_ctx *ctx = f->private_data;
1260 wait_queue_t *wq;
1261 struct userfaultfd_wait_queue *uwq;
1262 unsigned long pending = 0, total = 0;
1263
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001264 spin_lock(&ctx->fault_pending_wqh.lock);
1265 list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001266 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001267 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001268 total++;
1269 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001270 list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
1271 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
1272 total++;
1273 }
1274 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001275
1276 /*
1277 * If more protocols will be added, there will be all shown
1278 * separated by a space. Like this:
1279 * protocols: aa:... bb:...
1280 */
1281 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Pavel Emelyanov3f602d22015-09-04 15:46:34 -07001282 pending, total, UFFD_API, UFFD_API_FEATURES,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001283 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1284}
1285#endif
1286
1287static const struct file_operations userfaultfd_fops = {
1288#ifdef CONFIG_PROC_FS
1289 .show_fdinfo = userfaultfd_show_fdinfo,
1290#endif
1291 .release = userfaultfd_release,
1292 .poll = userfaultfd_poll,
1293 .read = userfaultfd_read,
1294 .unlocked_ioctl = userfaultfd_ioctl,
1295 .compat_ioctl = userfaultfd_ioctl,
1296 .llseek = noop_llseek,
1297};
1298
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001299static void init_once_userfaultfd_ctx(void *mem)
1300{
1301 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1302
1303 init_waitqueue_head(&ctx->fault_pending_wqh);
1304 init_waitqueue_head(&ctx->fault_wqh);
1305 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001306 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001307}
1308
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001309/**
1310 * userfaultfd_file_create - Creates an userfaultfd file pointer.
1311 * @flags: Flags for the userfaultfd file.
1312 *
1313 * This function creates an userfaultfd file pointer, w/out installing
1314 * it into the fd table. This is useful when the userfaultfd file is
1315 * used during the initialization of data structures that require
1316 * extra setup after the userfaultfd creation. So the userfaultfd
1317 * creation is split into the file pointer creation phase, and the
1318 * file descriptor installation phase. In this way races with
1319 * userspace closing the newly installed file descriptor can be
1320 * avoided. Returns an userfaultfd file pointer, or a proper error
1321 * pointer.
1322 */
1323static struct file *userfaultfd_file_create(int flags)
1324{
1325 struct file *file;
1326 struct userfaultfd_ctx *ctx;
1327
1328 BUG_ON(!current->mm);
1329
1330 /* Check the UFFD_* constants for consistency. */
1331 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1332 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1333
1334 file = ERR_PTR(-EINVAL);
1335 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1336 goto out;
1337
1338 file = ERR_PTR(-ENOMEM);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001339 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001340 if (!ctx)
1341 goto out;
1342
1343 atomic_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001344 ctx->flags = flags;
1345 ctx->state = UFFD_STATE_WAIT_API;
1346 ctx->released = false;
1347 ctx->mm = current->mm;
1348 /* prevent the mm struct to be freed */
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001349 atomic_inc(&ctx->mm->mm_count);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001350
1351 file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1352 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
Eric Biggersc03e9462015-09-17 16:01:54 -07001353 if (IS_ERR(file)) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001354 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001355 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001356 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001357out:
1358 return file;
1359}
1360
1361SYSCALL_DEFINE1(userfaultfd, int, flags)
1362{
1363 int fd, error;
1364 struct file *file;
1365
1366 error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1367 if (error < 0)
1368 return error;
1369 fd = error;
1370
1371 file = userfaultfd_file_create(flags);
1372 if (IS_ERR(file)) {
1373 error = PTR_ERR(file);
1374 goto err_put_unused_fd;
1375 }
1376 fd_install(fd, file);
1377
1378 return fd;
1379
1380err_put_unused_fd:
1381 put_unused_fd(fd);
1382
1383 return error;
1384}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001385
1386static int __init userfaultfd_init(void)
1387{
1388 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
1389 sizeof(struct userfaultfd_ctx),
1390 0,
1391 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1392 init_once_userfaultfd_ctx);
1393 return 0;
1394}
1395__initcall(userfaultfd_init);