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