| File: | fs/userfaultfd.c |
| Warning: | line 639, column 7 Copies out a struct with a union element with different sizes |
| 1 | /* | |||
| 2 | * fs/userfaultfd.c | |||
| 3 | * | |||
| 4 | * Copyright (C) 2007 Davide Libenzi <[email protected]> | |||
| 5 | * Copyright (C) 2008-2009 Red Hat, Inc. | |||
| 6 | * Copyright (C) 2015 Red Hat, Inc. | |||
| 7 | * | |||
| 8 | * This work is licensed under the terms of the GNU GPL, version 2. See | |||
| 9 | * the COPYING file in the top-level directory. | |||
| 10 | * | |||
| 11 | * Some part derived from fs/eventfd.c (anon inode setup) and | |||
| 12 | * mm/ksm.c (mm hashing). | |||
| 13 | */ | |||
| 14 | ||||
| 15 | #include <linux1/hashtable.h> | |||
| 16 | #include <linux1/sched.h> | |||
| 17 | #include <linux1/mm.h> | |||
| 18 | #include <linux1/poll.h> | |||
| 19 | #include <linux1/slab.h> | |||
| 20 | #include <linux1/seq_file.h> | |||
| 21 | #include <linux1/file.h> | |||
| 22 | #include <linux1/bug.h> | |||
| 23 | #include <linux1/anon_inodes.h> | |||
| 24 | #include <linux1/syscalls.h> | |||
| 25 | #include <linux1/userfaultfd_k.h> | |||
| 26 | #include <linux1/mempolicy.h> | |||
| 27 | #include <linux1/ioctl.h> | |||
| 28 | #include <linux1/security.h> | |||
| 29 | ||||
| 30 | static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly__attribute__((__section__(".data..read_mostly"))); | |||
| 31 | ||||
| 32 | enum userfaultfd_state { | |||
| 33 | UFFD_STATE_WAIT_API, | |||
| 34 | UFFD_STATE_RUNNING, | |||
| 35 | }; | |||
| 36 | ||||
| 37 | /* | |||
| 38 | * Start with fault_pending_wqh and fault_wqh so they're more likely | |||
| 39 | * to be in the same cacheline. | |||
| 40 | */ | |||
| 41 | struct userfaultfd_ctx { | |||
| 42 | /* waitqueue head for the pending (i.e. not read) userfaults */ | |||
| 43 | wait_queue_head_t fault_pending_wqh; | |||
| 44 | /* waitqueue head for the userfaults */ | |||
| 45 | wait_queue_head_t fault_wqh; | |||
| 46 | /* waitqueue head for the pseudo fd to wakeup poll/read */ | |||
| 47 | wait_queue_head_t fd_wqh; | |||
| 48 | /* a refile sequence protected by fault_pending_wqh lock */ | |||
| 49 | struct seqcount refile_seq; | |||
| 50 | /* pseudo fd refcounting */ | |||
| 51 | atomic_t refcount; | |||
| 52 | /* userfaultfd syscall flags */ | |||
| 53 | unsigned int flags; | |||
| 54 | /* state machine */ | |||
| 55 | enum userfaultfd_state state; | |||
| 56 | /* released */ | |||
| 57 | bool released; | |||
| 58 | /* mm with one ore more vmas attached to this userfaultfd_ctx */ | |||
| 59 | struct mm_struct *mm; | |||
| 60 | }; | |||
| 61 | ||||
| 62 | struct userfaultfd_wait_queue { | |||
| 63 | struct uffd_msg msg; | |||
| 64 | wait_queue_t wq; | |||
| 65 | struct userfaultfd_ctx *ctx; | |||
| 66 | }; | |||
| 67 | ||||
| 68 | struct userfaultfd_wake_range { | |||
| 69 | unsigned long start; | |||
| 70 | unsigned long len; | |||
| 71 | }; | |||
| 72 | ||||
| 73 | static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode, | |||
| 74 | int wake_flags, void *key) | |||
| 75 | { | |||
| 76 | struct userfaultfd_wake_range *range = key; | |||
| 77 | int ret; | |||
| 78 | struct userfaultfd_wait_queue *uwq; | |||
| 79 | unsigned long start, len; | |||
| 80 | ||||
| 81 | uwq = container_of(wq, struct userfaultfd_wait_queue, wq)({ const typeof( ((struct userfaultfd_wait_queue *)0)->wq ) *__mptr = (wq); (struct userfaultfd_wait_queue *)( (char *)__mptr - __builtin_offsetof(struct userfaultfd_wait_queue, wq) );}); | |||
| 82 | ret = 0; | |||
| 83 | /* len == 0 means wake all */ | |||
| 84 | start = range->start; | |||
| 85 | len = range->len; | |||
| 86 | if (len && (start > uwq->msg.arg.pagefault.address || | |||
| 87 | start + len <= uwq->msg.arg.pagefault.address)) | |||
| 88 | goto out; | |||
| 89 | ret = wake_up_state(wq->private, mode); | |||
| 90 | if (ret) | |||
| 91 | /* | |||
| 92 | * Wake only once, autoremove behavior. | |||
| 93 | * | |||
| 94 | * After the effect of list_del_init is visible to the | |||
| 95 | * other CPUs, the waitqueue may disappear from under | |||
| 96 | * us, see the !list_empty_careful() in | |||
| 97 | * handle_userfault(). try_to_wake_up() has an | |||
| 98 | * implicit smp_mb__before_spinlock, and the | |||
| 99 | * wq->private is read before calling the extern | |||
| 100 | * function "wake_up_state" (which in turns calls | |||
| 101 | * try_to_wake_up). While the spin_lock;spin_unlock; | |||
| 102 | * wouldn't be enough, the smp_mb__before_spinlock is | |||
| 103 | * enough to avoid an explicit smp_mb() here. | |||
| 104 | */ | |||
| 105 | list_del_init(&wq->task_list); | |||
| 106 | out: | |||
| 107 | return ret; | |||
| 108 | } | |||
| 109 | ||||
| 110 | /** | |||
| 111 | * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd | |||
| 112 | * context. | |||
| 113 | * @ctx: [in] Pointer to the userfaultfd context. | |||
| 114 | * | |||
| 115 | * Returns: In case of success, returns not zero. | |||
| 116 | */ | |||
| 117 | static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx) | |||
| 118 | { | |||
| 119 | if (!atomic_inc_not_zero(&ctx->refcount)atomic_add_unless((&ctx->refcount), 1, 0)) | |||
| 120 | BUG()do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (120), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); | |||
| 121 | } | |||
| 122 | ||||
| 123 | /** | |||
| 124 | * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd | |||
| 125 | * context. | |||
| 126 | * @ctx: [in] Pointer to userfaultfd context. | |||
| 127 | * | |||
| 128 | * The userfaultfd context reference must have been previously acquired either | |||
| 129 | * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget(). | |||
| 130 | */ | |||
| 131 | static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx) | |||
| 132 | { | |||
| 133 | if (atomic_dec_and_test(&ctx->refcount)) { | |||
| 134 | VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock))do { if ((spin_is_locked(&ctx->fault_pending_wqh.lock) )) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (134), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 135 | VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh))do { if ((waitqueue_active(&ctx->fault_pending_wqh))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (135), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 136 | VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock))do { if ((spin_is_locked(&ctx->fault_wqh.lock))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (136), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 137 | VM_BUG_ON(waitqueue_active(&ctx->fault_wqh))do { if ((waitqueue_active(&ctx->fault_wqh))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (137), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 138 | VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock))do { if ((spin_is_locked(&ctx->fd_wqh.lock))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (138), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 139 | VM_BUG_ON(waitqueue_active(&ctx->fd_wqh))do { if ((waitqueue_active(&ctx->fd_wqh))) do { asm volatile ("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (139), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 140 | mmdrop(ctx->mm); | |||
| 141 | kmem_cache_free(userfaultfd_ctx_cachep, ctx); | |||
| 142 | } | |||
| 143 | } | |||
| 144 | ||||
| 145 | static inlineinline __attribute__((no_instrument_function)) void msg_init(struct uffd_msg *msg) | |||
| 146 | { | |||
| 147 | BUILD_BUG_ON(sizeof(struct uffd_msg) != 32)do { bool __cond = !(!(sizeof(struct uffd_msg) != 32)); extern void __compiletime_assert_147(void) ; if (__cond) __compiletime_assert_147 (); do { ((void)sizeof(char[1 - 2 * __cond])); } while (0); } while (0); | |||
| 148 | /* | |||
| 149 | * Must use memset to zero out the paddings or kernel data is | |||
| 150 | * leaked to userland. | |||
| 151 | */ | |||
| 152 | memset(msg, 0, sizeof(struct uffd_msg)); | |||
| 153 | } | |||
| 154 | ||||
| 155 | static inlineinline __attribute__((no_instrument_function)) struct uffd_msg userfault_msg(unsigned long address, | |||
| 156 | unsigned int flags, | |||
| 157 | unsigned long reason) | |||
| 158 | { | |||
| 159 | struct uffd_msg msg; | |||
| 160 | msg_init(&msg); | |||
| 161 | msg.event = UFFD_EVENT_PAGEFAULT0x12; | |||
| 162 | msg.arg.pagefault.address = address; | |||
| 163 | if (flags & FAULT_FLAG_WRITE0x01) | |||
| 164 | /* | |||
| 165 | * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the | |||
| 166 | * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE | |||
| 167 | * was not set in a UFFD_EVENT_PAGEFAULT, it means it | |||
| 168 | * was a read fault, otherwise if set it means it's | |||
| 169 | * a write fault. | |||
| 170 | */ | |||
| 171 | msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE(1<<0); | |||
| 172 | if (reason & VM_UFFD_WP0x00001000) | |||
| 173 | /* | |||
| 174 | * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the | |||
| 175 | * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was | |||
| 176 | * not set in a UFFD_EVENT_PAGEFAULT, it means it was | |||
| 177 | * a missing fault, otherwise if set it means it's a | |||
| 178 | * write protect fault. | |||
| 179 | */ | |||
| 180 | msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP(1<<1); | |||
| 181 | return msg; | |||
| 182 | } | |||
| 183 | ||||
| 184 | /* | |||
| 185 | * Verify the pagetables are still not ok after having reigstered into | |||
| 186 | * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any | |||
| 187 | * userfault that has already been resolved, if userfaultfd_read and | |||
| 188 | * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different | |||
| 189 | * threads. | |||
| 190 | */ | |||
| 191 | static inlineinline __attribute__((no_instrument_function)) bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, | |||
| 192 | unsigned long address, | |||
| 193 | unsigned long flags, | |||
| 194 | unsigned long reason) | |||
| 195 | { | |||
| 196 | struct mm_struct *mm = ctx->mm; | |||
| 197 | pgd_t *pgd; | |||
| 198 | pud_t *pud; | |||
| 199 | pmd_t *pmd, _pmd; | |||
| 200 | pte_t *pte; | |||
| 201 | bool ret = true; | |||
| 202 | ||||
| 203 | VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem))do { if ((!rwsem_is_locked(&mm->mmap_sem))) do { asm volatile ("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (203), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 204 | ||||
| 205 | pgd = pgd_offset(mm, address)((mm)->pgd + ((((address)) >> 39) & (512 - 1))); | |||
| 206 | if (!pgd_present(*pgd)) | |||
| 207 | goto out; | |||
| 208 | pud = pud_offset(pgd, address); | |||
| 209 | if (!pud_present(*pud)) | |||
| 210 | goto out; | |||
| 211 | pmd = pmd_offset(pud, address); | |||
| 212 | /* | |||
| 213 | * READ_ONCE must function as a barrier with narrower scope | |||
| 214 | * and it must be equivalent to: | |||
| 215 | * _pmd = *pmd; barrier(); | |||
| 216 | * | |||
| 217 | * This is to deal with the instability (as in | |||
| 218 | * pmd_trans_unstable) of the pmd. | |||
| 219 | */ | |||
| 220 | _pmd = READ_ONCE(*pmd)({ union { typeof(*pmd) __val; char __c[1]; } __u; if (1) __read_once_size (&(*pmd), __u.__c, sizeof(*pmd)); else __read_once_size_nocheck (&(*pmd), __u.__c, sizeof(*pmd)); __u.__val; }); | |||
| 221 | if (!pmd_present(_pmd)) | |||
| 222 | goto out; | |||
| 223 | ||||
| 224 | ret = false; | |||
| 225 | if (pmd_trans_huge(_pmd)) | |||
| 226 | goto out; | |||
| 227 | ||||
| 228 | /* | |||
| 229 | * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it | |||
| 230 | * and use the standard pte_offset_map() instead of parsing _pmd. | |||
| 231 | */ | |||
| 232 | pte = pte_offset_map(pmd, address)pte_offset_kernel((pmd), (address)); | |||
| 233 | /* | |||
| 234 | * Lockless access: we're in a wait_event so it's ok if it | |||
| 235 | * changes under us. | |||
| 236 | */ | |||
| 237 | if (pte_none(*pte)) | |||
| 238 | ret = true; | |||
| 239 | pte_unmap(pte)((void)(pte)); | |||
| 240 | ||||
| 241 | out: | |||
| 242 | return ret; | |||
| 243 | } | |||
| 244 | ||||
| 245 | /* | |||
| 246 | * The locking rules involved in returning VM_FAULT_RETRY depending on | |||
| 247 | * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and | |||
| 248 | * FAULT_FLAG_KILLABLE are not straightforward. The "Caution" | |||
| 249 | * recommendation in __lock_page_or_retry is not an understatement. | |||
| 250 | * | |||
| 251 | * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released | |||
| 252 | * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is | |||
| 253 | * not set. | |||
| 254 | * | |||
| 255 | * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not | |||
| 256 | * set, VM_FAULT_RETRY can still be returned if and only if there are | |||
| 257 | * fatal_signal_pending()s, and the mmap_sem must be released before | |||
| 258 | * returning it. | |||
| 259 | */ | |||
| 260 | int handle_userfault(struct fault_env *fe, unsigned long reason) | |||
| 261 | { | |||
| 262 | struct mm_struct *mm = fe->vma->vm_mm; | |||
| 263 | struct userfaultfd_ctx *ctx; | |||
| 264 | struct userfaultfd_wait_queue uwq; | |||
| 265 | int ret; | |||
| 266 | bool must_wait, return_to_userland; | |||
| 267 | ||||
| 268 | BUG_ON(!rwsem_is_locked(&mm->mmap_sem))do { if ((!rwsem_is_locked(&mm->mmap_sem))) do { asm volatile ("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (268), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 269 | ||||
| 270 | ret = VM_FAULT_SIGBUS0x0002; | |||
| 271 | ctx = fe->vma->vm_userfaultfd_ctx.ctx; | |||
| 272 | if (!ctx) | |||
| 273 | goto out; | |||
| 274 | ||||
| 275 | BUG_ON(ctx->mm != mm)do { if ((ctx->mm != mm)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (275), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 276 | ||||
| 277 | VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP))do { if ((reason & ~(0x00000200|0x00001000))) do { asm volatile ("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (277), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 278 | VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP))do { if ((!(reason & 0x00000200) ^ !!(reason & 0x00001000 ))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (278), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 279 | ||||
| 280 | /* | |||
| 281 | * If it's already released don't get it. This avoids to loop | |||
| 282 | * in __get_user_pages if userfaultfd_release waits on the | |||
| 283 | * caller of handle_userfault to release the mmap_sem. | |||
| 284 | */ | |||
| 285 | if (unlikely(ACCESS_ONCE(ctx->released))((*({ __attribute__((unused)) typeof(ctx->released) __var = ( typeof(ctx->released)) 0; (volatile typeof(ctx->released ) *)&(ctx->released); })))) | |||
| 286 | goto out; | |||
| 287 | ||||
| 288 | /* | |||
| 289 | * We don't do userfault handling for the final child pid update. | |||
| 290 | */ | |||
| 291 | if (currentget_current()->flags & PF_EXITING0x00000004) | |||
| 292 | goto out; | |||
| 293 | ||||
| 294 | /* | |||
| 295 | * Check that we can return VM_FAULT_RETRY. | |||
| 296 | * | |||
| 297 | * NOTE: it should become possible to return VM_FAULT_RETRY | |||
| 298 | * even if FAULT_FLAG_TRIED is set without leading to gup() | |||
| 299 | * -EBUSY failures, if the userfaultfd is to be extended for | |||
| 300 | * VM_UFFD_WP tracking and we intend to arm the userfault | |||
| 301 | * without first stopping userland access to the memory. For | |||
| 302 | * VM_UFFD_MISSING userfaults this is enough for now. | |||
| 303 | */ | |||
| 304 | if (unlikely(!(fe->flags & FAULT_FLAG_ALLOW_RETRY))(!(fe->flags & 0x04))) { | |||
| 305 | /* | |||
| 306 | * Validate the invariant that nowait must allow retry | |||
| 307 | * to be sure not to return SIGBUS erroneously on | |||
| 308 | * nowait invocations. | |||
| 309 | */ | |||
| 310 | BUG_ON(fe->flags & FAULT_FLAG_RETRY_NOWAIT)do { if ((fe->flags & 0x08)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (310), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 311 | #ifdef CONFIG_DEBUG_VM1 | |||
| 312 | if (printk_ratelimit()__printk_ratelimit(__func__)) { | |||
| 313 | printk(KERN_WARNING"\001" "4" | |||
| 314 | "FAULT_FLAG_ALLOW_RETRY missing %x\n", fe->flags); | |||
| 315 | dump_stack(); | |||
| 316 | } | |||
| 317 | #endif | |||
| 318 | goto out; | |||
| 319 | } | |||
| 320 | ||||
| 321 | /* | |||
| 322 | * Handle nowait, not much to do other than tell it to retry | |||
| 323 | * and wait. | |||
| 324 | */ | |||
| 325 | ret = VM_FAULT_RETRY0x0400; | |||
| 326 | if (fe->flags & FAULT_FLAG_RETRY_NOWAIT0x08) | |||
| 327 | goto out; | |||
| 328 | ||||
| 329 | /* take the reference before dropping the mmap_sem */ | |||
| 330 | userfaultfd_ctx_get(ctx); | |||
| 331 | ||||
| 332 | init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function); | |||
| 333 | uwq.wq.private = currentget_current(); | |||
| 334 | uwq.msg = userfault_msg(fe->address, fe->flags, reason); | |||
| 335 | uwq.ctx = ctx; | |||
| 336 | ||||
| 337 | return_to_userland = | |||
| 338 | (fe->flags & (FAULT_FLAG_USER0x40|FAULT_FLAG_KILLABLE0x10)) == | |||
| 339 | (FAULT_FLAG_USER0x40|FAULT_FLAG_KILLABLE0x10); | |||
| 340 | ||||
| 341 | spin_lock(&ctx->fault_pending_wqh.lock); | |||
| 342 | /* | |||
| 343 | * After the __add_wait_queue the uwq is visible to userland | |||
| 344 | * through poll/read(). | |||
| 345 | */ | |||
| 346 | __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq); | |||
| 347 | /* | |||
| 348 | * The smp_mb() after __set_current_state prevents the reads | |||
| 349 | * following the spin_unlock to happen before the list_add in | |||
| 350 | * __add_wait_queue. | |||
| 351 | */ | |||
| 352 | set_current_state(return_to_userland ? TASK_INTERRUPTIBLE :do { get_current()->task_state_change = ({ __label__ __here ; __here: (unsigned long)&&__here; }); do { (void)({ __typeof__ (*((&get_current()->state))) __ret = (((return_to_userland ? 1 : (128 | 2)))); switch (sizeof(*((&get_current()-> state)))) { case 1: asm volatile ("" "xchg" "b %b0, %1\n" : "+q" (__ret), "+m" (*((&get_current()->state))) : : "memory" , "cc"); break; case 2: asm volatile ("" "xchg" "w %w0, %1\n" : "+r" (__ret), "+m" (*((&get_current()->state))) : : "memory", "cc"); break; case 4: asm volatile ("" "xchg" "l %0, %1\n" : "+r" (__ret), "+m" (*((&get_current()->state))) : : "memory", "cc"); break; case 8: asm volatile ("" "xchg" "q %q0, %1\n" : "+r" (__ret), "+m" (*((&get_current()->state))) : : "memory", "cc"); break; default: __xchg_wrong_size(); } __ret ; }); } while (0); } while (0) | |||
| 353 | TASK_KILLABLE)do { get_current()->task_state_change = ({ __label__ __here ; __here: (unsigned long)&&__here; }); do { (void)({ __typeof__ (*((&get_current()->state))) __ret = (((return_to_userland ? 1 : (128 | 2)))); switch (sizeof(*((&get_current()-> state)))) { case 1: asm volatile ("" "xchg" "b %b0, %1\n" : "+q" (__ret), "+m" (*((&get_current()->state))) : : "memory" , "cc"); break; case 2: asm volatile ("" "xchg" "w %w0, %1\n" : "+r" (__ret), "+m" (*((&get_current()->state))) : : "memory", "cc"); break; case 4: asm volatile ("" "xchg" "l %0, %1\n" : "+r" (__ret), "+m" (*((&get_current()->state))) : : "memory", "cc"); break; case 8: asm volatile ("" "xchg" "q %q0, %1\n" : "+r" (__ret), "+m" (*((&get_current()->state))) : : "memory", "cc"); break; default: __xchg_wrong_size(); } __ret ; }); } while (0); } while (0); | |||
| 354 | spin_unlock(&ctx->fault_pending_wqh.lock); | |||
| 355 | ||||
| 356 | must_wait = userfaultfd_must_wait(ctx, fe->address, fe->flags, reason); | |||
| 357 | up_read(&mm->mmap_sem); | |||
| 358 | ||||
| 359 | if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&(must_wait && !(*({ __attribute__((unused)) typeof(ctx ->released) __var = ( typeof(ctx->released)) 0; (volatile typeof(ctx->released) *)&(ctx->released); })) && (return_to_userland ? !signal_pending(get_current()) : !fatal_signal_pending (get_current()))) | |||
| 360 | (return_to_userland ? !signal_pending(current) :(must_wait && !(*({ __attribute__((unused)) typeof(ctx ->released) __var = ( typeof(ctx->released)) 0; (volatile typeof(ctx->released) *)&(ctx->released); })) && (return_to_userland ? !signal_pending(get_current()) : !fatal_signal_pending (get_current()))) | |||
| 361 | !fatal_signal_pending(current)))(must_wait && !(*({ __attribute__((unused)) typeof(ctx ->released) __var = ( typeof(ctx->released)) 0; (volatile typeof(ctx->released) *)&(ctx->released); })) && (return_to_userland ? !signal_pending(get_current()) : !fatal_signal_pending (get_current())))) { | |||
| 362 | wake_up_poll(&ctx->fd_wqh, POLLIN)__wake_up(&ctx->fd_wqh, (1 | 2), 1, (void *) (0x0001)); | |||
| 363 | schedule(); | |||
| 364 | ret |= VM_FAULT_MAJOR0x0004; | |||
| 365 | } | |||
| 366 | ||||
| 367 | __set_current_state(TASK_RUNNING)do { get_current()->task_state_change = ({ __label__ __here ; __here: (unsigned long)&&__here; }); get_current()-> state = (0); } while (0); | |||
| 368 | ||||
| 369 | if (return_to_userland) { | |||
| 370 | if (signal_pending(currentget_current()) && | |||
| 371 | !fatal_signal_pending(currentget_current())) { | |||
| 372 | /* | |||
| 373 | * If we got a SIGSTOP or SIGCONT and this is | |||
| 374 | * a normal userland page fault, just let | |||
| 375 | * userland return so the signal will be | |||
| 376 | * handled and gdb debugging works. The page | |||
| 377 | * fault code immediately after we return from | |||
| 378 | * this function is going to release the | |||
| 379 | * mmap_sem and it's not depending on it | |||
| 380 | * (unlike gup would if we were not to return | |||
| 381 | * VM_FAULT_RETRY). | |||
| 382 | * | |||
| 383 | * If a fatal signal is pending we still take | |||
| 384 | * the streamlined VM_FAULT_RETRY failure path | |||
| 385 | * and there's no need to retake the mmap_sem | |||
| 386 | * in such case. | |||
| 387 | */ | |||
| 388 | down_read(&mm->mmap_sem); | |||
| 389 | ret = 0; | |||
| 390 | } | |||
| 391 | } | |||
| 392 | ||||
| 393 | /* | |||
| 394 | * Here we race with the list_del; list_add in | |||
| 395 | * userfaultfd_ctx_read(), however because we don't ever run | |||
| 396 | * list_del_init() to refile across the two lists, the prev | |||
| 397 | * and next pointers will never point to self. list_add also | |||
| 398 | * would never let any of the two pointers to point to | |||
| 399 | * self. So list_empty_careful won't risk to see both pointers | |||
| 400 | * pointing to self at any time during the list refile. The | |||
| 401 | * only case where list_del_init() is called is the full | |||
| 402 | * removal in the wake function and there we don't re-list_add | |||
| 403 | * and it's fine not to block on the spinlock. The uwq on this | |||
| 404 | * kernel stack can be released after the list_del_init. | |||
| 405 | */ | |||
| 406 | if (!list_empty_careful(&uwq.wq.task_list)) { | |||
| 407 | spin_lock(&ctx->fault_pending_wqh.lock); | |||
| 408 | /* | |||
| 409 | * No need of list_del_init(), the uwq on the stack | |||
| 410 | * will be freed shortly anyway. | |||
| 411 | */ | |||
| 412 | list_del(&uwq.wq.task_list); | |||
| 413 | spin_unlock(&ctx->fault_pending_wqh.lock); | |||
| 414 | } | |||
| 415 | ||||
| 416 | /* | |||
| 417 | * ctx may go away after this if the userfault pseudo fd is | |||
| 418 | * already released. | |||
| 419 | */ | |||
| 420 | userfaultfd_ctx_put(ctx); | |||
| 421 | ||||
| 422 | out: | |||
| 423 | return ret; | |||
| 424 | } | |||
| 425 | ||||
| 426 | static int userfaultfd_release(struct inode *inode, struct file *file) | |||
| 427 | { | |||
| 428 | struct userfaultfd_ctx *ctx = file->private_data; | |||
| 429 | struct mm_struct *mm = ctx->mm; | |||
| 430 | struct vm_area_struct *vma, *prev; | |||
| 431 | /* len == 0 means wake all */ | |||
| 432 | struct userfaultfd_wake_range range = { .len = 0, }; | |||
| 433 | unsigned long new_flags; | |||
| 434 | ||||
| 435 | ACCESS_ONCE(ctx->released)(*({ __attribute__((unused)) typeof(ctx->released) __var = ( typeof(ctx->released)) 0; (volatile typeof(ctx->released ) *)&(ctx->released); })) = true; | |||
| 436 | ||||
| 437 | if (!mmget_not_zero(mm)) | |||
| 438 | goto wakeup; | |||
| 439 | ||||
| 440 | /* | |||
| 441 | * Flush page faults out of all CPUs. NOTE: all page faults | |||
| 442 | * must be retried without returning VM_FAULT_SIGBUS if | |||
| 443 | * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx | |||
| 444 | * changes while handle_userfault released the mmap_sem. So | |||
| 445 | * it's critical that released is set to true (above), before | |||
| 446 | * taking the mmap_sem for writing. | |||
| 447 | */ | |||
| 448 | down_write(&mm->mmap_sem); | |||
| 449 | prev = NULL((void *)0); | |||
| 450 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |||
| 451 | cond_resched()({ ___might_sleep("fs/userfaultfd.c", 451, 0); _cond_resched( ); }); | |||
| 452 | BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^do { if ((!!vma->vm_userfaultfd_ctx.ctx ^ !!(vma->vm_flags & (0x00000200 | 0x00001000)))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (453), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0) | |||
| 453 | !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)))do { if ((!!vma->vm_userfaultfd_ctx.ctx ^ !!(vma->vm_flags & (0x00000200 | 0x00001000)))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (453), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 454 | if (vma->vm_userfaultfd_ctx.ctx != ctx) { | |||
| 455 | prev = vma; | |||
| 456 | continue; | |||
| 457 | } | |||
| 458 | new_flags = vma->vm_flags & ~(VM_UFFD_MISSING0x00000200 | VM_UFFD_WP0x00001000); | |||
| 459 | prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end, | |||
| 460 | new_flags, vma->anon_vma, | |||
| 461 | vma->vm_file, vma->vm_pgoff, | |||
| 462 | vma_policy(vma)((vma)->vm_policy), | |||
| 463 | NULL_VM_UFFD_CTX((struct vm_userfaultfd_ctx) { ((void *)0), })); | |||
| 464 | if (prev) | |||
| 465 | vma = prev; | |||
| 466 | else | |||
| 467 | prev = vma; | |||
| 468 | vma->vm_flags = new_flags; | |||
| 469 | vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX((struct vm_userfaultfd_ctx) { ((void *)0), }); | |||
| 470 | } | |||
| 471 | up_write(&mm->mmap_sem); | |||
| 472 | mmput(mm); | |||
| 473 | wakeup: | |||
| 474 | /* | |||
| 475 | * After no new page faults can wait on this fault_*wqh, flush | |||
| 476 | * the last page faults that may have been already waiting on | |||
| 477 | * the fault_*wqh. | |||
| 478 | */ | |||
| 479 | spin_lock(&ctx->fault_pending_wqh.lock); | |||
| 480 | __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL(1 | 2), &range); | |||
| 481 | __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL(1 | 2), &range); | |||
| 482 | spin_unlock(&ctx->fault_pending_wqh.lock); | |||
| 483 | ||||
| 484 | wake_up_poll(&ctx->fd_wqh, POLLHUP)__wake_up(&ctx->fd_wqh, (1 | 2), 1, (void *) (0x0010)); | |||
| 485 | userfaultfd_ctx_put(ctx); | |||
| 486 | return 0; | |||
| 487 | } | |||
| 488 | ||||
| 489 | /* fault_pending_wqh.lock must be hold by the caller */ | |||
| 490 | static inlineinline __attribute__((no_instrument_function)) struct userfaultfd_wait_queue *find_userfault( | |||
| 491 | struct userfaultfd_ctx *ctx) | |||
| 492 | { | |||
| 493 | wait_queue_t *wq; | |||
| 494 | struct userfaultfd_wait_queue *uwq; | |||
| 495 | ||||
| 496 | VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock))do { if ((!spin_is_locked(&ctx->fault_pending_wqh.lock ))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (496), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 497 | ||||
| 498 | uwq = NULL((void *)0); | |||
| 499 | if (!waitqueue_active(&ctx->fault_pending_wqh)) | |||
| 500 | goto out; | |||
| 501 | /* walk in reverse to provide FIFO behavior to read userfaults */ | |||
| 502 | wq = list_last_entry(&ctx->fault_pending_wqh.task_list,({ const typeof( ((typeof(*wq) *)0)->task_list ) *__mptr = ((&ctx->fault_pending_wqh.task_list)->prev); (typeof (*wq) *)( (char *)__mptr - __builtin_offsetof(typeof(*wq), task_list ) );}) | |||
| 503 | typeof(*wq), task_list)({ const typeof( ((typeof(*wq) *)0)->task_list ) *__mptr = ((&ctx->fault_pending_wqh.task_list)->prev); (typeof (*wq) *)( (char *)__mptr - __builtin_offsetof(typeof(*wq), task_list ) );}); | |||
| 504 | uwq = container_of(wq, struct userfaultfd_wait_queue, wq)({ const typeof( ((struct userfaultfd_wait_queue *)0)->wq ) *__mptr = (wq); (struct userfaultfd_wait_queue *)( (char *)__mptr - __builtin_offsetof(struct userfaultfd_wait_queue, wq) );}); | |||
| 505 | out: | |||
| 506 | return uwq; | |||
| 507 | } | |||
| 508 | ||||
| 509 | static unsigned int userfaultfd_poll(struct file *file, poll_table *wait) | |||
| 510 | { | |||
| 511 | struct userfaultfd_ctx *ctx = file->private_data; | |||
| 512 | unsigned int ret; | |||
| 513 | ||||
| 514 | poll_wait(file, &ctx->fd_wqh, wait); | |||
| 515 | ||||
| 516 | switch (ctx->state) { | |||
| 517 | case UFFD_STATE_WAIT_API: | |||
| 518 | return POLLERR0x0008; | |||
| 519 | case UFFD_STATE_RUNNING: | |||
| 520 | /* | |||
| 521 | * poll() never guarantees that read won't block. | |||
| 522 | * userfaults can be waken before they're read(). | |||
| 523 | */ | |||
| 524 | if (unlikely(!(file->f_flags & O_NONBLOCK))(!(file->f_flags & 00004000))) | |||
| 525 | return POLLERR0x0008; | |||
| 526 | /* | |||
| 527 | * lockless access to see if there are pending faults | |||
| 528 | * __pollwait last action is the add_wait_queue but | |||
| 529 | * the spin_unlock would allow the waitqueue_active to | |||
| 530 | * pass above the actual list_add inside | |||
| 531 | * add_wait_queue critical section. So use a full | |||
| 532 | * memory barrier to serialize the list_add write of | |||
| 533 | * add_wait_queue() with the waitqueue_active read | |||
| 534 | * below. | |||
| 535 | */ | |||
| 536 | ret = 0; | |||
| 537 | smp_mb()asm volatile("mfence":::"memory"); | |||
| 538 | if (waitqueue_active(&ctx->fault_pending_wqh)) | |||
| 539 | ret = POLLIN0x0001; | |||
| 540 | return ret; | |||
| 541 | default: | |||
| 542 | BUG()do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (542), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); | |||
| 543 | } | |||
| 544 | } | |||
| 545 | ||||
| 546 | static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, | |||
| 547 | struct uffd_msg *msg) | |||
| 548 | { | |||
| 549 | ssize_t ret; | |||
| 550 | DECLARE_WAITQUEUE(wait, current)wait_queue_t wait = { .private = get_current(), .func = default_wake_function , .task_list = { ((void *)0), ((void *)0) } }; | |||
| 551 | struct userfaultfd_wait_queue *uwq; | |||
| 552 | ||||
| 553 | /* always take the fd_wqh lock before the fault_pending_wqh lock */ | |||
| 554 | spin_lock(&ctx->fd_wqh.lock); | |||
| 555 | __add_wait_queue(&ctx->fd_wqh, &wait); | |||
| 556 | for (;;) { | |||
| 557 | set_current_state(TASK_INTERRUPTIBLE)do { get_current()->task_state_change = ({ __label__ __here ; __here: (unsigned long)&&__here; }); do { (void)({ __typeof__ (*((&get_current()->state))) __ret = (((1))); switch ( sizeof(*((&get_current()->state)))) { case 1: asm volatile ("" "xchg" "b %b0, %1\n" : "+q" (__ret), "+m" (*((&get_current ()->state))) : : "memory", "cc"); break; case 2: asm volatile ("" "xchg" "w %w0, %1\n" : "+r" (__ret), "+m" (*((&get_current ()->state))) : : "memory", "cc"); break; case 4: asm volatile ("" "xchg" "l %0, %1\n" : "+r" (__ret), "+m" (*((&get_current ()->state))) : : "memory", "cc"); break; case 8: asm volatile ("" "xchg" "q %q0, %1\n" : "+r" (__ret), "+m" (*((&get_current ()->state))) : : "memory", "cc"); break; default: __xchg_wrong_size (); } __ret; }); } while (0); } while (0); | |||
| 558 | spin_lock(&ctx->fault_pending_wqh.lock); | |||
| 559 | uwq = find_userfault(ctx); | |||
| 560 | if (uwq) { | |||
| 561 | /* | |||
| 562 | * Use a seqcount to repeat the lockless check | |||
| 563 | * in wake_userfault() to avoid missing | |||
| 564 | * wakeups because during the refile both | |||
| 565 | * waitqueue could become empty if this is the | |||
| 566 | * only userfault. | |||
| 567 | */ | |||
| 568 | write_seqcount_begin(&ctx->refile_seq); | |||
| 569 | ||||
| 570 | /* | |||
| 571 | * The fault_pending_wqh.lock prevents the uwq | |||
| 572 | * to disappear from under us. | |||
| 573 | * | |||
| 574 | * Refile this userfault from | |||
| 575 | * fault_pending_wqh to fault_wqh, it's not | |||
| 576 | * pending anymore after we read it. | |||
| 577 | * | |||
| 578 | * Use list_del() by hand (as | |||
| 579 | * userfaultfd_wake_function also uses | |||
| 580 | * list_del_init() by hand) to be sure nobody | |||
| 581 | * changes __remove_wait_queue() to use | |||
| 582 | * list_del_init() in turn breaking the | |||
| 583 | * !list_empty_careful() check in | |||
| 584 | * handle_userfault(). The uwq->wq.task_list | |||
| 585 | * must never be empty at any time during the | |||
| 586 | * refile, or the waitqueue could disappear | |||
| 587 | * from under us. The "wait_queue_head_t" | |||
| 588 | * parameter of __remove_wait_queue() is unused | |||
| 589 | * anyway. | |||
| 590 | */ | |||
| 591 | list_del(&uwq->wq.task_list); | |||
| 592 | __add_wait_queue(&ctx->fault_wqh, &uwq->wq); | |||
| 593 | ||||
| 594 | write_seqcount_end(&ctx->refile_seq); | |||
| 595 | ||||
| 596 | /* careful to always initialize msg if ret == 0 */ | |||
| 597 | *msg = uwq->msg; | |||
| 598 | spin_unlock(&ctx->fault_pending_wqh.lock); | |||
| 599 | ret = 0; | |||
| 600 | break; | |||
| 601 | } | |||
| 602 | spin_unlock(&ctx->fault_pending_wqh.lock); | |||
| 603 | if (signal_pending(currentget_current())) { | |||
| 604 | ret = -ERESTARTSYS512; | |||
| 605 | break; | |||
| 606 | } | |||
| 607 | if (no_wait) { | |||
| 608 | ret = -EAGAIN11; | |||
| 609 | break; | |||
| 610 | } | |||
| 611 | spin_unlock(&ctx->fd_wqh.lock); | |||
| 612 | schedule(); | |||
| 613 | spin_lock(&ctx->fd_wqh.lock); | |||
| 614 | } | |||
| 615 | __remove_wait_queue(&ctx->fd_wqh, &wait); | |||
| 616 | __set_current_state(TASK_RUNNING)do { get_current()->task_state_change = ({ __label__ __here ; __here: (unsigned long)&&__here; }); get_current()-> state = (0); } while (0); | |||
| 617 | spin_unlock(&ctx->fd_wqh.lock); | |||
| 618 | ||||
| 619 | return ret; | |||
| 620 | } | |||
| 621 | ||||
| 622 | static ssize_t userfaultfd_read(struct file *file, char __user *buf, | |||
| 623 | size_t count, loff_t *ppos) | |||
| 624 | { | |||
| 625 | struct userfaultfd_ctx *ctx = file->private_data; | |||
| 626 | ssize_t _ret, ret = 0; | |||
| 627 | struct uffd_msg msg; | |||
| 628 | int no_wait = file->f_flags & O_NONBLOCK00004000; | |||
| 629 | ||||
| 630 | if (ctx->state == UFFD_STATE_WAIT_API) | |||
| ||||
| 631 | return -EINVAL22; | |||
| 632 | ||||
| 633 | for (;;) { | |||
| 634 | if (count < sizeof(msg)) | |||
| 635 | return ret ? ret : -EINVAL22; | |||
| 636 | _ret = userfaultfd_ctx_read(ctx, no_wait, &msg); | |||
| 637 | if (_ret < 0) | |||
| 638 | return ret ? ret : _ret; | |||
| 639 | if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg))) | |||
| ||||
| 640 | return ret ? ret : -EFAULT14; | |||
| 641 | ret += sizeof(msg); | |||
| 642 | buf += sizeof(msg); | |||
| 643 | count -= sizeof(msg); | |||
| 644 | /* | |||
| 645 | * Allow to read more than one fault at time but only | |||
| 646 | * block if waiting for the very first one. | |||
| 647 | */ | |||
| 648 | no_wait = O_NONBLOCK00004000; | |||
| 649 | } | |||
| 650 | } | |||
| 651 | ||||
| 652 | static void __wake_userfault(struct userfaultfd_ctx *ctx, | |||
| 653 | struct userfaultfd_wake_range *range) | |||
| 654 | { | |||
| 655 | unsigned long start, end; | |||
| 656 | ||||
| 657 | start = range->start; | |||
| 658 | end = range->start + range->len; | |||
| 659 | ||||
| 660 | spin_lock(&ctx->fault_pending_wqh.lock); | |||
| 661 | /* wake all in the range and autoremove */ | |||
| 662 | if (waitqueue_active(&ctx->fault_pending_wqh)) | |||
| 663 | __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL(1 | 2), | |||
| 664 | range); | |||
| 665 | if (waitqueue_active(&ctx->fault_wqh)) | |||
| 666 | __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL(1 | 2), range); | |||
| 667 | spin_unlock(&ctx->fault_pending_wqh.lock); | |||
| 668 | } | |||
| 669 | ||||
| 670 | static __always_inlineinline __attribute__((no_instrument_function)) __attribute__( (always_inline)) void wake_userfault(struct userfaultfd_ctx *ctx, | |||
| 671 | struct userfaultfd_wake_range *range) | |||
| 672 | { | |||
| 673 | unsigned seq; | |||
| 674 | bool need_wakeup; | |||
| 675 | ||||
| 676 | /* | |||
| 677 | * To be sure waitqueue_active() is not reordered by the CPU | |||
| 678 | * before the pagetable update, use an explicit SMP memory | |||
| 679 | * barrier here. PT lock release or up_read(mmap_sem) still | |||
| 680 | * have release semantics that can allow the | |||
| 681 | * waitqueue_active() to be reordered before the pte update. | |||
| 682 | */ | |||
| 683 | smp_mb()asm volatile("mfence":::"memory"); | |||
| 684 | ||||
| 685 | /* | |||
| 686 | * Use waitqueue_active because it's very frequent to | |||
| 687 | * change the address space atomically even if there are no | |||
| 688 | * userfaults yet. So we take the spinlock only when we're | |||
| 689 | * sure we've userfaults to wake. | |||
| 690 | */ | |||
| 691 | do { | |||
| 692 | seq = read_seqcount_begin(&ctx->refile_seq); | |||
| 693 | need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) || | |||
| 694 | waitqueue_active(&ctx->fault_wqh); | |||
| 695 | cond_resched()({ ___might_sleep("fs/userfaultfd.c", 695, 0); _cond_resched( ); }); | |||
| 696 | } while (read_seqcount_retry(&ctx->refile_seq, seq)); | |||
| 697 | if (need_wakeup) | |||
| 698 | __wake_userfault(ctx, range); | |||
| 699 | } | |||
| 700 | ||||
| 701 | static __always_inlineinline __attribute__((no_instrument_function)) __attribute__( (always_inline)) int validate_range(struct mm_struct *mm, | |||
| 702 | __u64 start, __u64 len) | |||
| 703 | { | |||
| 704 | __u64 task_size = mm->task_size; | |||
| 705 | ||||
| 706 | if (start & ~PAGE_MASK(~(((1UL) << 12)-1))) | |||
| 707 | return -EINVAL22; | |||
| 708 | if (len & ~PAGE_MASK(~(((1UL) << 12)-1))) | |||
| 709 | return -EINVAL22; | |||
| 710 | if (!len) | |||
| 711 | return -EINVAL22; | |||
| 712 | if (start < mmap_min_addr) | |||
| 713 | return -EINVAL22; | |||
| 714 | if (start >= task_size) | |||
| 715 | return -EINVAL22; | |||
| 716 | if (len > task_size - start) | |||
| 717 | return -EINVAL22; | |||
| 718 | return 0; | |||
| 719 | } | |||
| 720 | ||||
| 721 | static int userfaultfd_register(struct userfaultfd_ctx *ctx, | |||
| 722 | unsigned long arg) | |||
| 723 | { | |||
| 724 | struct mm_struct *mm = ctx->mm; | |||
| 725 | struct vm_area_struct *vma, *prev, *cur; | |||
| 726 | int ret; | |||
| 727 | struct uffdio_register uffdio_register; | |||
| 728 | struct uffdio_register __user *user_uffdio_register; | |||
| 729 | unsigned long vm_flags, new_flags; | |||
| 730 | bool found; | |||
| 731 | unsigned long start, end, vma_end; | |||
| 732 | ||||
| 733 | user_uffdio_register = (struct uffdio_register __user *) arg; | |||
| 734 | ||||
| 735 | ret = -EFAULT14; | |||
| 736 | if (copy_from_user(&uffdio_register, user_uffdio_register, | |||
| 737 | sizeof(uffdio_register)-sizeof(__u64))) | |||
| 738 | goto out; | |||
| 739 | ||||
| 740 | ret = -EINVAL22; | |||
| 741 | if (!uffdio_register.mode) | |||
| 742 | goto out; | |||
| 743 | if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING((__u64)1<<0)| | |||
| 744 | UFFDIO_REGISTER_MODE_WP((__u64)1<<1))) | |||
| 745 | goto out; | |||
| 746 | vm_flags = 0; | |||
| 747 | if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING((__u64)1<<0)) | |||
| 748 | vm_flags |= VM_UFFD_MISSING0x00000200; | |||
| 749 | if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP((__u64)1<<1)) { | |||
| 750 | vm_flags |= VM_UFFD_WP0x00001000; | |||
| 751 | /* | |||
| 752 | * FIXME: remove the below error constraint by | |||
| 753 | * implementing the wprotect tracking mode. | |||
| 754 | */ | |||
| 755 | ret = -EINVAL22; | |||
| 756 | goto out; | |||
| 757 | } | |||
| 758 | ||||
| 759 | ret = validate_range(mm, uffdio_register.range.start, | |||
| 760 | uffdio_register.range.len); | |||
| 761 | if (ret) | |||
| 762 | goto out; | |||
| 763 | ||||
| 764 | start = uffdio_register.range.start; | |||
| 765 | end = start + uffdio_register.range.len; | |||
| 766 | ||||
| 767 | ret = -ENOMEM12; | |||
| 768 | if (!mmget_not_zero(mm)) | |||
| 769 | goto out; | |||
| 770 | ||||
| 771 | down_write(&mm->mmap_sem); | |||
| 772 | vma = find_vma_prev(mm, start, &prev); | |||
| 773 | if (!vma) | |||
| 774 | goto out_unlock; | |||
| 775 | ||||
| 776 | /* check that there's at least one vma in the range */ | |||
| 777 | ret = -EINVAL22; | |||
| 778 | if (vma->vm_start >= end) | |||
| 779 | goto out_unlock; | |||
| 780 | ||||
| 781 | /* | |||
| 782 | * Search for not compatible vmas. | |||
| 783 | * | |||
| 784 | * FIXME: this shall be relaxed later so that it doesn't fail | |||
| 785 | * on tmpfs backed vmas (in addition to the current allowance | |||
| 786 | * on anonymous vmas). | |||
| 787 | */ | |||
| 788 | found = false; | |||
| 789 | for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) { | |||
| 790 | cond_resched()({ ___might_sleep("fs/userfaultfd.c", 790, 0); _cond_resched( ); }); | |||
| 791 | ||||
| 792 | BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^do { if ((!!cur->vm_userfaultfd_ctx.ctx ^ !!(cur->vm_flags & (0x00000200 | 0x00001000)))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (793), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0) | |||
| 793 | !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)))do { if ((!!cur->vm_userfaultfd_ctx.ctx ^ !!(cur->vm_flags & (0x00000200 | 0x00001000)))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (793), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 794 | ||||
| 795 | /* check not compatible vmas */ | |||
| 796 | ret = -EINVAL22; | |||
| 797 | if (cur->vm_ops) | |||
| 798 | goto out_unlock; | |||
| 799 | ||||
| 800 | /* | |||
| 801 | * Check that this vma isn't already owned by a | |||
| 802 | * different userfaultfd. We can't allow more than one | |||
| 803 | * userfaultfd to own a single vma simultaneously or we | |||
| 804 | * wouldn't know which one to deliver the userfaults to. | |||
| 805 | */ | |||
| 806 | ret = -EBUSY16; | |||
| 807 | if (cur->vm_userfaultfd_ctx.ctx && | |||
| 808 | cur->vm_userfaultfd_ctx.ctx != ctx) | |||
| 809 | goto out_unlock; | |||
| 810 | ||||
| 811 | found = true; | |||
| 812 | } | |||
| 813 | BUG_ON(!found)do { if ((!found)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (813), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 814 | ||||
| 815 | if (vma->vm_start < start) | |||
| 816 | prev = vma; | |||
| 817 | ||||
| 818 | ret = 0; | |||
| 819 | do { | |||
| 820 | cond_resched()({ ___might_sleep("fs/userfaultfd.c", 820, 0); _cond_resched( ); }); | |||
| 821 | ||||
| 822 | BUG_ON(vma->vm_ops)do { if ((vma->vm_ops)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (822), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 823 | BUG_ON(vma->vm_userfaultfd_ctx.ctx &&do { if ((vma->vm_userfaultfd_ctx.ctx && vma->vm_userfaultfd_ctx .ctx != ctx)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (824), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0) | |||
| 824 | vma->vm_userfaultfd_ctx.ctx != ctx)do { if ((vma->vm_userfaultfd_ctx.ctx && vma->vm_userfaultfd_ctx .ctx != ctx)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (824), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 825 | ||||
| 826 | /* | |||
| 827 | * Nothing to do: this vma is already registered into this | |||
| 828 | * userfaultfd and with the right tracking mode too. | |||
| 829 | */ | |||
| 830 | if (vma->vm_userfaultfd_ctx.ctx == ctx && | |||
| 831 | (vma->vm_flags & vm_flags) == vm_flags) | |||
| 832 | goto skip; | |||
| 833 | ||||
| 834 | if (vma->vm_start > start) | |||
| 835 | start = vma->vm_start; | |||
| 836 | vma_end = min(end, vma->vm_end)({ typeof(end) __UNIQUE_ID_min1_22 = (end); typeof(vma->vm_end ) __UNIQUE_ID_min2_23 = (vma->vm_end); (void) (&__UNIQUE_ID_min1_22 == &__UNIQUE_ID_min2_23); __UNIQUE_ID_min1_22 < __UNIQUE_ID_min2_23 ? __UNIQUE_ID_min1_22 : __UNIQUE_ID_min2_23; }); | |||
| 837 | ||||
| 838 | new_flags = (vma->vm_flags & ~vm_flags) | vm_flags; | |||
| 839 | prev = vma_merge(mm, prev, start, vma_end, new_flags, | |||
| 840 | vma->anon_vma, vma->vm_file, vma->vm_pgoff, | |||
| 841 | vma_policy(vma)((vma)->vm_policy), | |||
| 842 | ((struct vm_userfaultfd_ctx){ ctx })); | |||
| 843 | if (prev) { | |||
| 844 | vma = prev; | |||
| 845 | goto next; | |||
| 846 | } | |||
| 847 | if (vma->vm_start < start) { | |||
| 848 | ret = split_vma(mm, vma, start, 1); | |||
| 849 | if (ret) | |||
| 850 | break; | |||
| 851 | } | |||
| 852 | if (vma->vm_end > end) { | |||
| 853 | ret = split_vma(mm, vma, end, 0); | |||
| 854 | if (ret) | |||
| 855 | break; | |||
| 856 | } | |||
| 857 | next: | |||
| 858 | /* | |||
| 859 | * In the vma_merge() successful mprotect-like case 8: | |||
| 860 | * the next vma was merged into the current one and | |||
| 861 | * the current one has not been updated yet. | |||
| 862 | */ | |||
| 863 | vma->vm_flags = new_flags; | |||
| 864 | vma->vm_userfaultfd_ctx.ctx = ctx; | |||
| 865 | ||||
| 866 | skip: | |||
| 867 | prev = vma; | |||
| 868 | start = vma->vm_end; | |||
| 869 | vma = vma->vm_next; | |||
| 870 | } while (vma && vma->vm_start < end); | |||
| 871 | out_unlock: | |||
| 872 | up_write(&mm->mmap_sem); | |||
| 873 | mmput(mm); | |||
| 874 | if (!ret) { | |||
| 875 | /* | |||
| 876 | * Now that we scanned all vmas we can already tell | |||
| 877 | * userland which ioctls methods are guaranteed to | |||
| 878 | * succeed on this range. | |||
| 879 | */ | |||
| 880 | if (put_user(UFFD_API_RANGE_IOCTLS,({ int __ret_pu; __typeof__(*(&user_uffdio_register->ioctls )) __pu_val; (void)0; __might_fault("fs/userfaultfd.c", 881); __pu_val = ((__u64)1 << (0x02) | (__u64)1 << (0x03 ) | (__u64)1 << (0x04)); switch (sizeof(*(&user_uffdio_register ->ioctls))) { case 1: asm volatile("call __put_user_" "1" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register-> ioctls)))(__pu_val)), "c" (&user_uffdio_register->ioctls ) : "ebx"); break; case 2: asm volatile("call __put_user_" "2" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; case 4: asm volatile("call __put_user_" "4" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; case 8: asm volatile("call __put_user_" "8" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; default: asm volatile("call __put_user_" "X" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; } __builtin_expect(__ret_pu, 0); }) | |||
| 881 | &user_uffdio_register->ioctls)({ int __ret_pu; __typeof__(*(&user_uffdio_register->ioctls )) __pu_val; (void)0; __might_fault("fs/userfaultfd.c", 881); __pu_val = ((__u64)1 << (0x02) | (__u64)1 << (0x03 ) | (__u64)1 << (0x04)); switch (sizeof(*(&user_uffdio_register ->ioctls))) { case 1: asm volatile("call __put_user_" "1" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register-> ioctls)))(__pu_val)), "c" (&user_uffdio_register->ioctls ) : "ebx"); break; case 2: asm volatile("call __put_user_" "2" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; case 4: asm volatile("call __put_user_" "4" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; case 8: asm volatile("call __put_user_" "8" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; default: asm volatile("call __put_user_" "X" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_register ->ioctls)))(__pu_val)), "c" (&user_uffdio_register-> ioctls) : "ebx"); break; } __builtin_expect(__ret_pu, 0); })) | |||
| 882 | ret = -EFAULT14; | |||
| 883 | } | |||
| 884 | out: | |||
| 885 | return ret; | |||
| 886 | } | |||
| 887 | ||||
| 888 | static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, | |||
| 889 | unsigned long arg) | |||
| 890 | { | |||
| 891 | struct mm_struct *mm = ctx->mm; | |||
| 892 | struct vm_area_struct *vma, *prev, *cur; | |||
| 893 | int ret; | |||
| 894 | struct uffdio_range uffdio_unregister; | |||
| 895 | unsigned long new_flags; | |||
| 896 | bool found; | |||
| 897 | unsigned long start, end, vma_end; | |||
| 898 | const void __user *buf = (void __user *)arg; | |||
| 899 | ||||
| 900 | ret = -EFAULT14; | |||
| 901 | if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister))) | |||
| 902 | goto out; | |||
| 903 | ||||
| 904 | ret = validate_range(mm, uffdio_unregister.start, | |||
| 905 | uffdio_unregister.len); | |||
| 906 | if (ret) | |||
| 907 | goto out; | |||
| 908 | ||||
| 909 | start = uffdio_unregister.start; | |||
| 910 | end = start + uffdio_unregister.len; | |||
| 911 | ||||
| 912 | ret = -ENOMEM12; | |||
| 913 | if (!mmget_not_zero(mm)) | |||
| 914 | goto out; | |||
| 915 | ||||
| 916 | down_write(&mm->mmap_sem); | |||
| 917 | vma = find_vma_prev(mm, start, &prev); | |||
| 918 | if (!vma) | |||
| 919 | goto out_unlock; | |||
| 920 | ||||
| 921 | /* check that there's at least one vma in the range */ | |||
| 922 | ret = -EINVAL22; | |||
| 923 | if (vma->vm_start >= end) | |||
| 924 | goto out_unlock; | |||
| 925 | ||||
| 926 | /* | |||
| 927 | * Search for not compatible vmas. | |||
| 928 | * | |||
| 929 | * FIXME: this shall be relaxed later so that it doesn't fail | |||
| 930 | * on tmpfs backed vmas (in addition to the current allowance | |||
| 931 | * on anonymous vmas). | |||
| 932 | */ | |||
| 933 | found = false; | |||
| 934 | ret = -EINVAL22; | |||
| 935 | for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) { | |||
| 936 | cond_resched()({ ___might_sleep("fs/userfaultfd.c", 936, 0); _cond_resched( ); }); | |||
| 937 | ||||
| 938 | BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^do { if ((!!cur->vm_userfaultfd_ctx.ctx ^ !!(cur->vm_flags & (0x00000200 | 0x00001000)))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (939), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0) | |||
| 939 | !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)))do { if ((!!cur->vm_userfaultfd_ctx.ctx ^ !!(cur->vm_flags & (0x00000200 | 0x00001000)))) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (939), "i" (sizeof(struct bug_entry) )); do { } while (1); } while (0); } while (0); | |||
| 940 | ||||
| 941 | /* | |||
| 942 | * Check not compatible vmas, not strictly required | |||
| 943 | * here as not compatible vmas cannot have an | |||
| 944 | * userfaultfd_ctx registered on them, but this | |||
| 945 | * provides for more strict behavior to notice | |||
| 946 | * unregistration errors. | |||
| 947 | */ | |||
| 948 | if (cur->vm_ops) | |||
| 949 | goto out_unlock; | |||
| 950 | ||||
| 951 | found = true; | |||
| 952 | } | |||
| 953 | BUG_ON(!found)do { if ((!found)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (953), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 954 | ||||
| 955 | if (vma->vm_start < start) | |||
| 956 | prev = vma; | |||
| 957 | ||||
| 958 | ret = 0; | |||
| 959 | do { | |||
| 960 | cond_resched()({ ___might_sleep("fs/userfaultfd.c", 960, 0); _cond_resched( ); }); | |||
| 961 | ||||
| 962 | BUG_ON(vma->vm_ops)do { if ((vma->vm_ops)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (962), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 963 | ||||
| 964 | /* | |||
| 965 | * Nothing to do: this vma is already registered into this | |||
| 966 | * userfaultfd and with the right tracking mode too. | |||
| 967 | */ | |||
| 968 | if (!vma->vm_userfaultfd_ctx.ctx) | |||
| 969 | goto skip; | |||
| 970 | ||||
| 971 | if (vma->vm_start > start) | |||
| 972 | start = vma->vm_start; | |||
| 973 | vma_end = min(end, vma->vm_end)({ typeof(end) __UNIQUE_ID_min1_24 = (end); typeof(vma->vm_end ) __UNIQUE_ID_min2_25 = (vma->vm_end); (void) (&__UNIQUE_ID_min1_24 == &__UNIQUE_ID_min2_25); __UNIQUE_ID_min1_24 < __UNIQUE_ID_min2_25 ? __UNIQUE_ID_min1_24 : __UNIQUE_ID_min2_25; }); | |||
| 974 | ||||
| 975 | new_flags = vma->vm_flags & ~(VM_UFFD_MISSING0x00000200 | VM_UFFD_WP0x00001000); | |||
| 976 | prev = vma_merge(mm, prev, start, vma_end, new_flags, | |||
| 977 | vma->anon_vma, vma->vm_file, vma->vm_pgoff, | |||
| 978 | vma_policy(vma)((vma)->vm_policy), | |||
| 979 | NULL_VM_UFFD_CTX((struct vm_userfaultfd_ctx) { ((void *)0), })); | |||
| 980 | if (prev) { | |||
| 981 | vma = prev; | |||
| 982 | goto next; | |||
| 983 | } | |||
| 984 | if (vma->vm_start < start) { | |||
| 985 | ret = split_vma(mm, vma, start, 1); | |||
| 986 | if (ret) | |||
| 987 | break; | |||
| 988 | } | |||
| 989 | if (vma->vm_end > end) { | |||
| 990 | ret = split_vma(mm, vma, end, 0); | |||
| 991 | if (ret) | |||
| 992 | break; | |||
| 993 | } | |||
| 994 | next: | |||
| 995 | /* | |||
| 996 | * In the vma_merge() successful mprotect-like case 8: | |||
| 997 | * the next vma was merged into the current one and | |||
| 998 | * the current one has not been updated yet. | |||
| 999 | */ | |||
| 1000 | vma->vm_flags = new_flags; | |||
| 1001 | vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX((struct vm_userfaultfd_ctx) { ((void *)0), }); | |||
| 1002 | ||||
| 1003 | skip: | |||
| 1004 | prev = vma; | |||
| 1005 | start = vma->vm_end; | |||
| 1006 | vma = vma->vm_next; | |||
| 1007 | } while (vma && vma->vm_start < end); | |||
| 1008 | out_unlock: | |||
| 1009 | up_write(&mm->mmap_sem); | |||
| 1010 | mmput(mm); | |||
| 1011 | out: | |||
| 1012 | return ret; | |||
| 1013 | } | |||
| 1014 | ||||
| 1015 | /* | |||
| 1016 | * userfaultfd_wake may be used in combination with the | |||
| 1017 | * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches. | |||
| 1018 | */ | |||
| 1019 | static int userfaultfd_wake(struct userfaultfd_ctx *ctx, | |||
| 1020 | unsigned long arg) | |||
| 1021 | { | |||
| 1022 | int ret; | |||
| 1023 | struct uffdio_range uffdio_wake; | |||
| 1024 | struct userfaultfd_wake_range range; | |||
| 1025 | const void __user *buf = (void __user *)arg; | |||
| 1026 | ||||
| 1027 | ret = -EFAULT14; | |||
| 1028 | if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake))) | |||
| 1029 | goto out; | |||
| 1030 | ||||
| 1031 | ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len); | |||
| 1032 | if (ret) | |||
| 1033 | goto out; | |||
| 1034 | ||||
| 1035 | range.start = uffdio_wake.start; | |||
| 1036 | range.len = uffdio_wake.len; | |||
| 1037 | ||||
| 1038 | /* | |||
| 1039 | * len == 0 means wake all and we don't want to wake all here, | |||
| 1040 | * so check it again to be sure. | |||
| 1041 | */ | |||
| 1042 | VM_BUG_ON(!range.len)do { if ((!range.len)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (1042), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 1043 | ||||
| 1044 | wake_userfault(ctx, &range); | |||
| 1045 | ret = 0; | |||
| 1046 | ||||
| 1047 | out: | |||
| 1048 | return ret; | |||
| 1049 | } | |||
| 1050 | ||||
| 1051 | static int userfaultfd_copy(struct userfaultfd_ctx *ctx, | |||
| 1052 | unsigned long arg) | |||
| 1053 | { | |||
| 1054 | __s64 ret; | |||
| 1055 | struct uffdio_copy uffdio_copy; | |||
| 1056 | struct uffdio_copy __user *user_uffdio_copy; | |||
| 1057 | struct userfaultfd_wake_range range; | |||
| 1058 | ||||
| 1059 | user_uffdio_copy = (struct uffdio_copy __user *) arg; | |||
| 1060 | ||||
| 1061 | ret = -EFAULT14; | |||
| 1062 | if (copy_from_user(&uffdio_copy, user_uffdio_copy, | |||
| 1063 | /* don't copy "copy" last field */ | |||
| 1064 | sizeof(uffdio_copy)-sizeof(__s64))) | |||
| 1065 | goto out; | |||
| 1066 | ||||
| 1067 | ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len); | |||
| 1068 | if (ret) | |||
| 1069 | goto out; | |||
| 1070 | /* | |||
| 1071 | * double check for wraparound just in case. copy_from_user() | |||
| 1072 | * will later check uffdio_copy.src + uffdio_copy.len to fit | |||
| 1073 | * in the userland range. | |||
| 1074 | */ | |||
| 1075 | ret = -EINVAL22; | |||
| 1076 | if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src) | |||
| 1077 | goto out; | |||
| 1078 | if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE((__u64)1<<0)) | |||
| 1079 | goto out; | |||
| 1080 | if (mmget_not_zero(ctx->mm)) { | |||
| 1081 | ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src, | |||
| 1082 | uffdio_copy.len); | |||
| 1083 | mmput(ctx->mm); | |||
| 1084 | } | |||
| 1085 | if (unlikely(put_user(ret, &user_uffdio_copy->copy))(({ int __ret_pu; __typeof__(*(&user_uffdio_copy->copy )) __pu_val; (void)0; __might_fault("fs/userfaultfd.c", 1085) ; __pu_val = ret; switch (sizeof(*(&user_uffdio_copy-> copy))) { case 1: asm volatile("call __put_user_" "1" : "=a" ( __ret_pu) : "0" ((typeof(*(&user_uffdio_copy->copy)))( __pu_val)), "c" (&user_uffdio_copy->copy) : "ebx"); break ; case 2: asm volatile("call __put_user_" "2" : "=a" (__ret_pu ) : "0" ((typeof(*(&user_uffdio_copy->copy)))(__pu_val )), "c" (&user_uffdio_copy->copy) : "ebx"); break; case 4: asm volatile("call __put_user_" "4" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_copy->copy)))(__pu_val)), "c" (&user_uffdio_copy->copy) : "ebx"); break; case 8: asm volatile("call __put_user_" "8" : "=a" (__ret_pu) : "0" ((typeof (*(&user_uffdio_copy->copy)))(__pu_val)), "c" (&user_uffdio_copy ->copy) : "ebx"); break; default: asm volatile("call __put_user_" "X" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_copy ->copy)))(__pu_val)), "c" (&user_uffdio_copy->copy) : "ebx"); break; } __builtin_expect(__ret_pu, 0); }))) | |||
| 1086 | return -EFAULT14; | |||
| 1087 | if (ret < 0) | |||
| 1088 | goto out; | |||
| 1089 | BUG_ON(!ret)do { if ((!ret)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (1089), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 1090 | /* len == 0 would wake all */ | |||
| 1091 | range.len = ret; | |||
| 1092 | if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE((__u64)1<<0))) { | |||
| 1093 | range.start = uffdio_copy.dst; | |||
| 1094 | wake_userfault(ctx, &range); | |||
| 1095 | } | |||
| 1096 | ret = range.len == uffdio_copy.len ? 0 : -EAGAIN11; | |||
| 1097 | out: | |||
| 1098 | return ret; | |||
| 1099 | } | |||
| 1100 | ||||
| 1101 | static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx, | |||
| 1102 | unsigned long arg) | |||
| 1103 | { | |||
| 1104 | __s64 ret; | |||
| 1105 | struct uffdio_zeropage uffdio_zeropage; | |||
| 1106 | struct uffdio_zeropage __user *user_uffdio_zeropage; | |||
| 1107 | struct userfaultfd_wake_range range; | |||
| 1108 | ||||
| 1109 | user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg; | |||
| 1110 | ||||
| 1111 | ret = -EFAULT14; | |||
| 1112 | if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage, | |||
| 1113 | /* don't copy "zeropage" last field */ | |||
| 1114 | sizeof(uffdio_zeropage)-sizeof(__s64))) | |||
| 1115 | goto out; | |||
| 1116 | ||||
| 1117 | ret = validate_range(ctx->mm, uffdio_zeropage.range.start, | |||
| 1118 | uffdio_zeropage.range.len); | |||
| 1119 | if (ret) | |||
| 1120 | goto out; | |||
| 1121 | ret = -EINVAL22; | |||
| 1122 | if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE((__u64)1<<0)) | |||
| 1123 | goto out; | |||
| 1124 | ||||
| 1125 | if (mmget_not_zero(ctx->mm)) { | |||
| 1126 | ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start, | |||
| 1127 | uffdio_zeropage.range.len); | |||
| 1128 | mmput(ctx->mm); | |||
| 1129 | } | |||
| 1130 | if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage))(({ int __ret_pu; __typeof__(*(&user_uffdio_zeropage-> zeropage)) __pu_val; (void)0; __might_fault("fs/userfaultfd.c" , 1130); __pu_val = ret; switch (sizeof(*(&user_uffdio_zeropage ->zeropage))) { case 1: asm volatile("call __put_user_" "1" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_zeropage ->zeropage)))(__pu_val)), "c" (&user_uffdio_zeropage-> zeropage) : "ebx"); break; case 2: asm volatile("call __put_user_" "2" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_zeropage ->zeropage)))(__pu_val)), "c" (&user_uffdio_zeropage-> zeropage) : "ebx"); break; case 4: asm volatile("call __put_user_" "4" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_zeropage ->zeropage)))(__pu_val)), "c" (&user_uffdio_zeropage-> zeropage) : "ebx"); break; case 8: asm volatile("call __put_user_" "8" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_zeropage ->zeropage)))(__pu_val)), "c" (&user_uffdio_zeropage-> zeropage) : "ebx"); break; default: asm volatile("call __put_user_" "X" : "=a" (__ret_pu) : "0" ((typeof(*(&user_uffdio_zeropage ->zeropage)))(__pu_val)), "c" (&user_uffdio_zeropage-> zeropage) : "ebx"); break; } __builtin_expect(__ret_pu, 0); } ))) | |||
| 1131 | return -EFAULT14; | |||
| 1132 | if (ret < 0) | |||
| 1133 | goto out; | |||
| 1134 | /* len == 0 would wake all */ | |||
| 1135 | BUG_ON(!ret)do { if ((!ret)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ("fs/userfaultfd.c"), "i" (1135), "i" ( sizeof(struct bug_entry))); do { } while (1); } while (0); } while (0); | |||
| 1136 | range.len = ret; | |||
| 1137 | if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE((__u64)1<<0))) { | |||
| 1138 | range.start = uffdio_zeropage.range.start; | |||
| 1139 | wake_userfault(ctx, &range); | |||
| 1140 | } | |||
| 1141 | ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN11; | |||
| 1142 | out: | |||
| 1143 | return ret; | |||
| 1144 | } | |||
| 1145 | ||||
| 1146 | /* | |||
| 1147 | * userland asks for a certain API version and we return which bits | |||
| 1148 | * and ioctl commands are implemented in this kernel for such API | |||
| 1149 | * version or -EINVAL if unknown. | |||
| 1150 | */ | |||
| 1151 | static int userfaultfd_api(struct userfaultfd_ctx *ctx, | |||
| 1152 | unsigned long arg) | |||
| 1153 | { | |||
| 1154 | struct uffdio_api uffdio_api; | |||
| 1155 | void __user *buf = (void __user *)arg; | |||
| 1156 | int ret; | |||
| 1157 | ||||
| 1158 | ret = -EINVAL22; | |||
| 1159 | if (ctx->state != UFFD_STATE_WAIT_API) | |||
| 1160 | goto out; | |||
| 1161 | ret = -EFAULT14; | |||
| 1162 | if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api))) | |||
| 1163 | goto out; | |||
| 1164 | if (uffdio_api.api != UFFD_API((__u64)0xAA) || uffdio_api.features) { | |||
| 1165 | memset(&uffdio_api, 0, sizeof(uffdio_api)); | |||
| 1166 | if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) | |||
| 1167 | goto out; | |||
| 1168 | ret = -EINVAL22; | |||
| 1169 | goto out; | |||
| 1170 | } | |||
| 1171 | uffdio_api.features = UFFD_API_FEATURES(0); | |||
| 1172 | uffdio_api.ioctls = UFFD_API_IOCTLS((__u64)1 << (0x00) | (__u64)1 << (0x01) | (__u64 )1 << (0x3F)); | |||
| 1173 | ret = -EFAULT14; | |||
| 1174 | if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) | |||
| 1175 | goto out; | |||
| 1176 | ctx->state = UFFD_STATE_RUNNING; | |||
| 1177 | ret = 0; | |||
| 1178 | out: | |||
| 1179 | return ret; | |||
| 1180 | } | |||
| 1181 | ||||
| 1182 | static long userfaultfd_ioctl(struct file *file, unsigned cmd, | |||
| 1183 | unsigned long arg) | |||
| 1184 | { | |||
| 1185 | int ret = -EINVAL22; | |||
| 1186 | struct userfaultfd_ctx *ctx = file->private_data; | |||
| 1187 | ||||
| 1188 | if (cmd != UFFDIO_API(((2U|1U) << (((0 +8)+8)+14)) | (((0xAA)) << (0 + 8)) | ((((0x3F))) << 0) | (((((sizeof(struct uffdio_api ) == sizeof(struct uffdio_api[1]) && sizeof(struct uffdio_api ) < (1 << 14)) ? sizeof(struct uffdio_api) : __invalid_size_argument_for_IOC ))) << ((0 +8)+8))) && ctx->state == UFFD_STATE_WAIT_API) | |||
| 1189 | return -EINVAL22; | |||
| 1190 | ||||
| 1191 | switch(cmd) { | |||
| 1192 | case UFFDIO_API(((2U|1U) << (((0 +8)+8)+14)) | (((0xAA)) << (0 + 8)) | ((((0x3F))) << 0) | (((((sizeof(struct uffdio_api ) == sizeof(struct uffdio_api[1]) && sizeof(struct uffdio_api ) < (1 << 14)) ? sizeof(struct uffdio_api) : __invalid_size_argument_for_IOC ))) << ((0 +8)+8))): | |||
| 1193 | ret = userfaultfd_api(ctx, arg); | |||
| 1194 | break; | |||
| 1195 | case UFFDIO_REGISTER(((2U|1U) << (((0 +8)+8)+14)) | (((0xAA)) << (0 + 8)) | ((((0x00))) << 0) | (((((sizeof(struct uffdio_register ) == sizeof(struct uffdio_register[1]) && sizeof(struct uffdio_register) < (1 << 14)) ? sizeof(struct uffdio_register ) : __invalid_size_argument_for_IOC))) << ((0 +8)+8))): | |||
| 1196 | ret = userfaultfd_register(ctx, arg); | |||
| 1197 | break; | |||
| 1198 | case UFFDIO_UNREGISTER(((2U) << (((0 +8)+8)+14)) | (((0xAA)) << (0 +8)) | ((((0x01))) << 0) | (((((sizeof(struct uffdio_range) == sizeof(struct uffdio_range[1]) && sizeof(struct uffdio_range ) < (1 << 14)) ? sizeof(struct uffdio_range) : __invalid_size_argument_for_IOC ))) << ((0 +8)+8))): | |||
| 1199 | ret = userfaultfd_unregister(ctx, arg); | |||
| 1200 | break; | |||
| 1201 | case UFFDIO_WAKE(((2U) << (((0 +8)+8)+14)) | (((0xAA)) << (0 +8)) | ((((0x02))) << 0) | (((((sizeof(struct uffdio_range) == sizeof(struct uffdio_range[1]) && sizeof(struct uffdio_range ) < (1 << 14)) ? sizeof(struct uffdio_range) : __invalid_size_argument_for_IOC ))) << ((0 +8)+8))): | |||
| 1202 | ret = userfaultfd_wake(ctx, arg); | |||
| 1203 | break; | |||
| 1204 | case UFFDIO_COPY(((2U|1U) << (((0 +8)+8)+14)) | (((0xAA)) << (0 + 8)) | ((((0x03))) << 0) | (((((sizeof(struct uffdio_copy ) == sizeof(struct uffdio_copy[1]) && sizeof(struct uffdio_copy ) < (1 << 14)) ? sizeof(struct uffdio_copy) : __invalid_size_argument_for_IOC ))) << ((0 +8)+8))): | |||
| 1205 | ret = userfaultfd_copy(ctx, arg); | |||
| 1206 | break; | |||
| 1207 | case UFFDIO_ZEROPAGE(((2U|1U) << (((0 +8)+8)+14)) | (((0xAA)) << (0 + 8)) | ((((0x04))) << 0) | (((((sizeof(struct uffdio_zeropage ) == sizeof(struct uffdio_zeropage[1]) && sizeof(struct uffdio_zeropage) < (1 << 14)) ? sizeof(struct uffdio_zeropage ) : __invalid_size_argument_for_IOC))) << ((0 +8)+8))): | |||
| 1208 | ret = userfaultfd_zeropage(ctx, arg); | |||
| 1209 | break; | |||
| 1210 | } | |||
| 1211 | return ret; | |||
| 1212 | } | |||
| 1213 | ||||
| 1214 | #ifdef CONFIG_PROC_FS1 | |||
| 1215 | static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f) | |||
| 1216 | { | |||
| 1217 | struct userfaultfd_ctx *ctx = f->private_data; | |||
| 1218 | wait_queue_t *wq; | |||
| 1219 | struct userfaultfd_wait_queue *uwq; | |||
| 1220 | unsigned long pending = 0, total = 0; | |||
| 1221 | ||||
| 1222 | spin_lock(&ctx->fault_pending_wqh.lock); | |||
| 1223 | list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list)for (wq = ({ const typeof( ((typeof(*wq) *)0)->task_list ) *__mptr = ((&ctx->fault_pending_wqh.task_list)->next ); (typeof(*wq) *)( (char *)__mptr - __builtin_offsetof(typeof (*wq), task_list) );}); &wq->task_list != (&ctx-> fault_pending_wqh.task_list); wq = ({ const typeof( ((typeof( *(wq)) *)0)->task_list ) *__mptr = ((wq)->task_list.next ); (typeof(*(wq)) *)( (char *)__mptr - __builtin_offsetof(typeof (*(wq)), task_list) );})) { | |||
| 1224 | uwq = container_of(wq, struct userfaultfd_wait_queue, wq)({ const typeof( ((struct userfaultfd_wait_queue *)0)->wq ) *__mptr = (wq); (struct userfaultfd_wait_queue *)( (char *)__mptr - __builtin_offsetof(struct userfaultfd_wait_queue, wq) );}); | |||
| 1225 | pending++; | |||
| 1226 | total++; | |||
| 1227 | } | |||
| 1228 | list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list)for (wq = ({ const typeof( ((typeof(*wq) *)0)->task_list ) *__mptr = ((&ctx->fault_wqh.task_list)->next); (typeof (*wq) *)( (char *)__mptr - __builtin_offsetof(typeof(*wq), task_list ) );}); &wq->task_list != (&ctx->fault_wqh.task_list ); wq = ({ const typeof( ((typeof(*(wq)) *)0)->task_list ) *__mptr = ((wq)->task_list.next); (typeof(*(wq)) *)( (char *)__mptr - __builtin_offsetof(typeof(*(wq)), task_list) );}) ) { | |||
| 1229 | uwq = container_of(wq, struct userfaultfd_wait_queue, wq)({ const typeof( ((struct userfaultfd_wait_queue *)0)->wq ) *__mptr = (wq); (struct userfaultfd_wait_queue *)( (char *)__mptr - __builtin_offsetof(struct userfaultfd_wait_queue, wq) );}); | |||
| 1230 | total++; | |||
| 1231 | } | |||
| 1232 | spin_unlock(&ctx->fault_pending_wqh.lock); | |||
| 1233 | ||||
| 1234 | /* | |||
| 1235 | * If more protocols will be added, there will be all shown | |||
| 1236 | * separated by a space. Like this: | |||
| 1237 | * protocols: aa:... bb:... | |||
| 1238 | */ | |||
| 1239 | seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n", | |||
| 1240 | pending, total, UFFD_API((__u64)0xAA), UFFD_API_FEATURES(0), | |||
| 1241 | UFFD_API_IOCTLS((__u64)1 << (0x00) | (__u64)1 << (0x01) | (__u64 )1 << (0x3F))|UFFD_API_RANGE_IOCTLS((__u64)1 << (0x02) | (__u64)1 << (0x03) | (__u64 )1 << (0x04))); | |||
| 1242 | } | |||
| 1243 | #endif | |||
| 1244 | ||||
| 1245 | static const struct file_operations userfaultfd_fops = { | |||
| 1246 | #ifdef CONFIG_PROC_FS1 | |||
| 1247 | .show_fdinfo = userfaultfd_show_fdinfo, | |||
| 1248 | #endif | |||
| 1249 | .release = userfaultfd_release, | |||
| 1250 | .poll = userfaultfd_poll, | |||
| 1251 | .read = userfaultfd_read, | |||
| 1252 | .unlocked_ioctl = userfaultfd_ioctl, | |||
| 1253 | .compat_ioctl = userfaultfd_ioctl, | |||
| 1254 | .llseek = noop_llseek, | |||
| 1255 | }; | |||
| 1256 | ||||
| 1257 | static void init_once_userfaultfd_ctx(void *mem) | |||
| 1258 | { | |||
| 1259 | struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem; | |||
| 1260 | ||||
| 1261 | init_waitqueue_head(&ctx->fault_pending_wqh)do { static struct lock_class_key __key; __init_waitqueue_head ((&ctx->fault_pending_wqh), "&ctx->fault_pending_wqh" , &__key); } while (0); | |||
| 1262 | init_waitqueue_head(&ctx->fault_wqh)do { static struct lock_class_key __key; __init_waitqueue_head ((&ctx->fault_wqh), "&ctx->fault_wqh", &__key ); } while (0); | |||
| 1263 | init_waitqueue_head(&ctx->fd_wqh)do { static struct lock_class_key __key; __init_waitqueue_head ((&ctx->fd_wqh), "&ctx->fd_wqh", &__key); } while (0); | |||
| 1264 | seqcount_init(&ctx->refile_seq)do { static struct lock_class_key __key; __seqcount_init((& ctx->refile_seq), "&ctx->refile_seq", &__key); } while (0); | |||
| 1265 | } | |||
| 1266 | ||||
| 1267 | /** | |||
| 1268 | * userfaultfd_file_create - Creates an userfaultfd file pointer. | |||
| 1269 | * @flags: Flags for the userfaultfd file. | |||
| 1270 | * | |||
| 1271 | * This function creates an userfaultfd file pointer, w/out installing | |||
| 1272 | * it into the fd table. This is useful when the userfaultfd file is | |||
| 1273 | * used during the initialization of data structures that require | |||
| 1274 | * extra setup after the userfaultfd creation. So the userfaultfd | |||
| 1275 | * creation is split into the file pointer creation phase, and the | |||
| 1276 | * file descriptor installation phase. In this way races with | |||
| 1277 | * userspace closing the newly installed file descriptor can be | |||
| 1278 | * avoided. Returns an userfaultfd file pointer, or a proper error | |||
| 1279 | * pointer. | |||
| 1280 | */ | |||
| 1281 | static struct file *userfaultfd_file_create(int flags) | |||
| 1282 | { | |||
| 1283 | struct file *file; | |||
| 1284 | struct userfaultfd_ctx *ctx; | |||
| 1285 | ||||
| 1286 | BUG_ON(!current->mm)do { if ((!get_current()->mm)) do { asm volatile("1:\tud2\n" ".pushsection __bug_table,\"a\"\n" "2:\t.long 1b - 2b, %c0 - 2b\n" "\t.word %c1, 0\n" "\t.org 2b+%c2\n" ".popsection" : : "i" ( "fs/userfaultfd.c"), "i" (1286), "i" (sizeof(struct bug_entry ))); do { } while (1); } while (0); } while (0); | |||
| 1287 | ||||
| 1288 | /* Check the UFFD_* constants for consistency. */ | |||
| 1289 | BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC)do { bool __cond = !(!(02000000 != 02000000)); extern void __compiletime_assert_1289 (void) ; if (__cond) __compiletime_assert_1289(); do { ((void )sizeof(char[1 - 2 * __cond])); } while (0); } while (0); | |||
| 1290 | BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK)do { bool __cond = !(!(00004000 != 00004000)); extern void __compiletime_assert_1290 (void) ; if (__cond) __compiletime_assert_1290(); do { ((void )sizeof(char[1 - 2 * __cond])); } while (0); } while (0); | |||
| 1291 | ||||
| 1292 | file = ERR_PTR(-EINVAL22); | |||
| 1293 | if (flags & ~UFFD_SHARED_FCNTL_FLAGS(02000000 | 00004000)) | |||
| 1294 | goto out; | |||
| 1295 | ||||
| 1296 | file = ERR_PTR(-ENOMEM12); | |||
| 1297 | ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL((( gfp_t)(0x400000u|0x2000000u)) | (( gfp_t)0x40u) | (( gfp_t )0x80u))); | |||
| 1298 | if (!ctx) | |||
| 1299 | goto out; | |||
| 1300 | ||||
| 1301 | atomic_set(&ctx->refcount, 1); | |||
| 1302 | ctx->flags = flags; | |||
| 1303 | ctx->state = UFFD_STATE_WAIT_API; | |||
| 1304 | ctx->released = false; | |||
| 1305 | ctx->mm = currentget_current()->mm; | |||
| 1306 | /* prevent the mm struct to be freed */ | |||
| 1307 | atomic_inc(&ctx->mm->mm_count); | |||
| 1308 | ||||
| 1309 | file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx, | |||
| 1310 | O_RDWR00000002 | (flags & UFFD_SHARED_FCNTL_FLAGS(02000000 | 00004000))); | |||
| 1311 | if (IS_ERR(file)) { | |||
| 1312 | mmdrop(ctx->mm); | |||
| 1313 | kmem_cache_free(userfaultfd_ctx_cachep, ctx); | |||
| 1314 | } | |||
| 1315 | out: | |||
| 1316 | return file; | |||
| 1317 | } | |||
| 1318 | ||||
| 1319 | SYSCALL_DEFINE1(userfaultfd, int, flags)static const char *types__userfaultfd[] = { "int" }; static const char *args__userfaultfd[] = { "flags" }; static struct syscall_metadata __syscall_meta__userfaultfd; static struct trace_event_call __attribute__ ((__used__)) event_enter__userfaultfd = { .class = &event_class_syscall_enter , { .name = "sys_enter""_userfaultfd", }, .event.funcs = & enter_syscall_print_funcs, .data = (void *)&__syscall_meta__userfaultfd , .flags = TRACE_EVENT_FL_CAP_ANY, }; static struct trace_event_call __attribute__((__used__)) __attribute__((section("_ftrace_events" ))) *__event_enter__userfaultfd = &event_enter__userfaultfd ;; static struct syscall_metadata __syscall_meta__userfaultfd ; static struct trace_event_call __attribute__((__used__)) event_exit__userfaultfd = { .class = &event_class_syscall_exit, { .name = "sys_exit" "_userfaultfd", }, .event.funcs = &exit_syscall_print_funcs , .data = (void *)&__syscall_meta__userfaultfd, .flags = TRACE_EVENT_FL_CAP_ANY , }; static struct trace_event_call __attribute__((__used__)) __attribute__((section("_ftrace_events"))) *__event_exit__userfaultfd = &event_exit__userfaultfd;; static struct syscall_metadata __attribute__((__used__)) __syscall_meta__userfaultfd = { .name = "sys""_userfaultfd", .syscall_nr = -1, .nb_args = 1, .types = 1 ? types__userfaultfd : ((void *)0), .args = 1 ? args__userfaultfd : ((void *)0), .enter_event = &event_enter__userfaultfd, .exit_event = &event_exit__userfaultfd, .enter_fields = { &(__syscall_meta__userfaultfd.enter_fields), &(__syscall_meta__userfaultfd .enter_fields) }, }; static struct syscall_metadata __attribute__ ((__used__)) __attribute__((section("__syscalls_metadata"))) * __p_syscall_meta__userfaultfd = &__syscall_meta__userfaultfd ; long sys_userfaultfd(int flags) __attribute__((alias("SyS_userfaultfd" ))); static inline __attribute__((no_instrument_function)) long SYSC_userfaultfd(int flags); long SyS_userfaultfd(__typeof(__builtin_choose_expr ((__builtin_types_compatible_p(typeof((int)0), typeof(0LL)) || __builtin_types_compatible_p(typeof((int)0), typeof(0ULL))), 0LL, 0L)) flags); long SyS_userfaultfd(__typeof(__builtin_choose_expr ((__builtin_types_compatible_p(typeof((int)0), typeof(0LL)) || __builtin_types_compatible_p(typeof((int)0), typeof(0ULL))), 0LL, 0L)) flags) { long ret = SYSC_userfaultfd((int) flags); (void)(sizeof(struct { int:-!!(!(__builtin_types_compatible_p (typeof((int)0), typeof(0LL)) || __builtin_types_compatible_p (typeof((int)0), typeof(0ULL))) && sizeof(int) > sizeof (long)); })); do { } while (0); return ret; } static inline __attribute__ ((no_instrument_function)) long SYSC_userfaultfd(int flags) | |||
| 1320 | { | |||
| 1321 | int fd, error; | |||
| 1322 | struct file *file; | |||
| 1323 | ||||
| 1324 | error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS(02000000 | 00004000)); | |||
| 1325 | if (error < 0) | |||
| 1326 | return error; | |||
| 1327 | fd = error; | |||
| 1328 | ||||
| 1329 | file = userfaultfd_file_create(flags); | |||
| 1330 | if (IS_ERR(file)) { | |||
| 1331 | error = PTR_ERR(file); | |||
| 1332 | goto err_put_unused_fd; | |||
| 1333 | } | |||
| 1334 | fd_install(fd, file); | |||
| 1335 | ||||
| 1336 | return fd; | |||
| 1337 | ||||
| 1338 | err_put_unused_fd: | |||
| 1339 | put_unused_fd(fd); | |||
| 1340 | ||||
| 1341 | return error; | |||
| 1342 | } | |||
| 1343 | ||||
| 1344 | static int __init__attribute__ ((__section__(".init.text"))) __attribute__((no_instrument_function )) userfaultfd_init(void) | |||
| 1345 | { | |||
| 1346 | userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache", | |||
| 1347 | sizeof(struct userfaultfd_ctx), | |||
| 1348 | 0, | |||
| 1349 | SLAB_HWCACHE_ALIGN0x00002000UL|SLAB_PANIC0x00040000UL, | |||
| 1350 | init_once_userfaultfd_ctx); | |||
| 1351 | return 0; | |||
| 1352 | } | |||
| 1353 | __initcall(userfaultfd_init)static initcall_t __initcall_userfaultfd_init6 __attribute__( (__used__)) __attribute__((__section__(".initcall" "6" ".init" ))) = userfaultfd_init;; |