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 folios 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 updated the folio 598 * mapcount, and zap_empty_pte_table() clears the PMD only after the rmaps 599 * for the table have been removed, but nothing orders the pmd_none() read 600 * against later mapcount reads. Skipping the PMD without synchronization 601 * could make folio unmapping code assume that unmapping failed. 602 * 603 * Wait for concurrent zapping to complete by grabbing the PTL. 604 */ 605 static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) 606 { 607 spinlock_t *ptl = pmd_lock(mm, pmdp); 608 609 spin_unlock(ptl); 610 } 611 612 struct zap_details; 613 void zap_vma_range_batched(struct mmu_gather *tlb, 614 struct vm_area_struct *vma, unsigned long addr, 615 unsigned long size, struct zap_details *details); 616 int zap_vma_for_reaping(struct vm_area_struct *vma); 617 int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, 618 gfp_t gfp); 619 620 void page_cache_ra_order(struct readahead_control *, struct file_ra_state *); 621 void force_page_cache_ra(struct readahead_control *, unsigned long nr); 622 static inline void force_page_cache_readahead(struct address_space *mapping, 623 struct file *file, pgoff_t index, unsigned long nr_to_read) 624 { 625 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index); 626 force_page_cache_ra(&ractl, nr_to_read); 627 } 628 629 /* 630 * Take two page table locks at the same level in address order such that 631 * we cannot deadlock. 632 */ 633 static inline void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2) 634 __acquires(ptl1) 635 __acquires(ptl2) 636 { 637 if (ptl1 > ptl2) 638 swap(ptl1, ptl2); 639 spin_lock(ptl1); 640 if (ptl1 != ptl2) 641 spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING); 642 else 643 __acquire(ptl2); 644 } 645 646 static inline void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2) 647 __releases(ptl1) 648 __releases(ptl2) 649 { 650 spin_unlock(ptl1); 651 if (ptl1 != ptl2) 652 spin_unlock(ptl2); 653 else 654 __release(ptl2); 655 } 656 657 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, 658 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 659 unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, 660 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 661 int truncate_inode_folio(struct address_space *mapping, struct folio *folio); 662 bool truncate_inode_partial_folio(struct folio *folio, loff_t lstart, 663 loff_t lend, pgoff_t *pstart, pgoff_t *pend); 664 long mapping_evict_folio(struct address_space *mapping, struct folio *folio); 665 unsigned long mapping_try_invalidate(struct address_space *mapping, 666 pgoff_t start, pgoff_t end, unsigned long *nr_failed); 667 668 /** 669 * folio_evictable - Test whether a folio is evictable. 670 * @folio: The folio to test. 671 * 672 * Test whether @folio is evictable -- i.e., should be placed on 673 * active/inactive lists vs unevictable list. 674 * 675 * Reasons folio might not be evictable: 676 * 1. folio's mapping marked unevictable 677 * 2. One of the pages in the folio is part of an mlocked VMA 678 */ 679 static inline bool folio_evictable(struct folio *folio) 680 { 681 bool ret; 682 683 /* Prevent address_space of inode and swap cache from being freed */ 684 rcu_read_lock(); 685 ret = !mapping_unevictable(folio_mapping(folio)) && 686 !folio_test_mlocked(folio); 687 rcu_read_unlock(); 688 return ret; 689 } 690 691 /* 692 * Turn a non-refcounted page (->_refcount == 0) into refcounted with 693 * a count of one. 694 */ 695 static inline void set_page_refcounted(struct page *page) 696 { 697 VM_BUG_ON_PAGE(PageTail(page), page); 698 VM_BUG_ON_PAGE(page_ref_count(page), page); 699 set_page_count(page, 1); 700 } 701 702 static inline void set_pages_refcounted(struct page *page, unsigned long nr_pages) 703 { 704 unsigned long pfn = page_to_pfn(page); 705 706 for (; nr_pages--; pfn++) 707 set_page_refcounted(pfn_to_page(pfn)); 708 } 709 710 /* 711 * Return true if a folio needs ->release_folio() calling upon it. 712 */ 713 static inline bool folio_needs_release(struct folio *folio) 714 { 715 struct address_space *mapping = folio_mapping(folio); 716 717 return folio_has_private(folio) || 718 (mapping && mapping_release_always(mapping)); 719 } 720 721 extern unsigned long highest_memmap_pfn; 722 723 /* 724 * Maximum number of reclaim retries without progress before the OOM 725 * killer is consider the only way forward. 726 */ 727 #define MAX_RECLAIM_RETRIES 16 728 729 /* 730 * in mm/vmscan.c: 731 */ 732 bool folio_isolate_lru(struct folio *folio); 733 void folio_putback_lru(struct folio *folio); 734 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason); 735 int user_proactive_reclaim(char *buf, 736 struct mem_cgroup *memcg, pg_data_t *pgdat); 737 738 /* 739 * in mm/rmap.c: 740 */ 741 pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address); 742 743 /* 744 * in mm/khugepaged.c 745 */ 746 void set_recommended_min_free_kbytes(void); 747 748 /* 749 * in mm/page_alloc.c 750 */ 751 #define K(x) ((x) << (PAGE_SHIFT-10)) 752 753 extern char * const zone_names[MAX_NR_ZONES]; 754 755 extern int min_free_kbytes; 756 extern int defrag_mode; 757 758 void setup_per_zone_wmarks(void); 759 void calculate_min_free_kbytes(void); 760 int __meminit init_per_zone_wmark_min(void); 761 762 extern int __isolate_free_page(struct page *page, unsigned int order); 763 extern void __putback_isolated_page(struct page *page, unsigned int order, 764 int mt); 765 766 /* 767 * This will have no effect, other than possibly generating a warning, if the 768 * caller passes in a non-large folio. 769 */ 770 static inline void folio_set_order(struct folio *folio, unsigned int order) 771 { 772 if (WARN_ON_ONCE(!order || !folio_test_large(folio))) 773 return; 774 VM_WARN_ON_ONCE(order > MAX_FOLIO_ORDER); 775 776 folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order; 777 #ifdef NR_PAGES_IN_LARGE_FOLIO 778 folio->_nr_pages = 1U << order; 779 #endif 780 } 781 782 bool __folio_unqueue_deferred_split(struct folio *folio); 783 static inline bool folio_unqueue_deferred_split(struct folio *folio) 784 { 785 if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio)) 786 return false; 787 788 /* 789 * At this point, there is no one trying to add the folio to 790 * deferred_list. If folio is not in deferred_list, it's safe 791 * to check without acquiring the list_lru lock. 792 */ 793 if (data_race(list_empty(&folio->_deferred_list))) 794 return false; 795 796 return __folio_unqueue_deferred_split(folio); 797 } 798 799 static inline struct folio *page_rmappable_folio(struct page *page) 800 { 801 struct folio *folio = (struct folio *)page; 802 803 if (folio && folio_test_large(folio)) 804 folio_set_large_rmappable(folio); 805 return folio; 806 } 807 808 static inline void prep_compound_head(struct page *page, unsigned int order) 809 { 810 struct folio *folio = (struct folio *)page; 811 812 folio_set_order(folio, order); 813 atomic_set(&folio->_large_mapcount, -1); 814 if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) 815 atomic_set(&folio->_nr_pages_mapped, 0); 816 if (IS_ENABLED(CONFIG_MM_ID)) { 817 folio->_mm_ids = 0; 818 folio->_mm_id_mapcount[0] = -1; 819 folio->_mm_id_mapcount[1] = -1; 820 } 821 if (IS_ENABLED(CONFIG_64BIT) || order > 1) { 822 atomic_set(&folio->_pincount, 0); 823 atomic_set(&folio->_entire_mapcount, -1); 824 } 825 if (order > 1) 826 INIT_LIST_HEAD(&folio->_deferred_list); 827 } 828 829 static inline void prep_compound_tail(struct page *tail, 830 const struct page *head, unsigned int order) 831 { 832 tail->mapping = TAIL_MAPPING; 833 set_compound_head(tail, head, order); 834 VM_WARN_ON_ONCE(tail->private); 835 } 836 837 void __prep_compound_page(struct page *page, unsigned int order); 838 839 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 840 841 /* 842 * in mm/compaction.c 843 */ 844 /* 845 * compact_control is used to track pages being migrated and the free pages 846 * they are being migrated to during memory compaction. The free_pfn starts 847 * at the end of a zone and migrate_pfn begins at the start. Movable pages 848 * are moved to the end of a zone during a compaction run and the run 849 * completes when free_pfn <= migrate_pfn 850 */ 851 struct compact_control { 852 struct list_head freepages[NR_PAGE_ORDERS]; /* List of free pages to migrate to */ 853 struct list_head migratepages; /* List of pages being migrated */ 854 unsigned int nr_freepages; /* Number of isolated free pages */ 855 unsigned int nr_migratepages; /* Number of pages to migrate */ 856 unsigned long free_pfn; /* isolate_freepages search base */ 857 /* 858 * Acts as an in/out parameter to page isolation for migration. 859 * isolate_migratepages uses it as a search base. 860 * isolate_migratepages_block will update the value to the next pfn 861 * after the last isolated one. 862 */ 863 unsigned long migrate_pfn; 864 unsigned long fast_start_pfn; /* a pfn to start linear scan from */ 865 struct zone *zone; 866 unsigned long total_migrate_scanned; 867 unsigned long total_free_scanned; 868 unsigned short fast_search_fail;/* failures to use free list searches */ 869 short search_order; /* order to start a fast search at */ 870 const gfp_t gfp_mask; /* gfp mask of a direct compactor */ 871 int order; /* order a direct compactor needs */ 872 int migratetype; /* migratetype of direct compactor */ 873 const unsigned int alloc_flags; /* alloc flags of a direct compactor */ 874 const int highest_zoneidx; /* zone index of a direct compactor */ 875 enum migrate_mode mode; /* Async or sync migration mode */ 876 bool ignore_skip_hint; /* Scan blocks even if marked skip */ 877 bool no_set_skip_hint; /* Don't mark blocks for skipping */ 878 bool ignore_block_suitable; /* Scan blocks considered unsuitable */ 879 bool direct_compaction; /* False from kcompactd or /proc/... */ 880 bool proactive_compaction; /* kcompactd proactive compaction */ 881 bool whole_zone; /* Whole zone should/has been scanned */ 882 bool contended; /* Signal lock contention */ 883 bool finish_pageblock; /* Scan the remainder of a pageblock. Used 884 * when there are potentially transient 885 * isolation or migration failures to 886 * ensure forward progress. 887 */ 888 bool alloc_contig; /* alloc_contig_range allocation */ 889 }; 890 891 /* 892 * Used in direct compaction when a page should be taken from the freelists 893 * immediately when one is created during the free path. 894 */ 895 struct capture_control { 896 struct zone *zone; 897 int migratetype; 898 /* 899 * Allocation request order. May differ from the compaction 900 * order: defrag_mode promotes sub-block allocations to 901 * pageblock-order compaction; capture still matches at the 902 * original allocation order so prep_new_page() is consistent. 903 */ 904 int order; 905 struct page *page; 906 }; 907 908 unsigned long 909 isolate_freepages_range(struct compact_control *cc, 910 unsigned long start_pfn, unsigned long end_pfn); 911 int 912 isolate_migratepages_range(struct compact_control *cc, 913 unsigned long low_pfn, unsigned long end_pfn); 914 915 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 916 917 struct cma; 918 919 #ifdef CONFIG_CMA 920 bool cma_validate_zones(struct cma *cma); 921 void *cma_reserve_early(struct cma *cma, unsigned long size); 922 #else 923 static inline bool cma_validate_zones(struct cma *cma) 924 { 925 return false; 926 } 927 static inline void *cma_reserve_early(struct cma *cma, unsigned long size) 928 { 929 return NULL; 930 } 931 #endif 932 933 /* mm/util.c */ 934 struct anon_vma *folio_anon_vma(const struct folio *folio); 935 936 #ifdef CONFIG_MMU 937 void unmap_mapping_folio(struct folio *folio); 938 extern long populate_vma_page_range(struct vm_area_struct *vma, 939 unsigned long start, unsigned long end, int *locked); 940 extern long faultin_page_range(struct mm_struct *mm, unsigned long start, 941 unsigned long end, bool write, int *locked); 942 bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked, 943 unsigned long bytes); 944 945 /* 946 * NOTE: This function can't tell whether the folio is "fully mapped" in the 947 * range. 948 * "fully mapped" means all the pages of folio is associated with the page 949 * table of range while this function just check whether the folio range is 950 * within the range [start, end). Function caller needs to do page table 951 * check if it cares about the page table association. 952 * 953 * Typical usage (like mlock or madvise) is: 954 * Caller knows at least 1 page of folio is associated with page table of VMA 955 * and the range [start, end) is intersect with the VMA range. Caller wants 956 * to know whether the folio is fully associated with the range. It calls 957 * this function to check whether the folio is in the range first. Then checks 958 * the page table to know whether the folio is fully mapped to the range. 959 */ 960 static inline bool 961 folio_within_range(struct folio *folio, struct vm_area_struct *vma, 962 unsigned long start, unsigned long end) 963 { 964 const unsigned long vma_pglen = vma_pages(vma); 965 pgoff_t pgoff_folio, pgoff_vma_start; 966 unsigned long addr; 967 968 VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio); 969 if (start > end) 970 return false; 971 972 pgoff_folio = folio_pgoff(folio); 973 pgoff_vma_start = folio_test_anon(folio) ? 974 vma_start_anon_pgoff(vma) : vma_start_pgoff(vma); 975 976 if (start < vma->vm_start) 977 start = vma->vm_start; 978 979 if (end > vma->vm_end) 980 end = vma->vm_end; 981 982 /* if folio start address is not in vma range */ 983 if (!in_range(pgoff_folio, pgoff_vma_start, vma_pglen)) 984 return false; 985 986 addr = vma->vm_start + ((pgoff_folio - pgoff_vma_start) << PAGE_SHIFT); 987 988 return !(addr < start || end - addr < folio_size(folio)); 989 } 990 991 static inline bool 992 folio_within_vma(struct folio *folio, struct vm_area_struct *vma) 993 { 994 return folio_within_range(folio, vma, vma->vm_start, vma->vm_end); 995 } 996 997 /* 998 * mlock_vma_folio() and munlock_vma_folio(): 999 * should be called with vma's mmap_lock held for read or write, 1000 * under page table lock for the pte/pmd being added or removed. 1001 * 1002 * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at 1003 * the end of folio_remove_rmap_*(); but new anon folios are managed by 1004 * folio_add_lru_vma() calling mlock_new_folio(). 1005 */ 1006 void mlock_folio(struct folio *folio); 1007 static inline void mlock_vma_folio(struct folio *folio, 1008 struct vm_area_struct *vma) 1009 { 1010 if (vma_test(vma, VMA_LOCKED_BIT)) 1011 mlock_folio(folio); 1012 } 1013 1014 void munlock_folio(struct folio *folio); 1015 static inline void munlock_vma_folio(struct folio *folio, 1016 struct vm_area_struct *vma) 1017 { 1018 /* 1019 * munlock if the function is called. Ideally, we should only 1020 * do munlock if any page of folio is unmapped from VMA and 1021 * cause folio not fully mapped to VMA. 1022 * 1023 * But it's not easy to confirm that's the situation. So we 1024 * always munlock the folio and page reclaim will correct it 1025 * if it's wrong. 1026 */ 1027 /* 1028 * VMA_LOCKONFAULT_BIT alone marks an mlock walk in progress, see 1029 * mlock_vma_pages_range(). An unmap racing with the walk must still 1030 * munlock folios the walk has already counted. 1031 */ 1032 if (unlikely(vma_test_any_mask(vma, VMA_LOCKED_MASK))) 1033 munlock_folio(folio); 1034 } 1035 1036 void mlock_new_folio(struct folio *folio); 1037 bool need_mlock_drain(int cpu); 1038 void mlock_drain_local(void); 1039 void mlock_drain_remote(int cpu); 1040 1041 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); 1042 1043 static inline unsigned long __vma_address(const struct vm_area_struct *vma, 1044 pgoff_t pgoff, pgoff_t pgoff_start, unsigned long nr_pages) 1045 { 1046 unsigned long address; 1047 1048 if (pgoff >= pgoff_start) { 1049 address = vma->vm_start + 1050 ((pgoff - pgoff_start) << PAGE_SHIFT); 1051 /* Check for address beyond vma (or wrapped through 0?) */ 1052 if (address < vma->vm_start || address >= vma->vm_end) 1053 address = -EFAULT; 1054 } else if (pgoff + nr_pages - 1 >= pgoff_start) { 1055 /* Test above avoids possibility of wrap to 0 on 32-bit */ 1056 address = vma->vm_start; 1057 } else { 1058 address = -EFAULT; 1059 } 1060 return address; 1061 } 1062 1063 /** 1064 * vma_filebacked_address - Find the virtual address a file-backed page range is 1065 * mapped at. 1066 * @vma: The vma which maps this object. 1067 * @pgoff: The page offset within its object. 1068 * @nr_pages: The number of pages to consider. 1069 * 1070 * Returns: If any page in this range is mapped by this VMA, return the first 1071 * address where any of these pages appear. Otherwise, return -EFAULT. 1072 */ 1073 static inline unsigned long vma_filebacked_address(const struct vm_area_struct *vma, 1074 pgoff_t pgoff, unsigned long nr_pages) 1075 { 1076 VM_WARN_ON_ONCE(vma_is_anonymous(vma)); 1077 1078 return __vma_address(vma, pgoff, vma_start_pgoff(vma), nr_pages); 1079 } 1080 1081 /** 1082 * vma_anon_address - Find the virtual address an anonymous page range is mapped 1083 * at. 1084 * @vma: The vma which maps this object. 1085 * @pgoff_anon: The anonymous page index belonging to the folio. 1086 * @nr_pages: The number of pages to consider. 1087 * 1088 * This is only valid for anonymous or MAP_PRIVATE-mapped file-backed VMAs. 1089 * 1090 * Returns: If any page in this range is mapped by this VMA, return the first 1091 * address where any of these pages appear. Otherwise, return -EFAULT. 1092 */ 1093 static inline unsigned long vma_anon_address(const struct vm_area_struct *vma, 1094 pgoff_t pgoff_anon, unsigned long nr_pages) 1095 { 1096 VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma)); 1097 1098 return __vma_address(vma, pgoff_anon, vma_start_anon_pgoff(vma), nr_pages); 1099 } 1100 1101 /* 1102 * At what user virtual address will none of the range be found in vma? 1103 * Assumes that vma_address() already returned a good starting address. 1104 */ 1105 static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw) 1106 { 1107 const pgoff_t pgoff_end = pvmw->pgoff + pvmw->nr_pages; 1108 const struct vm_area_struct *vma = pvmw->vma; 1109 pgoff_t pgoff_vma_start; 1110 unsigned long address; 1111 1112 /* Common case, plus ->pgoff is invalid for KSM */ 1113 if (pvmw->nr_pages == 1) 1114 return pvmw->address + PAGE_SIZE; 1115 1116 if (pvmw->pgoff_is_anon) 1117 pgoff_vma_start = vma_start_anon_pgoff(vma); 1118 else 1119 pgoff_vma_start = vma_start_pgoff(vma); 1120 1121 address = vma->vm_start + 1122 ((pgoff_end - pgoff_vma_start) << PAGE_SHIFT); 1123 /* Check for address beyond vma (or wrapped through 0?) */ 1124 if (address < vma->vm_start || address > vma->vm_end) 1125 address = vma->vm_end; 1126 return address; 1127 } 1128 1129 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, 1130 struct file *fpin) 1131 { 1132 int flags = vmf->flags; 1133 1134 if (fpin) 1135 return fpin; 1136 1137 /* 1138 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or 1139 * anything, so we only pin the file and drop the mmap_lock if only 1140 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. 1141 */ 1142 if (fault_flag_allow_retry_first(flags) && 1143 !(flags & FAULT_FLAG_RETRY_NOWAIT)) { 1144 fpin = get_file(vmf->vma->vm_file); 1145 release_fault_lock(vmf); 1146 } 1147 return fpin; 1148 } 1149 1150 static inline bool vma_supports_mlock(const struct vm_area_struct *vma) 1151 { 1152 if (!vma_is_mm_backed(vma)) 1153 return false; 1154 /* Core mm may discard droppable memory at any time. */ 1155 if (vma_test_single_mask(vma, VMA_DROPPABLE)) 1156 return false; 1157 if (vma_is_dax(vma) || vma_is_hugetlb(vma)) 1158 return false; 1159 return vma != get_gate_vma(current->mm); 1160 } 1161 1162 #else /* !CONFIG_MMU */ 1163 static inline void unmap_mapping_folio(struct folio *folio) { } 1164 static inline void mlock_new_folio(struct folio *folio) { } 1165 static inline bool need_mlock_drain(int cpu) { return false; } 1166 static inline void mlock_drain_local(void) { } 1167 static inline void mlock_drain_remote(int cpu) { } 1168 #endif /* !CONFIG_MMU */ 1169 1170 #ifdef CONFIG_NUMA 1171 extern int node_reclaim_mode; 1172 1173 extern unsigned long node_reclaim(struct pglist_data *pgdat, 1174 gfp_t gfp_mask, unsigned int order); 1175 int find_next_best_node_in(int node, nodemask_t *used_node_mask, 1176 const nodemask_t *candidates); 1177 #else 1178 #define node_reclaim_mode 0 1179 1180 static inline unsigned long node_reclaim(struct pglist_data *pgdat, 1181 gfp_t mask, unsigned int order) 1182 { 1183 return 0; 1184 } 1185 static inline int find_next_best_node_in(int node, nodemask_t *used_node_mask, 1186 const nodemask_t *candidates) 1187 { 1188 return NUMA_NO_NODE; 1189 } 1190 #endif 1191 1192 static inline bool node_reclaim_enabled(void) 1193 { 1194 /* Is any node_reclaim_mode bit set? */ 1195 return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP); 1196 } 1197 1198 /* 1199 * mm/memory-failure.c 1200 */ 1201 #ifdef CONFIG_MEMORY_FAILURE 1202 int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill); 1203 void shake_folio(struct folio *folio); 1204 typedef int hwpoison_filter_func_t(struct page *p); 1205 void hwpoison_filter_register(hwpoison_filter_func_t *filter); 1206 void hwpoison_filter_unregister(void); 1207 1208 #define MAGIC_HWPOISON 0x48575053U /* HWPS */ 1209 void SetPageHWPoisonTakenOff(struct page *page); 1210 void ClearPageHWPoisonTakenOff(struct page *page); 1211 bool take_page_off_buddy(struct page *page); 1212 bool put_page_back_buddy(struct page *page); 1213 struct task_struct *task_early_kill(struct task_struct *tsk, int force_early); 1214 void add_to_kill_ksm(struct task_struct *tsk, const struct page *p, 1215 struct vm_area_struct *vma, struct list_head *to_kill, 1216 unsigned long ksm_addr); 1217 unsigned long page_mapped_in_vma(const struct page *page, 1218 struct vm_area_struct *vma); 1219 1220 #else 1221 static inline int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill) 1222 { 1223 return -EBUSY; 1224 } 1225 #endif 1226 1227 extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long, 1228 unsigned long, unsigned long, 1229 unsigned long, unsigned long); 1230 1231 unsigned long reclaim_pages(struct list_head *folio_list); 1232 unsigned int reclaim_clean_pages_from_list(struct zone *zone, 1233 struct list_head *folio_list); 1234 1235 enum ttu_flags; 1236 struct tlbflush_unmap_batch; 1237 1238 1239 /* 1240 * only for MM internal work items which do not depend on 1241 * any allocations or locks which might depend on allocations 1242 */ 1243 extern struct workqueue_struct *mm_percpu_wq; 1244 1245 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1246 void try_to_unmap_flush(void); 1247 void try_to_unmap_flush_dirty(void); 1248 void flush_tlb_batched_pending(struct mm_struct *mm); 1249 #else 1250 static inline void try_to_unmap_flush(void) 1251 { 1252 } 1253 static inline void try_to_unmap_flush_dirty(void) 1254 { 1255 } 1256 static inline void flush_tlb_batched_pending(struct mm_struct *mm) 1257 { 1258 } 1259 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ 1260 1261 extern const struct trace_print_flags pageflag_names[]; 1262 extern const struct trace_print_flags vmaflag_names[]; 1263 extern const struct trace_print_flags gfpflag_names[]; 1264 1265 void setup_zone_pageset(struct zone *zone); 1266 1267 struct migration_target_control { 1268 int nid; /* preferred node id */ 1269 nodemask_t *nmask; 1270 gfp_t gfp_mask; 1271 enum migrate_reason reason; 1272 }; 1273 1274 /* 1275 * mm/filemap.c 1276 */ 1277 ssize_t splice_folio_into_pipe(struct pipe_inode_info *pipe, 1278 struct folio *folio, loff_t fpos, size_t size); 1279 1280 static inline bool vma_is_single_threaded_private(struct vm_area_struct *vma) 1281 { 1282 if (vma->vm_flags & VM_SHARED) 1283 return false; 1284 1285 return atomic_read(&vma->vm_mm->mm_users) == 1; 1286 } 1287 1288 #ifdef CONFIG_NUMA_BALANCING 1289 bool folio_can_map_prot_numa(struct folio *folio, struct vm_area_struct *vma, 1290 bool is_private_single_threaded); 1291 1292 #else 1293 static inline bool folio_can_map_prot_numa(struct folio *folio, 1294 struct vm_area_struct *vma, bool is_private_single_threaded) 1295 { 1296 return false; 1297 } 1298 #endif 1299 1300 int numa_migrate_check(struct folio *folio, struct vm_fault *vmf, 1301 unsigned long addr, int *flags, bool writable, 1302 int *last_cpupid); 1303 1304 void free_zone_device_folio(struct folio *folio); 1305 int migrate_device_coherent_folio(struct folio *folio); 1306 1307 /* 1308 * mm/gup.c 1309 */ 1310 int __must_check try_grab_folio(struct folio *folio, int refs, 1311 unsigned int flags); 1312 1313 /* 1314 * mm/huge_memory.c 1315 */ 1316 void touch_pud(struct vm_area_struct *vma, unsigned long addr, 1317 pud_t *pud, bool write); 1318 bool touch_pmd(struct vm_area_struct *vma, unsigned long addr, 1319 pmd_t *pmd, bool write); 1320 1321 /* 1322 * Parses a string with mem suffixes into its order. Useful to parse kernel 1323 * parameters. 1324 */ 1325 static inline int get_order_from_str(const char *size_str, 1326 unsigned long valid_orders) 1327 { 1328 unsigned long size; 1329 char *endptr; 1330 int order; 1331 1332 size = memparse(size_str, &endptr); 1333 1334 if (!is_power_of_2(size)) 1335 return -EINVAL; 1336 order = get_order(size); 1337 if (BIT(order) & ~valid_orders) 1338 return -EINVAL; 1339 1340 return order; 1341 } 1342 1343 enum { 1344 /* mark page accessed */ 1345 FOLL_TOUCH = 1 << 16, 1346 /* a retry, previous pass started an IO */ 1347 FOLL_TRIED = 1 << 17, 1348 /* we are working on non-current tsk/mm */ 1349 FOLL_REMOTE = 1 << 18, 1350 /* pages must be released via unpin_user_page */ 1351 FOLL_PIN = 1 << 19, 1352 /* gup_fast: prevent fall-back to slow gup */ 1353 FOLL_FAST_ONLY = 1 << 20, 1354 /* allow unlocking the mmap lock */ 1355 FOLL_UNLOCKABLE = 1 << 21, 1356 /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ 1357 FOLL_MADV_POPULATE = 1 << 22, 1358 }; 1359 1360 #define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ 1361 FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ 1362 FOLL_MADV_POPULATE) 1363 1364 /* 1365 * Indicates for which pages that are write-protected in the page table, 1366 * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the 1367 * GUP pin will remain consistent with the pages mapped into the page tables 1368 * of the MM. 1369 * 1370 * Temporary unmapping of PageAnonExclusive() pages or clearing of 1371 * PageAnonExclusive() has to protect against concurrent GUP: 1372 * * Ordinary GUP: Using the PT lock 1373 * * GUP-fast and fork(): mm->write_protect_seq 1374 * * GUP-fast and KSM or temporary unmapping (swap, migration): see 1375 * folio_try_share_anon_rmap_*() 1376 * 1377 * Must be called with the (sub)page that's actually referenced via the 1378 * page table entry, which might not necessarily be the head page for a 1379 * PTE-mapped THP. 1380 * 1381 * If the vma is NULL, we're coming from the GUP-fast path and might have 1382 * to fallback to the slow path just to lookup the vma. 1383 */ 1384 static inline bool gup_must_unshare(struct vm_area_struct *vma, 1385 unsigned int flags, struct page *page) 1386 { 1387 /* 1388 * FOLL_WRITE is implicitly handled correctly as the page table entry 1389 * has to be writable -- and if it references (part of) an anonymous 1390 * folio, that part is required to be marked exclusive. 1391 */ 1392 if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN) 1393 return false; 1394 /* 1395 * Note: PageAnon(page) is stable until the page is actually getting 1396 * freed. 1397 */ 1398 if (!PageAnon(page)) { 1399 /* 1400 * We only care about R/O long-term pining: R/O short-term 1401 * pinning does not have the semantics to observe successive 1402 * changes through the process page tables. 1403 */ 1404 if (!(flags & FOLL_LONGTERM)) 1405 return false; 1406 1407 /* We really need the vma ... */ 1408 if (!vma) 1409 return true; 1410 1411 /* 1412 * ... because we only care about writable private ("COW") 1413 * mappings where we have to break COW early. 1414 */ 1415 return vma_is_cow_mapping(vma); 1416 } 1417 1418 /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */ 1419 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) 1420 smp_rmb(); 1421 1422 /* 1423 * Note that KSM pages cannot be exclusive, and consequently, 1424 * cannot get pinned. 1425 */ 1426 return !PageAnonExclusive(page); 1427 } 1428 1429 1430 static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) 1431 { 1432 /* 1433 * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty 1434 * enablements, because when without soft-dirty being compiled in, 1435 * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) 1436 * will be constantly true. 1437 */ 1438 if (!pgtable_supports_soft_dirty()) 1439 return false; 1440 1441 /* 1442 * Soft-dirty is kind of special: its tracking is enabled when the 1443 * vma flags not set. 1444 */ 1445 return !(vma->vm_flags & VM_SOFTDIRTY); 1446 } 1447 1448 static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd) 1449 { 1450 return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd); 1451 } 1452 1453 static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte) 1454 { 1455 return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte); 1456 } 1457 1458 /* shrinker related functions */ 1459 unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg, 1460 int priority); 1461 1462 int shmem_add_to_page_cache(struct folio *folio, 1463 struct address_space *mapping, 1464 pgoff_t index, void *expected, gfp_t gfp); 1465 int shmem_inode_acct_blocks(struct inode *inode, long pages); 1466 bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped); 1467 1468 #ifdef CONFIG_SHRINKER_DEBUG 1469 static inline __printf(2, 0) int shrinker_debugfs_name_alloc( 1470 struct shrinker *shrinker, const char *fmt, va_list ap) 1471 { 1472 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap); 1473 1474 return shrinker->name ? 0 : -ENOMEM; 1475 } 1476 1477 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1478 { 1479 kfree_const(shrinker->name); 1480 shrinker->name = NULL; 1481 } 1482 1483 extern int shrinker_debugfs_add(struct shrinker *shrinker); 1484 extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1485 int *debugfs_id); 1486 extern void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1487 int debugfs_id); 1488 #else /* CONFIG_SHRINKER_DEBUG */ 1489 static inline int shrinker_debugfs_add(struct shrinker *shrinker) 1490 { 1491 return 0; 1492 } 1493 static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker, 1494 const char *fmt, va_list ap) 1495 { 1496 return 0; 1497 } 1498 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1499 { 1500 } 1501 static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1502 int *debugfs_id) 1503 { 1504 *debugfs_id = -1; 1505 return NULL; 1506 } 1507 static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1508 int debugfs_id) 1509 { 1510 } 1511 #endif /* CONFIG_SHRINKER_DEBUG */ 1512 1513 /* Only track the nodes of mappings with shadow entries */ 1514 void workingset_update_node(struct xa_node *node); 1515 extern struct list_lru shadow_nodes; 1516 #define mapping_set_update(xas, mapping) do { \ 1517 if (!dax_mapping(mapping) && !shmem_mapping(mapping)) { \ 1518 xas_set_update(xas, workingset_update_node); \ 1519 xas_set_lru(xas, &shadow_nodes); \ 1520 } \ 1521 } while (0) 1522 1523 /* mremap.c */ 1524 unsigned long move_page_tables(struct pagetable_move_control *pmc); 1525 1526 #ifdef CONFIG_UNACCEPTED_MEMORY 1527 void accept_page(struct page *page); 1528 #else /* CONFIG_UNACCEPTED_MEMORY */ 1529 static inline void accept_page(struct page *page) 1530 { 1531 } 1532 #endif /* CONFIG_UNACCEPTED_MEMORY */ 1533 1534 /* pagewalk.c */ 1535 int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start, 1536 unsigned long end, const struct mm_walk_ops *ops, 1537 void *private); 1538 int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start, 1539 unsigned long end, const struct mm_walk_ops *ops, 1540 void *private); 1541 int walk_page_range_debug(struct mm_struct *mm, unsigned long start, 1542 unsigned long end, const struct mm_walk_ops *ops, 1543 pgd_t *pgd, void *private); 1544 1545 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm); 1546 int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm); 1547 1548 int remap_pfn_range_prepare(struct vm_area_desc *desc); 1549 int remap_pfn_range_complete(struct vm_area_struct *vma, 1550 struct mmap_action *action); 1551 int simple_ioremap_prepare(struct vm_area_desc *desc); 1552 int map_kernel_pages_prepare(struct vm_area_desc *desc); 1553 int map_kernel_pages_complete(struct vm_area_struct *vma, 1554 struct mmap_action *action); 1555 int map_discontig_kernel_pages_prepare(struct vm_area_desc *desc); 1556 int map_discontig_kernel_pages_complete(struct vm_area_struct *vma, 1557 struct mmap_action *action); 1558 1559 static inline int io_remap_pfn_range_prepare(struct vm_area_desc *desc) 1560 { 1561 struct mmap_action *action = &desc->action; 1562 const unsigned long orig_pfn = action->remap.start_pfn; 1563 const pgprot_t orig_pgprot = action->remap.pgprot; 1564 const unsigned long size = action->remap.size; 1565 const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size); 1566 int err; 1567 1568 action->remap.start_pfn = pfn; 1569 action->remap.pgprot = pgprot_decrypted(orig_pgprot); 1570 err = remap_pfn_range_prepare(desc); 1571 if (err) 1572 return err; 1573 1574 /* Remap does the actual work. */ 1575 action->type = MMAP_REMAP_PFN; 1576 return 0; 1577 } 1578 1579 /* 1580 * When we succeed an mmap action or just before we unmap a VMA on error, we 1581 * need to ensure any rmap lock held is released. On unmap it's required to 1582 * avoid a deadlock. 1583 */ 1584 static inline void maybe_rmap_unlock_action(struct vm_area_struct *vma, 1585 struct mmap_action *action) 1586 { 1587 struct file *file; 1588 1589 if (!action->hide_from_rmap_until_complete) 1590 return; 1591 1592 VM_WARN_ON_ONCE(vma_is_anonymous(vma)); 1593 file = vma->vm_file; 1594 i_mmap_unlock_write(file->f_mapping); 1595 action->hide_from_rmap_until_complete = false; 1596 } 1597 1598 #ifdef CONFIG_MMU_NOTIFIER 1599 static inline bool clear_flush_young_ptes_notify(struct vm_area_struct *vma, 1600 unsigned long addr, pte_t *ptep, unsigned int nr) 1601 { 1602 bool young; 1603 1604 young = clear_flush_young_ptes(vma, addr, ptep, nr); 1605 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, 1606 addr + nr * PAGE_SIZE); 1607 return young; 1608 } 1609 1610 static inline bool pmdp_clear_flush_young_notify(struct vm_area_struct *vma, 1611 unsigned long addr, pmd_t *pmdp) 1612 { 1613 bool young; 1614 1615 young = pmdp_clear_flush_young(vma, addr, pmdp); 1616 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, addr + PMD_SIZE); 1617 return young; 1618 } 1619 1620 static inline bool test_and_clear_young_ptes_notify(struct vm_area_struct *vma, 1621 unsigned long addr, pte_t *ptep, unsigned int nr) 1622 { 1623 bool young; 1624 1625 young = test_and_clear_young_ptes(vma, addr, ptep, nr); 1626 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + nr * PAGE_SIZE); 1627 return young; 1628 } 1629 1630 static inline bool pmdp_test_and_clear_young_notify(struct vm_area_struct *vma, 1631 unsigned long addr, pmd_t *pmdp) 1632 { 1633 bool young; 1634 1635 young = pmdp_test_and_clear_young(vma, addr, pmdp); 1636 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + PMD_SIZE); 1637 return young; 1638 } 1639 1640 #else /* CONFIG_MMU_NOTIFIER */ 1641 1642 #define clear_flush_young_ptes_notify clear_flush_young_ptes 1643 #define pmdp_clear_flush_young_notify pmdp_clear_flush_young 1644 #define test_and_clear_young_ptes_notify test_and_clear_young_ptes 1645 #define pmdp_test_and_clear_young_notify pmdp_test_and_clear_young 1646 1647 #endif /* CONFIG_MMU_NOTIFIER */ 1648 1649 extern int sysctl_max_map_count; 1650 static inline int get_sysctl_max_map_count(void) 1651 { 1652 return READ_ONCE(sysctl_max_map_count); 1653 } 1654 1655 bool may_expand_vm(struct mm_struct *mm, const vma_flags_t *vma_flags, 1656 unsigned long npages); 1657 1658 static inline void mm_prepare_for_swap_entries(struct mm_struct *mm) 1659 { 1660 if (list_empty(&mm->mmlist)) { 1661 spin_lock(&mmlist_lock); 1662 if (list_empty(&mm->mmlist)) 1663 list_add(&mm->mmlist, &init_mm.mmlist); 1664 spin_unlock(&mmlist_lock); 1665 } 1666 } 1667 1668 static inline bool can_spin_trylock(void) 1669 { 1670 /* 1671 * In PREEMPT_RT spin_trylock() will call raw_spin_lock() which is 1672 * unsafe in NMI. If spin_trylock() is called from hard IRQ the current 1673 * task may be waiting for one rt_spin_lock, but rt_spin_trylock() will 1674 * mark the task as the owner of another rt_spin_lock which will 1675 * confuse PI logic, so return immediately if called from hard IRQ or 1676 * NMI. 1677 * 1678 * Note, irqs_disabled() case is ok. spin_trylock() can be called 1679 * from raw_spin_lock_irqsave region. 1680 */ 1681 if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq())) 1682 return false; 1683 1684 /* On UP, spin_trylock() always succeeds even when it is locked */ 1685 if (!IS_ENABLED(CONFIG_SMP) && in_nmi()) 1686 return false; 1687 1688 return true; 1689 } 1690 1691 /* char-mem.c */ 1692 bool file_is_dev_zero(const struct file *file); 1693 1694 #endif /* __MM_INTERNAL_H */ 1695