1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * vma.h 4 * 5 * Core VMA manipulation API implemented in vma.c, vma_init.c and vma_exec.c. 6 * 7 * Note that, in order for VMA logic to be userland testable, this header 8 * intentionally includes no dependencies. 9 * 10 * This is specifically scoped to mm-only. Users of this functionality (other 11 * than the core VMA implementation itself) should not include this header 12 * directly, but rather include internal.h. 13 */ 14 #ifndef __MM_VMA_H 15 #define __MM_VMA_H 16 17 /* 18 * VMA lock generalization 19 */ 20 struct vma_prepare { 21 struct vm_area_struct *vma; 22 struct vm_area_struct *adj_next; 23 struct file *file; 24 struct address_space *mapping; 25 struct anon_vma *anon_vma; 26 struct vm_area_struct *insert; 27 struct vm_area_struct *remove; 28 struct vm_area_struct *remove2; 29 30 bool skip_vma_uprobe :1; 31 bool file_pgoff_unchanged :1; 32 bool anon_pgoff_unchanged :1; 33 }; 34 35 struct unlink_vma_file_batch { 36 int count; 37 struct vm_area_struct *vmas[8]; 38 }; 39 40 /* 41 * vma munmap operation 42 */ 43 struct vma_munmap_struct { 44 struct vma_iterator *vmi; 45 struct vm_area_struct *vma; /* The first vma to munmap */ 46 struct vm_area_struct *prev; /* vma before the munmap area */ 47 struct vm_area_struct *next; /* vma after the munmap area */ 48 struct list_head *uf; /* Userfaultfd list_head */ 49 unsigned long start; /* Aligned start addr (inclusive) */ 50 unsigned long end; /* Aligned end addr (exclusive) */ 51 unsigned long unmap_start; /* Unmap PTE start */ 52 unsigned long unmap_end; /* Unmap PTE end */ 53 int vma_count; /* Number of vmas that will be removed */ 54 bool unlock; /* Unlock after the munmap */ 55 bool clear_ptes; /* If there are outstanding PTE to be cleared */ 56 /* 2 byte hole */ 57 unsigned long nr_pages; /* Number of pages being removed */ 58 unsigned long locked_vm; /* Number of locked pages */ 59 unsigned long nr_accounted; /* Number of VM_ACCOUNT pages */ 60 unsigned long exec_vm; 61 unsigned long stack_vm; 62 unsigned long data_vm; 63 }; 64 65 enum vma_merge_state { 66 VMA_MERGE_START, 67 VMA_MERGE_ERROR_NOMEM, 68 VMA_MERGE_NOMERGE, 69 VMA_MERGE_SUCCESS, 70 }; 71 72 /* 73 * Describes a VMA merge operation and is threaded throughout it. 74 * 75 * Any of the fields may be mutated by the merge operation, so no guarantees are 76 * made to the contents of this structure after a merge operation has completed. 77 */ 78 struct vma_merge_struct { 79 struct mm_struct *mm; 80 struct vma_iterator *vmi; 81 /* 82 * Adjacent VMAs, any of which may be NULL if not present: 83 * 84 * |------|--------|------| 85 * | prev | middle | next | 86 * |------|--------|------| 87 * 88 * middle may not yet exist in the case of a proposed new VMA being 89 * merged, or it may be an existing VMA. 90 * 91 * next may be assigned by the caller. 92 */ 93 struct vm_area_struct *prev; 94 struct vm_area_struct *middle; 95 struct vm_area_struct *next; 96 /* This is the VMA we ultimately target to become the merged VMA. */ 97 struct vm_area_struct *target; 98 /* 99 * Initially, the start, end, pgoff fields are provided by the caller 100 * and describe the proposed new VMA range, whether modifying an 101 * existing VMA (which will be 'middle'), or adding a new one. 102 * 103 * During the merge process these fields are updated to describe the new 104 * range _including those VMAs which will be merged_. 105 */ 106 unsigned long start; 107 unsigned long end; 108 pgoff_t pgoff; 109 pgoff_t anon_pgoff; 110 111 union { 112 /* Temporary while VMA flags are being converted. */ 113 vm_flags_t vm_flags; 114 vma_flags_t vma_flags; 115 }; 116 struct file *file; 117 struct anon_vma *anon_vma; 118 struct mempolicy *policy; 119 struct vm_userfaultfd_ctx uffd_ctx; 120 struct anon_vma_name *anon_name; 121 enum vma_merge_state state; 122 123 /* If copied from (i.e. mremap()'d) the VMA from which we are copying. */ 124 struct vm_area_struct *copied_from; 125 126 /* Flags which callers can use to modify merge behaviour: */ 127 128 /* 129 * If we can expand, simply do so. We know there is nothing to merge to 130 * the right. Does not reset state upon failure to merge. The VMA 131 * iterator is assumed to be positioned at the previous VMA, rather than 132 * at the gap. 133 */ 134 bool just_expand :1; 135 136 /* 137 * If a merge is possible, but an OOM error occurs, give up and don't 138 * execute the merge, returning NULL. 139 */ 140 bool give_up_on_oom :1; 141 142 /* 143 * If set, skip uprobe_mmap upon merged vma. 144 */ 145 bool skip_vma_uprobe :1; 146 147 /* Internal flags set during merge process: */ 148 149 /* 150 * Internal flag indicating the merge increases vmg->middle->vm_start 151 * (and thereby, vmg->prev->vm_end). 152 */ 153 bool __adjust_middle_start :1; 154 /* 155 * Internal flag indicating the merge decreases vmg->next->vm_start 156 * (and thereby, vmg->middle->vm_end). 157 */ 158 bool __adjust_next_start :1; 159 /* 160 * Internal flag used during the merge operation to indicate we will 161 * remove vmg->middle. 162 */ 163 bool __remove_middle :1; 164 /* 165 * Internal flag used during the merge operation to indicate we will 166 * remove vmg->next. 167 */ 168 bool __remove_next :1; 169 170 }; 171 172 struct unmap_desc { 173 struct ma_state *mas; /* the maple state point to the first vma */ 174 struct vm_area_struct *first; /* The first vma */ 175 unsigned long pg_start; /* The first pagetable address to free (floor) */ 176 unsigned long pg_end; /* The last pagetable address to free (ceiling) */ 177 unsigned long vma_start; /* The min vma address */ 178 unsigned long vma_end; /* The max vma address */ 179 unsigned long tree_end; /* Maximum for the vma tree search */ 180 unsigned long tree_reset; /* Where to reset the vma tree walk */ 181 bool mm_wr_locked; /* If the mmap write lock is held */ 182 }; 183 184 /* 185 * unmap_all_init() - Initialize unmap_desc to remove all vmas, point the 186 * pg_start and pg_end to a safe location. 187 */ 188 static inline void unmap_all_init(struct unmap_desc *unmap, 189 struct vma_iterator *vmi, struct vm_area_struct *vma) 190 { 191 unmap->mas = &vmi->mas; 192 unmap->first = vma; 193 unmap->pg_start = FIRST_USER_ADDRESS; 194 unmap->pg_end = USER_PGTABLES_CEILING; 195 unmap->vma_start = 0; 196 unmap->vma_end = ULONG_MAX; 197 unmap->tree_end = ULONG_MAX; 198 unmap->tree_reset = vma->vm_end; 199 unmap->mm_wr_locked = false; 200 } 201 202 /* 203 * unmap_pgtable_init() - Initialize unmap_desc to remove all page tables within 204 * the user range. 205 * 206 * ARM can have mappings outside of vmas. 207 * See: e2cdef8c847b4 ("[PATCH] freepgt: free_pgtables from FIRST_USER_ADDRESS") 208 * 209 * ARM LPAE uses page table mappings beyond the USER_PGTABLES_CEILING 210 * See: CONFIG_ARM_LPAE in arch/arm/include/asm/pgtable.h 211 */ 212 static inline void unmap_pgtable_init(struct unmap_desc *unmap, 213 struct vma_iterator *vmi) 214 { 215 vma_iter_set(vmi, unmap->tree_reset); 216 unmap->vma_start = FIRST_USER_ADDRESS; 217 unmap->vma_end = USER_PGTABLES_CEILING; 218 unmap->tree_end = USER_PGTABLES_CEILING; 219 } 220 221 #define UNMAP_STATE(name, _vmi, _vma, _vma_start, _vma_end, _prev, _next) \ 222 struct unmap_desc name = { \ 223 .mas = &(_vmi)->mas, \ 224 .first = _vma, \ 225 .pg_start = _prev ? ((struct vm_area_struct *)_prev)->vm_end : \ 226 FIRST_USER_ADDRESS, \ 227 .pg_end = _next ? ((struct vm_area_struct *)_next)->vm_start : \ 228 USER_PGTABLES_CEILING, \ 229 .vma_start = _vma_start, \ 230 .vma_end = _vma_end, \ 231 .tree_end = _next ? \ 232 ((struct vm_area_struct *)_next)->vm_start : \ 233 USER_PGTABLES_CEILING, \ 234 .tree_reset = _vma->vm_end, \ 235 .mm_wr_locked = true, \ 236 } 237 238 static inline bool vmg_nomem(struct vma_merge_struct *vmg) 239 { 240 return vmg->state == VMA_MERGE_ERROR_NOMEM; 241 } 242 243 static inline pgoff_t vmg_pages(const struct vma_merge_struct *vmg) 244 { 245 const unsigned long size = vmg->end - vmg->start; 246 247 return size >> PAGE_SHIFT; 248 } 249 250 static inline pgoff_t vmg_start_pgoff(const struct vma_merge_struct *vmg) 251 { 252 return vmg->pgoff; 253 } 254 255 static inline pgoff_t vmg_end_pgoff(const struct vma_merge_struct *vmg) 256 { 257 return vmg_start_pgoff(vmg) + vmg_pages(vmg); 258 } 259 260 /** 261 * vma_has_anon_rmap() - does @vma possess an anonymous reverse mapping? 262 * @vma: The VMA to be checked. 263 * 264 * If the VMA is attached, a VMA or mmap lock must be held. 265 * 266 * This state is only possible for CoW mappings, see the comment for 267 * vma_flags_is_cow_mapping() for details. 268 * 269 * Importantly, a VMA which possesses an anonymous rmap may map anonymous 270 * folios. 271 * 272 * This function will not result in a false positive. 273 * 274 * However, if only a read lock is held, it may give a false negative, in which 275 * case it should be re-checked with mm->page_table_lock held. 276 * 277 * Returns: true if @vma has an anonymous reverse mapping, otherwise false. 278 */ 279 static inline bool vma_has_anon_rmap(const struct vm_area_struct *vma) 280 { 281 if (vma_is_attached(vma)) 282 vma_assert_stabilised(vma); 283 /* KCSAN gets confused about the optimistic check. Silence it. */ 284 return data_race(vma->anon_vma); 285 } 286 287 static inline void assert_sane_pgoff(struct vm_area_struct *vma, pgoff_t pgoff) 288 { 289 /* nommu doesn't set a virtual pgoff for anon VMAs. */ 290 if (!IS_ENABLED(CONFIG_MMU)) 291 return; 292 /* 293 * File-backed VMAs have arbitrary page offset (either page offset into 294 * file or for pfnmap the PFN of the start of the range or drivers may 295 * set arbitrary page offset). 296 */ 297 if (!vma_is_anonymous(vma)) 298 return; 299 /* If faulted in, could have been remapped. */ 300 if (vma_has_anon_rmap(vma)) 301 return; 302 /* OK this is really an anon VMA - expect virtual page offset. */ 303 VM_WARN_ON_ONCE(pgoff != vma->vm_start >> PAGE_SHIFT); 304 } 305 306 static inline void vma_set_pgoff(struct vm_area_struct *vma, pgoff_t pgoff) 307 { 308 vma_assert_can_modify(vma); 309 assert_sane_pgoff(vma, pgoff); 310 vma->vm_pgoff = pgoff; 311 } 312 313 static inline pgoff_t vmg_start_anon_pgoff(const struct vma_merge_struct *vmg) 314 { 315 return vmg->anon_pgoff; 316 } 317 318 static inline pgoff_t vmg_end_anon_pgoff(const struct vma_merge_struct *vmg) 319 { 320 return vmg_start_anon_pgoff(vmg) + vmg_pages(vmg); 321 } 322 323 static inline void __vma_set_anon_pgoff(struct vm_area_struct *vma, pgoff_t pgoff) 324 { 325 #ifdef CONFIG_64BIT 326 vma->__vm_anon_pgoff_hi = pgoff >> 32; 327 #endif 328 vma->__vm_anon_pgoff_lo = pgoff & GENMASK(31, 0); 329 } 330 331 static inline void vma_set_anon_pgoff(struct vm_area_struct *vma, pgoff_t pgoff) 332 { 333 vma_assert_can_modify(vma); 334 __vma_set_anon_pgoff(vma, pgoff); 335 } 336 337 static inline void vma_add_pgoff(struct vm_area_struct *vma, pgoff_t delta) 338 { 339 vma_assert_can_modify(vma); 340 vma_set_pgoff(vma, vma_start_pgoff(vma) + delta); 341 vma_set_anon_pgoff(vma, vma_start_anon_pgoff(vma) + delta); 342 } 343 344 static inline void vma_sub_pgoff(struct vm_area_struct *vma, pgoff_t delta) 345 { 346 vma_assert_can_modify(vma); 347 vma_set_pgoff(vma, vma_start_pgoff(vma) - delta); 348 vma_set_anon_pgoff(vma, vma_start_anon_pgoff(vma) - delta); 349 } 350 351 #define VMG_STATE(name, mm_, vmi_, start_, end_, vma_flags_, pgoff_, anon_pgoff_) \ 352 struct vma_merge_struct name = { \ 353 .mm = mm_, \ 354 .vmi = vmi_, \ 355 .start = start_, \ 356 .end = end_, \ 357 .vma_flags = vma_flags_, \ 358 .pgoff = pgoff_, \ 359 .anon_pgoff = anon_pgoff_, \ 360 .state = VMA_MERGE_START, \ 361 } 362 363 #define VMG_VMA_STATE(name, vmi_, prev_, vma_, start_, end_) \ 364 struct vma_merge_struct name = { \ 365 .mm = vma_->vm_mm, \ 366 .vmi = vmi_, \ 367 .prev = prev_, \ 368 .middle = vma_, \ 369 .next = NULL, \ 370 .start = start_, \ 371 .end = end_, \ 372 .vm_flags = vma_->vm_flags, \ 373 .pgoff = linear_page_index(vma_, start_), \ 374 .anon_pgoff = __linear_anon_page_index(vma_, start_), \ 375 .file = vma_->vm_file, \ 376 .anon_vma = vma_->anon_vma, \ 377 .policy = vma_policy(vma_), \ 378 .uffd_ctx = vma_->vm_userfaultfd_ctx, \ 379 .anon_name = anon_vma_name(vma_), \ 380 .state = VMA_MERGE_START, \ 381 } 382 383 #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 384 void validate_mm(struct mm_struct *mm); 385 #else 386 #define validate_mm(mm) do { } while (0) 387 #endif 388 389 __must_check int vma_expand(struct vma_merge_struct *vmg); 390 __must_check int vma_shrink(struct vma_iterator *vmi, 391 struct vm_area_struct *vma, unsigned long end); 392 393 static inline int vma_iter_store_gfp(struct vma_iterator *vmi, 394 struct vm_area_struct *vma, gfp_t gfp) 395 396 { 397 if (vmi->mas.status != ma_start && 398 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start))) 399 vma_iter_invalidate(vmi); 400 401 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); 402 mas_store_gfp(&vmi->mas, vma, gfp); 403 if (unlikely(mas_is_err(&vmi->mas))) 404 return -ENOMEM; 405 406 vma_mark_attached(vma); 407 return 0; 408 } 409 410 /* 411 * Temporary helper function for stacked mmap handlers which specify 412 * f_op->mmap() but which might have an underlying file system which implements 413 * f_op->mmap_prepare(). 414 */ 415 static inline void compat_set_vma_from_desc(struct vm_area_struct *vma, 416 struct vm_area_desc *desc) 417 { 418 /* 419 * Since we're invoking .mmap_prepare() despite having a partially 420 * established VMA, we must take care to handle setting fields 421 * correctly. 422 */ 423 424 /* Mutable fields. Populated with initial state. */ 425 vma_set_pgoff(vma, desc->pgoff); 426 if (desc->vm_file != vma->vm_file) { 427 fput(vma->vm_file); 428 vma->vm_file = desc->vm_file; 429 } 430 vma->flags = desc->vma_flags; 431 vma->vm_page_prot = desc->page_prot; 432 433 /* User-defined fields. */ 434 vma->vm_ops = desc->vm_ops; 435 vma->vm_private_data = desc->private_data; 436 } 437 438 int 439 do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 440 struct mm_struct *mm, unsigned long start, 441 unsigned long end, struct list_head *uf, bool unlock); 442 443 int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, 444 unsigned long start, size_t len, struct list_head *uf, 445 bool unlock); 446 447 void remove_vma(struct vm_area_struct *vma); 448 void unmap_region(struct unmap_desc *unmap); 449 450 /** 451 * vma_modify_flags() - Perform any necessary split/merge in preparation for 452 * setting VMA flags to *@vm_flags in the range @start to @end contained within 453 * @vma. 454 * @vmi: Valid VMA iterator positioned at @vma. 455 * @prev: The VMA immediately prior to @vma or NULL if @vma is the first. 456 * @vma: The VMA containing the range @start to @end to be updated. 457 * @start: The start of the range to update. May be offset within @vma. 458 * @end: The exclusive end of the range to update, may be offset within @vma. 459 * @vma_flags_ptr: A pointer to the VMA flags that the @start to @end range is 460 * about to be set to. On merge, this will be updated to include sticky flags. 461 * 462 * IMPORTANT: The actual modification being requested here is NOT applied, 463 * rather the VMA is perhaps split, perhaps merged to accommodate the change, 464 * and the caller is expected to perform the actual modification. 465 * 466 * In order to account for sticky VMA flags, the @vma_flags_ptr parameter points 467 * to the requested flags which are then updated so the caller, should they 468 * overwrite any existing flags, correctly retains these. 469 * 470 * Returns: A VMA which contains the range @start to @end ready to have its 471 * flags altered to *@vma_flags. 472 */ 473 __must_check struct vm_area_struct *vma_modify_flags(struct vma_iterator *vmi, 474 struct vm_area_struct *prev, struct vm_area_struct *vma, 475 unsigned long start, unsigned long end, vma_flags_t *vma_flags_ptr); 476 477 /** 478 * vma_modify_name() - Perform any necessary split/merge in preparation for 479 * setting anonymous VMA name to @new_name in the range @start to @end contained 480 * within @vma. 481 * @vmi: Valid VMA iterator positioned at @vma. 482 * @prev: The VMA immediately prior to @vma or NULL if @vma is the first. 483 * @vma: The VMA containing the range @start to @end to be updated. 484 * @start: The start of the range to update. May be offset within @vma. 485 * @end: The exclusive end of the range to update, may be offset within @vma. 486 * @new_name: The anonymous VMA name that the @start to @end range is about to 487 * be set to. 488 * 489 * IMPORTANT: The actual modification being requested here is NOT applied, 490 * rather the VMA is perhaps split, perhaps merged to accommodate the change, 491 * and the caller is expected to perform the actual modification. 492 * 493 * Returns: A VMA which contains the range @start to @end ready to have its 494 * anonymous VMA name changed to @new_name. 495 */ 496 __must_check struct vm_area_struct *vma_modify_name(struct vma_iterator *vmi, 497 struct vm_area_struct *prev, struct vm_area_struct *vma, 498 unsigned long start, unsigned long end, 499 struct anon_vma_name *new_name); 500 501 /** 502 * vma_modify_policy() - Perform any necessary split/merge in preparation for 503 * setting NUMA policy to @new_pol in the range @start to @end contained 504 * within @vma. 505 * @vmi: Valid VMA iterator positioned at @vma. 506 * @prev: The VMA immediately prior to @vma or NULL if @vma is the first. 507 * @vma: The VMA containing the range @start to @end to be updated. 508 * @start: The start of the range to update. May be offset within @vma. 509 * @end: The exclusive end of the range to update, may be offset within @vma. 510 * @new_pol: The NUMA policy that the @start to @end range is about to be set 511 * to. 512 * 513 * IMPORTANT: The actual modification being requested here is NOT applied, 514 * rather the VMA is perhaps split, perhaps merged to accommodate the change, 515 * and the caller is expected to perform the actual modification. 516 * 517 * Returns: A VMA which contains the range @start to @end ready to have its 518 * NUMA policy changed to @new_pol. 519 */ 520 __must_check struct vm_area_struct *vma_modify_policy(struct vma_iterator *vmi, 521 struct vm_area_struct *prev, struct vm_area_struct *vma, 522 unsigned long start, unsigned long end, 523 struct mempolicy *new_pol); 524 525 /** 526 * vma_modify_flags_uffd() - Perform any necessary split/merge in preparation for 527 * setting VMA flags to @vm_flags and UFFD context to @new_ctx in the range 528 * @start to @end contained within @vma. 529 * @vmi: Valid VMA iterator positioned at @vma. 530 * @prev: The VMA immediately prior to @vma or NULL if @vma is the first. 531 * @vma: The VMA containing the range @start to @end to be updated. 532 * @start: The start of the range to update. May be offset within @vma. 533 * @end: The exclusive end of the range to update, may be offset within @vma. 534 * @vma_flags: The VMA flags that the @start to @end range is about to be set to. 535 * @new_ctx: The userfaultfd context that the @start to @end range is about to 536 * be set to. 537 * @give_up_on_oom: If an out of memory condition occurs on merge, simply give 538 * up on it and treat the merge as best-effort. 539 * 540 * IMPORTANT: The actual modification being requested here is NOT applied, 541 * rather the VMA is perhaps split, perhaps merged to accommodate the change, 542 * and the caller is expected to perform the actual modification. 543 * 544 * Returns: A VMA which contains the range @start to @end ready to have its VMA 545 * flags changed to @vma_flags and its userfaultfd context changed to @new_ctx. 546 */ 547 __must_check struct vm_area_struct *vma_modify_flags_uffd(struct vma_iterator *vmi, 548 struct vm_area_struct *prev, struct vm_area_struct *vma, 549 unsigned long start, unsigned long end, const vma_flags_t *vma_flags, 550 struct vm_userfaultfd_ctx new_ctx, bool give_up_on_oom); 551 552 __must_check struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg); 553 554 __must_check struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi, 555 struct vm_area_struct *vma, unsigned long delta); 556 557 void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb); 558 559 void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb); 560 561 void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb, 562 struct vm_area_struct *vma); 563 564 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 565 unsigned long addr, unsigned long len, pgoff_t pgoff, 566 pgoff_t anon_pgoff, bool *need_rmap_locks, bool keep_source); 567 568 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma); 569 570 bool vma_needs_dirty_tracking(struct vm_area_struct *vma); 571 bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot); 572 573 int mm_take_all_locks(struct mm_struct *mm); 574 void mm_drop_all_locks(struct mm_struct *mm); 575 576 unsigned long mmap_region(struct file *file, unsigned long addr, 577 unsigned long len, vma_flags_t vma_flags, unsigned long pgoff, 578 struct list_head *uf); 579 580 int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, 581 unsigned long addr, unsigned long request, 582 vma_flags_t vma_flags); 583 584 unsigned long unmapped_area(struct vm_unmapped_area_info *info); 585 unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info); 586 587 #ifdef CONFIG_MMU 588 int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 589 unsigned long addr, int new_below); 590 #endif 591 592 static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma) 593 { 594 /* 595 * We want to check manually if we can change individual PTEs writable 596 * if we can't do that automatically for all PTEs in a mapping. For 597 * private mappings, that's always the case when we have write 598 * permissions as we properly have to handle COW. 599 */ 600 if (vma->vm_flags & VM_SHARED) 601 return vma_wants_writenotify(vma, vma->vm_page_prot); 602 return !!(vma->vm_flags & VM_WRITE); 603 } 604 605 #ifdef CONFIG_MMU 606 static inline pgprot_t vma_pgprot_modify(pgprot_t oldprot, vma_flags_t vma_flags) 607 { 608 const pgprot_t prot = vma_flags_to_page_prot(vma_flags); 609 610 return pgprot_modify(oldprot, prot); 611 } 612 #endif 613 614 static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, 615 unsigned long min) 616 { 617 return mas_prev(&vmi->mas, min); 618 } 619 620 /* 621 * These three helpers classifies VMAs for virtual memory accounting. 622 */ 623 624 /* 625 * Executable code area - executable, not writable, not stack 626 */ 627 static inline bool is_exec_mapping(vm_flags_t flags) 628 { 629 return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC; 630 } 631 632 /* 633 * Stack area (including shadow stacks) 634 * 635 * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous: 636 * do_mmap() forbids all other combinations. 637 */ 638 static inline bool is_stack_mapping(vm_flags_t flags) 639 { 640 return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK); 641 } 642 643 /* 644 * Data area - private, writable, not stack 645 */ 646 static inline bool is_data_mapping(vm_flags_t flags) 647 { 648 return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE; 649 } 650 651 static inline bool is_data_mapping_vma_flags(const vma_flags_t *vma_flags) 652 { 653 return vma_flags_test(vma_flags, VMA_WRITE_BIT) && 654 !vma_flags_test_any(vma_flags, VMA_SHARED_BIT, VMA_STACK_BIT); 655 } 656 657 static inline void vma_iter_config(struct vma_iterator *vmi, 658 unsigned long index, unsigned long last) 659 { 660 __mas_set_range(&vmi->mas, index, last - 1); 661 } 662 663 static inline void vma_iter_reset(struct vma_iterator *vmi) 664 { 665 mas_reset(&vmi->mas); 666 } 667 668 static inline 669 struct vm_area_struct *vma_iter_prev_range_limit(struct vma_iterator *vmi, unsigned long min) 670 { 671 return mas_prev_range(&vmi->mas, min); 672 } 673 674 static inline 675 struct vm_area_struct *vma_iter_next_range_limit(struct vma_iterator *vmi, unsigned long max) 676 { 677 return mas_next_range(&vmi->mas, max); 678 } 679 680 static inline int vma_iter_area_lowest(struct vma_iterator *vmi, unsigned long min, 681 unsigned long max, unsigned long size) 682 { 683 return mas_empty_area(&vmi->mas, min, max - 1, size); 684 } 685 686 static inline int vma_iter_area_highest(struct vma_iterator *vmi, unsigned long min, 687 unsigned long max, unsigned long size) 688 { 689 return mas_empty_area_rev(&vmi->mas, min, max - 1, size); 690 } 691 692 /* 693 * VMA Iterator functions shared between nommu and mmap 694 */ 695 static inline int vma_iter_prealloc(struct vma_iterator *vmi, 696 struct vm_area_struct *vma) 697 { 698 return mas_preallocate(&vmi->mas, vma, GFP_KERNEL); 699 } 700 701 static inline void vma_iter_clear(struct vma_iterator *vmi) 702 { 703 mas_store_prealloc(&vmi->mas, NULL); 704 } 705 706 static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) 707 { 708 return mas_walk(&vmi->mas); 709 } 710 711 /* Store a VMA with preallocated memory */ 712 static inline void vma_iter_store_overwrite(struct vma_iterator *vmi, 713 struct vm_area_struct *vma) 714 { 715 vma_assert_attached(vma); 716 717 #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 718 if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start && 719 vmi->mas.index > vma->vm_start)) { 720 pr_warn("%lx > %lx\n store vma %lx-%lx\n into slot %lx-%lx\n", 721 vmi->mas.index, vma->vm_start, vma->vm_start, 722 vma->vm_end, vmi->mas.index, vmi->mas.last); 723 } 724 if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start && 725 vmi->mas.last < vma->vm_start)) { 726 pr_warn("%lx < %lx\nstore vma %lx-%lx\ninto slot %lx-%lx\n", 727 vmi->mas.last, vma->vm_start, vma->vm_start, vma->vm_end, 728 vmi->mas.index, vmi->mas.last); 729 } 730 #endif 731 732 if (vmi->mas.status != ma_start && 733 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start))) 734 vma_iter_invalidate(vmi); 735 736 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); 737 mas_store_prealloc(&vmi->mas, vma); 738 } 739 740 static inline void vma_iter_store_new(struct vma_iterator *vmi, 741 struct vm_area_struct *vma) 742 { 743 vma_mark_attached(vma); 744 vma_iter_store_overwrite(vmi, vma); 745 } 746 747 static inline unsigned long vma_iter_addr(struct vma_iterator *vmi) 748 { 749 return vmi->mas.index; 750 } 751 752 static inline unsigned long vma_iter_end(struct vma_iterator *vmi) 753 { 754 return vmi->mas.last + 1; 755 } 756 757 static inline 758 struct vm_area_struct *vma_iter_prev_range(struct vma_iterator *vmi) 759 { 760 return mas_prev_range(&vmi->mas, 0); 761 } 762 763 /* 764 * Retrieve the next VMA and rewind the iterator to end of the previous VMA, or 765 * if no previous VMA, to index 0. 766 */ 767 static inline 768 struct vm_area_struct *vma_iter_next_rewind(struct vma_iterator *vmi, 769 struct vm_area_struct **pprev) 770 { 771 struct vm_area_struct *next = vma_next(vmi); 772 struct vm_area_struct *prev = vma_prev(vmi); 773 774 /* 775 * Consider the case where no previous VMA exists. We advance to the 776 * next VMA, skipping any gap, then rewind to the start of the range. 777 * 778 * If we were to unconditionally advance to the next range we'd wind up 779 * at the next VMA again, so we check to ensure there is a previous VMA 780 * to skip over. 781 */ 782 if (prev) 783 vma_iter_next_range(vmi); 784 785 if (pprev) 786 *pprev = prev; 787 788 return next; 789 } 790 791 #ifdef CONFIG_64BIT 792 static inline bool vma_is_sealed(struct vm_area_struct *vma) 793 { 794 return (vma->vm_flags & VM_SEALED); 795 } 796 #else 797 static inline bool vma_is_sealed(struct vm_area_struct *vma) 798 { 799 return false; 800 } 801 #endif 802 803 #if defined(CONFIG_STACK_GROWSUP) 804 int expand_upwards(struct vm_area_struct *vma, unsigned long address); 805 #endif 806 807 int expand_downwards(struct vm_area_struct *vma, unsigned long address); 808 809 int __vm_munmap(unsigned long start, size_t len, bool unlock); 810 811 int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma); 812 813 /* vma_init.h, shared between CONFIG_MMU and nommu. */ 814 void __init vma_state_init(void); 815 struct vm_area_struct *vm_area_alloc(struct mm_struct *mm); 816 struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig); 817 void vm_area_free(struct vm_area_struct *vma); 818 819 #ifdef CONFIG_MMU 820 int mmap_prepare_validate(const struct vm_area_desc *orig_desc, 821 const struct vm_area_desc *desc); 822 823 int mmap_hook_validate(unsigned long orig_start, unsigned long orig_end, 824 const vma_flags_t *orig_flags, 825 const struct vm_area_struct *vma); 826 827 /* vma_exec.c */ 828 int create_init_stack_vma(struct mm_struct *mm, struct vm_area_struct **vmap, 829 unsigned long *top_mem_p); 830 int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift); 831 832 /* 833 * Denies creating a writable executable mapping or gaining executable permissions. 834 * 835 * This denies the following: 836 * 837 * a) mmap(PROT_WRITE | PROT_EXEC) 838 * 839 * b) mmap(PROT_WRITE) 840 * mprotect(PROT_EXEC) 841 * 842 * c) mmap(PROT_WRITE) 843 * mprotect(PROT_READ) 844 * mprotect(PROT_EXEC) 845 * 846 * But allows the following: 847 * 848 * d) mmap(PROT_READ | PROT_EXEC) 849 * mmap(PROT_READ | PROT_EXEC | PROT_BTI) 850 * 851 * This is only applicable if the user has set the Memory-Deny-Write-Execute 852 * (MDWE) protection mask for the current process. 853 * 854 * @old specifies the VMA flags the VMA originally possessed, and @new the ones 855 * we propose to set. 856 * 857 * Return: false if proposed change is OK, true if not ok and should be denied. 858 */ 859 static inline bool map_deny_write_exec(const vma_flags_t *old, 860 const vma_flags_t *new) 861 { 862 /* If MDWE is disabled, we have nothing to deny. */ 863 if (!mm_flags_test(MMF_HAS_MDWE, current->mm)) 864 return false; 865 866 /* If the new VMA is not executable, we have nothing to deny. */ 867 if (!vma_flags_test(new, VMA_EXEC_BIT)) 868 return false; 869 870 /* Under MDWE we do not accept newly writably executable VMAs... */ 871 if (vma_flags_test(new, VMA_WRITE_BIT)) 872 return true; 873 874 /* ...nor previously non-executable VMAs becoming executable. */ 875 if (!vma_flags_test(old, VMA_EXEC_BIT)) 876 return true; 877 878 return false; 879 } 880 #else 881 static inline int mmap_prepare_validate(const struct vm_area_desc *orig_desc, 882 const struct vm_area_desc *desc) 883 { 884 return 0; 885 } 886 887 static inline int mmap_hook_validate(unsigned long orig_start, 888 unsigned long orig_end, 889 const vma_flags_t *orig_flags, 890 const struct vm_area_struct *vma) 891 { 892 return 0; 893 } 894 #endif 895 896 struct vm_area_struct *__install_special_mapping(struct mm_struct *mm, 897 unsigned long addr, unsigned long len, 898 vm_flags_t vm_flags, void *priv, 899 const struct vm_operations_struct *ops); 900 901 #endif /* __MM_VMA_H */ 902