xref: /openbmc/linux/fs/userfaultfd.c (revision cab350afcbc9c8a744e0d164d1c26560568f770b)
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>
30*cab350afSMike Kravetz #include <linux/hugetlb.h>
3186039bd3SAndrea Arcangeli 
323004ec9cSAndrea Arcangeli static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
333004ec9cSAndrea Arcangeli 
3486039bd3SAndrea Arcangeli enum userfaultfd_state {
3586039bd3SAndrea Arcangeli 	UFFD_STATE_WAIT_API,
3686039bd3SAndrea Arcangeli 	UFFD_STATE_RUNNING,
3786039bd3SAndrea Arcangeli };
3886039bd3SAndrea Arcangeli 
393004ec9cSAndrea Arcangeli /*
403004ec9cSAndrea Arcangeli  * Start with fault_pending_wqh and fault_wqh so they're more likely
413004ec9cSAndrea Arcangeli  * to be in the same cacheline.
423004ec9cSAndrea Arcangeli  */
4386039bd3SAndrea Arcangeli struct userfaultfd_ctx {
4415b726efSAndrea Arcangeli 	/* waitqueue head for the pending (i.e. not read) userfaults */
4515b726efSAndrea Arcangeli 	wait_queue_head_t fault_pending_wqh;
4615b726efSAndrea Arcangeli 	/* waitqueue head for the userfaults */
4786039bd3SAndrea Arcangeli 	wait_queue_head_t fault_wqh;
4886039bd3SAndrea Arcangeli 	/* waitqueue head for the pseudo fd to wakeup poll/read */
4986039bd3SAndrea Arcangeli 	wait_queue_head_t fd_wqh;
509cd75c3cSPavel Emelyanov 	/* waitqueue head for events */
519cd75c3cSPavel Emelyanov 	wait_queue_head_t event_wqh;
522c5b7e1bSAndrea Arcangeli 	/* a refile sequence protected by fault_pending_wqh lock */
532c5b7e1bSAndrea Arcangeli 	struct seqcount refile_seq;
543004ec9cSAndrea Arcangeli 	/* pseudo fd refcounting */
553004ec9cSAndrea Arcangeli 	atomic_t refcount;
5686039bd3SAndrea Arcangeli 	/* userfaultfd syscall flags */
5786039bd3SAndrea Arcangeli 	unsigned int flags;
589cd75c3cSPavel Emelyanov 	/* features requested from the userspace */
599cd75c3cSPavel Emelyanov 	unsigned int features;
6086039bd3SAndrea Arcangeli 	/* state machine */
6186039bd3SAndrea Arcangeli 	enum userfaultfd_state state;
6286039bd3SAndrea Arcangeli 	/* released */
6386039bd3SAndrea Arcangeli 	bool released;
6486039bd3SAndrea Arcangeli 	/* mm with one ore more vmas attached to this userfaultfd_ctx */
6586039bd3SAndrea Arcangeli 	struct mm_struct *mm;
6686039bd3SAndrea Arcangeli };
6786039bd3SAndrea Arcangeli 
68893e26e6SPavel Emelyanov struct userfaultfd_fork_ctx {
69893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *orig;
70893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *new;
71893e26e6SPavel Emelyanov 	struct list_head list;
72893e26e6SPavel Emelyanov };
73893e26e6SPavel Emelyanov 
7486039bd3SAndrea Arcangeli struct userfaultfd_wait_queue {
75a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
7686039bd3SAndrea Arcangeli 	wait_queue_t wq;
7786039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
7815a77c6fSAndrea Arcangeli 	bool waken;
7986039bd3SAndrea Arcangeli };
8086039bd3SAndrea Arcangeli 
8186039bd3SAndrea Arcangeli struct userfaultfd_wake_range {
8286039bd3SAndrea Arcangeli 	unsigned long start;
8386039bd3SAndrea Arcangeli 	unsigned long len;
8486039bd3SAndrea Arcangeli };
8586039bd3SAndrea Arcangeli 
8686039bd3SAndrea Arcangeli static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
8786039bd3SAndrea Arcangeli 				     int wake_flags, void *key)
8886039bd3SAndrea Arcangeli {
8986039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range *range = key;
9086039bd3SAndrea Arcangeli 	int ret;
9186039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
9286039bd3SAndrea Arcangeli 	unsigned long start, len;
9386039bd3SAndrea Arcangeli 
9486039bd3SAndrea Arcangeli 	uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
9586039bd3SAndrea Arcangeli 	ret = 0;
9686039bd3SAndrea Arcangeli 	/* len == 0 means wake all */
9786039bd3SAndrea Arcangeli 	start = range->start;
9886039bd3SAndrea Arcangeli 	len = range->len;
99a9b85f94SAndrea Arcangeli 	if (len && (start > uwq->msg.arg.pagefault.address ||
100a9b85f94SAndrea Arcangeli 		    start + len <= uwq->msg.arg.pagefault.address))
10186039bd3SAndrea Arcangeli 		goto out;
10215a77c6fSAndrea Arcangeli 	WRITE_ONCE(uwq->waken, true);
10315a77c6fSAndrea Arcangeli 	/*
10415a77c6fSAndrea Arcangeli 	 * The implicit smp_mb__before_spinlock in try_to_wake_up()
10515a77c6fSAndrea Arcangeli 	 * renders uwq->waken visible to other CPUs before the task is
10615a77c6fSAndrea Arcangeli 	 * waken.
10715a77c6fSAndrea Arcangeli 	 */
10886039bd3SAndrea Arcangeli 	ret = wake_up_state(wq->private, mode);
10986039bd3SAndrea Arcangeli 	if (ret)
11086039bd3SAndrea Arcangeli 		/*
11186039bd3SAndrea Arcangeli 		 * Wake only once, autoremove behavior.
11286039bd3SAndrea Arcangeli 		 *
11386039bd3SAndrea Arcangeli 		 * After the effect of list_del_init is visible to the
11486039bd3SAndrea Arcangeli 		 * other CPUs, the waitqueue may disappear from under
11586039bd3SAndrea Arcangeli 		 * us, see the !list_empty_careful() in
11686039bd3SAndrea Arcangeli 		 * handle_userfault(). try_to_wake_up() has an
11786039bd3SAndrea Arcangeli 		 * implicit smp_mb__before_spinlock, and the
11886039bd3SAndrea Arcangeli 		 * wq->private is read before calling the extern
11986039bd3SAndrea Arcangeli 		 * function "wake_up_state" (which in turns calls
12086039bd3SAndrea Arcangeli 		 * try_to_wake_up). While the spin_lock;spin_unlock;
12186039bd3SAndrea Arcangeli 		 * wouldn't be enough, the smp_mb__before_spinlock is
12286039bd3SAndrea Arcangeli 		 * enough to avoid an explicit smp_mb() here.
12386039bd3SAndrea Arcangeli 		 */
12486039bd3SAndrea Arcangeli 		list_del_init(&wq->task_list);
12586039bd3SAndrea Arcangeli out:
12686039bd3SAndrea Arcangeli 	return ret;
12786039bd3SAndrea Arcangeli }
12886039bd3SAndrea Arcangeli 
12986039bd3SAndrea Arcangeli /**
13086039bd3SAndrea Arcangeli  * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
13186039bd3SAndrea Arcangeli  * context.
13286039bd3SAndrea Arcangeli  * @ctx: [in] Pointer to the userfaultfd context.
13386039bd3SAndrea Arcangeli  *
13486039bd3SAndrea Arcangeli  * Returns: In case of success, returns not zero.
13586039bd3SAndrea Arcangeli  */
13686039bd3SAndrea Arcangeli static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
13786039bd3SAndrea Arcangeli {
13886039bd3SAndrea Arcangeli 	if (!atomic_inc_not_zero(&ctx->refcount))
13986039bd3SAndrea Arcangeli 		BUG();
14086039bd3SAndrea Arcangeli }
14186039bd3SAndrea Arcangeli 
14286039bd3SAndrea Arcangeli /**
14386039bd3SAndrea Arcangeli  * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
14486039bd3SAndrea Arcangeli  * context.
14586039bd3SAndrea Arcangeli  * @ctx: [in] Pointer to userfaultfd context.
14686039bd3SAndrea Arcangeli  *
14786039bd3SAndrea Arcangeli  * The userfaultfd context reference must have been previously acquired either
14886039bd3SAndrea Arcangeli  * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
14986039bd3SAndrea Arcangeli  */
15086039bd3SAndrea Arcangeli static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
15186039bd3SAndrea Arcangeli {
15286039bd3SAndrea Arcangeli 	if (atomic_dec_and_test(&ctx->refcount)) {
15386039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
15486039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
15586039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
15686039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
1579cd75c3cSPavel Emelyanov 		VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
1589cd75c3cSPavel Emelyanov 		VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
15986039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
16086039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
161d2005e3fSOleg Nesterov 		mmdrop(ctx->mm);
1623004ec9cSAndrea Arcangeli 		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
16386039bd3SAndrea Arcangeli 	}
16486039bd3SAndrea Arcangeli }
16586039bd3SAndrea Arcangeli 
166a9b85f94SAndrea Arcangeli static inline void msg_init(struct uffd_msg *msg)
167a9b85f94SAndrea Arcangeli {
168a9b85f94SAndrea Arcangeli 	BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
169a9b85f94SAndrea Arcangeli 	/*
170a9b85f94SAndrea Arcangeli 	 * Must use memset to zero out the paddings or kernel data is
171a9b85f94SAndrea Arcangeli 	 * leaked to userland.
172a9b85f94SAndrea Arcangeli 	 */
173a9b85f94SAndrea Arcangeli 	memset(msg, 0, sizeof(struct uffd_msg));
174a9b85f94SAndrea Arcangeli }
175a9b85f94SAndrea Arcangeli 
176a9b85f94SAndrea Arcangeli static inline struct uffd_msg userfault_msg(unsigned long address,
17786039bd3SAndrea Arcangeli 					    unsigned int flags,
17886039bd3SAndrea Arcangeli 					    unsigned long reason)
17986039bd3SAndrea Arcangeli {
180a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
181a9b85f94SAndrea Arcangeli 	msg_init(&msg);
182a9b85f94SAndrea Arcangeli 	msg.event = UFFD_EVENT_PAGEFAULT;
183a9b85f94SAndrea Arcangeli 	msg.arg.pagefault.address = address;
18486039bd3SAndrea Arcangeli 	if (flags & FAULT_FLAG_WRITE)
18586039bd3SAndrea Arcangeli 		/*
186a4605a61SAndrea Arcangeli 		 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
187a9b85f94SAndrea Arcangeli 		 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
188a9b85f94SAndrea Arcangeli 		 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
189a9b85f94SAndrea Arcangeli 		 * was a read fault, otherwise if set it means it's
190a9b85f94SAndrea Arcangeli 		 * a write fault.
19186039bd3SAndrea Arcangeli 		 */
192a9b85f94SAndrea Arcangeli 		msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
19386039bd3SAndrea Arcangeli 	if (reason & VM_UFFD_WP)
19486039bd3SAndrea Arcangeli 		/*
195a9b85f94SAndrea Arcangeli 		 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
196a9b85f94SAndrea Arcangeli 		 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
197a9b85f94SAndrea Arcangeli 		 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
198a9b85f94SAndrea Arcangeli 		 * a missing fault, otherwise if set it means it's a
199a9b85f94SAndrea Arcangeli 		 * write protect fault.
20086039bd3SAndrea Arcangeli 		 */
201a9b85f94SAndrea Arcangeli 		msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
202a9b85f94SAndrea Arcangeli 	return msg;
20386039bd3SAndrea Arcangeli }
20486039bd3SAndrea Arcangeli 
20586039bd3SAndrea Arcangeli /*
2068d2afd96SAndrea Arcangeli  * Verify the pagetables are still not ok after having reigstered into
2078d2afd96SAndrea Arcangeli  * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
2088d2afd96SAndrea Arcangeli  * userfault that has already been resolved, if userfaultfd_read and
2098d2afd96SAndrea Arcangeli  * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
2108d2afd96SAndrea Arcangeli  * threads.
2118d2afd96SAndrea Arcangeli  */
2128d2afd96SAndrea Arcangeli static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
2138d2afd96SAndrea Arcangeli 					 unsigned long address,
2148d2afd96SAndrea Arcangeli 					 unsigned long flags,
2158d2afd96SAndrea Arcangeli 					 unsigned long reason)
2168d2afd96SAndrea Arcangeli {
2178d2afd96SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
2188d2afd96SAndrea Arcangeli 	pgd_t *pgd;
2198d2afd96SAndrea Arcangeli 	pud_t *pud;
2208d2afd96SAndrea Arcangeli 	pmd_t *pmd, _pmd;
2218d2afd96SAndrea Arcangeli 	pte_t *pte;
2228d2afd96SAndrea Arcangeli 	bool ret = true;
2238d2afd96SAndrea Arcangeli 
2248d2afd96SAndrea Arcangeli 	VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
2258d2afd96SAndrea Arcangeli 
2268d2afd96SAndrea Arcangeli 	pgd = pgd_offset(mm, address);
2278d2afd96SAndrea Arcangeli 	if (!pgd_present(*pgd))
2288d2afd96SAndrea Arcangeli 		goto out;
2298d2afd96SAndrea Arcangeli 	pud = pud_offset(pgd, address);
2308d2afd96SAndrea Arcangeli 	if (!pud_present(*pud))
2318d2afd96SAndrea Arcangeli 		goto out;
2328d2afd96SAndrea Arcangeli 	pmd = pmd_offset(pud, address);
2338d2afd96SAndrea Arcangeli 	/*
2348d2afd96SAndrea Arcangeli 	 * READ_ONCE must function as a barrier with narrower scope
2358d2afd96SAndrea Arcangeli 	 * and it must be equivalent to:
2368d2afd96SAndrea Arcangeli 	 *	_pmd = *pmd; barrier();
2378d2afd96SAndrea Arcangeli 	 *
2388d2afd96SAndrea Arcangeli 	 * This is to deal with the instability (as in
2398d2afd96SAndrea Arcangeli 	 * pmd_trans_unstable) of the pmd.
2408d2afd96SAndrea Arcangeli 	 */
2418d2afd96SAndrea Arcangeli 	_pmd = READ_ONCE(*pmd);
2428d2afd96SAndrea Arcangeli 	if (!pmd_present(_pmd))
2438d2afd96SAndrea Arcangeli 		goto out;
2448d2afd96SAndrea Arcangeli 
2458d2afd96SAndrea Arcangeli 	ret = false;
2468d2afd96SAndrea Arcangeli 	if (pmd_trans_huge(_pmd))
2478d2afd96SAndrea Arcangeli 		goto out;
2488d2afd96SAndrea Arcangeli 
2498d2afd96SAndrea Arcangeli 	/*
2508d2afd96SAndrea Arcangeli 	 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
2518d2afd96SAndrea Arcangeli 	 * and use the standard pte_offset_map() instead of parsing _pmd.
2528d2afd96SAndrea Arcangeli 	 */
2538d2afd96SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
2548d2afd96SAndrea Arcangeli 	/*
2558d2afd96SAndrea Arcangeli 	 * Lockless access: we're in a wait_event so it's ok if it
2568d2afd96SAndrea Arcangeli 	 * changes under us.
2578d2afd96SAndrea Arcangeli 	 */
2588d2afd96SAndrea Arcangeli 	if (pte_none(*pte))
2598d2afd96SAndrea Arcangeli 		ret = true;
2608d2afd96SAndrea Arcangeli 	pte_unmap(pte);
2618d2afd96SAndrea Arcangeli 
2628d2afd96SAndrea Arcangeli out:
2638d2afd96SAndrea Arcangeli 	return ret;
2648d2afd96SAndrea Arcangeli }
2658d2afd96SAndrea Arcangeli 
2668d2afd96SAndrea Arcangeli /*
26786039bd3SAndrea Arcangeli  * The locking rules involved in returning VM_FAULT_RETRY depending on
26886039bd3SAndrea Arcangeli  * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
26986039bd3SAndrea Arcangeli  * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
27086039bd3SAndrea Arcangeli  * recommendation in __lock_page_or_retry is not an understatement.
27186039bd3SAndrea Arcangeli  *
27286039bd3SAndrea Arcangeli  * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
27386039bd3SAndrea Arcangeli  * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
27486039bd3SAndrea Arcangeli  * not set.
27586039bd3SAndrea Arcangeli  *
27686039bd3SAndrea Arcangeli  * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
27786039bd3SAndrea Arcangeli  * set, VM_FAULT_RETRY can still be returned if and only if there are
27886039bd3SAndrea Arcangeli  * fatal_signal_pending()s, and the mmap_sem must be released before
27986039bd3SAndrea Arcangeli  * returning it.
28086039bd3SAndrea Arcangeli  */
28182b0f8c3SJan Kara int handle_userfault(struct vm_fault *vmf, unsigned long reason)
28286039bd3SAndrea Arcangeli {
28382b0f8c3SJan Kara 	struct mm_struct *mm = vmf->vma->vm_mm;
28486039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
28586039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue uwq;
286ba85c702SAndrea Arcangeli 	int ret;
287dfa37dc3SAndrea Arcangeli 	bool must_wait, return_to_userland;
28815a77c6fSAndrea Arcangeli 	long blocking_state;
28986039bd3SAndrea Arcangeli 
29086039bd3SAndrea Arcangeli 	BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
29186039bd3SAndrea Arcangeli 
292ba85c702SAndrea Arcangeli 	ret = VM_FAULT_SIGBUS;
29382b0f8c3SJan Kara 	ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
29486039bd3SAndrea Arcangeli 	if (!ctx)
295ba85c702SAndrea Arcangeli 		goto out;
29686039bd3SAndrea Arcangeli 
29786039bd3SAndrea Arcangeli 	BUG_ON(ctx->mm != mm);
29886039bd3SAndrea Arcangeli 
29986039bd3SAndrea Arcangeli 	VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
30086039bd3SAndrea Arcangeli 	VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
30186039bd3SAndrea Arcangeli 
30286039bd3SAndrea Arcangeli 	/*
30386039bd3SAndrea Arcangeli 	 * If it's already released don't get it. This avoids to loop
30486039bd3SAndrea Arcangeli 	 * in __get_user_pages if userfaultfd_release waits on the
30586039bd3SAndrea Arcangeli 	 * caller of handle_userfault to release the mmap_sem.
30686039bd3SAndrea Arcangeli 	 */
30786039bd3SAndrea Arcangeli 	if (unlikely(ACCESS_ONCE(ctx->released)))
308ba85c702SAndrea Arcangeli 		goto out;
30986039bd3SAndrea Arcangeli 
31086039bd3SAndrea Arcangeli 	/*
31139680f50SLinus Torvalds 	 * We don't do userfault handling for the final child pid update.
31239680f50SLinus Torvalds 	 */
31339680f50SLinus Torvalds 	if (current->flags & PF_EXITING)
31439680f50SLinus Torvalds 		goto out;
31539680f50SLinus Torvalds 
31639680f50SLinus Torvalds 	/*
31786039bd3SAndrea Arcangeli 	 * Check that we can return VM_FAULT_RETRY.
31886039bd3SAndrea Arcangeli 	 *
31986039bd3SAndrea Arcangeli 	 * NOTE: it should become possible to return VM_FAULT_RETRY
32086039bd3SAndrea Arcangeli 	 * even if FAULT_FLAG_TRIED is set without leading to gup()
32186039bd3SAndrea Arcangeli 	 * -EBUSY failures, if the userfaultfd is to be extended for
32286039bd3SAndrea Arcangeli 	 * VM_UFFD_WP tracking and we intend to arm the userfault
32386039bd3SAndrea Arcangeli 	 * without first stopping userland access to the memory. For
32486039bd3SAndrea Arcangeli 	 * VM_UFFD_MISSING userfaults this is enough for now.
32586039bd3SAndrea Arcangeli 	 */
32682b0f8c3SJan Kara 	if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
32786039bd3SAndrea Arcangeli 		/*
32886039bd3SAndrea Arcangeli 		 * Validate the invariant that nowait must allow retry
32986039bd3SAndrea Arcangeli 		 * to be sure not to return SIGBUS erroneously on
33086039bd3SAndrea Arcangeli 		 * nowait invocations.
33186039bd3SAndrea Arcangeli 		 */
33282b0f8c3SJan Kara 		BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
33386039bd3SAndrea Arcangeli #ifdef CONFIG_DEBUG_VM
33486039bd3SAndrea Arcangeli 		if (printk_ratelimit()) {
33586039bd3SAndrea Arcangeli 			printk(KERN_WARNING
33682b0f8c3SJan Kara 			       "FAULT_FLAG_ALLOW_RETRY missing %x\n",
33782b0f8c3SJan Kara 			       vmf->flags);
33886039bd3SAndrea Arcangeli 			dump_stack();
33986039bd3SAndrea Arcangeli 		}
34086039bd3SAndrea Arcangeli #endif
341ba85c702SAndrea Arcangeli 		goto out;
34286039bd3SAndrea Arcangeli 	}
34386039bd3SAndrea Arcangeli 
34486039bd3SAndrea Arcangeli 	/*
34586039bd3SAndrea Arcangeli 	 * Handle nowait, not much to do other than tell it to retry
34686039bd3SAndrea Arcangeli 	 * and wait.
34786039bd3SAndrea Arcangeli 	 */
348ba85c702SAndrea Arcangeli 	ret = VM_FAULT_RETRY;
34982b0f8c3SJan Kara 	if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
350ba85c702SAndrea Arcangeli 		goto out;
35186039bd3SAndrea Arcangeli 
35286039bd3SAndrea Arcangeli 	/* take the reference before dropping the mmap_sem */
35386039bd3SAndrea Arcangeli 	userfaultfd_ctx_get(ctx);
35486039bd3SAndrea Arcangeli 
35586039bd3SAndrea Arcangeli 	init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
35686039bd3SAndrea Arcangeli 	uwq.wq.private = current;
35782b0f8c3SJan Kara 	uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
35886039bd3SAndrea Arcangeli 	uwq.ctx = ctx;
35915a77c6fSAndrea Arcangeli 	uwq.waken = false;
36086039bd3SAndrea Arcangeli 
361bae473a4SKirill A. Shutemov 	return_to_userland =
36282b0f8c3SJan Kara 		(vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
363dfa37dc3SAndrea Arcangeli 		(FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
36415a77c6fSAndrea Arcangeli 	blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
36515a77c6fSAndrea Arcangeli 			 TASK_KILLABLE;
366dfa37dc3SAndrea Arcangeli 
36715b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
36886039bd3SAndrea Arcangeli 	/*
36986039bd3SAndrea Arcangeli 	 * After the __add_wait_queue the uwq is visible to userland
37086039bd3SAndrea Arcangeli 	 * through poll/read().
37186039bd3SAndrea Arcangeli 	 */
37215b726efSAndrea Arcangeli 	__add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
37315b726efSAndrea Arcangeli 	/*
37415b726efSAndrea Arcangeli 	 * The smp_mb() after __set_current_state prevents the reads
37515b726efSAndrea Arcangeli 	 * following the spin_unlock to happen before the list_add in
37615b726efSAndrea Arcangeli 	 * __add_wait_queue.
37715b726efSAndrea Arcangeli 	 */
37815a77c6fSAndrea Arcangeli 	set_current_state(blocking_state);
37915b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
38086039bd3SAndrea Arcangeli 
38182b0f8c3SJan Kara 	must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
38282b0f8c3SJan Kara 					  reason);
3838d2afd96SAndrea Arcangeli 	up_read(&mm->mmap_sem);
3848d2afd96SAndrea Arcangeli 
3858d2afd96SAndrea Arcangeli 	if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
386dfa37dc3SAndrea Arcangeli 		   (return_to_userland ? !signal_pending(current) :
387dfa37dc3SAndrea Arcangeli 		    !fatal_signal_pending(current)))) {
38886039bd3SAndrea Arcangeli 		wake_up_poll(&ctx->fd_wqh, POLLIN);
38986039bd3SAndrea Arcangeli 		schedule();
390ba85c702SAndrea Arcangeli 		ret |= VM_FAULT_MAJOR;
39115a77c6fSAndrea Arcangeli 
39215a77c6fSAndrea Arcangeli 		/*
39315a77c6fSAndrea Arcangeli 		 * False wakeups can orginate even from rwsem before
39415a77c6fSAndrea Arcangeli 		 * up_read() however userfaults will wait either for a
39515a77c6fSAndrea Arcangeli 		 * targeted wakeup on the specific uwq waitqueue from
39615a77c6fSAndrea Arcangeli 		 * wake_userfault() or for signals or for uffd
39715a77c6fSAndrea Arcangeli 		 * release.
39815a77c6fSAndrea Arcangeli 		 */
39915a77c6fSAndrea Arcangeli 		while (!READ_ONCE(uwq.waken)) {
40015a77c6fSAndrea Arcangeli 			/*
40115a77c6fSAndrea Arcangeli 			 * This needs the full smp_store_mb()
40215a77c6fSAndrea Arcangeli 			 * guarantee as the state write must be
40315a77c6fSAndrea Arcangeli 			 * visible to other CPUs before reading
40415a77c6fSAndrea Arcangeli 			 * uwq.waken from other CPUs.
40515a77c6fSAndrea Arcangeli 			 */
40615a77c6fSAndrea Arcangeli 			set_current_state(blocking_state);
40715a77c6fSAndrea Arcangeli 			if (READ_ONCE(uwq.waken) ||
40815a77c6fSAndrea Arcangeli 			    READ_ONCE(ctx->released) ||
40915a77c6fSAndrea Arcangeli 			    (return_to_userland ? signal_pending(current) :
41015a77c6fSAndrea Arcangeli 			     fatal_signal_pending(current)))
41115a77c6fSAndrea Arcangeli 				break;
41215a77c6fSAndrea Arcangeli 			schedule();
41315a77c6fSAndrea Arcangeli 		}
41486039bd3SAndrea Arcangeli 	}
415ba85c702SAndrea Arcangeli 
41686039bd3SAndrea Arcangeli 	__set_current_state(TASK_RUNNING);
41715b726efSAndrea Arcangeli 
418dfa37dc3SAndrea Arcangeli 	if (return_to_userland) {
419dfa37dc3SAndrea Arcangeli 		if (signal_pending(current) &&
420dfa37dc3SAndrea Arcangeli 		    !fatal_signal_pending(current)) {
421dfa37dc3SAndrea Arcangeli 			/*
422dfa37dc3SAndrea Arcangeli 			 * If we got a SIGSTOP or SIGCONT and this is
423dfa37dc3SAndrea Arcangeli 			 * a normal userland page fault, just let
424dfa37dc3SAndrea Arcangeli 			 * userland return so the signal will be
425dfa37dc3SAndrea Arcangeli 			 * handled and gdb debugging works.  The page
426dfa37dc3SAndrea Arcangeli 			 * fault code immediately after we return from
427dfa37dc3SAndrea Arcangeli 			 * this function is going to release the
428dfa37dc3SAndrea Arcangeli 			 * mmap_sem and it's not depending on it
429dfa37dc3SAndrea Arcangeli 			 * (unlike gup would if we were not to return
430dfa37dc3SAndrea Arcangeli 			 * VM_FAULT_RETRY).
431dfa37dc3SAndrea Arcangeli 			 *
432dfa37dc3SAndrea Arcangeli 			 * If a fatal signal is pending we still take
433dfa37dc3SAndrea Arcangeli 			 * the streamlined VM_FAULT_RETRY failure path
434dfa37dc3SAndrea Arcangeli 			 * and there's no need to retake the mmap_sem
435dfa37dc3SAndrea Arcangeli 			 * in such case.
436dfa37dc3SAndrea Arcangeli 			 */
437dfa37dc3SAndrea Arcangeli 			down_read(&mm->mmap_sem);
438dfa37dc3SAndrea Arcangeli 			ret = 0;
439dfa37dc3SAndrea Arcangeli 		}
440dfa37dc3SAndrea Arcangeli 	}
441dfa37dc3SAndrea Arcangeli 
44215b726efSAndrea Arcangeli 	/*
44315b726efSAndrea Arcangeli 	 * Here we race with the list_del; list_add in
44415b726efSAndrea Arcangeli 	 * userfaultfd_ctx_read(), however because we don't ever run
44515b726efSAndrea Arcangeli 	 * list_del_init() to refile across the two lists, the prev
44615b726efSAndrea Arcangeli 	 * and next pointers will never point to self. list_add also
44715b726efSAndrea Arcangeli 	 * would never let any of the two pointers to point to
44815b726efSAndrea Arcangeli 	 * self. So list_empty_careful won't risk to see both pointers
44915b726efSAndrea Arcangeli 	 * pointing to self at any time during the list refile. The
45015b726efSAndrea Arcangeli 	 * only case where list_del_init() is called is the full
45115b726efSAndrea Arcangeli 	 * removal in the wake function and there we don't re-list_add
45215b726efSAndrea Arcangeli 	 * and it's fine not to block on the spinlock. The uwq on this
45315b726efSAndrea Arcangeli 	 * kernel stack can be released after the list_del_init.
45415b726efSAndrea Arcangeli 	 */
455ba85c702SAndrea Arcangeli 	if (!list_empty_careful(&uwq.wq.task_list)) {
45615b726efSAndrea Arcangeli 		spin_lock(&ctx->fault_pending_wqh.lock);
45715b726efSAndrea Arcangeli 		/*
45815b726efSAndrea Arcangeli 		 * No need of list_del_init(), the uwq on the stack
45915b726efSAndrea Arcangeli 		 * will be freed shortly anyway.
46015b726efSAndrea Arcangeli 		 */
46115b726efSAndrea Arcangeli 		list_del(&uwq.wq.task_list);
46215b726efSAndrea Arcangeli 		spin_unlock(&ctx->fault_pending_wqh.lock);
463ba85c702SAndrea Arcangeli 	}
46486039bd3SAndrea Arcangeli 
46586039bd3SAndrea Arcangeli 	/*
46686039bd3SAndrea Arcangeli 	 * ctx may go away after this if the userfault pseudo fd is
46786039bd3SAndrea Arcangeli 	 * already released.
46886039bd3SAndrea Arcangeli 	 */
46986039bd3SAndrea Arcangeli 	userfaultfd_ctx_put(ctx);
47086039bd3SAndrea Arcangeli 
471ba85c702SAndrea Arcangeli out:
472ba85c702SAndrea Arcangeli 	return ret;
47386039bd3SAndrea Arcangeli }
47486039bd3SAndrea Arcangeli 
475893e26e6SPavel Emelyanov static int userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
4769cd75c3cSPavel Emelyanov 					     struct userfaultfd_wait_queue *ewq)
4779cd75c3cSPavel Emelyanov {
4789cd75c3cSPavel Emelyanov 	int ret = 0;
4799cd75c3cSPavel Emelyanov 
4809cd75c3cSPavel Emelyanov 	ewq->ctx = ctx;
4819cd75c3cSPavel Emelyanov 	init_waitqueue_entry(&ewq->wq, current);
4829cd75c3cSPavel Emelyanov 
4839cd75c3cSPavel Emelyanov 	spin_lock(&ctx->event_wqh.lock);
4849cd75c3cSPavel Emelyanov 	/*
4859cd75c3cSPavel Emelyanov 	 * After the __add_wait_queue the uwq is visible to userland
4869cd75c3cSPavel Emelyanov 	 * through poll/read().
4879cd75c3cSPavel Emelyanov 	 */
4889cd75c3cSPavel Emelyanov 	__add_wait_queue(&ctx->event_wqh, &ewq->wq);
4899cd75c3cSPavel Emelyanov 	for (;;) {
4909cd75c3cSPavel Emelyanov 		set_current_state(TASK_KILLABLE);
4919cd75c3cSPavel Emelyanov 		if (ewq->msg.event == 0)
4929cd75c3cSPavel Emelyanov 			break;
4939cd75c3cSPavel Emelyanov 		if (ACCESS_ONCE(ctx->released) ||
4949cd75c3cSPavel Emelyanov 		    fatal_signal_pending(current)) {
4959cd75c3cSPavel Emelyanov 			ret = -1;
4969cd75c3cSPavel Emelyanov 			__remove_wait_queue(&ctx->event_wqh, &ewq->wq);
4979cd75c3cSPavel Emelyanov 			break;
4989cd75c3cSPavel Emelyanov 		}
4999cd75c3cSPavel Emelyanov 
5009cd75c3cSPavel Emelyanov 		spin_unlock(&ctx->event_wqh.lock);
5019cd75c3cSPavel Emelyanov 
5029cd75c3cSPavel Emelyanov 		wake_up_poll(&ctx->fd_wqh, POLLIN);
5039cd75c3cSPavel Emelyanov 		schedule();
5049cd75c3cSPavel Emelyanov 
5059cd75c3cSPavel Emelyanov 		spin_lock(&ctx->event_wqh.lock);
5069cd75c3cSPavel Emelyanov 	}
5079cd75c3cSPavel Emelyanov 	__set_current_state(TASK_RUNNING);
5089cd75c3cSPavel Emelyanov 	spin_unlock(&ctx->event_wqh.lock);
5099cd75c3cSPavel Emelyanov 
5109cd75c3cSPavel Emelyanov 	/*
5119cd75c3cSPavel Emelyanov 	 * ctx may go away after this if the userfault pseudo fd is
5129cd75c3cSPavel Emelyanov 	 * already released.
5139cd75c3cSPavel Emelyanov 	 */
5149cd75c3cSPavel Emelyanov 
5159cd75c3cSPavel Emelyanov 	userfaultfd_ctx_put(ctx);
5169cd75c3cSPavel Emelyanov 	return ret;
5179cd75c3cSPavel Emelyanov }
5189cd75c3cSPavel Emelyanov 
5199cd75c3cSPavel Emelyanov static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
5209cd75c3cSPavel Emelyanov 				       struct userfaultfd_wait_queue *ewq)
5219cd75c3cSPavel Emelyanov {
5229cd75c3cSPavel Emelyanov 	ewq->msg.event = 0;
5239cd75c3cSPavel Emelyanov 	wake_up_locked(&ctx->event_wqh);
5249cd75c3cSPavel Emelyanov 	__remove_wait_queue(&ctx->event_wqh, &ewq->wq);
5259cd75c3cSPavel Emelyanov }
5269cd75c3cSPavel Emelyanov 
527893e26e6SPavel Emelyanov int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
528893e26e6SPavel Emelyanov {
529893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *ctx = NULL, *octx;
530893e26e6SPavel Emelyanov 	struct userfaultfd_fork_ctx *fctx;
531893e26e6SPavel Emelyanov 
532893e26e6SPavel Emelyanov 	octx = vma->vm_userfaultfd_ctx.ctx;
533893e26e6SPavel Emelyanov 	if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
534893e26e6SPavel Emelyanov 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
535893e26e6SPavel Emelyanov 		vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
536893e26e6SPavel Emelyanov 		return 0;
537893e26e6SPavel Emelyanov 	}
538893e26e6SPavel Emelyanov 
539893e26e6SPavel Emelyanov 	list_for_each_entry(fctx, fcs, list)
540893e26e6SPavel Emelyanov 		if (fctx->orig == octx) {
541893e26e6SPavel Emelyanov 			ctx = fctx->new;
542893e26e6SPavel Emelyanov 			break;
543893e26e6SPavel Emelyanov 		}
544893e26e6SPavel Emelyanov 
545893e26e6SPavel Emelyanov 	if (!ctx) {
546893e26e6SPavel Emelyanov 		fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
547893e26e6SPavel Emelyanov 		if (!fctx)
548893e26e6SPavel Emelyanov 			return -ENOMEM;
549893e26e6SPavel Emelyanov 
550893e26e6SPavel Emelyanov 		ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
551893e26e6SPavel Emelyanov 		if (!ctx) {
552893e26e6SPavel Emelyanov 			kfree(fctx);
553893e26e6SPavel Emelyanov 			return -ENOMEM;
554893e26e6SPavel Emelyanov 		}
555893e26e6SPavel Emelyanov 
556893e26e6SPavel Emelyanov 		atomic_set(&ctx->refcount, 1);
557893e26e6SPavel Emelyanov 		ctx->flags = octx->flags;
558893e26e6SPavel Emelyanov 		ctx->state = UFFD_STATE_RUNNING;
559893e26e6SPavel Emelyanov 		ctx->features = octx->features;
560893e26e6SPavel Emelyanov 		ctx->released = false;
561893e26e6SPavel Emelyanov 		ctx->mm = vma->vm_mm;
562d3aadc8eSMike Rapoport 		atomic_inc(&ctx->mm->mm_count);
563893e26e6SPavel Emelyanov 
564893e26e6SPavel Emelyanov 		userfaultfd_ctx_get(octx);
565893e26e6SPavel Emelyanov 		fctx->orig = octx;
566893e26e6SPavel Emelyanov 		fctx->new = ctx;
567893e26e6SPavel Emelyanov 		list_add_tail(&fctx->list, fcs);
568893e26e6SPavel Emelyanov 	}
569893e26e6SPavel Emelyanov 
570893e26e6SPavel Emelyanov 	vma->vm_userfaultfd_ctx.ctx = ctx;
571893e26e6SPavel Emelyanov 	return 0;
572893e26e6SPavel Emelyanov }
573893e26e6SPavel Emelyanov 
574893e26e6SPavel Emelyanov static int dup_fctx(struct userfaultfd_fork_ctx *fctx)
575893e26e6SPavel Emelyanov {
576893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *ctx = fctx->orig;
577893e26e6SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
578893e26e6SPavel Emelyanov 
579893e26e6SPavel Emelyanov 	msg_init(&ewq.msg);
580893e26e6SPavel Emelyanov 
581893e26e6SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_FORK;
582893e26e6SPavel Emelyanov 	ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
583893e26e6SPavel Emelyanov 
584893e26e6SPavel Emelyanov 	return userfaultfd_event_wait_completion(ctx, &ewq);
585893e26e6SPavel Emelyanov }
586893e26e6SPavel Emelyanov 
587893e26e6SPavel Emelyanov void dup_userfaultfd_complete(struct list_head *fcs)
588893e26e6SPavel Emelyanov {
589893e26e6SPavel Emelyanov 	int ret = 0;
590893e26e6SPavel Emelyanov 	struct userfaultfd_fork_ctx *fctx, *n;
591893e26e6SPavel Emelyanov 
592893e26e6SPavel Emelyanov 	list_for_each_entry_safe(fctx, n, fcs, list) {
593893e26e6SPavel Emelyanov 		if (!ret)
594893e26e6SPavel Emelyanov 			ret = dup_fctx(fctx);
595893e26e6SPavel Emelyanov 		list_del(&fctx->list);
596893e26e6SPavel Emelyanov 		kfree(fctx);
597893e26e6SPavel Emelyanov 	}
598893e26e6SPavel Emelyanov }
599893e26e6SPavel Emelyanov 
60072f87654SPavel Emelyanov void mremap_userfaultfd_prep(struct vm_area_struct *vma,
60172f87654SPavel Emelyanov 			     struct vm_userfaultfd_ctx *vm_ctx)
60272f87654SPavel Emelyanov {
60372f87654SPavel Emelyanov 	struct userfaultfd_ctx *ctx;
60472f87654SPavel Emelyanov 
60572f87654SPavel Emelyanov 	ctx = vma->vm_userfaultfd_ctx.ctx;
60672f87654SPavel Emelyanov 	if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
60772f87654SPavel Emelyanov 		vm_ctx->ctx = ctx;
60872f87654SPavel Emelyanov 		userfaultfd_ctx_get(ctx);
60972f87654SPavel Emelyanov 	}
61072f87654SPavel Emelyanov }
61172f87654SPavel Emelyanov 
61290794bf1SAndrea Arcangeli void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
61372f87654SPavel Emelyanov 				 unsigned long from, unsigned long to,
61472f87654SPavel Emelyanov 				 unsigned long len)
61572f87654SPavel Emelyanov {
61690794bf1SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = vm_ctx->ctx;
61772f87654SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
61872f87654SPavel Emelyanov 
61972f87654SPavel Emelyanov 	if (!ctx)
62072f87654SPavel Emelyanov 		return;
62172f87654SPavel Emelyanov 
62272f87654SPavel Emelyanov 	if (to & ~PAGE_MASK) {
62372f87654SPavel Emelyanov 		userfaultfd_ctx_put(ctx);
62472f87654SPavel Emelyanov 		return;
62572f87654SPavel Emelyanov 	}
62672f87654SPavel Emelyanov 
62772f87654SPavel Emelyanov 	msg_init(&ewq.msg);
62872f87654SPavel Emelyanov 
62972f87654SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_REMAP;
63072f87654SPavel Emelyanov 	ewq.msg.arg.remap.from = from;
63172f87654SPavel Emelyanov 	ewq.msg.arg.remap.to = to;
63272f87654SPavel Emelyanov 	ewq.msg.arg.remap.len = len;
63372f87654SPavel Emelyanov 
63472f87654SPavel Emelyanov 	userfaultfd_event_wait_completion(ctx, &ewq);
63572f87654SPavel Emelyanov }
63672f87654SPavel Emelyanov 
63705ce7724SPavel Emelyanov void madvise_userfault_dontneed(struct vm_area_struct *vma,
63805ce7724SPavel Emelyanov 				struct vm_area_struct **prev,
63905ce7724SPavel Emelyanov 				unsigned long start, unsigned long end)
64005ce7724SPavel Emelyanov {
64105ce7724SPavel Emelyanov 	struct mm_struct *mm = vma->vm_mm;
64205ce7724SPavel Emelyanov 	struct userfaultfd_ctx *ctx;
64305ce7724SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
64405ce7724SPavel Emelyanov 
64505ce7724SPavel Emelyanov 	ctx = vma->vm_userfaultfd_ctx.ctx;
64605ce7724SPavel Emelyanov 	if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_MADVDONTNEED))
64705ce7724SPavel Emelyanov 		return;
64805ce7724SPavel Emelyanov 
64905ce7724SPavel Emelyanov 	userfaultfd_ctx_get(ctx);
65005ce7724SPavel Emelyanov 	up_read(&mm->mmap_sem);
65105ce7724SPavel Emelyanov 
65205ce7724SPavel Emelyanov 	*prev = NULL; /* We wait for ACK w/o the mmap semaphore */
65305ce7724SPavel Emelyanov 
65405ce7724SPavel Emelyanov 	msg_init(&ewq.msg);
65505ce7724SPavel Emelyanov 
65605ce7724SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_MADVDONTNEED;
65705ce7724SPavel Emelyanov 	ewq.msg.arg.madv_dn.start = start;
65805ce7724SPavel Emelyanov 	ewq.msg.arg.madv_dn.end = end;
65905ce7724SPavel Emelyanov 
66005ce7724SPavel Emelyanov 	userfaultfd_event_wait_completion(ctx, &ewq);
66105ce7724SPavel Emelyanov 
66205ce7724SPavel Emelyanov 	down_read(&mm->mmap_sem);
66305ce7724SPavel Emelyanov }
66405ce7724SPavel Emelyanov 
66586039bd3SAndrea Arcangeli static int userfaultfd_release(struct inode *inode, struct file *file)
66686039bd3SAndrea Arcangeli {
66786039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
66886039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
66986039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev;
67086039bd3SAndrea Arcangeli 	/* len == 0 means wake all */
67186039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range range = { .len = 0, };
67286039bd3SAndrea Arcangeli 	unsigned long new_flags;
67386039bd3SAndrea Arcangeli 
67486039bd3SAndrea Arcangeli 	ACCESS_ONCE(ctx->released) = true;
67586039bd3SAndrea Arcangeli 
676d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
677d2005e3fSOleg Nesterov 		goto wakeup;
678d2005e3fSOleg Nesterov 
67986039bd3SAndrea Arcangeli 	/*
68086039bd3SAndrea Arcangeli 	 * Flush page faults out of all CPUs. NOTE: all page faults
68186039bd3SAndrea Arcangeli 	 * must be retried without returning VM_FAULT_SIGBUS if
68286039bd3SAndrea Arcangeli 	 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
68386039bd3SAndrea Arcangeli 	 * changes while handle_userfault released the mmap_sem. So
68486039bd3SAndrea Arcangeli 	 * it's critical that released is set to true (above), before
68586039bd3SAndrea Arcangeli 	 * taking the mmap_sem for writing.
68686039bd3SAndrea Arcangeli 	 */
68786039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
68886039bd3SAndrea Arcangeli 	prev = NULL;
68986039bd3SAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
69086039bd3SAndrea Arcangeli 		cond_resched();
69186039bd3SAndrea Arcangeli 		BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
69286039bd3SAndrea Arcangeli 		       !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
69386039bd3SAndrea Arcangeli 		if (vma->vm_userfaultfd_ctx.ctx != ctx) {
69486039bd3SAndrea Arcangeli 			prev = vma;
69586039bd3SAndrea Arcangeli 			continue;
69686039bd3SAndrea Arcangeli 		}
69786039bd3SAndrea Arcangeli 		new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
69886039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
69986039bd3SAndrea Arcangeli 				 new_flags, vma->anon_vma,
70086039bd3SAndrea Arcangeli 				 vma->vm_file, vma->vm_pgoff,
70186039bd3SAndrea Arcangeli 				 vma_policy(vma),
70286039bd3SAndrea Arcangeli 				 NULL_VM_UFFD_CTX);
70386039bd3SAndrea Arcangeli 		if (prev)
70486039bd3SAndrea Arcangeli 			vma = prev;
70586039bd3SAndrea Arcangeli 		else
70686039bd3SAndrea Arcangeli 			prev = vma;
70786039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
70886039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
70986039bd3SAndrea Arcangeli 	}
71086039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
711d2005e3fSOleg Nesterov 	mmput(mm);
712d2005e3fSOleg Nesterov wakeup:
71386039bd3SAndrea Arcangeli 	/*
71415b726efSAndrea Arcangeli 	 * After no new page faults can wait on this fault_*wqh, flush
71586039bd3SAndrea Arcangeli 	 * the last page faults that may have been already waiting on
71615b726efSAndrea Arcangeli 	 * the fault_*wqh.
71786039bd3SAndrea Arcangeli 	 */
71815b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
719ac5be6b4SAndrea Arcangeli 	__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
720ac5be6b4SAndrea Arcangeli 	__wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
72115b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
72286039bd3SAndrea Arcangeli 
72386039bd3SAndrea Arcangeli 	wake_up_poll(&ctx->fd_wqh, POLLHUP);
72486039bd3SAndrea Arcangeli 	userfaultfd_ctx_put(ctx);
72586039bd3SAndrea Arcangeli 	return 0;
72686039bd3SAndrea Arcangeli }
72786039bd3SAndrea Arcangeli 
72815b726efSAndrea Arcangeli /* fault_pending_wqh.lock must be hold by the caller */
7296dcc27fdSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault_in(
7306dcc27fdSPavel Emelyanov 		wait_queue_head_t *wqh)
73186039bd3SAndrea Arcangeli {
73286039bd3SAndrea Arcangeli 	wait_queue_t *wq;
73315b726efSAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
73486039bd3SAndrea Arcangeli 
7356dcc27fdSPavel Emelyanov 	VM_BUG_ON(!spin_is_locked(&wqh->lock));
73686039bd3SAndrea Arcangeli 
73715b726efSAndrea Arcangeli 	uwq = NULL;
7386dcc27fdSPavel Emelyanov 	if (!waitqueue_active(wqh))
73915b726efSAndrea Arcangeli 		goto out;
74015b726efSAndrea Arcangeli 	/* walk in reverse to provide FIFO behavior to read userfaults */
7416dcc27fdSPavel Emelyanov 	wq = list_last_entry(&wqh->task_list, typeof(*wq), task_list);
74215b726efSAndrea Arcangeli 	uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
74315b726efSAndrea Arcangeli out:
74415b726efSAndrea Arcangeli 	return uwq;
74586039bd3SAndrea Arcangeli }
74686039bd3SAndrea Arcangeli 
7476dcc27fdSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault(
7486dcc27fdSPavel Emelyanov 		struct userfaultfd_ctx *ctx)
7496dcc27fdSPavel Emelyanov {
7506dcc27fdSPavel Emelyanov 	return find_userfault_in(&ctx->fault_pending_wqh);
7516dcc27fdSPavel Emelyanov }
7526dcc27fdSPavel Emelyanov 
7539cd75c3cSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault_evt(
7549cd75c3cSPavel Emelyanov 		struct userfaultfd_ctx *ctx)
7559cd75c3cSPavel Emelyanov {
7569cd75c3cSPavel Emelyanov 	return find_userfault_in(&ctx->event_wqh);
7579cd75c3cSPavel Emelyanov }
7589cd75c3cSPavel Emelyanov 
75986039bd3SAndrea Arcangeli static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
76086039bd3SAndrea Arcangeli {
76186039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
76286039bd3SAndrea Arcangeli 	unsigned int ret;
76386039bd3SAndrea Arcangeli 
76486039bd3SAndrea Arcangeli 	poll_wait(file, &ctx->fd_wqh, wait);
76586039bd3SAndrea Arcangeli 
76686039bd3SAndrea Arcangeli 	switch (ctx->state) {
76786039bd3SAndrea Arcangeli 	case UFFD_STATE_WAIT_API:
76886039bd3SAndrea Arcangeli 		return POLLERR;
76986039bd3SAndrea Arcangeli 	case UFFD_STATE_RUNNING:
770ba85c702SAndrea Arcangeli 		/*
771ba85c702SAndrea Arcangeli 		 * poll() never guarantees that read won't block.
772ba85c702SAndrea Arcangeli 		 * userfaults can be waken before they're read().
773ba85c702SAndrea Arcangeli 		 */
774ba85c702SAndrea Arcangeli 		if (unlikely(!(file->f_flags & O_NONBLOCK)))
775ba85c702SAndrea Arcangeli 			return POLLERR;
77615b726efSAndrea Arcangeli 		/*
77715b726efSAndrea Arcangeli 		 * lockless access to see if there are pending faults
77815b726efSAndrea Arcangeli 		 * __pollwait last action is the add_wait_queue but
77915b726efSAndrea Arcangeli 		 * the spin_unlock would allow the waitqueue_active to
78015b726efSAndrea Arcangeli 		 * pass above the actual list_add inside
78115b726efSAndrea Arcangeli 		 * add_wait_queue critical section. So use a full
78215b726efSAndrea Arcangeli 		 * memory barrier to serialize the list_add write of
78315b726efSAndrea Arcangeli 		 * add_wait_queue() with the waitqueue_active read
78415b726efSAndrea Arcangeli 		 * below.
78515b726efSAndrea Arcangeli 		 */
78615b726efSAndrea Arcangeli 		ret = 0;
78715b726efSAndrea Arcangeli 		smp_mb();
78815b726efSAndrea Arcangeli 		if (waitqueue_active(&ctx->fault_pending_wqh))
78915b726efSAndrea Arcangeli 			ret = POLLIN;
7909cd75c3cSPavel Emelyanov 		else if (waitqueue_active(&ctx->event_wqh))
7919cd75c3cSPavel Emelyanov 			ret = POLLIN;
7929cd75c3cSPavel Emelyanov 
79386039bd3SAndrea Arcangeli 		return ret;
79486039bd3SAndrea Arcangeli 	default:
7958474901aSAndrea Arcangeli 		WARN_ON_ONCE(1);
7968474901aSAndrea Arcangeli 		return POLLERR;
79786039bd3SAndrea Arcangeli 	}
79886039bd3SAndrea Arcangeli }
79986039bd3SAndrea Arcangeli 
800893e26e6SPavel Emelyanov static const struct file_operations userfaultfd_fops;
801893e26e6SPavel Emelyanov 
802893e26e6SPavel Emelyanov static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
803893e26e6SPavel Emelyanov 				  struct userfaultfd_ctx *new,
804893e26e6SPavel Emelyanov 				  struct uffd_msg *msg)
805893e26e6SPavel Emelyanov {
806893e26e6SPavel Emelyanov 	int fd;
807893e26e6SPavel Emelyanov 	struct file *file;
808893e26e6SPavel Emelyanov 	unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
809893e26e6SPavel Emelyanov 
810893e26e6SPavel Emelyanov 	fd = get_unused_fd_flags(flags);
811893e26e6SPavel Emelyanov 	if (fd < 0)
812893e26e6SPavel Emelyanov 		return fd;
813893e26e6SPavel Emelyanov 
814893e26e6SPavel Emelyanov 	file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
815893e26e6SPavel Emelyanov 				  O_RDWR | flags);
816893e26e6SPavel Emelyanov 	if (IS_ERR(file)) {
817893e26e6SPavel Emelyanov 		put_unused_fd(fd);
818893e26e6SPavel Emelyanov 		return PTR_ERR(file);
819893e26e6SPavel Emelyanov 	}
820893e26e6SPavel Emelyanov 
821893e26e6SPavel Emelyanov 	fd_install(fd, file);
822893e26e6SPavel Emelyanov 	msg->arg.reserved.reserved1 = 0;
823893e26e6SPavel Emelyanov 	msg->arg.fork.ufd = fd;
824893e26e6SPavel Emelyanov 
825893e26e6SPavel Emelyanov 	return 0;
826893e26e6SPavel Emelyanov }
827893e26e6SPavel Emelyanov 
82886039bd3SAndrea Arcangeli static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
829a9b85f94SAndrea Arcangeli 				    struct uffd_msg *msg)
83086039bd3SAndrea Arcangeli {
83186039bd3SAndrea Arcangeli 	ssize_t ret;
83286039bd3SAndrea Arcangeli 	DECLARE_WAITQUEUE(wait, current);
83315b726efSAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
834893e26e6SPavel Emelyanov 	/*
835893e26e6SPavel Emelyanov 	 * Handling fork event requires sleeping operations, so
836893e26e6SPavel Emelyanov 	 * we drop the event_wqh lock, then do these ops, then
837893e26e6SPavel Emelyanov 	 * lock it back and wake up the waiter. While the lock is
838893e26e6SPavel Emelyanov 	 * dropped the ewq may go away so we keep track of it
839893e26e6SPavel Emelyanov 	 * carefully.
840893e26e6SPavel Emelyanov 	 */
841893e26e6SPavel Emelyanov 	LIST_HEAD(fork_event);
842893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *fork_nctx = NULL;
84386039bd3SAndrea Arcangeli 
84415b726efSAndrea Arcangeli 	/* always take the fd_wqh lock before the fault_pending_wqh lock */
84586039bd3SAndrea Arcangeli 	spin_lock(&ctx->fd_wqh.lock);
84686039bd3SAndrea Arcangeli 	__add_wait_queue(&ctx->fd_wqh, &wait);
84786039bd3SAndrea Arcangeli 	for (;;) {
84886039bd3SAndrea Arcangeli 		set_current_state(TASK_INTERRUPTIBLE);
84915b726efSAndrea Arcangeli 		spin_lock(&ctx->fault_pending_wqh.lock);
85015b726efSAndrea Arcangeli 		uwq = find_userfault(ctx);
85115b726efSAndrea Arcangeli 		if (uwq) {
85286039bd3SAndrea Arcangeli 			/*
8532c5b7e1bSAndrea Arcangeli 			 * Use a seqcount to repeat the lockless check
8542c5b7e1bSAndrea Arcangeli 			 * in wake_userfault() to avoid missing
8552c5b7e1bSAndrea Arcangeli 			 * wakeups because during the refile both
8562c5b7e1bSAndrea Arcangeli 			 * waitqueue could become empty if this is the
8572c5b7e1bSAndrea Arcangeli 			 * only userfault.
8582c5b7e1bSAndrea Arcangeli 			 */
8592c5b7e1bSAndrea Arcangeli 			write_seqcount_begin(&ctx->refile_seq);
8602c5b7e1bSAndrea Arcangeli 
8612c5b7e1bSAndrea Arcangeli 			/*
86215b726efSAndrea Arcangeli 			 * The fault_pending_wqh.lock prevents the uwq
86315b726efSAndrea Arcangeli 			 * to disappear from under us.
86415b726efSAndrea Arcangeli 			 *
86515b726efSAndrea Arcangeli 			 * Refile this userfault from
86615b726efSAndrea Arcangeli 			 * fault_pending_wqh to fault_wqh, it's not
86715b726efSAndrea Arcangeli 			 * pending anymore after we read it.
86815b726efSAndrea Arcangeli 			 *
86915b726efSAndrea Arcangeli 			 * Use list_del() by hand (as
87015b726efSAndrea Arcangeli 			 * userfaultfd_wake_function also uses
87115b726efSAndrea Arcangeli 			 * list_del_init() by hand) to be sure nobody
87215b726efSAndrea Arcangeli 			 * changes __remove_wait_queue() to use
87315b726efSAndrea Arcangeli 			 * list_del_init() in turn breaking the
87415b726efSAndrea Arcangeli 			 * !list_empty_careful() check in
87515b726efSAndrea Arcangeli 			 * handle_userfault(). The uwq->wq.task_list
87615b726efSAndrea Arcangeli 			 * must never be empty at any time during the
87715b726efSAndrea Arcangeli 			 * refile, or the waitqueue could disappear
87815b726efSAndrea Arcangeli 			 * from under us. The "wait_queue_head_t"
87915b726efSAndrea Arcangeli 			 * parameter of __remove_wait_queue() is unused
88015b726efSAndrea Arcangeli 			 * anyway.
88186039bd3SAndrea Arcangeli 			 */
88215b726efSAndrea Arcangeli 			list_del(&uwq->wq.task_list);
88315b726efSAndrea Arcangeli 			__add_wait_queue(&ctx->fault_wqh, &uwq->wq);
88415b726efSAndrea Arcangeli 
8852c5b7e1bSAndrea Arcangeli 			write_seqcount_end(&ctx->refile_seq);
8862c5b7e1bSAndrea Arcangeli 
887a9b85f94SAndrea Arcangeli 			/* careful to always initialize msg if ret == 0 */
888a9b85f94SAndrea Arcangeli 			*msg = uwq->msg;
88915b726efSAndrea Arcangeli 			spin_unlock(&ctx->fault_pending_wqh.lock);
89086039bd3SAndrea Arcangeli 			ret = 0;
89186039bd3SAndrea Arcangeli 			break;
89286039bd3SAndrea Arcangeli 		}
89315b726efSAndrea Arcangeli 		spin_unlock(&ctx->fault_pending_wqh.lock);
8949cd75c3cSPavel Emelyanov 
8959cd75c3cSPavel Emelyanov 		spin_lock(&ctx->event_wqh.lock);
8969cd75c3cSPavel Emelyanov 		uwq = find_userfault_evt(ctx);
8979cd75c3cSPavel Emelyanov 		if (uwq) {
8989cd75c3cSPavel Emelyanov 			*msg = uwq->msg;
8999cd75c3cSPavel Emelyanov 
900893e26e6SPavel Emelyanov 			if (uwq->msg.event == UFFD_EVENT_FORK) {
901893e26e6SPavel Emelyanov 				fork_nctx = (struct userfaultfd_ctx *)
902893e26e6SPavel Emelyanov 					(unsigned long)
903893e26e6SPavel Emelyanov 					uwq->msg.arg.reserved.reserved1;
904893e26e6SPavel Emelyanov 				list_move(&uwq->wq.task_list, &fork_event);
905893e26e6SPavel Emelyanov 				spin_unlock(&ctx->event_wqh.lock);
906893e26e6SPavel Emelyanov 				ret = 0;
907893e26e6SPavel Emelyanov 				break;
908893e26e6SPavel Emelyanov 			}
909893e26e6SPavel Emelyanov 
9109cd75c3cSPavel Emelyanov 			userfaultfd_event_complete(ctx, uwq);
9119cd75c3cSPavel Emelyanov 			spin_unlock(&ctx->event_wqh.lock);
9129cd75c3cSPavel Emelyanov 			ret = 0;
9139cd75c3cSPavel Emelyanov 			break;
9149cd75c3cSPavel Emelyanov 		}
9159cd75c3cSPavel Emelyanov 		spin_unlock(&ctx->event_wqh.lock);
9169cd75c3cSPavel Emelyanov 
91786039bd3SAndrea Arcangeli 		if (signal_pending(current)) {
91886039bd3SAndrea Arcangeli 			ret = -ERESTARTSYS;
91986039bd3SAndrea Arcangeli 			break;
92086039bd3SAndrea Arcangeli 		}
92186039bd3SAndrea Arcangeli 		if (no_wait) {
92286039bd3SAndrea Arcangeli 			ret = -EAGAIN;
92386039bd3SAndrea Arcangeli 			break;
92486039bd3SAndrea Arcangeli 		}
92586039bd3SAndrea Arcangeli 		spin_unlock(&ctx->fd_wqh.lock);
92686039bd3SAndrea Arcangeli 		schedule();
92786039bd3SAndrea Arcangeli 		spin_lock(&ctx->fd_wqh.lock);
92886039bd3SAndrea Arcangeli 	}
92986039bd3SAndrea Arcangeli 	__remove_wait_queue(&ctx->fd_wqh, &wait);
93086039bd3SAndrea Arcangeli 	__set_current_state(TASK_RUNNING);
93186039bd3SAndrea Arcangeli 	spin_unlock(&ctx->fd_wqh.lock);
93286039bd3SAndrea Arcangeli 
933893e26e6SPavel Emelyanov 	if (!ret && msg->event == UFFD_EVENT_FORK) {
934893e26e6SPavel Emelyanov 		ret = resolve_userfault_fork(ctx, fork_nctx, msg);
935893e26e6SPavel Emelyanov 
936893e26e6SPavel Emelyanov 		if (!ret) {
937893e26e6SPavel Emelyanov 			spin_lock(&ctx->event_wqh.lock);
938893e26e6SPavel Emelyanov 			if (!list_empty(&fork_event)) {
939893e26e6SPavel Emelyanov 				uwq = list_first_entry(&fork_event,
940893e26e6SPavel Emelyanov 						       typeof(*uwq),
941893e26e6SPavel Emelyanov 						       wq.task_list);
942893e26e6SPavel Emelyanov 				list_del(&uwq->wq.task_list);
943893e26e6SPavel Emelyanov 				__add_wait_queue(&ctx->event_wqh, &uwq->wq);
944893e26e6SPavel Emelyanov 				userfaultfd_event_complete(ctx, uwq);
945893e26e6SPavel Emelyanov 			}
946893e26e6SPavel Emelyanov 			spin_unlock(&ctx->event_wqh.lock);
947893e26e6SPavel Emelyanov 		}
948893e26e6SPavel Emelyanov 	}
949893e26e6SPavel Emelyanov 
95086039bd3SAndrea Arcangeli 	return ret;
95186039bd3SAndrea Arcangeli }
95286039bd3SAndrea Arcangeli 
95386039bd3SAndrea Arcangeli static ssize_t userfaultfd_read(struct file *file, char __user *buf,
95486039bd3SAndrea Arcangeli 				size_t count, loff_t *ppos)
95586039bd3SAndrea Arcangeli {
95686039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
95786039bd3SAndrea Arcangeli 	ssize_t _ret, ret = 0;
958a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
95986039bd3SAndrea Arcangeli 	int no_wait = file->f_flags & O_NONBLOCK;
96086039bd3SAndrea Arcangeli 
96186039bd3SAndrea Arcangeli 	if (ctx->state == UFFD_STATE_WAIT_API)
96286039bd3SAndrea Arcangeli 		return -EINVAL;
96386039bd3SAndrea Arcangeli 
96486039bd3SAndrea Arcangeli 	for (;;) {
965a9b85f94SAndrea Arcangeli 		if (count < sizeof(msg))
96686039bd3SAndrea Arcangeli 			return ret ? ret : -EINVAL;
967a9b85f94SAndrea Arcangeli 		_ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
96886039bd3SAndrea Arcangeli 		if (_ret < 0)
96986039bd3SAndrea Arcangeli 			return ret ? ret : _ret;
970a9b85f94SAndrea Arcangeli 		if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
97186039bd3SAndrea Arcangeli 			return ret ? ret : -EFAULT;
972a9b85f94SAndrea Arcangeli 		ret += sizeof(msg);
973a9b85f94SAndrea Arcangeli 		buf += sizeof(msg);
974a9b85f94SAndrea Arcangeli 		count -= sizeof(msg);
97586039bd3SAndrea Arcangeli 		/*
97686039bd3SAndrea Arcangeli 		 * Allow to read more than one fault at time but only
97786039bd3SAndrea Arcangeli 		 * block if waiting for the very first one.
97886039bd3SAndrea Arcangeli 		 */
97986039bd3SAndrea Arcangeli 		no_wait = O_NONBLOCK;
98086039bd3SAndrea Arcangeli 	}
98186039bd3SAndrea Arcangeli }
98286039bd3SAndrea Arcangeli 
98386039bd3SAndrea Arcangeli static void __wake_userfault(struct userfaultfd_ctx *ctx,
98486039bd3SAndrea Arcangeli 			     struct userfaultfd_wake_range *range)
98586039bd3SAndrea Arcangeli {
98686039bd3SAndrea Arcangeli 	unsigned long start, end;
98786039bd3SAndrea Arcangeli 
98886039bd3SAndrea Arcangeli 	start = range->start;
98986039bd3SAndrea Arcangeli 	end = range->start + range->len;
99086039bd3SAndrea Arcangeli 
99115b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
99286039bd3SAndrea Arcangeli 	/* wake all in the range and autoremove */
99315b726efSAndrea Arcangeli 	if (waitqueue_active(&ctx->fault_pending_wqh))
994ac5be6b4SAndrea Arcangeli 		__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
99515b726efSAndrea Arcangeli 				     range);
99615b726efSAndrea Arcangeli 	if (waitqueue_active(&ctx->fault_wqh))
997ac5be6b4SAndrea Arcangeli 		__wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
99815b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
99986039bd3SAndrea Arcangeli }
100086039bd3SAndrea Arcangeli 
100186039bd3SAndrea Arcangeli static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
100286039bd3SAndrea Arcangeli 					   struct userfaultfd_wake_range *range)
100386039bd3SAndrea Arcangeli {
10042c5b7e1bSAndrea Arcangeli 	unsigned seq;
10052c5b7e1bSAndrea Arcangeli 	bool need_wakeup;
10062c5b7e1bSAndrea Arcangeli 
100786039bd3SAndrea Arcangeli 	/*
100886039bd3SAndrea Arcangeli 	 * To be sure waitqueue_active() is not reordered by the CPU
100986039bd3SAndrea Arcangeli 	 * before the pagetable update, use an explicit SMP memory
101086039bd3SAndrea Arcangeli 	 * barrier here. PT lock release or up_read(mmap_sem) still
101186039bd3SAndrea Arcangeli 	 * have release semantics that can allow the
101286039bd3SAndrea Arcangeli 	 * waitqueue_active() to be reordered before the pte update.
101386039bd3SAndrea Arcangeli 	 */
101486039bd3SAndrea Arcangeli 	smp_mb();
101586039bd3SAndrea Arcangeli 
101686039bd3SAndrea Arcangeli 	/*
101786039bd3SAndrea Arcangeli 	 * Use waitqueue_active because it's very frequent to
101886039bd3SAndrea Arcangeli 	 * change the address space atomically even if there are no
101986039bd3SAndrea Arcangeli 	 * userfaults yet. So we take the spinlock only when we're
102086039bd3SAndrea Arcangeli 	 * sure we've userfaults to wake.
102186039bd3SAndrea Arcangeli 	 */
10222c5b7e1bSAndrea Arcangeli 	do {
10232c5b7e1bSAndrea Arcangeli 		seq = read_seqcount_begin(&ctx->refile_seq);
10242c5b7e1bSAndrea Arcangeli 		need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
10252c5b7e1bSAndrea Arcangeli 			waitqueue_active(&ctx->fault_wqh);
10262c5b7e1bSAndrea Arcangeli 		cond_resched();
10272c5b7e1bSAndrea Arcangeli 	} while (read_seqcount_retry(&ctx->refile_seq, seq));
10282c5b7e1bSAndrea Arcangeli 	if (need_wakeup)
102986039bd3SAndrea Arcangeli 		__wake_userfault(ctx, range);
103086039bd3SAndrea Arcangeli }
103186039bd3SAndrea Arcangeli 
103286039bd3SAndrea Arcangeli static __always_inline int validate_range(struct mm_struct *mm,
103386039bd3SAndrea Arcangeli 					  __u64 start, __u64 len)
103486039bd3SAndrea Arcangeli {
103586039bd3SAndrea Arcangeli 	__u64 task_size = mm->task_size;
103686039bd3SAndrea Arcangeli 
103786039bd3SAndrea Arcangeli 	if (start & ~PAGE_MASK)
103886039bd3SAndrea Arcangeli 		return -EINVAL;
103986039bd3SAndrea Arcangeli 	if (len & ~PAGE_MASK)
104086039bd3SAndrea Arcangeli 		return -EINVAL;
104186039bd3SAndrea Arcangeli 	if (!len)
104286039bd3SAndrea Arcangeli 		return -EINVAL;
104386039bd3SAndrea Arcangeli 	if (start < mmap_min_addr)
104486039bd3SAndrea Arcangeli 		return -EINVAL;
104586039bd3SAndrea Arcangeli 	if (start >= task_size)
104686039bd3SAndrea Arcangeli 		return -EINVAL;
104786039bd3SAndrea Arcangeli 	if (len > task_size - start)
104886039bd3SAndrea Arcangeli 		return -EINVAL;
104986039bd3SAndrea Arcangeli 	return 0;
105086039bd3SAndrea Arcangeli }
105186039bd3SAndrea Arcangeli 
105286039bd3SAndrea Arcangeli static int userfaultfd_register(struct userfaultfd_ctx *ctx,
105386039bd3SAndrea Arcangeli 				unsigned long arg)
105486039bd3SAndrea Arcangeli {
105586039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
105686039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev, *cur;
105786039bd3SAndrea Arcangeli 	int ret;
105886039bd3SAndrea Arcangeli 	struct uffdio_register uffdio_register;
105986039bd3SAndrea Arcangeli 	struct uffdio_register __user *user_uffdio_register;
106086039bd3SAndrea Arcangeli 	unsigned long vm_flags, new_flags;
106186039bd3SAndrea Arcangeli 	bool found;
1062*cab350afSMike Kravetz 	bool huge_pages;
106386039bd3SAndrea Arcangeli 	unsigned long start, end, vma_end;
106486039bd3SAndrea Arcangeli 
106586039bd3SAndrea Arcangeli 	user_uffdio_register = (struct uffdio_register __user *) arg;
106686039bd3SAndrea Arcangeli 
106786039bd3SAndrea Arcangeli 	ret = -EFAULT;
106886039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_register, user_uffdio_register,
106986039bd3SAndrea Arcangeli 			   sizeof(uffdio_register)-sizeof(__u64)))
107086039bd3SAndrea Arcangeli 		goto out;
107186039bd3SAndrea Arcangeli 
107286039bd3SAndrea Arcangeli 	ret = -EINVAL;
107386039bd3SAndrea Arcangeli 	if (!uffdio_register.mode)
107486039bd3SAndrea Arcangeli 		goto out;
107586039bd3SAndrea Arcangeli 	if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
107686039bd3SAndrea Arcangeli 				     UFFDIO_REGISTER_MODE_WP))
107786039bd3SAndrea Arcangeli 		goto out;
107886039bd3SAndrea Arcangeli 	vm_flags = 0;
107986039bd3SAndrea Arcangeli 	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
108086039bd3SAndrea Arcangeli 		vm_flags |= VM_UFFD_MISSING;
108186039bd3SAndrea Arcangeli 	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
108286039bd3SAndrea Arcangeli 		vm_flags |= VM_UFFD_WP;
108386039bd3SAndrea Arcangeli 		/*
108486039bd3SAndrea Arcangeli 		 * FIXME: remove the below error constraint by
108586039bd3SAndrea Arcangeli 		 * implementing the wprotect tracking mode.
108686039bd3SAndrea Arcangeli 		 */
108786039bd3SAndrea Arcangeli 		ret = -EINVAL;
108886039bd3SAndrea Arcangeli 		goto out;
108986039bd3SAndrea Arcangeli 	}
109086039bd3SAndrea Arcangeli 
109186039bd3SAndrea Arcangeli 	ret = validate_range(mm, uffdio_register.range.start,
109286039bd3SAndrea Arcangeli 			     uffdio_register.range.len);
109386039bd3SAndrea Arcangeli 	if (ret)
109486039bd3SAndrea Arcangeli 		goto out;
109586039bd3SAndrea Arcangeli 
109686039bd3SAndrea Arcangeli 	start = uffdio_register.range.start;
109786039bd3SAndrea Arcangeli 	end = start + uffdio_register.range.len;
109886039bd3SAndrea Arcangeli 
1099d2005e3fSOleg Nesterov 	ret = -ENOMEM;
1100d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
1101d2005e3fSOleg Nesterov 		goto out;
1102d2005e3fSOleg Nesterov 
110386039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
110486039bd3SAndrea Arcangeli 	vma = find_vma_prev(mm, start, &prev);
110586039bd3SAndrea Arcangeli 	if (!vma)
110686039bd3SAndrea Arcangeli 		goto out_unlock;
110786039bd3SAndrea Arcangeli 
110886039bd3SAndrea Arcangeli 	/* check that there's at least one vma in the range */
110986039bd3SAndrea Arcangeli 	ret = -EINVAL;
111086039bd3SAndrea Arcangeli 	if (vma->vm_start >= end)
111186039bd3SAndrea Arcangeli 		goto out_unlock;
111286039bd3SAndrea Arcangeli 
111386039bd3SAndrea Arcangeli 	/*
1114*cab350afSMike Kravetz 	 * If the first vma contains huge pages, make sure start address
1115*cab350afSMike Kravetz 	 * is aligned to huge page size.
1116*cab350afSMike Kravetz 	 */
1117*cab350afSMike Kravetz 	if (is_vm_hugetlb_page(vma)) {
1118*cab350afSMike Kravetz 		unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1119*cab350afSMike Kravetz 
1120*cab350afSMike Kravetz 		if (start & (vma_hpagesize - 1))
1121*cab350afSMike Kravetz 			goto out_unlock;
1122*cab350afSMike Kravetz 	}
1123*cab350afSMike Kravetz 
1124*cab350afSMike Kravetz 	/*
112586039bd3SAndrea Arcangeli 	 * Search for not compatible vmas.
112686039bd3SAndrea Arcangeli 	 *
112786039bd3SAndrea Arcangeli 	 * FIXME: this shall be relaxed later so that it doesn't fail
112886039bd3SAndrea Arcangeli 	 * on tmpfs backed vmas (in addition to the current allowance
112986039bd3SAndrea Arcangeli 	 * on anonymous vmas).
113086039bd3SAndrea Arcangeli 	 */
113186039bd3SAndrea Arcangeli 	found = false;
1132*cab350afSMike Kravetz 	huge_pages = false;
113386039bd3SAndrea Arcangeli 	for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
113486039bd3SAndrea Arcangeli 		cond_resched();
113586039bd3SAndrea Arcangeli 
113686039bd3SAndrea Arcangeli 		BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
113786039bd3SAndrea Arcangeli 		       !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
113886039bd3SAndrea Arcangeli 
113986039bd3SAndrea Arcangeli 		/* check not compatible vmas */
114086039bd3SAndrea Arcangeli 		ret = -EINVAL;
1141*cab350afSMike Kravetz 		if (!vma_is_anonymous(cur) && !is_vm_hugetlb_page(cur))
114286039bd3SAndrea Arcangeli 			goto out_unlock;
1143*cab350afSMike Kravetz 		/*
1144*cab350afSMike Kravetz 		 * If this vma contains ending address, and huge pages
1145*cab350afSMike Kravetz 		 * check alignment.
1146*cab350afSMike Kravetz 		 */
1147*cab350afSMike Kravetz 		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1148*cab350afSMike Kravetz 		    end > cur->vm_start) {
1149*cab350afSMike Kravetz 			unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1150*cab350afSMike Kravetz 
1151*cab350afSMike Kravetz 			ret = -EINVAL;
1152*cab350afSMike Kravetz 
1153*cab350afSMike Kravetz 			if (end & (vma_hpagesize - 1))
1154*cab350afSMike Kravetz 				goto out_unlock;
1155*cab350afSMike Kravetz 		}
115686039bd3SAndrea Arcangeli 
115786039bd3SAndrea Arcangeli 		/*
115886039bd3SAndrea Arcangeli 		 * Check that this vma isn't already owned by a
115986039bd3SAndrea Arcangeli 		 * different userfaultfd. We can't allow more than one
116086039bd3SAndrea Arcangeli 		 * userfaultfd to own a single vma simultaneously or we
116186039bd3SAndrea Arcangeli 		 * wouldn't know which one to deliver the userfaults to.
116286039bd3SAndrea Arcangeli 		 */
116386039bd3SAndrea Arcangeli 		ret = -EBUSY;
116486039bd3SAndrea Arcangeli 		if (cur->vm_userfaultfd_ctx.ctx &&
116586039bd3SAndrea Arcangeli 		    cur->vm_userfaultfd_ctx.ctx != ctx)
116686039bd3SAndrea Arcangeli 			goto out_unlock;
116786039bd3SAndrea Arcangeli 
1168*cab350afSMike Kravetz 		/*
1169*cab350afSMike Kravetz 		 * Note vmas containing huge pages
1170*cab350afSMike Kravetz 		 */
1171*cab350afSMike Kravetz 		if (is_vm_hugetlb_page(cur))
1172*cab350afSMike Kravetz 			huge_pages = true;
1173*cab350afSMike Kravetz 
117486039bd3SAndrea Arcangeli 		found = true;
117586039bd3SAndrea Arcangeli 	}
117686039bd3SAndrea Arcangeli 	BUG_ON(!found);
117786039bd3SAndrea Arcangeli 
117886039bd3SAndrea Arcangeli 	if (vma->vm_start < start)
117986039bd3SAndrea Arcangeli 		prev = vma;
118086039bd3SAndrea Arcangeli 
118186039bd3SAndrea Arcangeli 	ret = 0;
118286039bd3SAndrea Arcangeli 	do {
118386039bd3SAndrea Arcangeli 		cond_resched();
118486039bd3SAndrea Arcangeli 
1185*cab350afSMike Kravetz 		BUG_ON(!vma_is_anonymous(vma) && !is_vm_hugetlb_page(vma));
118686039bd3SAndrea Arcangeli 		BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
118786039bd3SAndrea Arcangeli 		       vma->vm_userfaultfd_ctx.ctx != ctx);
118886039bd3SAndrea Arcangeli 
118986039bd3SAndrea Arcangeli 		/*
119086039bd3SAndrea Arcangeli 		 * Nothing to do: this vma is already registered into this
119186039bd3SAndrea Arcangeli 		 * userfaultfd and with the right tracking mode too.
119286039bd3SAndrea Arcangeli 		 */
119386039bd3SAndrea Arcangeli 		if (vma->vm_userfaultfd_ctx.ctx == ctx &&
119486039bd3SAndrea Arcangeli 		    (vma->vm_flags & vm_flags) == vm_flags)
119586039bd3SAndrea Arcangeli 			goto skip;
119686039bd3SAndrea Arcangeli 
119786039bd3SAndrea Arcangeli 		if (vma->vm_start > start)
119886039bd3SAndrea Arcangeli 			start = vma->vm_start;
119986039bd3SAndrea Arcangeli 		vma_end = min(end, vma->vm_end);
120086039bd3SAndrea Arcangeli 
120186039bd3SAndrea Arcangeli 		new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
120286039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
120386039bd3SAndrea Arcangeli 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
120486039bd3SAndrea Arcangeli 				 vma_policy(vma),
120586039bd3SAndrea Arcangeli 				 ((struct vm_userfaultfd_ctx){ ctx }));
120686039bd3SAndrea Arcangeli 		if (prev) {
120786039bd3SAndrea Arcangeli 			vma = prev;
120886039bd3SAndrea Arcangeli 			goto next;
120986039bd3SAndrea Arcangeli 		}
121086039bd3SAndrea Arcangeli 		if (vma->vm_start < start) {
121186039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, start, 1);
121286039bd3SAndrea Arcangeli 			if (ret)
121386039bd3SAndrea Arcangeli 				break;
121486039bd3SAndrea Arcangeli 		}
121586039bd3SAndrea Arcangeli 		if (vma->vm_end > end) {
121686039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, end, 0);
121786039bd3SAndrea Arcangeli 			if (ret)
121886039bd3SAndrea Arcangeli 				break;
121986039bd3SAndrea Arcangeli 		}
122086039bd3SAndrea Arcangeli 	next:
122186039bd3SAndrea Arcangeli 		/*
122286039bd3SAndrea Arcangeli 		 * In the vma_merge() successful mprotect-like case 8:
122386039bd3SAndrea Arcangeli 		 * the next vma was merged into the current one and
122486039bd3SAndrea Arcangeli 		 * the current one has not been updated yet.
122586039bd3SAndrea Arcangeli 		 */
122686039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
122786039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx.ctx = ctx;
122886039bd3SAndrea Arcangeli 
122986039bd3SAndrea Arcangeli 	skip:
123086039bd3SAndrea Arcangeli 		prev = vma;
123186039bd3SAndrea Arcangeli 		start = vma->vm_end;
123286039bd3SAndrea Arcangeli 		vma = vma->vm_next;
123386039bd3SAndrea Arcangeli 	} while (vma && vma->vm_start < end);
123486039bd3SAndrea Arcangeli out_unlock:
123586039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
1236d2005e3fSOleg Nesterov 	mmput(mm);
123786039bd3SAndrea Arcangeli 	if (!ret) {
123886039bd3SAndrea Arcangeli 		/*
123986039bd3SAndrea Arcangeli 		 * Now that we scanned all vmas we can already tell
124086039bd3SAndrea Arcangeli 		 * userland which ioctls methods are guaranteed to
124186039bd3SAndrea Arcangeli 		 * succeed on this range.
124286039bd3SAndrea Arcangeli 		 */
1243*cab350afSMike Kravetz 		if (put_user(huge_pages ? UFFD_API_RANGE_IOCTLS_HPAGE :
1244*cab350afSMike Kravetz 			     UFFD_API_RANGE_IOCTLS,
124586039bd3SAndrea Arcangeli 			     &user_uffdio_register->ioctls))
124686039bd3SAndrea Arcangeli 			ret = -EFAULT;
124786039bd3SAndrea Arcangeli 	}
124886039bd3SAndrea Arcangeli out:
124986039bd3SAndrea Arcangeli 	return ret;
125086039bd3SAndrea Arcangeli }
125186039bd3SAndrea Arcangeli 
125286039bd3SAndrea Arcangeli static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
125386039bd3SAndrea Arcangeli 				  unsigned long arg)
125486039bd3SAndrea Arcangeli {
125586039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
125686039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev, *cur;
125786039bd3SAndrea Arcangeli 	int ret;
125886039bd3SAndrea Arcangeli 	struct uffdio_range uffdio_unregister;
125986039bd3SAndrea Arcangeli 	unsigned long new_flags;
126086039bd3SAndrea Arcangeli 	bool found;
126186039bd3SAndrea Arcangeli 	unsigned long start, end, vma_end;
126286039bd3SAndrea Arcangeli 	const void __user *buf = (void __user *)arg;
126386039bd3SAndrea Arcangeli 
126486039bd3SAndrea Arcangeli 	ret = -EFAULT;
126586039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
126686039bd3SAndrea Arcangeli 		goto out;
126786039bd3SAndrea Arcangeli 
126886039bd3SAndrea Arcangeli 	ret = validate_range(mm, uffdio_unregister.start,
126986039bd3SAndrea Arcangeli 			     uffdio_unregister.len);
127086039bd3SAndrea Arcangeli 	if (ret)
127186039bd3SAndrea Arcangeli 		goto out;
127286039bd3SAndrea Arcangeli 
127386039bd3SAndrea Arcangeli 	start = uffdio_unregister.start;
127486039bd3SAndrea Arcangeli 	end = start + uffdio_unregister.len;
127586039bd3SAndrea Arcangeli 
1276d2005e3fSOleg Nesterov 	ret = -ENOMEM;
1277d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
1278d2005e3fSOleg Nesterov 		goto out;
1279d2005e3fSOleg Nesterov 
128086039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
128186039bd3SAndrea Arcangeli 	vma = find_vma_prev(mm, start, &prev);
128286039bd3SAndrea Arcangeli 	if (!vma)
128386039bd3SAndrea Arcangeli 		goto out_unlock;
128486039bd3SAndrea Arcangeli 
128586039bd3SAndrea Arcangeli 	/* check that there's at least one vma in the range */
128686039bd3SAndrea Arcangeli 	ret = -EINVAL;
128786039bd3SAndrea Arcangeli 	if (vma->vm_start >= end)
128886039bd3SAndrea Arcangeli 		goto out_unlock;
128986039bd3SAndrea Arcangeli 
129086039bd3SAndrea Arcangeli 	/*
1291*cab350afSMike Kravetz 	 * If the first vma contains huge pages, make sure start address
1292*cab350afSMike Kravetz 	 * is aligned to huge page size.
1293*cab350afSMike Kravetz 	 */
1294*cab350afSMike Kravetz 	if (is_vm_hugetlb_page(vma)) {
1295*cab350afSMike Kravetz 		unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1296*cab350afSMike Kravetz 
1297*cab350afSMike Kravetz 		if (start & (vma_hpagesize - 1))
1298*cab350afSMike Kravetz 			goto out_unlock;
1299*cab350afSMike Kravetz 	}
1300*cab350afSMike Kravetz 
1301*cab350afSMike Kravetz 	/*
130286039bd3SAndrea Arcangeli 	 * Search for not compatible vmas.
130386039bd3SAndrea Arcangeli 	 *
130486039bd3SAndrea Arcangeli 	 * FIXME: this shall be relaxed later so that it doesn't fail
130586039bd3SAndrea Arcangeli 	 * on tmpfs backed vmas (in addition to the current allowance
130686039bd3SAndrea Arcangeli 	 * on anonymous vmas).
130786039bd3SAndrea Arcangeli 	 */
130886039bd3SAndrea Arcangeli 	found = false;
130986039bd3SAndrea Arcangeli 	ret = -EINVAL;
131086039bd3SAndrea Arcangeli 	for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
131186039bd3SAndrea Arcangeli 		cond_resched();
131286039bd3SAndrea Arcangeli 
131386039bd3SAndrea Arcangeli 		BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
131486039bd3SAndrea Arcangeli 		       !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
131586039bd3SAndrea Arcangeli 
131686039bd3SAndrea Arcangeli 		/*
131786039bd3SAndrea Arcangeli 		 * Check not compatible vmas, not strictly required
131886039bd3SAndrea Arcangeli 		 * here as not compatible vmas cannot have an
131986039bd3SAndrea Arcangeli 		 * userfaultfd_ctx registered on them, but this
132086039bd3SAndrea Arcangeli 		 * provides for more strict behavior to notice
132186039bd3SAndrea Arcangeli 		 * unregistration errors.
132286039bd3SAndrea Arcangeli 		 */
1323*cab350afSMike Kravetz 		if (!vma_is_anonymous(cur) && !is_vm_hugetlb_page(cur))
132486039bd3SAndrea Arcangeli 			goto out_unlock;
132586039bd3SAndrea Arcangeli 
132686039bd3SAndrea Arcangeli 		found = true;
132786039bd3SAndrea Arcangeli 	}
132886039bd3SAndrea Arcangeli 	BUG_ON(!found);
132986039bd3SAndrea Arcangeli 
133086039bd3SAndrea Arcangeli 	if (vma->vm_start < start)
133186039bd3SAndrea Arcangeli 		prev = vma;
133286039bd3SAndrea Arcangeli 
133386039bd3SAndrea Arcangeli 	ret = 0;
133486039bd3SAndrea Arcangeli 	do {
133586039bd3SAndrea Arcangeli 		cond_resched();
133686039bd3SAndrea Arcangeli 
1337*cab350afSMike Kravetz 		BUG_ON(!vma_is_anonymous(vma) && !is_vm_hugetlb_page(vma));
133886039bd3SAndrea Arcangeli 
133986039bd3SAndrea Arcangeli 		/*
134086039bd3SAndrea Arcangeli 		 * Nothing to do: this vma is already registered into this
134186039bd3SAndrea Arcangeli 		 * userfaultfd and with the right tracking mode too.
134286039bd3SAndrea Arcangeli 		 */
134386039bd3SAndrea Arcangeli 		if (!vma->vm_userfaultfd_ctx.ctx)
134486039bd3SAndrea Arcangeli 			goto skip;
134586039bd3SAndrea Arcangeli 
134686039bd3SAndrea Arcangeli 		if (vma->vm_start > start)
134786039bd3SAndrea Arcangeli 			start = vma->vm_start;
134886039bd3SAndrea Arcangeli 		vma_end = min(end, vma->vm_end);
134986039bd3SAndrea Arcangeli 
135009fa5296SAndrea Arcangeli 		if (userfaultfd_missing(vma)) {
135109fa5296SAndrea Arcangeli 			/*
135209fa5296SAndrea Arcangeli 			 * Wake any concurrent pending userfault while
135309fa5296SAndrea Arcangeli 			 * we unregister, so they will not hang
135409fa5296SAndrea Arcangeli 			 * permanently and it avoids userland to call
135509fa5296SAndrea Arcangeli 			 * UFFDIO_WAKE explicitly.
135609fa5296SAndrea Arcangeli 			 */
135709fa5296SAndrea Arcangeli 			struct userfaultfd_wake_range range;
135809fa5296SAndrea Arcangeli 			range.start = start;
135909fa5296SAndrea Arcangeli 			range.len = vma_end - start;
136009fa5296SAndrea Arcangeli 			wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
136109fa5296SAndrea Arcangeli 		}
136209fa5296SAndrea Arcangeli 
136386039bd3SAndrea Arcangeli 		new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
136486039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
136586039bd3SAndrea Arcangeli 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
136686039bd3SAndrea Arcangeli 				 vma_policy(vma),
136786039bd3SAndrea Arcangeli 				 NULL_VM_UFFD_CTX);
136886039bd3SAndrea Arcangeli 		if (prev) {
136986039bd3SAndrea Arcangeli 			vma = prev;
137086039bd3SAndrea Arcangeli 			goto next;
137186039bd3SAndrea Arcangeli 		}
137286039bd3SAndrea Arcangeli 		if (vma->vm_start < start) {
137386039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, start, 1);
137486039bd3SAndrea Arcangeli 			if (ret)
137586039bd3SAndrea Arcangeli 				break;
137686039bd3SAndrea Arcangeli 		}
137786039bd3SAndrea Arcangeli 		if (vma->vm_end > end) {
137886039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, end, 0);
137986039bd3SAndrea Arcangeli 			if (ret)
138086039bd3SAndrea Arcangeli 				break;
138186039bd3SAndrea Arcangeli 		}
138286039bd3SAndrea Arcangeli 	next:
138386039bd3SAndrea Arcangeli 		/*
138486039bd3SAndrea Arcangeli 		 * In the vma_merge() successful mprotect-like case 8:
138586039bd3SAndrea Arcangeli 		 * the next vma was merged into the current one and
138686039bd3SAndrea Arcangeli 		 * the current one has not been updated yet.
138786039bd3SAndrea Arcangeli 		 */
138886039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
138986039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
139086039bd3SAndrea Arcangeli 
139186039bd3SAndrea Arcangeli 	skip:
139286039bd3SAndrea Arcangeli 		prev = vma;
139386039bd3SAndrea Arcangeli 		start = vma->vm_end;
139486039bd3SAndrea Arcangeli 		vma = vma->vm_next;
139586039bd3SAndrea Arcangeli 	} while (vma && vma->vm_start < end);
139686039bd3SAndrea Arcangeli out_unlock:
139786039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
1398d2005e3fSOleg Nesterov 	mmput(mm);
139986039bd3SAndrea Arcangeli out:
140086039bd3SAndrea Arcangeli 	return ret;
140186039bd3SAndrea Arcangeli }
140286039bd3SAndrea Arcangeli 
140386039bd3SAndrea Arcangeli /*
1404ba85c702SAndrea Arcangeli  * userfaultfd_wake may be used in combination with the
1405ba85c702SAndrea Arcangeli  * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
140686039bd3SAndrea Arcangeli  */
140786039bd3SAndrea Arcangeli static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
140886039bd3SAndrea Arcangeli 			    unsigned long arg)
140986039bd3SAndrea Arcangeli {
141086039bd3SAndrea Arcangeli 	int ret;
141186039bd3SAndrea Arcangeli 	struct uffdio_range uffdio_wake;
141286039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range range;
141386039bd3SAndrea Arcangeli 	const void __user *buf = (void __user *)arg;
141486039bd3SAndrea Arcangeli 
141586039bd3SAndrea Arcangeli 	ret = -EFAULT;
141686039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
141786039bd3SAndrea Arcangeli 		goto out;
141886039bd3SAndrea Arcangeli 
141986039bd3SAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
142086039bd3SAndrea Arcangeli 	if (ret)
142186039bd3SAndrea Arcangeli 		goto out;
142286039bd3SAndrea Arcangeli 
142386039bd3SAndrea Arcangeli 	range.start = uffdio_wake.start;
142486039bd3SAndrea Arcangeli 	range.len = uffdio_wake.len;
142586039bd3SAndrea Arcangeli 
142686039bd3SAndrea Arcangeli 	/*
142786039bd3SAndrea Arcangeli 	 * len == 0 means wake all and we don't want to wake all here,
142886039bd3SAndrea Arcangeli 	 * so check it again to be sure.
142986039bd3SAndrea Arcangeli 	 */
143086039bd3SAndrea Arcangeli 	VM_BUG_ON(!range.len);
143186039bd3SAndrea Arcangeli 
143286039bd3SAndrea Arcangeli 	wake_userfault(ctx, &range);
143386039bd3SAndrea Arcangeli 	ret = 0;
143486039bd3SAndrea Arcangeli 
143586039bd3SAndrea Arcangeli out:
143686039bd3SAndrea Arcangeli 	return ret;
143786039bd3SAndrea Arcangeli }
143886039bd3SAndrea Arcangeli 
1439ad465caeSAndrea Arcangeli static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1440ad465caeSAndrea Arcangeli 			    unsigned long arg)
1441ad465caeSAndrea Arcangeli {
1442ad465caeSAndrea Arcangeli 	__s64 ret;
1443ad465caeSAndrea Arcangeli 	struct uffdio_copy uffdio_copy;
1444ad465caeSAndrea Arcangeli 	struct uffdio_copy __user *user_uffdio_copy;
1445ad465caeSAndrea Arcangeli 	struct userfaultfd_wake_range range;
1446ad465caeSAndrea Arcangeli 
1447ad465caeSAndrea Arcangeli 	user_uffdio_copy = (struct uffdio_copy __user *) arg;
1448ad465caeSAndrea Arcangeli 
1449ad465caeSAndrea Arcangeli 	ret = -EFAULT;
1450ad465caeSAndrea Arcangeli 	if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1451ad465caeSAndrea Arcangeli 			   /* don't copy "copy" last field */
1452ad465caeSAndrea Arcangeli 			   sizeof(uffdio_copy)-sizeof(__s64)))
1453ad465caeSAndrea Arcangeli 		goto out;
1454ad465caeSAndrea Arcangeli 
1455ad465caeSAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1456ad465caeSAndrea Arcangeli 	if (ret)
1457ad465caeSAndrea Arcangeli 		goto out;
1458ad465caeSAndrea Arcangeli 	/*
1459ad465caeSAndrea Arcangeli 	 * double check for wraparound just in case. copy_from_user()
1460ad465caeSAndrea Arcangeli 	 * will later check uffdio_copy.src + uffdio_copy.len to fit
1461ad465caeSAndrea Arcangeli 	 * in the userland range.
1462ad465caeSAndrea Arcangeli 	 */
1463ad465caeSAndrea Arcangeli 	ret = -EINVAL;
1464ad465caeSAndrea Arcangeli 	if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1465ad465caeSAndrea Arcangeli 		goto out;
1466ad465caeSAndrea Arcangeli 	if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1467ad465caeSAndrea Arcangeli 		goto out;
1468d2005e3fSOleg Nesterov 	if (mmget_not_zero(ctx->mm)) {
1469ad465caeSAndrea Arcangeli 		ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1470ad465caeSAndrea Arcangeli 				   uffdio_copy.len);
1471d2005e3fSOleg Nesterov 		mmput(ctx->mm);
1472d2005e3fSOleg Nesterov 	}
1473ad465caeSAndrea Arcangeli 	if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1474ad465caeSAndrea Arcangeli 		return -EFAULT;
1475ad465caeSAndrea Arcangeli 	if (ret < 0)
1476ad465caeSAndrea Arcangeli 		goto out;
1477ad465caeSAndrea Arcangeli 	BUG_ON(!ret);
1478ad465caeSAndrea Arcangeli 	/* len == 0 would wake all */
1479ad465caeSAndrea Arcangeli 	range.len = ret;
1480ad465caeSAndrea Arcangeli 	if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1481ad465caeSAndrea Arcangeli 		range.start = uffdio_copy.dst;
1482ad465caeSAndrea Arcangeli 		wake_userfault(ctx, &range);
1483ad465caeSAndrea Arcangeli 	}
1484ad465caeSAndrea Arcangeli 	ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1485ad465caeSAndrea Arcangeli out:
1486ad465caeSAndrea Arcangeli 	return ret;
1487ad465caeSAndrea Arcangeli }
1488ad465caeSAndrea Arcangeli 
1489ad465caeSAndrea Arcangeli static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1490ad465caeSAndrea Arcangeli 				unsigned long arg)
1491ad465caeSAndrea Arcangeli {
1492ad465caeSAndrea Arcangeli 	__s64 ret;
1493ad465caeSAndrea Arcangeli 	struct uffdio_zeropage uffdio_zeropage;
1494ad465caeSAndrea Arcangeli 	struct uffdio_zeropage __user *user_uffdio_zeropage;
1495ad465caeSAndrea Arcangeli 	struct userfaultfd_wake_range range;
1496ad465caeSAndrea Arcangeli 
1497ad465caeSAndrea Arcangeli 	user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1498ad465caeSAndrea Arcangeli 
1499ad465caeSAndrea Arcangeli 	ret = -EFAULT;
1500ad465caeSAndrea Arcangeli 	if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1501ad465caeSAndrea Arcangeli 			   /* don't copy "zeropage" last field */
1502ad465caeSAndrea Arcangeli 			   sizeof(uffdio_zeropage)-sizeof(__s64)))
1503ad465caeSAndrea Arcangeli 		goto out;
1504ad465caeSAndrea Arcangeli 
1505ad465caeSAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1506ad465caeSAndrea Arcangeli 			     uffdio_zeropage.range.len);
1507ad465caeSAndrea Arcangeli 	if (ret)
1508ad465caeSAndrea Arcangeli 		goto out;
1509ad465caeSAndrea Arcangeli 	ret = -EINVAL;
1510ad465caeSAndrea Arcangeli 	if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1511ad465caeSAndrea Arcangeli 		goto out;
1512ad465caeSAndrea Arcangeli 
1513d2005e3fSOleg Nesterov 	if (mmget_not_zero(ctx->mm)) {
1514ad465caeSAndrea Arcangeli 		ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1515ad465caeSAndrea Arcangeli 				     uffdio_zeropage.range.len);
1516d2005e3fSOleg Nesterov 		mmput(ctx->mm);
1517d2005e3fSOleg Nesterov 	}
1518ad465caeSAndrea Arcangeli 	if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1519ad465caeSAndrea Arcangeli 		return -EFAULT;
1520ad465caeSAndrea Arcangeli 	if (ret < 0)
1521ad465caeSAndrea Arcangeli 		goto out;
1522ad465caeSAndrea Arcangeli 	/* len == 0 would wake all */
1523ad465caeSAndrea Arcangeli 	BUG_ON(!ret);
1524ad465caeSAndrea Arcangeli 	range.len = ret;
1525ad465caeSAndrea Arcangeli 	if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1526ad465caeSAndrea Arcangeli 		range.start = uffdio_zeropage.range.start;
1527ad465caeSAndrea Arcangeli 		wake_userfault(ctx, &range);
1528ad465caeSAndrea Arcangeli 	}
1529ad465caeSAndrea Arcangeli 	ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1530ad465caeSAndrea Arcangeli out:
1531ad465caeSAndrea Arcangeli 	return ret;
1532ad465caeSAndrea Arcangeli }
1533ad465caeSAndrea Arcangeli 
15349cd75c3cSPavel Emelyanov static inline unsigned int uffd_ctx_features(__u64 user_features)
15359cd75c3cSPavel Emelyanov {
15369cd75c3cSPavel Emelyanov 	/*
15379cd75c3cSPavel Emelyanov 	 * For the current set of features the bits just coincide
15389cd75c3cSPavel Emelyanov 	 */
15399cd75c3cSPavel Emelyanov 	return (unsigned int)user_features;
15409cd75c3cSPavel Emelyanov }
15419cd75c3cSPavel Emelyanov 
154286039bd3SAndrea Arcangeli /*
154386039bd3SAndrea Arcangeli  * userland asks for a certain API version and we return which bits
154486039bd3SAndrea Arcangeli  * and ioctl commands are implemented in this kernel for such API
154586039bd3SAndrea Arcangeli  * version or -EINVAL if unknown.
154686039bd3SAndrea Arcangeli  */
154786039bd3SAndrea Arcangeli static int userfaultfd_api(struct userfaultfd_ctx *ctx,
154886039bd3SAndrea Arcangeli 			   unsigned long arg)
154986039bd3SAndrea Arcangeli {
155086039bd3SAndrea Arcangeli 	struct uffdio_api uffdio_api;
155186039bd3SAndrea Arcangeli 	void __user *buf = (void __user *)arg;
155286039bd3SAndrea Arcangeli 	int ret;
155365603144SAndrea Arcangeli 	__u64 features;
155486039bd3SAndrea Arcangeli 
155586039bd3SAndrea Arcangeli 	ret = -EINVAL;
155686039bd3SAndrea Arcangeli 	if (ctx->state != UFFD_STATE_WAIT_API)
155786039bd3SAndrea Arcangeli 		goto out;
155886039bd3SAndrea Arcangeli 	ret = -EFAULT;
1559a9b85f94SAndrea Arcangeli 	if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
156086039bd3SAndrea Arcangeli 		goto out;
156165603144SAndrea Arcangeli 	features = uffdio_api.features;
156265603144SAndrea Arcangeli 	if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
156386039bd3SAndrea Arcangeli 		memset(&uffdio_api, 0, sizeof(uffdio_api));
156486039bd3SAndrea Arcangeli 		if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
156586039bd3SAndrea Arcangeli 			goto out;
156686039bd3SAndrea Arcangeli 		ret = -EINVAL;
156786039bd3SAndrea Arcangeli 		goto out;
156886039bd3SAndrea Arcangeli 	}
156965603144SAndrea Arcangeli 	/* report all available features and ioctls to userland */
157065603144SAndrea Arcangeli 	uffdio_api.features = UFFD_API_FEATURES;
157186039bd3SAndrea Arcangeli 	uffdio_api.ioctls = UFFD_API_IOCTLS;
157286039bd3SAndrea Arcangeli 	ret = -EFAULT;
157386039bd3SAndrea Arcangeli 	if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
157486039bd3SAndrea Arcangeli 		goto out;
157586039bd3SAndrea Arcangeli 	ctx->state = UFFD_STATE_RUNNING;
157665603144SAndrea Arcangeli 	/* only enable the requested features for this uffd context */
157765603144SAndrea Arcangeli 	ctx->features = uffd_ctx_features(features);
157886039bd3SAndrea Arcangeli 	ret = 0;
157986039bd3SAndrea Arcangeli out:
158086039bd3SAndrea Arcangeli 	return ret;
158186039bd3SAndrea Arcangeli }
158286039bd3SAndrea Arcangeli 
158386039bd3SAndrea Arcangeli static long userfaultfd_ioctl(struct file *file, unsigned cmd,
158486039bd3SAndrea Arcangeli 			      unsigned long arg)
158586039bd3SAndrea Arcangeli {
158686039bd3SAndrea Arcangeli 	int ret = -EINVAL;
158786039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
158886039bd3SAndrea Arcangeli 
1589e6485a47SAndrea Arcangeli 	if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1590e6485a47SAndrea Arcangeli 		return -EINVAL;
1591e6485a47SAndrea Arcangeli 
159286039bd3SAndrea Arcangeli 	switch(cmd) {
159386039bd3SAndrea Arcangeli 	case UFFDIO_API:
159486039bd3SAndrea Arcangeli 		ret = userfaultfd_api(ctx, arg);
159586039bd3SAndrea Arcangeli 		break;
159686039bd3SAndrea Arcangeli 	case UFFDIO_REGISTER:
159786039bd3SAndrea Arcangeli 		ret = userfaultfd_register(ctx, arg);
159886039bd3SAndrea Arcangeli 		break;
159986039bd3SAndrea Arcangeli 	case UFFDIO_UNREGISTER:
160086039bd3SAndrea Arcangeli 		ret = userfaultfd_unregister(ctx, arg);
160186039bd3SAndrea Arcangeli 		break;
160286039bd3SAndrea Arcangeli 	case UFFDIO_WAKE:
160386039bd3SAndrea Arcangeli 		ret = userfaultfd_wake(ctx, arg);
160486039bd3SAndrea Arcangeli 		break;
1605ad465caeSAndrea Arcangeli 	case UFFDIO_COPY:
1606ad465caeSAndrea Arcangeli 		ret = userfaultfd_copy(ctx, arg);
1607ad465caeSAndrea Arcangeli 		break;
1608ad465caeSAndrea Arcangeli 	case UFFDIO_ZEROPAGE:
1609ad465caeSAndrea Arcangeli 		ret = userfaultfd_zeropage(ctx, arg);
1610ad465caeSAndrea Arcangeli 		break;
161186039bd3SAndrea Arcangeli 	}
161286039bd3SAndrea Arcangeli 	return ret;
161386039bd3SAndrea Arcangeli }
161486039bd3SAndrea Arcangeli 
161586039bd3SAndrea Arcangeli #ifdef CONFIG_PROC_FS
161686039bd3SAndrea Arcangeli static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
161786039bd3SAndrea Arcangeli {
161886039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = f->private_data;
161986039bd3SAndrea Arcangeli 	wait_queue_t *wq;
162086039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
162186039bd3SAndrea Arcangeli 	unsigned long pending = 0, total = 0;
162286039bd3SAndrea Arcangeli 
162315b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
162415b726efSAndrea Arcangeli 	list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
162586039bd3SAndrea Arcangeli 		uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
162686039bd3SAndrea Arcangeli 		pending++;
162786039bd3SAndrea Arcangeli 		total++;
162886039bd3SAndrea Arcangeli 	}
162915b726efSAndrea Arcangeli 	list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
163015b726efSAndrea Arcangeli 		uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
163115b726efSAndrea Arcangeli 		total++;
163215b726efSAndrea Arcangeli 	}
163315b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
163486039bd3SAndrea Arcangeli 
163586039bd3SAndrea Arcangeli 	/*
163686039bd3SAndrea Arcangeli 	 * If more protocols will be added, there will be all shown
163786039bd3SAndrea Arcangeli 	 * separated by a space. Like this:
163886039bd3SAndrea Arcangeli 	 *	protocols: aa:... bb:...
163986039bd3SAndrea Arcangeli 	 */
164086039bd3SAndrea Arcangeli 	seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
16413f602d27SPavel Emelyanov 		   pending, total, UFFD_API, UFFD_API_FEATURES,
164286039bd3SAndrea Arcangeli 		   UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
164386039bd3SAndrea Arcangeli }
164486039bd3SAndrea Arcangeli #endif
164586039bd3SAndrea Arcangeli 
164686039bd3SAndrea Arcangeli static const struct file_operations userfaultfd_fops = {
164786039bd3SAndrea Arcangeli #ifdef CONFIG_PROC_FS
164886039bd3SAndrea Arcangeli 	.show_fdinfo	= userfaultfd_show_fdinfo,
164986039bd3SAndrea Arcangeli #endif
165086039bd3SAndrea Arcangeli 	.release	= userfaultfd_release,
165186039bd3SAndrea Arcangeli 	.poll		= userfaultfd_poll,
165286039bd3SAndrea Arcangeli 	.read		= userfaultfd_read,
165386039bd3SAndrea Arcangeli 	.unlocked_ioctl = userfaultfd_ioctl,
165486039bd3SAndrea Arcangeli 	.compat_ioctl	= userfaultfd_ioctl,
165586039bd3SAndrea Arcangeli 	.llseek		= noop_llseek,
165686039bd3SAndrea Arcangeli };
165786039bd3SAndrea Arcangeli 
16583004ec9cSAndrea Arcangeli static void init_once_userfaultfd_ctx(void *mem)
16593004ec9cSAndrea Arcangeli {
16603004ec9cSAndrea Arcangeli 	struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
16613004ec9cSAndrea Arcangeli 
16623004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fault_pending_wqh);
16633004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fault_wqh);
16649cd75c3cSPavel Emelyanov 	init_waitqueue_head(&ctx->event_wqh);
16653004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fd_wqh);
16662c5b7e1bSAndrea Arcangeli 	seqcount_init(&ctx->refile_seq);
16673004ec9cSAndrea Arcangeli }
16683004ec9cSAndrea Arcangeli 
166986039bd3SAndrea Arcangeli /**
167086039bd3SAndrea Arcangeli  * userfaultfd_file_create - Creates an userfaultfd file pointer.
167186039bd3SAndrea Arcangeli  * @flags: Flags for the userfaultfd file.
167286039bd3SAndrea Arcangeli  *
167386039bd3SAndrea Arcangeli  * This function creates an userfaultfd file pointer, w/out installing
167486039bd3SAndrea Arcangeli  * it into the fd table. This is useful when the userfaultfd file is
167586039bd3SAndrea Arcangeli  * used during the initialization of data structures that require
167686039bd3SAndrea Arcangeli  * extra setup after the userfaultfd creation. So the userfaultfd
167786039bd3SAndrea Arcangeli  * creation is split into the file pointer creation phase, and the
167886039bd3SAndrea Arcangeli  * file descriptor installation phase.  In this way races with
167986039bd3SAndrea Arcangeli  * userspace closing the newly installed file descriptor can be
168086039bd3SAndrea Arcangeli  * avoided.  Returns an userfaultfd file pointer, or a proper error
168186039bd3SAndrea Arcangeli  * pointer.
168286039bd3SAndrea Arcangeli  */
168386039bd3SAndrea Arcangeli static struct file *userfaultfd_file_create(int flags)
168486039bd3SAndrea Arcangeli {
168586039bd3SAndrea Arcangeli 	struct file *file;
168686039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
168786039bd3SAndrea Arcangeli 
168886039bd3SAndrea Arcangeli 	BUG_ON(!current->mm);
168986039bd3SAndrea Arcangeli 
169086039bd3SAndrea Arcangeli 	/* Check the UFFD_* constants for consistency.  */
169186039bd3SAndrea Arcangeli 	BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
169286039bd3SAndrea Arcangeli 	BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
169386039bd3SAndrea Arcangeli 
169486039bd3SAndrea Arcangeli 	file = ERR_PTR(-EINVAL);
169586039bd3SAndrea Arcangeli 	if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
169686039bd3SAndrea Arcangeli 		goto out;
169786039bd3SAndrea Arcangeli 
169886039bd3SAndrea Arcangeli 	file = ERR_PTR(-ENOMEM);
16993004ec9cSAndrea Arcangeli 	ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
170086039bd3SAndrea Arcangeli 	if (!ctx)
170186039bd3SAndrea Arcangeli 		goto out;
170286039bd3SAndrea Arcangeli 
170386039bd3SAndrea Arcangeli 	atomic_set(&ctx->refcount, 1);
170486039bd3SAndrea Arcangeli 	ctx->flags = flags;
17059cd75c3cSPavel Emelyanov 	ctx->features = 0;
170686039bd3SAndrea Arcangeli 	ctx->state = UFFD_STATE_WAIT_API;
170786039bd3SAndrea Arcangeli 	ctx->released = false;
170886039bd3SAndrea Arcangeli 	ctx->mm = current->mm;
170986039bd3SAndrea Arcangeli 	/* prevent the mm struct to be freed */
1710d2005e3fSOleg Nesterov 	atomic_inc(&ctx->mm->mm_count);
171186039bd3SAndrea Arcangeli 
171286039bd3SAndrea Arcangeli 	file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
171386039bd3SAndrea Arcangeli 				  O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1714c03e946fSEric Biggers 	if (IS_ERR(file)) {
1715d2005e3fSOleg Nesterov 		mmdrop(ctx->mm);
17163004ec9cSAndrea Arcangeli 		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
1717c03e946fSEric Biggers 	}
171886039bd3SAndrea Arcangeli out:
171986039bd3SAndrea Arcangeli 	return file;
172086039bd3SAndrea Arcangeli }
172186039bd3SAndrea Arcangeli 
172286039bd3SAndrea Arcangeli SYSCALL_DEFINE1(userfaultfd, int, flags)
172386039bd3SAndrea Arcangeli {
172486039bd3SAndrea Arcangeli 	int fd, error;
172586039bd3SAndrea Arcangeli 	struct file *file;
172686039bd3SAndrea Arcangeli 
172786039bd3SAndrea Arcangeli 	error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
172886039bd3SAndrea Arcangeli 	if (error < 0)
172986039bd3SAndrea Arcangeli 		return error;
173086039bd3SAndrea Arcangeli 	fd = error;
173186039bd3SAndrea Arcangeli 
173286039bd3SAndrea Arcangeli 	file = userfaultfd_file_create(flags);
173386039bd3SAndrea Arcangeli 	if (IS_ERR(file)) {
173486039bd3SAndrea Arcangeli 		error = PTR_ERR(file);
173586039bd3SAndrea Arcangeli 		goto err_put_unused_fd;
173686039bd3SAndrea Arcangeli 	}
173786039bd3SAndrea Arcangeli 	fd_install(fd, file);
173886039bd3SAndrea Arcangeli 
173986039bd3SAndrea Arcangeli 	return fd;
174086039bd3SAndrea Arcangeli 
174186039bd3SAndrea Arcangeli err_put_unused_fd:
174286039bd3SAndrea Arcangeli 	put_unused_fd(fd);
174386039bd3SAndrea Arcangeli 
174486039bd3SAndrea Arcangeli 	return error;
174586039bd3SAndrea Arcangeli }
17463004ec9cSAndrea Arcangeli 
17473004ec9cSAndrea Arcangeli static int __init userfaultfd_init(void)
17483004ec9cSAndrea Arcangeli {
17493004ec9cSAndrea Arcangeli 	userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
17503004ec9cSAndrea Arcangeli 						sizeof(struct userfaultfd_ctx),
17513004ec9cSAndrea Arcangeli 						0,
17523004ec9cSAndrea Arcangeli 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
17533004ec9cSAndrea Arcangeli 						init_once_userfaultfd_ctx);
17543004ec9cSAndrea Arcangeli 	return 0;
17553004ec9cSAndrea Arcangeli }
17563004ec9cSAndrea Arcangeli __initcall(userfaultfd_init);
1757