1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * fs/userfaultfd.c 3 * fs/userfaultfd.c 4 * 4 * 5 * Copyright (C) 2007 Davide Libenzi <davide 5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> 6 * Copyright (C) 2008-2009 Red Hat, Inc. 6 * Copyright (C) 2008-2009 Red Hat, Inc. 7 * Copyright (C) 2015 Red Hat, Inc. 7 * Copyright (C) 2015 Red Hat, Inc. 8 * 8 * 9 * Some part derived from fs/eventfd.c (anon 9 * Some part derived from fs/eventfd.c (anon inode setup) and 10 * mm/ksm.c (mm hashing). 10 * mm/ksm.c (mm hashing). 11 */ 11 */ 12 12 13 #include <linux/list.h> 13 #include <linux/list.h> 14 #include <linux/hashtable.h> 14 #include <linux/hashtable.h> 15 #include <linux/sched/signal.h> 15 #include <linux/sched/signal.h> 16 #include <linux/sched/mm.h> 16 #include <linux/sched/mm.h> 17 #include <linux/mm.h> 17 #include <linux/mm.h> 18 #include <linux/mm_inline.h> << 19 #include <linux/mmu_notifier.h> << 20 #include <linux/poll.h> 18 #include <linux/poll.h> 21 #include <linux/slab.h> 19 #include <linux/slab.h> 22 #include <linux/seq_file.h> 20 #include <linux/seq_file.h> 23 #include <linux/file.h> 21 #include <linux/file.h> 24 #include <linux/bug.h> 22 #include <linux/bug.h> 25 #include <linux/anon_inodes.h> 23 #include <linux/anon_inodes.h> 26 #include <linux/syscalls.h> 24 #include <linux/syscalls.h> 27 #include <linux/userfaultfd_k.h> 25 #include <linux/userfaultfd_k.h> 28 #include <linux/mempolicy.h> 26 #include <linux/mempolicy.h> 29 #include <linux/ioctl.h> 27 #include <linux/ioctl.h> 30 #include <linux/security.h> 28 #include <linux/security.h> 31 #include <linux/hugetlb.h> 29 #include <linux/hugetlb.h> 32 #include <linux/swapops.h> !! 30 33 #include <linux/miscdevice.h> !! 31 int sysctl_unprivileged_userfaultfd __read_mostly = 1; 34 #include <linux/uio.h> !! 32 35 !! 33 static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly; 36 static int sysctl_unprivileged_userfaultfd __r !! 34 37 !! 35 enum userfaultfd_state { 38 #ifdef CONFIG_SYSCTL !! 36 UFFD_STATE_WAIT_API, 39 static struct ctl_table vm_userfaultfd_table[] !! 37 UFFD_STATE_RUNNING, 40 { << 41 .procname = "unprivilege << 42 .data = &sysctl_unpr << 43 .maxlen = sizeof(sysct << 44 .mode = 0644, << 45 .proc_handler = proc_dointve << 46 .extra1 = SYSCTL_ZERO, << 47 .extra2 = SYSCTL_ONE, << 48 }, << 49 }; 38 }; 50 #endif << 51 39 52 static struct kmem_cache *userfaultfd_ctx_cach !! 40 /* >> 41 * Start with fault_pending_wqh and fault_wqh so they're more likely >> 42 * to be in the same cacheline. >> 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. >> 53 */ >> 54 struct userfaultfd_ctx { >> 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 */ >> 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; >> 61 /* waitqueue head for events */ >> 62 wait_queue_head_t event_wqh; >> 63 /* a refile sequence protected by fault_pending_wqh lock */ >> 64 struct seqcount refile_seq; >> 65 /* pseudo fd refcounting */ >> 66 refcount_t refcount; >> 67 /* userfaultfd syscall flags */ >> 68 unsigned int flags; >> 69 /* features requested from the userspace */ >> 70 unsigned int features; >> 71 /* state machine */ >> 72 enum userfaultfd_state state; >> 73 /* released */ >> 74 bool released; >> 75 /* memory mappings are changing because of non-cooperative event */ >> 76 bool mmap_changing; >> 77 /* mm with one ore more vmas attached to this userfaultfd_ctx */ >> 78 struct mm_struct *mm; >> 79 }; 53 80 54 struct userfaultfd_fork_ctx { 81 struct userfaultfd_fork_ctx { 55 struct userfaultfd_ctx *orig; 82 struct userfaultfd_ctx *orig; 56 struct userfaultfd_ctx *new; 83 struct userfaultfd_ctx *new; 57 struct list_head list; 84 struct list_head list; 58 }; 85 }; 59 86 60 struct userfaultfd_unmap_ctx { 87 struct userfaultfd_unmap_ctx { 61 struct userfaultfd_ctx *ctx; 88 struct userfaultfd_ctx *ctx; 62 unsigned long start; 89 unsigned long start; 63 unsigned long end; 90 unsigned long end; 64 struct list_head list; 91 struct list_head list; 65 }; 92 }; 66 93 67 struct userfaultfd_wait_queue { 94 struct userfaultfd_wait_queue { 68 struct uffd_msg msg; 95 struct uffd_msg msg; 69 wait_queue_entry_t wq; 96 wait_queue_entry_t wq; 70 struct userfaultfd_ctx *ctx; 97 struct userfaultfd_ctx *ctx; 71 bool waken; 98 bool waken; 72 }; 99 }; 73 100 74 struct userfaultfd_wake_range { 101 struct userfaultfd_wake_range { 75 unsigned long start; 102 unsigned long start; 76 unsigned long len; 103 unsigned long len; 77 }; 104 }; 78 105 79 /* internal indication that UFFD_API ioctl was << 80 #define UFFD_FEATURE_INITIALIZED << 81 << 82 static bool userfaultfd_is_initialized(struct << 83 { << 84 return ctx->features & UFFD_FEATURE_IN << 85 } << 86 << 87 static bool userfaultfd_wp_async_ctx(struct us << 88 { << 89 return ctx && (ctx->features & UFFD_FE << 90 } << 91 << 92 /* << 93 * Whether WP_UNPOPULATED is enabled on the uf << 94 * meaningful when userfaultfd_wp()==true on t << 95 * anonymous. << 96 */ << 97 bool userfaultfd_wp_unpopulated(struct vm_area << 98 { << 99 struct userfaultfd_ctx *ctx = vma->vm_ << 100 << 101 if (!ctx) << 102 return false; << 103 << 104 return ctx->features & UFFD_FEATURE_WP << 105 } << 106 << 107 static void userfaultfd_set_vm_flags(struct vm << 108 vm_flags_ << 109 { << 110 const bool uffd_wp_changed = (vma->vm_ << 111 << 112 vm_flags_reset(vma, flags); << 113 /* << 114 * For shared mappings, we want to ena << 115 * userfaultfd-wp is enabled (see vma_ << 116 * recalculate vma->vm_page_prot whene << 117 */ << 118 if ((vma->vm_flags & VM_SHARED) && uff << 119 vma_set_page_prot(vma); << 120 } << 121 << 122 static int userfaultfd_wake_function(wait_queu 106 static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode, 123 int wake_ 107 int wake_flags, void *key) 124 { 108 { 125 struct userfaultfd_wake_range *range = 109 struct userfaultfd_wake_range *range = key; 126 int ret; 110 int ret; 127 struct userfaultfd_wait_queue *uwq; 111 struct userfaultfd_wait_queue *uwq; 128 unsigned long start, len; 112 unsigned long start, len; 129 113 130 uwq = container_of(wq, struct userfaul 114 uwq = container_of(wq, struct userfaultfd_wait_queue, wq); 131 ret = 0; 115 ret = 0; 132 /* len == 0 means wake all */ 116 /* len == 0 means wake all */ 133 start = range->start; 117 start = range->start; 134 len = range->len; 118 len = range->len; 135 if (len && (start > uwq->msg.arg.pagef 119 if (len && (start > uwq->msg.arg.pagefault.address || 136 start + len <= uwq->msg.ar 120 start + len <= uwq->msg.arg.pagefault.address)) 137 goto out; 121 goto out; 138 WRITE_ONCE(uwq->waken, true); 122 WRITE_ONCE(uwq->waken, true); 139 /* 123 /* 140 * The Program-Order guarantees provid 124 * The Program-Order guarantees provided by the scheduler 141 * ensure uwq->waken is visible before 125 * ensure uwq->waken is visible before the task is woken. 142 */ 126 */ 143 ret = wake_up_state(wq->private, mode) 127 ret = wake_up_state(wq->private, mode); 144 if (ret) { 128 if (ret) { 145 /* 129 /* 146 * Wake only once, autoremove 130 * Wake only once, autoremove behavior. 147 * 131 * 148 * After the effect of list_de 132 * After the effect of list_del_init is visible to the other 149 * CPUs, the waitqueue may dis 133 * CPUs, the waitqueue may disappear from under us, see the 150 * !list_empty_careful() in ha 134 * !list_empty_careful() in handle_userfault(). 151 * 135 * 152 * try_to_wake_up() has an imp 136 * try_to_wake_up() has an implicit smp_mb(), and the 153 * wq->private is read before 137 * wq->private is read before calling the extern function 154 * "wake_up_state" (which in t 138 * "wake_up_state" (which in turns calls try_to_wake_up). 155 */ 139 */ 156 list_del_init(&wq->entry); 140 list_del_init(&wq->entry); 157 } 141 } 158 out: 142 out: 159 return ret; 143 return ret; 160 } 144 } 161 145 162 /** 146 /** 163 * userfaultfd_ctx_get - Acquires a reference 147 * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd 164 * context. 148 * context. 165 * @ctx: [in] Pointer to the userfaultfd conte 149 * @ctx: [in] Pointer to the userfaultfd context. 166 */ 150 */ 167 static void userfaultfd_ctx_get(struct userfau 151 static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx) 168 { 152 { 169 refcount_inc(&ctx->refcount); 153 refcount_inc(&ctx->refcount); 170 } 154 } 171 155 172 /** 156 /** 173 * userfaultfd_ctx_put - Releases a reference 157 * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd 174 * context. 158 * context. 175 * @ctx: [in] Pointer to userfaultfd context. 159 * @ctx: [in] Pointer to userfaultfd context. 176 * 160 * 177 * The userfaultfd context reference must have 161 * The userfaultfd context reference must have been previously acquired either 178 * with userfaultfd_ctx_get() or userfaultfd_c 162 * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget(). 179 */ 163 */ 180 static void userfaultfd_ctx_put(struct userfau 164 static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx) 181 { 165 { 182 if (refcount_dec_and_test(&ctx->refcou 166 if (refcount_dec_and_test(&ctx->refcount)) { 183 VM_BUG_ON(spin_is_locked(&ctx- 167 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock)); 184 VM_BUG_ON(waitqueue_active(&ct 168 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh)); 185 VM_BUG_ON(spin_is_locked(&ctx- 169 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock)); 186 VM_BUG_ON(waitqueue_active(&ct 170 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh)); 187 VM_BUG_ON(spin_is_locked(&ctx- 171 VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock)); 188 VM_BUG_ON(waitqueue_active(&ct 172 VM_BUG_ON(waitqueue_active(&ctx->event_wqh)); 189 VM_BUG_ON(spin_is_locked(&ctx- 173 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock)); 190 VM_BUG_ON(waitqueue_active(&ct 174 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh)); 191 mmdrop(ctx->mm); 175 mmdrop(ctx->mm); 192 kmem_cache_free(userfaultfd_ct 176 kmem_cache_free(userfaultfd_ctx_cachep, ctx); 193 } 177 } 194 } 178 } 195 179 196 static inline void msg_init(struct uffd_msg *m 180 static inline void msg_init(struct uffd_msg *msg) 197 { 181 { 198 BUILD_BUG_ON(sizeof(struct uffd_msg) ! 182 BUILD_BUG_ON(sizeof(struct uffd_msg) != 32); 199 /* 183 /* 200 * Must use memset to zero out the pad 184 * Must use memset to zero out the paddings or kernel data is 201 * leaked to userland. 185 * leaked to userland. 202 */ 186 */ 203 memset(msg, 0, sizeof(struct uffd_msg) 187 memset(msg, 0, sizeof(struct uffd_msg)); 204 } 188 } 205 189 206 static inline struct uffd_msg userfault_msg(un 190 static inline struct uffd_msg userfault_msg(unsigned long address, 207 un << 208 un 191 unsigned int flags, 209 un 192 unsigned long reason, 210 un 193 unsigned int features) 211 { 194 { 212 struct uffd_msg msg; 195 struct uffd_msg msg; 213 << 214 msg_init(&msg); 196 msg_init(&msg); 215 msg.event = UFFD_EVENT_PAGEFAULT; 197 msg.event = UFFD_EVENT_PAGEFAULT; 216 !! 198 msg.arg.pagefault.address = address; 217 msg.arg.pagefault.address = (features << 218 real_addre << 219 << 220 /* << 221 * These flags indicate why the userfa << 222 * - UFFD_PAGEFAULT_FLAG_WP indicates << 223 * - UFFD_PAGEFAULT_FLAG_MINOR indicat << 224 * - Neither of these flags being set << 225 * << 226 * Separately, UFFD_PAGEFAULT_FLAG_WRI << 227 * fault. Otherwise, it was a read fau << 228 */ << 229 if (flags & FAULT_FLAG_WRITE) 199 if (flags & FAULT_FLAG_WRITE) >> 200 /* >> 201 * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the >> 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. >> 206 */ 230 msg.arg.pagefault.flags |= UFF 207 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE; 231 if (reason & VM_UFFD_WP) 208 if (reason & VM_UFFD_WP) >> 209 /* >> 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. >> 215 */ 232 msg.arg.pagefault.flags |= UFF 216 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP; 233 if (reason & VM_UFFD_MINOR) << 234 msg.arg.pagefault.flags |= UFF << 235 if (features & UFFD_FEATURE_THREAD_ID) 217 if (features & UFFD_FEATURE_THREAD_ID) 236 msg.arg.pagefault.feat.ptid = 218 msg.arg.pagefault.feat.ptid = task_pid_vnr(current); 237 return msg; 219 return msg; 238 } 220 } 239 221 240 #ifdef CONFIG_HUGETLB_PAGE 222 #ifdef CONFIG_HUGETLB_PAGE 241 /* 223 /* 242 * Same functionality as userfaultfd_must_wait 224 * Same functionality as userfaultfd_must_wait below with modifications for 243 * hugepmd ranges. 225 * hugepmd ranges. 244 */ 226 */ 245 static inline bool userfaultfd_huge_must_wait( 227 static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, 246 !! 228 struct vm_area_struct *vma, 247 !! 229 unsigned long address, >> 230 unsigned long flags, >> 231 unsigned long reason) 248 { 232 { 249 struct vm_area_struct *vma = vmf->vma; !! 233 struct mm_struct *mm = ctx->mm; 250 pte_t *ptep, pte; 234 pte_t *ptep, pte; 251 bool ret = true; 235 bool ret = true; 252 236 253 assert_fault_locked(vmf); !! 237 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem)); >> 238 >> 239 ptep = huge_pte_offset(mm, address, vma_mmu_pagesize(vma)); 254 240 255 ptep = hugetlb_walk(vma, vmf->address, << 256 if (!ptep) 241 if (!ptep) 257 goto out; 242 goto out; 258 243 259 ret = false; 244 ret = false; 260 pte = huge_ptep_get(vma->vm_mm, vmf->a !! 245 pte = huge_ptep_get(ptep); 261 246 262 /* 247 /* 263 * Lockless access: we're in a wait_ev 248 * Lockless access: we're in a wait_event so it's ok if it 264 * changes under us. PTE markers shou !! 249 * changes under us. 265 * ptes here. << 266 */ 250 */ 267 if (huge_pte_none_mostly(pte)) !! 251 if (huge_pte_none(pte)) 268 ret = true; 252 ret = true; 269 if (!huge_pte_write(pte) && (reason & 253 if (!huge_pte_write(pte) && (reason & VM_UFFD_WP)) 270 ret = true; 254 ret = true; 271 out: 255 out: 272 return ret; 256 return ret; 273 } 257 } 274 #else 258 #else 275 static inline bool userfaultfd_huge_must_wait( 259 static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, 276 !! 260 struct vm_area_struct *vma, 277 !! 261 unsigned long address, >> 262 unsigned long flags, >> 263 unsigned long reason) 278 { 264 { 279 return false; /* should never get he 265 return false; /* should never get here */ 280 } 266 } 281 #endif /* CONFIG_HUGETLB_PAGE */ 267 #endif /* CONFIG_HUGETLB_PAGE */ 282 268 283 /* 269 /* 284 * Verify the pagetables are still not ok afte 270 * Verify the pagetables are still not ok after having reigstered into 285 * the fault_pending_wqh to avoid userland hav 271 * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any 286 * userfault that has already been resolved, i !! 272 * userfault that has already been resolved, if userfaultfd_read and 287 * UFFDIO_COPY|ZEROPAGE are being run simultan 273 * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different 288 * threads. 274 * threads. 289 */ 275 */ 290 static inline bool userfaultfd_must_wait(struc 276 static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, 291 struc !! 277 unsigned long address, >> 278 unsigned long flags, 292 unsig 279 unsigned long reason) 293 { 280 { 294 struct mm_struct *mm = ctx->mm; 281 struct mm_struct *mm = ctx->mm; 295 unsigned long address = vmf->address; << 296 pgd_t *pgd; 282 pgd_t *pgd; 297 p4d_t *p4d; 283 p4d_t *p4d; 298 pud_t *pud; 284 pud_t *pud; 299 pmd_t *pmd, _pmd; 285 pmd_t *pmd, _pmd; 300 pte_t *pte; 286 pte_t *pte; 301 pte_t ptent; << 302 bool ret = true; 287 bool ret = true; 303 288 304 assert_fault_locked(vmf); !! 289 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem)); 305 290 306 pgd = pgd_offset(mm, address); 291 pgd = pgd_offset(mm, address); 307 if (!pgd_present(*pgd)) 292 if (!pgd_present(*pgd)) 308 goto out; 293 goto out; 309 p4d = p4d_offset(pgd, address); 294 p4d = p4d_offset(pgd, address); 310 if (!p4d_present(*p4d)) 295 if (!p4d_present(*p4d)) 311 goto out; 296 goto out; 312 pud = pud_offset(p4d, address); 297 pud = pud_offset(p4d, address); 313 if (!pud_present(*pud)) 298 if (!pud_present(*pud)) 314 goto out; 299 goto out; 315 pmd = pmd_offset(pud, address); 300 pmd = pmd_offset(pud, address); 316 again: !! 301 /* 317 _pmd = pmdp_get_lockless(pmd); !! 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); 318 if (pmd_none(_pmd)) 310 if (pmd_none(_pmd)) 319 goto out; 311 goto out; 320 312 321 ret = false; 313 ret = false; 322 if (!pmd_present(_pmd) || pmd_devmap(_ !! 314 if (!pmd_present(_pmd)) 323 goto out; 315 goto out; 324 316 325 if (pmd_trans_huge(_pmd)) { !! 317 if (pmd_trans_huge(_pmd)) 326 if (!pmd_write(_pmd) && (reaso << 327 ret = true; << 328 goto out; 318 goto out; 329 } << 330 319 >> 320 /* >> 321 * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it >> 322 * and use the standard pte_offset_map() instead of parsing _pmd. >> 323 */ 331 pte = pte_offset_map(pmd, address); 324 pte = pte_offset_map(pmd, address); 332 if (!pte) { << 333 ret = true; << 334 goto again; << 335 } << 336 /* 325 /* 337 * Lockless access: we're in a wait_ev 326 * Lockless access: we're in a wait_event so it's ok if it 338 * changes under us. PTE markers shou !! 327 * changes under us. 339 * ptes here. << 340 */ 328 */ 341 ptent = ptep_get(pte); !! 329 if (pte_none(*pte)) 342 if (pte_none_mostly(ptent)) << 343 ret = true; << 344 if (!pte_write(ptent) && (reason & VM_ << 345 ret = true; 330 ret = true; 346 pte_unmap(pte); 331 pte_unmap(pte); 347 332 348 out: 333 out: 349 return ret; 334 return ret; 350 } 335 } 351 336 352 static inline unsigned int userfaultfd_get_blo << 353 { << 354 if (flags & FAULT_FLAG_INTERRUPTIBLE) << 355 return TASK_INTERRUPTIBLE; << 356 << 357 if (flags & FAULT_FLAG_KILLABLE) << 358 return TASK_KILLABLE; << 359 << 360 return TASK_UNINTERRUPTIBLE; << 361 } << 362 << 363 /* 337 /* 364 * The locking rules involved in returning VM_ 338 * The locking rules involved in returning VM_FAULT_RETRY depending on 365 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NO 339 * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and 366 * FAULT_FLAG_KILLABLE are not straightforward 340 * FAULT_FLAG_KILLABLE are not straightforward. The "Caution" 367 * recommendation in __lock_page_or_retry is n 341 * recommendation in __lock_page_or_retry is not an understatement. 368 * 342 * 369 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_ !! 343 * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released 370 * before returning VM_FAULT_RETRY only if FAU 344 * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is 371 * not set. 345 * not set. 372 * 346 * 373 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_ 347 * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not 374 * set, VM_FAULT_RETRY can still be returned i 348 * set, VM_FAULT_RETRY can still be returned if and only if there are 375 * fatal_signal_pending()s, and the mmap_lock !! 349 * fatal_signal_pending()s, and the mmap_sem must be released before 376 * returning it. 350 * returning it. 377 */ 351 */ 378 vm_fault_t handle_userfault(struct vm_fault *v 352 vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) 379 { 353 { 380 struct vm_area_struct *vma = vmf->vma; !! 354 struct mm_struct *mm = vmf->vma->vm_mm; 381 struct mm_struct *mm = vma->vm_mm; << 382 struct userfaultfd_ctx *ctx; 355 struct userfaultfd_ctx *ctx; 383 struct userfaultfd_wait_queue uwq; 356 struct userfaultfd_wait_queue uwq; 384 vm_fault_t ret = VM_FAULT_SIGBUS; 357 vm_fault_t ret = VM_FAULT_SIGBUS; 385 bool must_wait; !! 358 bool must_wait, return_to_userland; 386 unsigned int blocking_state; !! 359 long blocking_state; 387 360 388 /* 361 /* 389 * We don't do userfault handling for 362 * We don't do userfault handling for the final child pid update. 390 * 363 * 391 * We also don't do userfault handling 364 * We also don't do userfault handling during 392 * coredumping. hugetlbfs has the spec 365 * coredumping. hugetlbfs has the special 393 * hugetlb_follow_page_mask() to skip !! 366 * follow_hugetlb_page() to skip missing pages in the 394 * FOLL_DUMP case, anon memory also ch 367 * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with 395 * the no_page_table() helper in follo 368 * the no_page_table() helper in follow_page_mask(), but the 396 * shmem_vm_ops->fault method is invok 369 * shmem_vm_ops->fault method is invoked even during 397 * coredumping and it ends up here. !! 370 * coredumping without mmap_sem and it ends up here. 398 */ 371 */ 399 if (current->flags & (PF_EXITING|PF_DU 372 if (current->flags & (PF_EXITING|PF_DUMPCORE)) 400 goto out; 373 goto out; 401 374 402 assert_fault_locked(vmf); !! 375 /* >> 376 * Coredumping runs without mmap_sem so we can only check that >> 377 * the mmap_sem is held, if PF_DUMPCORE was not set. >> 378 */ >> 379 WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem)); 403 380 404 ctx = vma->vm_userfaultfd_ctx.ctx; !! 381 ctx = vmf->vma->vm_userfaultfd_ctx.ctx; 405 if (!ctx) 382 if (!ctx) 406 goto out; 383 goto out; 407 384 408 BUG_ON(ctx->mm != mm); 385 BUG_ON(ctx->mm != mm); 409 386 410 /* Any unrecognized flag is a bug. */ !! 387 VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP)); 411 VM_BUG_ON(reason & ~__VM_UFFD_FLAGS); !! 388 VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP)); 412 /* 0 or > 1 flags set is a bug; we exp << 413 VM_BUG_ON(!reason || (reason & (reason << 414 389 415 if (ctx->features & UFFD_FEATURE_SIGBU 390 if (ctx->features & UFFD_FEATURE_SIGBUS) 416 goto out; 391 goto out; 417 if (!(vmf->flags & FAULT_FLAG_USER) && << 418 goto out; << 419 392 420 /* 393 /* 421 * If it's already released don't get 394 * If it's already released don't get it. This avoids to loop 422 * in __get_user_pages if userfaultfd_ 395 * in __get_user_pages if userfaultfd_release waits on the 423 * caller of handle_userfault to relea !! 396 * caller of handle_userfault to release the mmap_sem. 424 */ 397 */ 425 if (unlikely(READ_ONCE(ctx->released)) 398 if (unlikely(READ_ONCE(ctx->released))) { 426 /* 399 /* 427 * Don't return VM_FAULT_SIGBU 400 * Don't return VM_FAULT_SIGBUS in this case, so a non 428 * cooperative manager can clo 401 * cooperative manager can close the uffd after the 429 * last UFFDIO_COPY, without r 402 * last UFFDIO_COPY, without risking to trigger an 430 * involuntary SIGBUS if the p 403 * involuntary SIGBUS if the process was starting the 431 * userfaultfd while the userf 404 * userfaultfd while the userfaultfd was still armed 432 * (but after the last UFFDIO_ 405 * (but after the last UFFDIO_COPY). If the uffd 433 * wasn't already closed when 406 * wasn't already closed when the userfault reached 434 * this point, that would norm 407 * this point, that would normally be solved by 435 * userfaultfd_must_wait retur 408 * userfaultfd_must_wait returning 'false'. 436 * 409 * 437 * If we were to return VM_FAU 410 * If we were to return VM_FAULT_SIGBUS here, the non 438 * cooperative manager would b 411 * cooperative manager would be instead forced to 439 * always call UFFDIO_UNREGIST 412 * always call UFFDIO_UNREGISTER before it can safely 440 * close the uffd. 413 * close the uffd. 441 */ 414 */ 442 ret = VM_FAULT_NOPAGE; 415 ret = VM_FAULT_NOPAGE; 443 goto out; 416 goto out; 444 } 417 } 445 418 446 /* 419 /* 447 * Check that we can return VM_FAULT_R 420 * Check that we can return VM_FAULT_RETRY. 448 * 421 * 449 * NOTE: it should become possible to 422 * NOTE: it should become possible to return VM_FAULT_RETRY 450 * even if FAULT_FLAG_TRIED is set wit 423 * even if FAULT_FLAG_TRIED is set without leading to gup() 451 * -EBUSY failures, if the userfaultfd 424 * -EBUSY failures, if the userfaultfd is to be extended for 452 * VM_UFFD_WP tracking and we intend t 425 * VM_UFFD_WP tracking and we intend to arm the userfault 453 * without first stopping userland acc 426 * without first stopping userland access to the memory. For 454 * VM_UFFD_MISSING userfaults this is 427 * VM_UFFD_MISSING userfaults this is enough for now. 455 */ 428 */ 456 if (unlikely(!(vmf->flags & FAULT_FLAG 429 if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) { 457 /* 430 /* 458 * Validate the invariant that 431 * Validate the invariant that nowait must allow retry 459 * to be sure not to return SI 432 * to be sure not to return SIGBUS erroneously on 460 * nowait invocations. 433 * nowait invocations. 461 */ 434 */ 462 BUG_ON(vmf->flags & FAULT_FLAG 435 BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT); 463 #ifdef CONFIG_DEBUG_VM 436 #ifdef CONFIG_DEBUG_VM 464 if (printk_ratelimit()) { 437 if (printk_ratelimit()) { 465 printk(KERN_WARNING 438 printk(KERN_WARNING 466 "FAULT_FLAG_ALL 439 "FAULT_FLAG_ALLOW_RETRY missing %x\n", 467 vmf->flags); 440 vmf->flags); 468 dump_stack(); 441 dump_stack(); 469 } 442 } 470 #endif 443 #endif 471 goto out; 444 goto out; 472 } 445 } 473 446 474 /* 447 /* 475 * Handle nowait, not much to do other 448 * Handle nowait, not much to do other than tell it to retry 476 * and wait. 449 * and wait. 477 */ 450 */ 478 ret = VM_FAULT_RETRY; 451 ret = VM_FAULT_RETRY; 479 if (vmf->flags & FAULT_FLAG_RETRY_NOWA 452 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) 480 goto out; 453 goto out; 481 454 482 /* take the reference before dropping !! 455 /* take the reference before dropping the mmap_sem */ 483 userfaultfd_ctx_get(ctx); 456 userfaultfd_ctx_get(ctx); 484 457 485 init_waitqueue_func_entry(&uwq.wq, use 458 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function); 486 uwq.wq.private = current; 459 uwq.wq.private = current; 487 uwq.msg = userfault_msg(vmf->address, !! 460 uwq.msg = userfault_msg(vmf->address, vmf->flags, reason, 488 reason, ctx->f !! 461 ctx->features); 489 uwq.ctx = ctx; 462 uwq.ctx = ctx; 490 uwq.waken = false; 463 uwq.waken = false; 491 464 492 blocking_state = userfaultfd_get_block !! 465 return_to_userland = 493 !! 466 (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) == 494 /* !! 467 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE); 495 * Take the vma lock now, in order to !! 468 blocking_state = return_to_userland ? TASK_INTERRUPTIBLE : 496 * userfaultfd_huge_must_wait() later. !! 469 TASK_KILLABLE; 497 * (sleepable) vma lock can modify the << 498 * must be before explicitly calling s << 499 */ << 500 if (is_vm_hugetlb_page(vma)) << 501 hugetlb_vma_lock_read(vma); << 502 470 503 spin_lock_irq(&ctx->fault_pending_wqh. 471 spin_lock_irq(&ctx->fault_pending_wqh.lock); 504 /* 472 /* 505 * After the __add_wait_queue the uwq 473 * After the __add_wait_queue the uwq is visible to userland 506 * through poll/read(). 474 * through poll/read(). 507 */ 475 */ 508 __add_wait_queue(&ctx->fault_pending_w 476 __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq); 509 /* 477 /* 510 * The smp_mb() after __set_current_st 478 * The smp_mb() after __set_current_state prevents the reads 511 * following the spin_unlock to happen 479 * following the spin_unlock to happen before the list_add in 512 * __add_wait_queue. 480 * __add_wait_queue. 513 */ 481 */ 514 set_current_state(blocking_state); 482 set_current_state(blocking_state); 515 spin_unlock_irq(&ctx->fault_pending_wq 483 spin_unlock_irq(&ctx->fault_pending_wqh.lock); 516 484 517 if (!is_vm_hugetlb_page(vma)) !! 485 if (!is_vm_hugetlb_page(vmf->vma)) 518 must_wait = userfaultfd_must_w !! 486 must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags, >> 487 reason); 519 else 488 else 520 must_wait = userfaultfd_huge_m !! 489 must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma, 521 if (is_vm_hugetlb_page(vma)) !! 490 vmf->address, 522 hugetlb_vma_unlock_read(vma); !! 491 vmf->flags, reason); 523 release_fault_lock(vmf); !! 492 up_read(&mm->mmap_sem); 524 !! 493 525 if (likely(must_wait && !READ_ONCE(ctx !! 494 if (likely(must_wait && !READ_ONCE(ctx->released) && >> 495 (return_to_userland ? !signal_pending(current) : >> 496 !fatal_signal_pending(current)))) { 526 wake_up_poll(&ctx->fd_wqh, EPO 497 wake_up_poll(&ctx->fd_wqh, EPOLLIN); 527 schedule(); 498 schedule(); >> 499 ret |= VM_FAULT_MAJOR; >> 500 >> 501 /* >> 502 * False wakeups can orginate even from rwsem before >> 503 * up_read() however userfaults will wait either for a >> 504 * targeted wakeup on the specific uwq waitqueue from >> 505 * wake_userfault() or for signals or for uffd >> 506 * release. >> 507 */ >> 508 while (!READ_ONCE(uwq.waken)) { >> 509 /* >> 510 * This needs the full smp_store_mb() >> 511 * guarantee as the state write must be >> 512 * visible to other CPUs before reading >> 513 * uwq.waken from other CPUs. >> 514 */ >> 515 set_current_state(blocking_state); >> 516 if (READ_ONCE(uwq.waken) || >> 517 READ_ONCE(ctx->released) || >> 518 (return_to_userland ? signal_pending(current) : >> 519 fatal_signal_pending(current))) >> 520 break; >> 521 schedule(); >> 522 } 528 } 523 } 529 524 530 __set_current_state(TASK_RUNNING); 525 __set_current_state(TASK_RUNNING); 531 526 >> 527 if (return_to_userland) { >> 528 if (signal_pending(current) && >> 529 !fatal_signal_pending(current)) { >> 530 /* >> 531 * If we got a SIGSTOP or SIGCONT and this is >> 532 * a normal userland page fault, just let >> 533 * userland return so the signal will be >> 534 * handled and gdb debugging works. The page >> 535 * fault code immediately after we return from >> 536 * this function is going to release the >> 537 * mmap_sem and it's not depending on it >> 538 * (unlike gup would if we were not to return >> 539 * VM_FAULT_RETRY). >> 540 * >> 541 * If a fatal signal is pending we still take >> 542 * the streamlined VM_FAULT_RETRY failure path >> 543 * and there's no need to retake the mmap_sem >> 544 * in such case. >> 545 */ >> 546 down_read(&mm->mmap_sem); >> 547 ret = VM_FAULT_NOPAGE; >> 548 } >> 549 } >> 550 532 /* 551 /* 533 * Here we race with the list_del; lis 552 * Here we race with the list_del; list_add in 534 * userfaultfd_ctx_read(), however bec 553 * userfaultfd_ctx_read(), however because we don't ever run 535 * list_del_init() to refile across th 554 * list_del_init() to refile across the two lists, the prev 536 * and next pointers will never point 555 * and next pointers will never point to self. list_add also 537 * would never let any of the two poin 556 * would never let any of the two pointers to point to 538 * self. So list_empty_careful won't r 557 * self. So list_empty_careful won't risk to see both pointers 539 * pointing to self at any time during 558 * pointing to self at any time during the list refile. The 540 * only case where list_del_init() is 559 * only case where list_del_init() is called is the full 541 * removal in the wake function and th 560 * removal in the wake function and there we don't re-list_add 542 * and it's fine not to block on the s 561 * and it's fine not to block on the spinlock. The uwq on this 543 * kernel stack can be released after 562 * kernel stack can be released after the list_del_init. 544 */ 563 */ 545 if (!list_empty_careful(&uwq.wq.entry) 564 if (!list_empty_careful(&uwq.wq.entry)) { 546 spin_lock_irq(&ctx->fault_pend 565 spin_lock_irq(&ctx->fault_pending_wqh.lock); 547 /* 566 /* 548 * No need of list_del_init(), 567 * No need of list_del_init(), the uwq on the stack 549 * will be freed shortly anywa 568 * will be freed shortly anyway. 550 */ 569 */ 551 list_del(&uwq.wq.entry); 570 list_del(&uwq.wq.entry); 552 spin_unlock_irq(&ctx->fault_pe 571 spin_unlock_irq(&ctx->fault_pending_wqh.lock); 553 } 572 } 554 573 555 /* 574 /* 556 * ctx may go away after this if the u 575 * ctx may go away after this if the userfault pseudo fd is 557 * already released. 576 * already released. 558 */ 577 */ 559 userfaultfd_ctx_put(ctx); 578 userfaultfd_ctx_put(ctx); 560 579 561 out: 580 out: 562 return ret; 581 return ret; 563 } 582 } 564 583 565 static void userfaultfd_event_wait_completion( 584 static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx, 566 585 struct userfaultfd_wait_queue *ewq) 567 { 586 { 568 struct userfaultfd_ctx *release_new_ct 587 struct userfaultfd_ctx *release_new_ctx; 569 588 570 if (WARN_ON_ONCE(current->flags & PF_E 589 if (WARN_ON_ONCE(current->flags & PF_EXITING)) 571 goto out; 590 goto out; 572 591 573 ewq->ctx = ctx; 592 ewq->ctx = ctx; 574 init_waitqueue_entry(&ewq->wq, current 593 init_waitqueue_entry(&ewq->wq, current); 575 release_new_ctx = NULL; 594 release_new_ctx = NULL; 576 595 577 spin_lock_irq(&ctx->event_wqh.lock); 596 spin_lock_irq(&ctx->event_wqh.lock); 578 /* 597 /* 579 * After the __add_wait_queue the uwq 598 * After the __add_wait_queue the uwq is visible to userland 580 * through poll/read(). 599 * through poll/read(). 581 */ 600 */ 582 __add_wait_queue(&ctx->event_wqh, &ewq 601 __add_wait_queue(&ctx->event_wqh, &ewq->wq); 583 for (;;) { 602 for (;;) { 584 set_current_state(TASK_KILLABL 603 set_current_state(TASK_KILLABLE); 585 if (ewq->msg.event == 0) 604 if (ewq->msg.event == 0) 586 break; 605 break; 587 if (READ_ONCE(ctx->released) | 606 if (READ_ONCE(ctx->released) || 588 fatal_signal_pending(curre 607 fatal_signal_pending(current)) { 589 /* 608 /* 590 * &ewq->wq may be que 609 * &ewq->wq may be queued in fork_event, but 591 * __remove_wait_queue 610 * __remove_wait_queue ignores the head 592 * parameter. It would 611 * parameter. It would be a problem if it 593 * didn't. 612 * didn't. 594 */ 613 */ 595 __remove_wait_queue(&c 614 __remove_wait_queue(&ctx->event_wqh, &ewq->wq); 596 if (ewq->msg.event == 615 if (ewq->msg.event == UFFD_EVENT_FORK) { 597 struct userfau 616 struct userfaultfd_ctx *new; 598 617 599 new = (struct 618 new = (struct userfaultfd_ctx *) 600 (unsig 619 (unsigned long) 601 ewq->m 620 ewq->msg.arg.reserved.reserved1; 602 release_new_ct 621 release_new_ctx = new; 603 } 622 } 604 break; 623 break; 605 } 624 } 606 625 607 spin_unlock_irq(&ctx->event_wq 626 spin_unlock_irq(&ctx->event_wqh.lock); 608 627 609 wake_up_poll(&ctx->fd_wqh, EPO 628 wake_up_poll(&ctx->fd_wqh, EPOLLIN); 610 schedule(); 629 schedule(); 611 630 612 spin_lock_irq(&ctx->event_wqh. 631 spin_lock_irq(&ctx->event_wqh.lock); 613 } 632 } 614 __set_current_state(TASK_RUNNING); 633 __set_current_state(TASK_RUNNING); 615 spin_unlock_irq(&ctx->event_wqh.lock); 634 spin_unlock_irq(&ctx->event_wqh.lock); 616 635 617 if (release_new_ctx) { 636 if (release_new_ctx) { 618 struct vm_area_struct *vma; 637 struct vm_area_struct *vma; 619 struct mm_struct *mm = release 638 struct mm_struct *mm = release_new_ctx->mm; 620 VMA_ITERATOR(vmi, mm, 0); << 621 639 622 /* the various vma->vm_userfau 640 /* the various vma->vm_userfaultfd_ctx still points to it */ 623 mmap_write_lock(mm); !! 641 down_write(&mm->mmap_sem); 624 for_each_vma(vmi, vma) { !! 642 /* no task can run (and in turn coredump) yet */ >> 643 VM_WARN_ON(!mmget_still_valid(mm)); >> 644 for (vma = mm->mmap; vma; vma = vma->vm_next) 625 if (vma->vm_userfaultf 645 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) { 626 vma_start_writ << 627 vma->vm_userfa 646 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; 628 userfaultfd_se !! 647 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING); 629 << 630 } 648 } 631 } !! 649 up_write(&mm->mmap_sem); 632 mmap_write_unlock(mm); << 633 650 634 userfaultfd_ctx_put(release_ne 651 userfaultfd_ctx_put(release_new_ctx); 635 } 652 } 636 653 637 /* 654 /* 638 * ctx may go away after this if the u 655 * ctx may go away after this if the userfault pseudo fd is 639 * already released. 656 * already released. 640 */ 657 */ 641 out: 658 out: 642 atomic_dec(&ctx->mmap_changing); !! 659 WRITE_ONCE(ctx->mmap_changing, false); 643 VM_BUG_ON(atomic_read(&ctx->mmap_chang << 644 userfaultfd_ctx_put(ctx); 660 userfaultfd_ctx_put(ctx); 645 } 661 } 646 662 647 static void userfaultfd_event_complete(struct 663 static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx, 648 struct 664 struct userfaultfd_wait_queue *ewq) 649 { 665 { 650 ewq->msg.event = 0; 666 ewq->msg.event = 0; 651 wake_up_locked(&ctx->event_wqh); 667 wake_up_locked(&ctx->event_wqh); 652 __remove_wait_queue(&ctx->event_wqh, & 668 __remove_wait_queue(&ctx->event_wqh, &ewq->wq); 653 } 669 } 654 670 655 int dup_userfaultfd(struct vm_area_struct *vma 671 int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs) 656 { 672 { 657 struct userfaultfd_ctx *ctx = NULL, *o 673 struct userfaultfd_ctx *ctx = NULL, *octx; 658 struct userfaultfd_fork_ctx *fctx; 674 struct userfaultfd_fork_ctx *fctx; 659 675 660 octx = vma->vm_userfaultfd_ctx.ctx; 676 octx = vma->vm_userfaultfd_ctx.ctx; 661 if (!octx) !! 677 if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) { 662 return 0; << 663 << 664 if (!(octx->features & UFFD_FEATURE_EV << 665 vma_start_write(vma); << 666 vma->vm_userfaultfd_ctx = NULL 678 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; 667 userfaultfd_set_vm_flags(vma, !! 679 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING); 668 return 0; 680 return 0; 669 } 681 } 670 682 671 list_for_each_entry(fctx, fcs, list) 683 list_for_each_entry(fctx, fcs, list) 672 if (fctx->orig == octx) { 684 if (fctx->orig == octx) { 673 ctx = fctx->new; 685 ctx = fctx->new; 674 break; 686 break; 675 } 687 } 676 688 677 if (!ctx) { 689 if (!ctx) { 678 fctx = kmalloc(sizeof(*fctx), 690 fctx = kmalloc(sizeof(*fctx), GFP_KERNEL); 679 if (!fctx) 691 if (!fctx) 680 return -ENOMEM; 692 return -ENOMEM; 681 693 682 ctx = kmem_cache_alloc(userfau 694 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL); 683 if (!ctx) { 695 if (!ctx) { 684 kfree(fctx); 696 kfree(fctx); 685 return -ENOMEM; 697 return -ENOMEM; 686 } 698 } 687 699 688 refcount_set(&ctx->refcount, 1 700 refcount_set(&ctx->refcount, 1); 689 ctx->flags = octx->flags; 701 ctx->flags = octx->flags; >> 702 ctx->state = UFFD_STATE_RUNNING; 690 ctx->features = octx->features 703 ctx->features = octx->features; 691 ctx->released = false; 704 ctx->released = false; 692 init_rwsem(&ctx->map_changing_ !! 705 ctx->mmap_changing = false; 693 atomic_set(&ctx->mmap_changing << 694 ctx->mm = vma->vm_mm; 706 ctx->mm = vma->vm_mm; 695 mmgrab(ctx->mm); 707 mmgrab(ctx->mm); 696 708 697 userfaultfd_ctx_get(octx); 709 userfaultfd_ctx_get(octx); 698 down_write(&octx->map_changing !! 710 WRITE_ONCE(octx->mmap_changing, true); 699 atomic_inc(&octx->mmap_changin << 700 up_write(&octx->map_changing_l << 701 fctx->orig = octx; 711 fctx->orig = octx; 702 fctx->new = ctx; 712 fctx->new = ctx; 703 list_add_tail(&fctx->list, fcs 713 list_add_tail(&fctx->list, fcs); 704 } 714 } 705 715 706 vma->vm_userfaultfd_ctx.ctx = ctx; 716 vma->vm_userfaultfd_ctx.ctx = ctx; 707 return 0; 717 return 0; 708 } 718 } 709 719 710 static void dup_fctx(struct userfaultfd_fork_c 720 static void dup_fctx(struct userfaultfd_fork_ctx *fctx) 711 { 721 { 712 struct userfaultfd_ctx *ctx = fctx->or 722 struct userfaultfd_ctx *ctx = fctx->orig; 713 struct userfaultfd_wait_queue ewq; 723 struct userfaultfd_wait_queue ewq; 714 724 715 msg_init(&ewq.msg); 725 msg_init(&ewq.msg); 716 726 717 ewq.msg.event = UFFD_EVENT_FORK; 727 ewq.msg.event = UFFD_EVENT_FORK; 718 ewq.msg.arg.reserved.reserved1 = (unsi 728 ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new; 719 729 720 userfaultfd_event_wait_completion(ctx, 730 userfaultfd_event_wait_completion(ctx, &ewq); 721 } 731 } 722 732 723 void dup_userfaultfd_complete(struct list_head 733 void dup_userfaultfd_complete(struct list_head *fcs) 724 { 734 { 725 struct userfaultfd_fork_ctx *fctx, *n; 735 struct userfaultfd_fork_ctx *fctx, *n; 726 736 727 list_for_each_entry_safe(fctx, n, fcs, 737 list_for_each_entry_safe(fctx, n, fcs, list) { 728 dup_fctx(fctx); 738 dup_fctx(fctx); 729 list_del(&fctx->list); 739 list_del(&fctx->list); 730 kfree(fctx); 740 kfree(fctx); 731 } 741 } 732 } 742 } 733 743 734 void mremap_userfaultfd_prep(struct vm_area_st 744 void mremap_userfaultfd_prep(struct vm_area_struct *vma, 735 struct vm_userfau 745 struct vm_userfaultfd_ctx *vm_ctx) 736 { 746 { 737 struct userfaultfd_ctx *ctx; 747 struct userfaultfd_ctx *ctx; 738 748 739 ctx = vma->vm_userfaultfd_ctx.ctx; 749 ctx = vma->vm_userfaultfd_ctx.ctx; 740 750 741 if (!ctx) 751 if (!ctx) 742 return; 752 return; 743 753 744 if (ctx->features & UFFD_FEATURE_EVENT 754 if (ctx->features & UFFD_FEATURE_EVENT_REMAP) { 745 vm_ctx->ctx = ctx; 755 vm_ctx->ctx = ctx; 746 userfaultfd_ctx_get(ctx); 756 userfaultfd_ctx_get(ctx); 747 down_write(&ctx->map_changing_ !! 757 WRITE_ONCE(ctx->mmap_changing, true); 748 atomic_inc(&ctx->mmap_changing << 749 up_write(&ctx->map_changing_lo << 750 } else { 758 } else { 751 /* Drop uffd context if remap 759 /* Drop uffd context if remap feature not enabled */ 752 vma_start_write(vma); << 753 vma->vm_userfaultfd_ctx = NULL 760 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; 754 userfaultfd_set_vm_flags(vma, !! 761 vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING); 755 } 762 } 756 } 763 } 757 764 758 void mremap_userfaultfd_complete(struct vm_use 765 void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx, 759 unsigned long 766 unsigned long from, unsigned long to, 760 unsigned long 767 unsigned long len) 761 { 768 { 762 struct userfaultfd_ctx *ctx = vm_ctx-> 769 struct userfaultfd_ctx *ctx = vm_ctx->ctx; 763 struct userfaultfd_wait_queue ewq; 770 struct userfaultfd_wait_queue ewq; 764 771 765 if (!ctx) 772 if (!ctx) 766 return; 773 return; 767 774 768 if (to & ~PAGE_MASK) { 775 if (to & ~PAGE_MASK) { 769 userfaultfd_ctx_put(ctx); 776 userfaultfd_ctx_put(ctx); 770 return; 777 return; 771 } 778 } 772 779 773 msg_init(&ewq.msg); 780 msg_init(&ewq.msg); 774 781 775 ewq.msg.event = UFFD_EVENT_REMAP; 782 ewq.msg.event = UFFD_EVENT_REMAP; 776 ewq.msg.arg.remap.from = from; 783 ewq.msg.arg.remap.from = from; 777 ewq.msg.arg.remap.to = to; 784 ewq.msg.arg.remap.to = to; 778 ewq.msg.arg.remap.len = len; 785 ewq.msg.arg.remap.len = len; 779 786 780 userfaultfd_event_wait_completion(ctx, 787 userfaultfd_event_wait_completion(ctx, &ewq); 781 } 788 } 782 789 783 bool userfaultfd_remove(struct vm_area_struct 790 bool userfaultfd_remove(struct vm_area_struct *vma, 784 unsigned long start, u 791 unsigned long start, unsigned long end) 785 { 792 { 786 struct mm_struct *mm = vma->vm_mm; 793 struct mm_struct *mm = vma->vm_mm; 787 struct userfaultfd_ctx *ctx; 794 struct userfaultfd_ctx *ctx; 788 struct userfaultfd_wait_queue ewq; 795 struct userfaultfd_wait_queue ewq; 789 796 790 ctx = vma->vm_userfaultfd_ctx.ctx; 797 ctx = vma->vm_userfaultfd_ctx.ctx; 791 if (!ctx || !(ctx->features & UFFD_FEA 798 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE)) 792 return true; 799 return true; 793 800 794 userfaultfd_ctx_get(ctx); 801 userfaultfd_ctx_get(ctx); 795 down_write(&ctx->map_changing_lock); !! 802 WRITE_ONCE(ctx->mmap_changing, true); 796 atomic_inc(&ctx->mmap_changing); !! 803 up_read(&mm->mmap_sem); 797 up_write(&ctx->map_changing_lock); << 798 mmap_read_unlock(mm); << 799 804 800 msg_init(&ewq.msg); 805 msg_init(&ewq.msg); 801 806 802 ewq.msg.event = UFFD_EVENT_REMOVE; 807 ewq.msg.event = UFFD_EVENT_REMOVE; 803 ewq.msg.arg.remove.start = start; 808 ewq.msg.arg.remove.start = start; 804 ewq.msg.arg.remove.end = end; 809 ewq.msg.arg.remove.end = end; 805 810 806 userfaultfd_event_wait_completion(ctx, 811 userfaultfd_event_wait_completion(ctx, &ewq); 807 812 808 return false; 813 return false; 809 } 814 } 810 815 811 static bool has_unmap_ctx(struct userfaultfd_c 816 static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps, 812 unsigned long start, 817 unsigned long start, unsigned long end) 813 { 818 { 814 struct userfaultfd_unmap_ctx *unmap_ct 819 struct userfaultfd_unmap_ctx *unmap_ctx; 815 820 816 list_for_each_entry(unmap_ctx, unmaps, 821 list_for_each_entry(unmap_ctx, unmaps, list) 817 if (unmap_ctx->ctx == ctx && u 822 if (unmap_ctx->ctx == ctx && unmap_ctx->start == start && 818 unmap_ctx->end == end) 823 unmap_ctx->end == end) 819 return true; 824 return true; 820 825 821 return false; 826 return false; 822 } 827 } 823 828 824 int userfaultfd_unmap_prep(struct vm_area_stru !! 829 int userfaultfd_unmap_prep(struct vm_area_struct *vma, 825 unsigned long end, !! 830 unsigned long start, unsigned long end, 826 { !! 831 struct list_head *unmaps) 827 struct userfaultfd_unmap_ctx *unmap_ct !! 832 { 828 struct userfaultfd_ctx *ctx = vma->vm_ !! 833 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) { >> 834 struct userfaultfd_unmap_ctx *unmap_ctx; >> 835 struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx; 829 836 830 if (!ctx || !(ctx->features & UFFD_FEA !! 837 if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) || 831 has_unmap_ctx(ctx, unmaps, start, !! 838 has_unmap_ctx(ctx, unmaps, start, end)) 832 return 0; !! 839 continue; 833 840 834 unmap_ctx = kzalloc(sizeof(*unmap_ctx) !! 841 unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL); 835 if (!unmap_ctx) !! 842 if (!unmap_ctx) 836 return -ENOMEM; !! 843 return -ENOMEM; 837 844 838 userfaultfd_ctx_get(ctx); !! 845 userfaultfd_ctx_get(ctx); 839 down_write(&ctx->map_changing_lock); !! 846 WRITE_ONCE(ctx->mmap_changing, true); 840 atomic_inc(&ctx->mmap_changing); !! 847 unmap_ctx->ctx = ctx; 841 up_write(&ctx->map_changing_lock); !! 848 unmap_ctx->start = start; 842 unmap_ctx->ctx = ctx; !! 849 unmap_ctx->end = end; 843 unmap_ctx->start = start; !! 850 list_add_tail(&unmap_ctx->list, unmaps); 844 unmap_ctx->end = end; !! 851 } 845 list_add_tail(&unmap_ctx->list, unmaps << 846 852 847 return 0; 853 return 0; 848 } 854 } 849 855 850 void userfaultfd_unmap_complete(struct mm_stru 856 void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf) 851 { 857 { 852 struct userfaultfd_unmap_ctx *ctx, *n; 858 struct userfaultfd_unmap_ctx *ctx, *n; 853 struct userfaultfd_wait_queue ewq; 859 struct userfaultfd_wait_queue ewq; 854 860 855 list_for_each_entry_safe(ctx, n, uf, l 861 list_for_each_entry_safe(ctx, n, uf, list) { 856 msg_init(&ewq.msg); 862 msg_init(&ewq.msg); 857 863 858 ewq.msg.event = UFFD_EVENT_UNM 864 ewq.msg.event = UFFD_EVENT_UNMAP; 859 ewq.msg.arg.remove.start = ctx 865 ewq.msg.arg.remove.start = ctx->start; 860 ewq.msg.arg.remove.end = ctx-> 866 ewq.msg.arg.remove.end = ctx->end; 861 867 862 userfaultfd_event_wait_complet 868 userfaultfd_event_wait_completion(ctx->ctx, &ewq); 863 869 864 list_del(&ctx->list); 870 list_del(&ctx->list); 865 kfree(ctx); 871 kfree(ctx); 866 } 872 } 867 } 873 } 868 874 869 static int userfaultfd_release(struct inode *i 875 static int userfaultfd_release(struct inode *inode, struct file *file) 870 { 876 { 871 struct userfaultfd_ctx *ctx = file->pr 877 struct userfaultfd_ctx *ctx = file->private_data; 872 struct mm_struct *mm = ctx->mm; 878 struct mm_struct *mm = ctx->mm; 873 struct vm_area_struct *vma, *prev; 879 struct vm_area_struct *vma, *prev; 874 /* len == 0 means wake all */ 880 /* len == 0 means wake all */ 875 struct userfaultfd_wake_range range = 881 struct userfaultfd_wake_range range = { .len = 0, }; 876 unsigned long new_flags; 882 unsigned long new_flags; 877 VMA_ITERATOR(vmi, mm, 0); !! 883 bool still_valid; 878 884 879 WRITE_ONCE(ctx->released, true); 885 WRITE_ONCE(ctx->released, true); 880 886 881 if (!mmget_not_zero(mm)) 887 if (!mmget_not_zero(mm)) 882 goto wakeup; 888 goto wakeup; 883 889 884 /* 890 /* 885 * Flush page faults out of all CPUs. 891 * Flush page faults out of all CPUs. NOTE: all page faults 886 * must be retried without returning V 892 * must be retried without returning VM_FAULT_SIGBUS if 887 * userfaultfd_ctx_get() succeeds but 893 * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx 888 * changes while handle_userfault rele !! 894 * changes while handle_userfault released the mmap_sem. So 889 * it's critical that released is set 895 * it's critical that released is set to true (above), before 890 * taking the mmap_lock for writing. !! 896 * taking the mmap_sem for writing. 891 */ 897 */ 892 mmap_write_lock(mm); !! 898 down_write(&mm->mmap_sem); >> 899 still_valid = mmget_still_valid(mm); 893 prev = NULL; 900 prev = NULL; 894 for_each_vma(vmi, vma) { !! 901 for (vma = mm->mmap; vma; vma = vma->vm_next) { 895 cond_resched(); 902 cond_resched(); 896 BUG_ON(!!vma->vm_userfaultfd_c 903 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^ 897 !!(vma->vm_flags & __VM !! 904 !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP))); 898 if (vma->vm_userfaultfd_ctx.ct 905 if (vma->vm_userfaultfd_ctx.ctx != ctx) { 899 prev = vma; 906 prev = vma; 900 continue; 907 continue; 901 } 908 } 902 /* Reset ptes for the whole vm !! 909 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP); 903 if (userfaultfd_wp(vma)) !! 910 if (still_valid) { 904 uffd_wp_range(vma, vma !! 911 prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end, 905 vma->vm_ !! 912 new_flags, vma->anon_vma, 906 new_flags = vma->vm_flags & ~_ !! 913 vma->vm_file, vma->vm_pgoff, 907 vma = vma_modify_flags_uffd(&v !! 914 vma_policy(vma), 908 vm !! 915 NULL_VM_UFFD_CTX); 909 NU !! 916 if (prev) 910 !! 917 vma = prev; 911 vma_start_write(vma); !! 918 else 912 userfaultfd_set_vm_flags(vma, !! 919 prev = vma; >> 920 } >> 921 vma->vm_flags = new_flags; 913 vma->vm_userfaultfd_ctx = NULL 922 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; 914 << 915 prev = vma; << 916 } 923 } 917 mmap_write_unlock(mm); !! 924 up_write(&mm->mmap_sem); 918 mmput(mm); 925 mmput(mm); 919 wakeup: 926 wakeup: 920 /* 927 /* 921 * After no new page faults can wait o 928 * After no new page faults can wait on this fault_*wqh, flush 922 * the last page faults that may have 929 * the last page faults that may have been already waiting on 923 * the fault_*wqh. 930 * the fault_*wqh. 924 */ 931 */ 925 spin_lock_irq(&ctx->fault_pending_wqh. 932 spin_lock_irq(&ctx->fault_pending_wqh.lock); 926 __wake_up_locked_key(&ctx->fault_pendi 933 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range); 927 __wake_up(&ctx->fault_wqh, TASK_NORMAL 934 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range); 928 spin_unlock_irq(&ctx->fault_pending_wq 935 spin_unlock_irq(&ctx->fault_pending_wqh.lock); 929 936 930 /* Flush pending events that may still 937 /* Flush pending events that may still wait on event_wqh */ 931 wake_up_all(&ctx->event_wqh); 938 wake_up_all(&ctx->event_wqh); 932 939 933 wake_up_poll(&ctx->fd_wqh, EPOLLHUP); 940 wake_up_poll(&ctx->fd_wqh, EPOLLHUP); 934 userfaultfd_ctx_put(ctx); 941 userfaultfd_ctx_put(ctx); 935 return 0; 942 return 0; 936 } 943 } 937 944 938 /* fault_pending_wqh.lock must be hold by the 945 /* fault_pending_wqh.lock must be hold by the caller */ 939 static inline struct userfaultfd_wait_queue *f 946 static inline struct userfaultfd_wait_queue *find_userfault_in( 940 wait_queue_head_t *wqh) 947 wait_queue_head_t *wqh) 941 { 948 { 942 wait_queue_entry_t *wq; 949 wait_queue_entry_t *wq; 943 struct userfaultfd_wait_queue *uwq; 950 struct userfaultfd_wait_queue *uwq; 944 951 945 lockdep_assert_held(&wqh->lock); 952 lockdep_assert_held(&wqh->lock); 946 953 947 uwq = NULL; 954 uwq = NULL; 948 if (!waitqueue_active(wqh)) 955 if (!waitqueue_active(wqh)) 949 goto out; 956 goto out; 950 /* walk in reverse to provide FIFO beh 957 /* walk in reverse to provide FIFO behavior to read userfaults */ 951 wq = list_last_entry(&wqh->head, typeo 958 wq = list_last_entry(&wqh->head, typeof(*wq), entry); 952 uwq = container_of(wq, struct userfaul 959 uwq = container_of(wq, struct userfaultfd_wait_queue, wq); 953 out: 960 out: 954 return uwq; 961 return uwq; 955 } 962 } 956 963 957 static inline struct userfaultfd_wait_queue *f 964 static inline struct userfaultfd_wait_queue *find_userfault( 958 struct userfaultfd_ctx *ctx) 965 struct userfaultfd_ctx *ctx) 959 { 966 { 960 return find_userfault_in(&ctx->fault_p 967 return find_userfault_in(&ctx->fault_pending_wqh); 961 } 968 } 962 969 963 static inline struct userfaultfd_wait_queue *f 970 static inline struct userfaultfd_wait_queue *find_userfault_evt( 964 struct userfaultfd_ctx *ctx) 971 struct userfaultfd_ctx *ctx) 965 { 972 { 966 return find_userfault_in(&ctx->event_w 973 return find_userfault_in(&ctx->event_wqh); 967 } 974 } 968 975 969 static __poll_t userfaultfd_poll(struct file * 976 static __poll_t userfaultfd_poll(struct file *file, poll_table *wait) 970 { 977 { 971 struct userfaultfd_ctx *ctx = file->pr 978 struct userfaultfd_ctx *ctx = file->private_data; 972 __poll_t ret; 979 __poll_t ret; 973 980 974 poll_wait(file, &ctx->fd_wqh, wait); 981 poll_wait(file, &ctx->fd_wqh, wait); 975 982 976 if (!userfaultfd_is_initialized(ctx)) !! 983 switch (ctx->state) { >> 984 case UFFD_STATE_WAIT_API: 977 return EPOLLERR; 985 return EPOLLERR; >> 986 case UFFD_STATE_RUNNING: >> 987 /* >> 988 * poll() never guarantees that read won't block. >> 989 * userfaults can be waken before they're read(). >> 990 */ >> 991 if (unlikely(!(file->f_flags & O_NONBLOCK))) >> 992 return EPOLLERR; >> 993 /* >> 994 * lockless access to see if there are pending faults >> 995 * __pollwait last action is the add_wait_queue but >> 996 * the spin_unlock would allow the waitqueue_active to >> 997 * pass above the actual list_add inside >> 998 * add_wait_queue critical section. So use a full >> 999 * memory barrier to serialize the list_add write of >> 1000 * add_wait_queue() with the waitqueue_active read >> 1001 * below. >> 1002 */ >> 1003 ret = 0; >> 1004 smp_mb(); >> 1005 if (waitqueue_active(&ctx->fault_pending_wqh)) >> 1006 ret = EPOLLIN; >> 1007 else if (waitqueue_active(&ctx->event_wqh)) >> 1008 ret = EPOLLIN; 978 1009 979 /* !! 1010 return ret; 980 * poll() never guarantees that read w !! 1011 default: 981 * userfaults can be waken before they !! 1012 WARN_ON_ONCE(1); 982 */ << 983 if (unlikely(!(file->f_flags & O_NONBL << 984 return EPOLLERR; 1013 return EPOLLERR; 985 /* !! 1014 } 986 * lockless access to see if there are << 987 * __pollwait last action is the add_w << 988 * the spin_unlock would allow the wai << 989 * pass above the actual list_add insi << 990 * add_wait_queue critical section. So << 991 * memory barrier to serialize the lis << 992 * add_wait_queue() with the waitqueue << 993 * below. << 994 */ << 995 ret = 0; << 996 smp_mb(); << 997 if (waitqueue_active(&ctx->fault_pendi << 998 ret = EPOLLIN; << 999 else if (waitqueue_active(&ctx->event_ << 1000 ret = EPOLLIN; << 1001 << 1002 return ret; << 1003 } 1015 } 1004 1016 1005 static const struct file_operations userfault 1017 static const struct file_operations userfaultfd_fops; 1006 1018 1007 static int resolve_userfault_fork(struct user !! 1019 static int resolve_userfault_fork(struct userfaultfd_ctx *ctx, 1008 struct inod !! 1020 struct userfaultfd_ctx *new, 1009 struct uffd 1021 struct uffd_msg *msg) 1010 { 1022 { 1011 int fd; 1023 int fd; 1012 1024 1013 fd = anon_inode_create_getfd("[userfa !! 1025 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new, 1014 O_RDONLY | (new->flag !! 1026 O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS)); 1015 if (fd < 0) 1027 if (fd < 0) 1016 return fd; 1028 return fd; 1017 1029 1018 msg->arg.reserved.reserved1 = 0; 1030 msg->arg.reserved.reserved1 = 0; 1019 msg->arg.fork.ufd = fd; 1031 msg->arg.fork.ufd = fd; 1020 return 0; 1032 return 0; 1021 } 1033 } 1022 1034 1023 static ssize_t userfaultfd_ctx_read(struct us 1035 static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, 1024 struct uf !! 1036 struct uffd_msg *msg) 1025 { 1037 { 1026 ssize_t ret; 1038 ssize_t ret; 1027 DECLARE_WAITQUEUE(wait, current); 1039 DECLARE_WAITQUEUE(wait, current); 1028 struct userfaultfd_wait_queue *uwq; 1040 struct userfaultfd_wait_queue *uwq; 1029 /* 1041 /* 1030 * Handling fork event requires sleep 1042 * Handling fork event requires sleeping operations, so 1031 * we drop the event_wqh lock, then d 1043 * we drop the event_wqh lock, then do these ops, then 1032 * lock it back and wake up the waite 1044 * lock it back and wake up the waiter. While the lock is 1033 * dropped the ewq may go away so we 1045 * dropped the ewq may go away so we keep track of it 1034 * carefully. 1046 * carefully. 1035 */ 1047 */ 1036 LIST_HEAD(fork_event); 1048 LIST_HEAD(fork_event); 1037 struct userfaultfd_ctx *fork_nctx = N 1049 struct userfaultfd_ctx *fork_nctx = NULL; 1038 1050 1039 /* always take the fd_wqh lock before 1051 /* always take the fd_wqh lock before the fault_pending_wqh lock */ 1040 spin_lock_irq(&ctx->fd_wqh.lock); 1052 spin_lock_irq(&ctx->fd_wqh.lock); 1041 __add_wait_queue(&ctx->fd_wqh, &wait) 1053 __add_wait_queue(&ctx->fd_wqh, &wait); 1042 for (;;) { 1054 for (;;) { 1043 set_current_state(TASK_INTERR 1055 set_current_state(TASK_INTERRUPTIBLE); 1044 spin_lock(&ctx->fault_pending 1056 spin_lock(&ctx->fault_pending_wqh.lock); 1045 uwq = find_userfault(ctx); 1057 uwq = find_userfault(ctx); 1046 if (uwq) { 1058 if (uwq) { 1047 /* 1059 /* 1048 * Use a seqcount to 1060 * Use a seqcount to repeat the lockless check 1049 * in wake_userfault( 1061 * in wake_userfault() to avoid missing 1050 * wakeups because du 1062 * wakeups because during the refile both 1051 * waitqueue could be 1063 * waitqueue could become empty if this is the 1052 * only userfault. 1064 * only userfault. 1053 */ 1065 */ 1054 write_seqcount_begin( 1066 write_seqcount_begin(&ctx->refile_seq); 1055 1067 1056 /* 1068 /* 1057 * The fault_pending_ 1069 * The fault_pending_wqh.lock prevents the uwq 1058 * to disappear from 1070 * to disappear from under us. 1059 * 1071 * 1060 * Refile this userfa 1072 * Refile this userfault from 1061 * fault_pending_wqh 1073 * fault_pending_wqh to fault_wqh, it's not 1062 * pending anymore af 1074 * pending anymore after we read it. 1063 * 1075 * 1064 * Use list_del() by 1076 * Use list_del() by hand (as 1065 * userfaultfd_wake_f 1077 * userfaultfd_wake_function also uses 1066 * list_del_init() by 1078 * list_del_init() by hand) to be sure nobody 1067 * changes __remove_w 1079 * changes __remove_wait_queue() to use 1068 * list_del_init() in 1080 * list_del_init() in turn breaking the 1069 * !list_empty_carefu 1081 * !list_empty_careful() check in 1070 * handle_userfault() 1082 * handle_userfault(). The uwq->wq.head list 1071 * must never be empt 1083 * must never be empty at any time during the 1072 * refile, or the wai 1084 * refile, or the waitqueue could disappear 1073 * from under us. The 1085 * from under us. The "wait_queue_head_t" 1074 * parameter of __rem 1086 * parameter of __remove_wait_queue() is unused 1075 * anyway. 1087 * anyway. 1076 */ 1088 */ 1077 list_del(&uwq->wq.ent 1089 list_del(&uwq->wq.entry); 1078 add_wait_queue(&ctx-> 1090 add_wait_queue(&ctx->fault_wqh, &uwq->wq); 1079 1091 1080 write_seqcount_end(&c 1092 write_seqcount_end(&ctx->refile_seq); 1081 1093 1082 /* careful to always 1094 /* careful to always initialize msg if ret == 0 */ 1083 *msg = uwq->msg; 1095 *msg = uwq->msg; 1084 spin_unlock(&ctx->fau 1096 spin_unlock(&ctx->fault_pending_wqh.lock); 1085 ret = 0; 1097 ret = 0; 1086 break; 1098 break; 1087 } 1099 } 1088 spin_unlock(&ctx->fault_pendi 1100 spin_unlock(&ctx->fault_pending_wqh.lock); 1089 1101 1090 spin_lock(&ctx->event_wqh.loc 1102 spin_lock(&ctx->event_wqh.lock); 1091 uwq = find_userfault_evt(ctx) 1103 uwq = find_userfault_evt(ctx); 1092 if (uwq) { 1104 if (uwq) { 1093 *msg = uwq->msg; 1105 *msg = uwq->msg; 1094 1106 1095 if (uwq->msg.event == 1107 if (uwq->msg.event == UFFD_EVENT_FORK) { 1096 fork_nctx = ( 1108 fork_nctx = (struct userfaultfd_ctx *) 1097 (unsi 1109 (unsigned long) 1098 uwq-> 1110 uwq->msg.arg.reserved.reserved1; 1099 list_move(&uw 1111 list_move(&uwq->wq.entry, &fork_event); 1100 /* 1112 /* 1101 * fork_nctx 1113 * fork_nctx can be freed as soon as 1102 * we drop th 1114 * we drop the lock, unless we take a 1103 * reference 1115 * reference on it. 1104 */ 1116 */ 1105 userfaultfd_c 1117 userfaultfd_ctx_get(fork_nctx); 1106 spin_unlock(& 1118 spin_unlock(&ctx->event_wqh.lock); 1107 ret = 0; 1119 ret = 0; 1108 break; 1120 break; 1109 } 1121 } 1110 1122 1111 userfaultfd_event_com 1123 userfaultfd_event_complete(ctx, uwq); 1112 spin_unlock(&ctx->eve 1124 spin_unlock(&ctx->event_wqh.lock); 1113 ret = 0; 1125 ret = 0; 1114 break; 1126 break; 1115 } 1127 } 1116 spin_unlock(&ctx->event_wqh.l 1128 spin_unlock(&ctx->event_wqh.lock); 1117 1129 1118 if (signal_pending(current)) 1130 if (signal_pending(current)) { 1119 ret = -ERESTARTSYS; 1131 ret = -ERESTARTSYS; 1120 break; 1132 break; 1121 } 1133 } 1122 if (no_wait) { 1134 if (no_wait) { 1123 ret = -EAGAIN; 1135 ret = -EAGAIN; 1124 break; 1136 break; 1125 } 1137 } 1126 spin_unlock_irq(&ctx->fd_wqh. 1138 spin_unlock_irq(&ctx->fd_wqh.lock); 1127 schedule(); 1139 schedule(); 1128 spin_lock_irq(&ctx->fd_wqh.lo 1140 spin_lock_irq(&ctx->fd_wqh.lock); 1129 } 1141 } 1130 __remove_wait_queue(&ctx->fd_wqh, &wa 1142 __remove_wait_queue(&ctx->fd_wqh, &wait); 1131 __set_current_state(TASK_RUNNING); 1143 __set_current_state(TASK_RUNNING); 1132 spin_unlock_irq(&ctx->fd_wqh.lock); 1144 spin_unlock_irq(&ctx->fd_wqh.lock); 1133 1145 1134 if (!ret && msg->event == UFFD_EVENT_ 1146 if (!ret && msg->event == UFFD_EVENT_FORK) { 1135 ret = resolve_userfault_fork( !! 1147 ret = resolve_userfault_fork(ctx, fork_nctx, msg); 1136 spin_lock_irq(&ctx->event_wqh 1148 spin_lock_irq(&ctx->event_wqh.lock); 1137 if (!list_empty(&fork_event)) 1149 if (!list_empty(&fork_event)) { 1138 /* 1150 /* 1139 * The fork thread di 1151 * The fork thread didn't abort, so we can 1140 * drop the temporary 1152 * drop the temporary refcount. 1141 */ 1153 */ 1142 userfaultfd_ctx_put(f 1154 userfaultfd_ctx_put(fork_nctx); 1143 1155 1144 uwq = list_first_entr 1156 uwq = list_first_entry(&fork_event, 1145 1157 typeof(*uwq), 1146 1158 wq.entry); 1147 /* 1159 /* 1148 * If fork_event list 1160 * If fork_event list wasn't empty and in turn 1149 * the event wasn't a 1161 * the event wasn't already released by fork 1150 * (the event is allo 1162 * (the event is allocated on fork kernel 1151 * stack), put the ev 1163 * stack), put the event back to its place in 1152 * the event_wq. fork 1164 * the event_wq. fork_event head will be freed 1153 * as soon as we retu 1165 * as soon as we return so the event cannot 1154 * stay queued there 1166 * stay queued there no matter the current 1155 * "ret" value. 1167 * "ret" value. 1156 */ 1168 */ 1157 list_del(&uwq->wq.ent 1169 list_del(&uwq->wq.entry); 1158 __add_wait_queue(&ctx 1170 __add_wait_queue(&ctx->event_wqh, &uwq->wq); 1159 1171 1160 /* 1172 /* 1161 * Leave the event in 1173 * Leave the event in the waitqueue and report 1162 * error to userland 1174 * error to userland if we failed to resolve 1163 * the userfault fork 1175 * the userfault fork. 1164 */ 1176 */ 1165 if (likely(!ret)) 1177 if (likely(!ret)) 1166 userfaultfd_e 1178 userfaultfd_event_complete(ctx, uwq); 1167 } else { 1179 } else { 1168 /* 1180 /* 1169 * Here the fork thre 1181 * Here the fork thread aborted and the 1170 * refcount from the 1182 * refcount from the fork thread on fork_nctx 1171 * has already been r 1183 * has already been released. We still hold 1172 * the reference we t 1184 * the reference we took before releasing the 1173 * lock above. If res 1185 * lock above. If resolve_userfault_fork 1174 * failed we've to dr 1186 * failed we've to drop it because the 1175 * fork_nctx has to b 1187 * fork_nctx has to be freed in such case. If 1176 * it succeeded we'll 1188 * it succeeded we'll hold it because the new 1177 * uffd references it 1189 * uffd references it. 1178 */ 1190 */ 1179 if (ret) 1191 if (ret) 1180 userfaultfd_c 1192 userfaultfd_ctx_put(fork_nctx); 1181 } 1193 } 1182 spin_unlock_irq(&ctx->event_w 1194 spin_unlock_irq(&ctx->event_wqh.lock); 1183 } 1195 } 1184 1196 1185 return ret; 1197 return ret; 1186 } 1198 } 1187 1199 1188 static ssize_t userfaultfd_read_iter(struct k !! 1200 static ssize_t userfaultfd_read(struct file *file, char __user *buf, >> 1201 size_t count, loff_t *ppos) 1189 { 1202 { 1190 struct file *file = iocb->ki_filp; << 1191 struct userfaultfd_ctx *ctx = file->p 1203 struct userfaultfd_ctx *ctx = file->private_data; 1192 ssize_t _ret, ret = 0; 1204 ssize_t _ret, ret = 0; 1193 struct uffd_msg msg; 1205 struct uffd_msg msg; 1194 struct inode *inode = file_inode(file !! 1206 int no_wait = file->f_flags & O_NONBLOCK; 1195 bool no_wait; << 1196 1207 1197 if (!userfaultfd_is_initialized(ctx)) !! 1208 if (ctx->state == UFFD_STATE_WAIT_API) 1198 return -EINVAL; 1209 return -EINVAL; 1199 1210 1200 no_wait = file->f_flags & O_NONBLOCK << 1201 for (;;) { 1211 for (;;) { 1202 if (iov_iter_count(to) < size !! 1212 if (count < sizeof(msg)) 1203 return ret ? ret : -E 1213 return ret ? ret : -EINVAL; 1204 _ret = userfaultfd_ctx_read(c !! 1214 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg); 1205 if (_ret < 0) 1215 if (_ret < 0) 1206 return ret ? ret : _r 1216 return ret ? ret : _ret; 1207 _ret = !copy_to_iter_full(&ms !! 1217 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg))) 1208 if (_ret) << 1209 return ret ? ret : -E 1218 return ret ? ret : -EFAULT; 1210 ret += sizeof(msg); 1219 ret += sizeof(msg); >> 1220 buf += sizeof(msg); >> 1221 count -= sizeof(msg); 1211 /* 1222 /* 1212 * Allow to read more than on 1223 * Allow to read more than one fault at time but only 1213 * block if waiting for the v 1224 * block if waiting for the very first one. 1214 */ 1225 */ 1215 no_wait = true; !! 1226 no_wait = O_NONBLOCK; 1216 } 1227 } 1217 } 1228 } 1218 1229 1219 static void __wake_userfault(struct userfault 1230 static void __wake_userfault(struct userfaultfd_ctx *ctx, 1220 struct userfault 1231 struct userfaultfd_wake_range *range) 1221 { 1232 { 1222 spin_lock_irq(&ctx->fault_pending_wqh 1233 spin_lock_irq(&ctx->fault_pending_wqh.lock); 1223 /* wake all in the range and autoremo 1234 /* wake all in the range and autoremove */ 1224 if (waitqueue_active(&ctx->fault_pend 1235 if (waitqueue_active(&ctx->fault_pending_wqh)) 1225 __wake_up_locked_key(&ctx->fa 1236 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, 1226 range); 1237 range); 1227 if (waitqueue_active(&ctx->fault_wqh) 1238 if (waitqueue_active(&ctx->fault_wqh)) 1228 __wake_up(&ctx->fault_wqh, TA 1239 __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range); 1229 spin_unlock_irq(&ctx->fault_pending_w 1240 spin_unlock_irq(&ctx->fault_pending_wqh.lock); 1230 } 1241 } 1231 1242 1232 static __always_inline void wake_userfault(st 1243 static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx, 1233 st 1244 struct userfaultfd_wake_range *range) 1234 { 1245 { 1235 unsigned seq; 1246 unsigned seq; 1236 bool need_wakeup; 1247 bool need_wakeup; 1237 1248 1238 /* 1249 /* 1239 * To be sure waitqueue_active() is n 1250 * To be sure waitqueue_active() is not reordered by the CPU 1240 * before the pagetable update, use a 1251 * before the pagetable update, use an explicit SMP memory 1241 * barrier here. PT lock release or m !! 1252 * barrier here. PT lock release or up_read(mmap_sem) still 1242 * have release semantics that can al 1253 * have release semantics that can allow the 1243 * waitqueue_active() to be reordered 1254 * waitqueue_active() to be reordered before the pte update. 1244 */ 1255 */ 1245 smp_mb(); 1256 smp_mb(); 1246 1257 1247 /* 1258 /* 1248 * Use waitqueue_active because it's 1259 * Use waitqueue_active because it's very frequent to 1249 * change the address space atomicall 1260 * change the address space atomically even if there are no 1250 * userfaults yet. So we take the spi 1261 * userfaults yet. So we take the spinlock only when we're 1251 * sure we've userfaults to wake. 1262 * sure we've userfaults to wake. 1252 */ 1263 */ 1253 do { 1264 do { 1254 seq = read_seqcount_begin(&ct 1265 seq = read_seqcount_begin(&ctx->refile_seq); 1255 need_wakeup = waitqueue_activ 1266 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) || 1256 waitqueue_active(&ctx 1267 waitqueue_active(&ctx->fault_wqh); 1257 cond_resched(); 1268 cond_resched(); 1258 } while (read_seqcount_retry(&ctx->re 1269 } while (read_seqcount_retry(&ctx->refile_seq, seq)); 1259 if (need_wakeup) 1270 if (need_wakeup) 1260 __wake_userfault(ctx, range); 1271 __wake_userfault(ctx, range); 1261 } 1272 } 1262 1273 1263 static __always_inline int validate_unaligned !! 1274 static __always_inline int validate_range(struct mm_struct *mm, 1264 struct mm_struct *mm, __u64 start, __ !! 1275 __u64 *start, __u64 len) 1265 { 1276 { 1266 __u64 task_size = mm->task_size; 1277 __u64 task_size = mm->task_size; 1267 1278 >> 1279 *start = untagged_addr(*start); >> 1280 >> 1281 if (*start & ~PAGE_MASK) >> 1282 return -EINVAL; 1268 if (len & ~PAGE_MASK) 1283 if (len & ~PAGE_MASK) 1269 return -EINVAL; 1284 return -EINVAL; 1270 if (!len) 1285 if (!len) 1271 return -EINVAL; 1286 return -EINVAL; 1272 if (start < mmap_min_addr) !! 1287 if (*start < mmap_min_addr) 1273 return -EINVAL; << 1274 if (start >= task_size) << 1275 return -EINVAL; 1288 return -EINVAL; 1276 if (len > task_size - start) !! 1289 if (*start >= task_size) 1277 return -EINVAL; 1290 return -EINVAL; 1278 if (start + len <= start) !! 1291 if (len > task_size - *start) 1279 return -EINVAL; 1292 return -EINVAL; 1280 return 0; 1293 return 0; 1281 } 1294 } 1282 1295 1283 static __always_inline int validate_range(str !! 1296 static inline bool vma_can_userfault(struct vm_area_struct *vma) 1284 __u << 1285 { 1297 { 1286 if (start & ~PAGE_MASK) !! 1298 return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) || 1287 return -EINVAL; !! 1299 vma_is_shmem(vma); 1288 << 1289 return validate_unaligned_range(mm, s << 1290 } 1300 } 1291 1301 1292 static int userfaultfd_register(struct userfa 1302 static int userfaultfd_register(struct userfaultfd_ctx *ctx, 1293 unsigned long 1303 unsigned long arg) 1294 { 1304 { 1295 struct mm_struct *mm = ctx->mm; 1305 struct mm_struct *mm = ctx->mm; 1296 struct vm_area_struct *vma, *prev, *c 1306 struct vm_area_struct *vma, *prev, *cur; 1297 int ret; 1307 int ret; 1298 struct uffdio_register uffdio_registe 1308 struct uffdio_register uffdio_register; 1299 struct uffdio_register __user *user_u 1309 struct uffdio_register __user *user_uffdio_register; 1300 unsigned long vm_flags, new_flags; 1310 unsigned long vm_flags, new_flags; 1301 bool found; 1311 bool found; 1302 bool basic_ioctls; 1312 bool basic_ioctls; 1303 unsigned long start, end, vma_end; 1313 unsigned long start, end, vma_end; 1304 struct vma_iterator vmi; << 1305 bool wp_async = userfaultfd_wp_async_ << 1306 1314 1307 user_uffdio_register = (struct uffdio 1315 user_uffdio_register = (struct uffdio_register __user *) arg; 1308 1316 1309 ret = -EFAULT; 1317 ret = -EFAULT; 1310 if (copy_from_user(&uffdio_register, 1318 if (copy_from_user(&uffdio_register, user_uffdio_register, 1311 sizeof(uffdio_regi 1319 sizeof(uffdio_register)-sizeof(__u64))) 1312 goto out; 1320 goto out; 1313 1321 1314 ret = -EINVAL; 1322 ret = -EINVAL; 1315 if (!uffdio_register.mode) 1323 if (!uffdio_register.mode) 1316 goto out; 1324 goto out; 1317 if (uffdio_register.mode & ~UFFD_API_ !! 1325 if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING| >> 1326 UFFDIO_REGISTER_MODE_WP)) 1318 goto out; 1327 goto out; 1319 vm_flags = 0; 1328 vm_flags = 0; 1320 if (uffdio_register.mode & UFFDIO_REG 1329 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING) 1321 vm_flags |= VM_UFFD_MISSING; 1330 vm_flags |= VM_UFFD_MISSING; 1322 if (uffdio_register.mode & UFFDIO_REG 1331 if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) { 1323 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP << 1324 goto out; << 1325 #endif << 1326 vm_flags |= VM_UFFD_WP; 1332 vm_flags |= VM_UFFD_WP; 1327 } !! 1333 /* 1328 if (uffdio_register.mode & UFFDIO_REG !! 1334 * FIXME: remove the below error constraint by 1329 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR !! 1335 * implementing the wprotect tracking mode. >> 1336 */ >> 1337 ret = -EINVAL; 1330 goto out; 1338 goto out; 1331 #endif << 1332 vm_flags |= VM_UFFD_MINOR; << 1333 } 1339 } 1334 1340 1335 ret = validate_range(mm, uffdio_regis !! 1341 ret = validate_range(mm, &uffdio_register.range.start, 1336 uffdio_register. 1342 uffdio_register.range.len); 1337 if (ret) 1343 if (ret) 1338 goto out; 1344 goto out; 1339 1345 1340 start = uffdio_register.range.start; 1346 start = uffdio_register.range.start; 1341 end = start + uffdio_register.range.l 1347 end = start + uffdio_register.range.len; 1342 1348 1343 ret = -ENOMEM; 1349 ret = -ENOMEM; 1344 if (!mmget_not_zero(mm)) 1350 if (!mmget_not_zero(mm)) 1345 goto out; 1351 goto out; 1346 1352 1347 ret = -EINVAL; !! 1353 down_write(&mm->mmap_sem); 1348 mmap_write_lock(mm); !! 1354 if (!mmget_still_valid(mm)) 1349 vma_iter_init(&vmi, mm, start); !! 1355 goto out_unlock; 1350 vma = vma_find(&vmi, end); !! 1356 vma = find_vma_prev(mm, start, &prev); 1351 if (!vma) 1357 if (!vma) 1352 goto out_unlock; 1358 goto out_unlock; 1353 1359 >> 1360 /* check that there's at least one vma in the range */ >> 1361 ret = -EINVAL; >> 1362 if (vma->vm_start >= end) >> 1363 goto out_unlock; >> 1364 1354 /* 1365 /* 1355 * If the first vma contains huge pag 1366 * If the first vma contains huge pages, make sure start address 1356 * is aligned to huge page size. 1367 * is aligned to huge page size. 1357 */ 1368 */ 1358 if (is_vm_hugetlb_page(vma)) { 1369 if (is_vm_hugetlb_page(vma)) { 1359 unsigned long vma_hpagesize = 1370 unsigned long vma_hpagesize = vma_kernel_pagesize(vma); 1360 1371 1361 if (start & (vma_hpagesize - 1372 if (start & (vma_hpagesize - 1)) 1362 goto out_unlock; 1373 goto out_unlock; 1363 } 1374 } 1364 1375 1365 /* 1376 /* 1366 * Search for not compatible vmas. 1377 * Search for not compatible vmas. 1367 */ 1378 */ 1368 found = false; 1379 found = false; 1369 basic_ioctls = false; 1380 basic_ioctls = false; 1370 cur = vma; !! 1381 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) { 1371 do { << 1372 cond_resched(); 1382 cond_resched(); 1373 1383 1374 BUG_ON(!!cur->vm_userfaultfd_ 1384 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^ 1375 !!(cur->vm_flags & __V !! 1385 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP))); 1376 1386 1377 /* check not compatible vmas 1387 /* check not compatible vmas */ 1378 ret = -EINVAL; 1388 ret = -EINVAL; 1379 if (!vma_can_userfault(cur, v !! 1389 if (!vma_can_userfault(cur)) 1380 goto out_unlock; 1390 goto out_unlock; 1381 1391 1382 /* 1392 /* 1383 * UFFDIO_COPY will fill file 1393 * UFFDIO_COPY will fill file holes even without 1384 * PROT_WRITE. This check enf 1394 * PROT_WRITE. This check enforces that if this is a 1385 * MAP_SHARED, the process ha 1395 * MAP_SHARED, the process has write permission to the backing 1386 * file. If VM_MAYWRITE is se 1396 * file. If VM_MAYWRITE is set it also enforces that on a 1387 * MAP_SHARED vma: there is n 1397 * MAP_SHARED vma: there is no F_WRITE_SEAL and no further 1388 * F_WRITE_SEAL can be taken 1398 * F_WRITE_SEAL can be taken until the vma is destroyed. 1389 */ 1399 */ 1390 ret = -EPERM; 1400 ret = -EPERM; 1391 if (unlikely(!(cur->vm_flags 1401 if (unlikely(!(cur->vm_flags & VM_MAYWRITE))) 1392 goto out_unlock; 1402 goto out_unlock; 1393 1403 1394 /* 1404 /* 1395 * If this vma contains endin 1405 * If this vma contains ending address, and huge pages 1396 * check alignment. 1406 * check alignment. 1397 */ 1407 */ 1398 if (is_vm_hugetlb_page(cur) & 1408 if (is_vm_hugetlb_page(cur) && end <= cur->vm_end && 1399 end > cur->vm_start) { 1409 end > cur->vm_start) { 1400 unsigned long vma_hpa 1410 unsigned long vma_hpagesize = vma_kernel_pagesize(cur); 1401 1411 1402 ret = -EINVAL; 1412 ret = -EINVAL; 1403 1413 1404 if (end & (vma_hpages 1414 if (end & (vma_hpagesize - 1)) 1405 goto out_unlo 1415 goto out_unlock; 1406 } 1416 } 1407 if ((vm_flags & VM_UFFD_WP) & << 1408 goto out_unlock; << 1409 1417 1410 /* 1418 /* 1411 * Check that this vma isn't 1419 * Check that this vma isn't already owned by a 1412 * different userfaultfd. We 1420 * different userfaultfd. We can't allow more than one 1413 * userfaultfd to own a singl 1421 * userfaultfd to own a single vma simultaneously or we 1414 * wouldn't know which one to 1422 * wouldn't know which one to deliver the userfaults to. 1415 */ 1423 */ 1416 ret = -EBUSY; 1424 ret = -EBUSY; 1417 if (cur->vm_userfaultfd_ctx.c 1425 if (cur->vm_userfaultfd_ctx.ctx && 1418 cur->vm_userfaultfd_ctx.c 1426 cur->vm_userfaultfd_ctx.ctx != ctx) 1419 goto out_unlock; 1427 goto out_unlock; 1420 1428 1421 /* 1429 /* 1422 * Note vmas containing huge 1430 * Note vmas containing huge pages 1423 */ 1431 */ 1424 if (is_vm_hugetlb_page(cur)) 1432 if (is_vm_hugetlb_page(cur)) 1425 basic_ioctls = true; 1433 basic_ioctls = true; 1426 1434 1427 found = true; 1435 found = true; 1428 } for_each_vma_range(vmi, cur, end); !! 1436 } 1429 BUG_ON(!found); 1437 BUG_ON(!found); 1430 1438 1431 vma_iter_set(&vmi, start); << 1432 prev = vma_prev(&vmi); << 1433 if (vma->vm_start < start) 1439 if (vma->vm_start < start) 1434 prev = vma; 1440 prev = vma; 1435 1441 1436 ret = 0; 1442 ret = 0; 1437 for_each_vma_range(vmi, vma, end) { !! 1443 do { 1438 cond_resched(); 1444 cond_resched(); 1439 1445 1440 BUG_ON(!vma_can_userfault(vma !! 1446 BUG_ON(!vma_can_userfault(vma)); 1441 BUG_ON(vma->vm_userfaultfd_ct 1447 BUG_ON(vma->vm_userfaultfd_ctx.ctx && 1442 vma->vm_userfaultfd_ct 1448 vma->vm_userfaultfd_ctx.ctx != ctx); 1443 WARN_ON(!(vma->vm_flags & VM_ 1449 WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); 1444 1450 1445 /* 1451 /* 1446 * Nothing to do: this vma is 1452 * Nothing to do: this vma is already registered into this 1447 * userfaultfd and with the r 1453 * userfaultfd and with the right tracking mode too. 1448 */ 1454 */ 1449 if (vma->vm_userfaultfd_ctx.c 1455 if (vma->vm_userfaultfd_ctx.ctx == ctx && 1450 (vma->vm_flags & vm_flags 1456 (vma->vm_flags & vm_flags) == vm_flags) 1451 goto skip; 1457 goto skip; 1452 1458 1453 if (vma->vm_start > start) 1459 if (vma->vm_start > start) 1454 start = vma->vm_start 1460 start = vma->vm_start; 1455 vma_end = min(end, vma->vm_en 1461 vma_end = min(end, vma->vm_end); 1456 1462 1457 new_flags = (vma->vm_flags & !! 1463 new_flags = (vma->vm_flags & 1458 vma = vma_modify_flags_uffd(& !! 1464 ~(VM_UFFD_MISSING|VM_UFFD_WP)) | vm_flags; 1459 n !! 1465 prev = vma_merge(mm, prev, start, vma_end, new_flags, 1460 ( !! 1466 vma->anon_vma, vma->vm_file, vma->vm_pgoff, 1461 if (IS_ERR(vma)) { !! 1467 vma_policy(vma), 1462 ret = PTR_ERR(vma); !! 1468 ((struct vm_userfaultfd_ctx){ ctx })); 1463 break; !! 1469 if (prev) { >> 1470 vma = prev; >> 1471 goto next; 1464 } 1472 } 1465 !! 1473 if (vma->vm_start < start) { >> 1474 ret = split_vma(mm, vma, start, 1); >> 1475 if (ret) >> 1476 break; >> 1477 } >> 1478 if (vma->vm_end > end) { >> 1479 ret = split_vma(mm, vma, end, 0); >> 1480 if (ret) >> 1481 break; >> 1482 } >> 1483 next: 1466 /* 1484 /* 1467 * In the vma_merge() success 1485 * In the vma_merge() successful mprotect-like case 8: 1468 * the next vma was merged in 1486 * the next vma was merged into the current one and 1469 * the current one has not be 1487 * the current one has not been updated yet. 1470 */ 1488 */ 1471 vma_start_write(vma); !! 1489 vma->vm_flags = new_flags; 1472 userfaultfd_set_vm_flags(vma, << 1473 vma->vm_userfaultfd_ctx.ctx = 1490 vma->vm_userfaultfd_ctx.ctx = ctx; 1474 1491 1475 if (is_vm_hugetlb_page(vma) & << 1476 hugetlb_unshare_all_p << 1477 << 1478 skip: 1492 skip: 1479 prev = vma; 1493 prev = vma; 1480 start = vma->vm_end; 1494 start = vma->vm_end; 1481 } !! 1495 vma = vma->vm_next; 1482 !! 1496 } while (vma && vma->vm_start < end); 1483 out_unlock: 1497 out_unlock: 1484 mmap_write_unlock(mm); !! 1498 up_write(&mm->mmap_sem); 1485 mmput(mm); 1499 mmput(mm); 1486 if (!ret) { 1500 if (!ret) { 1487 __u64 ioctls_out; << 1488 << 1489 ioctls_out = basic_ioctls ? U << 1490 UFFD_API_RANGE_IOCTLS; << 1491 << 1492 /* << 1493 * Declare the WP ioctl only << 1494 * specified and all checks p << 1495 */ << 1496 if (!(uffdio_register.mode & << 1497 ioctls_out &= ~((__u6 << 1498 << 1499 /* CONTINUE ioctl is only sup << 1500 if (!(uffdio_register.mode & << 1501 ioctls_out &= ~((__u6 << 1502 << 1503 /* 1501 /* 1504 * Now that we scanned all vm 1502 * Now that we scanned all vmas we can already tell 1505 * userland which ioctls meth 1503 * userland which ioctls methods are guaranteed to 1506 * succeed on this range. 1504 * succeed on this range. 1507 */ 1505 */ 1508 if (put_user(ioctls_out, &use !! 1506 if (put_user(basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC : >> 1507 UFFD_API_RANGE_IOCTLS, >> 1508 &user_uffdio_register->ioctls)) 1509 ret = -EFAULT; 1509 ret = -EFAULT; 1510 } 1510 } 1511 out: 1511 out: 1512 return ret; 1512 return ret; 1513 } 1513 } 1514 1514 1515 static int userfaultfd_unregister(struct user 1515 static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, 1516 unsigned lo 1516 unsigned long arg) 1517 { 1517 { 1518 struct mm_struct *mm = ctx->mm; 1518 struct mm_struct *mm = ctx->mm; 1519 struct vm_area_struct *vma, *prev, *c 1519 struct vm_area_struct *vma, *prev, *cur; 1520 int ret; 1520 int ret; 1521 struct uffdio_range uffdio_unregister 1521 struct uffdio_range uffdio_unregister; 1522 unsigned long new_flags; 1522 unsigned long new_flags; 1523 bool found; 1523 bool found; 1524 unsigned long start, end, vma_end; 1524 unsigned long start, end, vma_end; 1525 const void __user *buf = (void __user 1525 const void __user *buf = (void __user *)arg; 1526 struct vma_iterator vmi; << 1527 bool wp_async = userfaultfd_wp_async_ << 1528 1526 1529 ret = -EFAULT; 1527 ret = -EFAULT; 1530 if (copy_from_user(&uffdio_unregister 1528 if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister))) 1531 goto out; 1529 goto out; 1532 1530 1533 ret = validate_range(mm, uffdio_unreg !! 1531 ret = validate_range(mm, &uffdio_unregister.start, 1534 uffdio_unregiste 1532 uffdio_unregister.len); 1535 if (ret) 1533 if (ret) 1536 goto out; 1534 goto out; 1537 1535 1538 start = uffdio_unregister.start; 1536 start = uffdio_unregister.start; 1539 end = start + uffdio_unregister.len; 1537 end = start + uffdio_unregister.len; 1540 1538 1541 ret = -ENOMEM; 1539 ret = -ENOMEM; 1542 if (!mmget_not_zero(mm)) 1540 if (!mmget_not_zero(mm)) 1543 goto out; 1541 goto out; 1544 1542 1545 mmap_write_lock(mm); !! 1543 down_write(&mm->mmap_sem); 1546 ret = -EINVAL; !! 1544 if (!mmget_still_valid(mm)) 1547 vma_iter_init(&vmi, mm, start); !! 1545 goto out_unlock; 1548 vma = vma_find(&vmi, end); !! 1546 vma = find_vma_prev(mm, start, &prev); 1549 if (!vma) 1547 if (!vma) 1550 goto out_unlock; 1548 goto out_unlock; 1551 1549 >> 1550 /* check that there's at least one vma in the range */ >> 1551 ret = -EINVAL; >> 1552 if (vma->vm_start >= end) >> 1553 goto out_unlock; >> 1554 1552 /* 1555 /* 1553 * If the first vma contains huge pag 1556 * If the first vma contains huge pages, make sure start address 1554 * is aligned to huge page size. 1557 * is aligned to huge page size. 1555 */ 1558 */ 1556 if (is_vm_hugetlb_page(vma)) { 1559 if (is_vm_hugetlb_page(vma)) { 1557 unsigned long vma_hpagesize = 1560 unsigned long vma_hpagesize = vma_kernel_pagesize(vma); 1558 1561 1559 if (start & (vma_hpagesize - 1562 if (start & (vma_hpagesize - 1)) 1560 goto out_unlock; 1563 goto out_unlock; 1561 } 1564 } 1562 1565 1563 /* 1566 /* 1564 * Search for not compatible vmas. 1567 * Search for not compatible vmas. 1565 */ 1568 */ 1566 found = false; 1569 found = false; 1567 cur = vma; !! 1570 ret = -EINVAL; 1568 do { !! 1571 for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) { 1569 cond_resched(); 1572 cond_resched(); 1570 1573 1571 BUG_ON(!!cur->vm_userfaultfd_ 1574 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^ 1572 !!(cur->vm_flags & __V !! 1575 !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP))); 1573 1576 1574 /* 1577 /* 1575 * Check not compatible vmas, 1578 * Check not compatible vmas, not strictly required 1576 * here as not compatible vma 1579 * here as not compatible vmas cannot have an 1577 * userfaultfd_ctx registered 1580 * userfaultfd_ctx registered on them, but this 1578 * provides for more strict b 1581 * provides for more strict behavior to notice 1579 * unregistration errors. 1582 * unregistration errors. 1580 */ 1583 */ 1581 if (!vma_can_userfault(cur, c !! 1584 if (!vma_can_userfault(cur)) 1582 goto out_unlock; 1585 goto out_unlock; 1583 1586 1584 found = true; 1587 found = true; 1585 } for_each_vma_range(vmi, cur, end); !! 1588 } 1586 BUG_ON(!found); 1589 BUG_ON(!found); 1587 1590 1588 vma_iter_set(&vmi, start); << 1589 prev = vma_prev(&vmi); << 1590 if (vma->vm_start < start) 1591 if (vma->vm_start < start) 1591 prev = vma; 1592 prev = vma; 1592 1593 1593 ret = 0; 1594 ret = 0; 1594 for_each_vma_range(vmi, vma, end) { !! 1595 do { 1595 cond_resched(); 1596 cond_resched(); 1596 1597 1597 BUG_ON(!vma_can_userfault(vma !! 1598 BUG_ON(!vma_can_userfault(vma)); 1598 1599 1599 /* 1600 /* 1600 * Nothing to do: this vma is 1601 * Nothing to do: this vma is already registered into this 1601 * userfaultfd and with the r 1602 * userfaultfd and with the right tracking mode too. 1602 */ 1603 */ 1603 if (!vma->vm_userfaultfd_ctx. 1604 if (!vma->vm_userfaultfd_ctx.ctx) 1604 goto skip; 1605 goto skip; 1605 1606 1606 WARN_ON(!(vma->vm_flags & VM_ 1607 WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); 1607 1608 1608 if (vma->vm_start > start) 1609 if (vma->vm_start > start) 1609 start = vma->vm_start 1610 start = vma->vm_start; 1610 vma_end = min(end, vma->vm_en 1611 vma_end = min(end, vma->vm_end); 1611 1612 1612 if (userfaultfd_missing(vma)) 1613 if (userfaultfd_missing(vma)) { 1613 /* 1614 /* 1614 * Wake any concurren 1615 * Wake any concurrent pending userfault while 1615 * we unregister, so 1616 * we unregister, so they will not hang 1616 * permanently and it 1617 * permanently and it avoids userland to call 1617 * UFFDIO_WAKE explic 1618 * UFFDIO_WAKE explicitly. 1618 */ 1619 */ 1619 struct userfaultfd_wa 1620 struct userfaultfd_wake_range range; 1620 range.start = start; 1621 range.start = start; 1621 range.len = vma_end - 1622 range.len = vma_end - start; 1622 wake_userfault(vma->v 1623 wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range); 1623 } 1624 } 1624 1625 1625 /* Reset ptes for the whole v !! 1626 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP); 1626 if (userfaultfd_wp(vma)) !! 1627 prev = vma_merge(mm, prev, start, vma_end, new_flags, 1627 uffd_wp_range(vma, st !! 1628 vma->anon_vma, vma->vm_file, vma->vm_pgoff, 1628 !! 1629 vma_policy(vma), 1629 new_flags = vma->vm_flags & ~ !! 1630 NULL_VM_UFFD_CTX); 1630 vma = vma_modify_flags_uffd(& !! 1631 if (prev) { 1631 n !! 1632 vma = prev; 1632 if (IS_ERR(vma)) { !! 1633 goto next; 1633 ret = PTR_ERR(vma); << 1634 break; << 1635 } 1634 } 1636 !! 1635 if (vma->vm_start < start) { >> 1636 ret = split_vma(mm, vma, start, 1); >> 1637 if (ret) >> 1638 break; >> 1639 } >> 1640 if (vma->vm_end > end) { >> 1641 ret = split_vma(mm, vma, end, 0); >> 1642 if (ret) >> 1643 break; >> 1644 } >> 1645 next: 1637 /* 1646 /* 1638 * In the vma_merge() success 1647 * In the vma_merge() successful mprotect-like case 8: 1639 * the next vma was merged in 1648 * the next vma was merged into the current one and 1640 * the current one has not be 1649 * the current one has not been updated yet. 1641 */ 1650 */ 1642 vma_start_write(vma); !! 1651 vma->vm_flags = new_flags; 1643 userfaultfd_set_vm_flags(vma, << 1644 vma->vm_userfaultfd_ctx = NUL 1652 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; 1645 1653 1646 skip: 1654 skip: 1647 prev = vma; 1655 prev = vma; 1648 start = vma->vm_end; 1656 start = vma->vm_end; 1649 } !! 1657 vma = vma->vm_next; 1650 !! 1658 } while (vma && vma->vm_start < end); 1651 out_unlock: 1659 out_unlock: 1652 mmap_write_unlock(mm); !! 1660 up_write(&mm->mmap_sem); 1653 mmput(mm); 1661 mmput(mm); 1654 out: 1662 out: 1655 return ret; 1663 return ret; 1656 } 1664 } 1657 1665 1658 /* 1666 /* 1659 * userfaultfd_wake may be used in combinatio 1667 * userfaultfd_wake may be used in combination with the 1660 * UFFDIO_*_MODE_DONTWAKE to wakeup userfault 1668 * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches. 1661 */ 1669 */ 1662 static int userfaultfd_wake(struct userfaultf 1670 static int userfaultfd_wake(struct userfaultfd_ctx *ctx, 1663 unsigned long arg 1671 unsigned long arg) 1664 { 1672 { 1665 int ret; 1673 int ret; 1666 struct uffdio_range uffdio_wake; 1674 struct uffdio_range uffdio_wake; 1667 struct userfaultfd_wake_range range; 1675 struct userfaultfd_wake_range range; 1668 const void __user *buf = (void __user 1676 const void __user *buf = (void __user *)arg; 1669 1677 1670 ret = -EFAULT; 1678 ret = -EFAULT; 1671 if (copy_from_user(&uffdio_wake, buf, 1679 if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake))) 1672 goto out; 1680 goto out; 1673 1681 1674 ret = validate_range(ctx->mm, uffdio_ !! 1682 ret = validate_range(ctx->mm, &uffdio_wake.start, uffdio_wake.len); 1675 if (ret) 1683 if (ret) 1676 goto out; 1684 goto out; 1677 1685 1678 range.start = uffdio_wake.start; 1686 range.start = uffdio_wake.start; 1679 range.len = uffdio_wake.len; 1687 range.len = uffdio_wake.len; 1680 1688 1681 /* 1689 /* 1682 * len == 0 means wake all and we don 1690 * len == 0 means wake all and we don't want to wake all here, 1683 * so check it again to be sure. 1691 * so check it again to be sure. 1684 */ 1692 */ 1685 VM_BUG_ON(!range.len); 1693 VM_BUG_ON(!range.len); 1686 1694 1687 wake_userfault(ctx, &range); 1695 wake_userfault(ctx, &range); 1688 ret = 0; 1696 ret = 0; 1689 1697 1690 out: 1698 out: 1691 return ret; 1699 return ret; 1692 } 1700 } 1693 1701 1694 static int userfaultfd_copy(struct userfaultf 1702 static int userfaultfd_copy(struct userfaultfd_ctx *ctx, 1695 unsigned long arg 1703 unsigned long arg) 1696 { 1704 { 1697 __s64 ret; 1705 __s64 ret; 1698 struct uffdio_copy uffdio_copy; 1706 struct uffdio_copy uffdio_copy; 1699 struct uffdio_copy __user *user_uffdi 1707 struct uffdio_copy __user *user_uffdio_copy; 1700 struct userfaultfd_wake_range range; 1708 struct userfaultfd_wake_range range; 1701 uffd_flags_t flags = 0; << 1702 1709 1703 user_uffdio_copy = (struct uffdio_cop 1710 user_uffdio_copy = (struct uffdio_copy __user *) arg; 1704 1711 1705 ret = -EAGAIN; 1712 ret = -EAGAIN; 1706 if (atomic_read(&ctx->mmap_changing)) !! 1713 if (READ_ONCE(ctx->mmap_changing)) 1707 goto out; 1714 goto out; 1708 1715 1709 ret = -EFAULT; 1716 ret = -EFAULT; 1710 if (copy_from_user(&uffdio_copy, user 1717 if (copy_from_user(&uffdio_copy, user_uffdio_copy, 1711 /* don't copy "cop 1718 /* don't copy "copy" last field */ 1712 sizeof(uffdio_copy 1719 sizeof(uffdio_copy)-sizeof(__s64))) 1713 goto out; 1720 goto out; 1714 1721 1715 ret = validate_unaligned_range(ctx->m !! 1722 ret = validate_range(ctx->mm, &uffdio_copy.dst, uffdio_copy.len); 1716 uffdio << 1717 if (ret) 1723 if (ret) 1718 goto out; 1724 goto out; 1719 ret = validate_range(ctx->mm, uffdio_ !! 1725 /* 1720 if (ret) !! 1726 * double check for wraparound just in case. copy_from_user() 1721 goto out; !! 1727 * will later check uffdio_copy.src + uffdio_copy.len to fit 1722 !! 1728 * in the userland range. >> 1729 */ 1723 ret = -EINVAL; 1730 ret = -EINVAL; 1724 if (uffdio_copy.mode & ~(UFFDIO_COPY_ !! 1731 if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src) >> 1732 goto out; >> 1733 if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE) 1725 goto out; 1734 goto out; 1726 if (uffdio_copy.mode & UFFDIO_COPY_MO << 1727 flags |= MFILL_ATOMIC_WP; << 1728 if (mmget_not_zero(ctx->mm)) { 1735 if (mmget_not_zero(ctx->mm)) { 1729 ret = mfill_atomic_copy(ctx, !! 1736 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src, 1730 uffdi !! 1737 uffdio_copy.len, &ctx->mmap_changing); 1731 mmput(ctx->mm); 1738 mmput(ctx->mm); 1732 } else { 1739 } else { 1733 return -ESRCH; 1740 return -ESRCH; 1734 } 1741 } 1735 if (unlikely(put_user(ret, &user_uffd 1742 if (unlikely(put_user(ret, &user_uffdio_copy->copy))) 1736 return -EFAULT; 1743 return -EFAULT; 1737 if (ret < 0) 1744 if (ret < 0) 1738 goto out; 1745 goto out; 1739 BUG_ON(!ret); 1746 BUG_ON(!ret); 1740 /* len == 0 would wake all */ 1747 /* len == 0 would wake all */ 1741 range.len = ret; 1748 range.len = ret; 1742 if (!(uffdio_copy.mode & UFFDIO_COPY_ 1749 if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) { 1743 range.start = uffdio_copy.dst 1750 range.start = uffdio_copy.dst; 1744 wake_userfault(ctx, &range); 1751 wake_userfault(ctx, &range); 1745 } 1752 } 1746 ret = range.len == uffdio_copy.len ? 1753 ret = range.len == uffdio_copy.len ? 0 : -EAGAIN; 1747 out: 1754 out: 1748 return ret; 1755 return ret; 1749 } 1756 } 1750 1757 1751 static int userfaultfd_zeropage(struct userfa 1758 static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx, 1752 unsigned long 1759 unsigned long arg) 1753 { 1760 { 1754 __s64 ret; 1761 __s64 ret; 1755 struct uffdio_zeropage uffdio_zeropag 1762 struct uffdio_zeropage uffdio_zeropage; 1756 struct uffdio_zeropage __user *user_u 1763 struct uffdio_zeropage __user *user_uffdio_zeropage; 1757 struct userfaultfd_wake_range range; 1764 struct userfaultfd_wake_range range; 1758 1765 1759 user_uffdio_zeropage = (struct uffdio 1766 user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg; 1760 1767 1761 ret = -EAGAIN; 1768 ret = -EAGAIN; 1762 if (atomic_read(&ctx->mmap_changing)) !! 1769 if (READ_ONCE(ctx->mmap_changing)) 1763 goto out; 1770 goto out; 1764 1771 1765 ret = -EFAULT; 1772 ret = -EFAULT; 1766 if (copy_from_user(&uffdio_zeropage, 1773 if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage, 1767 /* don't copy "zer 1774 /* don't copy "zeropage" last field */ 1768 sizeof(uffdio_zero 1775 sizeof(uffdio_zeropage)-sizeof(__s64))) 1769 goto out; 1776 goto out; 1770 1777 1771 ret = validate_range(ctx->mm, uffdio_ !! 1778 ret = validate_range(ctx->mm, &uffdio_zeropage.range.start, 1772 uffdio_zeropage. 1779 uffdio_zeropage.range.len); 1773 if (ret) 1780 if (ret) 1774 goto out; 1781 goto out; 1775 ret = -EINVAL; 1782 ret = -EINVAL; 1776 if (uffdio_zeropage.mode & ~UFFDIO_ZE 1783 if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE) 1777 goto out; 1784 goto out; 1778 1785 1779 if (mmget_not_zero(ctx->mm)) { 1786 if (mmget_not_zero(ctx->mm)) { 1780 ret = mfill_atomic_zeropage(c !! 1787 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start, 1781 uf !! 1788 uffdio_zeropage.range.len, >> 1789 &ctx->mmap_changing); 1782 mmput(ctx->mm); 1790 mmput(ctx->mm); 1783 } else { 1791 } else { 1784 return -ESRCH; 1792 return -ESRCH; 1785 } 1793 } 1786 if (unlikely(put_user(ret, &user_uffd 1794 if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage))) 1787 return -EFAULT; 1795 return -EFAULT; 1788 if (ret < 0) 1796 if (ret < 0) 1789 goto out; 1797 goto out; 1790 /* len == 0 would wake all */ 1798 /* len == 0 would wake all */ 1791 BUG_ON(!ret); 1799 BUG_ON(!ret); 1792 range.len = ret; 1800 range.len = ret; 1793 if (!(uffdio_zeropage.mode & UFFDIO_Z 1801 if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) { 1794 range.start = uffdio_zeropage 1802 range.start = uffdio_zeropage.range.start; 1795 wake_userfault(ctx, &range); 1803 wake_userfault(ctx, &range); 1796 } 1804 } 1797 ret = range.len == uffdio_zeropage.ra 1805 ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN; 1798 out: 1806 out: 1799 return ret; 1807 return ret; 1800 } 1808 } 1801 1809 1802 static int userfaultfd_writeprotect(struct us << 1803 unsigned << 1804 { << 1805 int ret; << 1806 struct uffdio_writeprotect uffdio_wp; << 1807 struct uffdio_writeprotect __user *us << 1808 struct userfaultfd_wake_range range; << 1809 bool mode_wp, mode_dontwake; << 1810 << 1811 if (atomic_read(&ctx->mmap_changing)) << 1812 return -EAGAIN; << 1813 << 1814 user_uffdio_wp = (struct uffdio_write << 1815 << 1816 if (copy_from_user(&uffdio_wp, user_u << 1817 sizeof(struct uffd << 1818 return -EFAULT; << 1819 << 1820 ret = validate_range(ctx->mm, uffdio_ << 1821 uffdio_wp.range. << 1822 if (ret) << 1823 return ret; << 1824 << 1825 if (uffdio_wp.mode & ~(UFFDIO_WRITEPR << 1826 UFFDIO_WRITEPR << 1827 return -EINVAL; << 1828 << 1829 mode_wp = uffdio_wp.mode & UFFDIO_WRI << 1830 mode_dontwake = uffdio_wp.mode & UFFD << 1831 << 1832 if (mode_wp && mode_dontwake) << 1833 return -EINVAL; << 1834 << 1835 if (mmget_not_zero(ctx->mm)) { << 1836 ret = mwriteprotect_range(ctx << 1837 uff << 1838 mmput(ctx->mm); << 1839 } else { << 1840 return -ESRCH; << 1841 } << 1842 << 1843 if (ret) << 1844 return ret; << 1845 << 1846 if (!mode_wp && !mode_dontwake) { << 1847 range.start = uffdio_wp.range << 1848 range.len = uffdio_wp.range.l << 1849 wake_userfault(ctx, &range); << 1850 } << 1851 return ret; << 1852 } << 1853 << 1854 static int userfaultfd_continue(struct userfa << 1855 { << 1856 __s64 ret; << 1857 struct uffdio_continue uffdio_continu << 1858 struct uffdio_continue __user *user_u << 1859 struct userfaultfd_wake_range range; << 1860 uffd_flags_t flags = 0; << 1861 << 1862 user_uffdio_continue = (struct uffdio << 1863 << 1864 ret = -EAGAIN; << 1865 if (atomic_read(&ctx->mmap_changing)) << 1866 goto out; << 1867 << 1868 ret = -EFAULT; << 1869 if (copy_from_user(&uffdio_continue, << 1870 /* don't copy the << 1871 sizeof(uffdio_cont << 1872 goto out; << 1873 << 1874 ret = validate_range(ctx->mm, uffdio_ << 1875 uffdio_continue. << 1876 if (ret) << 1877 goto out; << 1878 << 1879 ret = -EINVAL; << 1880 if (uffdio_continue.mode & ~(UFFDIO_C << 1881 UFFDIO_C << 1882 goto out; << 1883 if (uffdio_continue.mode & UFFDIO_CON << 1884 flags |= MFILL_ATOMIC_WP; << 1885 << 1886 if (mmget_not_zero(ctx->mm)) { << 1887 ret = mfill_atomic_continue(c << 1888 u << 1889 mmput(ctx->mm); << 1890 } else { << 1891 return -ESRCH; << 1892 } << 1893 << 1894 if (unlikely(put_user(ret, &user_uffd << 1895 return -EFAULT; << 1896 if (ret < 0) << 1897 goto out; << 1898 << 1899 /* len == 0 would wake all */ << 1900 BUG_ON(!ret); << 1901 range.len = ret; << 1902 if (!(uffdio_continue.mode & UFFDIO_C << 1903 range.start = uffdio_continue << 1904 wake_userfault(ctx, &range); << 1905 } << 1906 ret = range.len == uffdio_continue.ra << 1907 << 1908 out: << 1909 return ret; << 1910 } << 1911 << 1912 static inline int userfaultfd_poison(struct u << 1913 { << 1914 __s64 ret; << 1915 struct uffdio_poison uffdio_poison; << 1916 struct uffdio_poison __user *user_uff << 1917 struct userfaultfd_wake_range range; << 1918 << 1919 user_uffdio_poison = (struct uffdio_p << 1920 << 1921 ret = -EAGAIN; << 1922 if (atomic_read(&ctx->mmap_changing)) << 1923 goto out; << 1924 << 1925 ret = -EFAULT; << 1926 if (copy_from_user(&uffdio_poison, us << 1927 /* don't copy the << 1928 sizeof(uffdio_pois << 1929 goto out; << 1930 << 1931 ret = validate_range(ctx->mm, uffdio_ << 1932 uffdio_poison.ra << 1933 if (ret) << 1934 goto out; << 1935 << 1936 ret = -EINVAL; << 1937 if (uffdio_poison.mode & ~UFFDIO_POIS << 1938 goto out; << 1939 << 1940 if (mmget_not_zero(ctx->mm)) { << 1941 ret = mfill_atomic_poison(ctx << 1942 uff << 1943 mmput(ctx->mm); << 1944 } else { << 1945 return -ESRCH; << 1946 } << 1947 << 1948 if (unlikely(put_user(ret, &user_uffd << 1949 return -EFAULT; << 1950 if (ret < 0) << 1951 goto out; << 1952 << 1953 /* len == 0 would wake all */ << 1954 BUG_ON(!ret); << 1955 range.len = ret; << 1956 if (!(uffdio_poison.mode & UFFDIO_POI << 1957 range.start = uffdio_poison.r << 1958 wake_userfault(ctx, &range); << 1959 } << 1960 ret = range.len == uffdio_poison.rang << 1961 << 1962 out: << 1963 return ret; << 1964 } << 1965 << 1966 bool userfaultfd_wp_async(struct vm_area_stru << 1967 { << 1968 return userfaultfd_wp_async_ctx(vma-> << 1969 } << 1970 << 1971 static inline unsigned int uffd_ctx_features( 1810 static inline unsigned int uffd_ctx_features(__u64 user_features) 1972 { 1811 { 1973 /* 1812 /* 1974 * For the current set of features th !! 1813 * For the current set of features the bits just coincide 1975 * UFFD_FEATURE_INITIALIZED to mark t << 1976 */ 1814 */ 1977 return (unsigned int)user_features | !! 1815 return (unsigned int)user_features; 1978 } << 1979 << 1980 static int userfaultfd_move(struct userfaultf << 1981 unsigned long arg << 1982 { << 1983 __s64 ret; << 1984 struct uffdio_move uffdio_move; << 1985 struct uffdio_move __user *user_uffdi << 1986 struct userfaultfd_wake_range range; << 1987 struct mm_struct *mm = ctx->mm; << 1988 << 1989 user_uffdio_move = (struct uffdio_mov << 1990 << 1991 if (atomic_read(&ctx->mmap_changing)) << 1992 return -EAGAIN; << 1993 << 1994 if (copy_from_user(&uffdio_move, user << 1995 /* don't copy "mov << 1996 sizeof(uffdio_move << 1997 return -EFAULT; << 1998 << 1999 /* Do not allow cross-mm moves. */ << 2000 if (mm != current->mm) << 2001 return -EINVAL; << 2002 << 2003 ret = validate_range(mm, uffdio_move. << 2004 if (ret) << 2005 return ret; << 2006 << 2007 ret = validate_range(mm, uffdio_move. << 2008 if (ret) << 2009 return ret; << 2010 << 2011 if (uffdio_move.mode & ~(UFFDIO_MOVE_ << 2012 UFFDIO_MOVE << 2013 return -EINVAL; << 2014 << 2015 if (mmget_not_zero(mm)) { << 2016 ret = move_pages(ctx, uffdio_ << 2017 uffdio_move. << 2018 mmput(mm); << 2019 } else { << 2020 return -ESRCH; << 2021 } << 2022 << 2023 if (unlikely(put_user(ret, &user_uffd << 2024 return -EFAULT; << 2025 if (ret < 0) << 2026 goto out; << 2027 << 2028 /* len == 0 would wake all */ << 2029 VM_WARN_ON(!ret); << 2030 range.len = ret; << 2031 if (!(uffdio_move.mode & UFFDIO_MOVE_ << 2032 range.start = uffdio_move.dst << 2033 wake_userfault(ctx, &range); << 2034 } << 2035 ret = range.len == uffdio_move.len ? << 2036 << 2037 out: << 2038 return ret; << 2039 } 1816 } 2040 1817 2041 /* 1818 /* 2042 * userland asks for a certain API version an 1819 * userland asks for a certain API version and we return which bits 2043 * and ioctl commands are implemented in this 1820 * and ioctl commands are implemented in this kernel for such API 2044 * version or -EINVAL if unknown. 1821 * version or -EINVAL if unknown. 2045 */ 1822 */ 2046 static int userfaultfd_api(struct userfaultfd 1823 static int userfaultfd_api(struct userfaultfd_ctx *ctx, 2047 unsigned long arg) 1824 unsigned long arg) 2048 { 1825 { 2049 struct uffdio_api uffdio_api; 1826 struct uffdio_api uffdio_api; 2050 void __user *buf = (void __user *)arg 1827 void __user *buf = (void __user *)arg; 2051 unsigned int ctx_features; << 2052 int ret; 1828 int ret; 2053 __u64 features; 1829 __u64 features; 2054 1830 >> 1831 ret = -EINVAL; >> 1832 if (ctx->state != UFFD_STATE_WAIT_API) >> 1833 goto out; 2055 ret = -EFAULT; 1834 ret = -EFAULT; 2056 if (copy_from_user(&uffdio_api, buf, 1835 if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api))) 2057 goto out; 1836 goto out; 2058 features = uffdio_api.features; 1837 features = uffdio_api.features; 2059 ret = -EINVAL; 1838 ret = -EINVAL; 2060 if (uffdio_api.api != UFFD_API) !! 1839 if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) 2061 goto err_out; 1840 goto err_out; 2062 ret = -EPERM; 1841 ret = -EPERM; 2063 if ((features & UFFD_FEATURE_EVENT_FO 1842 if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE)) 2064 goto err_out; 1843 goto err_out; 2065 << 2066 /* WP_ASYNC relies on WP_UNPOPULATED, << 2067 if (features & UFFD_FEATURE_WP_ASYNC) << 2068 features |= UFFD_FEATURE_WP_U << 2069 << 2070 /* report all available features and 1844 /* report all available features and ioctls to userland */ 2071 uffdio_api.features = UFFD_API_FEATUR 1845 uffdio_api.features = UFFD_API_FEATURES; 2072 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR << 2073 uffdio_api.features &= << 2074 ~(UFFD_FEATURE_MINOR_HUGETLBF << 2075 #endif << 2076 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP << 2077 uffdio_api.features &= ~UFFD_FEATURE_ << 2078 #endif << 2079 #ifndef CONFIG_PTE_MARKER_UFFD_WP << 2080 uffdio_api.features &= ~UFFD_FEATURE_ << 2081 uffdio_api.features &= ~UFFD_FEATURE_ << 2082 uffdio_api.features &= ~UFFD_FEATURE_ << 2083 #endif << 2084 << 2085 ret = -EINVAL; << 2086 if (features & ~uffdio_api.features) << 2087 goto err_out; << 2088 << 2089 uffdio_api.ioctls = UFFD_API_IOCTLS; 1846 uffdio_api.ioctls = UFFD_API_IOCTLS; 2090 ret = -EFAULT; 1847 ret = -EFAULT; 2091 if (copy_to_user(buf, &uffdio_api, si 1848 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) 2092 goto out; 1849 goto out; 2093 !! 1850 ctx->state = UFFD_STATE_RUNNING; 2094 /* only enable the requested features 1851 /* only enable the requested features for this uffd context */ 2095 ctx_features = uffd_ctx_features(feat !! 1852 ctx->features = uffd_ctx_features(features); 2096 ret = -EINVAL; << 2097 if (cmpxchg(&ctx->features, 0, ctx_fe << 2098 goto err_out; << 2099 << 2100 ret = 0; 1853 ret = 0; 2101 out: 1854 out: 2102 return ret; 1855 return ret; 2103 err_out: 1856 err_out: 2104 memset(&uffdio_api, 0, sizeof(uffdio_ 1857 memset(&uffdio_api, 0, sizeof(uffdio_api)); 2105 if (copy_to_user(buf, &uffdio_api, si 1858 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) 2106 ret = -EFAULT; 1859 ret = -EFAULT; 2107 goto out; 1860 goto out; 2108 } 1861 } 2109 1862 2110 static long userfaultfd_ioctl(struct file *fi 1863 static long userfaultfd_ioctl(struct file *file, unsigned cmd, 2111 unsigned long a 1864 unsigned long arg) 2112 { 1865 { 2113 int ret = -EINVAL; 1866 int ret = -EINVAL; 2114 struct userfaultfd_ctx *ctx = file->p 1867 struct userfaultfd_ctx *ctx = file->private_data; 2115 1868 2116 if (cmd != UFFDIO_API && !userfaultfd !! 1869 if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API) 2117 return -EINVAL; 1870 return -EINVAL; 2118 1871 2119 switch(cmd) { 1872 switch(cmd) { 2120 case UFFDIO_API: 1873 case UFFDIO_API: 2121 ret = userfaultfd_api(ctx, ar 1874 ret = userfaultfd_api(ctx, arg); 2122 break; 1875 break; 2123 case UFFDIO_REGISTER: 1876 case UFFDIO_REGISTER: 2124 ret = userfaultfd_register(ct 1877 ret = userfaultfd_register(ctx, arg); 2125 break; 1878 break; 2126 case UFFDIO_UNREGISTER: 1879 case UFFDIO_UNREGISTER: 2127 ret = userfaultfd_unregister( 1880 ret = userfaultfd_unregister(ctx, arg); 2128 break; 1881 break; 2129 case UFFDIO_WAKE: 1882 case UFFDIO_WAKE: 2130 ret = userfaultfd_wake(ctx, a 1883 ret = userfaultfd_wake(ctx, arg); 2131 break; 1884 break; 2132 case UFFDIO_COPY: 1885 case UFFDIO_COPY: 2133 ret = userfaultfd_copy(ctx, a 1886 ret = userfaultfd_copy(ctx, arg); 2134 break; 1887 break; 2135 case UFFDIO_ZEROPAGE: 1888 case UFFDIO_ZEROPAGE: 2136 ret = userfaultfd_zeropage(ct 1889 ret = userfaultfd_zeropage(ctx, arg); 2137 break; 1890 break; 2138 case UFFDIO_MOVE: << 2139 ret = userfaultfd_move(ctx, a << 2140 break; << 2141 case UFFDIO_WRITEPROTECT: << 2142 ret = userfaultfd_writeprotec << 2143 break; << 2144 case UFFDIO_CONTINUE: << 2145 ret = userfaultfd_continue(ct << 2146 break; << 2147 case UFFDIO_POISON: << 2148 ret = userfaultfd_poison(ctx, << 2149 break; << 2150 } 1891 } 2151 return ret; 1892 return ret; 2152 } 1893 } 2153 1894 2154 #ifdef CONFIG_PROC_FS 1895 #ifdef CONFIG_PROC_FS 2155 static void userfaultfd_show_fdinfo(struct se 1896 static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f) 2156 { 1897 { 2157 struct userfaultfd_ctx *ctx = f->priv 1898 struct userfaultfd_ctx *ctx = f->private_data; 2158 wait_queue_entry_t *wq; 1899 wait_queue_entry_t *wq; 2159 unsigned long pending = 0, total = 0; 1900 unsigned long pending = 0, total = 0; 2160 1901 2161 spin_lock_irq(&ctx->fault_pending_wqh 1902 spin_lock_irq(&ctx->fault_pending_wqh.lock); 2162 list_for_each_entry(wq, &ctx->fault_p 1903 list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) { 2163 pending++; 1904 pending++; 2164 total++; 1905 total++; 2165 } 1906 } 2166 list_for_each_entry(wq, &ctx->fault_w 1907 list_for_each_entry(wq, &ctx->fault_wqh.head, entry) { 2167 total++; 1908 total++; 2168 } 1909 } 2169 spin_unlock_irq(&ctx->fault_pending_w 1910 spin_unlock_irq(&ctx->fault_pending_wqh.lock); 2170 1911 2171 /* 1912 /* 2172 * If more protocols will be added, t 1913 * If more protocols will be added, there will be all shown 2173 * separated by a space. Like this: 1914 * separated by a space. Like this: 2174 * protocols: aa:... bb:... 1915 * protocols: aa:... bb:... 2175 */ 1916 */ 2176 seq_printf(m, "pending:\t%lu\ntotal:\ 1917 seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n", 2177 pending, total, UFFD_API, 1918 pending, total, UFFD_API, ctx->features, 2178 UFFD_API_IOCTLS|UFFD_API_R 1919 UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS); 2179 } 1920 } 2180 #endif 1921 #endif 2181 1922 2182 static const struct file_operations userfault 1923 static const struct file_operations userfaultfd_fops = { 2183 #ifdef CONFIG_PROC_FS 1924 #ifdef CONFIG_PROC_FS 2184 .show_fdinfo = userfaultfd_show_fd 1925 .show_fdinfo = userfaultfd_show_fdinfo, 2185 #endif 1926 #endif 2186 .release = userfaultfd_release 1927 .release = userfaultfd_release, 2187 .poll = userfaultfd_poll, 1928 .poll = userfaultfd_poll, 2188 .read_iter = userfaultfd_read_it !! 1929 .read = userfaultfd_read, 2189 .unlocked_ioctl = userfaultfd_ioctl, 1930 .unlocked_ioctl = userfaultfd_ioctl, 2190 .compat_ioctl = compat_ptr_ioctl, 1931 .compat_ioctl = compat_ptr_ioctl, 2191 .llseek = noop_llseek, 1932 .llseek = noop_llseek, 2192 }; 1933 }; 2193 1934 2194 static void init_once_userfaultfd_ctx(void *m 1935 static void init_once_userfaultfd_ctx(void *mem) 2195 { 1936 { 2196 struct userfaultfd_ctx *ctx = (struct 1937 struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem; 2197 1938 2198 init_waitqueue_head(&ctx->fault_pendi 1939 init_waitqueue_head(&ctx->fault_pending_wqh); 2199 init_waitqueue_head(&ctx->fault_wqh); 1940 init_waitqueue_head(&ctx->fault_wqh); 2200 init_waitqueue_head(&ctx->event_wqh); 1941 init_waitqueue_head(&ctx->event_wqh); 2201 init_waitqueue_head(&ctx->fd_wqh); 1942 init_waitqueue_head(&ctx->fd_wqh); 2202 seqcount_spinlock_init(&ctx->refile_s !! 1943 seqcount_init(&ctx->refile_seq); 2203 } 1944 } 2204 1945 2205 static int new_userfaultfd(int flags) !! 1946 SYSCALL_DEFINE1(userfaultfd, int, flags) 2206 { 1947 { 2207 struct userfaultfd_ctx *ctx; 1948 struct userfaultfd_ctx *ctx; 2208 struct file *file; << 2209 int fd; 1949 int fd; 2210 1950 >> 1951 if (!sysctl_unprivileged_userfaultfd && !capable(CAP_SYS_PTRACE)) >> 1952 return -EPERM; >> 1953 2211 BUG_ON(!current->mm); 1954 BUG_ON(!current->mm); 2212 1955 2213 /* Check the UFFD_* constants for con 1956 /* Check the UFFD_* constants for consistency. */ 2214 BUILD_BUG_ON(UFFD_USER_MODE_ONLY & UF << 2215 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXE 1957 BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC); 2216 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBL 1958 BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK); 2217 1959 2218 if (flags & ~(UFFD_SHARED_FCNTL_FLAGS !! 1960 if (flags & ~UFFD_SHARED_FCNTL_FLAGS) 2219 return -EINVAL; 1961 return -EINVAL; 2220 1962 2221 ctx = kmem_cache_alloc(userfaultfd_ct 1963 ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL); 2222 if (!ctx) 1964 if (!ctx) 2223 return -ENOMEM; 1965 return -ENOMEM; 2224 1966 2225 refcount_set(&ctx->refcount, 1); 1967 refcount_set(&ctx->refcount, 1); 2226 ctx->flags = flags; 1968 ctx->flags = flags; 2227 ctx->features = 0; 1969 ctx->features = 0; >> 1970 ctx->state = UFFD_STATE_WAIT_API; 2228 ctx->released = false; 1971 ctx->released = false; 2229 init_rwsem(&ctx->map_changing_lock); !! 1972 ctx->mmap_changing = false; 2230 atomic_set(&ctx->mmap_changing, 0); << 2231 ctx->mm = current->mm; 1973 ctx->mm = current->mm; 2232 << 2233 fd = get_unused_fd_flags(flags & UFFD << 2234 if (fd < 0) << 2235 goto err_out; << 2236 << 2237 /* Create a new inode so that the LSM << 2238 file = anon_inode_create_getfile("[us << 2239 O_RDONLY | (flags & U << 2240 if (IS_ERR(file)) { << 2241 put_unused_fd(fd); << 2242 fd = PTR_ERR(file); << 2243 goto err_out; << 2244 } << 2245 /* prevent the mm struct to be freed 1974 /* prevent the mm struct to be freed */ 2246 mmgrab(ctx->mm); 1975 mmgrab(ctx->mm); 2247 file->f_mode |= FMODE_NOWAIT; << 2248 fd_install(fd, file); << 2249 return fd; << 2250 err_out: << 2251 kmem_cache_free(userfaultfd_ctx_cache << 2252 return fd; << 2253 } << 2254 << 2255 static inline bool userfaultfd_syscall_allowe << 2256 { << 2257 /* Userspace-only page faults are alw << 2258 if (flags & UFFD_USER_MODE_ONLY) << 2259 return true; << 2260 << 2261 /* << 2262 * The user is requesting a userfault << 2263 * Privileged users are always allowe << 2264 */ << 2265 if (capable(CAP_SYS_PTRACE)) << 2266 return true; << 2267 << 2268 /* Otherwise, access to kernel fault << 2269 return sysctl_unprivileged_userfaultf << 2270 } << 2271 1976 2272 SYSCALL_DEFINE1(userfaultfd, int, flags) !! 1977 fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx, 2273 { !! 1978 O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS)); 2274 if (!userfaultfd_syscall_allowed(flag !! 1979 if (fd < 0) { 2275 return -EPERM; !! 1980 mmdrop(ctx->mm); 2276 !! 1981 kmem_cache_free(userfaultfd_ctx_cachep, ctx); 2277 return new_userfaultfd(flags); !! 1982 } 2278 } !! 1983 return fd; 2279 << 2280 static long userfaultfd_dev_ioctl(struct file << 2281 { << 2282 if (cmd != USERFAULTFD_IOC_NEW) << 2283 return -EINVAL; << 2284 << 2285 return new_userfaultfd(flags); << 2286 } 1984 } 2287 1985 2288 static const struct file_operations userfault << 2289 .unlocked_ioctl = userfaultfd_dev_ioc << 2290 .compat_ioctl = userfaultfd_dev_ioctl << 2291 .owner = THIS_MODULE, << 2292 .llseek = noop_llseek, << 2293 }; << 2294 << 2295 static struct miscdevice userfaultfd_misc = { << 2296 .minor = MISC_DYNAMIC_MINOR, << 2297 .name = "userfaultfd", << 2298 .fops = &userfaultfd_dev_fops << 2299 }; << 2300 << 2301 static int __init userfaultfd_init(void) 1986 static int __init userfaultfd_init(void) 2302 { 1987 { 2303 int ret; << 2304 << 2305 ret = misc_register(&userfaultfd_misc << 2306 if (ret) << 2307 return ret; << 2308 << 2309 userfaultfd_ctx_cachep = kmem_cache_c 1988 userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache", 2310 1989 sizeof(struct userfaultfd_ctx), 2311 1990 0, 2312 1991 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 2313 1992 init_once_userfaultfd_ctx); 2314 #ifdef CONFIG_SYSCTL << 2315 register_sysctl_init("vm", vm_userfau << 2316 #endif << 2317 return 0; 1993 return 0; 2318 } 1994 } 2319 __initcall(userfaultfd_init); 1995 __initcall(userfaultfd_init); 2320 1996
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.