blob: 000b457ad087668e08d008d23fa4dd3223458127 [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 */
Ahmed S. Darwish2ca97ac2020-07-20 17:55:28 +020064 seqcount_spinlock_t 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 Arcangeli0cbb4b42018-01-04 16:18:09 -0800604 for (vma = mm->mmap; vma; vma = vma->vm_next)
Mike Rapoport31e810a2018-08-02 15:36:09 -0700605 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800606 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Mike Rapoport31e810a2018-08-02 15:36:09 -0700607 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
608 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700609 mmap_write_unlock(mm);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800610
611 userfaultfd_ctx_put(release_new_ctx);
612 }
613
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800614 /*
615 * ctx may go away after this if the userfault pseudo fd is
616 * already released.
617 */
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800618out:
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700619 WRITE_ONCE(ctx->mmap_changing, false);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800620 userfaultfd_ctx_put(ctx);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800621}
622
623static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
624 struct userfaultfd_wait_queue *ewq)
625{
626 ewq->msg.event = 0;
627 wake_up_locked(&ctx->event_wqh);
628 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
629}
630
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800631int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
632{
633 struct userfaultfd_ctx *ctx = NULL, *octx;
634 struct userfaultfd_fork_ctx *fctx;
635
636 octx = vma->vm_userfaultfd_ctx.ctx;
637 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
638 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
639 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
640 return 0;
641 }
642
643 list_for_each_entry(fctx, fcs, list)
644 if (fctx->orig == octx) {
645 ctx = fctx->new;
646 break;
647 }
648
649 if (!ctx) {
650 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
651 if (!fctx)
652 return -ENOMEM;
653
654 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
655 if (!ctx) {
656 kfree(fctx);
657 return -ENOMEM;
658 }
659
Eric Biggersca880422018-12-28 00:34:43 -0800660 refcount_set(&ctx->refcount, 1);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800661 ctx->flags = octx->flags;
662 ctx->state = UFFD_STATE_RUNNING;
663 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
939 switch (ctx->state) {
940 case UFFD_STATE_WAIT_API:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800941 return EPOLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700942 case UFFD_STATE_RUNNING:
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700943 /*
944 * poll() never guarantees that read won't block.
945 * userfaults can be waken before they're read().
946 */
947 if (unlikely(!(file->f_flags & O_NONBLOCK)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800948 return EPOLLERR;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700949 /*
950 * lockless access to see if there are pending faults
951 * __pollwait last action is the add_wait_queue but
952 * the spin_unlock would allow the waitqueue_active to
953 * pass above the actual list_add inside
954 * add_wait_queue critical section. So use a full
955 * memory barrier to serialize the list_add write of
956 * add_wait_queue() with the waitqueue_active read
957 * below.
958 */
959 ret = 0;
960 smp_mb();
961 if (waitqueue_active(&ctx->fault_pending_wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800962 ret = EPOLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800963 else if (waitqueue_active(&ctx->event_wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800964 ret = EPOLLIN;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800965
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700966 return ret;
967 default:
Andrea Arcangeli84749012017-02-22 15:42:12 -0800968 WARN_ON_ONCE(1);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800969 return EPOLLERR;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700970 }
971}
972
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800973static const struct file_operations userfaultfd_fops;
974
975static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
976 struct userfaultfd_ctx *new,
977 struct uffd_msg *msg)
978{
979 int fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800980
Eric Biggers284cd242018-01-31 16:19:48 -0800981 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new,
982 O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS));
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800983 if (fd < 0)
984 return fd;
985
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800986 msg->arg.reserved.reserved1 = 0;
987 msg->arg.fork.ufd = fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800988 return 0;
989}
990
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700991static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700992 struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700993{
994 ssize_t ret;
995 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700996 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800997 /*
998 * Handling fork event requires sleeping operations, so
999 * we drop the event_wqh lock, then do these ops, then
1000 * lock it back and wake up the waiter. While the lock is
1001 * dropped the ewq may go away so we keep track of it
1002 * carefully.
1003 */
1004 LIST_HEAD(fork_event);
1005 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001006
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001007 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Christoph Hellwigae62c162018-10-26 15:02:19 -07001008 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001009 __add_wait_queue(&ctx->fd_wqh, &wait);
1010 for (;;) {
1011 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001012 spin_lock(&ctx->fault_pending_wqh.lock);
1013 uwq = find_userfault(ctx);
1014 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001015 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001016 * Use a seqcount to repeat the lockless check
1017 * in wake_userfault() to avoid missing
1018 * wakeups because during the refile both
1019 * waitqueue could become empty if this is the
1020 * only userfault.
1021 */
1022 write_seqcount_begin(&ctx->refile_seq);
1023
1024 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001025 * The fault_pending_wqh.lock prevents the uwq
1026 * to disappear from under us.
1027 *
1028 * Refile this userfault from
1029 * fault_pending_wqh to fault_wqh, it's not
1030 * pending anymore after we read it.
1031 *
1032 * Use list_del() by hand (as
1033 * userfaultfd_wake_function also uses
1034 * list_del_init() by hand) to be sure nobody
1035 * changes __remove_wait_queue() to use
1036 * list_del_init() in turn breaking the
1037 * !list_empty_careful() check in
Ingo Molnar2055da92017-06-20 12:06:46 +02001038 * handle_userfault(). The uwq->wq.head list
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001039 * must never be empty at any time during the
1040 * refile, or the waitqueue could disappear
1041 * from under us. The "wait_queue_head_t"
1042 * parameter of __remove_wait_queue() is unused
1043 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001044 */
Ingo Molnar2055da92017-06-20 12:06:46 +02001045 list_del(&uwq->wq.entry);
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001046 add_wait_queue(&ctx->fault_wqh, &uwq->wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001047
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001048 write_seqcount_end(&ctx->refile_seq);
1049
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001050 /* careful to always initialize msg if ret == 0 */
1051 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001052 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001053 ret = 0;
1054 break;
1055 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001056 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001057
1058 spin_lock(&ctx->event_wqh.lock);
1059 uwq = find_userfault_evt(ctx);
1060 if (uwq) {
1061 *msg = uwq->msg;
1062
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001063 if (uwq->msg.event == UFFD_EVENT_FORK) {
1064 fork_nctx = (struct userfaultfd_ctx *)
1065 (unsigned long)
1066 uwq->msg.arg.reserved.reserved1;
Ingo Molnar2055da92017-06-20 12:06:46 +02001067 list_move(&uwq->wq.entry, &fork_event);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001068 /*
1069 * fork_nctx can be freed as soon as
1070 * we drop the lock, unless we take a
1071 * reference on it.
1072 */
1073 userfaultfd_ctx_get(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001074 spin_unlock(&ctx->event_wqh.lock);
1075 ret = 0;
1076 break;
1077 }
1078
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001079 userfaultfd_event_complete(ctx, uwq);
1080 spin_unlock(&ctx->event_wqh.lock);
1081 ret = 0;
1082 break;
1083 }
1084 spin_unlock(&ctx->event_wqh.lock);
1085
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001086 if (signal_pending(current)) {
1087 ret = -ERESTARTSYS;
1088 break;
1089 }
1090 if (no_wait) {
1091 ret = -EAGAIN;
1092 break;
1093 }
Christoph Hellwigae62c162018-10-26 15:02:19 -07001094 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001095 schedule();
Christoph Hellwigae62c162018-10-26 15:02:19 -07001096 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001097 }
1098 __remove_wait_queue(&ctx->fd_wqh, &wait);
1099 __set_current_state(TASK_RUNNING);
Christoph Hellwigae62c162018-10-26 15:02:19 -07001100 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001101
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001102 if (!ret && msg->event == UFFD_EVENT_FORK) {
1103 ret = resolve_userfault_fork(ctx, fork_nctx, msg);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001104 spin_lock_irq(&ctx->event_wqh.lock);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001105 if (!list_empty(&fork_event)) {
1106 /*
1107 * The fork thread didn't abort, so we can
1108 * drop the temporary refcount.
1109 */
1110 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001111
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001112 uwq = list_first_entry(&fork_event,
1113 typeof(*uwq),
1114 wq.entry);
1115 /*
1116 * If fork_event list wasn't empty and in turn
1117 * the event wasn't already released by fork
1118 * (the event is allocated on fork kernel
1119 * stack), put the event back to its place in
1120 * the event_wq. fork_event head will be freed
1121 * as soon as we return so the event cannot
1122 * stay queued there no matter the current
1123 * "ret" value.
1124 */
1125 list_del(&uwq->wq.entry);
1126 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1127
1128 /*
1129 * Leave the event in the waitqueue and report
1130 * error to userland if we failed to resolve
1131 * the userfault fork.
1132 */
1133 if (likely(!ret))
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001134 userfaultfd_event_complete(ctx, uwq);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001135 } else {
1136 /*
1137 * Here the fork thread aborted and the
1138 * refcount from the fork thread on fork_nctx
1139 * has already been released. We still hold
1140 * the reference we took before releasing the
1141 * lock above. If resolve_userfault_fork
1142 * failed we've to drop it because the
1143 * fork_nctx has to be freed in such case. If
1144 * it succeeded we'll hold it because the new
1145 * uffd references it.
1146 */
1147 if (ret)
1148 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001149 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07001150 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001151 }
1152
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001153 return ret;
1154}
1155
1156static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1157 size_t count, loff_t *ppos)
1158{
1159 struct userfaultfd_ctx *ctx = file->private_data;
1160 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001161 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001162 int no_wait = file->f_flags & O_NONBLOCK;
1163
1164 if (ctx->state == UFFD_STATE_WAIT_API)
1165 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001166
1167 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001168 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001169 return ret ? ret : -EINVAL;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001170 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001171 if (_ret < 0)
1172 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001173 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001174 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001175 ret += sizeof(msg);
1176 buf += sizeof(msg);
1177 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001178 /*
1179 * Allow to read more than one fault at time but only
1180 * block if waiting for the very first one.
1181 */
1182 no_wait = O_NONBLOCK;
1183 }
1184}
1185
1186static void __wake_userfault(struct userfaultfd_ctx *ctx,
1187 struct userfaultfd_wake_range *range)
1188{
Eric Biggerscbcfa132019-07-04 15:14:39 -07001189 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001190 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001191 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001192 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001193 range);
1194 if (waitqueue_active(&ctx->fault_wqh))
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001195 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001196 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001197}
1198
1199static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1200 struct userfaultfd_wake_range *range)
1201{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001202 unsigned seq;
1203 bool need_wakeup;
1204
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001205 /*
1206 * To be sure waitqueue_active() is not reordered by the CPU
1207 * before the pagetable update, use an explicit SMP memory
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001208 * barrier here. PT lock release or mmap_read_unlock(mm) still
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001209 * have release semantics that can allow the
1210 * waitqueue_active() to be reordered before the pte update.
1211 */
1212 smp_mb();
1213
1214 /*
1215 * Use waitqueue_active because it's very frequent to
1216 * change the address space atomically even if there are no
1217 * userfaults yet. So we take the spinlock only when we're
1218 * sure we've userfaults to wake.
1219 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001220 do {
1221 seq = read_seqcount_begin(&ctx->refile_seq);
1222 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1223 waitqueue_active(&ctx->fault_wqh);
1224 cond_resched();
1225 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1226 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001227 __wake_userfault(ctx, range);
1228}
1229
1230static __always_inline int validate_range(struct mm_struct *mm,
Andrey Konovalov7d032572019-09-25 16:48:44 -07001231 __u64 *start, __u64 len)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001232{
1233 __u64 task_size = mm->task_size;
1234
Andrey Konovalov7d032572019-09-25 16:48:44 -07001235 *start = untagged_addr(*start);
1236
1237 if (*start & ~PAGE_MASK)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001238 return -EINVAL;
1239 if (len & ~PAGE_MASK)
1240 return -EINVAL;
1241 if (!len)
1242 return -EINVAL;
Andrey Konovalov7d032572019-09-25 16:48:44 -07001243 if (*start < mmap_min_addr)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001244 return -EINVAL;
Andrey Konovalov7d032572019-09-25 16:48:44 -07001245 if (*start >= task_size)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001246 return -EINVAL;
Andrey Konovalov7d032572019-09-25 16:48:44 -07001247 if (len > task_size - *start)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001248 return -EINVAL;
1249 return 0;
1250}
1251
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001252static inline bool vma_can_userfault(struct vm_area_struct *vma,
1253 unsigned long vm_flags)
Mike Rapoportba6907d2017-02-22 15:43:22 -08001254{
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001255 /* FIXME: add WP support to hugetlbfs and shmem */
1256 return vma_is_anonymous(vma) ||
1257 ((is_vm_hugetlb_page(vma) || vma_is_shmem(vma)) &&
1258 !(vm_flags & VM_UFFD_WP));
Mike Rapoportba6907d2017-02-22 15:43:22 -08001259}
1260
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001261static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1262 unsigned long arg)
1263{
1264 struct mm_struct *mm = ctx->mm;
1265 struct vm_area_struct *vma, *prev, *cur;
1266 int ret;
1267 struct uffdio_register uffdio_register;
1268 struct uffdio_register __user *user_uffdio_register;
1269 unsigned long vm_flags, new_flags;
1270 bool found;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001271 bool basic_ioctls;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001272 unsigned long start, end, vma_end;
1273
1274 user_uffdio_register = (struct uffdio_register __user *) arg;
1275
1276 ret = -EFAULT;
1277 if (copy_from_user(&uffdio_register, user_uffdio_register,
1278 sizeof(uffdio_register)-sizeof(__u64)))
1279 goto out;
1280
1281 ret = -EINVAL;
1282 if (!uffdio_register.mode)
1283 goto out;
1284 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
1285 UFFDIO_REGISTER_MODE_WP))
1286 goto out;
1287 vm_flags = 0;
1288 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1289 vm_flags |= VM_UFFD_MISSING;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001290 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001291 vm_flags |= VM_UFFD_WP;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001292
Andrey Konovalov7d032572019-09-25 16:48:44 -07001293 ret = validate_range(mm, &uffdio_register.range.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001294 uffdio_register.range.len);
1295 if (ret)
1296 goto out;
1297
1298 start = uffdio_register.range.start;
1299 end = start + uffdio_register.range.len;
1300
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001301 ret = -ENOMEM;
1302 if (!mmget_not_zero(mm))
1303 goto out;
1304
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001305 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001306 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001307 if (!vma)
1308 goto out_unlock;
1309
1310 /* check that there's at least one vma in the range */
1311 ret = -EINVAL;
1312 if (vma->vm_start >= end)
1313 goto out_unlock;
1314
1315 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001316 * If the first vma contains huge pages, make sure start address
1317 * is aligned to huge page size.
1318 */
1319 if (is_vm_hugetlb_page(vma)) {
1320 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1321
1322 if (start & (vma_hpagesize - 1))
1323 goto out_unlock;
1324 }
1325
1326 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001327 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001328 */
1329 found = false;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001330 basic_ioctls = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001331 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1332 cond_resched();
1333
1334 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1335 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1336
1337 /* check not compatible vmas */
1338 ret = -EINVAL;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001339 if (!vma_can_userfault(cur, vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001340 goto out_unlock;
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001341
1342 /*
1343 * UFFDIO_COPY will fill file holes even without
1344 * PROT_WRITE. This check enforces that if this is a
1345 * MAP_SHARED, the process has write permission to the backing
1346 * file. If VM_MAYWRITE is set it also enforces that on a
1347 * MAP_SHARED vma: there is no F_WRITE_SEAL and no further
1348 * F_WRITE_SEAL can be taken until the vma is destroyed.
1349 */
1350 ret = -EPERM;
1351 if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
1352 goto out_unlock;
1353
Mike Kravetzcab350a2017-02-22 15:43:04 -08001354 /*
1355 * If this vma contains ending address, and huge pages
1356 * check alignment.
1357 */
1358 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1359 end > cur->vm_start) {
1360 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1361
1362 ret = -EINVAL;
1363
1364 if (end & (vma_hpagesize - 1))
1365 goto out_unlock;
1366 }
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001367 if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
1368 goto out_unlock;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001369
1370 /*
1371 * Check that this vma isn't already owned by a
1372 * different userfaultfd. We can't allow more than one
1373 * userfaultfd to own a single vma simultaneously or we
1374 * wouldn't know which one to deliver the userfaults to.
1375 */
1376 ret = -EBUSY;
1377 if (cur->vm_userfaultfd_ctx.ctx &&
1378 cur->vm_userfaultfd_ctx.ctx != ctx)
1379 goto out_unlock;
1380
Mike Kravetzcab350a2017-02-22 15:43:04 -08001381 /*
1382 * Note vmas containing huge pages
1383 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001384 if (is_vm_hugetlb_page(cur))
1385 basic_ioctls = true;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001386
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001387 found = true;
1388 }
1389 BUG_ON(!found);
1390
1391 if (vma->vm_start < start)
1392 prev = vma;
1393
1394 ret = 0;
1395 do {
1396 cond_resched();
1397
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001398 BUG_ON(!vma_can_userfault(vma, vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001399 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1400 vma->vm_userfaultfd_ctx.ctx != ctx);
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001401 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001402
1403 /*
1404 * Nothing to do: this vma is already registered into this
1405 * userfaultfd and with the right tracking mode too.
1406 */
1407 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1408 (vma->vm_flags & vm_flags) == vm_flags)
1409 goto skip;
1410
1411 if (vma->vm_start > start)
1412 start = vma->vm_start;
1413 vma_end = min(end, vma->vm_end);
1414
Andrea Arcangeli9d4678e2019-11-30 17:57:58 -08001415 new_flags = (vma->vm_flags &
1416 ~(VM_UFFD_MISSING|VM_UFFD_WP)) | vm_flags;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001417 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1418 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1419 vma_policy(vma),
1420 ((struct vm_userfaultfd_ctx){ ctx }));
1421 if (prev) {
1422 vma = prev;
1423 goto next;
1424 }
1425 if (vma->vm_start < start) {
1426 ret = split_vma(mm, vma, start, 1);
1427 if (ret)
1428 break;
1429 }
1430 if (vma->vm_end > end) {
1431 ret = split_vma(mm, vma, end, 0);
1432 if (ret)
1433 break;
1434 }
1435 next:
1436 /*
1437 * In the vma_merge() successful mprotect-like case 8:
1438 * the next vma was merged into the current one and
1439 * the current one has not been updated yet.
1440 */
1441 vma->vm_flags = new_flags;
1442 vma->vm_userfaultfd_ctx.ctx = ctx;
1443
1444 skip:
1445 prev = vma;
1446 start = vma->vm_end;
1447 vma = vma->vm_next;
1448 } while (vma && vma->vm_start < end);
1449out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001450 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001451 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001452 if (!ret) {
Peter Xu14819302020-04-06 20:06:29 -07001453 __u64 ioctls_out;
1454
1455 ioctls_out = basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
1456 UFFD_API_RANGE_IOCTLS;
1457
1458 /*
1459 * Declare the WP ioctl only if the WP mode is
1460 * specified and all checks passed with the range
1461 */
1462 if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_WP))
1463 ioctls_out &= ~((__u64)1 << _UFFDIO_WRITEPROTECT);
1464
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001465 /*
1466 * Now that we scanned all vmas we can already tell
1467 * userland which ioctls methods are guaranteed to
1468 * succeed on this range.
1469 */
Peter Xu14819302020-04-06 20:06:29 -07001470 if (put_user(ioctls_out, &user_uffdio_register->ioctls))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001471 ret = -EFAULT;
1472 }
1473out:
1474 return ret;
1475}
1476
1477static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1478 unsigned long arg)
1479{
1480 struct mm_struct *mm = ctx->mm;
1481 struct vm_area_struct *vma, *prev, *cur;
1482 int ret;
1483 struct uffdio_range uffdio_unregister;
1484 unsigned long new_flags;
1485 bool found;
1486 unsigned long start, end, vma_end;
1487 const void __user *buf = (void __user *)arg;
1488
1489 ret = -EFAULT;
1490 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1491 goto out;
1492
Andrey Konovalov7d032572019-09-25 16:48:44 -07001493 ret = validate_range(mm, &uffdio_unregister.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001494 uffdio_unregister.len);
1495 if (ret)
1496 goto out;
1497
1498 start = uffdio_unregister.start;
1499 end = start + uffdio_unregister.len;
1500
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001501 ret = -ENOMEM;
1502 if (!mmget_not_zero(mm))
1503 goto out;
1504
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001505 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001506 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001507 if (!vma)
1508 goto out_unlock;
1509
1510 /* check that there's at least one vma in the range */
1511 ret = -EINVAL;
1512 if (vma->vm_start >= end)
1513 goto out_unlock;
1514
1515 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001516 * If the first vma contains huge pages, make sure start address
1517 * is aligned to huge page size.
1518 */
1519 if (is_vm_hugetlb_page(vma)) {
1520 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1521
1522 if (start & (vma_hpagesize - 1))
1523 goto out_unlock;
1524 }
1525
1526 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001527 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001528 */
1529 found = false;
1530 ret = -EINVAL;
1531 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1532 cond_resched();
1533
1534 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1535 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1536
1537 /*
1538 * Check not compatible vmas, not strictly required
1539 * here as not compatible vmas cannot have an
1540 * userfaultfd_ctx registered on them, but this
1541 * provides for more strict behavior to notice
1542 * unregistration errors.
1543 */
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001544 if (!vma_can_userfault(cur, cur->vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001545 goto out_unlock;
1546
1547 found = true;
1548 }
1549 BUG_ON(!found);
1550
1551 if (vma->vm_start < start)
1552 prev = vma;
1553
1554 ret = 0;
1555 do {
1556 cond_resched();
1557
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001558 BUG_ON(!vma_can_userfault(vma, vma->vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001559
1560 /*
1561 * Nothing to do: this vma is already registered into this
1562 * userfaultfd and with the right tracking mode too.
1563 */
1564 if (!vma->vm_userfaultfd_ctx.ctx)
1565 goto skip;
1566
Andrea Arcangeli01e881f2018-12-14 14:17:17 -08001567 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
1568
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001569 if (vma->vm_start > start)
1570 start = vma->vm_start;
1571 vma_end = min(end, vma->vm_end);
1572
Andrea Arcangeli09fa5292017-02-22 15:42:46 -08001573 if (userfaultfd_missing(vma)) {
1574 /*
1575 * Wake any concurrent pending userfault while
1576 * we unregister, so they will not hang
1577 * permanently and it avoids userland to call
1578 * UFFDIO_WAKE explicitly.
1579 */
1580 struct userfaultfd_wake_range range;
1581 range.start = start;
1582 range.len = vma_end - start;
1583 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1584 }
1585
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001586 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1587 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1588 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1589 vma_policy(vma),
1590 NULL_VM_UFFD_CTX);
1591 if (prev) {
1592 vma = prev;
1593 goto next;
1594 }
1595 if (vma->vm_start < start) {
1596 ret = split_vma(mm, vma, start, 1);
1597 if (ret)
1598 break;
1599 }
1600 if (vma->vm_end > end) {
1601 ret = split_vma(mm, vma, end, 0);
1602 if (ret)
1603 break;
1604 }
1605 next:
1606 /*
1607 * In the vma_merge() successful mprotect-like case 8:
1608 * the next vma was merged into the current one and
1609 * the current one has not been updated yet.
1610 */
1611 vma->vm_flags = new_flags;
1612 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1613
1614 skip:
1615 prev = vma;
1616 start = vma->vm_end;
1617 vma = vma->vm_next;
1618 } while (vma && vma->vm_start < end);
1619out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001620 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001621 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001622out:
1623 return ret;
1624}
1625
1626/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001627 * userfaultfd_wake may be used in combination with the
1628 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001629 */
1630static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1631 unsigned long arg)
1632{
1633 int ret;
1634 struct uffdio_range uffdio_wake;
1635 struct userfaultfd_wake_range range;
1636 const void __user *buf = (void __user *)arg;
1637
1638 ret = -EFAULT;
1639 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1640 goto out;
1641
Andrey Konovalov7d032572019-09-25 16:48:44 -07001642 ret = validate_range(ctx->mm, &uffdio_wake.start, uffdio_wake.len);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001643 if (ret)
1644 goto out;
1645
1646 range.start = uffdio_wake.start;
1647 range.len = uffdio_wake.len;
1648
1649 /*
1650 * len == 0 means wake all and we don't want to wake all here,
1651 * so check it again to be sure.
1652 */
1653 VM_BUG_ON(!range.len);
1654
1655 wake_userfault(ctx, &range);
1656 ret = 0;
1657
1658out:
1659 return ret;
1660}
1661
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001662static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1663 unsigned long arg)
1664{
1665 __s64 ret;
1666 struct uffdio_copy uffdio_copy;
1667 struct uffdio_copy __user *user_uffdio_copy;
1668 struct userfaultfd_wake_range range;
1669
1670 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1671
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001672 ret = -EAGAIN;
1673 if (READ_ONCE(ctx->mmap_changing))
1674 goto out;
1675
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001676 ret = -EFAULT;
1677 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1678 /* don't copy "copy" last field */
1679 sizeof(uffdio_copy)-sizeof(__s64)))
1680 goto out;
1681
Andrey Konovalov7d032572019-09-25 16:48:44 -07001682 ret = validate_range(ctx->mm, &uffdio_copy.dst, uffdio_copy.len);
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001683 if (ret)
1684 goto out;
1685 /*
1686 * double check for wraparound just in case. copy_from_user()
1687 * will later check uffdio_copy.src + uffdio_copy.len to fit
1688 * in the userland range.
1689 */
1690 ret = -EINVAL;
1691 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1692 goto out;
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001693 if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP))
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001694 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001695 if (mmget_not_zero(ctx->mm)) {
1696 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001697 uffdio_copy.len, &ctx->mmap_changing,
1698 uffdio_copy.mode);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001699 mmput(ctx->mm);
Mike Rapoport96333182017-02-24 14:58:31 -08001700 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001701 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001702 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001703 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1704 return -EFAULT;
1705 if (ret < 0)
1706 goto out;
1707 BUG_ON(!ret);
1708 /* len == 0 would wake all */
1709 range.len = ret;
1710 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1711 range.start = uffdio_copy.dst;
1712 wake_userfault(ctx, &range);
1713 }
1714 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1715out:
1716 return ret;
1717}
1718
1719static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1720 unsigned long arg)
1721{
1722 __s64 ret;
1723 struct uffdio_zeropage uffdio_zeropage;
1724 struct uffdio_zeropage __user *user_uffdio_zeropage;
1725 struct userfaultfd_wake_range range;
1726
1727 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1728
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001729 ret = -EAGAIN;
1730 if (READ_ONCE(ctx->mmap_changing))
1731 goto out;
1732
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001733 ret = -EFAULT;
1734 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1735 /* don't copy "zeropage" last field */
1736 sizeof(uffdio_zeropage)-sizeof(__s64)))
1737 goto out;
1738
Andrey Konovalov7d032572019-09-25 16:48:44 -07001739 ret = validate_range(ctx->mm, &uffdio_zeropage.range.start,
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001740 uffdio_zeropage.range.len);
1741 if (ret)
1742 goto out;
1743 ret = -EINVAL;
1744 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1745 goto out;
1746
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001747 if (mmget_not_zero(ctx->mm)) {
1748 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001749 uffdio_zeropage.range.len,
1750 &ctx->mmap_changing);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001751 mmput(ctx->mm);
Mike Rapoport9d95aa42017-08-02 13:32:15 -07001752 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001753 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001754 }
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001755 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1756 return -EFAULT;
1757 if (ret < 0)
1758 goto out;
1759 /* len == 0 would wake all */
1760 BUG_ON(!ret);
1761 range.len = ret;
1762 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1763 range.start = uffdio_zeropage.range.start;
1764 wake_userfault(ctx, &range);
1765 }
1766 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1767out:
1768 return ret;
1769}
1770
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001771static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,
1772 unsigned long arg)
1773{
1774 int ret;
1775 struct uffdio_writeprotect uffdio_wp;
1776 struct uffdio_writeprotect __user *user_uffdio_wp;
1777 struct userfaultfd_wake_range range;
Peter Xu23080e22020-04-06 20:06:20 -07001778 bool mode_wp, mode_dontwake;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001779
1780 if (READ_ONCE(ctx->mmap_changing))
1781 return -EAGAIN;
1782
1783 user_uffdio_wp = (struct uffdio_writeprotect __user *) arg;
1784
1785 if (copy_from_user(&uffdio_wp, user_uffdio_wp,
1786 sizeof(struct uffdio_writeprotect)))
1787 return -EFAULT;
1788
1789 ret = validate_range(ctx->mm, &uffdio_wp.range.start,
1790 uffdio_wp.range.len);
1791 if (ret)
1792 return ret;
1793
1794 if (uffdio_wp.mode & ~(UFFDIO_WRITEPROTECT_MODE_DONTWAKE |
1795 UFFDIO_WRITEPROTECT_MODE_WP))
1796 return -EINVAL;
Peter Xu23080e22020-04-06 20:06:20 -07001797
1798 mode_wp = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_WP;
1799 mode_dontwake = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_DONTWAKE;
1800
1801 if (mode_wp && mode_dontwake)
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001802 return -EINVAL;
1803
1804 ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
Peter Xu23080e22020-04-06 20:06:20 -07001805 uffdio_wp.range.len, mode_wp,
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001806 &ctx->mmap_changing);
1807 if (ret)
1808 return ret;
1809
Peter Xu23080e22020-04-06 20:06:20 -07001810 if (!mode_wp && !mode_dontwake) {
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001811 range.start = uffdio_wp.range.start;
1812 range.len = uffdio_wp.range.len;
1813 wake_userfault(ctx, &range);
1814 }
1815 return ret;
1816}
1817
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001818static inline unsigned int uffd_ctx_features(__u64 user_features)
1819{
1820 /*
1821 * For the current set of features the bits just coincide
1822 */
1823 return (unsigned int)user_features;
1824}
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;
1836 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001837 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001838
1839 ret = -EINVAL;
1840 if (ctx->state != UFFD_STATE_WAIT_API)
1841 goto out;
1842 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001843 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001844 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001845 features = uffdio_api.features;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001846 ret = -EINVAL;
1847 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES))
1848 goto err_out;
1849 ret = -EPERM;
1850 if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE))
1851 goto err_out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001852 /* report all available features and ioctls to userland */
1853 uffdio_api.features = UFFD_API_FEATURES;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001854 uffdio_api.ioctls = UFFD_API_IOCTLS;
1855 ret = -EFAULT;
1856 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1857 goto out;
1858 ctx->state = UFFD_STATE_RUNNING;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001859 /* only enable the requested features for this uffd context */
1860 ctx->features = uffd_ctx_features(features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001861 ret = 0;
1862out:
1863 return ret;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001864err_out:
1865 memset(&uffdio_api, 0, sizeof(uffdio_api));
1866 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1867 ret = -EFAULT;
1868 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001869}
1870
1871static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1872 unsigned long arg)
1873{
1874 int ret = -EINVAL;
1875 struct userfaultfd_ctx *ctx = file->private_data;
1876
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001877 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
1878 return -EINVAL;
1879
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001880 switch(cmd) {
1881 case UFFDIO_API:
1882 ret = userfaultfd_api(ctx, arg);
1883 break;
1884 case UFFDIO_REGISTER:
1885 ret = userfaultfd_register(ctx, arg);
1886 break;
1887 case UFFDIO_UNREGISTER:
1888 ret = userfaultfd_unregister(ctx, arg);
1889 break;
1890 case UFFDIO_WAKE:
1891 ret = userfaultfd_wake(ctx, arg);
1892 break;
Andrea Arcangeliad465ca2015-09-04 15:47:11 -07001893 case UFFDIO_COPY:
1894 ret = userfaultfd_copy(ctx, arg);
1895 break;
1896 case UFFDIO_ZEROPAGE:
1897 ret = userfaultfd_zeropage(ctx, arg);
1898 break;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001899 case UFFDIO_WRITEPROTECT:
1900 ret = userfaultfd_writeprotect(ctx, arg);
1901 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001902 }
1903 return ret;
1904}
1905
1906#ifdef CONFIG_PROC_FS
1907static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
1908{
1909 struct userfaultfd_ctx *ctx = f->private_data;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001910 wait_queue_entry_t *wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001911 unsigned long pending = 0, total = 0;
1912
Eric Biggerscbcfa132019-07-04 15:14:39 -07001913 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Ingo Molnar2055da92017-06-20 12:06:46 +02001914 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001915 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001916 total++;
1917 }
Ingo Molnar2055da92017-06-20 12:06:46 +02001918 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001919 total++;
1920 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07001921 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001922
1923 /*
1924 * If more protocols will be added, there will be all shown
1925 * separated by a space. Like this:
1926 * protocols: aa:... bb:...
1927 */
1928 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Mike Rapoport045098e2017-04-07 16:04:42 -07001929 pending, total, UFFD_API, ctx->features,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001930 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
1931}
1932#endif
1933
1934static const struct file_operations userfaultfd_fops = {
1935#ifdef CONFIG_PROC_FS
1936 .show_fdinfo = userfaultfd_show_fdinfo,
1937#endif
1938 .release = userfaultfd_release,
1939 .poll = userfaultfd_poll,
1940 .read = userfaultfd_read,
1941 .unlocked_ioctl = userfaultfd_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +02001942 .compat_ioctl = compat_ptr_ioctl,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001943 .llseek = noop_llseek,
1944};
1945
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001946static void init_once_userfaultfd_ctx(void *mem)
1947{
1948 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
1949
1950 init_waitqueue_head(&ctx->fault_pending_wqh);
1951 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001952 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001953 init_waitqueue_head(&ctx->fd_wqh);
Ahmed S. Darwish2ca97ac2020-07-20 17:55:28 +02001954 seqcount_spinlock_init(&ctx->refile_seq, &ctx->fault_pending_wqh.lock);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001955}
1956
Eric Biggers284cd242018-01-31 16:19:48 -08001957SYSCALL_DEFINE1(userfaultfd, int, flags)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001958{
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001959 struct userfaultfd_ctx *ctx;
Eric Biggers284cd242018-01-31 16:19:48 -08001960 int fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001961
Peter Xucefdca02019-05-13 17:16:41 -07001962 if (!sysctl_unprivileged_userfaultfd && !capable(CAP_SYS_PTRACE))
1963 return -EPERM;
1964
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001965 BUG_ON(!current->mm);
1966
1967 /* Check the UFFD_* constants for consistency. */
1968 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
1969 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
1970
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001971 if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
Eric Biggers284cd242018-01-31 16:19:48 -08001972 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001973
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001974 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001975 if (!ctx)
Eric Biggers284cd242018-01-31 16:19:48 -08001976 return -ENOMEM;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001977
Eric Biggersca880422018-12-28 00:34:43 -08001978 refcount_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001979 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001980 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001981 ctx->state = UFFD_STATE_WAIT_API;
1982 ctx->released = false;
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001983 ctx->mmap_changing = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001984 ctx->mm = current->mm;
1985 /* prevent the mm struct to be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001986 mmgrab(ctx->mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001987
Eric Biggers284cd242018-01-31 16:19:48 -08001988 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
1989 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1990 if (fd < 0) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001991 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001992 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07001993 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001994 return fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001995}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07001996
1997static int __init userfaultfd_init(void)
1998{
1999 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
2000 sizeof(struct userfaultfd_ctx),
2001 0,
2002 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2003 init_once_userfaultfd_ctx);
2004 return 0;
2005}
2006__initcall(userfaultfd_init);