xref: /openbmc/linux/fs/userfaultfd.c (revision 2055da97389a605c8a00d163d40903afbe413921)
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>
17174cd4b1SIngo Molnar #include <linux/sched/signal.h>
186e84f315SIngo Molnar #include <linux/sched/mm.h>
1986039bd3SAndrea Arcangeli #include <linux/mm.h>
2086039bd3SAndrea Arcangeli #include <linux/poll.h>
2186039bd3SAndrea Arcangeli #include <linux/slab.h>
2286039bd3SAndrea Arcangeli #include <linux/seq_file.h>
2386039bd3SAndrea Arcangeli #include <linux/file.h>
2486039bd3SAndrea Arcangeli #include <linux/bug.h>
2586039bd3SAndrea Arcangeli #include <linux/anon_inodes.h>
2686039bd3SAndrea Arcangeli #include <linux/syscalls.h>
2786039bd3SAndrea Arcangeli #include <linux/userfaultfd_k.h>
2886039bd3SAndrea Arcangeli #include <linux/mempolicy.h>
2986039bd3SAndrea Arcangeli #include <linux/ioctl.h>
3086039bd3SAndrea Arcangeli #include <linux/security.h>
31cab350afSMike Kravetz #include <linux/hugetlb.h>
3286039bd3SAndrea Arcangeli 
333004ec9cSAndrea Arcangeli static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
343004ec9cSAndrea Arcangeli 
3586039bd3SAndrea Arcangeli enum userfaultfd_state {
3686039bd3SAndrea Arcangeli 	UFFD_STATE_WAIT_API,
3786039bd3SAndrea Arcangeli 	UFFD_STATE_RUNNING,
3886039bd3SAndrea Arcangeli };
3986039bd3SAndrea Arcangeli 
403004ec9cSAndrea Arcangeli /*
413004ec9cSAndrea Arcangeli  * Start with fault_pending_wqh and fault_wqh so they're more likely
423004ec9cSAndrea Arcangeli  * to be in the same cacheline.
433004ec9cSAndrea Arcangeli  */
4486039bd3SAndrea Arcangeli struct userfaultfd_ctx {
4515b726efSAndrea Arcangeli 	/* waitqueue head for the pending (i.e. not read) userfaults */
4615b726efSAndrea Arcangeli 	wait_queue_head_t fault_pending_wqh;
4715b726efSAndrea Arcangeli 	/* waitqueue head for the userfaults */
4886039bd3SAndrea Arcangeli 	wait_queue_head_t fault_wqh;
4986039bd3SAndrea Arcangeli 	/* waitqueue head for the pseudo fd to wakeup poll/read */
5086039bd3SAndrea Arcangeli 	wait_queue_head_t fd_wqh;
519cd75c3cSPavel Emelyanov 	/* waitqueue head for events */
529cd75c3cSPavel Emelyanov 	wait_queue_head_t event_wqh;
532c5b7e1bSAndrea Arcangeli 	/* a refile sequence protected by fault_pending_wqh lock */
542c5b7e1bSAndrea Arcangeli 	struct seqcount refile_seq;
553004ec9cSAndrea Arcangeli 	/* pseudo fd refcounting */
563004ec9cSAndrea Arcangeli 	atomic_t refcount;
5786039bd3SAndrea Arcangeli 	/* userfaultfd syscall flags */
5886039bd3SAndrea Arcangeli 	unsigned int flags;
599cd75c3cSPavel Emelyanov 	/* features requested from the userspace */
609cd75c3cSPavel Emelyanov 	unsigned int features;
6186039bd3SAndrea Arcangeli 	/* state machine */
6286039bd3SAndrea Arcangeli 	enum userfaultfd_state state;
6386039bd3SAndrea Arcangeli 	/* released */
6486039bd3SAndrea Arcangeli 	bool released;
6586039bd3SAndrea Arcangeli 	/* mm with one ore more vmas attached to this userfaultfd_ctx */
6686039bd3SAndrea Arcangeli 	struct mm_struct *mm;
6786039bd3SAndrea Arcangeli };
6886039bd3SAndrea Arcangeli 
69893e26e6SPavel Emelyanov struct userfaultfd_fork_ctx {
70893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *orig;
71893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *new;
72893e26e6SPavel Emelyanov 	struct list_head list;
73893e26e6SPavel Emelyanov };
74893e26e6SPavel Emelyanov 
75897ab3e0SMike Rapoport struct userfaultfd_unmap_ctx {
76897ab3e0SMike Rapoport 	struct userfaultfd_ctx *ctx;
77897ab3e0SMike Rapoport 	unsigned long start;
78897ab3e0SMike Rapoport 	unsigned long end;
79897ab3e0SMike Rapoport 	struct list_head list;
80897ab3e0SMike Rapoport };
81897ab3e0SMike Rapoport 
8286039bd3SAndrea Arcangeli struct userfaultfd_wait_queue {
83a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
84ac6424b9SIngo Molnar 	wait_queue_entry_t wq;
8586039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
8615a77c6fSAndrea Arcangeli 	bool waken;
8786039bd3SAndrea Arcangeli };
8886039bd3SAndrea Arcangeli 
8986039bd3SAndrea Arcangeli struct userfaultfd_wake_range {
9086039bd3SAndrea Arcangeli 	unsigned long start;
9186039bd3SAndrea Arcangeli 	unsigned long len;
9286039bd3SAndrea Arcangeli };
9386039bd3SAndrea Arcangeli 
94ac6424b9SIngo Molnar static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
9586039bd3SAndrea Arcangeli 				     int wake_flags, void *key)
9686039bd3SAndrea Arcangeli {
9786039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range *range = key;
9886039bd3SAndrea Arcangeli 	int ret;
9986039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
10086039bd3SAndrea Arcangeli 	unsigned long start, len;
10186039bd3SAndrea Arcangeli 
10286039bd3SAndrea Arcangeli 	uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
10386039bd3SAndrea Arcangeli 	ret = 0;
10486039bd3SAndrea Arcangeli 	/* len == 0 means wake all */
10586039bd3SAndrea Arcangeli 	start = range->start;
10686039bd3SAndrea Arcangeli 	len = range->len;
107a9b85f94SAndrea Arcangeli 	if (len && (start > uwq->msg.arg.pagefault.address ||
108a9b85f94SAndrea Arcangeli 		    start + len <= uwq->msg.arg.pagefault.address))
10986039bd3SAndrea Arcangeli 		goto out;
11015a77c6fSAndrea Arcangeli 	WRITE_ONCE(uwq->waken, true);
11115a77c6fSAndrea Arcangeli 	/*
11215a77c6fSAndrea Arcangeli 	 * The implicit smp_mb__before_spinlock in try_to_wake_up()
11315a77c6fSAndrea Arcangeli 	 * renders uwq->waken visible to other CPUs before the task is
11415a77c6fSAndrea Arcangeli 	 * waken.
11515a77c6fSAndrea Arcangeli 	 */
11686039bd3SAndrea Arcangeli 	ret = wake_up_state(wq->private, mode);
11786039bd3SAndrea Arcangeli 	if (ret)
11886039bd3SAndrea Arcangeli 		/*
11986039bd3SAndrea Arcangeli 		 * Wake only once, autoremove behavior.
12086039bd3SAndrea Arcangeli 		 *
12186039bd3SAndrea Arcangeli 		 * After the effect of list_del_init is visible to the
12286039bd3SAndrea Arcangeli 		 * other CPUs, the waitqueue may disappear from under
12386039bd3SAndrea Arcangeli 		 * us, see the !list_empty_careful() in
12486039bd3SAndrea Arcangeli 		 * handle_userfault(). try_to_wake_up() has an
12586039bd3SAndrea Arcangeli 		 * implicit smp_mb__before_spinlock, and the
12686039bd3SAndrea Arcangeli 		 * wq->private is read before calling the extern
12786039bd3SAndrea Arcangeli 		 * function "wake_up_state" (which in turns calls
12886039bd3SAndrea Arcangeli 		 * try_to_wake_up). While the spin_lock;spin_unlock;
12986039bd3SAndrea Arcangeli 		 * wouldn't be enough, the smp_mb__before_spinlock is
13086039bd3SAndrea Arcangeli 		 * enough to avoid an explicit smp_mb() here.
13186039bd3SAndrea Arcangeli 		 */
132*2055da97SIngo Molnar 		list_del_init(&wq->entry);
13386039bd3SAndrea Arcangeli out:
13486039bd3SAndrea Arcangeli 	return ret;
13586039bd3SAndrea Arcangeli }
13686039bd3SAndrea Arcangeli 
13786039bd3SAndrea Arcangeli /**
13886039bd3SAndrea Arcangeli  * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
13986039bd3SAndrea Arcangeli  * context.
14086039bd3SAndrea Arcangeli  * @ctx: [in] Pointer to the userfaultfd context.
14186039bd3SAndrea Arcangeli  */
14286039bd3SAndrea Arcangeli static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
14386039bd3SAndrea Arcangeli {
14486039bd3SAndrea Arcangeli 	if (!atomic_inc_not_zero(&ctx->refcount))
14586039bd3SAndrea Arcangeli 		BUG();
14686039bd3SAndrea Arcangeli }
14786039bd3SAndrea Arcangeli 
14886039bd3SAndrea Arcangeli /**
14986039bd3SAndrea Arcangeli  * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
15086039bd3SAndrea Arcangeli  * context.
15186039bd3SAndrea Arcangeli  * @ctx: [in] Pointer to userfaultfd context.
15286039bd3SAndrea Arcangeli  *
15386039bd3SAndrea Arcangeli  * The userfaultfd context reference must have been previously acquired either
15486039bd3SAndrea Arcangeli  * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
15586039bd3SAndrea Arcangeli  */
15686039bd3SAndrea Arcangeli static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
15786039bd3SAndrea Arcangeli {
15886039bd3SAndrea Arcangeli 	if (atomic_dec_and_test(&ctx->refcount)) {
15986039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
16086039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
16186039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
16286039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
1639cd75c3cSPavel Emelyanov 		VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
1649cd75c3cSPavel Emelyanov 		VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
16586039bd3SAndrea Arcangeli 		VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
16686039bd3SAndrea Arcangeli 		VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
167d2005e3fSOleg Nesterov 		mmdrop(ctx->mm);
1683004ec9cSAndrea Arcangeli 		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
16986039bd3SAndrea Arcangeli 	}
17086039bd3SAndrea Arcangeli }
17186039bd3SAndrea Arcangeli 
172a9b85f94SAndrea Arcangeli static inline void msg_init(struct uffd_msg *msg)
173a9b85f94SAndrea Arcangeli {
174a9b85f94SAndrea Arcangeli 	BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
175a9b85f94SAndrea Arcangeli 	/*
176a9b85f94SAndrea Arcangeli 	 * Must use memset to zero out the paddings or kernel data is
177a9b85f94SAndrea Arcangeli 	 * leaked to userland.
178a9b85f94SAndrea Arcangeli 	 */
179a9b85f94SAndrea Arcangeli 	memset(msg, 0, sizeof(struct uffd_msg));
180a9b85f94SAndrea Arcangeli }
181a9b85f94SAndrea Arcangeli 
182a9b85f94SAndrea Arcangeli static inline struct uffd_msg userfault_msg(unsigned long address,
18386039bd3SAndrea Arcangeli 					    unsigned int flags,
18486039bd3SAndrea Arcangeli 					    unsigned long reason)
18586039bd3SAndrea Arcangeli {
186a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
187a9b85f94SAndrea Arcangeli 	msg_init(&msg);
188a9b85f94SAndrea Arcangeli 	msg.event = UFFD_EVENT_PAGEFAULT;
189a9b85f94SAndrea Arcangeli 	msg.arg.pagefault.address = address;
19086039bd3SAndrea Arcangeli 	if (flags & FAULT_FLAG_WRITE)
19186039bd3SAndrea Arcangeli 		/*
192a4605a61SAndrea Arcangeli 		 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
193a9b85f94SAndrea Arcangeli 		 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
194a9b85f94SAndrea Arcangeli 		 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
195a9b85f94SAndrea Arcangeli 		 * was a read fault, otherwise if set it means it's
196a9b85f94SAndrea Arcangeli 		 * a write fault.
19786039bd3SAndrea Arcangeli 		 */
198a9b85f94SAndrea Arcangeli 		msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
19986039bd3SAndrea Arcangeli 	if (reason & VM_UFFD_WP)
20086039bd3SAndrea Arcangeli 		/*
201a9b85f94SAndrea Arcangeli 		 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
202a9b85f94SAndrea Arcangeli 		 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
203a9b85f94SAndrea Arcangeli 		 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
204a9b85f94SAndrea Arcangeli 		 * a missing fault, otherwise if set it means it's a
205a9b85f94SAndrea Arcangeli 		 * write protect fault.
20686039bd3SAndrea Arcangeli 		 */
207a9b85f94SAndrea Arcangeli 		msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
208a9b85f94SAndrea Arcangeli 	return msg;
20986039bd3SAndrea Arcangeli }
21086039bd3SAndrea Arcangeli 
211369cd212SMike Kravetz #ifdef CONFIG_HUGETLB_PAGE
212369cd212SMike Kravetz /*
213369cd212SMike Kravetz  * Same functionality as userfaultfd_must_wait below with modifications for
214369cd212SMike Kravetz  * hugepmd ranges.
215369cd212SMike Kravetz  */
216369cd212SMike Kravetz static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
217369cd212SMike Kravetz 					 unsigned long address,
218369cd212SMike Kravetz 					 unsigned long flags,
219369cd212SMike Kravetz 					 unsigned long reason)
220369cd212SMike Kravetz {
221369cd212SMike Kravetz 	struct mm_struct *mm = ctx->mm;
222369cd212SMike Kravetz 	pte_t *pte;
223369cd212SMike Kravetz 	bool ret = true;
224369cd212SMike Kravetz 
225369cd212SMike Kravetz 	VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
226369cd212SMike Kravetz 
227369cd212SMike Kravetz 	pte = huge_pte_offset(mm, address);
228369cd212SMike Kravetz 	if (!pte)
229369cd212SMike Kravetz 		goto out;
230369cd212SMike Kravetz 
231369cd212SMike Kravetz 	ret = false;
232369cd212SMike Kravetz 
233369cd212SMike Kravetz 	/*
234369cd212SMike Kravetz 	 * Lockless access: we're in a wait_event so it's ok if it
235369cd212SMike Kravetz 	 * changes under us.
236369cd212SMike Kravetz 	 */
237369cd212SMike Kravetz 	if (huge_pte_none(*pte))
238369cd212SMike Kravetz 		ret = true;
239369cd212SMike Kravetz 	if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
240369cd212SMike Kravetz 		ret = true;
241369cd212SMike Kravetz out:
242369cd212SMike Kravetz 	return ret;
243369cd212SMike Kravetz }
244369cd212SMike Kravetz #else
245369cd212SMike Kravetz static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
246369cd212SMike Kravetz 					 unsigned long address,
247369cd212SMike Kravetz 					 unsigned long flags,
248369cd212SMike Kravetz 					 unsigned long reason)
249369cd212SMike Kravetz {
250369cd212SMike Kravetz 	return false;	/* should never get here */
251369cd212SMike Kravetz }
252369cd212SMike Kravetz #endif /* CONFIG_HUGETLB_PAGE */
253369cd212SMike Kravetz 
25486039bd3SAndrea Arcangeli /*
2558d2afd96SAndrea Arcangeli  * Verify the pagetables are still not ok after having reigstered into
2568d2afd96SAndrea Arcangeli  * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
2578d2afd96SAndrea Arcangeli  * userfault that has already been resolved, if userfaultfd_read and
2588d2afd96SAndrea Arcangeli  * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
2598d2afd96SAndrea Arcangeli  * threads.
2608d2afd96SAndrea Arcangeli  */
2618d2afd96SAndrea Arcangeli static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
2628d2afd96SAndrea Arcangeli 					 unsigned long address,
2638d2afd96SAndrea Arcangeli 					 unsigned long flags,
2648d2afd96SAndrea Arcangeli 					 unsigned long reason)
2658d2afd96SAndrea Arcangeli {
2668d2afd96SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
2678d2afd96SAndrea Arcangeli 	pgd_t *pgd;
268c2febafcSKirill A. Shutemov 	p4d_t *p4d;
2698d2afd96SAndrea Arcangeli 	pud_t *pud;
2708d2afd96SAndrea Arcangeli 	pmd_t *pmd, _pmd;
2718d2afd96SAndrea Arcangeli 	pte_t *pte;
2728d2afd96SAndrea Arcangeli 	bool ret = true;
2738d2afd96SAndrea Arcangeli 
2748d2afd96SAndrea Arcangeli 	VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
2758d2afd96SAndrea Arcangeli 
2768d2afd96SAndrea Arcangeli 	pgd = pgd_offset(mm, address);
2778d2afd96SAndrea Arcangeli 	if (!pgd_present(*pgd))
2788d2afd96SAndrea Arcangeli 		goto out;
279c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
280c2febafcSKirill A. Shutemov 	if (!p4d_present(*p4d))
281c2febafcSKirill A. Shutemov 		goto out;
282c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, address);
2838d2afd96SAndrea Arcangeli 	if (!pud_present(*pud))
2848d2afd96SAndrea Arcangeli 		goto out;
2858d2afd96SAndrea Arcangeli 	pmd = pmd_offset(pud, address);
2868d2afd96SAndrea Arcangeli 	/*
2878d2afd96SAndrea Arcangeli 	 * READ_ONCE must function as a barrier with narrower scope
2888d2afd96SAndrea Arcangeli 	 * and it must be equivalent to:
2898d2afd96SAndrea Arcangeli 	 *	_pmd = *pmd; barrier();
2908d2afd96SAndrea Arcangeli 	 *
2918d2afd96SAndrea Arcangeli 	 * This is to deal with the instability (as in
2928d2afd96SAndrea Arcangeli 	 * pmd_trans_unstable) of the pmd.
2938d2afd96SAndrea Arcangeli 	 */
2948d2afd96SAndrea Arcangeli 	_pmd = READ_ONCE(*pmd);
2958d2afd96SAndrea Arcangeli 	if (!pmd_present(_pmd))
2968d2afd96SAndrea Arcangeli 		goto out;
2978d2afd96SAndrea Arcangeli 
2988d2afd96SAndrea Arcangeli 	ret = false;
2998d2afd96SAndrea Arcangeli 	if (pmd_trans_huge(_pmd))
3008d2afd96SAndrea Arcangeli 		goto out;
3018d2afd96SAndrea Arcangeli 
3028d2afd96SAndrea Arcangeli 	/*
3038d2afd96SAndrea Arcangeli 	 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
3048d2afd96SAndrea Arcangeli 	 * and use the standard pte_offset_map() instead of parsing _pmd.
3058d2afd96SAndrea Arcangeli 	 */
3068d2afd96SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
3078d2afd96SAndrea Arcangeli 	/*
3088d2afd96SAndrea Arcangeli 	 * Lockless access: we're in a wait_event so it's ok if it
3098d2afd96SAndrea Arcangeli 	 * changes under us.
3108d2afd96SAndrea Arcangeli 	 */
3118d2afd96SAndrea Arcangeli 	if (pte_none(*pte))
3128d2afd96SAndrea Arcangeli 		ret = true;
3138d2afd96SAndrea Arcangeli 	pte_unmap(pte);
3148d2afd96SAndrea Arcangeli 
3158d2afd96SAndrea Arcangeli out:
3168d2afd96SAndrea Arcangeli 	return ret;
3178d2afd96SAndrea Arcangeli }
3188d2afd96SAndrea Arcangeli 
3198d2afd96SAndrea Arcangeli /*
32086039bd3SAndrea Arcangeli  * The locking rules involved in returning VM_FAULT_RETRY depending on
32186039bd3SAndrea Arcangeli  * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
32286039bd3SAndrea Arcangeli  * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
32386039bd3SAndrea Arcangeli  * recommendation in __lock_page_or_retry is not an understatement.
32486039bd3SAndrea Arcangeli  *
32586039bd3SAndrea Arcangeli  * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
32686039bd3SAndrea Arcangeli  * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
32786039bd3SAndrea Arcangeli  * not set.
32886039bd3SAndrea Arcangeli  *
32986039bd3SAndrea Arcangeli  * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
33086039bd3SAndrea Arcangeli  * set, VM_FAULT_RETRY can still be returned if and only if there are
33186039bd3SAndrea Arcangeli  * fatal_signal_pending()s, and the mmap_sem must be released before
33286039bd3SAndrea Arcangeli  * returning it.
33386039bd3SAndrea Arcangeli  */
33482b0f8c3SJan Kara int handle_userfault(struct vm_fault *vmf, unsigned long reason)
33586039bd3SAndrea Arcangeli {
33682b0f8c3SJan Kara 	struct mm_struct *mm = vmf->vma->vm_mm;
33786039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
33886039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue uwq;
339ba85c702SAndrea Arcangeli 	int ret;
340dfa37dc3SAndrea Arcangeli 	bool must_wait, return_to_userland;
34115a77c6fSAndrea Arcangeli 	long blocking_state;
34286039bd3SAndrea Arcangeli 
343ba85c702SAndrea Arcangeli 	ret = VM_FAULT_SIGBUS;
34464c2b203SAndrea Arcangeli 
34564c2b203SAndrea Arcangeli 	/*
34664c2b203SAndrea Arcangeli 	 * We don't do userfault handling for the final child pid update.
34764c2b203SAndrea Arcangeli 	 *
34864c2b203SAndrea Arcangeli 	 * We also don't do userfault handling during
34964c2b203SAndrea Arcangeli 	 * coredumping. hugetlbfs has the special
35064c2b203SAndrea Arcangeli 	 * follow_hugetlb_page() to skip missing pages in the
35164c2b203SAndrea Arcangeli 	 * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
35264c2b203SAndrea Arcangeli 	 * the no_page_table() helper in follow_page_mask(), but the
35364c2b203SAndrea Arcangeli 	 * shmem_vm_ops->fault method is invoked even during
35464c2b203SAndrea Arcangeli 	 * coredumping without mmap_sem and it ends up here.
35564c2b203SAndrea Arcangeli 	 */
35664c2b203SAndrea Arcangeli 	if (current->flags & (PF_EXITING|PF_DUMPCORE))
35764c2b203SAndrea Arcangeli 		goto out;
35864c2b203SAndrea Arcangeli 
35964c2b203SAndrea Arcangeli 	/*
36064c2b203SAndrea Arcangeli 	 * Coredumping runs without mmap_sem so we can only check that
36164c2b203SAndrea Arcangeli 	 * the mmap_sem is held, if PF_DUMPCORE was not set.
36264c2b203SAndrea Arcangeli 	 */
36364c2b203SAndrea Arcangeli 	WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem));
36464c2b203SAndrea Arcangeli 
36582b0f8c3SJan Kara 	ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
36686039bd3SAndrea Arcangeli 	if (!ctx)
367ba85c702SAndrea Arcangeli 		goto out;
36886039bd3SAndrea Arcangeli 
36986039bd3SAndrea Arcangeli 	BUG_ON(ctx->mm != mm);
37086039bd3SAndrea Arcangeli 
37186039bd3SAndrea Arcangeli 	VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
37286039bd3SAndrea Arcangeli 	VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
37386039bd3SAndrea Arcangeli 
37486039bd3SAndrea Arcangeli 	/*
37586039bd3SAndrea Arcangeli 	 * If it's already released don't get it. This avoids to loop
37686039bd3SAndrea Arcangeli 	 * in __get_user_pages if userfaultfd_release waits on the
37786039bd3SAndrea Arcangeli 	 * caller of handle_userfault to release the mmap_sem.
37886039bd3SAndrea Arcangeli 	 */
37986039bd3SAndrea Arcangeli 	if (unlikely(ACCESS_ONCE(ctx->released)))
380ba85c702SAndrea Arcangeli 		goto out;
38186039bd3SAndrea Arcangeli 
38286039bd3SAndrea Arcangeli 	/*
38386039bd3SAndrea Arcangeli 	 * Check that we can return VM_FAULT_RETRY.
38486039bd3SAndrea Arcangeli 	 *
38586039bd3SAndrea Arcangeli 	 * NOTE: it should become possible to return VM_FAULT_RETRY
38686039bd3SAndrea Arcangeli 	 * even if FAULT_FLAG_TRIED is set without leading to gup()
38786039bd3SAndrea Arcangeli 	 * -EBUSY failures, if the userfaultfd is to be extended for
38886039bd3SAndrea Arcangeli 	 * VM_UFFD_WP tracking and we intend to arm the userfault
38986039bd3SAndrea Arcangeli 	 * without first stopping userland access to the memory. For
39086039bd3SAndrea Arcangeli 	 * VM_UFFD_MISSING userfaults this is enough for now.
39186039bd3SAndrea Arcangeli 	 */
39282b0f8c3SJan Kara 	if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
39386039bd3SAndrea Arcangeli 		/*
39486039bd3SAndrea Arcangeli 		 * Validate the invariant that nowait must allow retry
39586039bd3SAndrea Arcangeli 		 * to be sure not to return SIGBUS erroneously on
39686039bd3SAndrea Arcangeli 		 * nowait invocations.
39786039bd3SAndrea Arcangeli 		 */
39882b0f8c3SJan Kara 		BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
39986039bd3SAndrea Arcangeli #ifdef CONFIG_DEBUG_VM
40086039bd3SAndrea Arcangeli 		if (printk_ratelimit()) {
40186039bd3SAndrea Arcangeli 			printk(KERN_WARNING
40282b0f8c3SJan Kara 			       "FAULT_FLAG_ALLOW_RETRY missing %x\n",
40382b0f8c3SJan Kara 			       vmf->flags);
40486039bd3SAndrea Arcangeli 			dump_stack();
40586039bd3SAndrea Arcangeli 		}
40686039bd3SAndrea Arcangeli #endif
407ba85c702SAndrea Arcangeli 		goto out;
40886039bd3SAndrea Arcangeli 	}
40986039bd3SAndrea Arcangeli 
41086039bd3SAndrea Arcangeli 	/*
41186039bd3SAndrea Arcangeli 	 * Handle nowait, not much to do other than tell it to retry
41286039bd3SAndrea Arcangeli 	 * and wait.
41386039bd3SAndrea Arcangeli 	 */
414ba85c702SAndrea Arcangeli 	ret = VM_FAULT_RETRY;
41582b0f8c3SJan Kara 	if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
416ba85c702SAndrea Arcangeli 		goto out;
41786039bd3SAndrea Arcangeli 
41886039bd3SAndrea Arcangeli 	/* take the reference before dropping the mmap_sem */
41986039bd3SAndrea Arcangeli 	userfaultfd_ctx_get(ctx);
42086039bd3SAndrea Arcangeli 
42186039bd3SAndrea Arcangeli 	init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
42286039bd3SAndrea Arcangeli 	uwq.wq.private = current;
42382b0f8c3SJan Kara 	uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
42486039bd3SAndrea Arcangeli 	uwq.ctx = ctx;
42515a77c6fSAndrea Arcangeli 	uwq.waken = false;
42686039bd3SAndrea Arcangeli 
427bae473a4SKirill A. Shutemov 	return_to_userland =
42882b0f8c3SJan Kara 		(vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
429dfa37dc3SAndrea Arcangeli 		(FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
43015a77c6fSAndrea Arcangeli 	blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
43115a77c6fSAndrea Arcangeli 			 TASK_KILLABLE;
432dfa37dc3SAndrea Arcangeli 
43315b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
43486039bd3SAndrea Arcangeli 	/*
43586039bd3SAndrea Arcangeli 	 * After the __add_wait_queue the uwq is visible to userland
43686039bd3SAndrea Arcangeli 	 * through poll/read().
43786039bd3SAndrea Arcangeli 	 */
43815b726efSAndrea Arcangeli 	__add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
43915b726efSAndrea Arcangeli 	/*
44015b726efSAndrea Arcangeli 	 * The smp_mb() after __set_current_state prevents the reads
44115b726efSAndrea Arcangeli 	 * following the spin_unlock to happen before the list_add in
44215b726efSAndrea Arcangeli 	 * __add_wait_queue.
44315b726efSAndrea Arcangeli 	 */
44415a77c6fSAndrea Arcangeli 	set_current_state(blocking_state);
44515b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
44686039bd3SAndrea Arcangeli 
447369cd212SMike Kravetz 	if (!is_vm_hugetlb_page(vmf->vma))
44882b0f8c3SJan Kara 		must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
44982b0f8c3SJan Kara 						  reason);
450369cd212SMike Kravetz 	else
451369cd212SMike Kravetz 		must_wait = userfaultfd_huge_must_wait(ctx, vmf->address,
452369cd212SMike Kravetz 						       vmf->flags, reason);
4538d2afd96SAndrea Arcangeli 	up_read(&mm->mmap_sem);
4548d2afd96SAndrea Arcangeli 
4558d2afd96SAndrea Arcangeli 	if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
456dfa37dc3SAndrea Arcangeli 		   (return_to_userland ? !signal_pending(current) :
457dfa37dc3SAndrea Arcangeli 		    !fatal_signal_pending(current)))) {
45886039bd3SAndrea Arcangeli 		wake_up_poll(&ctx->fd_wqh, POLLIN);
45986039bd3SAndrea Arcangeli 		schedule();
460ba85c702SAndrea Arcangeli 		ret |= VM_FAULT_MAJOR;
46115a77c6fSAndrea Arcangeli 
46215a77c6fSAndrea Arcangeli 		/*
46315a77c6fSAndrea Arcangeli 		 * False wakeups can orginate even from rwsem before
46415a77c6fSAndrea Arcangeli 		 * up_read() however userfaults will wait either for a
46515a77c6fSAndrea Arcangeli 		 * targeted wakeup on the specific uwq waitqueue from
46615a77c6fSAndrea Arcangeli 		 * wake_userfault() or for signals or for uffd
46715a77c6fSAndrea Arcangeli 		 * release.
46815a77c6fSAndrea Arcangeli 		 */
46915a77c6fSAndrea Arcangeli 		while (!READ_ONCE(uwq.waken)) {
47015a77c6fSAndrea Arcangeli 			/*
47115a77c6fSAndrea Arcangeli 			 * This needs the full smp_store_mb()
47215a77c6fSAndrea Arcangeli 			 * guarantee as the state write must be
47315a77c6fSAndrea Arcangeli 			 * visible to other CPUs before reading
47415a77c6fSAndrea Arcangeli 			 * uwq.waken from other CPUs.
47515a77c6fSAndrea Arcangeli 			 */
47615a77c6fSAndrea Arcangeli 			set_current_state(blocking_state);
47715a77c6fSAndrea Arcangeli 			if (READ_ONCE(uwq.waken) ||
47815a77c6fSAndrea Arcangeli 			    READ_ONCE(ctx->released) ||
47915a77c6fSAndrea Arcangeli 			    (return_to_userland ? signal_pending(current) :
48015a77c6fSAndrea Arcangeli 			     fatal_signal_pending(current)))
48115a77c6fSAndrea Arcangeli 				break;
48215a77c6fSAndrea Arcangeli 			schedule();
48315a77c6fSAndrea Arcangeli 		}
48486039bd3SAndrea Arcangeli 	}
485ba85c702SAndrea Arcangeli 
48686039bd3SAndrea Arcangeli 	__set_current_state(TASK_RUNNING);
48715b726efSAndrea Arcangeli 
488dfa37dc3SAndrea Arcangeli 	if (return_to_userland) {
489dfa37dc3SAndrea Arcangeli 		if (signal_pending(current) &&
490dfa37dc3SAndrea Arcangeli 		    !fatal_signal_pending(current)) {
491dfa37dc3SAndrea Arcangeli 			/*
492dfa37dc3SAndrea Arcangeli 			 * If we got a SIGSTOP or SIGCONT and this is
493dfa37dc3SAndrea Arcangeli 			 * a normal userland page fault, just let
494dfa37dc3SAndrea Arcangeli 			 * userland return so the signal will be
495dfa37dc3SAndrea Arcangeli 			 * handled and gdb debugging works.  The page
496dfa37dc3SAndrea Arcangeli 			 * fault code immediately after we return from
497dfa37dc3SAndrea Arcangeli 			 * this function is going to release the
498dfa37dc3SAndrea Arcangeli 			 * mmap_sem and it's not depending on it
499dfa37dc3SAndrea Arcangeli 			 * (unlike gup would if we were not to return
500dfa37dc3SAndrea Arcangeli 			 * VM_FAULT_RETRY).
501dfa37dc3SAndrea Arcangeli 			 *
502dfa37dc3SAndrea Arcangeli 			 * If a fatal signal is pending we still take
503dfa37dc3SAndrea Arcangeli 			 * the streamlined VM_FAULT_RETRY failure path
504dfa37dc3SAndrea Arcangeli 			 * and there's no need to retake the mmap_sem
505dfa37dc3SAndrea Arcangeli 			 * in such case.
506dfa37dc3SAndrea Arcangeli 			 */
507dfa37dc3SAndrea Arcangeli 			down_read(&mm->mmap_sem);
5086bbc4a41SAndrea Arcangeli 			ret = VM_FAULT_NOPAGE;
509dfa37dc3SAndrea Arcangeli 		}
510dfa37dc3SAndrea Arcangeli 	}
511dfa37dc3SAndrea Arcangeli 
51215b726efSAndrea Arcangeli 	/*
51315b726efSAndrea Arcangeli 	 * Here we race with the list_del; list_add in
51415b726efSAndrea Arcangeli 	 * userfaultfd_ctx_read(), however because we don't ever run
51515b726efSAndrea Arcangeli 	 * list_del_init() to refile across the two lists, the prev
51615b726efSAndrea Arcangeli 	 * and next pointers will never point to self. list_add also
51715b726efSAndrea Arcangeli 	 * would never let any of the two pointers to point to
51815b726efSAndrea Arcangeli 	 * self. So list_empty_careful won't risk to see both pointers
51915b726efSAndrea Arcangeli 	 * pointing to self at any time during the list refile. The
52015b726efSAndrea Arcangeli 	 * only case where list_del_init() is called is the full
52115b726efSAndrea Arcangeli 	 * removal in the wake function and there we don't re-list_add
52215b726efSAndrea Arcangeli 	 * and it's fine not to block on the spinlock. The uwq on this
52315b726efSAndrea Arcangeli 	 * kernel stack can be released after the list_del_init.
52415b726efSAndrea Arcangeli 	 */
525*2055da97SIngo Molnar 	if (!list_empty_careful(&uwq.wq.entry)) {
52615b726efSAndrea Arcangeli 		spin_lock(&ctx->fault_pending_wqh.lock);
52715b726efSAndrea Arcangeli 		/*
52815b726efSAndrea Arcangeli 		 * No need of list_del_init(), the uwq on the stack
52915b726efSAndrea Arcangeli 		 * will be freed shortly anyway.
53015b726efSAndrea Arcangeli 		 */
531*2055da97SIngo Molnar 		list_del(&uwq.wq.entry);
53215b726efSAndrea Arcangeli 		spin_unlock(&ctx->fault_pending_wqh.lock);
533ba85c702SAndrea Arcangeli 	}
53486039bd3SAndrea Arcangeli 
53586039bd3SAndrea Arcangeli 	/*
53686039bd3SAndrea Arcangeli 	 * ctx may go away after this if the userfault pseudo fd is
53786039bd3SAndrea Arcangeli 	 * already released.
53886039bd3SAndrea Arcangeli 	 */
53986039bd3SAndrea Arcangeli 	userfaultfd_ctx_put(ctx);
54086039bd3SAndrea Arcangeli 
541ba85c702SAndrea Arcangeli out:
542ba85c702SAndrea Arcangeli 	return ret;
54386039bd3SAndrea Arcangeli }
54486039bd3SAndrea Arcangeli 
5458c9e7bb7SAndrea Arcangeli static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
5469cd75c3cSPavel Emelyanov 					      struct userfaultfd_wait_queue *ewq)
5479cd75c3cSPavel Emelyanov {
5489a69a829SAndrea Arcangeli 	if (WARN_ON_ONCE(current->flags & PF_EXITING))
5499a69a829SAndrea Arcangeli 		goto out;
5509a69a829SAndrea Arcangeli 
5519cd75c3cSPavel Emelyanov 	ewq->ctx = ctx;
5529cd75c3cSPavel Emelyanov 	init_waitqueue_entry(&ewq->wq, current);
5539cd75c3cSPavel Emelyanov 
5549cd75c3cSPavel Emelyanov 	spin_lock(&ctx->event_wqh.lock);
5559cd75c3cSPavel Emelyanov 	/*
5569cd75c3cSPavel Emelyanov 	 * After the __add_wait_queue the uwq is visible to userland
5579cd75c3cSPavel Emelyanov 	 * through poll/read().
5589cd75c3cSPavel Emelyanov 	 */
5599cd75c3cSPavel Emelyanov 	__add_wait_queue(&ctx->event_wqh, &ewq->wq);
5609cd75c3cSPavel Emelyanov 	for (;;) {
5619cd75c3cSPavel Emelyanov 		set_current_state(TASK_KILLABLE);
5629cd75c3cSPavel Emelyanov 		if (ewq->msg.event == 0)
5639cd75c3cSPavel Emelyanov 			break;
5649cd75c3cSPavel Emelyanov 		if (ACCESS_ONCE(ctx->released) ||
5659cd75c3cSPavel Emelyanov 		    fatal_signal_pending(current)) {
5669cd75c3cSPavel Emelyanov 			__remove_wait_queue(&ctx->event_wqh, &ewq->wq);
5677eb76d45SMike Rapoport 			if (ewq->msg.event == UFFD_EVENT_FORK) {
5687eb76d45SMike Rapoport 				struct userfaultfd_ctx *new;
5697eb76d45SMike Rapoport 
5707eb76d45SMike Rapoport 				new = (struct userfaultfd_ctx *)
5717eb76d45SMike Rapoport 					(unsigned long)
5727eb76d45SMike Rapoport 					ewq->msg.arg.reserved.reserved1;
5737eb76d45SMike Rapoport 
5747eb76d45SMike Rapoport 				userfaultfd_ctx_put(new);
5757eb76d45SMike Rapoport 			}
5769cd75c3cSPavel Emelyanov 			break;
5779cd75c3cSPavel Emelyanov 		}
5789cd75c3cSPavel Emelyanov 
5799cd75c3cSPavel Emelyanov 		spin_unlock(&ctx->event_wqh.lock);
5809cd75c3cSPavel Emelyanov 
5819cd75c3cSPavel Emelyanov 		wake_up_poll(&ctx->fd_wqh, POLLIN);
5829cd75c3cSPavel Emelyanov 		schedule();
5839cd75c3cSPavel Emelyanov 
5849cd75c3cSPavel Emelyanov 		spin_lock(&ctx->event_wqh.lock);
5859cd75c3cSPavel Emelyanov 	}
5869cd75c3cSPavel Emelyanov 	__set_current_state(TASK_RUNNING);
5879cd75c3cSPavel Emelyanov 	spin_unlock(&ctx->event_wqh.lock);
5889cd75c3cSPavel Emelyanov 
5899cd75c3cSPavel Emelyanov 	/*
5909cd75c3cSPavel Emelyanov 	 * ctx may go away after this if the userfault pseudo fd is
5919cd75c3cSPavel Emelyanov 	 * already released.
5929cd75c3cSPavel Emelyanov 	 */
5939a69a829SAndrea Arcangeli out:
5949cd75c3cSPavel Emelyanov 	userfaultfd_ctx_put(ctx);
5959cd75c3cSPavel Emelyanov }
5969cd75c3cSPavel Emelyanov 
5979cd75c3cSPavel Emelyanov static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
5989cd75c3cSPavel Emelyanov 				       struct userfaultfd_wait_queue *ewq)
5999cd75c3cSPavel Emelyanov {
6009cd75c3cSPavel Emelyanov 	ewq->msg.event = 0;
6019cd75c3cSPavel Emelyanov 	wake_up_locked(&ctx->event_wqh);
6029cd75c3cSPavel Emelyanov 	__remove_wait_queue(&ctx->event_wqh, &ewq->wq);
6039cd75c3cSPavel Emelyanov }
6049cd75c3cSPavel Emelyanov 
605893e26e6SPavel Emelyanov int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
606893e26e6SPavel Emelyanov {
607893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *ctx = NULL, *octx;
608893e26e6SPavel Emelyanov 	struct userfaultfd_fork_ctx *fctx;
609893e26e6SPavel Emelyanov 
610893e26e6SPavel Emelyanov 	octx = vma->vm_userfaultfd_ctx.ctx;
611893e26e6SPavel Emelyanov 	if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
612893e26e6SPavel Emelyanov 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
613893e26e6SPavel Emelyanov 		vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
614893e26e6SPavel Emelyanov 		return 0;
615893e26e6SPavel Emelyanov 	}
616893e26e6SPavel Emelyanov 
617893e26e6SPavel Emelyanov 	list_for_each_entry(fctx, fcs, list)
618893e26e6SPavel Emelyanov 		if (fctx->orig == octx) {
619893e26e6SPavel Emelyanov 			ctx = fctx->new;
620893e26e6SPavel Emelyanov 			break;
621893e26e6SPavel Emelyanov 		}
622893e26e6SPavel Emelyanov 
623893e26e6SPavel Emelyanov 	if (!ctx) {
624893e26e6SPavel Emelyanov 		fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
625893e26e6SPavel Emelyanov 		if (!fctx)
626893e26e6SPavel Emelyanov 			return -ENOMEM;
627893e26e6SPavel Emelyanov 
628893e26e6SPavel Emelyanov 		ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
629893e26e6SPavel Emelyanov 		if (!ctx) {
630893e26e6SPavel Emelyanov 			kfree(fctx);
631893e26e6SPavel Emelyanov 			return -ENOMEM;
632893e26e6SPavel Emelyanov 		}
633893e26e6SPavel Emelyanov 
634893e26e6SPavel Emelyanov 		atomic_set(&ctx->refcount, 1);
635893e26e6SPavel Emelyanov 		ctx->flags = octx->flags;
636893e26e6SPavel Emelyanov 		ctx->state = UFFD_STATE_RUNNING;
637893e26e6SPavel Emelyanov 		ctx->features = octx->features;
638893e26e6SPavel Emelyanov 		ctx->released = false;
639893e26e6SPavel Emelyanov 		ctx->mm = vma->vm_mm;
640d3aadc8eSMike Rapoport 		atomic_inc(&ctx->mm->mm_count);
641893e26e6SPavel Emelyanov 
642893e26e6SPavel Emelyanov 		userfaultfd_ctx_get(octx);
643893e26e6SPavel Emelyanov 		fctx->orig = octx;
644893e26e6SPavel Emelyanov 		fctx->new = ctx;
645893e26e6SPavel Emelyanov 		list_add_tail(&fctx->list, fcs);
646893e26e6SPavel Emelyanov 	}
647893e26e6SPavel Emelyanov 
648893e26e6SPavel Emelyanov 	vma->vm_userfaultfd_ctx.ctx = ctx;
649893e26e6SPavel Emelyanov 	return 0;
650893e26e6SPavel Emelyanov }
651893e26e6SPavel Emelyanov 
6528c9e7bb7SAndrea Arcangeli static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
653893e26e6SPavel Emelyanov {
654893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *ctx = fctx->orig;
655893e26e6SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
656893e26e6SPavel Emelyanov 
657893e26e6SPavel Emelyanov 	msg_init(&ewq.msg);
658893e26e6SPavel Emelyanov 
659893e26e6SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_FORK;
660893e26e6SPavel Emelyanov 	ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
661893e26e6SPavel Emelyanov 
6628c9e7bb7SAndrea Arcangeli 	userfaultfd_event_wait_completion(ctx, &ewq);
663893e26e6SPavel Emelyanov }
664893e26e6SPavel Emelyanov 
665893e26e6SPavel Emelyanov void dup_userfaultfd_complete(struct list_head *fcs)
666893e26e6SPavel Emelyanov {
667893e26e6SPavel Emelyanov 	struct userfaultfd_fork_ctx *fctx, *n;
668893e26e6SPavel Emelyanov 
669893e26e6SPavel Emelyanov 	list_for_each_entry_safe(fctx, n, fcs, list) {
6708c9e7bb7SAndrea Arcangeli 		dup_fctx(fctx);
671893e26e6SPavel Emelyanov 		list_del(&fctx->list);
672893e26e6SPavel Emelyanov 		kfree(fctx);
673893e26e6SPavel Emelyanov 	}
674893e26e6SPavel Emelyanov }
675893e26e6SPavel Emelyanov 
67672f87654SPavel Emelyanov void mremap_userfaultfd_prep(struct vm_area_struct *vma,
67772f87654SPavel Emelyanov 			     struct vm_userfaultfd_ctx *vm_ctx)
67872f87654SPavel Emelyanov {
67972f87654SPavel Emelyanov 	struct userfaultfd_ctx *ctx;
68072f87654SPavel Emelyanov 
68172f87654SPavel Emelyanov 	ctx = vma->vm_userfaultfd_ctx.ctx;
68272f87654SPavel Emelyanov 	if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
68372f87654SPavel Emelyanov 		vm_ctx->ctx = ctx;
68472f87654SPavel Emelyanov 		userfaultfd_ctx_get(ctx);
68572f87654SPavel Emelyanov 	}
68672f87654SPavel Emelyanov }
68772f87654SPavel Emelyanov 
68890794bf1SAndrea Arcangeli void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
68972f87654SPavel Emelyanov 				 unsigned long from, unsigned long to,
69072f87654SPavel Emelyanov 				 unsigned long len)
69172f87654SPavel Emelyanov {
69290794bf1SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = vm_ctx->ctx;
69372f87654SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
69472f87654SPavel Emelyanov 
69572f87654SPavel Emelyanov 	if (!ctx)
69672f87654SPavel Emelyanov 		return;
69772f87654SPavel Emelyanov 
69872f87654SPavel Emelyanov 	if (to & ~PAGE_MASK) {
69972f87654SPavel Emelyanov 		userfaultfd_ctx_put(ctx);
70072f87654SPavel Emelyanov 		return;
70172f87654SPavel Emelyanov 	}
70272f87654SPavel Emelyanov 
70372f87654SPavel Emelyanov 	msg_init(&ewq.msg);
70472f87654SPavel Emelyanov 
70572f87654SPavel Emelyanov 	ewq.msg.event = UFFD_EVENT_REMAP;
70672f87654SPavel Emelyanov 	ewq.msg.arg.remap.from = from;
70772f87654SPavel Emelyanov 	ewq.msg.arg.remap.to = to;
70872f87654SPavel Emelyanov 	ewq.msg.arg.remap.len = len;
70972f87654SPavel Emelyanov 
71072f87654SPavel Emelyanov 	userfaultfd_event_wait_completion(ctx, &ewq);
71172f87654SPavel Emelyanov }
71272f87654SPavel Emelyanov 
71370ccb92fSAndrea Arcangeli bool userfaultfd_remove(struct vm_area_struct *vma,
71405ce7724SPavel Emelyanov 			unsigned long start, unsigned long end)
71505ce7724SPavel Emelyanov {
71605ce7724SPavel Emelyanov 	struct mm_struct *mm = vma->vm_mm;
71705ce7724SPavel Emelyanov 	struct userfaultfd_ctx *ctx;
71805ce7724SPavel Emelyanov 	struct userfaultfd_wait_queue ewq;
71905ce7724SPavel Emelyanov 
72005ce7724SPavel Emelyanov 	ctx = vma->vm_userfaultfd_ctx.ctx;
721d811914dSMike Rapoport 	if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
72270ccb92fSAndrea Arcangeli 		return true;
72305ce7724SPavel Emelyanov 
72405ce7724SPavel Emelyanov 	userfaultfd_ctx_get(ctx);
72505ce7724SPavel Emelyanov 	up_read(&mm->mmap_sem);
72605ce7724SPavel Emelyanov 
72705ce7724SPavel Emelyanov 	msg_init(&ewq.msg);
72805ce7724SPavel Emelyanov 
729d811914dSMike Rapoport 	ewq.msg.event = UFFD_EVENT_REMOVE;
730d811914dSMike Rapoport 	ewq.msg.arg.remove.start = start;
731d811914dSMike Rapoport 	ewq.msg.arg.remove.end = end;
73205ce7724SPavel Emelyanov 
73305ce7724SPavel Emelyanov 	userfaultfd_event_wait_completion(ctx, &ewq);
73405ce7724SPavel Emelyanov 
73570ccb92fSAndrea Arcangeli 	return false;
73605ce7724SPavel Emelyanov }
73705ce7724SPavel Emelyanov 
738897ab3e0SMike Rapoport static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
739897ab3e0SMike Rapoport 			  unsigned long start, unsigned long end)
740897ab3e0SMike Rapoport {
741897ab3e0SMike Rapoport 	struct userfaultfd_unmap_ctx *unmap_ctx;
742897ab3e0SMike Rapoport 
743897ab3e0SMike Rapoport 	list_for_each_entry(unmap_ctx, unmaps, list)
744897ab3e0SMike Rapoport 		if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
745897ab3e0SMike Rapoport 		    unmap_ctx->end == end)
746897ab3e0SMike Rapoport 			return true;
747897ab3e0SMike Rapoport 
748897ab3e0SMike Rapoport 	return false;
749897ab3e0SMike Rapoport }
750897ab3e0SMike Rapoport 
751897ab3e0SMike Rapoport int userfaultfd_unmap_prep(struct vm_area_struct *vma,
752897ab3e0SMike Rapoport 			   unsigned long start, unsigned long end,
753897ab3e0SMike Rapoport 			   struct list_head *unmaps)
754897ab3e0SMike Rapoport {
755897ab3e0SMike Rapoport 	for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
756897ab3e0SMike Rapoport 		struct userfaultfd_unmap_ctx *unmap_ctx;
757897ab3e0SMike Rapoport 		struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
758897ab3e0SMike Rapoport 
759897ab3e0SMike Rapoport 		if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
760897ab3e0SMike Rapoport 		    has_unmap_ctx(ctx, unmaps, start, end))
761897ab3e0SMike Rapoport 			continue;
762897ab3e0SMike Rapoport 
763897ab3e0SMike Rapoport 		unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
764897ab3e0SMike Rapoport 		if (!unmap_ctx)
765897ab3e0SMike Rapoport 			return -ENOMEM;
766897ab3e0SMike Rapoport 
767897ab3e0SMike Rapoport 		userfaultfd_ctx_get(ctx);
768897ab3e0SMike Rapoport 		unmap_ctx->ctx = ctx;
769897ab3e0SMike Rapoport 		unmap_ctx->start = start;
770897ab3e0SMike Rapoport 		unmap_ctx->end = end;
771897ab3e0SMike Rapoport 		list_add_tail(&unmap_ctx->list, unmaps);
772897ab3e0SMike Rapoport 	}
773897ab3e0SMike Rapoport 
774897ab3e0SMike Rapoport 	return 0;
775897ab3e0SMike Rapoport }
776897ab3e0SMike Rapoport 
777897ab3e0SMike Rapoport void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
778897ab3e0SMike Rapoport {
779897ab3e0SMike Rapoport 	struct userfaultfd_unmap_ctx *ctx, *n;
780897ab3e0SMike Rapoport 	struct userfaultfd_wait_queue ewq;
781897ab3e0SMike Rapoport 
782897ab3e0SMike Rapoport 	list_for_each_entry_safe(ctx, n, uf, list) {
783897ab3e0SMike Rapoport 		msg_init(&ewq.msg);
784897ab3e0SMike Rapoport 
785897ab3e0SMike Rapoport 		ewq.msg.event = UFFD_EVENT_UNMAP;
786897ab3e0SMike Rapoport 		ewq.msg.arg.remove.start = ctx->start;
787897ab3e0SMike Rapoport 		ewq.msg.arg.remove.end = ctx->end;
788897ab3e0SMike Rapoport 
789897ab3e0SMike Rapoport 		userfaultfd_event_wait_completion(ctx->ctx, &ewq);
790897ab3e0SMike Rapoport 
791897ab3e0SMike Rapoport 		list_del(&ctx->list);
792897ab3e0SMike Rapoport 		kfree(ctx);
793897ab3e0SMike Rapoport 	}
794897ab3e0SMike Rapoport }
795897ab3e0SMike Rapoport 
79686039bd3SAndrea Arcangeli static int userfaultfd_release(struct inode *inode, struct file *file)
79786039bd3SAndrea Arcangeli {
79886039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
79986039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
80086039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev;
80186039bd3SAndrea Arcangeli 	/* len == 0 means wake all */
80286039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range range = { .len = 0, };
80386039bd3SAndrea Arcangeli 	unsigned long new_flags;
80486039bd3SAndrea Arcangeli 
80586039bd3SAndrea Arcangeli 	ACCESS_ONCE(ctx->released) = true;
80686039bd3SAndrea Arcangeli 
807d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
808d2005e3fSOleg Nesterov 		goto wakeup;
809d2005e3fSOleg Nesterov 
81086039bd3SAndrea Arcangeli 	/*
81186039bd3SAndrea Arcangeli 	 * Flush page faults out of all CPUs. NOTE: all page faults
81286039bd3SAndrea Arcangeli 	 * must be retried without returning VM_FAULT_SIGBUS if
81386039bd3SAndrea Arcangeli 	 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
81486039bd3SAndrea Arcangeli 	 * changes while handle_userfault released the mmap_sem. So
81586039bd3SAndrea Arcangeli 	 * it's critical that released is set to true (above), before
81686039bd3SAndrea Arcangeli 	 * taking the mmap_sem for writing.
81786039bd3SAndrea Arcangeli 	 */
81886039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
81986039bd3SAndrea Arcangeli 	prev = NULL;
82086039bd3SAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
82186039bd3SAndrea Arcangeli 		cond_resched();
82286039bd3SAndrea Arcangeli 		BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
82386039bd3SAndrea Arcangeli 		       !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
82486039bd3SAndrea Arcangeli 		if (vma->vm_userfaultfd_ctx.ctx != ctx) {
82586039bd3SAndrea Arcangeli 			prev = vma;
82686039bd3SAndrea Arcangeli 			continue;
82786039bd3SAndrea Arcangeli 		}
82886039bd3SAndrea Arcangeli 		new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
82986039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
83086039bd3SAndrea Arcangeli 				 new_flags, vma->anon_vma,
83186039bd3SAndrea Arcangeli 				 vma->vm_file, vma->vm_pgoff,
83286039bd3SAndrea Arcangeli 				 vma_policy(vma),
83386039bd3SAndrea Arcangeli 				 NULL_VM_UFFD_CTX);
83486039bd3SAndrea Arcangeli 		if (prev)
83586039bd3SAndrea Arcangeli 			vma = prev;
83686039bd3SAndrea Arcangeli 		else
83786039bd3SAndrea Arcangeli 			prev = vma;
83886039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
83986039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
84086039bd3SAndrea Arcangeli 	}
84186039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
842d2005e3fSOleg Nesterov 	mmput(mm);
843d2005e3fSOleg Nesterov wakeup:
84486039bd3SAndrea Arcangeli 	/*
84515b726efSAndrea Arcangeli 	 * After no new page faults can wait on this fault_*wqh, flush
84686039bd3SAndrea Arcangeli 	 * the last page faults that may have been already waiting on
84715b726efSAndrea Arcangeli 	 * the fault_*wqh.
84886039bd3SAndrea Arcangeli 	 */
84915b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
850ac5be6b4SAndrea Arcangeli 	__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
851ac5be6b4SAndrea Arcangeli 	__wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
85215b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
85386039bd3SAndrea Arcangeli 
85486039bd3SAndrea Arcangeli 	wake_up_poll(&ctx->fd_wqh, POLLHUP);
85586039bd3SAndrea Arcangeli 	userfaultfd_ctx_put(ctx);
85686039bd3SAndrea Arcangeli 	return 0;
85786039bd3SAndrea Arcangeli }
85886039bd3SAndrea Arcangeli 
85915b726efSAndrea Arcangeli /* fault_pending_wqh.lock must be hold by the caller */
8606dcc27fdSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault_in(
8616dcc27fdSPavel Emelyanov 		wait_queue_head_t *wqh)
86286039bd3SAndrea Arcangeli {
863ac6424b9SIngo Molnar 	wait_queue_entry_t *wq;
86415b726efSAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
86586039bd3SAndrea Arcangeli 
8666dcc27fdSPavel Emelyanov 	VM_BUG_ON(!spin_is_locked(&wqh->lock));
86786039bd3SAndrea Arcangeli 
86815b726efSAndrea Arcangeli 	uwq = NULL;
8696dcc27fdSPavel Emelyanov 	if (!waitqueue_active(wqh))
87015b726efSAndrea Arcangeli 		goto out;
87115b726efSAndrea Arcangeli 	/* walk in reverse to provide FIFO behavior to read userfaults */
872*2055da97SIngo Molnar 	wq = list_last_entry(&wqh->head, typeof(*wq), entry);
87315b726efSAndrea Arcangeli 	uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
87415b726efSAndrea Arcangeli out:
87515b726efSAndrea Arcangeli 	return uwq;
87686039bd3SAndrea Arcangeli }
87786039bd3SAndrea Arcangeli 
8786dcc27fdSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault(
8796dcc27fdSPavel Emelyanov 		struct userfaultfd_ctx *ctx)
8806dcc27fdSPavel Emelyanov {
8816dcc27fdSPavel Emelyanov 	return find_userfault_in(&ctx->fault_pending_wqh);
8826dcc27fdSPavel Emelyanov }
8836dcc27fdSPavel Emelyanov 
8849cd75c3cSPavel Emelyanov static inline struct userfaultfd_wait_queue *find_userfault_evt(
8859cd75c3cSPavel Emelyanov 		struct userfaultfd_ctx *ctx)
8869cd75c3cSPavel Emelyanov {
8879cd75c3cSPavel Emelyanov 	return find_userfault_in(&ctx->event_wqh);
8889cd75c3cSPavel Emelyanov }
8899cd75c3cSPavel Emelyanov 
89086039bd3SAndrea Arcangeli static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
89186039bd3SAndrea Arcangeli {
89286039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
89386039bd3SAndrea Arcangeli 	unsigned int ret;
89486039bd3SAndrea Arcangeli 
89586039bd3SAndrea Arcangeli 	poll_wait(file, &ctx->fd_wqh, wait);
89686039bd3SAndrea Arcangeli 
89786039bd3SAndrea Arcangeli 	switch (ctx->state) {
89886039bd3SAndrea Arcangeli 	case UFFD_STATE_WAIT_API:
89986039bd3SAndrea Arcangeli 		return POLLERR;
90086039bd3SAndrea Arcangeli 	case UFFD_STATE_RUNNING:
901ba85c702SAndrea Arcangeli 		/*
902ba85c702SAndrea Arcangeli 		 * poll() never guarantees that read won't block.
903ba85c702SAndrea Arcangeli 		 * userfaults can be waken before they're read().
904ba85c702SAndrea Arcangeli 		 */
905ba85c702SAndrea Arcangeli 		if (unlikely(!(file->f_flags & O_NONBLOCK)))
906ba85c702SAndrea Arcangeli 			return POLLERR;
90715b726efSAndrea Arcangeli 		/*
90815b726efSAndrea Arcangeli 		 * lockless access to see if there are pending faults
90915b726efSAndrea Arcangeli 		 * __pollwait last action is the add_wait_queue but
91015b726efSAndrea Arcangeli 		 * the spin_unlock would allow the waitqueue_active to
91115b726efSAndrea Arcangeli 		 * pass above the actual list_add inside
91215b726efSAndrea Arcangeli 		 * add_wait_queue critical section. So use a full
91315b726efSAndrea Arcangeli 		 * memory barrier to serialize the list_add write of
91415b726efSAndrea Arcangeli 		 * add_wait_queue() with the waitqueue_active read
91515b726efSAndrea Arcangeli 		 * below.
91615b726efSAndrea Arcangeli 		 */
91715b726efSAndrea Arcangeli 		ret = 0;
91815b726efSAndrea Arcangeli 		smp_mb();
91915b726efSAndrea Arcangeli 		if (waitqueue_active(&ctx->fault_pending_wqh))
92015b726efSAndrea Arcangeli 			ret = POLLIN;
9219cd75c3cSPavel Emelyanov 		else if (waitqueue_active(&ctx->event_wqh))
9229cd75c3cSPavel Emelyanov 			ret = POLLIN;
9239cd75c3cSPavel Emelyanov 
92486039bd3SAndrea Arcangeli 		return ret;
92586039bd3SAndrea Arcangeli 	default:
9268474901aSAndrea Arcangeli 		WARN_ON_ONCE(1);
9278474901aSAndrea Arcangeli 		return POLLERR;
92886039bd3SAndrea Arcangeli 	}
92986039bd3SAndrea Arcangeli }
93086039bd3SAndrea Arcangeli 
931893e26e6SPavel Emelyanov static const struct file_operations userfaultfd_fops;
932893e26e6SPavel Emelyanov 
933893e26e6SPavel Emelyanov static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
934893e26e6SPavel Emelyanov 				  struct userfaultfd_ctx *new,
935893e26e6SPavel Emelyanov 				  struct uffd_msg *msg)
936893e26e6SPavel Emelyanov {
937893e26e6SPavel Emelyanov 	int fd;
938893e26e6SPavel Emelyanov 	struct file *file;
939893e26e6SPavel Emelyanov 	unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
940893e26e6SPavel Emelyanov 
941893e26e6SPavel Emelyanov 	fd = get_unused_fd_flags(flags);
942893e26e6SPavel Emelyanov 	if (fd < 0)
943893e26e6SPavel Emelyanov 		return fd;
944893e26e6SPavel Emelyanov 
945893e26e6SPavel Emelyanov 	file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
946893e26e6SPavel Emelyanov 				  O_RDWR | flags);
947893e26e6SPavel Emelyanov 	if (IS_ERR(file)) {
948893e26e6SPavel Emelyanov 		put_unused_fd(fd);
949893e26e6SPavel Emelyanov 		return PTR_ERR(file);
950893e26e6SPavel Emelyanov 	}
951893e26e6SPavel Emelyanov 
952893e26e6SPavel Emelyanov 	fd_install(fd, file);
953893e26e6SPavel Emelyanov 	msg->arg.reserved.reserved1 = 0;
954893e26e6SPavel Emelyanov 	msg->arg.fork.ufd = fd;
955893e26e6SPavel Emelyanov 
956893e26e6SPavel Emelyanov 	return 0;
957893e26e6SPavel Emelyanov }
958893e26e6SPavel Emelyanov 
95986039bd3SAndrea Arcangeli static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
960a9b85f94SAndrea Arcangeli 				    struct uffd_msg *msg)
96186039bd3SAndrea Arcangeli {
96286039bd3SAndrea Arcangeli 	ssize_t ret;
96386039bd3SAndrea Arcangeli 	DECLARE_WAITQUEUE(wait, current);
96415b726efSAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
965893e26e6SPavel Emelyanov 	/*
966893e26e6SPavel Emelyanov 	 * Handling fork event requires sleeping operations, so
967893e26e6SPavel Emelyanov 	 * we drop the event_wqh lock, then do these ops, then
968893e26e6SPavel Emelyanov 	 * lock it back and wake up the waiter. While the lock is
969893e26e6SPavel Emelyanov 	 * dropped the ewq may go away so we keep track of it
970893e26e6SPavel Emelyanov 	 * carefully.
971893e26e6SPavel Emelyanov 	 */
972893e26e6SPavel Emelyanov 	LIST_HEAD(fork_event);
973893e26e6SPavel Emelyanov 	struct userfaultfd_ctx *fork_nctx = NULL;
97486039bd3SAndrea Arcangeli 
97515b726efSAndrea Arcangeli 	/* always take the fd_wqh lock before the fault_pending_wqh lock */
97686039bd3SAndrea Arcangeli 	spin_lock(&ctx->fd_wqh.lock);
97786039bd3SAndrea Arcangeli 	__add_wait_queue(&ctx->fd_wqh, &wait);
97886039bd3SAndrea Arcangeli 	for (;;) {
97986039bd3SAndrea Arcangeli 		set_current_state(TASK_INTERRUPTIBLE);
98015b726efSAndrea Arcangeli 		spin_lock(&ctx->fault_pending_wqh.lock);
98115b726efSAndrea Arcangeli 		uwq = find_userfault(ctx);
98215b726efSAndrea Arcangeli 		if (uwq) {
98386039bd3SAndrea Arcangeli 			/*
9842c5b7e1bSAndrea Arcangeli 			 * Use a seqcount to repeat the lockless check
9852c5b7e1bSAndrea Arcangeli 			 * in wake_userfault() to avoid missing
9862c5b7e1bSAndrea Arcangeli 			 * wakeups because during the refile both
9872c5b7e1bSAndrea Arcangeli 			 * waitqueue could become empty if this is the
9882c5b7e1bSAndrea Arcangeli 			 * only userfault.
9892c5b7e1bSAndrea Arcangeli 			 */
9902c5b7e1bSAndrea Arcangeli 			write_seqcount_begin(&ctx->refile_seq);
9912c5b7e1bSAndrea Arcangeli 
9922c5b7e1bSAndrea Arcangeli 			/*
99315b726efSAndrea Arcangeli 			 * The fault_pending_wqh.lock prevents the uwq
99415b726efSAndrea Arcangeli 			 * to disappear from under us.
99515b726efSAndrea Arcangeli 			 *
99615b726efSAndrea Arcangeli 			 * Refile this userfault from
99715b726efSAndrea Arcangeli 			 * fault_pending_wqh to fault_wqh, it's not
99815b726efSAndrea Arcangeli 			 * pending anymore after we read it.
99915b726efSAndrea Arcangeli 			 *
100015b726efSAndrea Arcangeli 			 * Use list_del() by hand (as
100115b726efSAndrea Arcangeli 			 * userfaultfd_wake_function also uses
100215b726efSAndrea Arcangeli 			 * list_del_init() by hand) to be sure nobody
100315b726efSAndrea Arcangeli 			 * changes __remove_wait_queue() to use
100415b726efSAndrea Arcangeli 			 * list_del_init() in turn breaking the
100515b726efSAndrea Arcangeli 			 * !list_empty_careful() check in
1006*2055da97SIngo Molnar 			 * handle_userfault(). The uwq->wq.head list
100715b726efSAndrea Arcangeli 			 * must never be empty at any time during the
100815b726efSAndrea Arcangeli 			 * refile, or the waitqueue could disappear
100915b726efSAndrea Arcangeli 			 * from under us. The "wait_queue_head_t"
101015b726efSAndrea Arcangeli 			 * parameter of __remove_wait_queue() is unused
101115b726efSAndrea Arcangeli 			 * anyway.
101286039bd3SAndrea Arcangeli 			 */
1013*2055da97SIngo Molnar 			list_del(&uwq->wq.entry);
101415b726efSAndrea Arcangeli 			__add_wait_queue(&ctx->fault_wqh, &uwq->wq);
101515b726efSAndrea Arcangeli 
10162c5b7e1bSAndrea Arcangeli 			write_seqcount_end(&ctx->refile_seq);
10172c5b7e1bSAndrea Arcangeli 
1018a9b85f94SAndrea Arcangeli 			/* careful to always initialize msg if ret == 0 */
1019a9b85f94SAndrea Arcangeli 			*msg = uwq->msg;
102015b726efSAndrea Arcangeli 			spin_unlock(&ctx->fault_pending_wqh.lock);
102186039bd3SAndrea Arcangeli 			ret = 0;
102286039bd3SAndrea Arcangeli 			break;
102386039bd3SAndrea Arcangeli 		}
102415b726efSAndrea Arcangeli 		spin_unlock(&ctx->fault_pending_wqh.lock);
10259cd75c3cSPavel Emelyanov 
10269cd75c3cSPavel Emelyanov 		spin_lock(&ctx->event_wqh.lock);
10279cd75c3cSPavel Emelyanov 		uwq = find_userfault_evt(ctx);
10289cd75c3cSPavel Emelyanov 		if (uwq) {
10299cd75c3cSPavel Emelyanov 			*msg = uwq->msg;
10309cd75c3cSPavel Emelyanov 
1031893e26e6SPavel Emelyanov 			if (uwq->msg.event == UFFD_EVENT_FORK) {
1032893e26e6SPavel Emelyanov 				fork_nctx = (struct userfaultfd_ctx *)
1033893e26e6SPavel Emelyanov 					(unsigned long)
1034893e26e6SPavel Emelyanov 					uwq->msg.arg.reserved.reserved1;
1035*2055da97SIngo Molnar 				list_move(&uwq->wq.entry, &fork_event);
1036893e26e6SPavel Emelyanov 				spin_unlock(&ctx->event_wqh.lock);
1037893e26e6SPavel Emelyanov 				ret = 0;
1038893e26e6SPavel Emelyanov 				break;
1039893e26e6SPavel Emelyanov 			}
1040893e26e6SPavel Emelyanov 
10419cd75c3cSPavel Emelyanov 			userfaultfd_event_complete(ctx, uwq);
10429cd75c3cSPavel Emelyanov 			spin_unlock(&ctx->event_wqh.lock);
10439cd75c3cSPavel Emelyanov 			ret = 0;
10449cd75c3cSPavel Emelyanov 			break;
10459cd75c3cSPavel Emelyanov 		}
10469cd75c3cSPavel Emelyanov 		spin_unlock(&ctx->event_wqh.lock);
10479cd75c3cSPavel Emelyanov 
104886039bd3SAndrea Arcangeli 		if (signal_pending(current)) {
104986039bd3SAndrea Arcangeli 			ret = -ERESTARTSYS;
105086039bd3SAndrea Arcangeli 			break;
105186039bd3SAndrea Arcangeli 		}
105286039bd3SAndrea Arcangeli 		if (no_wait) {
105386039bd3SAndrea Arcangeli 			ret = -EAGAIN;
105486039bd3SAndrea Arcangeli 			break;
105586039bd3SAndrea Arcangeli 		}
105686039bd3SAndrea Arcangeli 		spin_unlock(&ctx->fd_wqh.lock);
105786039bd3SAndrea Arcangeli 		schedule();
105886039bd3SAndrea Arcangeli 		spin_lock(&ctx->fd_wqh.lock);
105986039bd3SAndrea Arcangeli 	}
106086039bd3SAndrea Arcangeli 	__remove_wait_queue(&ctx->fd_wqh, &wait);
106186039bd3SAndrea Arcangeli 	__set_current_state(TASK_RUNNING);
106286039bd3SAndrea Arcangeli 	spin_unlock(&ctx->fd_wqh.lock);
106386039bd3SAndrea Arcangeli 
1064893e26e6SPavel Emelyanov 	if (!ret && msg->event == UFFD_EVENT_FORK) {
1065893e26e6SPavel Emelyanov 		ret = resolve_userfault_fork(ctx, fork_nctx, msg);
1066893e26e6SPavel Emelyanov 
1067893e26e6SPavel Emelyanov 		if (!ret) {
1068893e26e6SPavel Emelyanov 			spin_lock(&ctx->event_wqh.lock);
1069893e26e6SPavel Emelyanov 			if (!list_empty(&fork_event)) {
1070893e26e6SPavel Emelyanov 				uwq = list_first_entry(&fork_event,
1071893e26e6SPavel Emelyanov 						       typeof(*uwq),
1072*2055da97SIngo Molnar 						       wq.entry);
1073*2055da97SIngo Molnar 				list_del(&uwq->wq.entry);
1074893e26e6SPavel Emelyanov 				__add_wait_queue(&ctx->event_wqh, &uwq->wq);
1075893e26e6SPavel Emelyanov 				userfaultfd_event_complete(ctx, uwq);
1076893e26e6SPavel Emelyanov 			}
1077893e26e6SPavel Emelyanov 			spin_unlock(&ctx->event_wqh.lock);
1078893e26e6SPavel Emelyanov 		}
1079893e26e6SPavel Emelyanov 	}
1080893e26e6SPavel Emelyanov 
108186039bd3SAndrea Arcangeli 	return ret;
108286039bd3SAndrea Arcangeli }
108386039bd3SAndrea Arcangeli 
108486039bd3SAndrea Arcangeli static ssize_t userfaultfd_read(struct file *file, char __user *buf,
108586039bd3SAndrea Arcangeli 				size_t count, loff_t *ppos)
108686039bd3SAndrea Arcangeli {
108786039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
108886039bd3SAndrea Arcangeli 	ssize_t _ret, ret = 0;
1089a9b85f94SAndrea Arcangeli 	struct uffd_msg msg;
109086039bd3SAndrea Arcangeli 	int no_wait = file->f_flags & O_NONBLOCK;
109186039bd3SAndrea Arcangeli 
109286039bd3SAndrea Arcangeli 	if (ctx->state == UFFD_STATE_WAIT_API)
109386039bd3SAndrea Arcangeli 		return -EINVAL;
109486039bd3SAndrea Arcangeli 
109586039bd3SAndrea Arcangeli 	for (;;) {
1096a9b85f94SAndrea Arcangeli 		if (count < sizeof(msg))
109786039bd3SAndrea Arcangeli 			return ret ? ret : -EINVAL;
1098a9b85f94SAndrea Arcangeli 		_ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
109986039bd3SAndrea Arcangeli 		if (_ret < 0)
110086039bd3SAndrea Arcangeli 			return ret ? ret : _ret;
1101a9b85f94SAndrea Arcangeli 		if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
110286039bd3SAndrea Arcangeli 			return ret ? ret : -EFAULT;
1103a9b85f94SAndrea Arcangeli 		ret += sizeof(msg);
1104a9b85f94SAndrea Arcangeli 		buf += sizeof(msg);
1105a9b85f94SAndrea Arcangeli 		count -= sizeof(msg);
110686039bd3SAndrea Arcangeli 		/*
110786039bd3SAndrea Arcangeli 		 * Allow to read more than one fault at time but only
110886039bd3SAndrea Arcangeli 		 * block if waiting for the very first one.
110986039bd3SAndrea Arcangeli 		 */
111086039bd3SAndrea Arcangeli 		no_wait = O_NONBLOCK;
111186039bd3SAndrea Arcangeli 	}
111286039bd3SAndrea Arcangeli }
111386039bd3SAndrea Arcangeli 
111486039bd3SAndrea Arcangeli static void __wake_userfault(struct userfaultfd_ctx *ctx,
111586039bd3SAndrea Arcangeli 			     struct userfaultfd_wake_range *range)
111686039bd3SAndrea Arcangeli {
111786039bd3SAndrea Arcangeli 	unsigned long start, end;
111886039bd3SAndrea Arcangeli 
111986039bd3SAndrea Arcangeli 	start = range->start;
112086039bd3SAndrea Arcangeli 	end = range->start + range->len;
112186039bd3SAndrea Arcangeli 
112215b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
112386039bd3SAndrea Arcangeli 	/* wake all in the range and autoremove */
112415b726efSAndrea Arcangeli 	if (waitqueue_active(&ctx->fault_pending_wqh))
1125ac5be6b4SAndrea Arcangeli 		__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
112615b726efSAndrea Arcangeli 				     range);
112715b726efSAndrea Arcangeli 	if (waitqueue_active(&ctx->fault_wqh))
1128ac5be6b4SAndrea Arcangeli 		__wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
112915b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
113086039bd3SAndrea Arcangeli }
113186039bd3SAndrea Arcangeli 
113286039bd3SAndrea Arcangeli static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
113386039bd3SAndrea Arcangeli 					   struct userfaultfd_wake_range *range)
113486039bd3SAndrea Arcangeli {
11352c5b7e1bSAndrea Arcangeli 	unsigned seq;
11362c5b7e1bSAndrea Arcangeli 	bool need_wakeup;
11372c5b7e1bSAndrea Arcangeli 
113886039bd3SAndrea Arcangeli 	/*
113986039bd3SAndrea Arcangeli 	 * To be sure waitqueue_active() is not reordered by the CPU
114086039bd3SAndrea Arcangeli 	 * before the pagetable update, use an explicit SMP memory
114186039bd3SAndrea Arcangeli 	 * barrier here. PT lock release or up_read(mmap_sem) still
114286039bd3SAndrea Arcangeli 	 * have release semantics that can allow the
114386039bd3SAndrea Arcangeli 	 * waitqueue_active() to be reordered before the pte update.
114486039bd3SAndrea Arcangeli 	 */
114586039bd3SAndrea Arcangeli 	smp_mb();
114686039bd3SAndrea Arcangeli 
114786039bd3SAndrea Arcangeli 	/*
114886039bd3SAndrea Arcangeli 	 * Use waitqueue_active because it's very frequent to
114986039bd3SAndrea Arcangeli 	 * change the address space atomically even if there are no
115086039bd3SAndrea Arcangeli 	 * userfaults yet. So we take the spinlock only when we're
115186039bd3SAndrea Arcangeli 	 * sure we've userfaults to wake.
115286039bd3SAndrea Arcangeli 	 */
11532c5b7e1bSAndrea Arcangeli 	do {
11542c5b7e1bSAndrea Arcangeli 		seq = read_seqcount_begin(&ctx->refile_seq);
11552c5b7e1bSAndrea Arcangeli 		need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
11562c5b7e1bSAndrea Arcangeli 			waitqueue_active(&ctx->fault_wqh);
11572c5b7e1bSAndrea Arcangeli 		cond_resched();
11582c5b7e1bSAndrea Arcangeli 	} while (read_seqcount_retry(&ctx->refile_seq, seq));
11592c5b7e1bSAndrea Arcangeli 	if (need_wakeup)
116086039bd3SAndrea Arcangeli 		__wake_userfault(ctx, range);
116186039bd3SAndrea Arcangeli }
116286039bd3SAndrea Arcangeli 
116386039bd3SAndrea Arcangeli static __always_inline int validate_range(struct mm_struct *mm,
116486039bd3SAndrea Arcangeli 					  __u64 start, __u64 len)
116586039bd3SAndrea Arcangeli {
116686039bd3SAndrea Arcangeli 	__u64 task_size = mm->task_size;
116786039bd3SAndrea Arcangeli 
116886039bd3SAndrea Arcangeli 	if (start & ~PAGE_MASK)
116986039bd3SAndrea Arcangeli 		return -EINVAL;
117086039bd3SAndrea Arcangeli 	if (len & ~PAGE_MASK)
117186039bd3SAndrea Arcangeli 		return -EINVAL;
117286039bd3SAndrea Arcangeli 	if (!len)
117386039bd3SAndrea Arcangeli 		return -EINVAL;
117486039bd3SAndrea Arcangeli 	if (start < mmap_min_addr)
117586039bd3SAndrea Arcangeli 		return -EINVAL;
117686039bd3SAndrea Arcangeli 	if (start >= task_size)
117786039bd3SAndrea Arcangeli 		return -EINVAL;
117886039bd3SAndrea Arcangeli 	if (len > task_size - start)
117986039bd3SAndrea Arcangeli 		return -EINVAL;
118086039bd3SAndrea Arcangeli 	return 0;
118186039bd3SAndrea Arcangeli }
118286039bd3SAndrea Arcangeli 
1183ba6907dbSMike Rapoport static inline bool vma_can_userfault(struct vm_area_struct *vma)
1184ba6907dbSMike Rapoport {
1185cac67329SMike Rapoport 	return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1186cac67329SMike Rapoport 		vma_is_shmem(vma);
1187ba6907dbSMike Rapoport }
1188ba6907dbSMike Rapoport 
118986039bd3SAndrea Arcangeli static int userfaultfd_register(struct userfaultfd_ctx *ctx,
119086039bd3SAndrea Arcangeli 				unsigned long arg)
119186039bd3SAndrea Arcangeli {
119286039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
119386039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev, *cur;
119486039bd3SAndrea Arcangeli 	int ret;
119586039bd3SAndrea Arcangeli 	struct uffdio_register uffdio_register;
119686039bd3SAndrea Arcangeli 	struct uffdio_register __user *user_uffdio_register;
119786039bd3SAndrea Arcangeli 	unsigned long vm_flags, new_flags;
119886039bd3SAndrea Arcangeli 	bool found;
1199cac67329SMike Rapoport 	bool non_anon_pages;
120086039bd3SAndrea Arcangeli 	unsigned long start, end, vma_end;
120186039bd3SAndrea Arcangeli 
120286039bd3SAndrea Arcangeli 	user_uffdio_register = (struct uffdio_register __user *) arg;
120386039bd3SAndrea Arcangeli 
120486039bd3SAndrea Arcangeli 	ret = -EFAULT;
120586039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_register, user_uffdio_register,
120686039bd3SAndrea Arcangeli 			   sizeof(uffdio_register)-sizeof(__u64)))
120786039bd3SAndrea Arcangeli 		goto out;
120886039bd3SAndrea Arcangeli 
120986039bd3SAndrea Arcangeli 	ret = -EINVAL;
121086039bd3SAndrea Arcangeli 	if (!uffdio_register.mode)
121186039bd3SAndrea Arcangeli 		goto out;
121286039bd3SAndrea Arcangeli 	if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
121386039bd3SAndrea Arcangeli 				     UFFDIO_REGISTER_MODE_WP))
121486039bd3SAndrea Arcangeli 		goto out;
121586039bd3SAndrea Arcangeli 	vm_flags = 0;
121686039bd3SAndrea Arcangeli 	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
121786039bd3SAndrea Arcangeli 		vm_flags |= VM_UFFD_MISSING;
121886039bd3SAndrea Arcangeli 	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
121986039bd3SAndrea Arcangeli 		vm_flags |= VM_UFFD_WP;
122086039bd3SAndrea Arcangeli 		/*
122186039bd3SAndrea Arcangeli 		 * FIXME: remove the below error constraint by
122286039bd3SAndrea Arcangeli 		 * implementing the wprotect tracking mode.
122386039bd3SAndrea Arcangeli 		 */
122486039bd3SAndrea Arcangeli 		ret = -EINVAL;
122586039bd3SAndrea Arcangeli 		goto out;
122686039bd3SAndrea Arcangeli 	}
122786039bd3SAndrea Arcangeli 
122886039bd3SAndrea Arcangeli 	ret = validate_range(mm, uffdio_register.range.start,
122986039bd3SAndrea Arcangeli 			     uffdio_register.range.len);
123086039bd3SAndrea Arcangeli 	if (ret)
123186039bd3SAndrea Arcangeli 		goto out;
123286039bd3SAndrea Arcangeli 
123386039bd3SAndrea Arcangeli 	start = uffdio_register.range.start;
123486039bd3SAndrea Arcangeli 	end = start + uffdio_register.range.len;
123586039bd3SAndrea Arcangeli 
1236d2005e3fSOleg Nesterov 	ret = -ENOMEM;
1237d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
1238d2005e3fSOleg Nesterov 		goto out;
1239d2005e3fSOleg Nesterov 
124086039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
124186039bd3SAndrea Arcangeli 	vma = find_vma_prev(mm, start, &prev);
124286039bd3SAndrea Arcangeli 	if (!vma)
124386039bd3SAndrea Arcangeli 		goto out_unlock;
124486039bd3SAndrea Arcangeli 
124586039bd3SAndrea Arcangeli 	/* check that there's at least one vma in the range */
124686039bd3SAndrea Arcangeli 	ret = -EINVAL;
124786039bd3SAndrea Arcangeli 	if (vma->vm_start >= end)
124886039bd3SAndrea Arcangeli 		goto out_unlock;
124986039bd3SAndrea Arcangeli 
125086039bd3SAndrea Arcangeli 	/*
1251cab350afSMike Kravetz 	 * If the first vma contains huge pages, make sure start address
1252cab350afSMike Kravetz 	 * is aligned to huge page size.
1253cab350afSMike Kravetz 	 */
1254cab350afSMike Kravetz 	if (is_vm_hugetlb_page(vma)) {
1255cab350afSMike Kravetz 		unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1256cab350afSMike Kravetz 
1257cab350afSMike Kravetz 		if (start & (vma_hpagesize - 1))
1258cab350afSMike Kravetz 			goto out_unlock;
1259cab350afSMike Kravetz 	}
1260cab350afSMike Kravetz 
1261cab350afSMike Kravetz 	/*
126286039bd3SAndrea Arcangeli 	 * Search for not compatible vmas.
126386039bd3SAndrea Arcangeli 	 */
126486039bd3SAndrea Arcangeli 	found = false;
1265cac67329SMike Rapoport 	non_anon_pages = false;
126686039bd3SAndrea Arcangeli 	for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
126786039bd3SAndrea Arcangeli 		cond_resched();
126886039bd3SAndrea Arcangeli 
126986039bd3SAndrea Arcangeli 		BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
127086039bd3SAndrea Arcangeli 		       !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
127186039bd3SAndrea Arcangeli 
127286039bd3SAndrea Arcangeli 		/* check not compatible vmas */
127386039bd3SAndrea Arcangeli 		ret = -EINVAL;
1274ba6907dbSMike Rapoport 		if (!vma_can_userfault(cur))
127586039bd3SAndrea Arcangeli 			goto out_unlock;
1276cab350afSMike Kravetz 		/*
1277cab350afSMike Kravetz 		 * If this vma contains ending address, and huge pages
1278cab350afSMike Kravetz 		 * check alignment.
1279cab350afSMike Kravetz 		 */
1280cab350afSMike Kravetz 		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1281cab350afSMike Kravetz 		    end > cur->vm_start) {
1282cab350afSMike Kravetz 			unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1283cab350afSMike Kravetz 
1284cab350afSMike Kravetz 			ret = -EINVAL;
1285cab350afSMike Kravetz 
1286cab350afSMike Kravetz 			if (end & (vma_hpagesize - 1))
1287cab350afSMike Kravetz 				goto out_unlock;
1288cab350afSMike Kravetz 		}
128986039bd3SAndrea Arcangeli 
129086039bd3SAndrea Arcangeli 		/*
129186039bd3SAndrea Arcangeli 		 * Check that this vma isn't already owned by a
129286039bd3SAndrea Arcangeli 		 * different userfaultfd. We can't allow more than one
129386039bd3SAndrea Arcangeli 		 * userfaultfd to own a single vma simultaneously or we
129486039bd3SAndrea Arcangeli 		 * wouldn't know which one to deliver the userfaults to.
129586039bd3SAndrea Arcangeli 		 */
129686039bd3SAndrea Arcangeli 		ret = -EBUSY;
129786039bd3SAndrea Arcangeli 		if (cur->vm_userfaultfd_ctx.ctx &&
129886039bd3SAndrea Arcangeli 		    cur->vm_userfaultfd_ctx.ctx != ctx)
129986039bd3SAndrea Arcangeli 			goto out_unlock;
130086039bd3SAndrea Arcangeli 
1301cab350afSMike Kravetz 		/*
1302cab350afSMike Kravetz 		 * Note vmas containing huge pages
1303cab350afSMike Kravetz 		 */
1304cac67329SMike Rapoport 		if (is_vm_hugetlb_page(cur) || vma_is_shmem(cur))
1305cac67329SMike Rapoport 			non_anon_pages = true;
1306cab350afSMike Kravetz 
130786039bd3SAndrea Arcangeli 		found = true;
130886039bd3SAndrea Arcangeli 	}
130986039bd3SAndrea Arcangeli 	BUG_ON(!found);
131086039bd3SAndrea Arcangeli 
131186039bd3SAndrea Arcangeli 	if (vma->vm_start < start)
131286039bd3SAndrea Arcangeli 		prev = vma;
131386039bd3SAndrea Arcangeli 
131486039bd3SAndrea Arcangeli 	ret = 0;
131586039bd3SAndrea Arcangeli 	do {
131686039bd3SAndrea Arcangeli 		cond_resched();
131786039bd3SAndrea Arcangeli 
1318ba6907dbSMike Rapoport 		BUG_ON(!vma_can_userfault(vma));
131986039bd3SAndrea Arcangeli 		BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
132086039bd3SAndrea Arcangeli 		       vma->vm_userfaultfd_ctx.ctx != ctx);
132186039bd3SAndrea Arcangeli 
132286039bd3SAndrea Arcangeli 		/*
132386039bd3SAndrea Arcangeli 		 * Nothing to do: this vma is already registered into this
132486039bd3SAndrea Arcangeli 		 * userfaultfd and with the right tracking mode too.
132586039bd3SAndrea Arcangeli 		 */
132686039bd3SAndrea Arcangeli 		if (vma->vm_userfaultfd_ctx.ctx == ctx &&
132786039bd3SAndrea Arcangeli 		    (vma->vm_flags & vm_flags) == vm_flags)
132886039bd3SAndrea Arcangeli 			goto skip;
132986039bd3SAndrea Arcangeli 
133086039bd3SAndrea Arcangeli 		if (vma->vm_start > start)
133186039bd3SAndrea Arcangeli 			start = vma->vm_start;
133286039bd3SAndrea Arcangeli 		vma_end = min(end, vma->vm_end);
133386039bd3SAndrea Arcangeli 
133486039bd3SAndrea Arcangeli 		new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
133586039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
133686039bd3SAndrea Arcangeli 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
133786039bd3SAndrea Arcangeli 				 vma_policy(vma),
133886039bd3SAndrea Arcangeli 				 ((struct vm_userfaultfd_ctx){ ctx }));
133986039bd3SAndrea Arcangeli 		if (prev) {
134086039bd3SAndrea Arcangeli 			vma = prev;
134186039bd3SAndrea Arcangeli 			goto next;
134286039bd3SAndrea Arcangeli 		}
134386039bd3SAndrea Arcangeli 		if (vma->vm_start < start) {
134486039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, start, 1);
134586039bd3SAndrea Arcangeli 			if (ret)
134686039bd3SAndrea Arcangeli 				break;
134786039bd3SAndrea Arcangeli 		}
134886039bd3SAndrea Arcangeli 		if (vma->vm_end > end) {
134986039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, end, 0);
135086039bd3SAndrea Arcangeli 			if (ret)
135186039bd3SAndrea Arcangeli 				break;
135286039bd3SAndrea Arcangeli 		}
135386039bd3SAndrea Arcangeli 	next:
135486039bd3SAndrea Arcangeli 		/*
135586039bd3SAndrea Arcangeli 		 * In the vma_merge() successful mprotect-like case 8:
135686039bd3SAndrea Arcangeli 		 * the next vma was merged into the current one and
135786039bd3SAndrea Arcangeli 		 * the current one has not been updated yet.
135886039bd3SAndrea Arcangeli 		 */
135986039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
136086039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx.ctx = ctx;
136186039bd3SAndrea Arcangeli 
136286039bd3SAndrea Arcangeli 	skip:
136386039bd3SAndrea Arcangeli 		prev = vma;
136486039bd3SAndrea Arcangeli 		start = vma->vm_end;
136586039bd3SAndrea Arcangeli 		vma = vma->vm_next;
136686039bd3SAndrea Arcangeli 	} while (vma && vma->vm_start < end);
136786039bd3SAndrea Arcangeli out_unlock:
136886039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
1369d2005e3fSOleg Nesterov 	mmput(mm);
137086039bd3SAndrea Arcangeli 	if (!ret) {
137186039bd3SAndrea Arcangeli 		/*
137286039bd3SAndrea Arcangeli 		 * Now that we scanned all vmas we can already tell
137386039bd3SAndrea Arcangeli 		 * userland which ioctls methods are guaranteed to
137486039bd3SAndrea Arcangeli 		 * succeed on this range.
137586039bd3SAndrea Arcangeli 		 */
1376cac67329SMike Rapoport 		if (put_user(non_anon_pages ? UFFD_API_RANGE_IOCTLS_BASIC :
1377cab350afSMike Kravetz 			     UFFD_API_RANGE_IOCTLS,
137886039bd3SAndrea Arcangeli 			     &user_uffdio_register->ioctls))
137986039bd3SAndrea Arcangeli 			ret = -EFAULT;
138086039bd3SAndrea Arcangeli 	}
138186039bd3SAndrea Arcangeli out:
138286039bd3SAndrea Arcangeli 	return ret;
138386039bd3SAndrea Arcangeli }
138486039bd3SAndrea Arcangeli 
138586039bd3SAndrea Arcangeli static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
138686039bd3SAndrea Arcangeli 				  unsigned long arg)
138786039bd3SAndrea Arcangeli {
138886039bd3SAndrea Arcangeli 	struct mm_struct *mm = ctx->mm;
138986039bd3SAndrea Arcangeli 	struct vm_area_struct *vma, *prev, *cur;
139086039bd3SAndrea Arcangeli 	int ret;
139186039bd3SAndrea Arcangeli 	struct uffdio_range uffdio_unregister;
139286039bd3SAndrea Arcangeli 	unsigned long new_flags;
139386039bd3SAndrea Arcangeli 	bool found;
139486039bd3SAndrea Arcangeli 	unsigned long start, end, vma_end;
139586039bd3SAndrea Arcangeli 	const void __user *buf = (void __user *)arg;
139686039bd3SAndrea Arcangeli 
139786039bd3SAndrea Arcangeli 	ret = -EFAULT;
139886039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
139986039bd3SAndrea Arcangeli 		goto out;
140086039bd3SAndrea Arcangeli 
140186039bd3SAndrea Arcangeli 	ret = validate_range(mm, uffdio_unregister.start,
140286039bd3SAndrea Arcangeli 			     uffdio_unregister.len);
140386039bd3SAndrea Arcangeli 	if (ret)
140486039bd3SAndrea Arcangeli 		goto out;
140586039bd3SAndrea Arcangeli 
140686039bd3SAndrea Arcangeli 	start = uffdio_unregister.start;
140786039bd3SAndrea Arcangeli 	end = start + uffdio_unregister.len;
140886039bd3SAndrea Arcangeli 
1409d2005e3fSOleg Nesterov 	ret = -ENOMEM;
1410d2005e3fSOleg Nesterov 	if (!mmget_not_zero(mm))
1411d2005e3fSOleg Nesterov 		goto out;
1412d2005e3fSOleg Nesterov 
141386039bd3SAndrea Arcangeli 	down_write(&mm->mmap_sem);
141486039bd3SAndrea Arcangeli 	vma = find_vma_prev(mm, start, &prev);
141586039bd3SAndrea Arcangeli 	if (!vma)
141686039bd3SAndrea Arcangeli 		goto out_unlock;
141786039bd3SAndrea Arcangeli 
141886039bd3SAndrea Arcangeli 	/* check that there's at least one vma in the range */
141986039bd3SAndrea Arcangeli 	ret = -EINVAL;
142086039bd3SAndrea Arcangeli 	if (vma->vm_start >= end)
142186039bd3SAndrea Arcangeli 		goto out_unlock;
142286039bd3SAndrea Arcangeli 
142386039bd3SAndrea Arcangeli 	/*
1424cab350afSMike Kravetz 	 * If the first vma contains huge pages, make sure start address
1425cab350afSMike Kravetz 	 * is aligned to huge page size.
1426cab350afSMike Kravetz 	 */
1427cab350afSMike Kravetz 	if (is_vm_hugetlb_page(vma)) {
1428cab350afSMike Kravetz 		unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1429cab350afSMike Kravetz 
1430cab350afSMike Kravetz 		if (start & (vma_hpagesize - 1))
1431cab350afSMike Kravetz 			goto out_unlock;
1432cab350afSMike Kravetz 	}
1433cab350afSMike Kravetz 
1434cab350afSMike Kravetz 	/*
143586039bd3SAndrea Arcangeli 	 * Search for not compatible vmas.
143686039bd3SAndrea Arcangeli 	 */
143786039bd3SAndrea Arcangeli 	found = false;
143886039bd3SAndrea Arcangeli 	ret = -EINVAL;
143986039bd3SAndrea Arcangeli 	for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
144086039bd3SAndrea Arcangeli 		cond_resched();
144186039bd3SAndrea Arcangeli 
144286039bd3SAndrea Arcangeli 		BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
144386039bd3SAndrea Arcangeli 		       !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
144486039bd3SAndrea Arcangeli 
144586039bd3SAndrea Arcangeli 		/*
144686039bd3SAndrea Arcangeli 		 * Check not compatible vmas, not strictly required
144786039bd3SAndrea Arcangeli 		 * here as not compatible vmas cannot have an
144886039bd3SAndrea Arcangeli 		 * userfaultfd_ctx registered on them, but this
144986039bd3SAndrea Arcangeli 		 * provides for more strict behavior to notice
145086039bd3SAndrea Arcangeli 		 * unregistration errors.
145186039bd3SAndrea Arcangeli 		 */
1452ba6907dbSMike Rapoport 		if (!vma_can_userfault(cur))
145386039bd3SAndrea Arcangeli 			goto out_unlock;
145486039bd3SAndrea Arcangeli 
145586039bd3SAndrea Arcangeli 		found = true;
145686039bd3SAndrea Arcangeli 	}
145786039bd3SAndrea Arcangeli 	BUG_ON(!found);
145886039bd3SAndrea Arcangeli 
145986039bd3SAndrea Arcangeli 	if (vma->vm_start < start)
146086039bd3SAndrea Arcangeli 		prev = vma;
146186039bd3SAndrea Arcangeli 
146286039bd3SAndrea Arcangeli 	ret = 0;
146386039bd3SAndrea Arcangeli 	do {
146486039bd3SAndrea Arcangeli 		cond_resched();
146586039bd3SAndrea Arcangeli 
1466ba6907dbSMike Rapoport 		BUG_ON(!vma_can_userfault(vma));
146786039bd3SAndrea Arcangeli 
146886039bd3SAndrea Arcangeli 		/*
146986039bd3SAndrea Arcangeli 		 * Nothing to do: this vma is already registered into this
147086039bd3SAndrea Arcangeli 		 * userfaultfd and with the right tracking mode too.
147186039bd3SAndrea Arcangeli 		 */
147286039bd3SAndrea Arcangeli 		if (!vma->vm_userfaultfd_ctx.ctx)
147386039bd3SAndrea Arcangeli 			goto skip;
147486039bd3SAndrea Arcangeli 
147586039bd3SAndrea Arcangeli 		if (vma->vm_start > start)
147686039bd3SAndrea Arcangeli 			start = vma->vm_start;
147786039bd3SAndrea Arcangeli 		vma_end = min(end, vma->vm_end);
147886039bd3SAndrea Arcangeli 
147909fa5296SAndrea Arcangeli 		if (userfaultfd_missing(vma)) {
148009fa5296SAndrea Arcangeli 			/*
148109fa5296SAndrea Arcangeli 			 * Wake any concurrent pending userfault while
148209fa5296SAndrea Arcangeli 			 * we unregister, so they will not hang
148309fa5296SAndrea Arcangeli 			 * permanently and it avoids userland to call
148409fa5296SAndrea Arcangeli 			 * UFFDIO_WAKE explicitly.
148509fa5296SAndrea Arcangeli 			 */
148609fa5296SAndrea Arcangeli 			struct userfaultfd_wake_range range;
148709fa5296SAndrea Arcangeli 			range.start = start;
148809fa5296SAndrea Arcangeli 			range.len = vma_end - start;
148909fa5296SAndrea Arcangeli 			wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
149009fa5296SAndrea Arcangeli 		}
149109fa5296SAndrea Arcangeli 
149286039bd3SAndrea Arcangeli 		new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
149386039bd3SAndrea Arcangeli 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
149486039bd3SAndrea Arcangeli 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
149586039bd3SAndrea Arcangeli 				 vma_policy(vma),
149686039bd3SAndrea Arcangeli 				 NULL_VM_UFFD_CTX);
149786039bd3SAndrea Arcangeli 		if (prev) {
149886039bd3SAndrea Arcangeli 			vma = prev;
149986039bd3SAndrea Arcangeli 			goto next;
150086039bd3SAndrea Arcangeli 		}
150186039bd3SAndrea Arcangeli 		if (vma->vm_start < start) {
150286039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, start, 1);
150386039bd3SAndrea Arcangeli 			if (ret)
150486039bd3SAndrea Arcangeli 				break;
150586039bd3SAndrea Arcangeli 		}
150686039bd3SAndrea Arcangeli 		if (vma->vm_end > end) {
150786039bd3SAndrea Arcangeli 			ret = split_vma(mm, vma, end, 0);
150886039bd3SAndrea Arcangeli 			if (ret)
150986039bd3SAndrea Arcangeli 				break;
151086039bd3SAndrea Arcangeli 		}
151186039bd3SAndrea Arcangeli 	next:
151286039bd3SAndrea Arcangeli 		/*
151386039bd3SAndrea Arcangeli 		 * In the vma_merge() successful mprotect-like case 8:
151486039bd3SAndrea Arcangeli 		 * the next vma was merged into the current one and
151586039bd3SAndrea Arcangeli 		 * the current one has not been updated yet.
151686039bd3SAndrea Arcangeli 		 */
151786039bd3SAndrea Arcangeli 		vma->vm_flags = new_flags;
151886039bd3SAndrea Arcangeli 		vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
151986039bd3SAndrea Arcangeli 
152086039bd3SAndrea Arcangeli 	skip:
152186039bd3SAndrea Arcangeli 		prev = vma;
152286039bd3SAndrea Arcangeli 		start = vma->vm_end;
152386039bd3SAndrea Arcangeli 		vma = vma->vm_next;
152486039bd3SAndrea Arcangeli 	} while (vma && vma->vm_start < end);
152586039bd3SAndrea Arcangeli out_unlock:
152686039bd3SAndrea Arcangeli 	up_write(&mm->mmap_sem);
1527d2005e3fSOleg Nesterov 	mmput(mm);
152886039bd3SAndrea Arcangeli out:
152986039bd3SAndrea Arcangeli 	return ret;
153086039bd3SAndrea Arcangeli }
153186039bd3SAndrea Arcangeli 
153286039bd3SAndrea Arcangeli /*
1533ba85c702SAndrea Arcangeli  * userfaultfd_wake may be used in combination with the
1534ba85c702SAndrea Arcangeli  * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
153586039bd3SAndrea Arcangeli  */
153686039bd3SAndrea Arcangeli static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
153786039bd3SAndrea Arcangeli 			    unsigned long arg)
153886039bd3SAndrea Arcangeli {
153986039bd3SAndrea Arcangeli 	int ret;
154086039bd3SAndrea Arcangeli 	struct uffdio_range uffdio_wake;
154186039bd3SAndrea Arcangeli 	struct userfaultfd_wake_range range;
154286039bd3SAndrea Arcangeli 	const void __user *buf = (void __user *)arg;
154386039bd3SAndrea Arcangeli 
154486039bd3SAndrea Arcangeli 	ret = -EFAULT;
154586039bd3SAndrea Arcangeli 	if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
154686039bd3SAndrea Arcangeli 		goto out;
154786039bd3SAndrea Arcangeli 
154886039bd3SAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
154986039bd3SAndrea Arcangeli 	if (ret)
155086039bd3SAndrea Arcangeli 		goto out;
155186039bd3SAndrea Arcangeli 
155286039bd3SAndrea Arcangeli 	range.start = uffdio_wake.start;
155386039bd3SAndrea Arcangeli 	range.len = uffdio_wake.len;
155486039bd3SAndrea Arcangeli 
155586039bd3SAndrea Arcangeli 	/*
155686039bd3SAndrea Arcangeli 	 * len == 0 means wake all and we don't want to wake all here,
155786039bd3SAndrea Arcangeli 	 * so check it again to be sure.
155886039bd3SAndrea Arcangeli 	 */
155986039bd3SAndrea Arcangeli 	VM_BUG_ON(!range.len);
156086039bd3SAndrea Arcangeli 
156186039bd3SAndrea Arcangeli 	wake_userfault(ctx, &range);
156286039bd3SAndrea Arcangeli 	ret = 0;
156386039bd3SAndrea Arcangeli 
156486039bd3SAndrea Arcangeli out:
156586039bd3SAndrea Arcangeli 	return ret;
156686039bd3SAndrea Arcangeli }
156786039bd3SAndrea Arcangeli 
1568ad465caeSAndrea Arcangeli static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1569ad465caeSAndrea Arcangeli 			    unsigned long arg)
1570ad465caeSAndrea Arcangeli {
1571ad465caeSAndrea Arcangeli 	__s64 ret;
1572ad465caeSAndrea Arcangeli 	struct uffdio_copy uffdio_copy;
1573ad465caeSAndrea Arcangeli 	struct uffdio_copy __user *user_uffdio_copy;
1574ad465caeSAndrea Arcangeli 	struct userfaultfd_wake_range range;
1575ad465caeSAndrea Arcangeli 
1576ad465caeSAndrea Arcangeli 	user_uffdio_copy = (struct uffdio_copy __user *) arg;
1577ad465caeSAndrea Arcangeli 
1578ad465caeSAndrea Arcangeli 	ret = -EFAULT;
1579ad465caeSAndrea Arcangeli 	if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1580ad465caeSAndrea Arcangeli 			   /* don't copy "copy" last field */
1581ad465caeSAndrea Arcangeli 			   sizeof(uffdio_copy)-sizeof(__s64)))
1582ad465caeSAndrea Arcangeli 		goto out;
1583ad465caeSAndrea Arcangeli 
1584ad465caeSAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1585ad465caeSAndrea Arcangeli 	if (ret)
1586ad465caeSAndrea Arcangeli 		goto out;
1587ad465caeSAndrea Arcangeli 	/*
1588ad465caeSAndrea Arcangeli 	 * double check for wraparound just in case. copy_from_user()
1589ad465caeSAndrea Arcangeli 	 * will later check uffdio_copy.src + uffdio_copy.len to fit
1590ad465caeSAndrea Arcangeli 	 * in the userland range.
1591ad465caeSAndrea Arcangeli 	 */
1592ad465caeSAndrea Arcangeli 	ret = -EINVAL;
1593ad465caeSAndrea Arcangeli 	if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1594ad465caeSAndrea Arcangeli 		goto out;
1595ad465caeSAndrea Arcangeli 	if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1596ad465caeSAndrea Arcangeli 		goto out;
1597d2005e3fSOleg Nesterov 	if (mmget_not_zero(ctx->mm)) {
1598ad465caeSAndrea Arcangeli 		ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1599ad465caeSAndrea Arcangeli 				   uffdio_copy.len);
1600d2005e3fSOleg Nesterov 		mmput(ctx->mm);
160196333187SMike Rapoport 	} else {
160296333187SMike Rapoport 		return -ENOSPC;
1603d2005e3fSOleg Nesterov 	}
1604ad465caeSAndrea Arcangeli 	if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1605ad465caeSAndrea Arcangeli 		return -EFAULT;
1606ad465caeSAndrea Arcangeli 	if (ret < 0)
1607ad465caeSAndrea Arcangeli 		goto out;
1608ad465caeSAndrea Arcangeli 	BUG_ON(!ret);
1609ad465caeSAndrea Arcangeli 	/* len == 0 would wake all */
1610ad465caeSAndrea Arcangeli 	range.len = ret;
1611ad465caeSAndrea Arcangeli 	if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1612ad465caeSAndrea Arcangeli 		range.start = uffdio_copy.dst;
1613ad465caeSAndrea Arcangeli 		wake_userfault(ctx, &range);
1614ad465caeSAndrea Arcangeli 	}
1615ad465caeSAndrea Arcangeli 	ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1616ad465caeSAndrea Arcangeli out:
1617ad465caeSAndrea Arcangeli 	return ret;
1618ad465caeSAndrea Arcangeli }
1619ad465caeSAndrea Arcangeli 
1620ad465caeSAndrea Arcangeli static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1621ad465caeSAndrea Arcangeli 				unsigned long arg)
1622ad465caeSAndrea Arcangeli {
1623ad465caeSAndrea Arcangeli 	__s64 ret;
1624ad465caeSAndrea Arcangeli 	struct uffdio_zeropage uffdio_zeropage;
1625ad465caeSAndrea Arcangeli 	struct uffdio_zeropage __user *user_uffdio_zeropage;
1626ad465caeSAndrea Arcangeli 	struct userfaultfd_wake_range range;
1627ad465caeSAndrea Arcangeli 
1628ad465caeSAndrea Arcangeli 	user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1629ad465caeSAndrea Arcangeli 
1630ad465caeSAndrea Arcangeli 	ret = -EFAULT;
1631ad465caeSAndrea Arcangeli 	if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1632ad465caeSAndrea Arcangeli 			   /* don't copy "zeropage" last field */
1633ad465caeSAndrea Arcangeli 			   sizeof(uffdio_zeropage)-sizeof(__s64)))
1634ad465caeSAndrea Arcangeli 		goto out;
1635ad465caeSAndrea Arcangeli 
1636ad465caeSAndrea Arcangeli 	ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1637ad465caeSAndrea Arcangeli 			     uffdio_zeropage.range.len);
1638ad465caeSAndrea Arcangeli 	if (ret)
1639ad465caeSAndrea Arcangeli 		goto out;
1640ad465caeSAndrea Arcangeli 	ret = -EINVAL;
1641ad465caeSAndrea Arcangeli 	if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1642ad465caeSAndrea Arcangeli 		goto out;
1643ad465caeSAndrea Arcangeli 
1644d2005e3fSOleg Nesterov 	if (mmget_not_zero(ctx->mm)) {
1645ad465caeSAndrea Arcangeli 		ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1646ad465caeSAndrea Arcangeli 				     uffdio_zeropage.range.len);
1647d2005e3fSOleg Nesterov 		mmput(ctx->mm);
1648d2005e3fSOleg Nesterov 	}
1649ad465caeSAndrea Arcangeli 	if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1650ad465caeSAndrea Arcangeli 		return -EFAULT;
1651ad465caeSAndrea Arcangeli 	if (ret < 0)
1652ad465caeSAndrea Arcangeli 		goto out;
1653ad465caeSAndrea Arcangeli 	/* len == 0 would wake all */
1654ad465caeSAndrea Arcangeli 	BUG_ON(!ret);
1655ad465caeSAndrea Arcangeli 	range.len = ret;
1656ad465caeSAndrea Arcangeli 	if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1657ad465caeSAndrea Arcangeli 		range.start = uffdio_zeropage.range.start;
1658ad465caeSAndrea Arcangeli 		wake_userfault(ctx, &range);
1659ad465caeSAndrea Arcangeli 	}
1660ad465caeSAndrea Arcangeli 	ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1661ad465caeSAndrea Arcangeli out:
1662ad465caeSAndrea Arcangeli 	return ret;
1663ad465caeSAndrea Arcangeli }
1664ad465caeSAndrea Arcangeli 
16659cd75c3cSPavel Emelyanov static inline unsigned int uffd_ctx_features(__u64 user_features)
16669cd75c3cSPavel Emelyanov {
16679cd75c3cSPavel Emelyanov 	/*
16689cd75c3cSPavel Emelyanov 	 * For the current set of features the bits just coincide
16699cd75c3cSPavel Emelyanov 	 */
16709cd75c3cSPavel Emelyanov 	return (unsigned int)user_features;
16719cd75c3cSPavel Emelyanov }
16729cd75c3cSPavel Emelyanov 
167386039bd3SAndrea Arcangeli /*
167486039bd3SAndrea Arcangeli  * userland asks for a certain API version and we return which bits
167586039bd3SAndrea Arcangeli  * and ioctl commands are implemented in this kernel for such API
167686039bd3SAndrea Arcangeli  * version or -EINVAL if unknown.
167786039bd3SAndrea Arcangeli  */
167886039bd3SAndrea Arcangeli static int userfaultfd_api(struct userfaultfd_ctx *ctx,
167986039bd3SAndrea Arcangeli 			   unsigned long arg)
168086039bd3SAndrea Arcangeli {
168186039bd3SAndrea Arcangeli 	struct uffdio_api uffdio_api;
168286039bd3SAndrea Arcangeli 	void __user *buf = (void __user *)arg;
168386039bd3SAndrea Arcangeli 	int ret;
168465603144SAndrea Arcangeli 	__u64 features;
168586039bd3SAndrea Arcangeli 
168686039bd3SAndrea Arcangeli 	ret = -EINVAL;
168786039bd3SAndrea Arcangeli 	if (ctx->state != UFFD_STATE_WAIT_API)
168886039bd3SAndrea Arcangeli 		goto out;
168986039bd3SAndrea Arcangeli 	ret = -EFAULT;
1690a9b85f94SAndrea Arcangeli 	if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
169186039bd3SAndrea Arcangeli 		goto out;
169265603144SAndrea Arcangeli 	features = uffdio_api.features;
169365603144SAndrea Arcangeli 	if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
169486039bd3SAndrea Arcangeli 		memset(&uffdio_api, 0, sizeof(uffdio_api));
169586039bd3SAndrea Arcangeli 		if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
169686039bd3SAndrea Arcangeli 			goto out;
169786039bd3SAndrea Arcangeli 		ret = -EINVAL;
169886039bd3SAndrea Arcangeli 		goto out;
169986039bd3SAndrea Arcangeli 	}
170065603144SAndrea Arcangeli 	/* report all available features and ioctls to userland */
170165603144SAndrea Arcangeli 	uffdio_api.features = UFFD_API_FEATURES;
170286039bd3SAndrea Arcangeli 	uffdio_api.ioctls = UFFD_API_IOCTLS;
170386039bd3SAndrea Arcangeli 	ret = -EFAULT;
170486039bd3SAndrea Arcangeli 	if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
170586039bd3SAndrea Arcangeli 		goto out;
170686039bd3SAndrea Arcangeli 	ctx->state = UFFD_STATE_RUNNING;
170765603144SAndrea Arcangeli 	/* only enable the requested features for this uffd context */
170865603144SAndrea Arcangeli 	ctx->features = uffd_ctx_features(features);
170986039bd3SAndrea Arcangeli 	ret = 0;
171086039bd3SAndrea Arcangeli out:
171186039bd3SAndrea Arcangeli 	return ret;
171286039bd3SAndrea Arcangeli }
171386039bd3SAndrea Arcangeli 
171486039bd3SAndrea Arcangeli static long userfaultfd_ioctl(struct file *file, unsigned cmd,
171586039bd3SAndrea Arcangeli 			      unsigned long arg)
171686039bd3SAndrea Arcangeli {
171786039bd3SAndrea Arcangeli 	int ret = -EINVAL;
171886039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = file->private_data;
171986039bd3SAndrea Arcangeli 
1720e6485a47SAndrea Arcangeli 	if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1721e6485a47SAndrea Arcangeli 		return -EINVAL;
1722e6485a47SAndrea Arcangeli 
172386039bd3SAndrea Arcangeli 	switch(cmd) {
172486039bd3SAndrea Arcangeli 	case UFFDIO_API:
172586039bd3SAndrea Arcangeli 		ret = userfaultfd_api(ctx, arg);
172686039bd3SAndrea Arcangeli 		break;
172786039bd3SAndrea Arcangeli 	case UFFDIO_REGISTER:
172886039bd3SAndrea Arcangeli 		ret = userfaultfd_register(ctx, arg);
172986039bd3SAndrea Arcangeli 		break;
173086039bd3SAndrea Arcangeli 	case UFFDIO_UNREGISTER:
173186039bd3SAndrea Arcangeli 		ret = userfaultfd_unregister(ctx, arg);
173286039bd3SAndrea Arcangeli 		break;
173386039bd3SAndrea Arcangeli 	case UFFDIO_WAKE:
173486039bd3SAndrea Arcangeli 		ret = userfaultfd_wake(ctx, arg);
173586039bd3SAndrea Arcangeli 		break;
1736ad465caeSAndrea Arcangeli 	case UFFDIO_COPY:
1737ad465caeSAndrea Arcangeli 		ret = userfaultfd_copy(ctx, arg);
1738ad465caeSAndrea Arcangeli 		break;
1739ad465caeSAndrea Arcangeli 	case UFFDIO_ZEROPAGE:
1740ad465caeSAndrea Arcangeli 		ret = userfaultfd_zeropage(ctx, arg);
1741ad465caeSAndrea Arcangeli 		break;
174286039bd3SAndrea Arcangeli 	}
174386039bd3SAndrea Arcangeli 	return ret;
174486039bd3SAndrea Arcangeli }
174586039bd3SAndrea Arcangeli 
174686039bd3SAndrea Arcangeli #ifdef CONFIG_PROC_FS
174786039bd3SAndrea Arcangeli static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
174886039bd3SAndrea Arcangeli {
174986039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx = f->private_data;
1750ac6424b9SIngo Molnar 	wait_queue_entry_t *wq;
175186039bd3SAndrea Arcangeli 	struct userfaultfd_wait_queue *uwq;
175286039bd3SAndrea Arcangeli 	unsigned long pending = 0, total = 0;
175386039bd3SAndrea Arcangeli 
175415b726efSAndrea Arcangeli 	spin_lock(&ctx->fault_pending_wqh.lock);
1755*2055da97SIngo Molnar 	list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
175686039bd3SAndrea Arcangeli 		uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
175786039bd3SAndrea Arcangeli 		pending++;
175886039bd3SAndrea Arcangeli 		total++;
175986039bd3SAndrea Arcangeli 	}
1760*2055da97SIngo Molnar 	list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
176115b726efSAndrea Arcangeli 		uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
176215b726efSAndrea Arcangeli 		total++;
176315b726efSAndrea Arcangeli 	}
176415b726efSAndrea Arcangeli 	spin_unlock(&ctx->fault_pending_wqh.lock);
176586039bd3SAndrea Arcangeli 
176686039bd3SAndrea Arcangeli 	/*
176786039bd3SAndrea Arcangeli 	 * If more protocols will be added, there will be all shown
176886039bd3SAndrea Arcangeli 	 * separated by a space. Like this:
176986039bd3SAndrea Arcangeli 	 *	protocols: aa:... bb:...
177086039bd3SAndrea Arcangeli 	 */
177186039bd3SAndrea Arcangeli 	seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
1772045098e9SMike Rapoport 		   pending, total, UFFD_API, ctx->features,
177386039bd3SAndrea Arcangeli 		   UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
177486039bd3SAndrea Arcangeli }
177586039bd3SAndrea Arcangeli #endif
177686039bd3SAndrea Arcangeli 
177786039bd3SAndrea Arcangeli static const struct file_operations userfaultfd_fops = {
177886039bd3SAndrea Arcangeli #ifdef CONFIG_PROC_FS
177986039bd3SAndrea Arcangeli 	.show_fdinfo	= userfaultfd_show_fdinfo,
178086039bd3SAndrea Arcangeli #endif
178186039bd3SAndrea Arcangeli 	.release	= userfaultfd_release,
178286039bd3SAndrea Arcangeli 	.poll		= userfaultfd_poll,
178386039bd3SAndrea Arcangeli 	.read		= userfaultfd_read,
178486039bd3SAndrea Arcangeli 	.unlocked_ioctl = userfaultfd_ioctl,
178586039bd3SAndrea Arcangeli 	.compat_ioctl	= userfaultfd_ioctl,
178686039bd3SAndrea Arcangeli 	.llseek		= noop_llseek,
178786039bd3SAndrea Arcangeli };
178886039bd3SAndrea Arcangeli 
17893004ec9cSAndrea Arcangeli static void init_once_userfaultfd_ctx(void *mem)
17903004ec9cSAndrea Arcangeli {
17913004ec9cSAndrea Arcangeli 	struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
17923004ec9cSAndrea Arcangeli 
17933004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fault_pending_wqh);
17943004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fault_wqh);
17959cd75c3cSPavel Emelyanov 	init_waitqueue_head(&ctx->event_wqh);
17963004ec9cSAndrea Arcangeli 	init_waitqueue_head(&ctx->fd_wqh);
17972c5b7e1bSAndrea Arcangeli 	seqcount_init(&ctx->refile_seq);
17983004ec9cSAndrea Arcangeli }
17993004ec9cSAndrea Arcangeli 
180086039bd3SAndrea Arcangeli /**
18019332ef9dSMasahiro Yamada  * userfaultfd_file_create - Creates a userfaultfd file pointer.
180286039bd3SAndrea Arcangeli  * @flags: Flags for the userfaultfd file.
180386039bd3SAndrea Arcangeli  *
18049332ef9dSMasahiro Yamada  * This function creates a userfaultfd file pointer, w/out installing
180586039bd3SAndrea Arcangeli  * it into the fd table. This is useful when the userfaultfd file is
180686039bd3SAndrea Arcangeli  * used during the initialization of data structures that require
180786039bd3SAndrea Arcangeli  * extra setup after the userfaultfd creation. So the userfaultfd
180886039bd3SAndrea Arcangeli  * creation is split into the file pointer creation phase, and the
180986039bd3SAndrea Arcangeli  * file descriptor installation phase.  In this way races with
181086039bd3SAndrea Arcangeli  * userspace closing the newly installed file descriptor can be
18119332ef9dSMasahiro Yamada  * avoided.  Returns a userfaultfd file pointer, or a proper error
181286039bd3SAndrea Arcangeli  * pointer.
181386039bd3SAndrea Arcangeli  */
181486039bd3SAndrea Arcangeli static struct file *userfaultfd_file_create(int flags)
181586039bd3SAndrea Arcangeli {
181686039bd3SAndrea Arcangeli 	struct file *file;
181786039bd3SAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
181886039bd3SAndrea Arcangeli 
181986039bd3SAndrea Arcangeli 	BUG_ON(!current->mm);
182086039bd3SAndrea Arcangeli 
182186039bd3SAndrea Arcangeli 	/* Check the UFFD_* constants for consistency.  */
182286039bd3SAndrea Arcangeli 	BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
182386039bd3SAndrea Arcangeli 	BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
182486039bd3SAndrea Arcangeli 
182586039bd3SAndrea Arcangeli 	file = ERR_PTR(-EINVAL);
182686039bd3SAndrea Arcangeli 	if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
182786039bd3SAndrea Arcangeli 		goto out;
182886039bd3SAndrea Arcangeli 
182986039bd3SAndrea Arcangeli 	file = ERR_PTR(-ENOMEM);
18303004ec9cSAndrea Arcangeli 	ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
183186039bd3SAndrea Arcangeli 	if (!ctx)
183286039bd3SAndrea Arcangeli 		goto out;
183386039bd3SAndrea Arcangeli 
183486039bd3SAndrea Arcangeli 	atomic_set(&ctx->refcount, 1);
183586039bd3SAndrea Arcangeli 	ctx->flags = flags;
18369cd75c3cSPavel Emelyanov 	ctx->features = 0;
183786039bd3SAndrea Arcangeli 	ctx->state = UFFD_STATE_WAIT_API;
183886039bd3SAndrea Arcangeli 	ctx->released = false;
183986039bd3SAndrea Arcangeli 	ctx->mm = current->mm;
184086039bd3SAndrea Arcangeli 	/* prevent the mm struct to be freed */
1841f1f10076SVegard Nossum 	mmgrab(ctx->mm);
184286039bd3SAndrea Arcangeli 
184386039bd3SAndrea Arcangeli 	file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
184486039bd3SAndrea Arcangeli 				  O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1845c03e946fSEric Biggers 	if (IS_ERR(file)) {
1846d2005e3fSOleg Nesterov 		mmdrop(ctx->mm);
18473004ec9cSAndrea Arcangeli 		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
1848c03e946fSEric Biggers 	}
184986039bd3SAndrea Arcangeli out:
185086039bd3SAndrea Arcangeli 	return file;
185186039bd3SAndrea Arcangeli }
185286039bd3SAndrea Arcangeli 
185386039bd3SAndrea Arcangeli SYSCALL_DEFINE1(userfaultfd, int, flags)
185486039bd3SAndrea Arcangeli {
185586039bd3SAndrea Arcangeli 	int fd, error;
185686039bd3SAndrea Arcangeli 	struct file *file;
185786039bd3SAndrea Arcangeli 
185886039bd3SAndrea Arcangeli 	error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
185986039bd3SAndrea Arcangeli 	if (error < 0)
186086039bd3SAndrea Arcangeli 		return error;
186186039bd3SAndrea Arcangeli 	fd = error;
186286039bd3SAndrea Arcangeli 
186386039bd3SAndrea Arcangeli 	file = userfaultfd_file_create(flags);
186486039bd3SAndrea Arcangeli 	if (IS_ERR(file)) {
186586039bd3SAndrea Arcangeli 		error = PTR_ERR(file);
186686039bd3SAndrea Arcangeli 		goto err_put_unused_fd;
186786039bd3SAndrea Arcangeli 	}
186886039bd3SAndrea Arcangeli 	fd_install(fd, file);
186986039bd3SAndrea Arcangeli 
187086039bd3SAndrea Arcangeli 	return fd;
187186039bd3SAndrea Arcangeli 
187286039bd3SAndrea Arcangeli err_put_unused_fd:
187386039bd3SAndrea Arcangeli 	put_unused_fd(fd);
187486039bd3SAndrea Arcangeli 
187586039bd3SAndrea Arcangeli 	return error;
187686039bd3SAndrea Arcangeli }
18773004ec9cSAndrea Arcangeli 
18783004ec9cSAndrea Arcangeli static int __init userfaultfd_init(void)
18793004ec9cSAndrea Arcangeli {
18803004ec9cSAndrea Arcangeli 	userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
18813004ec9cSAndrea Arcangeli 						sizeof(struct userfaultfd_ctx),
18823004ec9cSAndrea Arcangeli 						0,
18833004ec9cSAndrea Arcangeli 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
18843004ec9cSAndrea Arcangeli 						init_once_userfaultfd_ctx);
18853004ec9cSAndrea Arcangeli 	return 0;
18863004ec9cSAndrea Arcangeli }
18873004ec9cSAndrea Arcangeli __initcall(userfaultfd_init);
1888