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/swapops.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 & ~PAGE_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 = call_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 most 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 #ifdef CONFIG_MMU 201 202 /* Flags for folio_pte_batch(). */ 203 typedef int __bitwise fpb_t; 204 205 /* Compare PTEs after pte_mkclean(), ignoring the dirty bit. */ 206 #define FPB_IGNORE_DIRTY ((__force fpb_t)BIT(0)) 207 208 /* Compare PTEs after pte_clear_soft_dirty(), ignoring the soft-dirty bit. */ 209 #define FPB_IGNORE_SOFT_DIRTY ((__force fpb_t)BIT(1)) 210 211 static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) 212 { 213 if (flags & FPB_IGNORE_DIRTY) 214 pte = pte_mkclean(pte); 215 if (likely(flags & FPB_IGNORE_SOFT_DIRTY)) 216 pte = pte_clear_soft_dirty(pte); 217 return pte_wrprotect(pte_mkold(pte)); 218 } 219 220 /** 221 * folio_pte_batch - detect a PTE batch for a large folio 222 * @folio: The large folio to detect a PTE batch for. 223 * @addr: The user virtual address the first page is mapped at. 224 * @start_ptep: Page table pointer for the first entry. 225 * @pte: Page table entry for the first page. 226 * @max_nr: The maximum number of table entries to consider. 227 * @flags: Flags to modify the PTE batch semantics. 228 * @any_writable: Optional pointer to indicate whether any entry except the 229 * first one is writable. 230 * @any_young: Optional pointer to indicate whether any entry except the 231 * first one is young. 232 * @any_dirty: Optional pointer to indicate whether any entry except the 233 * first one is dirty. 234 * 235 * Detect a PTE batch: consecutive (present) PTEs that map consecutive 236 * pages of the same large folio. 237 * 238 * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN, 239 * the accessed bit, writable bit, dirty bit (with FPB_IGNORE_DIRTY) and 240 * soft-dirty bit (with FPB_IGNORE_SOFT_DIRTY). 241 * 242 * start_ptep must map any page of the folio. max_nr must be at least one and 243 * must be limited by the caller so scanning cannot exceed a single page table. 244 * 245 * Return: the number of table entries in the batch. 246 */ 247 static inline int folio_pte_batch(struct folio *folio, unsigned long addr, 248 pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags, 249 bool *any_writable, bool *any_young, bool *any_dirty) 250 { 251 pte_t expected_pte, *ptep; 252 bool writable, young, dirty; 253 int nr, cur_nr; 254 255 if (any_writable) 256 *any_writable = false; 257 if (any_young) 258 *any_young = false; 259 if (any_dirty) 260 *any_dirty = false; 261 262 VM_WARN_ON_FOLIO(!pte_present(pte), folio); 263 VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio); 264 VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio); 265 266 /* Limit max_nr to the actual remaining PFNs in the folio we could batch. */ 267 max_nr = min_t(unsigned long, max_nr, 268 folio_pfn(folio) + folio_nr_pages(folio) - pte_pfn(pte)); 269 270 nr = pte_batch_hint(start_ptep, pte); 271 expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); 272 ptep = start_ptep + nr; 273 274 while (nr < max_nr) { 275 pte = ptep_get(ptep); 276 if (any_writable) 277 writable = !!pte_write(pte); 278 if (any_young) 279 young = !!pte_young(pte); 280 if (any_dirty) 281 dirty = !!pte_dirty(pte); 282 pte = __pte_batch_clear_ignored(pte, flags); 283 284 if (!pte_same(pte, expected_pte)) 285 break; 286 287 if (any_writable) 288 *any_writable |= writable; 289 if (any_young) 290 *any_young |= young; 291 if (any_dirty) 292 *any_dirty |= dirty; 293 294 cur_nr = pte_batch_hint(ptep, pte); 295 expected_pte = pte_advance_pfn(expected_pte, cur_nr); 296 ptep += cur_nr; 297 nr += cur_nr; 298 } 299 300 return min(nr, max_nr); 301 } 302 303 /** 304 * pte_move_swp_offset - Move the swap entry offset field of a swap pte 305 * forward or backward by delta 306 * @pte: The initial pte state; is_swap_pte(pte) must be true and 307 * non_swap_entry() must be false. 308 * @delta: The direction and the offset we are moving; forward if delta 309 * is positive; backward if delta is negative 310 * 311 * Moves the swap offset, while maintaining all other fields, including 312 * swap type, and any swp pte bits. The resulting pte is returned. 313 */ 314 static inline pte_t pte_move_swp_offset(pte_t pte, long delta) 315 { 316 swp_entry_t entry = pte_to_swp_entry(pte); 317 pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry), 318 (swp_offset(entry) + delta))); 319 320 if (pte_swp_soft_dirty(pte)) 321 new = pte_swp_mksoft_dirty(new); 322 if (pte_swp_exclusive(pte)) 323 new = pte_swp_mkexclusive(new); 324 if (pte_swp_uffd_wp(pte)) 325 new = pte_swp_mkuffd_wp(new); 326 327 return new; 328 } 329 330 331 /** 332 * pte_next_swp_offset - Increment the swap entry offset field of a swap pte. 333 * @pte: The initial pte state; is_swap_pte(pte) must be true and 334 * non_swap_entry() must be false. 335 * 336 * Increments the swap offset, while maintaining all other fields, including 337 * swap type, and any swp pte bits. The resulting pte is returned. 338 */ 339 static inline pte_t pte_next_swp_offset(pte_t pte) 340 { 341 return pte_move_swp_offset(pte, 1); 342 } 343 344 /** 345 * swap_pte_batch - detect a PTE batch for a set of contiguous swap entries 346 * @start_ptep: Page table pointer for the first entry. 347 * @max_nr: The maximum number of table entries to consider. 348 * @pte: Page table entry for the first entry. 349 * 350 * Detect a batch of contiguous swap entries: consecutive (non-present) PTEs 351 * containing swap entries all with consecutive offsets and targeting the same 352 * swap type, all with matching swp pte bits. 353 * 354 * max_nr must be at least one and must be limited by the caller so scanning 355 * cannot exceed a single page table. 356 * 357 * Return: the number of table entries in the batch. 358 */ 359 static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte) 360 { 361 pte_t expected_pte = pte_next_swp_offset(pte); 362 const pte_t *end_ptep = start_ptep + max_nr; 363 swp_entry_t entry = pte_to_swp_entry(pte); 364 pte_t *ptep = start_ptep + 1; 365 unsigned short cgroup_id; 366 367 VM_WARN_ON(max_nr < 1); 368 VM_WARN_ON(!is_swap_pte(pte)); 369 VM_WARN_ON(non_swap_entry(entry)); 370 371 cgroup_id = lookup_swap_cgroup_id(entry); 372 while (ptep < end_ptep) { 373 pte = ptep_get(ptep); 374 375 if (!pte_same(pte, expected_pte)) 376 break; 377 if (lookup_swap_cgroup_id(pte_to_swp_entry(pte)) != cgroup_id) 378 break; 379 expected_pte = pte_next_swp_offset(expected_pte); 380 ptep++; 381 } 382 383 return ptep - start_ptep; 384 } 385 #endif /* CONFIG_MMU */ 386 387 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 388 int nr_throttled); 389 static inline void acct_reclaim_writeback(struct folio *folio) 390 { 391 pg_data_t *pgdat = folio_pgdat(folio); 392 int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled); 393 394 if (nr_throttled) 395 __acct_reclaim_writeback(pgdat, folio, nr_throttled); 396 } 397 398 static inline void wake_throttle_isolated(pg_data_t *pgdat) 399 { 400 wait_queue_head_t *wqh; 401 402 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED]; 403 if (waitqueue_active(wqh)) 404 wake_up(wqh); 405 } 406 407 vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf); 408 static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) 409 { 410 vm_fault_t ret = __vmf_anon_prepare(vmf); 411 412 if (unlikely(ret & VM_FAULT_RETRY)) 413 vma_end_read(vmf->vma); 414 return ret; 415 } 416 417 vm_fault_t do_swap_page(struct vm_fault *vmf); 418 void folio_rotate_reclaimable(struct folio *folio); 419 bool __folio_end_writeback(struct folio *folio); 420 void deactivate_file_folio(struct folio *folio); 421 void folio_activate(struct folio *folio); 422 423 void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas, 424 struct vm_area_struct *start_vma, unsigned long floor, 425 unsigned long ceiling, bool mm_wr_locked); 426 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); 427 428 struct zap_details; 429 void unmap_page_range(struct mmu_gather *tlb, 430 struct vm_area_struct *vma, 431 unsigned long addr, unsigned long end, 432 struct zap_details *details); 433 void zap_page_range_single_batched(struct mmu_gather *tlb, 434 struct vm_area_struct *vma, unsigned long addr, 435 unsigned long size, struct zap_details *details); 436 int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, 437 gfp_t gfp); 438 439 void page_cache_ra_order(struct readahead_control *, struct file_ra_state *); 440 void force_page_cache_ra(struct readahead_control *, unsigned long nr); 441 static inline void force_page_cache_readahead(struct address_space *mapping, 442 struct file *file, pgoff_t index, unsigned long nr_to_read) 443 { 444 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index); 445 force_page_cache_ra(&ractl, nr_to_read); 446 } 447 448 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, 449 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 450 unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, 451 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 452 void filemap_free_folio(struct address_space *mapping, struct folio *folio); 453 int truncate_inode_folio(struct address_space *mapping, struct folio *folio); 454 bool truncate_inode_partial_folio(struct folio *folio, loff_t start, 455 loff_t end); 456 long mapping_evict_folio(struct address_space *mapping, struct folio *folio); 457 unsigned long mapping_try_invalidate(struct address_space *mapping, 458 pgoff_t start, pgoff_t end, unsigned long *nr_failed); 459 460 /** 461 * folio_evictable - Test whether a folio is evictable. 462 * @folio: The folio to test. 463 * 464 * Test whether @folio is evictable -- i.e., should be placed on 465 * active/inactive lists vs unevictable list. 466 * 467 * Reasons folio might not be evictable: 468 * 1. folio's mapping marked unevictable 469 * 2. One of the pages in the folio is part of an mlocked VMA 470 */ 471 static inline bool folio_evictable(struct folio *folio) 472 { 473 bool ret; 474 475 /* Prevent address_space of inode and swap cache from being freed */ 476 rcu_read_lock(); 477 ret = !mapping_unevictable(folio_mapping(folio)) && 478 !folio_test_mlocked(folio); 479 rcu_read_unlock(); 480 return ret; 481 } 482 483 /* 484 * Turn a non-refcounted page (->_refcount == 0) into refcounted with 485 * a count of one. 486 */ 487 static inline void set_page_refcounted(struct page *page) 488 { 489 VM_BUG_ON_PAGE(PageTail(page), page); 490 VM_BUG_ON_PAGE(page_ref_count(page), page); 491 set_page_count(page, 1); 492 } 493 494 /* 495 * Return true if a folio needs ->release_folio() calling upon it. 496 */ 497 static inline bool folio_needs_release(struct folio *folio) 498 { 499 struct address_space *mapping = folio_mapping(folio); 500 501 return folio_has_private(folio) || 502 (mapping && mapping_release_always(mapping)); 503 } 504 505 extern unsigned long highest_memmap_pfn; 506 507 /* 508 * Maximum number of reclaim retries without progress before the OOM 509 * killer is consider the only way forward. 510 */ 511 #define MAX_RECLAIM_RETRIES 16 512 513 /* 514 * in mm/vmscan.c: 515 */ 516 bool folio_isolate_lru(struct folio *folio); 517 void folio_putback_lru(struct folio *folio); 518 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason); 519 520 /* 521 * in mm/rmap.c: 522 */ 523 pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address); 524 525 /* 526 * in mm/page_alloc.c 527 */ 528 #define K(x) ((x) << (PAGE_SHIFT-10)) 529 530 extern char * const zone_names[MAX_NR_ZONES]; 531 532 /* perform sanity checks on struct pages being allocated or freed */ 533 DECLARE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled); 534 535 extern int min_free_kbytes; 536 extern int defrag_mode; 537 538 void setup_per_zone_wmarks(void); 539 void calculate_min_free_kbytes(void); 540 int __meminit init_per_zone_wmark_min(void); 541 void page_alloc_sysctl_init(void); 542 543 /* 544 * Structure for holding the mostly immutable allocation parameters passed 545 * between functions involved in allocations, including the alloc_pages* 546 * family of functions. 547 * 548 * nodemask, migratetype and highest_zoneidx are initialized only once in 549 * __alloc_pages() and then never change. 550 * 551 * zonelist, preferred_zone and highest_zoneidx are set first in 552 * __alloc_pages() for the fast path, and might be later changed 553 * in __alloc_pages_slowpath(). All other functions pass the whole structure 554 * by a const pointer. 555 */ 556 struct alloc_context { 557 struct zonelist *zonelist; 558 nodemask_t *nodemask; 559 struct zoneref *preferred_zoneref; 560 int migratetype; 561 562 /* 563 * highest_zoneidx represents highest usable zone index of 564 * the allocation request. Due to the nature of the zone, 565 * memory on lower zone than the highest_zoneidx will be 566 * protected by lowmem_reserve[highest_zoneidx]. 567 * 568 * highest_zoneidx is also used by reclaim/compaction to limit 569 * the target zone since higher zone than this index cannot be 570 * usable for this allocation request. 571 */ 572 enum zone_type highest_zoneidx; 573 bool spread_dirty_pages; 574 }; 575 576 /* 577 * This function returns the order of a free page in the buddy system. In 578 * general, page_zone(page)->lock must be held by the caller to prevent the 579 * page from being allocated in parallel and returning garbage as the order. 580 * If a caller does not hold page_zone(page)->lock, it must guarantee that the 581 * page cannot be allocated or merged in parallel. Alternatively, it must 582 * handle invalid values gracefully, and use buddy_order_unsafe() below. 583 */ 584 static inline unsigned int buddy_order(struct page *page) 585 { 586 /* PageBuddy() must be checked by the caller */ 587 return page_private(page); 588 } 589 590 /* 591 * Like buddy_order(), but for callers who cannot afford to hold the zone lock. 592 * PageBuddy() should be checked first by the caller to minimize race window, 593 * and invalid values must be handled gracefully. 594 * 595 * READ_ONCE is used so that if the caller assigns the result into a local 596 * variable and e.g. tests it for valid range before using, the compiler cannot 597 * decide to remove the variable and inline the page_private(page) multiple 598 * times, potentially observing different values in the tests and the actual 599 * use of the result. 600 */ 601 #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) 602 603 /* 604 * This function checks whether a page is free && is the buddy 605 * we can coalesce a page and its buddy if 606 * (a) the buddy is not in a hole (check before calling!) && 607 * (b) the buddy is in the buddy system && 608 * (c) a page and its buddy have the same order && 609 * (d) a page and its buddy are in the same zone. 610 * 611 * For recording whether a page is in the buddy system, we set PageBuddy. 612 * Setting, clearing, and testing PageBuddy is serialized by zone->lock. 613 * 614 * For recording page's order, we use page_private(page). 615 */ 616 static inline bool page_is_buddy(struct page *page, struct page *buddy, 617 unsigned int order) 618 { 619 if (!page_is_guard(buddy) && !PageBuddy(buddy)) 620 return false; 621 622 if (buddy_order(buddy) != order) 623 return false; 624 625 /* 626 * zone check is done late to avoid uselessly calculating 627 * zone/node ids for pages that could never merge. 628 */ 629 if (page_zone_id(page) != page_zone_id(buddy)) 630 return false; 631 632 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); 633 634 return true; 635 } 636 637 /* 638 * Locate the struct page for both the matching buddy in our 639 * pair (buddy1) and the combined O(n+1) page they form (page). 640 * 641 * 1) Any buddy B1 will have an order O twin B2 which satisfies 642 * the following equation: 643 * B2 = B1 ^ (1 << O) 644 * For example, if the starting buddy (buddy2) is #8 its order 645 * 1 buddy is #10: 646 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10 647 * 648 * 2) Any buddy B will have an order O+1 parent P which 649 * satisfies the following equation: 650 * P = B & ~(1 << O) 651 * 652 * Assumption: *_mem_map is contiguous at least up to MAX_PAGE_ORDER 653 */ 654 static inline unsigned long 655 __find_buddy_pfn(unsigned long page_pfn, unsigned int order) 656 { 657 return page_pfn ^ (1 << order); 658 } 659 660 /* 661 * Find the buddy of @page and validate it. 662 * @page: The input page 663 * @pfn: The pfn of the page, it saves a call to page_to_pfn() when the 664 * function is used in the performance-critical __free_one_page(). 665 * @order: The order of the page 666 * @buddy_pfn: The output pointer to the buddy pfn, it also saves a call to 667 * page_to_pfn(). 668 * 669 * The found buddy can be a non PageBuddy, out of @page's zone, or its order is 670 * not the same as @page. The validation is necessary before use it. 671 * 672 * Return: the found buddy page or NULL if not found. 673 */ 674 static inline struct page *find_buddy_page_pfn(struct page *page, 675 unsigned long pfn, unsigned int order, unsigned long *buddy_pfn) 676 { 677 unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order); 678 struct page *buddy; 679 680 buddy = page + (__buddy_pfn - pfn); 681 if (buddy_pfn) 682 *buddy_pfn = __buddy_pfn; 683 684 if (page_is_buddy(page, buddy, order)) 685 return buddy; 686 return NULL; 687 } 688 689 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn, 690 unsigned long end_pfn, struct zone *zone); 691 692 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, 693 unsigned long end_pfn, struct zone *zone) 694 { 695 if (zone->contiguous) 696 return pfn_to_page(start_pfn); 697 698 return __pageblock_pfn_to_page(start_pfn, end_pfn, zone); 699 } 700 701 void set_zone_contiguous(struct zone *zone); 702 bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, 703 unsigned long nr_pages); 704 705 static inline void clear_zone_contiguous(struct zone *zone) 706 { 707 zone->contiguous = false; 708 } 709 710 extern int __isolate_free_page(struct page *page, unsigned int order); 711 extern void __putback_isolated_page(struct page *page, unsigned int order, 712 int mt); 713 extern void memblock_free_pages(struct page *page, unsigned long pfn, 714 unsigned int order); 715 extern void __free_pages_core(struct page *page, unsigned int order, 716 enum meminit_context context); 717 718 /* 719 * This will have no effect, other than possibly generating a warning, if the 720 * caller passes in a non-large folio. 721 */ 722 static inline void folio_set_order(struct folio *folio, unsigned int order) 723 { 724 if (WARN_ON_ONCE(!order || !folio_test_large(folio))) 725 return; 726 727 folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order; 728 #ifdef NR_PAGES_IN_LARGE_FOLIO 729 folio->_nr_pages = 1U << order; 730 #endif 731 } 732 733 bool __folio_unqueue_deferred_split(struct folio *folio); 734 static inline bool folio_unqueue_deferred_split(struct folio *folio) 735 { 736 if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio)) 737 return false; 738 739 /* 740 * At this point, there is no one trying to add the folio to 741 * deferred_list. If folio is not in deferred_list, it's safe 742 * to check without acquiring the split_queue_lock. 743 */ 744 if (data_race(list_empty(&folio->_deferred_list))) 745 return false; 746 747 return __folio_unqueue_deferred_split(folio); 748 } 749 750 static inline struct folio *page_rmappable_folio(struct page *page) 751 { 752 struct folio *folio = (struct folio *)page; 753 754 if (folio && folio_test_large(folio)) 755 folio_set_large_rmappable(folio); 756 return folio; 757 } 758 759 static inline void prep_compound_head(struct page *page, unsigned int order) 760 { 761 struct folio *folio = (struct folio *)page; 762 763 folio_set_order(folio, order); 764 atomic_set(&folio->_large_mapcount, -1); 765 if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) 766 atomic_set(&folio->_nr_pages_mapped, 0); 767 if (IS_ENABLED(CONFIG_MM_ID)) { 768 folio->_mm_ids = 0; 769 folio->_mm_id_mapcount[0] = -1; 770 folio->_mm_id_mapcount[1] = -1; 771 } 772 if (IS_ENABLED(CONFIG_64BIT) || order > 1) { 773 atomic_set(&folio->_pincount, 0); 774 atomic_set(&folio->_entire_mapcount, -1); 775 } 776 if (order > 1) 777 INIT_LIST_HEAD(&folio->_deferred_list); 778 } 779 780 static inline void prep_compound_tail(struct page *head, int tail_idx) 781 { 782 struct page *p = head + tail_idx; 783 784 p->mapping = TAIL_MAPPING; 785 set_compound_head(p, head); 786 set_page_private(p, 0); 787 } 788 789 void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags); 790 extern bool free_pages_prepare(struct page *page, unsigned int order); 791 792 extern int user_min_free_kbytes; 793 794 struct page *__alloc_frozen_pages_noprof(gfp_t, unsigned int order, int nid, 795 nodemask_t *); 796 #define __alloc_frozen_pages(...) \ 797 alloc_hooks(__alloc_frozen_pages_noprof(__VA_ARGS__)) 798 void free_frozen_pages(struct page *page, unsigned int order); 799 void free_unref_folios(struct folio_batch *fbatch); 800 801 #ifdef CONFIG_NUMA 802 struct page *alloc_frozen_pages_noprof(gfp_t, unsigned int order); 803 #else 804 static inline struct page *alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order) 805 { 806 return __alloc_frozen_pages_noprof(gfp, order, numa_node_id(), NULL); 807 } 808 #endif 809 810 #define alloc_frozen_pages(...) \ 811 alloc_hooks(alloc_frozen_pages_noprof(__VA_ARGS__)) 812 813 extern void zone_pcp_reset(struct zone *zone); 814 extern void zone_pcp_disable(struct zone *zone); 815 extern void zone_pcp_enable(struct zone *zone); 816 extern void zone_pcp_init(struct zone *zone); 817 818 extern void *memmap_alloc(phys_addr_t size, phys_addr_t align, 819 phys_addr_t min_addr, 820 int nid, bool exact_nid); 821 822 void memmap_init_range(unsigned long, int, unsigned long, unsigned long, 823 unsigned long, enum meminit_context, struct vmem_altmap *, int); 824 825 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 826 827 /* 828 * in mm/compaction.c 829 */ 830 /* 831 * compact_control is used to track pages being migrated and the free pages 832 * they are being migrated to during memory compaction. The free_pfn starts 833 * at the end of a zone and migrate_pfn begins at the start. Movable pages 834 * are moved to the end of a zone during a compaction run and the run 835 * completes when free_pfn <= migrate_pfn 836 */ 837 struct compact_control { 838 struct list_head freepages[NR_PAGE_ORDERS]; /* List of free pages to migrate to */ 839 struct list_head migratepages; /* List of pages being migrated */ 840 unsigned int nr_freepages; /* Number of isolated free pages */ 841 unsigned int nr_migratepages; /* Number of pages to migrate */ 842 unsigned long free_pfn; /* isolate_freepages search base */ 843 /* 844 * Acts as an in/out parameter to page isolation for migration. 845 * isolate_migratepages uses it as a search base. 846 * isolate_migratepages_block will update the value to the next pfn 847 * after the last isolated one. 848 */ 849 unsigned long migrate_pfn; 850 unsigned long fast_start_pfn; /* a pfn to start linear scan from */ 851 struct zone *zone; 852 unsigned long total_migrate_scanned; 853 unsigned long total_free_scanned; 854 unsigned short fast_search_fail;/* failures to use free list searches */ 855 short search_order; /* order to start a fast search at */ 856 const gfp_t gfp_mask; /* gfp mask of a direct compactor */ 857 int order; /* order a direct compactor needs */ 858 int migratetype; /* migratetype of direct compactor */ 859 const unsigned int alloc_flags; /* alloc flags of a direct compactor */ 860 const int highest_zoneidx; /* zone index of a direct compactor */ 861 enum migrate_mode mode; /* Async or sync migration mode */ 862 bool ignore_skip_hint; /* Scan blocks even if marked skip */ 863 bool no_set_skip_hint; /* Don't mark blocks for skipping */ 864 bool ignore_block_suitable; /* Scan blocks considered unsuitable */ 865 bool direct_compaction; /* False from kcompactd or /proc/... */ 866 bool proactive_compaction; /* kcompactd proactive compaction */ 867 bool whole_zone; /* Whole zone should/has been scanned */ 868 bool contended; /* Signal lock contention */ 869 bool finish_pageblock; /* Scan the remainder of a pageblock. Used 870 * when there are potentially transient 871 * isolation or migration failures to 872 * ensure forward progress. 873 */ 874 bool alloc_contig; /* alloc_contig_range allocation */ 875 }; 876 877 /* 878 * Used in direct compaction when a page should be taken from the freelists 879 * immediately when one is created during the free path. 880 */ 881 struct capture_control { 882 struct compact_control *cc; 883 struct page *page; 884 }; 885 886 unsigned long 887 isolate_freepages_range(struct compact_control *cc, 888 unsigned long start_pfn, unsigned long end_pfn); 889 int 890 isolate_migratepages_range(struct compact_control *cc, 891 unsigned long low_pfn, unsigned long end_pfn); 892 893 /* Free whole pageblock and set its migration type to MIGRATE_CMA. */ 894 void init_cma_reserved_pageblock(struct page *page); 895 896 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 897 898 struct cma; 899 900 #ifdef CONFIG_CMA 901 void *cma_reserve_early(struct cma *cma, unsigned long size); 902 void init_cma_pageblock(struct page *page); 903 #else 904 static inline void *cma_reserve_early(struct cma *cma, unsigned long size) 905 { 906 return NULL; 907 } 908 static inline void init_cma_pageblock(struct page *page) 909 { 910 } 911 #endif 912 913 914 int find_suitable_fallback(struct free_area *area, unsigned int order, 915 int migratetype, bool claimable); 916 917 static inline bool free_area_empty(struct free_area *area, int migratetype) 918 { 919 return list_empty(&area->free_list[migratetype]); 920 } 921 922 /* mm/util.c */ 923 struct anon_vma *folio_anon_vma(const struct folio *folio); 924 925 #ifdef CONFIG_MMU 926 void unmap_mapping_folio(struct folio *folio); 927 extern long populate_vma_page_range(struct vm_area_struct *vma, 928 unsigned long start, unsigned long end, int *locked); 929 extern long faultin_page_range(struct mm_struct *mm, unsigned long start, 930 unsigned long end, bool write, int *locked); 931 extern bool mlock_future_ok(struct mm_struct *mm, unsigned long flags, 932 unsigned long bytes); 933 934 /* 935 * NOTE: This function can't tell whether the folio is "fully mapped" in the 936 * range. 937 * "fully mapped" means all the pages of folio is associated with the page 938 * table of range while this function just check whether the folio range is 939 * within the range [start, end). Function caller needs to do page table 940 * check if it cares about the page table association. 941 * 942 * Typical usage (like mlock or madvise) is: 943 * Caller knows at least 1 page of folio is associated with page table of VMA 944 * and the range [start, end) is intersect with the VMA range. Caller wants 945 * to know whether the folio is fully associated with the range. It calls 946 * this function to check whether the folio is in the range first. Then checks 947 * the page table to know whether the folio is fully mapped to the range. 948 */ 949 static inline bool 950 folio_within_range(struct folio *folio, struct vm_area_struct *vma, 951 unsigned long start, unsigned long end) 952 { 953 pgoff_t pgoff, addr; 954 unsigned long vma_pglen = vma_pages(vma); 955 956 VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio); 957 if (start > end) 958 return false; 959 960 if (start < vma->vm_start) 961 start = vma->vm_start; 962 963 if (end > vma->vm_end) 964 end = vma->vm_end; 965 966 pgoff = folio_pgoff(folio); 967 968 /* if folio start address is not in vma range */ 969 if (!in_range(pgoff, vma->vm_pgoff, vma_pglen)) 970 return false; 971 972 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 973 974 return !(addr < start || end - addr < folio_size(folio)); 975 } 976 977 static inline bool 978 folio_within_vma(struct folio *folio, struct vm_area_struct *vma) 979 { 980 return folio_within_range(folio, vma, vma->vm_start, vma->vm_end); 981 } 982 983 /* 984 * mlock_vma_folio() and munlock_vma_folio(): 985 * should be called with vma's mmap_lock held for read or write, 986 * under page table lock for the pte/pmd being added or removed. 987 * 988 * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at 989 * the end of folio_remove_rmap_*(); but new anon folios are managed by 990 * folio_add_lru_vma() calling mlock_new_folio(). 991 */ 992 void mlock_folio(struct folio *folio); 993 static inline void mlock_vma_folio(struct folio *folio, 994 struct vm_area_struct *vma) 995 { 996 /* 997 * The VM_SPECIAL check here serves two purposes. 998 * 1) VM_IO check prevents migration from double-counting during mlock. 999 * 2) Although mmap_region() and mlock_fixup() take care that VM_LOCKED 1000 * is never left set on a VM_SPECIAL vma, there is an interval while 1001 * file->f_op->mmap() is using vm_insert_page(s), when VM_LOCKED may 1002 * still be set while VM_SPECIAL bits are added: so ignore it then. 1003 */ 1004 if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED)) 1005 mlock_folio(folio); 1006 } 1007 1008 void munlock_folio(struct folio *folio); 1009 static inline void munlock_vma_folio(struct folio *folio, 1010 struct vm_area_struct *vma) 1011 { 1012 /* 1013 * munlock if the function is called. Ideally, we should only 1014 * do munlock if any page of folio is unmapped from VMA and 1015 * cause folio not fully mapped to VMA. 1016 * 1017 * But it's not easy to confirm that's the situation. So we 1018 * always munlock the folio and page reclaim will correct it 1019 * if it's wrong. 1020 */ 1021 if (unlikely(vma->vm_flags & VM_LOCKED)) 1022 munlock_folio(folio); 1023 } 1024 1025 void mlock_new_folio(struct folio *folio); 1026 bool need_mlock_drain(int cpu); 1027 void mlock_drain_local(void); 1028 void mlock_drain_remote(int cpu); 1029 1030 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); 1031 1032 /** 1033 * vma_address - Find the virtual address a page range is mapped at 1034 * @vma: The vma which maps this object. 1035 * @pgoff: The page offset within its object. 1036 * @nr_pages: The number of pages to consider. 1037 * 1038 * If any page in this range is mapped by this VMA, return the first address 1039 * where any of these pages appear. Otherwise, return -EFAULT. 1040 */ 1041 static inline unsigned long vma_address(const struct vm_area_struct *vma, 1042 pgoff_t pgoff, unsigned long nr_pages) 1043 { 1044 unsigned long address; 1045 1046 if (pgoff >= vma->vm_pgoff) { 1047 address = vma->vm_start + 1048 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1049 /* Check for address beyond vma (or wrapped through 0?) */ 1050 if (address < vma->vm_start || address >= vma->vm_end) 1051 address = -EFAULT; 1052 } else if (pgoff + nr_pages - 1 >= vma->vm_pgoff) { 1053 /* Test above avoids possibility of wrap to 0 on 32-bit */ 1054 address = vma->vm_start; 1055 } else { 1056 address = -EFAULT; 1057 } 1058 return address; 1059 } 1060 1061 /* 1062 * Then at what user virtual address will none of the range be found in vma? 1063 * Assumes that vma_address() already returned a good starting address. 1064 */ 1065 static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw) 1066 { 1067 struct vm_area_struct *vma = pvmw->vma; 1068 pgoff_t pgoff; 1069 unsigned long address; 1070 1071 /* Common case, plus ->pgoff is invalid for KSM */ 1072 if (pvmw->nr_pages == 1) 1073 return pvmw->address + PAGE_SIZE; 1074 1075 pgoff = pvmw->pgoff + pvmw->nr_pages; 1076 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1077 /* Check for address beyond vma (or wrapped through 0?) */ 1078 if (address < vma->vm_start || address > vma->vm_end) 1079 address = vma->vm_end; 1080 return address; 1081 } 1082 1083 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, 1084 struct file *fpin) 1085 { 1086 int flags = vmf->flags; 1087 1088 if (fpin) 1089 return fpin; 1090 1091 /* 1092 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or 1093 * anything, so we only pin the file and drop the mmap_lock if only 1094 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. 1095 */ 1096 if (fault_flag_allow_retry_first(flags) && 1097 !(flags & FAULT_FLAG_RETRY_NOWAIT)) { 1098 fpin = get_file(vmf->vma->vm_file); 1099 release_fault_lock(vmf); 1100 } 1101 return fpin; 1102 } 1103 #else /* !CONFIG_MMU */ 1104 static inline void unmap_mapping_folio(struct folio *folio) { } 1105 static inline void mlock_new_folio(struct folio *folio) { } 1106 static inline bool need_mlock_drain(int cpu) { return false; } 1107 static inline void mlock_drain_local(void) { } 1108 static inline void mlock_drain_remote(int cpu) { } 1109 static inline void vunmap_range_noflush(unsigned long start, unsigned long end) 1110 { 1111 } 1112 #endif /* !CONFIG_MMU */ 1113 1114 /* Memory initialisation debug and verification */ 1115 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1116 DECLARE_STATIC_KEY_TRUE(deferred_pages); 1117 1118 bool __init deferred_grow_zone(struct zone *zone, unsigned int order); 1119 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 1120 1121 void init_deferred_page(unsigned long pfn, int nid); 1122 1123 enum mminit_level { 1124 MMINIT_WARNING, 1125 MMINIT_VERIFY, 1126 MMINIT_TRACE 1127 }; 1128 1129 #ifdef CONFIG_DEBUG_MEMORY_INIT 1130 1131 extern int mminit_loglevel; 1132 1133 #define mminit_dprintk(level, prefix, fmt, arg...) \ 1134 do { \ 1135 if (level < mminit_loglevel) { \ 1136 if (level <= MMINIT_WARNING) \ 1137 pr_warn("mminit::" prefix " " fmt, ##arg); \ 1138 else \ 1139 printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \ 1140 } \ 1141 } while (0) 1142 1143 extern void mminit_verify_pageflags_layout(void); 1144 extern void mminit_verify_zonelist(void); 1145 #else 1146 1147 static inline void mminit_dprintk(enum mminit_level level, 1148 const char *prefix, const char *fmt, ...) 1149 { 1150 } 1151 1152 static inline void mminit_verify_pageflags_layout(void) 1153 { 1154 } 1155 1156 static inline void mminit_verify_zonelist(void) 1157 { 1158 } 1159 #endif /* CONFIG_DEBUG_MEMORY_INIT */ 1160 1161 #define NODE_RECLAIM_NOSCAN -2 1162 #define NODE_RECLAIM_FULL -1 1163 #define NODE_RECLAIM_SOME 0 1164 #define NODE_RECLAIM_SUCCESS 1 1165 1166 #ifdef CONFIG_NUMA 1167 extern int node_reclaim_mode; 1168 1169 extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int); 1170 extern int find_next_best_node(int node, nodemask_t *used_node_mask); 1171 #else 1172 #define node_reclaim_mode 0 1173 1174 static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask, 1175 unsigned int order) 1176 { 1177 return NODE_RECLAIM_NOSCAN; 1178 } 1179 static inline int find_next_best_node(int node, nodemask_t *used_node_mask) 1180 { 1181 return NUMA_NO_NODE; 1182 } 1183 #endif 1184 1185 static inline bool node_reclaim_enabled(void) 1186 { 1187 /* Is any node_reclaim_mode bit set? */ 1188 return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP); 1189 } 1190 1191 /* 1192 * mm/memory-failure.c 1193 */ 1194 #ifdef CONFIG_MEMORY_FAILURE 1195 int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill); 1196 void shake_folio(struct folio *folio); 1197 extern int hwpoison_filter(struct page *p); 1198 1199 extern u32 hwpoison_filter_dev_major; 1200 extern u32 hwpoison_filter_dev_minor; 1201 extern u64 hwpoison_filter_flags_mask; 1202 extern u64 hwpoison_filter_flags_value; 1203 extern u64 hwpoison_filter_memcg; 1204 extern u32 hwpoison_filter_enable; 1205 #define MAGIC_HWPOISON 0x48575053U /* HWPS */ 1206 void SetPageHWPoisonTakenOff(struct page *page); 1207 void ClearPageHWPoisonTakenOff(struct page *page); 1208 bool take_page_off_buddy(struct page *page); 1209 bool put_page_back_buddy(struct page *page); 1210 struct task_struct *task_early_kill(struct task_struct *tsk, int force_early); 1211 void add_to_kill_ksm(struct task_struct *tsk, const struct page *p, 1212 struct vm_area_struct *vma, struct list_head *to_kill, 1213 unsigned long ksm_addr); 1214 unsigned long page_mapped_in_vma(const struct page *page, 1215 struct vm_area_struct *vma); 1216 1217 #else 1218 static inline int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill) 1219 { 1220 return -EBUSY; 1221 } 1222 #endif 1223 1224 extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long, 1225 unsigned long, unsigned long, 1226 unsigned long, unsigned long); 1227 1228 extern void set_pageblock_order(void); 1229 struct folio *alloc_migrate_folio(struct folio *src, unsigned long private); 1230 unsigned long reclaim_pages(struct list_head *folio_list); 1231 unsigned int reclaim_clean_pages_from_list(struct zone *zone, 1232 struct list_head *folio_list); 1233 /* The ALLOC_WMARK bits are used as an index to zone->watermark */ 1234 #define ALLOC_WMARK_MIN WMARK_MIN 1235 #define ALLOC_WMARK_LOW WMARK_LOW 1236 #define ALLOC_WMARK_HIGH WMARK_HIGH 1237 #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */ 1238 1239 /* Mask to get the watermark bits */ 1240 #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1) 1241 1242 /* 1243 * Only MMU archs have async oom victim reclaim - aka oom_reaper so we 1244 * cannot assume a reduced access to memory reserves is sufficient for 1245 * !MMU 1246 */ 1247 #ifdef CONFIG_MMU 1248 #define ALLOC_OOM 0x08 1249 #else 1250 #define ALLOC_OOM ALLOC_NO_WATERMARKS 1251 #endif 1252 1253 #define ALLOC_NON_BLOCK 0x10 /* Caller cannot block. Allow access 1254 * to 25% of the min watermark or 1255 * 62.5% if __GFP_HIGH is set. 1256 */ 1257 #define ALLOC_MIN_RESERVE 0x20 /* __GFP_HIGH set. Allow access to 50% 1258 * of the min watermark. 1259 */ 1260 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */ 1261 #define ALLOC_CMA 0x80 /* allow allocations from CMA areas */ 1262 #ifdef CONFIG_ZONE_DMA32 1263 #define ALLOC_NOFRAGMENT 0x100 /* avoid mixing pageblock types */ 1264 #else 1265 #define ALLOC_NOFRAGMENT 0x0 1266 #endif 1267 #define ALLOC_HIGHATOMIC 0x200 /* Allows access to MIGRATE_HIGHATOMIC */ 1268 #define ALLOC_TRYLOCK 0x400 /* Only use spin_trylock in allocation path */ 1269 #define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */ 1270 1271 /* Flags that allow allocations below the min watermark. */ 1272 #define ALLOC_RESERVES (ALLOC_NON_BLOCK|ALLOC_MIN_RESERVE|ALLOC_HIGHATOMIC|ALLOC_OOM) 1273 1274 enum ttu_flags; 1275 struct tlbflush_unmap_batch; 1276 1277 1278 /* 1279 * only for MM internal work items which do not depend on 1280 * any allocations or locks which might depend on allocations 1281 */ 1282 extern struct workqueue_struct *mm_percpu_wq; 1283 1284 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1285 void try_to_unmap_flush(void); 1286 void try_to_unmap_flush_dirty(void); 1287 void flush_tlb_batched_pending(struct mm_struct *mm); 1288 #else 1289 static inline void try_to_unmap_flush(void) 1290 { 1291 } 1292 static inline void try_to_unmap_flush_dirty(void) 1293 { 1294 } 1295 static inline void flush_tlb_batched_pending(struct mm_struct *mm) 1296 { 1297 } 1298 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ 1299 1300 extern const struct trace_print_flags pageflag_names[]; 1301 extern const struct trace_print_flags vmaflag_names[]; 1302 extern const struct trace_print_flags gfpflag_names[]; 1303 1304 static inline bool is_migrate_highatomic(enum migratetype migratetype) 1305 { 1306 return migratetype == MIGRATE_HIGHATOMIC; 1307 } 1308 1309 void setup_zone_pageset(struct zone *zone); 1310 1311 struct migration_target_control { 1312 int nid; /* preferred node id */ 1313 nodemask_t *nmask; 1314 gfp_t gfp_mask; 1315 enum migrate_reason reason; 1316 }; 1317 1318 /* 1319 * mm/filemap.c 1320 */ 1321 size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, 1322 struct folio *folio, loff_t fpos, size_t size); 1323 1324 /* 1325 * mm/vmalloc.c 1326 */ 1327 #ifdef CONFIG_MMU 1328 void __init vmalloc_init(void); 1329 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 1330 pgprot_t prot, struct page **pages, unsigned int page_shift); 1331 unsigned int get_vm_area_page_order(struct vm_struct *vm); 1332 #else 1333 static inline void vmalloc_init(void) 1334 { 1335 } 1336 1337 static inline 1338 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 1339 pgprot_t prot, struct page **pages, unsigned int page_shift) 1340 { 1341 return -EINVAL; 1342 } 1343 #endif 1344 1345 int __must_check __vmap_pages_range_noflush(unsigned long addr, 1346 unsigned long end, pgprot_t prot, 1347 struct page **pages, unsigned int page_shift); 1348 1349 void vunmap_range_noflush(unsigned long start, unsigned long end); 1350 1351 void __vunmap_range_noflush(unsigned long start, unsigned long end); 1352 1353 int numa_migrate_check(struct folio *folio, struct vm_fault *vmf, 1354 unsigned long addr, int *flags, bool writable, 1355 int *last_cpupid); 1356 1357 void free_zone_device_folio(struct folio *folio); 1358 int migrate_device_coherent_folio(struct folio *folio); 1359 1360 struct vm_struct *__get_vm_area_node(unsigned long size, 1361 unsigned long align, unsigned long shift, 1362 unsigned long flags, unsigned long start, 1363 unsigned long end, int node, gfp_t gfp_mask, 1364 const void *caller); 1365 1366 /* 1367 * mm/gup.c 1368 */ 1369 int __must_check try_grab_folio(struct folio *folio, int refs, 1370 unsigned int flags); 1371 1372 /* 1373 * mm/huge_memory.c 1374 */ 1375 void touch_pud(struct vm_area_struct *vma, unsigned long addr, 1376 pud_t *pud, bool write); 1377 void touch_pmd(struct vm_area_struct *vma, unsigned long addr, 1378 pmd_t *pmd, bool write); 1379 1380 /* 1381 * Parses a string with mem suffixes into its order. Useful to parse kernel 1382 * parameters. 1383 */ 1384 static inline int get_order_from_str(const char *size_str, 1385 unsigned long valid_orders) 1386 { 1387 unsigned long size; 1388 char *endptr; 1389 int order; 1390 1391 size = memparse(size_str, &endptr); 1392 1393 if (!is_power_of_2(size)) 1394 return -EINVAL; 1395 order = get_order(size); 1396 if (BIT(order) & ~valid_orders) 1397 return -EINVAL; 1398 1399 return order; 1400 } 1401 1402 enum { 1403 /* mark page accessed */ 1404 FOLL_TOUCH = 1 << 16, 1405 /* a retry, previous pass started an IO */ 1406 FOLL_TRIED = 1 << 17, 1407 /* we are working on non-current tsk/mm */ 1408 FOLL_REMOTE = 1 << 18, 1409 /* pages must be released via unpin_user_page */ 1410 FOLL_PIN = 1 << 19, 1411 /* gup_fast: prevent fall-back to slow gup */ 1412 FOLL_FAST_ONLY = 1 << 20, 1413 /* allow unlocking the mmap lock */ 1414 FOLL_UNLOCKABLE = 1 << 21, 1415 /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ 1416 FOLL_MADV_POPULATE = 1 << 22, 1417 }; 1418 1419 #define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ 1420 FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ 1421 FOLL_MADV_POPULATE) 1422 1423 /* 1424 * Indicates for which pages that are write-protected in the page table, 1425 * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the 1426 * GUP pin will remain consistent with the pages mapped into the page tables 1427 * of the MM. 1428 * 1429 * Temporary unmapping of PageAnonExclusive() pages or clearing of 1430 * PageAnonExclusive() has to protect against concurrent GUP: 1431 * * Ordinary GUP: Using the PT lock 1432 * * GUP-fast and fork(): mm->write_protect_seq 1433 * * GUP-fast and KSM or temporary unmapping (swap, migration): see 1434 * folio_try_share_anon_rmap_*() 1435 * 1436 * Must be called with the (sub)page that's actually referenced via the 1437 * page table entry, which might not necessarily be the head page for a 1438 * PTE-mapped THP. 1439 * 1440 * If the vma is NULL, we're coming from the GUP-fast path and might have 1441 * to fallback to the slow path just to lookup the vma. 1442 */ 1443 static inline bool gup_must_unshare(struct vm_area_struct *vma, 1444 unsigned int flags, struct page *page) 1445 { 1446 /* 1447 * FOLL_WRITE is implicitly handled correctly as the page table entry 1448 * has to be writable -- and if it references (part of) an anonymous 1449 * folio, that part is required to be marked exclusive. 1450 */ 1451 if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN) 1452 return false; 1453 /* 1454 * Note: PageAnon(page) is stable until the page is actually getting 1455 * freed. 1456 */ 1457 if (!PageAnon(page)) { 1458 /* 1459 * We only care about R/O long-term pining: R/O short-term 1460 * pinning does not have the semantics to observe successive 1461 * changes through the process page tables. 1462 */ 1463 if (!(flags & FOLL_LONGTERM)) 1464 return false; 1465 1466 /* We really need the vma ... */ 1467 if (!vma) 1468 return true; 1469 1470 /* 1471 * ... because we only care about writable private ("COW") 1472 * mappings where we have to break COW early. 1473 */ 1474 return is_cow_mapping(vma->vm_flags); 1475 } 1476 1477 /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */ 1478 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) 1479 smp_rmb(); 1480 1481 /* 1482 * Note that KSM pages cannot be exclusive, and consequently, 1483 * cannot get pinned. 1484 */ 1485 return !PageAnonExclusive(page); 1486 } 1487 1488 extern bool mirrored_kernelcore; 1489 bool memblock_has_mirror(void); 1490 void memblock_free_all(void); 1491 1492 static __always_inline void vma_set_range(struct vm_area_struct *vma, 1493 unsigned long start, unsigned long end, 1494 pgoff_t pgoff) 1495 { 1496 vma->vm_start = start; 1497 vma->vm_end = end; 1498 vma->vm_pgoff = pgoff; 1499 } 1500 1501 static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) 1502 { 1503 /* 1504 * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty 1505 * enablements, because when without soft-dirty being compiled in, 1506 * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) 1507 * will be constantly true. 1508 */ 1509 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 1510 return false; 1511 1512 /* 1513 * Soft-dirty is kind of special: its tracking is enabled when the 1514 * vma flags not set. 1515 */ 1516 return !(vma->vm_flags & VM_SOFTDIRTY); 1517 } 1518 1519 static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd) 1520 { 1521 return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd); 1522 } 1523 1524 static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte) 1525 { 1526 return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte); 1527 } 1528 1529 void __meminit __init_single_page(struct page *page, unsigned long pfn, 1530 unsigned long zone, int nid); 1531 void __meminit __init_page_from_nid(unsigned long pfn, int nid); 1532 1533 /* shrinker related functions */ 1534 unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg, 1535 int priority); 1536 1537 #ifdef CONFIG_SHRINKER_DEBUG 1538 static inline __printf(2, 0) int shrinker_debugfs_name_alloc( 1539 struct shrinker *shrinker, const char *fmt, va_list ap) 1540 { 1541 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap); 1542 1543 return shrinker->name ? 0 : -ENOMEM; 1544 } 1545 1546 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1547 { 1548 kfree_const(shrinker->name); 1549 shrinker->name = NULL; 1550 } 1551 1552 extern int shrinker_debugfs_add(struct shrinker *shrinker); 1553 extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1554 int *debugfs_id); 1555 extern void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1556 int debugfs_id); 1557 #else /* CONFIG_SHRINKER_DEBUG */ 1558 static inline int shrinker_debugfs_add(struct shrinker *shrinker) 1559 { 1560 return 0; 1561 } 1562 static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker, 1563 const char *fmt, va_list ap) 1564 { 1565 return 0; 1566 } 1567 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1568 { 1569 } 1570 static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1571 int *debugfs_id) 1572 { 1573 *debugfs_id = -1; 1574 return NULL; 1575 } 1576 static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1577 int debugfs_id) 1578 { 1579 } 1580 #endif /* CONFIG_SHRINKER_DEBUG */ 1581 1582 /* Only track the nodes of mappings with shadow entries */ 1583 void workingset_update_node(struct xa_node *node); 1584 extern struct list_lru shadow_nodes; 1585 #define mapping_set_update(xas, mapping) do { \ 1586 if (!dax_mapping(mapping) && !shmem_mapping(mapping)) { \ 1587 xas_set_update(xas, workingset_update_node); \ 1588 xas_set_lru(xas, &shadow_nodes); \ 1589 } \ 1590 } while (0) 1591 1592 /* mremap.c */ 1593 unsigned long move_page_tables(struct pagetable_move_control *pmc); 1594 1595 #ifdef CONFIG_UNACCEPTED_MEMORY 1596 void accept_page(struct page *page); 1597 #else /* CONFIG_UNACCEPTED_MEMORY */ 1598 static inline void accept_page(struct page *page) 1599 { 1600 } 1601 #endif /* CONFIG_UNACCEPTED_MEMORY */ 1602 1603 /* pagewalk.c */ 1604 int walk_page_range_mm(struct mm_struct *mm, unsigned long start, 1605 unsigned long end, const struct mm_walk_ops *ops, 1606 void *private); 1607 int walk_page_range_debug(struct mm_struct *mm, unsigned long start, 1608 unsigned long end, const struct mm_walk_ops *ops, 1609 pgd_t *pgd, void *private); 1610 1611 /* pt_reclaim.c */ 1612 bool try_get_and_clear_pmd(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdval); 1613 void free_pte(struct mm_struct *mm, unsigned long addr, struct mmu_gather *tlb, 1614 pmd_t pmdval); 1615 void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr, 1616 struct mmu_gather *tlb); 1617 1618 #ifdef CONFIG_PT_RECLAIM 1619 bool reclaim_pt_is_enabled(unsigned long start, unsigned long end, 1620 struct zap_details *details); 1621 #else 1622 static inline bool reclaim_pt_is_enabled(unsigned long start, unsigned long end, 1623 struct zap_details *details) 1624 { 1625 return false; 1626 } 1627 #endif /* CONFIG_PT_RECLAIM */ 1628 1629 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm); 1630 int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm); 1631 1632 #endif /* __MM_INTERNAL_H */ 1633