blob: 3d903681af82379525c7a66daee88b759ec823ff [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 Arcangeli86039bd2015-09-04 15:46:31 -070035enum userfaultfd_state {
36 UFFD_STATE_WAIT_API,
37 UFFD_STATE_RUNNING,
38};
39
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070040/*
41 * Start with fault_pending_wqh and fault_wqh so they're more likely
42 * to be in the same cacheline.
Eric Biggerscbcfa132019-07-04 15:14:39 -070043 *
44 * Locking order:
45 * fd_wqh.lock
46 * fault_pending_wqh.lock
47 * fault_wqh.lock
48 * event_wqh.lock
49 *
50 * To avoid deadlocks, IRQs must be disabled when taking any of the above locks,
51 * since fd_wqh.lock is taken by aio_poll() while it's holding a lock that's
52 * also taken in IRQ context.
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070053 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070054struct userfaultfd_ctx {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -070055 /* waitqueue head for the pending (i.e. not read) userfaults */
56 wait_queue_head_t fault_pending_wqh;
57 /* waitqueue head for the userfaults */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070058 wait_queue_head_t fault_wqh;
59 /* waitqueue head for the pseudo fd to wakeup poll/read */
60 wait_queue_head_t fd_wqh;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080061 /* waitqueue head for events */
62 wait_queue_head_t event_wqh;
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -070063 /* a refile sequence protected by fault_pending_wqh lock */
64 struct seqcount refile_seq;
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070065 /* pseudo fd refcounting */
Eric Biggersca880422018-12-28 00:34:43 -080066 refcount_t refcount;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070067 /* userfaultfd syscall flags */
68 unsigned int flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080069 /* features requested from the userspace */
70 unsigned int features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070071 /* state machine */
72 enum userfaultfd_state state;
73 /* released */
74 bool released;
Mike Rapoportdf2cc962018-06-07 17:09:25 -070075 /* memory mappings are changing because of non-cooperative event */
76 bool mmap_changing;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070077 /* mm with one ore more vmas attached to this userfaultfd_ctx */
78 struct mm_struct *mm;
79};
80
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080081struct userfaultfd_fork_ctx {
82 struct userfaultfd_ctx *orig;
83 struct userfaultfd_ctx *new;
84 struct list_head list;
85};
86
Mike Rapoport897ab3e2017-02-24 14:58:22 -080087struct userfaultfd_unmap_ctx {
88 struct userfaultfd_ctx *ctx;
89 unsigned long start;
90 unsigned long end;
91 struct list_head list;
92};
93
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070094struct userfaultfd_wait_queue {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070095 struct uffd_msg msg;
Ingo Molnarac6424b2017-06-20 12:06:13 +020096 wait_queue_entry_t wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070097 struct userfaultfd_ctx *ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -080098 bool waken;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070099};
100
101struct userfaultfd_wake_range {
102 unsigned long start;
103 unsigned long len;
104};
105
Ingo Molnarac6424b2017-06-20 12:06:13 +0200106static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700107 int wake_flags, void *key)
108{
109 struct userfaultfd_wake_range *range = key;
110 int ret;
111 struct userfaultfd_wait_queue *uwq;
112 unsigned long start, len;
113
114 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
115 ret = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700116 /* len == 0 means wake all */
117 start = range->start;
118 len = range->len;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700119 if (len && (start > uwq->msg.arg.pagefault.address ||
120 start + len <= uwq->msg.arg.pagefault.address))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700121 goto out;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800122 WRITE_ONCE(uwq->waken, true);
123 /*
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200124 * The Program-Order guarantees provided by the scheduler
125 * ensure uwq->waken is visible before the task is woken.
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800126 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700127 ret = wake_up_state(wq->private, mode);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200128 if (ret) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700129 /*
130 * Wake only once, autoremove behavior.
131 *
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200132 * After the effect of list_del_init is visible to the other
133 * CPUs, the waitqueue may disappear from under us, see the
134 * !list_empty_careful() in handle_userfault().
135 *
136 * try_to_wake_up() has an implicit smp_mb(), and the
137 * wq->private is read before calling the extern function
138 * "wake_up_state" (which in turns calls try_to_wake_up).
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700139 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200140 list_del_init(&wq->entry);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200141 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700142out:
143 return ret;
144}
145
146/**
147 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
148 * context.
149 * @ctx: [in] Pointer to the userfaultfd context.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700150 */
151static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
152{
Eric Biggersca880422018-12-28 00:34:43 -0800153 refcount_inc(&ctx->refcount);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700154}
155
156/**
157 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
158 * context.
159 * @ctx: [in] Pointer to userfaultfd context.
160 *
161 * The userfaultfd context reference must have been previously acquired either
162 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
163 */
164static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
165{
Eric Biggersca880422018-12-28 00:34:43 -0800166 if (refcount_dec_and_test(&ctx->refcount)) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700167 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
168 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
169 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
170 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800171 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
172 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700173 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
174 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700175 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700176 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700177 }
178}
179
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700180static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700181{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700182 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
183 /*
184 * Must use memset to zero out the paddings or kernel data is
185 * leaked to userland.
186 */
187 memset(msg, 0, sizeof(struct uffd_msg));
188}
189
190static inline struct uffd_msg userfault_msg(unsigned long address,
191 unsigned int flags,
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700192 unsigned long reason,
193 unsigned int features)
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700194{
195 struct uffd_msg msg;
196 msg_init(&msg);
197 msg.event = UFFD_EVENT_PAGEFAULT;
198 msg.arg.pagefault.address = address;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700199 if (flags & FAULT_FLAG_WRITE)
200 /*
Andrea Arcangelia4605a62017-02-22 15:42:09 -0800201 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700202 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
203 * was not set in a UFFD_EVENT_PAGEFAULT, it means it
204 * was a read fault, otherwise if set it means it's
205 * a write fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700206 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700207 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700208 if (reason & VM_UFFD_WP)
209 /*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700210 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
211 * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
212 * not set in a UFFD_EVENT_PAGEFAULT, it means it was
213 * a missing fault, otherwise if set it means it's a
214 * write protect fault.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700215 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700216 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700217 if (features & UFFD_FEATURE_THREAD_ID)
Andrea Arcangelia36985d2017-09-06 16:23:59 -0700218 msg.arg.pagefault.feat.ptid = task_pid_vnr(current);
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700219 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700220}
221
Mike Kravetz369cd212017-02-22 15:43:10 -0800222#ifdef CONFIG_HUGETLB_PAGE
223/*
224 * Same functionality as userfaultfd_must_wait below with modifications for
225 * hugepmd ranges.
226 */
227static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700228 struct vm_area_struct *vma,
Mike Kravetz369cd212017-02-22 15:43:10 -0800229 unsigned long address,
230 unsigned long flags,
231 unsigned long reason)
232{
233 struct mm_struct *mm = ctx->mm;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700234 pte_t *ptep, pte;
Mike Kravetz369cd212017-02-22 15:43:10 -0800235 bool ret = true;
236
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700237 mmap_assert_locked(mm);
Mike Kravetz369cd212017-02-22 15:43:10 -0800238
Janosch Frank1e2c0432018-07-03 17:02:39 -0700239 ptep = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
240
241 if (!ptep)
Mike Kravetz369cd212017-02-22 15:43:10 -0800242 goto out;
243
244 ret = false;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700245 pte = huge_ptep_get(ptep);
Mike Kravetz369cd212017-02-22 15:43:10 -0800246
247 /*
248 * Lockless access: we're in a wait_event so it's ok if it
249 * changes under us.
250 */
Janosch Frank1e2c0432018-07-03 17:02:39 -0700251 if (huge_pte_none(pte))
Mike Kravetz369cd212017-02-22 15:43:10 -0800252 ret = true;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700253 if (!huge_pte_write(pte) && (reason & VM_UFFD_WP))
Mike Kravetz369cd212017-02-22 15:43:10 -0800254 ret = true;
255out:
256 return ret;
257}
258#else
259static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700260 struct vm_area_struct *vma,
Mike Kravetz369cd212017-02-22 15:43:10 -0800261 unsigned long address,
262 unsigned long flags,
263 unsigned long reason)
264{
265 return false; /* should never get here */
266}
267#endif /* CONFIG_HUGETLB_PAGE */
268
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700269/*
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700270 * Verify the pagetables are still not ok after having reigstered into
271 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
272 * userfault that has already been resolved, if userfaultfd_read and
273 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
274 * threads.
275 */
276static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
277 unsigned long address,
278 unsigned long flags,
279 unsigned long reason)
280{
281 struct mm_struct *mm = ctx->mm;
282 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300283 p4d_t *p4d;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700284 pud_t *pud;
285 pmd_t *pmd, _pmd;
286 pte_t *pte;
287 bool ret = true;
288
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700289 mmap_assert_locked(mm);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700290
291 pgd = pgd_offset(mm, address);
292 if (!pgd_present(*pgd))
293 goto out;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300294 p4d = p4d_offset(pgd, address);
295 if (!p4d_present(*p4d))
296 goto out;
297 pud = pud_offset(p4d, address);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700298 if (!pud_present(*pud))
299 goto out;
300 pmd = pmd_offset(pud, address);
301 /*
302 * READ_ONCE must function as a barrier with narrower scope
303 * and it must be equivalent to:
304 * _pmd = *pmd; barrier();
305 *
306 * This is to deal with the instability (as in
307 * pmd_trans_unstable) of the pmd.
308 */
309 _pmd = READ_ONCE(*pmd);
Huang Yinga365ac02018-01-31 16:17:32 -0800310 if (pmd_none(_pmd))
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700311 goto out;
312
313 ret = false;
Huang Yinga365ac02018-01-31 16:17:32 -0800314 if (!pmd_present(_pmd))
315 goto out;
316
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700317 if (pmd_trans_huge(_pmd)) {
318 if (!pmd_write(_pmd) && (reason & VM_UFFD_WP))
319 ret = true;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700320 goto out;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700321 }
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700322
323 /*
324 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
325 * and use the standard pte_offset_map() instead of parsing _pmd.
326 */
327 pte = pte_offset_map(pmd, address);
328 /*
329 * Lockless access: we're in a wait_event so it's ok if it
330 * changes under us.
331 */
332 if (pte_none(*pte))
333 ret = true;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700334 if (!pte_write(*pte) && (reason & VM_UFFD_WP))
335 ret = true;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700336 pte_unmap(pte);
337
338out:
339 return ret;
340}
341
Peter Xu3e69ad02020-04-01 21:09:00 -0700342static inline long userfaultfd_get_blocking_state(unsigned int flags)
343{
344 if (flags & FAULT_FLAG_INTERRUPTIBLE)
345 return TASK_INTERRUPTIBLE;
346
347 if (flags & FAULT_FLAG_KILLABLE)
348 return TASK_KILLABLE;
349
350 return TASK_UNINTERRUPTIBLE;
351}
352
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700353/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700354 * The locking rules involved in returning VM_FAULT_RETRY depending on
355 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
356 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
357 * recommendation in __lock_page_or_retry is not an understatement.
358 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700359 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_lock must be released
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700360 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
361 * not set.
362 *
363 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
364 * set, VM_FAULT_RETRY can still be returned if and only if there are
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700365 * fatal_signal_pending()s, and the mmap_lock must be released before
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700366 * returning it.
367 */
Souptick Joarder2b740302018-08-23 17:01:36 -0700368vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700369{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800370 struct mm_struct *mm = vmf->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700371 struct userfaultfd_ctx *ctx;
372 struct userfaultfd_wait_queue uwq;
Souptick Joarder2b740302018-08-23 17:01:36 -0700373 vm_fault_t ret = VM_FAULT_SIGBUS;
Peter Xu3e69ad02020-04-01 21:09:00 -0700374 bool must_wait;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800375 long blocking_state;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700376
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700377 /*
378 * We don't do userfault handling for the final child pid update.
379 *
380 * We also don't do userfault handling during
381 * coredumping. hugetlbfs has the special
382 * follow_hugetlb_page() to skip missing pages in the
383 * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
384 * the no_page_table() helper in follow_page_mask(), but the
385 * shmem_vm_ops->fault method is invoked even during
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700386 * coredumping without mmap_lock and it ends up here.
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700387 */
388 if (current->flags & (PF_EXITING|PF_DUMPCORE))
389 goto out;
390
391 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700392 * Coredumping runs without mmap_lock so we can only check that
393 * the mmap_lock is held, if PF_DUMPCORE was not set.
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700394 */
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700395 mmap_assert_locked(mm);
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700396
Jan Kara82b0f8c2016-12-14 15:06:58 -0800397 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700398 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700399 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700400
401 BUG_ON(ctx->mm != mm);
402
403 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
404 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
405
Prakash Sangappa2d6d6f52017-09-06 16:23:39 -0700406 if (ctx->features & UFFD_FEATURE_SIGBUS)
407 goto out;
408
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700409 /*
410 * If it's already released don't get it. This avoids to loop
411 * in __get_user_pages if userfaultfd_release waits on the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700412 * caller of handle_userfault to release the mmap_lock.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700413 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700414 if (unlikely(READ_ONCE(ctx->released))) {
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700415 /*
416 * Don't return VM_FAULT_SIGBUS in this case, so a non
417 * cooperative manager can close the uffd after the
418 * last UFFDIO_COPY, without risking to trigger an
419 * involuntary SIGBUS if the process was starting the
420 * userfaultfd while the userfaultfd was still armed
421 * (but after the last UFFDIO_COPY). If the uffd
422 * wasn't already closed when the userfault reached
423 * this point, that would normally be solved by
424 * userfaultfd_must_wait returning 'false'.
425 *
426 * If we were to return VM_FAULT_SIGBUS here, the non
427 * cooperative manager would be instead forced to
428 * always call UFFDIO_UNREGISTER before it can safely
429 * close the uffd.
430 */
431 ret = VM_FAULT_NOPAGE;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700432 goto out;
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700433 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700434
435 /*
436 * Check that we can return VM_FAULT_RETRY.
437 *
438 * NOTE: it should become possible to return VM_FAULT_RETRY
439 * even if FAULT_FLAG_TRIED is set without leading to gup()
440 * -EBUSY failures, if the userfaultfd is to be extended for
441 * VM_UFFD_WP tracking and we intend to arm the userfault
442 * without first stopping userland access to the memory. For
443 * VM_UFFD_MISSING userfaults this is enough for now.
444 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800445 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700446 /*
447 * Validate the invariant that nowait must allow retry
448 * to be sure not to return SIGBUS erroneously on
449 * nowait invocations.
450 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800451 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700452#ifdef CONFIG_DEBUG_VM
453 if (printk_ratelimit()) {
454 printk(KERN_WARNING
Jan Kara82b0f8c2016-12-14 15:06:58 -0800455 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
456 vmf->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700457 dump_stack();
458 }
459#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700460 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700461 }
462
463 /*
464 * Handle nowait, not much to do other than tell it to retry
465 * and wait.
466 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700467 ret = VM_FAULT_RETRY;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800468 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700469 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700470
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700471 /* take the reference before dropping the mmap_lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700472 userfaultfd_ctx_get(ctx);
473
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700474 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
475 uwq.wq.private = current;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700476 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason,
477 ctx->features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700478 uwq.ctx = ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800479 uwq.waken = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700480
Peter Xu3e69ad02020-04-01 21:09:00 -0700481 blocking_state = userfaultfd_get_blocking_state(vmf->flags);
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700482
Eric Biggerscbcfa132019-07-04 15:14:39 -0700483 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700484 /*
485 * After the __add_wait_queue the uwq is visible to userland
486 * through poll/read().
487 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700488 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
489 /*
490 * The smp_mb() after __set_current_state prevents the reads
491 * following the spin_unlock to happen before the list_add in
492 * __add_wait_queue.
493 */
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800494 set_current_state(blocking_state);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700495 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700496
Mike Kravetz369cd212017-02-22 15:43:10 -0800497 if (!is_vm_hugetlb_page(vmf->vma))
498 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
499 reason);
500 else
Punit Agrawal7868a202017-07-06 15:39:42 -0700501 must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma,
502 vmf->address,
Mike Kravetz369cd212017-02-22 15:43:10 -0800503 vmf->flags, reason);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700504 mmap_read_unlock(mm);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700505
Linus Torvaldsf9bf3522020-08-02 10:42:31 -0700506 if (likely(must_wait && !READ_ONCE(ctx->released))) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800507 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700508 schedule();
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700509 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700510
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700511 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700512
513 /*
514 * Here we race with the list_del; list_add in
515 * userfaultfd_ctx_read(), however because we don't ever run
516 * list_del_init() to refile across the two lists, the prev
517 * and next pointers will never point to self. list_add also
518 * would never let any of the two pointers to point to
519 * self. So list_empty_careful won't risk to see both pointers
520 * pointing to self at any time during the list refile. The
521 * only case where list_del_init() is called is the full
522 * removal in the wake function and there we don't re-list_add
523 * and it's fine not to block on the spinlock. The uwq on this
524 * kernel stack can be released after the list_del_init.
525 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200526 if (!list_empty_careful(&uwq.wq.entry)) {
Eric Biggerscbcfa132019-07-04 15:14:39 -0700527 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700528 /*
529 * No need of list_del_init(), the uwq on the stack
530 * will be freed shortly anyway.
531 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200532 list_del(&uwq.wq.entry);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700533 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700534 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700535
536 /*
537 * ctx may go away after this if the userfault pseudo fd is
538 * already released.
539 */
540 userfaultfd_ctx_put(ctx);
541
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700542out:
543 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700544}
545
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800546static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
547 struct userfaultfd_wait_queue *ewq)
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800548{
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800549 struct userfaultfd_ctx *release_new_ctx;
550
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800551 if (WARN_ON_ONCE(current->flags & PF_EXITING))
552 goto out;
553
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800554 ewq->ctx = ctx;
555 init_waitqueue_entry(&ewq->wq, current);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800556 release_new_ctx = NULL;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800557
Eric Biggerscbcfa132019-07-04 15:14:39 -0700558 spin_lock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800559 /*
560 * After the __add_wait_queue the uwq is visible to userland
561 * through poll/read().
562 */
563 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
564 for (;;) {
565 set_current_state(TASK_KILLABLE);
566 if (ewq->msg.event == 0)
567 break;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700568 if (READ_ONCE(ctx->released) ||
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800569 fatal_signal_pending(current)) {
Andrea Arcangeli384632e2017-10-03 16:15:38 -0700570 /*
571 * &ewq->wq may be queued in fork_event, but
572 * __remove_wait_queue ignores the head
573 * parameter. It would be a problem if it
574 * didn't.
575 */
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800576 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800577 if (ewq->msg.event == UFFD_EVENT_FORK) {
578 struct userfaultfd_ctx *new;
579
580 new = (struct userfaultfd_ctx *)
581 (unsigned long)
582 ewq->msg.arg.reserved.reserved1;
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800583 release_new_ctx = new;
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800584 }
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800585 break;
586 }
587
Eric Biggerscbcfa132019-07-04 15:14:39 -0700588 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800589
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800590 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800591 schedule();
592
Eric Biggerscbcfa132019-07-04 15:14:39 -0700593 spin_lock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800594 }
595 __set_current_state(TASK_RUNNING);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700596 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800597
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800598 if (release_new_ctx) {
599 struct vm_area_struct *vma;
600 struct mm_struct *mm = release_new_ctx->mm;
601
602 /* the various vma->vm_userfaultfd_ctx still points to it */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700603 mmap_write_lock(mm);
Andrea Arcangeli04f58662019-04-18 17:50:52 -0700604 /* no task can run (and in turn coredump) yet */
605 VM_WARN_ON(!mmget_still_valid(mm));
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800606 for (vma = mm->mmap; vma; vma = vma->vm_next)
Mike Rapoport31e810a2018-08-02 15:36:09 -0700607 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800608 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Mike Rapoport31e810a2018-08-02 15:36:09 -0700609 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
610 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700611 mmap_write_unlock(mm);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800612
613 userfaultfd_ctx_put(release_new_ctx);
614 }
615
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800616 /*
617 * ctx may go away after this if the userfault pseudo fd is
618 * already released.
619 */
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800620out:
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700621 WRITE_ONCE(ctx->mmap_changing, false);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800622 userfaultfd_ctx_put(ctx);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800623}
624
625static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
626 struct userfaultfd_wait_queue *ewq)
627{
628 ewq->msg.event = 0;
629 wake_up_locked(&ctx->event_wqh);
630 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
631}
632
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800633int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
634{
635 struct userfaultfd_ctx *ctx = NULL, *octx;
636 struct userfaultfd_fork_ctx *fctx;
637
638 octx = vma->vm_userfaultfd_ctx.ctx;
639 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
640 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
641 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
642 return 0;
643 }
644
645 list_for_each_entry(fctx, fcs, list)
646 if (fctx->orig == octx) {
647 ctx = fctx->new;
648 break;
649 }
650
651 if (!ctx) {
652 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
653 if (!fctx)
654 return -ENOMEM;
655
656 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
657 if (!ctx) {
658 kfree(fctx);
659 return -ENOMEM;
660 }
661
Eric Biggersca880422018-12-28 00:34:43 -0800662 refcount_set(&ctx->refcount, 1);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800663 ctx->flags = octx->flags;
664 ctx->state = UFFD_STATE_RUNNING;
665 ctx->features = octx->features;
666 ctx->released = false;
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700667 ctx->mmap_changing = false;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800668 ctx->mm = vma->vm_mm;
Mike Rapoport00bb31f2017-11-15 17:36:56 -0800669 mmgrab(ctx->mm);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800670
671 userfaultfd_ctx_get(octx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700672 WRITE_ONCE(octx->mmap_changing, true);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800673 fctx->orig = octx;
674 fctx->new = ctx;
675 list_add_tail(&fctx->list, fcs);
676 }
677
678 vma->vm_userfaultfd_ctx.ctx = ctx;
679 return 0;
680}
681
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800682static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800683{
684 struct userfaultfd_ctx *ctx = fctx->orig;
685 struct userfaultfd_wait_queue ewq;
686
687 msg_init(&ewq.msg);
688
689 ewq.msg.event = UFFD_EVENT_FORK;
690 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
691
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800692 userfaultfd_event_wait_completion(ctx, &ewq);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800693}
694
695void dup_userfaultfd_complete(struct list_head *fcs)
696{
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800697 struct userfaultfd_fork_ctx *fctx, *n;
698
699 list_for_each_entry_safe(fctx, n, fcs, list) {
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800700 dup_fctx(fctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800701 list_del(&fctx->list);
702 kfree(fctx);
703 }
704}
705
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800706void mremap_userfaultfd_prep(struct vm_area_struct *vma,
707 struct vm_userfaultfd_ctx *vm_ctx)
708{
709 struct userfaultfd_ctx *ctx;
710
711 ctx = vma->vm_userfaultfd_ctx.ctx;
Peter Xu3cfd22b2018-12-28 00:38:47 -0800712
713 if (!ctx)
714 return;
715
716 if (ctx->features & UFFD_FEATURE_EVENT_REMAP) {
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800717 vm_ctx->ctx = ctx;
718 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700719 WRITE_ONCE(ctx->mmap_changing, true);
Peter Xu3cfd22b2018-12-28 00:38:47 -0800720 } else {
721 /* Drop uffd context if remap feature not enabled */
722 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
723 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800724 }
725}
726
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800727void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800728 unsigned long from, unsigned long to,
729 unsigned long len)
730{
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800731 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800732 struct userfaultfd_wait_queue ewq;
733
734 if (!ctx)
735 return;
736
737 if (to & ~PAGE_MASK) {
738 userfaultfd_ctx_put(ctx);
739 return;
740 }
741
742 msg_init(&ewq.msg);
743
744 ewq.msg.event = UFFD_EVENT_REMAP;
745 ewq.msg.arg.remap.from = from;
746 ewq.msg.arg.remap.to = to;
747 ewq.msg.arg.remap.len = len;
748
749 userfaultfd_event_wait_completion(ctx, &ewq);
750}
751
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800752bool userfaultfd_remove(struct vm_area_struct *vma,
Mike Rapoportd8119142017-02-24 14:56:02 -0800753 unsigned long start, unsigned long end)
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800754{
755 struct mm_struct *mm = vma->vm_mm;
756 struct userfaultfd_ctx *ctx;
757 struct userfaultfd_wait_queue ewq;
758
759 ctx = vma->vm_userfaultfd_ctx.ctx;
Mike Rapoportd8119142017-02-24 14:56:02 -0800760 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800761 return true;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800762
763 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700764 WRITE_ONCE(ctx->mmap_changing, true);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700765 mmap_read_unlock(mm);
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800766
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800767 msg_init(&ewq.msg);
768
Mike Rapoportd8119142017-02-24 14:56:02 -0800769 ewq.msg.event = UFFD_EVENT_REMOVE;
770 ewq.msg.arg.remove.start = start;
771 ewq.msg.arg.remove.end = end;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800772
773 userfaultfd_event_wait_completion(ctx, &ewq);
774
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800775 return false;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800776}
777
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800778static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
779 unsigned long start, unsigned long end)
780{
781 struct userfaultfd_unmap_ctx *unmap_ctx;
782
783 list_for_each_entry(unmap_ctx, unmaps, list)
784 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
785 unmap_ctx->end == end)
786 return true;
787
788 return false;
789}
790
791int userfaultfd_unmap_prep(struct vm_area_struct *vma,
792 unsigned long start, unsigned long end,
793 struct list_head *unmaps)
794{
795 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
796 struct userfaultfd_unmap_ctx *unmap_ctx;
797 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
798
799 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
800 has_unmap_ctx(ctx, unmaps, start, end))
801 continue;
802
803 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
804 if (!unmap_ctx)
805 return -ENOMEM;
806
807 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700808 WRITE_ONCE(ctx->mmap_changing, true);
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800809 unmap_ctx->ctx = ctx;
810 unmap_ctx->start = start;
811 unmap_ctx->end = end;
812 list_add_tail(&unmap_ctx->list, unmaps);
813 }
814
815 return 0;
816}
817
818void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
819{
820 struct userfaultfd_unmap_ctx *ctx, *n;
821 struct userfaultfd_wait_queue ewq;
822
823 list_for_each_entry_safe(ctx, n, uf, list) {
824 msg_init(&ewq.msg);
825
826 ewq.msg.event = UFFD_EVENT_UNMAP;
827 ewq.msg.arg.remove.start = ctx->start;
828 ewq.msg.arg.remove.end = ctx->end;
829
830 userfaultfd_event_wait_completion(ctx->ctx, &ewq);
831
832 list_del(&ctx->list);
833 kfree(ctx);
834 }
835}
836
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700837static int userfaultfd_release(struct inode *inode, struct file *file)
838{
839 struct userfaultfd_ctx *ctx = file->private_data;
840 struct mm_struct *mm = ctx->mm;
841 struct vm_area_struct *vma, *prev;
842 /* len == 0 means wake all */
843 struct userfaultfd_wake_range range = { .len = 0, };
844 unsigned long new_flags;
Oleg Nesterov46d0b242019-08-24 17:54:56 -0700845 bool still_valid;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700846
Mark Rutland6aa7de02017-10-23 14:07:29 -0700847 WRITE_ONCE(ctx->released, true);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700848
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700849 if (!mmget_not_zero(mm))
850 goto wakeup;
851
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700852 /*
853 * Flush page faults out of all CPUs. NOTE: all page faults
854 * must be retried without returning VM_FAULT_SIGBUS if
855 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700856 * changes while handle_userfault released the mmap_lock. So
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700857 * it's critical that released is set to true (above), before
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700858 * taking the mmap_lock for writing.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700859 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700860 mmap_write_lock(mm);
Oleg Nesterov46d0b242019-08-24 17:54:56 -0700861 still_valid = mmget_still_valid(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700862 prev = NULL;
863 for (vma = mm->mmap; vma; vma = vma->vm_next) {
864 cond_resched();
865 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
866 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
867 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
868 prev = vma;
869 continue;
870 }
871 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
Oleg Nesterov46d0b242019-08-24 17:54:56 -0700872 if (still_valid) {
873 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
874 new_flags, vma->anon_vma,
875 vma->vm_file, vma->vm_pgoff,
876 vma_policy(vma),
Greg Kroah-Hartmanad455d82019-08-26 16:45:30 +0200877 NULL_VM_UFFD_CTX,
878 vma_get_anon_name(vma));
Oleg Nesterov46d0b242019-08-24 17:54:56 -0700879 if (prev)
880 vma = prev;
881 else
882 prev = vma;
883 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700884 vma->vm_flags = new_flags;
885 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
886 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700887 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700888 mmput(mm);
889wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700890 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700891 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700892 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700893 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700894 */
Eric Biggerscbcfa132019-07-04 15:14:39 -0700895 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700896 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -0700897 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700898 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700899
Mike Rapoport5a18b642017-08-02 13:32:24 -0700900 /* Flush pending events that may still wait on event_wqh */
901 wake_up_all(&ctx->event_wqh);
902
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800903 wake_up_poll(&ctx->fd_wqh, EPOLLHUP);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700904 userfaultfd_ctx_put(ctx);
905 return 0;
906}
907
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700908/* fault_pending_wqh.lock must be hold by the caller */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800909static inline struct userfaultfd_wait_queue *find_userfault_in(
910 wait_queue_head_t *wqh)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700911{
Ingo Molnarac6424b2017-06-20 12:06:13 +0200912 wait_queue_entry_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700913 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700914
Lance Roy456a7372018-10-04 23:45:44 -0700915 lockdep_assert_held(&wqh->lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700916
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700917 uwq = NULL;
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800918 if (!waitqueue_active(wqh))
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700919 goto out;
920 /* walk in reverse to provide FIFO behavior to read userfaults */
Ingo Molnar2055da92017-06-20 12:06:46 +0200921 wq = list_last_entry(&wqh->head, typeof(*wq), entry);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700922 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
923out:
924 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700925}
926
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800927static inline struct userfaultfd_wait_queue *find_userfault(
928 struct userfaultfd_ctx *ctx)
929{
930 return find_userfault_in(&ctx->fault_pending_wqh);
931}
932
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800933static inline struct userfaultfd_wait_queue *find_userfault_evt(
934 struct userfaultfd_ctx *ctx)
935{
936 return find_userfault_in(&ctx->event_wqh);
937}
938
Al Viro076ccb72017-07-03 01:02:18 -0400939static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700940{
941 struct userfaultfd_ctx *ctx = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400942 __poll_t ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700943
944 poll_wait(file, &ctx->fd_wqh, wait);
945
946 switch (ctx->state) {
947 case UFFD_STATE_WAIT_API:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800948 return EPOLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700949 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700950 /*
951 * poll() never guarantees that read won't block.
952 * userfaults can be waken before they're read().
953 */
954 if (unlikely(!(file->f_flags & O_NONBLOCK)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800955 return EPOLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700956 /*
957 * lockless access to see if there are pending faults
958 * __pollwait last action is the add_wait_queue but
959 * the spin_unlock would allow the waitqueue_active to
960 * pass above the actual list_add inside
961 * add_wait_queue critical section. So use a full
962 * memory barrier to serialize the list_add write of
963 * add_wait_queue() with the waitqueue_active read
964 * below.
965 */
966 ret = 0;
967 smp_mb();
968 if (waitqueue_active(&ctx->fault_pending_wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800969 ret = EPOLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800970 else if (waitqueue_active(&ctx->event_wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800971 ret = EPOLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800972
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700973 return ret;
974 default:
Andrea Arcangeli84749012017-02-22 15:42:12 -0800975 WARN_ON_ONCE(1);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800976 return EPOLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700977 }
978}
979
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800980static const struct file_operations userfaultfd_fops;
981
982static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
983 struct userfaultfd_ctx *new,
984 struct uffd_msg *msg)
985{
986 int fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800987
Eric Biggers284cd242018-01-31 16:19:48 -0800988 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new,
989 O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS));
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800990 if (fd < 0)
991 return fd;
992
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800993 msg->arg.reserved.reserved1 = 0;
994 msg->arg.fork.ufd = fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800995 return 0;
996}
997
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700998static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700999 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001000{
1001 ssize_t ret;
1002 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001003 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001004 /*
1005 * Handling fork event requires sleeping operations, so
1006 * we drop the event_wqh lock, then do these ops, then
1007 * lock it back and wake up the waiter. While the lock is
1008 * dropped the ewq may go away so we keep track of it
1009 * carefully.
1010 */
1011 LIST_HEAD(fork_event);
1012 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001013
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001014 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Christoph Hellwigae62c162018-10-26 15:02:19 -07001015 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001016 __add_wait_queue(&ctx->fd_wqh, &wait);
1017 for (;;) {
1018 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001019 spin_lock(&ctx->fault_pending_wqh.lock);
1020 uwq = find_userfault(ctx);
1021 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001022 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001023 * Use a seqcount to repeat the lockless check
1024 * in wake_userfault() to avoid missing
1025 * wakeups because during the refile both
1026 * waitqueue could become empty if this is the
1027 * only userfault.
1028 */
1029 write_seqcount_begin(&ctx->refile_seq);
1030
1031 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001032 * The fault_pending_wqh.lock prevents the uwq
1033 * to disappear from under us.
1034 *
1035 * Refile this userfault from
1036 * fault_pending_wqh to fault_wqh, it's not
1037 * pending anymore after we read it.
1038 *
1039 * Use list_del() by hand (as
1040 * userfaultfd_wake_function also uses
1041 * list_del_init() by hand) to be sure nobody
1042 * changes __remove_wait_queue() to use
1043 * list_del_init() in turn breaking the
1044 * !list_empty_careful() check in
Ingo Molnar2055da92017-06-20 12:06:46 +02001045 * handle_userfault(). The uwq->wq.head list
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001046 * must never be empty at any time during the
1047 * refile, or the waitqueue could disappear
1048 * from under us. The "wait_queue_head_t"
1049 * parameter of __remove_wait_queue() is unused
1050 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001051 */
Ingo Molnar2055da92017-06-20 12:06:46 +02001052 list_del(&uwq->wq.entry);
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001053 add_wait_queue(&ctx->fault_wqh, &uwq->wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001054
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001055 write_seqcount_end(&ctx->refile_seq);
1056
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001057 /* careful to always initialize msg if ret == 0 */
1058 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001059 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001060 ret = 0;
1061 break;
1062 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001063 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001064
1065 spin_lock(&ctx->event_wqh.lock);
1066 uwq = find_userfault_evt(ctx);
1067 if (uwq) {
1068 *msg = uwq->msg;
1069
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001070 if (uwq->msg.event == UFFD_EVENT_FORK) {
1071 fork_nctx = (struct userfaultfd_ctx *)
1072 (unsigned long)
1073 uwq->msg.arg.reserved.reserved1;
Ingo Molnar2055da92017-06-20 12:06:46 +02001074 list_move(&uwq->wq.entry, &fork_event);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001075 /*
1076 * fork_nctx can be freed as soon as
1077 * we drop the lock, unless we take a
1078 * reference on it.
1079 */
1080 userfaultfd_ctx_get(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001081 spin_unlock(&ctx->event_wqh.lock);
1082 ret = 0;
1083 break;
1084 }
1085
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001086 userfaultfd_event_complete(ctx, uwq);
1087 spin_unlock(&ctx->event_wqh.lock);
1088 ret = 0;
1089 break;
1090 }
1091 spin_unlock(&ctx->event_wqh.lock);
1092
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001093 if (signal_pending(current)) {
1094 ret = -ERESTARTSYS;
1095 break;
1096 }
1097 if (no_wait) {
1098 ret = -EAGAIN;
1099 break;
1100 }
Christoph Hellwigae62c162018-10-26 15:02:19 -07001101 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001102 schedule();
Christoph Hellwigae62c162018-10-26 15:02:19 -07001103 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001104 }
1105 __remove_wait_queue(&ctx->fd_wqh, &wait);
1106 __set_current_state(TASK_RUNNING);
Christoph Hellwigae62c162018-10-26 15:02:19 -07001107 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001108
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001109 if (!ret && msg->event == UFFD_EVENT_FORK) {
1110 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001111 spin_lock_irq(&ctx->event_wqh.lock);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001112 if (!list_empty(&fork_event)) {
1113 /*
1114 * The fork thread didn't abort, so we can
1115 * drop the temporary refcount.
1116 */
1117 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001118
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001119 uwq = list_first_entry(&fork_event,
1120 typeof(*uwq),
1121 wq.entry);
1122 /*
1123 * If fork_event list wasn't empty and in turn
1124 * the event wasn't already released by fork
1125 * (the event is allocated on fork kernel
1126 * stack), put the event back to its place in
1127 * the event_wq. fork_event head will be freed
1128 * as soon as we return so the event cannot
1129 * stay queued there no matter the current
1130 * "ret" value.
1131 */
1132 list_del(&uwq->wq.entry);
1133 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1134
1135 /*
1136 * Leave the event in the waitqueue and report
1137 * error to userland if we failed to resolve
1138 * the userfault fork.
1139 */
1140 if (likely(!ret))
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001141 userfaultfd_event_complete(ctx, uwq);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001142 } else {
1143 /*
1144 * Here the fork thread aborted and the
1145 * refcount from the fork thread on fork_nctx
1146 * has already been released. We still hold
1147 * the reference we took before releasing the
1148 * lock above. If resolve_userfault_fork
1149 * failed we've to drop it because the
1150 * fork_nctx has to be freed in such case. If
1151 * it succeeded we'll hold it because the new
1152 * uffd references it.
1153 */
1154 if (ret)
1155 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001156 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07001157 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001158 }
1159
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001160 return ret;
1161}
1162
1163static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
1166 struct userfaultfd_ctx *ctx = file->private_data;
1167 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001168 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001169 int no_wait = file->f_flags & O_NONBLOCK;
1170
1171 if (ctx->state == UFFD_STATE_WAIT_API)
1172 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001173
1174 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001175 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001176 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001177 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001178 if (_ret < 0)
1179 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001180 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001181 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001182 ret += sizeof(msg);
1183 buf += sizeof(msg);
1184 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001185 /*
1186 * Allow to read more than one fault at time but only
1187 * block if waiting for the very first one.
1188 */
1189 no_wait = O_NONBLOCK;
1190 }
1191}
1192
1193static void __wake_userfault(struct userfaultfd_ctx *ctx,
1194 struct userfaultfd_wake_range *range)
1195{
Eric Biggerscbcfa132019-07-04 15:14:39 -07001196 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001197 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001198 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001199 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001200 range);
1201 if (waitqueue_active(&ctx->fault_wqh))
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001202 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001203 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001204}
1205
1206static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1207 struct userfaultfd_wake_range *range)
1208{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001209 unsigned seq;
1210 bool need_wakeup;
1211
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001212 /*
1213 * To be sure waitqueue_active() is not reordered by the CPU
1214 * before the pagetable update, use an explicit SMP memory
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001215 * barrier here. PT lock release or mmap_read_unlock(mm) still
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001216 * have release semantics that can allow the
1217 * waitqueue_active() to be reordered before the pte update.
1218 */
1219 smp_mb();
1220
1221 /*
1222 * Use waitqueue_active because it's very frequent to
1223 * change the address space atomically even if there are no
1224 * userfaults yet. So we take the spinlock only when we're
1225 * sure we've userfaults to wake.
1226 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001227 do {
1228 seq = read_seqcount_begin(&ctx->refile_seq);
1229 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1230 waitqueue_active(&ctx->fault_wqh);
1231 cond_resched();
1232 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1233 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001234 __wake_userfault(ctx, range);
1235}
1236
1237static __always_inline int validate_range(struct mm_struct *mm,
Andrey Konovalov7d032572019-09-25 16:48:44 -07001238 __u64 *start, __u64 len)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001239{
1240 __u64 task_size = mm->task_size;
1241
Andrey Konovalov7d032572019-09-25 16:48:44 -07001242 *start = untagged_addr(*start);
1243
1244 if (*start & ~PAGE_MASK)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001245 return -EINVAL;
1246 if (len & ~PAGE_MASK)
1247 return -EINVAL;
1248 if (!len)
1249 return -EINVAL;
Andrey Konovalov7d032572019-09-25 16:48:44 -07001250 if (*start < mmap_min_addr)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001251 return -EINVAL;
Andrey Konovalov7d032572019-09-25 16:48:44 -07001252 if (*start >= task_size)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001253 return -EINVAL;
Andrey Konovalov7d032572019-09-25 16:48:44 -07001254 if (len > task_size - *start)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001255 return -EINVAL;
1256 return 0;
1257}
1258
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001259static inline bool vma_can_userfault(struct vm_area_struct *vma,
1260 unsigned long vm_flags)
Mike Rapoportba6907d2017-02-22 15:43:22 -08001261{
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001262 /* FIXME: add WP support to hugetlbfs and shmem */
1263 return vma_is_anonymous(vma) ||
1264 ((is_vm_hugetlb_page(vma) || vma_is_shmem(vma)) &&
1265 !(vm_flags & VM_UFFD_WP));
Mike Rapoportba6907d2017-02-22 15:43:22 -08001266}
1267
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001268static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1269 unsigned long arg)
1270{
1271 struct mm_struct *mm = ctx->mm;
1272 struct vm_area_struct *vma, *prev, *cur;
1273 int ret;
1274 struct uffdio_register uffdio_register;
1275 struct uffdio_register __user *user_uffdio_register;
1276 unsigned long vm_flags, new_flags;
1277 bool found;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001278 bool basic_ioctls;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001279 unsigned long start, end, vma_end;
1280
1281 user_uffdio_register = (struct uffdio_register __user *) arg;
1282
1283 ret = -EFAULT;
1284 if (copy_from_user(&uffdio_register, user_uffdio_register,
1285 sizeof(uffdio_register)-sizeof(__u64)))
1286 goto out;
1287
1288 ret = -EINVAL;
1289 if (!uffdio_register.mode)
1290 goto out;
1291 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1292 UFFDIO_REGISTER_MODE_WP))
1293 goto out;
1294 vm_flags = 0;
1295 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1296 vm_flags |= VM_UFFD_MISSING;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001297 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001298 vm_flags |= VM_UFFD_WP;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001299
Andrey Konovalov7d032572019-09-25 16:48:44 -07001300 ret = validate_range(mm, &uffdio_register.range.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001301 uffdio_register.range.len);
1302 if (ret)
1303 goto out;
1304
1305 start = uffdio_register.range.start;
1306 end = start + uffdio_register.range.len;
1307
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001308 ret = -ENOMEM;
1309 if (!mmget_not_zero(mm))
1310 goto out;
1311
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001312 mmap_write_lock(mm);
Andrea Arcangeli04f58662019-04-18 17:50:52 -07001313 if (!mmget_still_valid(mm))
1314 goto out_unlock;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001315 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001316 if (!vma)
1317 goto out_unlock;
1318
1319 /* check that there's at least one vma in the range */
1320 ret = -EINVAL;
1321 if (vma->vm_start >= end)
1322 goto out_unlock;
1323
1324 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001325 * If the first vma contains huge pages, make sure start address
1326 * is aligned to huge page size.
1327 */
1328 if (is_vm_hugetlb_page(vma)) {
1329 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1330
1331 if (start & (vma_hpagesize - 1))
1332 goto out_unlock;
1333 }
1334
1335 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001336 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001337 */
1338 found = false;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001339 basic_ioctls = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001340 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1341 cond_resched();
1342
1343 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1344 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1345
1346 /* check not compatible vmas */
1347 ret = -EINVAL;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001348 if (!vma_can_userfault(cur, vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001349 goto out_unlock;
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001350
1351 /*
1352 * UFFDIO_COPY will fill file holes even without
1353 * PROT_WRITE. This check enforces that if this is a
1354 * MAP_SHARED, the process has write permission to the backing
1355 * file. If VM_MAYWRITE is set it also enforces that on a
1356 * MAP_SHARED vma: there is no F_WRITE_SEAL and no further
1357 * F_WRITE_SEAL can be taken until the vma is destroyed.
1358 */
1359 ret = -EPERM;
1360 if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
1361 goto out_unlock;
1362
Mike Kravetzcab350a2017-02-22 15:43:04 -08001363 /*
1364 * If this vma contains ending address, and huge pages
1365 * check alignment.
1366 */
1367 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1368 end > cur->vm_start) {
1369 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1370
1371 ret = -EINVAL;
1372
1373 if (end & (vma_hpagesize - 1))
1374 goto out_unlock;
1375 }
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001376 if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
1377 goto out_unlock;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001378
1379 /*
1380 * Check that this vma isn't already owned by a
1381 * different userfaultfd. We can't allow more than one
1382 * userfaultfd to own a single vma simultaneously or we
1383 * wouldn't know which one to deliver the userfaults to.
1384 */
1385 ret = -EBUSY;
1386 if (cur->vm_userfaultfd_ctx.ctx &&
1387 cur->vm_userfaultfd_ctx.ctx != ctx)
1388 goto out_unlock;
1389
Mike Kravetzcab350a2017-02-22 15:43:04 -08001390 /*
1391 * Note vmas containing huge pages
1392 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001393 if (is_vm_hugetlb_page(cur))
1394 basic_ioctls = true;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001395
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001396 found = true;
1397 }
1398 BUG_ON(!found);
1399
1400 if (vma->vm_start < start)
1401 prev = vma;
1402
1403 ret = 0;
1404 do {
1405 cond_resched();
1406
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001407 BUG_ON(!vma_can_userfault(vma, vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001408 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1409 vma->vm_userfaultfd_ctx.ctx != ctx);
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001410 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001411
1412 /*
1413 * Nothing to do: this vma is already registered into this
1414 * userfaultfd and with the right tracking mode too.
1415 */
1416 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1417 (vma->vm_flags & vm_flags) == vm_flags)
1418 goto skip;
1419
1420 if (vma->vm_start > start)
1421 start = vma->vm_start;
1422 vma_end = min(end, vma->vm_end);
1423
Andrea Arcangeli9d4678e2019-11-30 17:57:58 -08001424 new_flags = (vma->vm_flags &
1425 ~(VM_UFFD_MISSING|VM_UFFD_WP)) | vm_flags;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001426 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1427 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1428 vma_policy(vma),
Colin Cross60500a42015-10-27 16:42:08 -07001429 ((struct vm_userfaultfd_ctx){ ctx }),
1430 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001431 if (prev) {
1432 vma = prev;
1433 goto next;
1434 }
1435 if (vma->vm_start < start) {
1436 ret = split_vma(mm, vma, start, 1);
1437 if (ret)
1438 break;
1439 }
1440 if (vma->vm_end > end) {
1441 ret = split_vma(mm, vma, end, 0);
1442 if (ret)
1443 break;
1444 }
1445 next:
1446 /*
1447 * In the vma_merge() successful mprotect-like case 8:
1448 * the next vma was merged into the current one and
1449 * the current one has not been updated yet.
1450 */
1451 vma->vm_flags = new_flags;
1452 vma->vm_userfaultfd_ctx.ctx = ctx;
1453
1454 skip:
1455 prev = vma;
1456 start = vma->vm_end;
1457 vma = vma->vm_next;
1458 } while (vma && vma->vm_start < end);
1459out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001460 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001461 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001462 if (!ret) {
Peter Xu14819302020-04-06 20:06:29 -07001463 __u64 ioctls_out;
1464
1465 ioctls_out = basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
1466 UFFD_API_RANGE_IOCTLS;
1467
1468 /*
1469 * Declare the WP ioctl only if the WP mode is
1470 * specified and all checks passed with the range
1471 */
1472 if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_WP))
1473 ioctls_out &= ~((__u64)1 << _UFFDIO_WRITEPROTECT);
1474
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001475 /*
1476 * Now that we scanned all vmas we can already tell
1477 * userland which ioctls methods are guaranteed to
1478 * succeed on this range.
1479 */
Peter Xu14819302020-04-06 20:06:29 -07001480 if (put_user(ioctls_out, &user_uffdio_register->ioctls))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001481 ret = -EFAULT;
1482 }
1483out:
1484 return ret;
1485}
1486
1487static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1488 unsigned long arg)
1489{
1490 struct mm_struct *mm = ctx->mm;
1491 struct vm_area_struct *vma, *prev, *cur;
1492 int ret;
1493 struct uffdio_range uffdio_unregister;
1494 unsigned long new_flags;
1495 bool found;
1496 unsigned long start, end, vma_end;
1497 const void __user *buf = (void __user *)arg;
1498
1499 ret = -EFAULT;
1500 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1501 goto out;
1502
Andrey Konovalov7d032572019-09-25 16:48:44 -07001503 ret = validate_range(mm, &uffdio_unregister.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001504 uffdio_unregister.len);
1505 if (ret)
1506 goto out;
1507
1508 start = uffdio_unregister.start;
1509 end = start + uffdio_unregister.len;
1510
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001511 ret = -ENOMEM;
1512 if (!mmget_not_zero(mm))
1513 goto out;
1514
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001515 mmap_write_lock(mm);
Andrea Arcangeli04f58662019-04-18 17:50:52 -07001516 if (!mmget_still_valid(mm))
1517 goto out_unlock;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001518 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001519 if (!vma)
1520 goto out_unlock;
1521
1522 /* check that there's at least one vma in the range */
1523 ret = -EINVAL;
1524 if (vma->vm_start >= end)
1525 goto out_unlock;
1526
1527 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001528 * If the first vma contains huge pages, make sure start address
1529 * is aligned to huge page size.
1530 */
1531 if (is_vm_hugetlb_page(vma)) {
1532 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1533
1534 if (start & (vma_hpagesize - 1))
1535 goto out_unlock;
1536 }
1537
1538 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001539 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001540 */
1541 found = false;
1542 ret = -EINVAL;
1543 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1544 cond_resched();
1545
1546 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1547 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1548
1549 /*
1550 * Check not compatible vmas, not strictly required
1551 * here as not compatible vmas cannot have an
1552 * userfaultfd_ctx registered on them, but this
1553 * provides for more strict behavior to notice
1554 * unregistration errors.
1555 */
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001556 if (!vma_can_userfault(cur, cur->vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001557 goto out_unlock;
1558
1559 found = true;
1560 }
1561 BUG_ON(!found);
1562
1563 if (vma->vm_start < start)
1564 prev = vma;
1565
1566 ret = 0;
1567 do {
1568 cond_resched();
1569
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001570 BUG_ON(!vma_can_userfault(vma, vma->vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001571
1572 /*
1573 * Nothing to do: this vma is already registered into this
1574 * userfaultfd and with the right tracking mode too.
1575 */
1576 if (!vma->vm_userfaultfd_ctx.ctx)
1577 goto skip;
1578
Andrea Arcangeli01e881f2018-12-14 14:17:17 -08001579 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
1580
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001581 if (vma->vm_start > start)
1582 start = vma->vm_start;
1583 vma_end = min(end, vma->vm_end);
1584
Andrea Arcangeli09fa5292017-02-22 15:42:46 -08001585 if (userfaultfd_missing(vma)) {
1586 /*
1587 * Wake any concurrent pending userfault while
1588 * we unregister, so they will not hang
1589 * permanently and it avoids userland to call
1590 * UFFDIO_WAKE explicitly.
1591 */
1592 struct userfaultfd_wake_range range;
1593 range.start = start;
1594 range.len = vma_end - start;
1595 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1596 }
1597
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001598 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1599 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1600 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1601 vma_policy(vma),
Colin Cross60500a42015-10-27 16:42:08 -07001602 NULL_VM_UFFD_CTX,
1603 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001604 if (prev) {
1605 vma = prev;
1606 goto next;
1607 }
1608 if (vma->vm_start < start) {
1609 ret = split_vma(mm, vma, start, 1);
1610 if (ret)
1611 break;
1612 }
1613 if (vma->vm_end > end) {
1614 ret = split_vma(mm, vma, end, 0);
1615 if (ret)
1616 break;
1617 }
1618 next:
1619 /*
1620 * In the vma_merge() successful mprotect-like case 8:
1621 * the next vma was merged into the current one and
1622 * the current one has not been updated yet.
1623 */
1624 vma->vm_flags = new_flags;
1625 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1626
1627 skip:
1628 prev = vma;
1629 start = vma->vm_end;
1630 vma = vma->vm_next;
1631 } while (vma && vma->vm_start < end);
1632out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001633 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001634 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001635out:
1636 return ret;
1637}
1638
1639/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001640 * userfaultfd_wake may be used in combination with the
1641 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001642 */
1643static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1644 unsigned long arg)
1645{
1646 int ret;
1647 struct uffdio_range uffdio_wake;
1648 struct userfaultfd_wake_range range;
1649 const void __user *buf = (void __user *)arg;
1650
1651 ret = -EFAULT;
1652 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1653 goto out;
1654
Andrey Konovalov7d032572019-09-25 16:48:44 -07001655 ret = validate_range(ctx->mm, &uffdio_wake.start, uffdio_wake.len);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001656 if (ret)
1657 goto out;
1658
1659 range.start = uffdio_wake.start;
1660 range.len = uffdio_wake.len;
1661
1662 /*
1663 * len == 0 means wake all and we don't want to wake all here,
1664 * so check it again to be sure.
1665 */
1666 VM_BUG_ON(!range.len);
1667
1668 wake_userfault(ctx, &range);
1669 ret = 0;
1670
1671out:
1672 return ret;
1673}
1674
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001675static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1676 unsigned long arg)
1677{
1678 __s64 ret;
1679 struct uffdio_copy uffdio_copy;
1680 struct uffdio_copy __user *user_uffdio_copy;
1681 struct userfaultfd_wake_range range;
1682
1683 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1684
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001685 ret = -EAGAIN;
1686 if (READ_ONCE(ctx->mmap_changing))
1687 goto out;
1688
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001689 ret = -EFAULT;
1690 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1691 /* don't copy "copy" last field */
1692 sizeof(uffdio_copy)-sizeof(__s64)))
1693 goto out;
1694
Andrey Konovalov7d032572019-09-25 16:48:44 -07001695 ret = validate_range(ctx->mm, &uffdio_copy.dst, uffdio_copy.len);
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001696 if (ret)
1697 goto out;
1698 /*
1699 * double check for wraparound just in case. copy_from_user()
1700 * will later check uffdio_copy.src + uffdio_copy.len to fit
1701 * in the userland range.
1702 */
1703 ret = -EINVAL;
1704 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1705 goto out;
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001706 if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP))
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001707 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001708 if (mmget_not_zero(ctx->mm)) {
1709 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001710 uffdio_copy.len, &ctx->mmap_changing,
1711 uffdio_copy.mode);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001712 mmput(ctx->mm);
Mike Rapoport96333182017-02-24 14:58:31 -08001713 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001714 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001715 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001716 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1717 return -EFAULT;
1718 if (ret < 0)
1719 goto out;
1720 BUG_ON(!ret);
1721 /* len == 0 would wake all */
1722 range.len = ret;
1723 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1724 range.start = uffdio_copy.dst;
1725 wake_userfault(ctx, &range);
1726 }
1727 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1728out:
1729 return ret;
1730}
1731
1732static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1733 unsigned long arg)
1734{
1735 __s64 ret;
1736 struct uffdio_zeropage uffdio_zeropage;
1737 struct uffdio_zeropage __user *user_uffdio_zeropage;
1738 struct userfaultfd_wake_range range;
1739
1740 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1741
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001742 ret = -EAGAIN;
1743 if (READ_ONCE(ctx->mmap_changing))
1744 goto out;
1745
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001746 ret = -EFAULT;
1747 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1748 /* don't copy "zeropage" last field */
1749 sizeof(uffdio_zeropage)-sizeof(__s64)))
1750 goto out;
1751
Andrey Konovalov7d032572019-09-25 16:48:44 -07001752 ret = validate_range(ctx->mm, &uffdio_zeropage.range.start,
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001753 uffdio_zeropage.range.len);
1754 if (ret)
1755 goto out;
1756 ret = -EINVAL;
1757 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1758 goto out;
1759
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001760 if (mmget_not_zero(ctx->mm)) {
1761 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001762 uffdio_zeropage.range.len,
1763 &ctx->mmap_changing);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001764 mmput(ctx->mm);
Mike Rapoport9d95aa42017-08-02 13:32:15 -07001765 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001766 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001767 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001768 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1769 return -EFAULT;
1770 if (ret < 0)
1771 goto out;
1772 /* len == 0 would wake all */
1773 BUG_ON(!ret);
1774 range.len = ret;
1775 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1776 range.start = uffdio_zeropage.range.start;
1777 wake_userfault(ctx, &range);
1778 }
1779 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1780out:
1781 return ret;
1782}
1783
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001784static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,
1785 unsigned long arg)
1786{
1787 int ret;
1788 struct uffdio_writeprotect uffdio_wp;
1789 struct uffdio_writeprotect __user *user_uffdio_wp;
1790 struct userfaultfd_wake_range range;
Peter Xu23080e22020-04-06 20:06:20 -07001791 bool mode_wp, mode_dontwake;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001792
1793 if (READ_ONCE(ctx->mmap_changing))
1794 return -EAGAIN;
1795
1796 user_uffdio_wp = (struct uffdio_writeprotect __user *) arg;
1797
1798 if (copy_from_user(&uffdio_wp, user_uffdio_wp,
1799 sizeof(struct uffdio_writeprotect)))
1800 return -EFAULT;
1801
1802 ret = validate_range(ctx->mm, &uffdio_wp.range.start,
1803 uffdio_wp.range.len);
1804 if (ret)
1805 return ret;
1806
1807 if (uffdio_wp.mode & ~(UFFDIO_WRITEPROTECT_MODE_DONTWAKE |
1808 UFFDIO_WRITEPROTECT_MODE_WP))
1809 return -EINVAL;
Peter Xu23080e22020-04-06 20:06:20 -07001810
1811 mode_wp = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_WP;
1812 mode_dontwake = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_DONTWAKE;
1813
1814 if (mode_wp && mode_dontwake)
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001815 return -EINVAL;
1816
1817 ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
Peter Xu23080e22020-04-06 20:06:20 -07001818 uffdio_wp.range.len, mode_wp,
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001819 &ctx->mmap_changing);
1820 if (ret)
1821 return ret;
1822
Peter Xu23080e22020-04-06 20:06:20 -07001823 if (!mode_wp && !mode_dontwake) {
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001824 range.start = uffdio_wp.range.start;
1825 range.len = uffdio_wp.range.len;
1826 wake_userfault(ctx, &range);
1827 }
1828 return ret;
1829}
1830
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001831static inline unsigned int uffd_ctx_features(__u64 user_features)
1832{
1833 /*
1834 * For the current set of features the bits just coincide
1835 */
1836 return (unsigned int)user_features;
1837}
1838
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001839/*
1840 * userland asks for a certain API version and we return which bits
1841 * and ioctl commands are implemented in this kernel for such API
1842 * version or -EINVAL if unknown.
1843 */
1844static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1845 unsigned long arg)
1846{
1847 struct uffdio_api uffdio_api;
1848 void __user *buf = (void __user *)arg;
1849 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001850 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001851
1852 ret = -EINVAL;
1853 if (ctx->state != UFFD_STATE_WAIT_API)
1854 goto out;
1855 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001856 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001857 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001858 features = uffdio_api.features;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001859 ret = -EINVAL;
1860 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES))
1861 goto err_out;
1862 ret = -EPERM;
1863 if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE))
1864 goto err_out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001865 /* report all available features and ioctls to userland */
1866 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001867 uffdio_api.ioctls = UFFD_API_IOCTLS;
1868 ret = -EFAULT;
1869 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1870 goto out;
1871 ctx->state = UFFD_STATE_RUNNING;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001872 /* only enable the requested features for this uffd context */
1873 ctx->features = uffd_ctx_features(features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001874 ret = 0;
1875out:
1876 return ret;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001877err_out:
1878 memset(&uffdio_api, 0, sizeof(uffdio_api));
1879 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1880 ret = -EFAULT;
1881 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001882}
1883
1884static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1885 unsigned long arg)
1886{
1887 int ret = -EINVAL;
1888 struct userfaultfd_ctx *ctx = file->private_data;
1889
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001890 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1891 return -EINVAL;
1892
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001893 switch(cmd) {
1894 case UFFDIO_API:
1895 ret = userfaultfd_api(ctx, arg);
1896 break;
1897 case UFFDIO_REGISTER:
1898 ret = userfaultfd_register(ctx, arg);
1899 break;
1900 case UFFDIO_UNREGISTER:
1901 ret = userfaultfd_unregister(ctx, arg);
1902 break;
1903 case UFFDIO_WAKE:
1904 ret = userfaultfd_wake(ctx, arg);
1905 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001906 case UFFDIO_COPY:
1907 ret = userfaultfd_copy(ctx, arg);
1908 break;
1909 case UFFDIO_ZEROPAGE:
1910 ret = userfaultfd_zeropage(ctx, arg);
1911 break;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001912 case UFFDIO_WRITEPROTECT:
1913 ret = userfaultfd_writeprotect(ctx, arg);
1914 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001915 }
1916 return ret;
1917}
1918
1919#ifdef CONFIG_PROC_FS
1920static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1921{
1922 struct userfaultfd_ctx *ctx = f->private_data;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001923 wait_queue_entry_t *wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001924 unsigned long pending = 0, total = 0;
1925
Eric Biggerscbcfa132019-07-04 15:14:39 -07001926 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Ingo Molnar2055da92017-06-20 12:06:46 +02001927 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001928 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001929 total++;
1930 }
Ingo Molnar2055da92017-06-20 12:06:46 +02001931 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001932 total++;
1933 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07001934 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001935
1936 /*
1937 * If more protocols will be added, there will be all shown
1938 * separated by a space. Like this:
1939 * protocols: aa:... bb:...
1940 */
1941 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Mike Rapoport045098e2017-04-07 16:04:42 -07001942 pending, total, UFFD_API, ctx->features,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001943 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1944}
1945#endif
1946
1947static const struct file_operations userfaultfd_fops = {
1948#ifdef CONFIG_PROC_FS
1949 .show_fdinfo = userfaultfd_show_fdinfo,
1950#endif
1951 .release = userfaultfd_release,
1952 .poll = userfaultfd_poll,
1953 .read = userfaultfd_read,
1954 .unlocked_ioctl = userfaultfd_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +02001955 .compat_ioctl = compat_ptr_ioctl,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001956 .llseek = noop_llseek,
1957};
1958
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001959static void init_once_userfaultfd_ctx(void *mem)
1960{
1961 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1962
1963 init_waitqueue_head(&ctx->fault_pending_wqh);
1964 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001965 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001966 init_waitqueue_head(&ctx->fd_wqh);
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001967 seqcount_init(&ctx->refile_seq);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001968}
1969
Eric Biggers284cd242018-01-31 16:19:48 -08001970SYSCALL_DEFINE1(userfaultfd, int, flags)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001971{
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001972 struct userfaultfd_ctx *ctx;
Eric Biggers284cd242018-01-31 16:19:48 -08001973 int fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001974
Peter Xucefdca02019-05-13 17:16:41 -07001975 if (!sysctl_unprivileged_userfaultfd && !capable(CAP_SYS_PTRACE))
1976 return -EPERM;
1977
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001978 BUG_ON(!current->mm);
1979
1980 /* Check the UFFD_* constants for consistency. */
1981 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1982 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1983
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001984 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
Eric Biggers284cd242018-01-31 16:19:48 -08001985 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001986
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001987 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001988 if (!ctx)
Eric Biggers284cd242018-01-31 16:19:48 -08001989 return -ENOMEM;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001990
Eric Biggersca880422018-12-28 00:34:43 -08001991 refcount_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001992 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001993 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001994 ctx->state = UFFD_STATE_WAIT_API;
1995 ctx->released = false;
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001996 ctx->mmap_changing = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001997 ctx->mm = current->mm;
1998 /* prevent the mm struct to be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001999 mmgrab(ctx->mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002000
Eric Biggers284cd242018-01-31 16:19:48 -08002001 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
2002 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
2003 if (fd < 0) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07002004 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002005 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07002006 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002007 return fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002008}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002009
2010static int __init userfaultfd_init(void)
2011{
2012 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
2013 sizeof(struct userfaultfd_ctx),
2014 0,
2015 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2016 init_once_userfaultfd_ctx);
2017 return 0;
2018}
2019__initcall(userfaultfd_init);