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