xref: /openbmc/linux/fs/userfaultfd.c (revision 05ce77249d5068b057082d24ec22d3824f4816ac)
186039bd3SAndrea Arcangeli /*
286039bd3SAndrea Arcangeli  *  fs/userfaultfd.c
386039bd3SAndrea Arcangeli  *
486039bd3SAndrea Arcangeli  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
586039bd3SAndrea Arcangeli  *  Copyright (C) 2008-2009 Red Hat, Inc.
686039bd3SAndrea Arcangeli  *  Copyright (C) 2015  Red Hat, Inc.
786039bd3SAndrea Arcangeli  *
886039bd3SAndrea Arcangeli  *  This work is licensed under the terms of the GNU GPL, version 2. See
986039bd3SAndrea Arcangeli  *  the COPYING file in the top-level directory.
1086039bd3SAndrea Arcangeli  *
1186039bd3SAndrea Arcangeli  *  Some part derived from fs/eventfd.c (anon inode setup) and
1286039bd3SAndrea Arcangeli  *  mm/ksm.c (mm hashing).
1386039bd3SAndrea Arcangeli  */
1486039bd3SAndrea Arcangeli 
159cd75c3cSPavel Emelyanov #include <linux/list.h>
1686039bd3SAndrea Arcangeli #include <linux/hashtable.h>
1786039bd3SAndrea Arcangeli #include <linux/sched.h>
1886039bd3SAndrea Arcangeli #include <linux/mm.h>
1986039bd3SAndrea Arcangeli #include <linux/poll.h>
2086039bd3SAndrea Arcangeli #include <linux/slab.h>
2186039bd3SAndrea Arcangeli #include <linux/seq_file.h>
2286039bd3SAndrea Arcangeli #include <linux/file.h>
2386039bd3SAndrea Arcangeli #include <linux/bug.h>
2486039bd3SAndrea Arcangeli #include <linux/anon_inodes.h>
2586039bd3SAndrea Arcangeli #include <linux/syscalls.h>
2686039bd3SAndrea Arcangeli #include <linux/userfaultfd_k.h>
2786039bd3SAndrea Arcangeli #include <linux/mempolicy.h>
2886039bd3SAndrea Arcangeli #include <linux/ioctl.h>
2986039bd3SAndrea Arcangeli #include <linux/security.h>
3086039bd3SAndrea Arcangeli 
313004ec9cSAndrea Arcangeli static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
323004ec9cSAndrea Arcangeli 
3386039bd3SAndrea Arcangeli enum userfaultfd_state {
3486039bd3SAndrea Arcangeli 	UFFD_STATE_WAIT_API,
3586039bd3SAndrea Arcangeli 	UFFD_STATE_RUNNING,
3686039bd3SAndrea Arcangeli };
3786039bd3SAndrea Arcangeli 
383004ec9cSAndrea Arcangeli /*
393004ec9cSAndrea Arcangeli  * Start with fault_pending_wqh and fault_wqh so they're more likely
403004ec9cSAndrea Arcangeli  * to be in the same cacheline.
413004ec9cSAndrea Arcangeli  */
4286039bd3SAndrea Arcangeli struct userfaultfd_ctx {
4315b726efSAndrea Arcangeli 	/* waitqueue head for the pending (i.e. not read) userfaults */
4415b726efSAndrea Arcangeli 	wait_queue_head_t fault_pending_wqh;
4515b726efSAndrea Arcangeli 	/* waitqueue head for the userfaults */
4686039bd3SAndrea Arcangeli 	wait_queue_head_t fault_wqh;
4786039bd3SAndrea Arcangeli 	/* waitqueue head for the pseudo fd to wakeup poll/read */
4886039bd3SAndrea Arcangeli 	wait_queue_head_t fd_wqh;
499cd75c3cSPavel Emelyanov 	/* waitqueue head for events */
509cd75c3cSPavel Emelyanov 	wait_queue_head_t event_wqh;
512c5b7e1bSAndrea Arcangeli 	/* a refile sequence protected by fault_pending_wqh lock */
522c5b7e1bSAndrea Arcangeli 	struct seqcount refile_seq;
533004ec9cSAndrea Arcangeli 	/* pseudo fd refcounting */
543004ec9cSAndrea Arcangeli 	atomic_t refcount;
5586039bd3SAndrea Arcangeli 	/* userfaultfd syscall flags */
5686039bd3SAndrea Arcangeli 	unsigned int flags;
579cd75c3cSPavel Emelyanov 	/* features requested from the userspace */
589cd75c3cSPavel Emelyanov 	unsigned int features;
5986039bd3SAndrea Arcangeli 	/* state machine */
6086039bd3SAndrea Arcangeli 	enum userfaultfd_state state;
6186039bd3SAndrea Arcangeli 	/* released */
6286039bd3SAndrea Arcangeli 	bool released;
6386039bd3SAndrea Arcangeli 	/* mm with one ore more vmas attached to this userfaultfd_ctx */
6486039bd3SAndrea Arcangeli 	struct mm_struct *mm;
6586039bd3SAndrea Arcangeli };
6686039bd3SAndrea Arcangeli 
67893e26e6SPavel Emelyanov struct userfaultfd_fork_ctx {
68893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *orig;
69893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *new;
70893e26e6SPavel Emelyanov 	struct list_head list;
71893e26e6SPavel Emelyanov };
72893e26e6SPavel Emelyanov 
7386039bd3SAndrea Arcangeli struct userfaultfd_wait_queue {
74a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
7586039bd3SAndrea Arcangeli 	wait_queue_t wq;
7686039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
7715a77c6fSAndrea Arcangeli 	bool waken;
7886039bd3SAndrea Arcangeli };
7986039bd3SAndrea Arcangeli 
8086039bd3SAndrea Arcangeli struct userfaultfd_wake_range {
8186039bd3SAndrea Arcangeli 	unsigned long start;
8286039bd3SAndrea Arcangeli 	unsigned long len;
8386039bd3SAndrea Arcangeli };
8486039bd3SAndrea Arcangeli 
8586039bd3SAndrea Arcangeli static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
8686039bd3SAndrea Arcangeli 				     int wake_flags, void *key)
8786039bd3SAndrea Arcangeli {
8886039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range *range = key;
8986039bd3SAndrea Arcangeli 	int ret;
9086039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
9186039bd3SAndrea Arcangeli 	unsigned long start, len;
9286039bd3SAndrea Arcangeli 
9386039bd3SAndrea Arcangeli 	uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
9486039bd3SAndrea Arcangeli 	ret = 0;
9586039bd3SAndrea Arcangeli 	/* len == 0 means wake all */
9686039bd3SAndrea Arcangeli 	start = range->start;
9786039bd3SAndrea Arcangeli 	len = range->len;
98a9b85f94SAndrea Arcangeli 	if (len && (start > uwq->msg.arg.pagefault.address ||
99a9b85f94SAndrea Arcangeli 		    start + len <= uwq->msg.arg.pagefault.address))
10086039bd3SAndrea Arcangeli 		goto out;
10115a77c6fSAndrea Arcangeli 	WRITE_ONCE(uwq->waken, true);
10215a77c6fSAndrea Arcangeli 	/*
10315a77c6fSAndrea Arcangeli 	 * The implicit smp_mb__before_spinlock in try_to_wake_up()
10415a77c6fSAndrea Arcangeli 	 * renders uwq->waken visible to other CPUs before the task is
10515a77c6fSAndrea Arcangeli 	 * waken.
10615a77c6fSAndrea Arcangeli 	 */
10786039bd3SAndrea Arcangeli 	ret = wake_up_state(wq->private, mode);
10886039bd3SAndrea Arcangeli 	if (ret)
10986039bd3SAndrea Arcangeli 		/*
11086039bd3SAndrea Arcangeli 		 * Wake only once, autoremove behavior.
11186039bd3SAndrea Arcangeli 		 *
11286039bd3SAndrea Arcangeli 		 * After the effect of list_del_init is visible to the
11386039bd3SAndrea Arcangeli 		 * other CPUs, the waitqueue may disappear from under
11486039bd3SAndrea Arcangeli 		 * us, see the !list_empty_careful() in
11586039bd3SAndrea Arcangeli 		 * handle_userfault(). try_to_wake_up() has an
11686039bd3SAndrea Arcangeli 		 * implicit smp_mb__before_spinlock, and the
11786039bd3SAndrea Arcangeli 		 * wq->private is read before calling the extern
11886039bd3SAndrea Arcangeli 		 * function "wake_up_state" (which in turns calls
11986039bd3SAndrea Arcangeli 		 * try_to_wake_up). While the spin_lock;spin_unlock;
12086039bd3SAndrea Arcangeli 		 * wouldn't be enough, the smp_mb__before_spinlock is
12186039bd3SAndrea Arcangeli 		 * enough to avoid an explicit smp_mb() here.
12286039bd3SAndrea Arcangeli 		 */
12386039bd3SAndrea Arcangeli 		list_del_init(&wq->task_list);
12486039bd3SAndrea Arcangeli out:
12586039bd3SAndrea Arcangeli 	return ret;
12686039bd3SAndrea Arcangeli }
12786039bd3SAndrea Arcangeli 
12886039bd3SAndrea Arcangeli /**
12986039bd3SAndrea Arcangeli  * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
13086039bd3SAndrea Arcangeli  * context.
13186039bd3SAndrea Arcangeli  * @ctx: [in] Pointer to the userfaultfd context.
13286039bd3SAndrea Arcangeli  *
13386039bd3SAndrea Arcangeli  * Returns: In case of success, returns not zero.
13486039bd3SAndrea Arcangeli  */
13586039bd3SAndrea Arcangeli static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
13686039bd3SAndrea Arcangeli {
13786039bd3SAndrea Arcangeli 	if (!atomic_inc_not_zero(&ctx->refcount))
13886039bd3SAndrea Arcangeli 		BUG();
13986039bd3SAndrea Arcangeli }
14086039bd3SAndrea Arcangeli 
14186039bd3SAndrea Arcangeli /**
14286039bd3SAndrea Arcangeli  * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
14386039bd3SAndrea Arcangeli  * context.
14486039bd3SAndrea Arcangeli  * @ctx: [in] Pointer to userfaultfd context.
14586039bd3SAndrea Arcangeli  *
14686039bd3SAndrea Arcangeli  * The userfaultfd context reference must have been previously acquired either
14786039bd3SAndrea Arcangeli  * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
14886039bd3SAndrea Arcangeli  */
14986039bd3SAndrea Arcangeli static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
15086039bd3SAndrea Arcangeli {
15186039bd3SAndrea Arcangeli 	if (atomic_dec_and_test(&ctx->refcount)) {
15286039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
15386039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
15486039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
15586039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
1569cd75c3cSPavel Emelyanov 		VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
1579cd75c3cSPavel Emelyanov 		VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
15886039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
15986039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
160d2005e3fSOleg Nesterov 		mmdrop(ctx->mm);
1613004ec9cSAndrea Arcangeli 		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
16286039bd3SAndrea Arcangeli 	}
16386039bd3SAndrea Arcangeli }
16486039bd3SAndrea Arcangeli 
165a9b85f94SAndrea Arcangeli static inline void msg_init(struct uffd_msg *msg)
166a9b85f94SAndrea Arcangeli {
167a9b85f94SAndrea Arcangeli 	BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
168a9b85f94SAndrea Arcangeli 	/*
169a9b85f94SAndrea Arcangeli 	 * Must use memset to zero out the paddings or kernel data is
170a9b85f94SAndrea Arcangeli 	 * leaked to userland.
171a9b85f94SAndrea Arcangeli 	 */
172a9b85f94SAndrea Arcangeli 	memset(msg, 0, sizeof(struct uffd_msg));
173a9b85f94SAndrea Arcangeli }
174a9b85f94SAndrea Arcangeli 
175a9b85f94SAndrea Arcangeli static inline struct uffd_msg userfault_msg(unsigned long address,
17686039bd3SAndrea Arcangeli 					    unsigned int flags,
17786039bd3SAndrea Arcangeli 					    unsigned long reason)
17886039bd3SAndrea Arcangeli {
179a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
180a9b85f94SAndrea Arcangeli 	msg_init(&msg);
181a9b85f94SAndrea Arcangeli 	msg.event = UFFD_EVENT_PAGEFAULT;
182a9b85f94SAndrea Arcangeli 	msg.arg.pagefault.address = address;
18386039bd3SAndrea Arcangeli 	if (flags & FAULT_FLAG_WRITE)
18486039bd3SAndrea Arcangeli 		/*
185a4605a61SAndrea Arcangeli 		 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
186a9b85f94SAndrea Arcangeli 		 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
187a9b85f94SAndrea Arcangeli 		 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
188a9b85f94SAndrea Arcangeli 		 * was a read fault, otherwise if set it means it's
189a9b85f94SAndrea Arcangeli 		 * a write fault.
19086039bd3SAndrea Arcangeli 		 */
191a9b85f94SAndrea Arcangeli 		msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
19286039bd3SAndrea Arcangeli 	if (reason & VM_UFFD_WP)
19386039bd3SAndrea Arcangeli 		/*
194a9b85f94SAndrea Arcangeli 		 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
195a9b85f94SAndrea Arcangeli 		 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
196a9b85f94SAndrea Arcangeli 		 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
197a9b85f94SAndrea Arcangeli 		 * a missing fault, otherwise if set it means it's a
198a9b85f94SAndrea Arcangeli 		 * write protect fault.
19986039bd3SAndrea Arcangeli 		 */
200a9b85f94SAndrea Arcangeli 		msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
201a9b85f94SAndrea Arcangeli 	return msg;
20286039bd3SAndrea Arcangeli }
20386039bd3SAndrea Arcangeli 
20486039bd3SAndrea Arcangeli /*
2058d2afd96SAndrea Arcangeli  * Verify the pagetables are still not ok after having reigstered into
2068d2afd96SAndrea Arcangeli  * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
2078d2afd96SAndrea Arcangeli  * userfault that has already been resolved, if userfaultfd_read and
2088d2afd96SAndrea Arcangeli  * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
2098d2afd96SAndrea Arcangeli  * threads.
2108d2afd96SAndrea Arcangeli  */
2118d2afd96SAndrea Arcangeli static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
2128d2afd96SAndrea Arcangeli 					 unsigned long address,
2138d2afd96SAndrea Arcangeli 					 unsigned long flags,
2148d2afd96SAndrea Arcangeli 					 unsigned long reason)
2158d2afd96SAndrea Arcangeli {
2168d2afd96SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
2178d2afd96SAndrea Arcangeli 	pgd_t *pgd;
2188d2afd96SAndrea Arcangeli 	pud_t *pud;
2198d2afd96SAndrea Arcangeli 	pmd_t *pmd, _pmd;
2208d2afd96SAndrea Arcangeli 	pte_t *pte;
2218d2afd96SAndrea Arcangeli 	bool ret = true;
2228d2afd96SAndrea Arcangeli 
2238d2afd96SAndrea Arcangeli 	VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
2248d2afd96SAndrea Arcangeli 
2258d2afd96SAndrea Arcangeli 	pgd = pgd_offset(mm, address);
2268d2afd96SAndrea Arcangeli 	if (!pgd_present(*pgd))
2278d2afd96SAndrea Arcangeli 		goto out;
2288d2afd96SAndrea Arcangeli 	pud = pud_offset(pgd, address);
2298d2afd96SAndrea Arcangeli 	if (!pud_present(*pud))
2308d2afd96SAndrea Arcangeli 		goto out;
2318d2afd96SAndrea Arcangeli 	pmd = pmd_offset(pud, address);
2328d2afd96SAndrea Arcangeli 	/*
2338d2afd96SAndrea Arcangeli 	 * READ_ONCE must function as a barrier with narrower scope
2348d2afd96SAndrea Arcangeli 	 * and it must be equivalent to:
2358d2afd96SAndrea Arcangeli 	 *	_pmd = *pmd; barrier();
2368d2afd96SAndrea Arcangeli 	 *
2378d2afd96SAndrea Arcangeli 	 * This is to deal with the instability (as in
2388d2afd96SAndrea Arcangeli 	 * pmd_trans_unstable) of the pmd.
2398d2afd96SAndrea Arcangeli 	 */
2408d2afd96SAndrea Arcangeli 	_pmd = READ_ONCE(*pmd);
2418d2afd96SAndrea Arcangeli 	if (!pmd_present(_pmd))
2428d2afd96SAndrea Arcangeli 		goto out;
2438d2afd96SAndrea Arcangeli 
2448d2afd96SAndrea Arcangeli 	ret = false;
2458d2afd96SAndrea Arcangeli 	if (pmd_trans_huge(_pmd))
2468d2afd96SAndrea Arcangeli 		goto out;
2478d2afd96SAndrea Arcangeli 
2488d2afd96SAndrea Arcangeli 	/*
2498d2afd96SAndrea Arcangeli 	 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
2508d2afd96SAndrea Arcangeli 	 * and use the standard pte_offset_map() instead of parsing _pmd.
2518d2afd96SAndrea Arcangeli 	 */
2528d2afd96SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
2538d2afd96SAndrea Arcangeli 	/*
2548d2afd96SAndrea Arcangeli 	 * Lockless access: we're in a wait_event so it's ok if it
2558d2afd96SAndrea Arcangeli 	 * changes under us.
2568d2afd96SAndrea Arcangeli 	 */
2578d2afd96SAndrea Arcangeli 	if (pte_none(*pte))
2588d2afd96SAndrea Arcangeli 		ret = true;
2598d2afd96SAndrea Arcangeli 	pte_unmap(pte);
2608d2afd96SAndrea Arcangeli 
2618d2afd96SAndrea Arcangeli out:
2628d2afd96SAndrea Arcangeli 	return ret;
2638d2afd96SAndrea Arcangeli }
2648d2afd96SAndrea Arcangeli 
2658d2afd96SAndrea Arcangeli /*
26686039bd3SAndrea Arcangeli  * The locking rules involved in returning VM_FAULT_RETRY depending on
26786039bd3SAndrea Arcangeli  * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
26886039bd3SAndrea Arcangeli  * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
26986039bd3SAndrea Arcangeli  * recommendation in __lock_page_or_retry is not an understatement.
27086039bd3SAndrea Arcangeli  *
27186039bd3SAndrea Arcangeli  * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
27286039bd3SAndrea Arcangeli  * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
27386039bd3SAndrea Arcangeli  * not set.
27486039bd3SAndrea Arcangeli  *
27586039bd3SAndrea Arcangeli  * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
27686039bd3SAndrea Arcangeli  * set, VM_FAULT_RETRY can still be returned if and only if there are
27786039bd3SAndrea Arcangeli  * fatal_signal_pending()s, and the mmap_sem must be released before
27886039bd3SAndrea Arcangeli  * returning it.
27986039bd3SAndrea Arcangeli  */
28082b0f8c3SJan Kara int handle_userfault(struct vm_fault *vmf, unsigned long reason)
28186039bd3SAndrea Arcangeli {
28282b0f8c3SJan Kara 	struct mm_struct *mm = vmf->vma->vm_mm;
28386039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
28486039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue uwq;
285ba85c702SAndrea Arcangeli 	int ret;
286dfa37dc3SAndrea Arcangeli 	bool must_wait, return_to_userland;
28715a77c6fSAndrea Arcangeli 	long blocking_state;
28886039bd3SAndrea Arcangeli 
28986039bd3SAndrea Arcangeli 	BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
29086039bd3SAndrea Arcangeli 
291ba85c702SAndrea Arcangeli 	ret = VM_FAULT_SIGBUS;
29282b0f8c3SJan Kara 	ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
29386039bd3SAndrea Arcangeli 	if (!ctx)
294ba85c702SAndrea Arcangeli 		goto out;
29586039bd3SAndrea Arcangeli 
29686039bd3SAndrea Arcangeli 	BUG_ON(ctx->mm != mm);
29786039bd3SAndrea Arcangeli 
29886039bd3SAndrea Arcangeli 	VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
29986039bd3SAndrea Arcangeli 	VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
30086039bd3SAndrea Arcangeli 
30186039bd3SAndrea Arcangeli 	/*
30286039bd3SAndrea Arcangeli 	 * If it's already released don't get it. This avoids to loop
30386039bd3SAndrea Arcangeli 	 * in __get_user_pages if userfaultfd_release waits on the
30486039bd3SAndrea Arcangeli 	 * caller of handle_userfault to release the mmap_sem.
30586039bd3SAndrea Arcangeli 	 */
30686039bd3SAndrea Arcangeli 	if (unlikely(ACCESS_ONCE(ctx->released)))
307ba85c702SAndrea Arcangeli 		goto out;
30886039bd3SAndrea Arcangeli 
30986039bd3SAndrea Arcangeli 	/*
31039680f50SLinus Torvalds 	 * We don't do userfault handling for the final child pid update.
31139680f50SLinus Torvalds 	 */
31239680f50SLinus Torvalds 	if (current->flags & PF_EXITING)
31339680f50SLinus Torvalds 		goto out;
31439680f50SLinus Torvalds 
31539680f50SLinus Torvalds 	/*
31686039bd3SAndrea Arcangeli 	 * Check that we can return VM_FAULT_RETRY.
31786039bd3SAndrea Arcangeli 	 *
31886039bd3SAndrea Arcangeli 	 * NOTE: it should become possible to return VM_FAULT_RETRY
31986039bd3SAndrea Arcangeli 	 * even if FAULT_FLAG_TRIED is set without leading to gup()
32086039bd3SAndrea Arcangeli 	 * -EBUSY failures, if the userfaultfd is to be extended for
32186039bd3SAndrea Arcangeli 	 * VM_UFFD_WP tracking and we intend to arm the userfault
32286039bd3SAndrea Arcangeli 	 * without first stopping userland access to the memory. For
32386039bd3SAndrea Arcangeli 	 * VM_UFFD_MISSING userfaults this is enough for now.
32486039bd3SAndrea Arcangeli 	 */
32582b0f8c3SJan Kara 	if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
32686039bd3SAndrea Arcangeli 		/*
32786039bd3SAndrea Arcangeli 		 * Validate the invariant that nowait must allow retry
32886039bd3SAndrea Arcangeli 		 * to be sure not to return SIGBUS erroneously on
32986039bd3SAndrea Arcangeli 		 * nowait invocations.
33086039bd3SAndrea Arcangeli 		 */
33182b0f8c3SJan Kara 		BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
33286039bd3SAndrea Arcangeli #ifdef CONFIG_DEBUG_VM
33386039bd3SAndrea Arcangeli 		if (printk_ratelimit()) {
33486039bd3SAndrea Arcangeli 			printk(KERN_WARNING
33582b0f8c3SJan Kara 			       "FAULT_FLAG_ALLOW_RETRY missing %x\n",
33682b0f8c3SJan Kara 			       vmf->flags);
33786039bd3SAndrea Arcangeli 			dump_stack();
33886039bd3SAndrea Arcangeli 		}
33986039bd3SAndrea Arcangeli #endif
340ba85c702SAndrea Arcangeli 		goto out;
34186039bd3SAndrea Arcangeli 	}
34286039bd3SAndrea Arcangeli 
34386039bd3SAndrea Arcangeli 	/*
34486039bd3SAndrea Arcangeli 	 * Handle nowait, not much to do other than tell it to retry
34586039bd3SAndrea Arcangeli 	 * and wait.
34686039bd3SAndrea Arcangeli 	 */
347ba85c702SAndrea Arcangeli 	ret = VM_FAULT_RETRY;
34882b0f8c3SJan Kara 	if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
349ba85c702SAndrea Arcangeli 		goto out;
35086039bd3SAndrea Arcangeli 
35186039bd3SAndrea Arcangeli 	/* take the reference before dropping the mmap_sem */
35286039bd3SAndrea Arcangeli 	userfaultfd_ctx_get(ctx);
35386039bd3SAndrea Arcangeli 
35486039bd3SAndrea Arcangeli 	init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
35586039bd3SAndrea Arcangeli 	uwq.wq.private = current;
35682b0f8c3SJan Kara 	uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
35786039bd3SAndrea Arcangeli 	uwq.ctx = ctx;
35815a77c6fSAndrea Arcangeli 	uwq.waken = false;
35986039bd3SAndrea Arcangeli 
360bae473a4SKirill A. Shutemov 	return_to_userland =
36182b0f8c3SJan Kara 		(vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
362dfa37dc3SAndrea Arcangeli 		(FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
36315a77c6fSAndrea Arcangeli 	blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
36415a77c6fSAndrea Arcangeli 			 TASK_KILLABLE;
365dfa37dc3SAndrea Arcangeli 
36615b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
36786039bd3SAndrea Arcangeli 	/*
36886039bd3SAndrea Arcangeli 	 * After the __add_wait_queue the uwq is visible to userland
36986039bd3SAndrea Arcangeli 	 * through poll/read().
37086039bd3SAndrea Arcangeli 	 */
37115b726efSAndrea Arcangeli 	__add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
37215b726efSAndrea Arcangeli 	/*
37315b726efSAndrea Arcangeli 	 * The smp_mb() after __set_current_state prevents the reads
37415b726efSAndrea Arcangeli 	 * following the spin_unlock to happen before the list_add in
37515b726efSAndrea Arcangeli 	 * __add_wait_queue.
37615b726efSAndrea Arcangeli 	 */
37715a77c6fSAndrea Arcangeli 	set_current_state(blocking_state);
37815b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
37986039bd3SAndrea Arcangeli 
38082b0f8c3SJan Kara 	must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
38182b0f8c3SJan Kara 					  reason);
3828d2afd96SAndrea Arcangeli 	up_read(&mm->mmap_sem);
3838d2afd96SAndrea Arcangeli 
3848d2afd96SAndrea Arcangeli 	if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
385dfa37dc3SAndrea Arcangeli 		   (return_to_userland ? !signal_pending(current) :
386dfa37dc3SAndrea Arcangeli 		    !fatal_signal_pending(current)))) {
38786039bd3SAndrea Arcangeli 		wake_up_poll(&ctx->fd_wqh, POLLIN);
38886039bd3SAndrea Arcangeli 		schedule();
389ba85c702SAndrea Arcangeli 		ret |= VM_FAULT_MAJOR;
39015a77c6fSAndrea Arcangeli 
39115a77c6fSAndrea Arcangeli 		/*
39215a77c6fSAndrea Arcangeli 		 * False wakeups can orginate even from rwsem before
39315a77c6fSAndrea Arcangeli 		 * up_read() however userfaults will wait either for a
39415a77c6fSAndrea Arcangeli 		 * targeted wakeup on the specific uwq waitqueue from
39515a77c6fSAndrea Arcangeli 		 * wake_userfault() or for signals or for uffd
39615a77c6fSAndrea Arcangeli 		 * release.
39715a77c6fSAndrea Arcangeli 		 */
39815a77c6fSAndrea Arcangeli 		while (!READ_ONCE(uwq.waken)) {
39915a77c6fSAndrea Arcangeli 			/*
40015a77c6fSAndrea Arcangeli 			 * This needs the full smp_store_mb()
40115a77c6fSAndrea Arcangeli 			 * guarantee as the state write must be
40215a77c6fSAndrea Arcangeli 			 * visible to other CPUs before reading
40315a77c6fSAndrea Arcangeli 			 * uwq.waken from other CPUs.
40415a77c6fSAndrea Arcangeli 			 */
40515a77c6fSAndrea Arcangeli 			set_current_state(blocking_state);
40615a77c6fSAndrea Arcangeli 			if (READ_ONCE(uwq.waken) ||
40715a77c6fSAndrea Arcangeli 			    READ_ONCE(ctx->released) ||
40815a77c6fSAndrea Arcangeli 			    (return_to_userland ? signal_pending(current) :
40915a77c6fSAndrea Arcangeli 			     fatal_signal_pending(current)))
41015a77c6fSAndrea Arcangeli 				break;
41115a77c6fSAndrea Arcangeli 			schedule();
41215a77c6fSAndrea Arcangeli 		}
41386039bd3SAndrea Arcangeli 	}
414ba85c702SAndrea Arcangeli 
41586039bd3SAndrea Arcangeli 	__set_current_state(TASK_RUNNING);
41615b726efSAndrea Arcangeli 
417dfa37dc3SAndrea Arcangeli 	if (return_to_userland) {
418dfa37dc3SAndrea Arcangeli 		if (signal_pending(current) &&
419dfa37dc3SAndrea Arcangeli 		    !fatal_signal_pending(current)) {
420dfa37dc3SAndrea Arcangeli 			/*
421dfa37dc3SAndrea Arcangeli 			 * If we got a SIGSTOP or SIGCONT and this is
422dfa37dc3SAndrea Arcangeli 			 * a normal userland page fault, just let
423dfa37dc3SAndrea Arcangeli 			 * userland return so the signal will be
424dfa37dc3SAndrea Arcangeli 			 * handled and gdb debugging works.  The page
425dfa37dc3SAndrea Arcangeli 			 * fault code immediately after we return from
426dfa37dc3SAndrea Arcangeli 			 * this function is going to release the
427dfa37dc3SAndrea Arcangeli 			 * mmap_sem and it's not depending on it
428dfa37dc3SAndrea Arcangeli 			 * (unlike gup would if we were not to return
429dfa37dc3SAndrea Arcangeli 			 * VM_FAULT_RETRY).
430dfa37dc3SAndrea Arcangeli 			 *
431dfa37dc3SAndrea Arcangeli 			 * If a fatal signal is pending we still take
432dfa37dc3SAndrea Arcangeli 			 * the streamlined VM_FAULT_RETRY failure path
433dfa37dc3SAndrea Arcangeli 			 * and there's no need to retake the mmap_sem
434dfa37dc3SAndrea Arcangeli 			 * in such case.
435dfa37dc3SAndrea Arcangeli 			 */
436dfa37dc3SAndrea Arcangeli 			down_read(&mm->mmap_sem);
437dfa37dc3SAndrea Arcangeli 			ret = 0;
438dfa37dc3SAndrea Arcangeli 		}
439dfa37dc3SAndrea Arcangeli 	}
440dfa37dc3SAndrea Arcangeli 
44115b726efSAndrea Arcangeli 	/*
44215b726efSAndrea Arcangeli 	 * Here we race with the list_del; list_add in
44315b726efSAndrea Arcangeli 	 * userfaultfd_ctx_read(), however because we don't ever run
44415b726efSAndrea Arcangeli 	 * list_del_init() to refile across the two lists, the prev
44515b726efSAndrea Arcangeli 	 * and next pointers will never point to self. list_add also
44615b726efSAndrea Arcangeli 	 * would never let any of the two pointers to point to
44715b726efSAndrea Arcangeli 	 * self. So list_empty_careful won't risk to see both pointers
44815b726efSAndrea Arcangeli 	 * pointing to self at any time during the list refile. The
44915b726efSAndrea Arcangeli 	 * only case where list_del_init() is called is the full
45015b726efSAndrea Arcangeli 	 * removal in the wake function and there we don't re-list_add
45115b726efSAndrea Arcangeli 	 * and it's fine not to block on the spinlock. The uwq on this
45215b726efSAndrea Arcangeli 	 * kernel stack can be released after the list_del_init.
45315b726efSAndrea Arcangeli 	 */
454ba85c702SAndrea Arcangeli 	if (!list_empty_careful(&uwq.wq.task_list)) {
45515b726efSAndrea Arcangeli 		spin_lock(&ctx->fault_pending_wqh.lock);
45615b726efSAndrea Arcangeli 		/*
45715b726efSAndrea Arcangeli 		 * No need of list_del_init(), the uwq on the stack
45815b726efSAndrea Arcangeli 		 * will be freed shortly anyway.
45915b726efSAndrea Arcangeli 		 */
46015b726efSAndrea Arcangeli 		list_del(&uwq.wq.task_list);
46115b726efSAndrea Arcangeli 		spin_unlock(&ctx->fault_pending_wqh.lock);
462ba85c702SAndrea Arcangeli 	}
46386039bd3SAndrea Arcangeli 
46486039bd3SAndrea Arcangeli 	/*
46586039bd3SAndrea Arcangeli 	 * ctx may go away after this if the userfault pseudo fd is
46686039bd3SAndrea Arcangeli 	 * already released.
46786039bd3SAndrea Arcangeli 	 */
46886039bd3SAndrea Arcangeli 	userfaultfd_ctx_put(ctx);
46986039bd3SAndrea Arcangeli 
470ba85c702SAndrea Arcangeli out:
471ba85c702SAndrea Arcangeli 	return ret;
47286039bd3SAndrea Arcangeli }
47386039bd3SAndrea Arcangeli 
474893e26e6SPavel Emelyanov static int userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
4759cd75c3cSPavel Emelyanov 					     struct userfaultfd_wait_queue *ewq)
4769cd75c3cSPavel Emelyanov {
4779cd75c3cSPavel Emelyanov 	int ret = 0;
4789cd75c3cSPavel Emelyanov 
4799cd75c3cSPavel Emelyanov 	ewq->ctx = ctx;
4809cd75c3cSPavel Emelyanov 	init_waitqueue_entry(&ewq->wq, current);
4819cd75c3cSPavel Emelyanov 
4829cd75c3cSPavel Emelyanov 	spin_lock(&ctx->event_wqh.lock);
4839cd75c3cSPavel Emelyanov 	/*
4849cd75c3cSPavel Emelyanov 	 * After the __add_wait_queue the uwq is visible to userland
4859cd75c3cSPavel Emelyanov 	 * through poll/read().
4869cd75c3cSPavel Emelyanov 	 */
4879cd75c3cSPavel Emelyanov 	__add_wait_queue(&ctx->event_wqh, &ewq->wq);
4889cd75c3cSPavel Emelyanov 	for (;;) {
4899cd75c3cSPavel Emelyanov 		set_current_state(TASK_KILLABLE);
4909cd75c3cSPavel Emelyanov 		if (ewq->msg.event == 0)
4919cd75c3cSPavel Emelyanov 			break;
4929cd75c3cSPavel Emelyanov 		if (ACCESS_ONCE(ctx->released) ||
4939cd75c3cSPavel Emelyanov 		    fatal_signal_pending(current)) {
4949cd75c3cSPavel Emelyanov 			ret = -1;
4959cd75c3cSPavel Emelyanov 			__remove_wait_queue(&ctx->event_wqh, &ewq->wq);
4969cd75c3cSPavel Emelyanov 			break;
4979cd75c3cSPavel Emelyanov 		}
4989cd75c3cSPavel Emelyanov 
4999cd75c3cSPavel Emelyanov 		spin_unlock(&ctx->event_wqh.lock);
5009cd75c3cSPavel Emelyanov 
5019cd75c3cSPavel Emelyanov 		wake_up_poll(&ctx->fd_wqh, POLLIN);
5029cd75c3cSPavel Emelyanov 		schedule();
5039cd75c3cSPavel Emelyanov 
5049cd75c3cSPavel Emelyanov 		spin_lock(&ctx->event_wqh.lock);
5059cd75c3cSPavel Emelyanov 	}
5069cd75c3cSPavel Emelyanov 	__set_current_state(TASK_RUNNING);
5079cd75c3cSPavel Emelyanov 	spin_unlock(&ctx->event_wqh.lock);
5089cd75c3cSPavel Emelyanov 
5099cd75c3cSPavel Emelyanov 	/*
5109cd75c3cSPavel Emelyanov 	 * ctx may go away after this if the userfault pseudo fd is
5119cd75c3cSPavel Emelyanov 	 * already released.
5129cd75c3cSPavel Emelyanov 	 */
5139cd75c3cSPavel Emelyanov 
5149cd75c3cSPavel Emelyanov 	userfaultfd_ctx_put(ctx);
5159cd75c3cSPavel Emelyanov 	return ret;
5169cd75c3cSPavel Emelyanov }
5179cd75c3cSPavel Emelyanov 
5189cd75c3cSPavel Emelyanov static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
5199cd75c3cSPavel Emelyanov 				       struct userfaultfd_wait_queue *ewq)
5209cd75c3cSPavel Emelyanov {
5219cd75c3cSPavel Emelyanov 	ewq->msg.event = 0;
5229cd75c3cSPavel Emelyanov 	wake_up_locked(&ctx->event_wqh);
5239cd75c3cSPavel Emelyanov 	__remove_wait_queue(&ctx->event_wqh, &ewq->wq);
5249cd75c3cSPavel Emelyanov }
5259cd75c3cSPavel Emelyanov 
526893e26e6SPavel Emelyanov int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
527893e26e6SPavel Emelyanov {
528893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *ctx = NULL, *octx;
529893e26e6SPavel Emelyanov 	struct userfaultfd_fork_ctx *fctx;
530893e26e6SPavel Emelyanov 
531893e26e6SPavel Emelyanov 	octx = vma->vm_userfaultfd_ctx.ctx;
532893e26e6SPavel Emelyanov 	if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
533893e26e6SPavel Emelyanov 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
534893e26e6SPavel Emelyanov 		vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
535893e26e6SPavel Emelyanov 		return 0;
536893e26e6SPavel Emelyanov 	}
537893e26e6SPavel Emelyanov 
538893e26e6SPavel Emelyanov 	list_for_each_entry(fctx, fcs, list)
539893e26e6SPavel Emelyanov 		if (fctx->orig == octx) {
540893e26e6SPavel Emelyanov 			ctx = fctx->new;
541893e26e6SPavel Emelyanov 			break;
542893e26e6SPavel Emelyanov 		}
543893e26e6SPavel Emelyanov 
544893e26e6SPavel Emelyanov 	if (!ctx) {
545893e26e6SPavel Emelyanov 		fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
546893e26e6SPavel Emelyanov 		if (!fctx)
547893e26e6SPavel Emelyanov 			return -ENOMEM;
548893e26e6SPavel Emelyanov 
549893e26e6SPavel Emelyanov 		ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
550893e26e6SPavel Emelyanov 		if (!ctx) {
551893e26e6SPavel Emelyanov 			kfree(fctx);
552893e26e6SPavel Emelyanov 			return -ENOMEM;
553893e26e6SPavel Emelyanov 		}
554893e26e6SPavel Emelyanov 
555893e26e6SPavel Emelyanov 		atomic_set(&ctx->refcount, 1);
556893e26e6SPavel Emelyanov 		ctx->flags = octx->flags;
557893e26e6SPavel Emelyanov 		ctx->state = UFFD_STATE_RUNNING;
558893e26e6SPavel Emelyanov 		ctx->features = octx->features;
559893e26e6SPavel Emelyanov 		ctx->released = false;
560893e26e6SPavel Emelyanov 		ctx->mm = vma->vm_mm;
561d3aadc8eSMike Rapoport 		atomic_inc(&ctx->mm->mm_count);
562893e26e6SPavel Emelyanov 
563893e26e6SPavel Emelyanov 		userfaultfd_ctx_get(octx);
564893e26e6SPavel Emelyanov 		fctx->orig = octx;
565893e26e6SPavel Emelyanov 		fctx->new = ctx;
566893e26e6SPavel Emelyanov 		list_add_tail(&fctx->list, fcs);
567893e26e6SPavel Emelyanov 	}
568893e26e6SPavel Emelyanov 
569893e26e6SPavel Emelyanov 	vma->vm_userfaultfd_ctx.ctx = ctx;
570893e26e6SPavel Emelyanov 	return 0;
571893e26e6SPavel Emelyanov }
572893e26e6SPavel Emelyanov 
573893e26e6SPavel Emelyanov static int dup_fctx(struct userfaultfd_fork_ctx *fctx)
574893e26e6SPavel Emelyanov {
575893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *ctx = fctx->orig;
576893e26e6SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
577893e26e6SPavel Emelyanov 
578893e26e6SPavel Emelyanov 	msg_init(&ewq.msg);
579893e26e6SPavel Emelyanov 
580893e26e6SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_FORK;
581893e26e6SPavel Emelyanov 	ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
582893e26e6SPavel Emelyanov 
583893e26e6SPavel Emelyanov 	return userfaultfd_event_wait_completion(ctx, &ewq);
584893e26e6SPavel Emelyanov }
585893e26e6SPavel Emelyanov 
586893e26e6SPavel Emelyanov void dup_userfaultfd_complete(struct list_head *fcs)
587893e26e6SPavel Emelyanov {
588893e26e6SPavel Emelyanov 	int ret = 0;
589893e26e6SPavel Emelyanov 	struct userfaultfd_fork_ctx *fctx, *n;
590893e26e6SPavel Emelyanov 
591893e26e6SPavel Emelyanov 	list_for_each_entry_safe(fctx, n, fcs, list) {
592893e26e6SPavel Emelyanov 		if (!ret)
593893e26e6SPavel Emelyanov 			ret = dup_fctx(fctx);
594893e26e6SPavel Emelyanov 		list_del(&fctx->list);
595893e26e6SPavel Emelyanov 		kfree(fctx);
596893e26e6SPavel Emelyanov 	}
597893e26e6SPavel Emelyanov }
598893e26e6SPavel Emelyanov 
59972f87654SPavel Emelyanov void mremap_userfaultfd_prep(struct vm_area_struct *vma,
60072f87654SPavel Emelyanov 			     struct vm_userfaultfd_ctx *vm_ctx)
60172f87654SPavel Emelyanov {
60272f87654SPavel Emelyanov 	struct userfaultfd_ctx *ctx;
60372f87654SPavel Emelyanov 
60472f87654SPavel Emelyanov 	ctx = vma->vm_userfaultfd_ctx.ctx;
60572f87654SPavel Emelyanov 	if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
60672f87654SPavel Emelyanov 		vm_ctx->ctx = ctx;
60772f87654SPavel Emelyanov 		userfaultfd_ctx_get(ctx);
60872f87654SPavel Emelyanov 	}
60972f87654SPavel Emelyanov }
61072f87654SPavel Emelyanov 
61190794bf1SAndrea Arcangeli void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
61272f87654SPavel Emelyanov 				 unsigned long from, unsigned long to,
61372f87654SPavel Emelyanov 				 unsigned long len)
61472f87654SPavel Emelyanov {
61590794bf1SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = vm_ctx->ctx;
61672f87654SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
61772f87654SPavel Emelyanov 
61872f87654SPavel Emelyanov 	if (!ctx)
61972f87654SPavel Emelyanov 		return;
62072f87654SPavel Emelyanov 
62172f87654SPavel Emelyanov 	if (to & ~PAGE_MASK) {
62272f87654SPavel Emelyanov 		userfaultfd_ctx_put(ctx);
62372f87654SPavel Emelyanov 		return;
62472f87654SPavel Emelyanov 	}
62572f87654SPavel Emelyanov 
62672f87654SPavel Emelyanov 	msg_init(&ewq.msg);
62772f87654SPavel Emelyanov 
62872f87654SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_REMAP;
62972f87654SPavel Emelyanov 	ewq.msg.arg.remap.from = from;
63072f87654SPavel Emelyanov 	ewq.msg.arg.remap.to = to;
63172f87654SPavel Emelyanov 	ewq.msg.arg.remap.len = len;
63272f87654SPavel Emelyanov 
63372f87654SPavel Emelyanov 	userfaultfd_event_wait_completion(ctx, &ewq);
63472f87654SPavel Emelyanov }
63572f87654SPavel Emelyanov 
636*05ce7724SPavel Emelyanov void madvise_userfault_dontneed(struct vm_area_struct *vma,
637*05ce7724SPavel Emelyanov 				struct vm_area_struct **prev,
638*05ce7724SPavel Emelyanov 				unsigned long start, unsigned long end)
639*05ce7724SPavel Emelyanov {
640*05ce7724SPavel Emelyanov 	struct mm_struct *mm = vma->vm_mm;
641*05ce7724SPavel Emelyanov 	struct userfaultfd_ctx *ctx;
642*05ce7724SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
643*05ce7724SPavel Emelyanov 
644*05ce7724SPavel Emelyanov 	ctx = vma->vm_userfaultfd_ctx.ctx;
645*05ce7724SPavel Emelyanov 	if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_MADVDONTNEED))
646*05ce7724SPavel Emelyanov 		return;
647*05ce7724SPavel Emelyanov 
648*05ce7724SPavel Emelyanov 	userfaultfd_ctx_get(ctx);
649*05ce7724SPavel Emelyanov 	up_read(&mm->mmap_sem);
650*05ce7724SPavel Emelyanov 
651*05ce7724SPavel Emelyanov 	*prev = NULL; /* We wait for ACK w/o the mmap semaphore */
652*05ce7724SPavel Emelyanov 
653*05ce7724SPavel Emelyanov 	msg_init(&ewq.msg);
654*05ce7724SPavel Emelyanov 
655*05ce7724SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_MADVDONTNEED;
656*05ce7724SPavel Emelyanov 	ewq.msg.arg.madv_dn.start = start;
657*05ce7724SPavel Emelyanov 	ewq.msg.arg.madv_dn.end = end;
658*05ce7724SPavel Emelyanov 
659*05ce7724SPavel Emelyanov 	userfaultfd_event_wait_completion(ctx, &ewq);
660*05ce7724SPavel Emelyanov 
661*05ce7724SPavel Emelyanov 	down_read(&mm->mmap_sem);
662*05ce7724SPavel Emelyanov }
663*05ce7724SPavel Emelyanov 
66486039bd3SAndrea Arcangeli static int userfaultfd_release(struct inode *inode, struct file *file)
66586039bd3SAndrea Arcangeli {
66686039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
66786039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
66886039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev;
66986039bd3SAndrea Arcangeli 	/* len == 0 means wake all */
67086039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range range = { .len = 0, };
67186039bd3SAndrea Arcangeli 	unsigned long new_flags;
67286039bd3SAndrea Arcangeli 
67386039bd3SAndrea Arcangeli 	ACCESS_ONCE(ctx->released) = true;
67486039bd3SAndrea Arcangeli 
675d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
676d2005e3fSOleg Nesterov 		goto wakeup;
677d2005e3fSOleg Nesterov 
67886039bd3SAndrea Arcangeli 	/*
67986039bd3SAndrea Arcangeli 	 * Flush page faults out of all CPUs. NOTE: all page faults
68086039bd3SAndrea Arcangeli 	 * must be retried without returning VM_FAULT_SIGBUS if
68186039bd3SAndrea Arcangeli 	 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
68286039bd3SAndrea Arcangeli 	 * changes while handle_userfault released the mmap_sem. So
68386039bd3SAndrea Arcangeli 	 * it's critical that released is set to true (above), before
68486039bd3SAndrea Arcangeli 	 * taking the mmap_sem for writing.
68586039bd3SAndrea Arcangeli 	 */
68686039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
68786039bd3SAndrea Arcangeli 	prev = NULL;
68886039bd3SAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
68986039bd3SAndrea Arcangeli 		cond_resched();
69086039bd3SAndrea Arcangeli 		BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
69186039bd3SAndrea Arcangeli 		       !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
69286039bd3SAndrea Arcangeli 		if (vma->vm_userfaultfd_ctx.ctx != ctx) {
69386039bd3SAndrea Arcangeli 			prev = vma;
69486039bd3SAndrea Arcangeli 			continue;
69586039bd3SAndrea Arcangeli 		}
69686039bd3SAndrea Arcangeli 		new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
69786039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
69886039bd3SAndrea Arcangeli 				 new_flags, vma->anon_vma,
69986039bd3SAndrea Arcangeli 				 vma->vm_file, vma->vm_pgoff,
70086039bd3SAndrea Arcangeli 				 vma_policy(vma),
70186039bd3SAndrea Arcangeli 				 NULL_VM_UFFD_CTX);
70286039bd3SAndrea Arcangeli 		if (prev)
70386039bd3SAndrea Arcangeli 			vma = prev;
70486039bd3SAndrea Arcangeli 		else
70586039bd3SAndrea Arcangeli 			prev = vma;
70686039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
70786039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
70886039bd3SAndrea Arcangeli 	}
70986039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
710d2005e3fSOleg Nesterov 	mmput(mm);
711d2005e3fSOleg Nesterov wakeup:
71286039bd3SAndrea Arcangeli 	/*
71315b726efSAndrea Arcangeli 	 * After no new page faults can wait on this fault_*wqh, flush
71486039bd3SAndrea Arcangeli 	 * the last page faults that may have been already waiting on
71515b726efSAndrea Arcangeli 	 * the fault_*wqh.
71686039bd3SAndrea Arcangeli 	 */
71715b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
718ac5be6b4SAndrea Arcangeli 	__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
719ac5be6b4SAndrea Arcangeli 	__wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
72015b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
72186039bd3SAndrea Arcangeli 
72286039bd3SAndrea Arcangeli 	wake_up_poll(&ctx->fd_wqh, POLLHUP);
72386039bd3SAndrea Arcangeli 	userfaultfd_ctx_put(ctx);
72486039bd3SAndrea Arcangeli 	return 0;
72586039bd3SAndrea Arcangeli }
72686039bd3SAndrea Arcangeli 
72715b726efSAndrea Arcangeli /* fault_pending_wqh.lock must be hold by the caller */
7286dcc27fdSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault_in(
7296dcc27fdSPavel Emelyanov 		wait_queue_head_t *wqh)
73086039bd3SAndrea Arcangeli {
73186039bd3SAndrea Arcangeli 	wait_queue_t *wq;
73215b726efSAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
73386039bd3SAndrea Arcangeli 
7346dcc27fdSPavel Emelyanov 	VM_BUG_ON(!spin_is_locked(&wqh->lock));
73586039bd3SAndrea Arcangeli 
73615b726efSAndrea Arcangeli 	uwq = NULL;
7376dcc27fdSPavel Emelyanov 	if (!waitqueue_active(wqh))
73815b726efSAndrea Arcangeli 		goto out;
73915b726efSAndrea Arcangeli 	/* walk in reverse to provide FIFO behavior to read userfaults */
7406dcc27fdSPavel Emelyanov 	wq = list_last_entry(&wqh->task_list, typeof(*wq), task_list);
74115b726efSAndrea Arcangeli 	uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
74215b726efSAndrea Arcangeli out:
74315b726efSAndrea Arcangeli 	return uwq;
74486039bd3SAndrea Arcangeli }
74586039bd3SAndrea Arcangeli 
7466dcc27fdSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault(
7476dcc27fdSPavel Emelyanov 		struct userfaultfd_ctx *ctx)
7486dcc27fdSPavel Emelyanov {
7496dcc27fdSPavel Emelyanov 	return find_userfault_in(&ctx->fault_pending_wqh);
7506dcc27fdSPavel Emelyanov }
7516dcc27fdSPavel Emelyanov 
7529cd75c3cSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault_evt(
7539cd75c3cSPavel Emelyanov 		struct userfaultfd_ctx *ctx)
7549cd75c3cSPavel Emelyanov {
7559cd75c3cSPavel Emelyanov 	return find_userfault_in(&ctx->event_wqh);
7569cd75c3cSPavel Emelyanov }
7579cd75c3cSPavel Emelyanov 
75886039bd3SAndrea Arcangeli static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
75986039bd3SAndrea Arcangeli {
76086039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
76186039bd3SAndrea Arcangeli 	unsigned int ret;
76286039bd3SAndrea Arcangeli 
76386039bd3SAndrea Arcangeli 	poll_wait(file, &ctx->fd_wqh, wait);
76486039bd3SAndrea Arcangeli 
76586039bd3SAndrea Arcangeli 	switch (ctx->state) {
76686039bd3SAndrea Arcangeli 	case UFFD_STATE_WAIT_API:
76786039bd3SAndrea Arcangeli 		return POLLERR;
76886039bd3SAndrea Arcangeli 	case UFFD_STATE_RUNNING:
769ba85c702SAndrea Arcangeli 		/*
770ba85c702SAndrea Arcangeli 		 * poll() never guarantees that read won't block.
771ba85c702SAndrea Arcangeli 		 * userfaults can be waken before they're read().
772ba85c702SAndrea Arcangeli 		 */
773ba85c702SAndrea Arcangeli 		if (unlikely(!(file->f_flags & O_NONBLOCK)))
774ba85c702SAndrea Arcangeli 			return POLLERR;
77515b726efSAndrea Arcangeli 		/*
77615b726efSAndrea Arcangeli 		 * lockless access to see if there are pending faults
77715b726efSAndrea Arcangeli 		 * __pollwait last action is the add_wait_queue but
77815b726efSAndrea Arcangeli 		 * the spin_unlock would allow the waitqueue_active to
77915b726efSAndrea Arcangeli 		 * pass above the actual list_add inside
78015b726efSAndrea Arcangeli 		 * add_wait_queue critical section. So use a full
78115b726efSAndrea Arcangeli 		 * memory barrier to serialize the list_add write of
78215b726efSAndrea Arcangeli 		 * add_wait_queue() with the waitqueue_active read
78315b726efSAndrea Arcangeli 		 * below.
78415b726efSAndrea Arcangeli 		 */
78515b726efSAndrea Arcangeli 		ret = 0;
78615b726efSAndrea Arcangeli 		smp_mb();
78715b726efSAndrea Arcangeli 		if (waitqueue_active(&ctx->fault_pending_wqh))
78815b726efSAndrea Arcangeli 			ret = POLLIN;
7899cd75c3cSPavel Emelyanov 		else if (waitqueue_active(&ctx->event_wqh))
7909cd75c3cSPavel Emelyanov 			ret = POLLIN;
7919cd75c3cSPavel Emelyanov 
79286039bd3SAndrea Arcangeli 		return ret;
79386039bd3SAndrea Arcangeli 	default:
7948474901aSAndrea Arcangeli 		WARN_ON_ONCE(1);
7958474901aSAndrea Arcangeli 		return POLLERR;
79686039bd3SAndrea Arcangeli 	}
79786039bd3SAndrea Arcangeli }
79886039bd3SAndrea Arcangeli 
799893e26e6SPavel Emelyanov static const struct file_operations userfaultfd_fops;
800893e26e6SPavel Emelyanov 
801893e26e6SPavel Emelyanov static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
802893e26e6SPavel Emelyanov 				  struct userfaultfd_ctx *new,
803893e26e6SPavel Emelyanov 				  struct uffd_msg *msg)
804893e26e6SPavel Emelyanov {
805893e26e6SPavel Emelyanov 	int fd;
806893e26e6SPavel Emelyanov 	struct file *file;
807893e26e6SPavel Emelyanov 	unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
808893e26e6SPavel Emelyanov 
809893e26e6SPavel Emelyanov 	fd = get_unused_fd_flags(flags);
810893e26e6SPavel Emelyanov 	if (fd < 0)
811893e26e6SPavel Emelyanov 		return fd;
812893e26e6SPavel Emelyanov 
813893e26e6SPavel Emelyanov 	file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
814893e26e6SPavel Emelyanov 				  O_RDWR | flags);
815893e26e6SPavel Emelyanov 	if (IS_ERR(file)) {
816893e26e6SPavel Emelyanov 		put_unused_fd(fd);
817893e26e6SPavel Emelyanov 		return PTR_ERR(file);
818893e26e6SPavel Emelyanov 	}
819893e26e6SPavel Emelyanov 
820893e26e6SPavel Emelyanov 	fd_install(fd, file);
821893e26e6SPavel Emelyanov 	msg->arg.reserved.reserved1 = 0;
822893e26e6SPavel Emelyanov 	msg->arg.fork.ufd = fd;
823893e26e6SPavel Emelyanov 
824893e26e6SPavel Emelyanov 	return 0;
825893e26e6SPavel Emelyanov }
826893e26e6SPavel Emelyanov 
82786039bd3SAndrea Arcangeli static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
828a9b85f94SAndrea Arcangeli 				    struct uffd_msg *msg)
82986039bd3SAndrea Arcangeli {
83086039bd3SAndrea Arcangeli 	ssize_t ret;
83186039bd3SAndrea Arcangeli 	DECLARE_WAITQUEUE(wait, current);
83215b726efSAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
833893e26e6SPavel Emelyanov 	/*
834893e26e6SPavel Emelyanov 	 * Handling fork event requires sleeping operations, so
835893e26e6SPavel Emelyanov 	 * we drop the event_wqh lock, then do these ops, then
836893e26e6SPavel Emelyanov 	 * lock it back and wake up the waiter. While the lock is
837893e26e6SPavel Emelyanov 	 * dropped the ewq may go away so we keep track of it
838893e26e6SPavel Emelyanov 	 * carefully.
839893e26e6SPavel Emelyanov 	 */
840893e26e6SPavel Emelyanov 	LIST_HEAD(fork_event);
841893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *fork_nctx = NULL;
84286039bd3SAndrea Arcangeli 
84315b726efSAndrea Arcangeli 	/* always take the fd_wqh lock before the fault_pending_wqh lock */
84486039bd3SAndrea Arcangeli 	spin_lock(&ctx->fd_wqh.lock);
84586039bd3SAndrea Arcangeli 	__add_wait_queue(&ctx->fd_wqh, &wait);
84686039bd3SAndrea Arcangeli 	for (;;) {
84786039bd3SAndrea Arcangeli 		set_current_state(TASK_INTERRUPTIBLE);
84815b726efSAndrea Arcangeli 		spin_lock(&ctx->fault_pending_wqh.lock);
84915b726efSAndrea Arcangeli 		uwq = find_userfault(ctx);
85015b726efSAndrea Arcangeli 		if (uwq) {
85186039bd3SAndrea Arcangeli 			/*
8522c5b7e1bSAndrea Arcangeli 			 * Use a seqcount to repeat the lockless check
8532c5b7e1bSAndrea Arcangeli 			 * in wake_userfault() to avoid missing
8542c5b7e1bSAndrea Arcangeli 			 * wakeups because during the refile both
8552c5b7e1bSAndrea Arcangeli 			 * waitqueue could become empty if this is the
8562c5b7e1bSAndrea Arcangeli 			 * only userfault.
8572c5b7e1bSAndrea Arcangeli 			 */
8582c5b7e1bSAndrea Arcangeli 			write_seqcount_begin(&ctx->refile_seq);
8592c5b7e1bSAndrea Arcangeli 
8602c5b7e1bSAndrea Arcangeli 			/*
86115b726efSAndrea Arcangeli 			 * The fault_pending_wqh.lock prevents the uwq
86215b726efSAndrea Arcangeli 			 * to disappear from under us.
86315b726efSAndrea Arcangeli 			 *
86415b726efSAndrea Arcangeli 			 * Refile this userfault from
86515b726efSAndrea Arcangeli 			 * fault_pending_wqh to fault_wqh, it's not
86615b726efSAndrea Arcangeli 			 * pending anymore after we read it.
86715b726efSAndrea Arcangeli 			 *
86815b726efSAndrea Arcangeli 			 * Use list_del() by hand (as
86915b726efSAndrea Arcangeli 			 * userfaultfd_wake_function also uses
87015b726efSAndrea Arcangeli 			 * list_del_init() by hand) to be sure nobody
87115b726efSAndrea Arcangeli 			 * changes __remove_wait_queue() to use
87215b726efSAndrea Arcangeli 			 * list_del_init() in turn breaking the
87315b726efSAndrea Arcangeli 			 * !list_empty_careful() check in
87415b726efSAndrea Arcangeli 			 * handle_userfault(). The uwq->wq.task_list
87515b726efSAndrea Arcangeli 			 * must never be empty at any time during the
87615b726efSAndrea Arcangeli 			 * refile, or the waitqueue could disappear
87715b726efSAndrea Arcangeli 			 * from under us. The "wait_queue_head_t"
87815b726efSAndrea Arcangeli 			 * parameter of __remove_wait_queue() is unused
87915b726efSAndrea Arcangeli 			 * anyway.
88086039bd3SAndrea Arcangeli 			 */
88115b726efSAndrea Arcangeli 			list_del(&uwq->wq.task_list);
88215b726efSAndrea Arcangeli 			__add_wait_queue(&ctx->fault_wqh, &uwq->wq);
88315b726efSAndrea Arcangeli 
8842c5b7e1bSAndrea Arcangeli 			write_seqcount_end(&ctx->refile_seq);
8852c5b7e1bSAndrea Arcangeli 
886a9b85f94SAndrea Arcangeli 			/* careful to always initialize msg if ret == 0 */
887a9b85f94SAndrea Arcangeli 			*msg = uwq->msg;
88815b726efSAndrea Arcangeli 			spin_unlock(&ctx->fault_pending_wqh.lock);
88986039bd3SAndrea Arcangeli 			ret = 0;
89086039bd3SAndrea Arcangeli 			break;
89186039bd3SAndrea Arcangeli 		}
89215b726efSAndrea Arcangeli 		spin_unlock(&ctx->fault_pending_wqh.lock);
8939cd75c3cSPavel Emelyanov 
8949cd75c3cSPavel Emelyanov 		spin_lock(&ctx->event_wqh.lock);
8959cd75c3cSPavel Emelyanov 		uwq = find_userfault_evt(ctx);
8969cd75c3cSPavel Emelyanov 		if (uwq) {
8979cd75c3cSPavel Emelyanov 			*msg = uwq->msg;
8989cd75c3cSPavel Emelyanov 
899893e26e6SPavel Emelyanov 			if (uwq->msg.event == UFFD_EVENT_FORK) {
900893e26e6SPavel Emelyanov 				fork_nctx = (struct userfaultfd_ctx *)
901893e26e6SPavel Emelyanov 					(unsigned long)
902893e26e6SPavel Emelyanov 					uwq->msg.arg.reserved.reserved1;
903893e26e6SPavel Emelyanov 				list_move(&uwq->wq.task_list, &fork_event);
904893e26e6SPavel Emelyanov 				spin_unlock(&ctx->event_wqh.lock);
905893e26e6SPavel Emelyanov 				ret = 0;
906893e26e6SPavel Emelyanov 				break;
907893e26e6SPavel Emelyanov 			}
908893e26e6SPavel Emelyanov 
9099cd75c3cSPavel Emelyanov 			userfaultfd_event_complete(ctx, uwq);
9109cd75c3cSPavel Emelyanov 			spin_unlock(&ctx->event_wqh.lock);
9119cd75c3cSPavel Emelyanov 			ret = 0;
9129cd75c3cSPavel Emelyanov 			break;
9139cd75c3cSPavel Emelyanov 		}
9149cd75c3cSPavel Emelyanov 		spin_unlock(&ctx->event_wqh.lock);
9159cd75c3cSPavel Emelyanov 
91686039bd3SAndrea Arcangeli 		if (signal_pending(current)) {
91786039bd3SAndrea Arcangeli 			ret = -ERESTARTSYS;
91886039bd3SAndrea Arcangeli 			break;
91986039bd3SAndrea Arcangeli 		}
92086039bd3SAndrea Arcangeli 		if (no_wait) {
92186039bd3SAndrea Arcangeli 			ret = -EAGAIN;
92286039bd3SAndrea Arcangeli 			break;
92386039bd3SAndrea Arcangeli 		}
92486039bd3SAndrea Arcangeli 		spin_unlock(&ctx->fd_wqh.lock);
92586039bd3SAndrea Arcangeli 		schedule();
92686039bd3SAndrea Arcangeli 		spin_lock(&ctx->fd_wqh.lock);
92786039bd3SAndrea Arcangeli 	}
92886039bd3SAndrea Arcangeli 	__remove_wait_queue(&ctx->fd_wqh, &wait);
92986039bd3SAndrea Arcangeli 	__set_current_state(TASK_RUNNING);
93086039bd3SAndrea Arcangeli 	spin_unlock(&ctx->fd_wqh.lock);
93186039bd3SAndrea Arcangeli 
932893e26e6SPavel Emelyanov 	if (!ret && msg->event == UFFD_EVENT_FORK) {
933893e26e6SPavel Emelyanov 		ret = resolve_userfault_fork(ctx, fork_nctx, msg);
934893e26e6SPavel Emelyanov 
935893e26e6SPavel Emelyanov 		if (!ret) {
936893e26e6SPavel Emelyanov 			spin_lock(&ctx->event_wqh.lock);
937893e26e6SPavel Emelyanov 			if (!list_empty(&fork_event)) {
938893e26e6SPavel Emelyanov 				uwq = list_first_entry(&fork_event,
939893e26e6SPavel Emelyanov 						       typeof(*uwq),
940893e26e6SPavel Emelyanov 						       wq.task_list);
941893e26e6SPavel Emelyanov 				list_del(&uwq->wq.task_list);
942893e26e6SPavel Emelyanov 				__add_wait_queue(&ctx->event_wqh, &uwq->wq);
943893e26e6SPavel Emelyanov 				userfaultfd_event_complete(ctx, uwq);
944893e26e6SPavel Emelyanov 			}
945893e26e6SPavel Emelyanov 			spin_unlock(&ctx->event_wqh.lock);
946893e26e6SPavel Emelyanov 		}
947893e26e6SPavel Emelyanov 	}
948893e26e6SPavel Emelyanov 
94986039bd3SAndrea Arcangeli 	return ret;
95086039bd3SAndrea Arcangeli }
95186039bd3SAndrea Arcangeli 
95286039bd3SAndrea Arcangeli static ssize_t userfaultfd_read(struct file *file, char __user *buf,
95386039bd3SAndrea Arcangeli 				size_t count, loff_t *ppos)
95486039bd3SAndrea Arcangeli {
95586039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
95686039bd3SAndrea Arcangeli 	ssize_t _ret, ret = 0;
957a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
95886039bd3SAndrea Arcangeli 	int no_wait = file->f_flags & O_NONBLOCK;
95986039bd3SAndrea Arcangeli 
96086039bd3SAndrea Arcangeli 	if (ctx->state == UFFD_STATE_WAIT_API)
96186039bd3SAndrea Arcangeli 		return -EINVAL;
96286039bd3SAndrea Arcangeli 
96386039bd3SAndrea Arcangeli 	for (;;) {
964a9b85f94SAndrea Arcangeli 		if (count < sizeof(msg))
96586039bd3SAndrea Arcangeli 			return ret ? ret : -EINVAL;
966a9b85f94SAndrea Arcangeli 		_ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
96786039bd3SAndrea Arcangeli 		if (_ret < 0)
96886039bd3SAndrea Arcangeli 			return ret ? ret : _ret;
969a9b85f94SAndrea Arcangeli 		if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
97086039bd3SAndrea Arcangeli 			return ret ? ret : -EFAULT;
971a9b85f94SAndrea Arcangeli 		ret += sizeof(msg);
972a9b85f94SAndrea Arcangeli 		buf += sizeof(msg);
973a9b85f94SAndrea Arcangeli 		count -= sizeof(msg);
97486039bd3SAndrea Arcangeli 		/*
97586039bd3SAndrea Arcangeli 		 * Allow to read more than one fault at time but only
97686039bd3SAndrea Arcangeli 		 * block if waiting for the very first one.
97786039bd3SAndrea Arcangeli 		 */
97886039bd3SAndrea Arcangeli 		no_wait = O_NONBLOCK;
97986039bd3SAndrea Arcangeli 	}
98086039bd3SAndrea Arcangeli }
98186039bd3SAndrea Arcangeli 
98286039bd3SAndrea Arcangeli static void __wake_userfault(struct userfaultfd_ctx *ctx,
98386039bd3SAndrea Arcangeli 			     struct userfaultfd_wake_range *range)
98486039bd3SAndrea Arcangeli {
98586039bd3SAndrea Arcangeli 	unsigned long start, end;
98686039bd3SAndrea Arcangeli 
98786039bd3SAndrea Arcangeli 	start = range->start;
98886039bd3SAndrea Arcangeli 	end = range->start + range->len;
98986039bd3SAndrea Arcangeli 
99015b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
99186039bd3SAndrea Arcangeli 	/* wake all in the range and autoremove */
99215b726efSAndrea Arcangeli 	if (waitqueue_active(&ctx->fault_pending_wqh))
993ac5be6b4SAndrea Arcangeli 		__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
99415b726efSAndrea Arcangeli 				     range);
99515b726efSAndrea Arcangeli 	if (waitqueue_active(&ctx->fault_wqh))
996ac5be6b4SAndrea Arcangeli 		__wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
99715b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
99886039bd3SAndrea Arcangeli }
99986039bd3SAndrea Arcangeli 
100086039bd3SAndrea Arcangeli static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
100186039bd3SAndrea Arcangeli 					   struct userfaultfd_wake_range *range)
100286039bd3SAndrea Arcangeli {
10032c5b7e1bSAndrea Arcangeli 	unsigned seq;
10042c5b7e1bSAndrea Arcangeli 	bool need_wakeup;
10052c5b7e1bSAndrea Arcangeli 
100686039bd3SAndrea Arcangeli 	/*
100786039bd3SAndrea Arcangeli 	 * To be sure waitqueue_active() is not reordered by the CPU
100886039bd3SAndrea Arcangeli 	 * before the pagetable update, use an explicit SMP memory
100986039bd3SAndrea Arcangeli 	 * barrier here. PT lock release or up_read(mmap_sem) still
101086039bd3SAndrea Arcangeli 	 * have release semantics that can allow the
101186039bd3SAndrea Arcangeli 	 * waitqueue_active() to be reordered before the pte update.
101286039bd3SAndrea Arcangeli 	 */
101386039bd3SAndrea Arcangeli 	smp_mb();
101486039bd3SAndrea Arcangeli 
101586039bd3SAndrea Arcangeli 	/*
101686039bd3SAndrea Arcangeli 	 * Use waitqueue_active because it's very frequent to
101786039bd3SAndrea Arcangeli 	 * change the address space atomically even if there are no
101886039bd3SAndrea Arcangeli 	 * userfaults yet. So we take the spinlock only when we're
101986039bd3SAndrea Arcangeli 	 * sure we've userfaults to wake.
102086039bd3SAndrea Arcangeli 	 */
10212c5b7e1bSAndrea Arcangeli 	do {
10222c5b7e1bSAndrea Arcangeli 		seq = read_seqcount_begin(&ctx->refile_seq);
10232c5b7e1bSAndrea Arcangeli 		need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
10242c5b7e1bSAndrea Arcangeli 			waitqueue_active(&ctx->fault_wqh);
10252c5b7e1bSAndrea Arcangeli 		cond_resched();
10262c5b7e1bSAndrea Arcangeli 	} while (read_seqcount_retry(&ctx->refile_seq, seq));
10272c5b7e1bSAndrea Arcangeli 	if (need_wakeup)
102886039bd3SAndrea Arcangeli 		__wake_userfault(ctx, range);
102986039bd3SAndrea Arcangeli }
103086039bd3SAndrea Arcangeli 
103186039bd3SAndrea Arcangeli static __always_inline int validate_range(struct mm_struct *mm,
103286039bd3SAndrea Arcangeli 					  __u64 start, __u64 len)
103386039bd3SAndrea Arcangeli {
103486039bd3SAndrea Arcangeli 	__u64 task_size = mm->task_size;
103586039bd3SAndrea Arcangeli 
103686039bd3SAndrea Arcangeli 	if (start & ~PAGE_MASK)
103786039bd3SAndrea Arcangeli 		return -EINVAL;
103886039bd3SAndrea Arcangeli 	if (len & ~PAGE_MASK)
103986039bd3SAndrea Arcangeli 		return -EINVAL;
104086039bd3SAndrea Arcangeli 	if (!len)
104186039bd3SAndrea Arcangeli 		return -EINVAL;
104286039bd3SAndrea Arcangeli 	if (start < mmap_min_addr)
104386039bd3SAndrea Arcangeli 		return -EINVAL;
104486039bd3SAndrea Arcangeli 	if (start >= task_size)
104586039bd3SAndrea Arcangeli 		return -EINVAL;
104686039bd3SAndrea Arcangeli 	if (len > task_size - start)
104786039bd3SAndrea Arcangeli 		return -EINVAL;
104886039bd3SAndrea Arcangeli 	return 0;
104986039bd3SAndrea Arcangeli }
105086039bd3SAndrea Arcangeli 
105186039bd3SAndrea Arcangeli static int userfaultfd_register(struct userfaultfd_ctx *ctx,
105286039bd3SAndrea Arcangeli 				unsigned long arg)
105386039bd3SAndrea Arcangeli {
105486039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
105586039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev, *cur;
105686039bd3SAndrea Arcangeli 	int ret;
105786039bd3SAndrea Arcangeli 	struct uffdio_register uffdio_register;
105886039bd3SAndrea Arcangeli 	struct uffdio_register __user *user_uffdio_register;
105986039bd3SAndrea Arcangeli 	unsigned long vm_flags, new_flags;
106086039bd3SAndrea Arcangeli 	bool found;
106186039bd3SAndrea Arcangeli 	unsigned long start, end, vma_end;
106286039bd3SAndrea Arcangeli 
106386039bd3SAndrea Arcangeli 	user_uffdio_register = (struct uffdio_register __user *) arg;
106486039bd3SAndrea Arcangeli 
106586039bd3SAndrea Arcangeli 	ret = -EFAULT;
106686039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_register, user_uffdio_register,
106786039bd3SAndrea Arcangeli 			   sizeof(uffdio_register)-sizeof(__u64)))
106886039bd3SAndrea Arcangeli 		goto out;
106986039bd3SAndrea Arcangeli 
107086039bd3SAndrea Arcangeli 	ret = -EINVAL;
107186039bd3SAndrea Arcangeli 	if (!uffdio_register.mode)
107286039bd3SAndrea Arcangeli 		goto out;
107386039bd3SAndrea Arcangeli 	if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
107486039bd3SAndrea Arcangeli 				     UFFDIO_REGISTER_MODE_WP))
107586039bd3SAndrea Arcangeli 		goto out;
107686039bd3SAndrea Arcangeli 	vm_flags = 0;
107786039bd3SAndrea Arcangeli 	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
107886039bd3SAndrea Arcangeli 		vm_flags |= VM_UFFD_MISSING;
107986039bd3SAndrea Arcangeli 	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
108086039bd3SAndrea Arcangeli 		vm_flags |= VM_UFFD_WP;
108186039bd3SAndrea Arcangeli 		/*
108286039bd3SAndrea Arcangeli 		 * FIXME: remove the below error constraint by
108386039bd3SAndrea Arcangeli 		 * implementing the wprotect tracking mode.
108486039bd3SAndrea Arcangeli 		 */
108586039bd3SAndrea Arcangeli 		ret = -EINVAL;
108686039bd3SAndrea Arcangeli 		goto out;
108786039bd3SAndrea Arcangeli 	}
108886039bd3SAndrea Arcangeli 
108986039bd3SAndrea Arcangeli 	ret = validate_range(mm, uffdio_register.range.start,
109086039bd3SAndrea Arcangeli 			     uffdio_register.range.len);
109186039bd3SAndrea Arcangeli 	if (ret)
109286039bd3SAndrea Arcangeli 		goto out;
109386039bd3SAndrea Arcangeli 
109486039bd3SAndrea Arcangeli 	start = uffdio_register.range.start;
109586039bd3SAndrea Arcangeli 	end = start + uffdio_register.range.len;
109686039bd3SAndrea Arcangeli 
1097d2005e3fSOleg Nesterov 	ret = -ENOMEM;
1098d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
1099d2005e3fSOleg Nesterov 		goto out;
1100d2005e3fSOleg Nesterov 
110186039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
110286039bd3SAndrea Arcangeli 	vma = find_vma_prev(mm, start, &prev);
110386039bd3SAndrea Arcangeli 	if (!vma)
110486039bd3SAndrea Arcangeli 		goto out_unlock;
110586039bd3SAndrea Arcangeli 
110686039bd3SAndrea Arcangeli 	/* check that there's at least one vma in the range */
110786039bd3SAndrea Arcangeli 	ret = -EINVAL;
110886039bd3SAndrea Arcangeli 	if (vma->vm_start >= end)
110986039bd3SAndrea Arcangeli 		goto out_unlock;
111086039bd3SAndrea Arcangeli 
111186039bd3SAndrea Arcangeli 	/*
111286039bd3SAndrea Arcangeli 	 * Search for not compatible vmas.
111386039bd3SAndrea Arcangeli 	 *
111486039bd3SAndrea Arcangeli 	 * FIXME: this shall be relaxed later so that it doesn't fail
111586039bd3SAndrea Arcangeli 	 * on tmpfs backed vmas (in addition to the current allowance
111686039bd3SAndrea Arcangeli 	 * on anonymous vmas).
111786039bd3SAndrea Arcangeli 	 */
111886039bd3SAndrea Arcangeli 	found = false;
111986039bd3SAndrea Arcangeli 	for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
112086039bd3SAndrea Arcangeli 		cond_resched();
112186039bd3SAndrea Arcangeli 
112286039bd3SAndrea Arcangeli 		BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
112386039bd3SAndrea Arcangeli 		       !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
112486039bd3SAndrea Arcangeli 
112586039bd3SAndrea Arcangeli 		/* check not compatible vmas */
112686039bd3SAndrea Arcangeli 		ret = -EINVAL;
1127a94720bfSAndrea Arcangeli 		if (!vma_is_anonymous(cur))
112886039bd3SAndrea Arcangeli 			goto out_unlock;
112986039bd3SAndrea Arcangeli 
113086039bd3SAndrea Arcangeli 		/*
113186039bd3SAndrea Arcangeli 		 * Check that this vma isn't already owned by a
113286039bd3SAndrea Arcangeli 		 * different userfaultfd. We can't allow more than one
113386039bd3SAndrea Arcangeli 		 * userfaultfd to own a single vma simultaneously or we
113486039bd3SAndrea Arcangeli 		 * wouldn't know which one to deliver the userfaults to.
113586039bd3SAndrea Arcangeli 		 */
113686039bd3SAndrea Arcangeli 		ret = -EBUSY;
113786039bd3SAndrea Arcangeli 		if (cur->vm_userfaultfd_ctx.ctx &&
113886039bd3SAndrea Arcangeli 		    cur->vm_userfaultfd_ctx.ctx != ctx)
113986039bd3SAndrea Arcangeli 			goto out_unlock;
114086039bd3SAndrea Arcangeli 
114186039bd3SAndrea Arcangeli 		found = true;
114286039bd3SAndrea Arcangeli 	}
114386039bd3SAndrea Arcangeli 	BUG_ON(!found);
114486039bd3SAndrea Arcangeli 
114586039bd3SAndrea Arcangeli 	if (vma->vm_start < start)
114686039bd3SAndrea Arcangeli 		prev = vma;
114786039bd3SAndrea Arcangeli 
114886039bd3SAndrea Arcangeli 	ret = 0;
114986039bd3SAndrea Arcangeli 	do {
115086039bd3SAndrea Arcangeli 		cond_resched();
115186039bd3SAndrea Arcangeli 
1152a94720bfSAndrea Arcangeli 		BUG_ON(!vma_is_anonymous(vma));
115386039bd3SAndrea Arcangeli 		BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
115486039bd3SAndrea Arcangeli 		       vma->vm_userfaultfd_ctx.ctx != ctx);
115586039bd3SAndrea Arcangeli 
115686039bd3SAndrea Arcangeli 		/*
115786039bd3SAndrea Arcangeli 		 * Nothing to do: this vma is already registered into this
115886039bd3SAndrea Arcangeli 		 * userfaultfd and with the right tracking mode too.
115986039bd3SAndrea Arcangeli 		 */
116086039bd3SAndrea Arcangeli 		if (vma->vm_userfaultfd_ctx.ctx == ctx &&
116186039bd3SAndrea Arcangeli 		    (vma->vm_flags & vm_flags) == vm_flags)
116286039bd3SAndrea Arcangeli 			goto skip;
116386039bd3SAndrea Arcangeli 
116486039bd3SAndrea Arcangeli 		if (vma->vm_start > start)
116586039bd3SAndrea Arcangeli 			start = vma->vm_start;
116686039bd3SAndrea Arcangeli 		vma_end = min(end, vma->vm_end);
116786039bd3SAndrea Arcangeli 
116886039bd3SAndrea Arcangeli 		new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
116986039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
117086039bd3SAndrea Arcangeli 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
117186039bd3SAndrea Arcangeli 				 vma_policy(vma),
117286039bd3SAndrea Arcangeli 				 ((struct vm_userfaultfd_ctx){ ctx }));
117386039bd3SAndrea Arcangeli 		if (prev) {
117486039bd3SAndrea Arcangeli 			vma = prev;
117586039bd3SAndrea Arcangeli 			goto next;
117686039bd3SAndrea Arcangeli 		}
117786039bd3SAndrea Arcangeli 		if (vma->vm_start < start) {
117886039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, start, 1);
117986039bd3SAndrea Arcangeli 			if (ret)
118086039bd3SAndrea Arcangeli 				break;
118186039bd3SAndrea Arcangeli 		}
118286039bd3SAndrea Arcangeli 		if (vma->vm_end > end) {
118386039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, end, 0);
118486039bd3SAndrea Arcangeli 			if (ret)
118586039bd3SAndrea Arcangeli 				break;
118686039bd3SAndrea Arcangeli 		}
118786039bd3SAndrea Arcangeli 	next:
118886039bd3SAndrea Arcangeli 		/*
118986039bd3SAndrea Arcangeli 		 * In the vma_merge() successful mprotect-like case 8:
119086039bd3SAndrea Arcangeli 		 * the next vma was merged into the current one and
119186039bd3SAndrea Arcangeli 		 * the current one has not been updated yet.
119286039bd3SAndrea Arcangeli 		 */
119386039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
119486039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx.ctx = ctx;
119586039bd3SAndrea Arcangeli 
119686039bd3SAndrea Arcangeli 	skip:
119786039bd3SAndrea Arcangeli 		prev = vma;
119886039bd3SAndrea Arcangeli 		start = vma->vm_end;
119986039bd3SAndrea Arcangeli 		vma = vma->vm_next;
120086039bd3SAndrea Arcangeli 	} while (vma && vma->vm_start < end);
120186039bd3SAndrea Arcangeli out_unlock:
120286039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
1203d2005e3fSOleg Nesterov 	mmput(mm);
120486039bd3SAndrea Arcangeli 	if (!ret) {
120586039bd3SAndrea Arcangeli 		/*
120686039bd3SAndrea Arcangeli 		 * Now that we scanned all vmas we can already tell
120786039bd3SAndrea Arcangeli 		 * userland which ioctls methods are guaranteed to
120886039bd3SAndrea Arcangeli 		 * succeed on this range.
120986039bd3SAndrea Arcangeli 		 */
121086039bd3SAndrea Arcangeli 		if (put_user(UFFD_API_RANGE_IOCTLS,
121186039bd3SAndrea Arcangeli 			     &user_uffdio_register->ioctls))
121286039bd3SAndrea Arcangeli 			ret = -EFAULT;
121386039bd3SAndrea Arcangeli 	}
121486039bd3SAndrea Arcangeli out:
121586039bd3SAndrea Arcangeli 	return ret;
121686039bd3SAndrea Arcangeli }
121786039bd3SAndrea Arcangeli 
121886039bd3SAndrea Arcangeli static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
121986039bd3SAndrea Arcangeli 				  unsigned long arg)
122086039bd3SAndrea Arcangeli {
122186039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
122286039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev, *cur;
122386039bd3SAndrea Arcangeli 	int ret;
122486039bd3SAndrea Arcangeli 	struct uffdio_range uffdio_unregister;
122586039bd3SAndrea Arcangeli 	unsigned long new_flags;
122686039bd3SAndrea Arcangeli 	bool found;
122786039bd3SAndrea Arcangeli 	unsigned long start, end, vma_end;
122886039bd3SAndrea Arcangeli 	const void __user *buf = (void __user *)arg;
122986039bd3SAndrea Arcangeli 
123086039bd3SAndrea Arcangeli 	ret = -EFAULT;
123186039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
123286039bd3SAndrea Arcangeli 		goto out;
123386039bd3SAndrea Arcangeli 
123486039bd3SAndrea Arcangeli 	ret = validate_range(mm, uffdio_unregister.start,
123586039bd3SAndrea Arcangeli 			     uffdio_unregister.len);
123686039bd3SAndrea Arcangeli 	if (ret)
123786039bd3SAndrea Arcangeli 		goto out;
123886039bd3SAndrea Arcangeli 
123986039bd3SAndrea Arcangeli 	start = uffdio_unregister.start;
124086039bd3SAndrea Arcangeli 	end = start + uffdio_unregister.len;
124186039bd3SAndrea Arcangeli 
1242d2005e3fSOleg Nesterov 	ret = -ENOMEM;
1243d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
1244d2005e3fSOleg Nesterov 		goto out;
1245d2005e3fSOleg Nesterov 
124686039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
124786039bd3SAndrea Arcangeli 	vma = find_vma_prev(mm, start, &prev);
124886039bd3SAndrea Arcangeli 	if (!vma)
124986039bd3SAndrea Arcangeli 		goto out_unlock;
125086039bd3SAndrea Arcangeli 
125186039bd3SAndrea Arcangeli 	/* check that there's at least one vma in the range */
125286039bd3SAndrea Arcangeli 	ret = -EINVAL;
125386039bd3SAndrea Arcangeli 	if (vma->vm_start >= end)
125486039bd3SAndrea Arcangeli 		goto out_unlock;
125586039bd3SAndrea Arcangeli 
125686039bd3SAndrea Arcangeli 	/*
125786039bd3SAndrea Arcangeli 	 * Search for not compatible vmas.
125886039bd3SAndrea Arcangeli 	 *
125986039bd3SAndrea Arcangeli 	 * FIXME: this shall be relaxed later so that it doesn't fail
126086039bd3SAndrea Arcangeli 	 * on tmpfs backed vmas (in addition to the current allowance
126186039bd3SAndrea Arcangeli 	 * on anonymous vmas).
126286039bd3SAndrea Arcangeli 	 */
126386039bd3SAndrea Arcangeli 	found = false;
126486039bd3SAndrea Arcangeli 	ret = -EINVAL;
126586039bd3SAndrea Arcangeli 	for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
126686039bd3SAndrea Arcangeli 		cond_resched();
126786039bd3SAndrea Arcangeli 
126886039bd3SAndrea Arcangeli 		BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
126986039bd3SAndrea Arcangeli 		       !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
127086039bd3SAndrea Arcangeli 
127186039bd3SAndrea Arcangeli 		/*
127286039bd3SAndrea Arcangeli 		 * Check not compatible vmas, not strictly required
127386039bd3SAndrea Arcangeli 		 * here as not compatible vmas cannot have an
127486039bd3SAndrea Arcangeli 		 * userfaultfd_ctx registered on them, but this
127586039bd3SAndrea Arcangeli 		 * provides for more strict behavior to notice
127686039bd3SAndrea Arcangeli 		 * unregistration errors.
127786039bd3SAndrea Arcangeli 		 */
1278a94720bfSAndrea Arcangeli 		if (!vma_is_anonymous(cur))
127986039bd3SAndrea Arcangeli 			goto out_unlock;
128086039bd3SAndrea Arcangeli 
128186039bd3SAndrea Arcangeli 		found = true;
128286039bd3SAndrea Arcangeli 	}
128386039bd3SAndrea Arcangeli 	BUG_ON(!found);
128486039bd3SAndrea Arcangeli 
128586039bd3SAndrea Arcangeli 	if (vma->vm_start < start)
128686039bd3SAndrea Arcangeli 		prev = vma;
128786039bd3SAndrea Arcangeli 
128886039bd3SAndrea Arcangeli 	ret = 0;
128986039bd3SAndrea Arcangeli 	do {
129086039bd3SAndrea Arcangeli 		cond_resched();
129186039bd3SAndrea Arcangeli 
1292a94720bfSAndrea Arcangeli 		BUG_ON(!vma_is_anonymous(vma));
129386039bd3SAndrea Arcangeli 
129486039bd3SAndrea Arcangeli 		/*
129586039bd3SAndrea Arcangeli 		 * Nothing to do: this vma is already registered into this
129686039bd3SAndrea Arcangeli 		 * userfaultfd and with the right tracking mode too.
129786039bd3SAndrea Arcangeli 		 */
129886039bd3SAndrea Arcangeli 		if (!vma->vm_userfaultfd_ctx.ctx)
129986039bd3SAndrea Arcangeli 			goto skip;
130086039bd3SAndrea Arcangeli 
130186039bd3SAndrea Arcangeli 		if (vma->vm_start > start)
130286039bd3SAndrea Arcangeli 			start = vma->vm_start;
130386039bd3SAndrea Arcangeli 		vma_end = min(end, vma->vm_end);
130486039bd3SAndrea Arcangeli 
130586039bd3SAndrea Arcangeli 		new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
130686039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
130786039bd3SAndrea Arcangeli 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
130886039bd3SAndrea Arcangeli 				 vma_policy(vma),
130986039bd3SAndrea Arcangeli 				 NULL_VM_UFFD_CTX);
131086039bd3SAndrea Arcangeli 		if (prev) {
131186039bd3SAndrea Arcangeli 			vma = prev;
131286039bd3SAndrea Arcangeli 			goto next;
131386039bd3SAndrea Arcangeli 		}
131486039bd3SAndrea Arcangeli 		if (vma->vm_start < start) {
131586039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, start, 1);
131686039bd3SAndrea Arcangeli 			if (ret)
131786039bd3SAndrea Arcangeli 				break;
131886039bd3SAndrea Arcangeli 		}
131986039bd3SAndrea Arcangeli 		if (vma->vm_end > end) {
132086039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, end, 0);
132186039bd3SAndrea Arcangeli 			if (ret)
132286039bd3SAndrea Arcangeli 				break;
132386039bd3SAndrea Arcangeli 		}
132486039bd3SAndrea Arcangeli 	next:
132586039bd3SAndrea Arcangeli 		/*
132686039bd3SAndrea Arcangeli 		 * In the vma_merge() successful mprotect-like case 8:
132786039bd3SAndrea Arcangeli 		 * the next vma was merged into the current one and
132886039bd3SAndrea Arcangeli 		 * the current one has not been updated yet.
132986039bd3SAndrea Arcangeli 		 */
133086039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
133186039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
133286039bd3SAndrea Arcangeli 
133386039bd3SAndrea Arcangeli 	skip:
133486039bd3SAndrea Arcangeli 		prev = vma;
133586039bd3SAndrea Arcangeli 		start = vma->vm_end;
133686039bd3SAndrea Arcangeli 		vma = vma->vm_next;
133786039bd3SAndrea Arcangeli 	} while (vma && vma->vm_start < end);
133886039bd3SAndrea Arcangeli out_unlock:
133986039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
1340d2005e3fSOleg Nesterov 	mmput(mm);
134186039bd3SAndrea Arcangeli out:
134286039bd3SAndrea Arcangeli 	return ret;
134386039bd3SAndrea Arcangeli }
134486039bd3SAndrea Arcangeli 
134586039bd3SAndrea Arcangeli /*
1346ba85c702SAndrea Arcangeli  * userfaultfd_wake may be used in combination with the
1347ba85c702SAndrea Arcangeli  * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
134886039bd3SAndrea Arcangeli  */
134986039bd3SAndrea Arcangeli static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
135086039bd3SAndrea Arcangeli 			    unsigned long arg)
135186039bd3SAndrea Arcangeli {
135286039bd3SAndrea Arcangeli 	int ret;
135386039bd3SAndrea Arcangeli 	struct uffdio_range uffdio_wake;
135486039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range range;
135586039bd3SAndrea Arcangeli 	const void __user *buf = (void __user *)arg;
135686039bd3SAndrea Arcangeli 
135786039bd3SAndrea Arcangeli 	ret = -EFAULT;
135886039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
135986039bd3SAndrea Arcangeli 		goto out;
136086039bd3SAndrea Arcangeli 
136186039bd3SAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
136286039bd3SAndrea Arcangeli 	if (ret)
136386039bd3SAndrea Arcangeli 		goto out;
136486039bd3SAndrea Arcangeli 
136586039bd3SAndrea Arcangeli 	range.start = uffdio_wake.start;
136686039bd3SAndrea Arcangeli 	range.len = uffdio_wake.len;
136786039bd3SAndrea Arcangeli 
136886039bd3SAndrea Arcangeli 	/*
136986039bd3SAndrea Arcangeli 	 * len == 0 means wake all and we don't want to wake all here,
137086039bd3SAndrea Arcangeli 	 * so check it again to be sure.
137186039bd3SAndrea Arcangeli 	 */
137286039bd3SAndrea Arcangeli 	VM_BUG_ON(!range.len);
137386039bd3SAndrea Arcangeli 
137486039bd3SAndrea Arcangeli 	wake_userfault(ctx, &range);
137586039bd3SAndrea Arcangeli 	ret = 0;
137686039bd3SAndrea Arcangeli 
137786039bd3SAndrea Arcangeli out:
137886039bd3SAndrea Arcangeli 	return ret;
137986039bd3SAndrea Arcangeli }
138086039bd3SAndrea Arcangeli 
1381ad465caeSAndrea Arcangeli static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1382ad465caeSAndrea Arcangeli 			    unsigned long arg)
1383ad465caeSAndrea Arcangeli {
1384ad465caeSAndrea Arcangeli 	__s64 ret;
1385ad465caeSAndrea Arcangeli 	struct uffdio_copy uffdio_copy;
1386ad465caeSAndrea Arcangeli 	struct uffdio_copy __user *user_uffdio_copy;
1387ad465caeSAndrea Arcangeli 	struct userfaultfd_wake_range range;
1388ad465caeSAndrea Arcangeli 
1389ad465caeSAndrea Arcangeli 	user_uffdio_copy = (struct uffdio_copy __user *) arg;
1390ad465caeSAndrea Arcangeli 
1391ad465caeSAndrea Arcangeli 	ret = -EFAULT;
1392ad465caeSAndrea Arcangeli 	if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1393ad465caeSAndrea Arcangeli 			   /* don't copy "copy" last field */
1394ad465caeSAndrea Arcangeli 			   sizeof(uffdio_copy)-sizeof(__s64)))
1395ad465caeSAndrea Arcangeli 		goto out;
1396ad465caeSAndrea Arcangeli 
1397ad465caeSAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1398ad465caeSAndrea Arcangeli 	if (ret)
1399ad465caeSAndrea Arcangeli 		goto out;
1400ad465caeSAndrea Arcangeli 	/*
1401ad465caeSAndrea Arcangeli 	 * double check for wraparound just in case. copy_from_user()
1402ad465caeSAndrea Arcangeli 	 * will later check uffdio_copy.src + uffdio_copy.len to fit
1403ad465caeSAndrea Arcangeli 	 * in the userland range.
1404ad465caeSAndrea Arcangeli 	 */
1405ad465caeSAndrea Arcangeli 	ret = -EINVAL;
1406ad465caeSAndrea Arcangeli 	if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1407ad465caeSAndrea Arcangeli 		goto out;
1408ad465caeSAndrea Arcangeli 	if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1409ad465caeSAndrea Arcangeli 		goto out;
1410d2005e3fSOleg Nesterov 	if (mmget_not_zero(ctx->mm)) {
1411ad465caeSAndrea Arcangeli 		ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1412ad465caeSAndrea Arcangeli 				   uffdio_copy.len);
1413d2005e3fSOleg Nesterov 		mmput(ctx->mm);
1414d2005e3fSOleg Nesterov 	}
1415ad465caeSAndrea Arcangeli 	if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1416ad465caeSAndrea Arcangeli 		return -EFAULT;
1417ad465caeSAndrea Arcangeli 	if (ret < 0)
1418ad465caeSAndrea Arcangeli 		goto out;
1419ad465caeSAndrea Arcangeli 	BUG_ON(!ret);
1420ad465caeSAndrea Arcangeli 	/* len == 0 would wake all */
1421ad465caeSAndrea Arcangeli 	range.len = ret;
1422ad465caeSAndrea Arcangeli 	if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1423ad465caeSAndrea Arcangeli 		range.start = uffdio_copy.dst;
1424ad465caeSAndrea Arcangeli 		wake_userfault(ctx, &range);
1425ad465caeSAndrea Arcangeli 	}
1426ad465caeSAndrea Arcangeli 	ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1427ad465caeSAndrea Arcangeli out:
1428ad465caeSAndrea Arcangeli 	return ret;
1429ad465caeSAndrea Arcangeli }
1430ad465caeSAndrea Arcangeli 
1431ad465caeSAndrea Arcangeli static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1432ad465caeSAndrea Arcangeli 				unsigned long arg)
1433ad465caeSAndrea Arcangeli {
1434ad465caeSAndrea Arcangeli 	__s64 ret;
1435ad465caeSAndrea Arcangeli 	struct uffdio_zeropage uffdio_zeropage;
1436ad465caeSAndrea Arcangeli 	struct uffdio_zeropage __user *user_uffdio_zeropage;
1437ad465caeSAndrea Arcangeli 	struct userfaultfd_wake_range range;
1438ad465caeSAndrea Arcangeli 
1439ad465caeSAndrea Arcangeli 	user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1440ad465caeSAndrea Arcangeli 
1441ad465caeSAndrea Arcangeli 	ret = -EFAULT;
1442ad465caeSAndrea Arcangeli 	if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1443ad465caeSAndrea Arcangeli 			   /* don't copy "zeropage" last field */
1444ad465caeSAndrea Arcangeli 			   sizeof(uffdio_zeropage)-sizeof(__s64)))
1445ad465caeSAndrea Arcangeli 		goto out;
1446ad465caeSAndrea Arcangeli 
1447ad465caeSAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1448ad465caeSAndrea Arcangeli 			     uffdio_zeropage.range.len);
1449ad465caeSAndrea Arcangeli 	if (ret)
1450ad465caeSAndrea Arcangeli 		goto out;
1451ad465caeSAndrea Arcangeli 	ret = -EINVAL;
1452ad465caeSAndrea Arcangeli 	if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1453ad465caeSAndrea Arcangeli 		goto out;
1454ad465caeSAndrea Arcangeli 
1455d2005e3fSOleg Nesterov 	if (mmget_not_zero(ctx->mm)) {
1456ad465caeSAndrea Arcangeli 		ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1457ad465caeSAndrea Arcangeli 				     uffdio_zeropage.range.len);
1458d2005e3fSOleg Nesterov 		mmput(ctx->mm);
1459d2005e3fSOleg Nesterov 	}
1460ad465caeSAndrea Arcangeli 	if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1461ad465caeSAndrea Arcangeli 		return -EFAULT;
1462ad465caeSAndrea Arcangeli 	if (ret < 0)
1463ad465caeSAndrea Arcangeli 		goto out;
1464ad465caeSAndrea Arcangeli 	/* len == 0 would wake all */
1465ad465caeSAndrea Arcangeli 	BUG_ON(!ret);
1466ad465caeSAndrea Arcangeli 	range.len = ret;
1467ad465caeSAndrea Arcangeli 	if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1468ad465caeSAndrea Arcangeli 		range.start = uffdio_zeropage.range.start;
1469ad465caeSAndrea Arcangeli 		wake_userfault(ctx, &range);
1470ad465caeSAndrea Arcangeli 	}
1471ad465caeSAndrea Arcangeli 	ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1472ad465caeSAndrea Arcangeli out:
1473ad465caeSAndrea Arcangeli 	return ret;
1474ad465caeSAndrea Arcangeli }
1475ad465caeSAndrea Arcangeli 
14769cd75c3cSPavel Emelyanov static inline unsigned int uffd_ctx_features(__u64 user_features)
14779cd75c3cSPavel Emelyanov {
14789cd75c3cSPavel Emelyanov 	/*
14799cd75c3cSPavel Emelyanov 	 * For the current set of features the bits just coincide
14809cd75c3cSPavel Emelyanov 	 */
14819cd75c3cSPavel Emelyanov 	return (unsigned int)user_features;
14829cd75c3cSPavel Emelyanov }
14839cd75c3cSPavel Emelyanov 
148486039bd3SAndrea Arcangeli /*
148586039bd3SAndrea Arcangeli  * userland asks for a certain API version and we return which bits
148686039bd3SAndrea Arcangeli  * and ioctl commands are implemented in this kernel for such API
148786039bd3SAndrea Arcangeli  * version or -EINVAL if unknown.
148886039bd3SAndrea Arcangeli  */
148986039bd3SAndrea Arcangeli static int userfaultfd_api(struct userfaultfd_ctx *ctx,
149086039bd3SAndrea Arcangeli 			   unsigned long arg)
149186039bd3SAndrea Arcangeli {
149286039bd3SAndrea Arcangeli 	struct uffdio_api uffdio_api;
149386039bd3SAndrea Arcangeli 	void __user *buf = (void __user *)arg;
149486039bd3SAndrea Arcangeli 	int ret;
149565603144SAndrea Arcangeli 	__u64 features;
149686039bd3SAndrea Arcangeli 
149786039bd3SAndrea Arcangeli 	ret = -EINVAL;
149886039bd3SAndrea Arcangeli 	if (ctx->state != UFFD_STATE_WAIT_API)
149986039bd3SAndrea Arcangeli 		goto out;
150086039bd3SAndrea Arcangeli 	ret = -EFAULT;
1501a9b85f94SAndrea Arcangeli 	if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
150286039bd3SAndrea Arcangeli 		goto out;
150365603144SAndrea Arcangeli 	features = uffdio_api.features;
150465603144SAndrea Arcangeli 	if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
150586039bd3SAndrea Arcangeli 		memset(&uffdio_api, 0, sizeof(uffdio_api));
150686039bd3SAndrea Arcangeli 		if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
150786039bd3SAndrea Arcangeli 			goto out;
150886039bd3SAndrea Arcangeli 		ret = -EINVAL;
150986039bd3SAndrea Arcangeli 		goto out;
151086039bd3SAndrea Arcangeli 	}
151165603144SAndrea Arcangeli 	/* report all available features and ioctls to userland */
151265603144SAndrea Arcangeli 	uffdio_api.features = UFFD_API_FEATURES;
151386039bd3SAndrea Arcangeli 	uffdio_api.ioctls = UFFD_API_IOCTLS;
151486039bd3SAndrea Arcangeli 	ret = -EFAULT;
151586039bd3SAndrea Arcangeli 	if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
151686039bd3SAndrea Arcangeli 		goto out;
151786039bd3SAndrea Arcangeli 	ctx->state = UFFD_STATE_RUNNING;
151865603144SAndrea Arcangeli 	/* only enable the requested features for this uffd context */
151965603144SAndrea Arcangeli 	ctx->features = uffd_ctx_features(features);
152086039bd3SAndrea Arcangeli 	ret = 0;
152186039bd3SAndrea Arcangeli out:
152286039bd3SAndrea Arcangeli 	return ret;
152386039bd3SAndrea Arcangeli }
152486039bd3SAndrea Arcangeli 
152586039bd3SAndrea Arcangeli static long userfaultfd_ioctl(struct file *file, unsigned cmd,
152686039bd3SAndrea Arcangeli 			      unsigned long arg)
152786039bd3SAndrea Arcangeli {
152886039bd3SAndrea Arcangeli 	int ret = -EINVAL;
152986039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
153086039bd3SAndrea Arcangeli 
1531e6485a47SAndrea Arcangeli 	if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1532e6485a47SAndrea Arcangeli 		return -EINVAL;
1533e6485a47SAndrea Arcangeli 
153486039bd3SAndrea Arcangeli 	switch(cmd) {
153586039bd3SAndrea Arcangeli 	case UFFDIO_API:
153686039bd3SAndrea Arcangeli 		ret = userfaultfd_api(ctx, arg);
153786039bd3SAndrea Arcangeli 		break;
153886039bd3SAndrea Arcangeli 	case UFFDIO_REGISTER:
153986039bd3SAndrea Arcangeli 		ret = userfaultfd_register(ctx, arg);
154086039bd3SAndrea Arcangeli 		break;
154186039bd3SAndrea Arcangeli 	case UFFDIO_UNREGISTER:
154286039bd3SAndrea Arcangeli 		ret = userfaultfd_unregister(ctx, arg);
154386039bd3SAndrea Arcangeli 		break;
154486039bd3SAndrea Arcangeli 	case UFFDIO_WAKE:
154586039bd3SAndrea Arcangeli 		ret = userfaultfd_wake(ctx, arg);
154686039bd3SAndrea Arcangeli 		break;
1547ad465caeSAndrea Arcangeli 	case UFFDIO_COPY:
1548ad465caeSAndrea Arcangeli 		ret = userfaultfd_copy(ctx, arg);
1549ad465caeSAndrea Arcangeli 		break;
1550ad465caeSAndrea Arcangeli 	case UFFDIO_ZEROPAGE:
1551ad465caeSAndrea Arcangeli 		ret = userfaultfd_zeropage(ctx, arg);
1552ad465caeSAndrea Arcangeli 		break;
155386039bd3SAndrea Arcangeli 	}
155486039bd3SAndrea Arcangeli 	return ret;
155586039bd3SAndrea Arcangeli }
155686039bd3SAndrea Arcangeli 
155786039bd3SAndrea Arcangeli #ifdef CONFIG_PROC_FS
155886039bd3SAndrea Arcangeli static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
155986039bd3SAndrea Arcangeli {
156086039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = f->private_data;
156186039bd3SAndrea Arcangeli 	wait_queue_t *wq;
156286039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
156386039bd3SAndrea Arcangeli 	unsigned long pending = 0, total = 0;
156486039bd3SAndrea Arcangeli 
156515b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
156615b726efSAndrea Arcangeli 	list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
156786039bd3SAndrea Arcangeli 		uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
156886039bd3SAndrea Arcangeli 		pending++;
156986039bd3SAndrea Arcangeli 		total++;
157086039bd3SAndrea Arcangeli 	}
157115b726efSAndrea Arcangeli 	list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
157215b726efSAndrea Arcangeli 		uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
157315b726efSAndrea Arcangeli 		total++;
157415b726efSAndrea Arcangeli 	}
157515b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
157686039bd3SAndrea Arcangeli 
157786039bd3SAndrea Arcangeli 	/*
157886039bd3SAndrea Arcangeli 	 * If more protocols will be added, there will be all shown
157986039bd3SAndrea Arcangeli 	 * separated by a space. Like this:
158086039bd3SAndrea Arcangeli 	 *	protocols: aa:... bb:...
158186039bd3SAndrea Arcangeli 	 */
158286039bd3SAndrea Arcangeli 	seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
15833f602d27SPavel Emelyanov 		   pending, total, UFFD_API, UFFD_API_FEATURES,
158486039bd3SAndrea Arcangeli 		   UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
158586039bd3SAndrea Arcangeli }
158686039bd3SAndrea Arcangeli #endif
158786039bd3SAndrea Arcangeli 
158886039bd3SAndrea Arcangeli static const struct file_operations userfaultfd_fops = {
158986039bd3SAndrea Arcangeli #ifdef CONFIG_PROC_FS
159086039bd3SAndrea Arcangeli 	.show_fdinfo	= userfaultfd_show_fdinfo,
159186039bd3SAndrea Arcangeli #endif
159286039bd3SAndrea Arcangeli 	.release	= userfaultfd_release,
159386039bd3SAndrea Arcangeli 	.poll		= userfaultfd_poll,
159486039bd3SAndrea Arcangeli 	.read		= userfaultfd_read,
159586039bd3SAndrea Arcangeli 	.unlocked_ioctl = userfaultfd_ioctl,
159686039bd3SAndrea Arcangeli 	.compat_ioctl	= userfaultfd_ioctl,
159786039bd3SAndrea Arcangeli 	.llseek		= noop_llseek,
159886039bd3SAndrea Arcangeli };
159986039bd3SAndrea Arcangeli 
16003004ec9cSAndrea Arcangeli static void init_once_userfaultfd_ctx(void *mem)
16013004ec9cSAndrea Arcangeli {
16023004ec9cSAndrea Arcangeli 	struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
16033004ec9cSAndrea Arcangeli 
16043004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fault_pending_wqh);
16053004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fault_wqh);
16069cd75c3cSPavel Emelyanov 	init_waitqueue_head(&ctx->event_wqh);
16073004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fd_wqh);
16082c5b7e1bSAndrea Arcangeli 	seqcount_init(&ctx->refile_seq);
16093004ec9cSAndrea Arcangeli }
16103004ec9cSAndrea Arcangeli 
161186039bd3SAndrea Arcangeli /**
161286039bd3SAndrea Arcangeli  * userfaultfd_file_create - Creates an userfaultfd file pointer.
161386039bd3SAndrea Arcangeli  * @flags: Flags for the userfaultfd file.
161486039bd3SAndrea Arcangeli  *
161586039bd3SAndrea Arcangeli  * This function creates an userfaultfd file pointer, w/out installing
161686039bd3SAndrea Arcangeli  * it into the fd table. This is useful when the userfaultfd file is
161786039bd3SAndrea Arcangeli  * used during the initialization of data structures that require
161886039bd3SAndrea Arcangeli  * extra setup after the userfaultfd creation. So the userfaultfd
161986039bd3SAndrea Arcangeli  * creation is split into the file pointer creation phase, and the
162086039bd3SAndrea Arcangeli  * file descriptor installation phase.  In this way races with
162186039bd3SAndrea Arcangeli  * userspace closing the newly installed file descriptor can be
162286039bd3SAndrea Arcangeli  * avoided.  Returns an userfaultfd file pointer, or a proper error
162386039bd3SAndrea Arcangeli  * pointer.
162486039bd3SAndrea Arcangeli  */
162586039bd3SAndrea Arcangeli static struct file *userfaultfd_file_create(int flags)
162686039bd3SAndrea Arcangeli {
162786039bd3SAndrea Arcangeli 	struct file *file;
162886039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
162986039bd3SAndrea Arcangeli 
163086039bd3SAndrea Arcangeli 	BUG_ON(!current->mm);
163186039bd3SAndrea Arcangeli 
163286039bd3SAndrea Arcangeli 	/* Check the UFFD_* constants for consistency.  */
163386039bd3SAndrea Arcangeli 	BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
163486039bd3SAndrea Arcangeli 	BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
163586039bd3SAndrea Arcangeli 
163686039bd3SAndrea Arcangeli 	file = ERR_PTR(-EINVAL);
163786039bd3SAndrea Arcangeli 	if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
163886039bd3SAndrea Arcangeli 		goto out;
163986039bd3SAndrea Arcangeli 
164086039bd3SAndrea Arcangeli 	file = ERR_PTR(-ENOMEM);
16413004ec9cSAndrea Arcangeli 	ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
164286039bd3SAndrea Arcangeli 	if (!ctx)
164386039bd3SAndrea Arcangeli 		goto out;
164486039bd3SAndrea Arcangeli 
164586039bd3SAndrea Arcangeli 	atomic_set(&ctx->refcount, 1);
164686039bd3SAndrea Arcangeli 	ctx->flags = flags;
16479cd75c3cSPavel Emelyanov 	ctx->features = 0;
164886039bd3SAndrea Arcangeli 	ctx->state = UFFD_STATE_WAIT_API;
164986039bd3SAndrea Arcangeli 	ctx->released = false;
165086039bd3SAndrea Arcangeli 	ctx->mm = current->mm;
165186039bd3SAndrea Arcangeli 	/* prevent the mm struct to be freed */
1652d2005e3fSOleg Nesterov 	atomic_inc(&ctx->mm->mm_count);
165386039bd3SAndrea Arcangeli 
165486039bd3SAndrea Arcangeli 	file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
165586039bd3SAndrea Arcangeli 				  O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1656c03e946fSEric Biggers 	if (IS_ERR(file)) {
1657d2005e3fSOleg Nesterov 		mmdrop(ctx->mm);
16583004ec9cSAndrea Arcangeli 		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
1659c03e946fSEric Biggers 	}
166086039bd3SAndrea Arcangeli out:
166186039bd3SAndrea Arcangeli 	return file;
166286039bd3SAndrea Arcangeli }
166386039bd3SAndrea Arcangeli 
166486039bd3SAndrea Arcangeli SYSCALL_DEFINE1(userfaultfd, int, flags)
166586039bd3SAndrea Arcangeli {
166686039bd3SAndrea Arcangeli 	int fd, error;
166786039bd3SAndrea Arcangeli 	struct file *file;
166886039bd3SAndrea Arcangeli 
166986039bd3SAndrea Arcangeli 	error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
167086039bd3SAndrea Arcangeli 	if (error < 0)
167186039bd3SAndrea Arcangeli 		return error;
167286039bd3SAndrea Arcangeli 	fd = error;
167386039bd3SAndrea Arcangeli 
167486039bd3SAndrea Arcangeli 	file = userfaultfd_file_create(flags);
167586039bd3SAndrea Arcangeli 	if (IS_ERR(file)) {
167686039bd3SAndrea Arcangeli 		error = PTR_ERR(file);
167786039bd3SAndrea Arcangeli 		goto err_put_unused_fd;
167886039bd3SAndrea Arcangeli 	}
167986039bd3SAndrea Arcangeli 	fd_install(fd, file);
168086039bd3SAndrea Arcangeli 
168186039bd3SAndrea Arcangeli 	return fd;
168286039bd3SAndrea Arcangeli 
168386039bd3SAndrea Arcangeli err_put_unused_fd:
168486039bd3SAndrea Arcangeli 	put_unused_fd(fd);
168586039bd3SAndrea Arcangeli 
168686039bd3SAndrea Arcangeli 	return error;
168786039bd3SAndrea Arcangeli }
16883004ec9cSAndrea Arcangeli 
16893004ec9cSAndrea Arcangeli static int __init userfaultfd_init(void)
16903004ec9cSAndrea Arcangeli {
16913004ec9cSAndrea Arcangeli 	userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
16923004ec9cSAndrea Arcangeli 						sizeof(struct userfaultfd_ctx),
16933004ec9cSAndrea Arcangeli 						0,
16943004ec9cSAndrea Arcangeli 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
16953004ec9cSAndrea Arcangeli 						init_once_userfaultfd_ctx);
16963004ec9cSAndrea Arcangeli 	return 0;
16973004ec9cSAndrea Arcangeli }
16983004ec9cSAndrea Arcangeli __initcall(userfaultfd_init);
1699