blob: 01cdd2c57b655a5556df523f490b829005343223 [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>
Peter Xu343cacf2021-03-18 17:01:47 +110018#include <linux/mmu_notifier.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070019#include <linux/poll.h>
20#include <linux/slab.h>
21#include <linux/seq_file.h>
22#include <linux/file.h>
23#include <linux/bug.h>
24#include <linux/anon_inodes.h>
25#include <linux/syscalls.h>
26#include <linux/userfaultfd_k.h>
27#include <linux/mempolicy.h>
28#include <linux/ioctl.h>
29#include <linux/security.h>
Mike Kravetzcab350a2017-02-22 15:43:04 -080030#include <linux/hugetlb.h>
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070031
Lokesh Gidra6a6bc062020-12-14 19:13:54 -080032int sysctl_unprivileged_userfaultfd __read_mostly;
Peter Xucefdca02019-05-13 17:16:41 -070033
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070034static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
35
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070036/*
37 * Start with fault_pending_wqh and fault_wqh so they're more likely
38 * to be in the same cacheline.
Eric Biggerscbcfa132019-07-04 15:14:39 -070039 *
40 * Locking order:
41 * fd_wqh.lock
42 * fault_pending_wqh.lock
43 * fault_wqh.lock
44 * event_wqh.lock
45 *
46 * To avoid deadlocks, IRQs must be disabled when taking any of the above locks,
47 * since fd_wqh.lock is taken by aio_poll() while it's holding a lock that's
48 * also taken in IRQ context.
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070049 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070050struct userfaultfd_ctx {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -070051 /* waitqueue head for the pending (i.e. not read) userfaults */
52 wait_queue_head_t fault_pending_wqh;
53 /* waitqueue head for the userfaults */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070054 wait_queue_head_t fault_wqh;
55 /* waitqueue head for the pseudo fd to wakeup poll/read */
56 wait_queue_head_t fd_wqh;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080057 /* waitqueue head for events */
58 wait_queue_head_t event_wqh;
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -070059 /* a refile sequence protected by fault_pending_wqh lock */
Ahmed S. Darwish2ca97ac2020-07-20 17:55:28 +020060 seqcount_spinlock_t refile_seq;
Andrea Arcangeli3004ec92015-09-04 15:46:48 -070061 /* pseudo fd refcounting */
Eric Biggersca880422018-12-28 00:34:43 -080062 refcount_t refcount;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070063 /* userfaultfd syscall flags */
64 unsigned int flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -080065 /* features requested from the userspace */
66 unsigned int features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070067 /* released */
68 bool released;
Mike Rapoportdf2cc962018-06-07 17:09:25 -070069 /* memory mappings are changing because of non-cooperative event */
70 bool mmap_changing;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070071 /* mm with one ore more vmas attached to this userfaultfd_ctx */
72 struct mm_struct *mm;
73};
74
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080075struct userfaultfd_fork_ctx {
76 struct userfaultfd_ctx *orig;
77 struct userfaultfd_ctx *new;
78 struct list_head list;
79};
80
Mike Rapoport897ab3e2017-02-24 14:58:22 -080081struct userfaultfd_unmap_ctx {
82 struct userfaultfd_ctx *ctx;
83 unsigned long start;
84 unsigned long end;
85 struct list_head list;
86};
87
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070088struct userfaultfd_wait_queue {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070089 struct uffd_msg msg;
Ingo Molnarac6424b2017-06-20 12:06:13 +020090 wait_queue_entry_t wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070091 struct userfaultfd_ctx *ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -080092 bool waken;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -070093};
94
95struct userfaultfd_wake_range {
96 unsigned long start;
97 unsigned long len;
98};
99
Nadav Amit6afd1e02021-09-02 14:58:59 -0700100/* internal indication that UFFD_API ioctl was successfully executed */
101#define UFFD_FEATURE_INITIALIZED (1u << 31)
102
103static bool userfaultfd_is_initialized(struct userfaultfd_ctx *ctx)
104{
105 return ctx->features & UFFD_FEATURE_INITIALIZED;
106}
107
Ingo Molnarac6424b2017-06-20 12:06:13 +0200108static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700109 int wake_flags, void *key)
110{
111 struct userfaultfd_wake_range *range = key;
112 int ret;
113 struct userfaultfd_wait_queue *uwq;
114 unsigned long start, len;
115
116 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
117 ret = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700118 /* len == 0 means wake all */
119 start = range->start;
120 len = range->len;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700121 if (len && (start > uwq->msg.arg.pagefault.address ||
122 start + len <= uwq->msg.arg.pagefault.address))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700123 goto out;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800124 WRITE_ONCE(uwq->waken, true);
125 /*
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200126 * The Program-Order guarantees provided by the scheduler
127 * ensure uwq->waken is visible before the task is woken.
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800128 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700129 ret = wake_up_state(wq->private, mode);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200130 if (ret) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700131 /*
132 * Wake only once, autoremove behavior.
133 *
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200134 * After the effect of list_del_init is visible to the other
135 * CPUs, the waitqueue may disappear from under us, see the
136 * !list_empty_careful() in handle_userfault().
137 *
138 * try_to_wake_up() has an implicit smp_mb(), and the
139 * wq->private is read before calling the extern function
140 * "wake_up_state" (which in turns calls try_to_wake_up).
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700141 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200142 list_del_init(&wq->entry);
Peter Zijlstraa9668cd2017-06-07 17:51:27 +0200143 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700144out:
145 return ret;
146}
147
148/**
149 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
150 * context.
151 * @ctx: [in] Pointer to the userfaultfd context.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700152 */
153static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
154{
Eric Biggersca880422018-12-28 00:34:43 -0800155 refcount_inc(&ctx->refcount);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700156}
157
158/**
159 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
160 * context.
161 * @ctx: [in] Pointer to userfaultfd context.
162 *
163 * The userfaultfd context reference must have been previously acquired either
164 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
165 */
166static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
167{
Eric Biggersca880422018-12-28 00:34:43 -0800168 if (refcount_dec_and_test(&ctx->refcount)) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700169 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
170 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
171 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
172 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800173 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
174 VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700175 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
176 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700177 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -0700178 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700179 }
180}
181
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700182static inline void msg_init(struct uffd_msg *msg)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700183{
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700184 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
185 /*
186 * Must use memset to zero out the paddings or kernel data is
187 * leaked to userland.
188 */
189 memset(msg, 0, sizeof(struct uffd_msg));
190}
191
192static inline struct uffd_msg userfault_msg(unsigned long address,
193 unsigned int flags,
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700194 unsigned long reason,
195 unsigned int features)
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700196{
197 struct uffd_msg msg;
198 msg_init(&msg);
199 msg.event = UFFD_EVENT_PAGEFAULT;
200 msg.arg.pagefault.address = address;
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100201 /*
202 * These flags indicate why the userfault occurred:
203 * - UFFD_PAGEFAULT_FLAG_WP indicates a write protect fault.
204 * - UFFD_PAGEFAULT_FLAG_MINOR indicates a minor fault.
205 * - Neither of these flags being set indicates a MISSING fault.
206 *
207 * Separately, UFFD_PAGEFAULT_FLAG_WRITE indicates it was a write
208 * fault. Otherwise, it was a read fault.
209 */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700210 if (flags & FAULT_FLAG_WRITE)
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700211 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700212 if (reason & VM_UFFD_WP)
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700213 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100214 if (reason & VM_UFFD_MINOR)
215 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_MINOR;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700216 if (features & UFFD_FEATURE_THREAD_ID)
Andrea Arcangelia36985d2017-09-06 16:23:59 -0700217 msg.arg.pagefault.feat.ptid = task_pid_vnr(current);
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700218 return msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700219}
220
Mike Kravetz369cd2122017-02-22 15:43:10 -0800221#ifdef CONFIG_HUGETLB_PAGE
222/*
223 * Same functionality as userfaultfd_must_wait below with modifications for
224 * hugepmd ranges.
225 */
226static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700227 struct vm_area_struct *vma,
Mike Kravetz369cd2122017-02-22 15:43:10 -0800228 unsigned long address,
229 unsigned long flags,
230 unsigned long reason)
231{
232 struct mm_struct *mm = ctx->mm;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700233 pte_t *ptep, pte;
Mike Kravetz369cd2122017-02-22 15:43:10 -0800234 bool ret = true;
235
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700236 mmap_assert_locked(mm);
Mike Kravetz369cd2122017-02-22 15:43:10 -0800237
Janosch Frank1e2c0432018-07-03 17:02:39 -0700238 ptep = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
239
240 if (!ptep)
Mike Kravetz369cd2122017-02-22 15:43:10 -0800241 goto out;
242
243 ret = false;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700244 pte = huge_ptep_get(ptep);
Mike Kravetz369cd2122017-02-22 15:43:10 -0800245
246 /*
247 * Lockless access: we're in a wait_event so it's ok if it
248 * changes under us.
249 */
Janosch Frank1e2c0432018-07-03 17:02:39 -0700250 if (huge_pte_none(pte))
Mike Kravetz369cd2122017-02-22 15:43:10 -0800251 ret = true;
Janosch Frank1e2c0432018-07-03 17:02:39 -0700252 if (!huge_pte_write(pte) && (reason & VM_UFFD_WP))
Mike Kravetz369cd2122017-02-22 15:43:10 -0800253 ret = true;
254out:
255 return ret;
256}
257#else
258static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
Punit Agrawal7868a202017-07-06 15:39:42 -0700259 struct vm_area_struct *vma,
Mike Kravetz369cd2122017-02-22 15:43:10 -0800260 unsigned long address,
261 unsigned long flags,
262 unsigned long reason)
263{
264 return false; /* should never get here */
265}
266#endif /* CONFIG_HUGETLB_PAGE */
267
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700268/*
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700269 * Verify the pagetables are still not ok after having reigstered into
270 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
271 * userfault that has already been resolved, if userfaultfd_read and
272 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
273 * threads.
274 */
275static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
276 unsigned long address,
277 unsigned long flags,
278 unsigned long reason)
279{
280 struct mm_struct *mm = ctx->mm;
281 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300282 p4d_t *p4d;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700283 pud_t *pud;
284 pmd_t *pmd, _pmd;
285 pte_t *pte;
286 bool ret = true;
287
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700288 mmap_assert_locked(mm);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700289
290 pgd = pgd_offset(mm, address);
291 if (!pgd_present(*pgd))
292 goto out;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300293 p4d = p4d_offset(pgd, address);
294 if (!p4d_present(*p4d))
295 goto out;
296 pud = pud_offset(p4d, address);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700297 if (!pud_present(*pud))
298 goto out;
299 pmd = pmd_offset(pud, address);
300 /*
301 * READ_ONCE must function as a barrier with narrower scope
302 * and it must be equivalent to:
303 * _pmd = *pmd; barrier();
304 *
305 * This is to deal with the instability (as in
306 * pmd_trans_unstable) of the pmd.
307 */
308 _pmd = READ_ONCE(*pmd);
Huang Yinga365ac02018-01-31 16:17:32 -0800309 if (pmd_none(_pmd))
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700310 goto out;
311
312 ret = false;
Huang Yinga365ac02018-01-31 16:17:32 -0800313 if (!pmd_present(_pmd))
314 goto out;
315
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700316 if (pmd_trans_huge(_pmd)) {
317 if (!pmd_write(_pmd) && (reason & VM_UFFD_WP))
318 ret = true;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700319 goto out;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700320 }
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700321
322 /*
323 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
324 * and use the standard pte_offset_map() instead of parsing _pmd.
325 */
326 pte = pte_offset_map(pmd, address);
327 /*
328 * Lockless access: we're in a wait_event so it's ok if it
329 * changes under us.
330 */
331 if (pte_none(*pte))
332 ret = true;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -0700333 if (!pte_write(*pte) && (reason & VM_UFFD_WP))
334 ret = true;
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700335 pte_unmap(pte);
336
337out:
338 return ret;
339}
340
Peter Xu3e69ad02020-04-01 21:09:00 -0700341static inline long userfaultfd_get_blocking_state(unsigned int flags)
342{
343 if (flags & FAULT_FLAG_INTERRUPTIBLE)
344 return TASK_INTERRUPTIBLE;
345
346 if (flags & FAULT_FLAG_KILLABLE)
347 return TASK_KILLABLE;
348
349 return TASK_UNINTERRUPTIBLE;
350}
351
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700352/*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700353 * The locking rules involved in returning VM_FAULT_RETRY depending on
354 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
355 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
356 * recommendation in __lock_page_or_retry is not an understatement.
357 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700358 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_lock must be released
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700359 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
360 * not set.
361 *
362 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
363 * set, VM_FAULT_RETRY can still be returned if and only if there are
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700364 * fatal_signal_pending()s, and the mmap_lock must be released before
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700365 * returning it.
366 */
Souptick Joarder2b740302018-08-23 17:01:36 -0700367vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700368{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800369 struct mm_struct *mm = vmf->vma->vm_mm;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700370 struct userfaultfd_ctx *ctx;
371 struct userfaultfd_wait_queue uwq;
Souptick Joarder2b740302018-08-23 17:01:36 -0700372 vm_fault_t ret = VM_FAULT_SIGBUS;
Peter Xu3e69ad02020-04-01 21:09:00 -0700373 bool must_wait;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800374 long blocking_state;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700375
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700376 /*
377 * We don't do userfault handling for the final child pid update.
378 *
379 * We also don't do userfault handling during
380 * coredumping. hugetlbfs has the special
381 * follow_hugetlb_page() to skip missing pages in the
382 * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
383 * the no_page_table() helper in follow_page_mask(), but the
384 * shmem_vm_ops->fault method is invoked even during
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700385 * coredumping without mmap_lock and it ends up here.
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700386 */
387 if (current->flags & (PF_EXITING|PF_DUMPCORE))
388 goto out;
389
390 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700391 * Coredumping runs without mmap_lock so we can only check that
392 * the mmap_lock is held, if PF_DUMPCORE was not set.
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700393 */
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700394 mmap_assert_locked(mm);
Andrea Arcangeli64c2b202017-06-16 14:02:37 -0700395
Jan Kara82b0f8c2016-12-14 15:06:58 -0800396 ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700397 if (!ctx)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700398 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700399
400 BUG_ON(ctx->mm != mm);
401
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100402 /* Any unrecognized flag is a bug. */
403 VM_BUG_ON(reason & ~__VM_UFFD_FLAGS);
404 /* 0 or > 1 flags set is a bug; we expect exactly 1. */
405 VM_BUG_ON(!reason || (reason & (reason - 1)));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700406
Prakash Sangappa2d6d6f52017-09-06 16:23:39 -0700407 if (ctx->features & UFFD_FEATURE_SIGBUS)
408 goto out;
Lokesh Gidrab8af1f92020-12-14 19:13:49 -0800409 if ((vmf->flags & FAULT_FLAG_USER) == 0 &&
410 ctx->flags & UFFD_USER_MODE_ONLY) {
411 printk_once(KERN_WARNING "uffd: Set unprivileged_userfaultfd "
412 "sysctl knob to 1 if kernel faults must be handled "
413 "without obtaining CAP_SYS_PTRACE capability\n");
414 goto out;
415 }
Prakash Sangappa2d6d6f52017-09-06 16:23:39 -0700416
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700417 /*
418 * If it's already released don't get it. This avoids to loop
419 * in __get_user_pages if userfaultfd_release waits on the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700420 * caller of handle_userfault to release the mmap_lock.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700421 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700422 if (unlikely(READ_ONCE(ctx->released))) {
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700423 /*
424 * Don't return VM_FAULT_SIGBUS in this case, so a non
425 * cooperative manager can close the uffd after the
426 * last UFFDIO_COPY, without risking to trigger an
427 * involuntary SIGBUS if the process was starting the
428 * userfaultfd while the userfaultfd was still armed
429 * (but after the last UFFDIO_COPY). If the uffd
430 * wasn't already closed when the userfault reached
431 * this point, that would normally be solved by
432 * userfaultfd_must_wait returning 'false'.
433 *
434 * If we were to return VM_FAULT_SIGBUS here, the non
435 * cooperative manager would be instead forced to
436 * always call UFFDIO_UNREGISTER before it can safely
437 * close the uffd.
438 */
439 ret = VM_FAULT_NOPAGE;
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700440 goto out;
Andrea Arcangeli656710a2017-09-08 16:12:42 -0700441 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700442
443 /*
444 * Check that we can return VM_FAULT_RETRY.
445 *
446 * NOTE: it should become possible to return VM_FAULT_RETRY
447 * even if FAULT_FLAG_TRIED is set without leading to gup()
448 * -EBUSY failures, if the userfaultfd is to be extended for
449 * VM_UFFD_WP tracking and we intend to arm the userfault
450 * without first stopping userland access to the memory. For
451 * VM_UFFD_MISSING userfaults this is enough for now.
452 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800453 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700454 /*
455 * Validate the invariant that nowait must allow retry
456 * to be sure not to return SIGBUS erroneously on
457 * nowait invocations.
458 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800459 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700460#ifdef CONFIG_DEBUG_VM
461 if (printk_ratelimit()) {
462 printk(KERN_WARNING
Jan Kara82b0f8c2016-12-14 15:06:58 -0800463 "FAULT_FLAG_ALLOW_RETRY missing %x\n",
464 vmf->flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700465 dump_stack();
466 }
467#endif
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700468 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700469 }
470
471 /*
472 * Handle nowait, not much to do other than tell it to retry
473 * and wait.
474 */
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700475 ret = VM_FAULT_RETRY;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800476 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700477 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700478
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700479 /* take the reference before dropping the mmap_lock */
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700480 userfaultfd_ctx_get(ctx);
481
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700482 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
483 uwq.wq.private = current;
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700484 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason,
485 ctx->features);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700486 uwq.ctx = ctx;
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800487 uwq.waken = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700488
Peter Xu3e69ad02020-04-01 21:09:00 -0700489 blocking_state = userfaultfd_get_blocking_state(vmf->flags);
Andrea Arcangelidfa37dc2015-09-04 15:47:18 -0700490
Eric Biggerscbcfa132019-07-04 15:14:39 -0700491 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700492 /*
493 * After the __add_wait_queue the uwq is visible to userland
494 * through poll/read().
495 */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700496 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
497 /*
498 * The smp_mb() after __set_current_state prevents the reads
499 * following the spin_unlock to happen before the list_add in
500 * __add_wait_queue.
501 */
Andrea Arcangeli15a77c62017-01-24 15:17:59 -0800502 set_current_state(blocking_state);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700503 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700504
Mike Kravetz369cd2122017-02-22 15:43:10 -0800505 if (!is_vm_hugetlb_page(vmf->vma))
506 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
507 reason);
508 else
Punit Agrawal7868a202017-07-06 15:39:42 -0700509 must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma,
510 vmf->address,
Mike Kravetz369cd2122017-02-22 15:43:10 -0800511 vmf->flags, reason);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700512 mmap_read_unlock(mm);
Andrea Arcangeli8d2afd92015-09-04 15:46:51 -0700513
Linus Torvaldsf9bf3522020-08-02 10:42:31 -0700514 if (likely(must_wait && !READ_ONCE(ctx->released))) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800515 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700516 schedule();
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700517 }
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700518
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700519 __set_current_state(TASK_RUNNING);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700520
521 /*
522 * Here we race with the list_del; list_add in
523 * userfaultfd_ctx_read(), however because we don't ever run
524 * list_del_init() to refile across the two lists, the prev
525 * and next pointers will never point to self. list_add also
526 * would never let any of the two pointers to point to
527 * self. So list_empty_careful won't risk to see both pointers
528 * pointing to self at any time during the list refile. The
529 * only case where list_del_init() is called is the full
530 * removal in the wake function and there we don't re-list_add
531 * and it's fine not to block on the spinlock. The uwq on this
532 * kernel stack can be released after the list_del_init.
533 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200534 if (!list_empty_careful(&uwq.wq.entry)) {
Eric Biggerscbcfa132019-07-04 15:14:39 -0700535 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700536 /*
537 * No need of list_del_init(), the uwq on the stack
538 * will be freed shortly anyway.
539 */
Ingo Molnar2055da92017-06-20 12:06:46 +0200540 list_del(&uwq.wq.entry);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700541 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700542 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700543
544 /*
545 * ctx may go away after this if the userfault pseudo fd is
546 * already released.
547 */
548 userfaultfd_ctx_put(ctx);
549
Andrea Arcangeliba85c702015-09-04 15:46:41 -0700550out:
551 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700552}
553
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800554static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
555 struct userfaultfd_wait_queue *ewq)
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800556{
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800557 struct userfaultfd_ctx *release_new_ctx;
558
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800559 if (WARN_ON_ONCE(current->flags & PF_EXITING))
560 goto out;
561
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800562 ewq->ctx = ctx;
563 init_waitqueue_entry(&ewq->wq, current);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800564 release_new_ctx = NULL;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800565
Eric Biggerscbcfa132019-07-04 15:14:39 -0700566 spin_lock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800567 /*
568 * After the __add_wait_queue the uwq is visible to userland
569 * through poll/read().
570 */
571 __add_wait_queue(&ctx->event_wqh, &ewq->wq);
572 for (;;) {
573 set_current_state(TASK_KILLABLE);
574 if (ewq->msg.event == 0)
575 break;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700576 if (READ_ONCE(ctx->released) ||
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800577 fatal_signal_pending(current)) {
Andrea Arcangeli384632e2017-10-03 16:15:38 -0700578 /*
579 * &ewq->wq may be queued in fork_event, but
580 * __remove_wait_queue ignores the head
581 * parameter. It would be a problem if it
582 * didn't.
583 */
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800584 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800585 if (ewq->msg.event == UFFD_EVENT_FORK) {
586 struct userfaultfd_ctx *new;
587
588 new = (struct userfaultfd_ctx *)
589 (unsigned long)
590 ewq->msg.arg.reserved.reserved1;
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800591 release_new_ctx = new;
Mike Rapoport7eb76d42017-03-09 16:17:09 -0800592 }
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800593 break;
594 }
595
Eric Biggerscbcfa132019-07-04 15:14:39 -0700596 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800597
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800598 wake_up_poll(&ctx->fd_wqh, EPOLLIN);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800599 schedule();
600
Eric Biggerscbcfa132019-07-04 15:14:39 -0700601 spin_lock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800602 }
603 __set_current_state(TASK_RUNNING);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700604 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800605
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800606 if (release_new_ctx) {
607 struct vm_area_struct *vma;
608 struct mm_struct *mm = release_new_ctx->mm;
609
610 /* the various vma->vm_userfaultfd_ctx still points to it */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700611 mmap_write_lock(mm);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800612 for (vma = mm->mmap; vma; vma = vma->vm_next)
Mike Rapoport31e810a2018-08-02 15:36:09 -0700613 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800614 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100615 vma->vm_flags &= ~__VM_UFFD_FLAGS;
Mike Rapoport31e810a2018-08-02 15:36:09 -0700616 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700617 mmap_write_unlock(mm);
Andrea Arcangeli0cbb4b42018-01-04 16:18:09 -0800618
619 userfaultfd_ctx_put(release_new_ctx);
620 }
621
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800622 /*
623 * ctx may go away after this if the userfault pseudo fd is
624 * already released.
625 */
Andrea Arcangeli9a69a822017-03-09 16:16:52 -0800626out:
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700627 WRITE_ONCE(ctx->mmap_changing, false);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800628 userfaultfd_ctx_put(ctx);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800629}
630
631static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
632 struct userfaultfd_wait_queue *ewq)
633{
634 ewq->msg.event = 0;
635 wake_up_locked(&ctx->event_wqh);
636 __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
637}
638
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800639int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
640{
641 struct userfaultfd_ctx *ctx = NULL, *octx;
642 struct userfaultfd_fork_ctx *fctx;
643
644 octx = vma->vm_userfaultfd_ctx.ctx;
645 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200646 vm_write_begin(vma);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800647 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200648 WRITE_ONCE(vma->vm_flags,
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100649 vma->vm_flags & ~__VM_UFFD_FLAGS);
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200650 vm_write_end(vma);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800651 return 0;
652 }
653
654 list_for_each_entry(fctx, fcs, list)
655 if (fctx->orig == octx) {
656 ctx = fctx->new;
657 break;
658 }
659
660 if (!ctx) {
661 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
662 if (!fctx)
663 return -ENOMEM;
664
665 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
666 if (!ctx) {
667 kfree(fctx);
668 return -ENOMEM;
669 }
670
Eric Biggersca880422018-12-28 00:34:43 -0800671 refcount_set(&ctx->refcount, 1);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800672 ctx->flags = octx->flags;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800673 ctx->features = octx->features;
674 ctx->released = false;
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700675 ctx->mmap_changing = false;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800676 ctx->mm = vma->vm_mm;
Mike Rapoport00bb31f2017-11-15 17:36:56 -0800677 mmgrab(ctx->mm);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800678
679 userfaultfd_ctx_get(octx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700680 WRITE_ONCE(octx->mmap_changing, true);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800681 fctx->orig = octx;
682 fctx->new = ctx;
683 list_add_tail(&fctx->list, fcs);
684 }
685
686 vma->vm_userfaultfd_ctx.ctx = ctx;
687 return 0;
688}
689
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800690static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800691{
692 struct userfaultfd_ctx *ctx = fctx->orig;
693 struct userfaultfd_wait_queue ewq;
694
695 msg_init(&ewq.msg);
696
697 ewq.msg.event = UFFD_EVENT_FORK;
698 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
699
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800700 userfaultfd_event_wait_completion(ctx, &ewq);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800701}
702
703void dup_userfaultfd_complete(struct list_head *fcs)
704{
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800705 struct userfaultfd_fork_ctx *fctx, *n;
706
707 list_for_each_entry_safe(fctx, n, fcs, list) {
Andrea Arcangeli8c9e7bb2017-03-09 16:16:54 -0800708 dup_fctx(fctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800709 list_del(&fctx->list);
710 kfree(fctx);
711 }
712}
713
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800714void mremap_userfaultfd_prep(struct vm_area_struct *vma,
715 struct vm_userfaultfd_ctx *vm_ctx)
716{
717 struct userfaultfd_ctx *ctx;
718
719 ctx = vma->vm_userfaultfd_ctx.ctx;
Peter Xu3cfd22b2018-12-28 00:38:47 -0800720
721 if (!ctx)
722 return;
723
724 if (ctx->features & UFFD_FEATURE_EVENT_REMAP) {
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800725 vm_ctx->ctx = ctx;
726 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700727 WRITE_ONCE(ctx->mmap_changing, true);
Peter Xu3cfd22b2018-12-28 00:38:47 -0800728 } else {
729 /* Drop uffd context if remap feature not enabled */
730 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100731 vma->vm_flags &= ~__VM_UFFD_FLAGS;
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800732 }
733}
734
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800735void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800736 unsigned long from, unsigned long to,
737 unsigned long len)
738{
Andrea Arcangeli90794bf2017-02-22 15:42:37 -0800739 struct userfaultfd_ctx *ctx = vm_ctx->ctx;
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800740 struct userfaultfd_wait_queue ewq;
741
742 if (!ctx)
743 return;
744
745 if (to & ~PAGE_MASK) {
746 userfaultfd_ctx_put(ctx);
747 return;
748 }
749
750 msg_init(&ewq.msg);
751
752 ewq.msg.event = UFFD_EVENT_REMAP;
753 ewq.msg.arg.remap.from = from;
754 ewq.msg.arg.remap.to = to;
755 ewq.msg.arg.remap.len = len;
756
757 userfaultfd_event_wait_completion(ctx, &ewq);
758}
759
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800760bool userfaultfd_remove(struct vm_area_struct *vma,
Mike Rapoportd8119142017-02-24 14:56:02 -0800761 unsigned long start, unsigned long end)
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800762{
763 struct mm_struct *mm = vma->vm_mm;
764 struct userfaultfd_ctx *ctx;
765 struct userfaultfd_wait_queue ewq;
766
767 ctx = vma->vm_userfaultfd_ctx.ctx;
Mike Rapoportd8119142017-02-24 14:56:02 -0800768 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800769 return true;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800770
771 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700772 WRITE_ONCE(ctx->mmap_changing, true);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700773 mmap_read_unlock(mm);
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800774
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800775 msg_init(&ewq.msg);
776
Mike Rapoportd8119142017-02-24 14:56:02 -0800777 ewq.msg.event = UFFD_EVENT_REMOVE;
778 ewq.msg.arg.remove.start = start;
779 ewq.msg.arg.remove.end = end;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800780
781 userfaultfd_event_wait_completion(ctx, &ewq);
782
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800783 return false;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800784}
785
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800786static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
787 unsigned long start, unsigned long end)
788{
789 struct userfaultfd_unmap_ctx *unmap_ctx;
790
791 list_for_each_entry(unmap_ctx, unmaps, list)
792 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
793 unmap_ctx->end == end)
794 return true;
795
796 return false;
797}
798
799int userfaultfd_unmap_prep(struct vm_area_struct *vma,
800 unsigned long start, unsigned long end,
801 struct list_head *unmaps)
802{
803 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
804 struct userfaultfd_unmap_ctx *unmap_ctx;
805 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
806
807 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
808 has_unmap_ctx(ctx, unmaps, start, end))
809 continue;
810
811 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
812 if (!unmap_ctx)
813 return -ENOMEM;
814
815 userfaultfd_ctx_get(ctx);
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700816 WRITE_ONCE(ctx->mmap_changing, true);
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800817 unmap_ctx->ctx = ctx;
818 unmap_ctx->start = start;
819 unmap_ctx->end = end;
820 list_add_tail(&unmap_ctx->list, unmaps);
821 }
822
823 return 0;
824}
825
826void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
827{
828 struct userfaultfd_unmap_ctx *ctx, *n;
829 struct userfaultfd_wait_queue ewq;
830
831 list_for_each_entry_safe(ctx, n, uf, list) {
832 msg_init(&ewq.msg);
833
834 ewq.msg.event = UFFD_EVENT_UNMAP;
835 ewq.msg.arg.remove.start = ctx->start;
836 ewq.msg.arg.remove.end = ctx->end;
837
838 userfaultfd_event_wait_completion(ctx->ctx, &ewq);
839
840 list_del(&ctx->list);
841 kfree(ctx);
842 }
843}
844
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700845static int userfaultfd_release(struct inode *inode, struct file *file)
846{
847 struct userfaultfd_ctx *ctx = file->private_data;
848 struct mm_struct *mm = ctx->mm;
849 struct vm_area_struct *vma, *prev;
850 /* len == 0 means wake all */
851 struct userfaultfd_wake_range range = { .len = 0, };
852 unsigned long new_flags;
853
Mark Rutland6aa7de02017-10-23 14:07:29 -0700854 WRITE_ONCE(ctx->released, true);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700855
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700856 if (!mmget_not_zero(mm))
857 goto wakeup;
858
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700859 /*
860 * Flush page faults out of all CPUs. NOTE: all page faults
861 * must be retried without returning VM_FAULT_SIGBUS if
862 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700863 * changes while handle_userfault released the mmap_lock. So
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700864 * it's critical that released is set to true (above), before
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700865 * taking the mmap_lock for writing.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700866 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700867 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700868 prev = NULL;
869 for (vma = mm->mmap; vma; vma = vma->vm_next) {
870 cond_resched();
871 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100872 !!(vma->vm_flags & __VM_UFFD_FLAGS));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700873 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
874 prev = vma;
875 continue;
876 }
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100877 new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS;
Jann Horn4d45e752020-10-15 20:13:00 -0700878 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
879 new_flags, vma->anon_vma,
880 vma->vm_file, vma->vm_pgoff,
881 vma_policy(vma),
Greg Kroah-Hartman05d2a662020-10-26 08:26:18 +0100882 NULL_VM_UFFD_CTX,
883 vma_get_anon_name(vma));
Jann Horn4d45e752020-10-15 20:13:00 -0700884 if (prev)
885 vma = prev;
886 else
887 prev = vma;
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200888 vm_write_begin(vma);
889 WRITE_ONCE(vma->vm_flags, new_flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700890 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200891 vm_write_end(vma);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700892 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700893 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -0700894 mmput(mm);
895wakeup:
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700896 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700897 * After no new page faults can wait on this fault_*wqh, flush
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700898 * the last page faults that may have been already waiting on
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700899 * the fault_*wqh.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700900 */
Eric Biggerscbcfa132019-07-04 15:14:39 -0700901 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700902 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -0700903 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range);
Eric Biggerscbcfa132019-07-04 15:14:39 -0700904 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700905
Mike Rapoport5a18b642017-08-02 13:32:24 -0700906 /* Flush pending events that may still wait on event_wqh */
907 wake_up_all(&ctx->event_wqh);
908
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800909 wake_up_poll(&ctx->fd_wqh, EPOLLHUP);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700910 userfaultfd_ctx_put(ctx);
911 return 0;
912}
913
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700914/* fault_pending_wqh.lock must be hold by the caller */
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800915static inline struct userfaultfd_wait_queue *find_userfault_in(
916 wait_queue_head_t *wqh)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700917{
Ingo Molnarac6424b2017-06-20 12:06:13 +0200918 wait_queue_entry_t *wq;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700919 struct userfaultfd_wait_queue *uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700920
Lance Roy456a7372018-10-04 23:45:44 -0700921 lockdep_assert_held(&wqh->lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700922
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700923 uwq = NULL;
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800924 if (!waitqueue_active(wqh))
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700925 goto out;
926 /* walk in reverse to provide FIFO behavior to read userfaults */
Ingo Molnar2055da92017-06-20 12:06:46 +0200927 wq = list_last_entry(&wqh->head, typeof(*wq), entry);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -0700928 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
929out:
930 return uwq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700931}
932
Pavel Emelyanov6dcc27f2017-02-22 15:42:18 -0800933static inline struct userfaultfd_wait_queue *find_userfault(
934 struct userfaultfd_ctx *ctx)
935{
936 return find_userfault_in(&ctx->fault_pending_wqh);
937}
938
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800939static inline struct userfaultfd_wait_queue *find_userfault_evt(
940 struct userfaultfd_ctx *ctx)
941{
942 return find_userfault_in(&ctx->event_wqh);
943}
944
Al Viro076ccb72017-07-03 01:02:18 -0400945static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700946{
947 struct userfaultfd_ctx *ctx = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400948 __poll_t ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700949
950 poll_wait(file, &ctx->fd_wqh, wait);
951
Nadav Amit6afd1e02021-09-02 14:58:59 -0700952 if (!userfaultfd_is_initialized(ctx))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800953 return EPOLLERR;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -0800954
Nadav Amit6afd1e02021-09-02 14:58:59 -0700955 /*
956 * poll() never guarantees that read won't block.
957 * userfaults can be waken before they're read().
958 */
959 if (unlikely(!(file->f_flags & O_NONBLOCK)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800960 return EPOLLERR;
Nadav Amit6afd1e02021-09-02 14:58:59 -0700961 /*
962 * lockless access to see if there are pending faults
963 * __pollwait last action is the add_wait_queue but
964 * the spin_unlock would allow the waitqueue_active to
965 * pass above the actual list_add inside
966 * add_wait_queue critical section. So use a full
967 * memory barrier to serialize the list_add write of
968 * add_wait_queue() with the waitqueue_active read
969 * below.
970 */
971 ret = 0;
972 smp_mb();
973 if (waitqueue_active(&ctx->fault_pending_wqh))
974 ret = EPOLLIN;
975 else if (waitqueue_active(&ctx->event_wqh))
976 ret = EPOLLIN;
977
978 return ret;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700979}
980
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800981static const struct file_operations userfaultfd_fops;
982
Daniel Colascionedbc935c2021-01-08 14:22:23 -0800983static int resolve_userfault_fork(struct userfaultfd_ctx *new,
984 struct inode *inode,
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800985 struct uffd_msg *msg)
986{
987 int fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800988
Daniel Colascionedbc935c2021-01-08 14:22:23 -0800989 fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, new,
990 O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS), inode);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800991 if (fd < 0)
992 return fd;
993
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800994 msg->arg.reserved.reserved1 = 0;
995 msg->arg.fork.ufd = fd;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800996 return 0;
997}
998
Andrea Arcangeli86039bd2015-09-04 15:46:31 -0700999static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
Daniel Colascionedbc935c2021-01-08 14:22:23 -08001000 struct uffd_msg *msg, struct inode *inode)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001001{
1002 ssize_t ret;
1003 DECLARE_WAITQUEUE(wait, current);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001004 struct userfaultfd_wait_queue *uwq;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001005 /*
1006 * Handling fork event requires sleeping operations, so
1007 * we drop the event_wqh lock, then do these ops, then
1008 * lock it back and wake up the waiter. While the lock is
1009 * dropped the ewq may go away so we keep track of it
1010 * carefully.
1011 */
1012 LIST_HEAD(fork_event);
1013 struct userfaultfd_ctx *fork_nctx = NULL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001014
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001015 /* always take the fd_wqh lock before the fault_pending_wqh lock */
Christoph Hellwigae62c162018-10-26 15:02:19 -07001016 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001017 __add_wait_queue(&ctx->fd_wqh, &wait);
1018 for (;;) {
1019 set_current_state(TASK_INTERRUPTIBLE);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001020 spin_lock(&ctx->fault_pending_wqh.lock);
1021 uwq = find_userfault(ctx);
1022 if (uwq) {
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001023 /*
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001024 * Use a seqcount to repeat the lockless check
1025 * in wake_userfault() to avoid missing
1026 * wakeups because during the refile both
1027 * waitqueue could become empty if this is the
1028 * only userfault.
1029 */
1030 write_seqcount_begin(&ctx->refile_seq);
1031
1032 /*
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001033 * The fault_pending_wqh.lock prevents the uwq
1034 * to disappear from under us.
1035 *
1036 * Refile this userfault from
1037 * fault_pending_wqh to fault_wqh, it's not
1038 * pending anymore after we read it.
1039 *
1040 * Use list_del() by hand (as
1041 * userfaultfd_wake_function also uses
1042 * list_del_init() by hand) to be sure nobody
1043 * changes __remove_wait_queue() to use
1044 * list_del_init() in turn breaking the
1045 * !list_empty_careful() check in
Ingo Molnar2055da92017-06-20 12:06:46 +02001046 * handle_userfault(). The uwq->wq.head list
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001047 * must never be empty at any time during the
1048 * refile, or the waitqueue could disappear
1049 * from under us. The "wait_queue_head_t"
1050 * parameter of __remove_wait_queue() is unused
1051 * anyway.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001052 */
Ingo Molnar2055da92017-06-20 12:06:46 +02001053 list_del(&uwq->wq.entry);
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001054 add_wait_queue(&ctx->fault_wqh, &uwq->wq);
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001055
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001056 write_seqcount_end(&ctx->refile_seq);
1057
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001058 /* careful to always initialize msg if ret == 0 */
1059 *msg = uwq->msg;
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001060 spin_unlock(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001061 ret = 0;
1062 break;
1063 }
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001064 spin_unlock(&ctx->fault_pending_wqh.lock);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001065
1066 spin_lock(&ctx->event_wqh.lock);
1067 uwq = find_userfault_evt(ctx);
1068 if (uwq) {
1069 *msg = uwq->msg;
1070
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001071 if (uwq->msg.event == UFFD_EVENT_FORK) {
1072 fork_nctx = (struct userfaultfd_ctx *)
1073 (unsigned long)
1074 uwq->msg.arg.reserved.reserved1;
Ingo Molnar2055da92017-06-20 12:06:46 +02001075 list_move(&uwq->wq.entry, &fork_event);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001076 /*
1077 * fork_nctx can be freed as soon as
1078 * we drop the lock, unless we take a
1079 * reference on it.
1080 */
1081 userfaultfd_ctx_get(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001082 spin_unlock(&ctx->event_wqh.lock);
1083 ret = 0;
1084 break;
1085 }
1086
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001087 userfaultfd_event_complete(ctx, uwq);
1088 spin_unlock(&ctx->event_wqh.lock);
1089 ret = 0;
1090 break;
1091 }
1092 spin_unlock(&ctx->event_wqh.lock);
1093
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001094 if (signal_pending(current)) {
1095 ret = -ERESTARTSYS;
1096 break;
1097 }
1098 if (no_wait) {
1099 ret = -EAGAIN;
1100 break;
1101 }
Christoph Hellwigae62c162018-10-26 15:02:19 -07001102 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001103 schedule();
Christoph Hellwigae62c162018-10-26 15:02:19 -07001104 spin_lock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001105 }
1106 __remove_wait_queue(&ctx->fd_wqh, &wait);
1107 __set_current_state(TASK_RUNNING);
Christoph Hellwigae62c162018-10-26 15:02:19 -07001108 spin_unlock_irq(&ctx->fd_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001109
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001110 if (!ret && msg->event == UFFD_EVENT_FORK) {
Daniel Colascionedbc935c2021-01-08 14:22:23 -08001111 ret = resolve_userfault_fork(fork_nctx, inode, msg);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001112 spin_lock_irq(&ctx->event_wqh.lock);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001113 if (!list_empty(&fork_event)) {
1114 /*
1115 * The fork thread didn't abort, so we can
1116 * drop the temporary refcount.
1117 */
1118 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001119
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001120 uwq = list_first_entry(&fork_event,
1121 typeof(*uwq),
1122 wq.entry);
1123 /*
1124 * If fork_event list wasn't empty and in turn
1125 * the event wasn't already released by fork
1126 * (the event is allocated on fork kernel
1127 * stack), put the event back to its place in
1128 * the event_wq. fork_event head will be freed
1129 * as soon as we return so the event cannot
1130 * stay queued there no matter the current
1131 * "ret" value.
1132 */
1133 list_del(&uwq->wq.entry);
1134 __add_wait_queue(&ctx->event_wqh, &uwq->wq);
1135
1136 /*
1137 * Leave the event in the waitqueue and report
1138 * error to userland if we failed to resolve
1139 * the userfault fork.
1140 */
1141 if (likely(!ret))
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001142 userfaultfd_event_complete(ctx, uwq);
Andrea Arcangeli384632e2017-10-03 16:15:38 -07001143 } else {
1144 /*
1145 * Here the fork thread aborted and the
1146 * refcount from the fork thread on fork_nctx
1147 * has already been released. We still hold
1148 * the reference we took before releasing the
1149 * lock above. If resolve_userfault_fork
1150 * failed we've to drop it because the
1151 * fork_nctx has to be freed in such case. If
1152 * it succeeded we'll hold it because the new
1153 * uffd references it.
1154 */
1155 if (ret)
1156 userfaultfd_ctx_put(fork_nctx);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001157 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07001158 spin_unlock_irq(&ctx->event_wqh.lock);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -08001159 }
1160
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001161 return ret;
1162}
1163
1164static ssize_t userfaultfd_read(struct file *file, char __user *buf,
1165 size_t count, loff_t *ppos)
1166{
1167 struct userfaultfd_ctx *ctx = file->private_data;
1168 ssize_t _ret, ret = 0;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001169 struct uffd_msg msg;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001170 int no_wait = file->f_flags & O_NONBLOCK;
Daniel Colascionedbc935c2021-01-08 14:22:23 -08001171 struct inode *inode = file_inode(file);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001172
Nadav Amit6afd1e02021-09-02 14:58:59 -07001173 if (!userfaultfd_is_initialized(ctx))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001174 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001175
1176 for (;;) {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001177 if (count < sizeof(msg))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001178 return ret ? ret : -EINVAL;
Daniel Colascionedbc935c2021-01-08 14:22:23 -08001179 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg, inode);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001180 if (_ret < 0)
1181 return ret ? ret : _ret;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001182 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001183 return ret ? ret : -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001184 ret += sizeof(msg);
1185 buf += sizeof(msg);
1186 count -= sizeof(msg);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001187 /*
1188 * Allow to read more than one fault at time but only
1189 * block if waiting for the very first one.
1190 */
1191 no_wait = O_NONBLOCK;
1192 }
1193}
1194
1195static void __wake_userfault(struct userfaultfd_ctx *ctx,
1196 struct userfaultfd_wake_range *range)
1197{
Eric Biggerscbcfa132019-07-04 15:14:39 -07001198 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001199 /* wake all in the range and autoremove */
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001200 if (waitqueue_active(&ctx->fault_pending_wqh))
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -07001201 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07001202 range);
1203 if (waitqueue_active(&ctx->fault_wqh))
Matthew Wilcoxc430d1e2018-08-21 21:56:30 -07001204 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
Eric Biggerscbcfa132019-07-04 15:14:39 -07001205 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001206}
1207
1208static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1209 struct userfaultfd_wake_range *range)
1210{
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001211 unsigned seq;
1212 bool need_wakeup;
1213
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001214 /*
1215 * To be sure waitqueue_active() is not reordered by the CPU
1216 * before the pagetable update, use an explicit SMP memory
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001217 * barrier here. PT lock release or mmap_read_unlock(mm) still
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001218 * have release semantics that can allow the
1219 * waitqueue_active() to be reordered before the pte update.
1220 */
1221 smp_mb();
1222
1223 /*
1224 * Use waitqueue_active because it's very frequent to
1225 * change the address space atomically even if there are no
1226 * userfaults yet. So we take the spinlock only when we're
1227 * sure we've userfaults to wake.
1228 */
Andrea Arcangeli2c5b7e12015-09-04 15:47:23 -07001229 do {
1230 seq = read_seqcount_begin(&ctx->refile_seq);
1231 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1232 waitqueue_active(&ctx->fault_wqh);
1233 cond_resched();
1234 } while (read_seqcount_retry(&ctx->refile_seq, seq));
1235 if (need_wakeup)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001236 __wake_userfault(ctx, range);
1237}
1238
1239static __always_inline int validate_range(struct mm_struct *mm,
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001240 __u64 start, __u64 len)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001241{
1242 __u64 task_size = mm->task_size;
1243
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001244 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;
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001250 if (start < mmap_min_addr)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001251 return -EINVAL;
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001252 if (start >= task_size)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001253 return -EINVAL;
Peter Collingbourne0b591c02021-07-23 15:50:01 -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 */
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001263 if (vm_flags & VM_UFFD_WP) {
1264 if (is_vm_hugetlb_page(vma) || vma_is_shmem(vma))
1265 return false;
1266 }
1267
1268 if (vm_flags & VM_UFFD_MINOR) {
Axel Rasmussend6721232021-03-18 17:01:51 +11001269 if (!(is_vm_hugetlb_page(vma) || vma_is_shmem(vma)))
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001270 return false;
1271 }
1272
1273 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1274 vma_is_shmem(vma);
Mike Rapoportba6907d2017-02-22 15:43:22 -08001275}
1276
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001277static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1278 unsigned long arg)
1279{
1280 struct mm_struct *mm = ctx->mm;
1281 struct vm_area_struct *vma, *prev, *cur;
1282 int ret;
1283 struct uffdio_register uffdio_register;
1284 struct uffdio_register __user *user_uffdio_register;
1285 unsigned long vm_flags, new_flags;
1286 bool found;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001287 bool basic_ioctls;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001288 unsigned long start, end, vma_end;
1289
1290 user_uffdio_register = (struct uffdio_register __user *) arg;
1291
1292 ret = -EFAULT;
1293 if (copy_from_user(&uffdio_register, user_uffdio_register,
1294 sizeof(uffdio_register)-sizeof(__u64)))
1295 goto out;
1296
1297 ret = -EINVAL;
1298 if (!uffdio_register.mode)
1299 goto out;
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001300 if (uffdio_register.mode & ~UFFD_API_REGISTER_MODES)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001301 goto out;
1302 vm_flags = 0;
1303 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1304 vm_flags |= VM_UFFD_MISSING;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001305 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001306 vm_flags |= VM_UFFD_WP;
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001307 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR) {
1308#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
1309 goto out;
1310#endif
1311 vm_flags |= VM_UFFD_MINOR;
1312 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001313
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001314 ret = validate_range(mm, uffdio_register.range.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001315 uffdio_register.range.len);
1316 if (ret)
1317 goto out;
1318
1319 start = uffdio_register.range.start;
1320 end = start + uffdio_register.range.len;
1321
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001322 ret = -ENOMEM;
1323 if (!mmget_not_zero(mm))
1324 goto out;
1325
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001326 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001327 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001328 if (!vma)
1329 goto out_unlock;
1330
1331 /* check that there's at least one vma in the range */
1332 ret = -EINVAL;
1333 if (vma->vm_start >= end)
1334 goto out_unlock;
1335
1336 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001337 * If the first vma contains huge pages, make sure start address
1338 * is aligned to huge page size.
1339 */
1340 if (is_vm_hugetlb_page(vma)) {
1341 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1342
1343 if (start & (vma_hpagesize - 1))
1344 goto out_unlock;
1345 }
1346
1347 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001348 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001349 */
1350 found = false;
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001351 basic_ioctls = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001352 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1353 cond_resched();
1354
1355 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001356 !!(cur->vm_flags & __VM_UFFD_FLAGS));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001357
1358 /* check not compatible vmas */
1359 ret = -EINVAL;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001360 if (!vma_can_userfault(cur, vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001361 goto out_unlock;
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001362
1363 /*
1364 * UFFDIO_COPY will fill file holes even without
1365 * PROT_WRITE. This check enforces that if this is a
1366 * MAP_SHARED, the process has write permission to the backing
1367 * file. If VM_MAYWRITE is set it also enforces that on a
1368 * MAP_SHARED vma: there is no F_WRITE_SEAL and no further
1369 * F_WRITE_SEAL can be taken until the vma is destroyed.
1370 */
1371 ret = -EPERM;
1372 if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
1373 goto out_unlock;
1374
Mike Kravetzcab350a2017-02-22 15:43:04 -08001375 /*
1376 * If this vma contains ending address, and huge pages
1377 * check alignment.
1378 */
1379 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
1380 end > cur->vm_start) {
1381 unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
1382
1383 ret = -EINVAL;
1384
1385 if (end & (vma_hpagesize - 1))
1386 goto out_unlock;
1387 }
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001388 if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
1389 goto out_unlock;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001390
1391 /*
1392 * Check that this vma isn't already owned by a
1393 * different userfaultfd. We can't allow more than one
1394 * userfaultfd to own a single vma simultaneously or we
1395 * wouldn't know which one to deliver the userfaults to.
1396 */
1397 ret = -EBUSY;
1398 if (cur->vm_userfaultfd_ctx.ctx &&
1399 cur->vm_userfaultfd_ctx.ctx != ctx)
1400 goto out_unlock;
1401
Mike Kravetzcab350a2017-02-22 15:43:04 -08001402 /*
1403 * Note vmas containing huge pages
1404 */
Mike Rapoportce53e8e2017-09-06 16:23:12 -07001405 if (is_vm_hugetlb_page(cur))
1406 basic_ioctls = true;
Mike Kravetzcab350a2017-02-22 15:43:04 -08001407
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001408 found = true;
1409 }
1410 BUG_ON(!found);
1411
1412 if (vma->vm_start < start)
1413 prev = vma;
1414
1415 ret = 0;
1416 do {
1417 cond_resched();
1418
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001419 BUG_ON(!vma_can_userfault(vma, vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001420 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1421 vma->vm_userfaultfd_ctx.ctx != ctx);
Andrea Arcangeli29ec90662018-11-30 14:09:32 -08001422 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001423
1424 /*
1425 * Nothing to do: this vma is already registered into this
1426 * userfaultfd and with the right tracking mode too.
1427 */
1428 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1429 (vma->vm_flags & vm_flags) == vm_flags)
1430 goto skip;
1431
1432 if (vma->vm_start > start)
1433 start = vma->vm_start;
1434 vma_end = min(end, vma->vm_end);
1435
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001436 new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001437 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1438 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1439 vma_policy(vma),
Colin Cross60500a42015-10-27 16:42:08 -07001440 ((struct vm_userfaultfd_ctx){ ctx }),
1441 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001442 if (prev) {
1443 vma = prev;
1444 goto next;
1445 }
1446 if (vma->vm_start < start) {
1447 ret = split_vma(mm, vma, start, 1);
1448 if (ret)
1449 break;
1450 }
1451 if (vma->vm_end > end) {
1452 ret = split_vma(mm, vma, end, 0);
1453 if (ret)
1454 break;
1455 }
1456 next:
1457 /*
1458 * In the vma_merge() successful mprotect-like case 8:
1459 * the next vma was merged into the current one and
1460 * the current one has not been updated yet.
1461 */
Laurent Dufour9cfe1682018-04-17 16:33:15 +02001462 vm_write_begin(vma);
1463 WRITE_ONCE(vma->vm_flags, new_flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001464 vma->vm_userfaultfd_ctx.ctx = ctx;
Laurent Dufour9cfe1682018-04-17 16:33:15 +02001465 vm_write_end(vma);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001466
Peter Xu343cacf2021-03-18 17:01:47 +11001467 if (is_vm_hugetlb_page(vma) && uffd_disable_huge_pmd_share(vma))
1468 hugetlb_unshare_all_pmds(vma);
1469
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001470 skip:
1471 prev = vma;
1472 start = vma->vm_end;
1473 vma = vma->vm_next;
1474 } while (vma && vma->vm_start < end);
1475out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001476 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001477 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001478 if (!ret) {
Peter Xu14819302020-04-06 20:06:29 -07001479 __u64 ioctls_out;
1480
1481 ioctls_out = basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
1482 UFFD_API_RANGE_IOCTLS;
1483
1484 /*
1485 * Declare the WP ioctl only if the WP mode is
1486 * specified and all checks passed with the range
1487 */
1488 if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_WP))
1489 ioctls_out &= ~((__u64)1 << _UFFDIO_WRITEPROTECT);
1490
Axel Rasmussen4a5cf922021-03-18 17:01:50 +11001491 /* CONTINUE ioctl is only supported for MINOR ranges. */
1492 if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR))
1493 ioctls_out &= ~((__u64)1 << _UFFDIO_CONTINUE);
1494
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001495 /*
1496 * Now that we scanned all vmas we can already tell
1497 * userland which ioctls methods are guaranteed to
1498 * succeed on this range.
1499 */
Peter Xu14819302020-04-06 20:06:29 -07001500 if (put_user(ioctls_out, &user_uffdio_register->ioctls))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001501 ret = -EFAULT;
1502 }
1503out:
1504 return ret;
1505}
1506
1507static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1508 unsigned long arg)
1509{
1510 struct mm_struct *mm = ctx->mm;
1511 struct vm_area_struct *vma, *prev, *cur;
1512 int ret;
1513 struct uffdio_range uffdio_unregister;
1514 unsigned long new_flags;
1515 bool found;
1516 unsigned long start, end, vma_end;
1517 const void __user *buf = (void __user *)arg;
1518
1519 ret = -EFAULT;
1520 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1521 goto out;
1522
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001523 ret = validate_range(mm, uffdio_unregister.start,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001524 uffdio_unregister.len);
1525 if (ret)
1526 goto out;
1527
1528 start = uffdio_unregister.start;
1529 end = start + uffdio_unregister.len;
1530
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001531 ret = -ENOMEM;
1532 if (!mmget_not_zero(mm))
1533 goto out;
1534
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001535 mmap_write_lock(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001536 vma = find_vma_prev(mm, start, &prev);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001537 if (!vma)
1538 goto out_unlock;
1539
1540 /* check that there's at least one vma in the range */
1541 ret = -EINVAL;
1542 if (vma->vm_start >= end)
1543 goto out_unlock;
1544
1545 /*
Mike Kravetzcab350a2017-02-22 15:43:04 -08001546 * If the first vma contains huge pages, make sure start address
1547 * is aligned to huge page size.
1548 */
1549 if (is_vm_hugetlb_page(vma)) {
1550 unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
1551
1552 if (start & (vma_hpagesize - 1))
1553 goto out_unlock;
1554 }
1555
1556 /*
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001557 * Search for not compatible vmas.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001558 */
1559 found = false;
1560 ret = -EINVAL;
1561 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1562 cond_resched();
1563
1564 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001565 !!(cur->vm_flags & __VM_UFFD_FLAGS));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001566
1567 /*
1568 * Check not compatible vmas, not strictly required
1569 * here as not compatible vmas cannot have an
1570 * userfaultfd_ctx registered on them, but this
1571 * provides for more strict behavior to notice
1572 * unregistration errors.
1573 */
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001574 if (!vma_can_userfault(cur, cur->vm_flags))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001575 goto out_unlock;
1576
1577 found = true;
1578 }
1579 BUG_ON(!found);
1580
1581 if (vma->vm_start < start)
1582 prev = vma;
1583
1584 ret = 0;
1585 do {
1586 cond_resched();
1587
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001588 BUG_ON(!vma_can_userfault(vma, vma->vm_flags));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001589
1590 /*
1591 * Nothing to do: this vma is already registered into this
1592 * userfaultfd and with the right tracking mode too.
1593 */
1594 if (!vma->vm_userfaultfd_ctx.ctx)
1595 goto skip;
1596
Andrea Arcangeli01e881f2018-12-14 14:17:17 -08001597 WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
1598
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001599 if (vma->vm_start > start)
1600 start = vma->vm_start;
1601 vma_end = min(end, vma->vm_end);
1602
Andrea Arcangeli09fa5292017-02-22 15:42:46 -08001603 if (userfaultfd_missing(vma)) {
1604 /*
1605 * Wake any concurrent pending userfault while
1606 * we unregister, so they will not hang
1607 * permanently and it avoids userland to call
1608 * UFFDIO_WAKE explicitly.
1609 */
1610 struct userfaultfd_wake_range range;
1611 range.start = start;
1612 range.len = vma_end - start;
1613 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
1614 }
1615
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001616 new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001617 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1618 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1619 vma_policy(vma),
Colin Cross60500a42015-10-27 16:42:08 -07001620 NULL_VM_UFFD_CTX,
1621 vma_get_anon_name(vma));
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001622 if (prev) {
1623 vma = prev;
1624 goto next;
1625 }
1626 if (vma->vm_start < start) {
1627 ret = split_vma(mm, vma, start, 1);
1628 if (ret)
1629 break;
1630 }
1631 if (vma->vm_end > end) {
1632 ret = split_vma(mm, vma, end, 0);
1633 if (ret)
1634 break;
1635 }
1636 next:
1637 /*
1638 * In the vma_merge() successful mprotect-like case 8:
1639 * the next vma was merged into the current one and
1640 * the current one has not been updated yet.
1641 */
Laurent Dufour9cfe1682018-04-17 16:33:15 +02001642 vm_write_begin(vma);
1643 WRITE_ONCE(vma->vm_flags, new_flags);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001644 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
Laurent Dufour9cfe1682018-04-17 16:33:15 +02001645 vm_write_end(vma);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001646
1647 skip:
1648 prev = vma;
1649 start = vma->vm_end;
1650 vma = vma->vm_next;
1651 } while (vma && vma->vm_start < end);
1652out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001653 mmap_write_unlock(mm);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001654 mmput(mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001655out:
1656 return ret;
1657}
1658
1659/*
Andrea Arcangeliba85c702015-09-04 15:46:41 -07001660 * userfaultfd_wake may be used in combination with the
1661 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001662 */
1663static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1664 unsigned long arg)
1665{
1666 int ret;
1667 struct uffdio_range uffdio_wake;
1668 struct userfaultfd_wake_range range;
1669 const void __user *buf = (void __user *)arg;
1670
1671 ret = -EFAULT;
1672 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1673 goto out;
1674
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001675 ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001676 if (ret)
1677 goto out;
1678
1679 range.start = uffdio_wake.start;
1680 range.len = uffdio_wake.len;
1681
1682 /*
1683 * len == 0 means wake all and we don't want to wake all here,
1684 * so check it again to be sure.
1685 */
1686 VM_BUG_ON(!range.len);
1687
1688 wake_userfault(ctx, &range);
1689 ret = 0;
1690
1691out:
1692 return ret;
1693}
1694
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001695static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1696 unsigned long arg)
1697{
1698 __s64 ret;
1699 struct uffdio_copy uffdio_copy;
1700 struct uffdio_copy __user *user_uffdio_copy;
1701 struct userfaultfd_wake_range range;
1702
1703 user_uffdio_copy = (struct uffdio_copy __user *) arg;
1704
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001705 ret = -EAGAIN;
1706 if (READ_ONCE(ctx->mmap_changing))
1707 goto out;
1708
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001709 ret = -EFAULT;
1710 if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1711 /* don't copy "copy" last field */
1712 sizeof(uffdio_copy)-sizeof(__s64)))
1713 goto out;
1714
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001715 ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001716 if (ret)
1717 goto out;
1718 /*
1719 * double check for wraparound just in case. copy_from_user()
1720 * will later check uffdio_copy.src + uffdio_copy.len to fit
1721 * in the userland range.
1722 */
1723 ret = -EINVAL;
1724 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
1725 goto out;
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001726 if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP))
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001727 goto out;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001728 if (mmget_not_zero(ctx->mm)) {
1729 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
Andrea Arcangeli72981e02020-04-06 20:05:41 -07001730 uffdio_copy.len, &ctx->mmap_changing,
1731 uffdio_copy.mode);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001732 mmput(ctx->mm);
Mike Rapoport96333182017-02-24 14:58:31 -08001733 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001734 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001735 }
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001736 if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1737 return -EFAULT;
1738 if (ret < 0)
1739 goto out;
1740 BUG_ON(!ret);
1741 /* len == 0 would wake all */
1742 range.len = ret;
1743 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1744 range.start = uffdio_copy.dst;
1745 wake_userfault(ctx, &range);
1746 }
1747 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1748out:
1749 return ret;
1750}
1751
1752static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1753 unsigned long arg)
1754{
1755 __s64 ret;
1756 struct uffdio_zeropage uffdio_zeropage;
1757 struct uffdio_zeropage __user *user_uffdio_zeropage;
1758 struct userfaultfd_wake_range range;
1759
1760 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1761
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001762 ret = -EAGAIN;
1763 if (READ_ONCE(ctx->mmap_changing))
1764 goto out;
1765
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001766 ret = -EFAULT;
1767 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1768 /* don't copy "zeropage" last field */
1769 sizeof(uffdio_zeropage)-sizeof(__s64)))
1770 goto out;
1771
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001772 ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001773 uffdio_zeropage.range.len);
1774 if (ret)
1775 goto out;
1776 ret = -EINVAL;
1777 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1778 goto out;
1779
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001780 if (mmget_not_zero(ctx->mm)) {
1781 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
Mike Rapoportdf2cc962018-06-07 17:09:25 -07001782 uffdio_zeropage.range.len,
1783 &ctx->mmap_changing);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001784 mmput(ctx->mm);
Mike Rapoport9d95aa42017-08-02 13:32:15 -07001785 } else {
Mike Rapoporte86b2982017-08-10 15:24:32 -07001786 return -ESRCH;
Oleg Nesterovd2005e32016-05-20 16:58:36 -07001787 }
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001788 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1789 return -EFAULT;
1790 if (ret < 0)
1791 goto out;
1792 /* len == 0 would wake all */
1793 BUG_ON(!ret);
1794 range.len = ret;
1795 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1796 range.start = uffdio_zeropage.range.start;
1797 wake_userfault(ctx, &range);
1798 }
1799 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1800out:
1801 return ret;
1802}
1803
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001804static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,
1805 unsigned long arg)
1806{
1807 int ret;
1808 struct uffdio_writeprotect uffdio_wp;
1809 struct uffdio_writeprotect __user *user_uffdio_wp;
1810 struct userfaultfd_wake_range range;
Peter Xu23080e22020-04-06 20:06:20 -07001811 bool mode_wp, mode_dontwake;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001812
1813 if (READ_ONCE(ctx->mmap_changing))
1814 return -EAGAIN;
1815
1816 user_uffdio_wp = (struct uffdio_writeprotect __user *) arg;
1817
1818 if (copy_from_user(&uffdio_wp, user_uffdio_wp,
1819 sizeof(struct uffdio_writeprotect)))
1820 return -EFAULT;
1821
Peter Collingbourne0b591c02021-07-23 15:50:01 -07001822 ret = validate_range(ctx->mm, uffdio_wp.range.start,
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001823 uffdio_wp.range.len);
1824 if (ret)
1825 return ret;
1826
1827 if (uffdio_wp.mode & ~(UFFDIO_WRITEPROTECT_MODE_DONTWAKE |
1828 UFFDIO_WRITEPROTECT_MODE_WP))
1829 return -EINVAL;
Peter Xu23080e22020-04-06 20:06:20 -07001830
1831 mode_wp = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_WP;
1832 mode_dontwake = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_DONTWAKE;
1833
1834 if (mode_wp && mode_dontwake)
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001835 return -EINVAL;
1836
Nadav Amit3cda4bf2021-10-18 15:15:25 -07001837 if (mmget_not_zero(ctx->mm)) {
1838 ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
1839 uffdio_wp.range.len, mode_wp,
1840 &ctx->mmap_changing);
1841 mmput(ctx->mm);
1842 } else {
1843 return -ESRCH;
1844 }
1845
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001846 if (ret)
1847 return ret;
1848
Peter Xu23080e22020-04-06 20:06:20 -07001849 if (!mode_wp && !mode_dontwake) {
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07001850 range.start = uffdio_wp.range.start;
1851 range.len = uffdio_wp.range.len;
1852 wake_userfault(ctx, &range);
1853 }
1854 return ret;
1855}
1856
Axel Rasmussen4a5cf922021-03-18 17:01:50 +11001857static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg)
1858{
1859 __s64 ret;
1860 struct uffdio_continue uffdio_continue;
1861 struct uffdio_continue __user *user_uffdio_continue;
1862 struct userfaultfd_wake_range range;
1863
1864 user_uffdio_continue = (struct uffdio_continue __user *)arg;
1865
1866 ret = -EAGAIN;
1867 if (READ_ONCE(ctx->mmap_changing))
1868 goto out;
1869
1870 ret = -EFAULT;
1871 if (copy_from_user(&uffdio_continue, user_uffdio_continue,
1872 /* don't copy the output fields */
1873 sizeof(uffdio_continue) - (sizeof(__s64))))
1874 goto out;
1875
Aaron Ding2ccbb922021-08-05 18:02:23 +08001876 ret = validate_range(ctx->mm, uffdio_continue.range.start,
Axel Rasmussen4a5cf922021-03-18 17:01:50 +11001877 uffdio_continue.range.len);
1878 if (ret)
1879 goto out;
1880
1881 ret = -EINVAL;
1882 /* double check for wraparound just in case. */
1883 if (uffdio_continue.range.start + uffdio_continue.range.len <=
1884 uffdio_continue.range.start) {
1885 goto out;
1886 }
1887 if (uffdio_continue.mode & ~UFFDIO_CONTINUE_MODE_DONTWAKE)
1888 goto out;
1889
1890 if (mmget_not_zero(ctx->mm)) {
1891 ret = mcopy_continue(ctx->mm, uffdio_continue.range.start,
1892 uffdio_continue.range.len,
1893 &ctx->mmap_changing);
1894 mmput(ctx->mm);
1895 } else {
1896 return -ESRCH;
1897 }
1898
1899 if (unlikely(put_user(ret, &user_uffdio_continue->mapped)))
1900 return -EFAULT;
1901 if (ret < 0)
1902 goto out;
1903
1904 /* len == 0 would wake all */
1905 BUG_ON(!ret);
1906 range.len = ret;
1907 if (!(uffdio_continue.mode & UFFDIO_CONTINUE_MODE_DONTWAKE)) {
1908 range.start = uffdio_continue.range.start;
1909 wake_userfault(ctx, &range);
1910 }
1911 ret = range.len == uffdio_continue.range.len ? 0 : -EAGAIN;
1912
1913out:
1914 return ret;
1915}
1916
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001917static inline unsigned int uffd_ctx_features(__u64 user_features)
1918{
1919 /*
Nadav Amit6afd1e02021-09-02 14:58:59 -07001920 * For the current set of features the bits just coincide. Set
1921 * UFFD_FEATURE_INITIALIZED to mark the features as enabled.
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001922 */
Nadav Amit6afd1e02021-09-02 14:58:59 -07001923 return (unsigned int)user_features | UFFD_FEATURE_INITIALIZED;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08001924}
1925
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001926/*
1927 * userland asks for a certain API version and we return which bits
1928 * and ioctl commands are implemented in this kernel for such API
1929 * version or -EINVAL if unknown.
1930 */
1931static int userfaultfd_api(struct userfaultfd_ctx *ctx,
1932 unsigned long arg)
1933{
1934 struct uffdio_api uffdio_api;
1935 void __user *buf = (void __user *)arg;
Nadav Amit6afd1e02021-09-02 14:58:59 -07001936 unsigned int ctx_features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001937 int ret;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001938 __u64 features;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001939
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001940 ret = -EFAULT;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -07001941 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001942 goto out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001943 features = uffdio_api.features;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001944 ret = -EINVAL;
1945 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES))
1946 goto err_out;
1947 ret = -EPERM;
1948 if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE))
1949 goto err_out;
Andrea Arcangeli65603142017-02-22 15:42:24 -08001950 /* report all available features and ioctls to userland */
1951 uffdio_api.features = UFFD_API_FEATURES;
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001952#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
Axel Rasmussend6721232021-03-18 17:01:51 +11001953 uffdio_api.features &=
1954 ~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM);
Axel Rasmussen4d3dd332021-03-18 17:01:50 +11001955#endif
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001956 uffdio_api.ioctls = UFFD_API_IOCTLS;
1957 ret = -EFAULT;
1958 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1959 goto out;
Nadav Amit6afd1e02021-09-02 14:58:59 -07001960
Andrea Arcangeli65603142017-02-22 15:42:24 -08001961 /* only enable the requested features for this uffd context */
Nadav Amit6afd1e02021-09-02 14:58:59 -07001962 ctx_features = uffd_ctx_features(features);
1963 ret = -EINVAL;
1964 if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
1965 goto err_out;
1966
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001967 ret = 0;
1968out:
1969 return ret;
Mike Rapoport3c1c24d2019-11-30 17:58:01 -08001970err_out:
1971 memset(&uffdio_api, 0, sizeof(uffdio_api));
1972 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
1973 ret = -EFAULT;
1974 goto out;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001975}
1976
1977static long userfaultfd_ioctl(struct file *file, unsigned cmd,
1978 unsigned long arg)
1979{
1980 int ret = -EINVAL;
1981 struct userfaultfd_ctx *ctx = file->private_data;
1982
Nadav Amit6afd1e02021-09-02 14:58:59 -07001983 if (cmd != UFFDIO_API && !userfaultfd_is_initialized(ctx))
Andrea Arcangelie6485a42015-09-04 15:47:15 -07001984 return -EINVAL;
1985
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07001986 switch(cmd) {
1987 case UFFDIO_API:
1988 ret = userfaultfd_api(ctx, arg);
1989 break;
1990 case UFFDIO_REGISTER:
1991 ret = userfaultfd_register(ctx, arg);
1992 break;
1993 case UFFDIO_UNREGISTER:
1994 ret = userfaultfd_unregister(ctx, arg);
1995 break;
1996 case UFFDIO_WAKE:
1997 ret = userfaultfd_wake(ctx, arg);
1998 break;
Andrea Arcangeliad465cae2015-09-04 15:47:11 -07001999 case UFFDIO_COPY:
2000 ret = userfaultfd_copy(ctx, arg);
2001 break;
2002 case UFFDIO_ZEROPAGE:
2003 ret = userfaultfd_zeropage(ctx, arg);
2004 break;
Andrea Arcangeli63b2d412020-04-06 20:06:12 -07002005 case UFFDIO_WRITEPROTECT:
2006 ret = userfaultfd_writeprotect(ctx, arg);
2007 break;
Axel Rasmussen4a5cf922021-03-18 17:01:50 +11002008 case UFFDIO_CONTINUE:
2009 ret = userfaultfd_continue(ctx, arg);
2010 break;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002011 }
2012 return ret;
2013}
2014
2015#ifdef CONFIG_PROC_FS
2016static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
2017{
2018 struct userfaultfd_ctx *ctx = f->private_data;
Ingo Molnarac6424b2017-06-20 12:06:13 +02002019 wait_queue_entry_t *wq;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002020 unsigned long pending = 0, total = 0;
2021
Eric Biggerscbcfa132019-07-04 15:14:39 -07002022 spin_lock_irq(&ctx->fault_pending_wqh.lock);
Ingo Molnar2055da92017-06-20 12:06:46 +02002023 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07002024 pending++;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002025 total++;
2026 }
Ingo Molnar2055da92017-06-20 12:06:46 +02002027 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
Andrea Arcangeli15b726e2015-09-04 15:46:44 -07002028 total++;
2029 }
Eric Biggerscbcfa132019-07-04 15:14:39 -07002030 spin_unlock_irq(&ctx->fault_pending_wqh.lock);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002031
2032 /*
2033 * If more protocols will be added, there will be all shown
2034 * separated by a space. Like this:
2035 * protocols: aa:... bb:...
2036 */
2037 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
Mike Rapoport045098e2017-04-07 16:04:42 -07002038 pending, total, UFFD_API, ctx->features,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002039 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
2040}
2041#endif
2042
2043static const struct file_operations userfaultfd_fops = {
2044#ifdef CONFIG_PROC_FS
2045 .show_fdinfo = userfaultfd_show_fdinfo,
2046#endif
2047 .release = userfaultfd_release,
2048 .poll = userfaultfd_poll,
2049 .read = userfaultfd_read,
2050 .unlocked_ioctl = userfaultfd_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +02002051 .compat_ioctl = compat_ptr_ioctl,
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002052 .llseek = noop_llseek,
2053};
2054
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002055static void init_once_userfaultfd_ctx(void *mem)
2056{
2057 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
2058
2059 init_waitqueue_head(&ctx->fault_pending_wqh);
2060 init_waitqueue_head(&ctx->fault_wqh);
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08002061 init_waitqueue_head(&ctx->event_wqh);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002062 init_waitqueue_head(&ctx->fd_wqh);
Ahmed S. Darwish2ca97ac2020-07-20 17:55:28 +02002063 seqcount_spinlock_init(&ctx->refile_seq, &ctx->fault_pending_wqh.lock);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002064}
2065
Eric Biggers284cd242018-01-31 16:19:48 -08002066SYSCALL_DEFINE1(userfaultfd, int, flags)
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002067{
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002068 struct userfaultfd_ctx *ctx;
Eric Biggers284cd242018-01-31 16:19:48 -08002069 int fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002070
Lokesh Gidra6a6bc062020-12-14 19:13:54 -08002071 if (!sysctl_unprivileged_userfaultfd &&
2072 (flags & UFFD_USER_MODE_ONLY) == 0 &&
2073 !capable(CAP_SYS_PTRACE)) {
2074 printk_once(KERN_WARNING "uffd: Set unprivileged_userfaultfd "
2075 "sysctl knob to 1 if kernel faults must be handled "
2076 "without obtaining CAP_SYS_PTRACE capability\n");
Peter Xucefdca02019-05-13 17:16:41 -07002077 return -EPERM;
Lokesh Gidra6a6bc062020-12-14 19:13:54 -08002078 }
Peter Xucefdca02019-05-13 17:16:41 -07002079
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002080 BUG_ON(!current->mm);
2081
2082 /* Check the UFFD_* constants for consistency. */
Lokesh Gidrab8af1f92020-12-14 19:13:49 -08002083 BUILD_BUG_ON(UFFD_USER_MODE_ONLY & UFFD_SHARED_FCNTL_FLAGS);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002084 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
2085 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
2086
Lokesh Gidrab8af1f92020-12-14 19:13:49 -08002087 if (flags & ~(UFFD_SHARED_FCNTL_FLAGS | UFFD_USER_MODE_ONLY))
Eric Biggers284cd242018-01-31 16:19:48 -08002088 return -EINVAL;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002089
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002090 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002091 if (!ctx)
Eric Biggers284cd242018-01-31 16:19:48 -08002092 return -ENOMEM;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002093
Eric Biggersca880422018-12-28 00:34:43 -08002094 refcount_set(&ctx->refcount, 1);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002095 ctx->flags = flags;
Pavel Emelyanov9cd75c32017-02-22 15:42:21 -08002096 ctx->features = 0;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002097 ctx->released = false;
Mike Rapoportdf2cc962018-06-07 17:09:25 -07002098 ctx->mmap_changing = false;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002099 ctx->mm = current->mm;
2100 /* prevent the mm struct to be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -08002101 mmgrab(ctx->mm);
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002102
Daniel Colascionedbc935c2021-01-08 14:22:23 -08002103 fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, ctx,
2104 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL);
Eric Biggers284cd242018-01-31 16:19:48 -08002105 if (fd < 0) {
Oleg Nesterovd2005e32016-05-20 16:58:36 -07002106 mmdrop(ctx->mm);
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002107 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
Eric Biggersc03e9462015-09-17 16:01:54 -07002108 }
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002109 return fd;
Andrea Arcangeli86039bd2015-09-04 15:46:31 -07002110}
Andrea Arcangeli3004ec92015-09-04 15:46:48 -07002111
2112static int __init userfaultfd_init(void)
2113{
2114 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
2115 sizeof(struct userfaultfd_ctx),
2116 0,
2117 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2118 init_once_userfaultfd_ctx);
2119 return 0;
2120}
2121__initcall(userfaultfd_init);