1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* internal.h: mm/ internal definitions 3 * 4 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 #ifndef __MM_INTERNAL_H 8 #define __MM_INTERNAL_H 9 10 #include <linux/fs.h> 11 #include <linux/khugepaged.h> 12 #include <linux/mm.h> 13 #include <linux/mm_inline.h> 14 #include <linux/mmu_notifier.h> 15 #include <linux/pagemap.h> 16 #include <linux/pagewalk.h> 17 #include <linux/rmap.h> 18 #include <linux/swap.h> 19 #include <linux/leafops.h> 20 #include <linux/swap_cgroup.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 /* 29 * Maintains state across a page table move. The operation assumes both source 30 * and destination VMAs already exist and are specified by the user. 31 * 32 * Partial moves are permitted, but the old and new ranges must both reside 33 * within a VMA. 34 * 35 * mmap lock must be held in write and VMA write locks must be held on any VMA 36 * that is visible. 37 * 38 * Use the PAGETABLE_MOVE() macro to initialise this struct. 39 * 40 * The old_addr and new_addr fields are updated as the page table move is 41 * executed. 42 * 43 * NOTE: The page table move is affected by reading from [old_addr, old_end), 44 * and old_addr may be updated for better page table alignment, so len_in 45 * represents the length of the range being copied as specified by the user. 46 */ 47 struct pagetable_move_control { 48 struct vm_area_struct *old; /* Source VMA. */ 49 struct vm_area_struct *new; /* Destination VMA. */ 50 unsigned long old_addr; /* Address from which the move begins. */ 51 unsigned long old_end; /* Exclusive address at which old range ends. */ 52 unsigned long new_addr; /* Address to move page tables to. */ 53 unsigned long len_in; /* Bytes to remap specified by user. */ 54 55 bool need_rmap_locks; /* Do rmap locks need to be taken? */ 56 bool for_stack; /* Is this an early temp stack being moved? */ 57 }; 58 59 #define PAGETABLE_MOVE(name, old_, new_, old_addr_, new_addr_, len_) \ 60 struct pagetable_move_control name = { \ 61 .old = old_, \ 62 .new = new_, \ 63 .old_addr = old_addr_, \ 64 .old_end = (old_addr_) + (len_), \ 65 .new_addr = new_addr_, \ 66 .len_in = len_, \ 67 } 68 69 /* 70 * The set of flags that only affect watermark checking and reclaim 71 * behaviour. This is used by the MM to obey the caller constraints 72 * about IO, FS and watermark checking while ignoring placement 73 * hints such as HIGHMEM usage. 74 */ 75 #define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\ 76 __GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\ 77 __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\ 78 __GFP_NOLOCKDEP) 79 80 /* The GFP flags allowed during early boot */ 81 #define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS)) 82 83 /* Control allocation cpuset and node placement constraints */ 84 #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE) 85 86 /* Do not use these with a slab allocator */ 87 #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK) 88 89 /* 90 * Different from WARN_ON_ONCE(), no warning will be issued 91 * when we specify __GFP_NOWARN. 92 */ 93 #define WARN_ON_ONCE_GFP(cond, gfp) ({ \ 94 static bool __section(".data..once") __warned; \ 95 int __ret_warn_once = !!(cond); \ 96 \ 97 if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \ 98 __warned = true; \ 99 WARN_ON(1); \ 100 } \ 101 unlikely(__ret_warn_once); \ 102 }) 103 104 void page_writeback_init(void); 105 106 /* 107 * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages, 108 * its nr_pages_mapped would be 0x400000: choose the ENTIRELY_MAPPED bit 109 * above that range, instead of 2*(PMD_SIZE/PAGE_SIZE). Hugetlb currently 110 * leaves nr_pages_mapped at 0, but avoid surprise if it participates later. 111 */ 112 #define ENTIRELY_MAPPED 0x800000 113 #define FOLIO_PAGES_MAPPED (ENTIRELY_MAPPED - 1) 114 115 /* 116 * Flags passed to __show_mem() and show_free_areas() to suppress output in 117 * various contexts. 118 */ 119 #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */ 120 121 /* 122 * How many individual pages have an elevated _mapcount. Excludes 123 * the folio's entire_mapcount. 124 * 125 * Don't use this function outside of debugging code. 126 */ 127 static inline int folio_nr_pages_mapped(const struct folio *folio) 128 { 129 if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) 130 return -1; 131 return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED; 132 } 133 134 /* 135 * Retrieve the first entry of a folio based on a provided entry within the 136 * folio. We cannot rely on folio->swap as there is no guarantee that it has 137 * been initialized. Used for calling arch_swap_restore() 138 */ 139 static inline swp_entry_t folio_swap(swp_entry_t entry, 140 const struct folio *folio) 141 { 142 swp_entry_t swap = { 143 .val = ALIGN_DOWN(entry.val, folio_nr_pages(folio)), 144 }; 145 146 return swap; 147 } 148 149 static inline void *folio_raw_mapping(const struct folio *folio) 150 { 151 unsigned long mapping = (unsigned long)folio->mapping; 152 153 return (void *)(mapping & ~FOLIO_MAPPING_FLAGS); 154 } 155 156 /* 157 * This is a file-backed mapping, and is about to be memory mapped - invoke its 158 * mmap hook and safely handle error conditions. On error, VMA hooks will be 159 * mutated. 160 * 161 * @file: File which backs the mapping. 162 * @vma: VMA which we are mapping. 163 * 164 * Returns: 0 if success, error otherwise. 165 */ 166 static inline int mmap_file(struct file *file, struct vm_area_struct *vma) 167 { 168 int err = vfs_mmap(file, vma); 169 170 if (likely(!err)) 171 return 0; 172 173 /* 174 * OK, we tried to call the file hook for mmap(), but an error 175 * arose. The mapping is in an inconsistent state and we must not invoke 176 * any further hooks on it. 177 */ 178 vma->vm_ops = &vma_dummy_vm_ops; 179 180 return err; 181 } 182 183 /* 184 * If the VMA has a close hook then close it, and since closing it might leave 185 * it in an inconsistent state which makes the use of any hooks suspect, clear 186 * them down by installing dummy empty hooks. 187 */ 188 static inline void vma_close(struct vm_area_struct *vma) 189 { 190 if (vma->vm_ops && vma->vm_ops->close) { 191 vma->vm_ops->close(vma); 192 193 /* 194 * The mapping is in an inconsistent state, and no further hooks 195 * may be invoked upon it. 196 */ 197 vma->vm_ops = &vma_dummy_vm_ops; 198 } 199 } 200 201 /* unmap_vmas is in mm/memory.c */ 202 void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap); 203 204 #ifdef CONFIG_MMU 205 206 static inline void get_anon_vma(struct anon_vma *anon_vma) 207 { 208 atomic_inc(&anon_vma->refcount); 209 } 210 211 void __put_anon_vma(struct anon_vma *anon_vma); 212 213 static inline void put_anon_vma(struct anon_vma *anon_vma) 214 { 215 if (atomic_dec_and_test(&anon_vma->refcount)) 216 __put_anon_vma(anon_vma); 217 } 218 219 static inline void anon_vma_lock_write(struct anon_vma *anon_vma) 220 { 221 down_write(&anon_vma->root->rwsem); 222 } 223 224 static inline int anon_vma_trylock_write(struct anon_vma *anon_vma) 225 { 226 return down_write_trylock(&anon_vma->root->rwsem); 227 } 228 229 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma) 230 { 231 up_write(&anon_vma->root->rwsem); 232 } 233 234 static inline void anon_vma_lock_read(struct anon_vma *anon_vma) 235 { 236 down_read(&anon_vma->root->rwsem); 237 } 238 239 static inline int anon_vma_trylock_read(struct anon_vma *anon_vma) 240 { 241 return down_read_trylock(&anon_vma->root->rwsem); 242 } 243 244 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma) 245 { 246 up_read(&anon_vma->root->rwsem); 247 } 248 249 struct anon_vma *folio_get_anon_vma(const struct folio *folio); 250 251 /* Operations which modify VMAs. */ 252 enum vma_operation { 253 VMA_OP_SPLIT, 254 VMA_OP_MERGE_UNFAULTED, 255 VMA_OP_REMAP, 256 VMA_OP_FORK, 257 }; 258 259 int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src, 260 enum vma_operation operation); 261 int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma); 262 int __anon_vma_prepare(struct vm_area_struct *vma); 263 void unlink_anon_vmas(struct vm_area_struct *vma); 264 265 static inline int anon_vma_prepare(struct vm_area_struct *vma) 266 { 267 if (likely(vma->anon_vma)) 268 return 0; 269 270 return __anon_vma_prepare(vma); 271 } 272 273 /* Flags for folio_pte_batch(). */ 274 typedef int __bitwise fpb_t; 275 276 /* Compare PTEs respecting the dirty bit. */ 277 #define FPB_RESPECT_DIRTY ((__force fpb_t)BIT(0)) 278 279 /* Compare PTEs respecting the soft-dirty bit. */ 280 #define FPB_RESPECT_SOFT_DIRTY ((__force fpb_t)BIT(1)) 281 282 /* Compare PTEs respecting the writable bit. */ 283 #define FPB_RESPECT_WRITE ((__force fpb_t)BIT(2)) 284 285 /* 286 * Merge PTE write bits: if any PTE in the batch is writable, modify the 287 * PTE at @ptentp to be writable. 288 */ 289 #define FPB_MERGE_WRITE ((__force fpb_t)BIT(3)) 290 291 /* 292 * Merge PTE young and dirty bits: if any PTE in the batch is young or dirty, 293 * modify the PTE at @ptentp to be young or dirty, respectively. 294 */ 295 #define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(4)) 296 297 static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) 298 { 299 if (!(flags & FPB_RESPECT_DIRTY)) 300 pte = pte_mkclean(pte); 301 if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY))) 302 pte = pte_clear_soft_dirty(pte); 303 if (likely(!(flags & FPB_RESPECT_WRITE))) 304 pte = pte_wrprotect(pte); 305 return pte_mkold(pte); 306 } 307 308 /** 309 * folio_pte_batch_flags - detect a PTE batch for a large folio 310 * @folio: The large folio to detect a PTE batch for. 311 * @vma: The VMA. Only relevant with FPB_MERGE_WRITE, otherwise can be NULL. 312 * @ptep: Page table pointer for the first entry. 313 * @ptentp: Pointer to a COPY of the first page table entry whose flags this 314 * function updates based on @flags if appropriate. 315 * @max_nr: The maximum number of table entries to consider. 316 * @flags: Flags to modify the PTE batch semantics. 317 * 318 * Detect a PTE batch: consecutive (present) PTEs that map consecutive 319 * pages of the same large folio in a single VMA and a single page table. 320 * 321 * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN, 322 * the accessed bit, writable bit, dirty bit (unless FPB_RESPECT_DIRTY is set) 323 * and soft-dirty bit (unless FPB_RESPECT_SOFT_DIRTY is set). 324 * 325 * @ptep must map any page of the folio. max_nr must be at least one and 326 * must be limited by the caller so scanning cannot exceed a single VMA and 327 * a single page table. 328 * 329 * Depending on the FPB_MERGE_* flags, the pte stored at @ptentp will 330 * be updated: it's crucial that a pointer to a COPY of the first 331 * page table entry, obtained through ptep_get(), is provided as @ptentp. 332 * 333 * This function will be inlined to optimize based on the input parameters; 334 * consider using folio_pte_batch() instead if applicable. 335 * 336 * Return: the number of table entries in the batch. 337 */ 338 static inline unsigned int folio_pte_batch_flags(struct folio *folio, 339 struct vm_area_struct *vma, pte_t *ptep, pte_t *ptentp, 340 unsigned int max_nr, fpb_t flags) 341 { 342 bool any_writable = false, any_young = false, any_dirty = false; 343 pte_t expected_pte, pte = *ptentp; 344 unsigned int nr, cur_nr; 345 346 VM_WARN_ON_FOLIO(!pte_present(pte), folio); 347 VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio); 348 VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio); 349 /* 350 * Ensure this is a pointer to a copy not a pointer into a page table. 351 * If this is a stack value, it won't be a valid virtual address, but 352 * that's fine because it also cannot be pointing into the page table. 353 */ 354 VM_WARN_ON(virt_addr_valid(ptentp) && PageTable(virt_to_page(ptentp))); 355 356 /* Limit max_nr to the actual remaining PFNs in the folio we could batch. */ 357 max_nr = min_t(unsigned long, max_nr, 358 folio_pfn(folio) + folio_nr_pages(folio) - pte_pfn(pte)); 359 360 nr = pte_batch_hint(ptep, pte); 361 expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); 362 ptep = ptep + nr; 363 364 while (nr < max_nr) { 365 pte = ptep_get(ptep); 366 367 if (!pte_same(__pte_batch_clear_ignored(pte, flags), expected_pte)) 368 break; 369 370 if (flags & FPB_MERGE_WRITE) 371 any_writable |= pte_write(pte); 372 if (flags & FPB_MERGE_YOUNG_DIRTY) { 373 any_young |= pte_young(pte); 374 any_dirty |= pte_dirty(pte); 375 } 376 377 cur_nr = pte_batch_hint(ptep, pte); 378 expected_pte = pte_advance_pfn(expected_pte, cur_nr); 379 ptep += cur_nr; 380 nr += cur_nr; 381 } 382 383 if (any_writable) 384 *ptentp = pte_mkwrite(*ptentp, vma); 385 if (any_young) 386 *ptentp = pte_mkyoung(*ptentp); 387 if (any_dirty) 388 *ptentp = pte_mkdirty(*ptentp); 389 390 return min(nr, max_nr); 391 } 392 393 unsigned int folio_pte_batch(struct folio *folio, pte_t *ptep, pte_t pte, 394 unsigned int max_nr); 395 396 /** 397 * pte_move_swp_offset - Move the swap entry offset field of a swap pte 398 * forward or backward by delta 399 * @pte: The initial pte state; must be a swap entry 400 * @delta: The direction and the offset we are moving; forward if delta 401 * is positive; backward if delta is negative 402 * 403 * Moves the swap offset, while maintaining all other fields, including 404 * swap type, and any swp pte bits. The resulting pte is returned. 405 */ 406 static inline pte_t pte_move_swp_offset(pte_t pte, long delta) 407 { 408 const softleaf_t entry = softleaf_from_pte(pte); 409 pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry), 410 (swp_offset(entry) + delta))); 411 412 if (pte_swp_soft_dirty(pte)) 413 new = pte_swp_mksoft_dirty(new); 414 if (pte_swp_exclusive(pte)) 415 new = pte_swp_mkexclusive(new); 416 if (pte_swp_uffd_wp(pte)) 417 new = pte_swp_mkuffd_wp(new); 418 419 return new; 420 } 421 422 423 /** 424 * pte_next_swp_offset - Increment the swap entry offset field of a swap pte. 425 * @pte: The initial pte state; must be a swap entry. 426 * 427 * Increments the swap offset, while maintaining all other fields, including 428 * swap type, and any swp pte bits. The resulting pte is returned. 429 */ 430 static inline pte_t pte_next_swp_offset(pte_t pte) 431 { 432 return pte_move_swp_offset(pte, 1); 433 } 434 435 /** 436 * swap_pte_batch - detect a PTE batch for a set of contiguous swap entries 437 * @start_ptep: Page table pointer for the first entry. 438 * @max_nr: The maximum number of table entries to consider. 439 * @pte: Page table entry for the first entry. 440 * 441 * Detect a batch of contiguous swap entries: consecutive (non-present) PTEs 442 * containing swap entries all with consecutive offsets and targeting the same 443 * swap type, all with matching swp pte bits. 444 * 445 * max_nr must be at least one and must be limited by the caller so scanning 446 * cannot exceed a single page table. 447 * 448 * Return: the number of table entries in the batch. 449 */ 450 static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte) 451 { 452 pte_t expected_pte = pte_next_swp_offset(pte); 453 const pte_t *end_ptep = start_ptep + max_nr; 454 const softleaf_t entry = softleaf_from_pte(pte); 455 pte_t *ptep = start_ptep + 1; 456 unsigned short cgroup_id; 457 458 VM_WARN_ON(max_nr < 1); 459 VM_WARN_ON(!softleaf_is_swap(entry)); 460 461 cgroup_id = lookup_swap_cgroup_id(entry); 462 while (ptep < end_ptep) { 463 softleaf_t entry; 464 465 pte = ptep_get(ptep); 466 467 if (!pte_same(pte, expected_pte)) 468 break; 469 entry = softleaf_from_pte(pte); 470 if (lookup_swap_cgroup_id(entry) != cgroup_id) 471 break; 472 expected_pte = pte_next_swp_offset(expected_pte); 473 ptep++; 474 } 475 476 return ptep - start_ptep; 477 } 478 #endif /* CONFIG_MMU */ 479 480 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 481 int nr_throttled); 482 static inline void acct_reclaim_writeback(struct folio *folio) 483 { 484 pg_data_t *pgdat = folio_pgdat(folio); 485 int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled); 486 487 if (nr_throttled) 488 __acct_reclaim_writeback(pgdat, folio, nr_throttled); 489 } 490 491 static inline void wake_throttle_isolated(pg_data_t *pgdat) 492 { 493 wait_queue_head_t *wqh; 494 495 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED]; 496 if (waitqueue_active(wqh)) 497 wake_up(wqh); 498 } 499 500 vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf); 501 static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) 502 { 503 vm_fault_t ret = __vmf_anon_prepare(vmf); 504 505 if (unlikely(ret & VM_FAULT_RETRY)) 506 vma_end_read(vmf->vma); 507 return ret; 508 } 509 510 vm_fault_t do_swap_page(struct vm_fault *vmf); 511 void folio_rotate_reclaimable(struct folio *folio); 512 bool __folio_end_writeback(struct folio *folio); 513 void deactivate_file_folio(struct folio *folio); 514 void folio_activate(struct folio *folio); 515 516 void free_pgtables(struct mmu_gather *tlb, struct unmap_desc *desc); 517 518 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); 519 520 /** 521 * sync_with_folio_pmd_zap - sync with concurrent zapping of a folio PMD 522 * @mm: The mm_struct. 523 * @pmdp: Pointer to the pmd that was found to be pmd_none(). 524 * 525 * When we find a pmd_none() while unmapping a folio without holding the PTL, 526 * zap_huge_pmd() may have cleared the PMD but not yet modified the folio to 527 * indicate that it's unmapped. Skipping the PMD without synchronization could 528 * make folio unmapping code assume that unmapping failed. 529 * 530 * Wait for concurrent zapping to complete by grabbing the PTL. 531 */ 532 static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) 533 { 534 spinlock_t *ptl = pmd_lock(mm, pmdp); 535 536 spin_unlock(ptl); 537 } 538 539 struct zap_details; 540 void zap_vma_range_batched(struct mmu_gather *tlb, 541 struct vm_area_struct *vma, unsigned long addr, 542 unsigned long size, struct zap_details *details); 543 int zap_vma_for_reaping(struct vm_area_struct *vma); 544 int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, 545 gfp_t gfp); 546 547 void page_cache_ra_order(struct readahead_control *, struct file_ra_state *); 548 void force_page_cache_ra(struct readahead_control *, unsigned long nr); 549 static inline void force_page_cache_readahead(struct address_space *mapping, 550 struct file *file, pgoff_t index, unsigned long nr_to_read) 551 { 552 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index); 553 force_page_cache_ra(&ractl, nr_to_read); 554 } 555 556 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, 557 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 558 unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, 559 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); 560 void filemap_free_folio(struct address_space *mapping, struct folio *folio); 561 int truncate_inode_folio(struct address_space *mapping, struct folio *folio); 562 bool truncate_inode_partial_folio(struct folio *folio, loff_t start, 563 loff_t end); 564 long mapping_evict_folio(struct address_space *mapping, struct folio *folio); 565 unsigned long mapping_try_invalidate(struct address_space *mapping, 566 pgoff_t start, pgoff_t end, unsigned long *nr_failed); 567 568 /** 569 * folio_evictable - Test whether a folio is evictable. 570 * @folio: The folio to test. 571 * 572 * Test whether @folio is evictable -- i.e., should be placed on 573 * active/inactive lists vs unevictable list. 574 * 575 * Reasons folio might not be evictable: 576 * 1. folio's mapping marked unevictable 577 * 2. One of the pages in the folio is part of an mlocked VMA 578 */ 579 static inline bool folio_evictable(struct folio *folio) 580 { 581 bool ret; 582 583 /* Prevent address_space of inode and swap cache from being freed */ 584 rcu_read_lock(); 585 ret = !mapping_unevictable(folio_mapping(folio)) && 586 !folio_test_mlocked(folio); 587 rcu_read_unlock(); 588 return ret; 589 } 590 591 /* 592 * Turn a non-refcounted page (->_refcount == 0) into refcounted with 593 * a count of one. 594 */ 595 static inline void set_page_refcounted(struct page *page) 596 { 597 VM_BUG_ON_PAGE(PageTail(page), page); 598 VM_BUG_ON_PAGE(page_ref_count(page), page); 599 set_page_count(page, 1); 600 } 601 602 static inline void set_pages_refcounted(struct page *page, unsigned long nr_pages) 603 { 604 unsigned long pfn = page_to_pfn(page); 605 606 for (; nr_pages--; pfn++) 607 set_page_refcounted(pfn_to_page(pfn)); 608 } 609 610 /* 611 * Return true if a folio needs ->release_folio() calling upon it. 612 */ 613 static inline bool folio_needs_release(struct folio *folio) 614 { 615 struct address_space *mapping = folio_mapping(folio); 616 617 return folio_has_private(folio) || 618 (mapping && mapping_release_always(mapping)); 619 } 620 621 extern unsigned long highest_memmap_pfn; 622 623 /* 624 * Maximum number of reclaim retries without progress before the OOM 625 * killer is consider the only way forward. 626 */ 627 #define MAX_RECLAIM_RETRIES 16 628 629 /* 630 * in mm/vmscan.c: 631 */ 632 bool folio_isolate_lru(struct folio *folio); 633 void folio_putback_lru(struct folio *folio); 634 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason); 635 int user_proactive_reclaim(char *buf, 636 struct mem_cgroup *memcg, pg_data_t *pgdat); 637 638 /* 639 * in mm/rmap.c: 640 */ 641 pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address); 642 643 /* 644 * in mm/khugepaged.c 645 */ 646 void set_recommended_min_free_kbytes(void); 647 648 /* 649 * in mm/page_alloc.c 650 */ 651 #define K(x) ((x) << (PAGE_SHIFT-10)) 652 653 extern char * const zone_names[MAX_NR_ZONES]; 654 655 /* perform sanity checks on struct pages being allocated or freed */ 656 DECLARE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled); 657 658 extern int min_free_kbytes; 659 extern int defrag_mode; 660 661 void setup_per_zone_wmarks(void); 662 void calculate_min_free_kbytes(void); 663 int __meminit init_per_zone_wmark_min(void); 664 void page_alloc_sysctl_init(void); 665 666 /* 667 * Structure for holding the mostly immutable allocation parameters passed 668 * between functions involved in allocations, including the alloc_pages* 669 * family of functions. 670 * 671 * nodemask, migratetype and highest_zoneidx are initialized only once in 672 * __alloc_pages() and then never change. 673 * 674 * zonelist, preferred_zone and highest_zoneidx are set first in 675 * __alloc_pages() for the fast path, and might be later changed 676 * in __alloc_pages_slowpath(). All other functions pass the whole structure 677 * by a const pointer. 678 */ 679 struct alloc_context { 680 struct zonelist *zonelist; 681 nodemask_t *nodemask; 682 struct zoneref *preferred_zoneref; 683 int migratetype; 684 685 /* 686 * highest_zoneidx represents highest usable zone index of 687 * the allocation request. Due to the nature of the zone, 688 * memory on lower zone than the highest_zoneidx will be 689 * protected by lowmem_reserve[highest_zoneidx]. 690 * 691 * highest_zoneidx is also used by reclaim/compaction to limit 692 * the target zone since higher zone than this index cannot be 693 * usable for this allocation request. 694 */ 695 enum zone_type highest_zoneidx; 696 bool spread_dirty_pages; 697 }; 698 699 /* 700 * This function returns the order of a free page in the buddy system. In 701 * general, page_zone(page)->lock must be held by the caller to prevent the 702 * page from being allocated in parallel and returning garbage as the order. 703 * If a caller does not hold page_zone(page)->lock, it must guarantee that the 704 * page cannot be allocated or merged in parallel. Alternatively, it must 705 * handle invalid values gracefully, and use buddy_order_unsafe() below. 706 */ 707 static inline unsigned int buddy_order(struct page *page) 708 { 709 /* PageBuddy() must be checked by the caller */ 710 return page_private(page); 711 } 712 713 /* 714 * Like buddy_order(), but for callers who cannot afford to hold the zone lock. 715 * PageBuddy() should be checked first by the caller to minimize race window, 716 * and invalid values must be handled gracefully. 717 * 718 * READ_ONCE is used so that if the caller assigns the result into a local 719 * variable and e.g. tests it for valid range before using, the compiler cannot 720 * decide to remove the variable and inline the page_private(page) multiple 721 * times, potentially observing different values in the tests and the actual 722 * use of the result. 723 */ 724 #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) 725 726 /* 727 * This function checks whether a page is free && is the buddy 728 * we can coalesce a page and its buddy if 729 * (a) the buddy is not in a hole (check before calling!) && 730 * (b) the buddy is in the buddy system && 731 * (c) a page and its buddy have the same order && 732 * (d) a page and its buddy are in the same zone. 733 * 734 * For recording whether a page is in the buddy system, we set PageBuddy. 735 * Setting, clearing, and testing PageBuddy is serialized by zone->lock. 736 * 737 * For recording page's order, we use page_private(page). 738 */ 739 static inline bool page_is_buddy(struct page *page, struct page *buddy, 740 unsigned int order) 741 { 742 if (!page_is_guard(buddy) && !PageBuddy(buddy)) 743 return false; 744 745 if (buddy_order(buddy) != order) 746 return false; 747 748 /* 749 * zone check is done late to avoid uselessly calculating 750 * zone/node ids for pages that could never merge. 751 */ 752 if (page_zone_id(page) != page_zone_id(buddy)) 753 return false; 754 755 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); 756 757 return true; 758 } 759 760 /* 761 * Locate the struct page for both the matching buddy in our 762 * pair (buddy1) and the combined O(n+1) page they form (page). 763 * 764 * 1) Any buddy B1 will have an order O twin B2 which satisfies 765 * the following equation: 766 * B2 = B1 ^ (1 << O) 767 * For example, if the starting buddy (buddy2) is #8 its order 768 * 1 buddy is #10: 769 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10 770 * 771 * 2) Any buddy B will have an order O+1 parent P which 772 * satisfies the following equation: 773 * P = B & ~(1 << O) 774 * 775 * Assumption: *_mem_map is contiguous at least up to MAX_PAGE_ORDER 776 */ 777 static inline unsigned long 778 __find_buddy_pfn(unsigned long page_pfn, unsigned int order) 779 { 780 return page_pfn ^ (1 << order); 781 } 782 783 /* 784 * Find the buddy of @page and validate it. 785 * @page: The input page 786 * @pfn: The pfn of the page, it saves a call to page_to_pfn() when the 787 * function is used in the performance-critical __free_one_page(). 788 * @order: The order of the page 789 * @buddy_pfn: The output pointer to the buddy pfn, it also saves a call to 790 * page_to_pfn(). 791 * 792 * The found buddy can be a non PageBuddy, out of @page's zone, or its order is 793 * not the same as @page. The validation is necessary before use it. 794 * 795 * Return: the found buddy page or NULL if not found. 796 */ 797 static inline struct page *find_buddy_page_pfn(struct page *page, 798 unsigned long pfn, unsigned int order, unsigned long *buddy_pfn) 799 { 800 unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order); 801 struct page *buddy; 802 803 buddy = page + (__buddy_pfn - pfn); 804 if (buddy_pfn) 805 *buddy_pfn = __buddy_pfn; 806 807 if (page_is_buddy(page, buddy, order)) 808 return buddy; 809 return NULL; 810 } 811 812 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn, 813 unsigned long end_pfn, struct zone *zone); 814 815 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, 816 unsigned long end_pfn, struct zone *zone) 817 { 818 if (zone->contiguous) 819 return pfn_to_page(start_pfn); 820 821 return __pageblock_pfn_to_page(start_pfn, end_pfn, zone); 822 } 823 824 void set_zone_contiguous(struct zone *zone); 825 bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, 826 unsigned long nr_pages); 827 828 static inline void clear_zone_contiguous(struct zone *zone) 829 { 830 zone->contiguous = false; 831 } 832 833 extern int __isolate_free_page(struct page *page, unsigned int order); 834 extern void __putback_isolated_page(struct page *page, unsigned int order, 835 int mt); 836 extern void memblock_free_pages(unsigned long pfn, unsigned int order); 837 extern void __free_pages_core(struct page *page, unsigned int order, 838 enum meminit_context context); 839 840 /* 841 * This will have no effect, other than possibly generating a warning, if the 842 * caller passes in a non-large folio. 843 */ 844 static inline void folio_set_order(struct folio *folio, unsigned int order) 845 { 846 if (WARN_ON_ONCE(!order || !folio_test_large(folio))) 847 return; 848 VM_WARN_ON_ONCE(order > MAX_FOLIO_ORDER); 849 850 folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order; 851 #ifdef NR_PAGES_IN_LARGE_FOLIO 852 folio->_nr_pages = 1U << order; 853 #endif 854 } 855 856 bool __folio_unqueue_deferred_split(struct folio *folio); 857 static inline bool folio_unqueue_deferred_split(struct folio *folio) 858 { 859 if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio)) 860 return false; 861 862 /* 863 * At this point, there is no one trying to add the folio to 864 * deferred_list. If folio is not in deferred_list, it's safe 865 * to check without acquiring the split_queue_lock. 866 */ 867 if (data_race(list_empty(&folio->_deferred_list))) 868 return false; 869 870 return __folio_unqueue_deferred_split(folio); 871 } 872 873 static inline struct folio *page_rmappable_folio(struct page *page) 874 { 875 struct folio *folio = (struct folio *)page; 876 877 if (folio && folio_test_large(folio)) 878 folio_set_large_rmappable(folio); 879 return folio; 880 } 881 882 static inline void prep_compound_head(struct page *page, unsigned int order) 883 { 884 struct folio *folio = (struct folio *)page; 885 886 folio_set_order(folio, order); 887 atomic_set(&folio->_large_mapcount, -1); 888 if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) 889 atomic_set(&folio->_nr_pages_mapped, 0); 890 if (IS_ENABLED(CONFIG_MM_ID)) { 891 folio->_mm_ids = 0; 892 folio->_mm_id_mapcount[0] = -1; 893 folio->_mm_id_mapcount[1] = -1; 894 } 895 if (IS_ENABLED(CONFIG_64BIT) || order > 1) { 896 atomic_set(&folio->_pincount, 0); 897 atomic_set(&folio->_entire_mapcount, -1); 898 } 899 if (order > 1) 900 INIT_LIST_HEAD(&folio->_deferred_list); 901 } 902 903 static inline void prep_compound_tail(struct page *tail, 904 const struct page *head, unsigned int order) 905 { 906 tail->mapping = TAIL_MAPPING; 907 set_compound_head(tail, head, order); 908 set_page_private(tail, 0); 909 } 910 911 static inline void init_compound_tail(struct page *tail, 912 const struct page *head, unsigned int order, struct zone *zone) 913 { 914 atomic_set(&tail->_mapcount, -1); 915 set_page_node(tail, zone_to_nid(zone)); 916 set_page_zone(tail, zone_idx(zone)); 917 prep_compound_tail(tail, head, order); 918 } 919 920 void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags); 921 extern bool free_pages_prepare(struct page *page, unsigned int order); 922 923 extern int user_min_free_kbytes; 924 925 struct page *__alloc_frozen_pages_noprof(gfp_t, unsigned int order, int nid, 926 nodemask_t *); 927 #define __alloc_frozen_pages(...) \ 928 alloc_hooks(__alloc_frozen_pages_noprof(__VA_ARGS__)) 929 void free_frozen_pages(struct page *page, unsigned int order); 930 void free_unref_folios(struct folio_batch *fbatch); 931 932 #ifdef CONFIG_NUMA 933 struct page *alloc_frozen_pages_noprof(gfp_t, unsigned int order); 934 #else 935 static inline struct page *alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order) 936 { 937 return __alloc_frozen_pages_noprof(gfp, order, numa_node_id(), NULL); 938 } 939 #endif 940 941 #define alloc_frozen_pages(...) \ 942 alloc_hooks(alloc_frozen_pages_noprof(__VA_ARGS__)) 943 944 struct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order); 945 #define alloc_frozen_pages_nolock(...) \ 946 alloc_hooks(alloc_frozen_pages_nolock_noprof(__VA_ARGS__)) 947 void free_frozen_pages_nolock(struct page *page, unsigned int order); 948 949 extern void zone_pcp_reset(struct zone *zone); 950 extern void zone_pcp_disable(struct zone *zone); 951 extern void zone_pcp_enable(struct zone *zone); 952 extern void zone_pcp_init(struct zone *zone); 953 954 extern void *memmap_alloc(phys_addr_t size, phys_addr_t align, 955 phys_addr_t min_addr, 956 int nid, bool exact_nid); 957 958 void memmap_init_range(unsigned long, int, unsigned long, unsigned long, 959 unsigned long, enum meminit_context, struct vmem_altmap *, int, 960 bool); 961 962 /* 963 * mm/sparse.c 964 */ 965 #ifdef CONFIG_SPARSEMEM 966 void sparse_init(void); 967 int sparse_index_init(unsigned long section_nr, int nid); 968 969 static inline void sparse_init_one_section(struct mem_section *ms, 970 unsigned long pnum, struct page *mem_map, 971 struct mem_section_usage *usage, unsigned long flags) 972 { 973 unsigned long coded_mem_map; 974 975 BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT); 976 977 /* 978 * We encode the start PFN of the section into the mem_map such that 979 * page_to_pfn() on !CONFIG_SPARSEMEM_VMEMMAP can simply subtract it 980 * from the page pointer to obtain the PFN. 981 */ 982 coded_mem_map = (unsigned long)(mem_map - section_nr_to_pfn(pnum)); 983 VM_WARN_ON_ONCE(coded_mem_map & ~SECTION_MAP_MASK); 984 985 ms->section_mem_map &= ~SECTION_MAP_MASK; 986 ms->section_mem_map |= coded_mem_map; 987 ms->section_mem_map |= flags | SECTION_HAS_MEM_MAP; 988 ms->usage = usage; 989 } 990 991 static inline void __section_mark_present(struct mem_section *ms, 992 unsigned long section_nr) 993 { 994 if (section_nr > __highest_present_section_nr) 995 __highest_present_section_nr = section_nr; 996 997 ms->section_mem_map |= SECTION_MARKED_PRESENT; 998 } 999 #else 1000 static inline void sparse_init(void) {} 1001 #endif /* CONFIG_SPARSEMEM */ 1002 1003 /* 1004 * mm/sparse-vmemmap.c 1005 */ 1006 #ifdef CONFIG_SPARSEMEM_VMEMMAP 1007 void sparse_init_subsection_map(unsigned long pfn, unsigned long nr_pages); 1008 #else 1009 static inline void sparse_init_subsection_map(unsigned long pfn, 1010 unsigned long nr_pages) 1011 { 1012 } 1013 #endif /* CONFIG_SPARSEMEM_VMEMMAP */ 1014 1015 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 1016 1017 /* 1018 * in mm/compaction.c 1019 */ 1020 /* 1021 * compact_control is used to track pages being migrated and the free pages 1022 * they are being migrated to during memory compaction. The free_pfn starts 1023 * at the end of a zone and migrate_pfn begins at the start. Movable pages 1024 * are moved to the end of a zone during a compaction run and the run 1025 * completes when free_pfn <= migrate_pfn 1026 */ 1027 struct compact_control { 1028 struct list_head freepages[NR_PAGE_ORDERS]; /* List of free pages to migrate to */ 1029 struct list_head migratepages; /* List of pages being migrated */ 1030 unsigned int nr_freepages; /* Number of isolated free pages */ 1031 unsigned int nr_migratepages; /* Number of pages to migrate */ 1032 unsigned long free_pfn; /* isolate_freepages search base */ 1033 /* 1034 * Acts as an in/out parameter to page isolation for migration. 1035 * isolate_migratepages uses it as a search base. 1036 * isolate_migratepages_block will update the value to the next pfn 1037 * after the last isolated one. 1038 */ 1039 unsigned long migrate_pfn; 1040 unsigned long fast_start_pfn; /* a pfn to start linear scan from */ 1041 struct zone *zone; 1042 unsigned long total_migrate_scanned; 1043 unsigned long total_free_scanned; 1044 unsigned short fast_search_fail;/* failures to use free list searches */ 1045 short search_order; /* order to start a fast search at */ 1046 const gfp_t gfp_mask; /* gfp mask of a direct compactor */ 1047 int order; /* order a direct compactor needs */ 1048 int migratetype; /* migratetype of direct compactor */ 1049 const unsigned int alloc_flags; /* alloc flags of a direct compactor */ 1050 const int highest_zoneidx; /* zone index of a direct compactor */ 1051 enum migrate_mode mode; /* Async or sync migration mode */ 1052 bool ignore_skip_hint; /* Scan blocks even if marked skip */ 1053 bool no_set_skip_hint; /* Don't mark blocks for skipping */ 1054 bool ignore_block_suitable; /* Scan blocks considered unsuitable */ 1055 bool direct_compaction; /* False from kcompactd or /proc/... */ 1056 bool proactive_compaction; /* kcompactd proactive compaction */ 1057 bool whole_zone; /* Whole zone should/has been scanned */ 1058 bool contended; /* Signal lock contention */ 1059 bool finish_pageblock; /* Scan the remainder of a pageblock. Used 1060 * when there are potentially transient 1061 * isolation or migration failures to 1062 * ensure forward progress. 1063 */ 1064 bool alloc_contig; /* alloc_contig_range allocation */ 1065 }; 1066 1067 /* 1068 * Used in direct compaction when a page should be taken from the freelists 1069 * immediately when one is created during the free path. 1070 */ 1071 struct capture_control { 1072 struct compact_control *cc; 1073 struct page *page; 1074 }; 1075 1076 unsigned long 1077 isolate_freepages_range(struct compact_control *cc, 1078 unsigned long start_pfn, unsigned long end_pfn); 1079 int 1080 isolate_migratepages_range(struct compact_control *cc, 1081 unsigned long low_pfn, unsigned long end_pfn); 1082 1083 /* Free whole pageblock and set its migration type to MIGRATE_CMA. */ 1084 void init_cma_reserved_pageblock(struct page *page); 1085 1086 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 1087 1088 struct cma; 1089 1090 #ifdef CONFIG_CMA 1091 bool cma_validate_zones(struct cma *cma); 1092 void *cma_reserve_early(struct cma *cma, unsigned long size); 1093 void init_cma_pageblock(struct page *page); 1094 #else 1095 static inline bool cma_validate_zones(struct cma *cma) 1096 { 1097 return false; 1098 } 1099 static inline void *cma_reserve_early(struct cma *cma, unsigned long size) 1100 { 1101 return NULL; 1102 } 1103 static inline void init_cma_pageblock(struct page *page) 1104 { 1105 } 1106 #endif 1107 1108 1109 int find_suitable_fallback(struct free_area *area, unsigned int order, 1110 int migratetype, bool claimable); 1111 1112 static inline bool free_area_empty(struct free_area *area, int migratetype) 1113 { 1114 return list_empty(&area->free_list[migratetype]); 1115 } 1116 1117 /* mm/util.c */ 1118 struct anon_vma *folio_anon_vma(const struct folio *folio); 1119 1120 #ifdef CONFIG_MMU 1121 void unmap_mapping_folio(struct folio *folio); 1122 extern long populate_vma_page_range(struct vm_area_struct *vma, 1123 unsigned long start, unsigned long end, int *locked); 1124 extern long faultin_page_range(struct mm_struct *mm, unsigned long start, 1125 unsigned long end, bool write, int *locked); 1126 bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked, 1127 unsigned long bytes); 1128 1129 /* 1130 * NOTE: This function can't tell whether the folio is "fully mapped" in the 1131 * range. 1132 * "fully mapped" means all the pages of folio is associated with the page 1133 * table of range while this function just check whether the folio range is 1134 * within the range [start, end). Function caller needs to do page table 1135 * check if it cares about the page table association. 1136 * 1137 * Typical usage (like mlock or madvise) is: 1138 * Caller knows at least 1 page of folio is associated with page table of VMA 1139 * and the range [start, end) is intersect with the VMA range. Caller wants 1140 * to know whether the folio is fully associated with the range. It calls 1141 * this function to check whether the folio is in the range first. Then checks 1142 * the page table to know whether the folio is fully mapped to the range. 1143 */ 1144 static inline bool 1145 folio_within_range(struct folio *folio, struct vm_area_struct *vma, 1146 unsigned long start, unsigned long end) 1147 { 1148 pgoff_t pgoff, addr; 1149 unsigned long vma_pglen = vma_pages(vma); 1150 1151 VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio); 1152 if (start > end) 1153 return false; 1154 1155 if (start < vma->vm_start) 1156 start = vma->vm_start; 1157 1158 if (end > vma->vm_end) 1159 end = vma->vm_end; 1160 1161 pgoff = folio_pgoff(folio); 1162 1163 /* if folio start address is not in vma range */ 1164 if (!in_range(pgoff, vma->vm_pgoff, vma_pglen)) 1165 return false; 1166 1167 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1168 1169 return !(addr < start || end - addr < folio_size(folio)); 1170 } 1171 1172 static inline bool 1173 folio_within_vma(struct folio *folio, struct vm_area_struct *vma) 1174 { 1175 return folio_within_range(folio, vma, vma->vm_start, vma->vm_end); 1176 } 1177 1178 /* 1179 * mlock_vma_folio() and munlock_vma_folio(): 1180 * should be called with vma's mmap_lock held for read or write, 1181 * under page table lock for the pte/pmd being added or removed. 1182 * 1183 * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at 1184 * the end of folio_remove_rmap_*(); but new anon folios are managed by 1185 * folio_add_lru_vma() calling mlock_new_folio(). 1186 */ 1187 void mlock_folio(struct folio *folio); 1188 static inline void mlock_vma_folio(struct folio *folio, 1189 struct vm_area_struct *vma) 1190 { 1191 /* 1192 * The VM_SPECIAL check here serves two purposes. 1193 * 1) VM_IO check prevents migration from double-counting during mlock. 1194 * 2) Although mmap_region() and mlock_fixup() take care that VM_LOCKED 1195 * is never left set on a VM_SPECIAL vma, there is an interval while 1196 * file->f_op->mmap() is using vm_insert_page(s), when VM_LOCKED may 1197 * still be set while VM_SPECIAL bits are added: so ignore it then. 1198 */ 1199 if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED)) 1200 mlock_folio(folio); 1201 } 1202 1203 void munlock_folio(struct folio *folio); 1204 static inline void munlock_vma_folio(struct folio *folio, 1205 struct vm_area_struct *vma) 1206 { 1207 /* 1208 * munlock if the function is called. Ideally, we should only 1209 * do munlock if any page of folio is unmapped from VMA and 1210 * cause folio not fully mapped to VMA. 1211 * 1212 * But it's not easy to confirm that's the situation. So we 1213 * always munlock the folio and page reclaim will correct it 1214 * if it's wrong. 1215 */ 1216 if (unlikely(vma->vm_flags & VM_LOCKED)) 1217 munlock_folio(folio); 1218 } 1219 1220 void mlock_new_folio(struct folio *folio); 1221 bool need_mlock_drain(int cpu); 1222 void mlock_drain_local(void); 1223 void mlock_drain_remote(int cpu); 1224 1225 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); 1226 1227 /** 1228 * vma_address - Find the virtual address a page range is mapped at 1229 * @vma: The vma which maps this object. 1230 * @pgoff: The page offset within its object. 1231 * @nr_pages: The number of pages to consider. 1232 * 1233 * If any page in this range is mapped by this VMA, return the first address 1234 * where any of these pages appear. Otherwise, return -EFAULT. 1235 */ 1236 static inline unsigned long vma_address(const struct vm_area_struct *vma, 1237 pgoff_t pgoff, unsigned long nr_pages) 1238 { 1239 unsigned long address; 1240 1241 if (pgoff >= vma->vm_pgoff) { 1242 address = vma->vm_start + 1243 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1244 /* Check for address beyond vma (or wrapped through 0?) */ 1245 if (address < vma->vm_start || address >= vma->vm_end) 1246 address = -EFAULT; 1247 } else if (pgoff + nr_pages - 1 >= vma->vm_pgoff) { 1248 /* Test above avoids possibility of wrap to 0 on 32-bit */ 1249 address = vma->vm_start; 1250 } else { 1251 address = -EFAULT; 1252 } 1253 return address; 1254 } 1255 1256 /* 1257 * Then at what user virtual address will none of the range be found in vma? 1258 * Assumes that vma_address() already returned a good starting address. 1259 */ 1260 static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw) 1261 { 1262 struct vm_area_struct *vma = pvmw->vma; 1263 pgoff_t pgoff; 1264 unsigned long address; 1265 1266 /* Common case, plus ->pgoff is invalid for KSM */ 1267 if (pvmw->nr_pages == 1) 1268 return pvmw->address + PAGE_SIZE; 1269 1270 pgoff = pvmw->pgoff + pvmw->nr_pages; 1271 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 1272 /* Check for address beyond vma (or wrapped through 0?) */ 1273 if (address < vma->vm_start || address > vma->vm_end) 1274 address = vma->vm_end; 1275 return address; 1276 } 1277 1278 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, 1279 struct file *fpin) 1280 { 1281 int flags = vmf->flags; 1282 1283 if (fpin) 1284 return fpin; 1285 1286 /* 1287 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or 1288 * anything, so we only pin the file and drop the mmap_lock if only 1289 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. 1290 */ 1291 if (fault_flag_allow_retry_first(flags) && 1292 !(flags & FAULT_FLAG_RETRY_NOWAIT)) { 1293 fpin = get_file(vmf->vma->vm_file); 1294 release_fault_lock(vmf); 1295 } 1296 return fpin; 1297 } 1298 1299 static inline bool vma_supports_mlock(const struct vm_area_struct *vma) 1300 { 1301 if (vma_test_any_mask(vma, VMA_SPECIAL_FLAGS)) 1302 return false; 1303 if (vma_test_single_mask(vma, VMA_DROPPABLE)) 1304 return false; 1305 if (vma_is_dax(vma) || is_vm_hugetlb_page(vma)) 1306 return false; 1307 return vma != get_gate_vma(current->mm); 1308 } 1309 1310 #else /* !CONFIG_MMU */ 1311 static inline void unmap_mapping_folio(struct folio *folio) { } 1312 static inline void mlock_new_folio(struct folio *folio) { } 1313 static inline bool need_mlock_drain(int cpu) { return false; } 1314 static inline void mlock_drain_local(void) { } 1315 static inline void mlock_drain_remote(int cpu) { } 1316 static inline void vunmap_range_noflush(unsigned long start, unsigned long end) 1317 { 1318 } 1319 #endif /* !CONFIG_MMU */ 1320 1321 /* Memory initialisation debug and verification */ 1322 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1323 DECLARE_STATIC_KEY_TRUE(deferred_pages); 1324 1325 bool __init deferred_grow_zone(struct zone *zone, unsigned int order); 1326 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 1327 1328 void init_deferred_page(unsigned long pfn, int nid); 1329 1330 enum mminit_level { 1331 MMINIT_WARNING, 1332 MMINIT_VERIFY, 1333 MMINIT_TRACE 1334 }; 1335 1336 #ifdef CONFIG_DEBUG_MEMORY_INIT 1337 1338 extern int mminit_loglevel; 1339 1340 #define mminit_dprintk(level, prefix, fmt, arg...) \ 1341 do { \ 1342 if (level < mminit_loglevel) { \ 1343 if (level <= MMINIT_WARNING) \ 1344 pr_warn("mminit::" prefix " " fmt, ##arg); \ 1345 else \ 1346 printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \ 1347 } \ 1348 } while (0) 1349 1350 extern void mminit_verify_pageflags_layout(void); 1351 extern void mminit_verify_zonelist(void); 1352 #else 1353 1354 static inline void mminit_dprintk(enum mminit_level level, 1355 const char *prefix, const char *fmt, ...) 1356 { 1357 } 1358 1359 static inline void mminit_verify_pageflags_layout(void) 1360 { 1361 } 1362 1363 static inline void mminit_verify_zonelist(void) 1364 { 1365 } 1366 #endif /* CONFIG_DEBUG_MEMORY_INIT */ 1367 1368 #define NODE_RECLAIM_NOSCAN -2 1369 #define NODE_RECLAIM_FULL -1 1370 #define NODE_RECLAIM_SOME 0 1371 #define NODE_RECLAIM_SUCCESS 1 1372 1373 #ifdef CONFIG_NUMA 1374 extern int node_reclaim_mode; 1375 1376 extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int); 1377 extern int find_next_best_node(int node, nodemask_t *used_node_mask); 1378 #else 1379 #define node_reclaim_mode 0 1380 1381 static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask, 1382 unsigned int order) 1383 { 1384 return NODE_RECLAIM_NOSCAN; 1385 } 1386 static inline int find_next_best_node(int node, nodemask_t *used_node_mask) 1387 { 1388 return NUMA_NO_NODE; 1389 } 1390 #endif 1391 1392 static inline bool node_reclaim_enabled(void) 1393 { 1394 /* Is any node_reclaim_mode bit set? */ 1395 return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP); 1396 } 1397 1398 /* 1399 * mm/memory-failure.c 1400 */ 1401 #ifdef CONFIG_MEMORY_FAILURE 1402 int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill); 1403 void shake_folio(struct folio *folio); 1404 typedef int hwpoison_filter_func_t(struct page *p); 1405 void hwpoison_filter_register(hwpoison_filter_func_t *filter); 1406 void hwpoison_filter_unregister(void); 1407 1408 #define MAGIC_HWPOISON 0x48575053U /* HWPS */ 1409 void SetPageHWPoisonTakenOff(struct page *page); 1410 void ClearPageHWPoisonTakenOff(struct page *page); 1411 bool take_page_off_buddy(struct page *page); 1412 bool put_page_back_buddy(struct page *page); 1413 struct task_struct *task_early_kill(struct task_struct *tsk, int force_early); 1414 void add_to_kill_ksm(struct task_struct *tsk, const struct page *p, 1415 struct vm_area_struct *vma, struct list_head *to_kill, 1416 unsigned long ksm_addr); 1417 unsigned long page_mapped_in_vma(const struct page *page, 1418 struct vm_area_struct *vma); 1419 1420 #else 1421 static inline int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill) 1422 { 1423 return -EBUSY; 1424 } 1425 #endif 1426 1427 extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long, 1428 unsigned long, unsigned long, 1429 unsigned long, unsigned long); 1430 1431 extern void set_pageblock_order(void); 1432 unsigned long reclaim_pages(struct list_head *folio_list); 1433 unsigned int reclaim_clean_pages_from_list(struct zone *zone, 1434 struct list_head *folio_list); 1435 /* The ALLOC_WMARK bits are used as an index to zone->watermark */ 1436 #define ALLOC_WMARK_MIN WMARK_MIN 1437 #define ALLOC_WMARK_LOW WMARK_LOW 1438 #define ALLOC_WMARK_HIGH WMARK_HIGH 1439 #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */ 1440 1441 /* Mask to get the watermark bits */ 1442 #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1) 1443 1444 /* 1445 * Only MMU archs have async oom victim reclaim - aka oom_reaper so we 1446 * cannot assume a reduced access to memory reserves is sufficient for 1447 * !MMU 1448 */ 1449 #ifdef CONFIG_MMU 1450 #define ALLOC_OOM 0x08 1451 #else 1452 #define ALLOC_OOM ALLOC_NO_WATERMARKS 1453 #endif 1454 1455 #define ALLOC_NON_BLOCK 0x10 /* Caller cannot block. Allow access 1456 * to 25% of the min watermark or 1457 * 62.5% if __GFP_HIGH is set. 1458 */ 1459 #define ALLOC_MIN_RESERVE 0x20 /* __GFP_HIGH set. Allow access to 50% 1460 * of the min watermark. 1461 */ 1462 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */ 1463 #define ALLOC_CMA 0x80 /* allow allocations from CMA areas */ 1464 #ifdef CONFIG_ZONE_DMA32 1465 #define ALLOC_NOFRAGMENT 0x100 /* avoid mixing pageblock types */ 1466 #else 1467 #define ALLOC_NOFRAGMENT 0x0 1468 #endif 1469 #define ALLOC_HIGHATOMIC 0x200 /* Allows access to MIGRATE_HIGHATOMIC */ 1470 #define ALLOC_TRYLOCK 0x400 /* Only use spin_trylock in allocation path */ 1471 #define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */ 1472 1473 /* Flags that allow allocations below the min watermark. */ 1474 #define ALLOC_RESERVES (ALLOC_NON_BLOCK|ALLOC_MIN_RESERVE|ALLOC_HIGHATOMIC|ALLOC_OOM) 1475 1476 enum ttu_flags; 1477 struct tlbflush_unmap_batch; 1478 1479 1480 /* 1481 * only for MM internal work items which do not depend on 1482 * any allocations or locks which might depend on allocations 1483 */ 1484 extern struct workqueue_struct *mm_percpu_wq; 1485 1486 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1487 void try_to_unmap_flush(void); 1488 void try_to_unmap_flush_dirty(void); 1489 void flush_tlb_batched_pending(struct mm_struct *mm); 1490 #else 1491 static inline void try_to_unmap_flush(void) 1492 { 1493 } 1494 static inline void try_to_unmap_flush_dirty(void) 1495 { 1496 } 1497 static inline void flush_tlb_batched_pending(struct mm_struct *mm) 1498 { 1499 } 1500 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ 1501 1502 extern const struct trace_print_flags pageflag_names[]; 1503 extern const struct trace_print_flags vmaflag_names[]; 1504 extern const struct trace_print_flags gfpflag_names[]; 1505 1506 void setup_zone_pageset(struct zone *zone); 1507 1508 struct migration_target_control { 1509 int nid; /* preferred node id */ 1510 nodemask_t *nmask; 1511 gfp_t gfp_mask; 1512 enum migrate_reason reason; 1513 }; 1514 1515 /* 1516 * mm/filemap.c 1517 */ 1518 size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, 1519 struct folio *folio, loff_t fpos, size_t size); 1520 1521 /* 1522 * mm/vmalloc.c 1523 */ 1524 #ifdef CONFIG_MMU 1525 void __init vmalloc_init(void); 1526 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 1527 pgprot_t prot, struct page **pages, unsigned int page_shift, gfp_t gfp_mask); 1528 unsigned int get_vm_area_page_order(struct vm_struct *vm); 1529 #else 1530 static inline void vmalloc_init(void) 1531 { 1532 } 1533 1534 static inline 1535 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, 1536 pgprot_t prot, struct page **pages, unsigned int page_shift, gfp_t gfp_mask) 1537 { 1538 return -EINVAL; 1539 } 1540 #endif 1541 1542 void clear_vm_uninitialized_flag(struct vm_struct *vm); 1543 1544 int __must_check __vmap_pages_range_noflush(unsigned long addr, 1545 unsigned long end, pgprot_t prot, 1546 struct page **pages, unsigned int page_shift); 1547 1548 void vunmap_range_noflush(unsigned long start, unsigned long end); 1549 1550 void __vunmap_range_noflush(unsigned long start, unsigned long end); 1551 1552 static inline bool vma_is_single_threaded_private(struct vm_area_struct *vma) 1553 { 1554 if (vma->vm_flags & VM_SHARED) 1555 return false; 1556 1557 return atomic_read(&vma->vm_mm->mm_users) == 1; 1558 } 1559 1560 #ifdef CONFIG_NUMA_BALANCING 1561 bool folio_can_map_prot_numa(struct folio *folio, struct vm_area_struct *vma, 1562 bool is_private_single_threaded); 1563 1564 #else 1565 static inline bool folio_can_map_prot_numa(struct folio *folio, 1566 struct vm_area_struct *vma, bool is_private_single_threaded) 1567 { 1568 return false; 1569 } 1570 #endif 1571 1572 int numa_migrate_check(struct folio *folio, struct vm_fault *vmf, 1573 unsigned long addr, int *flags, bool writable, 1574 int *last_cpupid); 1575 1576 void free_zone_device_folio(struct folio *folio); 1577 int migrate_device_coherent_folio(struct folio *folio); 1578 1579 struct vm_struct *__get_vm_area_node(unsigned long size, 1580 unsigned long align, unsigned long shift, 1581 unsigned long vm_flags, unsigned long start, 1582 unsigned long end, int node, gfp_t gfp_mask, 1583 const void *caller); 1584 1585 /* 1586 * mm/gup.c 1587 */ 1588 int __must_check try_grab_folio(struct folio *folio, int refs, 1589 unsigned int flags); 1590 1591 /* 1592 * mm/huge_memory.c 1593 */ 1594 void touch_pud(struct vm_area_struct *vma, unsigned long addr, 1595 pud_t *pud, bool write); 1596 bool touch_pmd(struct vm_area_struct *vma, unsigned long addr, 1597 pmd_t *pmd, bool write); 1598 1599 /* 1600 * Parses a string with mem suffixes into its order. Useful to parse kernel 1601 * parameters. 1602 */ 1603 static inline int get_order_from_str(const char *size_str, 1604 unsigned long valid_orders) 1605 { 1606 unsigned long size; 1607 char *endptr; 1608 int order; 1609 1610 size = memparse(size_str, &endptr); 1611 1612 if (!is_power_of_2(size)) 1613 return -EINVAL; 1614 order = get_order(size); 1615 if (BIT(order) & ~valid_orders) 1616 return -EINVAL; 1617 1618 return order; 1619 } 1620 1621 enum { 1622 /* mark page accessed */ 1623 FOLL_TOUCH = 1 << 16, 1624 /* a retry, previous pass started an IO */ 1625 FOLL_TRIED = 1 << 17, 1626 /* we are working on non-current tsk/mm */ 1627 FOLL_REMOTE = 1 << 18, 1628 /* pages must be released via unpin_user_page */ 1629 FOLL_PIN = 1 << 19, 1630 /* gup_fast: prevent fall-back to slow gup */ 1631 FOLL_FAST_ONLY = 1 << 20, 1632 /* allow unlocking the mmap lock */ 1633 FOLL_UNLOCKABLE = 1 << 21, 1634 /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ 1635 FOLL_MADV_POPULATE = 1 << 22, 1636 }; 1637 1638 #define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ 1639 FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ 1640 FOLL_MADV_POPULATE) 1641 1642 /* 1643 * Indicates for which pages that are write-protected in the page table, 1644 * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the 1645 * GUP pin will remain consistent with the pages mapped into the page tables 1646 * of the MM. 1647 * 1648 * Temporary unmapping of PageAnonExclusive() pages or clearing of 1649 * PageAnonExclusive() has to protect against concurrent GUP: 1650 * * Ordinary GUP: Using the PT lock 1651 * * GUP-fast and fork(): mm->write_protect_seq 1652 * * GUP-fast and KSM or temporary unmapping (swap, migration): see 1653 * folio_try_share_anon_rmap_*() 1654 * 1655 * Must be called with the (sub)page that's actually referenced via the 1656 * page table entry, which might not necessarily be the head page for a 1657 * PTE-mapped THP. 1658 * 1659 * If the vma is NULL, we're coming from the GUP-fast path and might have 1660 * to fallback to the slow path just to lookup the vma. 1661 */ 1662 static inline bool gup_must_unshare(struct vm_area_struct *vma, 1663 unsigned int flags, struct page *page) 1664 { 1665 /* 1666 * FOLL_WRITE is implicitly handled correctly as the page table entry 1667 * has to be writable -- and if it references (part of) an anonymous 1668 * folio, that part is required to be marked exclusive. 1669 */ 1670 if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN) 1671 return false; 1672 /* 1673 * Note: PageAnon(page) is stable until the page is actually getting 1674 * freed. 1675 */ 1676 if (!PageAnon(page)) { 1677 /* 1678 * We only care about R/O long-term pining: R/O short-term 1679 * pinning does not have the semantics to observe successive 1680 * changes through the process page tables. 1681 */ 1682 if (!(flags & FOLL_LONGTERM)) 1683 return false; 1684 1685 /* We really need the vma ... */ 1686 if (!vma) 1687 return true; 1688 1689 /* 1690 * ... because we only care about writable private ("COW") 1691 * mappings where we have to break COW early. 1692 */ 1693 return is_cow_mapping(vma->vm_flags); 1694 } 1695 1696 /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */ 1697 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) 1698 smp_rmb(); 1699 1700 /* 1701 * Note that KSM pages cannot be exclusive, and consequently, 1702 * cannot get pinned. 1703 */ 1704 return !PageAnonExclusive(page); 1705 } 1706 1707 extern bool mirrored_kernelcore; 1708 bool memblock_has_mirror(void); 1709 void memblock_free_all(void); 1710 1711 static __always_inline void vma_set_range(struct vm_area_struct *vma, 1712 unsigned long start, unsigned long end, 1713 pgoff_t pgoff) 1714 { 1715 vma->vm_start = start; 1716 vma->vm_end = end; 1717 vma->vm_pgoff = pgoff; 1718 } 1719 1720 static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) 1721 { 1722 /* 1723 * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty 1724 * enablements, because when without soft-dirty being compiled in, 1725 * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) 1726 * will be constantly true. 1727 */ 1728 if (!pgtable_supports_soft_dirty()) 1729 return false; 1730 1731 /* 1732 * Soft-dirty is kind of special: its tracking is enabled when the 1733 * vma flags not set. 1734 */ 1735 return !(vma->vm_flags & VM_SOFTDIRTY); 1736 } 1737 1738 static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd) 1739 { 1740 return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd); 1741 } 1742 1743 static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte) 1744 { 1745 return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte); 1746 } 1747 1748 void __meminit __init_single_page(struct page *page, unsigned long pfn, 1749 unsigned long zone, int nid); 1750 void __meminit __init_page_from_nid(unsigned long pfn, int nid); 1751 1752 /* shrinker related functions */ 1753 unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg, 1754 int priority); 1755 1756 int shmem_add_to_page_cache(struct folio *folio, 1757 struct address_space *mapping, 1758 pgoff_t index, void *expected, gfp_t gfp); 1759 int shmem_inode_acct_blocks(struct inode *inode, long pages); 1760 bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped); 1761 1762 #ifdef CONFIG_SHRINKER_DEBUG 1763 static inline __printf(2, 0) int shrinker_debugfs_name_alloc( 1764 struct shrinker *shrinker, const char *fmt, va_list ap) 1765 { 1766 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap); 1767 1768 return shrinker->name ? 0 : -ENOMEM; 1769 } 1770 1771 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1772 { 1773 kfree_const(shrinker->name); 1774 shrinker->name = NULL; 1775 } 1776 1777 extern int shrinker_debugfs_add(struct shrinker *shrinker); 1778 extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1779 int *debugfs_id); 1780 extern void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1781 int debugfs_id); 1782 #else /* CONFIG_SHRINKER_DEBUG */ 1783 static inline int shrinker_debugfs_add(struct shrinker *shrinker) 1784 { 1785 return 0; 1786 } 1787 static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker, 1788 const char *fmt, va_list ap) 1789 { 1790 return 0; 1791 } 1792 static inline void shrinker_debugfs_name_free(struct shrinker *shrinker) 1793 { 1794 } 1795 static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, 1796 int *debugfs_id) 1797 { 1798 *debugfs_id = -1; 1799 return NULL; 1800 } 1801 static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, 1802 int debugfs_id) 1803 { 1804 } 1805 #endif /* CONFIG_SHRINKER_DEBUG */ 1806 1807 /* Only track the nodes of mappings with shadow entries */ 1808 void workingset_update_node(struct xa_node *node); 1809 extern struct list_lru shadow_nodes; 1810 #define mapping_set_update(xas, mapping) do { \ 1811 if (!dax_mapping(mapping) && !shmem_mapping(mapping)) { \ 1812 xas_set_update(xas, workingset_update_node); \ 1813 xas_set_lru(xas, &shadow_nodes); \ 1814 } \ 1815 } while (0) 1816 1817 /* mremap.c */ 1818 unsigned long move_page_tables(struct pagetable_move_control *pmc); 1819 1820 #ifdef CONFIG_UNACCEPTED_MEMORY 1821 void accept_page(struct page *page); 1822 #else /* CONFIG_UNACCEPTED_MEMORY */ 1823 static inline void accept_page(struct page *page) 1824 { 1825 } 1826 #endif /* CONFIG_UNACCEPTED_MEMORY */ 1827 1828 /* pagewalk.c */ 1829 int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start, 1830 unsigned long end, const struct mm_walk_ops *ops, 1831 void *private); 1832 int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start, 1833 unsigned long end, const struct mm_walk_ops *ops, 1834 void *private); 1835 int walk_page_range_debug(struct mm_struct *mm, unsigned long start, 1836 unsigned long end, const struct mm_walk_ops *ops, 1837 pgd_t *pgd, void *private); 1838 1839 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm); 1840 int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm); 1841 1842 void remap_pfn_range_prepare(struct vm_area_desc *desc, unsigned long pfn); 1843 int remap_pfn_range_complete(struct vm_area_struct *vma, unsigned long addr, 1844 unsigned long pfn, unsigned long size, pgprot_t pgprot); 1845 1846 static inline void io_remap_pfn_range_prepare(struct vm_area_desc *desc, 1847 unsigned long orig_pfn, unsigned long size) 1848 { 1849 const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size); 1850 1851 return remap_pfn_range_prepare(desc, pfn); 1852 } 1853 1854 static inline int io_remap_pfn_range_complete(struct vm_area_struct *vma, 1855 unsigned long addr, unsigned long orig_pfn, unsigned long size, 1856 pgprot_t orig_prot) 1857 { 1858 const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size); 1859 const pgprot_t prot = pgprot_decrypted(orig_prot); 1860 1861 return remap_pfn_range_complete(vma, addr, pfn, size, prot); 1862 } 1863 1864 #ifdef CONFIG_MMU_NOTIFIER 1865 static inline bool clear_flush_young_ptes_notify(struct vm_area_struct *vma, 1866 unsigned long addr, pte_t *ptep, unsigned int nr) 1867 { 1868 bool young; 1869 1870 young = clear_flush_young_ptes(vma, addr, ptep, nr); 1871 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, 1872 addr + nr * PAGE_SIZE); 1873 return young; 1874 } 1875 1876 static inline bool pmdp_clear_flush_young_notify(struct vm_area_struct *vma, 1877 unsigned long addr, pmd_t *pmdp) 1878 { 1879 bool young; 1880 1881 young = pmdp_clear_flush_young(vma, addr, pmdp); 1882 young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, addr + PMD_SIZE); 1883 return young; 1884 } 1885 1886 static inline bool test_and_clear_young_ptes_notify(struct vm_area_struct *vma, 1887 unsigned long addr, pte_t *ptep, unsigned int nr) 1888 { 1889 bool young; 1890 1891 young = test_and_clear_young_ptes(vma, addr, ptep, nr); 1892 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + nr * PAGE_SIZE); 1893 return young; 1894 } 1895 1896 static inline bool pmdp_test_and_clear_young_notify(struct vm_area_struct *vma, 1897 unsigned long addr, pmd_t *pmdp) 1898 { 1899 bool young; 1900 1901 young = pmdp_test_and_clear_young(vma, addr, pmdp); 1902 young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + PMD_SIZE); 1903 return young; 1904 } 1905 1906 #else /* CONFIG_MMU_NOTIFIER */ 1907 1908 #define clear_flush_young_ptes_notify clear_flush_young_ptes 1909 #define pmdp_clear_flush_young_notify pmdp_clear_flush_young 1910 #define test_and_clear_young_ptes_notify test_and_clear_young_ptes 1911 #define pmdp_test_and_clear_young_notify pmdp_test_and_clear_young 1912 1913 #endif /* CONFIG_MMU_NOTIFIER */ 1914 1915 extern int sysctl_max_map_count; 1916 static inline int get_sysctl_max_map_count(void) 1917 { 1918 return READ_ONCE(sysctl_max_map_count); 1919 } 1920 1921 bool may_expand_vm(struct mm_struct *mm, const vma_flags_t *vma_flags, 1922 unsigned long npages); 1923 1924 #endif /* __MM_INTERNAL_H */ 1925