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