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/mm.h> 12 #include <linux/pagemap.h> 13 #include <linux/rmap.h> 14 #include <linux/tracepoint-defs.h> 15 16 struct folio_batch; 17 18 /* 19 * The set of flags that only affect watermark checking and reclaim 20 * behaviour. This is used by the MM to obey the caller constraints 21 * about IO, FS and watermark checking while ignoring placement 22 * hints such as HIGHMEM usage. 23 */ 24 #define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\ 25 __GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\ 26 __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\ 27 __GFP_NOLOCKDEP) 28 29 /* The GFP flags allowed during early boot */ 30 #define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS)) 31 32 /* Control allocation cpuset and node placement constraints */ 33 #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE) 34 35 /* Do not use these with a slab allocator */ 36 #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK) 37 38 /* 39 * Different from WARN_ON_ONCE(), no warning will be issued 40 * when we specify __GFP_NOWARN. 41 */ 42 #define WARN_ON_ONCE_GFP(cond, gfp) ({ \ 43 static bool __section(".data..once") __warned; \ 44 int __ret_warn_once = !!(cond); \ 45 \ 46 if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \ 47 __warned = true; \ 48 WARN_ON(1); \ 49 } \ 50 unlikely(__ret_warn_once); \ 51 }) 52 53 void page_writeback_init(void); 54 55 /* 56 * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages, 57 * its nr_pages_mapped would be 0x400000: choose the COMPOUND_MAPPED bit 58 * above that range, instead of 2*(PMD_SIZE/PAGE_SIZE). Hugetlb currently 59 * leaves nr_pages_mapped at 0, but avoid surprise if it participates later. 60 */ 61 #define COMPOUND_MAPPED 0x800000 62 #define FOLIO_PAGES_MAPPED (COMPOUND_MAPPED - 1) 63 64 /* 65 * Flags passed to __show_mem() and show_free_areas() to suppress output in 66 * various contexts. 67 */ 68 #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */ 69 70 /* 71 * How many individual pages have an elevated _mapcount. Excludes 72 * the folio's entire_mapcount. 73 */ 74 static inline int folio_nr_pages_mapped(struct folio *folio) 75 { 76 return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED; 77 } 78 79 static inline void *folio_raw_mapping(struct folio *folio) 80 { 81 unsigned long mapping = (unsigned long)folio->mapping; 82 83 return (void *)(mapping & ~PAGE_MAPPING_FLAGS); 84 } 85 86 /* 87 * This is a file-backed mapping, and is about to be memory mapped - invoke its 88 * mmap hook and safely handle error conditions. On error, VMA hooks will be 89 * mutated. 90 * 91 * @file: File which backs the mapping. 92 * @vma: VMA which we are mapping. 93 * 94 * Returns: 0 if success, error otherwise. 95 */ 96 static inline int mmap_file(struct file *file, struct vm_area_struct *vma) 97 { 98 int err = call_mmap(file, vma); 99 100 if (likely(!err)) 101 return 0; 102 103 /* 104 * OK, we tried to call the file hook for mmap(), but an error 105 * arose. The mapping is in an inconsistent state and we most not invoke 106 * any further hooks on it. 107 */ 108 vma->vm_ops = &vma_dummy_vm_ops; 109 110 return err; 111 } 112 113 /* 114 * If the VMA has a close hook then close it, and since closing it might leave 115 * it in an inconsistent state which makes the use of any hooks suspect, clear 116 * them down by installing dummy empty hooks. 117 */ 118 static inline void vma_close(struct vm_area_struct *vma) 119 { 120 if (vma->vm_ops && vma->vm_ops->close) { 121 vma->vm_ops->close(vma); 122 123 /* 124 * The mapping is in an inconsistent state, and no further hooks 125 * may be invoked upon it. 126 */ 127 vma->vm_ops = &vma_dummy_vm_ops; 128 } 129 } 130 131 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 132 int nr_throttled); 133 static inline void acct_reclaim_writeback(struct folio *folio) 134 { 135 pg_data_t *pgdat = folio_pgdat(folio); 136 int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled); 137 138 if (nr_throttled) 139 __acct_reclaim_writeback(pgdat, folio, nr_throttled); 140 } 141 142 static inline void wake_throttle_isolated(pg_data_t *pgdat) 143 { 144 wait_queue_head_t *wqh; 145 146 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED]; 147 if (waitqueue_active(wqh)) 148 wake_up(wqh); 149 } 150 151 vm_fault_t do_swap_page(struct vm_fault *vmf); 152 void folio_rotate_reclaimable(struct folio *folio); 153 bool __folio_end_writeback(struct folio *folio); 154 void deactivate_file_folio(struct folio *folio); 155 void folio_activate(struct folio *folio); 156 157 void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas, 158 struct vm_area_struct *start_vma, unsigned long floor, 159 unsigned long ceiling, bool mm_wr_locked); 160 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); 161 162 struct zap_details; 163 void unmap_page_range(struct mmu_gather *tlb, 164 struct vm_area_struct *vma, 165 unsigned long addr, unsigned long end, 166 struct zap_details *details); 167 168 void page_cache_ra_order(struct readahead_control *, struct file_ra_state *, 169 unsigned int order); 170 void force_page_cache_ra(struct readahead_control *, unsigned long nr); 171 static inline void force_page_cache_readahead(struct address_space *mapping, 172 struct file *file, pgoff_t index, unsigned long nr_to_read) 173 { 174 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index); 175 force_page_cache_ra(&ractl, nr_to_read); 176 } 177 178 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, 179 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 180 unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, 181 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 182 void filemap_free_folio(struct address_space *mapping, struct folio *folio); 183 int truncate_inode_folio(struct address_space *mapping, struct folio *folio); 184 bool truncate_inode_partial_folio(struct folio *folio, loff_t start, 185 loff_t end); 186 long invalidate_inode_page(struct page *page); 187 unsigned long mapping_try_invalidate(struct address_space *mapping, 188 pgoff_t start, pgoff_t end, unsigned long *nr_failed); 189 190 /** 191 * folio_evictable - Test whether a folio is evictable. 192 * @folio: The folio to test. 193 * 194 * Test whether @folio is evictable -- i.e., should be placed on 195 * active/inactive lists vs unevictable list. 196 * 197 * Reasons folio might not be evictable: 198 * 1. folio's mapping marked unevictable 199 * 2. One of the pages in the folio is part of an mlocked VMA 200 */ 201 static inline bool folio_evictable(struct folio *folio) 202 { 203 bool ret; 204 205 /* Prevent address_space of inode and swap cache from being freed */ 206 rcu_read_lock(); 207 ret = !mapping_unevictable(folio_mapping(folio)) && 208 !folio_test_mlocked(folio); 209 rcu_read_unlock(); 210 return ret; 211 } 212 213 /* 214 * Turn a non-refcounted page (->_refcount == 0) into refcounted with 215 * a count of one. 216 */ 217 static inline void set_page_refcounted(struct page *page) 218 { 219 VM_BUG_ON_PAGE(PageTail(page), page); 220 VM_BUG_ON_PAGE(page_ref_count(page), page); 221 set_page_count(page, 1); 222 } 223 224 /* 225 * Return true if a folio needs ->release_folio() calling upon it. 226 */ 227 static inline bool folio_needs_release(struct folio *folio) 228 { 229 struct address_space *mapping = folio_mapping(folio); 230 231 return folio_has_private(folio) || 232 (mapping && mapping_release_always(mapping)); 233 } 234 235 extern unsigned long highest_memmap_pfn; 236 237 /* 238 * Maximum number of reclaim retries without progress before the OOM 239 * killer is consider the only way forward. 240 */ 241 #define MAX_RECLAIM_RETRIES 16 242 243 /* 244 * in mm/vmscan.c: 245 */ 246 bool isolate_lru_page(struct page *page); 247 bool folio_isolate_lru(struct folio *folio); 248 void putback_lru_page(struct page *page); 249 void folio_putback_lru(struct folio *folio); 250 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason); 251 252 /* 253 * in mm/rmap.c: 254 */ 255 pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address); 256 257 /* 258 * in mm/page_alloc.c 259 */ 260 #define K(x) ((x) << (PAGE_SHIFT-10)) 261 262 extern char * const zone_names[MAX_NR_ZONES]; 263 264 /* perform sanity checks on struct pages being allocated or freed */ 265 DECLARE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled); 266 267 extern int min_free_kbytes; 268 269 void setup_per_zone_wmarks(void); 270 void calculate_min_free_kbytes(void); 271 int __meminit init_per_zone_wmark_min(void); 272 void page_alloc_sysctl_init(void); 273 274 /* 275 * Structure for holding the mostly immutable allocation parameters passed 276 * between functions involved in allocations, including the alloc_pages* 277 * family of functions. 278 * 279 * nodemask, migratetype and highest_zoneidx are initialized only once in 280 * __alloc_pages() and then never change. 281 * 282 * zonelist, preferred_zone and highest_zoneidx are set first in 283 * __alloc_pages() for the fast path, and might be later changed 284 * in __alloc_pages_slowpath(). All other functions pass the whole structure 285 * by a const pointer. 286 */ 287 struct alloc_context { 288 struct zonelist *zonelist; 289 nodemask_t *nodemask; 290 struct zoneref *preferred_zoneref; 291 int migratetype; 292 293 /* 294 * highest_zoneidx represents highest usable zone index of 295 * the allocation request. Due to the nature of the zone, 296 * memory on lower zone than the highest_zoneidx will be 297 * protected by lowmem_reserve[highest_zoneidx]. 298 * 299 * highest_zoneidx is also used by reclaim/compaction to limit 300 * the target zone since higher zone than this index cannot be 301 * usable for this allocation request. 302 */ 303 enum zone_type highest_zoneidx; 304 bool spread_dirty_pages; 305 }; 306 307 /* 308 * This function returns the order of a free page in the buddy system. In 309 * general, page_zone(page)->lock must be held by the caller to prevent the 310 * page from being allocated in parallel and returning garbage as the order. 311 * If a caller does not hold page_zone(page)->lock, it must guarantee that the 312 * page cannot be allocated or merged in parallel. Alternatively, it must 313 * handle invalid values gracefully, and use buddy_order_unsafe() below. 314 */ 315 static inline unsigned int buddy_order(struct page *page) 316 { 317 /* PageBuddy() must be checked by the caller */ 318 return page_private(page); 319 } 320 321 /* 322 * Like buddy_order(), but for callers who cannot afford to hold the zone lock. 323 * PageBuddy() should be checked first by the caller to minimize race window, 324 * and invalid values must be handled gracefully. 325 * 326 * READ_ONCE is used so that if the caller assigns the result into a local 327 * variable and e.g. tests it for valid range before using, the compiler cannot 328 * decide to remove the variable and inline the page_private(page) multiple 329 * times, potentially observing different values in the tests and the actual 330 * use of the result. 331 */ 332 #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) 333 334 /* 335 * This function checks whether a page is free && is the buddy 336 * we can coalesce a page and its buddy if 337 * (a) the buddy is not in a hole (check before calling!) && 338 * (b) the buddy is in the buddy system && 339 * (c) a page and its buddy have the same order && 340 * (d) a page and its buddy are in the same zone. 341 * 342 * For recording whether a page is in the buddy system, we set PageBuddy. 343 * Setting, clearing, and testing PageBuddy is serialized by zone->lock. 344 * 345 * For recording page's order, we use page_private(page). 346 */ 347 static inline bool page_is_buddy(struct page *page, struct page *buddy, 348 unsigned int order) 349 { 350 if (!page_is_guard(buddy) && !PageBuddy(buddy)) 351 return false; 352 353 if (buddy_order(buddy) != order) 354 return false; 355 356 /* 357 * zone check is done late to avoid uselessly calculating 358 * zone/node ids for pages that could never merge. 359 */ 360 if (page_zone_id(page) != page_zone_id(buddy)) 361 return false; 362 363 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); 364 365 return true; 366 } 367 368 /* 369 * Locate the struct page for both the matching buddy in our 370 * pair (buddy1) and the combined O(n+1) page they form (page). 371 * 372 * 1) Any buddy B1 will have an order O twin B2 which satisfies 373 * the following equation: 374 * B2 = B1 ^ (1 << O) 375 * For example, if the starting buddy (buddy2) is #8 its order 376 * 1 buddy is #10: 377 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10 378 * 379 * 2) Any buddy B will have an order O+1 parent P which 380 * satisfies the following equation: 381 * P = B & ~(1 << O) 382 * 383 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER 384 */ 385 static inline unsigned long 386 __find_buddy_pfn(unsigned long page_pfn, unsigned int order) 387 { 388 return page_pfn ^ (1 << order); 389 } 390 391 /* 392 * Find the buddy of @page and validate it. 393 * @page: The input page 394 * @pfn: The pfn of the page, it saves a call to page_to_pfn() when the 395 * function is used in the performance-critical __free_one_page(). 396 * @order: The order of the page 397 * @buddy_pfn: The output pointer to the buddy pfn, it also saves a call to 398 * page_to_pfn(). 399 * 400 * The found buddy can be a non PageBuddy, out of @page's zone, or its order is 401 * not the same as @page. The validation is necessary before use it. 402 * 403 * Return: the found buddy page or NULL if not found. 404 */ 405 static inline struct page *find_buddy_page_pfn(struct page *page, 406 unsigned long pfn, unsigned int order, unsigned long *buddy_pfn) 407 { 408 unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order); 409 struct page *buddy; 410 411 buddy = page + (__buddy_pfn - pfn); 412 if (buddy_pfn) 413 *buddy_pfn = __buddy_pfn; 414 415 if (page_is_buddy(page, buddy, order)) 416 return buddy; 417 return NULL; 418 } 419 420 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn, 421 unsigned long end_pfn, struct zone *zone); 422 423 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, 424 unsigned long end_pfn, struct zone *zone) 425 { 426 if (zone->contiguous) 427 return pfn_to_page(start_pfn); 428 429 return __pageblock_pfn_to_page(start_pfn, end_pfn, zone); 430 } 431 432 void set_zone_contiguous(struct zone *zone); 433 434 static inline void clear_zone_contiguous(struct zone *zone) 435 { 436 zone->contiguous = false; 437 } 438 439 extern int __isolate_free_page(struct page *page, unsigned int order); 440 extern void __putback_isolated_page(struct page *page, unsigned int order, 441 int mt); 442 extern void memblock_free_pages(struct page *page, unsigned long pfn, 443 unsigned int order); 444 extern void __free_pages_core(struct page *page, unsigned int order); 445 446 /* 447 * This will have no effect, other than possibly generating a warning, if the 448 * caller passes in a non-large folio. 449 */ 450 static inline void folio_set_order(struct folio *folio, unsigned int order) 451 { 452 if (WARN_ON_ONCE(!order || !folio_test_large(folio))) 453 return; 454 455 folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order; 456 #ifdef CONFIG_64BIT 457 folio->_folio_nr_pages = 1U << order; 458 #endif 459 } 460 461 bool __folio_unqueue_deferred_split(struct folio *folio); 462 static inline bool folio_unqueue_deferred_split(struct folio *folio) 463 { 464 if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio)) 465 return false; 466 467 /* 468 * At this point, there is no one trying to add the folio to 469 * deferred_list. If folio is not in deferred_list, it's safe 470 * to check without acquiring the split_queue_lock. 471 */ 472 if (data_race(list_empty(&folio->_deferred_list))) 473 return false; 474 475 return __folio_unqueue_deferred_split(folio); 476 } 477 478 static inline struct folio *page_rmappable_folio(struct page *page) 479 { 480 struct folio *folio = (struct folio *)page; 481 482 folio_prep_large_rmappable(folio); 483 return folio; 484 } 485 486 static inline void prep_compound_head(struct page *page, unsigned int order) 487 { 488 struct folio *folio = (struct folio *)page; 489 490 folio_set_order(folio, order); 491 atomic_set(&folio->_entire_mapcount, -1); 492 atomic_set(&folio->_nr_pages_mapped, 0); 493 atomic_set(&folio->_pincount, 0); 494 if (order > 1) 495 INIT_LIST_HEAD(&folio->_deferred_list); 496 } 497 498 static inline void prep_compound_tail(struct page *head, int tail_idx) 499 { 500 struct page *p = head + tail_idx; 501 502 p->mapping = TAIL_MAPPING; 503 set_compound_head(p, head); 504 set_page_private(p, 0); 505 } 506 507 extern void prep_compound_page(struct page *page, unsigned int order); 508 509 extern void post_alloc_hook(struct page *page, unsigned int order, 510 gfp_t gfp_flags); 511 extern int user_min_free_kbytes; 512 513 extern void free_unref_page(struct page *page, unsigned int order); 514 extern void free_unref_page_list(struct list_head *list); 515 516 extern void zone_pcp_reset(struct zone *zone); 517 extern void zone_pcp_disable(struct zone *zone); 518 extern void zone_pcp_enable(struct zone *zone); 519 extern void zone_pcp_init(struct zone *zone); 520 521 extern void *memmap_alloc(phys_addr_t size, phys_addr_t align, 522 phys_addr_t min_addr, 523 int nid, bool exact_nid); 524 525 void memmap_init_range(unsigned long, int, unsigned long, unsigned long, 526 unsigned long, enum meminit_context, struct vmem_altmap *, int); 527 528 529 int split_free_page(struct page *free_page, 530 unsigned int order, unsigned long split_pfn_offset); 531 532 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 533 534 /* 535 * in mm/compaction.c 536 */ 537 /* 538 * compact_control is used to track pages being migrated and the free pages 539 * they are being migrated to during memory compaction. The free_pfn starts 540 * at the end of a zone and migrate_pfn begins at the start. Movable pages 541 * are moved to the end of a zone during a compaction run and the run 542 * completes when free_pfn <= migrate_pfn 543 */ 544 struct compact_control { 545 struct list_head freepages; /* List of free pages to migrate to */ 546 struct list_head migratepages; /* List of pages being migrated */ 547 unsigned int nr_freepages; /* Number of isolated free pages */ 548 unsigned int nr_migratepages; /* Number of pages to migrate */ 549 unsigned long free_pfn; /* isolate_freepages search base */ 550 /* 551 * Acts as an in/out parameter to page isolation for migration. 552 * isolate_migratepages uses it as a search base. 553 * isolate_migratepages_block will update the value to the next pfn 554 * after the last isolated one. 555 */ 556 unsigned long migrate_pfn; 557 unsigned long fast_start_pfn; /* a pfn to start linear scan from */ 558 struct zone *zone; 559 unsigned long total_migrate_scanned; 560 unsigned long total_free_scanned; 561 unsigned short fast_search_fail;/* failures to use free list searches */ 562 short search_order; /* order to start a fast search at */ 563 const gfp_t gfp_mask; /* gfp mask of a direct compactor */ 564 int order; /* order a direct compactor needs */ 565 int migratetype; /* migratetype of direct compactor */ 566 const unsigned int alloc_flags; /* alloc flags of a direct compactor */ 567 const int highest_zoneidx; /* zone index of a direct compactor */ 568 enum migrate_mode mode; /* Async or sync migration mode */ 569 bool ignore_skip_hint; /* Scan blocks even if marked skip */ 570 bool no_set_skip_hint; /* Don't mark blocks for skipping */ 571 bool ignore_block_suitable; /* Scan blocks considered unsuitable */ 572 bool direct_compaction; /* False from kcompactd or /proc/... */ 573 bool proactive_compaction; /* kcompactd proactive compaction */ 574 bool whole_zone; /* Whole zone should/has been scanned */ 575 bool contended; /* Signal lock contention */ 576 bool finish_pageblock; /* Scan the remainder of a pageblock. Used 577 * when there are potentially transient 578 * isolation or migration failures to 579 * ensure forward progress. 580 */ 581 bool alloc_contig; /* alloc_contig_range allocation */ 582 }; 583 584 /* 585 * Used in direct compaction when a page should be taken from the freelists 586 * immediately when one is created during the free path. 587 */ 588 struct capture_control { 589 struct compact_control *cc; 590 struct page *page; 591 }; 592 593 unsigned long 594 isolate_freepages_range(struct compact_control *cc, 595 unsigned long start_pfn, unsigned long end_pfn); 596 int 597 isolate_migratepages_range(struct compact_control *cc, 598 unsigned long low_pfn, unsigned long end_pfn); 599 600 int __alloc_contig_migrate_range(struct compact_control *cc, 601 unsigned long start, unsigned long end); 602 603 /* Free whole pageblock and set its migration type to MIGRATE_CMA. */ 604 void init_cma_reserved_pageblock(struct page *page); 605 606 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 607 608 int find_suitable_fallback(struct free_area *area, unsigned int order, 609 int migratetype, bool only_stealable, bool *can_steal); 610 611 static inline bool free_area_empty(struct free_area *area, int migratetype) 612 { 613 return list_empty(&area->free_list[migratetype]); 614 } 615 616 /* 617 * These three helpers classifies VMAs for virtual memory accounting. 618 */ 619 620 /* 621 * Executable code area - executable, not writable, not stack 622 */ 623 static inline bool is_exec_mapping(vm_flags_t flags) 624 { 625 return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC; 626 } 627 628 /* 629 * Stack area (including shadow stacks) 630 * 631 * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous: 632 * do_mmap() forbids all other combinations. 633 */ 634 static inline bool is_stack_mapping(vm_flags_t flags) 635 { 636 return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK); 637 } 638 639 /* 640 * Data area - private, writable, not stack 641 */ 642 static inline bool is_data_mapping(vm_flags_t flags) 643 { 644 return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE; 645 } 646 647 /* mm/util.c */ 648 struct anon_vma *folio_anon_vma(struct folio *folio); 649 650 #ifdef CONFIG_MMU 651 void unmap_mapping_folio(struct folio *folio); 652 extern long populate_vma_page_range(struct vm_area_struct *vma, 653 unsigned long start, unsigned long end, int *locked); 654 extern long faultin_page_range(struct mm_struct *mm, unsigned long start, 655 unsigned long end, bool write, int *locked); 656 extern bool mlock_future_ok(struct mm_struct *mm, unsigned long flags, 657 unsigned long bytes); 658 /* 659 * mlock_vma_folio() and munlock_vma_folio(): 660 * should be called with vma's mmap_lock held for read or write, 661 * under page table lock for the pte/pmd being added or removed. 662 * 663 * mlock is usually called at the end of page_add_*_rmap(), munlock at 664 * the end of page_remove_rmap(); but new anon folios are managed by 665 * folio_add_lru_vma() calling mlock_new_folio(). 666 * 667 * @compound is used to include pmd mappings of THPs, but filter out 668 * pte mappings of THPs, which cannot be consistently counted: a pte 669 * mapping of the THP head cannot be distinguished by the page alone. 670 */ 671 void mlock_folio(struct folio *folio); 672 static inline void mlock_vma_folio(struct folio *folio, 673 struct vm_area_struct *vma, bool compound) 674 { 675 /* 676 * The VM_SPECIAL check here serves two purposes. 677 * 1) VM_IO check prevents migration from double-counting during mlock. 678 * 2) Although mmap_region() and mlock_fixup() take care that VM_LOCKED 679 * is never left set on a VM_SPECIAL vma, there is an interval while 680 * file->f_op->mmap() is using vm_insert_page(s), when VM_LOCKED may 681 * still be set while VM_SPECIAL bits are added: so ignore it then. 682 */ 683 if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED) && 684 (compound || !folio_test_large(folio))) 685 mlock_folio(folio); 686 } 687 688 void munlock_folio(struct folio *folio); 689 static inline void munlock_vma_folio(struct folio *folio, 690 struct vm_area_struct *vma, bool compound) 691 { 692 if (unlikely(vma->vm_flags & VM_LOCKED) && 693 (compound || !folio_test_large(folio))) 694 munlock_folio(folio); 695 } 696 697 void mlock_new_folio(struct folio *folio); 698 bool need_mlock_drain(int cpu); 699 void mlock_drain_local(void); 700 void mlock_drain_remote(int cpu); 701 702 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); 703 704 /* 705 * Return the start of user virtual address at the specific offset within 706 * a vma. 707 */ 708 static inline unsigned long 709 vma_pgoff_address(pgoff_t pgoff, unsigned long nr_pages, 710 struct vm_area_struct *vma) 711 { 712 unsigned long address; 713 714 if (pgoff >= vma->vm_pgoff) { 715 address = vma->vm_start + 716 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 717 /* Check for address beyond vma (or wrapped through 0?) */ 718 if (address < vma->vm_start || address >= vma->vm_end) 719 address = -EFAULT; 720 } else if (pgoff + nr_pages - 1 >= vma->vm_pgoff) { 721 /* Test above avoids possibility of wrap to 0 on 32-bit */ 722 address = vma->vm_start; 723 } else { 724 address = -EFAULT; 725 } 726 return address; 727 } 728 729 /* 730 * Return the start of user virtual address of a page within a vma. 731 * Returns -EFAULT if all of the page is outside the range of vma. 732 * If page is a compound head, the entire compound page is considered. 733 */ 734 static inline unsigned long 735 vma_address(struct page *page, struct vm_area_struct *vma) 736 { 737 VM_BUG_ON_PAGE(PageKsm(page), page); /* KSM page->index unusable */ 738 return vma_pgoff_address(page_to_pgoff(page), compound_nr(page), vma); 739 } 740 741 /* 742 * Then at what user virtual address will none of the range be found in vma? 743 * Assumes that vma_address() already returned a good starting address. 744 */ 745 static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw) 746 { 747 struct vm_area_struct *vma = pvmw->vma; 748 pgoff_t pgoff; 749 unsigned long address; 750 751 /* Common case, plus ->pgoff is invalid for KSM */ 752 if (pvmw->nr_pages == 1) 753 return pvmw->address + PAGE_SIZE; 754 755 pgoff = pvmw->pgoff + pvmw->nr_pages; 756 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 757 /* Check for address beyond vma (or wrapped through 0?) */ 758 if (address < vma->vm_start || address > vma->vm_end) 759 address = vma->vm_end; 760 return address; 761 } 762 763 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, 764 struct file *fpin) 765 { 766 int flags = vmf->flags; 767 768 if (fpin) 769 return fpin; 770 771 /* 772 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or 773 * anything, so we only pin the file and drop the mmap_lock if only 774 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. 775 */ 776 if (fault_flag_allow_retry_first(flags) && 777 !(flags & FAULT_FLAG_RETRY_NOWAIT)) { 778 fpin = get_file(vmf->vma->vm_file); 779 release_fault_lock(vmf); 780 } 781 return fpin; 782 } 783 #else /* !CONFIG_MMU */ 784 static inline void unmap_mapping_folio(struct folio *folio) { } 785 static inline void mlock_new_folio(struct folio *folio) { } 786 static inline bool need_mlock_drain(int cpu) { return false; } 787 static inline void mlock_drain_local(void) { } 788 static inline void mlock_drain_remote(int cpu) { } 789 static inline void vunmap_range_noflush(unsigned long start, unsigned long end) 790 { 791 } 792 #endif /* !CONFIG_MMU */ 793 794 /* Memory initialisation debug and verification */ 795 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 796 DECLARE_STATIC_KEY_TRUE(deferred_pages); 797 798 bool __init deferred_grow_zone(struct zone *zone, unsigned int order); 799 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 800 801 enum mminit_level { 802 MMINIT_WARNING, 803 MMINIT_VERIFY, 804 MMINIT_TRACE 805 }; 806 807 #ifdef CONFIG_DEBUG_MEMORY_INIT 808 809 extern int mminit_loglevel; 810 811 #define mminit_dprintk(level, prefix, fmt, arg...) \ 812 do { \ 813 if (level < mminit_loglevel) { \ 814 if (level <= MMINIT_WARNING) \ 815 pr_warn("mminit::" prefix " " fmt, ##arg); \ 816 else \ 817 printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \ 818 } \ 819 } while (0) 820 821 extern void mminit_verify_pageflags_layout(void); 822 extern void mminit_verify_zonelist(void); 823 #else 824 825 static inline void mminit_dprintk(enum mminit_level level, 826 const char *prefix, const char *fmt, ...) 827 { 828 } 829 830 static inline void mminit_verify_pageflags_layout(void) 831 { 832 } 833 834 static inline void mminit_verify_zonelist(void) 835 { 836 } 837 #endif /* CONFIG_DEBUG_MEMORY_INIT */ 838 839 #define NODE_RECLAIM_NOSCAN -2 840 #define NODE_RECLAIM_FULL -1 841 #define NODE_RECLAIM_SOME 0 842 #define NODE_RECLAIM_SUCCESS 1 843 844 #ifdef CONFIG_NUMA 845 extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int); 846 extern int find_next_best_node(int node, nodemask_t *used_node_mask); 847 #else 848 static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask, 849 unsigned int order) 850 { 851 return NODE_RECLAIM_NOSCAN; 852 } 853 static inline int find_next_best_node(int node, nodemask_t *used_node_mask) 854 { 855 return NUMA_NO_NODE; 856 } 857 #endif 858 859 /* 860 * mm/memory-failure.c 861 */ 862 extern int hwpoison_filter(struct page *p); 863 864 extern u32 hwpoison_filter_dev_major; 865 extern u32 hwpoison_filter_dev_minor; 866 extern u64 hwpoison_filter_flags_mask; 867 extern u64 hwpoison_filter_flags_value; 868 extern u64 hwpoison_filter_memcg; 869 extern u32 hwpoison_filter_enable; 870 871 extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long, 872 unsigned long, unsigned long, 873 unsigned long, unsigned long); 874 875 extern void set_pageblock_order(void); 876 unsigned long reclaim_pages(struct list_head *folio_list); 877 unsigned int reclaim_clean_pages_from_list(struct zone *zone, 878 struct list_head *folio_list); 879 /* The ALLOC_WMARK bits are used as an index to zone->watermark */ 880 #define ALLOC_WMARK_MIN WMARK_MIN 881 #define ALLOC_WMARK_LOW WMARK_LOW 882 #define ALLOC_WMARK_HIGH WMARK_HIGH 883 #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */ 884 885 /* Mask to get the watermark bits */ 886 #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1) 887 888 /* 889 * Only MMU archs have async oom victim reclaim - aka oom_reaper so we 890 * cannot assume a reduced access to memory reserves is sufficient for 891 * !MMU 892 */ 893 #ifdef CONFIG_MMU 894 #define ALLOC_OOM 0x08 895 #else 896 #define ALLOC_OOM ALLOC_NO_WATERMARKS 897 #endif 898 899 #define ALLOC_NON_BLOCK 0x10 /* Caller cannot block. Allow access 900 * to 25% of the min watermark or 901 * 62.5% if __GFP_HIGH is set. 902 */ 903 #define ALLOC_MIN_RESERVE 0x20 /* __GFP_HIGH set. Allow access to 50% 904 * of the min watermark. 905 */ 906 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */ 907 #define ALLOC_CMA 0x80 /* allow allocations from CMA areas */ 908 #ifdef CONFIG_ZONE_DMA32 909 #define ALLOC_NOFRAGMENT 0x100 /* avoid mixing pageblock types */ 910 #else 911 #define ALLOC_NOFRAGMENT 0x0 912 #endif 913 #define ALLOC_HIGHATOMIC 0x200 /* Allows access to MIGRATE_HIGHATOMIC */ 914 #define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */ 915 916 /* Flags that allow allocations below the min watermark. */ 917 #define ALLOC_RESERVES (ALLOC_NON_BLOCK|ALLOC_MIN_RESERVE|ALLOC_HIGHATOMIC|ALLOC_OOM) 918 919 enum ttu_flags; 920 struct tlbflush_unmap_batch; 921 922 923 /* 924 * only for MM internal work items which do not depend on 925 * any allocations or locks which might depend on allocations 926 */ 927 extern struct workqueue_struct *mm_percpu_wq; 928 929 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 930 void try_to_unmap_flush(void); 931 void try_to_unmap_flush_dirty(void); 932 void flush_tlb_batched_pending(struct mm_struct *mm); 933 #else 934 static inline void try_to_unmap_flush(void) 935 { 936 } 937 static inline void try_to_unmap_flush_dirty(void) 938 { 939 } 940 static inline void flush_tlb_batched_pending(struct mm_struct *mm) 941 { 942 } 943 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ 944 945 extern const struct trace_print_flags pageflag_names[]; 946 extern const struct trace_print_flags pagetype_names[]; 947 extern const struct trace_print_flags vmaflag_names[]; 948 extern const struct trace_print_flags gfpflag_names[]; 949 950 static inline bool is_migrate_highatomic(enum migratetype migratetype) 951 { 952 return migratetype == MIGRATE_HIGHATOMIC; 953 } 954 955 static inline bool is_migrate_highatomic_page(struct page *page) 956 { 957 return get_pageblock_migratetype(page) == MIGRATE_HIGHATOMIC; 958 } 959 960 void setup_zone_pageset(struct zone *zone); 961 962 struct migration_target_control { 963 int nid; /* preferred node id */ 964 nodemask_t *nmask; 965 gfp_t gfp_mask; 966 }; 967 968 /* 969 * mm/filemap.c 970 */ 971 size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, 972 struct folio *folio, loff_t fpos, size_t size); 973 974 /* 975 * mm/vmalloc.c 976 */ 977 #ifdef CONFIG_MMU 978 void __init vmalloc_init(void); 979 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 980 pgprot_t prot, struct page **pages, unsigned int page_shift); 981 #else 982 static inline void vmalloc_init(void) 983 { 984 } 985 986 static inline 987 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 988 pgprot_t prot, struct page **pages, unsigned int page_shift) 989 { 990 return -EINVAL; 991 } 992 #endif 993 994 int __must_check __vmap_pages_range_noflush(unsigned long addr, 995 unsigned long end, pgprot_t prot, 996 struct page **pages, unsigned int page_shift); 997 998 void vunmap_range_noflush(unsigned long start, unsigned long end); 999 1000 void __vunmap_range_noflush(unsigned long start, unsigned long end); 1001 1002 int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 1003 unsigned long addr, int page_nid, int *flags); 1004 1005 void free_zone_device_page(struct page *page); 1006 int migrate_device_coherent_page(struct page *page); 1007 1008 /* 1009 * mm/gup.c 1010 */ 1011 int __must_check try_grab_folio(struct folio *folio, int refs, 1012 unsigned int flags); 1013 1014 /* 1015 * mm/huge_memory.c 1016 */ 1017 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, 1018 unsigned long addr, pmd_t *pmd, 1019 unsigned int flags); 1020 1021 enum { 1022 /* mark page accessed */ 1023 FOLL_TOUCH = 1 << 16, 1024 /* a retry, previous pass started an IO */ 1025 FOLL_TRIED = 1 << 17, 1026 /* we are working on non-current tsk/mm */ 1027 FOLL_REMOTE = 1 << 18, 1028 /* pages must be released via unpin_user_page */ 1029 FOLL_PIN = 1 << 19, 1030 /* gup_fast: prevent fall-back to slow gup */ 1031 FOLL_FAST_ONLY = 1 << 20, 1032 /* allow unlocking the mmap lock */ 1033 FOLL_UNLOCKABLE = 1 << 21, 1034 /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ 1035 FOLL_MADV_POPULATE = 1 << 22, 1036 }; 1037 1038 #define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ 1039 FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ 1040 FOLL_MADV_POPULATE) 1041 1042 /* 1043 * Indicates for which pages that are write-protected in the page table, 1044 * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the 1045 * GUP pin will remain consistent with the pages mapped into the page tables 1046 * of the MM. 1047 * 1048 * Temporary unmapping of PageAnonExclusive() pages or clearing of 1049 * PageAnonExclusive() has to protect against concurrent GUP: 1050 * * Ordinary GUP: Using the PT lock 1051 * * GUP-fast and fork(): mm->write_protect_seq 1052 * * GUP-fast and KSM or temporary unmapping (swap, migration): see 1053 * page_try_share_anon_rmap() 1054 * 1055 * Must be called with the (sub)page that's actually referenced via the 1056 * page table entry, which might not necessarily be the head page for a 1057 * PTE-mapped THP. 1058 * 1059 * If the vma is NULL, we're coming from the GUP-fast path and might have 1060 * to fallback to the slow path just to lookup the vma. 1061 */ 1062 static inline bool gup_must_unshare(struct vm_area_struct *vma, 1063 unsigned int flags, struct page *page) 1064 { 1065 /* 1066 * FOLL_WRITE is implicitly handled correctly as the page table entry 1067 * has to be writable -- and if it references (part of) an anonymous 1068 * folio, that part is required to be marked exclusive. 1069 */ 1070 if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN) 1071 return false; 1072 /* 1073 * Note: PageAnon(page) is stable until the page is actually getting 1074 * freed. 1075 */ 1076 if (!PageAnon(page)) { 1077 /* 1078 * We only care about R/O long-term pining: R/O short-term 1079 * pinning does not have the semantics to observe successive 1080 * changes through the process page tables. 1081 */ 1082 if (!(flags & FOLL_LONGTERM)) 1083 return false; 1084 1085 /* We really need the vma ... */ 1086 if (!vma) 1087 return true; 1088 1089 /* 1090 * ... because we only care about writable private ("COW") 1091 * mappings where we have to break COW early. 1092 */ 1093 return is_cow_mapping(vma->vm_flags); 1094 } 1095 1096 /* Paired with a memory barrier in page_try_share_anon_rmap(). */ 1097 if (IS_ENABLED(CONFIG_HAVE_FAST_GUP)) 1098 smp_rmb(); 1099 1100 /* 1101 * During GUP-fast we might not get called on the head page for a 1102 * hugetlb page that is mapped using cont-PTE, because GUP-fast does 1103 * not work with the abstracted hugetlb PTEs that always point at the 1104 * head page. For hugetlb, PageAnonExclusive only applies on the head 1105 * page (as it cannot be partially COW-shared), so lookup the head page. 1106 */ 1107 if (unlikely(!PageHead(page) && PageHuge(page))) 1108 page = compound_head(page); 1109 1110 /* 1111 * Note that PageKsm() pages cannot be exclusive, and consequently, 1112 * cannot get pinned. 1113 */ 1114 return !PageAnonExclusive(page); 1115 } 1116 1117 extern bool mirrored_kernelcore; 1118 extern bool memblock_has_mirror(void); 1119 1120 static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) 1121 { 1122 /* 1123 * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty 1124 * enablements, because when without soft-dirty being compiled in, 1125 * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) 1126 * will be constantly true. 1127 */ 1128 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 1129 return false; 1130 1131 /* 1132 * Soft-dirty is kind of special: its tracking is enabled when the 1133 * vma flags not set. 1134 */ 1135 return !(vma->vm_flags & VM_SOFTDIRTY); 1136 } 1137 1138 static inline void vma_iter_config(struct vma_iterator *vmi, 1139 unsigned long index, unsigned long last) 1140 { 1141 MAS_BUG_ON(&vmi->mas, vmi->mas.node != MAS_START && 1142 (vmi->mas.index > index || vmi->mas.last < index)); 1143 __mas_set_range(&vmi->mas, index, last - 1); 1144 } 1145 1146 /* 1147 * VMA Iterator functions shared between nommu and mmap 1148 */ 1149 static inline int vma_iter_prealloc(struct vma_iterator *vmi, 1150 struct vm_area_struct *vma) 1151 { 1152 return mas_preallocate(&vmi->mas, vma, GFP_KERNEL); 1153 } 1154 1155 static inline void vma_iter_clear(struct vma_iterator *vmi) 1156 { 1157 mas_store_prealloc(&vmi->mas, NULL); 1158 } 1159 1160 static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, 1161 unsigned long start, unsigned long end, gfp_t gfp) 1162 { 1163 __mas_set_range(&vmi->mas, start, end - 1); 1164 mas_store_gfp(&vmi->mas, NULL, gfp); 1165 if (unlikely(mas_is_err(&vmi->mas))) 1166 return -ENOMEM; 1167 1168 return 0; 1169 } 1170 1171 static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) 1172 { 1173 return mas_walk(&vmi->mas); 1174 } 1175 1176 /* Store a VMA with preallocated memory */ 1177 static inline void vma_iter_store(struct vma_iterator *vmi, 1178 struct vm_area_struct *vma) 1179 { 1180 1181 #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 1182 if (MAS_WARN_ON(&vmi->mas, vmi->mas.node != MAS_START && 1183 vmi->mas.index > vma->vm_start)) { 1184 pr_warn("%lx > %lx\n store vma %lx-%lx\n into slot %lx-%lx\n", 1185 vmi->mas.index, vma->vm_start, vma->vm_start, 1186 vma->vm_end, vmi->mas.index, vmi->mas.last); 1187 } 1188 if (MAS_WARN_ON(&vmi->mas, vmi->mas.node != MAS_START && 1189 vmi->mas.last < vma->vm_start)) { 1190 pr_warn("%lx < %lx\nstore vma %lx-%lx\ninto slot %lx-%lx\n", 1191 vmi->mas.last, vma->vm_start, vma->vm_start, vma->vm_end, 1192 vmi->mas.index, vmi->mas.last); 1193 } 1194 #endif 1195 1196 if (vmi->mas.node != MAS_START && 1197 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start))) 1198 vma_iter_invalidate(vmi); 1199 1200 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); 1201 mas_store_prealloc(&vmi->mas, vma); 1202 } 1203 1204 static inline int vma_iter_store_gfp(struct vma_iterator *vmi, 1205 struct vm_area_struct *vma, gfp_t gfp) 1206 { 1207 if (vmi->mas.node != MAS_START && 1208 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start))) 1209 vma_iter_invalidate(vmi); 1210 1211 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); 1212 mas_store_gfp(&vmi->mas, vma, gfp); 1213 if (unlikely(mas_is_err(&vmi->mas))) 1214 return -ENOMEM; 1215 1216 return 0; 1217 } 1218 1219 /* 1220 * VMA lock generalization 1221 */ 1222 struct vma_prepare { 1223 struct vm_area_struct *vma; 1224 struct vm_area_struct *adj_next; 1225 struct file *file; 1226 struct address_space *mapping; 1227 struct anon_vma *anon_vma; 1228 struct vm_area_struct *insert; 1229 struct vm_area_struct *remove; 1230 struct vm_area_struct *remove2; 1231 }; 1232 #endif /* __MM_INTERNAL_H */ 1233