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