Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 2 | /* |
| 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 Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 9 | * Some part derived from fs/eventfd.c (anon inode setup) and |
| 10 | * mm/ksm.c (mm hashing). |
| 11 | */ |
| 12 | |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 13 | #include <linux/list.h> |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 14 | #include <linux/hashtable.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 15 | #include <linux/sched/signal.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 16 | #include <linux/sched/mm.h> |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <linux/poll.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/seq_file.h> |
| 21 | #include <linux/file.h> |
| 22 | #include <linux/bug.h> |
| 23 | #include <linux/anon_inodes.h> |
| 24 | #include <linux/syscalls.h> |
| 25 | #include <linux/userfaultfd_k.h> |
| 26 | #include <linux/mempolicy.h> |
| 27 | #include <linux/ioctl.h> |
| 28 | #include <linux/security.h> |
Mike Kravetz | cab350a | 2017-02-22 15:43:04 -0800 | [diff] [blame] | 29 | #include <linux/hugetlb.h> |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 30 | |
Peter Xu | cefdca0 | 2019-05-13 17:16:41 -0700 | [diff] [blame] | 31 | int sysctl_unprivileged_userfaultfd __read_mostly = 1; |
| 32 | |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 33 | static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly; |
| 34 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 35 | enum userfaultfd_state { |
| 36 | UFFD_STATE_WAIT_API, |
| 37 | UFFD_STATE_RUNNING, |
| 38 | }; |
| 39 | |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Start with fault_pending_wqh and fault_wqh so they're more likely |
| 42 | * to be in the same cacheline. |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 43 | * |
| 44 | * Locking order: |
| 45 | * fd_wqh.lock |
| 46 | * fault_pending_wqh.lock |
| 47 | * fault_wqh.lock |
| 48 | * event_wqh.lock |
| 49 | * |
| 50 | * To avoid deadlocks, IRQs must be disabled when taking any of the above locks, |
| 51 | * since fd_wqh.lock is taken by aio_poll() while it's holding a lock that's |
| 52 | * also taken in IRQ context. |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 53 | */ |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 54 | struct userfaultfd_ctx { |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 55 | /* waitqueue head for the pending (i.e. not read) userfaults */ |
| 56 | wait_queue_head_t fault_pending_wqh; |
| 57 | /* waitqueue head for the userfaults */ |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 58 | wait_queue_head_t fault_wqh; |
| 59 | /* waitqueue head for the pseudo fd to wakeup poll/read */ |
| 60 | wait_queue_head_t fd_wqh; |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 61 | /* waitqueue head for events */ |
| 62 | wait_queue_head_t event_wqh; |
Andrea Arcangeli | 2c5b7e1 | 2015-09-04 15:47:23 -0700 | [diff] [blame] | 63 | /* a refile sequence protected by fault_pending_wqh lock */ |
| 64 | struct seqcount refile_seq; |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 65 | /* pseudo fd refcounting */ |
Eric Biggers | ca88042 | 2018-12-28 00:34:43 -0800 | [diff] [blame] | 66 | refcount_t refcount; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 67 | /* userfaultfd syscall flags */ |
| 68 | unsigned int flags; |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 69 | /* features requested from the userspace */ |
| 70 | unsigned int features; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 71 | /* state machine */ |
| 72 | enum userfaultfd_state state; |
| 73 | /* released */ |
| 74 | bool released; |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 75 | /* memory mappings are changing because of non-cooperative event */ |
| 76 | bool mmap_changing; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 77 | /* mm with one ore more vmas attached to this userfaultfd_ctx */ |
| 78 | struct mm_struct *mm; |
| 79 | }; |
| 80 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 81 | struct userfaultfd_fork_ctx { |
| 82 | struct userfaultfd_ctx *orig; |
| 83 | struct userfaultfd_ctx *new; |
| 84 | struct list_head list; |
| 85 | }; |
| 86 | |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 87 | struct userfaultfd_unmap_ctx { |
| 88 | struct userfaultfd_ctx *ctx; |
| 89 | unsigned long start; |
| 90 | unsigned long end; |
| 91 | struct list_head list; |
| 92 | }; |
| 93 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 94 | struct userfaultfd_wait_queue { |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 95 | struct uffd_msg msg; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 96 | wait_queue_entry_t wq; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 97 | struct userfaultfd_ctx *ctx; |
Andrea Arcangeli | 15a77c6 | 2017-01-24 15:17:59 -0800 | [diff] [blame] | 98 | bool waken; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct userfaultfd_wake_range { |
| 102 | unsigned long start; |
| 103 | unsigned long len; |
| 104 | }; |
| 105 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 106 | static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode, |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 107 | int wake_flags, void *key) |
| 108 | { |
| 109 | struct userfaultfd_wake_range *range = key; |
| 110 | int ret; |
| 111 | struct userfaultfd_wait_queue *uwq; |
| 112 | unsigned long start, len; |
| 113 | |
| 114 | uwq = container_of(wq, struct userfaultfd_wait_queue, wq); |
| 115 | ret = 0; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 116 | /* len == 0 means wake all */ |
| 117 | start = range->start; |
| 118 | len = range->len; |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 119 | if (len && (start > uwq->msg.arg.pagefault.address || |
| 120 | start + len <= uwq->msg.arg.pagefault.address)) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 121 | goto out; |
Andrea Arcangeli | 15a77c6 | 2017-01-24 15:17:59 -0800 | [diff] [blame] | 122 | WRITE_ONCE(uwq->waken, true); |
| 123 | /* |
Peter Zijlstra | a9668cd | 2017-06-07 17:51:27 +0200 | [diff] [blame] | 124 | * The Program-Order guarantees provided by the scheduler |
| 125 | * ensure uwq->waken is visible before the task is woken. |
Andrea Arcangeli | 15a77c6 | 2017-01-24 15:17:59 -0800 | [diff] [blame] | 126 | */ |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 127 | ret = wake_up_state(wq->private, mode); |
Peter Zijlstra | a9668cd | 2017-06-07 17:51:27 +0200 | [diff] [blame] | 128 | if (ret) { |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 129 | /* |
| 130 | * Wake only once, autoremove behavior. |
| 131 | * |
Peter Zijlstra | a9668cd | 2017-06-07 17:51:27 +0200 | [diff] [blame] | 132 | * After the effect of list_del_init is visible to the other |
| 133 | * CPUs, the waitqueue may disappear from under us, see the |
| 134 | * !list_empty_careful() in handle_userfault(). |
| 135 | * |
| 136 | * try_to_wake_up() has an implicit smp_mb(), and the |
| 137 | * wq->private is read before calling the extern function |
| 138 | * "wake_up_state" (which in turns calls try_to_wake_up). |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 139 | */ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 140 | list_del_init(&wq->entry); |
Peter Zijlstra | a9668cd | 2017-06-07 17:51:27 +0200 | [diff] [blame] | 141 | } |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 142 | out: |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd |
| 148 | * context. |
| 149 | * @ctx: [in] Pointer to the userfaultfd context. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 150 | */ |
| 151 | static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx) |
| 152 | { |
Eric Biggers | ca88042 | 2018-12-28 00:34:43 -0800 | [diff] [blame] | 153 | refcount_inc(&ctx->refcount); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd |
| 158 | * context. |
| 159 | * @ctx: [in] Pointer to userfaultfd context. |
| 160 | * |
| 161 | * The userfaultfd context reference must have been previously acquired either |
| 162 | * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget(). |
| 163 | */ |
| 164 | static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx) |
| 165 | { |
Eric Biggers | ca88042 | 2018-12-28 00:34:43 -0800 | [diff] [blame] | 166 | if (refcount_dec_and_test(&ctx->refcount)) { |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 167 | VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock)); |
| 168 | VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh)); |
| 169 | VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock)); |
| 170 | VM_BUG_ON(waitqueue_active(&ctx->fault_wqh)); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 171 | VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock)); |
| 172 | VM_BUG_ON(waitqueue_active(&ctx->event_wqh)); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 173 | VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock)); |
| 174 | VM_BUG_ON(waitqueue_active(&ctx->fd_wqh)); |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 175 | mmdrop(ctx->mm); |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 176 | kmem_cache_free(userfaultfd_ctx_cachep, ctx); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 180 | static inline void msg_init(struct uffd_msg *msg) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 181 | { |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 182 | BUILD_BUG_ON(sizeof(struct uffd_msg) != 32); |
| 183 | /* |
| 184 | * Must use memset to zero out the paddings or kernel data is |
| 185 | * leaked to userland. |
| 186 | */ |
| 187 | memset(msg, 0, sizeof(struct uffd_msg)); |
| 188 | } |
| 189 | |
| 190 | static inline struct uffd_msg userfault_msg(unsigned long address, |
| 191 | unsigned int flags, |
Alexey Perevalov | 9d4ac93 | 2017-09-06 16:23:56 -0700 | [diff] [blame] | 192 | unsigned long reason, |
| 193 | unsigned int features) |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 194 | { |
| 195 | struct uffd_msg msg; |
| 196 | msg_init(&msg); |
| 197 | msg.event = UFFD_EVENT_PAGEFAULT; |
| 198 | msg.arg.pagefault.address = address; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 199 | if (flags & FAULT_FLAG_WRITE) |
| 200 | /* |
Andrea Arcangeli | a4605a6 | 2017-02-22 15:42:09 -0800 | [diff] [blame] | 201 | * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 202 | * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE |
| 203 | * was not set in a UFFD_EVENT_PAGEFAULT, it means it |
| 204 | * was a read fault, otherwise if set it means it's |
| 205 | * a write fault. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 206 | */ |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 207 | msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 208 | if (reason & VM_UFFD_WP) |
| 209 | /* |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 210 | * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the |
| 211 | * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was |
| 212 | * not set in a UFFD_EVENT_PAGEFAULT, it means it was |
| 213 | * a missing fault, otherwise if set it means it's a |
| 214 | * write protect fault. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 215 | */ |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 216 | msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP; |
Alexey Perevalov | 9d4ac93 | 2017-09-06 16:23:56 -0700 | [diff] [blame] | 217 | if (features & UFFD_FEATURE_THREAD_ID) |
Andrea Arcangeli | a36985d | 2017-09-06 16:23:59 -0700 | [diff] [blame] | 218 | msg.arg.pagefault.feat.ptid = task_pid_vnr(current); |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 219 | return msg; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 222 | #ifdef CONFIG_HUGETLB_PAGE |
| 223 | /* |
| 224 | * Same functionality as userfaultfd_must_wait below with modifications for |
| 225 | * hugepmd ranges. |
| 226 | */ |
| 227 | static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, |
Punit Agrawal | 7868a20 | 2017-07-06 15:39:42 -0700 | [diff] [blame] | 228 | struct vm_area_struct *vma, |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 229 | unsigned long address, |
| 230 | unsigned long flags, |
| 231 | unsigned long reason) |
| 232 | { |
| 233 | struct mm_struct *mm = ctx->mm; |
Janosch Frank | 1e2c043 | 2018-07-03 17:02:39 -0700 | [diff] [blame] | 234 | pte_t *ptep, pte; |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 235 | bool ret = true; |
| 236 | |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 237 | mmap_assert_locked(mm); |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 238 | |
Janosch Frank | 1e2c043 | 2018-07-03 17:02:39 -0700 | [diff] [blame] | 239 | ptep = huge_pte_offset(mm, address, vma_mmu_pagesize(vma)); |
| 240 | |
| 241 | if (!ptep) |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 242 | goto out; |
| 243 | |
| 244 | ret = false; |
Janosch Frank | 1e2c043 | 2018-07-03 17:02:39 -0700 | [diff] [blame] | 245 | pte = huge_ptep_get(ptep); |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 246 | |
| 247 | /* |
| 248 | * Lockless access: we're in a wait_event so it's ok if it |
| 249 | * changes under us. |
| 250 | */ |
Janosch Frank | 1e2c043 | 2018-07-03 17:02:39 -0700 | [diff] [blame] | 251 | if (huge_pte_none(pte)) |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 252 | ret = true; |
Janosch Frank | 1e2c043 | 2018-07-03 17:02:39 -0700 | [diff] [blame] | 253 | if (!huge_pte_write(pte) && (reason & VM_UFFD_WP)) |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 254 | ret = true; |
| 255 | out: |
| 256 | return ret; |
| 257 | } |
| 258 | #else |
| 259 | static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, |
Punit Agrawal | 7868a20 | 2017-07-06 15:39:42 -0700 | [diff] [blame] | 260 | struct vm_area_struct *vma, |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 261 | unsigned long address, |
| 262 | unsigned long flags, |
| 263 | unsigned long reason) |
| 264 | { |
| 265 | return false; /* should never get here */ |
| 266 | } |
| 267 | #endif /* CONFIG_HUGETLB_PAGE */ |
| 268 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 269 | /* |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 270 | * Verify the pagetables are still not ok after having reigstered into |
| 271 | * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any |
| 272 | * userfault that has already been resolved, if userfaultfd_read and |
| 273 | * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different |
| 274 | * threads. |
| 275 | */ |
| 276 | static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, |
| 277 | unsigned long address, |
| 278 | unsigned long flags, |
| 279 | unsigned long reason) |
| 280 | { |
| 281 | struct mm_struct *mm = ctx->mm; |
| 282 | pgd_t *pgd; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 283 | p4d_t *p4d; |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 284 | pud_t *pud; |
| 285 | pmd_t *pmd, _pmd; |
| 286 | pte_t *pte; |
| 287 | bool ret = true; |
| 288 | |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 289 | mmap_assert_locked(mm); |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 290 | |
| 291 | pgd = pgd_offset(mm, address); |
| 292 | if (!pgd_present(*pgd)) |
| 293 | goto out; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 294 | p4d = p4d_offset(pgd, address); |
| 295 | if (!p4d_present(*p4d)) |
| 296 | goto out; |
| 297 | pud = pud_offset(p4d, address); |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 298 | if (!pud_present(*pud)) |
| 299 | goto out; |
| 300 | pmd = pmd_offset(pud, address); |
| 301 | /* |
| 302 | * READ_ONCE must function as a barrier with narrower scope |
| 303 | * and it must be equivalent to: |
| 304 | * _pmd = *pmd; barrier(); |
| 305 | * |
| 306 | * This is to deal with the instability (as in |
| 307 | * pmd_trans_unstable) of the pmd. |
| 308 | */ |
| 309 | _pmd = READ_ONCE(*pmd); |
Huang Ying | a365ac0 | 2018-01-31 16:17:32 -0800 | [diff] [blame] | 310 | if (pmd_none(_pmd)) |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 311 | goto out; |
| 312 | |
| 313 | ret = false; |
Huang Ying | a365ac0 | 2018-01-31 16:17:32 -0800 | [diff] [blame] | 314 | if (!pmd_present(_pmd)) |
| 315 | goto out; |
| 316 | |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 317 | if (pmd_trans_huge(_pmd)) { |
| 318 | if (!pmd_write(_pmd) && (reason & VM_UFFD_WP)) |
| 319 | ret = true; |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 320 | goto out; |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 321 | } |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it |
| 325 | * and use the standard pte_offset_map() instead of parsing _pmd. |
| 326 | */ |
| 327 | pte = pte_offset_map(pmd, address); |
| 328 | /* |
| 329 | * Lockless access: we're in a wait_event so it's ok if it |
| 330 | * changes under us. |
| 331 | */ |
| 332 | if (pte_none(*pte)) |
| 333 | ret = true; |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 334 | if (!pte_write(*pte) && (reason & VM_UFFD_WP)) |
| 335 | ret = true; |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 336 | pte_unmap(pte); |
| 337 | |
| 338 | out: |
| 339 | return ret; |
| 340 | } |
| 341 | |
Peter Xu | 3e69ad0 | 2020-04-01 21:09:00 -0700 | [diff] [blame] | 342 | static inline long userfaultfd_get_blocking_state(unsigned int flags) |
| 343 | { |
| 344 | if (flags & FAULT_FLAG_INTERRUPTIBLE) |
| 345 | return TASK_INTERRUPTIBLE; |
| 346 | |
| 347 | if (flags & FAULT_FLAG_KILLABLE) |
| 348 | return TASK_KILLABLE; |
| 349 | |
| 350 | return TASK_UNINTERRUPTIBLE; |
| 351 | } |
| 352 | |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 353 | /* |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 354 | * The locking rules involved in returning VM_FAULT_RETRY depending on |
| 355 | * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and |
| 356 | * FAULT_FLAG_KILLABLE are not straightforward. The "Caution" |
| 357 | * recommendation in __lock_page_or_retry is not an understatement. |
| 358 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 359 | * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_lock must be released |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 360 | * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is |
| 361 | * not set. |
| 362 | * |
| 363 | * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not |
| 364 | * set, VM_FAULT_RETRY can still be returned if and only if there are |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 365 | * fatal_signal_pending()s, and the mmap_lock must be released before |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 366 | * returning it. |
| 367 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 368 | vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 369 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 370 | struct mm_struct *mm = vmf->vma->vm_mm; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 371 | struct userfaultfd_ctx *ctx; |
| 372 | struct userfaultfd_wait_queue uwq; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 373 | vm_fault_t ret = VM_FAULT_SIGBUS; |
Peter Xu | 3e69ad0 | 2020-04-01 21:09:00 -0700 | [diff] [blame] | 374 | bool must_wait; |
Andrea Arcangeli | 15a77c6 | 2017-01-24 15:17:59 -0800 | [diff] [blame] | 375 | long blocking_state; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 376 | |
Andrea Arcangeli | 64c2b20 | 2017-06-16 14:02:37 -0700 | [diff] [blame] | 377 | /* |
| 378 | * We don't do userfault handling for the final child pid update. |
| 379 | * |
| 380 | * We also don't do userfault handling during |
| 381 | * coredumping. hugetlbfs has the special |
| 382 | * follow_hugetlb_page() to skip missing pages in the |
| 383 | * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with |
| 384 | * the no_page_table() helper in follow_page_mask(), but the |
| 385 | * shmem_vm_ops->fault method is invoked even during |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 386 | * coredumping without mmap_lock and it ends up here. |
Andrea Arcangeli | 64c2b20 | 2017-06-16 14:02:37 -0700 | [diff] [blame] | 387 | */ |
| 388 | if (current->flags & (PF_EXITING|PF_DUMPCORE)) |
| 389 | goto out; |
| 390 | |
| 391 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 392 | * Coredumping runs without mmap_lock so we can only check that |
| 393 | * the mmap_lock is held, if PF_DUMPCORE was not set. |
Andrea Arcangeli | 64c2b20 | 2017-06-16 14:02:37 -0700 | [diff] [blame] | 394 | */ |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 395 | mmap_assert_locked(mm); |
Andrea Arcangeli | 64c2b20 | 2017-06-16 14:02:37 -0700 | [diff] [blame] | 396 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 397 | ctx = vmf->vma->vm_userfaultfd_ctx.ctx; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 398 | if (!ctx) |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 399 | goto out; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 400 | |
| 401 | BUG_ON(ctx->mm != mm); |
| 402 | |
| 403 | VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP)); |
| 404 | VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP)); |
| 405 | |
Prakash Sangappa | 2d6d6f5 | 2017-09-06 16:23:39 -0700 | [diff] [blame] | 406 | if (ctx->features & UFFD_FEATURE_SIGBUS) |
| 407 | goto out; |
| 408 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 409 | /* |
| 410 | * If it's already released don't get it. This avoids to loop |
| 411 | * in __get_user_pages if userfaultfd_release waits on the |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 412 | * caller of handle_userfault to release the mmap_lock. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 413 | */ |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 414 | if (unlikely(READ_ONCE(ctx->released))) { |
Andrea Arcangeli | 656710a | 2017-09-08 16:12:42 -0700 | [diff] [blame] | 415 | /* |
| 416 | * Don't return VM_FAULT_SIGBUS in this case, so a non |
| 417 | * cooperative manager can close the uffd after the |
| 418 | * last UFFDIO_COPY, without risking to trigger an |
| 419 | * involuntary SIGBUS if the process was starting the |
| 420 | * userfaultfd while the userfaultfd was still armed |
| 421 | * (but after the last UFFDIO_COPY). If the uffd |
| 422 | * wasn't already closed when the userfault reached |
| 423 | * this point, that would normally be solved by |
| 424 | * userfaultfd_must_wait returning 'false'. |
| 425 | * |
| 426 | * If we were to return VM_FAULT_SIGBUS here, the non |
| 427 | * cooperative manager would be instead forced to |
| 428 | * always call UFFDIO_UNREGISTER before it can safely |
| 429 | * close the uffd. |
| 430 | */ |
| 431 | ret = VM_FAULT_NOPAGE; |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 432 | goto out; |
Andrea Arcangeli | 656710a | 2017-09-08 16:12:42 -0700 | [diff] [blame] | 433 | } |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * Check that we can return VM_FAULT_RETRY. |
| 437 | * |
| 438 | * NOTE: it should become possible to return VM_FAULT_RETRY |
| 439 | * even if FAULT_FLAG_TRIED is set without leading to gup() |
| 440 | * -EBUSY failures, if the userfaultfd is to be extended for |
| 441 | * VM_UFFD_WP tracking and we intend to arm the userfault |
| 442 | * without first stopping userland access to the memory. For |
| 443 | * VM_UFFD_MISSING userfaults this is enough for now. |
| 444 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 445 | if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) { |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 446 | /* |
| 447 | * Validate the invariant that nowait must allow retry |
| 448 | * to be sure not to return SIGBUS erroneously on |
| 449 | * nowait invocations. |
| 450 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 451 | BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 452 | #ifdef CONFIG_DEBUG_VM |
| 453 | if (printk_ratelimit()) { |
| 454 | printk(KERN_WARNING |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 455 | "FAULT_FLAG_ALLOW_RETRY missing %x\n", |
| 456 | vmf->flags); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 457 | dump_stack(); |
| 458 | } |
| 459 | #endif |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 460 | goto out; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Handle nowait, not much to do other than tell it to retry |
| 465 | * and wait. |
| 466 | */ |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 467 | ret = VM_FAULT_RETRY; |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 468 | if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 469 | goto out; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 470 | |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 471 | /* take the reference before dropping the mmap_lock */ |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 472 | userfaultfd_ctx_get(ctx); |
| 473 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 474 | init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function); |
| 475 | uwq.wq.private = current; |
Alexey Perevalov | 9d4ac93 | 2017-09-06 16:23:56 -0700 | [diff] [blame] | 476 | uwq.msg = userfault_msg(vmf->address, vmf->flags, reason, |
| 477 | ctx->features); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 478 | uwq.ctx = ctx; |
Andrea Arcangeli | 15a77c6 | 2017-01-24 15:17:59 -0800 | [diff] [blame] | 479 | uwq.waken = false; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 480 | |
Peter Xu | 3e69ad0 | 2020-04-01 21:09:00 -0700 | [diff] [blame] | 481 | blocking_state = userfaultfd_get_blocking_state(vmf->flags); |
Andrea Arcangeli | dfa37dc | 2015-09-04 15:47:18 -0700 | [diff] [blame] | 482 | |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 483 | spin_lock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 484 | /* |
| 485 | * After the __add_wait_queue the uwq is visible to userland |
| 486 | * through poll/read(). |
| 487 | */ |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 488 | __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq); |
| 489 | /* |
| 490 | * The smp_mb() after __set_current_state prevents the reads |
| 491 | * following the spin_unlock to happen before the list_add in |
| 492 | * __add_wait_queue. |
| 493 | */ |
Andrea Arcangeli | 15a77c6 | 2017-01-24 15:17:59 -0800 | [diff] [blame] | 494 | set_current_state(blocking_state); |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 495 | spin_unlock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 496 | |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 497 | if (!is_vm_hugetlb_page(vmf->vma)) |
| 498 | must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags, |
| 499 | reason); |
| 500 | else |
Punit Agrawal | 7868a20 | 2017-07-06 15:39:42 -0700 | [diff] [blame] | 501 | must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma, |
| 502 | vmf->address, |
Mike Kravetz | 369cd21 | 2017-02-22 15:43:10 -0800 | [diff] [blame] | 503 | vmf->flags, reason); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 504 | mmap_read_unlock(mm); |
Andrea Arcangeli | 8d2afd9 | 2015-09-04 15:46:51 -0700 | [diff] [blame] | 505 | |
Linus Torvalds | f9bf352 | 2020-08-02 10:42:31 -0700 | [diff] [blame] | 506 | if (likely(must_wait && !READ_ONCE(ctx->released))) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 507 | wake_up_poll(&ctx->fd_wqh, EPOLLIN); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 508 | schedule(); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 509 | } |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 510 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 511 | __set_current_state(TASK_RUNNING); |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * Here we race with the list_del; list_add in |
| 515 | * userfaultfd_ctx_read(), however because we don't ever run |
| 516 | * list_del_init() to refile across the two lists, the prev |
| 517 | * and next pointers will never point to self. list_add also |
| 518 | * would never let any of the two pointers to point to |
| 519 | * self. So list_empty_careful won't risk to see both pointers |
| 520 | * pointing to self at any time during the list refile. The |
| 521 | * only case where list_del_init() is called is the full |
| 522 | * removal in the wake function and there we don't re-list_add |
| 523 | * and it's fine not to block on the spinlock. The uwq on this |
| 524 | * kernel stack can be released after the list_del_init. |
| 525 | */ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 526 | if (!list_empty_careful(&uwq.wq.entry)) { |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 527 | spin_lock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 528 | /* |
| 529 | * No need of list_del_init(), the uwq on the stack |
| 530 | * will be freed shortly anyway. |
| 531 | */ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 532 | list_del(&uwq.wq.entry); |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 533 | spin_unlock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 534 | } |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * ctx may go away after this if the userfault pseudo fd is |
| 538 | * already released. |
| 539 | */ |
| 540 | userfaultfd_ctx_put(ctx); |
| 541 | |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 542 | out: |
| 543 | return ret; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Andrea Arcangeli | 8c9e7bb | 2017-03-09 16:16:54 -0800 | [diff] [blame] | 546 | static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx, |
| 547 | struct userfaultfd_wait_queue *ewq) |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 548 | { |
Andrea Arcangeli | 0cbb4b4 | 2018-01-04 16:18:09 -0800 | [diff] [blame] | 549 | struct userfaultfd_ctx *release_new_ctx; |
| 550 | |
Andrea Arcangeli | 9a69a82 | 2017-03-09 16:16:52 -0800 | [diff] [blame] | 551 | if (WARN_ON_ONCE(current->flags & PF_EXITING)) |
| 552 | goto out; |
| 553 | |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 554 | ewq->ctx = ctx; |
| 555 | init_waitqueue_entry(&ewq->wq, current); |
Andrea Arcangeli | 0cbb4b4 | 2018-01-04 16:18:09 -0800 | [diff] [blame] | 556 | release_new_ctx = NULL; |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 557 | |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 558 | spin_lock_irq(&ctx->event_wqh.lock); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 559 | /* |
| 560 | * After the __add_wait_queue the uwq is visible to userland |
| 561 | * through poll/read(). |
| 562 | */ |
| 563 | __add_wait_queue(&ctx->event_wqh, &ewq->wq); |
| 564 | for (;;) { |
| 565 | set_current_state(TASK_KILLABLE); |
| 566 | if (ewq->msg.event == 0) |
| 567 | break; |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 568 | if (READ_ONCE(ctx->released) || |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 569 | fatal_signal_pending(current)) { |
Andrea Arcangeli | 384632e | 2017-10-03 16:15:38 -0700 | [diff] [blame] | 570 | /* |
| 571 | * &ewq->wq may be queued in fork_event, but |
| 572 | * __remove_wait_queue ignores the head |
| 573 | * parameter. It would be a problem if it |
| 574 | * didn't. |
| 575 | */ |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 576 | __remove_wait_queue(&ctx->event_wqh, &ewq->wq); |
Mike Rapoport | 7eb76d4 | 2017-03-09 16:17:09 -0800 | [diff] [blame] | 577 | if (ewq->msg.event == UFFD_EVENT_FORK) { |
| 578 | struct userfaultfd_ctx *new; |
| 579 | |
| 580 | new = (struct userfaultfd_ctx *) |
| 581 | (unsigned long) |
| 582 | ewq->msg.arg.reserved.reserved1; |
Andrea Arcangeli | 0cbb4b4 | 2018-01-04 16:18:09 -0800 | [diff] [blame] | 583 | release_new_ctx = new; |
Mike Rapoport | 7eb76d4 | 2017-03-09 16:17:09 -0800 | [diff] [blame] | 584 | } |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 588 | spin_unlock_irq(&ctx->event_wqh.lock); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 589 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 590 | wake_up_poll(&ctx->fd_wqh, EPOLLIN); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 591 | schedule(); |
| 592 | |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 593 | spin_lock_irq(&ctx->event_wqh.lock); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 594 | } |
| 595 | __set_current_state(TASK_RUNNING); |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 596 | spin_unlock_irq(&ctx->event_wqh.lock); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 597 | |
Andrea Arcangeli | 0cbb4b4 | 2018-01-04 16:18:09 -0800 | [diff] [blame] | 598 | if (release_new_ctx) { |
| 599 | struct vm_area_struct *vma; |
| 600 | struct mm_struct *mm = release_new_ctx->mm; |
| 601 | |
| 602 | /* the various vma->vm_userfaultfd_ctx still points to it */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 603 | mmap_write_lock(mm); |
Andrea Arcangeli | 04f5866 | 2019-04-18 17:50:52 -0700 | [diff] [blame] | 604 | /* no task can run (and in turn coredump) yet */ |
| 605 | VM_WARN_ON(!mmget_still_valid(mm)); |
Andrea Arcangeli | 0cbb4b4 | 2018-01-04 16:18:09 -0800 | [diff] [blame] | 606 | for (vma = mm->mmap; vma; vma = vma->vm_next) |
Mike Rapoport | 31e810a | 2018-08-02 15:36:09 -0700 | [diff] [blame] | 607 | if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) { |
Andrea Arcangeli | 0cbb4b4 | 2018-01-04 16:18:09 -0800 | [diff] [blame] | 608 | vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; |
Mike Rapoport | 31e810a | 2018-08-02 15:36:09 -0700 | [diff] [blame] | 609 | vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING); |
| 610 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 611 | mmap_write_unlock(mm); |
Andrea Arcangeli | 0cbb4b4 | 2018-01-04 16:18:09 -0800 | [diff] [blame] | 612 | |
| 613 | userfaultfd_ctx_put(release_new_ctx); |
| 614 | } |
| 615 | |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 616 | /* |
| 617 | * ctx may go away after this if the userfault pseudo fd is |
| 618 | * already released. |
| 619 | */ |
Andrea Arcangeli | 9a69a82 | 2017-03-09 16:16:52 -0800 | [diff] [blame] | 620 | out: |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 621 | WRITE_ONCE(ctx->mmap_changing, false); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 622 | userfaultfd_ctx_put(ctx); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx, |
| 626 | struct userfaultfd_wait_queue *ewq) |
| 627 | { |
| 628 | ewq->msg.event = 0; |
| 629 | wake_up_locked(&ctx->event_wqh); |
| 630 | __remove_wait_queue(&ctx->event_wqh, &ewq->wq); |
| 631 | } |
| 632 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 633 | int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs) |
| 634 | { |
| 635 | struct userfaultfd_ctx *ctx = NULL, *octx; |
| 636 | struct userfaultfd_fork_ctx *fctx; |
| 637 | |
| 638 | octx = vma->vm_userfaultfd_ctx.ctx; |
| 639 | if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) { |
| 640 | vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; |
| 641 | vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING); |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | list_for_each_entry(fctx, fcs, list) |
| 646 | if (fctx->orig == octx) { |
| 647 | ctx = fctx->new; |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | if (!ctx) { |
| 652 | fctx = kmalloc(sizeof(*fctx), GFP_KERNEL); |
| 653 | if (!fctx) |
| 654 | return -ENOMEM; |
| 655 | |
| 656 | ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL); |
| 657 | if (!ctx) { |
| 658 | kfree(fctx); |
| 659 | return -ENOMEM; |
| 660 | } |
| 661 | |
Eric Biggers | ca88042 | 2018-12-28 00:34:43 -0800 | [diff] [blame] | 662 | refcount_set(&ctx->refcount, 1); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 663 | ctx->flags = octx->flags; |
| 664 | ctx->state = UFFD_STATE_RUNNING; |
| 665 | ctx->features = octx->features; |
| 666 | ctx->released = false; |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 667 | ctx->mmap_changing = false; |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 668 | ctx->mm = vma->vm_mm; |
Mike Rapoport | 00bb31f | 2017-11-15 17:36:56 -0800 | [diff] [blame] | 669 | mmgrab(ctx->mm); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 670 | |
| 671 | userfaultfd_ctx_get(octx); |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 672 | WRITE_ONCE(octx->mmap_changing, true); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 673 | fctx->orig = octx; |
| 674 | fctx->new = ctx; |
| 675 | list_add_tail(&fctx->list, fcs); |
| 676 | } |
| 677 | |
| 678 | vma->vm_userfaultfd_ctx.ctx = ctx; |
| 679 | return 0; |
| 680 | } |
| 681 | |
Andrea Arcangeli | 8c9e7bb | 2017-03-09 16:16:54 -0800 | [diff] [blame] | 682 | static void dup_fctx(struct userfaultfd_fork_ctx *fctx) |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 683 | { |
| 684 | struct userfaultfd_ctx *ctx = fctx->orig; |
| 685 | struct userfaultfd_wait_queue ewq; |
| 686 | |
| 687 | msg_init(&ewq.msg); |
| 688 | |
| 689 | ewq.msg.event = UFFD_EVENT_FORK; |
| 690 | ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new; |
| 691 | |
Andrea Arcangeli | 8c9e7bb | 2017-03-09 16:16:54 -0800 | [diff] [blame] | 692 | userfaultfd_event_wait_completion(ctx, &ewq); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void dup_userfaultfd_complete(struct list_head *fcs) |
| 696 | { |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 697 | struct userfaultfd_fork_ctx *fctx, *n; |
| 698 | |
| 699 | list_for_each_entry_safe(fctx, n, fcs, list) { |
Andrea Arcangeli | 8c9e7bb | 2017-03-09 16:16:54 -0800 | [diff] [blame] | 700 | dup_fctx(fctx); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 701 | list_del(&fctx->list); |
| 702 | kfree(fctx); |
| 703 | } |
| 704 | } |
| 705 | |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 706 | void mremap_userfaultfd_prep(struct vm_area_struct *vma, |
| 707 | struct vm_userfaultfd_ctx *vm_ctx) |
| 708 | { |
| 709 | struct userfaultfd_ctx *ctx; |
| 710 | |
| 711 | ctx = vma->vm_userfaultfd_ctx.ctx; |
Peter Xu | 3cfd22b | 2018-12-28 00:38:47 -0800 | [diff] [blame] | 712 | |
| 713 | if (!ctx) |
| 714 | return; |
| 715 | |
| 716 | if (ctx->features & UFFD_FEATURE_EVENT_REMAP) { |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 717 | vm_ctx->ctx = ctx; |
| 718 | userfaultfd_ctx_get(ctx); |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 719 | WRITE_ONCE(ctx->mmap_changing, true); |
Peter Xu | 3cfd22b | 2018-12-28 00:38:47 -0800 | [diff] [blame] | 720 | } else { |
| 721 | /* Drop uffd context if remap feature not enabled */ |
| 722 | vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; |
| 723 | vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING); |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Andrea Arcangeli | 90794bf | 2017-02-22 15:42:37 -0800 | [diff] [blame] | 727 | void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx, |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 728 | unsigned long from, unsigned long to, |
| 729 | unsigned long len) |
| 730 | { |
Andrea Arcangeli | 90794bf | 2017-02-22 15:42:37 -0800 | [diff] [blame] | 731 | struct userfaultfd_ctx *ctx = vm_ctx->ctx; |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 732 | struct userfaultfd_wait_queue ewq; |
| 733 | |
| 734 | if (!ctx) |
| 735 | return; |
| 736 | |
| 737 | if (to & ~PAGE_MASK) { |
| 738 | userfaultfd_ctx_put(ctx); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | msg_init(&ewq.msg); |
| 743 | |
| 744 | ewq.msg.event = UFFD_EVENT_REMAP; |
| 745 | ewq.msg.arg.remap.from = from; |
| 746 | ewq.msg.arg.remap.to = to; |
| 747 | ewq.msg.arg.remap.len = len; |
| 748 | |
| 749 | userfaultfd_event_wait_completion(ctx, &ewq); |
| 750 | } |
| 751 | |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 752 | bool userfaultfd_remove(struct vm_area_struct *vma, |
Mike Rapoport | d811914 | 2017-02-24 14:56:02 -0800 | [diff] [blame] | 753 | unsigned long start, unsigned long end) |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 754 | { |
| 755 | struct mm_struct *mm = vma->vm_mm; |
| 756 | struct userfaultfd_ctx *ctx; |
| 757 | struct userfaultfd_wait_queue ewq; |
| 758 | |
| 759 | ctx = vma->vm_userfaultfd_ctx.ctx; |
Mike Rapoport | d811914 | 2017-02-24 14:56:02 -0800 | [diff] [blame] | 760 | if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE)) |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 761 | return true; |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 762 | |
| 763 | userfaultfd_ctx_get(ctx); |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 764 | WRITE_ONCE(ctx->mmap_changing, true); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 765 | mmap_read_unlock(mm); |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 766 | |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 767 | msg_init(&ewq.msg); |
| 768 | |
Mike Rapoport | d811914 | 2017-02-24 14:56:02 -0800 | [diff] [blame] | 769 | ewq.msg.event = UFFD_EVENT_REMOVE; |
| 770 | ewq.msg.arg.remove.start = start; |
| 771 | ewq.msg.arg.remove.end = end; |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 772 | |
| 773 | userfaultfd_event_wait_completion(ctx, &ewq); |
| 774 | |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 775 | return false; |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 778 | static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps, |
| 779 | unsigned long start, unsigned long end) |
| 780 | { |
| 781 | struct userfaultfd_unmap_ctx *unmap_ctx; |
| 782 | |
| 783 | list_for_each_entry(unmap_ctx, unmaps, list) |
| 784 | if (unmap_ctx->ctx == ctx && unmap_ctx->start == start && |
| 785 | unmap_ctx->end == end) |
| 786 | return true; |
| 787 | |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | int userfaultfd_unmap_prep(struct vm_area_struct *vma, |
| 792 | unsigned long start, unsigned long end, |
| 793 | struct list_head *unmaps) |
| 794 | { |
| 795 | for ( ; vma && vma->vm_start < end; vma = vma->vm_next) { |
| 796 | struct userfaultfd_unmap_ctx *unmap_ctx; |
| 797 | struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx; |
| 798 | |
| 799 | if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) || |
| 800 | has_unmap_ctx(ctx, unmaps, start, end)) |
| 801 | continue; |
| 802 | |
| 803 | unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL); |
| 804 | if (!unmap_ctx) |
| 805 | return -ENOMEM; |
| 806 | |
| 807 | userfaultfd_ctx_get(ctx); |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 808 | WRITE_ONCE(ctx->mmap_changing, true); |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 809 | unmap_ctx->ctx = ctx; |
| 810 | unmap_ctx->start = start; |
| 811 | unmap_ctx->end = end; |
| 812 | list_add_tail(&unmap_ctx->list, unmaps); |
| 813 | } |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf) |
| 819 | { |
| 820 | struct userfaultfd_unmap_ctx *ctx, *n; |
| 821 | struct userfaultfd_wait_queue ewq; |
| 822 | |
| 823 | list_for_each_entry_safe(ctx, n, uf, list) { |
| 824 | msg_init(&ewq.msg); |
| 825 | |
| 826 | ewq.msg.event = UFFD_EVENT_UNMAP; |
| 827 | ewq.msg.arg.remove.start = ctx->start; |
| 828 | ewq.msg.arg.remove.end = ctx->end; |
| 829 | |
| 830 | userfaultfd_event_wait_completion(ctx->ctx, &ewq); |
| 831 | |
| 832 | list_del(&ctx->list); |
| 833 | kfree(ctx); |
| 834 | } |
| 835 | } |
| 836 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 837 | static int userfaultfd_release(struct inode *inode, struct file *file) |
| 838 | { |
| 839 | struct userfaultfd_ctx *ctx = file->private_data; |
| 840 | struct mm_struct *mm = ctx->mm; |
| 841 | struct vm_area_struct *vma, *prev; |
| 842 | /* len == 0 means wake all */ |
| 843 | struct userfaultfd_wake_range range = { .len = 0, }; |
| 844 | unsigned long new_flags; |
Oleg Nesterov | 46d0b24 | 2019-08-24 17:54:56 -0700 | [diff] [blame] | 845 | bool still_valid; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 846 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 847 | WRITE_ONCE(ctx->released, true); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 848 | |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 849 | if (!mmget_not_zero(mm)) |
| 850 | goto wakeup; |
| 851 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 852 | /* |
| 853 | * Flush page faults out of all CPUs. NOTE: all page faults |
| 854 | * must be retried without returning VM_FAULT_SIGBUS if |
| 855 | * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 856 | * changes while handle_userfault released the mmap_lock. So |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 857 | * it's critical that released is set to true (above), before |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 858 | * taking the mmap_lock for writing. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 859 | */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 860 | mmap_write_lock(mm); |
Oleg Nesterov | 46d0b24 | 2019-08-24 17:54:56 -0700 | [diff] [blame] | 861 | still_valid = mmget_still_valid(mm); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 862 | prev = NULL; |
| 863 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 864 | cond_resched(); |
| 865 | BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^ |
| 866 | !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP))); |
| 867 | if (vma->vm_userfaultfd_ctx.ctx != ctx) { |
| 868 | prev = vma; |
| 869 | continue; |
| 870 | } |
| 871 | new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP); |
Oleg Nesterov | 46d0b24 | 2019-08-24 17:54:56 -0700 | [diff] [blame] | 872 | if (still_valid) { |
| 873 | prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end, |
| 874 | new_flags, vma->anon_vma, |
| 875 | vma->vm_file, vma->vm_pgoff, |
| 876 | vma_policy(vma), |
Greg Kroah-Hartman | ad455d8 | 2019-08-26 16:45:30 +0200 | [diff] [blame] | 877 | NULL_VM_UFFD_CTX, |
| 878 | vma_get_anon_name(vma)); |
Oleg Nesterov | 46d0b24 | 2019-08-24 17:54:56 -0700 | [diff] [blame] | 879 | if (prev) |
| 880 | vma = prev; |
| 881 | else |
| 882 | prev = vma; |
| 883 | } |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 884 | vma->vm_flags = new_flags; |
| 885 | vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; |
| 886 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 887 | mmap_write_unlock(mm); |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 888 | mmput(mm); |
| 889 | wakeup: |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 890 | /* |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 891 | * After no new page faults can wait on this fault_*wqh, flush |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 892 | * the last page faults that may have been already waiting on |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 893 | * the fault_*wqh. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 894 | */ |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 895 | spin_lock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | ac5be6b | 2015-09-22 14:58:49 -0700 | [diff] [blame] | 896 | __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range); |
Matthew Wilcox | c430d1e | 2018-08-21 21:56:30 -0700 | [diff] [blame] | 897 | __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range); |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 898 | spin_unlock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 899 | |
Mike Rapoport | 5a18b64 | 2017-08-02 13:32:24 -0700 | [diff] [blame] | 900 | /* Flush pending events that may still wait on event_wqh */ |
| 901 | wake_up_all(&ctx->event_wqh); |
| 902 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 903 | wake_up_poll(&ctx->fd_wqh, EPOLLHUP); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 904 | userfaultfd_ctx_put(ctx); |
| 905 | return 0; |
| 906 | } |
| 907 | |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 908 | /* fault_pending_wqh.lock must be hold by the caller */ |
Pavel Emelyanov | 6dcc27f | 2017-02-22 15:42:18 -0800 | [diff] [blame] | 909 | static inline struct userfaultfd_wait_queue *find_userfault_in( |
| 910 | wait_queue_head_t *wqh) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 911 | { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 912 | wait_queue_entry_t *wq; |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 913 | struct userfaultfd_wait_queue *uwq; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 914 | |
Lance Roy | 456a737 | 2018-10-04 23:45:44 -0700 | [diff] [blame] | 915 | lockdep_assert_held(&wqh->lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 916 | |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 917 | uwq = NULL; |
Pavel Emelyanov | 6dcc27f | 2017-02-22 15:42:18 -0800 | [diff] [blame] | 918 | if (!waitqueue_active(wqh)) |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 919 | goto out; |
| 920 | /* walk in reverse to provide FIFO behavior to read userfaults */ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 921 | wq = list_last_entry(&wqh->head, typeof(*wq), entry); |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 922 | uwq = container_of(wq, struct userfaultfd_wait_queue, wq); |
| 923 | out: |
| 924 | return uwq; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Pavel Emelyanov | 6dcc27f | 2017-02-22 15:42:18 -0800 | [diff] [blame] | 927 | static inline struct userfaultfd_wait_queue *find_userfault( |
| 928 | struct userfaultfd_ctx *ctx) |
| 929 | { |
| 930 | return find_userfault_in(&ctx->fault_pending_wqh); |
| 931 | } |
| 932 | |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 933 | static inline struct userfaultfd_wait_queue *find_userfault_evt( |
| 934 | struct userfaultfd_ctx *ctx) |
| 935 | { |
| 936 | return find_userfault_in(&ctx->event_wqh); |
| 937 | } |
| 938 | |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 939 | static __poll_t userfaultfd_poll(struct file *file, poll_table *wait) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 940 | { |
| 941 | struct userfaultfd_ctx *ctx = file->private_data; |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 942 | __poll_t ret; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 943 | |
| 944 | poll_wait(file, &ctx->fd_wqh, wait); |
| 945 | |
| 946 | switch (ctx->state) { |
| 947 | case UFFD_STATE_WAIT_API: |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 948 | return EPOLLERR; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 949 | case UFFD_STATE_RUNNING: |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 950 | /* |
| 951 | * poll() never guarantees that read won't block. |
| 952 | * userfaults can be waken before they're read(). |
| 953 | */ |
| 954 | if (unlikely(!(file->f_flags & O_NONBLOCK))) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 955 | return EPOLLERR; |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 956 | /* |
| 957 | * lockless access to see if there are pending faults |
| 958 | * __pollwait last action is the add_wait_queue but |
| 959 | * the spin_unlock would allow the waitqueue_active to |
| 960 | * pass above the actual list_add inside |
| 961 | * add_wait_queue critical section. So use a full |
| 962 | * memory barrier to serialize the list_add write of |
| 963 | * add_wait_queue() with the waitqueue_active read |
| 964 | * below. |
| 965 | */ |
| 966 | ret = 0; |
| 967 | smp_mb(); |
| 968 | if (waitqueue_active(&ctx->fault_pending_wqh)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 969 | ret = EPOLLIN; |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 970 | else if (waitqueue_active(&ctx->event_wqh)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 971 | ret = EPOLLIN; |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 972 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 973 | return ret; |
| 974 | default: |
Andrea Arcangeli | 8474901 | 2017-02-22 15:42:12 -0800 | [diff] [blame] | 975 | WARN_ON_ONCE(1); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 976 | return EPOLLERR; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 980 | static const struct file_operations userfaultfd_fops; |
| 981 | |
| 982 | static int resolve_userfault_fork(struct userfaultfd_ctx *ctx, |
| 983 | struct userfaultfd_ctx *new, |
| 984 | struct uffd_msg *msg) |
| 985 | { |
| 986 | int fd; |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 987 | |
Eric Biggers | 284cd24 | 2018-01-31 16:19:48 -0800 | [diff] [blame] | 988 | fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new, |
| 989 | O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS)); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 990 | if (fd < 0) |
| 991 | return fd; |
| 992 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 993 | msg->arg.reserved.reserved1 = 0; |
| 994 | msg->arg.fork.ufd = fd; |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 995 | return 0; |
| 996 | } |
| 997 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 998 | static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 999 | struct uffd_msg *msg) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1000 | { |
| 1001 | ssize_t ret; |
| 1002 | DECLARE_WAITQUEUE(wait, current); |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1003 | struct userfaultfd_wait_queue *uwq; |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1004 | /* |
| 1005 | * Handling fork event requires sleeping operations, so |
| 1006 | * we drop the event_wqh lock, then do these ops, then |
| 1007 | * lock it back and wake up the waiter. While the lock is |
| 1008 | * dropped the ewq may go away so we keep track of it |
| 1009 | * carefully. |
| 1010 | */ |
| 1011 | LIST_HEAD(fork_event); |
| 1012 | struct userfaultfd_ctx *fork_nctx = NULL; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1013 | |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1014 | /* always take the fd_wqh lock before the fault_pending_wqh lock */ |
Christoph Hellwig | ae62c16 | 2018-10-26 15:02:19 -0700 | [diff] [blame] | 1015 | spin_lock_irq(&ctx->fd_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1016 | __add_wait_queue(&ctx->fd_wqh, &wait); |
| 1017 | for (;;) { |
| 1018 | set_current_state(TASK_INTERRUPTIBLE); |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1019 | spin_lock(&ctx->fault_pending_wqh.lock); |
| 1020 | uwq = find_userfault(ctx); |
| 1021 | if (uwq) { |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1022 | /* |
Andrea Arcangeli | 2c5b7e1 | 2015-09-04 15:47:23 -0700 | [diff] [blame] | 1023 | * Use a seqcount to repeat the lockless check |
| 1024 | * in wake_userfault() to avoid missing |
| 1025 | * wakeups because during the refile both |
| 1026 | * waitqueue could become empty if this is the |
| 1027 | * only userfault. |
| 1028 | */ |
| 1029 | write_seqcount_begin(&ctx->refile_seq); |
| 1030 | |
| 1031 | /* |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1032 | * The fault_pending_wqh.lock prevents the uwq |
| 1033 | * to disappear from under us. |
| 1034 | * |
| 1035 | * Refile this userfault from |
| 1036 | * fault_pending_wqh to fault_wqh, it's not |
| 1037 | * pending anymore after we read it. |
| 1038 | * |
| 1039 | * Use list_del() by hand (as |
| 1040 | * userfaultfd_wake_function also uses |
| 1041 | * list_del_init() by hand) to be sure nobody |
| 1042 | * changes __remove_wait_queue() to use |
| 1043 | * list_del_init() in turn breaking the |
| 1044 | * !list_empty_careful() check in |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1045 | * handle_userfault(). The uwq->wq.head list |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1046 | * must never be empty at any time during the |
| 1047 | * refile, or the waitqueue could disappear |
| 1048 | * from under us. The "wait_queue_head_t" |
| 1049 | * parameter of __remove_wait_queue() is unused |
| 1050 | * anyway. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1051 | */ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1052 | list_del(&uwq->wq.entry); |
Matthew Wilcox | c430d1e | 2018-08-21 21:56:30 -0700 | [diff] [blame] | 1053 | add_wait_queue(&ctx->fault_wqh, &uwq->wq); |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1054 | |
Andrea Arcangeli | 2c5b7e1 | 2015-09-04 15:47:23 -0700 | [diff] [blame] | 1055 | write_seqcount_end(&ctx->refile_seq); |
| 1056 | |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 1057 | /* careful to always initialize msg if ret == 0 */ |
| 1058 | *msg = uwq->msg; |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1059 | spin_unlock(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1060 | ret = 0; |
| 1061 | break; |
| 1062 | } |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1063 | spin_unlock(&ctx->fault_pending_wqh.lock); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 1064 | |
| 1065 | spin_lock(&ctx->event_wqh.lock); |
| 1066 | uwq = find_userfault_evt(ctx); |
| 1067 | if (uwq) { |
| 1068 | *msg = uwq->msg; |
| 1069 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1070 | if (uwq->msg.event == UFFD_EVENT_FORK) { |
| 1071 | fork_nctx = (struct userfaultfd_ctx *) |
| 1072 | (unsigned long) |
| 1073 | uwq->msg.arg.reserved.reserved1; |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1074 | list_move(&uwq->wq.entry, &fork_event); |
Andrea Arcangeli | 384632e | 2017-10-03 16:15:38 -0700 | [diff] [blame] | 1075 | /* |
| 1076 | * fork_nctx can be freed as soon as |
| 1077 | * we drop the lock, unless we take a |
| 1078 | * reference on it. |
| 1079 | */ |
| 1080 | userfaultfd_ctx_get(fork_nctx); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1081 | spin_unlock(&ctx->event_wqh.lock); |
| 1082 | ret = 0; |
| 1083 | break; |
| 1084 | } |
| 1085 | |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 1086 | userfaultfd_event_complete(ctx, uwq); |
| 1087 | spin_unlock(&ctx->event_wqh.lock); |
| 1088 | ret = 0; |
| 1089 | break; |
| 1090 | } |
| 1091 | spin_unlock(&ctx->event_wqh.lock); |
| 1092 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1093 | if (signal_pending(current)) { |
| 1094 | ret = -ERESTARTSYS; |
| 1095 | break; |
| 1096 | } |
| 1097 | if (no_wait) { |
| 1098 | ret = -EAGAIN; |
| 1099 | break; |
| 1100 | } |
Christoph Hellwig | ae62c16 | 2018-10-26 15:02:19 -0700 | [diff] [blame] | 1101 | spin_unlock_irq(&ctx->fd_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1102 | schedule(); |
Christoph Hellwig | ae62c16 | 2018-10-26 15:02:19 -0700 | [diff] [blame] | 1103 | spin_lock_irq(&ctx->fd_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1104 | } |
| 1105 | __remove_wait_queue(&ctx->fd_wqh, &wait); |
| 1106 | __set_current_state(TASK_RUNNING); |
Christoph Hellwig | ae62c16 | 2018-10-26 15:02:19 -0700 | [diff] [blame] | 1107 | spin_unlock_irq(&ctx->fd_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1108 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1109 | if (!ret && msg->event == UFFD_EVENT_FORK) { |
| 1110 | ret = resolve_userfault_fork(ctx, fork_nctx, msg); |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 1111 | spin_lock_irq(&ctx->event_wqh.lock); |
Andrea Arcangeli | 384632e | 2017-10-03 16:15:38 -0700 | [diff] [blame] | 1112 | if (!list_empty(&fork_event)) { |
| 1113 | /* |
| 1114 | * The fork thread didn't abort, so we can |
| 1115 | * drop the temporary refcount. |
| 1116 | */ |
| 1117 | userfaultfd_ctx_put(fork_nctx); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1118 | |
Andrea Arcangeli | 384632e | 2017-10-03 16:15:38 -0700 | [diff] [blame] | 1119 | uwq = list_first_entry(&fork_event, |
| 1120 | typeof(*uwq), |
| 1121 | wq.entry); |
| 1122 | /* |
| 1123 | * If fork_event list wasn't empty and in turn |
| 1124 | * the event wasn't already released by fork |
| 1125 | * (the event is allocated on fork kernel |
| 1126 | * stack), put the event back to its place in |
| 1127 | * the event_wq. fork_event head will be freed |
| 1128 | * as soon as we return so the event cannot |
| 1129 | * stay queued there no matter the current |
| 1130 | * "ret" value. |
| 1131 | */ |
| 1132 | list_del(&uwq->wq.entry); |
| 1133 | __add_wait_queue(&ctx->event_wqh, &uwq->wq); |
| 1134 | |
| 1135 | /* |
| 1136 | * Leave the event in the waitqueue and report |
| 1137 | * error to userland if we failed to resolve |
| 1138 | * the userfault fork. |
| 1139 | */ |
| 1140 | if (likely(!ret)) |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1141 | userfaultfd_event_complete(ctx, uwq); |
Andrea Arcangeli | 384632e | 2017-10-03 16:15:38 -0700 | [diff] [blame] | 1142 | } else { |
| 1143 | /* |
| 1144 | * Here the fork thread aborted and the |
| 1145 | * refcount from the fork thread on fork_nctx |
| 1146 | * has already been released. We still hold |
| 1147 | * the reference we took before releasing the |
| 1148 | * lock above. If resolve_userfault_fork |
| 1149 | * failed we've to drop it because the |
| 1150 | * fork_nctx has to be freed in such case. If |
| 1151 | * it succeeded we'll hold it because the new |
| 1152 | * uffd references it. |
| 1153 | */ |
| 1154 | if (ret) |
| 1155 | userfaultfd_ctx_put(fork_nctx); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1156 | } |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 1157 | spin_unlock_irq(&ctx->event_wqh.lock); |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1160 | return ret; |
| 1161 | } |
| 1162 | |
| 1163 | static ssize_t userfaultfd_read(struct file *file, char __user *buf, |
| 1164 | size_t count, loff_t *ppos) |
| 1165 | { |
| 1166 | struct userfaultfd_ctx *ctx = file->private_data; |
| 1167 | ssize_t _ret, ret = 0; |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 1168 | struct uffd_msg msg; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1169 | int no_wait = file->f_flags & O_NONBLOCK; |
| 1170 | |
| 1171 | if (ctx->state == UFFD_STATE_WAIT_API) |
| 1172 | return -EINVAL; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1173 | |
| 1174 | for (;;) { |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 1175 | if (count < sizeof(msg)) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1176 | return ret ? ret : -EINVAL; |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 1177 | _ret = userfaultfd_ctx_read(ctx, no_wait, &msg); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1178 | if (_ret < 0) |
| 1179 | return ret ? ret : _ret; |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 1180 | if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg))) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1181 | return ret ? ret : -EFAULT; |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 1182 | ret += sizeof(msg); |
| 1183 | buf += sizeof(msg); |
| 1184 | count -= sizeof(msg); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1185 | /* |
| 1186 | * Allow to read more than one fault at time but only |
| 1187 | * block if waiting for the very first one. |
| 1188 | */ |
| 1189 | no_wait = O_NONBLOCK; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | static void __wake_userfault(struct userfaultfd_ctx *ctx, |
| 1194 | struct userfaultfd_wake_range *range) |
| 1195 | { |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 1196 | spin_lock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1197 | /* wake all in the range and autoremove */ |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1198 | if (waitqueue_active(&ctx->fault_pending_wqh)) |
Andrea Arcangeli | ac5be6b | 2015-09-22 14:58:49 -0700 | [diff] [blame] | 1199 | __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1200 | range); |
| 1201 | if (waitqueue_active(&ctx->fault_wqh)) |
Matthew Wilcox | c430d1e | 2018-08-21 21:56:30 -0700 | [diff] [blame] | 1202 | __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range); |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 1203 | spin_unlock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx, |
| 1207 | struct userfaultfd_wake_range *range) |
| 1208 | { |
Andrea Arcangeli | 2c5b7e1 | 2015-09-04 15:47:23 -0700 | [diff] [blame] | 1209 | unsigned seq; |
| 1210 | bool need_wakeup; |
| 1211 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1212 | /* |
| 1213 | * To be sure waitqueue_active() is not reordered by the CPU |
| 1214 | * before the pagetable update, use an explicit SMP memory |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 1215 | * barrier here. PT lock release or mmap_read_unlock(mm) still |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1216 | * have release semantics that can allow the |
| 1217 | * waitqueue_active() to be reordered before the pte update. |
| 1218 | */ |
| 1219 | smp_mb(); |
| 1220 | |
| 1221 | /* |
| 1222 | * Use waitqueue_active because it's very frequent to |
| 1223 | * change the address space atomically even if there are no |
| 1224 | * userfaults yet. So we take the spinlock only when we're |
| 1225 | * sure we've userfaults to wake. |
| 1226 | */ |
Andrea Arcangeli | 2c5b7e1 | 2015-09-04 15:47:23 -0700 | [diff] [blame] | 1227 | do { |
| 1228 | seq = read_seqcount_begin(&ctx->refile_seq); |
| 1229 | need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) || |
| 1230 | waitqueue_active(&ctx->fault_wqh); |
| 1231 | cond_resched(); |
| 1232 | } while (read_seqcount_retry(&ctx->refile_seq, seq)); |
| 1233 | if (need_wakeup) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1234 | __wake_userfault(ctx, range); |
| 1235 | } |
| 1236 | |
| 1237 | static __always_inline int validate_range(struct mm_struct *mm, |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1238 | __u64 *start, __u64 len) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1239 | { |
| 1240 | __u64 task_size = mm->task_size; |
| 1241 | |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1242 | *start = untagged_addr(*start); |
| 1243 | |
| 1244 | if (*start & ~PAGE_MASK) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1245 | return -EINVAL; |
| 1246 | if (len & ~PAGE_MASK) |
| 1247 | return -EINVAL; |
| 1248 | if (!len) |
| 1249 | return -EINVAL; |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1250 | if (*start < mmap_min_addr) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1251 | return -EINVAL; |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1252 | if (*start >= task_size) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1253 | return -EINVAL; |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1254 | if (len > task_size - *start) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1255 | return -EINVAL; |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1259 | static inline bool vma_can_userfault(struct vm_area_struct *vma, |
| 1260 | unsigned long vm_flags) |
Mike Rapoport | ba6907d | 2017-02-22 15:43:22 -0800 | [diff] [blame] | 1261 | { |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1262 | /* FIXME: add WP support to hugetlbfs and shmem */ |
| 1263 | return vma_is_anonymous(vma) || |
| 1264 | ((is_vm_hugetlb_page(vma) || vma_is_shmem(vma)) && |
| 1265 | !(vm_flags & VM_UFFD_WP)); |
Mike Rapoport | ba6907d | 2017-02-22 15:43:22 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1268 | static int userfaultfd_register(struct userfaultfd_ctx *ctx, |
| 1269 | unsigned long arg) |
| 1270 | { |
| 1271 | struct mm_struct *mm = ctx->mm; |
| 1272 | struct vm_area_struct *vma, *prev, *cur; |
| 1273 | int ret; |
| 1274 | struct uffdio_register uffdio_register; |
| 1275 | struct uffdio_register __user *user_uffdio_register; |
| 1276 | unsigned long vm_flags, new_flags; |
| 1277 | bool found; |
Mike Rapoport | ce53e8e | 2017-09-06 16:23:12 -0700 | [diff] [blame] | 1278 | bool basic_ioctls; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1279 | unsigned long start, end, vma_end; |
| 1280 | |
| 1281 | user_uffdio_register = (struct uffdio_register __user *) arg; |
| 1282 | |
| 1283 | ret = -EFAULT; |
| 1284 | if (copy_from_user(&uffdio_register, user_uffdio_register, |
| 1285 | sizeof(uffdio_register)-sizeof(__u64))) |
| 1286 | goto out; |
| 1287 | |
| 1288 | ret = -EINVAL; |
| 1289 | if (!uffdio_register.mode) |
| 1290 | goto out; |
| 1291 | if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING| |
| 1292 | UFFDIO_REGISTER_MODE_WP)) |
| 1293 | goto out; |
| 1294 | vm_flags = 0; |
| 1295 | if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING) |
| 1296 | vm_flags |= VM_UFFD_MISSING; |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1297 | if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1298 | vm_flags |= VM_UFFD_WP; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1299 | |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1300 | ret = validate_range(mm, &uffdio_register.range.start, |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1301 | uffdio_register.range.len); |
| 1302 | if (ret) |
| 1303 | goto out; |
| 1304 | |
| 1305 | start = uffdio_register.range.start; |
| 1306 | end = start + uffdio_register.range.len; |
| 1307 | |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1308 | ret = -ENOMEM; |
| 1309 | if (!mmget_not_zero(mm)) |
| 1310 | goto out; |
| 1311 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1312 | mmap_write_lock(mm); |
Andrea Arcangeli | 04f5866 | 2019-04-18 17:50:52 -0700 | [diff] [blame] | 1313 | if (!mmget_still_valid(mm)) |
| 1314 | goto out_unlock; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1315 | vma = find_vma_prev(mm, start, &prev); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1316 | if (!vma) |
| 1317 | goto out_unlock; |
| 1318 | |
| 1319 | /* check that there's at least one vma in the range */ |
| 1320 | ret = -EINVAL; |
| 1321 | if (vma->vm_start >= end) |
| 1322 | goto out_unlock; |
| 1323 | |
| 1324 | /* |
Mike Kravetz | cab350a | 2017-02-22 15:43:04 -0800 | [diff] [blame] | 1325 | * If the first vma contains huge pages, make sure start address |
| 1326 | * is aligned to huge page size. |
| 1327 | */ |
| 1328 | if (is_vm_hugetlb_page(vma)) { |
| 1329 | unsigned long vma_hpagesize = vma_kernel_pagesize(vma); |
| 1330 | |
| 1331 | if (start & (vma_hpagesize - 1)) |
| 1332 | goto out_unlock; |
| 1333 | } |
| 1334 | |
| 1335 | /* |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1336 | * Search for not compatible vmas. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1337 | */ |
| 1338 | found = false; |
Mike Rapoport | ce53e8e | 2017-09-06 16:23:12 -0700 | [diff] [blame] | 1339 | basic_ioctls = false; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1340 | for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) { |
| 1341 | cond_resched(); |
| 1342 | |
| 1343 | BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^ |
| 1344 | !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP))); |
| 1345 | |
| 1346 | /* check not compatible vmas */ |
| 1347 | ret = -EINVAL; |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1348 | if (!vma_can_userfault(cur, vm_flags)) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1349 | goto out_unlock; |
Andrea Arcangeli | 29ec9066 | 2018-11-30 14:09:32 -0800 | [diff] [blame] | 1350 | |
| 1351 | /* |
| 1352 | * UFFDIO_COPY will fill file holes even without |
| 1353 | * PROT_WRITE. This check enforces that if this is a |
| 1354 | * MAP_SHARED, the process has write permission to the backing |
| 1355 | * file. If VM_MAYWRITE is set it also enforces that on a |
| 1356 | * MAP_SHARED vma: there is no F_WRITE_SEAL and no further |
| 1357 | * F_WRITE_SEAL can be taken until the vma is destroyed. |
| 1358 | */ |
| 1359 | ret = -EPERM; |
| 1360 | if (unlikely(!(cur->vm_flags & VM_MAYWRITE))) |
| 1361 | goto out_unlock; |
| 1362 | |
Mike Kravetz | cab350a | 2017-02-22 15:43:04 -0800 | [diff] [blame] | 1363 | /* |
| 1364 | * If this vma contains ending address, and huge pages |
| 1365 | * check alignment. |
| 1366 | */ |
| 1367 | if (is_vm_hugetlb_page(cur) && end <= cur->vm_end && |
| 1368 | end > cur->vm_start) { |
| 1369 | unsigned long vma_hpagesize = vma_kernel_pagesize(cur); |
| 1370 | |
| 1371 | ret = -EINVAL; |
| 1372 | |
| 1373 | if (end & (vma_hpagesize - 1)) |
| 1374 | goto out_unlock; |
| 1375 | } |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1376 | if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE)) |
| 1377 | goto out_unlock; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1378 | |
| 1379 | /* |
| 1380 | * Check that this vma isn't already owned by a |
| 1381 | * different userfaultfd. We can't allow more than one |
| 1382 | * userfaultfd to own a single vma simultaneously or we |
| 1383 | * wouldn't know which one to deliver the userfaults to. |
| 1384 | */ |
| 1385 | ret = -EBUSY; |
| 1386 | if (cur->vm_userfaultfd_ctx.ctx && |
| 1387 | cur->vm_userfaultfd_ctx.ctx != ctx) |
| 1388 | goto out_unlock; |
| 1389 | |
Mike Kravetz | cab350a | 2017-02-22 15:43:04 -0800 | [diff] [blame] | 1390 | /* |
| 1391 | * Note vmas containing huge pages |
| 1392 | */ |
Mike Rapoport | ce53e8e | 2017-09-06 16:23:12 -0700 | [diff] [blame] | 1393 | if (is_vm_hugetlb_page(cur)) |
| 1394 | basic_ioctls = true; |
Mike Kravetz | cab350a | 2017-02-22 15:43:04 -0800 | [diff] [blame] | 1395 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1396 | found = true; |
| 1397 | } |
| 1398 | BUG_ON(!found); |
| 1399 | |
| 1400 | if (vma->vm_start < start) |
| 1401 | prev = vma; |
| 1402 | |
| 1403 | ret = 0; |
| 1404 | do { |
| 1405 | cond_resched(); |
| 1406 | |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1407 | BUG_ON(!vma_can_userfault(vma, vm_flags)); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1408 | BUG_ON(vma->vm_userfaultfd_ctx.ctx && |
| 1409 | vma->vm_userfaultfd_ctx.ctx != ctx); |
Andrea Arcangeli | 29ec9066 | 2018-11-30 14:09:32 -0800 | [diff] [blame] | 1410 | WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1411 | |
| 1412 | /* |
| 1413 | * Nothing to do: this vma is already registered into this |
| 1414 | * userfaultfd and with the right tracking mode too. |
| 1415 | */ |
| 1416 | if (vma->vm_userfaultfd_ctx.ctx == ctx && |
| 1417 | (vma->vm_flags & vm_flags) == vm_flags) |
| 1418 | goto skip; |
| 1419 | |
| 1420 | if (vma->vm_start > start) |
| 1421 | start = vma->vm_start; |
| 1422 | vma_end = min(end, vma->vm_end); |
| 1423 | |
Andrea Arcangeli | 9d4678e | 2019-11-30 17:57:58 -0800 | [diff] [blame] | 1424 | new_flags = (vma->vm_flags & |
| 1425 | ~(VM_UFFD_MISSING|VM_UFFD_WP)) | vm_flags; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1426 | prev = vma_merge(mm, prev, start, vma_end, new_flags, |
| 1427 | vma->anon_vma, vma->vm_file, vma->vm_pgoff, |
| 1428 | vma_policy(vma), |
Colin Cross | 60500a4 | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1429 | ((struct vm_userfaultfd_ctx){ ctx }), |
| 1430 | vma_get_anon_name(vma)); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1431 | if (prev) { |
| 1432 | vma = prev; |
| 1433 | goto next; |
| 1434 | } |
| 1435 | if (vma->vm_start < start) { |
| 1436 | ret = split_vma(mm, vma, start, 1); |
| 1437 | if (ret) |
| 1438 | break; |
| 1439 | } |
| 1440 | if (vma->vm_end > end) { |
| 1441 | ret = split_vma(mm, vma, end, 0); |
| 1442 | if (ret) |
| 1443 | break; |
| 1444 | } |
| 1445 | next: |
| 1446 | /* |
| 1447 | * In the vma_merge() successful mprotect-like case 8: |
| 1448 | * the next vma was merged into the current one and |
| 1449 | * the current one has not been updated yet. |
| 1450 | */ |
| 1451 | vma->vm_flags = new_flags; |
| 1452 | vma->vm_userfaultfd_ctx.ctx = ctx; |
| 1453 | |
| 1454 | skip: |
| 1455 | prev = vma; |
| 1456 | start = vma->vm_end; |
| 1457 | vma = vma->vm_next; |
| 1458 | } while (vma && vma->vm_start < end); |
| 1459 | out_unlock: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1460 | mmap_write_unlock(mm); |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1461 | mmput(mm); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1462 | if (!ret) { |
Peter Xu | 1481930 | 2020-04-06 20:06:29 -0700 | [diff] [blame] | 1463 | __u64 ioctls_out; |
| 1464 | |
| 1465 | ioctls_out = basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC : |
| 1466 | UFFD_API_RANGE_IOCTLS; |
| 1467 | |
| 1468 | /* |
| 1469 | * Declare the WP ioctl only if the WP mode is |
| 1470 | * specified and all checks passed with the range |
| 1471 | */ |
| 1472 | if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_WP)) |
| 1473 | ioctls_out &= ~((__u64)1 << _UFFDIO_WRITEPROTECT); |
| 1474 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1475 | /* |
| 1476 | * Now that we scanned all vmas we can already tell |
| 1477 | * userland which ioctls methods are guaranteed to |
| 1478 | * succeed on this range. |
| 1479 | */ |
Peter Xu | 1481930 | 2020-04-06 20:06:29 -0700 | [diff] [blame] | 1480 | if (put_user(ioctls_out, &user_uffdio_register->ioctls)) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1481 | ret = -EFAULT; |
| 1482 | } |
| 1483 | out: |
| 1484 | return ret; |
| 1485 | } |
| 1486 | |
| 1487 | static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, |
| 1488 | unsigned long arg) |
| 1489 | { |
| 1490 | struct mm_struct *mm = ctx->mm; |
| 1491 | struct vm_area_struct *vma, *prev, *cur; |
| 1492 | int ret; |
| 1493 | struct uffdio_range uffdio_unregister; |
| 1494 | unsigned long new_flags; |
| 1495 | bool found; |
| 1496 | unsigned long start, end, vma_end; |
| 1497 | const void __user *buf = (void __user *)arg; |
| 1498 | |
| 1499 | ret = -EFAULT; |
| 1500 | if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister))) |
| 1501 | goto out; |
| 1502 | |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1503 | ret = validate_range(mm, &uffdio_unregister.start, |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1504 | uffdio_unregister.len); |
| 1505 | if (ret) |
| 1506 | goto out; |
| 1507 | |
| 1508 | start = uffdio_unregister.start; |
| 1509 | end = start + uffdio_unregister.len; |
| 1510 | |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1511 | ret = -ENOMEM; |
| 1512 | if (!mmget_not_zero(mm)) |
| 1513 | goto out; |
| 1514 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1515 | mmap_write_lock(mm); |
Andrea Arcangeli | 04f5866 | 2019-04-18 17:50:52 -0700 | [diff] [blame] | 1516 | if (!mmget_still_valid(mm)) |
| 1517 | goto out_unlock; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1518 | vma = find_vma_prev(mm, start, &prev); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1519 | if (!vma) |
| 1520 | goto out_unlock; |
| 1521 | |
| 1522 | /* check that there's at least one vma in the range */ |
| 1523 | ret = -EINVAL; |
| 1524 | if (vma->vm_start >= end) |
| 1525 | goto out_unlock; |
| 1526 | |
| 1527 | /* |
Mike Kravetz | cab350a | 2017-02-22 15:43:04 -0800 | [diff] [blame] | 1528 | * If the first vma contains huge pages, make sure start address |
| 1529 | * is aligned to huge page size. |
| 1530 | */ |
| 1531 | if (is_vm_hugetlb_page(vma)) { |
| 1532 | unsigned long vma_hpagesize = vma_kernel_pagesize(vma); |
| 1533 | |
| 1534 | if (start & (vma_hpagesize - 1)) |
| 1535 | goto out_unlock; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1539 | * Search for not compatible vmas. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1540 | */ |
| 1541 | found = false; |
| 1542 | ret = -EINVAL; |
| 1543 | for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) { |
| 1544 | cond_resched(); |
| 1545 | |
| 1546 | BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^ |
| 1547 | !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP))); |
| 1548 | |
| 1549 | /* |
| 1550 | * Check not compatible vmas, not strictly required |
| 1551 | * here as not compatible vmas cannot have an |
| 1552 | * userfaultfd_ctx registered on them, but this |
| 1553 | * provides for more strict behavior to notice |
| 1554 | * unregistration errors. |
| 1555 | */ |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1556 | if (!vma_can_userfault(cur, cur->vm_flags)) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1557 | goto out_unlock; |
| 1558 | |
| 1559 | found = true; |
| 1560 | } |
| 1561 | BUG_ON(!found); |
| 1562 | |
| 1563 | if (vma->vm_start < start) |
| 1564 | prev = vma; |
| 1565 | |
| 1566 | ret = 0; |
| 1567 | do { |
| 1568 | cond_resched(); |
| 1569 | |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1570 | BUG_ON(!vma_can_userfault(vma, vma->vm_flags)); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1571 | |
| 1572 | /* |
| 1573 | * Nothing to do: this vma is already registered into this |
| 1574 | * userfaultfd and with the right tracking mode too. |
| 1575 | */ |
| 1576 | if (!vma->vm_userfaultfd_ctx.ctx) |
| 1577 | goto skip; |
| 1578 | |
Andrea Arcangeli | 01e881f | 2018-12-14 14:17:17 -0800 | [diff] [blame] | 1579 | WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); |
| 1580 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1581 | if (vma->vm_start > start) |
| 1582 | start = vma->vm_start; |
| 1583 | vma_end = min(end, vma->vm_end); |
| 1584 | |
Andrea Arcangeli | 09fa529 | 2017-02-22 15:42:46 -0800 | [diff] [blame] | 1585 | if (userfaultfd_missing(vma)) { |
| 1586 | /* |
| 1587 | * Wake any concurrent pending userfault while |
| 1588 | * we unregister, so they will not hang |
| 1589 | * permanently and it avoids userland to call |
| 1590 | * UFFDIO_WAKE explicitly. |
| 1591 | */ |
| 1592 | struct userfaultfd_wake_range range; |
| 1593 | range.start = start; |
| 1594 | range.len = vma_end - start; |
| 1595 | wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range); |
| 1596 | } |
| 1597 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1598 | new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP); |
| 1599 | prev = vma_merge(mm, prev, start, vma_end, new_flags, |
| 1600 | vma->anon_vma, vma->vm_file, vma->vm_pgoff, |
| 1601 | vma_policy(vma), |
Colin Cross | 60500a4 | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1602 | NULL_VM_UFFD_CTX, |
| 1603 | vma_get_anon_name(vma)); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1604 | if (prev) { |
| 1605 | vma = prev; |
| 1606 | goto next; |
| 1607 | } |
| 1608 | if (vma->vm_start < start) { |
| 1609 | ret = split_vma(mm, vma, start, 1); |
| 1610 | if (ret) |
| 1611 | break; |
| 1612 | } |
| 1613 | if (vma->vm_end > end) { |
| 1614 | ret = split_vma(mm, vma, end, 0); |
| 1615 | if (ret) |
| 1616 | break; |
| 1617 | } |
| 1618 | next: |
| 1619 | /* |
| 1620 | * In the vma_merge() successful mprotect-like case 8: |
| 1621 | * the next vma was merged into the current one and |
| 1622 | * the current one has not been updated yet. |
| 1623 | */ |
| 1624 | vma->vm_flags = new_flags; |
| 1625 | vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; |
| 1626 | |
| 1627 | skip: |
| 1628 | prev = vma; |
| 1629 | start = vma->vm_end; |
| 1630 | vma = vma->vm_next; |
| 1631 | } while (vma && vma->vm_start < end); |
| 1632 | out_unlock: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1633 | mmap_write_unlock(mm); |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1634 | mmput(mm); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1635 | out: |
| 1636 | return ret; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
Andrea Arcangeli | ba85c70 | 2015-09-04 15:46:41 -0700 | [diff] [blame] | 1640 | * userfaultfd_wake may be used in combination with the |
| 1641 | * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches. |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1642 | */ |
| 1643 | static int userfaultfd_wake(struct userfaultfd_ctx *ctx, |
| 1644 | unsigned long arg) |
| 1645 | { |
| 1646 | int ret; |
| 1647 | struct uffdio_range uffdio_wake; |
| 1648 | struct userfaultfd_wake_range range; |
| 1649 | const void __user *buf = (void __user *)arg; |
| 1650 | |
| 1651 | ret = -EFAULT; |
| 1652 | if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake))) |
| 1653 | goto out; |
| 1654 | |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1655 | ret = validate_range(ctx->mm, &uffdio_wake.start, uffdio_wake.len); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1656 | if (ret) |
| 1657 | goto out; |
| 1658 | |
| 1659 | range.start = uffdio_wake.start; |
| 1660 | range.len = uffdio_wake.len; |
| 1661 | |
| 1662 | /* |
| 1663 | * len == 0 means wake all and we don't want to wake all here, |
| 1664 | * so check it again to be sure. |
| 1665 | */ |
| 1666 | VM_BUG_ON(!range.len); |
| 1667 | |
| 1668 | wake_userfault(ctx, &range); |
| 1669 | ret = 0; |
| 1670 | |
| 1671 | out: |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1675 | static int userfaultfd_copy(struct userfaultfd_ctx *ctx, |
| 1676 | unsigned long arg) |
| 1677 | { |
| 1678 | __s64 ret; |
| 1679 | struct uffdio_copy uffdio_copy; |
| 1680 | struct uffdio_copy __user *user_uffdio_copy; |
| 1681 | struct userfaultfd_wake_range range; |
| 1682 | |
| 1683 | user_uffdio_copy = (struct uffdio_copy __user *) arg; |
| 1684 | |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 1685 | ret = -EAGAIN; |
| 1686 | if (READ_ONCE(ctx->mmap_changing)) |
| 1687 | goto out; |
| 1688 | |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1689 | ret = -EFAULT; |
| 1690 | if (copy_from_user(&uffdio_copy, user_uffdio_copy, |
| 1691 | /* don't copy "copy" last field */ |
| 1692 | sizeof(uffdio_copy)-sizeof(__s64))) |
| 1693 | goto out; |
| 1694 | |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1695 | ret = validate_range(ctx->mm, &uffdio_copy.dst, uffdio_copy.len); |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1696 | if (ret) |
| 1697 | goto out; |
| 1698 | /* |
| 1699 | * double check for wraparound just in case. copy_from_user() |
| 1700 | * will later check uffdio_copy.src + uffdio_copy.len to fit |
| 1701 | * in the userland range. |
| 1702 | */ |
| 1703 | ret = -EINVAL; |
| 1704 | if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src) |
| 1705 | goto out; |
Andrea Arcangeli | 72981e0 | 2020-04-06 20:05:41 -0700 | [diff] [blame] | 1706 | if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP)) |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1707 | goto out; |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1708 | if (mmget_not_zero(ctx->mm)) { |
| 1709 | ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src, |
Andrea Arcangeli | 72981e0 | 2020-04-06 20:05:41 -0700 | [diff] [blame] | 1710 | uffdio_copy.len, &ctx->mmap_changing, |
| 1711 | uffdio_copy.mode); |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1712 | mmput(ctx->mm); |
Mike Rapoport | 9633318 | 2017-02-24 14:58:31 -0800 | [diff] [blame] | 1713 | } else { |
Mike Rapoport | e86b298 | 2017-08-10 15:24:32 -0700 | [diff] [blame] | 1714 | return -ESRCH; |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1715 | } |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1716 | if (unlikely(put_user(ret, &user_uffdio_copy->copy))) |
| 1717 | return -EFAULT; |
| 1718 | if (ret < 0) |
| 1719 | goto out; |
| 1720 | BUG_ON(!ret); |
| 1721 | /* len == 0 would wake all */ |
| 1722 | range.len = ret; |
| 1723 | if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) { |
| 1724 | range.start = uffdio_copy.dst; |
| 1725 | wake_userfault(ctx, &range); |
| 1726 | } |
| 1727 | ret = range.len == uffdio_copy.len ? 0 : -EAGAIN; |
| 1728 | out: |
| 1729 | return ret; |
| 1730 | } |
| 1731 | |
| 1732 | static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx, |
| 1733 | unsigned long arg) |
| 1734 | { |
| 1735 | __s64 ret; |
| 1736 | struct uffdio_zeropage uffdio_zeropage; |
| 1737 | struct uffdio_zeropage __user *user_uffdio_zeropage; |
| 1738 | struct userfaultfd_wake_range range; |
| 1739 | |
| 1740 | user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg; |
| 1741 | |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 1742 | ret = -EAGAIN; |
| 1743 | if (READ_ONCE(ctx->mmap_changing)) |
| 1744 | goto out; |
| 1745 | |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1746 | ret = -EFAULT; |
| 1747 | if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage, |
| 1748 | /* don't copy "zeropage" last field */ |
| 1749 | sizeof(uffdio_zeropage)-sizeof(__s64))) |
| 1750 | goto out; |
| 1751 | |
Andrey Konovalov | 7d03257 | 2019-09-25 16:48:44 -0700 | [diff] [blame] | 1752 | ret = validate_range(ctx->mm, &uffdio_zeropage.range.start, |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1753 | uffdio_zeropage.range.len); |
| 1754 | if (ret) |
| 1755 | goto out; |
| 1756 | ret = -EINVAL; |
| 1757 | if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE) |
| 1758 | goto out; |
| 1759 | |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1760 | if (mmget_not_zero(ctx->mm)) { |
| 1761 | ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start, |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 1762 | uffdio_zeropage.range.len, |
| 1763 | &ctx->mmap_changing); |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1764 | mmput(ctx->mm); |
Mike Rapoport | 9d95aa4 | 2017-08-02 13:32:15 -0700 | [diff] [blame] | 1765 | } else { |
Mike Rapoport | e86b298 | 2017-08-10 15:24:32 -0700 | [diff] [blame] | 1766 | return -ESRCH; |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 1767 | } |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1768 | if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage))) |
| 1769 | return -EFAULT; |
| 1770 | if (ret < 0) |
| 1771 | goto out; |
| 1772 | /* len == 0 would wake all */ |
| 1773 | BUG_ON(!ret); |
| 1774 | range.len = ret; |
| 1775 | if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) { |
| 1776 | range.start = uffdio_zeropage.range.start; |
| 1777 | wake_userfault(ctx, &range); |
| 1778 | } |
| 1779 | ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN; |
| 1780 | out: |
| 1781 | return ret; |
| 1782 | } |
| 1783 | |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1784 | static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx, |
| 1785 | unsigned long arg) |
| 1786 | { |
| 1787 | int ret; |
| 1788 | struct uffdio_writeprotect uffdio_wp; |
| 1789 | struct uffdio_writeprotect __user *user_uffdio_wp; |
| 1790 | struct userfaultfd_wake_range range; |
Peter Xu | 23080e2 | 2020-04-06 20:06:20 -0700 | [diff] [blame] | 1791 | bool mode_wp, mode_dontwake; |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1792 | |
| 1793 | if (READ_ONCE(ctx->mmap_changing)) |
| 1794 | return -EAGAIN; |
| 1795 | |
| 1796 | user_uffdio_wp = (struct uffdio_writeprotect __user *) arg; |
| 1797 | |
| 1798 | if (copy_from_user(&uffdio_wp, user_uffdio_wp, |
| 1799 | sizeof(struct uffdio_writeprotect))) |
| 1800 | return -EFAULT; |
| 1801 | |
| 1802 | ret = validate_range(ctx->mm, &uffdio_wp.range.start, |
| 1803 | uffdio_wp.range.len); |
| 1804 | if (ret) |
| 1805 | return ret; |
| 1806 | |
| 1807 | if (uffdio_wp.mode & ~(UFFDIO_WRITEPROTECT_MODE_DONTWAKE | |
| 1808 | UFFDIO_WRITEPROTECT_MODE_WP)) |
| 1809 | return -EINVAL; |
Peter Xu | 23080e2 | 2020-04-06 20:06:20 -0700 | [diff] [blame] | 1810 | |
| 1811 | mode_wp = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_WP; |
| 1812 | mode_dontwake = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_DONTWAKE; |
| 1813 | |
| 1814 | if (mode_wp && mode_dontwake) |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1815 | return -EINVAL; |
| 1816 | |
| 1817 | ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start, |
Peter Xu | 23080e2 | 2020-04-06 20:06:20 -0700 | [diff] [blame] | 1818 | uffdio_wp.range.len, mode_wp, |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1819 | &ctx->mmap_changing); |
| 1820 | if (ret) |
| 1821 | return ret; |
| 1822 | |
Peter Xu | 23080e2 | 2020-04-06 20:06:20 -0700 | [diff] [blame] | 1823 | if (!mode_wp && !mode_dontwake) { |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1824 | range.start = uffdio_wp.range.start; |
| 1825 | range.len = uffdio_wp.range.len; |
| 1826 | wake_userfault(ctx, &range); |
| 1827 | } |
| 1828 | return ret; |
| 1829 | } |
| 1830 | |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 1831 | static inline unsigned int uffd_ctx_features(__u64 user_features) |
| 1832 | { |
| 1833 | /* |
| 1834 | * For the current set of features the bits just coincide |
| 1835 | */ |
| 1836 | return (unsigned int)user_features; |
| 1837 | } |
| 1838 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1839 | /* |
| 1840 | * userland asks for a certain API version and we return which bits |
| 1841 | * and ioctl commands are implemented in this kernel for such API |
| 1842 | * version or -EINVAL if unknown. |
| 1843 | */ |
| 1844 | static int userfaultfd_api(struct userfaultfd_ctx *ctx, |
| 1845 | unsigned long arg) |
| 1846 | { |
| 1847 | struct uffdio_api uffdio_api; |
| 1848 | void __user *buf = (void __user *)arg; |
| 1849 | int ret; |
Andrea Arcangeli | 6560314 | 2017-02-22 15:42:24 -0800 | [diff] [blame] | 1850 | __u64 features; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1851 | |
| 1852 | ret = -EINVAL; |
| 1853 | if (ctx->state != UFFD_STATE_WAIT_API) |
| 1854 | goto out; |
| 1855 | ret = -EFAULT; |
Andrea Arcangeli | a9b85f9 | 2015-09-04 15:46:37 -0700 | [diff] [blame] | 1856 | if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api))) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1857 | goto out; |
Andrea Arcangeli | 6560314 | 2017-02-22 15:42:24 -0800 | [diff] [blame] | 1858 | features = uffdio_api.features; |
Mike Rapoport | 3c1c24d | 2019-11-30 17:58:01 -0800 | [diff] [blame] | 1859 | ret = -EINVAL; |
| 1860 | if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) |
| 1861 | goto err_out; |
| 1862 | ret = -EPERM; |
| 1863 | if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE)) |
| 1864 | goto err_out; |
Andrea Arcangeli | 6560314 | 2017-02-22 15:42:24 -0800 | [diff] [blame] | 1865 | /* report all available features and ioctls to userland */ |
| 1866 | uffdio_api.features = UFFD_API_FEATURES; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1867 | uffdio_api.ioctls = UFFD_API_IOCTLS; |
| 1868 | ret = -EFAULT; |
| 1869 | if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) |
| 1870 | goto out; |
| 1871 | ctx->state = UFFD_STATE_RUNNING; |
Andrea Arcangeli | 6560314 | 2017-02-22 15:42:24 -0800 | [diff] [blame] | 1872 | /* only enable the requested features for this uffd context */ |
| 1873 | ctx->features = uffd_ctx_features(features); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1874 | ret = 0; |
| 1875 | out: |
| 1876 | return ret; |
Mike Rapoport | 3c1c24d | 2019-11-30 17:58:01 -0800 | [diff] [blame] | 1877 | err_out: |
| 1878 | memset(&uffdio_api, 0, sizeof(uffdio_api)); |
| 1879 | if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) |
| 1880 | ret = -EFAULT; |
| 1881 | goto out; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | static long userfaultfd_ioctl(struct file *file, unsigned cmd, |
| 1885 | unsigned long arg) |
| 1886 | { |
| 1887 | int ret = -EINVAL; |
| 1888 | struct userfaultfd_ctx *ctx = file->private_data; |
| 1889 | |
Andrea Arcangeli | e6485a4 | 2015-09-04 15:47:15 -0700 | [diff] [blame] | 1890 | if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API) |
| 1891 | return -EINVAL; |
| 1892 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1893 | switch(cmd) { |
| 1894 | case UFFDIO_API: |
| 1895 | ret = userfaultfd_api(ctx, arg); |
| 1896 | break; |
| 1897 | case UFFDIO_REGISTER: |
| 1898 | ret = userfaultfd_register(ctx, arg); |
| 1899 | break; |
| 1900 | case UFFDIO_UNREGISTER: |
| 1901 | ret = userfaultfd_unregister(ctx, arg); |
| 1902 | break; |
| 1903 | case UFFDIO_WAKE: |
| 1904 | ret = userfaultfd_wake(ctx, arg); |
| 1905 | break; |
Andrea Arcangeli | ad465ca | 2015-09-04 15:47:11 -0700 | [diff] [blame] | 1906 | case UFFDIO_COPY: |
| 1907 | ret = userfaultfd_copy(ctx, arg); |
| 1908 | break; |
| 1909 | case UFFDIO_ZEROPAGE: |
| 1910 | ret = userfaultfd_zeropage(ctx, arg); |
| 1911 | break; |
Andrea Arcangeli | 63b2d41 | 2020-04-06 20:06:12 -0700 | [diff] [blame] | 1912 | case UFFDIO_WRITEPROTECT: |
| 1913 | ret = userfaultfd_writeprotect(ctx, arg); |
| 1914 | break; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1915 | } |
| 1916 | return ret; |
| 1917 | } |
| 1918 | |
| 1919 | #ifdef CONFIG_PROC_FS |
| 1920 | static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f) |
| 1921 | { |
| 1922 | struct userfaultfd_ctx *ctx = f->private_data; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1923 | wait_queue_entry_t *wq; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1924 | unsigned long pending = 0, total = 0; |
| 1925 | |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 1926 | spin_lock_irq(&ctx->fault_pending_wqh.lock); |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1927 | list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) { |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1928 | pending++; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1929 | total++; |
| 1930 | } |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1931 | list_for_each_entry(wq, &ctx->fault_wqh.head, entry) { |
Andrea Arcangeli | 15b726e | 2015-09-04 15:46:44 -0700 | [diff] [blame] | 1932 | total++; |
| 1933 | } |
Eric Biggers | cbcfa13 | 2019-07-04 15:14:39 -0700 | [diff] [blame] | 1934 | spin_unlock_irq(&ctx->fault_pending_wqh.lock); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1935 | |
| 1936 | /* |
| 1937 | * If more protocols will be added, there will be all shown |
| 1938 | * separated by a space. Like this: |
| 1939 | * protocols: aa:... bb:... |
| 1940 | */ |
| 1941 | seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n", |
Mike Rapoport | 045098e | 2017-04-07 16:04:42 -0700 | [diff] [blame] | 1942 | pending, total, UFFD_API, ctx->features, |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1943 | UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS); |
| 1944 | } |
| 1945 | #endif |
| 1946 | |
| 1947 | static const struct file_operations userfaultfd_fops = { |
| 1948 | #ifdef CONFIG_PROC_FS |
| 1949 | .show_fdinfo = userfaultfd_show_fdinfo, |
| 1950 | #endif |
| 1951 | .release = userfaultfd_release, |
| 1952 | .poll = userfaultfd_poll, |
| 1953 | .read = userfaultfd_read, |
| 1954 | .unlocked_ioctl = userfaultfd_ioctl, |
Arnd Bergmann | 1832f2d | 2018-09-11 21:59:08 +0200 | [diff] [blame] | 1955 | .compat_ioctl = compat_ptr_ioctl, |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1956 | .llseek = noop_llseek, |
| 1957 | }; |
| 1958 | |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 1959 | static void init_once_userfaultfd_ctx(void *mem) |
| 1960 | { |
| 1961 | struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem; |
| 1962 | |
| 1963 | init_waitqueue_head(&ctx->fault_pending_wqh); |
| 1964 | init_waitqueue_head(&ctx->fault_wqh); |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 1965 | init_waitqueue_head(&ctx->event_wqh); |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 1966 | init_waitqueue_head(&ctx->fd_wqh); |
Andrea Arcangeli | 2c5b7e1 | 2015-09-04 15:47:23 -0700 | [diff] [blame] | 1967 | seqcount_init(&ctx->refile_seq); |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
Eric Biggers | 284cd24 | 2018-01-31 16:19:48 -0800 | [diff] [blame] | 1970 | SYSCALL_DEFINE1(userfaultfd, int, flags) |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1971 | { |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1972 | struct userfaultfd_ctx *ctx; |
Eric Biggers | 284cd24 | 2018-01-31 16:19:48 -0800 | [diff] [blame] | 1973 | int fd; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1974 | |
Peter Xu | cefdca0 | 2019-05-13 17:16:41 -0700 | [diff] [blame] | 1975 | if (!sysctl_unprivileged_userfaultfd && !capable(CAP_SYS_PTRACE)) |
| 1976 | return -EPERM; |
| 1977 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1978 | BUG_ON(!current->mm); |
| 1979 | |
| 1980 | /* Check the UFFD_* constants for consistency. */ |
| 1981 | BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC); |
| 1982 | BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK); |
| 1983 | |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1984 | if (flags & ~UFFD_SHARED_FCNTL_FLAGS) |
Eric Biggers | 284cd24 | 2018-01-31 16:19:48 -0800 | [diff] [blame] | 1985 | return -EINVAL; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1986 | |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 1987 | ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1988 | if (!ctx) |
Eric Biggers | 284cd24 | 2018-01-31 16:19:48 -0800 | [diff] [blame] | 1989 | return -ENOMEM; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1990 | |
Eric Biggers | ca88042 | 2018-12-28 00:34:43 -0800 | [diff] [blame] | 1991 | refcount_set(&ctx->refcount, 1); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1992 | ctx->flags = flags; |
Pavel Emelyanov | 9cd75c3 | 2017-02-22 15:42:21 -0800 | [diff] [blame] | 1993 | ctx->features = 0; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1994 | ctx->state = UFFD_STATE_WAIT_API; |
| 1995 | ctx->released = false; |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 1996 | ctx->mmap_changing = false; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 1997 | ctx->mm = current->mm; |
| 1998 | /* prevent the mm struct to be freed */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 1999 | mmgrab(ctx->mm); |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 2000 | |
Eric Biggers | 284cd24 | 2018-01-31 16:19:48 -0800 | [diff] [blame] | 2001 | fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx, |
| 2002 | O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS)); |
| 2003 | if (fd < 0) { |
Oleg Nesterov | d2005e3 | 2016-05-20 16:58:36 -0700 | [diff] [blame] | 2004 | mmdrop(ctx->mm); |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 2005 | kmem_cache_free(userfaultfd_ctx_cachep, ctx); |
Eric Biggers | c03e946 | 2015-09-17 16:01:54 -0700 | [diff] [blame] | 2006 | } |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 2007 | return fd; |
Andrea Arcangeli | 86039bd | 2015-09-04 15:46:31 -0700 | [diff] [blame] | 2008 | } |
Andrea Arcangeli | 3004ec9 | 2015-09-04 15:46:48 -0700 | [diff] [blame] | 2009 | |
| 2010 | static int __init userfaultfd_init(void) |
| 2011 | { |
| 2012 | userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache", |
| 2013 | sizeof(struct userfaultfd_ctx), |
| 2014 | 0, |
| 2015 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
| 2016 | init_once_userfaultfd_ctx); |
| 2017 | return 0; |
| 2018 | } |
| 2019 | __initcall(userfaultfd_init); |