]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - fs/userfaultfd.c
userfaultfd: UFFD_FEATURE_WP_ASYNC
[thirdparty/kernel/stable.git] / fs / userfaultfd.c
CommitLineData
20c8ccb1 1// SPDX-License-Identifier: GPL-2.0-only
86039bd3
AA
2/*
3 * fs/userfaultfd.c
4 *
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 * Copyright (C) 2008-2009 Red Hat, Inc.
7 * Copyright (C) 2015 Red Hat, Inc.
8 *
86039bd3
AA
9 * Some part derived from fs/eventfd.c (anon inode setup) and
10 * mm/ksm.c (mm hashing).
11 */
12
9cd75c3c 13#include <linux/list.h>
86039bd3 14#include <linux/hashtable.h>
174cd4b1 15#include <linux/sched/signal.h>
6e84f315 16#include <linux/sched/mm.h>
86039bd3 17#include <linux/mm.h>
17fca131 18#include <linux/mm_inline.h>
6dfeaff9 19#include <linux/mmu_notifier.h>
86039bd3
AA
20#include <linux/poll.h>
21#include <linux/slab.h>
22#include <linux/seq_file.h>
23#include <linux/file.h>
24#include <linux/bug.h>
25#include <linux/anon_inodes.h>
26#include <linux/syscalls.h>
27#include <linux/userfaultfd_k.h>
28#include <linux/mempolicy.h>
29#include <linux/ioctl.h>
30#include <linux/security.h>
cab350af 31#include <linux/hugetlb.h>
5c041f5d 32#include <linux/swapops.h>
2d5de004 33#include <linux/miscdevice.h>
86039bd3 34
2d337b71
Z
35static int sysctl_unprivileged_userfaultfd __read_mostly;
36
37#ifdef CONFIG_SYSCTL
38static struct ctl_table vm_userfaultfd_table[] = {
39 {
40 .procname = "unprivileged_userfaultfd",
41 .data = &sysctl_unprivileged_userfaultfd,
42 .maxlen = sizeof(sysctl_unprivileged_userfaultfd),
43 .mode = 0644,
44 .proc_handler = proc_dointvec_minmax,
45 .extra1 = SYSCTL_ZERO,
46 .extra2 = SYSCTL_ONE,
47 },
48 { }
49};
50#endif
cefdca0a 51
3004ec9c
AA
52static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
53
3004ec9c
AA
54/*
55 * Start with fault_pending_wqh and fault_wqh so they're more likely
56 * to be in the same cacheline.
cbcfa130
EB
57 *
58 * Locking order:
59 * fd_wqh.lock
60 * fault_pending_wqh.lock
61 * fault_wqh.lock
62 * event_wqh.lock
63 *
64 * To avoid deadlocks, IRQs must be disabled when taking any of the above locks,
65 * since fd_wqh.lock is taken by aio_poll() while it's holding a lock that's
66 * also taken in IRQ context.
3004ec9c 67 */
86039bd3 68struct userfaultfd_ctx {
15b726ef
AA
69 /* waitqueue head for the pending (i.e. not read) userfaults */
70 wait_queue_head_t fault_pending_wqh;
71 /* waitqueue head for the userfaults */
86039bd3
AA
72 wait_queue_head_t fault_wqh;
73 /* waitqueue head for the pseudo fd to wakeup poll/read */
74 wait_queue_head_t fd_wqh;
9cd75c3c
PE
75 /* waitqueue head for events */
76 wait_queue_head_t event_wqh;
2c5b7e1b 77 /* a refile sequence protected by fault_pending_wqh lock */
2ca97ac8 78 seqcount_spinlock_t refile_seq;
3004ec9c 79 /* pseudo fd refcounting */
ca880420 80 refcount_t refcount;
86039bd3
AA
81 /* userfaultfd syscall flags */
82 unsigned int flags;
9cd75c3c
PE
83 /* features requested from the userspace */
84 unsigned int features;
86039bd3
AA
85 /* released */
86 bool released;
df2cc96e 87 /* memory mappings are changing because of non-cooperative event */
a759a909 88 atomic_t mmap_changing;
86039bd3
AA
89 /* mm with one ore more vmas attached to this userfaultfd_ctx */
90 struct mm_struct *mm;
91};
92
893e26e6
PE
93struct userfaultfd_fork_ctx {
94 struct userfaultfd_ctx *orig;
95 struct userfaultfd_ctx *new;
96 struct list_head list;
97};
98
897ab3e0
MR
99struct userfaultfd_unmap_ctx {
100 struct userfaultfd_ctx *ctx;
101 unsigned long start;
102 unsigned long end;
103 struct list_head list;
104};
105
86039bd3 106struct userfaultfd_wait_queue {
a9b85f94 107 struct uffd_msg msg;
ac6424b9 108 wait_queue_entry_t wq;
86039bd3 109 struct userfaultfd_ctx *ctx;
15a77c6f 110 bool waken;
86039bd3
AA
111};
112
113struct userfaultfd_wake_range {
114 unsigned long start;
115 unsigned long len;
116};
117
22e5fe2a
NA
118/* internal indication that UFFD_API ioctl was successfully executed */
119#define UFFD_FEATURE_INITIALIZED (1u << 31)
120
121static bool userfaultfd_is_initialized(struct userfaultfd_ctx *ctx)
122{
123 return ctx->features & UFFD_FEATURE_INITIALIZED;
124}
125
d61ea1cb
PX
126static bool userfaultfd_wp_async_ctx(struct userfaultfd_ctx *ctx)
127{
128 return ctx && (ctx->features & UFFD_FEATURE_WP_ASYNC);
129}
130
2bad466c
PX
131/*
132 * Whether WP_UNPOPULATED is enabled on the uffd context. It is only
133 * meaningful when userfaultfd_wp()==true on the vma and when it's
134 * anonymous.
135 */
136bool userfaultfd_wp_unpopulated(struct vm_area_struct *vma)
137{
138 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
139
140 if (!ctx)
141 return false;
142
143 return ctx->features & UFFD_FEATURE_WP_UNPOPULATED;
144}
145
51d3d5eb
DH
146static void userfaultfd_set_vm_flags(struct vm_area_struct *vma,
147 vm_flags_t flags)
148{
149 const bool uffd_wp_changed = (vma->vm_flags ^ flags) & VM_UFFD_WP;
150
1c71222e 151 vm_flags_reset(vma, flags);
51d3d5eb
DH
152 /*
153 * For shared mappings, we want to enable writenotify while
154 * userfaultfd-wp is enabled (see vma_wants_writenotify()). We'll simply
155 * recalculate vma->vm_page_prot whenever userfaultfd-wp changes.
156 */
157 if ((vma->vm_flags & VM_SHARED) && uffd_wp_changed)
158 vma_set_page_prot(vma);
159}
160
ac6424b9 161static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
86039bd3
AA
162 int wake_flags, void *key)
163{
164 struct userfaultfd_wake_range *range = key;
165 int ret;
166 struct userfaultfd_wait_queue *uwq;
167 unsigned long start, len;
168
169 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
170 ret = 0;
86039bd3
AA
171 /* len == 0 means wake all */
172 start = range->start;
173 len = range->len;
a9b85f94
AA
174 if (len && (start > uwq->msg.arg.pagefault.address ||
175 start + len <= uwq->msg.arg.pagefault.address))
86039bd3 176 goto out;
15a77c6f
AA
177 WRITE_ONCE(uwq->waken, true);
178 /*
a9668cd6
PZ
179 * The Program-Order guarantees provided by the scheduler
180 * ensure uwq->waken is visible before the task is woken.
15a77c6f 181 */
86039bd3 182 ret = wake_up_state(wq->private, mode);
a9668cd6 183 if (ret) {
86039bd3
AA
184 /*
185 * Wake only once, autoremove behavior.
186 *
a9668cd6
PZ
187 * After the effect of list_del_init is visible to the other
188 * CPUs, the waitqueue may disappear from under us, see the
189 * !list_empty_careful() in handle_userfault().
190 *
191 * try_to_wake_up() has an implicit smp_mb(), and the
192 * wq->private is read before calling the extern function
193 * "wake_up_state" (which in turns calls try_to_wake_up).
86039bd3 194 */
2055da97 195 list_del_init(&wq->entry);
a9668cd6 196 }
86039bd3
AA
197out:
198 return ret;
199}
200
201/**
202 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
203 * context.
204 * @ctx: [in] Pointer to the userfaultfd context.
86039bd3
AA
205 */
206static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
207{
ca880420 208 refcount_inc(&ctx->refcount);
86039bd3
AA
209}
210
211/**
212 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
213 * context.
214 * @ctx: [in] Pointer to userfaultfd context.
215 *
216 * The userfaultfd context reference must have been previously acquired either
217 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
218 */
219static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
220{
ca880420 221 if (refcount_dec_and_test(&ctx->refcount)) {
86039bd3
AA
222 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
223 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
224 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
225 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
9cd75c3c
PE
226 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
227 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
86039bd3
AA
228 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
229 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
d2005e3f 230 mmdrop(ctx->mm);
3004ec9c 231 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
86039bd3
AA
232 }
233}
234
a9b85f94 235static inline void msg_init(struct uffd_msg *msg)
86039bd3 236{
a9b85f94
AA
237 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
238 /*
239 * Must use memset to zero out the paddings or kernel data is
240 * leaked to userland.
241 */
242 memset(msg, 0, sizeof(struct uffd_msg));
243}
244
245static inline struct uffd_msg userfault_msg(unsigned long address,
d172b1a3 246 unsigned long real_address,
a9b85f94 247 unsigned int flags,
9d4ac934
AP
248 unsigned long reason,
249 unsigned int features)
a9b85f94
AA
250{
251 struct uffd_msg msg;
d172b1a3 252
a9b85f94
AA
253 msg_init(&msg);
254 msg.event = UFFD_EVENT_PAGEFAULT;
824ddc60 255
d172b1a3
NA
256 msg.arg.pagefault.address = (features & UFFD_FEATURE_EXACT_ADDRESS) ?
257 real_address : address;
258
7677f7fd
AR
259 /*
260 * These flags indicate why the userfault occurred:
261 * - UFFD_PAGEFAULT_FLAG_WP indicates a write protect fault.
262 * - UFFD_PAGEFAULT_FLAG_MINOR indicates a minor fault.
263 * - Neither of these flags being set indicates a MISSING fault.
264 *
265 * Separately, UFFD_PAGEFAULT_FLAG_WRITE indicates it was a write
266 * fault. Otherwise, it was a read fault.
267 */
86039bd3 268 if (flags & FAULT_FLAG_WRITE)
a9b85f94 269 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
86039bd3 270 if (reason & VM_UFFD_WP)
a9b85f94 271 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
7677f7fd
AR
272 if (reason & VM_UFFD_MINOR)
273 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_MINOR;
9d4ac934 274 if (features & UFFD_FEATURE_THREAD_ID)
a36985d3 275 msg.arg.pagefault.feat.ptid = task_pid_vnr(current);
a9b85f94 276 return msg;
86039bd3
AA
277}
278
369cd212
MK
279#ifdef CONFIG_HUGETLB_PAGE
280/*
281 * Same functionality as userfaultfd_must_wait below with modifications for
282 * hugepmd ranges.
283 */
284static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
29a22b9e
SB
285 struct vm_fault *vmf,
286 unsigned long reason)
369cd212 287{
29a22b9e 288 struct vm_area_struct *vma = vmf->vma;
1e2c0436 289 pte_t *ptep, pte;
369cd212
MK
290 bool ret = true;
291
29a22b9e 292 assert_fault_locked(vmf);
1e2c0436 293
29a22b9e 294 ptep = hugetlb_walk(vma, vmf->address, vma_mmu_pagesize(vma));
1e2c0436 295 if (!ptep)
369cd212
MK
296 goto out;
297
298 ret = false;
1e2c0436 299 pte = huge_ptep_get(ptep);
369cd212
MK
300
301 /*
302 * Lockless access: we're in a wait_event so it's ok if it
5c041f5d
PX
303 * changes under us. PTE markers should be handled the same as none
304 * ptes here.
369cd212 305 */
5c041f5d 306 if (huge_pte_none_mostly(pte))
369cd212 307 ret = true;
1e2c0436 308 if (!huge_pte_write(pte) && (reason & VM_UFFD_WP))
369cd212
MK
309 ret = true;
310out:
311 return ret;
312}
313#else
314static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
29a22b9e
SB
315 struct vm_fault *vmf,
316 unsigned long reason)
369cd212
MK
317{
318 return false; /* should never get here */
319}
320#endif /* CONFIG_HUGETLB_PAGE */
321
8d2afd96
AA
322/*
323 * Verify the pagetables are still not ok after having reigstered into
324 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
325 * userfault that has already been resolved, if userfaultfd_read and
326 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
327 * threads.
328 */
329static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
29a22b9e 330 struct vm_fault *vmf,
8d2afd96
AA
331 unsigned long reason)
332{
333 struct mm_struct *mm = ctx->mm;
29a22b9e 334 unsigned long address = vmf->address;
8d2afd96 335 pgd_t *pgd;
c2febafc 336 p4d_t *p4d;
8d2afd96
AA
337 pud_t *pud;
338 pmd_t *pmd, _pmd;
339 pte_t *pte;
c33c7948 340 pte_t ptent;
8d2afd96
AA
341 bool ret = true;
342
29a22b9e 343 assert_fault_locked(vmf);
8d2afd96
AA
344
345 pgd = pgd_offset(mm, address);
346 if (!pgd_present(*pgd))
347 goto out;
c2febafc
KS
348 p4d = p4d_offset(pgd, address);
349 if (!p4d_present(*p4d))
350 goto out;
351 pud = pud_offset(p4d, address);
8d2afd96
AA
352 if (!pud_present(*pud))
353 goto out;
354 pmd = pmd_offset(pud, address);
2b683a4f 355again:
26e1a0c3 356 _pmd = pmdp_get_lockless(pmd);
a365ac09 357 if (pmd_none(_pmd))
8d2afd96
AA
358 goto out;
359
360 ret = false;
2b683a4f 361 if (!pmd_present(_pmd) || pmd_devmap(_pmd))
a365ac09
HY
362 goto out;
363
63b2d417
AA
364 if (pmd_trans_huge(_pmd)) {
365 if (!pmd_write(_pmd) && (reason & VM_UFFD_WP))
366 ret = true;
8d2afd96 367 goto out;
63b2d417 368 }
8d2afd96 369
8d2afd96 370 pte = pte_offset_map(pmd, address);
2b683a4f
HD
371 if (!pte) {
372 ret = true;
373 goto again;
374 }
8d2afd96
AA
375 /*
376 * Lockless access: we're in a wait_event so it's ok if it
5c041f5d
PX
377 * changes under us. PTE markers should be handled the same as none
378 * ptes here.
8d2afd96 379 */
c33c7948
RR
380 ptent = ptep_get(pte);
381 if (pte_none_mostly(ptent))
8d2afd96 382 ret = true;
c33c7948 383 if (!pte_write(ptent) && (reason & VM_UFFD_WP))
63b2d417 384 ret = true;
8d2afd96
AA
385 pte_unmap(pte);
386
387out:
388 return ret;
389}
390
2f064a59 391static inline unsigned int userfaultfd_get_blocking_state(unsigned int flags)
3e69ad08
PX
392{
393 if (flags & FAULT_FLAG_INTERRUPTIBLE)
394 return TASK_INTERRUPTIBLE;
395
396 if (flags & FAULT_FLAG_KILLABLE)
397 return TASK_KILLABLE;
398
399 return TASK_UNINTERRUPTIBLE;
400}
401
86039bd3
AA
402/*
403 * The locking rules involved in returning VM_FAULT_RETRY depending on
404 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
405 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
406 * recommendation in __lock_page_or_retry is not an understatement.
407 *
c1e8d7c6 408 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_lock must be released
86039bd3
AA
409 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
410 * not set.
411 *
412 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
413 * set, VM_FAULT_RETRY can still be returned if and only if there are
c1e8d7c6 414 * fatal_signal_pending()s, and the mmap_lock must be released before
86039bd3
AA
415 * returning it.
416 */
2b740303 417vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)
86039bd3 418{
b8da2e46
PX
419 struct vm_area_struct *vma = vmf->vma;
420 struct mm_struct *mm = vma->vm_mm;
86039bd3
AA
421 struct userfaultfd_ctx *ctx;
422 struct userfaultfd_wait_queue uwq;
2b740303 423 vm_fault_t ret = VM_FAULT_SIGBUS;
3e69ad08 424 bool must_wait;
2f064a59 425 unsigned int blocking_state;
86039bd3 426
64c2b203
AA
427 /*
428 * We don't do userfault handling for the final child pid update.
429 *
430 * We also don't do userfault handling during
431 * coredumping. hugetlbfs has the special
48498071 432 * hugetlb_follow_page_mask() to skip missing pages in the
64c2b203
AA
433 * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
434 * the no_page_table() helper in follow_page_mask(), but the
435 * shmem_vm_ops->fault method is invoked even during
004a9a38 436 * coredumping and it ends up here.
64c2b203
AA
437 */
438 if (current->flags & (PF_EXITING|PF_DUMPCORE))
439 goto out;
440
29a22b9e 441 assert_fault_locked(vmf);
64c2b203 442
b8da2e46 443 ctx = vma->vm_userfaultfd_ctx.ctx;
86039bd3 444 if (!ctx)
ba85c702 445 goto out;
86039bd3
AA
446
447 BUG_ON(ctx->mm != mm);
448
7677f7fd
AR
449 /* Any unrecognized flag is a bug. */
450 VM_BUG_ON(reason & ~__VM_UFFD_FLAGS);
451 /* 0 or > 1 flags set is a bug; we expect exactly 1. */
452 VM_BUG_ON(!reason || (reason & (reason - 1)));
86039bd3 453
2d6d6f5a
PS
454 if (ctx->features & UFFD_FEATURE_SIGBUS)
455 goto out;
2d5de004 456 if (!(vmf->flags & FAULT_FLAG_USER) && (ctx->flags & UFFD_USER_MODE_ONLY))
37cd0575 457 goto out;
2d6d6f5a 458
86039bd3
AA
459 /*
460 * If it's already released don't get it. This avoids to loop
461 * in __get_user_pages if userfaultfd_release waits on the
c1e8d7c6 462 * caller of handle_userfault to release the mmap_lock.
86039bd3 463 */
6aa7de05 464 if (unlikely(READ_ONCE(ctx->released))) {
656710a6
AA
465 /*
466 * Don't return VM_FAULT_SIGBUS in this case, so a non
467 * cooperative manager can close the uffd after the
468 * last UFFDIO_COPY, without risking to trigger an
469 * involuntary SIGBUS if the process was starting the
470 * userfaultfd while the userfaultfd was still armed
471 * (but after the last UFFDIO_COPY). If the uffd
472 * wasn't already closed when the userfault reached
473 * this point, that would normally be solved by
474 * userfaultfd_must_wait returning 'false'.
475 *
476 * If we were to return VM_FAULT_SIGBUS here, the non
477 * cooperative manager would be instead forced to
478 * always call UFFDIO_UNREGISTER before it can safely
479 * close the uffd.
480 */
481 ret = VM_FAULT_NOPAGE;
ba85c702 482 goto out;
656710a6 483 }
86039bd3
AA
484
485 /*
486 * Check that we can return VM_FAULT_RETRY.
487 *
488 * NOTE: it should become possible to return VM_FAULT_RETRY
489 * even if FAULT_FLAG_TRIED is set without leading to gup()
490 * -EBUSY failures, if the userfaultfd is to be extended for
491 * VM_UFFD_WP tracking and we intend to arm the userfault
492 * without first stopping userland access to the memory. For
493 * VM_UFFD_MISSING userfaults this is enough for now.
494 */
82b0f8c3 495 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
86039bd3
AA
496 /*
497 * Validate the invariant that nowait must allow retry
498 * to be sure not to return SIGBUS erroneously on
499 * nowait invocations.
500 */
82b0f8c3 501 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
86039bd3
AA
502#ifdef CONFIG_DEBUG_VM
503 if (printk_ratelimit()) {
504 printk(KERN_WARNING
82b0f8c3
JK
505 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
506 vmf->flags);
86039bd3
AA
507 dump_stack();
508 }
509#endif
ba85c702 510 goto out;
86039bd3
AA
511 }
512
513 /*
514 * Handle nowait, not much to do other than tell it to retry
515 * and wait.
516 */
ba85c702 517 ret = VM_FAULT_RETRY;
82b0f8c3 518 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
ba85c702 519 goto out;
86039bd3 520
c1e8d7c6 521 /* take the reference before dropping the mmap_lock */
86039bd3
AA
522 userfaultfd_ctx_get(ctx);
523
86039bd3
AA
524 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
525 uwq.wq.private = current;
d172b1a3
NA
526 uwq.msg = userfault_msg(vmf->address, vmf->real_address, vmf->flags,
527 reason, ctx->features);
86039bd3 528 uwq.ctx = ctx;
15a77c6f 529 uwq.waken = false;
86039bd3 530
3e69ad08 531 blocking_state = userfaultfd_get_blocking_state(vmf->flags);
dfa37dc3 532
b8da2e46
PX
533 /*
534 * Take the vma lock now, in order to safely call
535 * userfaultfd_huge_must_wait() later. Since acquiring the
536 * (sleepable) vma lock can modify the current task state, that
537 * must be before explicitly calling set_current_state().
538 */
539 if (is_vm_hugetlb_page(vma))
540 hugetlb_vma_lock_read(vma);
541
cbcfa130 542 spin_lock_irq(&ctx->fault_pending_wqh.lock);
86039bd3
AA
543 /*
544 * After the __add_wait_queue the uwq is visible to userland
545 * through poll/read().
546 */
15b726ef
AA
547 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
548 /*
549 * The smp_mb() after __set_current_state prevents the reads
550 * following the spin_unlock to happen before the list_add in
551 * __add_wait_queue.
552 */
15a77c6f 553 set_current_state(blocking_state);
cbcfa130 554 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
86039bd3 555
b8da2e46 556 if (!is_vm_hugetlb_page(vma))
29a22b9e 557 must_wait = userfaultfd_must_wait(ctx, vmf, reason);
369cd212 558 else
29a22b9e 559 must_wait = userfaultfd_huge_must_wait(ctx, vmf, reason);
b8da2e46
PX
560 if (is_vm_hugetlb_page(vma))
561 hugetlb_vma_unlock_read(vma);
29a22b9e 562 release_fault_lock(vmf);
8d2afd96 563
f9bf3522 564 if (likely(must_wait && !READ_ONCE(ctx->released))) {
a9a08845 565 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
86039bd3 566 schedule();
ba85c702 567 }
86039bd3 568
ba85c702 569 __set_current_state(TASK_RUNNING);
15b726ef
AA
570
571 /*
572 * Here we race with the list_del; list_add in
573 * userfaultfd_ctx_read(), however because we don't ever run
574 * list_del_init() to refile across the two lists, the prev
575 * and next pointers will never point to self. list_add also
576 * would never let any of the two pointers to point to
577 * self. So list_empty_careful won't risk to see both pointers
578 * pointing to self at any time during the list refile. The
579 * only case where list_del_init() is called is the full
580 * removal in the wake function and there we don't re-list_add
581 * and it's fine not to block on the spinlock. The uwq on this
582 * kernel stack can be released after the list_del_init.
583 */
2055da97 584 if (!list_empty_careful(&uwq.wq.entry)) {
cbcfa130 585 spin_lock_irq(&ctx->fault_pending_wqh.lock);
15b726ef
AA
586 /*
587 * No need of list_del_init(), the uwq on the stack
588 * will be freed shortly anyway.
589 */
2055da97 590 list_del(&uwq.wq.entry);
cbcfa130 591 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
86039bd3 592 }
86039bd3
AA
593
594 /*
595 * ctx may go away after this if the userfault pseudo fd is
596 * already released.
597 */
598 userfaultfd_ctx_put(ctx);
599
ba85c702
AA
600out:
601 return ret;
86039bd3
AA
602}
603
8c9e7bb7
AA
604static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
605 struct userfaultfd_wait_queue *ewq)
9cd75c3c 606{
0cbb4b4f
AA
607 struct userfaultfd_ctx *release_new_ctx;
608
9a69a829
AA
609 if (WARN_ON_ONCE(current->flags & PF_EXITING))
610 goto out;
9cd75c3c
PE
611
612 ewq->ctx = ctx;
613 init_waitqueue_entry(&ewq->wq, current);
0cbb4b4f 614 release_new_ctx = NULL;
9cd75c3c 615
cbcfa130 616 spin_lock_irq(&ctx->event_wqh.lock);
9cd75c3c
PE
617 /*
618 * After the __add_wait_queue the uwq is visible to userland
619 * through poll/read().
620 */
621 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
622 for (;;) {
623 set_current_state(TASK_KILLABLE);
624 if (ewq->msg.event == 0)
625 break;
6aa7de05 626 if (READ_ONCE(ctx->released) ||
9cd75c3c 627 fatal_signal_pending(current)) {
384632e6
AA
628 /*
629 * &ewq->wq may be queued in fork_event, but
630 * __remove_wait_queue ignores the head
631 * parameter. It would be a problem if it
632 * didn't.
633 */
9cd75c3c 634 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
7eb76d45
MR
635 if (ewq->msg.event == UFFD_EVENT_FORK) {
636 struct userfaultfd_ctx *new;
637
638 new = (struct userfaultfd_ctx *)
639 (unsigned long)
640 ewq->msg.arg.reserved.reserved1;
0cbb4b4f 641 release_new_ctx = new;
7eb76d45 642 }
9cd75c3c
PE
643 break;
644 }
645
cbcfa130 646 spin_unlock_irq(&ctx->event_wqh.lock);
9cd75c3c 647
a9a08845 648 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
9cd75c3c
PE
649 schedule();
650
cbcfa130 651 spin_lock_irq(&ctx->event_wqh.lock);
9cd75c3c
PE
652 }
653 __set_current_state(TASK_RUNNING);
cbcfa130 654 spin_unlock_irq(&ctx->event_wqh.lock);
9cd75c3c 655
0cbb4b4f
AA
656 if (release_new_ctx) {
657 struct vm_area_struct *vma;
658 struct mm_struct *mm = release_new_ctx->mm;
69dbe6da 659 VMA_ITERATOR(vmi, mm, 0);
0cbb4b4f
AA
660
661 /* the various vma->vm_userfaultfd_ctx still points to it */
d8ed45c5 662 mmap_write_lock(mm);
69dbe6da 663 for_each_vma(vmi, vma) {
31e810aa 664 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
60081bf1 665 vma_start_write(vma);
0cbb4b4f 666 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
51d3d5eb
DH
667 userfaultfd_set_vm_flags(vma,
668 vma->vm_flags & ~__VM_UFFD_FLAGS);
31e810aa 669 }
69dbe6da 670 }
d8ed45c5 671 mmap_write_unlock(mm);
0cbb4b4f
AA
672
673 userfaultfd_ctx_put(release_new_ctx);
674 }
675
9cd75c3c
PE
676 /*
677 * ctx may go away after this if the userfault pseudo fd is
678 * already released.
679 */
9a69a829 680out:
a759a909
NA
681 atomic_dec(&ctx->mmap_changing);
682 VM_BUG_ON(atomic_read(&ctx->mmap_changing) < 0);
9cd75c3c 683 userfaultfd_ctx_put(ctx);
9cd75c3c
PE
684}
685
686static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
687 struct userfaultfd_wait_queue *ewq)
688{
689 ewq->msg.event = 0;
690 wake_up_locked(&ctx->event_wqh);
691 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
692}
693
893e26e6
PE
694int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
695{
696 struct userfaultfd_ctx *ctx = NULL, *octx;
697 struct userfaultfd_fork_ctx *fctx;
698
699 octx = vma->vm_userfaultfd_ctx.ctx;
700 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
60081bf1 701 vma_start_write(vma);
893e26e6 702 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
51d3d5eb 703 userfaultfd_set_vm_flags(vma, vma->vm_flags & ~__VM_UFFD_FLAGS);
893e26e6
PE
704 return 0;
705 }
706
707 list_for_each_entry(fctx, fcs, list)
708 if (fctx->orig == octx) {
709 ctx = fctx->new;
710 break;
711 }
712
713 if (!ctx) {
714 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
715 if (!fctx)
716 return -ENOMEM;
717
718 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
719 if (!ctx) {
720 kfree(fctx);
721 return -ENOMEM;
722 }
723
ca880420 724 refcount_set(&ctx->refcount, 1);
893e26e6 725 ctx->flags = octx->flags;
893e26e6
PE
726 ctx->features = octx->features;
727 ctx->released = false;
a759a909 728 atomic_set(&ctx->mmap_changing, 0);
893e26e6 729 ctx->mm = vma->vm_mm;
00bb31fa 730 mmgrab(ctx->mm);
893e26e6
PE
731
732 userfaultfd_ctx_get(octx);
a759a909 733 atomic_inc(&octx->mmap_changing);
893e26e6
PE
734 fctx->orig = octx;
735 fctx->new = ctx;
736 list_add_tail(&fctx->list, fcs);
737 }
738
739 vma->vm_userfaultfd_ctx.ctx = ctx;
740 return 0;
741}
742
8c9e7bb7 743static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
893e26e6
PE
744{
745 struct userfaultfd_ctx *ctx = fctx->orig;
746 struct userfaultfd_wait_queue ewq;
747
748 msg_init(&ewq.msg);
749
750 ewq.msg.event = UFFD_EVENT_FORK;
751 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
752
8c9e7bb7 753 userfaultfd_event_wait_completion(ctx, &ewq);
893e26e6
PE
754}
755
756void dup_userfaultfd_complete(struct list_head *fcs)
757{
893e26e6
PE
758 struct userfaultfd_fork_ctx *fctx, *n;
759
760 list_for_each_entry_safe(fctx, n, fcs, list) {
8c9e7bb7 761 dup_fctx(fctx);
893e26e6
PE
762 list_del(&fctx->list);
763 kfree(fctx);
764 }
765}
766
72f87654
PE
767void mremap_userfaultfd_prep(struct vm_area_struct *vma,
768 struct vm_userfaultfd_ctx *vm_ctx)
769{
770 struct userfaultfd_ctx *ctx;
771
772 ctx = vma->vm_userfaultfd_ctx.ctx;
3cfd22be
PX
773
774 if (!ctx)
775 return;
776
777 if (ctx->features & UFFD_FEATURE_EVENT_REMAP) {
72f87654
PE
778 vm_ctx->ctx = ctx;
779 userfaultfd_ctx_get(ctx);
a759a909 780 atomic_inc(&ctx->mmap_changing);
3cfd22be
PX
781 } else {
782 /* Drop uffd context if remap feature not enabled */
60081bf1 783 vma_start_write(vma);
3cfd22be 784 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
51d3d5eb 785 userfaultfd_set_vm_flags(vma, vma->vm_flags & ~__VM_UFFD_FLAGS);
72f87654
PE
786 }
787}
788
90794bf1 789void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
72f87654
PE
790 unsigned long from, unsigned long to,
791 unsigned long len)
792{
90794bf1 793 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
72f87654
PE
794 struct userfaultfd_wait_queue ewq;
795
796 if (!ctx)
797 return;
798
799 if (to & ~PAGE_MASK) {
800 userfaultfd_ctx_put(ctx);
801 return;
802 }
803
804 msg_init(&ewq.msg);
805
806 ewq.msg.event = UFFD_EVENT_REMAP;
807 ewq.msg.arg.remap.from = from;
808 ewq.msg.arg.remap.to = to;
809 ewq.msg.arg.remap.len = len;
810
811 userfaultfd_event_wait_completion(ctx, &ewq);
812}
813
70ccb92f 814bool userfaultfd_remove(struct vm_area_struct *vma,
d811914d 815 unsigned long start, unsigned long end)
05ce7724
PE
816{
817 struct mm_struct *mm = vma->vm_mm;
818 struct userfaultfd_ctx *ctx;
819 struct userfaultfd_wait_queue ewq;
820
821 ctx = vma->vm_userfaultfd_ctx.ctx;
d811914d 822 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
70ccb92f 823 return true;
05ce7724
PE
824
825 userfaultfd_ctx_get(ctx);
a759a909 826 atomic_inc(&ctx->mmap_changing);
d8ed45c5 827 mmap_read_unlock(mm);
05ce7724 828
05ce7724
PE
829 msg_init(&ewq.msg);
830
d811914d
MR
831 ewq.msg.event = UFFD_EVENT_REMOVE;
832 ewq.msg.arg.remove.start = start;
833 ewq.msg.arg.remove.end = end;
05ce7724
PE
834
835 userfaultfd_event_wait_completion(ctx, &ewq);
836
70ccb92f 837 return false;
05ce7724
PE
838}
839
897ab3e0
MR
840static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
841 unsigned long start, unsigned long end)
842{
843 struct userfaultfd_unmap_ctx *unmap_ctx;
844
845 list_for_each_entry(unmap_ctx, unmaps, list)
846 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
847 unmap_ctx->end == end)
848 return true;
849
850 return false;
851}
852
65ac1320 853int userfaultfd_unmap_prep(struct vm_area_struct *vma, unsigned long start,
69dbe6da 854 unsigned long end, struct list_head *unmaps)
897ab3e0 855{
65ac1320
LH
856 struct userfaultfd_unmap_ctx *unmap_ctx;
857 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
897ab3e0 858
65ac1320
LH
859 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
860 has_unmap_ctx(ctx, unmaps, start, end))
861 return 0;
897ab3e0 862
65ac1320
LH
863 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
864 if (!unmap_ctx)
865 return -ENOMEM;
897ab3e0 866
65ac1320
LH
867 userfaultfd_ctx_get(ctx);
868 atomic_inc(&ctx->mmap_changing);
869 unmap_ctx->ctx = ctx;
870 unmap_ctx->start = start;
871 unmap_ctx->end = end;
872 list_add_tail(&unmap_ctx->list, unmaps);
897ab3e0
MR
873
874 return 0;
875}
876
877void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
878{
879 struct userfaultfd_unmap_ctx *ctx, *n;
880 struct userfaultfd_wait_queue ewq;
881
882 list_for_each_entry_safe(ctx, n, uf, list) {
883 msg_init(&ewq.msg);
884
885 ewq.msg.event = UFFD_EVENT_UNMAP;
886 ewq.msg.arg.remove.start = ctx->start;
887 ewq.msg.arg.remove.end = ctx->end;
888
889 userfaultfd_event_wait_completion(ctx->ctx, &ewq);
890
891 list_del(&ctx->list);
892 kfree(ctx);
893 }
894}
895
86039bd3
AA
896static int userfaultfd_release(struct inode *inode, struct file *file)
897{
898 struct userfaultfd_ctx *ctx = file->private_data;
899 struct mm_struct *mm = ctx->mm;
900 struct vm_area_struct *vma, *prev;
901 /* len == 0 means wake all */
902 struct userfaultfd_wake_range range = { .len = 0, };
903 unsigned long new_flags;
11a9b902 904 VMA_ITERATOR(vmi, mm, 0);
86039bd3 905
6aa7de05 906 WRITE_ONCE(ctx->released, true);
86039bd3 907
d2005e3f
ON
908 if (!mmget_not_zero(mm))
909 goto wakeup;
910
86039bd3
AA
911 /*
912 * Flush page faults out of all CPUs. NOTE: all page faults
913 * must be retried without returning VM_FAULT_SIGBUS if
914 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
c1e8d7c6 915 * changes while handle_userfault released the mmap_lock. So
86039bd3 916 * it's critical that released is set to true (above), before
c1e8d7c6 917 * taking the mmap_lock for writing.
86039bd3 918 */
d8ed45c5 919 mmap_write_lock(mm);
86039bd3 920 prev = NULL;
11a9b902 921 for_each_vma(vmi, vma) {
86039bd3
AA
922 cond_resched();
923 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
7677f7fd 924 !!(vma->vm_flags & __VM_UFFD_FLAGS));
86039bd3
AA
925 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
926 prev = vma;
927 continue;
928 }
7677f7fd 929 new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS;
9760ebff 930 prev = vma_merge(&vmi, mm, prev, vma->vm_start, vma->vm_end,
4d45e75a
JH
931 new_flags, vma->anon_vma,
932 vma->vm_file, vma->vm_pgoff,
933 vma_policy(vma),
5c26f6ac 934 NULL_VM_UFFD_CTX, anon_vma_name(vma));
69dbe6da 935 if (prev) {
4d45e75a 936 vma = prev;
69dbe6da 937 } else {
4d45e75a 938 prev = vma;
69dbe6da
LH
939 }
940
60081bf1 941 vma_start_write(vma);
51d3d5eb 942 userfaultfd_set_vm_flags(vma, new_flags);
86039bd3
AA
943 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
944 }
d8ed45c5 945 mmap_write_unlock(mm);
d2005e3f
ON
946 mmput(mm);
947wakeup:
86039bd3 948 /*
15b726ef 949 * After no new page faults can wait on this fault_*wqh, flush
86039bd3 950 * the last page faults that may have been already waiting on
15b726ef 951 * the fault_*wqh.
86039bd3 952 */
cbcfa130 953 spin_lock_irq(&ctx->fault_pending_wqh.lock);
ac5be6b4 954 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
c430d1e8 955 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range);
cbcfa130 956 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
86039bd3 957
5a18b64e
MR
958 /* Flush pending events that may still wait on event_wqh */
959 wake_up_all(&ctx->event_wqh);
960
a9a08845 961 wake_up_poll(&ctx->fd_wqh, EPOLLHUP);
86039bd3
AA
962 userfaultfd_ctx_put(ctx);
963 return 0;
964}
965
15b726ef 966/* fault_pending_wqh.lock must be hold by the caller */
6dcc27fd
PE
967static inline struct userfaultfd_wait_queue *find_userfault_in(
968 wait_queue_head_t *wqh)
86039bd3 969{
ac6424b9 970 wait_queue_entry_t *wq;
15b726ef 971 struct userfaultfd_wait_queue *uwq;
86039bd3 972
456a7378 973 lockdep_assert_held(&wqh->lock);
86039bd3 974
15b726ef 975 uwq = NULL;
6dcc27fd 976 if (!waitqueue_active(wqh))
15b726ef
AA
977 goto out;
978 /* walk in reverse to provide FIFO behavior to read userfaults */
2055da97 979 wq = list_last_entry(&wqh->head, typeof(*wq), entry);
15b726ef
AA
980 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
981out:
982 return uwq;
86039bd3 983}
6dcc27fd
PE
984
985static inline struct userfaultfd_wait_queue *find_userfault(
986 struct userfaultfd_ctx *ctx)
987{
988 return find_userfault_in(&ctx->fault_pending_wqh);
989}
86039bd3 990
9cd75c3c
PE
991static inline struct userfaultfd_wait_queue *find_userfault_evt(
992 struct userfaultfd_ctx *ctx)
993{
994 return find_userfault_in(&ctx->event_wqh);
995}
996
076ccb76 997static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
86039bd3
AA
998{
999 struct userfaultfd_ctx *ctx = file->private_data;
076ccb76 1000 __poll_t ret;
86039bd3
AA
1001
1002 poll_wait(file, &ctx->fd_wqh, wait);
1003
22e5fe2a 1004 if (!userfaultfd_is_initialized(ctx))
a9a08845 1005 return EPOLLERR;
9cd75c3c 1006
22e5fe2a
NA
1007 /*
1008 * poll() never guarantees that read won't block.
1009 * userfaults can be waken before they're read().
1010 */
1011 if (unlikely(!(file->f_flags & O_NONBLOCK)))
a9a08845 1012 return EPOLLERR;
22e5fe2a
NA
1013 /*
1014 * lockless access to see if there are pending faults
1015 * __pollwait last action is the add_wait_queue but
1016 * the spin_unlock would allow the waitqueue_active to
1017 * pass above the actual list_add inside
1018 * add_wait_queue critical section. So use a full
1019 * memory barrier to serialize the list_add write of
1020 * add_wait_queue() with the waitqueue_active read
1021 * below.
1022 */
1023 ret = 0;
1024 smp_mb();
1025 if (waitqueue_active(&ctx->fault_pending_wqh))
1026 ret = EPOLLIN;
1027 else if (waitqueue_active(&ctx->event_wqh))
1028 ret = EPOLLIN;
1029
1030 return ret;
86039bd3
AA
1031}
1032
893e26e6
PE
1033static const struct file_operations userfaultfd_fops;
1034
b537900f
DC
1035static int resolve_userfault_fork(struct userfaultfd_ctx *new,
1036 struct inode *inode,
893e26e6
PE
1037 struct uffd_msg *msg)
1038{
1039 int fd;
893e26e6 1040
b537900f 1041 fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, new,
abec3d01 1042 O_RDONLY | (new->flags & UFFD_SHARED_FCNTL_FLAGS), inode);
893e26e6
PE
1043 if (fd < 0)
1044 return fd;
1045
893e26e6
PE
1046 msg->arg.reserved.reserved1 = 0;
1047 msg->arg.fork.ufd = fd;
893e26e6
PE
1048 return 0;
1049}
1050
86039bd3 1051static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
b537900f 1052 struct uffd_msg *msg, struct inode *inode)
86039bd3
AA
1053{
1054 ssize_t ret;
1055 DECLARE_WAITQUEUE(wait, current);
15b726ef 1056 struct userfaultfd_wait_queue *uwq;
893e26e6
PE
1057 /*
1058 * Handling fork event requires sleeping operations, so
1059 * we drop the event_wqh lock, then do these ops, then
1060 * lock it back and wake up the waiter. While the lock is
1061 * dropped the ewq may go away so we keep track of it
1062 * carefully.
1063 */
1064 LIST_HEAD(fork_event);
1065 struct userfaultfd_ctx *fork_nctx = NULL;
86039bd3 1066
15b726ef 1067 /* always take the fd_wqh lock before the fault_pending_wqh lock */
ae62c16e 1068 spin_lock_irq(&ctx->fd_wqh.lock);
86039bd3
AA
1069 __add_wait_queue(&ctx->fd_wqh, &wait);
1070 for (;;) {
1071 set_current_state(TASK_INTERRUPTIBLE);
15b726ef
AA
1072 spin_lock(&ctx->fault_pending_wqh.lock);
1073 uwq = find_userfault(ctx);
1074 if (uwq) {
2c5b7e1b
AA
1075 /*
1076 * Use a seqcount to repeat the lockless check
1077 * in wake_userfault() to avoid missing
1078 * wakeups because during the refile both
1079 * waitqueue could become empty if this is the
1080 * only userfault.
1081 */
1082 write_seqcount_begin(&ctx->refile_seq);
1083
86039bd3 1084 /*
15b726ef
AA
1085 * The fault_pending_wqh.lock prevents the uwq
1086 * to disappear from under us.
1087 *
1088 * Refile this userfault from
1089 * fault_pending_wqh to fault_wqh, it's not
1090 * pending anymore after we read it.
1091 *
1092 * Use list_del() by hand (as
1093 * userfaultfd_wake_function also uses
1094 * list_del_init() by hand) to be sure nobody
1095 * changes __remove_wait_queue() to use
1096 * list_del_init() in turn breaking the
1097 * !list_empty_careful() check in
2055da97 1098 * handle_userfault(). The uwq->wq.head list
15b726ef
AA
1099 * must never be empty at any time during the
1100 * refile, or the waitqueue could disappear
1101 * from under us. The "wait_queue_head_t"
1102 * parameter of __remove_wait_queue() is unused
1103 * anyway.
86039bd3 1104 */
2055da97 1105 list_del(&uwq->wq.entry);
c430d1e8 1106 add_wait_queue(&ctx->fault_wqh, &uwq->wq);
15b726ef 1107
2c5b7e1b
AA
1108 write_seqcount_end(&ctx->refile_seq);
1109
a9b85f94
AA
1110 /* careful to always initialize msg if ret == 0 */
1111 *msg = uwq->msg;
15b726ef 1112 spin_unlock(&ctx->fault_pending_wqh.lock);
86039bd3
AA
1113 ret = 0;
1114 break;
1115 }
15b726ef 1116 spin_unlock(&ctx->fault_pending_wqh.lock);
9cd75c3c
PE
1117
1118 spin_lock(&ctx->event_wqh.lock);
1119 uwq = find_userfault_evt(ctx);
1120 if (uwq) {
1121 *msg = uwq->msg;
1122
893e26e6
PE
1123 if (uwq->msg.event == UFFD_EVENT_FORK) {
1124 fork_nctx = (struct userfaultfd_ctx *)
1125 (unsigned long)
1126 uwq->msg.arg.reserved.reserved1;
2055da97 1127 list_move(&uwq->wq.entry, &fork_event);
384632e6
AA
1128 /*
1129 * fork_nctx can be freed as soon as
1130 * we drop the lock, unless we take a
1131 * reference on it.
1132 */
1133 userfaultfd_ctx_get(fork_nctx);
893e26e6
PE
1134 spin_unlock(&ctx->event_wqh.lock);
1135 ret = 0;
1136 break;
1137 }
1138
9cd75c3c
PE
1139 userfaultfd_event_complete(ctx, uwq);
1140 spin_unlock(&ctx->event_wqh.lock);
1141 ret = 0;
1142 break;
1143 }
1144 spin_unlock(&ctx->event_wqh.lock);
1145
86039bd3
AA
1146 if (signal_pending(current)) {
1147 ret = -ERESTARTSYS;
1148 break;
1149 }
1150 if (no_wait) {
1151 ret = -EAGAIN;
1152 break;
1153 }
ae62c16e 1154 spin_unlock_irq(&ctx->fd_wqh.lock);
86039bd3 1155 schedule();
ae62c16e 1156 spin_lock_irq(&ctx->fd_wqh.lock);
86039bd3
AA
1157 }
1158 __remove_wait_queue(&ctx->fd_wqh, &wait);
1159 __set_current_state(TASK_RUNNING);
ae62c16e 1160 spin_unlock_irq(&ctx->fd_wqh.lock);
86039bd3 1161
893e26e6 1162 if (!ret && msg->event == UFFD_EVENT_FORK) {
b537900f 1163 ret = resolve_userfault_fork(fork_nctx, inode, msg);
cbcfa130 1164 spin_lock_irq(&ctx->event_wqh.lock);
384632e6
AA
1165 if (!list_empty(&fork_event)) {
1166 /*
1167 * The fork thread didn't abort, so we can
1168 * drop the temporary refcount.
1169 */
1170 userfaultfd_ctx_put(fork_nctx);
1171
1172 uwq = list_first_entry(&fork_event,
1173 typeof(*uwq),
1174 wq.entry);
1175 /*
1176 * If fork_event list wasn't empty and in turn
1177 * the event wasn't already released by fork
1178 * (the event is allocated on fork kernel
1179 * stack), put the event back to its place in
1180 * the event_wq. fork_event head will be freed
1181 * as soon as we return so the event cannot
1182 * stay queued there no matter the current
1183 * "ret" value.
1184 */
1185 list_del(&uwq->wq.entry);
1186 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
893e26e6 1187
384632e6
AA
1188 /*
1189 * Leave the event in the waitqueue and report
1190 * error to userland if we failed to resolve
1191 * the userfault fork.
1192 */
1193 if (likely(!ret))
893e26e6 1194 userfaultfd_event_complete(ctx, uwq);
384632e6
AA
1195 } else {
1196 /*
1197 * Here the fork thread aborted and the
1198 * refcount from the fork thread on fork_nctx
1199 * has already been released. We still hold
1200 * the reference we took before releasing the
1201 * lock above. If resolve_userfault_fork
1202 * failed we've to drop it because the
1203 * fork_nctx has to be freed in such case. If
1204 * it succeeded we'll hold it because the new
1205 * uffd references it.
1206 */
1207 if (ret)
1208 userfaultfd_ctx_put(fork_nctx);
893e26e6 1209 }
cbcfa130 1210 spin_unlock_irq(&ctx->event_wqh.lock);
893e26e6
PE
1211 }
1212
86039bd3
AA
1213 return ret;
1214}
1215
1216static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1217 size_t count, loff_t *ppos)
1218{
1219 struct userfaultfd_ctx *ctx = file->private_data;
1220 ssize_t _ret, ret = 0;
a9b85f94 1221 struct uffd_msg msg;
86039bd3 1222 int no_wait = file->f_flags & O_NONBLOCK;
b537900f 1223 struct inode *inode = file_inode(file);
86039bd3 1224
22e5fe2a 1225 if (!userfaultfd_is_initialized(ctx))
86039bd3 1226 return -EINVAL;
86039bd3
AA
1227
1228 for (;;) {
a9b85f94 1229 if (count < sizeof(msg))
86039bd3 1230 return ret ? ret : -EINVAL;
b537900f 1231 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg, inode);
86039bd3
AA
1232 if (_ret < 0)
1233 return ret ? ret : _ret;
a9b85f94 1234 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
86039bd3 1235 return ret ? ret : -EFAULT;
a9b85f94
AA
1236 ret += sizeof(msg);
1237 buf += sizeof(msg);
1238 count -= sizeof(msg);
86039bd3
AA
1239 /*
1240 * Allow to read more than one fault at time but only
1241 * block if waiting for the very first one.
1242 */
1243 no_wait = O_NONBLOCK;
1244 }
1245}
1246
1247static void __wake_userfault(struct userfaultfd_ctx *ctx,
1248 struct userfaultfd_wake_range *range)
1249{
cbcfa130 1250 spin_lock_irq(&ctx->fault_pending_wqh.lock);
86039bd3 1251 /* wake all in the range and autoremove */
15b726ef 1252 if (waitqueue_active(&ctx->fault_pending_wqh))
ac5be6b4 1253 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
15b726ef
AA
1254 range);
1255 if (waitqueue_active(&ctx->fault_wqh))
c430d1e8 1256 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
cbcfa130 1257 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
86039bd3
AA
1258}
1259
1260static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1261 struct userfaultfd_wake_range *range)
1262{
2c5b7e1b
AA
1263 unsigned seq;
1264 bool need_wakeup;
1265
86039bd3
AA
1266 /*
1267 * To be sure waitqueue_active() is not reordered by the CPU
1268 * before the pagetable update, use an explicit SMP memory
3e4e28c5 1269 * barrier here. PT lock release or mmap_read_unlock(mm) still
86039bd3
AA
1270 * have release semantics that can allow the
1271 * waitqueue_active() to be reordered before the pte update.
1272 */
1273 smp_mb();
1274
1275 /*
1276 * Use waitqueue_active because it's very frequent to
1277 * change the address space atomically even if there are no
1278 * userfaults yet. So we take the spinlock only when we're
1279 * sure we've userfaults to wake.
1280 */
2c5b7e1b
AA
1281 do {
1282 seq = read_seqcount_begin(&ctx->refile_seq);
1283 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1284 waitqueue_active(&ctx->fault_wqh);
1285 cond_resched();
1286 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1287 if (need_wakeup)
86039bd3
AA
1288 __wake_userfault(ctx, range);
1289}
1290
2ef5d724
AR
1291static __always_inline int validate_unaligned_range(
1292 struct mm_struct *mm, __u64 start, __u64 len)
86039bd3
AA
1293{
1294 __u64 task_size = mm->task_size;
1295
86039bd3
AA
1296 if (len & ~PAGE_MASK)
1297 return -EINVAL;
1298 if (!len)
1299 return -EINVAL;
e71e2ace 1300 if (start < mmap_min_addr)
86039bd3 1301 return -EINVAL;
e71e2ace 1302 if (start >= task_size)
86039bd3 1303 return -EINVAL;
e71e2ace 1304 if (len > task_size - start)
86039bd3 1305 return -EINVAL;
2ef5d724
AR
1306 if (start + len <= start)
1307 return -EINVAL;
86039bd3
AA
1308 return 0;
1309}
1310
2ef5d724
AR
1311static __always_inline int validate_range(struct mm_struct *mm,
1312 __u64 start, __u64 len)
1313{
1314 if (start & ~PAGE_MASK)
1315 return -EINVAL;
1316
1317 return validate_unaligned_range(mm, start, len);
1318}
1319
86039bd3
AA
1320static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1321 unsigned long arg)
1322{
1323 struct mm_struct *mm = ctx->mm;
1324 struct vm_area_struct *vma, *prev, *cur;
1325 int ret;
1326 struct uffdio_register uffdio_register;
1327 struct uffdio_register __user *user_uffdio_register;
1328 unsigned long vm_flags, new_flags;
1329 bool found;
ce53e8e6 1330 bool basic_ioctls;
86039bd3 1331 unsigned long start, end, vma_end;
11a9b902 1332 struct vma_iterator vmi;
d61ea1cb 1333 bool wp_async = userfaultfd_wp_async_ctx(ctx);
5543d3c4 1334 pgoff_t pgoff;
86039bd3
AA
1335
1336 user_uffdio_register = (struct uffdio_register __user *) arg;
1337
1338 ret = -EFAULT;
1339 if (copy_from_user(&uffdio_register, user_uffdio_register,
1340 sizeof(uffdio_register)-sizeof(__u64)))
1341 goto out;
1342
1343 ret = -EINVAL;
1344 if (!uffdio_register.mode)
1345 goto out;
7677f7fd 1346 if (uffdio_register.mode & ~UFFD_API_REGISTER_MODES)
86039bd3
AA
1347 goto out;
1348 vm_flags = 0;
1349 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1350 vm_flags |= VM_UFFD_MISSING;
00b151f2
PX
1351 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1352#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP
1353 goto out;
1354#endif
86039bd3 1355 vm_flags |= VM_UFFD_WP;
00b151f2 1356 }
7677f7fd
AR
1357 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR) {
1358#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
1359 goto out;
1360#endif
1361 vm_flags |= VM_UFFD_MINOR;
1362 }
86039bd3 1363
e71e2ace 1364 ret = validate_range(mm, uffdio_register.range.start,
86039bd3
AA
1365 uffdio_register.range.len);
1366 if (ret)
1367 goto out;
1368
1369 start = uffdio_register.range.start;
1370 end = start + uffdio_register.range.len;
1371
d2005e3f
ON
1372 ret = -ENOMEM;
1373 if (!mmget_not_zero(mm))
1374 goto out;
1375
11a9b902 1376 ret = -EINVAL;
d8ed45c5 1377 mmap_write_lock(mm);
11a9b902
LH
1378 vma_iter_init(&vmi, mm, start);
1379 vma = vma_find(&vmi, end);
86039bd3
AA
1380 if (!vma)
1381 goto out_unlock;
1382
cab350af
MK
1383 /*
1384 * If the first vma contains huge pages, make sure start address
1385 * is aligned to huge page size.
1386 */
1387 if (is_vm_hugetlb_page(vma)) {
1388 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1389
1390 if (start & (vma_hpagesize - 1))
1391 goto out_unlock;
1392 }
1393
86039bd3
AA
1394 /*
1395 * Search for not compatible vmas.
86039bd3
AA
1396 */
1397 found = false;
ce53e8e6 1398 basic_ioctls = false;
11a9b902
LH
1399 cur = vma;
1400 do {
86039bd3
AA
1401 cond_resched();
1402
1403 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
7677f7fd 1404 !!(cur->vm_flags & __VM_UFFD_FLAGS));
86039bd3
AA
1405
1406 /* check not compatible vmas */
1407 ret = -EINVAL;
d61ea1cb 1408 if (!vma_can_userfault(cur, vm_flags, wp_async))
86039bd3 1409 goto out_unlock;
29ec9066
AA
1410
1411 /*
1412 * UFFDIO_COPY will fill file holes even without
1413 * PROT_WRITE. This check enforces that if this is a
1414 * MAP_SHARED, the process has write permission to the backing
1415 * file. If VM_MAYWRITE is set it also enforces that on a
1416 * MAP_SHARED vma: there is no F_WRITE_SEAL and no further
1417 * F_WRITE_SEAL can be taken until the vma is destroyed.
1418 */
1419 ret = -EPERM;
1420 if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
1421 goto out_unlock;
1422
cab350af
MK
1423 /*
1424 * If this vma contains ending address, and huge pages
1425 * check alignment.
1426 */
1427 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1428 end > cur->vm_start) {
1429 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1430
1431 ret = -EINVAL;
1432
1433 if (end & (vma_hpagesize - 1))
1434 goto out_unlock;
1435 }
63b2d417
AA
1436 if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
1437 goto out_unlock;
86039bd3
AA
1438
1439 /*
1440 * Check that this vma isn't already owned by a
1441 * different userfaultfd. We can't allow more than one
1442 * userfaultfd to own a single vma simultaneously or we
1443 * wouldn't know which one to deliver the userfaults to.
1444 */
1445 ret = -EBUSY;
1446 if (cur->vm_userfaultfd_ctx.ctx &&
1447 cur->vm_userfaultfd_ctx.ctx != ctx)
1448 goto out_unlock;
1449
cab350af
MK
1450 /*
1451 * Note vmas containing huge pages
1452 */
ce53e8e6
MR
1453 if (is_vm_hugetlb_page(cur))
1454 basic_ioctls = true;
cab350af 1455
86039bd3 1456 found = true;
11a9b902 1457 } for_each_vma_range(vmi, cur, end);
86039bd3
AA
1458 BUG_ON(!found);
1459
11a9b902
LH
1460 vma_iter_set(&vmi, start);
1461 prev = vma_prev(&vmi);
270aa010
PX
1462 if (vma->vm_start < start)
1463 prev = vma;
86039bd3
AA
1464
1465 ret = 0;
11a9b902 1466 for_each_vma_range(vmi, vma, end) {
86039bd3
AA
1467 cond_resched();
1468
d61ea1cb 1469 BUG_ON(!vma_can_userfault(vma, vm_flags, wp_async));
86039bd3
AA
1470 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1471 vma->vm_userfaultfd_ctx.ctx != ctx);
29ec9066 1472 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
86039bd3
AA
1473
1474 /*
1475 * Nothing to do: this vma is already registered into this
1476 * userfaultfd and with the right tracking mode too.
1477 */
1478 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1479 (vma->vm_flags & vm_flags) == vm_flags)
1480 goto skip;
1481
1482 if (vma->vm_start > start)
1483 start = vma->vm_start;
1484 vma_end = min(end, vma->vm_end);
1485
7677f7fd 1486 new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags;
5543d3c4 1487 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
9760ebff 1488 prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags,
5543d3c4 1489 vma->anon_vma, vma->vm_file, pgoff,
86039bd3 1490 vma_policy(vma),
9a10064f 1491 ((struct vm_userfaultfd_ctx){ ctx }),
5c26f6ac 1492 anon_vma_name(vma));
86039bd3 1493 if (prev) {
69dbe6da 1494 /* vma_merge() invalidated the mas */
86039bd3
AA
1495 vma = prev;
1496 goto next;
1497 }
1498 if (vma->vm_start < start) {
9760ebff 1499 ret = split_vma(&vmi, vma, start, 1);
86039bd3
AA
1500 if (ret)
1501 break;
1502 }
1503 if (vma->vm_end > end) {
9760ebff 1504 ret = split_vma(&vmi, vma, end, 0);
86039bd3
AA
1505 if (ret)
1506 break;
1507 }
1508 next:
1509 /*
1510 * In the vma_merge() successful mprotect-like case 8:
1511 * the next vma was merged into the current one and
1512 * the current one has not been updated yet.
1513 */
60081bf1 1514 vma_start_write(vma);
51d3d5eb 1515 userfaultfd_set_vm_flags(vma, new_flags);
86039bd3
AA
1516 vma->vm_userfaultfd_ctx.ctx = ctx;
1517
6dfeaff9
PX
1518 if (is_vm_hugetlb_page(vma) && uffd_disable_huge_pmd_share(vma))
1519 hugetlb_unshare_all_pmds(vma);
1520
86039bd3
AA
1521 skip:
1522 prev = vma;
1523 start = vma->vm_end;
11a9b902
LH
1524 }
1525
86039bd3 1526out_unlock:
d8ed45c5 1527 mmap_write_unlock(mm);
d2005e3f 1528 mmput(mm);
86039bd3 1529 if (!ret) {
14819305
PX
1530 __u64 ioctls_out;
1531
1532 ioctls_out = basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
1533 UFFD_API_RANGE_IOCTLS;
1534
1535 /*
1536 * Declare the WP ioctl only if the WP mode is
1537 * specified and all checks passed with the range
1538 */
1539 if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_WP))
1540 ioctls_out &= ~((__u64)1 << _UFFDIO_WRITEPROTECT);
1541
f6191471
AR
1542 /* CONTINUE ioctl is only supported for MINOR ranges. */
1543 if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR))
1544 ioctls_out &= ~((__u64)1 << _UFFDIO_CONTINUE);
1545
86039bd3
AA
1546 /*
1547 * Now that we scanned all vmas we can already tell
1548 * userland which ioctls methods are guaranteed to
1549 * succeed on this range.
1550 */
14819305 1551 if (put_user(ioctls_out, &user_uffdio_register->ioctls))
86039bd3
AA
1552 ret = -EFAULT;
1553 }
1554out:
1555 return ret;
1556}
1557
1558static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1559 unsigned long arg)
1560{
1561 struct mm_struct *mm = ctx->mm;
1562 struct vm_area_struct *vma, *prev, *cur;
1563 int ret;
1564 struct uffdio_range uffdio_unregister;
1565 unsigned long new_flags;
1566 bool found;
1567 unsigned long start, end, vma_end;
1568 const void __user *buf = (void __user *)arg;
11a9b902 1569 struct vma_iterator vmi;
d61ea1cb 1570 bool wp_async = userfaultfd_wp_async_ctx(ctx);
5543d3c4 1571 pgoff_t pgoff;
86039bd3
AA
1572
1573 ret = -EFAULT;
1574 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1575 goto out;
1576
e71e2ace 1577 ret = validate_range(mm, uffdio_unregister.start,
86039bd3
AA
1578 uffdio_unregister.len);
1579 if (ret)
1580 goto out;
1581
1582 start = uffdio_unregister.start;
1583 end = start + uffdio_unregister.len;
1584
d2005e3f
ON
1585 ret = -ENOMEM;
1586 if (!mmget_not_zero(mm))
1587 goto out;
1588
d8ed45c5 1589 mmap_write_lock(mm);
86039bd3 1590 ret = -EINVAL;
11a9b902
LH
1591 vma_iter_init(&vmi, mm, start);
1592 vma = vma_find(&vmi, end);
1593 if (!vma)
86039bd3
AA
1594 goto out_unlock;
1595
cab350af
MK
1596 /*
1597 * If the first vma contains huge pages, make sure start address
1598 * is aligned to huge page size.
1599 */
1600 if (is_vm_hugetlb_page(vma)) {
1601 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1602
1603 if (start & (vma_hpagesize - 1))
1604 goto out_unlock;
1605 }
1606
86039bd3
AA
1607 /*
1608 * Search for not compatible vmas.
86039bd3
AA
1609 */
1610 found = false;
11a9b902
LH
1611 cur = vma;
1612 do {
86039bd3
AA
1613 cond_resched();
1614
1615 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
7677f7fd 1616 !!(cur->vm_flags & __VM_UFFD_FLAGS));
86039bd3
AA
1617
1618 /*
1619 * Check not compatible vmas, not strictly required
1620 * here as not compatible vmas cannot have an
1621 * userfaultfd_ctx registered on them, but this
1622 * provides for more strict behavior to notice
1623 * unregistration errors.
1624 */
d61ea1cb 1625 if (!vma_can_userfault(cur, cur->vm_flags, wp_async))
86039bd3
AA
1626 goto out_unlock;
1627
1628 found = true;
11a9b902 1629 } for_each_vma_range(vmi, cur, end);
86039bd3
AA
1630 BUG_ON(!found);
1631
11a9b902
LH
1632 vma_iter_set(&vmi, start);
1633 prev = vma_prev(&vmi);
270aa010
PX
1634 if (vma->vm_start < start)
1635 prev = vma;
1636
86039bd3 1637 ret = 0;
11a9b902 1638 for_each_vma_range(vmi, vma, end) {
86039bd3
AA
1639 cond_resched();
1640
d61ea1cb 1641 BUG_ON(!vma_can_userfault(vma, vma->vm_flags, wp_async));
86039bd3
AA
1642
1643 /*
1644 * Nothing to do: this vma is already registered into this
1645 * userfaultfd and with the right tracking mode too.
1646 */
1647 if (!vma->vm_userfaultfd_ctx.ctx)
1648 goto skip;
1649
01e881f5
AA
1650 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
1651
86039bd3
AA
1652 if (vma->vm_start > start)
1653 start = vma->vm_start;
1654 vma_end = min(end, vma->vm_end);
1655
09fa5296
AA
1656 if (userfaultfd_missing(vma)) {
1657 /*
1658 * Wake any concurrent pending userfault while
1659 * we unregister, so they will not hang
1660 * permanently and it avoids userland to call
1661 * UFFDIO_WAKE explicitly.
1662 */
1663 struct userfaultfd_wake_range range;
1664 range.start = start;
1665 range.len = vma_end - start;
1666 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1667 }
1668
f369b07c
PX
1669 /* Reset ptes for the whole vma range if wr-protected */
1670 if (userfaultfd_wp(vma))
61c50040 1671 uffd_wp_range(vma, start, vma_end - start, false);
f369b07c 1672
7677f7fd 1673 new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS;
5543d3c4 1674 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
9760ebff 1675 prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags,
5543d3c4 1676 vma->anon_vma, vma->vm_file, pgoff,
86039bd3 1677 vma_policy(vma),
5c26f6ac 1678 NULL_VM_UFFD_CTX, anon_vma_name(vma));
86039bd3
AA
1679 if (prev) {
1680 vma = prev;
1681 goto next;
1682 }
1683 if (vma->vm_start < start) {
9760ebff 1684 ret = split_vma(&vmi, vma, start, 1);
86039bd3
AA
1685 if (ret)
1686 break;
1687 }
1688 if (vma->vm_end > end) {
9760ebff 1689 ret = split_vma(&vmi, vma, end, 0);
86039bd3
AA
1690 if (ret)
1691 break;
1692 }
1693 next:
1694 /*
1695 * In the vma_merge() successful mprotect-like case 8:
1696 * the next vma was merged into the current one and
1697 * the current one has not been updated yet.
1698 */
60081bf1 1699 vma_start_write(vma);
51d3d5eb 1700 userfaultfd_set_vm_flags(vma, new_flags);
86039bd3
AA
1701 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1702
1703 skip:
1704 prev = vma;
1705 start = vma->vm_end;
11a9b902
LH
1706 }
1707
86039bd3 1708out_unlock:
d8ed45c5 1709 mmap_write_unlock(mm);
d2005e3f 1710 mmput(mm);
86039bd3
AA
1711out:
1712 return ret;
1713}
1714
1715/*
ba85c702
AA
1716 * userfaultfd_wake may be used in combination with the
1717 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
86039bd3
AA
1718 */
1719static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1720 unsigned long arg)
1721{
1722 int ret;
1723 struct uffdio_range uffdio_wake;
1724 struct userfaultfd_wake_range range;
1725 const void __user *buf = (void __user *)arg;
1726
1727 ret = -EFAULT;
1728 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1729 goto out;
1730
e71e2ace 1731 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
86039bd3
AA
1732 if (ret)
1733 goto out;
1734
1735 range.start = uffdio_wake.start;
1736 range.len = uffdio_wake.len;
1737
1738 /*
1739 * len == 0 means wake all and we don't want to wake all here,
1740 * so check it again to be sure.
1741 */
1742 VM_BUG_ON(!range.len);
1743
1744 wake_userfault(ctx, &range);
1745 ret = 0;
1746
1747out:
1748 return ret;
1749}
1750
ad465cae
AA
1751static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1752 unsigned long arg)
1753{
1754 __s64 ret;
1755 struct uffdio_copy uffdio_copy;
1756 struct uffdio_copy __user *user_uffdio_copy;
1757 struct userfaultfd_wake_range range;
d9712937 1758 uffd_flags_t flags = 0;
ad465cae
AA
1759
1760 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1761
df2cc96e 1762 ret = -EAGAIN;
a759a909 1763 if (atomic_read(&ctx->mmap_changing))
df2cc96e
MR
1764 goto out;
1765
ad465cae
AA
1766 ret = -EFAULT;
1767 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1768 /* don't copy "copy" last field */
1769 sizeof(uffdio_copy)-sizeof(__s64)))
1770 goto out;
1771
2ef5d724
AR
1772 ret = validate_unaligned_range(ctx->mm, uffdio_copy.src,
1773 uffdio_copy.len);
1774 if (ret)
1775 goto out;
e71e2ace 1776 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
ad465cae
AA
1777 if (ret)
1778 goto out;
2ef5d724 1779
ad465cae 1780 ret = -EINVAL;
72981e0e 1781 if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP))
ad465cae 1782 goto out;
d9712937
AR
1783 if (uffdio_copy.mode & UFFDIO_COPY_MODE_WP)
1784 flags |= MFILL_ATOMIC_WP;
d2005e3f 1785 if (mmget_not_zero(ctx->mm)) {
a734991c
AR
1786 ret = mfill_atomic_copy(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1787 uffdio_copy.len, &ctx->mmap_changing,
d9712937 1788 flags);
d2005e3f 1789 mmput(ctx->mm);
96333187 1790 } else {
e86b298b 1791 return -ESRCH;
d2005e3f 1792 }
ad465cae
AA
1793 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1794 return -EFAULT;
1795 if (ret < 0)
1796 goto out;
1797 BUG_ON(!ret);
1798 /* len == 0 would wake all */
1799 range.len = ret;
1800 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1801 range.start = uffdio_copy.dst;
1802 wake_userfault(ctx, &range);
1803 }
1804 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1805out:
1806 return ret;
1807}
1808
1809static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1810 unsigned long arg)
1811{
1812 __s64 ret;
1813 struct uffdio_zeropage uffdio_zeropage;
1814 struct uffdio_zeropage __user *user_uffdio_zeropage;
1815 struct userfaultfd_wake_range range;
1816
1817 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1818
df2cc96e 1819 ret = -EAGAIN;
a759a909 1820 if (atomic_read(&ctx->mmap_changing))
df2cc96e
MR
1821 goto out;
1822
ad465cae
AA
1823 ret = -EFAULT;
1824 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1825 /* don't copy "zeropage" last field */
1826 sizeof(uffdio_zeropage)-sizeof(__s64)))
1827 goto out;
1828
e71e2ace 1829 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
ad465cae
AA
1830 uffdio_zeropage.range.len);
1831 if (ret)
1832 goto out;
1833 ret = -EINVAL;
1834 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1835 goto out;
1836
d2005e3f 1837 if (mmget_not_zero(ctx->mm)) {
a734991c
AR
1838 ret = mfill_atomic_zeropage(ctx->mm, uffdio_zeropage.range.start,
1839 uffdio_zeropage.range.len,
1840 &ctx->mmap_changing);
d2005e3f 1841 mmput(ctx->mm);
9d95aa4b 1842 } else {
e86b298b 1843 return -ESRCH;
d2005e3f 1844 }
ad465cae
AA
1845 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1846 return -EFAULT;
1847 if (ret < 0)
1848 goto out;
1849 /* len == 0 would wake all */
1850 BUG_ON(!ret);
1851 range.len = ret;
1852 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1853 range.start = uffdio_zeropage.range.start;
1854 wake_userfault(ctx, &range);
1855 }
1856 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1857out:
1858 return ret;
1859}
1860
63b2d417
AA
1861static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,
1862 unsigned long arg)
1863{
1864 int ret;
1865 struct uffdio_writeprotect uffdio_wp;
1866 struct uffdio_writeprotect __user *user_uffdio_wp;
1867 struct userfaultfd_wake_range range;
23080e27 1868 bool mode_wp, mode_dontwake;
63b2d417 1869
a759a909 1870 if (atomic_read(&ctx->mmap_changing))
63b2d417
AA
1871 return -EAGAIN;
1872
1873 user_uffdio_wp = (struct uffdio_writeprotect __user *) arg;
1874
1875 if (copy_from_user(&uffdio_wp, user_uffdio_wp,
1876 sizeof(struct uffdio_writeprotect)))
1877 return -EFAULT;
1878
e71e2ace 1879 ret = validate_range(ctx->mm, uffdio_wp.range.start,
63b2d417
AA
1880 uffdio_wp.range.len);
1881 if (ret)
1882 return ret;
1883
1884 if (uffdio_wp.mode & ~(UFFDIO_WRITEPROTECT_MODE_DONTWAKE |
1885 UFFDIO_WRITEPROTECT_MODE_WP))
1886 return -EINVAL;
23080e27
PX
1887
1888 mode_wp = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_WP;
1889 mode_dontwake = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_DONTWAKE;
1890
1891 if (mode_wp && mode_dontwake)
63b2d417
AA
1892 return -EINVAL;
1893
cb185d5f
NA
1894 if (mmget_not_zero(ctx->mm)) {
1895 ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
1896 uffdio_wp.range.len, mode_wp,
1897 &ctx->mmap_changing);
1898 mmput(ctx->mm);
1899 } else {
1900 return -ESRCH;
1901 }
1902
63b2d417
AA
1903 if (ret)
1904 return ret;
1905
23080e27 1906 if (!mode_wp && !mode_dontwake) {
63b2d417
AA
1907 range.start = uffdio_wp.range.start;
1908 range.len = uffdio_wp.range.len;
1909 wake_userfault(ctx, &range);
1910 }
1911 return ret;
1912}
1913
f6191471
AR
1914static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg)
1915{
1916 __s64 ret;
1917 struct uffdio_continue uffdio_continue;
1918 struct uffdio_continue __user *user_uffdio_continue;
1919 struct userfaultfd_wake_range range;
02891844 1920 uffd_flags_t flags = 0;
f6191471
AR
1921
1922 user_uffdio_continue = (struct uffdio_continue __user *)arg;
1923
1924 ret = -EAGAIN;
a759a909 1925 if (atomic_read(&ctx->mmap_changing))
f6191471
AR
1926 goto out;
1927
1928 ret = -EFAULT;
1929 if (copy_from_user(&uffdio_continue, user_uffdio_continue,
1930 /* don't copy the output fields */
1931 sizeof(uffdio_continue) - (sizeof(__s64))))
1932 goto out;
1933
e71e2ace 1934 ret = validate_range(ctx->mm, uffdio_continue.range.start,
f6191471
AR
1935 uffdio_continue.range.len);
1936 if (ret)
1937 goto out;
1938
1939 ret = -EINVAL;
02891844
AR
1940 if (uffdio_continue.mode & ~(UFFDIO_CONTINUE_MODE_DONTWAKE |
1941 UFFDIO_CONTINUE_MODE_WP))
f6191471 1942 goto out;
02891844
AR
1943 if (uffdio_continue.mode & UFFDIO_CONTINUE_MODE_WP)
1944 flags |= MFILL_ATOMIC_WP;
f6191471
AR
1945
1946 if (mmget_not_zero(ctx->mm)) {
a734991c
AR
1947 ret = mfill_atomic_continue(ctx->mm, uffdio_continue.range.start,
1948 uffdio_continue.range.len,
02891844 1949 &ctx->mmap_changing, flags);
f6191471
AR
1950 mmput(ctx->mm);
1951 } else {
1952 return -ESRCH;
1953 }
1954
1955 if (unlikely(put_user(ret, &user_uffdio_continue->mapped)))
1956 return -EFAULT;
1957 if (ret < 0)
1958 goto out;
1959
1960 /* len == 0 would wake all */
1961 BUG_ON(!ret);
1962 range.len = ret;
1963 if (!(uffdio_continue.mode & UFFDIO_CONTINUE_MODE_DONTWAKE)) {
1964 range.start = uffdio_continue.range.start;
1965 wake_userfault(ctx, &range);
1966 }
1967 ret = range.len == uffdio_continue.range.len ? 0 : -EAGAIN;
1968
1969out:
1970 return ret;
1971}
1972
fc71884a
AR
1973static inline int userfaultfd_poison(struct userfaultfd_ctx *ctx, unsigned long arg)
1974{
1975 __s64 ret;
1976 struct uffdio_poison uffdio_poison;
1977 struct uffdio_poison __user *user_uffdio_poison;
1978 struct userfaultfd_wake_range range;
1979
1980 user_uffdio_poison = (struct uffdio_poison __user *)arg;
1981
1982 ret = -EAGAIN;
1983 if (atomic_read(&ctx->mmap_changing))
1984 goto out;
1985
1986 ret = -EFAULT;
1987 if (copy_from_user(&uffdio_poison, user_uffdio_poison,
1988 /* don't copy the output fields */
1989 sizeof(uffdio_poison) - (sizeof(__s64))))
1990 goto out;
1991
1992 ret = validate_range(ctx->mm, uffdio_poison.range.start,
1993 uffdio_poison.range.len);
1994 if (ret)
1995 goto out;
1996
1997 ret = -EINVAL;
1998 if (uffdio_poison.mode & ~UFFDIO_POISON_MODE_DONTWAKE)
1999 goto out;
2000
2001 if (mmget_not_zero(ctx->mm)) {
2002 ret = mfill_atomic_poison(ctx->mm, uffdio_poison.range.start,
2003 uffdio_poison.range.len,
2004 &ctx->mmap_changing, 0);
2005 mmput(ctx->mm);
2006 } else {
2007 return -ESRCH;
2008 }
2009
2010 if (unlikely(put_user(ret, &user_uffdio_poison->updated)))
2011 return -EFAULT;
2012 if (ret < 0)
2013 goto out;
2014
2015 /* len == 0 would wake all */
2016 BUG_ON(!ret);
2017 range.len = ret;
2018 if (!(uffdio_poison.mode & UFFDIO_POISON_MODE_DONTWAKE)) {
2019 range.start = uffdio_poison.range.start;
2020 wake_userfault(ctx, &range);
2021 }
2022 ret = range.len == uffdio_poison.range.len ? 0 : -EAGAIN;
2023
2024out:
2025 return ret;
2026}
2027
d61ea1cb
PX
2028bool userfaultfd_wp_async(struct vm_area_struct *vma)
2029{
2030 return userfaultfd_wp_async_ctx(vma->vm_userfaultfd_ctx.ctx);
2031}
2032
9cd75c3c
PE
2033static inline unsigned int uffd_ctx_features(__u64 user_features)
2034{
2035 /*
22e5fe2a
NA
2036 * For the current set of features the bits just coincide. Set
2037 * UFFD_FEATURE_INITIALIZED to mark the features as enabled.
9cd75c3c 2038 */
22e5fe2a 2039 return (unsigned int)user_features | UFFD_FEATURE_INITIALIZED;
9cd75c3c
PE
2040}
2041
86039bd3
AA
2042/*
2043 * userland asks for a certain API version and we return which bits
2044 * and ioctl commands are implemented in this kernel for such API
2045 * version or -EINVAL if unknown.
2046 */
2047static int userfaultfd_api(struct userfaultfd_ctx *ctx,
2048 unsigned long arg)
2049{
2050 struct uffdio_api uffdio_api;
2051 void __user *buf = (void __user *)arg;
22e5fe2a 2052 unsigned int ctx_features;
86039bd3 2053 int ret;
65603144 2054 __u64 features;
86039bd3 2055
86039bd3 2056 ret = -EFAULT;
a9b85f94 2057 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
86039bd3 2058 goto out;
2ff559f3
PX
2059 features = uffdio_api.features;
2060 ret = -EINVAL;
2061 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES))
2062 goto err_out;
3c1c24d9
MR
2063 ret = -EPERM;
2064 if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE))
2065 goto err_out;
d61ea1cb
PX
2066
2067 /* WP_ASYNC relies on WP_UNPOPULATED, choose it unconditionally */
2068 if (features & UFFD_FEATURE_WP_ASYNC)
2069 features |= UFFD_FEATURE_WP_UNPOPULATED;
2070
65603144
AA
2071 /* report all available features and ioctls to userland */
2072 uffdio_api.features = UFFD_API_FEATURES;
7677f7fd 2073#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
964ab004
AR
2074 uffdio_api.features &=
2075 ~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM);
00b151f2
PX
2076#endif
2077#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP
2078 uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP;
b1f9e876
PX
2079#endif
2080#ifndef CONFIG_PTE_MARKER_UFFD_WP
2081 uffdio_api.features &= ~UFFD_FEATURE_WP_HUGETLBFS_SHMEM;
2bad466c 2082 uffdio_api.features &= ~UFFD_FEATURE_WP_UNPOPULATED;
d61ea1cb 2083 uffdio_api.features &= ~UFFD_FEATURE_WP_ASYNC;
7677f7fd 2084#endif
86039bd3
AA
2085 uffdio_api.ioctls = UFFD_API_IOCTLS;
2086 ret = -EFAULT;
2087 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
2088 goto out;
22e5fe2a 2089
65603144 2090 /* only enable the requested features for this uffd context */
22e5fe2a
NA
2091 ctx_features = uffd_ctx_features(features);
2092 ret = -EINVAL;
2093 if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
2094 goto err_out;
2095
86039bd3
AA
2096 ret = 0;
2097out:
2098 return ret;
3c1c24d9
MR
2099err_out:
2100 memset(&uffdio_api, 0, sizeof(uffdio_api));
2101 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
2102 ret = -EFAULT;
2103 goto out;
86039bd3
AA
2104}
2105
2106static long userfaultfd_ioctl(struct file *file, unsigned cmd,
2107 unsigned long arg)
2108{
2109 int ret = -EINVAL;
2110 struct userfaultfd_ctx *ctx = file->private_data;
2111
22e5fe2a 2112 if (cmd != UFFDIO_API && !userfaultfd_is_initialized(ctx))
e6485a47
AA
2113 return -EINVAL;
2114
86039bd3
AA
2115 switch(cmd) {
2116 case UFFDIO_API:
2117 ret = userfaultfd_api(ctx, arg);
2118 break;
2119 case UFFDIO_REGISTER:
2120 ret = userfaultfd_register(ctx, arg);
2121 break;
2122 case UFFDIO_UNREGISTER:
2123 ret = userfaultfd_unregister(ctx, arg);
2124 break;
2125 case UFFDIO_WAKE:
2126 ret = userfaultfd_wake(ctx, arg);
2127 break;
ad465cae
AA
2128 case UFFDIO_COPY:
2129 ret = userfaultfd_copy(ctx, arg);
2130 break;
2131 case UFFDIO_ZEROPAGE:
2132 ret = userfaultfd_zeropage(ctx, arg);
2133 break;
63b2d417
AA
2134 case UFFDIO_WRITEPROTECT:
2135 ret = userfaultfd_writeprotect(ctx, arg);
2136 break;
f6191471
AR
2137 case UFFDIO_CONTINUE:
2138 ret = userfaultfd_continue(ctx, arg);
2139 break;
fc71884a
AR
2140 case UFFDIO_POISON:
2141 ret = userfaultfd_poison(ctx, arg);
2142 break;
86039bd3
AA
2143 }
2144 return ret;
2145}
2146
2147#ifdef CONFIG_PROC_FS
2148static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
2149{
2150 struct userfaultfd_ctx *ctx = f->private_data;
ac6424b9 2151 wait_queue_entry_t *wq;
86039bd3
AA
2152 unsigned long pending = 0, total = 0;
2153
cbcfa130 2154 spin_lock_irq(&ctx->fault_pending_wqh.lock);
2055da97 2155 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
15b726ef
AA
2156 pending++;
2157 total++;
2158 }
2055da97 2159 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
86039bd3
AA
2160 total++;
2161 }
cbcfa130 2162 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
86039bd3
AA
2163
2164 /*
2165 * If more protocols will be added, there will be all shown
2166 * separated by a space. Like this:
2167 * protocols: aa:... bb:...
2168 */
2169 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
045098e9 2170 pending, total, UFFD_API, ctx->features,
86039bd3
AA
2171 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
2172}
2173#endif
2174
2175static const struct file_operations userfaultfd_fops = {
2176#ifdef CONFIG_PROC_FS
2177 .show_fdinfo = userfaultfd_show_fdinfo,
2178#endif
2179 .release = userfaultfd_release,
2180 .poll = userfaultfd_poll,
2181 .read = userfaultfd_read,
2182 .unlocked_ioctl = userfaultfd_ioctl,
1832f2d8 2183 .compat_ioctl = compat_ptr_ioctl,
86039bd3
AA
2184 .llseek = noop_llseek,
2185};
2186
3004ec9c
AA
2187static void init_once_userfaultfd_ctx(void *mem)
2188{
2189 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
2190
2191 init_waitqueue_head(&ctx->fault_pending_wqh);
2192 init_waitqueue_head(&ctx->fault_wqh);
9cd75c3c 2193 init_waitqueue_head(&ctx->event_wqh);
3004ec9c 2194 init_waitqueue_head(&ctx->fd_wqh);
2ca97ac8 2195 seqcount_spinlock_init(&ctx->refile_seq, &ctx->fault_pending_wqh.lock);
3004ec9c
AA
2196}
2197
2d5de004 2198static int new_userfaultfd(int flags)
86039bd3 2199{
86039bd3 2200 struct userfaultfd_ctx *ctx;
284cd241 2201 int fd;
86039bd3
AA
2202
2203 BUG_ON(!current->mm);
2204
2205 /* Check the UFFD_* constants for consistency. */
37cd0575 2206 BUILD_BUG_ON(UFFD_USER_MODE_ONLY & UFFD_SHARED_FCNTL_FLAGS);
86039bd3
AA
2207 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
2208 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
2209
37cd0575 2210 if (flags & ~(UFFD_SHARED_FCNTL_FLAGS | UFFD_USER_MODE_ONLY))
284cd241 2211 return -EINVAL;
86039bd3 2212
3004ec9c 2213 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
86039bd3 2214 if (!ctx)
284cd241 2215 return -ENOMEM;
86039bd3 2216
ca880420 2217 refcount_set(&ctx->refcount, 1);
86039bd3 2218 ctx->flags = flags;
9cd75c3c 2219 ctx->features = 0;
86039bd3 2220 ctx->released = false;
a759a909 2221 atomic_set(&ctx->mmap_changing, 0);
86039bd3
AA
2222 ctx->mm = current->mm;
2223 /* prevent the mm struct to be freed */
f1f10076 2224 mmgrab(ctx->mm);
86039bd3 2225
b537900f 2226 fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, ctx,
abec3d01 2227 O_RDONLY | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL);
284cd241 2228 if (fd < 0) {
d2005e3f 2229 mmdrop(ctx->mm);
3004ec9c 2230 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
c03e946f 2231 }
86039bd3 2232 return fd;
86039bd3 2233}
3004ec9c 2234
2d5de004
AR
2235static inline bool userfaultfd_syscall_allowed(int flags)
2236{
2237 /* Userspace-only page faults are always allowed */
2238 if (flags & UFFD_USER_MODE_ONLY)
2239 return true;
2240
2241 /*
2242 * The user is requesting a userfaultfd which can handle kernel faults.
2243 * Privileged users are always allowed to do this.
2244 */
2245 if (capable(CAP_SYS_PTRACE))
2246 return true;
2247
2248 /* Otherwise, access to kernel fault handling is sysctl controlled. */
2249 return sysctl_unprivileged_userfaultfd;
2250}
2251
2252SYSCALL_DEFINE1(userfaultfd, int, flags)
2253{
2254 if (!userfaultfd_syscall_allowed(flags))
2255 return -EPERM;
2256
2257 return new_userfaultfd(flags);
2258}
2259
2260static long userfaultfd_dev_ioctl(struct file *file, unsigned int cmd, unsigned long flags)
2261{
2262 if (cmd != USERFAULTFD_IOC_NEW)
2263 return -EINVAL;
2264
2265 return new_userfaultfd(flags);
2266}
2267
2268static const struct file_operations userfaultfd_dev_fops = {
2269 .unlocked_ioctl = userfaultfd_dev_ioctl,
2270 .compat_ioctl = userfaultfd_dev_ioctl,
2271 .owner = THIS_MODULE,
2272 .llseek = noop_llseek,
2273};
2274
2275static struct miscdevice userfaultfd_misc = {
2276 .minor = MISC_DYNAMIC_MINOR,
2277 .name = "userfaultfd",
2278 .fops = &userfaultfd_dev_fops
2279};
2280
3004ec9c
AA
2281static int __init userfaultfd_init(void)
2282{
2d5de004
AR
2283 int ret;
2284
2285 ret = misc_register(&userfaultfd_misc);
2286 if (ret)
2287 return ret;
2288
3004ec9c
AA
2289 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
2290 sizeof(struct userfaultfd_ctx),
2291 0,
2292 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2293 init_once_userfaultfd_ctx);
2d337b71
Z
2294#ifdef CONFIG_SYSCTL
2295 register_sysctl_init("vm", vm_userfaultfd_table);
2296#endif
3004ec9c
AA
2297 return 0;
2298}
2299__initcall(userfaultfd_init);