1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * vma.h 4 * 5 * Core VMA manipulation API implemented in vma.c. 6 */ 7 #ifndef __MM_VMA_H 8 #define __MM_VMA_H 9 10 /* 11 * VMA lock generalization 12 */ 13 struct vma_prepare { 14 struct vm_area_struct *vma; 15 struct vm_area_struct *adj_next; 16 struct file *file; 17 struct address_space *mapping; 18 struct anon_vma *anon_vma; 19 struct vm_area_struct *insert; 20 struct vm_area_struct *remove; 21 struct vm_area_struct *remove2; 22 }; 23 24 struct unlink_vma_file_batch { 25 int count; 26 struct vm_area_struct *vmas[8]; 27 }; 28 29 /* 30 * vma munmap operation 31 */ 32 struct vma_munmap_struct { 33 struct vma_iterator *vmi; 34 struct mm_struct *mm; 35 struct vm_area_struct *vma; /* The first vma to munmap */ 36 struct vm_area_struct *prev; /* vma before the munmap area */ 37 struct vm_area_struct *next; /* vma after the munmap area */ 38 struct list_head *uf; /* Userfaultfd list_head */ 39 unsigned long start; /* Aligned start addr (inclusive) */ 40 unsigned long end; /* Aligned end addr (exclusive) */ 41 unsigned long unmap_start; /* Unmap PTE start */ 42 unsigned long unmap_end; /* Unmap PTE end */ 43 int vma_count; /* Number of vmas that will be removed */ 44 unsigned long nr_pages; /* Number of pages being removed */ 45 unsigned long locked_vm; /* Number of locked pages */ 46 unsigned long nr_accounted; /* Number of VM_ACCOUNT pages */ 47 unsigned long exec_vm; 48 unsigned long stack_vm; 49 unsigned long data_vm; 50 bool unlock; /* Unlock after the munmap */ 51 bool clear_ptes; /* If there are outstanding PTE to be cleared */ 52 bool closed_vm_ops; /* call_mmap() was encountered, so vmas may be closed */ 53 }; 54 55 #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 56 void validate_mm(struct mm_struct *mm); 57 #else 58 #define validate_mm(mm) do { } while (0) 59 #endif 60 61 /* Required for expand_downwards(). */ 62 void anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma); 63 64 /* Required for expand_downwards(). */ 65 void anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma); 66 67 /* Required for do_brk_flags(). */ 68 void vma_prepare(struct vma_prepare *vp); 69 70 /* Required for do_brk_flags(). */ 71 void init_vma_prep(struct vma_prepare *vp, 72 struct vm_area_struct *vma); 73 74 /* Required for do_brk_flags(). */ 75 void vma_complete(struct vma_prepare *vp, 76 struct vma_iterator *vmi, struct mm_struct *mm); 77 78 int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, 79 unsigned long start, unsigned long end, pgoff_t pgoff, 80 struct vm_area_struct *next); 81 82 int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, 83 unsigned long start, unsigned long end, pgoff_t pgoff); 84 85 #ifdef CONFIG_MMU 86 /* 87 * init_vma_munmap() - Initializer wrapper for vma_munmap_struct 88 * @vms: The vma munmap struct 89 * @vmi: The vma iterator 90 * @vma: The first vm_area_struct to munmap 91 * @start: The aligned start address to munmap 92 * @end: The aligned end address to munmap 93 * @uf: The userfaultfd list_head 94 * @unlock: Unlock after the operation. Only unlocked on success 95 */ 96 static inline void init_vma_munmap(struct vma_munmap_struct *vms, 97 struct vma_iterator *vmi, struct vm_area_struct *vma, 98 unsigned long start, unsigned long end, struct list_head *uf, 99 bool unlock) 100 { 101 vms->mm = current->mm; 102 vms->vmi = vmi; 103 vms->vma = vma; 104 if (vma) { 105 vms->start = start; 106 vms->end = end; 107 } else { 108 vms->start = vms->end = 0; 109 } 110 vms->unlock = unlock; 111 vms->uf = uf; 112 vms->vma_count = 0; 113 vms->nr_pages = vms->locked_vm = vms->nr_accounted = 0; 114 vms->exec_vm = vms->stack_vm = vms->data_vm = 0; 115 vms->unmap_start = FIRST_USER_ADDRESS; 116 vms->unmap_end = USER_PGTABLES_CEILING; 117 vms->clear_ptes = false; 118 vms->closed_vm_ops = false; 119 } 120 #endif 121 122 int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, 123 struct ma_state *mas_detach); 124 125 void vms_complete_munmap_vmas(struct vma_munmap_struct *vms, 126 struct ma_state *mas_detach); 127 128 void vms_clean_up_area(struct vma_munmap_struct *vms, 129 struct ma_state *mas_detach, bool mm_wr_locked); 130 131 /* 132 * abort_munmap_vmas - Undo any munmap work and free resources 133 * 134 * Reattach any detached vmas and free up the maple tree used to track the vmas. 135 */ 136 static inline void abort_munmap_vmas(struct ma_state *mas_detach, bool closed) 137 { 138 struct vm_area_struct *vma; 139 140 mas_set(mas_detach, 0); 141 mas_for_each(mas_detach, vma, ULONG_MAX) { 142 vma_mark_detached(vma, false); 143 if (closed && vma->vm_ops && vma->vm_ops->open) 144 vma->vm_ops->open(vma); 145 } 146 147 __mt_destroy(mas_detach->tree); 148 } 149 150 int 151 do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 152 struct mm_struct *mm, unsigned long start, 153 unsigned long end, struct list_head *uf, bool unlock); 154 155 int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, 156 unsigned long start, size_t len, struct list_head *uf, 157 bool unlock); 158 159 void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed); 160 161 void unmap_region(struct ma_state *mas, struct vm_area_struct *vma, 162 struct vm_area_struct *prev, struct vm_area_struct *next); 163 164 /* Required by mmap_region(). */ 165 bool 166 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 167 struct anon_vma *anon_vma, struct file *file, 168 pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 169 struct anon_vma_name *anon_name); 170 171 /* Required by mmap_region() and do_brk_flags(). */ 172 bool 173 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 174 struct anon_vma *anon_vma, struct file *file, 175 pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 176 struct anon_vma_name *anon_name); 177 178 struct vm_area_struct *vma_modify(struct vma_iterator *vmi, 179 struct vm_area_struct *prev, 180 struct vm_area_struct *vma, 181 unsigned long start, unsigned long end, 182 unsigned long vm_flags, 183 struct mempolicy *policy, 184 struct vm_userfaultfd_ctx uffd_ctx, 185 struct anon_vma_name *anon_name); 186 187 /* We are about to modify the VMA's flags. */ 188 static inline struct vm_area_struct 189 *vma_modify_flags(struct vma_iterator *vmi, 190 struct vm_area_struct *prev, 191 struct vm_area_struct *vma, 192 unsigned long start, unsigned long end, 193 unsigned long new_flags) 194 { 195 return vma_modify(vmi, prev, vma, start, end, new_flags, 196 vma_policy(vma), vma->vm_userfaultfd_ctx, 197 anon_vma_name(vma)); 198 } 199 200 /* We are about to modify the VMA's flags and/or anon_name. */ 201 static inline struct vm_area_struct 202 *vma_modify_flags_name(struct vma_iterator *vmi, 203 struct vm_area_struct *prev, 204 struct vm_area_struct *vma, 205 unsigned long start, 206 unsigned long end, 207 unsigned long new_flags, 208 struct anon_vma_name *new_name) 209 { 210 return vma_modify(vmi, prev, vma, start, end, new_flags, 211 vma_policy(vma), vma->vm_userfaultfd_ctx, new_name); 212 } 213 214 /* We are about to modify the VMA's memory policy. */ 215 static inline struct vm_area_struct 216 *vma_modify_policy(struct vma_iterator *vmi, 217 struct vm_area_struct *prev, 218 struct vm_area_struct *vma, 219 unsigned long start, unsigned long end, 220 struct mempolicy *new_pol) 221 { 222 return vma_modify(vmi, prev, vma, start, end, vma->vm_flags, 223 new_pol, vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 224 } 225 226 /* We are about to modify the VMA's flags and/or uffd context. */ 227 static inline struct vm_area_struct 228 *vma_modify_flags_uffd(struct vma_iterator *vmi, 229 struct vm_area_struct *prev, 230 struct vm_area_struct *vma, 231 unsigned long start, unsigned long end, 232 unsigned long new_flags, 233 struct vm_userfaultfd_ctx new_ctx) 234 { 235 return vma_modify(vmi, prev, vma, start, end, new_flags, 236 vma_policy(vma), new_ctx, anon_vma_name(vma)); 237 } 238 239 struct vm_area_struct 240 *vma_merge_new_vma(struct vma_iterator *vmi, struct vm_area_struct *prev, 241 struct vm_area_struct *vma, unsigned long start, 242 unsigned long end, pgoff_t pgoff); 243 244 struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi, 245 struct vm_area_struct *vma, 246 unsigned long delta); 247 248 void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb); 249 250 void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb); 251 252 void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb, 253 struct vm_area_struct *vma); 254 255 void unlink_file_vma(struct vm_area_struct *vma); 256 257 void vma_link_file(struct vm_area_struct *vma); 258 259 int vma_link(struct mm_struct *mm, struct vm_area_struct *vma); 260 261 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 262 unsigned long addr, unsigned long len, pgoff_t pgoff, 263 bool *need_rmap_locks); 264 265 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma); 266 267 bool vma_needs_dirty_tracking(struct vm_area_struct *vma); 268 bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot); 269 270 int mm_take_all_locks(struct mm_struct *mm); 271 void mm_drop_all_locks(struct mm_struct *mm); 272 unsigned long count_vma_pages_range(struct mm_struct *mm, 273 unsigned long addr, unsigned long end, 274 unsigned long *nr_accounted); 275 276 static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma) 277 { 278 /* 279 * We want to check manually if we can change individual PTEs writable 280 * if we can't do that automatically for all PTEs in a mapping. For 281 * private mappings, that's always the case when we have write 282 * permissions as we properly have to handle COW. 283 */ 284 if (vma->vm_flags & VM_SHARED) 285 return vma_wants_writenotify(vma, vma->vm_page_prot); 286 return !!(vma->vm_flags & VM_WRITE); 287 } 288 289 #ifdef CONFIG_MMU 290 static inline pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) 291 { 292 return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); 293 } 294 #endif 295 296 static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, 297 unsigned long min) 298 { 299 return mas_prev(&vmi->mas, min); 300 } 301 302 static inline int vma_iter_store_gfp(struct vma_iterator *vmi, 303 struct vm_area_struct *vma, gfp_t gfp) 304 { 305 if (vmi->mas.status != ma_start && 306 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start))) 307 vma_iter_invalidate(vmi); 308 309 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); 310 mas_store_gfp(&vmi->mas, vma, gfp); 311 if (unlikely(mas_is_err(&vmi->mas))) 312 return -ENOMEM; 313 314 return 0; 315 } 316 317 318 /* 319 * These three helpers classifies VMAs for virtual memory accounting. 320 */ 321 322 /* 323 * Executable code area - executable, not writable, not stack 324 */ 325 static inline bool is_exec_mapping(vm_flags_t flags) 326 { 327 return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC; 328 } 329 330 /* 331 * Stack area (including shadow stacks) 332 * 333 * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous: 334 * do_mmap() forbids all other combinations. 335 */ 336 static inline bool is_stack_mapping(vm_flags_t flags) 337 { 338 return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK); 339 } 340 341 /* 342 * Data area - private, writable, not stack 343 */ 344 static inline bool is_data_mapping(vm_flags_t flags) 345 { 346 return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE; 347 } 348 349 350 static inline void vma_iter_config(struct vma_iterator *vmi, 351 unsigned long index, unsigned long last) 352 { 353 __mas_set_range(&vmi->mas, index, last - 1); 354 } 355 356 static inline void vma_iter_reset(struct vma_iterator *vmi) 357 { 358 mas_reset(&vmi->mas); 359 } 360 361 static inline 362 struct vm_area_struct *vma_iter_prev_range_limit(struct vma_iterator *vmi, unsigned long min) 363 { 364 return mas_prev_range(&vmi->mas, min); 365 } 366 367 static inline 368 struct vm_area_struct *vma_iter_next_range_limit(struct vma_iterator *vmi, unsigned long max) 369 { 370 return mas_next_range(&vmi->mas, max); 371 } 372 373 static inline int vma_iter_area_lowest(struct vma_iterator *vmi, unsigned long min, 374 unsigned long max, unsigned long size) 375 { 376 return mas_empty_area(&vmi->mas, min, max - 1, size); 377 } 378 379 static inline int vma_iter_area_highest(struct vma_iterator *vmi, unsigned long min, 380 unsigned long max, unsigned long size) 381 { 382 return mas_empty_area_rev(&vmi->mas, min, max - 1, size); 383 } 384 385 /* 386 * VMA Iterator functions shared between nommu and mmap 387 */ 388 static inline int vma_iter_prealloc(struct vma_iterator *vmi, 389 struct vm_area_struct *vma) 390 { 391 return mas_preallocate(&vmi->mas, vma, GFP_KERNEL); 392 } 393 394 static inline void vma_iter_clear(struct vma_iterator *vmi) 395 { 396 mas_store_prealloc(&vmi->mas, NULL); 397 } 398 399 static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) 400 { 401 return mas_walk(&vmi->mas); 402 } 403 404 /* Store a VMA with preallocated memory */ 405 static inline void vma_iter_store(struct vma_iterator *vmi, 406 struct vm_area_struct *vma) 407 { 408 409 #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 410 if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start && 411 vmi->mas.index > vma->vm_start)) { 412 pr_warn("%lx > %lx\n store vma %lx-%lx\n into slot %lx-%lx\n", 413 vmi->mas.index, vma->vm_start, vma->vm_start, 414 vma->vm_end, vmi->mas.index, vmi->mas.last); 415 } 416 if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start && 417 vmi->mas.last < vma->vm_start)) { 418 pr_warn("%lx < %lx\nstore vma %lx-%lx\ninto slot %lx-%lx\n", 419 vmi->mas.last, vma->vm_start, vma->vm_start, vma->vm_end, 420 vmi->mas.index, vmi->mas.last); 421 } 422 #endif 423 424 if (vmi->mas.status != ma_start && 425 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start))) 426 vma_iter_invalidate(vmi); 427 428 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); 429 mas_store_prealloc(&vmi->mas, vma); 430 } 431 432 static inline unsigned long vma_iter_addr(struct vma_iterator *vmi) 433 { 434 return vmi->mas.index; 435 } 436 437 static inline unsigned long vma_iter_end(struct vma_iterator *vmi) 438 { 439 return vmi->mas.last + 1; 440 } 441 442 static inline int vma_iter_bulk_alloc(struct vma_iterator *vmi, 443 unsigned long count) 444 { 445 return mas_expected_entries(&vmi->mas, count); 446 } 447 448 static inline 449 struct vm_area_struct *vma_iter_prev_range(struct vma_iterator *vmi) 450 { 451 return mas_prev_range(&vmi->mas, 0); 452 } 453 454 #ifdef CONFIG_64BIT 455 456 static inline bool vma_is_sealed(struct vm_area_struct *vma) 457 { 458 return (vma->vm_flags & VM_SEALED); 459 } 460 461 /* 462 * check if a vma is sealed for modification. 463 * return true, if modification is allowed. 464 */ 465 static inline bool can_modify_vma(struct vm_area_struct *vma) 466 { 467 if (unlikely(vma_is_sealed(vma))) 468 return false; 469 470 return true; 471 } 472 473 bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior); 474 475 #else 476 477 static inline bool can_modify_vma(struct vm_area_struct *vma) 478 { 479 return true; 480 } 481 482 static inline bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) 483 { 484 return true; 485 } 486 487 #endif 488 489 #endif /* __MM_VMA_H */ 490