1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_MM_H 3#define _LINUX_MM_H 4 5#include <linux/errno.h> 6 7#ifdef __KERNEL__ 8 9#include <linux/mmdebug.h> 10#include <linux/gfp.h> 11#include <linux/bug.h> 12#include <linux/list.h> 13#include <linux/mmzone.h> 14#include <linux/rbtree.h> 15#include <linux/atomic.h> 16#include <linux/debug_locks.h> 17#include <linux/mm_types.h> 18#include <linux/mmap_lock.h> 19#include <linux/range.h> 20#include <linux/pfn.h> 21#include <linux/percpu-refcount.h> 22#include <linux/bit_spinlock.h> 23#include <linux/shrinker.h> 24#include <linux/resource.h> 25#include <linux/page_ext.h> 26#include <linux/err.h> 27#include <linux/page-flags.h> 28#include <linux/page_ref.h> 29#include <linux/memremap.h> 30#include <linux/overflow.h> 31#include <linux/sizes.h> 32#include <linux/sched.h> 33#include <linux/pgtable.h> 34 35struct mempolicy; 36struct anon_vma; 37struct anon_vma_chain; 38struct file_ra_state; 39struct user_struct; 40struct writeback_control; 41struct bdi_writeback; 42struct pt_regs; 43 44extern int sysctl_page_lock_unfairness; 45 46void init_mm_internals(void); 47 48#ifndef CONFIG_NEED_MULTIPLE_NODES /* Don't use mapnrs, do it properly */ 49extern unsigned long max_mapnr; 50 51static inline void set_max_mapnr(unsigned long limit) 52{ 53 max_mapnr = limit; 54} 55#else 56static inline void set_max_mapnr(unsigned long limit) 57{ 58} 59#endif 60 61extern atomic_long_t _totalram_pages; 62static inline unsigned long totalram_pages(void) 63{ 64 return (unsigned long)atomic_long_read(&_totalram_pages); 65} 66 67static inline void totalram_pages_inc(void) 68{ 69 atomic_long_inc(&_totalram_pages); 70} 71 72static inline void totalram_pages_dec(void) 73{ 74 atomic_long_dec(&_totalram_pages); 75} 76 77static inline void totalram_pages_add(long count) 78{ 79 atomic_long_add(count, &_totalram_pages); 80} 81 82extern void *high_memory; 83extern int page_cluster; 84 85#ifdef CONFIG_SYSCTL 86extern int sysctl_legacy_va_layout; 87#else 88#define sysctl_legacy_va_layout 0 89#endif 90 91#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS 92extern const int mmap_rnd_bits_min; 93extern const int mmap_rnd_bits_max; 94extern int mmap_rnd_bits __read_mostly; 95#endif 96#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS 97extern const int mmap_rnd_compat_bits_min; 98extern const int mmap_rnd_compat_bits_max; 99extern int mmap_rnd_compat_bits __read_mostly; 100#endif 101 102#include <asm/page.h> 103#include <asm/processor.h> 104 105/* 106 * Architectures that support memory tagging (assigning tags to memory regions, 107 * embedding these tags into addresses that point to these memory regions, and 108 * checking that the memory and the pointer tags match on memory accesses) 109 * redefine this macro to strip tags from pointers. 110 * It's defined as noop for arcitectures that don't support memory tagging. 111 */ 112#ifndef untagged_addr 113#define untagged_addr(addr) (addr) 114#endif 115 116#ifndef __pa_symbol 117#define __pa_symbol(x) __pa(RELOC_HIDE((unsigned long)(x), 0)) 118#endif 119 120#ifndef page_to_virt 121#define page_to_virt(x) __va(PFN_PHYS(page_to_pfn(x))) 122#endif 123 124#ifndef lm_alias 125#define lm_alias(x) __va(__pa_symbol(x)) 126#endif 127 128/* 129 * With CONFIG_CFI_CLANG, the compiler replaces function addresses in 130 * instrumented C code with jump table addresses. Architectures that 131 * support CFI can define this macro to return the actual function address 132 * when needed. 133 */ 134#ifndef function_nocfi 135#define function_nocfi(x) (x) 136#endif 137 138#define MM_ZERO 0 139#define MM_ONE 1 140#define MM_TWO 2 141#define MM_THREE 3 142#define MM_FOUR 4 143#define MM_FIVE 5 144#define MM_SIX 6 145#define MM_SEVEN 7 146#define MM_EIGHT 8 147#define MM_NINE 9 148#define MM_FIFTYSIX 56 149#define MM_SIXTYFOUR 64 150#define MM_SEVENTYTWO 72 151#define MM_EIGHTY 80 152 153/* 154 * To prevent common memory management code establishing 155 * a zero page mapping on a read fault. 156 * This macro should be defined within <asm/pgtable.h>. 157 * s390 does this to prevent multiplexing of hardware bits 158 * related to the physical page in case of virtualization. 159 */ 160#ifndef mm_forbids_zeropage 161#define mm_forbids_zeropage(X) (0) 162#endif 163 164/* 165 * On some architectures it is expensive to call memset() for small sizes. 166 * If an architecture decides to implement their own version of 167 * mm_zero_struct_page they should wrap the defines below in a #ifndef and 168 * define their own version of this macro in <asm/pgtable.h> 169 */ 170#if BITS_PER_LONG == 64 171/* This function must be updated when the size of struct page grows above 80 172 * or reduces below 56. The idea that compiler optimizes out switch() 173 * statement, and only leaves move/store instructions. Also the compiler can 174 * combine write statments if they are both assignments and can be reordered, 175 * this can result in several of the writes here being dropped. 176 */ 177#define mm_zero_struct_page(pp) _mm_zero_struct_page(pp) 178static inline void _mm_zero_struct_page(struct page *page) 179{ 180 unsigned long *_pp = (void *)page; 181 182 /* Check that struct page is either 56, 64, 72, or 80 bytes */ 183 BUILD_BUG_ON(sizeof(struct page) & MM_SEVEN); 184 BUILD_BUG_ON(sizeof(struct page) < MM_FIFTYSIX); 185 BUILD_BUG_ON(sizeof(struct page) > MM_EIGHTY); 186 187 switch (sizeof(struct page)) { 188 case MM_EIGHTY: 189 _pp[MM_NINE] = 0; 190 fallthrough; 191 case MM_SEVENTYTWO: 192 _pp[MM_EIGHT] = 0; 193 fallthrough; 194 case MM_SIXTYFOUR: 195 _pp[MM_SEVEN] = 0; 196 fallthrough; 197 case MM_FIFTYSIX: 198 _pp[MM_SIX] = 0; 199 _pp[MM_FIVE] = 0; 200 _pp[MM_FOUR] = 0; 201 _pp[MM_THREE] = 0; 202 _pp[MM_TWO] = 0; 203 _pp[MM_ONE] = 0; 204 _pp[MM_ZERO] = 0; 205 } 206} 207#else 208#define mm_zero_struct_page(pp) ((void)memset((pp), 0, sizeof(struct page))) 209#endif 210 211/* 212 * Default maximum number of active map areas, this limits the number of vmas 213 * per mm struct. Users can overwrite this number by sysctl but there is a 214 * problem. 215 * 216 * When a program's coredump is generated as ELF format, a section is created 217 * per a vma. In ELF, the number of sections is represented in unsigned short. 218 * This means the number of sections should be smaller than 65535 at coredump. 219 * Because the kernel adds some informative sections to a image of program at 220 * generating coredump, we need some margin. The number of extra sections is 221 * 1-3 now and depends on arch. We use "5" as safe margin, here. 222 * 223 * ELF extended numbering allows more than 65535 sections, so 16-bit bound is 224 * not a hard limit any more. Although some userspace tools can be surprised by 225 * that. 226 */ 227#define MAPCOUNT_ELF_CORE_MARGIN (5) 228#define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN) 229 230extern int sysctl_max_map_count; 231 232extern unsigned long sysctl_user_reserve_kbytes; 233extern unsigned long sysctl_admin_reserve_kbytes; 234 235extern int sysctl_overcommit_memory; 236extern int sysctl_overcommit_ratio; 237extern unsigned long sysctl_overcommit_kbytes; 238 239int overcommit_ratio_handler(struct ctl_table *, int, void *, size_t *, loff_t *); 240int overcommit_kbytes_handler(struct ctl_table *, int, void *, size_t *, loff_t *); 241int overcommit_policy_handler(struct ctl_table *, int, void *, size_t *, loff_t *); 242 243#define nth_page(page, n) pfn_to_page(page_to_pfn((page)) + (n)) 244 245/* to align the pointer to the (next) page boundary */ 246#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) 247 248/* test whether an address (unsigned long or pointer) is aligned to PAGE_SIZE */ 249#define PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), PAGE_SIZE) 250 251#define lru_to_page(head) (list_entry((head)->prev, struct page, lru)) 252 253/* 254 * Linux kernel virtual memory manager primitives. 255 * The idea being to have a "virtual" mm in the same way 256 * we have a virtual fs - giving a cleaner interface to the 257 * mm details, and allowing different kinds of memory mappings 258 * (from shared memory to executable loading to arbitrary 259 * mmap() functions). 260 */ 261 262struct vm_area_struct *vm_area_alloc(struct mm_struct *); 263struct vm_area_struct *vm_area_dup(struct vm_area_struct *); 264void vm_area_free(struct vm_area_struct *); 265 266#ifndef CONFIG_MMU 267extern struct rb_root nommu_region_tree; 268extern struct rw_semaphore nommu_region_sem; 269 270extern unsigned int kobjsize(const void *objp); 271#endif 272 273/* 274 * vm_flags in vm_area_struct, see mm_types.h. 275 * When changing, update also include/trace/events/mmflags.h 276 */ 277#define VM_NONE 0x00000000 278 279#define VM_READ 0x00000001 /* currently active flags */ 280#define VM_WRITE 0x00000002 281#define VM_EXEC 0x00000004 282#define VM_SHARED 0x00000008 283 284/* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */ 285#define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */ 286#define VM_MAYWRITE 0x00000020 287#define VM_MAYEXEC 0x00000040 288#define VM_MAYSHARE 0x00000080 289 290#define VM_GROWSDOWN 0x00000100 /* general info on the segment */ 291#define VM_UFFD_MISSING 0x00000200 /* missing pages tracking */ 292#define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */ 293#define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */ 294#define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */ 295 296#define VM_LOCKED 0x00002000 297#define VM_IO 0x00004000 /* Memory mapped I/O or similar */ 298 299/* Used by sys_madvise() */ 300#define VM_SEQ_READ 0x00008000 /* App will access data sequentially */ 301#define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */ 302 303#define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */ 304#define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */ 305#define VM_LOCKONFAULT 0x00080000 /* Lock the pages covered when they are faulted in */ 306#define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */ 307#define VM_NORESERVE 0x00200000 /* should the VM suppress accounting */ 308#define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */ 309#define VM_SYNC 0x00800000 /* Synchronous page faults */ 310#define VM_ARCH_1 0x01000000 /* Architecture-specific flag */ 311#define VM_WIPEONFORK 0x02000000 /* Wipe VMA contents in child. */ 312#define VM_DONTDUMP 0x04000000 /* Do not include in the core dump */ 313 314#ifdef CONFIG_MEM_SOFT_DIRTY 315#define VM_SOFTDIRTY 0x08000000 /* Not soft dirty clean area */ 316#else 317#define VM_SOFTDIRTY 0 318#endif 319 320#define VM_MIXEDMAP 0x10000000 /* Can contain "struct page" and pure PFN pages */ 321#define VM_HUGEPAGE 0x20000000 /* MADV_HUGEPAGE marked this vma */ 322#define VM_NOHUGEPAGE 0x40000000 /* MADV_NOHUGEPAGE marked this vma */ 323#define VM_MERGEABLE 0x80000000 /* KSM may merge identical pages */ 324 325#ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS 326#define VM_HIGH_ARCH_BIT_0 32 /* bit only usable on 64-bit architectures */ 327#define VM_HIGH_ARCH_BIT_1 33 /* bit only usable on 64-bit architectures */ 328#define VM_HIGH_ARCH_BIT_2 34 /* bit only usable on 64-bit architectures */ 329#define VM_HIGH_ARCH_BIT_3 35 /* bit only usable on 64-bit architectures */ 330#define VM_HIGH_ARCH_BIT_4 36 /* bit only usable on 64-bit architectures */ 331#define VM_HIGH_ARCH_BIT_5 37 /* bit only usable on 64-bit architectures */ 332#define VM_HIGH_ARCH_BIT_6 38 /* bit only usable on 64-bit architectures */ 333#define VM_HIGH_ARCH_BIT_7 39 /* bit only usable on 64-bit architectures */ 334#define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0) 335#define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1) 336#define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2) 337#define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3) 338#define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4) 339#define VM_HIGH_ARCH_5 BIT(VM_HIGH_ARCH_BIT_5) 340#define VM_HIGH_ARCH_6 BIT(VM_HIGH_ARCH_BIT_6) 341#define VM_HIGH_ARCH_7 BIT(VM_HIGH_ARCH_BIT_7) 342#endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */ 343 344#ifdef CONFIG_MEM_PURGEABLE 345#define VM_PURGEABLE VM_HIGH_ARCH_5 346#define VM_USEREXPTE VM_HIGH_ARCH_6 347#else /* CONFIG_MEM_PURGEABLE */ 348#define VM_PURGEABLE 0 349#define VM_USEREXPTE 0 350#endif /* CONFIG_MEM_PURGEABLE */ 351 352#ifdef CONFIG_SECURITY_XPM 353#define VM_XPM VM_HIGH_ARCH_7 354#else /* CONFIG_MEM_PURGEABLE */ 355#define VM_XPM VM_NONE 356#endif /* CONFIG_MEM_PURGEABLE */ 357 358#ifdef CONFIG_ARCH_HAS_PKEYS 359#define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0 360#define VM_PKEY_BIT0 VM_HIGH_ARCH_0 /* A protection key is a 4-bit value */ 361#define VM_PKEY_BIT1 VM_HIGH_ARCH_1 /* on x86 and 5-bit value on ppc64 */ 362#define VM_PKEY_BIT2 VM_HIGH_ARCH_2 363#define VM_PKEY_BIT3 VM_HIGH_ARCH_3 364#ifdef CONFIG_PPC 365#define VM_PKEY_BIT4 VM_HIGH_ARCH_4 366#else 367#define VM_PKEY_BIT4 0 368#endif 369#endif /* CONFIG_ARCH_HAS_PKEYS */ 370 371#if defined(CONFIG_X86) 372#define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */ 373#elif defined(CONFIG_PPC) 374#define VM_SAO VM_ARCH_1 /* Strong Access Ordering (powerpc) */ 375#elif defined(CONFIG_PARISC) 376#define VM_GROWSUP VM_ARCH_1 377#elif defined(CONFIG_IA64) 378#define VM_GROWSUP VM_ARCH_1 379#elif defined(CONFIG_SPARC64) 380#define VM_SPARC_ADI VM_ARCH_1 /* Uses ADI tag for access control */ 381#define VM_ARCH_CLEAR VM_SPARC_ADI 382#elif defined(CONFIG_ARM64) 383#define VM_ARM64_BTI VM_ARCH_1 /* BTI guarded page, a.k.a. GP bit */ 384#define VM_ARCH_CLEAR VM_ARM64_BTI 385#elif !defined(CONFIG_MMU) 386#define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */ 387#endif 388 389#if defined(CONFIG_ARM64_MTE) 390#define VM_MTE VM_HIGH_ARCH_0 /* Use Tagged memory for access control */ 391#define VM_MTE_ALLOWED VM_HIGH_ARCH_1 /* Tagged memory permitted */ 392#else 393#define VM_MTE VM_NONE 394#define VM_MTE_ALLOWED VM_NONE 395#endif 396 397#ifndef VM_GROWSUP 398#define VM_GROWSUP VM_NONE 399#endif 400 401/* Bits set in the VMA until the stack is in its final location */ 402#define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ) 403 404#define TASK_EXEC ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0) 405 406/* Common data flag combinations */ 407#define VM_DATA_FLAGS_TSK_EXEC (VM_READ | VM_WRITE | TASK_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) 408#define VM_DATA_FLAGS_NON_EXEC (VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) 409#define VM_DATA_FLAGS_EXEC (VM_READ | VM_WRITE | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) 410 411#ifndef VM_DATA_DEFAULT_FLAGS /* arch can override this */ 412#define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_EXEC 413#endif 414 415#ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */ 416#define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS 417#endif 418 419#ifdef CONFIG_STACK_GROWSUP 420#define VM_STACK VM_GROWSUP 421#else 422#define VM_STACK VM_GROWSDOWN 423#endif 424 425#define VM_STACK_FLAGS (VM_STACK | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT) 426 427/* VMA basic access permission flags */ 428#define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC) 429 430/* 431 * Special vmas that are non-mergable, non-mlock()able. 432 */ 433#define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP) 434 435/* This mask prevents VMA from being scanned with khugepaged */ 436#define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB) 437 438/* This mask defines which mm->def_flags a process can inherit its parent */ 439#define VM_INIT_DEF_MASK VM_NOHUGEPAGE 440 441/* This mask is used to clear all the VMA flags used by mlock */ 442#define VM_LOCKED_CLEAR_MASK (~(VM_LOCKED | VM_LOCKONFAULT)) 443 444/* Arch-specific flags to clear when updating VM flags on protection change */ 445#ifndef VM_ARCH_CLEAR 446#define VM_ARCH_CLEAR VM_NONE 447#endif 448#define VM_FLAGS_CLEAR (ARCH_VM_PKEY_FLAGS | VM_ARCH_CLEAR) 449 450/* 451 * mapping from the currently active vm_flags protection bits (the 452 * low four bits) to a page protection mask.. 453 */ 454extern pgprot_t protection_map[16]; 455 456/** 457 * Fault flag definitions. 458 * 459 * @FAULT_FLAG_WRITE: Fault was a write fault. 460 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE. 461 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked. 462 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying. 463 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region. 464 * @FAULT_FLAG_TRIED: The fault has been tried once. 465 * @FAULT_FLAG_USER: The fault originated in userspace. 466 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm. 467 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. 468 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. 469 * 470 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify 471 * whether we would allow page faults to retry by specifying these two 472 * fault flags correctly. Currently there can be three legal combinations: 473 * 474 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and 475 * this is the first try 476 * 477 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and 478 * we've already tried at least once 479 * 480 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry 481 * 482 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never 483 * be used. Note that page faults can be allowed to retry for multiple times, 484 * in which case we'll have an initial fault with flags (a) then later on 485 * continuous faults with flags (b). We should always try to detect pending 486 * signals before a retry to make sure the continuous page faults can still be 487 * interrupted if necessary. 488 */ 489#define FAULT_FLAG_WRITE 0x01 490#define FAULT_FLAG_MKWRITE 0x02 491#define FAULT_FLAG_ALLOW_RETRY 0x04 492#define FAULT_FLAG_RETRY_NOWAIT 0x08 493#define FAULT_FLAG_KILLABLE 0x10 494#define FAULT_FLAG_TRIED 0x20 495#define FAULT_FLAG_USER 0x40 496#define FAULT_FLAG_REMOTE 0x80 497#define FAULT_FLAG_INSTRUCTION 0x100 498#define FAULT_FLAG_INTERRUPTIBLE 0x200 499 500/* 501 * The default fault flags that should be used by most of the 502 * arch-specific page fault handlers. 503 */ 504#define FAULT_FLAG_DEFAULT (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE | FAULT_FLAG_INTERRUPTIBLE) 505 506/** 507 * fault_flag_allow_retry_first - check ALLOW_RETRY the first time 508 * 509 * This is mostly used for places where we want to try to avoid taking 510 * the mmap_lock for too long a time when waiting for another condition 511 * to change, in which case we can try to be polite to release the 512 * mmap_lock in the first round to avoid potential starvation of other 513 * processes that would also want the mmap_lock. 514 * 515 * Return: true if the page fault allows retry and this is the first 516 * attempt of the fault handling; false otherwise. 517 */ 518static inline bool fault_flag_allow_retry_first(unsigned int flags) 519{ 520 return (flags & FAULT_FLAG_ALLOW_RETRY) && (!(flags & FAULT_FLAG_TRIED)); 521} 522 523#define FAULT_FLAG_TRACE \ 524 {FAULT_FLAG_WRITE, "WRITE"}, {FAULT_FLAG_MKWRITE, "MKWRITE"}, {FAULT_FLAG_ALLOW_RETRY, "ALLOW_RETRY"}, \ 525 {FAULT_FLAG_RETRY_NOWAIT, "RETRY_NOWAIT"}, {FAULT_FLAG_KILLABLE, "KILLABLE"}, {FAULT_FLAG_TRIED, "TRIED"}, \ 526 {FAULT_FLAG_USER, "USER"}, {FAULT_FLAG_REMOTE, "REMOTE"}, {FAULT_FLAG_INSTRUCTION, "INSTRUCTION"}, \ 527 { \ 528 FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" \ 529 } 530 531/* 532 * vm_fault is filled by the pagefault handler and passed to the vma's 533 * ->fault function. The vma's ->fault is responsible for returning a bitmask 534 * of VM_FAULT_xxx flags that give details about how the fault was handled. 535 * 536 * MM layer fills up gfp_mask for page allocations but fault handler might 537 * alter it if its implementation requires a different allocation context. 538 * 539 * pgoff should be used in favour of virtual_address, if possible. 540 */ 541struct vm_fault { 542 struct vm_area_struct *vma; /* Target VMA */ 543 unsigned int flags; /* FAULT_FLAG_xxx flags */ 544 gfp_t gfp_mask; /* gfp mask to be used for allocations */ 545 pgoff_t pgoff; /* Logical page offset based on vma */ 546 unsigned long address; /* Faulting virtual address */ 547 pmd_t *pmd; /* Pointer to pmd entry matching 548 * the 'address' */ 549 pud_t *pud; /* Pointer to pud entry matching 550 * the 'address' 551 */ 552 pte_t orig_pte; /* Value of PTE at the time of fault */ 553 554 struct page *cow_page; /* Page handler may use for COW fault */ 555 struct page *page; /* ->fault handlers should return a 556 * page here, unless VM_FAULT_NOPAGE 557 * is set (which is also implied by 558 * VM_FAULT_ERROR). 559 */ 560 /* These three entries are valid only while holding ptl lock */ 561 pte_t *pte; /* Pointer to pte entry matching 562 * the 'address'. NULL if the page 563 * table hasn't been allocated. 564 */ 565 spinlock_t *ptl; /* Page table lock. 566 * Protects pte page table if 'pte' 567 * is not NULL, otherwise pmd. 568 */ 569 pgtable_t prealloc_pte; /* Pre-allocated pte page table. 570 * vm_ops->map_pages() calls 571 * alloc_set_pte() from atomic context. 572 * do_fault_around() pre-allocates 573 * page table to avoid allocation from 574 * atomic context. 575 */ 576}; 577 578/* page entry size for vm->huge_fault() */ 579enum page_entry_size { 580 PE_SIZE_PTE = 0, 581 PE_SIZE_PMD, 582 PE_SIZE_PUD, 583}; 584 585/* 586 * These are the virtual MM functions - opening of an area, closing and 587 * unmapping it (needed to keep files on disk up-to-date etc), pointer 588 * to the functions called when a no-page or a wp-page exception occurs. 589 */ 590struct vm_operations_struct { 591 void (*open)(struct vm_area_struct *area); 592 void (*close)(struct vm_area_struct *area); 593 int (*split)(struct vm_area_struct *area, unsigned long addr); 594 int (*mremap)(struct vm_area_struct *area); 595 vm_fault_t (*fault)(struct vm_fault *vmf); 596 vm_fault_t (*huge_fault)(struct vm_fault *vmf, enum page_entry_size pe_size); 597 void (*map_pages)(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); 598 unsigned long (*pagesize)(struct vm_area_struct *area); 599 600 /* notification that a previously read-only page is about to become 601 * writable, if an error is returned it will cause a SIGBUS */ 602 vm_fault_t (*page_mkwrite)(struct vm_fault *vmf); 603 604 /* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */ 605 vm_fault_t (*pfn_mkwrite)(struct vm_fault *vmf); 606 607 /* called by access_process_vm when get_user_pages() fails, typically 608 * for use by special VMAs that can switch between memory and hardware 609 */ 610 int (*access)(struct vm_area_struct *vma, unsigned long addr, void *buf, int len, int write); 611 612 /* Called by the /proc/PID/maps code to ask the vma whether it 613 * has a special name. Returning non-NULL will also cause this 614 * vma to be dumped unconditionally. */ 615 const char *(*name)(struct vm_area_struct *vma); 616 617#ifdef CONFIG_NUMA 618 /* 619 * set_policy() op must add a reference to any non-NULL @new mempolicy 620 * to hold the policy upon return. Caller should pass NULL @new to 621 * remove a policy and fall back to surrounding context--i.e. do not 622 * install a MPOL_DEFAULT policy, nor the task or system default 623 * mempolicy. 624 */ 625 int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new); 626 627 /* 628 * get_policy() op must add reference [mpol_get()] to any policy at 629 * (vma,addr) marked as MPOL_SHARED. The shared policy infrastructure 630 * in mm/mempolicy.c will do this automatically. 631 * get_policy() must NOT add a ref if the policy at (vma,addr) is not 632 * marked as MPOL_SHARED. vma policies are protected by the mmap_lock. 633 * If no [shared/vma] mempolicy exists at the addr, get_policy() op 634 * must return NULL--i.e., do not "fallback" to task or system default 635 * policy. 636 */ 637 struct mempolicy *(*get_policy)(struct vm_area_struct *vma, unsigned long addr); 638#endif 639 /* 640 * Called by vm_normal_page() for special PTEs to find the 641 * page for @addr. This is useful if the default behavior 642 * (using pte_page()) would not find the correct page. 643 */ 644 struct page *(*find_special_page)(struct vm_area_struct *vma, unsigned long addr); 645}; 646 647static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) 648{ 649 static const struct vm_operations_struct dummy_vm_ops = {}; 650 651 memset(vma, 0, sizeof(*vma)); 652 vma->vm_mm = mm; 653 vma->vm_ops = &dummy_vm_ops; 654 INIT_LIST_HEAD(&vma->anon_vma_chain); 655} 656 657static inline void vma_set_anonymous(struct vm_area_struct *vma) 658{ 659 vma->vm_ops = NULL; 660} 661 662static inline bool vma_is_anonymous(struct vm_area_struct *vma) 663{ 664 return !vma->vm_ops; 665} 666 667static inline bool vma_is_temporary_stack(struct vm_area_struct *vma) 668{ 669 int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP); 670 671 if (!maybe_stack) { 672 return false; 673 } 674 675 if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) == VM_STACK_INCOMPLETE_SETUP) { 676 return true; 677 } 678 679 return false; 680} 681 682static inline bool vma_is_foreign(struct vm_area_struct *vma) 683{ 684 if (!current->mm) { 685 return true; 686 } 687 688 if (current->mm != vma->vm_mm) { 689 return true; 690 } 691 692 return false; 693} 694 695static inline bool vma_is_accessible(struct vm_area_struct *vma) 696{ 697 return vma->vm_flags & VM_ACCESS_FLAGS; 698} 699 700#ifdef CONFIG_SHMEM 701/* 702 * The vma_is_shmem is not inline because it is used only by slow 703 * paths in userfault. 704 */ 705bool vma_is_shmem(struct vm_area_struct *vma); 706#else 707static inline bool vma_is_shmem(struct vm_area_struct *vma) 708{ 709 return false; 710} 711#endif 712 713int vma_is_stack_for_current(struct vm_area_struct *vma); 714 715/* flush_tlb_range() takes a vma, not a mm, and can care about flags */ 716#define TLB_FLUSH_VMA(mm, flags) \ 717 { \ 718 .vm_mm = (mm), .vm_flags = (flags) \ 719 } 720 721struct mmu_gather; 722struct inode; 723 724#include <linux/huge_mm.h> 725 726/* 727 * Methods to modify the page usage count. 728 * 729 * What counts for a page usage: 730 * - cache mapping (page->mapping) 731 * - private data (page->private) 732 * - page mapped in a task's page tables, each mapping 733 * is counted separately 734 * 735 * Also, many kernel routines increase the page count before a critical 736 * routine so they can be sure the page doesn't go away from under them. 737 */ 738 739/* 740 * Drop a ref, return true if the refcount fell to zero (the page has no users) 741 */ 742static inline int put_page_testzero(struct page *page) 743{ 744 VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); 745 return page_ref_dec_and_test(page); 746} 747 748/* 749 * Try to grab a ref unless the page has a refcount of zero, return false if 750 * that is the case. 751 * This can be called when MMU is off so it must not access 752 * any of the virtual mappings. 753 */ 754static inline int get_page_unless_zero(struct page *page) 755{ 756 return page_ref_add_unless(page, 1, 0); 757} 758 759extern int page_is_ram(unsigned long pfn); 760 761enum { 762 REGION_INTERSECTS, 763 REGION_DISJOINT, 764 REGION_MIXED, 765}; 766 767int region_intersects(resource_size_t offset, size_t size, unsigned long flags, unsigned long desc); 768 769/* Support for virtually mapped pages */ 770struct page *vmalloc_to_page(const void *addr); 771unsigned long vmalloc_to_pfn(const void *addr); 772 773/* 774 * Determine if an address is within the vmalloc range 775 * 776 * On nommu, vmalloc/vfree wrap through kmalloc/kfree directly, so there 777 * is no special casing required. 778 */ 779 780#ifndef is_ioremap_addr 781#define is_ioremap_addr(x) is_vmalloc_addr(x) 782#endif 783 784#ifdef CONFIG_MMU 785extern bool is_vmalloc_addr(const void *x); 786extern int is_vmalloc_or_module_addr(const void *x); 787#else 788static inline bool is_vmalloc_addr(const void *x) 789{ 790 return false; 791} 792static inline int is_vmalloc_or_module_addr(const void *x) 793{ 794 return 0; 795} 796#endif 797 798extern void *kvmalloc_node(size_t size, gfp_t flags, int node); 799static inline void *kvmalloc(size_t size, gfp_t flags) 800{ 801 return kvmalloc_node(size, flags, NUMA_NO_NODE); 802} 803static inline void *kvzalloc_node(size_t size, gfp_t flags, int node) 804{ 805 return kvmalloc_node(size, flags | __GFP_ZERO, node); 806} 807static inline void *kvzalloc(size_t size, gfp_t flags) 808{ 809 return kvmalloc(size, flags | __GFP_ZERO); 810} 811 812static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) 813{ 814 size_t bytes; 815 816 if (unlikely(check_mul_overflow(n, size, &bytes))) { 817 return NULL; 818 } 819 820 return kvmalloc(bytes, flags); 821} 822 823static inline void *kvcalloc(size_t n, size_t size, gfp_t flags) 824{ 825 return kvmalloc_array(n, size, flags | __GFP_ZERO); 826} 827 828extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags); 829extern void kvfree(const void *addr); 830extern void kvfree_sensitive(const void *addr, size_t len); 831 832static inline int head_compound_mapcount(struct page *head) 833{ 834 return atomic_read(compound_mapcount_ptr(head)) + 1; 835} 836 837/* 838 * Mapcount of compound page as a whole, does not include mapped sub-pages. 839 * 840 * Must be called only for compound pages or any their tail sub-pages. 841 */ 842static inline int compound_mapcount(struct page *page) 843{ 844 VM_BUG_ON_PAGE(!PageCompound(page), page); 845 page = compound_head(page); 846 return head_compound_mapcount(page); 847} 848 849/* 850 * The atomic page->_mapcount, starts from -1: so that transitions 851 * both from it and to it can be tracked, using atomic_inc_and_test 852 * and atomic_add_negative(-1). 853 */ 854static inline void page_mapcount_reset(struct page *page) 855{ 856 atomic_set(&(page)->_mapcount, -1); 857} 858 859int __page_mapcount(struct page *page); 860 861/* 862 * Mapcount of 0-order page; when compound sub-page, includes 863 * compound_mapcount(). 864 * 865 * Result is undefined for pages which cannot be mapped into userspace. 866 * For example SLAB or special types of pages. See function page_has_type(). 867 * They use this place in struct page differently. 868 */ 869static inline int page_mapcount(struct page *page) 870{ 871 if (unlikely(PageCompound(page))) { 872 return __page_mapcount(page); 873 } 874 return atomic_read(&page->_mapcount) + 1; 875} 876 877#ifdef CONFIG_TRANSPARENT_HUGEPAGE 878int total_mapcount(struct page *page); 879int page_trans_huge_mapcount(struct page *page, int *total_mapcount); 880#else 881static inline int total_mapcount(struct page *page) 882{ 883 return page_mapcount(page); 884} 885static inline int page_trans_huge_mapcount(struct page *page, int *total_mapcount) 886{ 887 int mapcount = page_mapcount(page); 888 if (total_mapcount) { 889 *total_mapcount = mapcount; 890 } 891 return mapcount; 892} 893#endif 894 895static inline struct page *virt_to_head_page(const void *x) 896{ 897 struct page *page = virt_to_page(x); 898 899 return compound_head(page); 900} 901 902void __put_page(struct page *page); 903 904void put_pages_list(struct list_head *pages); 905 906void split_page(struct page *page, unsigned int order); 907 908/* 909 * Compound pages have a destructor function. Provide a 910 * prototype for that function and accessor functions. 911 * These are _only_ valid on the head of a compound page. 912 */ 913typedef void compound_page_dtor(struct page *); 914 915/* Keep the enum in sync with compound_page_dtors array in mm/page_alloc.c */ 916enum compound_dtor_id { 917 NULL_COMPOUND_DTOR, 918 COMPOUND_PAGE_DTOR, 919#ifdef CONFIG_HUGETLB_PAGE 920 HUGETLB_PAGE_DTOR, 921#endif 922#ifdef CONFIG_TRANSPARENT_HUGEPAGE 923 TRANSHUGE_PAGE_DTOR, 924#endif 925 NR_COMPOUND_DTORS, 926}; 927extern compound_page_dtor *const compound_page_dtors[NR_COMPOUND_DTORS]; 928 929static inline void set_compound_page_dtor(struct page *page, enum compound_dtor_id compound_dtor) 930{ 931 VM_BUG_ON_PAGE(compound_dtor >= NR_COMPOUND_DTORS, page); 932 page[1].compound_dtor = compound_dtor; 933} 934 935static inline void destroy_compound_page(struct page *page) 936{ 937 VM_BUG_ON_PAGE(page[1].compound_dtor >= NR_COMPOUND_DTORS, page); 938 compound_page_dtors[page[1].compound_dtor](page); 939} 940 941static inline unsigned int compound_order(struct page *page) 942{ 943 if (!PageHead(page)) { 944 return 0; 945 } 946 return page[1].compound_order; 947} 948 949static inline bool hpage_pincount_available(struct page *page) 950{ 951 /* 952 * Can the page->hpage_pinned_refcount field be used? That field is in 953 * the 3rd page of the compound page, so the smallest (2-page) compound 954 * pages cannot support it. 955 */ 956 page = compound_head(page); 957 return PageCompound(page) && compound_order(page) > 1; 958} 959 960static inline int head_compound_pincount(struct page *head) 961{ 962 return atomic_read(compound_pincount_ptr(head)); 963} 964 965static inline int compound_pincount(struct page *page) 966{ 967 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page); 968 page = compound_head(page); 969 return head_compound_pincount(page); 970} 971 972static inline void set_compound_order(struct page *page, unsigned int order) 973{ 974 page[1].compound_order = order; 975 page[1].compound_nr = 1U << order; 976} 977 978/* Returns the number of pages in this potentially compound page. */ 979static inline unsigned long compound_nr(struct page *page) 980{ 981 if (!PageHead(page)) { 982 return 1; 983 } 984 return page[1].compound_nr; 985} 986 987/* Returns the number of bytes in this potentially compound page. */ 988static inline unsigned long page_size(struct page *page) 989{ 990 return PAGE_SIZE << compound_order(page); 991} 992 993/* Returns the number of bits needed for the number of bytes in a page */ 994static inline unsigned int page_shift(struct page *page) 995{ 996 return PAGE_SHIFT + compound_order(page); 997} 998 999void free_compound_page(struct page *page); 1000 1001#ifdef CONFIG_MMU 1002/* 1003 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when 1004 * servicing faults for write access. In the normal case, do always want 1005 * pte_mkwrite. But get_user_pages can cause write faults for mappings 1006 * that do not have writing enabled, when used by access_process_vm. 1007 */ 1008static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) 1009{ 1010 if (likely(vma->vm_flags & VM_WRITE)) { 1011 pte = pte_mkwrite(pte); 1012 } 1013 return pte; 1014} 1015 1016vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct page *page); 1017vm_fault_t finish_fault(struct vm_fault *vmf); 1018vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf); 1019#endif 1020 1021/* 1022 * Multiple processes may "see" the same page. E.g. for untouched 1023 * mappings of /dev/null, all processes see the same page full of 1024 * zeroes, and text pages of executables and shared libraries have 1025 * only one copy in memory, at most, normally. 1026 * 1027 * For the non-reserved pages, page_count(page) denotes a reference count. 1028 * page_count() == 0 means the page is free. page->lru is then used for 1029 * freelist management in the buddy allocator. 1030 * page_count() > 0 means the page has been allocated. 1031 * 1032 * Pages are allocated by the slab allocator in order to provide memory 1033 * to kmalloc and kmem_cache_alloc. In this case, the management of the 1034 * page, and the fields in 'struct page' are the responsibility of mm/slab.c 1035 * unless a particular usage is carefully commented. (the responsibility of 1036 * freeing the kmalloc memory is the caller's, of course). 1037 * 1038 * A page may be used by anyone else who does a __get_free_page(). 1039 * In this case, page_count still tracks the references, and should only 1040 * be used through the normal accessor functions. The top bits of page->flags 1041 * and page->virtual store page management information, but all other fields 1042 * are unused and could be used privately, carefully. The management of this 1043 * page is the responsibility of the one who allocated it, and those who have 1044 * subsequently been given references to it. 1045 * 1046 * The other pages (we may call them "pagecache pages") are completely 1047 * managed by the Linux memory manager: I/O, buffers, swapping etc. 1048 * The following discussion applies only to them. 1049 * 1050 * A pagecache page contains an opaque `private' member, which belongs to the 1051 * page's address_space. Usually, this is the address of a circular list of 1052 * the page's disk buffers. PG_private must be set to tell the VM to call 1053 * into the filesystem to release these pages. 1054 * 1055 * A page may belong to an inode's memory mapping. In this case, page->mapping 1056 * is the pointer to the inode, and page->index is the file offset of the page, 1057 * in units of PAGE_SIZE. 1058 * 1059 * If pagecache pages are not associated with an inode, they are said to be 1060 * anonymous pages. These may become associated with the swapcache, and in that 1061 * case PG_swapcache is set, and page->private is an offset into the swapcache. 1062 * 1063 * In either case (swapcache or inode backed), the pagecache itself holds one 1064 * reference to the page. Setting PG_private should also increment the 1065 * refcount. The each user mapping also has a reference to the page. 1066 * 1067 * The pagecache pages are stored in a per-mapping radix tree, which is 1068 * rooted at mapping->i_pages, and indexed by offset. 1069 * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space 1070 * lists, we instead now tag pages as dirty/writeback in the radix tree. 1071 * 1072 * All pagecache pages may be subject to I/O: 1073 * - inode pages may need to be read from disk, 1074 * - inode pages which have been modified and are MAP_SHARED may need 1075 * to be written back to the inode on disk, 1076 * - anonymous pages (including MAP_PRIVATE file mappings) which have been 1077 * modified may need to be swapped out to swap space and (later) to be read 1078 * back into memory. 1079 */ 1080 1081/* 1082 * The zone field is never updated after free_area_init_core() 1083 * sets it, so none of the operations on it need to be atomic. 1084 */ 1085 1086/* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_CPUPID] | ... | FLAGS | */ 1087#define SECTIONS_PGOFF ((sizeof(unsigned long) * 8) - SECTIONS_WIDTH) 1088#define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH) 1089#define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH) 1090#define LAST_CPUPID_PGOFF (ZONES_PGOFF - LAST_CPUPID_WIDTH) 1091#define KASAN_TAG_PGOFF (LAST_CPUPID_PGOFF - KASAN_TAG_WIDTH) 1092 1093/* 1094 * Define the bit shifts to access each section. For non-existent 1095 * sections we define the shift as 0; that plus a 0 mask ensures 1096 * the compiler will optimise away reference to them. 1097 */ 1098#define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0)) 1099#define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0)) 1100#define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0)) 1101#define LAST_CPUPID_PGSHIFT (LAST_CPUPID_PGOFF * (LAST_CPUPID_WIDTH != 0)) 1102#define KASAN_TAG_PGSHIFT (KASAN_TAG_PGOFF * (KASAN_TAG_WIDTH != 0)) 1103 1104/* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */ 1105#ifdef NODE_NOT_IN_PAGE_FLAGS 1106#define ZONEID_SHIFT (SECTIONS_SHIFT + ZONES_SHIFT) 1107#define ZONEID_PGOFF ((SECTIONS_PGOFF < ZONES_PGOFF) ? SECTIONS_PGOFF : ZONES_PGOFF) 1108#else 1109#define ZONEID_SHIFT (NODES_SHIFT + ZONES_SHIFT) 1110#define ZONEID_PGOFF ((NODES_PGOFF < ZONES_PGOFF) ? NODES_PGOFF : ZONES_PGOFF) 1111#endif 1112 1113#define ZONEID_PGSHIFT (ZONEID_PGOFF * (ZONEID_SHIFT != 0)) 1114 1115#define ZONES_MASK ((1UL << ZONES_WIDTH) - 1) 1116#define NODES_MASK ((1UL << NODES_WIDTH) - 1) 1117#define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1) 1118#define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1) 1119#define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1) 1120#define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1) 1121 1122static inline enum zone_type page_zonenum(const struct page *page) 1123{ 1124 ASSERT_EXCLUSIVE_BITS(page->flags, ZONES_MASK << ZONES_PGSHIFT); 1125 return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK; 1126} 1127 1128#ifdef CONFIG_ZONE_DEVICE 1129static inline bool is_zone_device_page(const struct page *page) 1130{ 1131 return page_zonenum(page) == ZONE_DEVICE; 1132} 1133extern void memmap_init_zone_device(struct zone *, unsigned long, unsigned long, struct dev_pagemap *); 1134#else 1135static inline bool is_zone_device_page(const struct page *page) 1136{ 1137 return false; 1138} 1139#endif 1140 1141#ifdef CONFIG_DEV_PAGEMAP_OPS 1142void free_devmap_managed_page(struct page *page); 1143DECLARE_STATIC_KEY_FALSE(devmap_managed_key); 1144 1145static inline bool page_is_devmap_managed(struct page *page) 1146{ 1147 if (!static_branch_unlikely(&devmap_managed_key)) { 1148 return false; 1149 } 1150 if (!is_zone_device_page(page)) { 1151 return false; 1152 } 1153 switch (page->pgmap->type) { 1154 case MEMORY_DEVICE_PRIVATE: 1155 case MEMORY_DEVICE_FS_DAX: 1156 return true; 1157 default: 1158 break; 1159 } 1160 return false; 1161} 1162 1163void put_devmap_managed_page(struct page *page); 1164 1165#else /* CONFIG_DEV_PAGEMAP_OPS */ 1166static inline bool page_is_devmap_managed(struct page *page) 1167{ 1168 return false; 1169} 1170 1171static inline void put_devmap_managed_page(struct page *page) 1172{ 1173} 1174#endif /* CONFIG_DEV_PAGEMAP_OPS */ 1175 1176static inline bool is_device_private_page(const struct page *page) 1177{ 1178 return IS_ENABLED(CONFIG_DEV_PAGEMAP_OPS) && IS_ENABLED(CONFIG_DEVICE_PRIVATE) && is_zone_device_page(page) && 1179 page->pgmap->type == MEMORY_DEVICE_PRIVATE; 1180} 1181 1182static inline bool is_pci_p2pdma_page(const struct page *page) 1183{ 1184 return IS_ENABLED(CONFIG_DEV_PAGEMAP_OPS) && IS_ENABLED(CONFIG_PCI_P2PDMA) && is_zone_device_page(page) && 1185 page->pgmap->type == MEMORY_DEVICE_PCI_P2PDMA; 1186} 1187 1188/* 127: arbitrary random number, small enough to assemble well */ 1189#define page_ref_zero_or_close_to_overflow(page) ((unsigned int)page_ref_count(page) + 127u <= 127u) 1190 1191static inline void get_page(struct page *page) 1192{ 1193 page = compound_head(page); 1194 /* 1195 * Getting a normal page or the head of a compound page 1196 * requires to already have an elevated page->_refcount. 1197 */ 1198 VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page); 1199 page_ref_inc(page); 1200} 1201 1202bool __must_check try_grab_page(struct page *page, unsigned int flags); 1203 1204static inline __must_check bool try_get_page(struct page *page) 1205{ 1206 page = compound_head(page); 1207 if (WARN_ON_ONCE(page_ref_count(page) <= 0)) { 1208 return false; 1209 } 1210 page_ref_inc(page); 1211 return true; 1212} 1213 1214static inline void put_page(struct page *page) 1215{ 1216 page = compound_head(page); 1217 /* 1218 * For devmap managed pages we need to catch refcount transition from 1219 * 2 to 1, when refcount reach one it means the page is free and we 1220 * need to inform the device driver through callback. See 1221 * include/linux/memremap.h and HMM for details. 1222 */ 1223 if (page_is_devmap_managed(page)) { 1224 put_devmap_managed_page(page); 1225 return; 1226 } 1227 1228 if (put_page_testzero(page)) { 1229 __put_page(page); 1230 } 1231} 1232 1233/* 1234 * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload 1235 * the page's refcount so that two separate items are tracked: the original page 1236 * reference count, and also a new count of how many pin_user_pages() calls were 1237 * made against the page. ("gup-pinned" is another term for the latter). 1238 * 1239 * With this scheme, pin_user_pages() becomes special: such pages are marked as 1240 * distinct from normal pages. As such, the unpin_user_page() call (and its 1241 * variants) must be used in order to release gup-pinned pages. 1242 * 1243 * Choice of value: 1244 * 1245 * By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference 1246 * counts with respect to pin_user_pages() and unpin_user_page() becomes 1247 * simpler, due to the fact that adding an even power of two to the page 1248 * refcount has the effect of using only the upper N bits, for the code that 1249 * counts up using the bias value. This means that the lower bits are left for 1250 * the exclusive use of the original code that increments and decrements by one 1251 * (or at least, by much smaller values than the bias value). 1252 * 1253 * Of course, once the lower bits overflow into the upper bits (and this is 1254 * OK, because subtraction recovers the original values), then visual inspection 1255 * no longer suffices to directly view the separate counts. However, for normal 1256 * applications that don't have huge page reference counts, this won't be an 1257 * issue. 1258 * 1259 * Locking: the lockless algorithm described in page_cache_get_speculative() 1260 * and page_cache_gup_pin_speculative() provides safe operation for 1261 * get_user_pages and page_mkclean and other calls that race to set up page 1262 * table entries. 1263 */ 1264#define GUP_PIN_COUNTING_BIAS (1U << 10) 1265 1266void unpin_user_page(struct page *page); 1267void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, bool make_dirty); 1268void unpin_user_pages(struct page **pages, unsigned long npages); 1269 1270/** 1271 * page_maybe_dma_pinned() - report if a page is pinned for DMA. 1272 * 1273 * This function checks if a page has been pinned via a call to 1274 * pin_user_pages*(). 1275 * 1276 * For non-huge pages, the return value is partially fuzzy: false is not fuzzy, 1277 * because it means "definitely not pinned for DMA", but true means "probably 1278 * pinned for DMA, but possibly a false positive due to having at least 1279 * GUP_PIN_COUNTING_BIAS worth of normal page references". 1280 * 1281 * False positives are OK, because: a) it's unlikely for a page to get that many 1282 * refcounts, and b) all the callers of this routine are expected to be able to 1283 * deal gracefully with a false positive. 1284 * 1285 * For huge pages, the result will be exactly correct. That's because we have 1286 * more tracking data available: the 3rd struct page in the compound page is 1287 * used to track the pincount (instead using of the GUP_PIN_COUNTING_BIAS 1288 * scheme). 1289 * 1290 * For more information, please see Documentation/core-api/pin_user_pages.rst. 1291 * 1292 * @page: pointer to page to be queried. 1293 * @Return: True, if it is likely that the page has been "dma-pinned". 1294 * False, if the page is definitely not dma-pinned. 1295 */ 1296static inline bool page_maybe_dma_pinned(struct page *page) 1297{ 1298 if (hpage_pincount_available(page)) { 1299 return compound_pincount(page) > 0; 1300 } 1301 1302 /* 1303 * page_ref_count() is signed. If that refcount overflows, then 1304 * page_ref_count() returns a negative value, and callers will avoid 1305 * further incrementing the refcount. 1306 * 1307 * Here, for that overflow case, use the signed bit to count a little 1308 * bit higher via unsigned math, and thus still get an accurate result. 1309 */ 1310 return ((unsigned int)page_ref_count(compound_head(page))) >= GUP_PIN_COUNTING_BIAS; 1311} 1312 1313#if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP) 1314#define SECTION_IN_PAGE_FLAGS 1315#endif 1316 1317/* 1318 * The identification function is mainly used by the buddy allocator for 1319 * determining if two pages could be buddies. We are not really identifying 1320 * the zone since we could be using the section number id if we do not have 1321 * node id available in page flags. 1322 * We only guarantee that it will return the same value for two combinable 1323 * pages in a zone. 1324 */ 1325static inline int page_zone_id(struct page *page) 1326{ 1327 return (page->flags >> ZONEID_PGSHIFT) & ZONEID_MASK; 1328} 1329 1330#ifdef NODE_NOT_IN_PAGE_FLAGS 1331extern int page_to_nid(const struct page *page); 1332#else 1333static inline int page_to_nid(const struct page *page) 1334{ 1335 struct page *p = (struct page *)page; 1336 1337 return (PF_POISONED_CHECK(p)->flags >> NODES_PGSHIFT) & NODES_MASK; 1338} 1339#endif 1340 1341#ifdef CONFIG_NUMA_BALANCING 1342static inline int cpu_pid_to_cpupid(int cpu, int pid) 1343{ 1344 return ((cpu & LAST__CPU_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK); 1345} 1346 1347static inline int cpupid_to_pid(int cpupid) 1348{ 1349 return cpupid & LAST__PID_MASK; 1350} 1351 1352static inline int cpupid_to_cpu(int cpupid) 1353{ 1354 return (cpupid >> LAST__PID_SHIFT) & LAST__CPU_MASK; 1355} 1356 1357static inline int cpupid_to_nid(int cpupid) 1358{ 1359 return cpu_to_node(cpupid_to_cpu(cpupid)); 1360} 1361 1362static inline bool cpupid_pid_unset(int cpupid) 1363{ 1364 return cpupid_to_pid(cpupid) == (-1 & LAST__PID_MASK); 1365} 1366 1367static inline bool cpupid_cpu_unset(int cpupid) 1368{ 1369 return cpupid_to_cpu(cpupid) == (-1 & LAST__CPU_MASK); 1370} 1371 1372static inline bool _cpupid_match_pid(pid_t task_pid, int cpupid) 1373{ 1374 return (task_pid & LAST__PID_MASK) == cpupid_to_pid(cpupid); 1375} 1376 1377#define cpupid_match_pid(task, cpupid) _cpupid_match_pid(task->pid, cpupid) 1378#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 1379static inline int page_cpupid_xchg_last(struct page *page, int cpupid) 1380{ 1381 return xchg(&page->_last_cpupid, cpupid & LAST_CPUPID_MASK); 1382} 1383 1384static inline int page_cpupid_last(struct page *page) 1385{ 1386 return page->_last_cpupid; 1387} 1388static inline void page_cpupid_reset_last(struct page *page) 1389{ 1390 page->_last_cpupid = -1 & LAST_CPUPID_MASK; 1391} 1392#else 1393static inline int page_cpupid_last(struct page *page) 1394{ 1395 return (page->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK; 1396} 1397 1398extern int page_cpupid_xchg_last(struct page *page, int cpupid); 1399 1400static inline void page_cpupid_reset_last(struct page *page) 1401{ 1402 page->flags |= LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT; 1403} 1404#endif /* LAST_CPUPID_NOT_IN_PAGE_FLAGS */ 1405#else /* !CONFIG_NUMA_BALANCING */ 1406static inline int page_cpupid_xchg_last(struct page *page, int cpupid) 1407{ 1408 return page_to_nid(page); /* XXX */ 1409} 1410 1411static inline int page_cpupid_last(struct page *page) 1412{ 1413 return page_to_nid(page); /* XXX */ 1414} 1415 1416static inline int cpupid_to_nid(int cpupid) 1417{ 1418 return -1; 1419} 1420 1421static inline int cpupid_to_pid(int cpupid) 1422{ 1423 return -1; 1424} 1425 1426static inline int cpupid_to_cpu(int cpupid) 1427{ 1428 return -1; 1429} 1430 1431static inline int cpu_pid_to_cpupid(int nid, int pid) 1432{ 1433 return -1; 1434} 1435 1436static inline bool cpupid_pid_unset(int cpupid) 1437{ 1438 return true; 1439} 1440 1441static inline void page_cpupid_reset_last(struct page *page) 1442{ 1443} 1444 1445static inline bool cpupid_match_pid(struct task_struct *task, int cpupid) 1446{ 1447 return false; 1448} 1449#endif /* CONFIG_NUMA_BALANCING */ 1450 1451#ifdef CONFIG_KASAN_SW_TAGS 1452 1453/* 1454 * KASAN per-page tags are stored xor'ed with 0xff. This allows to avoid 1455 * setting tags for all pages to native kernel tag value 0xff, as the default 1456 * value 0x00 maps to 0xff. 1457 */ 1458 1459static inline u8 page_kasan_tag(const struct page *page) 1460{ 1461 u8 tag; 1462 1463 tag = (page->flags >> KASAN_TAG_PGSHIFT) & KASAN_TAG_MASK; 1464 tag ^= 0xff; 1465 1466 return tag; 1467} 1468 1469static inline void page_kasan_tag_set(struct page *page, u8 tag) 1470{ 1471 tag ^= 0xff; 1472 page->flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT); 1473 page->flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT; 1474} 1475 1476static inline void page_kasan_tag_reset(struct page *page) 1477{ 1478 page_kasan_tag_set(page, 0xff); 1479} 1480#else 1481static inline u8 page_kasan_tag(const struct page *page) 1482{ 1483 return 0xff; 1484} 1485 1486static inline void page_kasan_tag_set(struct page *page, u8 tag) 1487{ 1488} 1489static inline void page_kasan_tag_reset(struct page *page) 1490{ 1491} 1492#endif 1493 1494static inline struct zone *page_zone(const struct page *page) 1495{ 1496 return &NODE_DATA(page_to_nid(page))->node_zones[page_zonenum(page)]; 1497} 1498 1499static inline pg_data_t *page_pgdat(const struct page *page) 1500{ 1501 return NODE_DATA(page_to_nid(page)); 1502} 1503 1504#ifdef SECTION_IN_PAGE_FLAGS 1505static inline void set_page_section(struct page *page, unsigned long section) 1506{ 1507 page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT); 1508 page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT; 1509} 1510 1511static inline unsigned long page_to_section(const struct page *page) 1512{ 1513 return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK; 1514} 1515#endif 1516 1517static inline void set_page_zone(struct page *page, enum zone_type zone) 1518{ 1519 page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT); 1520 page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT; 1521} 1522 1523static inline void set_page_node(struct page *page, unsigned long node) 1524{ 1525 page->flags &= ~(NODES_MASK << NODES_PGSHIFT); 1526 page->flags |= (node & NODES_MASK) << NODES_PGSHIFT; 1527} 1528 1529static inline void set_page_links(struct page *page, enum zone_type zone, unsigned long node, unsigned long pfn) 1530{ 1531 set_page_zone(page, zone); 1532 set_page_node(page, node); 1533#ifdef SECTION_IN_PAGE_FLAGS 1534 set_page_section(page, pfn_to_section_nr(pfn)); 1535#endif 1536} 1537 1538#ifdef CONFIG_MEMCG 1539static inline struct mem_cgroup *page_memcg(struct page *page) 1540{ 1541 return page->mem_cgroup; 1542} 1543static inline struct mem_cgroup *page_memcg_rcu(struct page *page) 1544{ 1545 WARN_ON_ONCE(!rcu_read_lock_held()); 1546 return READ_ONCE(page->mem_cgroup); 1547} 1548#else 1549static inline struct mem_cgroup *page_memcg(struct page *page) 1550{ 1551 return NULL; 1552} 1553static inline struct mem_cgroup *page_memcg_rcu(struct page *page) 1554{ 1555 WARN_ON_ONCE(!rcu_read_lock_held()); 1556 return NULL; 1557} 1558#endif 1559 1560/* 1561 * Some inline functions in vmstat.h depend on page_zone() 1562 */ 1563#include <linux/vmstat.h> 1564 1565static __always_inline void *lowmem_page_address(const struct page *page) 1566{ 1567 return page_to_virt(page); 1568} 1569 1570#if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) 1571#define HASHED_PAGE_VIRTUAL 1572#endif 1573 1574#if defined(WANT_PAGE_VIRTUAL) 1575static inline void *page_address(const struct page *page) 1576{ 1577 return page->virtual; 1578} 1579static inline void set_page_address(struct page *page, void *address) 1580{ 1581 page->virtual = address; 1582} 1583#define page_address_init() \ 1584 do { \ 1585 } while (0) 1586#endif 1587 1588#if defined(HASHED_PAGE_VIRTUAL) 1589void *page_address(const struct page *page); 1590void set_page_address(struct page *page, void *virtual); 1591void page_address_init(void); 1592#endif 1593 1594#if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL) 1595#define page_address(page) lowmem_page_address(page) 1596#define set_page_address(page, address) \ 1597 do { \ 1598 } while (0) 1599#define page_address_init() \ 1600 do { \ 1601 } while (0) 1602#endif 1603 1604extern void *page_rmapping(struct page *page); 1605extern struct anon_vma *page_anon_vma(struct page *page); 1606extern struct address_space *page_mapping(struct page *page); 1607 1608extern struct address_space *__page_file_mapping(struct page *); 1609 1610static inline struct address_space *page_file_mapping(struct page *page) 1611{ 1612 if (unlikely(PageSwapCache(page))) { 1613 return __page_file_mapping(page); 1614 } 1615 1616 return page->mapping; 1617} 1618 1619extern pgoff_t __page_file_index(struct page *page); 1620 1621/* 1622 * Return the pagecache index of the passed page. Regular pagecache pages 1623 * use ->index whereas swapcache pages use swp_offset(->private) 1624 */ 1625static inline pgoff_t page_index(struct page *page) 1626{ 1627 if (unlikely(PageSwapCache(page))) { 1628 return __page_file_index(page); 1629 } 1630 return page->index; 1631} 1632 1633bool page_mapped(struct page *page); 1634struct address_space *page_mapping(struct page *page); 1635struct address_space *page_mapping_file(struct page *page); 1636 1637/* 1638 * Return true only if the page has been allocated with 1639 * ALLOC_NO_WATERMARKS and the low watermark was not 1640 * met implying that the system is under some pressure. 1641 */ 1642static inline bool page_is_pfmemalloc(struct page *page) 1643{ 1644 /* 1645 * Page index cannot be this large so this must be 1646 * a pfmemalloc page. 1647 */ 1648 return page->index == -1UL; 1649} 1650 1651/* 1652 * Only to be called by the page allocator on a freshly allocated 1653 * page. 1654 */ 1655static inline void set_page_pfmemalloc(struct page *page) 1656{ 1657 page->index = -1UL; 1658} 1659 1660static inline void clear_page_pfmemalloc(struct page *page) 1661{ 1662 page->index = 0; 1663} 1664 1665/* 1666 * Can be called by the pagefault handler when it gets a VM_FAULT_OOM. 1667 */ 1668extern void pagefault_out_of_memory(void); 1669 1670#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK) 1671#define offset_in_thp(page, p) ((unsigned long)(p) & (thp_size(page) - 1)) 1672 1673/* 1674 * Flags passed to show_mem() and show_free_areas() to suppress output in 1675 * various contexts. 1676 */ 1677#define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */ 1678 1679extern void show_free_areas(unsigned int flags, nodemask_t *nodemask); 1680 1681#ifdef CONFIG_MMU 1682extern bool can_do_mlock(void); 1683#else 1684static inline bool can_do_mlock(void) 1685{ 1686 return false; 1687} 1688#endif 1689extern int user_shm_lock(size_t, struct user_struct *); 1690extern void user_shm_unlock(size_t, struct user_struct *); 1691 1692/* 1693 * Parameter block passed down to zap_pte_range in exceptional cases. 1694 */ 1695struct zap_details { 1696 struct address_space *check_mapping; /* Check page->mapping if set */ 1697 pgoff_t first_index; /* Lowest page->index to unmap */ 1698 pgoff_t last_index; /* Highest page->index to unmap */ 1699 struct page *single_page; /* Locked page to be unmapped */ 1700}; 1701 1702struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte); 1703struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t pmd); 1704 1705void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, unsigned long size); 1706void zap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size); 1707void unmap_vmas(struct mmu_gather *tlb, struct vm_area_struct *start_vma, unsigned long start, unsigned long end); 1708 1709struct mmu_notifier_range; 1710 1711void free_pgd_range(struct mmu_gather *tlb, unsigned long addr, unsigned long end, unsigned long floor, 1712 unsigned long ceiling); 1713int copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma); 1714int follow_invalidate_pte(struct mm_struct *mm, unsigned long address, struct mmu_notifier_range *range, pte_t **ptepp, 1715 pmd_t **pmdpp, spinlock_t **ptlp); 1716int follow_pte(struct mm_struct *mm, unsigned long address, pte_t **ptepp, spinlock_t **ptlp); 1717int follow_pfn(struct vm_area_struct *vma, unsigned long address, unsigned long *pfn); 1718int follow_phys(struct vm_area_struct *vma, unsigned long address, unsigned int flags, unsigned long *prot, 1719 resource_size_t *phys); 1720int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, void *buf, int len, int write); 1721 1722extern void truncate_pagecache(struct inode *inode, loff_t new); 1723extern void truncate_setsize(struct inode *inode, loff_t newsize); 1724void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to); 1725void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end); 1726int truncate_inode_page(struct address_space *mapping, struct page *page); 1727int generic_error_remove_page(struct address_space *mapping, struct page *page); 1728int invalidate_inode_page(struct page *page); 1729 1730#ifdef CONFIG_MMU 1731extern vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, unsigned int flags, 1732 struct pt_regs *regs); 1733extern int fixup_user_fault(struct mm_struct *mm, unsigned long address, unsigned int fault_flags, bool *unlocked); 1734void unmap_mapping_page(struct page *page); 1735void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, pgoff_t nr, bool even_cows); 1736void unmap_mapping_range(struct address_space *mapping, loff_t const holebegin, loff_t const holelen, int even_cows); 1737#else 1738static inline vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, unsigned int flags, 1739 struct pt_regs *regs) 1740{ 1741 /* should never happen if there's no MMU */ 1742 BUG(); 1743 return VM_FAULT_SIGBUS; 1744} 1745static inline int fixup_user_fault(struct mm_struct *mm, unsigned long address, unsigned int fault_flags, 1746 bool *unlocked) 1747{ 1748 /* should never happen if there's no MMU */ 1749 BUG(); 1750 return -EFAULT; 1751} 1752static inline void unmap_mapping_page(struct page *page) 1753{ 1754} 1755static inline void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, pgoff_t nr, bool even_cows) 1756{ 1757} 1758static inline void unmap_mapping_range(struct address_space *mapping, loff_t const holebegin, loff_t const holelen, 1759 int even_cows) 1760{ 1761} 1762#endif 1763 1764static inline void unmap_shared_mapping_range(struct address_space *mapping, loff_t const holebegin, 1765 loff_t const holelen) 1766{ 1767 unmap_mapping_range(mapping, holebegin, holelen, 0); 1768} 1769 1770extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, unsigned int gup_flags); 1771extern int access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags); 1772extern int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, unsigned long addr, void *buf, int len, 1773 unsigned int gup_flags); 1774 1775long get_user_pages_remote(struct mm_struct *mm, unsigned long start, unsigned long nr_pages, unsigned int gup_flags, 1776 struct page **pages, struct vm_area_struct **vmas, int *locked); 1777long pin_user_pages_remote(struct mm_struct *mm, unsigned long start, unsigned long nr_pages, unsigned int gup_flags, 1778 struct page **pages, struct vm_area_struct **vmas, int *locked); 1779long get_user_pages(unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, 1780 struct vm_area_struct **vmas); 1781long pin_user_pages(unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, 1782 struct vm_area_struct **vmas); 1783long get_user_pages_locked(unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, 1784 int *locked); 1785long pin_user_pages_locked(unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, 1786 int *locked); 1787long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); 1788long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); 1789 1790int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); 1791int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); 1792 1793int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc); 1794int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, struct task_struct *task, 1795 bool bypass_rlim); 1796 1797/* Container for pinned pfns / pages */ 1798struct frame_vector { 1799 unsigned int nr_allocated; /* Number of frames we have space for */ 1800 unsigned int nr_frames; /* Number of frames stored in ptrs array */ 1801 bool got_ref; /* Did we pin pages by getting page ref? */ 1802 bool is_pfns; /* Does array contain pages or pfns? */ 1803 void *ptrs[]; /* Array of pinned pfns / pages. Use 1804 * pfns_vector_pages() or pfns_vector_pfns() 1805 * for access */ 1806}; 1807 1808struct frame_vector *frame_vector_create(unsigned int nr_frames); 1809void frame_vector_destroy(struct frame_vector *vec); 1810int get_vaddr_frames(unsigned long start, unsigned int nr_pfns, unsigned int gup_flags, struct frame_vector *vec); 1811void put_vaddr_frames(struct frame_vector *vec); 1812int frame_vector_to_pages(struct frame_vector *vec); 1813void frame_vector_to_pfns(struct frame_vector *vec); 1814 1815static inline unsigned int frame_vector_count(struct frame_vector *vec) 1816{ 1817 return vec->nr_frames; 1818} 1819 1820static inline struct page **frame_vector_pages(struct frame_vector *vec) 1821{ 1822 if (vec->is_pfns) { 1823 int err = frame_vector_to_pages(vec); 1824 if (err) { 1825 return ERR_PTR(err); 1826 } 1827 } 1828 return (struct page **)(vec->ptrs); 1829} 1830 1831static inline unsigned long *frame_vector_pfns(struct frame_vector *vec) 1832{ 1833 if (!vec->is_pfns) { 1834 frame_vector_to_pfns(vec); 1835 } 1836 return (unsigned long *)(vec->ptrs); 1837} 1838 1839struct kvec; 1840int get_kernel_pages(const struct kvec *iov, int nr_pages, int write, struct page **pages); 1841int get_kernel_page(unsigned long start, int write, struct page **pages); 1842struct page *get_dump_page(unsigned long addr); 1843 1844extern int try_to_release_page(struct page *page, gfp_t gfp_mask); 1845extern void do_invalidatepage(struct page *page, unsigned int offset, unsigned int length); 1846 1847void __set_page_dirty(struct page *, struct address_space *, int warn); 1848int __set_page_dirty_nobuffers(struct page *page); 1849int __set_page_dirty_no_writeback(struct page *page); 1850int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page); 1851void account_page_dirtied(struct page *page, struct address_space *mapping); 1852void account_page_cleaned(struct page *page, struct address_space *mapping, struct bdi_writeback *wb); 1853int set_page_dirty(struct page *page); 1854int set_page_dirty_lock(struct page *page); 1855void __cancel_dirty_page(struct page *page); 1856static inline void cancel_dirty_page(struct page *page) 1857{ 1858 /* Avoid atomic ops, locking, etc. when not actually needed. */ 1859 if (PageDirty(page)) { 1860 __cancel_dirty_page(page); 1861 } 1862} 1863int clear_page_dirty_for_io(struct page *page); 1864 1865int get_cmdline(struct task_struct *task, char *buffer, int buflen); 1866 1867extern unsigned long move_page_tables(struct vm_area_struct *vma, unsigned long old_addr, 1868 struct vm_area_struct *new_vma, unsigned long new_addr, unsigned long len, 1869 bool need_rmap_locks); 1870 1871/* 1872 * Flags used by change_protection(). For now we make it a bitmap so 1873 * that we can pass in multiple flags just like parameters. However 1874 * for now all the callers are only use one of the flags at the same 1875 * time. 1876 */ 1877/* Whether we should allow dirty bit accounting */ 1878#define MM_CP_DIRTY_ACCT (1UL << 0) 1879/* Whether this protection change is for NUMA hints */ 1880#define MM_CP_PROT_NUMA (1UL << 1) 1881/* Whether this change is for write protecting */ 1882#define MM_CP_UFFD_WP (1UL << 2) /* do wp */ 1883#define MM_CP_UFFD_WP_RESOLVE (1UL << 3) /* Resolve wp */ 1884#define MM_CP_UFFD_WP_ALL (MM_CP_UFFD_WP | MM_CP_UFFD_WP_RESOLVE) 1885 1886extern unsigned long change_protection(struct vm_area_struct *vma, unsigned long start, unsigned long end, 1887 pgprot_t newprot, unsigned long cp_flags); 1888extern int mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev, unsigned long start, 1889 unsigned long end, unsigned long newflags); 1890 1891/* 1892 * doesn't attempt to fault and will return short. 1893 */ 1894int get_user_pages_fast_only(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); 1895int pin_user_pages_fast_only(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); 1896 1897static inline bool get_user_page_fast_only(unsigned long addr, unsigned int gup_flags, struct page **pagep) 1898{ 1899 return get_user_pages_fast_only(addr, 1, gup_flags, pagep) == 1; 1900} 1901/* 1902 * per-process(per-mm_struct) statistics. 1903 */ 1904static inline unsigned long get_mm_counter(struct mm_struct *mm, int member) 1905{ 1906 long val = atomic_long_read(&mm->rss_stat.count[member]); 1907#ifdef SPLIT_RSS_COUNTING 1908 /* 1909 * counter is updated in asynchronous manner and may go to minus. 1910 * But it's never be expected number for users. 1911 */ 1912 if (val < 0) { 1913 val = 0; 1914 } 1915#endif 1916 return (unsigned long)val; 1917} 1918 1919void mm_trace_rss_stat(struct mm_struct *mm, int member, long count); 1920 1921#ifdef CONFIG_RSS_THRESHOLD 1922void listen_rss_threshold(struct mm_struct *mm); 1923#endif 1924 1925static inline void add_mm_counter(struct mm_struct *mm, int member, long value) 1926{ 1927 long count = atomic_long_add_return(value, &mm->rss_stat.count[member]); 1928 1929#ifdef CONFIG_RSS_THRESHOLD 1930 listen_rss_threshold(mm); 1931#endif 1932 1933 mm_trace_rss_stat(mm, member, count); 1934} 1935 1936static inline void inc_mm_counter(struct mm_struct *mm, int member) 1937{ 1938 long count = atomic_long_inc_return(&mm->rss_stat.count[member]); 1939 1940#ifdef CONFIG_RSS_THRESHOLD 1941 listen_rss_threshold(mm); 1942#endif 1943 1944 mm_trace_rss_stat(mm, member, count); 1945} 1946 1947static inline void dec_mm_counter(struct mm_struct *mm, int member) 1948{ 1949 long count = atomic_long_dec_return(&mm->rss_stat.count[member]); 1950 1951 mm_trace_rss_stat(mm, member, count); 1952} 1953 1954/* Optimized variant when page is already known not to be PageAnon */ 1955static inline int mm_counter_file(struct page *page) 1956{ 1957 if (PageSwapBacked(page)) { 1958 return MM_SHMEMPAGES; 1959 } 1960 return MM_FILEPAGES; 1961} 1962 1963static inline int mm_counter(struct page *page) 1964{ 1965 if (PageAnon(page)) { 1966 return MM_ANONPAGES; 1967 } 1968 return mm_counter_file(page); 1969} 1970 1971static inline unsigned long get_mm_rss(struct mm_struct *mm) 1972{ 1973 return get_mm_counter(mm, MM_FILEPAGES) + get_mm_counter(mm, MM_ANONPAGES) + get_mm_counter(mm, MM_SHMEMPAGES); 1974} 1975 1976static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm) 1977{ 1978 return max(mm->hiwater_rss, get_mm_rss(mm)); 1979} 1980 1981static inline unsigned long get_mm_hiwater_vm(struct mm_struct *mm) 1982{ 1983 return max(mm->hiwater_vm, mm->total_vm); 1984} 1985 1986static inline void update_hiwater_rss(struct mm_struct *mm) 1987{ 1988 unsigned long _rss = get_mm_rss(mm); 1989 if ((mm)->hiwater_rss < _rss) { 1990 (mm)->hiwater_rss = _rss; 1991 } 1992} 1993 1994static inline void update_hiwater_vm(struct mm_struct *mm) 1995{ 1996 if (mm->hiwater_vm < mm->total_vm) { 1997 mm->hiwater_vm = mm->total_vm; 1998 } 1999} 2000 2001static inline void reset_mm_hiwater_rss(struct mm_struct *mm) 2002{ 2003 mm->hiwater_rss = get_mm_rss(mm); 2004} 2005 2006static inline void setmax_mm_hiwater_rss(unsigned long *maxrss, struct mm_struct *mm) 2007{ 2008 unsigned long hiwater_rss = get_mm_hiwater_rss(mm); 2009 if (*maxrss < hiwater_rss) { 2010 *maxrss = hiwater_rss; 2011 } 2012} 2013 2014#if defined(SPLIT_RSS_COUNTING) 2015void sync_mm_rss(struct mm_struct *mm); 2016#else 2017static inline void sync_mm_rss(struct mm_struct *mm) 2018{ 2019} 2020#endif 2021 2022#ifndef CONFIG_ARCH_HAS_PTE_SPECIAL 2023static inline int pte_special(pte_t pte) 2024{ 2025 return 0; 2026} 2027 2028static inline pte_t pte_mkspecial(pte_t pte) 2029{ 2030 return pte; 2031} 2032#endif 2033 2034#ifndef CONFIG_ARCH_HAS_PTE_DEVMAP 2035static inline int pte_devmap(pte_t pte) 2036{ 2037 return 0; 2038} 2039#endif 2040 2041int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot); 2042 2043extern pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl); 2044static inline pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl) 2045{ 2046 pte_t *ptep; 2047 __cond_lock(*ptl, ptep = __get_locked_pte(mm, addr, ptl)); 2048 return ptep; 2049} 2050 2051#ifdef __PAGETABLE_P4D_FOLDED 2052static inline int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 2053{ 2054 return 0; 2055} 2056#else 2057int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address); 2058#endif 2059 2060#if defined(__PAGETABLE_PUD_FOLDED) || !defined(CONFIG_MMU) 2061static inline int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 2062{ 2063 return 0; 2064} 2065static inline void mm_inc_nr_puds(struct mm_struct *mm) 2066{ 2067} 2068static inline void mm_dec_nr_puds(struct mm_struct *mm) 2069{ 2070} 2071 2072#else 2073int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address); 2074 2075static inline void mm_inc_nr_puds(struct mm_struct *mm) 2076{ 2077 if (mm_pud_folded(mm)) { 2078 return; 2079 } 2080 atomic_long_add(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes); 2081} 2082 2083static inline void mm_dec_nr_puds(struct mm_struct *mm) 2084{ 2085 if (mm_pud_folded(mm)) { 2086 return; 2087 } 2088 atomic_long_sub(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes); 2089} 2090#endif 2091 2092#if defined(__PAGETABLE_PMD_FOLDED) || !defined(CONFIG_MMU) 2093static inline int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 2094{ 2095 return 0; 2096} 2097 2098static inline void mm_inc_nr_pmds(struct mm_struct *mm) 2099{ 2100} 2101static inline void mm_dec_nr_pmds(struct mm_struct *mm) 2102{ 2103} 2104 2105#else 2106int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address); 2107 2108static inline void mm_inc_nr_pmds(struct mm_struct *mm) 2109{ 2110 if (mm_pmd_folded(mm)) { 2111 return; 2112 } 2113 atomic_long_add(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes); 2114} 2115 2116static inline void mm_dec_nr_pmds(struct mm_struct *mm) 2117{ 2118 if (mm_pmd_folded(mm)) { 2119 return; 2120 } 2121 atomic_long_sub(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes); 2122} 2123#endif 2124 2125#ifdef CONFIG_MMU 2126static inline void mm_pgtables_bytes_init(struct mm_struct *mm) 2127{ 2128 atomic_long_set(&mm->pgtables_bytes, 0); 2129} 2130 2131static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm) 2132{ 2133 return atomic_long_read(&mm->pgtables_bytes); 2134} 2135 2136static inline void mm_inc_nr_ptes(struct mm_struct *mm) 2137{ 2138 atomic_long_add(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes); 2139} 2140 2141static inline void mm_dec_nr_ptes(struct mm_struct *mm) 2142{ 2143 atomic_long_sub(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes); 2144} 2145#else 2146 2147static inline void mm_pgtables_bytes_init(struct mm_struct *mm) 2148{ 2149} 2150static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm) 2151{ 2152 return 0; 2153} 2154 2155static inline void mm_inc_nr_ptes(struct mm_struct *mm) 2156{ 2157} 2158static inline void mm_dec_nr_ptes(struct mm_struct *mm) 2159{ 2160} 2161#endif 2162 2163int __pte_alloc(struct mm_struct *mm, pmd_t *pmd); 2164int __pte_alloc_kernel(pmd_t *pmd); 2165 2166#if defined(CONFIG_MMU) 2167 2168static inline p4d_t *p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 2169{ 2170 return (unlikely(pgd_none(*pgd)) && __p4d_alloc(mm, pgd, address)) ? NULL : p4d_offset(pgd, address); 2171} 2172 2173static inline pud_t *pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 2174{ 2175 return (unlikely(p4d_none(*p4d)) && __pud_alloc(mm, p4d, address)) ? NULL : pud_offset(p4d, address); 2176} 2177 2178static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 2179{ 2180 return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address)) ? NULL : pmd_offset(pud, address); 2181} 2182#endif /* CONFIG_MMU */ 2183 2184#if USE_SPLIT_PTE_PTLOCKS 2185#if ALLOC_SPLIT_PTLOCKS 2186void __init ptlock_cache_init(void); 2187extern bool ptlock_alloc(struct page *page); 2188extern void ptlock_free(struct page *page); 2189 2190static inline spinlock_t *ptlock_ptr(struct page *page) 2191{ 2192 return page->ptl; 2193} 2194#else /* ALLOC_SPLIT_PTLOCKS */ 2195static inline void ptlock_cache_init(void) 2196{ 2197} 2198 2199static inline bool ptlock_alloc(struct page *page) 2200{ 2201 return true; 2202} 2203 2204static inline void ptlock_free(struct page *page) 2205{ 2206} 2207 2208static inline spinlock_t *ptlock_ptr(struct page *page) 2209{ 2210 return &page->ptl; 2211} 2212#endif /* ALLOC_SPLIT_PTLOCKS */ 2213 2214static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd) 2215{ 2216 return ptlock_ptr(pmd_page(*pmd)); 2217} 2218 2219static inline bool ptlock_init(struct page *page) 2220{ 2221 /* 2222 * prep_new_page() initialize page->private (and therefore page->ptl) 2223 * with 0. Make sure nobody took it in use in between. 2224 * 2225 * It can happen if arch try to use slab for page table allocation: 2226 * slab code uses page->slab_cache, which share storage with page->ptl. 2227 */ 2228 VM_BUG_ON_PAGE(*(unsigned long *)&page->ptl, page); 2229 if (!ptlock_alloc(page)) { 2230 return false; 2231 } 2232 spin_lock_init(ptlock_ptr(page)); 2233 return true; 2234} 2235 2236#else /* !USE_SPLIT_PTE_PTLOCKS */ 2237/* 2238 * We use mm->page_table_lock to guard all pagetable pages of the mm. 2239 */ 2240static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd) 2241{ 2242 return &mm->page_table_lock; 2243} 2244static inline void ptlock_cache_init(void) 2245{ 2246} 2247static inline bool ptlock_init(struct page *page) 2248{ 2249 return true; 2250} 2251static inline void ptlock_free(struct page *page) 2252{ 2253} 2254#endif /* USE_SPLIT_PTE_PTLOCKS */ 2255 2256static inline void pgtable_init(void) 2257{ 2258 ptlock_cache_init(); 2259 pgtable_cache_init(); 2260} 2261 2262static inline bool pgtable_pte_page_ctor(struct page *page) 2263{ 2264 if (!ptlock_init(page)) { 2265 return false; 2266 } 2267 __SetPageTable(page); 2268 inc_zone_page_state(page, NR_PAGETABLE); 2269 return true; 2270} 2271 2272static inline void pgtable_pte_page_dtor(struct page *page) 2273{ 2274 ptlock_free(page); 2275 __ClearPageTable(page); 2276 dec_zone_page_state(page, NR_PAGETABLE); 2277} 2278 2279#define pte_offset_map_lock(mm, pmd, address, ptlp) \ 2280 ( { \ 2281 spinlock_t *__ptl = pte_lockptr(mm, pmd); \ 2282 pte_t *__pte = pte_offset_map(pmd, address); \ 2283 *(ptlp) = __ptl; \ 2284 spin_lock(__ptl); \ 2285 __pte; \ 2286 }) 2287 2288#define pte_unmap_unlock(pte, ptl) \ 2289 do { \ 2290 spin_unlock(ptl); \ 2291 pte_unmap(pte); \ 2292 } while (0) 2293 2294#define pte_alloc(mm, pmd) (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd)) 2295 2296#define pte_alloc_map(mm, pmd, address) (pte_alloc(mm, pmd) ? NULL : pte_offset_map(pmd, address)) 2297 2298#define pte_alloc_map_lock(mm, pmd, address, ptlp) \ 2299 (pte_alloc(mm, pmd) ? NULL : pte_offset_map_lock(mm, pmd, address, ptlp)) 2300 2301#define pte_alloc_kernel(pmd, address) \ 2302 ((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd)) ? NULL : pte_offset_kernel(pmd, address)) 2303 2304#if USE_SPLIT_PMD_PTLOCKS 2305 2306static struct page *pmd_to_page(pmd_t *pmd) 2307{ 2308 unsigned long mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1); 2309 return virt_to_page((void *)((unsigned long)pmd & mask)); 2310} 2311 2312static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd) 2313{ 2314 return ptlock_ptr(pmd_to_page(pmd)); 2315} 2316 2317static inline bool pmd_ptlock_init(struct page *page) 2318{ 2319#ifdef CONFIG_TRANSPARENT_HUGEPAGE 2320 page->pmd_huge_pte = NULL; 2321#endif 2322 return ptlock_init(page); 2323} 2324 2325static inline void pmd_ptlock_free(struct page *page) 2326{ 2327#ifdef CONFIG_TRANSPARENT_HUGEPAGE 2328 VM_BUG_ON_PAGE(page->pmd_huge_pte, page); 2329#endif 2330 ptlock_free(page); 2331} 2332 2333#define pmd_huge_pte(mm, pmd) (pmd_to_page(pmd)->pmd_huge_pte) 2334 2335#else 2336 2337static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd) 2338{ 2339 return &mm->page_table_lock; 2340} 2341 2342static inline bool pmd_ptlock_init(struct page *page) 2343{ 2344 return true; 2345} 2346static inline void pmd_ptlock_free(struct page *page) 2347{ 2348} 2349 2350#define pmd_huge_pte(mm, pmd) ((mm)->pmd_huge_pte) 2351 2352#endif 2353 2354static inline spinlock_t *pmd_lock(struct mm_struct *mm, pmd_t *pmd) 2355{ 2356 spinlock_t *ptl = pmd_lockptr(mm, pmd); 2357 spin_lock(ptl); 2358 return ptl; 2359} 2360 2361static inline bool pgtable_pmd_page_ctor(struct page *page) 2362{ 2363 if (!pmd_ptlock_init(page)) { 2364 return false; 2365 } 2366 __SetPageTable(page); 2367 inc_zone_page_state(page, NR_PAGETABLE); 2368 return true; 2369} 2370 2371static inline void pgtable_pmd_page_dtor(struct page *page) 2372{ 2373 pmd_ptlock_free(page); 2374 __ClearPageTable(page); 2375 dec_zone_page_state(page, NR_PAGETABLE); 2376} 2377 2378/* 2379 * No scalability reason to split PUD locks yet, but follow the same pattern 2380 * as the PMD locks to make it easier if we decide to. The VM should not be 2381 * considered ready to switch to split PUD locks yet; there may be places 2382 * which need to be converted from page_table_lock. 2383 */ 2384static inline spinlock_t *pud_lockptr(struct mm_struct *mm, pud_t *pud) 2385{ 2386 return &mm->page_table_lock; 2387} 2388 2389static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud) 2390{ 2391 spinlock_t *ptl = pud_lockptr(mm, pud); 2392 2393 spin_lock(ptl); 2394 return ptl; 2395} 2396 2397extern void __init pagecache_init(void); 2398extern void __init free_area_init_memoryless_node(int nid); 2399extern void free_initmem(void); 2400 2401/* 2402 * Free reserved pages within range [PAGE_ALIGN(start), end & PAGE_MASK) 2403 * into the buddy system. The freed pages will be poisoned with pattern 2404 * "poison" if it's within range [0, UCHAR_MAX]. 2405 * Return pages freed into the buddy system. 2406 */ 2407extern unsigned long free_reserved_area(void *start, void *end, int poison, const char *s); 2408 2409#ifdef CONFIG_HIGHMEM 2410/* 2411 * Free a highmem page into the buddy system, adjusting totalhigh_pages 2412 * and totalram_pages. 2413 */ 2414extern void free_highmem_page(struct page *page); 2415#endif 2416 2417extern void adjust_managed_page_count(struct page *page, long count); 2418extern void mem_init_print_info(const char *str); 2419 2420extern void reserve_bootmem_region(phys_addr_t start, phys_addr_t end); 2421 2422/* Free the reserved page into the buddy system, so it gets managed. */ 2423static inline void __free_reserved_page(struct page *page) 2424{ 2425 ClearPageReserved(page); 2426 init_page_count(page); 2427 __free_page(page); 2428} 2429 2430static inline void free_reserved_page(struct page *page) 2431{ 2432 __free_reserved_page(page); 2433 adjust_managed_page_count(page, 1); 2434} 2435 2436static inline void mark_page_reserved(struct page *page) 2437{ 2438 SetPageReserved(page); 2439 adjust_managed_page_count(page, -1); 2440} 2441 2442/* 2443 * Default method to free all the __init memory into the buddy system. 2444 * The freed pages will be poisoned with pattern "poison" if it's within 2445 * range [0, UCHAR_MAX]. 2446 * Return pages freed into the buddy system. 2447 */ 2448static inline unsigned long free_initmem_default(int poison) 2449{ 2450 extern char __init_begin[], __init_end[]; 2451 2452 return free_reserved_area(&__init_begin, &__init_end, poison, "unused kernel"); 2453} 2454 2455static inline unsigned long get_num_physpages(void) 2456{ 2457 int nid; 2458 unsigned long phys_pages = 0; 2459 2460 for_each_online_node(nid) phys_pages += node_present_pages(nid); 2461 2462 return phys_pages; 2463} 2464 2465/* 2466 * Using memblock node mappings, an architecture may initialise its 2467 * zones, allocate the backing mem_map and account for memory holes in an 2468 * architecture independent manner. 2469 * 2470 * An architecture is expected to register range of page frames backed by 2471 * physical memory with memblock_add[_node]() before calling 2472 * free_area_init() passing in the PFN each zone ends at. At a basic 2473 * usage, an architecture is expected to do something like 2474 * 2475 * unsigned long max_zone_pfns[MAX_NR_ZONES] = {max_dma, max_normal_pfn, 2476 * max_highmem_pfn}; 2477 * for_each_valid_physical_page_range() 2478 * memblock_add_node(base, size, nid) 2479 * free_area_init(max_zone_pfns); 2480 */ 2481void free_area_init(unsigned long *max_zone_pfn); 2482unsigned long node_map_pfn_alignment(void); 2483unsigned long __absent_pages_in_range(int nid, unsigned long start_pfn, unsigned long end_pfn); 2484extern unsigned long absent_pages_in_range(unsigned long start_pfn, unsigned long end_pfn); 2485extern void get_pfn_range_for_nid(unsigned int nid, unsigned long *start_pfn, unsigned long *end_pfn); 2486extern unsigned long find_min_pfn_with_active_regions(void); 2487 2488#ifndef CONFIG_NEED_MULTIPLE_NODES 2489static inline int early_pfn_to_nid(unsigned long pfn) 2490{ 2491 return 0; 2492} 2493#else 2494/* please see mm/page_alloc.c */ 2495extern int __meminit early_pfn_to_nid(unsigned long pfn); 2496/* there is a per-arch backend function. */ 2497extern int __meminit __early_pfn_to_nid(unsigned long pfn, struct mminit_pfnnid_cache *state); 2498#endif 2499 2500extern void set_dma_reserve(unsigned long new_dma_reserve); 2501extern void memmap_init_zone(unsigned long, int, unsigned long, unsigned long, unsigned long, enum meminit_context, 2502 struct vmem_altmap *, int migratetype); 2503extern void setup_per_zone_wmarks(void); 2504extern int __meminit init_per_zone_wmark_min(void); 2505extern void mem_init(void); 2506extern void __init mmap_init(void); 2507extern void show_mem(unsigned int flags, nodemask_t *nodemask); 2508extern long si_mem_available(void); 2509extern void si_meminfo(struct sysinfo *val); 2510extern void si_meminfo_node(struct sysinfo *val, int nid); 2511#ifdef __HAVE_ARCH_RESERVED_KERNEL_PAGES 2512extern unsigned long arch_reserved_kernel_pages(void); 2513#endif 2514 2515extern __printf(3, 4) void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...); 2516 2517extern void setup_per_cpu_pageset(void); 2518 2519/* page_alloc.c */ 2520extern int min_free_kbytes; 2521extern int watermark_boost_factor; 2522extern int watermark_scale_factor; 2523extern bool arch_has_descending_max_zone_pfns(void); 2524 2525/* nommu.c */ 2526extern atomic_long_t mmap_pages_allocated; 2527extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t); 2528 2529/* interval_tree.c */ 2530void vma_interval_tree_insert(struct vm_area_struct *node, struct rb_root_cached *root); 2531void vma_interval_tree_insert_after(struct vm_area_struct *node, struct vm_area_struct *prev, 2532 struct rb_root_cached *root); 2533void vma_interval_tree_remove(struct vm_area_struct *node, struct rb_root_cached *root); 2534struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root, unsigned long start, 2535 unsigned long last); 2536struct vm_area_struct *vma_interval_tree_iter_next(struct vm_area_struct *node, unsigned long start, 2537 unsigned long last); 2538 2539#define vma_interval_tree_foreach(vma, root, start, last) \ 2540 for (vma = vma_interval_tree_iter_first(root, start, last); vma; \ 2541 vma = vma_interval_tree_iter_next(vma, start, last)) 2542 2543void anon_vma_interval_tree_insert(struct anon_vma_chain *node, struct rb_root_cached *root); 2544void anon_vma_interval_tree_remove(struct anon_vma_chain *node, struct rb_root_cached *root); 2545struct anon_vma_chain *anon_vma_interval_tree_iter_first(struct rb_root_cached *root, unsigned long start, 2546 unsigned long last); 2547struct anon_vma_chain *anon_vma_interval_tree_iter_next(struct anon_vma_chain *node, unsigned long start, 2548 unsigned long last); 2549#ifdef CONFIG_DEBUG_VM_RB 2550void anon_vma_interval_tree_verify(struct anon_vma_chain *node); 2551#endif 2552 2553#define anon_vma_interval_tree_foreach(avc, root, start, last) \ 2554 for (avc = anon_vma_interval_tree_iter_first(root, start, last); avc; \ 2555 avc = anon_vma_interval_tree_iter_next(avc, start, last)) 2556 2557/* mmap.c */ 2558extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin); 2559extern int __vma_adjust(struct vm_area_struct *vma, unsigned long start, unsigned long end, pgoff_t pgoff, 2560 struct vm_area_struct *insert, struct vm_area_struct *expand); 2561static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start, unsigned long end, pgoff_t pgoff, 2562 struct vm_area_struct *insert) 2563{ 2564 return __vma_adjust(vma, start, end, pgoff, insert, NULL); 2565} 2566extern struct vm_area_struct *vma_merge(struct mm_struct *, struct vm_area_struct *prev, unsigned long addr, 2567 unsigned long end, unsigned long vm_flags, struct anon_vma *, struct file *, 2568 pgoff_t, struct mempolicy *, struct vm_userfaultfd_ctx, struct anon_vma_name *); 2569extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *); 2570extern int __split_vma(struct mm_struct *, struct vm_area_struct *, unsigned long addr, int new_below); 2571extern int split_vma(struct mm_struct *, struct vm_area_struct *, unsigned long addr, int new_below); 2572extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *); 2573extern void __vma_link_rb(struct mm_struct *, struct vm_area_struct *, struct rb_node **, struct rb_node *); 2574extern void unlink_file_vma(struct vm_area_struct *); 2575extern struct vm_area_struct *copy_vma(struct vm_area_struct **, unsigned long addr, unsigned long len, pgoff_t pgoff, 2576 bool *need_rmap_locks); 2577extern void exit_mmap(struct mm_struct *); 2578 2579static inline int check_data_rlimit(unsigned long rlim, unsigned long new, unsigned long start, unsigned long end_data, 2580 unsigned long start_data) 2581{ 2582 if (rlim < RLIM_INFINITY) { 2583 if (((new - start) + (end_data - start_data)) > rlim) { 2584 return -ENOSPC; 2585 } 2586 } 2587 2588 return 0; 2589} 2590 2591extern int mm_take_all_locks(struct mm_struct *mm); 2592extern void mm_drop_all_locks(struct mm_struct *mm); 2593 2594extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file); 2595extern struct file *get_mm_exe_file(struct mm_struct *mm); 2596extern struct file *get_task_exe_file(struct task_struct *task); 2597 2598extern bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long npages); 2599extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages); 2600 2601extern bool vma_is_special_mapping(const struct vm_area_struct *vma, const struct vm_special_mapping *sm); 2602extern struct vm_area_struct *_install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, 2603 unsigned long flags, const struct vm_special_mapping *spec); 2604/* This is an obsolete alternative to _install_special_mapping. */ 2605extern int install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, unsigned long flags, 2606 struct page **pages); 2607 2608unsigned long randomize_stack_top(unsigned long stack_top); 2609unsigned long randomize_page(unsigned long start, unsigned long range); 2610 2611extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); 2612 2613extern unsigned long mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, 2614 unsigned long pgoff, struct list_head *uf); 2615extern unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, 2616 unsigned long flags, unsigned long pgoff, unsigned long *populate, struct list_head *uf); 2617extern int __do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf, bool downgrade); 2618extern int do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf); 2619extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior); 2620 2621#ifdef CONFIG_MMU 2622extern int __mm_populate(unsigned long addr, unsigned long len, int ignore_errors); 2623static inline void mm_populate(unsigned long addr, unsigned long len) 2624{ 2625 /* Ignore errors */ 2626 (void)__mm_populate(addr, len, 1); 2627} 2628#else 2629static inline void mm_populate(unsigned long addr, unsigned long len) 2630{ 2631} 2632#endif 2633 2634/* These take the mm semaphore themselves */ 2635extern int __must_check vm_brk(unsigned long, unsigned long); 2636extern int __must_check vm_brk_flags(unsigned long, unsigned long, unsigned long); 2637extern int vm_munmap(unsigned long, size_t); 2638extern unsigned long __must_check vm_mmap(struct file *, unsigned long, unsigned long, unsigned long, unsigned long, 2639 unsigned long); 2640 2641struct vm_unmapped_area_info { 2642#define VM_UNMAPPED_AREA_TOPDOWN 1 2643 unsigned long flags; 2644 unsigned long length; 2645 unsigned long low_limit; 2646 unsigned long high_limit; 2647 unsigned long align_mask; 2648 unsigned long align_offset; 2649}; 2650 2651extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info); 2652 2653/* truncate.c */ 2654extern void truncate_inode_pages(struct address_space *, loff_t); 2655extern void truncate_inode_pages_range(struct address_space *, loff_t lstart, loff_t lend); 2656extern void truncate_inode_pages_final(struct address_space *); 2657 2658/* generic vm_area_ops exported for stackable file systems */ 2659extern vm_fault_t filemap_fault(struct vm_fault *vmf); 2660extern void filemap_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); 2661extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf); 2662 2663/* mm/page-writeback.c */ 2664int __must_check write_one_page(struct page *page); 2665void task_dirty_inc(struct task_struct *tsk); 2666 2667extern unsigned long stack_guard_gap; 2668/* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */ 2669extern int expand_stack(struct vm_area_struct *vma, unsigned long address); 2670 2671/* CONFIG_STACK_GROWSUP still needs to grow downwards at some places */ 2672extern int expand_downwards(struct vm_area_struct *vma, unsigned long address); 2673#if VM_GROWSUP 2674extern int expand_upwards(struct vm_area_struct *vma, unsigned long address); 2675#else 2676#define expand_upwards(vma, address) (0) 2677#endif 2678 2679/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ 2680extern struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr); 2681extern struct vm_area_struct *find_vma_prev(struct mm_struct *mm, unsigned long addr, struct vm_area_struct **pprev); 2682 2683/* Look up the first VMA which intersects the interval start_addr..end_addr-1, 2684 NULL if none. Assume start_addr < end_addr. */ 2685static inline struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, unsigned long start_addr, 2686 unsigned long end_addr) 2687{ 2688 struct vm_area_struct *vma = find_vma(mm, start_addr); 2689 2690 if (vma && end_addr <= vma->vm_start) { 2691 vma = NULL; 2692 } 2693 return vma; 2694} 2695 2696static inline unsigned long vm_start_gap(struct vm_area_struct *vma) 2697{ 2698 unsigned long vm_start = vma->vm_start; 2699 2700 if (vma->vm_flags & VM_GROWSDOWN) { 2701 vm_start -= stack_guard_gap; 2702 if (vm_start > vma->vm_start) { 2703 vm_start = 0; 2704 } 2705 } 2706 return vm_start; 2707} 2708 2709static inline unsigned long vm_end_gap(struct vm_area_struct *vma) 2710{ 2711 unsigned long vm_end = vma->vm_end; 2712 2713 if (vma->vm_flags & VM_GROWSUP) { 2714 vm_end += stack_guard_gap; 2715 if (vm_end < vma->vm_end) { 2716 vm_end = -PAGE_SIZE; 2717 } 2718 } 2719 return vm_end; 2720} 2721 2722static inline unsigned long vma_pages(struct vm_area_struct *vma) 2723{ 2724 return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 2725} 2726 2727/* Look up the first VMA which exactly match the interval vm_start ... vm_end */ 2728static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm, unsigned long vm_start, unsigned long vm_end) 2729{ 2730 struct vm_area_struct *vma = find_vma(mm, vm_start); 2731 2732 if (vma && (vma->vm_start != vm_start || vma->vm_end != vm_end)) { 2733 vma = NULL; 2734 } 2735 2736 return vma; 2737} 2738 2739static inline bool range_in_vma(struct vm_area_struct *vma, unsigned long start, unsigned long end) 2740{ 2741 return (vma && vma->vm_start <= start && end <= vma->vm_end); 2742} 2743 2744#ifdef CONFIG_MMU 2745pgprot_t vm_get_page_prot(unsigned long vm_flags); 2746void vma_set_page_prot(struct vm_area_struct *vma); 2747#else 2748static inline pgprot_t vm_get_page_prot(unsigned long vm_flags) 2749{ 2750 return __pgprot(0); 2751} 2752static inline void vma_set_page_prot(struct vm_area_struct *vma) 2753{ 2754 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 2755} 2756#endif 2757 2758#ifdef CONFIG_NUMA_BALANCING 2759unsigned long change_prot_numa(struct vm_area_struct *vma, unsigned long start, unsigned long end); 2760#endif 2761 2762struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr); 2763int remap_pfn_range(struct vm_area_struct *, unsigned long addr, unsigned long pfn, unsigned long size, pgprot_t); 2764int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *); 2765int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, struct page **pages, unsigned long *num); 2766int vm_map_pages(struct vm_area_struct *vma, struct page **pages, unsigned long num); 2767int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, unsigned long num); 2768vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn); 2769vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, pgprot_t pgprot); 2770vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, pfn_t pfn); 2771vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr, pfn_t pfn, pgprot_t pgprot); 2772vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, unsigned long addr, pfn_t pfn); 2773int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len); 2774 2775static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page) 2776{ 2777 int err = vm_insert_page(vma, addr, page); 2778 if (err == -ENOMEM) { 2779 return VM_FAULT_OOM; 2780 } 2781 if (err < 0 && err != -EBUSY) { 2782 return VM_FAULT_SIGBUS; 2783 } 2784 return VM_FAULT_NOPAGE; 2785} 2786 2787#ifndef io_remap_pfn_range 2788static inline int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, 2789 unsigned long size, pgprot_t prot) 2790{ 2791 return remap_pfn_range(vma, addr, pfn, size, pgprot_decrypted(prot)); 2792} 2793#endif 2794 2795static inline vm_fault_t vmf_error(int err) 2796{ 2797 if (err == -ENOMEM) { 2798 return VM_FAULT_OOM; 2799 } 2800 return VM_FAULT_SIGBUS; 2801} 2802 2803struct page *follow_page(struct vm_area_struct *vma, unsigned long address, unsigned int foll_flags); 2804 2805#define FOLL_WRITE 0x01 /* check pte is writable */ 2806#define FOLL_TOUCH 0x02 /* mark page accessed */ 2807#define FOLL_GET 0x04 /* do get_page on page */ 2808#define FOLL_DUMP 0x08 /* give error on hole if it would be zero */ 2809#define FOLL_FORCE 0x10 /* get_user_pages read/write w/o permission */ 2810#define FOLL_NOWAIT \ 2811 0x20 /* if a disk transfer is needed, start the IO \ 2812 * and return without waiting upon it */ 2813#define FOLL_POPULATE 0x40 /* fault in page */ 2814#define FOLL_SPLIT 0x80 /* don't return transhuge pages, split them */ 2815#define FOLL_HWPOISON 0x100 /* check page is hwpoisoned */ 2816#define FOLL_NUMA 0x200 /* force NUMA hinting page fault */ 2817#define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */ 2818#define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */ 2819#define FOLL_MLOCK 0x1000 /* lock present pages */ 2820#define FOLL_REMOTE 0x2000 /* we are working on non-current tsk/mm */ 2821#define FOLL_COW 0x4000 /* internal GUP flag */ 2822#define FOLL_ANON 0x8000 /* don't do file mappings */ 2823#define FOLL_LONGTERM 0x10000 /* mapping lifetime is indefinite: see below */ 2824#define FOLL_SPLIT_PMD 0x20000 /* split huge pmd before returning */ 2825#define FOLL_PIN 0x40000 /* pages must be released via unpin_user_page */ 2826#define FOLL_FAST_ONLY 0x80000 /* gup_fast: prevent fall-back to slow gup */ 2827 2828/* 2829 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each 2830 * other. Here is what they mean, and how to use them: 2831 * 2832 * FOLL_LONGTERM indicates that the page will be held for an indefinite time 2833 * period _often_ under userspace control. This is in contrast to 2834 * iov_iter_get_pages(), whose usages are transient. 2835 * 2836 * For pages which are part of a filesystem, mappings are subject to the 2837 * lifetime enforced by the filesystem and we need guarantees that longterm 2838 * users like RDMA and V4L2 only establish mappings which coordinate usage with 2839 * the filesystem. Ideas for this coordination include revoking the longterm 2840 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was 2841 * added after the problem with filesystems was found FS DAX VMAs are 2842 * specifically failed. Filesystem pages are still subject to bugs and use of 2843 * FOLL_LONGTERM should be avoided on those pages. 2844 * 2845 * Also NOTE that FOLL_LONGTERM is not supported in every GUP call. 2846 * Currently only get_user_pages() and get_user_pages_fast() support this flag 2847 * and calls to get_user_pages_[un]locked are specifically not allowed. This 2848 * is due to an incompatibility with the FS DAX check and 2849 * FAULT_FLAG_ALLOW_RETRY. 2850 * 2851 * In the CMA case: long term pins in a CMA region would unnecessarily fragment 2852 * that region. And so, CMA attempts to migrate the page before pinning, when 2853 * FOLL_LONGTERM is specified. 2854 * 2855 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount, 2856 * but an additional pin counting system) will be invoked. This is intended for 2857 * anything that gets a page reference and then touches page data (for example, 2858 * Direct IO). This lets the filesystem know that some non-file-system entity is 2859 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages 2860 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by 2861 * a call to unpin_user_page(). 2862 * 2863 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different 2864 * and separate refcounting mechanisms, however, and that means that each has 2865 * its own acquire and release mechanisms: 2866 * 2867 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release. 2868 * 2869 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release. 2870 * 2871 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call. 2872 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based 2873 * calls applied to them, and that's perfectly OK. This is a constraint on the 2874 * callers, not on the pages.) 2875 * 2876 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never 2877 * directly by the caller. That's in order to help avoid mismatches when 2878 * releasing pages: get_user_pages*() pages must be released via put_page(), 2879 * while pin_user_pages*() pages must be released via unpin_user_page(). 2880 * 2881 * Please see Documentation/core-api/pin_user_pages.rst for more information. 2882 */ 2883 2884static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags) 2885{ 2886 if (vm_fault & VM_FAULT_OOM) { 2887 return -ENOMEM; 2888 } 2889 if (vm_fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) { 2890 return (foll_flags & FOLL_HWPOISON) ? -EHWPOISON : -EFAULT; 2891 } 2892 if (vm_fault & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) { 2893 return -EFAULT; 2894 } 2895 return 0; 2896} 2897 2898typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data); 2899extern int apply_to_page_range(struct mm_struct *mm, unsigned long address, unsigned long size, pte_fn_t fn, 2900 void *data); 2901extern int apply_to_existing_page_range(struct mm_struct *mm, unsigned long address, unsigned long size, pte_fn_t fn, 2902 void *data); 2903 2904#ifdef CONFIG_PAGE_POISONING 2905extern bool page_poisoning_enabled(void); 2906extern void kernel_poison_pages(struct page *page, int numpages, int enable); 2907#else 2908static inline bool page_poisoning_enabled(void) 2909{ 2910 return false; 2911} 2912static inline bool page_poisoning_enabled_static(void) 2913{ 2914 return false; 2915} 2916static inline void _kernel_poison_pages(struct page *page, int nunmpages) 2917{ 2918} 2919static inline void kernel_poison_pages(struct page *page, int numpages, int enable) 2920{ 2921} 2922#endif 2923 2924#ifdef CONFIG_INIT_ON_ALLOC_DEFAULT_ON 2925DECLARE_STATIC_KEY_TRUE(init_on_alloc); 2926#else 2927DECLARE_STATIC_KEY_FALSE(init_on_alloc); 2928#endif 2929static inline bool want_init_on_alloc(gfp_t flags) 2930{ 2931 if (static_branch_unlikely(&init_on_alloc) && !page_poisoning_enabled()) { 2932 return true; 2933 } 2934 return flags & __GFP_ZERO; 2935} 2936 2937#ifdef CONFIG_INIT_ON_FREE_DEFAULT_ON 2938DECLARE_STATIC_KEY_TRUE(init_on_free); 2939#else 2940DECLARE_STATIC_KEY_FALSE(init_on_free); 2941#endif 2942static inline bool want_init_on_free(void) 2943{ 2944 return static_branch_unlikely(&init_on_free) && !page_poisoning_enabled(); 2945} 2946 2947#ifdef CONFIG_DEBUG_PAGEALLOC 2948extern void init_debug_pagealloc(void); 2949#else 2950static inline void init_debug_pagealloc(void) 2951{ 2952} 2953#endif 2954extern bool _debug_pagealloc_enabled_early; 2955DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled); 2956 2957static inline bool debug_pagealloc_enabled(void) 2958{ 2959 return IS_ENABLED(CONFIG_DEBUG_PAGEALLOC) && _debug_pagealloc_enabled_early; 2960} 2961 2962/* 2963 * For use in fast paths after init_debug_pagealloc() has run, or when a 2964 * false negative result is not harmful when called too early. 2965 */ 2966static inline bool debug_pagealloc_enabled_static(void) 2967{ 2968 if (!IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) { 2969 return false; 2970 } 2971 2972 return static_branch_unlikely(&_debug_pagealloc_enabled); 2973} 2974 2975#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_ARCH_HAS_SET_DIRECT_MAP) 2976extern void __kernel_map_pages(struct page *page, int numpages, int enable); 2977 2978/* 2979 * When called in DEBUG_PAGEALLOC context, the call should most likely be 2980 * guarded by debug_pagealloc_enabled() or debug_pagealloc_enabled_static() 2981 */ 2982static inline void kernel_map_pages(struct page *page, int numpages, int enable) 2983{ 2984 __kernel_map_pages(page, numpages, enable); 2985} 2986#ifdef CONFIG_HIBERNATION 2987extern bool kernel_page_present(struct page *page); 2988#endif /* CONFIG_HIBERNATION */ 2989#else /* CONFIG_DEBUG_PAGEALLOC || CONFIG_ARCH_HAS_SET_DIRECT_MAP */ 2990static inline void kernel_map_pages(struct page *page, int numpages, int enable) 2991{ 2992} 2993#ifdef CONFIG_HIBERNATION 2994static inline bool kernel_page_present(struct page *page) 2995{ 2996 return true; 2997} 2998#endif /* CONFIG_HIBERNATION */ 2999#endif /* CONFIG_DEBUG_PAGEALLOC || CONFIG_ARCH_HAS_SET_DIRECT_MAP */ 3000 3001#ifdef __HAVE_ARCH_GATE_AREA 3002extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm); 3003extern int in_gate_area_no_mm(unsigned long addr); 3004extern int in_gate_area(struct mm_struct *mm, unsigned long addr); 3005#else 3006static inline struct vm_area_struct *get_gate_vma(struct mm_struct *mm) 3007{ 3008 return NULL; 3009} 3010static inline int in_gate_area_no_mm(unsigned long addr) 3011{ 3012 return 0; 3013} 3014static inline int in_gate_area(struct mm_struct *mm, unsigned long addr) 3015{ 3016 return 0; 3017} 3018#endif /* __HAVE_ARCH_GATE_AREA */ 3019 3020extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm); 3021 3022#ifdef CONFIG_SYSCTL 3023extern int sysctl_drop_caches; 3024int drop_caches_sysctl_handler(struct ctl_table *, int, void *, size_t *, loff_t *); 3025#endif 3026 3027void drop_slab(void); 3028void drop_slab_node(int nid); 3029 3030#ifndef CONFIG_MMU 3031#define randomize_va_space 0 3032#else 3033extern int randomize_va_space; 3034#endif 3035 3036const char *arch_vma_name(struct vm_area_struct *vma); 3037#ifdef CONFIG_MMU 3038void print_vma_addr(char *prefix, unsigned long rip); 3039#else 3040static inline void print_vma_addr(char *prefix, unsigned long rip) 3041{ 3042} 3043#endif 3044 3045void *sparse_buffer_alloc(unsigned long size); 3046struct page *__populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap); 3047pgd_t *vmemmap_pgd_populate(unsigned long addr, int node); 3048p4d_t *vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node); 3049pud_t *vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node); 3050pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node); 3051pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node, struct vmem_altmap *altmap); 3052void *vmemmap_alloc_block(unsigned long size, int node); 3053struct vmem_altmap; 3054void *vmemmap_alloc_block_buf(unsigned long size, int node, struct vmem_altmap *altmap); 3055void vmemmap_verify(pte_t *, int, unsigned long, unsigned long); 3056int vmemmap_populate_basepages(unsigned long start, unsigned long end, int node, struct vmem_altmap *altmap); 3057int vmemmap_populate(unsigned long start, unsigned long end, int node, struct vmem_altmap *altmap); 3058void vmemmap_populate_print_last(void); 3059#ifdef CONFIG_MEMORY_HOTPLUG 3060void vmemmap_free(unsigned long start, unsigned long end, struct vmem_altmap *altmap); 3061#endif 3062void register_page_bootmem_memmap(unsigned long section_nr, struct page *map, unsigned long nr_pages); 3063 3064enum mf_flags { 3065 MF_COUNT_INCREASED = 1 << 0, 3066 MF_ACTION_REQUIRED = 1 << 1, 3067 MF_MUST_KILL = 1 << 2, 3068 MF_SOFT_OFFLINE = 1 << 3, 3069}; 3070extern int memory_failure(unsigned long pfn, int flags); 3071extern void memory_failure_queue(unsigned long pfn, int flags); 3072extern void memory_failure_queue_kick(int cpu); 3073extern int unpoison_memory(unsigned long pfn); 3074extern int sysctl_memory_failure_early_kill; 3075extern int sysctl_memory_failure_recovery; 3076extern void shake_page(struct page *p, int access); 3077extern atomic_long_t num_poisoned_pages __read_mostly; 3078extern int soft_offline_page(unsigned long pfn, int flags); 3079 3080/* 3081 * Error handlers for various types of pages. 3082 */ 3083enum mf_result { 3084 MF_IGNORED, /* Error: cannot be handled */ 3085 MF_FAILED, /* Error: handling failed */ 3086 MF_DELAYED, /* Will be handled later */ 3087 MF_RECOVERED, /* Successfully recovered */ 3088}; 3089 3090enum mf_action_page_type { 3091 MF_MSG_KERNEL, 3092 MF_MSG_KERNEL_HIGH_ORDER, 3093 MF_MSG_SLAB, 3094 MF_MSG_DIFFERENT_COMPOUND, 3095 MF_MSG_POISONED_HUGE, 3096 MF_MSG_HUGE, 3097 MF_MSG_FREE_HUGE, 3098 MF_MSG_NON_PMD_HUGE, 3099 MF_MSG_UNMAP_FAILED, 3100 MF_MSG_DIRTY_SWAPCACHE, 3101 MF_MSG_CLEAN_SWAPCACHE, 3102 MF_MSG_DIRTY_MLOCKED_LRU, 3103 MF_MSG_CLEAN_MLOCKED_LRU, 3104 MF_MSG_DIRTY_UNEVICTABLE_LRU, 3105 MF_MSG_CLEAN_UNEVICTABLE_LRU, 3106 MF_MSG_DIRTY_LRU, 3107 MF_MSG_CLEAN_LRU, 3108 MF_MSG_TRUNCATED_LRU, 3109 MF_MSG_BUDDY, 3110 MF_MSG_BUDDY_2ND, 3111 MF_MSG_DAX, 3112 MF_MSG_UNSPLIT_THP, 3113 MF_MSG_UNKNOWN, 3114}; 3115 3116#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 3117extern void clear_huge_page(struct page *page, unsigned long addr_hint, unsigned int pages_per_huge_page); 3118extern void copy_user_huge_page(struct page *dst, struct page *src, unsigned long addr_hint, struct vm_area_struct *vma, 3119 unsigned int pages_per_huge_page); 3120extern long copy_huge_page_from_user(struct page *dst_page, const void __user *usr_src, 3121 unsigned int pages_per_huge_page, bool allow_pagefault); 3122 3123/** 3124 * vma_is_special_huge - Are transhuge page-table entries considered special? 3125 * @vma: Pointer to the struct vm_area_struct to consider 3126 * 3127 * Whether transhuge page-table entries are considered "special" following 3128 * the definition in vm_normal_page(). 3129 * 3130 * Return: true if transhuge page-table entries should be considered special, 3131 * false otherwise. 3132 */ 3133static inline bool vma_is_special_huge(const struct vm_area_struct *vma) 3134{ 3135 return vma_is_dax(vma) || (vma->vm_file && (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))); 3136} 3137 3138#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 3139 3140#ifdef CONFIG_DEBUG_PAGEALLOC 3141extern unsigned int _debug_guardpage_minorder; 3142DECLARE_STATIC_KEY_FALSE(_debug_guardpage_enabled); 3143 3144static inline unsigned int debug_guardpage_minorder(void) 3145{ 3146 return _debug_guardpage_minorder; 3147} 3148 3149static inline bool debug_guardpage_enabled(void) 3150{ 3151 return static_branch_unlikely(&_debug_guardpage_enabled); 3152} 3153 3154static inline bool page_is_guard(struct page *page) 3155{ 3156 if (!debug_guardpage_enabled()) { 3157 return false; 3158 } 3159 3160 return PageGuard(page); 3161} 3162#else 3163static inline unsigned int debug_guardpage_minorder(void) 3164{ 3165 return 0; 3166} 3167static inline bool debug_guardpage_enabled(void) 3168{ 3169 return false; 3170} 3171static inline bool page_is_guard(struct page *page) 3172{ 3173 return false; 3174} 3175#endif /* CONFIG_DEBUG_PAGEALLOC */ 3176 3177#if MAX_NUMNODES > 1 3178void __init setup_nr_node_ids(void); 3179#else 3180static inline void setup_nr_node_ids(void) 3181{ 3182} 3183#endif 3184 3185extern int memcmp_pages(struct page *page1, struct page *page2); 3186 3187static inline int pages_identical(struct page *page1, struct page *page2) 3188{ 3189 return !memcmp_pages(page1, page2); 3190} 3191 3192#ifdef CONFIG_MAPPING_DIRTY_HELPERS 3193unsigned long clean_record_shared_mapping_range(struct address_space *mapping, pgoff_t first_index, pgoff_t nr, 3194 pgoff_t bitmap_pgoff, unsigned long *bitmap, pgoff_t *start, 3195 pgoff_t *end); 3196 3197unsigned long wp_shared_mapping_range(struct address_space *mapping, pgoff_t first_index, pgoff_t nr); 3198#endif 3199 3200extern int sysctl_nr_trim_pages; 3201 3202/** 3203 * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it 3204 * @seals: the seals to check 3205 * @vma: the vma to operate on 3206 * 3207 * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on 3208 * the vma flags. Return 0 if check pass, or <0 for errors. 3209 */ 3210static inline int seal_check_future_write(int seals, struct vm_area_struct *vma) 3211{ 3212 if (seals & F_SEAL_FUTURE_WRITE) { 3213 /* 3214 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when 3215 * "future write" seal active. 3216 */ 3217 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) { 3218 return -EPERM; 3219 } 3220 3221 /* 3222 * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as 3223 * MAP_SHARED and read-only, take care to not allow mprotect to 3224 * revert protections on such mappings. Do this only for shared 3225 * mappings. For private mappings, don't need to mask 3226 * VM_MAYWRITE as we still want them to be COW-writable. 3227 */ 3228 if (vma->vm_flags & VM_SHARED) { 3229 vma->vm_flags &= ~(VM_MAYWRITE); 3230 } 3231 } 3232 3233 return 0; 3234} 3235 3236#ifdef CONFIG_ANON_VMA_NAME 3237int madvise_set_anon_name(struct mm_struct *mm, unsigned long start, unsigned long len_in, 3238 struct anon_vma_name *anon_name); 3239#else 3240static inline int madvise_set_anon_name(struct mm_struct *mm, unsigned long start, unsigned long len_in, 3241 struct anon_vma_name *anon_name) 3242{ 3243 return 0; 3244} 3245#endif 3246 3247#endif /* __KERNEL__ */ 3248#endif /* _LINUX_MM_H */ 3249