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/file.h> 11 #include <linux/fs.h> 12 #include <linux/khugepaged.h> 13 #include <linux/mm.h> 14 #include <linux/mm_inline.h> 15 #include <linux/mmu_notifier.h> 16 #include <linux/pagemap.h> 17 #include <linux/pagewalk.h> 18 #include <linux/rmap.h> 19 #include <linux/swap.h> 20 #include <linux/leafops.h> 21 #include <linux/tracepoint-defs.h> 22 23 /* Internal core VMA manipulation functions. */ 24 #include "vma.h" 25 26 struct folio_batch; 27 28 int __copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, 29 void *buf, int len, unsigned int gup_flags); 30 31 /* mm/workingset.c */ 32 bool workingset_test_recent(void *shadow, bool file, bool *workingset, 33 bool flush); 34 void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages); 35 void *workingset_eviction(struct folio *folio, 36 struct mem_cgroup *target_memcg); 37 void workingset_refault(struct folio *folio, void *shadow); 38 void workingset_activation(struct folio *folio); 39 40 /* mm/folio.c */ 41 void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma); 42 43 static inline bool folio_may_be_lru_cached(const struct folio *folio) 44 { 45 /* 46 * Holding PMD-sized folios in per-CPU LRU cache unbalances accounting. 47 * Holding small numbers of low-order mTHP folios in per-CPU LRU cache 48 * will be sensible, but nobody has implemented and tested that yet. 49 */ 50 return !folio_test_large(folio); 51 } 52 53 static inline void lru_cache_enable(void) 54 { 55 atomic_dec(&lru_disable_count); 56 } 57 58 void lru_cache_disable(void); 59 void lru_add_drain(void); 60 void lru_add_drain_cpu(int cpu); 61 void lru_add_drain_cpu_zone(struct zone *zone); 62 void folio_deactivate(struct folio *folio); 63 void folio_mark_lazyfree(struct folio *folio); 64 65 /* mm/vmscan.c */ 66 unsigned long zone_reclaimable_pages(struct zone *zone); 67 unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 68 gfp_t gfp_mask, const nodemask_t *mask); 69 unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, 70 int zone_idx); 71 72 #define MEMCG_RECLAIM_MAY_SWAP (1 << 1) 73 #define MEMCG_RECLAIM_PROACTIVE (1 << 2) 74 enum { 75 MIN_SWAPPINESS = 0, 76 MAX_SWAPPINESS = 200, 77 78 /* Just reclaim from anon folios in proactive memory reclaim */ 79 SWAPPINESS_ANON_ONLY = MAX_SWAPPINESS + 1, 80 }; 81 82 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 83 unsigned long nr_pages, 84 gfp_t gfp_mask, 85 unsigned int reclaim_options, 86 int *swappiness); 87 88 #ifdef CONFIG_NUMA 89 extern int sysctl_min_unmapped_ratio; 90 extern int sysctl_min_slab_ratio; 91 #endif 92 93 /* 94 * Maintains state across a page table move. The operation assumes both source 95 * and destination VMAs already exist and are specified by the user. 96 * 97 * Partial moves are permitted, but the old and new ranges must both reside 98 * within a VMA. 99 * 100 * mmap lock must be held in write and VMA write locks must be held on any VMA 101 * that is visible. 102 * 103 * Use the PAGETABLE_MOVE() macro to initialise this struct. 104 * 105 * The old_addr and new_addr fields are updated as the page table move is 106 * executed. 107 * 108 * NOTE: The page table move is affected by reading from [old_addr, old_end), 109 * and old_addr may be updated for better page table alignment, so len_in 110 * represents the length of the range being copied as specified by the user. 111 */ 112 struct pagetable_move_control { 113 struct vm_area_struct *old; /* Source VMA. */ 114 struct vm_area_struct *new; /* Destination VMA. */ 115 unsigned long old_addr; /* Address from which the move begins. */ 116 unsigned long old_end; /* Exclusive address at which old range ends. */ 117 unsigned long new_addr; /* Address to move page tables to. */ 118 unsigned long len_in; /* Bytes to remap specified by user. */ 119 120 bool need_rmap_locks; /* Do rmap locks need to be taken? */ 121 bool for_stack; /* Is this an early temp stack being moved? */ 122 }; 123 124 #define PAGETABLE_MOVE(name, old_, new_, old_addr_, new_addr_, len_) \ 125 struct pagetable_move_control name = { \ 126 .old = old_, \ 127 .new = new_, \ 128 .old_addr = old_addr_, \ 129 .old_end = (old_addr_) + (len_), \ 130 .new_addr = new_addr_, \ 131 .len_in = len_, \ 132 } 133 134 /* 135 * The set of flags that only affect watermark checking and reclaim 136 * behaviour. This is used by the MM to obey the caller constraints 137 * about IO, FS and watermark checking while ignoring placement 138 * hints such as HIGHMEM usage. 139 */ 140 #define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\ 141 __GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\ 142 __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\ 143 __GFP_NOLOCKDEP) 144 145 /* The GFP flags allowed during early boot */ 146 #define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS)) 147 148 /* Control allocation cpuset and node placement constraints */ 149 #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE) 150 151 /* Do not use these with a slab allocator */ 152 #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK) 153 154 /* 155 * Different from WARN_ON_ONCE(), no warning will be issued 156 * when we specify __GFP_NOWARN. 157 */ 158 #define WARN_ON_ONCE_GFP(cond, gfp) ({ \ 159 static bool __section(".data..once") __warned; \ 160 int __ret_warn_once = !!(cond); \ 161 \ 162 if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \ 163 __warned = true; \ 164 WARN_ON(1); \ 165 } \ 166 unlikely(__ret_warn_once); \ 167 }) 168 169 void page_writeback_init(void); 170 171 /* 172 * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages, 173 * its nr_pages_mapped would be 0x400000: choose the ENTIRELY_MAPPED bit 174 * above that range, instead of 2*(PMD_SIZE/PAGE_SIZE). Hugetlb currently 175 * leaves nr_pages_mapped at 0, but avoid surprise if it participates later. 176 */ 177 #define ENTIRELY_MAPPED 0x800000 178 #define FOLIO_PAGES_MAPPED (ENTIRELY_MAPPED - 1) 179 180 /* 181 * Flags passed to __show_mem() and show_free_areas() to suppress output in 182 * various contexts. 183 */ 184 #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */ 185 186 /* 187 * How many individual pages have an elevated _mapcount. Excludes 188 * the folio's entire_mapcount. 189 * 190 * Don't use this function outside of debugging code. 191 */ 192 static inline int folio_nr_pages_mapped(const struct folio *folio) 193 { 194 if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) 195 return -1; 196 return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED; 197 } 198 199 /* 200 * Retrieve the first entry of a folio based on a provided entry within the 201 * folio. We cannot rely on folio->swap as there is no guarantee that it has 202 * been initialized. Used for calling arch_swap_restore() 203 */ 204 static inline swp_entry_t folio_swap(swp_entry_t entry, 205 const struct folio *folio) 206 { 207 swp_entry_t swap = { 208 .val = ALIGN_DOWN(entry.val, folio_nr_pages(folio)), 209 }; 210 211 return swap; 212 } 213 214 static inline void *folio_raw_mapping(const struct folio *folio) 215 { 216 unsigned long mapping = (unsigned long)folio->mapping; 217 218 return (void *)(mapping & ~FOLIO_MAPPING_FLAGS); 219 } 220 221 /* 222 * If the VMA has a close hook then close it, and since closing it might leave 223 * it in an inconsistent state which makes the use of any hooks suspect, clear 224 * them down by installing dummy empty hooks. 225 */ 226 static inline void vma_close(struct vm_area_struct *vma) 227 { 228 if (vma->vm_ops && vma->vm_ops->close) { 229 vma->vm_ops->close(vma); 230 231 /* 232 * The mapping is in an inconsistent state, and no further hooks 233 * may be invoked upon it. 234 */ 235 vma->vm_ops = &vma_dummy_vm_ops; 236 } 237 } 238 239 /* 240 * This is a file-backed mapping, and is about to be memory mapped - invoke its 241 * mmap hook and safely handle error conditions. On error, VMA hooks will be 242 * mutated. 243 * 244 * @file: File which backs the mapping. 245 * @vma: VMA which we are mapping. 246 * 247 * Returns: 0 if success, error otherwise. 248 */ 249 static inline int mmap_file(struct file *file, struct vm_area_struct *vma) 250 { 251 const unsigned long orig_start = vma->vm_start; 252 const unsigned long orig_end = vma->vm_end; 253 const vma_flags_t orig_flags = vma->flags; 254 int err; 255 256 err = vfs_mmap(file, vma); 257 /* 258 * Either we tried to call the file hook for mmap() and an error arose 259 * or a driver set vma->vm_ops = NULL intending there to be no VMA 260 * operations. 261 * 262 * In the former case the VMA is in an inconsistent state and we mustn't 263 * invoke any further hooks on it, in the latter case the hook actually 264 * wanted no further hooks to be invoked, so fix both by setting dummy 265 * VMA ops. 266 */ 267 if (unlikely(err || !vma->vm_ops)) 268 vma->vm_ops = &vma_dummy_vm_ops; 269 if (unlikely(err)) 270 return err; 271 272 err = mmap_hook_validate(orig_start, orig_end, &orig_flags, vma); 273 if (unlikely(err)) { 274 vma->vm_start = orig_start; 275 vma->vm_end = orig_end; 276 } 277 278 return err; 279 } 280 281 /* unmap_vmas is in mm/memory.c */ 282 void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap); 283 284 #ifdef CONFIG_MMU 285 286 bool cond_install_uffd_wp_ptes(struct vm_area_struct *vma, 287 unsigned long addr, pte_t *ptep, pte_t pte, 288 unsigned long nr_ptes); 289 290 static inline void get_anon_vma(struct anon_vma *anon_vma) 291 { 292 atomic_inc(&anon_vma->refcount); 293 } 294 295 void __put_anon_vma(struct anon_vma *anon_vma); 296 297 static inline void put_anon_vma(struct anon_vma *anon_vma) 298 { 299 if (atomic_dec_and_test(&anon_vma->refcount)) 300 __put_anon_vma(anon_vma); 301 } 302 303 static inline void anon_vma_lock_write(struct anon_vma *anon_vma) 304 { 305 down_write(&anon_vma->root->rwsem); 306 } 307 308 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma) 309 { 310 up_write(&anon_vma->root->rwsem); 311 } 312 313 static inline void anon_vma_lock_read(struct anon_vma *anon_vma) 314 { 315 down_read(&anon_vma->root->rwsem); 316 } 317 318 static inline int anon_vma_trylock_read(struct anon_vma *anon_vma) 319 { 320 return down_read_trylock(&anon_vma->root->rwsem); 321 } 322 323 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma) 324 { 325 up_read(&anon_vma->root->rwsem); 326 } 327 328 struct anon_vma *folio_get_anon_vma(const struct folio *folio); 329 330 /* Operations which modify VMAs. */ 331 enum vma_operation { 332 VMA_OP_SPLIT, 333 VMA_OP_MERGE_UNFAULTED, 334 VMA_OP_REMAP, 335 VMA_OP_FORK, 336 }; 337 338 int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src, 339 enum vma_operation operation); 340 int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma); 341 int __anon_vma_prepare(struct vm_area_struct *vma); 342 void unlink_anon_vmas(struct vm_area_struct *vma); 343 344 static inline int anon_vma_prepare(struct vm_area_struct *vma) 345 { 346 if (likely(vma_has_anon_rmap(vma))) 347 return 0; 348 349 return __anon_vma_prepare(vma); 350 } 351 352 /* Flags for folio_pte_batch(). */ 353 typedef int __bitwise fpb_t; 354 355 /* Compare PTEs respecting the dirty bit. */ 356 #define FPB_RESPECT_DIRTY ((__force fpb_t)BIT(0)) 357 358 /* Compare PTEs respecting the soft-dirty bit. */ 359 #define FPB_RESPECT_SOFT_DIRTY ((__force fpb_t)BIT(1)) 360 361 /* Compare PTEs respecting the writable bit. */ 362 #define FPB_RESPECT_WRITE ((__force fpb_t)BIT(2)) 363 364 /* 365 * Merge PTE write bits: if any PTE in the batch is writable, modify the 366 * PTE at @ptentp to be writable. 367 */ 368 #define FPB_MERGE_WRITE ((__force fpb_t)BIT(3)) 369 370 /* 371 * Merge PTE young and dirty bits: if any PTE in the batch is young or dirty, 372 * modify the PTE at @ptentp to be young or dirty, respectively. 373 */ 374 #define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(4)) 375 376 static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) 377 { 378 if (!(flags & FPB_RESPECT_DIRTY)) 379 pte = pte_mkclean(pte); 380 if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY))) 381 pte = pte_clear_soft_dirty(pte); 382 if (likely(!(flags & FPB_RESPECT_WRITE))) 383 pte = pte_wrprotect(pte); 384 return pte_mkold(pte); 385 } 386 387 /** 388 * folio_pte_batch_flags - detect a PTE batch for a large folio 389 * @folio: The large folio to detect a PTE batch for. 390 * @vma: The VMA. Only relevant with FPB_MERGE_WRITE, otherwise can be NULL. 391 * @ptep: Page table pointer for the first entry. 392 * @ptentp: Pointer to a COPY of the first page table entry whose flags this 393 * function updates based on @flags if appropriate. 394 * @max_nr: The maximum number of table entries to consider. 395 * @flags: Flags to modify the PTE batch semantics. 396 * 397 * Detect a PTE batch: consecutive (present) PTEs that map consecutive 398 * pages of the same large folio in a single VMA and a single page table. 399 * 400 * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN, 401 * the accessed bit, writable bit, dirty bit (unless FPB_RESPECT_DIRTY is set) 402 * and soft-dirty bit (unless FPB_RESPECT_SOFT_DIRTY is set). 403 * 404 * @ptep must map any page of the folio. max_nr must be at least one and 405 * must be limited by the caller so scanning cannot exceed a single VMA and 406 * a single page table. 407 * 408 * Depending on the FPB_MERGE_* flags, the pte stored at @ptentp will 409 * be updated: it's crucial that a pointer to a COPY of the first 410 * page table entry, obtained through ptep_get(), is provided as @ptentp. 411 * 412 * This function will be inlined to optimize based on the input parameters; 413 * consider using folio_pte_batch() instead if applicable. 414 * 415 * Return: the number of table entries in the batch. 416 */ 417 static inline unsigned int folio_pte_batch_flags(struct folio *folio, 418 struct vm_area_struct *vma, pte_t *ptep, pte_t *ptentp, 419 unsigned int max_nr, fpb_t flags) 420 { 421 bool any_writable = false, any_young = false, any_dirty = false; 422 pte_t expected_pte, pte = *ptentp; 423 unsigned int nr, cur_nr; 424 425 VM_WARN_ON_FOLIO(!pte_present(pte), folio); 426 VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio); 427 VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio); 428 /* 429 * Ensure this is a pointer to a copy not a pointer into a page table. 430 * If this is a stack value, it won't be a valid virtual address, but 431 * that's fine because it also cannot be pointing into the page table. 432 */ 433 VM_WARN_ON(virt_addr_valid(ptentp) && PageTable(virt_to_page(ptentp))); 434 435 /* Limit max_nr to the actual remaining PFNs in the folio we could batch. */ 436 max_nr = min_t(unsigned long, max_nr, 437 folio_pfn(folio) + folio_nr_pages(folio) - pte_pfn(pte)); 438 439 nr = pte_batch_hint(ptep, pte); 440 expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); 441 ptep = ptep + nr; 442 443 while (nr < max_nr) { 444 pte = ptep_get(ptep); 445 446 if (!pte_same(__pte_batch_clear_ignored(pte, flags), expected_pte)) 447 break; 448 449 if (flags & FPB_MERGE_WRITE) 450 any_writable |= pte_write(pte); 451 if (flags & FPB_MERGE_YOUNG_DIRTY) { 452 any_young |= pte_young(pte); 453 any_dirty |= pte_dirty(pte); 454 } 455 456 cur_nr = pte_batch_hint(ptep, pte); 457 expected_pte = pte_advance_pfn(expected_pte, cur_nr); 458 ptep += cur_nr; 459 nr += cur_nr; 460 } 461 462 if (any_writable) 463 *ptentp = pte_mkwrite(*ptentp, vma); 464 if (any_young) 465 *ptentp = pte_mkyoung(*ptentp); 466 if (any_dirty) 467 *ptentp = pte_mkdirty(*ptentp); 468 469 return min(nr, max_nr); 470 } 471 472 unsigned int folio_pte_batch(struct folio *folio, pte_t *ptep, pte_t pte, 473 unsigned int max_nr); 474 475 /** 476 * pte_move_swp_offset - Move the swap entry offset field of a swap pte 477 * forward or backward by delta 478 * @pte: The initial pte state; must be a swap entry 479 * @delta: The direction and the offset we are moving; forward if delta 480 * is positive; backward if delta is negative 481 * 482 * Moves the swap offset, while maintaining all other fields, including 483 * swap type, and any swp pte bits. The resulting pte is returned. 484 */ 485 static inline pte_t pte_move_swp_offset(pte_t pte, long delta) 486 { 487 const softleaf_t entry = softleaf_from_pte(pte); 488 pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry), 489 (swp_offset(entry) + delta))); 490 491 if (pte_swp_soft_dirty(pte)) 492 new = pte_swp_mksoft_dirty(new); 493 if (pte_swp_exclusive(pte)) 494 new = pte_swp_mkexclusive(new); 495 if (pte_swp_uffd(pte)) 496 new = pte_swp_mkuffd(new); 497 498 return new; 499 } 500 501 502 /** 503 * pte_next_swp_offset - Increment the swap entry offset field of a swap pte. 504 * @pte: The initial pte state; must be a swap entry. 505 * 506 * Increments the swap offset, while maintaining all other fields, including 507 * swap type, and any swp pte bits. The resulting pte is returned. 508 */ 509 static inline pte_t pte_next_swp_offset(pte_t pte) 510 { 511 return pte_move_swp_offset(pte, 1); 512 } 513 514 /** 515 * swap_pte_batch - detect a PTE batch for a set of contiguous swap entries 516 * @start_ptep: Page table pointer for the first entry. 517 * @max_nr: The maximum number of table entries to consider. 518 * @pte: Page table entry for the first entry. 519 * 520 * Detect a batch of contiguous swap entries: consecutive (non-present) PTEs 521 * containing swap entries all with consecutive offsets and targeting the same 522 * swap type, all with matching swp pte bits. 523 * 524 * max_nr must be at least one and must be limited by the caller so scanning 525 * cannot exceed a single page table. 526 * 527 * Return: the number of table entries in the batch. 528 */ 529 static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte) 530 { 531 pte_t expected_pte = pte_next_swp_offset(pte); 532 const pte_t *end_ptep = start_ptep + max_nr; 533 pte_t *ptep = start_ptep + 1; 534 535 VM_WARN_ON(max_nr < 1); 536 VM_WARN_ON(!softleaf_is_swap(softleaf_from_pte(pte))); 537 538 while (ptep < end_ptep) { 539 pte = ptep_get(ptep); 540 541 if (!pte_same(pte, expected_pte)) 542 break; 543 expected_pte = pte_next_swp_offset(expected_pte); 544 ptep++; 545 } 546 547 return ptep - start_ptep; 548 } 549 #endif /* CONFIG_MMU */ 550 551 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 552 int nr_throttled); 553 static inline void acct_reclaim_writeback(struct folio *folio) 554 { 555 pg_data_t *pgdat = folio_pgdat(folio); 556 int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled); 557 558 if (nr_throttled) 559 __acct_reclaim_writeback(pgdat, folio, nr_throttled); 560 } 561 562 static inline void wake_throttle_isolated(pg_data_t *pgdat) 563 { 564 wait_queue_head_t *wqh; 565 566 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED]; 567 if (waitqueue_active(wqh)) 568 wake_up(wqh); 569 } 570 571 vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf); 572 static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) 573 { 574 vm_fault_t ret = __vmf_anon_prepare(vmf); 575 576 if (unlikely(ret & VM_FAULT_RETRY)) 577 vma_end_read(vmf->vma); 578 return ret; 579 } 580 581 vm_fault_t do_swap_page(struct vm_fault *vmf); 582 void folio_rotate_reclaimable(struct folio *folio); 583 bool __folio_end_writeback(struct folio *folio); 584 void deactivate_file_folio(struct folio *folio); 585 void folio_activate(struct folio *folio); 586 587 void free_pgtables(struct mmu_gather *tlb, struct unmap_desc *desc); 588 589 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); 590 591 /** 592 * sync_with_folio_pmd_zap - sync with concurrent zapping of a folio PMD 593 * @mm: The mm_struct. 594 * @pmdp: Pointer to the pmd that was found to be pmd_none(). 595 * 596 * When we find a pmd_none() while unmapping a folio without holding the PTL, 597 * zap_huge_pmd() may have cleared the PMD but not yet modified the folio to 598 * indicate that it's unmapped. Skipping the PMD without synchronization could 599 * make folio unmapping code assume that unmapping failed. 600 * 601 * Wait for concurrent zapping to complete by grabbing the PTL. 602 */ 603 static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) 604 { 605 spinlock_t *ptl = pmd_lock(mm, pmdp); 606 607 spin_unlock(ptl); 608 } 609 610 struct zap_details; 611 void zap_vma_range_batched(struct mmu_gather *tlb, 612 struct vm_area_struct *vma, unsigned long addr, 613 unsigned long size, struct zap_details *details); 614 int zap_vma_for_reaping(struct vm_area_struct *vma); 615 int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, 616 gfp_t gfp); 617 618 void page_cache_ra_order(struct readahead_control *, struct file_ra_state *); 619 void force_page_cache_ra(struct readahead_control *, unsigned long nr); 620 static inline void force_page_cache_readahead(struct address_space *mapping, 621 struct file *file, pgoff_t index, unsigned long nr_to_read) 622 { 623 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index); 624 force_page_cache_ra(&ractl, nr_to_read); 625 } 626 627 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, 628 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 629 unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, 630 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 631 int truncate_inode_folio(struct address_space *mapping, struct folio *folio); 632 bool truncate_inode_partial_folio(struct folio *folio, loff_t lstart, 633 loff_t lend, pgoff_t *pstart, pgoff_t *pend); 634 long mapping_evict_folio(struct address_space *mapping, struct folio *folio); 635 unsigned long mapping_try_invalidate(struct address_space *mapping, 636 pgoff_t start, pgoff_t end, unsigned long *nr_failed); 637 638 /** 639 * folio_evictable - Test whether a folio is evictable. 640 * @folio: The folio to test. 641 * 642 * Test whether @folio is evictable -- i.e., should be placed on 643 * active/inactive lists vs unevictable list. 644 * 645 * Reasons folio might not be evictable: 646 * 1. folio's mapping marked unevictable 647 * 2. One of the pages in the folio is part of an mlocked VMA 648 */ 649 static inline bool folio_evictable(struct folio *folio) 650 { 651 bool ret; 652 653 /* Prevent address_space of inode and swap cache from being freed */ 654 rcu_read_lock(); 655 ret = !mapping_unevictable(folio_mapping(folio)) && 656 !folio_test_mlocked(folio); 657 rcu_read_unlock(); 658 return ret; 659 } 660 661 /* 662 * Turn a non-refcounted page (->_refcount == 0) into refcounted with 663 * a count of one. 664 */ 665 static inline void set_page_refcounted(struct page *page) 666 { 667 VM_BUG_ON_PAGE(PageTail(page), page); 668 VM_BUG_ON_PAGE(page_ref_count(page), page); 669 set_page_count(page, 1); 670 } 671 672 static inline void set_pages_refcounted(struct page *page, unsigned long nr_pages) 673 { 674 unsigned long pfn = page_to_pfn(page); 675 676 for (; nr_pages--; pfn++) 677 set_page_refcounted(pfn_to_page(pfn)); 678 } 679 680 /* 681 * Return true if a folio needs ->release_folio() calling upon it. 682 */ 683 static inline bool folio_needs_release(struct folio *folio) 684 { 685 struct address_space *mapping = folio_mapping(folio); 686 687 return folio_has_private(folio) || 688 (mapping && mapping_release_always(mapping)); 689 } 690 691 extern unsigned long highest_memmap_pfn; 692 693 /* 694 * Maximum number of reclaim retries without progress before the OOM 695 * killer is consider the only way forward. 696 */ 697 #define MAX_RECLAIM_RETRIES 16 698 699 /* 700 * in mm/vmscan.c: 701 */ 702 bool folio_isolate_lru(struct folio *folio); 703 void folio_putback_lru(struct folio *folio); 704 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason); 705 int user_proactive_reclaim(char *buf, 706 struct mem_cgroup *memcg, pg_data_t *pgdat); 707 708 /* 709 * in mm/rmap.c: 710 */ 711 pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address); 712 713 /* 714 * in mm/khugepaged.c 715 */ 716 void set_recommended_min_free_kbytes(void); 717 718 /* 719 * in mm/page_alloc.c 720 */ 721 #define K(x) ((x) << (PAGE_SHIFT-10)) 722 723 extern char * const zone_names[MAX_NR_ZONES]; 724 725 extern int min_free_kbytes; 726 extern int defrag_mode; 727 728 void setup_per_zone_wmarks(void); 729 void calculate_min_free_kbytes(void); 730 int __meminit init_per_zone_wmark_min(void); 731 732 extern int __isolate_free_page(struct page *page, unsigned int order); 733 extern void __putback_isolated_page(struct page *page, unsigned int order, 734 int mt); 735 736 /* 737 * This will have no effect, other than possibly generating a warning, if the 738 * caller passes in a non-large folio. 739 */ 740 static inline void folio_set_order(struct folio *folio, unsigned int order) 741 { 742 if (WARN_ON_ONCE(!order || !folio_test_large(folio))) 743 return; 744 VM_WARN_ON_ONCE(order > MAX_FOLIO_ORDER); 745 746 folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order; 747 #ifdef NR_PAGES_IN_LARGE_FOLIO 748 folio->_nr_pages = 1U << order; 749 #endif 750 } 751 752 bool __folio_unqueue_deferred_split(struct folio *folio); 753 static inline bool folio_unqueue_deferred_split(struct folio *folio) 754 { 755 if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio)) 756 return false; 757 758 /* 759 * At this point, there is no one trying to add the folio to 760 * deferred_list. If folio is not in deferred_list, it's safe 761 * to check without acquiring the list_lru lock. 762 */ 763 if (data_race(list_empty(&folio->_deferred_list))) 764 return false; 765 766 return __folio_unqueue_deferred_split(folio); 767 } 768 769 static inline struct folio *page_rmappable_folio(struct page *page) 770 { 771 struct folio *folio = (struct folio *)page; 772 773 if (folio && folio_test_large(folio)) 774 folio_set_large_rmappable(folio); 775 return folio; 776 } 777 778 static inline void prep_compound_head(struct page *page, unsigned int order) 779 { 780 struct folio *folio = (struct folio *)page; 781 782 folio_set_order(folio, order); 783 atomic_set(&folio->_large_mapcount, -1); 784 if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) 785 atomic_set(&folio->_nr_pages_mapped, 0); 786 if (IS_ENABLED(CONFIG_MM_ID)) { 787 folio->_mm_ids = 0; 788 folio->_mm_id_mapcount[0] = -1; 789 folio->_mm_id_mapcount[1] = -1; 790 } 791 if (IS_ENABLED(CONFIG_64BIT) || order > 1) { 792 atomic_set(&folio->_pincount, 0); 793 atomic_set(&folio->_entire_mapcount, -1); 794 } 795 if (order > 1) 796 INIT_LIST_HEAD(&folio->_deferred_list); 797 } 798 799 static inline void prep_compound_tail(struct page *tail, 800 const struct page *head, unsigned int order) 801 { 802 tail->mapping = TAIL_MAPPING; 803 set_compound_head(tail, head, order); 804 VM_WARN_ON_ONCE(tail->private); 805 } 806 807 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 808 809 /* 810 * in mm/compaction.c 811 */ 812 /* 813 * compact_control is used to track pages being migrated and the free pages 814 * they are being migrated to during memory compaction. The free_pfn starts 815 * at the end of a zone and migrate_pfn begins at the start. Movable pages 816 * are moved to the end of a zone during a compaction run and the run 817 * completes when free_pfn <= migrate_pfn 818 */ 819 struct compact_control { 820 struct list_head freepages[NR_PAGE_ORDERS]; /* List of free pages to migrate to */ 821 struct list_head migratepages; /* List of pages being migrated */ 822 unsigned int nr_freepages; /* Number of isolated free pages */ 823 unsigned int nr_migratepages; /* Number of pages to migrate */ 824 unsigned long free_pfn; /* isolate_freepages search base */ 825 /* 826 * Acts as an in/out parameter to page isolation for migration. 827 * isolate_migratepages uses it as a search base. 828 * isolate_migratepages_block will update the value to the next pfn 829 * after the last isolated one. 830 */ 831 unsigned long migrate_pfn; 832 unsigned long fast_start_pfn; /* a pfn to start linear scan from */ 833 struct zone *zone; 834 unsigned long total_migrate_scanned; 835 unsigned long total_free_scanned; 836 unsigned short fast_search_fail;/* failures to use free list searches */ 837 short search_order; /* order to start a fast search at */ 838 const gfp_t gfp_mask; /* gfp mask of a direct compactor */ 839 int order; /* order a direct compactor needs */ 840 int migratetype; /* migratetype of direct compactor */ 841 const unsigned int alloc_flags; /* alloc flags of a direct compactor */ 842 const int highest_zoneidx; /* zone index of a direct compactor */ 843 enum migrate_mode mode; /* Async or sync migration mode */ 844 bool ignore_skip_hint; /* Scan blocks even if marked skip */ 845 bool no_set_skip_hint; /* Don't mark blocks for skipping */ 846 bool ignore_block_suitable; /* Scan blocks considered unsuitable */ 847 bool direct_compaction; /* False from kcompactd or /proc/... */ 848 bool proactive_compaction; /* kcompactd proactive compaction */ 849 bool whole_zone; /* Whole zone should/has been scanned */ 850 bool contended; /* Signal lock contention */ 851 bool finish_pageblock; /* Scan the remainder of a pageblock. Used 852 * when there are potentially transient 853 * isolation or migration failures to 854 * ensure forward progress. 855 */ 856 bool alloc_contig; /* alloc_contig_range allocation */ 857 }; 858 859 /* 860 * Used in direct compaction when a page should be taken from the freelists 861 * immediately when one is created during the free path. 862 */ 863 struct capture_control { 864 struct zone *zone; 865 int migratetype; 866 /* 867 * Allocation request order. May differ from the compaction 868 * order: defrag_mode promotes sub-block allocations to 869 * pageblock-order compaction; capture still matches at the 870 * original allocation order so prep_new_page() is consistent. 871 */ 872 int order; 873 struct page *page; 874 }; 875 876 unsigned long 877 isolate_freepages_range(struct compact_control *cc, 878 unsigned long start_pfn, unsigned long end_pfn); 879 int 880 isolate_migratepages_range(struct compact_control *cc, 881 unsigned long low_pfn, unsigned long end_pfn); 882 883 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 884 885 struct cma; 886 887 #ifdef CONFIG_CMA 888 bool cma_validate_zones(struct cma *cma); 889 void *cma_reserve_early(struct cma *cma, unsigned long size); 890 #else 891 static inline bool cma_validate_zones(struct cma *cma) 892 { 893 return false; 894 } 895 static inline void *cma_reserve_early(struct cma *cma, unsigned long size) 896 { 897 return NULL; 898 } 899 #endif 900 901 /* mm/util.c */ 902 struct anon_vma *folio_anon_vma(const struct folio *folio); 903 904 #ifdef CONFIG_MMU 905 void unmap_mapping_folio(struct folio *folio); 906 extern long populate_vma_page_range(struct vm_area_struct *vma, 907 unsigned long start, unsigned long end, int *locked); 908 extern long faultin_page_range(struct mm_struct *mm, unsigned long start, 909 unsigned long end, bool write, int *locked); 910 bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked, 911 unsigned long bytes); 912 913 /* 914 * NOTE: This function can't tell whether the folio is "fully mapped" in the 915 * range. 916 * "fully mapped" means all the pages of folio is associated with the page 917 * table of range while this function just check whether the folio range is 918 * within the range [start, end). Function caller needs to do page table 919 * check if it cares about the page table association. 920 * 921 * Typical usage (like mlock or madvise) is: 922 * Caller knows at least 1 page of folio is associated with page table of VMA 923 * and the range [start, end) is intersect with the VMA range. Caller wants 924 * to know whether the folio is fully associated with the range. It calls 925 * this function to check whether the folio is in the range first. Then checks 926 * the page table to know whether the folio is fully mapped to the range. 927 */ 928 static inline bool 929 folio_within_range(struct folio *folio, struct vm_area_struct *vma, 930 unsigned long start, unsigned long end) 931 { 932 const unsigned long vma_pglen = vma_pages(vma); 933 pgoff_t pgoff_folio, pgoff_vma_start; 934 unsigned long addr; 935 936 VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio); 937 if (start > end) 938 return false; 939 940 pgoff_folio = folio_pgoff(folio); 941 pgoff_vma_start = folio_test_anon(folio) ? 942 vma_start_anon_pgoff(vma) : vma_start_pgoff(vma); 943 944 if (start < vma->vm_start) 945 start = vma->vm_start; 946 947 if (end > vma->vm_end) 948 end = vma->vm_end; 949 950 /* if folio start address is not in vma range */ 951 if (!in_range(pgoff_folio, pgoff_vma_start, vma_pglen)) 952 return false; 953 954 addr = vma->vm_start + ((pgoff_folio - pgoff_vma_start) << PAGE_SHIFT); 955 956 return !(addr < start || end - addr < folio_size(folio)); 957 } 958 959 static inline bool 960 folio_within_vma(struct folio *folio, struct vm_area_struct *vma) 961 { 962 return folio_within_range(folio, vma, vma->vm_start, vma->vm_end); 963 } 964 965 /* 966 * mlock_vma_folio() and munlock_vma_folio(): 967 * should be called with vma's mmap_lock held for read or write, 968 * under page table lock for the pte/pmd being added or removed. 969 * 970 * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at 971 * the end of folio_remove_rmap_*(); but new anon folios are managed by 972 * folio_add_lru_vma() calling mlock_new_folio(). 973 */ 974 void mlock_folio(struct folio *folio); 975 static inline void mlock_vma_folio(struct folio *folio, 976 struct vm_area_struct *vma) 977 { 978 if (vma_test(vma, VMA_LOCKED_BIT)) 979 mlock_folio(folio); 980 } 981 982 void munlock_folio(struct folio *folio); 983 static inline void munlock_vma_folio(struct folio *folio, 984 struct vm_area_struct *vma) 985 { 986 /* 987 * munlock if the function is called. Ideally, we should only 988 * do munlock if any page of folio is unmapped from VMA and 989 * cause folio not fully mapped to VMA. 990 * 991 * But it's not easy to confirm that's the situation. So we 992 * always munlock the folio and page reclaim will correct it 993 * if it's wrong. 994 */ 995 /* 996 * VMA_LOCKONFAULT_BIT alone marks an mlock walk in progress, see 997 * mlock_vma_pages_range(). An unmap racing with the walk must still 998 * munlock folios the walk has already counted. 999 */ 1000 if (unlikely(vma_test_any_mask(vma, VMA_LOCKED_MASK))) 1001 munlock_folio(folio); 1002 } 1003 1004 void mlock_new_folio(struct folio *folio); 1005 bool need_mlock_drain(int cpu); 1006 void mlock_drain_local(void); 1007 void mlock_drain_remote(int cpu); 1008 1009 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); 1010 1011 static inline unsigned long __vma_address(const struct vm_area_struct *vma, 1012 pgoff_t pgoff, pgoff_t pgoff_start, unsigned long nr_pages) 1013 { 1014 unsigned long address; 1015 1016 if (pgoff >= pgoff_start) { 1017 address = vma->vm_start + 1018 ((pgoff - pgoff_start) << PAGE_SHIFT); 1019 /* Check for address beyond vma (or wrapped through 0?) */ 1020 if (address < vma->vm_start || address >= vma->vm_end) 1021 address = -EFAULT; 1022 } else if (pgoff + nr_pages - 1 >= pgoff_start) { 1023 /* Test above avoids possibility of wrap to 0 on 32-bit */ 1024 address = vma->vm_start; 1025 } else { 1026 address = -EFAULT; 1027 } 1028 return address; 1029 } 1030 1031 /** 1032 * vma_filebacked_address - Find the virtual address a file-backed page range is 1033 * 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 * Returns: If any page in this range is mapped by this VMA, return the first 1039 * address where any of these pages appear. Otherwise, return -EFAULT. 1040 */ 1041 static inline unsigned long vma_filebacked_address(const struct vm_area_struct *vma, 1042 pgoff_t pgoff, unsigned long nr_pages) 1043 { 1044 VM_WARN_ON_ONCE(vma_is_anonymous(vma)); 1045 1046 return __vma_address(vma, pgoff, vma_start_pgoff(vma), nr_pages); 1047 } 1048 1049 /** 1050 * vma_anon_address - Find the virtual address an anonymous page range is mapped 1051 * at. 1052 * @vma: The vma which maps this object. 1053 * @pgoff_anon: The anonymous page index belonging to the folio. 1054 * @nr_pages: The number of pages to consider. 1055 * 1056 * This is only valid for anonymous or MAP_PRIVATE-mapped file-backed VMAs. 1057 * 1058 * Returns: If any page in this range is mapped by this VMA, return the first 1059 * address where any of these pages appear. Otherwise, return -EFAULT. 1060 */ 1061 static inline unsigned long vma_anon_address(const struct vm_area_struct *vma, 1062 pgoff_t pgoff_anon, unsigned long nr_pages) 1063 { 1064 VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma)); 1065 1066 return __vma_address(vma, pgoff_anon, vma_start_anon_pgoff(vma), nr_pages); 1067 } 1068 1069 /* 1070 * At what user virtual address will none of the range be found in vma? 1071 * Assumes that vma_address() already returned a good starting address. 1072 */ 1073 static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw) 1074 { 1075 const pgoff_t pgoff_end = pvmw->pgoff + pvmw->nr_pages; 1076 const struct vm_area_struct *vma = pvmw->vma; 1077 pgoff_t pgoff_vma_start; 1078 unsigned long address; 1079 1080 /* Common case, plus ->pgoff is invalid for KSM */ 1081 if (pvmw->nr_pages == 1) 1082 return pvmw->address + PAGE_SIZE; 1083 1084 if (pvmw->pgoff_is_anon) 1085 pgoff_vma_start = vma_start_anon_pgoff(vma); 1086 else 1087 pgoff_vma_start = vma_start_pgoff(vma); 1088 1089 address = vma->vm_start + 1090 ((pgoff_end - pgoff_vma_start) << PAGE_SHIFT); 1091 /* Check for address beyond vma (or wrapped through 0?) */ 1092 if (address < vma->vm_start || address > vma->vm_end) 1093 address = vma->vm_end; 1094 return address; 1095 } 1096 1097 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, 1098 struct file *fpin) 1099 { 1100 int flags = vmf->flags; 1101 1102 if (fpin) 1103 return fpin; 1104 1105 /* 1106 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or 1107 * anything, so we only pin the file and drop the mmap_lock if only 1108 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. 1109 */ 1110 if (fault_flag_allow_retry_first(flags) && 1111 !(flags & FAULT_FLAG_RETRY_NOWAIT)) { 1112 fpin = get_file(vmf->vma->vm_file); 1113 release_fault_lock(vmf); 1114 } 1115 return fpin; 1116 } 1117 1118 static inline bool vma_supports_mlock(const struct vm_area_struct *vma) 1119 { 1120 if (!vma_is_mm_backed(vma)) 1121 return false; 1122 /* Core mm may discard droppable memory at any time. */ 1123 if (vma_test_single_mask(vma, VMA_DROPPABLE)) 1124 return false; 1125 if (vma_is_dax(vma) || vma_is_hugetlb(vma)) 1126 return false; 1127 return vma != get_gate_vma(current->mm); 1128 } 1129 1130 #else /* !CONFIG_MMU */ 1131 static inline void unmap_mapping_folio(struct folio *folio) { } 1132 static inline void mlock_new_folio(struct folio *folio) { } 1133 static inline bool need_mlock_drain(int cpu) { return false; } 1134 static inline void mlock_drain_local(void) { } 1135 static inline void mlock_drain_remote(int cpu) { } 1136 #endif /* !CONFIG_MMU */ 1137 1138 #ifdef CONFIG_NUMA 1139 extern int node_reclaim_mode; 1140 1141 extern unsigned long node_reclaim(struct pglist_data *pgdat, 1142 gfp_t gfp_mask, unsigned int order); 1143 int find_next_best_node_in(int node, nodemask_t *used_node_mask, 1144 const nodemask_t *candidates); 1145 #else 1146 #define node_reclaim_mode 0 1147 1148 static inline unsigned long node_reclaim(struct pglist_data *pgdat, 1149 gfp_t mask, unsigned int order) 1150 { 1151 return 0; 1152 } 1153 static inline int find_next_best_node_in(int node, nodemask_t *used_node_mask, 1154 const nodemask_t *candidates) 1155 { 1156 return NUMA_NO_NODE; 1157 } 1158 #endif 1159 1160 static inline bool node_reclaim_enabled(void) 1161 { 1162 /* Is any node_reclaim_mode bit set? */ 1163 return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP); 1164 } 1165 1166 /* 1167 * mm/memory-failure.c 1168 */ 1169 #ifdef CONFIG_MEMORY_FAILURE 1170 int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill); 1171 void shake_folio(struct folio *folio); 1172 typedef int hwpoison_filter_func_t(struct page *p); 1173 void hwpoison_filter_register(hwpoison_filter_func_t *filter); 1174 void hwpoison_filter_unregister(void); 1175 1176 #define MAGIC_HWPOISON 0x48575053U /* HWPS */ 1177 void SetPageHWPoisonTakenOff(struct page *page); 1178 void ClearPageHWPoisonTakenOff(struct page *page); 1179 bool take_page_off_buddy(struct page *page); 1180 bool put_page_back_buddy(struct page *page); 1181 struct task_struct *task_early_kill(struct task_struct *tsk, int force_early); 1182 void add_to_kill_ksm(struct task_struct *tsk, const struct page *p, 1183 struct vm_area_struct *vma, struct list_head *to_kill, 1184 unsigned long ksm_addr); 1185 unsigned long page_mapped_in_vma(const struct page *page, 1186 struct vm_area_struct *vma); 1187 1188 #else 1189 static inline int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill) 1190 { 1191 return -EBUSY; 1192 } 1193 #endif 1194 1195 extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long, 1196 unsigned long, unsigned long, 1197 unsigned long, unsigned long); 1198 1199 unsigned long reclaim_pages(struct list_head *folio_list); 1200 unsigned int reclaim_clean_pages_from_list(struct zone *zone, 1201 struct list_head *folio_list); 1202 1203 enum ttu_flags; 1204 struct tlbflush_unmap_batch; 1205 1206 1207 /* 1208 * only for MM internal work items which do not depend on 1209 * any allocations or locks which might depend on allocations 1210 */ 1211 extern struct workqueue_struct *mm_percpu_wq; 1212 1213 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1214 void try_to_unmap_flush(void); 1215 void try_to_unmap_flush_dirty(void); 1216 void flush_tlb_batched_pending(struct mm_struct *mm); 1217 #else 1218 static inline void try_to_unmap_flush(void) 1219 { 1220 } 1221 static inline void try_to_unmap_flush_dirty(void) 1222 { 1223 } 1224 static inline void flush_tlb_batched_pending(struct mm_struct *mm) 1225 { 1226 } 1227 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ 1228 1229 extern const struct trace_print_flags pageflag_names[]; 1230 extern const struct trace_print_flags vmaflag_names[]; 1231 extern const struct trace_print_flags gfpflag_names[]; 1232 1233 void setup_zone_pageset(struct zone *zone); 1234 1235 struct migration_target_control { 1236 int nid; /* preferred node id */ 1237 nodemask_t *nmask; 1238 gfp_t gfp_mask; 1239 enum migrate_reason reason; 1240 }; 1241 1242 /* 1243 * mm/filemap.c 1244 */ 1245 size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, 1246 struct folio *folio, loff_t fpos, size_t size); 1247 1248 static inline bool vma_is_single_threaded_private(struct vm_area_struct *vma) 1249 { 1250 if (vma->vm_flags & VM_SHARED) 1251 return false; 1252 1253 return atomic_read(&vma->vm_mm->mm_users) == 1; 1254 } 1255 1256 #ifdef CONFIG_NUMA_BALANCING 1257 bool folio_can_map_prot_numa(struct folio *folio, struct vm_area_struct *vma, 1258 bool is_private_single_threaded); 1259 1260 #else 1261 static inline bool folio_can_map_prot_numa(struct folio *folio, 1262 struct vm_area_struct *vma, bool is_private_single_threaded) 1263 { 1264 return false; 1265 } 1266 #endif 1267 1268 int numa_migrate_check(struct folio *folio, struct vm_fault *vmf, 1269 unsigned long addr, int *flags, bool writable, 1270 int *last_cpupid); 1271 1272 void free_zone_device_folio(struct folio *folio); 1273 int migrate_device_coherent_folio(struct folio *folio); 1274 1275 /* 1276 * mm/gup.c 1277 */ 1278 int __must_check try_grab_folio(struct folio *folio, int refs, 1279 unsigned int flags); 1280 1281 /* 1282 * mm/huge_memory.c 1283 */ 1284 void touch_pud(struct vm_area_struct *vma, unsigned long addr, 1285 pud_t *pud, bool write); 1286 bool touch_pmd(struct vm_area_struct *vma, unsigned long addr, 1287 pmd_t *pmd, bool write); 1288 1289 /* 1290 * Parses a string with mem suffixes into its order. Useful to parse kernel 1291 * parameters. 1292 */ 1293 static inline int get_order_from_str(const char *size_str, 1294 unsigned long valid_orders) 1295 { 1296 unsigned long size; 1297 char *endptr; 1298 int order; 1299 1300 size = memparse(size_str, &endptr); 1301 1302 if (!is_power_of_2(size)) 1303 return -EINVAL; 1304 order = get_order(size); 1305 if (BIT(order) & ~valid_orders) 1306 return -EINVAL; 1307 1308 return order; 1309 } 1310 1311 enum { 1312 /* mark page accessed */ 1313 FOLL_TOUCH = 1 << 16, 1314 /* a retry, previous pass started an IO */ 1315 FOLL_TRIED = 1 << 17, 1316 /* we are working on non-current tsk/mm */ 1317 FOLL_REMOTE = 1 << 18, 1318 /* pages must be released via unpin_user_page */ 1319 FOLL_PIN = 1 << 19, 1320 /* gup_fast: prevent fall-back to slow gup */ 1321 FOLL_FAST_ONLY = 1 << 20, 1322 /* allow unlocking the mmap lock */ 1323 FOLL_UNLOCKABLE = 1 << 21, 1324 /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ 1325 FOLL_MADV_POPULATE = 1 << 22, 1326 }; 1327 1328 #define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ 1329 FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ 1330 FOLL_MADV_POPULATE) 1331 1332 /* 1333 * Indicates for which pages that are write-protected in the page table, 1334 * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the 1335 * GUP pin will remain consistent with the pages mapped into the page tables 1336 * of the MM. 1337 * 1338 * Temporary unmapping of PageAnonExclusive() pages or clearing of 1339 * PageAnonExclusive() has to protect against concurrent GUP: 1340 * * Ordinary GUP: Using the PT lock 1341 * * GUP-fast and fork(): mm->write_protect_seq 1342 * * GUP-fast and KSM or temporary unmapping (swap, migration): see 1343 * folio_try_share_anon_rmap_*() 1344 * 1345 * Must be called with the (sub)page that's actually referenced via the 1346 * page table entry, which might not necessarily be the head page for a 1347 * PTE-mapped THP. 1348 * 1349 * If the vma is NULL, we're coming from the GUP-fast path and might have 1350 * to fallback to the slow path just to lookup the vma. 1351 */ 1352 static inline bool gup_must_unshare(struct vm_area_struct *vma, 1353 unsigned int flags, struct page *page) 1354 { 1355 /* 1356 * FOLL_WRITE is implicitly handled correctly as the page table entry 1357 * has to be writable -- and if it references (part of) an anonymous 1358 * folio, that part is required to be marked exclusive. 1359 */ 1360 if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN) 1361 return false; 1362 /* 1363 * Note: PageAnon(page) is stable until the page is actually getting 1364 * freed. 1365 */ 1366 if (!PageAnon(page)) { 1367 /* 1368 * We only care about R/O long-term pining: R/O short-term 1369 * pinning does not have the semantics to observe successive 1370 * changes through the process page tables. 1371 */ 1372 if (!(flags & FOLL_LONGTERM)) 1373 return false; 1374 1375 /* We really need the vma ... */ 1376 if (!vma) 1377 return true; 1378 1379 /* 1380 * ... because we only care about writable private ("COW") 1381 * mappings where we have to break COW early. 1382 */ 1383 return vma_is_cow_mapping(vma); 1384 } 1385 1386 /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */ 1387 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) 1388 smp_rmb(); 1389 1390 /* 1391 * Note that KSM pages cannot be exclusive, and consequently, 1392 * cannot get pinned. 1393 */ 1394 return !PageAnonExclusive(page); 1395 } 1396 1397 1398 static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) 1399 { 1400 /* 1401 * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty 1402 * enablements, because when without soft-dirty being compiled in, 1403 * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) 1404 * will be constantly true. 1405 */ 1406 if (!pgtable_supports_soft_dirty()) 1407 return false; 1408 1409 /* 1410 * Soft-dirty is kind of special: its tracking is enabled when the 1411 * vma flags not set. 1412 */ 1413 return !(vma->vm_flags & VM_SOFTDIRTY); 1414 } 1415 1416 static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd) 1417 { 1418 return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd); 1419 } 1420 1421 static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte) 1422 { 1423 return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte); 1424 } 1425 1426 /* shrinker related functions */ 1427 unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg, 1428 int priority); 1429 1430 int shmem_add_to_page_cache(struct folio *folio, 1431 struct address_space *mapping, 1432 pgoff_t index, void *expected, gfp_t gfp); 1433 int shmem_inode_acct_blocks(struct inode *inode, long pages); 1434 bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped); 1435 1436 #ifdef CONFIG_SHRINKER_DEBUG 1437 static inline __printf(2, 0) int shrinker_debugfs_name_alloc( 1438 struct shrinker *shrinker, const char *fmt, va_list ap) 1439 { 1440 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap); 1441 1442 return shrinker->name ? 0 : -ENOMEM; 1443 } 1444 1445 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1446 { 1447 kfree_const(shrinker->name); 1448 shrinker->name = NULL; 1449 } 1450 1451 extern int shrinker_debugfs_add(struct shrinker *shrinker); 1452 extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1453 int *debugfs_id); 1454 extern void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1455 int debugfs_id); 1456 #else /* CONFIG_SHRINKER_DEBUG */ 1457 static inline int shrinker_debugfs_add(struct shrinker *shrinker) 1458 { 1459 return 0; 1460 } 1461 static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker, 1462 const char *fmt, va_list ap) 1463 { 1464 return 0; 1465 } 1466 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1467 { 1468 } 1469 static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1470 int *debugfs_id) 1471 { 1472 *debugfs_id = -1; 1473 return NULL; 1474 } 1475 static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1476 int debugfs_id) 1477 { 1478 } 1479 #endif /* CONFIG_SHRINKER_DEBUG */ 1480 1481 /* Only track the nodes of mappings with shadow entries */ 1482 void workingset_update_node(struct xa_node *node); 1483 extern struct list_lru shadow_nodes; 1484 #define mapping_set_update(xas, mapping) do { \ 1485 if (!dax_mapping(mapping) && !shmem_mapping(mapping)) { \ 1486 xas_set_update(xas, workingset_update_node); \ 1487 xas_set_lru(xas, &shadow_nodes); \ 1488 } \ 1489 } while (0) 1490 1491 /* mremap.c */ 1492 unsigned long move_page_tables(struct pagetable_move_control *pmc); 1493 1494 #ifdef CONFIG_UNACCEPTED_MEMORY 1495 void accept_page(struct page *page); 1496 #else /* CONFIG_UNACCEPTED_MEMORY */ 1497 static inline void accept_page(struct page *page) 1498 { 1499 } 1500 #endif /* CONFIG_UNACCEPTED_MEMORY */ 1501 1502 /* pagewalk.c */ 1503 int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start, 1504 unsigned long end, const struct mm_walk_ops *ops, 1505 void *private); 1506 int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start, 1507 unsigned long end, const struct mm_walk_ops *ops, 1508 void *private); 1509 int walk_page_range_debug(struct mm_struct *mm, unsigned long start, 1510 unsigned long end, const struct mm_walk_ops *ops, 1511 pgd_t *pgd, void *private); 1512 1513 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm); 1514 int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm); 1515 1516 int remap_pfn_range_prepare(struct vm_area_desc *desc); 1517 int remap_pfn_range_complete(struct vm_area_struct *vma, 1518 struct mmap_action *action); 1519 int simple_ioremap_prepare(struct vm_area_desc *desc); 1520 int map_kernel_pages_prepare(struct vm_area_desc *desc); 1521 int map_kernel_pages_complete(struct vm_area_struct *vma, 1522 struct mmap_action *action); 1523 int map_discontig_kernel_pages_prepare(struct vm_area_desc *desc); 1524 int map_discontig_kernel_pages_complete(struct vm_area_struct *vma, 1525 struct mmap_action *action); 1526 1527 static inline int io_remap_pfn_range_prepare(struct vm_area_desc *desc) 1528 { 1529 struct mmap_action *action = &desc->action; 1530 const unsigned long orig_pfn = action->remap.start_pfn; 1531 const pgprot_t orig_pgprot = action->remap.pgprot; 1532 const unsigned long size = action->remap.size; 1533 const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size); 1534 int err; 1535 1536 action->remap.start_pfn = pfn; 1537 action->remap.pgprot = pgprot_decrypted(orig_pgprot); 1538 err = remap_pfn_range_prepare(desc); 1539 if (err) 1540 return err; 1541 1542 /* Remap does the actual work. */ 1543 action->type = MMAP_REMAP_PFN; 1544 return 0; 1545 } 1546 1547 /* 1548 * When we succeed an mmap action or just before we unmap a VMA on error, we 1549 * need to ensure any rmap lock held is released. On unmap it's required to 1550 * avoid a deadlock. 1551 */ 1552 static inline void maybe_rmap_unlock_action(struct vm_area_struct *vma, 1553 struct mmap_action *action) 1554 { 1555 struct file *file; 1556 1557 if (!action->hide_from_rmap_until_complete) 1558 return; 1559 1560 VM_WARN_ON_ONCE(vma_is_anonymous(vma)); 1561 file = vma->vm_file; 1562 i_mmap_unlock_write(file->f_mapping); 1563 action->hide_from_rmap_until_complete = false; 1564 } 1565 1566 #ifdef CONFIG_MMU_NOTIFIER 1567 static inline bool clear_flush_young_ptes_notify(struct vm_area_struct *vma, 1568 unsigned long addr, pte_t *ptep, unsigned int nr) 1569 { 1570 bool young; 1571 1572 young = clear_flush_young_ptes(vma, addr, ptep, nr); 1573 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, 1574 addr + nr * PAGE_SIZE); 1575 return young; 1576 } 1577 1578 static inline bool pmdp_clear_flush_young_notify(struct vm_area_struct *vma, 1579 unsigned long addr, pmd_t *pmdp) 1580 { 1581 bool young; 1582 1583 young = pmdp_clear_flush_young(vma, addr, pmdp); 1584 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, addr + PMD_SIZE); 1585 return young; 1586 } 1587 1588 static inline bool test_and_clear_young_ptes_notify(struct vm_area_struct *vma, 1589 unsigned long addr, pte_t *ptep, unsigned int nr) 1590 { 1591 bool young; 1592 1593 young = test_and_clear_young_ptes(vma, addr, ptep, nr); 1594 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + nr * PAGE_SIZE); 1595 return young; 1596 } 1597 1598 static inline bool pmdp_test_and_clear_young_notify(struct vm_area_struct *vma, 1599 unsigned long addr, pmd_t *pmdp) 1600 { 1601 bool young; 1602 1603 young = pmdp_test_and_clear_young(vma, addr, pmdp); 1604 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + PMD_SIZE); 1605 return young; 1606 } 1607 1608 #else /* CONFIG_MMU_NOTIFIER */ 1609 1610 #define clear_flush_young_ptes_notify clear_flush_young_ptes 1611 #define pmdp_clear_flush_young_notify pmdp_clear_flush_young 1612 #define test_and_clear_young_ptes_notify test_and_clear_young_ptes 1613 #define pmdp_test_and_clear_young_notify pmdp_test_and_clear_young 1614 1615 #endif /* CONFIG_MMU_NOTIFIER */ 1616 1617 extern int sysctl_max_map_count; 1618 static inline int get_sysctl_max_map_count(void) 1619 { 1620 return READ_ONCE(sysctl_max_map_count); 1621 } 1622 1623 bool may_expand_vm(struct mm_struct *mm, const vma_flags_t *vma_flags, 1624 unsigned long npages); 1625 1626 static inline void mm_prepare_for_swap_entries(struct mm_struct *mm) 1627 { 1628 if (list_empty(&mm->mmlist)) { 1629 spin_lock(&mmlist_lock); 1630 if (list_empty(&mm->mmlist)) 1631 list_add(&mm->mmlist, &init_mm.mmlist); 1632 spin_unlock(&mmlist_lock); 1633 } 1634 } 1635 1636 static inline bool can_spin_trylock(void) 1637 { 1638 /* 1639 * In PREEMPT_RT spin_trylock() will call raw_spin_lock() which is 1640 * unsafe in NMI. If spin_trylock() is called from hard IRQ the current 1641 * task may be waiting for one rt_spin_lock, but rt_spin_trylock() will 1642 * mark the task as the owner of another rt_spin_lock which will 1643 * confuse PI logic, so return immediately if called from hard IRQ or 1644 * NMI. 1645 * 1646 * Note, irqs_disabled() case is ok. spin_trylock() can be called 1647 * from raw_spin_lock_irqsave region. 1648 */ 1649 if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq())) 1650 return false; 1651 1652 /* On UP, spin_trylock() always succeeds even when it is locked */ 1653 if (!IS_ENABLED(CONFIG_SMP) && in_nmi()) 1654 return false; 1655 1656 return true; 1657 } 1658 1659 /* char-mem.c */ 1660 bool file_is_dev_zero(const struct file *file); 1661 1662 #endif /* __MM_INTERNAL_H */ 1663