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