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