blob: aef0da5d6f6369033f37e61dd3175f4e7410d65b [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002/*
3 * fs/userfaultfd.c
4 *
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 * Copyright (C) 2008-2009 Red Hat, Inc.
7 * Copyright (C) 2015 Red Hat, Inc.
8 *
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07009 * Some part derived from fs/eventfd.c (anon inode setup) and
10 * mm/ksm.c (mm hashing).
11 */
12
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080013#include <linux/list.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070014#include <linux/hashtable.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010016#include <linux/sched/mm.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070017#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/seq_file.h>
21#include <linux/file.h>
22#include <linux/bug.h>
23#include <linux/anon_inodes.h>
24#include <linux/syscalls.h>
25#include <linux/userfaultfd_k.h>
26#include <linux/mempolicy.h>
27#include <linux/ioctl.h>
28#include <linux/security.h>
Mike Kravetzcab350a2017-02-22 15:43:04 -080029#include <linux/hugetlb.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070030
Peter Xucefdca02019-05-13 17:16:41 -070031int sysctl_unprivileged_userfaultfd __read_mostly = 1;
32
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070033static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
34
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070035/*
36 * Start with fault_pending_wqh and fault_wqh so they're more likely
37 * to be in the same cacheline.
Eric Biggerscbcfa132019-07-04 15:14:39 -070038 *
39 * Locking order:
40 * fd_wqh.lock
41 * fault_pending_wqh.lock
42 * fault_wqh.lock
43 * event_wqh.lock
44 *
45 * To avoid deadlocks, IRQs must be disabled when taking any of the above locks,
46 * since fd_wqh.lock is taken by aio_poll() while it's holding a lock that's
47 * also taken in IRQ context.
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070048 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070049struct userfaultfd_ctx {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -070050 /* waitqueue head for the pending (i.e. not read) userfaults */
51 wait_queue_head_t fault_pending_wqh;
52 /* waitqueue head for the userfaults */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070053 wait_queue_head_t fault_wqh;
54 /* waitqueue head for the pseudo fd to wakeup poll/read */
55 wait_queue_head_t fd_wqh;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080056 /* waitqueue head for events */
57 wait_queue_head_t event_wqh;
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -070058 /* a refile sequence protected by fault_pending_wqh lock */
Ahmed S. Darwish2ca97ac2020-07-20 17:55:28 +020059 seqcount_spinlock_t refile_seq;
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070060 /* pseudo fd refcounting */
Eric Biggersca880422018-12-28 00:34:43 -080061 refcount_t refcount;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070062 /* userfaultfd syscall flags */
63 unsigned int flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080064 /* features requested from the userspace */
65 unsigned int features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070066 /* released */
67 bool released;
Mike Rapoportdf2cc962018-06-07 17:09:25 -070068 /* memory mappings are changing because of non-cooperative event */
69 bool mmap_changing;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070070 /* mm with one ore more vmas attached to this userfaultfd_ctx */
71 struct mm_struct *mm;
72};
73
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080074struct userfaultfd_fork_ctx {
75 struct userfaultfd_ctx *orig;
76 struct userfaultfd_ctx *new;
77 struct list_head list;
78};
79
Mike Rapoport897ab3e2017-02-24 14:58:22 -080080struct userfaultfd_unmap_ctx {
81 struct userfaultfd_ctx *ctx;
82 unsigned long start;
83 unsigned long end;
84 struct list_head list;
85};
86
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070087struct userfaultfd_wait_queue {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070088 struct uffd_msg msg;
Ingo Molnarac6424b2017-06-20 12:06:13 +020089 wait_queue_entry_t wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070090 struct userfaultfd_ctx *ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -080091 bool waken;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070092};
93
94struct userfaultfd_wake_range {
95 unsigned long start;
96 unsigned long len;
97};
98
Nadav Amit6afd1e02021-09-02 14:58:59 -070099/* internal indication that UFFD_API ioctl was successfully executed */
100#define UFFD_FEATURE_INITIALIZED (1u << 31)
101
102static bool userfaultfd_is_initialized(struct userfaultfd_ctx *ctx)
103{
104 return ctx->features & UFFD_FEATURE_INITIALIZED;
105}
106
Ingo Molnarac6424b2017-06-20 12:06:13 +0200107static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700108 int wake_flags, void *key)
109{
110 struct userfaultfd_wake_range *range = key;
111 int ret;
112 struct userfaultfd_wait_queue *uwq;
113 unsigned long start, len;
114
115 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
116 ret = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700117 /* len == 0 means wake all */
118 start = range->start;
119 len = range->len;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700120 if (len && (start > uwq->msg.arg.pagefault.address ||
121 start + len <= uwq->msg.arg.pagefault.address))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700122 goto out;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800123 WRITE_ONCE(uwq->waken, true);
124 /*
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200125 * The Program-Order guarantees provided by the scheduler
126 * ensure uwq->waken is visible before the task is woken.
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800127 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700128 ret = wake_up_state(wq->private, mode);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200129 if (ret) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700130 /*
131 * Wake only once, autoremove behavior.
132 *
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200133 * After the effect of list_del_init is visible to the other
134 * CPUs, the waitqueue may disappear from under us, see the
135 * !list_empty_careful() in handle_userfault().
136 *
137 * try_to_wake_up() has an implicit smp_mb(), and the
138 * wq->private is read before calling the extern function
139 * "wake_up_state" (which in turns calls try_to_wake_up).
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700140 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200141 list_del_init(&wq->entry);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200142 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700143out:
144 return ret;
145}
146
147/**
148 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
149 * context.
150 * @ctx: [in] Pointer to the userfaultfd context.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700151 */
152static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
153{
Eric Biggersca880422018-12-28 00:34:43 -0800154 refcount_inc(&ctx->refcount);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700155}
156
157/**
158 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
159 * context.
160 * @ctx: [in] Pointer to userfaultfd context.
161 *
162 * The userfaultfd context reference must have been previously acquired either
163 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
164 */
165static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
166{
Eric Biggersca880422018-12-28 00:34:43 -0800167 if (refcount_dec_and_test(&ctx->refcount)) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700168 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
169 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
170 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
171 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800172 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
173 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700174 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
175 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700176 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700177 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700178 }
179}
180
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700181static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700182{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700183 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
184 /*
185 * Must use memset to zero out the paddings or kernel data is
186 * leaked to userland.
187 */
188 memset(msg, 0, sizeof(struct uffd_msg));
189}
190
191static inline struct uffd_msg userfault_msg(unsigned long address,
192 unsigned int flags,
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700193 unsigned long reason,
194 unsigned int features)
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700195{
196 struct uffd_msg msg;
197 msg_init(&msg);
198 msg.event = UFFD_EVENT_PAGEFAULT;
199 msg.arg.pagefault.address = address;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700200 if (flags & FAULT_FLAG_WRITE)
201 /*
Andrea Arcangelia4605a62017-02-22 15:42:09 -0800202 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700203 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
204 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
205 * was a read fault, otherwise if set it means it's
206 * a write fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700207 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700208 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700209 if (reason & VM_UFFD_WP)
210 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700211 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
212 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
213 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
214 * a missing fault, otherwise if set it means it's a
215 * write protect fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700216 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700217 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700218 if (features & UFFD_FEATURE_THREAD_ID)
Andrea Arcangelia36985d2017-09-06 16:23:59 -0700219 msg.arg.pagefault.feat.ptid = task_pid_vnr(current);
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700220 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700221}
222
Mike Kravetz369cd212017-02-22 15:43:10 -0800223#ifdef CONFIG_HUGETLB_PAGE
224/*
225 * Same functionality as userfaultfd_must_wait below with modifications for
226 * hugepmd ranges.
227 */
228static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700229 struct vm_area_struct *vma,
Mike Kravetz369cd212017-02-22 15:43:10 -0800230 unsigned long address,
231 unsigned long flags,
232 unsigned long reason)
233{
234 struct mm_struct *mm = ctx->mm;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700235 pte_t *ptep, pte;
Mike Kravetz369cd212017-02-22 15:43:10 -0800236 bool ret = true;
237
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700238 mmap_assert_locked(mm);
Mike Kravetz369cd212017-02-22 15:43:10 -0800239
Janosch Frank1e2c0432018-07-03 17:02:39 -0700240 ptep = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
241
242 if (!ptep)
Mike Kravetz369cd212017-02-22 15:43:10 -0800243 goto out;
244
245 ret = false;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700246 pte = huge_ptep_get(ptep);
Mike Kravetz369cd212017-02-22 15:43:10 -0800247
248 /*
249 * Lockless access: we're in a wait_event so it's ok if it
250 * changes under us.
251 */
Janosch Frank1e2c0432018-07-03 17:02:39 -0700252 if (huge_pte_none(pte))
Mike Kravetz369cd212017-02-22 15:43:10 -0800253 ret = true;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700254 if (!huge_pte_write(pte) && (reason & VM_UFFD_WP))
Mike Kravetz369cd212017-02-22 15:43:10 -0800255 ret = true;
256out:
257 return ret;
258}
259#else
260static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700261 struct vm_area_struct *vma,
Mike Kravetz369cd212017-02-22 15:43:10 -0800262 unsigned long address,
263 unsigned long flags,
264 unsigned long reason)
265{
266 return false; /* should never get here */
267}
268#endif /* CONFIG_HUGETLB_PAGE */
269
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700270/*
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700271 * Verify the pagetables are still not ok after having reigstered into
272 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
273 * userfault that has already been resolved, if userfaultfd_read and
274 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
275 * threads.
276 */
277static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
278 unsigned long address,
279 unsigned long flags,
280 unsigned long reason)
281{
282 struct mm_struct *mm = ctx->mm;
283 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300284 p4d_t *p4d;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700285 pud_t *pud;
286 pmd_t *pmd, _pmd;
287 pte_t *pte;
288 bool ret = true;
289
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700290 mmap_assert_locked(mm);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700291
292 pgd = pgd_offset(mm, address);
293 if (!pgd_present(*pgd))
294 goto out;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300295 p4d = p4d_offset(pgd, address);
296 if (!p4d_present(*p4d))
297 goto out;
298 pud = pud_offset(p4d, address);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700299 if (!pud_present(*pud))
300 goto out;
301 pmd = pmd_offset(pud, address);
302 /*
303 * READ_ONCE must function as a barrier with narrower scope
304 * and it must be equivalent to:
305 * _pmd = *pmd; barrier();
306 *
307 * This is to deal with the instability (as in
308 * pmd_trans_unstable) of the pmd.
309 */
310 _pmd = READ_ONCE(*pmd);
Huang Yinga365ac02018-01-31 16:17:32 -0800311 if (pmd_none(_pmd))
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700312 goto out;
313
314 ret = false;
Huang Yinga365ac02018-01-31 16:17:32 -0800315 if (!pmd_present(_pmd))
316 goto out;
317
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700318 if (pmd_trans_huge(_pmd)) {
319 if (!pmd_write(_pmd) && (reason & VM_UFFD_WP))
320 ret = true;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700321 goto out;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700322 }
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700323
324 /*
325 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
326 * and use the standard pte_offset_map() instead of parsing _pmd.
327 */
328 pte = pte_offset_map(pmd, address);
329 /*
330 * Lockless access: we're in a wait_event so it's ok if it
331 * changes under us.
332 */
333 if (pte_none(*pte))
334 ret = true;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700335 if (!pte_write(*pte) && (reason & VM_UFFD_WP))
336 ret = true;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700337 pte_unmap(pte);
338
339out:
340 return ret;
341}
342
Peter Xu3e69ad02020-04-01 21:09:00 -0700343static inline long userfaultfd_get_blocking_state(unsigned int flags)
344{
345 if (flags & FAULT_FLAG_INTERRUPTIBLE)
346 return TASK_INTERRUPTIBLE;
347
348 if (flags & FAULT_FLAG_KILLABLE)
349 return TASK_KILLABLE;
350
351 return TASK_UNINTERRUPTIBLE;
352}
353
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700354/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700355 * The locking rules involved in returning VM_FAULT_RETRY depending on
356 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
357 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
358 * recommendation in __lock_page_or_retry is not an understatement.
359 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700360 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_lock must be released
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700361 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
362 * not set.
363 *
364 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
365 * set, VM_FAULT_RETRY can still be returned if and only if there are
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700366 * fatal_signal_pending()s, and the mmap_lock must be released before
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700367 * returning it.
368 */
Souptick Joarder2b740302018-08-23 17:01:36 -0700369vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700370{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800371 struct mm_struct *mm = vmf->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700372 struct userfaultfd_ctx *ctx;
373 struct userfaultfd_wait_queue uwq;
Souptick Joarder2b740302018-08-23 17:01:36 -0700374 vm_fault_t ret = VM_FAULT_SIGBUS;
Peter Xu3e69ad02020-04-01 21:09:00 -0700375 bool must_wait;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800376 long blocking_state;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700377
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700378 /*
379 * We don't do userfault handling for the final child pid update.
380 *
381 * We also don't do userfault handling during
382 * coredumping. hugetlbfs has the special
383 * follow_hugetlb_page() to skip missing pages in the
384 * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
385 * the no_page_table() helper in follow_page_mask(), but the
386 * shmem_vm_ops->fault method is invoked even during
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700387 * coredumping without mmap_lock and it ends up here.
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700388 */
389 if (current->flags & (PF_EXITING|PF_DUMPCORE))
390 goto out;
391
392 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700393 * Coredumping runs without mmap_lock so we can only check that
394 * the mmap_lock is held, if PF_DUMPCORE was not set.
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700395 */
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700396 mmap_assert_locked(mm);
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700397
Jan Kara82b0f8c2016-12-14 15:06:58 -0800398 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700399 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700400 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700401
402 BUG_ON(ctx->mm != mm);
403
404 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
405 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
406
Prakash Sangappa2d6d6f52017-09-06 16:23:39 -0700407 if (ctx->features & UFFD_FEATURE_SIGBUS)
408 goto out;
409
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700410 /*
411 * If it's already released don't get it. This avoids to loop
412 * in __get_user_pages if userfaultfd_release waits on the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700413 * caller of handle_userfault to release the mmap_lock.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700414 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700415 if (unlikely(READ_ONCE(ctx->released))) {
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700416 /*
417 * Don't return VM_FAULT_SIGBUS in this case, so a non
418 * cooperative manager can close the uffd after the
419 * last UFFDIO_COPY, without risking to trigger an
420 * involuntary SIGBUS if the process was starting the
421 * userfaultfd while the userfaultfd was still armed
422 * (but after the last UFFDIO_COPY). If the uffd
423 * wasn't already closed when the userfault reached
424 * this point, that would normally be solved by
425 * userfaultfd_must_wait returning 'false'.
426 *
427 * If we were to return VM_FAULT_SIGBUS here, the non
428 * cooperative manager would be instead forced to
429 * always call UFFDIO_UNREGISTER before it can safely
430 * close the uffd.
431 */
432 ret = VM_FAULT_NOPAGE;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700433 goto out;
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700434 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700435
436 /*
437 * Check that we can return VM_FAULT_RETRY.
438 *
439 * NOTE: it should become possible to return VM_FAULT_RETRY
440 * even if FAULT_FLAG_TRIED is set without leading to gup()
441 * -EBUSY failures, if the userfaultfd is to be extended for
442 * VM_UFFD_WP tracking and we intend to arm the userfault
443 * without first stopping userland access to the memory. For
444 * VM_UFFD_MISSING userfaults this is enough for now.
445 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800446 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700447 /*
448 * Validate the invariant that nowait must allow retry
449 * to be sure not to return SIGBUS erroneously on
450 * nowait invocations.
451 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800452 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700453#ifdef CONFIG_DEBUG_VM
454 if (printk_ratelimit()) {
455 printk(KERN_WARNING
Jan Kara82b0f8c2016-12-14 15:06:58 -0800456 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
457 vmf->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700458 dump_stack();
459 }
460#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700461 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700462 }
463
464 /*
465 * Handle nowait, not much to do other than tell it to retry
466 * and wait.
467 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700468 ret = VM_FAULT_RETRY;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800469 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700470 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700471
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700472 /* take the reference before dropping the mmap_lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700473 userfaultfd_ctx_get(ctx);
474
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700475 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
476 uwq.wq.private = current;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700477 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason,
478 ctx->features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700479 uwq.ctx = ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800480 uwq.waken = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700481
Peter Xu3e69ad02020-04-01 21:09:00 -0700482 blocking_state = userfaultfd_get_blocking_state(vmf->flags);
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700483
Eric Biggerscbcfa132019-07-04 15:14:39 -0700484 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700485 /*
486 * After the __add_wait_queue the uwq is visible to userland
487 * through poll/read().
488 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700489 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
490 /*
491 * The smp_mb() after __set_current_state prevents the reads
492 * following the spin_unlock to happen before the list_add in
493 * __add_wait_queue.
494 */
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800495 set_current_state(blocking_state);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700496 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700497
Mike Kravetz369cd212017-02-22 15:43:10 -0800498 if (!is_vm_hugetlb_page(vmf->vma))
499 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
500 reason);
501 else
Punit Agrawal7868a202017-07-06 15:39:42 -0700502 must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma,
503 vmf->address,
Mike Kravetz369cd212017-02-22 15:43:10 -0800504 vmf->flags, reason);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700505 mmap_read_unlock(mm);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700506
Linus Torvaldsf9bf3522020-08-02 10:42:31 -0700507 if (likely(must_wait && !READ_ONCE(ctx->released))) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800508 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700509 schedule();
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700510 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700511
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700512 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700513
514 /*
515 * Here we race with the list_del; list_add in
516 * userfaultfd_ctx_read(), however because we don't ever run
517 * list_del_init() to refile across the two lists, the prev
518 * and next pointers will never point to self. list_add also
519 * would never let any of the two pointers to point to
520 * self. So list_empty_careful won't risk to see both pointers
521 * pointing to self at any time during the list refile. The
522 * only case where list_del_init() is called is the full
523 * removal in the wake function and there we don't re-list_add
524 * and it's fine not to block on the spinlock. The uwq on this
525 * kernel stack can be released after the list_del_init.
526 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200527 if (!list_empty_careful(&uwq.wq.entry)) {
Eric Biggerscbcfa132019-07-04 15:14:39 -0700528 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700529 /*
530 * No need of list_del_init(), the uwq on the stack
531 * will be freed shortly anyway.
532 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200533 list_del(&uwq.wq.entry);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700534 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700535 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700536
537 /*
538 * ctx may go away after this if the userfault pseudo fd is
539 * already released.
540 */
541 userfaultfd_ctx_put(ctx);
542
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700543out:
544 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700545}
546
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800547static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
548 struct userfaultfd_wait_queue *ewq)
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800549{
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800550 struct userfaultfd_ctx *release_new_ctx;
551
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800552 if (WARN_ON_ONCE(current->flags & PF_EXITING))
553 goto out;
554
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800555 ewq->ctx = ctx;
556 init_waitqueue_entry(&ewq->wq, current);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800557 release_new_ctx = NULL;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800558
Eric Biggerscbcfa132019-07-04 15:14:39 -0700559 spin_lock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800560 /*
561 * After the __add_wait_queue the uwq is visible to userland
562 * through poll/read().
563 */
564 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
565 for (;;) {
566 set_current_state(TASK_KILLABLE);
567 if (ewq->msg.event == 0)
568 break;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700569 if (READ_ONCE(ctx->released) ||
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800570 fatal_signal_pending(current)) {
Andrea Arcangeli384632e2017-10-03 16:15:38 -0700571 /*
572 * &ewq->wq may be queued in fork_event, but
573 * __remove_wait_queue ignores the head
574 * parameter. It would be a problem if it
575 * didn't.
576 */
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800577 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800578 if (ewq->msg.event == UFFD_EVENT_FORK) {
579 struct userfaultfd_ctx *new;
580
581 new = (struct userfaultfd_ctx *)
582 (unsigned long)
583 ewq->msg.arg.reserved.reserved1;
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800584 release_new_ctx = new;
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800585 }
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800586 break;
587 }
588
Eric Biggerscbcfa132019-07-04 15:14:39 -0700589 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800590
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800591 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800592 schedule();
593
Eric Biggerscbcfa132019-07-04 15:14:39 -0700594 spin_lock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800595 }
596 __set_current_state(TASK_RUNNING);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700597 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800598
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800599 if (release_new_ctx) {
600 struct vm_area_struct *vma;
601 struct mm_struct *mm = release_new_ctx->mm;
602
603 /* the various vma->vm_userfaultfd_ctx still points to it */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700604 mmap_write_lock(mm);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800605 for (vma = mm->mmap; vma; vma = vma->vm_next)
Mike Rapoport31e810a2018-08-02 15:36:09 -0700606 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800607 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Mike Rapoport31e810a2018-08-02 15:36:09 -0700608 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
609 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700610 mmap_write_unlock(mm);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800611
612 userfaultfd_ctx_put(release_new_ctx);
613 }
614
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800615 /*
616 * ctx may go away after this if the userfault pseudo fd is
617 * already released.
618 */
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800619out:
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700620 WRITE_ONCE(ctx->mmap_changing, false);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800621 userfaultfd_ctx_put(ctx);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800622}
623
624static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
625 struct userfaultfd_wait_queue *ewq)
626{
627 ewq->msg.event = 0;
628 wake_up_locked(&ctx->event_wqh);
629 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
630}
631
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800632int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
633{
634 struct userfaultfd_ctx *ctx = NULL, *octx;
635 struct userfaultfd_fork_ctx *fctx;
636
637 octx = vma->vm_userfaultfd_ctx.ctx;
638 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
639 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
640 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
641 return 0;
642 }
643
644 list_for_each_entry(fctx, fcs, list)
645 if (fctx->orig == octx) {
646 ctx = fctx->new;
647 break;
648 }
649
650 if (!ctx) {
651 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
652 if (!fctx)
653 return -ENOMEM;
654
655 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
656 if (!ctx) {
657 kfree(fctx);
658 return -ENOMEM;
659 }
660
Eric Biggersca880422018-12-28 00:34:43 -0800661 refcount_set(&ctx->refcount, 1);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800662 ctx->flags = octx->flags;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800663 ctx->features = octx->features;
664 ctx->released = false;
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700665 ctx->mmap_changing = false;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800666 ctx->mm = vma->vm_mm;
Mike Rapoport00bb31f2017-11-15 17:36:56 -0800667 mmgrab(ctx->mm);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800668
669 userfaultfd_ctx_get(octx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700670 WRITE_ONCE(octx->mmap_changing, true);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800671 fctx->orig = octx;
672 fctx->new = ctx;
673 list_add_tail(&fctx->list, fcs);
674 }
675
676 vma->vm_userfaultfd_ctx.ctx = ctx;
677 return 0;
678}
679
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800680static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800681{
682 struct userfaultfd_ctx *ctx = fctx->orig;
683 struct userfaultfd_wait_queue ewq;
684
685 msg_init(&ewq.msg);
686
687 ewq.msg.event = UFFD_EVENT_FORK;
688 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
689
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800690 userfaultfd_event_wait_completion(ctx, &ewq);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800691}
692
693void dup_userfaultfd_complete(struct list_head *fcs)
694{
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800695 struct userfaultfd_fork_ctx *fctx, *n;
696
697 list_for_each_entry_safe(fctx, n, fcs, list) {
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800698 dup_fctx(fctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800699 list_del(&fctx->list);
700 kfree(fctx);
701 }
702}
703
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800704void mremap_userfaultfd_prep(struct vm_area_struct *vma,
705 struct vm_userfaultfd_ctx *vm_ctx)
706{
707 struct userfaultfd_ctx *ctx;
708
709 ctx = vma->vm_userfaultfd_ctx.ctx;
Peter Xu3cfd22b2018-12-28 00:38:47 -0800710
711 if (!ctx)
712 return;
713
714 if (ctx->features & UFFD_FEATURE_EVENT_REMAP) {
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800715 vm_ctx->ctx = ctx;
716 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700717 WRITE_ONCE(ctx->mmap_changing, true);
Peter Xu3cfd22b2018-12-28 00:38:47 -0800718 } else {
719 /* Drop uffd context if remap feature not enabled */
720 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
721 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800722 }
723}
724
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800725void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800726 unsigned long from, unsigned long to,
727 unsigned long len)
728{
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800729 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800730 struct userfaultfd_wait_queue ewq;
731
732 if (!ctx)
733 return;
734
735 if (to & ~PAGE_MASK) {
736 userfaultfd_ctx_put(ctx);
737 return;
738 }
739
740 msg_init(&ewq.msg);
741
742 ewq.msg.event = UFFD_EVENT_REMAP;
743 ewq.msg.arg.remap.from = from;
744 ewq.msg.arg.remap.to = to;
745 ewq.msg.arg.remap.len = len;
746
747 userfaultfd_event_wait_completion(ctx, &ewq);
748}
749
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800750bool userfaultfd_remove(struct vm_area_struct *vma,
Mike Rapoportd8119142017-02-24 14:56:02 -0800751 unsigned long start, unsigned long end)
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800752{
753 struct mm_struct *mm = vma->vm_mm;
754 struct userfaultfd_ctx *ctx;
755 struct userfaultfd_wait_queue ewq;
756
757 ctx = vma->vm_userfaultfd_ctx.ctx;
Mike Rapoportd8119142017-02-24 14:56:02 -0800758 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800759 return true;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800760
761 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700762 WRITE_ONCE(ctx->mmap_changing, true);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700763 mmap_read_unlock(mm);
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800764
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800765 msg_init(&ewq.msg);
766
Mike Rapoportd8119142017-02-24 14:56:02 -0800767 ewq.msg.event = UFFD_EVENT_REMOVE;
768 ewq.msg.arg.remove.start = start;
769 ewq.msg.arg.remove.end = end;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800770
771 userfaultfd_event_wait_completion(ctx, &ewq);
772
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800773 return false;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800774}
775
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800776static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
777 unsigned long start, unsigned long end)
778{
779 struct userfaultfd_unmap_ctx *unmap_ctx;
780
781 list_for_each_entry(unmap_ctx, unmaps, list)
782 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
783 unmap_ctx->end == end)
784 return true;
785
786 return false;
787}
788
789int userfaultfd_unmap_prep(struct vm_area_struct *vma,
790 unsigned long start, unsigned long end,
791 struct list_head *unmaps)
792{
793 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
794 struct userfaultfd_unmap_ctx *unmap_ctx;
795 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
796
797 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
798 has_unmap_ctx(ctx, unmaps, start, end))
799 continue;
800
801 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
802 if (!unmap_ctx)
803 return -ENOMEM;
804
805 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700806 WRITE_ONCE(ctx->mmap_changing, true);
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800807 unmap_ctx->ctx = ctx;
808 unmap_ctx->start = start;
809 unmap_ctx->end = end;
810 list_add_tail(&unmap_ctx->list, unmaps);
811 }
812
813 return 0;
814}
815
816void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
817{
818 struct userfaultfd_unmap_ctx *ctx, *n;
819 struct userfaultfd_wait_queue ewq;
820
821 list_for_each_entry_safe(ctx, n, uf, list) {
822 msg_init(&ewq.msg);
823
824 ewq.msg.event = UFFD_EVENT_UNMAP;
825 ewq.msg.arg.remove.start = ctx->start;
826 ewq.msg.arg.remove.end = ctx->end;
827
828 userfaultfd_event_wait_completion(ctx->ctx, &ewq);
829
830 list_del(&ctx->list);
831 kfree(ctx);
832 }
833}
834
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700835static int userfaultfd_release(struct inode *inode, struct file *file)
836{
837 struct userfaultfd_ctx *ctx = file->private_data;
838 struct mm_struct *mm = ctx->mm;
839 struct vm_area_struct *vma, *prev;
840 /* len == 0 means wake all */
841 struct userfaultfd_wake_range range = { .len = 0, };
842 unsigned long new_flags;
843
Mark Rutland6aa7de02017-10-23 14:07:29 -0700844 WRITE_ONCE(ctx->released, true);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700845
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700846 if (!mmget_not_zero(mm))
847 goto wakeup;
848
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700849 /*
850 * Flush page faults out of all CPUs. NOTE: all page faults
851 * must be retried without returning VM_FAULT_SIGBUS if
852 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700853 * changes while handle_userfault released the mmap_lock. So
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700854 * it's critical that released is set to true (above), before
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700855 * taking the mmap_lock for writing.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700856 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700857 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700858 prev = NULL;
859 for (vma = mm->mmap; vma; vma = vma->vm_next) {
860 cond_resched();
861 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
862 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
863 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
864 prev = vma;
865 continue;
866 }
867 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
Jann Horn4d45e752020-10-15 20:13:00 -0700868 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
869 new_flags, vma->anon_vma,
870 vma->vm_file, vma->vm_pgoff,
871 vma_policy(vma),
872 NULL_VM_UFFD_CTX);
873 if (prev)
874 vma = prev;
875 else
876 prev = vma;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700877 vma->vm_flags = new_flags;
878 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
879 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700880 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700881 mmput(mm);
882wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700883 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700884 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700885 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700886 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700887 */
Eric Biggerscbcfa132019-07-04 15:14:39 -0700888 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700889 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -0700890 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700891 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700892
Mike Rapoport5a18b642017-08-02 13:32:24 -0700893 /* Flush pending events that may still wait on event_wqh */
894 wake_up_all(&ctx->event_wqh);
895
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800896 wake_up_poll(&ctx->fd_wqh, EPOLLHUP);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700897 userfaultfd_ctx_put(ctx);
898 return 0;
899}
900
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700901/* fault_pending_wqh.lock must be hold by the caller */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800902static inline struct userfaultfd_wait_queue *find_userfault_in(
903 wait_queue_head_t *wqh)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700904{
Ingo Molnarac6424b2017-06-20 12:06:13 +0200905 wait_queue_entry_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700906 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700907
Lance Roy456a7372018-10-04 23:45:44 -0700908 lockdep_assert_held(&wqh->lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700909
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700910 uwq = NULL;
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800911 if (!waitqueue_active(wqh))
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700912 goto out;
913 /* walk in reverse to provide FIFO behavior to read userfaults */
Ingo Molnar2055da92017-06-20 12:06:46 +0200914 wq = list_last_entry(&wqh->head, typeof(*wq), entry);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700915 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
916out:
917 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700918}
919
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800920static inline struct userfaultfd_wait_queue *find_userfault(
921 struct userfaultfd_ctx *ctx)
922{
923 return find_userfault_in(&ctx->fault_pending_wqh);
924}
925
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800926static inline struct userfaultfd_wait_queue *find_userfault_evt(
927 struct userfaultfd_ctx *ctx)
928{
929 return find_userfault_in(&ctx->event_wqh);
930}
931
Al Viro076ccb72017-07-03 01:02:18 -0400932static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700933{
934 struct userfaultfd_ctx *ctx = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400935 __poll_t ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700936
937 poll_wait(file, &ctx->fd_wqh, wait);
938
Nadav Amit6afd1e02021-09-02 14:58:59 -0700939 if (!userfaultfd_is_initialized(ctx))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800940 return EPOLLERR;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800941
Nadav Amit6afd1e02021-09-02 14:58:59 -0700942 /*
943 * poll() never guarantees that read won't block.
944 * userfaults can be waken before they're read().
945 */
946 if (unlikely(!(file->f_flags & O_NONBLOCK)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800947 return EPOLLERR;
Nadav Amit6afd1e02021-09-02 14:58:59 -0700948 /*
949 * lockless access to see if there are pending faults
950 * __pollwait last action is the add_wait_queue but
951 * the spin_unlock would allow the waitqueue_active to
952 * pass above the actual list_add inside
953 * add_wait_queue critical section. So use a full
954 * memory barrier to serialize the list_add write of
955 * add_wait_queue() with the waitqueue_active read
956 * below.
957 */
958 ret = 0;
959 smp_mb();
960 if (waitqueue_active(&ctx->fault_pending_wqh))
961 ret = EPOLLIN;
962 else if (waitqueue_active(&ctx->event_wqh))
963 ret = EPOLLIN;
964
965 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700966}
967
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800968static const struct file_operations userfaultfd_fops;
969
970static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
971 struct userfaultfd_ctx *new,
972 struct uffd_msg *msg)
973{
974 int fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800975
Eric Biggers284cd242018-01-31 16:19:48 -0800976 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new,
977 O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS));
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800978 if (fd < 0)
979 return fd;
980
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800981 msg->arg.reserved.reserved1 = 0;
982 msg->arg.fork.ufd = fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800983 return 0;
984}
985
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700986static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700987 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700988{
989 ssize_t ret;
990 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700991 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800992 /*
993 * Handling fork event requires sleeping operations, so
994 * we drop the event_wqh lock, then do these ops, then
995 * lock it back and wake up the waiter. While the lock is
996 * dropped the ewq may go away so we keep track of it
997 * carefully.
998 */
999 LIST_HEAD(fork_event);
1000 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001001
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001002 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Christoph Hellwigae62c162018-10-26 15:02:19 -07001003 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001004 __add_wait_queue(&ctx->fd_wqh, &wait);
1005 for (;;) {
1006 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001007 spin_lock(&ctx->fault_pending_wqh.lock);
1008 uwq = find_userfault(ctx);
1009 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001010 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001011 * Use a seqcount to repeat the lockless check
1012 * in wake_userfault() to avoid missing
1013 * wakeups because during the refile both
1014 * waitqueue could become empty if this is the
1015 * only userfault.
1016 */
1017 write_seqcount_begin(&ctx->refile_seq);
1018
1019 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001020 * The fault_pending_wqh.lock prevents the uwq
1021 * to disappear from under us.
1022 *
1023 * Refile this userfault from
1024 * fault_pending_wqh to fault_wqh, it's not
1025 * pending anymore after we read it.
1026 *
1027 * Use list_del() by hand (as
1028 * userfaultfd_wake_function also uses
1029 * list_del_init() by hand) to be sure nobody
1030 * changes __remove_wait_queue() to use
1031 * list_del_init() in turn breaking the
1032 * !list_empty_careful() check in
Ingo Molnar2055da92017-06-20 12:06:46 +02001033 * handle_userfault(). The uwq->wq.head list
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001034 * must never be empty at any time during the
1035 * refile, or the waitqueue could disappear
1036 * from under us. The "wait_queue_head_t"
1037 * parameter of __remove_wait_queue() is unused
1038 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001039 */
Ingo Molnar2055da92017-06-20 12:06:46 +02001040 list_del(&uwq->wq.entry);
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001041 add_wait_queue(&ctx->fault_wqh, &uwq->wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001042
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001043 write_seqcount_end(&ctx->refile_seq);
1044
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001045 /* careful to always initialize msg if ret == 0 */
1046 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001047 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001048 ret = 0;
1049 break;
1050 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001051 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001052
1053 spin_lock(&ctx->event_wqh.lock);
1054 uwq = find_userfault_evt(ctx);
1055 if (uwq) {
1056 *msg = uwq->msg;
1057
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001058 if (uwq->msg.event == UFFD_EVENT_FORK) {
1059 fork_nctx = (struct userfaultfd_ctx *)
1060 (unsigned long)
1061 uwq->msg.arg.reserved.reserved1;
Ingo Molnar2055da92017-06-20 12:06:46 +02001062 list_move(&uwq->wq.entry, &fork_event);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001063 /*
1064 * fork_nctx can be freed as soon as
1065 * we drop the lock, unless we take a
1066 * reference on it.
1067 */
1068 userfaultfd_ctx_get(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001069 spin_unlock(&ctx->event_wqh.lock);
1070 ret = 0;
1071 break;
1072 }
1073
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001074 userfaultfd_event_complete(ctx, uwq);
1075 spin_unlock(&ctx->event_wqh.lock);
1076 ret = 0;
1077 break;
1078 }
1079 spin_unlock(&ctx->event_wqh.lock);
1080
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001081 if (signal_pending(current)) {
1082 ret = -ERESTARTSYS;
1083 break;
1084 }
1085 if (no_wait) {
1086 ret = -EAGAIN;
1087 break;
1088 }
Christoph Hellwigae62c162018-10-26 15:02:19 -07001089 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001090 schedule();
Christoph Hellwigae62c162018-10-26 15:02:19 -07001091 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001092 }
1093 __remove_wait_queue(&ctx->fd_wqh, &wait);
1094 __set_current_state(TASK_RUNNING);
Christoph Hellwigae62c162018-10-26 15:02:19 -07001095 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001096
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001097 if (!ret && msg->event == UFFD_EVENT_FORK) {
1098 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001099 spin_lock_irq(&ctx->event_wqh.lock);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001100 if (!list_empty(&fork_event)) {
1101 /*
1102 * The fork thread didn't abort, so we can
1103 * drop the temporary refcount.
1104 */
1105 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001106
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001107 uwq = list_first_entry(&fork_event,
1108 typeof(*uwq),
1109 wq.entry);
1110 /*
1111 * If fork_event list wasn't empty and in turn
1112 * the event wasn't already released by fork
1113 * (the event is allocated on fork kernel
1114 * stack), put the event back to its place in
1115 * the event_wq. fork_event head will be freed
1116 * as soon as we return so the event cannot
1117 * stay queued there no matter the current
1118 * "ret" value.
1119 */
1120 list_del(&uwq->wq.entry);
1121 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1122
1123 /*
1124 * Leave the event in the waitqueue and report
1125 * error to userland if we failed to resolve
1126 * the userfault fork.
1127 */
1128 if (likely(!ret))
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001129 userfaultfd_event_complete(ctx, uwq);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001130 } else {
1131 /*
1132 * Here the fork thread aborted and the
1133 * refcount from the fork thread on fork_nctx
1134 * has already been released. We still hold
1135 * the reference we took before releasing the
1136 * lock above. If resolve_userfault_fork
1137 * failed we've to drop it because the
1138 * fork_nctx has to be freed in such case. If
1139 * it succeeded we'll hold it because the new
1140 * uffd references it.
1141 */
1142 if (ret)
1143 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001144 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07001145 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001146 }
1147
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001148 return ret;
1149}
1150
1151static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1152 size_t count, loff_t *ppos)
1153{
1154 struct userfaultfd_ctx *ctx = file->private_data;
1155 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001156 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001157 int no_wait = file->f_flags & O_NONBLOCK;
1158
Nadav Amit6afd1e02021-09-02 14:58:59 -07001159 if (!userfaultfd_is_initialized(ctx))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001160 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001161
1162 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001163 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001164 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001165 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001166 if (_ret < 0)
1167 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001168 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001169 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001170 ret += sizeof(msg);
1171 buf += sizeof(msg);
1172 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001173 /*
1174 * Allow to read more than one fault at time but only
1175 * block if waiting for the very first one.
1176 */
1177 no_wait = O_NONBLOCK;
1178 }
1179}
1180
1181static void __wake_userfault(struct userfaultfd_ctx *ctx,
1182 struct userfaultfd_wake_range *range)
1183{
Eric Biggerscbcfa132019-07-04 15:14:39 -07001184 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001185 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001186 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001187 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001188 range);
1189 if (waitqueue_active(&ctx->fault_wqh))
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001190 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001191 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001192}
1193
1194static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1195 struct userfaultfd_wake_range *range)
1196{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001197 unsigned seq;
1198 bool need_wakeup;
1199
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001200 /*
1201 * To be sure waitqueue_active() is not reordered by the CPU
1202 * before the pagetable update, use an explicit SMP memory
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001203 * barrier here. PT lock release or mmap_read_unlock(mm) still
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001204 * have release semantics that can allow the
1205 * waitqueue_active() to be reordered before the pte update.
1206 */
1207 smp_mb();
1208
1209 /*
1210 * Use waitqueue_active because it's very frequent to
1211 * change the address space atomically even if there are no
1212 * userfaults yet. So we take the spinlock only when we're
1213 * sure we've userfaults to wake.
1214 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001215 do {
1216 seq = read_seqcount_begin(&ctx->refile_seq);
1217 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1218 waitqueue_active(&ctx->fault_wqh);
1219 cond_resched();
1220 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1221 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001222 __wake_userfault(ctx, range);
1223}
1224
1225static __always_inline int validate_range(struct mm_struct *mm,
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001226 __u64 start, __u64 len)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001227{
1228 __u64 task_size = mm->task_size;
1229
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001230 if (start & ~PAGE_MASK)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001231 return -EINVAL;
1232 if (len & ~PAGE_MASK)
1233 return -EINVAL;
1234 if (!len)
1235 return -EINVAL;
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001236 if (start < mmap_min_addr)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001237 return -EINVAL;
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001238 if (start >= task_size)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001239 return -EINVAL;
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001240 if (len > task_size - start)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001241 return -EINVAL;
1242 return 0;
1243}
1244
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001245static inline bool vma_can_userfault(struct vm_area_struct *vma,
1246 unsigned long vm_flags)
Mike Rapoportba6907d2017-02-22 15:43:22 -08001247{
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001248 /* FIXME: add WP support to hugetlbfs and shmem */
1249 return vma_is_anonymous(vma) ||
1250 ((is_vm_hugetlb_page(vma) || vma_is_shmem(vma)) &&
1251 !(vm_flags & VM_UFFD_WP));
Mike Rapoportba6907d2017-02-22 15:43:22 -08001252}
1253
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001254static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1255 unsigned long arg)
1256{
1257 struct mm_struct *mm = ctx->mm;
1258 struct vm_area_struct *vma, *prev, *cur;
1259 int ret;
1260 struct uffdio_register uffdio_register;
1261 struct uffdio_register __user *user_uffdio_register;
1262 unsigned long vm_flags, new_flags;
1263 bool found;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001264 bool basic_ioctls;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001265 unsigned long start, end, vma_end;
1266
1267 user_uffdio_register = (struct uffdio_register __user *) arg;
1268
1269 ret = -EFAULT;
1270 if (copy_from_user(&uffdio_register, user_uffdio_register,
1271 sizeof(uffdio_register)-sizeof(__u64)))
1272 goto out;
1273
1274 ret = -EINVAL;
1275 if (!uffdio_register.mode)
1276 goto out;
1277 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1278 UFFDIO_REGISTER_MODE_WP))
1279 goto out;
1280 vm_flags = 0;
1281 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1282 vm_flags |= VM_UFFD_MISSING;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001283 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001284 vm_flags |= VM_UFFD_WP;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001285
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001286 ret = validate_range(mm, uffdio_register.range.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001287 uffdio_register.range.len);
1288 if (ret)
1289 goto out;
1290
1291 start = uffdio_register.range.start;
1292 end = start + uffdio_register.range.len;
1293
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001294 ret = -ENOMEM;
1295 if (!mmget_not_zero(mm))
1296 goto out;
1297
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001298 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001299 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001300 if (!vma)
1301 goto out_unlock;
1302
1303 /* check that there's at least one vma in the range */
1304 ret = -EINVAL;
1305 if (vma->vm_start >= end)
1306 goto out_unlock;
1307
1308 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001309 * If the first vma contains huge pages, make sure start address
1310 * is aligned to huge page size.
1311 */
1312 if (is_vm_hugetlb_page(vma)) {
1313 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1314
1315 if (start & (vma_hpagesize - 1))
1316 goto out_unlock;
1317 }
1318
1319 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001320 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001321 */
1322 found = false;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001323 basic_ioctls = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001324 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1325 cond_resched();
1326
1327 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1328 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1329
1330 /* check not compatible vmas */
1331 ret = -EINVAL;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001332 if (!vma_can_userfault(cur, vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001333 goto out_unlock;
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001334
1335 /*
1336 * UFFDIO_COPY will fill file holes even without
1337 * PROT_WRITE. This check enforces that if this is a
1338 * MAP_SHARED, the process has write permission to the backing
1339 * file. If VM_MAYWRITE is set it also enforces that on a
1340 * MAP_SHARED vma: there is no F_WRITE_SEAL and no further
1341 * F_WRITE_SEAL can be taken until the vma is destroyed.
1342 */
1343 ret = -EPERM;
1344 if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
1345 goto out_unlock;
1346
Mike Kravetzcab350a2017-02-22 15:43:04 -08001347 /*
1348 * If this vma contains ending address, and huge pages
1349 * check alignment.
1350 */
1351 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1352 end > cur->vm_start) {
1353 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1354
1355 ret = -EINVAL;
1356
1357 if (end & (vma_hpagesize - 1))
1358 goto out_unlock;
1359 }
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001360 if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
1361 goto out_unlock;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001362
1363 /*
1364 * Check that this vma isn't already owned by a
1365 * different userfaultfd. We can't allow more than one
1366 * userfaultfd to own a single vma simultaneously or we
1367 * wouldn't know which one to deliver the userfaults to.
1368 */
1369 ret = -EBUSY;
1370 if (cur->vm_userfaultfd_ctx.ctx &&
1371 cur->vm_userfaultfd_ctx.ctx != ctx)
1372 goto out_unlock;
1373
Mike Kravetzcab350a2017-02-22 15:43:04 -08001374 /*
1375 * Note vmas containing huge pages
1376 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001377 if (is_vm_hugetlb_page(cur))
1378 basic_ioctls = true;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001379
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001380 found = true;
1381 }
1382 BUG_ON(!found);
1383
1384 if (vma->vm_start < start)
1385 prev = vma;
1386
1387 ret = 0;
1388 do {
1389 cond_resched();
1390
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001391 BUG_ON(!vma_can_userfault(vma, vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001392 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1393 vma->vm_userfaultfd_ctx.ctx != ctx);
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001394 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001395
1396 /*
1397 * Nothing to do: this vma is already registered into this
1398 * userfaultfd and with the right tracking mode too.
1399 */
1400 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1401 (vma->vm_flags & vm_flags) == vm_flags)
1402 goto skip;
1403
1404 if (vma->vm_start > start)
1405 start = vma->vm_start;
1406 vma_end = min(end, vma->vm_end);
1407
Andrea Arcangeli9d4678e2019-11-30 17:57:58 -08001408 new_flags = (vma->vm_flags &
1409 ~(VM_UFFD_MISSING|VM_UFFD_WP)) | vm_flags;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001410 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1411 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1412 vma_policy(vma),
1413 ((struct vm_userfaultfd_ctx){ ctx }));
1414 if (prev) {
1415 vma = prev;
1416 goto next;
1417 }
1418 if (vma->vm_start < start) {
1419 ret = split_vma(mm, vma, start, 1);
1420 if (ret)
1421 break;
1422 }
1423 if (vma->vm_end > end) {
1424 ret = split_vma(mm, vma, end, 0);
1425 if (ret)
1426 break;
1427 }
1428 next:
1429 /*
1430 * In the vma_merge() successful mprotect-like case 8:
1431 * the next vma was merged into the current one and
1432 * the current one has not been updated yet.
1433 */
1434 vma->vm_flags = new_flags;
1435 vma->vm_userfaultfd_ctx.ctx = ctx;
1436
1437 skip:
1438 prev = vma;
1439 start = vma->vm_end;
1440 vma = vma->vm_next;
1441 } while (vma && vma->vm_start < end);
1442out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001443 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001444 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001445 if (!ret) {
Peter Xu14819302020-04-06 20:06:29 -07001446 __u64 ioctls_out;
1447
1448 ioctls_out = basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
1449 UFFD_API_RANGE_IOCTLS;
1450
1451 /*
1452 * Declare the WP ioctl only if the WP mode is
1453 * specified and all checks passed with the range
1454 */
1455 if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_WP))
1456 ioctls_out &= ~((__u64)1 << _UFFDIO_WRITEPROTECT);
1457
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001458 /*
1459 * Now that we scanned all vmas we can already tell
1460 * userland which ioctls methods are guaranteed to
1461 * succeed on this range.
1462 */
Peter Xu14819302020-04-06 20:06:29 -07001463 if (put_user(ioctls_out, &user_uffdio_register->ioctls))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001464 ret = -EFAULT;
1465 }
1466out:
1467 return ret;
1468}
1469
1470static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1471 unsigned long arg)
1472{
1473 struct mm_struct *mm = ctx->mm;
1474 struct vm_area_struct *vma, *prev, *cur;
1475 int ret;
1476 struct uffdio_range uffdio_unregister;
1477 unsigned long new_flags;
1478 bool found;
1479 unsigned long start, end, vma_end;
1480 const void __user *buf = (void __user *)arg;
1481
1482 ret = -EFAULT;
1483 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1484 goto out;
1485
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001486 ret = validate_range(mm, uffdio_unregister.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001487 uffdio_unregister.len);
1488 if (ret)
1489 goto out;
1490
1491 start = uffdio_unregister.start;
1492 end = start + uffdio_unregister.len;
1493
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001494 ret = -ENOMEM;
1495 if (!mmget_not_zero(mm))
1496 goto out;
1497
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001498 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001499 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001500 if (!vma)
1501 goto out_unlock;
1502
1503 /* check that there's at least one vma in the range */
1504 ret = -EINVAL;
1505 if (vma->vm_start >= end)
1506 goto out_unlock;
1507
1508 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001509 * If the first vma contains huge pages, make sure start address
1510 * is aligned to huge page size.
1511 */
1512 if (is_vm_hugetlb_page(vma)) {
1513 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1514
1515 if (start & (vma_hpagesize - 1))
1516 goto out_unlock;
1517 }
1518
1519 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001520 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001521 */
1522 found = false;
1523 ret = -EINVAL;
1524 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1525 cond_resched();
1526
1527 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1528 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1529
1530 /*
1531 * Check not compatible vmas, not strictly required
1532 * here as not compatible vmas cannot have an
1533 * userfaultfd_ctx registered on them, but this
1534 * provides for more strict behavior to notice
1535 * unregistration errors.
1536 */
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001537 if (!vma_can_userfault(cur, cur->vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001538 goto out_unlock;
1539
1540 found = true;
1541 }
1542 BUG_ON(!found);
1543
1544 if (vma->vm_start < start)
1545 prev = vma;
1546
1547 ret = 0;
1548 do {
1549 cond_resched();
1550
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001551 BUG_ON(!vma_can_userfault(vma, vma->vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001552
1553 /*
1554 * Nothing to do: this vma is already registered into this
1555 * userfaultfd and with the right tracking mode too.
1556 */
1557 if (!vma->vm_userfaultfd_ctx.ctx)
1558 goto skip;
1559
Andrea Arcangeli01e881f2018-12-14 14:17:17 -08001560 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
1561
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001562 if (vma->vm_start > start)
1563 start = vma->vm_start;
1564 vma_end = min(end, vma->vm_end);
1565
Andrea Arcangeli09fa5292017-02-22 15:42:46 -08001566 if (userfaultfd_missing(vma)) {
1567 /*
1568 * Wake any concurrent pending userfault while
1569 * we unregister, so they will not hang
1570 * permanently and it avoids userland to call
1571 * UFFDIO_WAKE explicitly.
1572 */
1573 struct userfaultfd_wake_range range;
1574 range.start = start;
1575 range.len = vma_end - start;
1576 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1577 }
1578
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001579 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1580 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1581 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1582 vma_policy(vma),
1583 NULL_VM_UFFD_CTX);
1584 if (prev) {
1585 vma = prev;
1586 goto next;
1587 }
1588 if (vma->vm_start < start) {
1589 ret = split_vma(mm, vma, start, 1);
1590 if (ret)
1591 break;
1592 }
1593 if (vma->vm_end > end) {
1594 ret = split_vma(mm, vma, end, 0);
1595 if (ret)
1596 break;
1597 }
1598 next:
1599 /*
1600 * In the vma_merge() successful mprotect-like case 8:
1601 * the next vma was merged into the current one and
1602 * the current one has not been updated yet.
1603 */
1604 vma->vm_flags = new_flags;
1605 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1606
1607 skip:
1608 prev = vma;
1609 start = vma->vm_end;
1610 vma = vma->vm_next;
1611 } while (vma && vma->vm_start < end);
1612out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001613 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001614 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001615out:
1616 return ret;
1617}
1618
1619/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001620 * userfaultfd_wake may be used in combination with the
1621 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001622 */
1623static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1624 unsigned long arg)
1625{
1626 int ret;
1627 struct uffdio_range uffdio_wake;
1628 struct userfaultfd_wake_range range;
1629 const void __user *buf = (void __user *)arg;
1630
1631 ret = -EFAULT;
1632 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1633 goto out;
1634
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001635 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001636 if (ret)
1637 goto out;
1638
1639 range.start = uffdio_wake.start;
1640 range.len = uffdio_wake.len;
1641
1642 /*
1643 * len == 0 means wake all and we don't want to wake all here,
1644 * so check it again to be sure.
1645 */
1646 VM_BUG_ON(!range.len);
1647
1648 wake_userfault(ctx, &range);
1649 ret = 0;
1650
1651out:
1652 return ret;
1653}
1654
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001655static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1656 unsigned long arg)
1657{
1658 __s64 ret;
1659 struct uffdio_copy uffdio_copy;
1660 struct uffdio_copy __user *user_uffdio_copy;
1661 struct userfaultfd_wake_range range;
1662
1663 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1664
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001665 ret = -EAGAIN;
1666 if (READ_ONCE(ctx->mmap_changing))
1667 goto out;
1668
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001669 ret = -EFAULT;
1670 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1671 /* don't copy "copy" last field */
1672 sizeof(uffdio_copy)-sizeof(__s64)))
1673 goto out;
1674
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001675 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001676 if (ret)
1677 goto out;
1678 /*
1679 * double check for wraparound just in case. copy_from_user()
1680 * will later check uffdio_copy.src + uffdio_copy.len to fit
1681 * in the userland range.
1682 */
1683 ret = -EINVAL;
1684 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1685 goto out;
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001686 if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP))
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001687 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001688 if (mmget_not_zero(ctx->mm)) {
1689 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001690 uffdio_copy.len, &ctx->mmap_changing,
1691 uffdio_copy.mode);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001692 mmput(ctx->mm);
Mike Rapoport96333182017-02-24 14:58:31 -08001693 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001694 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001695 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001696 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1697 return -EFAULT;
1698 if (ret < 0)
1699 goto out;
1700 BUG_ON(!ret);
1701 /* len == 0 would wake all */
1702 range.len = ret;
1703 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1704 range.start = uffdio_copy.dst;
1705 wake_userfault(ctx, &range);
1706 }
1707 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1708out:
1709 return ret;
1710}
1711
1712static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1713 unsigned long arg)
1714{
1715 __s64 ret;
1716 struct uffdio_zeropage uffdio_zeropage;
1717 struct uffdio_zeropage __user *user_uffdio_zeropage;
1718 struct userfaultfd_wake_range range;
1719
1720 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1721
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001722 ret = -EAGAIN;
1723 if (READ_ONCE(ctx->mmap_changing))
1724 goto out;
1725
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001726 ret = -EFAULT;
1727 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1728 /* don't copy "zeropage" last field */
1729 sizeof(uffdio_zeropage)-sizeof(__s64)))
1730 goto out;
1731
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001732 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001733 uffdio_zeropage.range.len);
1734 if (ret)
1735 goto out;
1736 ret = -EINVAL;
1737 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1738 goto out;
1739
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001740 if (mmget_not_zero(ctx->mm)) {
1741 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001742 uffdio_zeropage.range.len,
1743 &ctx->mmap_changing);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001744 mmput(ctx->mm);
Mike Rapoport9d95aa42017-08-02 13:32:15 -07001745 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001746 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001747 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001748 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1749 return -EFAULT;
1750 if (ret < 0)
1751 goto out;
1752 /* len == 0 would wake all */
1753 BUG_ON(!ret);
1754 range.len = ret;
1755 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1756 range.start = uffdio_zeropage.range.start;
1757 wake_userfault(ctx, &range);
1758 }
1759 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1760out:
1761 return ret;
1762}
1763
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001764static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,
1765 unsigned long arg)
1766{
1767 int ret;
1768 struct uffdio_writeprotect uffdio_wp;
1769 struct uffdio_writeprotect __user *user_uffdio_wp;
1770 struct userfaultfd_wake_range range;
Peter Xu23080e22020-04-06 20:06:20 -07001771 bool mode_wp, mode_dontwake;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001772
1773 if (READ_ONCE(ctx->mmap_changing))
1774 return -EAGAIN;
1775
1776 user_uffdio_wp = (struct uffdio_writeprotect __user *) arg;
1777
1778 if (copy_from_user(&uffdio_wp, user_uffdio_wp,
1779 sizeof(struct uffdio_writeprotect)))
1780 return -EFAULT;
1781
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001782 ret = validate_range(ctx->mm, uffdio_wp.range.start,
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001783 uffdio_wp.range.len);
1784 if (ret)
1785 return ret;
1786
1787 if (uffdio_wp.mode & ~(UFFDIO_WRITEPROTECT_MODE_DONTWAKE |
1788 UFFDIO_WRITEPROTECT_MODE_WP))
1789 return -EINVAL;
Peter Xu23080e22020-04-06 20:06:20 -07001790
1791 mode_wp = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_WP;
1792 mode_dontwake = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_DONTWAKE;
1793
1794 if (mode_wp && mode_dontwake)
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001795 return -EINVAL;
1796
Nadav Amit3cda4bf2021-10-18 15:15:25 -07001797 if (mmget_not_zero(ctx->mm)) {
1798 ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
1799 uffdio_wp.range.len, mode_wp,
1800 &ctx->mmap_changing);
1801 mmput(ctx->mm);
1802 } else {
1803 return -ESRCH;
1804 }
1805
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001806 if (ret)
1807 return ret;
1808
Peter Xu23080e22020-04-06 20:06:20 -07001809 if (!mode_wp && !mode_dontwake) {
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001810 range.start = uffdio_wp.range.start;
1811 range.len = uffdio_wp.range.len;
1812 wake_userfault(ctx, &range);
1813 }
1814 return ret;
1815}
1816
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001817static inline unsigned int uffd_ctx_features(__u64 user_features)
1818{
1819 /*
Nadav Amit6afd1e02021-09-02 14:58:59 -07001820 * For the current set of features the bits just coincide. Set
1821 * UFFD_FEATURE_INITIALIZED to mark the features as enabled.
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001822 */
Nadav Amit6afd1e02021-09-02 14:58:59 -07001823 return (unsigned int)user_features | UFFD_FEATURE_INITIALIZED;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001824}
1825
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001826/*
1827 * userland asks for a certain API version and we return which bits
1828 * and ioctl commands are implemented in this kernel for such API
1829 * version or -EINVAL if unknown.
1830 */
1831static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1832 unsigned long arg)
1833{
1834 struct uffdio_api uffdio_api;
1835 void __user *buf = (void __user *)arg;
Nadav Amit6afd1e02021-09-02 14:58:59 -07001836 unsigned int ctx_features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001837 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001838 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001839
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001840 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001841 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001842 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001843 features = uffdio_api.features;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001844 ret = -EINVAL;
1845 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES))
1846 goto err_out;
1847 ret = -EPERM;
1848 if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE))
1849 goto err_out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001850 /* report all available features and ioctls to userland */
1851 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001852 uffdio_api.ioctls = UFFD_API_IOCTLS;
1853 ret = -EFAULT;
1854 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1855 goto out;
Nadav Amit6afd1e02021-09-02 14:58:59 -07001856
Andrea Arcangeli65603142017-02-22 15:42:24 -08001857 /* only enable the requested features for this uffd context */
Nadav Amit6afd1e02021-09-02 14:58:59 -07001858 ctx_features = uffd_ctx_features(features);
1859 ret = -EINVAL;
1860 if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
1861 goto err_out;
1862
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001863 ret = 0;
1864out:
1865 return ret;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001866err_out:
1867 memset(&uffdio_api, 0, sizeof(uffdio_api));
1868 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1869 ret = -EFAULT;
1870 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001871}
1872
1873static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1874 unsigned long arg)
1875{
1876 int ret = -EINVAL;
1877 struct userfaultfd_ctx *ctx = file->private_data;
1878
Nadav Amit6afd1e02021-09-02 14:58:59 -07001879 if (cmd != UFFDIO_API && !userfaultfd_is_initialized(ctx))
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001880 return -EINVAL;
1881
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001882 switch(cmd) {
1883 case UFFDIO_API:
1884 ret = userfaultfd_api(ctx, arg);
1885 break;
1886 case UFFDIO_REGISTER:
1887 ret = userfaultfd_register(ctx, arg);
1888 break;
1889 case UFFDIO_UNREGISTER:
1890 ret = userfaultfd_unregister(ctx, arg);
1891 break;
1892 case UFFDIO_WAKE:
1893 ret = userfaultfd_wake(ctx, arg);
1894 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001895 case UFFDIO_COPY:
1896 ret = userfaultfd_copy(ctx, arg);
1897 break;
1898 case UFFDIO_ZEROPAGE:
1899 ret = userfaultfd_zeropage(ctx, arg);
1900 break;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001901 case UFFDIO_WRITEPROTECT:
1902 ret = userfaultfd_writeprotect(ctx, arg);
1903 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001904 }
1905 return ret;
1906}
1907
1908#ifdef CONFIG_PROC_FS
1909static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1910{
1911 struct userfaultfd_ctx *ctx = f->private_data;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001912 wait_queue_entry_t *wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001913 unsigned long pending = 0, total = 0;
1914
Eric Biggerscbcfa132019-07-04 15:14:39 -07001915 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Ingo Molnar2055da92017-06-20 12:06:46 +02001916 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001917 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001918 total++;
1919 }
Ingo Molnar2055da92017-06-20 12:06:46 +02001920 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001921 total++;
1922 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07001923 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001924
1925 /*
1926 * If more protocols will be added, there will be all shown
1927 * separated by a space. Like this:
1928 * protocols: aa:... bb:...
1929 */
1930 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Mike Rapoport045098e2017-04-07 16:04:42 -07001931 pending, total, UFFD_API, ctx->features,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001932 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1933}
1934#endif
1935
1936static const struct file_operations userfaultfd_fops = {
1937#ifdef CONFIG_PROC_FS
1938 .show_fdinfo = userfaultfd_show_fdinfo,
1939#endif
1940 .release = userfaultfd_release,
1941 .poll = userfaultfd_poll,
1942 .read = userfaultfd_read,
1943 .unlocked_ioctl = userfaultfd_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +02001944 .compat_ioctl = compat_ptr_ioctl,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001945 .llseek = noop_llseek,
1946};
1947
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001948static void init_once_userfaultfd_ctx(void *mem)
1949{
1950 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1951
1952 init_waitqueue_head(&ctx->fault_pending_wqh);
1953 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001954 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001955 init_waitqueue_head(&ctx->fd_wqh);
Ahmed S. Darwish2ca97ac2020-07-20 17:55:28 +02001956 seqcount_spinlock_init(&ctx->refile_seq, &ctx->fault_pending_wqh.lock);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001957}
1958
Eric Biggers284cd242018-01-31 16:19:48 -08001959SYSCALL_DEFINE1(userfaultfd, int, flags)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001960{
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001961 struct userfaultfd_ctx *ctx;
Eric Biggers284cd242018-01-31 16:19:48 -08001962 int fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001963
Peter Xucefdca02019-05-13 17:16:41 -07001964 if (!sysctl_unprivileged_userfaultfd && !capable(CAP_SYS_PTRACE))
1965 return -EPERM;
1966
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001967 BUG_ON(!current->mm);
1968
1969 /* Check the UFFD_* constants for consistency. */
1970 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1971 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1972
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001973 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
Eric Biggers284cd242018-01-31 16:19:48 -08001974 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001975
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001976 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001977 if (!ctx)
Eric Biggers284cd242018-01-31 16:19:48 -08001978 return -ENOMEM;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001979
Eric Biggersca880422018-12-28 00:34:43 -08001980 refcount_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001981 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001982 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001983 ctx->released = false;
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001984 ctx->mmap_changing = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001985 ctx->mm = current->mm;
1986 /* prevent the mm struct to be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001987 mmgrab(ctx->mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001988
Eric Biggers284cd242018-01-31 16:19:48 -08001989 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
1990 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1991 if (fd < 0) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001992 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001993 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001994 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001995 return fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001996}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001997
1998static int __init userfaultfd_init(void)
1999{
2000 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
2001 sizeof(struct userfaultfd_ctx),
2002 0,
2003 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2004 init_once_userfaultfd_ctx);
2005 return 0;
2006}
2007__initcall(userfaultfd_init);