1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* internal.h: mm/ internal definitions 3 * 4 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 #ifndef __MM_INTERNAL_H 8 #define __MM_INTERNAL_H 9 10 #include <linux/fs.h> 11 #include <linux/khugepaged.h> 12 #include <linux/mm.h> 13 #include <linux/mm_inline.h> 14 #include <linux/mmu_notifier.h> 15 #include <linux/pagemap.h> 16 #include <linux/pagewalk.h> 17 #include <linux/rmap.h> 18 #include <linux/swap.h> 19 #include <linux/leafops.h> 20 #include <linux/swap_cgroup.h> 21 #include <linux/tracepoint-defs.h> 22 23 /* Internal core VMA manipulation functions. */ 24 #include "vma.h" 25 26 struct folio_batch; 27 28 /* 29 * Maintains state across a page table move. The operation assumes both source 30 * and destination VMAs already exist and are specified by the user. 31 * 32 * Partial moves are permitted, but the old and new ranges must both reside 33 * within a VMA. 34 * 35 * mmap lock must be held in write and VMA write locks must be held on any VMA 36 * that is visible. 37 * 38 * Use the PAGETABLE_MOVE() macro to initialise this struct. 39 * 40 * The old_addr and new_addr fields are updated as the page table move is 41 * executed. 42 * 43 * NOTE: The page table move is affected by reading from [old_addr, old_end), 44 * and old_addr may be updated for better page table alignment, so len_in 45 * represents the length of the range being copied as specified by the user. 46 */ 47 struct pagetable_move_control { 48 struct vm_area_struct *old; /* Source VMA. */ 49 struct vm_area_struct *new; /* Destination VMA. */ 50 unsigned long old_addr; /* Address from which the move begins. */ 51 unsigned long old_end; /* Exclusive address at which old range ends. */ 52 unsigned long new_addr; /* Address to move page tables to. */ 53 unsigned long len_in; /* Bytes to remap specified by user. */ 54 55 bool need_rmap_locks; /* Do rmap locks need to be taken? */ 56 bool for_stack; /* Is this an early temp stack being moved? */ 57 }; 58 59 #define PAGETABLE_MOVE(name, old_, new_, old_addr_, new_addr_, len_) \ 60 struct pagetable_move_control name = { \ 61 .old = old_, \ 62 .new = new_, \ 63 .old_addr = old_addr_, \ 64 .old_end = (old_addr_) + (len_), \ 65 .new_addr = new_addr_, \ 66 .len_in = len_, \ 67 } 68 69 /* 70 * The set of flags that only affect watermark checking and reclaim 71 * behaviour. This is used by the MM to obey the caller constraints 72 * about IO, FS and watermark checking while ignoring placement 73 * hints such as HIGHMEM usage. 74 */ 75 #define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\ 76 __GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\ 77 __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\ 78 __GFP_NOLOCKDEP) 79 80 /* The GFP flags allowed during early boot */ 81 #define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS)) 82 83 /* Control allocation cpuset and node placement constraints */ 84 #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE) 85 86 /* Do not use these with a slab allocator */ 87 #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK) 88 89 /* 90 * Different from WARN_ON_ONCE(), no warning will be issued 91 * when we specify __GFP_NOWARN. 92 */ 93 #define WARN_ON_ONCE_GFP(cond, gfp) ({ \ 94 static bool __section(".data..once") __warned; \ 95 int __ret_warn_once = !!(cond); \ 96 \ 97 if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \ 98 __warned = true; \ 99 WARN_ON(1); \ 100 } \ 101 unlikely(__ret_warn_once); \ 102 }) 103 104 void page_writeback_init(void); 105 106 /* 107 * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages, 108 * its nr_pages_mapped would be 0x400000: choose the ENTIRELY_MAPPED bit 109 * above that range, instead of 2*(PMD_SIZE/PAGE_SIZE). Hugetlb currently 110 * leaves nr_pages_mapped at 0, but avoid surprise if it participates later. 111 */ 112 #define ENTIRELY_MAPPED 0x800000 113 #define FOLIO_PAGES_MAPPED (ENTIRELY_MAPPED - 1) 114 115 /* 116 * Flags passed to __show_mem() and show_free_areas() to suppress output in 117 * various contexts. 118 */ 119 #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */ 120 121 /* 122 * How many individual pages have an elevated _mapcount. Excludes 123 * the folio's entire_mapcount. 124 * 125 * Don't use this function outside of debugging code. 126 */ 127 static inline int folio_nr_pages_mapped(const struct folio *folio) 128 { 129 if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) 130 return -1; 131 return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED; 132 } 133 134 /* 135 * Retrieve the first entry of a folio based on a provided entry within the 136 * folio. We cannot rely on folio->swap as there is no guarantee that it has 137 * been initialized. Used for calling arch_swap_restore() 138 */ 139 static inline swp_entry_t folio_swap(swp_entry_t entry, 140 const struct folio *folio) 141 { 142 swp_entry_t swap = { 143 .val = ALIGN_DOWN(entry.val, folio_nr_pages(folio)), 144 }; 145 146 return swap; 147 } 148 149 static inline void *folio_raw_mapping(const struct folio *folio) 150 { 151 unsigned long mapping = (unsigned long)folio->mapping; 152 153 return (void *)(mapping & ~FOLIO_MAPPING_FLAGS); 154 } 155 156 /* 157 * This is a file-backed mapping, and is about to be memory mapped - invoke its 158 * mmap hook and safely handle error conditions. On error, VMA hooks will be 159 * mutated. 160 * 161 * @file: File which backs the mapping. 162 * @vma: VMA which we are mapping. 163 * 164 * Returns: 0 if success, error otherwise. 165 */ 166 static inline int mmap_file(struct file *file, struct vm_area_struct *vma) 167 { 168 int err = vfs_mmap(file, vma); 169 170 if (likely(!err)) 171 return 0; 172 173 /* 174 * OK, we tried to call the file hook for mmap(), but an error 175 * arose. The mapping is in an inconsistent state and we must not invoke 176 * any further hooks on it. 177 */ 178 vma->vm_ops = &vma_dummy_vm_ops; 179 180 return err; 181 } 182 183 /* 184 * If the VMA has a close hook then close it, and since closing it might leave 185 * it in an inconsistent state which makes the use of any hooks suspect, clear 186 * them down by installing dummy empty hooks. 187 */ 188 static inline void vma_close(struct vm_area_struct *vma) 189 { 190 if (vma->vm_ops && vma->vm_ops->close) { 191 vma->vm_ops->close(vma); 192 193 /* 194 * The mapping is in an inconsistent state, and no further hooks 195 * may be invoked upon it. 196 */ 197 vma->vm_ops = &vma_dummy_vm_ops; 198 } 199 } 200 201 /* unmap_vmas is in mm/memory.c */ 202 void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap); 203 204 #ifdef CONFIG_MMU 205 206 static inline void get_anon_vma(struct anon_vma *anon_vma) 207 { 208 atomic_inc(&anon_vma->refcount); 209 } 210 211 void __put_anon_vma(struct anon_vma *anon_vma); 212 213 static inline void put_anon_vma(struct anon_vma *anon_vma) 214 { 215 if (atomic_dec_and_test(&anon_vma->refcount)) 216 __put_anon_vma(anon_vma); 217 } 218 219 static inline void anon_vma_lock_write(struct anon_vma *anon_vma) 220 { 221 down_write(&anon_vma->root->rwsem); 222 } 223 224 static inline int anon_vma_trylock_write(struct anon_vma *anon_vma) 225 { 226 return down_write_trylock(&anon_vma->root->rwsem); 227 } 228 229 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma) 230 { 231 up_write(&anon_vma->root->rwsem); 232 } 233 234 static inline void anon_vma_lock_read(struct anon_vma *anon_vma) 235 { 236 down_read(&anon_vma->root->rwsem); 237 } 238 239 static inline int anon_vma_trylock_read(struct anon_vma *anon_vma) 240 { 241 return down_read_trylock(&anon_vma->root->rwsem); 242 } 243 244 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma) 245 { 246 up_read(&anon_vma->root->rwsem); 247 } 248 249 struct anon_vma *folio_get_anon_vma(const struct folio *folio); 250 251 /* Operations which modify VMAs. */ 252 enum vma_operation { 253 VMA_OP_SPLIT, 254 VMA_OP_MERGE_UNFAULTED, 255 VMA_OP_REMAP, 256 VMA_OP_FORK, 257 }; 258 259 int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src, 260 enum vma_operation operation); 261 int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma); 262 int __anon_vma_prepare(struct vm_area_struct *vma); 263 void unlink_anon_vmas(struct vm_area_struct *vma); 264 265 static inline int anon_vma_prepare(struct vm_area_struct *vma) 266 { 267 if (likely(vma->anon_vma)) 268 return 0; 269 270 return __anon_vma_prepare(vma); 271 } 272 273 /* Flags for folio_pte_batch(). */ 274 typedef int __bitwise fpb_t; 275 276 /* Compare PTEs respecting the dirty bit. */ 277 #define FPB_RESPECT_DIRTY ((__force fpb_t)BIT(0)) 278 279 /* Compare PTEs respecting the soft-dirty bit. */ 280 #define FPB_RESPECT_SOFT_DIRTY ((__force fpb_t)BIT(1)) 281 282 /* Compare PTEs respecting the writable bit. */ 283 #define FPB_RESPECT_WRITE ((__force fpb_t)BIT(2)) 284 285 /* 286 * Merge PTE write bits: if any PTE in the batch is writable, modify the 287 * PTE at @ptentp to be writable. 288 */ 289 #define FPB_MERGE_WRITE ((__force fpb_t)BIT(3)) 290 291 /* 292 * Merge PTE young and dirty bits: if any PTE in the batch is young or dirty, 293 * modify the PTE at @ptentp to be young or dirty, respectively. 294 */ 295 #define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(4)) 296 297 static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) 298 { 299 if (!(flags & FPB_RESPECT_DIRTY)) 300 pte = pte_mkclean(pte); 301 if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY))) 302 pte = pte_clear_soft_dirty(pte); 303 if (likely(!(flags & FPB_RESPECT_WRITE))) 304 pte = pte_wrprotect(pte); 305 return pte_mkold(pte); 306 } 307 308 /** 309 * folio_pte_batch_flags - detect a PTE batch for a large folio 310 * @folio: The large folio to detect a PTE batch for. 311 * @vma: The VMA. Only relevant with FPB_MERGE_WRITE, otherwise can be NULL. 312 * @ptep: Page table pointer for the first entry. 313 * @ptentp: Pointer to a COPY of the first page table entry whose flags this 314 * function updates based on @flags if appropriate. 315 * @max_nr: The maximum number of table entries to consider. 316 * @flags: Flags to modify the PTE batch semantics. 317 * 318 * Detect a PTE batch: consecutive (present) PTEs that map consecutive 319 * pages of the same large folio in a single VMA and a single page table. 320 * 321 * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN, 322 * the accessed bit, writable bit, dirty bit (unless FPB_RESPECT_DIRTY is set) 323 * and soft-dirty bit (unless FPB_RESPECT_SOFT_DIRTY is set). 324 * 325 * @ptep must map any page of the folio. max_nr must be at least one and 326 * must be limited by the caller so scanning cannot exceed a single VMA and 327 * a single page table. 328 * 329 * Depending on the FPB_MERGE_* flags, the pte stored at @ptentp will 330 * be updated: it's crucial that a pointer to a COPY of the first 331 * page table entry, obtained through ptep_get(), is provided as @ptentp. 332 * 333 * This function will be inlined to optimize based on the input parameters; 334 * consider using folio_pte_batch() instead if applicable. 335 * 336 * Return: the number of table entries in the batch. 337 */ 338 static inline unsigned int folio_pte_batch_flags(struct folio *folio, 339 struct vm_area_struct *vma, pte_t *ptep, pte_t *ptentp, 340 unsigned int max_nr, fpb_t flags) 341 { 342 bool any_writable = false, any_young = false, any_dirty = false; 343 pte_t expected_pte, pte = *ptentp; 344 unsigned int nr, cur_nr; 345 346 VM_WARN_ON_FOLIO(!pte_present(pte), folio); 347 VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio); 348 VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio); 349 /* 350 * Ensure this is a pointer to a copy not a pointer into a page table. 351 * If this is a stack value, it won't be a valid virtual address, but 352 * that's fine because it also cannot be pointing into the page table. 353 */ 354 VM_WARN_ON(virt_addr_valid(ptentp) && PageTable(virt_to_page(ptentp))); 355 356 /* Limit max_nr to the actual remaining PFNs in the folio we could batch. */ 357 max_nr = min_t(unsigned long, max_nr, 358 folio_pfn(folio) + folio_nr_pages(folio) - pte_pfn(pte)); 359 360 nr = pte_batch_hint(ptep, pte); 361 expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); 362 ptep = ptep + nr; 363 364 while (nr < max_nr) { 365 pte = ptep_get(ptep); 366 367 if (!pte_same(__pte_batch_clear_ignored(pte, flags), expected_pte)) 368 break; 369 370 if (flags & FPB_MERGE_WRITE) 371 any_writable |= pte_write(pte); 372 if (flags & FPB_MERGE_YOUNG_DIRTY) { 373 any_young |= pte_young(pte); 374 any_dirty |= pte_dirty(pte); 375 } 376 377 cur_nr = pte_batch_hint(ptep, pte); 378 expected_pte = pte_advance_pfn(expected_pte, cur_nr); 379 ptep += cur_nr; 380 nr += cur_nr; 381 } 382 383 if (any_writable) 384 *ptentp = pte_mkwrite(*ptentp, vma); 385 if (any_young) 386 *ptentp = pte_mkyoung(*ptentp); 387 if (any_dirty) 388 *ptentp = pte_mkdirty(*ptentp); 389 390 return min(nr, max_nr); 391 } 392 393 unsigned int folio_pte_batch(struct folio *folio, pte_t *ptep, pte_t pte, 394 unsigned int max_nr); 395 396 /** 397 * pte_move_swp_offset - Move the swap entry offset field of a swap pte 398 * forward or backward by delta 399 * @pte: The initial pte state; must be a swap entry 400 * @delta: The direction and the offset we are moving; forward if delta 401 * is positive; backward if delta is negative 402 * 403 * Moves the swap offset, while maintaining all other fields, including 404 * swap type, and any swp pte bits. The resulting pte is returned. 405 */ 406 static inline pte_t pte_move_swp_offset(pte_t pte, long delta) 407 { 408 const softleaf_t entry = softleaf_from_pte(pte); 409 pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry), 410 (swp_offset(entry) + delta))); 411 412 if (pte_swp_soft_dirty(pte)) 413 new = pte_swp_mksoft_dirty(new); 414 if (pte_swp_exclusive(pte)) 415 new = pte_swp_mkexclusive(new); 416 if (pte_swp_uffd_wp(pte)) 417 new = pte_swp_mkuffd_wp(new); 418 419 return new; 420 } 421 422 423 /** 424 * pte_next_swp_offset - Increment the swap entry offset field of a swap pte. 425 * @pte: The initial pte state; must be a swap entry. 426 * 427 * Increments the swap offset, while maintaining all other fields, including 428 * swap type, and any swp pte bits. The resulting pte is returned. 429 */ 430 static inline pte_t pte_next_swp_offset(pte_t pte) 431 { 432 return pte_move_swp_offset(pte, 1); 433 } 434 435 /** 436 * swap_pte_batch - detect a PTE batch for a set of contiguous swap entries 437 * @start_ptep: Page table pointer for the first entry. 438 * @max_nr: The maximum number of table entries to consider. 439 * @pte: Page table entry for the first entry. 440 * 441 * Detect a batch of contiguous swap entries: consecutive (non-present) PTEs 442 * containing swap entries all with consecutive offsets and targeting the same 443 * swap type, all with matching swp pte bits. 444 * 445 * max_nr must be at least one and must be limited by the caller so scanning 446 * cannot exceed a single page table. 447 * 448 * Return: the number of table entries in the batch. 449 */ 450 static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte) 451 { 452 pte_t expected_pte = pte_next_swp_offset(pte); 453 const pte_t *end_ptep = start_ptep + max_nr; 454 const softleaf_t entry = softleaf_from_pte(pte); 455 pte_t *ptep = start_ptep + 1; 456 unsigned short cgroup_id; 457 458 VM_WARN_ON(max_nr < 1); 459 VM_WARN_ON(!softleaf_is_swap(entry)); 460 461 cgroup_id = lookup_swap_cgroup_id(entry); 462 while (ptep < end_ptep) { 463 softleaf_t entry; 464 465 pte = ptep_get(ptep); 466 467 if (!pte_same(pte, expected_pte)) 468 break; 469 entry = softleaf_from_pte(pte); 470 if (lookup_swap_cgroup_id(entry) != cgroup_id) 471 break; 472 expected_pte = pte_next_swp_offset(expected_pte); 473 ptep++; 474 } 475 476 return ptep - start_ptep; 477 } 478 #endif /* CONFIG_MMU */ 479 480 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 481 int nr_throttled); 482 static inline void acct_reclaim_writeback(struct folio *folio) 483 { 484 pg_data_t *pgdat = folio_pgdat(folio); 485 int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled); 486 487 if (nr_throttled) 488 __acct_reclaim_writeback(pgdat, folio, nr_throttled); 489 } 490 491 static inline void wake_throttle_isolated(pg_data_t *pgdat) 492 { 493 wait_queue_head_t *wqh; 494 495 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED]; 496 if (waitqueue_active(wqh)) 497 wake_up(wqh); 498 } 499 500 vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf); 501 static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) 502 { 503 vm_fault_t ret = __vmf_anon_prepare(vmf); 504 505 if (unlikely(ret & VM_FAULT_RETRY)) 506 vma_end_read(vmf->vma); 507 return ret; 508 } 509 510 vm_fault_t do_swap_page(struct vm_fault *vmf); 511 void folio_rotate_reclaimable(struct folio *folio); 512 bool __folio_end_writeback(struct folio *folio); 513 void deactivate_file_folio(struct folio *folio); 514 void folio_activate(struct folio *folio); 515 516 void free_pgtables(struct mmu_gather *tlb, struct unmap_desc *desc); 517 518 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); 519 520 /** 521 * sync_with_folio_pmd_zap - sync with concurrent zapping of a folio PMD 522 * @mm: The mm_struct. 523 * @pmdp: Pointer to the pmd that was found to be pmd_none(). 524 * 525 * When we find a pmd_none() while unmapping a folio without holding the PTL, 526 * zap_huge_pmd() may have cleared the PMD but not yet modified the folio to 527 * indicate that it's unmapped. Skipping the PMD without synchronization could 528 * make folio unmapping code assume that unmapping failed. 529 * 530 * Wait for concurrent zapping to complete by grabbing the PTL. 531 */ 532 static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) 533 { 534 spinlock_t *ptl = pmd_lock(mm, pmdp); 535 536 spin_unlock(ptl); 537 } 538 539 struct zap_details; 540 void zap_vma_range_batched(struct mmu_gather *tlb, 541 struct vm_area_struct *vma, unsigned long addr, 542 unsigned long size, struct zap_details *details); 543 int zap_vma_for_reaping(struct vm_area_struct *vma); 544 int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, 545 gfp_t gfp); 546 547 void page_cache_ra_order(struct readahead_control *, struct file_ra_state *); 548 void force_page_cache_ra(struct readahead_control *, unsigned long nr); 549 static inline void force_page_cache_readahead(struct address_space *mapping, 550 struct file *file, pgoff_t index, unsigned long nr_to_read) 551 { 552 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index); 553 force_page_cache_ra(&ractl, nr_to_read); 554 } 555 556 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, 557 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 558 unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, 559 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 560 int truncate_inode_folio(struct address_space *mapping, struct folio *folio); 561 bool truncate_inode_partial_folio(struct folio *folio, loff_t start, 562 loff_t end); 563 long mapping_evict_folio(struct address_space *mapping, struct folio *folio); 564 unsigned long mapping_try_invalidate(struct address_space *mapping, 565 pgoff_t start, pgoff_t end, unsigned long *nr_failed); 566 567 /** 568 * folio_evictable - Test whether a folio is evictable. 569 * @folio: The folio to test. 570 * 571 * Test whether @folio is evictable -- i.e., should be placed on 572 * active/inactive lists vs unevictable list. 573 * 574 * Reasons folio might not be evictable: 575 * 1. folio's mapping marked unevictable 576 * 2. One of the pages in the folio is part of an mlocked VMA 577 */ 578 static inline bool folio_evictable(struct folio *folio) 579 { 580 bool ret; 581 582 /* Prevent address_space of inode and swap cache from being freed */ 583 rcu_read_lock(); 584 ret = !mapping_unevictable(folio_mapping(folio)) && 585 !folio_test_mlocked(folio); 586 rcu_read_unlock(); 587 return ret; 588 } 589 590 /* 591 * Turn a non-refcounted page (->_refcount == 0) into refcounted with 592 * a count of one. 593 */ 594 static inline void set_page_refcounted(struct page *page) 595 { 596 VM_BUG_ON_PAGE(PageTail(page), page); 597 VM_BUG_ON_PAGE(page_ref_count(page), page); 598 set_page_count(page, 1); 599 } 600 601 static inline void set_pages_refcounted(struct page *page, unsigned long nr_pages) 602 { 603 unsigned long pfn = page_to_pfn(page); 604 605 for (; nr_pages--; pfn++) 606 set_page_refcounted(pfn_to_page(pfn)); 607 } 608 609 /* 610 * Return true if a folio needs ->release_folio() calling upon it. 611 */ 612 static inline bool folio_needs_release(struct folio *folio) 613 { 614 struct address_space *mapping = folio_mapping(folio); 615 616 return folio_has_private(folio) || 617 (mapping && mapping_release_always(mapping)); 618 } 619 620 extern unsigned long highest_memmap_pfn; 621 622 /* 623 * Maximum number of reclaim retries without progress before the OOM 624 * killer is consider the only way forward. 625 */ 626 #define MAX_RECLAIM_RETRIES 16 627 628 /* 629 * in mm/vmscan.c: 630 */ 631 bool folio_isolate_lru(struct folio *folio); 632 void folio_putback_lru(struct folio *folio); 633 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason); 634 int user_proactive_reclaim(char *buf, 635 struct mem_cgroup *memcg, pg_data_t *pgdat); 636 637 /* 638 * in mm/rmap.c: 639 */ 640 pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address); 641 642 /* 643 * in mm/khugepaged.c 644 */ 645 void set_recommended_min_free_kbytes(void); 646 647 /* 648 * in mm/page_alloc.c 649 */ 650 #define K(x) ((x) << (PAGE_SHIFT-10)) 651 652 extern char * const zone_names[MAX_NR_ZONES]; 653 654 /* perform sanity checks on struct pages being allocated or freed */ 655 DECLARE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled); 656 657 extern int min_free_kbytes; 658 extern int defrag_mode; 659 660 void setup_per_zone_wmarks(void); 661 void calculate_min_free_kbytes(void); 662 int __meminit init_per_zone_wmark_min(void); 663 void page_alloc_sysctl_init(void); 664 665 /* 666 * Structure for holding the mostly immutable allocation parameters passed 667 * between functions involved in allocations, including the alloc_pages* 668 * family of functions. 669 * 670 * nodemask, migratetype and highest_zoneidx are initialized only once in 671 * __alloc_pages() and then never change. 672 * 673 * zonelist, preferred_zone and highest_zoneidx are set first in 674 * __alloc_pages() for the fast path, and might be later changed 675 * in __alloc_pages_slowpath(). All other functions pass the whole structure 676 * by a const pointer. 677 */ 678 struct alloc_context { 679 struct zonelist *zonelist; 680 nodemask_t *nodemask; 681 struct zoneref *preferred_zoneref; 682 int migratetype; 683 684 /* 685 * highest_zoneidx represents highest usable zone index of 686 * the allocation request. Due to the nature of the zone, 687 * memory on lower zone than the highest_zoneidx will be 688 * protected by lowmem_reserve[highest_zoneidx]. 689 * 690 * highest_zoneidx is also used by reclaim/compaction to limit 691 * the target zone since higher zone than this index cannot be 692 * usable for this allocation request. 693 */ 694 enum zone_type highest_zoneidx; 695 bool spread_dirty_pages; 696 }; 697 698 /* 699 * This function returns the order of a free page in the buddy system. In 700 * general, page_zone(page)->lock must be held by the caller to prevent the 701 * page from being allocated in parallel and returning garbage as the order. 702 * If a caller does not hold page_zone(page)->lock, it must guarantee that the 703 * page cannot be allocated or merged in parallel. Alternatively, it must 704 * handle invalid values gracefully, and use buddy_order_unsafe() below. 705 */ 706 static inline unsigned int buddy_order(struct page *page) 707 { 708 /* PageBuddy() must be checked by the caller */ 709 return page_private(page); 710 } 711 712 /* 713 * Like buddy_order(), but for callers who cannot afford to hold the zone lock. 714 * PageBuddy() should be checked first by the caller to minimize race window, 715 * and invalid values must be handled gracefully. 716 * 717 * READ_ONCE is used so that if the caller assigns the result into a local 718 * variable and e.g. tests it for valid range before using, the compiler cannot 719 * decide to remove the variable and inline the page_private(page) multiple 720 * times, potentially observing different values in the tests and the actual 721 * use of the result. 722 */ 723 #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) 724 725 /* 726 * This function checks whether a page is free && is the buddy 727 * we can coalesce a page and its buddy if 728 * (a) the buddy is not in a hole (check before calling!) && 729 * (b) the buddy is in the buddy system && 730 * (c) a page and its buddy have the same order && 731 * (d) a page and its buddy are in the same zone. 732 * 733 * For recording whether a page is in the buddy system, we set PageBuddy. 734 * Setting, clearing, and testing PageBuddy is serialized by zone->lock. 735 * 736 * For recording page's order, we use page_private(page). 737 */ 738 static inline bool page_is_buddy(struct page *page, struct page *buddy, 739 unsigned int order) 740 { 741 if (!page_is_guard(buddy) && !PageBuddy(buddy)) 742 return false; 743 744 if (buddy_order(buddy) != order) 745 return false; 746 747 /* 748 * zone check is done late to avoid uselessly calculating 749 * zone/node ids for pages that could never merge. 750 */ 751 if (page_zone_id(page) != page_zone_id(buddy)) 752 return false; 753 754 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); 755 756 return true; 757 } 758 759 /* 760 * Locate the struct page for both the matching buddy in our 761 * pair (buddy1) and the combined O(n+1) page they form (page). 762 * 763 * 1) Any buddy B1 will have an order O twin B2 which satisfies 764 * the following equation: 765 * B2 = B1 ^ (1 << O) 766 * For example, if the starting buddy (buddy2) is #8 its order 767 * 1 buddy is #10: 768 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10 769 * 770 * 2) Any buddy B will have an order O+1 parent P which 771 * satisfies the following equation: 772 * P = B & ~(1 << O) 773 * 774 * Assumption: *_mem_map is contiguous at least up to MAX_PAGE_ORDER 775 */ 776 static inline unsigned long 777 __find_buddy_pfn(unsigned long page_pfn, unsigned int order) 778 { 779 return page_pfn ^ (1 << order); 780 } 781 782 /* 783 * Find the buddy of @page and validate it. 784 * @page: The input page 785 * @pfn: The pfn of the page, it saves a call to page_to_pfn() when the 786 * function is used in the performance-critical __free_one_page(). 787 * @order: The order of the page 788 * @buddy_pfn: The output pointer to the buddy pfn, it also saves a call to 789 * page_to_pfn(). 790 * 791 * The found buddy can be a non PageBuddy, out of @page's zone, or its order is 792 * not the same as @page. The validation is necessary before use it. 793 * 794 * Return: the found buddy page or NULL if not found. 795 */ 796 static inline struct page *find_buddy_page_pfn(struct page *page, 797 unsigned long pfn, unsigned int order, unsigned long *buddy_pfn) 798 { 799 unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order); 800 struct page *buddy; 801 802 buddy = page + (__buddy_pfn - pfn); 803 if (buddy_pfn) 804 *buddy_pfn = __buddy_pfn; 805 806 if (page_is_buddy(page, buddy, order)) 807 return buddy; 808 return NULL; 809 } 810 811 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn, 812 unsigned long end_pfn, struct zone *zone); 813 814 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, 815 unsigned long end_pfn, struct zone *zone) 816 { 817 if (zone->contiguous) 818 return pfn_to_page(start_pfn); 819 820 return __pageblock_pfn_to_page(start_pfn, end_pfn, zone); 821 } 822 823 void set_zone_contiguous(struct zone *zone); 824 bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, 825 unsigned long nr_pages); 826 827 static inline void clear_zone_contiguous(struct zone *zone) 828 { 829 zone->contiguous = false; 830 } 831 832 extern int __isolate_free_page(struct page *page, unsigned int order); 833 extern void __putback_isolated_page(struct page *page, unsigned int order, 834 int mt); 835 extern void memblock_free_pages(unsigned long pfn, unsigned int order); 836 extern void __free_pages_core(struct page *page, unsigned int order, 837 enum meminit_context context); 838 839 /* 840 * This will have no effect, other than possibly generating a warning, if the 841 * caller passes in a non-large folio. 842 */ 843 static inline void folio_set_order(struct folio *folio, unsigned int order) 844 { 845 if (WARN_ON_ONCE(!order || !folio_test_large(folio))) 846 return; 847 VM_WARN_ON_ONCE(order > MAX_FOLIO_ORDER); 848 849 folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order; 850 #ifdef NR_PAGES_IN_LARGE_FOLIO 851 folio->_nr_pages = 1U << order; 852 #endif 853 } 854 855 bool __folio_unqueue_deferred_split(struct folio *folio); 856 static inline bool folio_unqueue_deferred_split(struct folio *folio) 857 { 858 if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio)) 859 return false; 860 861 /* 862 * At this point, there is no one trying to add the folio to 863 * deferred_list. If folio is not in deferred_list, it's safe 864 * to check without acquiring the split_queue_lock. 865 */ 866 if (data_race(list_empty(&folio->_deferred_list))) 867 return false; 868 869 return __folio_unqueue_deferred_split(folio); 870 } 871 872 static inline struct folio *page_rmappable_folio(struct page *page) 873 { 874 struct folio *folio = (struct folio *)page; 875 876 if (folio && folio_test_large(folio)) 877 folio_set_large_rmappable(folio); 878 return folio; 879 } 880 881 static inline void prep_compound_head(struct page *page, unsigned int order) 882 { 883 struct folio *folio = (struct folio *)page; 884 885 folio_set_order(folio, order); 886 atomic_set(&folio->_large_mapcount, -1); 887 if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) 888 atomic_set(&folio->_nr_pages_mapped, 0); 889 if (IS_ENABLED(CONFIG_MM_ID)) { 890 folio->_mm_ids = 0; 891 folio->_mm_id_mapcount[0] = -1; 892 folio->_mm_id_mapcount[1] = -1; 893 } 894 if (IS_ENABLED(CONFIG_64BIT) || order > 1) { 895 atomic_set(&folio->_pincount, 0); 896 atomic_set(&folio->_entire_mapcount, -1); 897 } 898 if (order > 1) 899 INIT_LIST_HEAD(&folio->_deferred_list); 900 } 901 902 static inline void prep_compound_tail(struct page *tail, 903 const struct page *head, unsigned int order) 904 { 905 tail->mapping = TAIL_MAPPING; 906 set_compound_head(tail, head, order); 907 set_page_private(tail, 0); 908 } 909 910 static inline void init_compound_tail(struct page *tail, 911 const struct page *head, unsigned int order, struct zone *zone) 912 { 913 atomic_set(&tail->_mapcount, -1); 914 set_page_node(tail, zone_to_nid(zone)); 915 set_page_zone(tail, zone_idx(zone)); 916 prep_compound_tail(tail, head, order); 917 } 918 919 void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags); 920 extern bool free_pages_prepare(struct page *page, unsigned int order); 921 922 extern int user_min_free_kbytes; 923 924 struct page *__alloc_frozen_pages_noprof(gfp_t, unsigned int order, int nid, 925 nodemask_t *); 926 #define __alloc_frozen_pages(...) \ 927 alloc_hooks(__alloc_frozen_pages_noprof(__VA_ARGS__)) 928 void free_frozen_pages(struct page *page, unsigned int order); 929 void free_unref_folios(struct folio_batch *fbatch); 930 931 #ifdef CONFIG_NUMA 932 struct page *alloc_frozen_pages_noprof(gfp_t, unsigned int order); 933 #else 934 static inline struct page *alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order) 935 { 936 return __alloc_frozen_pages_noprof(gfp, order, numa_node_id(), NULL); 937 } 938 #endif 939 940 #define alloc_frozen_pages(...) \ 941 alloc_hooks(alloc_frozen_pages_noprof(__VA_ARGS__)) 942 943 struct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order); 944 #define alloc_frozen_pages_nolock(...) \ 945 alloc_hooks(alloc_frozen_pages_nolock_noprof(__VA_ARGS__)) 946 void free_frozen_pages_nolock(struct page *page, unsigned int order); 947 948 extern void zone_pcp_reset(struct zone *zone); 949 extern void zone_pcp_disable(struct zone *zone); 950 extern void zone_pcp_enable(struct zone *zone); 951 extern void zone_pcp_init(struct zone *zone); 952 953 extern void *memmap_alloc(phys_addr_t size, phys_addr_t align, 954 phys_addr_t min_addr, 955 int nid, bool exact_nid); 956 957 void memmap_init_range(unsigned long, int, unsigned long, unsigned long, 958 unsigned long, enum meminit_context, struct vmem_altmap *, int, 959 bool); 960 961 /* 962 * mm/sparse.c 963 */ 964 #ifdef CONFIG_SPARSEMEM 965 void sparse_init(void); 966 int sparse_index_init(unsigned long section_nr, int nid); 967 968 static inline void sparse_init_one_section(struct mem_section *ms, 969 unsigned long pnum, struct page *mem_map, 970 struct mem_section_usage *usage, unsigned long flags) 971 { 972 unsigned long coded_mem_map; 973 974 BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT); 975 976 /* 977 * We encode the start PFN of the section into the mem_map such that 978 * page_to_pfn() on !CONFIG_SPARSEMEM_VMEMMAP can simply subtract it 979 * from the page pointer to obtain the PFN. 980 */ 981 coded_mem_map = (unsigned long)(mem_map - section_nr_to_pfn(pnum)); 982 VM_WARN_ON_ONCE(coded_mem_map & ~SECTION_MAP_MASK); 983 984 ms->section_mem_map &= ~SECTION_MAP_MASK; 985 ms->section_mem_map |= coded_mem_map; 986 ms->section_mem_map |= flags | SECTION_HAS_MEM_MAP; 987 ms->usage = usage; 988 } 989 990 static inline void __section_mark_present(struct mem_section *ms, 991 unsigned long section_nr) 992 { 993 if (section_nr > __highest_present_section_nr) 994 __highest_present_section_nr = section_nr; 995 996 ms->section_mem_map |= SECTION_MARKED_PRESENT; 997 } 998 #else 999 static inline void sparse_init(void) {} 1000 #endif /* CONFIG_SPARSEMEM */ 1001 1002 /* 1003 * mm/sparse-vmemmap.c 1004 */ 1005 #ifdef CONFIG_SPARSEMEM_VMEMMAP 1006 void sparse_init_subsection_map(unsigned long pfn, unsigned long nr_pages); 1007 #else 1008 static inline void sparse_init_subsection_map(unsigned long pfn, 1009 unsigned long nr_pages) 1010 { 1011 } 1012 #endif /* CONFIG_SPARSEMEM_VMEMMAP */ 1013 1014 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 1015 1016 /* 1017 * in mm/compaction.c 1018 */ 1019 /* 1020 * compact_control is used to track pages being migrated and the free pages 1021 * they are being migrated to during memory compaction. The free_pfn starts 1022 * at the end of a zone and migrate_pfn begins at the start. Movable pages 1023 * are moved to the end of a zone during a compaction run and the run 1024 * completes when free_pfn <= migrate_pfn 1025 */ 1026 struct compact_control { 1027 struct list_head freepages[NR_PAGE_ORDERS]; /* List of free pages to migrate to */ 1028 struct list_head migratepages; /* List of pages being migrated */ 1029 unsigned int nr_freepages; /* Number of isolated free pages */ 1030 unsigned int nr_migratepages; /* Number of pages to migrate */ 1031 unsigned long free_pfn; /* isolate_freepages search base */ 1032 /* 1033 * Acts as an in/out parameter to page isolation for migration. 1034 * isolate_migratepages uses it as a search base. 1035 * isolate_migratepages_block will update the value to the next pfn 1036 * after the last isolated one. 1037 */ 1038 unsigned long migrate_pfn; 1039 unsigned long fast_start_pfn; /* a pfn to start linear scan from */ 1040 struct zone *zone; 1041 unsigned long total_migrate_scanned; 1042 unsigned long total_free_scanned; 1043 unsigned short fast_search_fail;/* failures to use free list searches */ 1044 short search_order; /* order to start a fast search at */ 1045 const gfp_t gfp_mask; /* gfp mask of a direct compactor */ 1046 int order; /* order a direct compactor needs */ 1047 int migratetype; /* migratetype of direct compactor */ 1048 const unsigned int alloc_flags; /* alloc flags of a direct compactor */ 1049 const int highest_zoneidx; /* zone index of a direct compactor */ 1050 enum migrate_mode mode; /* Async or sync migration mode */ 1051 bool ignore_skip_hint; /* Scan blocks even if marked skip */ 1052 bool no_set_skip_hint; /* Don't mark blocks for skipping */ 1053 bool ignore_block_suitable; /* Scan blocks considered unsuitable */ 1054 bool direct_compaction; /* False from kcompactd or /proc/... */ 1055 bool proactive_compaction; /* kcompactd proactive compaction */ 1056 bool whole_zone; /* Whole zone should/has been scanned */ 1057 bool contended; /* Signal lock contention */ 1058 bool finish_pageblock; /* Scan the remainder of a pageblock. Used 1059 * when there are potentially transient 1060 * isolation or migration failures to 1061 * ensure forward progress. 1062 */ 1063 bool alloc_contig; /* alloc_contig_range allocation */ 1064 }; 1065 1066 /* 1067 * Used in direct compaction when a page should be taken from the freelists 1068 * immediately when one is created during the free path. 1069 */ 1070 struct capture_control { 1071 struct compact_control *cc; 1072 struct page *page; 1073 }; 1074 1075 unsigned long 1076 isolate_freepages_range(struct compact_control *cc, 1077 unsigned long start_pfn, unsigned long end_pfn); 1078 int 1079 isolate_migratepages_range(struct compact_control *cc, 1080 unsigned long low_pfn, unsigned long end_pfn); 1081 1082 /* Free whole pageblock and set its migration type to MIGRATE_CMA. */ 1083 void init_cma_reserved_pageblock(struct page *page); 1084 1085 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 1086 1087 struct cma; 1088 1089 #ifdef CONFIG_CMA 1090 bool cma_validate_zones(struct cma *cma); 1091 void *cma_reserve_early(struct cma *cma, unsigned long size); 1092 void init_cma_pageblock(struct page *page); 1093 #else 1094 static inline bool cma_validate_zones(struct cma *cma) 1095 { 1096 return false; 1097 } 1098 static inline void *cma_reserve_early(struct cma *cma, unsigned long size) 1099 { 1100 return NULL; 1101 } 1102 static inline void init_cma_pageblock(struct page *page) 1103 { 1104 } 1105 #endif 1106 1107 1108 int find_suitable_fallback(struct free_area *area, unsigned int order, 1109 int migratetype, bool claimable); 1110 1111 static inline bool free_area_empty(struct free_area *area, int migratetype) 1112 { 1113 return list_empty(&area->free_list[migratetype]); 1114 } 1115 1116 /* mm/util.c */ 1117 struct anon_vma *folio_anon_vma(const struct folio *folio); 1118 1119 #ifdef CONFIG_MMU 1120 void unmap_mapping_folio(struct folio *folio); 1121 extern long populate_vma_page_range(struct vm_area_struct *vma, 1122 unsigned long start, unsigned long end, int *locked); 1123 extern long faultin_page_range(struct mm_struct *mm, unsigned long start, 1124 unsigned long end, bool write, int *locked); 1125 bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked, 1126 unsigned long bytes); 1127 1128 /* 1129 * NOTE: This function can't tell whether the folio is "fully mapped" in the 1130 * range. 1131 * "fully mapped" means all the pages of folio is associated with the page 1132 * table of range while this function just check whether the folio range is 1133 * within the range [start, end). Function caller needs to do page table 1134 * check if it cares about the page table association. 1135 * 1136 * Typical usage (like mlock or madvise) is: 1137 * Caller knows at least 1 page of folio is associated with page table of VMA 1138 * and the range [start, end) is intersect with the VMA range. Caller wants 1139 * to know whether the folio is fully associated with the range. It calls 1140 * this function to check whether the folio is in the range first. Then checks 1141 * the page table to know whether the folio is fully mapped to the range. 1142 */ 1143 static inline bool 1144 folio_within_range(struct folio *folio, struct vm_area_struct *vma, 1145 unsigned long start, unsigned long end) 1146 { 1147 pgoff_t pgoff, addr; 1148 unsigned long vma_pglen = vma_pages(vma); 1149 1150 VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio); 1151 if (start > end) 1152 return false; 1153 1154 if (start < vma->vm_start) 1155 start = vma->vm_start; 1156 1157 if (end > vma->vm_end) 1158 end = vma->vm_end; 1159 1160 pgoff = folio_pgoff(folio); 1161 1162 /* if folio start address is not in vma range */ 1163 if (!in_range(pgoff, vma->vm_pgoff, vma_pglen)) 1164 return false; 1165 1166 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1167 1168 return !(addr < start || end - addr < folio_size(folio)); 1169 } 1170 1171 static inline bool 1172 folio_within_vma(struct folio *folio, struct vm_area_struct *vma) 1173 { 1174 return folio_within_range(folio, vma, vma->vm_start, vma->vm_end); 1175 } 1176 1177 /* 1178 * mlock_vma_folio() and munlock_vma_folio(): 1179 * should be called with vma's mmap_lock held for read or write, 1180 * under page table lock for the pte/pmd being added or removed. 1181 * 1182 * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at 1183 * the end of folio_remove_rmap_*(); but new anon folios are managed by 1184 * folio_add_lru_vma() calling mlock_new_folio(). 1185 */ 1186 void mlock_folio(struct folio *folio); 1187 static inline void mlock_vma_folio(struct folio *folio, 1188 struct vm_area_struct *vma) 1189 { 1190 /* 1191 * The VM_SPECIAL check here serves two purposes. 1192 * 1) VM_IO check prevents migration from double-counting during mlock. 1193 * 2) Although mmap_region() and mlock_fixup() take care that VM_LOCKED 1194 * is never left set on a VM_SPECIAL vma, there is an interval while 1195 * file->f_op->mmap() is using vm_insert_page(s), when VM_LOCKED may 1196 * still be set while VM_SPECIAL bits are added: so ignore it then. 1197 */ 1198 if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED)) 1199 mlock_folio(folio); 1200 } 1201 1202 void munlock_folio(struct folio *folio); 1203 static inline void munlock_vma_folio(struct folio *folio, 1204 struct vm_area_struct *vma) 1205 { 1206 /* 1207 * munlock if the function is called. Ideally, we should only 1208 * do munlock if any page of folio is unmapped from VMA and 1209 * cause folio not fully mapped to VMA. 1210 * 1211 * But it's not easy to confirm that's the situation. So we 1212 * always munlock the folio and page reclaim will correct it 1213 * if it's wrong. 1214 */ 1215 if (unlikely(vma->vm_flags & VM_LOCKED)) 1216 munlock_folio(folio); 1217 } 1218 1219 void mlock_new_folio(struct folio *folio); 1220 bool need_mlock_drain(int cpu); 1221 void mlock_drain_local(void); 1222 void mlock_drain_remote(int cpu); 1223 1224 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); 1225 1226 /** 1227 * vma_address - Find the virtual address a page range is mapped at 1228 * @vma: The vma which maps this object. 1229 * @pgoff: The page offset within its object. 1230 * @nr_pages: The number of pages to consider. 1231 * 1232 * If any page in this range is mapped by this VMA, return the first address 1233 * where any of these pages appear. Otherwise, return -EFAULT. 1234 */ 1235 static inline unsigned long vma_address(const struct vm_area_struct *vma, 1236 pgoff_t pgoff, unsigned long nr_pages) 1237 { 1238 unsigned long address; 1239 1240 if (pgoff >= vma->vm_pgoff) { 1241 address = vma->vm_start + 1242 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1243 /* Check for address beyond vma (or wrapped through 0?) */ 1244 if (address < vma->vm_start || address >= vma->vm_end) 1245 address = -EFAULT; 1246 } else if (pgoff + nr_pages - 1 >= vma->vm_pgoff) { 1247 /* Test above avoids possibility of wrap to 0 on 32-bit */ 1248 address = vma->vm_start; 1249 } else { 1250 address = -EFAULT; 1251 } 1252 return address; 1253 } 1254 1255 /* 1256 * Then at what user virtual address will none of the range be found in vma? 1257 * Assumes that vma_address() already returned a good starting address. 1258 */ 1259 static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw) 1260 { 1261 struct vm_area_struct *vma = pvmw->vma; 1262 pgoff_t pgoff; 1263 unsigned long address; 1264 1265 /* Common case, plus ->pgoff is invalid for KSM */ 1266 if (pvmw->nr_pages == 1) 1267 return pvmw->address + PAGE_SIZE; 1268 1269 pgoff = pvmw->pgoff + pvmw->nr_pages; 1270 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1271 /* Check for address beyond vma (or wrapped through 0?) */ 1272 if (address < vma->vm_start || address > vma->vm_end) 1273 address = vma->vm_end; 1274 return address; 1275 } 1276 1277 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, 1278 struct file *fpin) 1279 { 1280 int flags = vmf->flags; 1281 1282 if (fpin) 1283 return fpin; 1284 1285 /* 1286 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or 1287 * anything, so we only pin the file and drop the mmap_lock if only 1288 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. 1289 */ 1290 if (fault_flag_allow_retry_first(flags) && 1291 !(flags & FAULT_FLAG_RETRY_NOWAIT)) { 1292 fpin = get_file(vmf->vma->vm_file); 1293 release_fault_lock(vmf); 1294 } 1295 return fpin; 1296 } 1297 1298 static inline bool vma_supports_mlock(const struct vm_area_struct *vma) 1299 { 1300 if (vma_test_any_mask(vma, VMA_SPECIAL_FLAGS)) 1301 return false; 1302 if (vma_test_single_mask(vma, VMA_DROPPABLE)) 1303 return false; 1304 if (vma_is_dax(vma) || is_vm_hugetlb_page(vma)) 1305 return false; 1306 return vma != get_gate_vma(current->mm); 1307 } 1308 1309 #else /* !CONFIG_MMU */ 1310 static inline void unmap_mapping_folio(struct folio *folio) { } 1311 static inline void mlock_new_folio(struct folio *folio) { } 1312 static inline bool need_mlock_drain(int cpu) { return false; } 1313 static inline void mlock_drain_local(void) { } 1314 static inline void mlock_drain_remote(int cpu) { } 1315 static inline void vunmap_range_noflush(unsigned long start, unsigned long end) 1316 { 1317 } 1318 #endif /* !CONFIG_MMU */ 1319 1320 /* Memory initialisation debug and verification */ 1321 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1322 DECLARE_STATIC_KEY_TRUE(deferred_pages); 1323 1324 static inline bool deferred_pages_enabled(void) 1325 { 1326 return static_branch_unlikely(&deferred_pages); 1327 } 1328 1329 bool __init deferred_grow_zone(struct zone *zone, unsigned int order); 1330 #else 1331 static inline bool deferred_pages_enabled(void) 1332 { 1333 return false; 1334 } 1335 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 1336 1337 void init_deferred_page(unsigned long pfn, int nid); 1338 1339 enum mminit_level { 1340 MMINIT_WARNING, 1341 MMINIT_VERIFY, 1342 MMINIT_TRACE 1343 }; 1344 1345 #ifdef CONFIG_DEBUG_MEMORY_INIT 1346 1347 extern int mminit_loglevel; 1348 1349 #define mminit_dprintk(level, prefix, fmt, arg...) \ 1350 do { \ 1351 if (level < mminit_loglevel) { \ 1352 if (level <= MMINIT_WARNING) \ 1353 pr_warn("mminit::" prefix " " fmt, ##arg); \ 1354 else \ 1355 printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \ 1356 } \ 1357 } while (0) 1358 1359 extern void mminit_verify_pageflags_layout(void); 1360 extern void mminit_verify_zonelist(void); 1361 #else 1362 1363 static inline void mminit_dprintk(enum mminit_level level, 1364 const char *prefix, const char *fmt, ...) 1365 { 1366 } 1367 1368 static inline void mminit_verify_pageflags_layout(void) 1369 { 1370 } 1371 1372 static inline void mminit_verify_zonelist(void) 1373 { 1374 } 1375 #endif /* CONFIG_DEBUG_MEMORY_INIT */ 1376 1377 #define NODE_RECLAIM_NOSCAN -2 1378 #define NODE_RECLAIM_FULL -1 1379 #define NODE_RECLAIM_SOME 0 1380 #define NODE_RECLAIM_SUCCESS 1 1381 1382 #ifdef CONFIG_NUMA 1383 extern int node_reclaim_mode; 1384 1385 extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int); 1386 extern int find_next_best_node(int node, nodemask_t *used_node_mask); 1387 #else 1388 #define node_reclaim_mode 0 1389 1390 static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask, 1391 unsigned int order) 1392 { 1393 return NODE_RECLAIM_NOSCAN; 1394 } 1395 static inline int find_next_best_node(int node, nodemask_t *used_node_mask) 1396 { 1397 return NUMA_NO_NODE; 1398 } 1399 #endif 1400 1401 static inline bool node_reclaim_enabled(void) 1402 { 1403 /* Is any node_reclaim_mode bit set? */ 1404 return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP); 1405 } 1406 1407 /* 1408 * mm/memory-failure.c 1409 */ 1410 #ifdef CONFIG_MEMORY_FAILURE 1411 int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill); 1412 void shake_folio(struct folio *folio); 1413 typedef int hwpoison_filter_func_t(struct page *p); 1414 void hwpoison_filter_register(hwpoison_filter_func_t *filter); 1415 void hwpoison_filter_unregister(void); 1416 1417 #define MAGIC_HWPOISON 0x48575053U /* HWPS */ 1418 void SetPageHWPoisonTakenOff(struct page *page); 1419 void ClearPageHWPoisonTakenOff(struct page *page); 1420 bool take_page_off_buddy(struct page *page); 1421 bool put_page_back_buddy(struct page *page); 1422 struct task_struct *task_early_kill(struct task_struct *tsk, int force_early); 1423 void add_to_kill_ksm(struct task_struct *tsk, const struct page *p, 1424 struct vm_area_struct *vma, struct list_head *to_kill, 1425 unsigned long ksm_addr); 1426 unsigned long page_mapped_in_vma(const struct page *page, 1427 struct vm_area_struct *vma); 1428 1429 #else 1430 static inline int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill) 1431 { 1432 return -EBUSY; 1433 } 1434 #endif 1435 1436 extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long, 1437 unsigned long, unsigned long, 1438 unsigned long, unsigned long); 1439 1440 extern void set_pageblock_order(void); 1441 unsigned long reclaim_pages(struct list_head *folio_list); 1442 unsigned int reclaim_clean_pages_from_list(struct zone *zone, 1443 struct list_head *folio_list); 1444 /* The ALLOC_WMARK bits are used as an index to zone->watermark */ 1445 #define ALLOC_WMARK_MIN WMARK_MIN 1446 #define ALLOC_WMARK_LOW WMARK_LOW 1447 #define ALLOC_WMARK_HIGH WMARK_HIGH 1448 #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */ 1449 1450 /* Mask to get the watermark bits */ 1451 #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1) 1452 1453 /* 1454 * Only MMU archs have async oom victim reclaim - aka oom_reaper so we 1455 * cannot assume a reduced access to memory reserves is sufficient for 1456 * !MMU 1457 */ 1458 #ifdef CONFIG_MMU 1459 #define ALLOC_OOM 0x08 1460 #else 1461 #define ALLOC_OOM ALLOC_NO_WATERMARKS 1462 #endif 1463 1464 #define ALLOC_NON_BLOCK 0x10 /* Caller cannot block. Allow access 1465 * to 25% of the min watermark or 1466 * 62.5% if __GFP_HIGH is set. 1467 */ 1468 #define ALLOC_MIN_RESERVE 0x20 /* __GFP_HIGH set. Allow access to 50% 1469 * of the min watermark. 1470 */ 1471 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */ 1472 #define ALLOC_CMA 0x80 /* allow allocations from CMA areas */ 1473 #ifdef CONFIG_ZONE_DMA32 1474 #define ALLOC_NOFRAGMENT 0x100 /* avoid mixing pageblock types */ 1475 #else 1476 #define ALLOC_NOFRAGMENT 0x0 1477 #endif 1478 #define ALLOC_HIGHATOMIC 0x200 /* Allows access to MIGRATE_HIGHATOMIC */ 1479 #define ALLOC_TRYLOCK 0x400 /* Only use spin_trylock in allocation path */ 1480 #define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */ 1481 1482 /* Flags that allow allocations below the min watermark. */ 1483 #define ALLOC_RESERVES (ALLOC_NON_BLOCK|ALLOC_MIN_RESERVE|ALLOC_HIGHATOMIC|ALLOC_OOM) 1484 1485 enum ttu_flags; 1486 struct tlbflush_unmap_batch; 1487 1488 1489 /* 1490 * only for MM internal work items which do not depend on 1491 * any allocations or locks which might depend on allocations 1492 */ 1493 extern struct workqueue_struct *mm_percpu_wq; 1494 1495 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1496 void try_to_unmap_flush(void); 1497 void try_to_unmap_flush_dirty(void); 1498 void flush_tlb_batched_pending(struct mm_struct *mm); 1499 #else 1500 static inline void try_to_unmap_flush(void) 1501 { 1502 } 1503 static inline void try_to_unmap_flush_dirty(void) 1504 { 1505 } 1506 static inline void flush_tlb_batched_pending(struct mm_struct *mm) 1507 { 1508 } 1509 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ 1510 1511 extern const struct trace_print_flags pageflag_names[]; 1512 extern const struct trace_print_flags vmaflag_names[]; 1513 extern const struct trace_print_flags gfpflag_names[]; 1514 1515 void setup_zone_pageset(struct zone *zone); 1516 1517 struct migration_target_control { 1518 int nid; /* preferred node id */ 1519 nodemask_t *nmask; 1520 gfp_t gfp_mask; 1521 enum migrate_reason reason; 1522 }; 1523 1524 /* 1525 * mm/filemap.c 1526 */ 1527 size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, 1528 struct folio *folio, loff_t fpos, size_t size); 1529 1530 /* 1531 * mm/vmalloc.c 1532 */ 1533 #ifdef CONFIG_MMU 1534 void __init vmalloc_init(void); 1535 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 1536 pgprot_t prot, struct page **pages, unsigned int page_shift, gfp_t gfp_mask); 1537 unsigned int get_vm_area_page_order(struct vm_struct *vm); 1538 #else 1539 static inline void vmalloc_init(void) 1540 { 1541 } 1542 1543 static inline 1544 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 1545 pgprot_t prot, struct page **pages, unsigned int page_shift, gfp_t gfp_mask) 1546 { 1547 return -EINVAL; 1548 } 1549 #endif 1550 1551 void clear_vm_uninitialized_flag(struct vm_struct *vm); 1552 1553 int __must_check __vmap_pages_range_noflush(unsigned long addr, 1554 unsigned long end, pgprot_t prot, 1555 struct page **pages, unsigned int page_shift); 1556 1557 void vunmap_range_noflush(unsigned long start, unsigned long end); 1558 1559 void __vunmap_range_noflush(unsigned long start, unsigned long end); 1560 1561 static inline bool vma_is_single_threaded_private(struct vm_area_struct *vma) 1562 { 1563 if (vma->vm_flags & VM_SHARED) 1564 return false; 1565 1566 return atomic_read(&vma->vm_mm->mm_users) == 1; 1567 } 1568 1569 #ifdef CONFIG_NUMA_BALANCING 1570 bool folio_can_map_prot_numa(struct folio *folio, struct vm_area_struct *vma, 1571 bool is_private_single_threaded); 1572 1573 #else 1574 static inline bool folio_can_map_prot_numa(struct folio *folio, 1575 struct vm_area_struct *vma, bool is_private_single_threaded) 1576 { 1577 return false; 1578 } 1579 #endif 1580 1581 int numa_migrate_check(struct folio *folio, struct vm_fault *vmf, 1582 unsigned long addr, int *flags, bool writable, 1583 int *last_cpupid); 1584 1585 void free_zone_device_folio(struct folio *folio); 1586 int migrate_device_coherent_folio(struct folio *folio); 1587 1588 struct vm_struct *__get_vm_area_node(unsigned long size, 1589 unsigned long align, unsigned long shift, 1590 unsigned long vm_flags, unsigned long start, 1591 unsigned long end, int node, gfp_t gfp_mask, 1592 const void *caller); 1593 1594 /* 1595 * mm/gup.c 1596 */ 1597 int __must_check try_grab_folio(struct folio *folio, int refs, 1598 unsigned int flags); 1599 1600 /* 1601 * mm/huge_memory.c 1602 */ 1603 void touch_pud(struct vm_area_struct *vma, unsigned long addr, 1604 pud_t *pud, bool write); 1605 bool touch_pmd(struct vm_area_struct *vma, unsigned long addr, 1606 pmd_t *pmd, bool write); 1607 1608 /* 1609 * Parses a string with mem suffixes into its order. Useful to parse kernel 1610 * parameters. 1611 */ 1612 static inline int get_order_from_str(const char *size_str, 1613 unsigned long valid_orders) 1614 { 1615 unsigned long size; 1616 char *endptr; 1617 int order; 1618 1619 size = memparse(size_str, &endptr); 1620 1621 if (!is_power_of_2(size)) 1622 return -EINVAL; 1623 order = get_order(size); 1624 if (BIT(order) & ~valid_orders) 1625 return -EINVAL; 1626 1627 return order; 1628 } 1629 1630 enum { 1631 /* mark page accessed */ 1632 FOLL_TOUCH = 1 << 16, 1633 /* a retry, previous pass started an IO */ 1634 FOLL_TRIED = 1 << 17, 1635 /* we are working on non-current tsk/mm */ 1636 FOLL_REMOTE = 1 << 18, 1637 /* pages must be released via unpin_user_page */ 1638 FOLL_PIN = 1 << 19, 1639 /* gup_fast: prevent fall-back to slow gup */ 1640 FOLL_FAST_ONLY = 1 << 20, 1641 /* allow unlocking the mmap lock */ 1642 FOLL_UNLOCKABLE = 1 << 21, 1643 /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ 1644 FOLL_MADV_POPULATE = 1 << 22, 1645 }; 1646 1647 #define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ 1648 FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ 1649 FOLL_MADV_POPULATE) 1650 1651 /* 1652 * Indicates for which pages that are write-protected in the page table, 1653 * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the 1654 * GUP pin will remain consistent with the pages mapped into the page tables 1655 * of the MM. 1656 * 1657 * Temporary unmapping of PageAnonExclusive() pages or clearing of 1658 * PageAnonExclusive() has to protect against concurrent GUP: 1659 * * Ordinary GUP: Using the PT lock 1660 * * GUP-fast and fork(): mm->write_protect_seq 1661 * * GUP-fast and KSM or temporary unmapping (swap, migration): see 1662 * folio_try_share_anon_rmap_*() 1663 * 1664 * Must be called with the (sub)page that's actually referenced via the 1665 * page table entry, which might not necessarily be the head page for a 1666 * PTE-mapped THP. 1667 * 1668 * If the vma is NULL, we're coming from the GUP-fast path and might have 1669 * to fallback to the slow path just to lookup the vma. 1670 */ 1671 static inline bool gup_must_unshare(struct vm_area_struct *vma, 1672 unsigned int flags, struct page *page) 1673 { 1674 /* 1675 * FOLL_WRITE is implicitly handled correctly as the page table entry 1676 * has to be writable -- and if it references (part of) an anonymous 1677 * folio, that part is required to be marked exclusive. 1678 */ 1679 if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN) 1680 return false; 1681 /* 1682 * Note: PageAnon(page) is stable until the page is actually getting 1683 * freed. 1684 */ 1685 if (!PageAnon(page)) { 1686 /* 1687 * We only care about R/O long-term pining: R/O short-term 1688 * pinning does not have the semantics to observe successive 1689 * changes through the process page tables. 1690 */ 1691 if (!(flags & FOLL_LONGTERM)) 1692 return false; 1693 1694 /* We really need the vma ... */ 1695 if (!vma) 1696 return true; 1697 1698 /* 1699 * ... because we only care about writable private ("COW") 1700 * mappings where we have to break COW early. 1701 */ 1702 return is_cow_mapping(vma->vm_flags); 1703 } 1704 1705 /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */ 1706 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) 1707 smp_rmb(); 1708 1709 /* 1710 * Note that KSM pages cannot be exclusive, and consequently, 1711 * cannot get pinned. 1712 */ 1713 return !PageAnonExclusive(page); 1714 } 1715 1716 extern bool mirrored_kernelcore; 1717 bool memblock_has_mirror(void); 1718 void memblock_free_all(void); 1719 1720 static __always_inline void vma_set_range(struct vm_area_struct *vma, 1721 unsigned long start, unsigned long end, 1722 pgoff_t pgoff) 1723 { 1724 vma->vm_start = start; 1725 vma->vm_end = end; 1726 vma->vm_pgoff = pgoff; 1727 } 1728 1729 static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) 1730 { 1731 /* 1732 * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty 1733 * enablements, because when without soft-dirty being compiled in, 1734 * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) 1735 * will be constantly true. 1736 */ 1737 if (!pgtable_supports_soft_dirty()) 1738 return false; 1739 1740 /* 1741 * Soft-dirty is kind of special: its tracking is enabled when the 1742 * vma flags not set. 1743 */ 1744 return !(vma->vm_flags & VM_SOFTDIRTY); 1745 } 1746 1747 static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd) 1748 { 1749 return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd); 1750 } 1751 1752 static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte) 1753 { 1754 return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte); 1755 } 1756 1757 void __meminit __init_single_page(struct page *page, unsigned long pfn, 1758 unsigned long zone, int nid); 1759 void __meminit __init_page_from_nid(unsigned long pfn, int nid); 1760 1761 /* shrinker related functions */ 1762 unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg, 1763 int priority); 1764 1765 int shmem_add_to_page_cache(struct folio *folio, 1766 struct address_space *mapping, 1767 pgoff_t index, void *expected, gfp_t gfp); 1768 int shmem_inode_acct_blocks(struct inode *inode, long pages); 1769 bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped); 1770 1771 #ifdef CONFIG_SHRINKER_DEBUG 1772 static inline __printf(2, 0) int shrinker_debugfs_name_alloc( 1773 struct shrinker *shrinker, const char *fmt, va_list ap) 1774 { 1775 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap); 1776 1777 return shrinker->name ? 0 : -ENOMEM; 1778 } 1779 1780 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1781 { 1782 kfree_const(shrinker->name); 1783 shrinker->name = NULL; 1784 } 1785 1786 extern int shrinker_debugfs_add(struct shrinker *shrinker); 1787 extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1788 int *debugfs_id); 1789 extern void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1790 int debugfs_id); 1791 #else /* CONFIG_SHRINKER_DEBUG */ 1792 static inline int shrinker_debugfs_add(struct shrinker *shrinker) 1793 { 1794 return 0; 1795 } 1796 static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker, 1797 const char *fmt, va_list ap) 1798 { 1799 return 0; 1800 } 1801 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1802 { 1803 } 1804 static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1805 int *debugfs_id) 1806 { 1807 *debugfs_id = -1; 1808 return NULL; 1809 } 1810 static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1811 int debugfs_id) 1812 { 1813 } 1814 #endif /* CONFIG_SHRINKER_DEBUG */ 1815 1816 /* Only track the nodes of mappings with shadow entries */ 1817 void workingset_update_node(struct xa_node *node); 1818 extern struct list_lru shadow_nodes; 1819 #define mapping_set_update(xas, mapping) do { \ 1820 if (!dax_mapping(mapping) && !shmem_mapping(mapping)) { \ 1821 xas_set_update(xas, workingset_update_node); \ 1822 xas_set_lru(xas, &shadow_nodes); \ 1823 } \ 1824 } while (0) 1825 1826 /* mremap.c */ 1827 unsigned long move_page_tables(struct pagetable_move_control *pmc); 1828 1829 #ifdef CONFIG_UNACCEPTED_MEMORY 1830 void accept_page(struct page *page); 1831 #else /* CONFIG_UNACCEPTED_MEMORY */ 1832 static inline void accept_page(struct page *page) 1833 { 1834 } 1835 #endif /* CONFIG_UNACCEPTED_MEMORY */ 1836 1837 /* pagewalk.c */ 1838 int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start, 1839 unsigned long end, const struct mm_walk_ops *ops, 1840 void *private); 1841 int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start, 1842 unsigned long end, const struct mm_walk_ops *ops, 1843 void *private); 1844 int walk_page_range_debug(struct mm_struct *mm, unsigned long start, 1845 unsigned long end, const struct mm_walk_ops *ops, 1846 pgd_t *pgd, void *private); 1847 1848 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm); 1849 int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm); 1850 1851 int remap_pfn_range_prepare(struct vm_area_desc *desc); 1852 int remap_pfn_range_complete(struct vm_area_struct *vma, 1853 struct mmap_action *action); 1854 int simple_ioremap_prepare(struct vm_area_desc *desc); 1855 1856 static inline int io_remap_pfn_range_prepare(struct vm_area_desc *desc) 1857 { 1858 struct mmap_action *action = &desc->action; 1859 const unsigned long orig_pfn = action->remap.start_pfn; 1860 const pgprot_t orig_pgprot = action->remap.pgprot; 1861 const unsigned long size = action->remap.size; 1862 const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size); 1863 int err; 1864 1865 action->remap.start_pfn = pfn; 1866 action->remap.pgprot = pgprot_decrypted(orig_pgprot); 1867 err = remap_pfn_range_prepare(desc); 1868 if (err) 1869 return err; 1870 1871 /* Remap does the actual work. */ 1872 action->type = MMAP_REMAP_PFN; 1873 return 0; 1874 } 1875 1876 /* 1877 * When we succeed an mmap action or just before we unmap a VMA on error, we 1878 * need to ensure any rmap lock held is released. On unmap it's required to 1879 * avoid a deadlock. 1880 */ 1881 static inline void maybe_rmap_unlock_action(struct vm_area_struct *vma, 1882 struct mmap_action *action) 1883 { 1884 struct file *file; 1885 1886 if (!action->hide_from_rmap_until_complete) 1887 return; 1888 1889 VM_WARN_ON_ONCE(vma_is_anonymous(vma)); 1890 file = vma->vm_file; 1891 i_mmap_unlock_write(file->f_mapping); 1892 action->hide_from_rmap_until_complete = false; 1893 } 1894 1895 #ifdef CONFIG_MMU_NOTIFIER 1896 static inline bool clear_flush_young_ptes_notify(struct vm_area_struct *vma, 1897 unsigned long addr, pte_t *ptep, unsigned int nr) 1898 { 1899 bool young; 1900 1901 young = clear_flush_young_ptes(vma, addr, ptep, nr); 1902 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, 1903 addr + nr * PAGE_SIZE); 1904 return young; 1905 } 1906 1907 static inline bool pmdp_clear_flush_young_notify(struct vm_area_struct *vma, 1908 unsigned long addr, pmd_t *pmdp) 1909 { 1910 bool young; 1911 1912 young = pmdp_clear_flush_young(vma, addr, pmdp); 1913 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, addr + PMD_SIZE); 1914 return young; 1915 } 1916 1917 static inline bool test_and_clear_young_ptes_notify(struct vm_area_struct *vma, 1918 unsigned long addr, pte_t *ptep, unsigned int nr) 1919 { 1920 bool young; 1921 1922 young = test_and_clear_young_ptes(vma, addr, ptep, nr); 1923 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + nr * PAGE_SIZE); 1924 return young; 1925 } 1926 1927 static inline bool pmdp_test_and_clear_young_notify(struct vm_area_struct *vma, 1928 unsigned long addr, pmd_t *pmdp) 1929 { 1930 bool young; 1931 1932 young = pmdp_test_and_clear_young(vma, addr, pmdp); 1933 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + PMD_SIZE); 1934 return young; 1935 } 1936 1937 #else /* CONFIG_MMU_NOTIFIER */ 1938 1939 #define clear_flush_young_ptes_notify clear_flush_young_ptes 1940 #define pmdp_clear_flush_young_notify pmdp_clear_flush_young 1941 #define test_and_clear_young_ptes_notify test_and_clear_young_ptes 1942 #define pmdp_test_and_clear_young_notify pmdp_test_and_clear_young 1943 1944 #endif /* CONFIG_MMU_NOTIFIER */ 1945 1946 extern int sysctl_max_map_count; 1947 static inline int get_sysctl_max_map_count(void) 1948 { 1949 return READ_ONCE(sysctl_max_map_count); 1950 } 1951 1952 bool may_expand_vm(struct mm_struct *mm, const vma_flags_t *vma_flags, 1953 unsigned long npages); 1954 1955 #endif /* __MM_INTERNAL_H */ 1956