1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_MM_H 3 #define _LINUX_MM_H 4 5 #include <linux/args.h> 6 #include <linux/errno.h> 7 #include <linux/mmdebug.h> 8 #include <linux/gfp.h> 9 #include <linux/pgalloc_tag.h> 10 #include <linux/bug.h> 11 #include <linux/list.h> 12 #include <linux/mmzone.h> 13 #include <linux/rbtree.h> 14 #include <linux/atomic.h> 15 #include <linux/debug_locks.h> 16 #include <linux/compiler.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/overflow.h> 30 #include <linux/sched.h> 31 #include <linux/pgtable.h> 32 #include <linux/kasan.h> 33 #include <linux/memremap.h> 34 #include <linux/slab.h> 35 #include <linux/cacheinfo.h> 36 #include <linux/rcuwait.h> 37 #include <linux/bitmap.h> 38 #include <linux/bitops.h> 39 #include <linux/iommu-debug-pagealloc.h> 40 #include <linux/kcsan-checks.h> 41 #include <linux/vmemmap-optimization.h> 42 43 struct mempolicy; 44 struct anon_vma; 45 struct anon_vma_chain; 46 struct user_struct; 47 struct pt_regs; 48 struct folio_batch; 49 50 void arch_mm_preinit(void); 51 void mm_core_init_early(void); 52 void mm_core_init(void); 53 void init_mm_internals(void); 54 55 extern atomic_long_t _totalram_pages; 56 static inline unsigned long totalram_pages(void) 57 { 58 return (unsigned long)atomic_long_read(&_totalram_pages); 59 } 60 61 static inline void totalram_pages_add(long count) 62 { 63 atomic_long_add(count, &_totalram_pages); 64 } 65 66 extern void * high_memory; 67 68 /* 69 * Convert between pages and MB 70 * 20 is the shift for 1MB (2^20 = 1MB) 71 * PAGE_SHIFT is the shift for page size (e.g., 12 for 4KB pages) 72 * So (20 - PAGE_SHIFT) converts between pages and MB 73 */ 74 #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT)) 75 #define MB_TO_PAGES(mb) ((mb) << (20 - PAGE_SHIFT)) 76 77 #ifdef CONFIG_SYSCTL 78 extern int sysctl_legacy_va_layout; 79 #else 80 #define sysctl_legacy_va_layout 0 81 #endif 82 83 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS 84 extern const int mmap_rnd_bits_min; 85 extern int mmap_rnd_bits_max __ro_after_init; 86 extern int mmap_rnd_bits __read_mostly; 87 #endif 88 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS 89 extern const int mmap_rnd_compat_bits_min; 90 extern const int mmap_rnd_compat_bits_max; 91 extern int mmap_rnd_compat_bits __read_mostly; 92 #endif 93 94 #ifndef DIRECT_MAP_PHYSMEM_END 95 # ifdef MAX_PHYSMEM_BITS 96 # define DIRECT_MAP_PHYSMEM_END ((1ULL << MAX_PHYSMEM_BITS) - 1) 97 # else 98 # define DIRECT_MAP_PHYSMEM_END (((phys_addr_t)-1)&~(1ULL<<63)) 99 # endif 100 #endif 101 102 #define INVALID_PHYS_ADDR (~(phys_addr_t)0) 103 104 #include <asm/page.h> 105 #include <asm/processor.h> 106 107 #ifndef __pa_symbol 108 #define __pa_symbol(x) __pa(RELOC_HIDE((unsigned long)(x), 0)) 109 #endif 110 111 #ifndef page_to_virt 112 #define page_to_virt(x) __va(PFN_PHYS(page_to_pfn(x))) 113 #endif 114 115 #ifndef lm_alias 116 #define lm_alias(x) __va(__pa_symbol(x)) 117 #endif 118 119 /* 120 * To prevent common memory management code establishing 121 * a zero page mapping on a read fault. 122 * This macro should be defined within <asm/pgtable.h>. 123 * s390 does this to prevent multiplexing of hardware bits 124 * related to the physical page in case of virtualization. 125 */ 126 #ifndef mm_forbids_zeropage 127 #define mm_forbids_zeropage(X) (0) 128 #endif 129 130 /* 131 * On some architectures it is expensive to call memset() for small sizes. 132 * If an architecture decides to implement their own version of 133 * mm_zero_struct_page they should wrap the defines below in a #ifndef and 134 * define their own version of this macro in <asm/pgtable.h> 135 */ 136 #if BITS_PER_LONG == 64 137 /* This function must be updated when the size of struct page grows above 96 138 * or reduces below 56. The idea that compiler optimizes out switch() 139 * statement, and only leaves move/store instructions. Also the compiler can 140 * combine write statements if they are both assignments and can be reordered, 141 * this can result in several of the writes here being dropped. 142 */ 143 #define mm_zero_struct_page(pp) __mm_zero_struct_page(pp) 144 static inline void __mm_zero_struct_page(struct page *page) 145 { 146 unsigned long *_pp = (void *)page; 147 148 /* Check that struct page is either 56, 64, 72, 80, 88 or 96 bytes */ 149 BUILD_BUG_ON(sizeof(struct page) & 7); 150 BUILD_BUG_ON(sizeof(struct page) < 56); 151 BUILD_BUG_ON(sizeof(struct page) > 96); 152 153 switch (sizeof(struct page)) { 154 case 96: 155 _pp[11] = 0; 156 fallthrough; 157 case 88: 158 _pp[10] = 0; 159 fallthrough; 160 case 80: 161 _pp[9] = 0; 162 fallthrough; 163 case 72: 164 _pp[8] = 0; 165 fallthrough; 166 case 64: 167 _pp[7] = 0; 168 fallthrough; 169 case 56: 170 _pp[6] = 0; 171 _pp[5] = 0; 172 _pp[4] = 0; 173 _pp[3] = 0; 174 _pp[2] = 0; 175 _pp[1] = 0; 176 _pp[0] = 0; 177 } 178 } 179 #else 180 #define mm_zero_struct_page(pp) ((void)memset((pp), 0, sizeof(struct page))) 181 #endif 182 183 /* 184 * Default maximum number of active map areas, this limits the number of vmas 185 * per mm struct. Users can overwrite this number by sysctl but there is a 186 * problem. 187 * 188 * When a program's coredump is generated as ELF format, a section is created 189 * per a vma. In ELF, the number of sections is represented in unsigned short. 190 * This means the number of sections should be smaller than 65535 at coredump. 191 * Because the kernel adds some informative sections to a image of program at 192 * generating coredump, we need some margin. The number of extra sections is 193 * 1-3 now and depends on arch. We use "5" as safe margin, here. 194 * 195 * ELF extended numbering allows more than 65535 sections, so 16-bit bound is 196 * not a hard limit any more. Although some userspace tools can be surprised by 197 * that. 198 */ 199 #define MAPCOUNT_ELF_CORE_MARGIN (5) 200 #define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN) 201 202 extern unsigned long sysctl_user_reserve_kbytes; 203 extern unsigned long sysctl_admin_reserve_kbytes; 204 205 #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP) 206 bool page_range_contiguous(const struct page *page, unsigned long nr_pages); 207 #else 208 static inline bool page_range_contiguous(const struct page *page, 209 unsigned long nr_pages) 210 { 211 return true; 212 } 213 #endif 214 215 /* to align the pointer to the (next) page boundary */ 216 #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) 217 218 /* to align the pointer to the (prev) page boundary */ 219 #define PAGE_ALIGN_DOWN(addr) ALIGN_DOWN(addr, PAGE_SIZE) 220 221 /* test whether an address (unsigned long or pointer) is aligned to PAGE_SIZE */ 222 #define PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), PAGE_SIZE) 223 224 /** 225 * folio_page_idx - Return the number of a page in a folio. 226 * @folio: The folio. 227 * @page: The folio page. 228 * 229 * This function expects that the page is actually part of the folio. 230 * The returned number is relative to the start of the folio. 231 */ 232 static inline unsigned long folio_page_idx(const struct folio *folio, 233 const struct page *page) 234 { 235 return page - &folio->page; 236 } 237 238 static inline struct folio *lru_to_folio(struct list_head *head) 239 { 240 return list_entry((head)->prev, struct folio, lru); 241 } 242 243 void setup_initial_init_mm(void *start_code, void *end_code, 244 void *end_data, void *brk); 245 246 /* 247 * Linux kernel virtual memory manager primitives. 248 * The idea being to have a "virtual" mm in the same way 249 * we have a virtual fs - giving a cleaner interface to the 250 * mm details, and allowing different kinds of memory mappings 251 * (from shared memory to executable loading to arbitrary 252 * mmap() functions). 253 */ 254 255 struct vm_area_struct *vm_area_alloc(struct mm_struct *); 256 struct vm_area_struct *vm_area_dup(struct vm_area_struct *); 257 void vm_area_free(struct vm_area_struct *); 258 259 #ifndef CONFIG_MMU 260 extern struct rb_root nommu_region_tree; 261 extern struct rw_semaphore nommu_region_sem; 262 263 extern unsigned int kobjsize(const void *objp); 264 #endif 265 266 /* 267 * vm_flags in vm_area_struct, see mm_types.h. 268 * When changing, update also include/trace/events/mmflags.h 269 */ 270 271 #define VM_NONE 0x00000000 272 273 /** 274 * typedef vma_flag_t - specifies an individual VMA flag by bit number. 275 * 276 * This value is made type safe by sparse to avoid passing invalid flag values 277 * around. 278 */ 279 typedef int __bitwise vma_flag_t; 280 281 #define DECLARE_VMA_BIT(name, bitnum) \ 282 VMA_ ## name ## _BIT = ((__force vma_flag_t)bitnum) 283 #define DECLARE_VMA_BIT_ALIAS(name, aliased) \ 284 VMA_ ## name ## _BIT = (VMA_ ## aliased ## _BIT) 285 enum { 286 DECLARE_VMA_BIT(READ, 0), 287 DECLARE_VMA_BIT(WRITE, 1), 288 DECLARE_VMA_BIT(EXEC, 2), 289 DECLARE_VMA_BIT(SHARED, 3), 290 /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */ 291 DECLARE_VMA_BIT(MAYREAD, 4), /* limits for mprotect() etc. */ 292 DECLARE_VMA_BIT(MAYWRITE, 5), 293 DECLARE_VMA_BIT(MAYEXEC, 6), 294 DECLARE_VMA_BIT(MAYSHARE, 7), 295 DECLARE_VMA_BIT(GROWSDOWN, 8), /* general info on the segment */ 296 #ifdef CONFIG_MMU 297 DECLARE_VMA_BIT(UFFD_MISSING, 9),/* missing pages tracking */ 298 #else 299 /* nommu: R/O MAP_PRIVATE mapping that might overlay a file mapping */ 300 DECLARE_VMA_BIT(MAYOVERLAY, 9), 301 #endif /* CONFIG_MMU */ 302 /* Page-ranges managed without "struct page", just pure PFN */ 303 DECLARE_VMA_BIT(PFNMAP, 10), 304 DECLARE_VMA_BIT(MAYBE_GUARD, 11), 305 DECLARE_VMA_BIT(UFFD_WP, 12), /* wrprotect pages tracking */ 306 DECLARE_VMA_BIT(LOCKED, 13), 307 DECLARE_VMA_BIT(IO, 14), /* Memory mapped I/O or similar */ 308 DECLARE_VMA_BIT(SEQ_READ, 15), /* App will access data sequentially */ 309 DECLARE_VMA_BIT(RAND_READ, 16), /* App will not benefit from clustered reads */ 310 DECLARE_VMA_BIT(DONTCOPY, 17), /* Do not copy this vma on fork */ 311 DECLARE_VMA_BIT(DONTEXPAND, 18),/* Cannot expand with mremap() */ 312 DECLARE_VMA_BIT(LOCKONFAULT, 19),/* Lock pages covered when faulted in */ 313 DECLARE_VMA_BIT(ACCOUNT, 20), /* Is a VM accounted object */ 314 DECLARE_VMA_BIT(NORESERVE, 21), /* should the VM suppress accounting */ 315 DECLARE_VMA_BIT(HUGETLB, 22), /* Huge TLB Page VM */ 316 DECLARE_VMA_BIT(SYNC, 23), /* Synchronous page faults */ 317 DECLARE_VMA_BIT(ARCH_1, 24), /* Architecture-specific flag */ 318 DECLARE_VMA_BIT(WIPEONFORK, 25),/* Wipe VMA contents in child. */ 319 DECLARE_VMA_BIT(DONTDUMP, 26), /* Do not include in the core dump */ 320 DECLARE_VMA_BIT(SOFTDIRTY, 27), /* NOT soft dirty clean area */ 321 DECLARE_VMA_BIT(MIXEDMAP, 28), /* Can contain struct page and pure PFN pages */ 322 DECLARE_VMA_BIT(HUGEPAGE, 29), /* MADV_HUGEPAGE marked this vma */ 323 DECLARE_VMA_BIT(NOHUGEPAGE, 30),/* MADV_NOHUGEPAGE marked this vma */ 324 DECLARE_VMA_BIT(MERGEABLE, 31), /* KSM may merge identical pages */ 325 /* These bits are reused, we define specific uses below. */ 326 DECLARE_VMA_BIT(HIGH_ARCH_0, 32), 327 DECLARE_VMA_BIT(HIGH_ARCH_1, 33), 328 DECLARE_VMA_BIT(HIGH_ARCH_2, 34), 329 DECLARE_VMA_BIT(HIGH_ARCH_3, 35), 330 DECLARE_VMA_BIT(HIGH_ARCH_4, 36), 331 DECLARE_VMA_BIT(HIGH_ARCH_5, 37), 332 DECLARE_VMA_BIT(HIGH_ARCH_6, 38), 333 /* 334 * This flag is used to connect VFIO to arch specific KVM code. It 335 * indicates that the memory under this VMA is safe for use with any 336 * non-cachable memory type inside KVM. Some VFIO devices, on some 337 * platforms, are thought to be unsafe and can cause machine crashes 338 * if KVM does not lock down the memory type. 339 */ 340 DECLARE_VMA_BIT(ALLOW_ANY_UNCACHED, 39), 341 #if defined(CONFIG_PPC32) 342 DECLARE_VMA_BIT_ALIAS(DROPPABLE, ARCH_1), 343 #elif defined(CONFIG_64BIT) 344 DECLARE_VMA_BIT(DROPPABLE, 40), 345 #endif 346 DECLARE_VMA_BIT(UFFD_MINOR, 41), 347 DECLARE_VMA_BIT(SEALED, 42), 348 DECLARE_VMA_BIT(UFFD_RWP, 43), 349 /* Flags that reuse flags above. */ 350 DECLARE_VMA_BIT_ALIAS(PKEY_BIT0, HIGH_ARCH_0), 351 DECLARE_VMA_BIT_ALIAS(PKEY_BIT1, HIGH_ARCH_1), 352 DECLARE_VMA_BIT_ALIAS(PKEY_BIT2, HIGH_ARCH_2), 353 DECLARE_VMA_BIT_ALIAS(PKEY_BIT3, HIGH_ARCH_3), 354 DECLARE_VMA_BIT_ALIAS(PKEY_BIT4, HIGH_ARCH_4), 355 #if defined(CONFIG_X86_USER_SHADOW_STACK) || defined(CONFIG_RISCV_USER_CFI) 356 /* 357 * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of 358 * support core mm. 359 * 360 * These VMAs will get a single end guard page. This helps userspace 361 * protect itself from attacks. A single page is enough for current 362 * shadow stack archs (x86). See the comments near alloc_shstk() in 363 * arch/x86/kernel/shstk.c for more details on the guard size. 364 */ 365 DECLARE_VMA_BIT_ALIAS(SHADOW_STACK, HIGH_ARCH_5), 366 #elif defined(CONFIG_ARM64_GCS) 367 /* 368 * arm64's Guarded Control Stack implements similar functionality and 369 * has similar constraints to shadow stacks. 370 */ 371 DECLARE_VMA_BIT_ALIAS(SHADOW_STACK, HIGH_ARCH_6), 372 #endif 373 DECLARE_VMA_BIT_ALIAS(SAO, ARCH_1), /* Strong Access Ordering (powerpc) */ 374 DECLARE_VMA_BIT_ALIAS(GROWSUP, ARCH_1), /* parisc */ 375 DECLARE_VMA_BIT_ALIAS(SPARC_ADI, ARCH_1), /* sparc64 */ 376 DECLARE_VMA_BIT_ALIAS(ARM64_BTI, ARCH_1), /* arm64 */ 377 DECLARE_VMA_BIT_ALIAS(ARCH_CLEAR, ARCH_1), /* sparc64, arm64 */ 378 DECLARE_VMA_BIT_ALIAS(MAPPED_COPY, ARCH_1), /* !CONFIG_MMU */ 379 DECLARE_VMA_BIT_ALIAS(MTE, HIGH_ARCH_4), /* arm64 */ 380 DECLARE_VMA_BIT_ALIAS(MTE_ALLOWED, HIGH_ARCH_5),/* arm64 */ 381 #ifdef CONFIG_STACK_GROWSUP 382 DECLARE_VMA_BIT_ALIAS(STACK, GROWSUP), 383 DECLARE_VMA_BIT_ALIAS(STACK_EARLY, GROWSDOWN), 384 #else 385 DECLARE_VMA_BIT_ALIAS(STACK, GROWSDOWN), 386 #endif 387 }; 388 #undef DECLARE_VMA_BIT 389 #undef DECLARE_VMA_BIT_ALIAS 390 391 #define INIT_VM_FLAG(name) BIT((__force int) VMA_ ## name ## _BIT) 392 #define VM_READ INIT_VM_FLAG(READ) 393 #define VM_WRITE INIT_VM_FLAG(WRITE) 394 #define VM_EXEC INIT_VM_FLAG(EXEC) 395 #define VM_SHARED INIT_VM_FLAG(SHARED) 396 #define VM_MAYREAD INIT_VM_FLAG(MAYREAD) 397 #define VM_MAYWRITE INIT_VM_FLAG(MAYWRITE) 398 #define VM_MAYEXEC INIT_VM_FLAG(MAYEXEC) 399 #define VM_MAYSHARE INIT_VM_FLAG(MAYSHARE) 400 #define VM_GROWSDOWN INIT_VM_FLAG(GROWSDOWN) 401 #ifdef CONFIG_MMU 402 #define VM_UFFD_MISSING INIT_VM_FLAG(UFFD_MISSING) 403 #else 404 #define VM_UFFD_MISSING VM_NONE 405 #define VM_MAYOVERLAY INIT_VM_FLAG(MAYOVERLAY) 406 #endif 407 #define VM_PFNMAP INIT_VM_FLAG(PFNMAP) 408 #define VM_MAYBE_GUARD INIT_VM_FLAG(MAYBE_GUARD) 409 #define VM_UFFD_WP INIT_VM_FLAG(UFFD_WP) 410 #define VM_LOCKED INIT_VM_FLAG(LOCKED) 411 #define VM_IO INIT_VM_FLAG(IO) 412 #define VM_SEQ_READ INIT_VM_FLAG(SEQ_READ) 413 #define VM_RAND_READ INIT_VM_FLAG(RAND_READ) 414 #define VM_DONTCOPY INIT_VM_FLAG(DONTCOPY) 415 #define VM_DONTEXPAND INIT_VM_FLAG(DONTEXPAND) 416 #define VM_LOCKONFAULT INIT_VM_FLAG(LOCKONFAULT) 417 #define VM_ACCOUNT INIT_VM_FLAG(ACCOUNT) 418 #define VM_NORESERVE INIT_VM_FLAG(NORESERVE) 419 #define VM_HUGETLB INIT_VM_FLAG(HUGETLB) 420 #define VM_SYNC INIT_VM_FLAG(SYNC) 421 #define VM_ARCH_1 INIT_VM_FLAG(ARCH_1) 422 #define VM_WIPEONFORK INIT_VM_FLAG(WIPEONFORK) 423 #define VM_DONTDUMP INIT_VM_FLAG(DONTDUMP) 424 #ifdef CONFIG_MEM_SOFT_DIRTY 425 #define VM_SOFTDIRTY INIT_VM_FLAG(SOFTDIRTY) 426 #else 427 #define VM_SOFTDIRTY VM_NONE 428 #endif 429 #define VM_MIXEDMAP INIT_VM_FLAG(MIXEDMAP) 430 #define VM_HUGEPAGE INIT_VM_FLAG(HUGEPAGE) 431 #define VM_NOHUGEPAGE INIT_VM_FLAG(NOHUGEPAGE) 432 #define VM_MERGEABLE INIT_VM_FLAG(MERGEABLE) 433 #define VM_STACK INIT_VM_FLAG(STACK) 434 #ifdef CONFIG_STACK_GROWSUP 435 #define VM_STACK_EARLY INIT_VM_FLAG(STACK_EARLY) 436 #define VMA_STACK_EARLY mk_vma_flags(VMA_STACK_EARLY_BIT) 437 #else 438 #define VM_STACK_EARLY VM_NONE 439 #define VMA_STACK_EARLY EMPTY_VMA_FLAGS 440 #endif 441 #ifdef CONFIG_ARCH_HAS_PKEYS 442 #define VM_PKEY_SHIFT ((__force int)VMA_HIGH_ARCH_0_BIT) 443 /* Despite the naming, these are FLAGS not bits. */ 444 #define VM_PKEY_BIT0 INIT_VM_FLAG(PKEY_BIT0) 445 #define VM_PKEY_BIT1 INIT_VM_FLAG(PKEY_BIT1) 446 #define VM_PKEY_BIT2 INIT_VM_FLAG(PKEY_BIT2) 447 #if CONFIG_ARCH_PKEY_BITS > 3 448 #define VM_PKEY_BIT3 INIT_VM_FLAG(PKEY_BIT3) 449 #else 450 #define VM_PKEY_BIT3 VM_NONE 451 #endif /* CONFIG_ARCH_PKEY_BITS > 3 */ 452 #if CONFIG_ARCH_PKEY_BITS > 4 453 #define VM_PKEY_BIT4 INIT_VM_FLAG(PKEY_BIT4) 454 #else 455 #define VM_PKEY_BIT4 VM_NONE 456 #endif /* CONFIG_ARCH_PKEY_BITS > 4 */ 457 #endif /* CONFIG_ARCH_HAS_PKEYS */ 458 #if defined(CONFIG_X86_USER_SHADOW_STACK) || defined(CONFIG_ARM64_GCS) || \ 459 defined(CONFIG_RISCV_USER_CFI) 460 #define VM_SHADOW_STACK INIT_VM_FLAG(SHADOW_STACK) 461 #define VMA_SHADOW_STACK mk_vma_flags(VMA_SHADOW_STACK_BIT) 462 #define VMA_STARTGAP_FLAGS mk_vma_flags(VMA_GROWSDOWN_BIT, VMA_SHADOW_STACK_BIT) 463 #else 464 #define VM_SHADOW_STACK VM_NONE 465 #define VMA_SHADOW_STACK EMPTY_VMA_FLAGS 466 #define VMA_STARTGAP_FLAGS mk_vma_flags(VMA_GROWSDOWN_BIT) 467 #endif 468 #if defined(CONFIG_PPC64) 469 #define VM_SAO INIT_VM_FLAG(SAO) 470 #elif defined(CONFIG_PARISC) 471 #define VM_GROWSUP INIT_VM_FLAG(GROWSUP) 472 #define VMA_GROWSUP mk_vma_flags(VMA_GROWSUP_BIT) 473 #elif defined(CONFIG_SPARC64) 474 #define VM_SPARC_ADI INIT_VM_FLAG(SPARC_ADI) 475 #define VM_ARCH_CLEAR INIT_VM_FLAG(ARCH_CLEAR) 476 #elif defined(CONFIG_ARM64) 477 #define VM_ARM64_BTI INIT_VM_FLAG(ARM64_BTI) 478 #define VM_ARCH_CLEAR INIT_VM_FLAG(ARCH_CLEAR) 479 #elif !defined(CONFIG_MMU) 480 #define VM_MAPPED_COPY INIT_VM_FLAG(MAPPED_COPY) 481 #endif 482 #ifndef VM_GROWSUP 483 #define VM_GROWSUP VM_NONE 484 #define VMA_GROWSUP EMPTY_VMA_FLAGS 485 #endif 486 #ifdef CONFIG_ARM64_MTE 487 #define VM_MTE INIT_VM_FLAG(MTE) 488 #define VM_MTE_ALLOWED INIT_VM_FLAG(MTE_ALLOWED) 489 #else 490 #define VM_MTE VM_NONE 491 #define VM_MTE_ALLOWED VM_NONE 492 #endif 493 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 494 #define VM_UFFD_MINOR INIT_VM_FLAG(UFFD_MINOR) 495 #else 496 #define VM_UFFD_MINOR VM_NONE 497 #endif 498 #ifdef CONFIG_USERFAULTFD_RWP 499 #define VM_UFFD_RWP INIT_VM_FLAG(UFFD_RWP) 500 #else 501 #define VM_UFFD_RWP VM_NONE 502 #endif 503 504 /* 505 * vma_flags_t masks for the userfaultfd VMA flags. The two high-bit modes are 506 * gated on the same configs as their VM_* flags above -- both of which imply 507 * 64BIT -- so an out-of-range bit is never fed to mk_vma_flags() on a build 508 * whose bitmap cannot hold it. 509 */ 510 #define VMA_UFFD_MISSING mk_vma_flags(VMA_UFFD_MISSING_BIT) 511 #define VMA_UFFD_WP mk_vma_flags(VMA_UFFD_WP_BIT) 512 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 513 #define VMA_UFFD_MINOR mk_vma_flags(VMA_UFFD_MINOR_BIT) 514 #else 515 #define VMA_UFFD_MINOR EMPTY_VMA_FLAGS 516 #endif 517 #ifdef CONFIG_USERFAULTFD_RWP 518 #define VMA_UFFD_RWP mk_vma_flags(VMA_UFFD_RWP_BIT) 519 #else 520 #define VMA_UFFD_RWP EMPTY_VMA_FLAGS 521 #endif 522 523 #ifdef CONFIG_64BIT 524 #define VM_ALLOW_ANY_UNCACHED INIT_VM_FLAG(ALLOW_ANY_UNCACHED) 525 #define VM_SEALED INIT_VM_FLAG(SEALED) 526 #else 527 #define VM_ALLOW_ANY_UNCACHED VM_NONE 528 #define VM_SEALED VM_NONE 529 #endif 530 #if defined(CONFIG_64BIT) || defined(CONFIG_PPC32) 531 #define VM_DROPPABLE INIT_VM_FLAG(DROPPABLE) 532 #define VMA_DROPPABLE mk_vma_flags(VMA_DROPPABLE_BIT) 533 #else 534 #define VM_DROPPABLE VM_NONE 535 #define VMA_DROPPABLE EMPTY_VMA_FLAGS 536 #endif 537 538 /* Bits set in the VMA until the stack is in its final location */ 539 #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ | VM_STACK_EARLY) 540 #define VMA_STACK_INCOMPLETE_SETUP append_vma_flags( \ 541 VMA_STACK_EARLY, VMA_RAND_READ_BIT, VMA_SEQ_READ_BIT) 542 543 #define TASK_EXEC_BIT ((current->personality & READ_IMPLIES_EXEC) ? \ 544 VMA_EXEC_BIT : VMA_READ_BIT) 545 546 /* Common data flag combinations */ 547 #define VMA_DATA_FLAGS_TSK_EXEC mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, \ 548 TASK_EXEC_BIT, VMA_MAYREAD_BIT, VMA_MAYWRITE_BIT, \ 549 VMA_MAYEXEC_BIT) 550 #define VMA_DATA_FLAGS_NON_EXEC mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, \ 551 VMA_MAYREAD_BIT, VMA_MAYWRITE_BIT, VMA_MAYEXEC_BIT) 552 #define VMA_DATA_FLAGS_EXEC mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, \ 553 VMA_EXEC_BIT, VMA_MAYREAD_BIT, VMA_MAYWRITE_BIT, \ 554 VMA_MAYEXEC_BIT) 555 556 #ifndef VMA_DATA_DEFAULT_FLAGS /* arch can override this */ 557 #define VMA_DATA_DEFAULT_FLAGS VMA_DATA_FLAGS_EXEC 558 #endif 559 560 #ifndef VMA_STACK_DEFAULT_FLAGS /* arch can override this */ 561 #define VMA_STACK_DEFAULT_FLAGS VMA_DATA_DEFAULT_FLAGS 562 #endif 563 564 #define VMA_STACK_FLAGS append_vma_flags(VMA_STACK_DEFAULT_FLAGS, \ 565 VMA_STACK_BIT, VMA_ACCOUNT_BIT) 566 567 /* Temporary until VMA flags conversion complete. */ 568 #define VM_STACK_FLAGS vma_flags_to_legacy(VMA_STACK_FLAGS) 569 570 #ifdef CONFIG_MSEAL_SYSTEM_MAPPINGS 571 #define VM_SEALED_SYSMAP VM_SEALED 572 #else 573 #define VM_SEALED_SYSMAP VM_NONE 574 #endif 575 576 /* VMA basic access permission flags */ 577 #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC) 578 #define VMA_ACCESS_FLAGS mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT) 579 580 /* 581 * Physically remapped pages are special. Tell the 582 * rest of the world about it: 583 * IO tells people not to look at these pages 584 * (accesses can have side effects). 585 * PFNMAP tells the core MM that the base pages are just 586 * raw PFN mappings, and do not have a "struct page" associated 587 * with them. 588 * DONTEXPAND 589 * Disable vma merging and expanding with mremap(). 590 * DONTDUMP 591 * Omit vma from core dump, even when VM_IO turned off. 592 */ 593 #define VMA_REMAP_FLAGS mk_vma_flags(VMA_IO_BIT, VMA_PFNMAP_BIT, \ 594 VMA_DONTEXPAND_BIT, VMA_DONTDUMP_BIT) 595 596 /* This mask defines which mm->def_flags a process can inherit its parent */ 597 #define VM_INIT_DEF_MASK VM_NOHUGEPAGE 598 599 /* This mask represents all the VMA flag bits used by mlock */ 600 #define VM_LOCKED_MASK (VM_LOCKED | VM_LOCKONFAULT) 601 602 #define VMA_LOCKED_MASK mk_vma_flags(VMA_LOCKED_BIT, VMA_LOCKONFAULT_BIT) 603 604 /* These flags can be updated atomically via VMA/mmap read lock. */ 605 #define VMA_ATOMIC_SET_ALLOWED mk_vma_flags(VMA_MAYBE_GUARD_BIT) 606 607 /* Arch-specific flags to clear when updating VM flags on protection change */ 608 #ifndef VM_ARCH_CLEAR 609 #define VM_ARCH_CLEAR VM_NONE 610 #endif 611 #define VM_FLAGS_CLEAR (ARCH_VM_PKEY_FLAGS | VM_ARCH_CLEAR) 612 613 /* 614 * Flags which should be 'sticky' on merge - that is, flags which, when one VMA 615 * possesses it but the other does not, the merged VMA should nonetheless have 616 * applied to it: 617 * 618 * VMA_SOFTDIRTY_BIT - if a VMA is marked soft-dirty, that is has not had its 619 * references cleared via /proc/$pid/clear_refs, any 620 * merged VMA should be considered soft-dirty also as it 621 * operates at a VMA granularity. 622 * 623 * VMA_MAYBE_GUARD_BIT - If a VMA may have guard regions in place it implies 624 * that mapped page tables may contain metadata not 625 * described by the VMA and thus any merged VMA may also 626 * contain this metadata, and thus we must make this flag 627 * sticky. 628 */ 629 #ifdef CONFIG_MEM_SOFT_DIRTY 630 #define VMA_STICKY_FLAGS mk_vma_flags(VMA_SOFTDIRTY_BIT, VMA_MAYBE_GUARD_BIT) 631 #else 632 #define VMA_STICKY_FLAGS mk_vma_flags(VMA_MAYBE_GUARD_BIT) 633 #endif 634 635 /* 636 * VMA flags we ignore for the purposes of merge, i.e. one VMA possessing one 637 * of these flags and the other not does not preclude a merge. 638 * 639 * VMA_STICKY_FLAGS - When merging VMAs, VMA flags must match, unless they 640 * are 'sticky'. If any sticky flags exist in either VMA, 641 * we simply set all of them on the merged VMA. 642 */ 643 #define VMA_IGNORE_MERGE_FLAGS VMA_STICKY_FLAGS 644 645 /* 646 * Flags which should result in page tables being copied on fork. These are 647 * flags which indicate that the VMA maps page tables which cannot be 648 * reconsistuted upon page fault, so necessitate page table copying upon fork. 649 * 650 * Note that these flags should be compared with the DESTINATION VMA not the 651 * source: VM_UFFD_WP and VM_UFFD_RWP may be cleared on the destination 652 * (dup_userfaultfd() -> userfaultfd_reset_ctx() when the parent context did 653 * not negotiate UFFD_FEATURE_EVENT_FORK), while all other flags propagate. 654 * 655 * VM_PFNMAP / VM_MIXEDMAP - These contain kernel-mapped data which cannot be 656 * reasonably reconstructed on page fault. 657 * 658 * VM_UFFD_WP - Encodes metadata about an installed uffd 659 * VM_UFFD_RWP write- or read-write-protect handler, which 660 * cannot be reconstructed on page fault. 661 * 662 * We always copy pgtables when dst_vma has the 663 * uffd PTE bit in use even if it's file-backed 664 * (e.g. shmem). Because when the uffd bit is 665 * in use, the pgtable contains the protection 666 * information, that's something we can't 667 * retrieve from page cache, and skip copying 668 * will lose those info. 669 * 670 * VM_MAYBE_GUARD - Could contain page guard region markers which 671 * by design are a property of the page tables 672 * only and thus cannot be reconstructed on page 673 * fault. 674 */ 675 #define VM_COPY_ON_FORK (VM_PFNMAP | VM_MIXEDMAP | VM_UFFD_WP | VM_UFFD_RWP | \ 676 VM_MAYBE_GUARD) 677 678 /* 679 * mapping from the currently active vm_flags protection bits (the 680 * low four bits) to a page protection mask.. 681 */ 682 683 /* 684 * The default fault flags that should be used by most of the 685 * arch-specific page fault handlers. 686 */ 687 #define FAULT_FLAG_DEFAULT (FAULT_FLAG_ALLOW_RETRY | \ 688 FAULT_FLAG_KILLABLE | \ 689 FAULT_FLAG_INTERRUPTIBLE) 690 691 /** 692 * fault_flag_allow_retry_first - check ALLOW_RETRY the first time 693 * @flags: Fault flags. 694 * 695 * This is mostly used for places where we want to try to avoid taking 696 * the mmap_lock for too long a time when waiting for another condition 697 * to change, in which case we can try to be polite to release the 698 * mmap_lock in the first round to avoid potential starvation of other 699 * processes that would also want the mmap_lock. 700 * 701 * Return: true if the page fault allows retry and this is the first 702 * attempt of the fault handling; false otherwise. 703 */ 704 static inline bool fault_flag_allow_retry_first(enum fault_flag flags) 705 { 706 return (flags & FAULT_FLAG_ALLOW_RETRY) && 707 (!(flags & FAULT_FLAG_TRIED)); 708 } 709 710 #define FAULT_FLAG_TRACE \ 711 { FAULT_FLAG_WRITE, "WRITE" }, \ 712 { FAULT_FLAG_MKWRITE, "MKWRITE" }, \ 713 { FAULT_FLAG_ALLOW_RETRY, "ALLOW_RETRY" }, \ 714 { FAULT_FLAG_RETRY_NOWAIT, "RETRY_NOWAIT" }, \ 715 { FAULT_FLAG_KILLABLE, "KILLABLE" }, \ 716 { FAULT_FLAG_TRIED, "TRIED" }, \ 717 { FAULT_FLAG_USER, "USER" }, \ 718 { FAULT_FLAG_REMOTE, "REMOTE" }, \ 719 { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ 720 { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ 721 { FAULT_FLAG_VMA_LOCK, "VMA_LOCK" } 722 723 /* 724 * vm_fault is filled by the pagefault handler and passed to the vma's 725 * ->fault function. The vma's ->fault is responsible for returning a bitmask 726 * of VM_FAULT_xxx flags that give details about how the fault was handled. 727 * 728 * MM layer fills up gfp_mask for page allocations but fault handler might 729 * alter it if its implementation requires a different allocation context. 730 * 731 * pgoff should be used in favour of virtual_address, if possible. 732 */ 733 struct vm_fault { 734 const struct { 735 struct vm_area_struct *vma; /* Target VMA */ 736 gfp_t gfp_mask; /* gfp mask to be used for allocations */ 737 pgoff_t pgoff; /* Logical page offset based on vma */ 738 unsigned long address; /* Faulting virtual address - masked */ 739 unsigned long real_address; /* Faulting virtual address - unmasked */ 740 }; 741 enum fault_flag flags; /* FAULT_FLAG_xxx flags 742 * XXX: should really be 'const' */ 743 pmd_t *pmd; /* Pointer to pmd entry matching 744 * the 'address' */ 745 pud_t *pud; /* Pointer to pud entry matching 746 * the 'address' 747 */ 748 union { 749 pte_t orig_pte; /* Value of PTE at the time of fault */ 750 pmd_t orig_pmd; /* Value of PMD at the time of fault, 751 * used by PMD fault only. 752 */ 753 }; 754 755 struct page *cow_page; /* Page handler may use for COW fault */ 756 struct page *page; /* ->fault handlers should return a 757 * page here, unless VM_FAULT_NOPAGE 758 * is set (which is also implied by 759 * VM_FAULT_ERROR). 760 */ 761 /* These three entries are valid only while holding ptl lock */ 762 pte_t *pte; /* Pointer to pte entry matching 763 * the 'address'. NULL if the page 764 * table hasn't been allocated. 765 */ 766 spinlock_t *ptl; /* Page table lock. 767 * Protects pte page table if 'pte' 768 * is not NULL, otherwise pmd. 769 */ 770 pgtable_t prealloc_pte; /* Pre-allocated pte page table. 771 * vm_ops->map_pages() sets up a page 772 * table from atomic context. 773 * do_fault_around() pre-allocates 774 * page table to avoid allocation from 775 * atomic context. 776 */ 777 }; 778 779 struct vm_uffd_ops; 780 781 /* 782 * These are the virtual MM functions - opening of an area, closing and 783 * unmapping it (needed to keep files on disk up-to-date etc), pointer 784 * to the functions called when a no-page or a wp-page exception occurs. 785 */ 786 struct vm_operations_struct { 787 /** 788 * @open: Called when a VMA is remapped, split or forked. Not called 789 * upon first mapping a VMA. 790 * Context: User context. May sleep. Caller holds mmap_lock. 791 */ 792 void (*open)(struct vm_area_struct *vma); 793 /** 794 * @close: Called when the VMA is being removed from the MM. 795 * Context: User context. May sleep. Caller holds mmap_lock. 796 */ 797 void (*close)(struct vm_area_struct *vma); 798 /** 799 * @mapped: Called when the VMA is first mapped in the MM. Not called if 800 * the new VMA is merged with an adjacent VMA. 801 * 802 * The @vm_private_data field is an output field allowing the user to 803 * modify vma->vm_private_data as necessary. 804 * 805 * ONLY valid if set from f_op->mmap_prepare. Will result in an error if 806 * set from f_op->mmap. 807 * 808 * Returns %0 on success, or an error otherwise. On error, the VMA will 809 * be unmapped. 810 * 811 * Context: User context. May sleep. Caller holds mmap_lock. 812 */ 813 int (*mapped)(unsigned long start, unsigned long end, pgoff_t pgoff, 814 const struct file *file, void **vm_private_data); 815 /* Called any time before splitting to check if it's allowed */ 816 int (*may_split)(struct vm_area_struct *vma, unsigned long addr); 817 int (*mremap)(struct vm_area_struct *vma); 818 /* 819 * Called by mprotect() to make driver-specific permission 820 * checks before mprotect() is finalised. The VMA must not 821 * be modified. Returns 0 if mprotect() can proceed. 822 */ 823 int (*mprotect)(struct vm_area_struct *vma, unsigned long start, 824 unsigned long end, unsigned long newflags); 825 vm_fault_t (*fault)(struct vm_fault *vmf); 826 vm_fault_t (*huge_fault)(struct vm_fault *vmf, unsigned int order); 827 vm_fault_t (*map_pages)(struct vm_fault *vmf, 828 pgoff_t start_pgoff, pgoff_t end_pgoff); 829 unsigned long (*pagesize)(struct vm_area_struct *vma); 830 831 /* notification that a previously read-only page is about to become 832 * writable, if an error is returned it will cause a SIGBUS */ 833 vm_fault_t (*page_mkwrite)(struct vm_fault *vmf); 834 835 /* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */ 836 vm_fault_t (*pfn_mkwrite)(struct vm_fault *vmf); 837 838 /* called by access_process_vm when get_user_pages() fails, typically 839 * for use by special VMAs. See also generic_access_phys() for a generic 840 * implementation useful for any iomem mapping. 841 */ 842 int (*access)(struct vm_area_struct *vma, unsigned long addr, 843 void *buf, int len, int write); 844 845 /* Called by the /proc/PID/maps code to ask the vma whether it 846 * has a special name. Returning non-NULL will also cause this 847 * vma to be dumped unconditionally. */ 848 const char *(*name)(struct vm_area_struct *vma); 849 850 #ifdef CONFIG_NUMA 851 /* 852 * set_policy() op must add a reference to any non-NULL @new mempolicy 853 * to hold the policy upon return. Caller should pass NULL @new to 854 * remove a policy and fall back to surrounding context--i.e. do not 855 * install a MPOL_DEFAULT policy, nor the task or system default 856 * mempolicy. 857 */ 858 int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new); 859 860 /* 861 * get_policy() op must add reference [mpol_get()] to any policy at 862 * (vma,addr) marked as MPOL_SHARED. The shared policy infrastructure 863 * in mm/mempolicy.c will do this automatically. 864 * get_policy() must NOT add a ref if the policy at (vma,addr) is not 865 * marked as MPOL_SHARED. vma policies are protected by the mmap_lock. 866 * If no [shared/vma] mempolicy exists at the addr, get_policy() op 867 * must return NULL--i.e., do not "fallback" to task or system default 868 * policy. 869 */ 870 struct mempolicy *(*get_policy)(struct vm_area_struct *vma, 871 unsigned long addr, pgoff_t *ilx); 872 #endif 873 #ifdef CONFIG_FIND_NORMAL_PAGE 874 /* 875 * Called by vm_normal_page() for special PTEs in @vma at @addr. This 876 * allows for returning a "normal" page from vm_normal_page() even 877 * though the PTE indicates that the "struct page" either does not exist 878 * or should not be touched: "special". 879 * 880 * Do not add new users: this really only works when a "normal" page 881 * was mapped, but then the PTE got changed to something weird (+ 882 * marked special) that would not make pte_pfn() identify the originally 883 * inserted page. 884 */ 885 struct page *(*find_normal_page)(struct vm_area_struct *vma, 886 unsigned long addr); 887 #endif /* CONFIG_FIND_NORMAL_PAGE */ 888 #ifdef CONFIG_USERFAULTFD 889 const struct vm_uffd_ops *uffd_ops; 890 #endif 891 }; 892 893 #ifdef CONFIG_NUMA_BALANCING 894 static inline void vma_numab_state_init(struct vm_area_struct *vma) 895 { 896 vma->numab_state = NULL; 897 } 898 static inline void vma_numab_state_free(struct vm_area_struct *vma) 899 { 900 kfree(vma->numab_state); 901 } 902 #else 903 static inline void vma_numab_state_init(struct vm_area_struct *vma) {} 904 static inline void vma_numab_state_free(struct vm_area_struct *vma) {} 905 #endif /* CONFIG_NUMA_BALANCING */ 906 907 /* 908 * These must be here rather than mmap_lock.h as dependent on vm_fault type, 909 * declared in this header. 910 */ 911 static inline void release_fault_lock(struct vm_fault *vmf) 912 { 913 if (vmf->flags & FAULT_FLAG_VMA_LOCK) 914 vma_end_read(vmf->vma); 915 else 916 mmap_read_unlock(vmf->vma->vm_mm); 917 } 918 919 static inline void assert_fault_locked(const struct vm_fault *vmf) 920 { 921 if (vmf->flags & FAULT_FLAG_VMA_LOCK) 922 vma_assert_locked(vmf->vma); 923 else 924 mmap_assert_locked(vmf->vma->vm_mm); 925 } 926 927 static inline bool mm_flags_test(int flag, const struct mm_struct *mm) 928 { 929 return test_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); 930 } 931 932 static inline bool mm_flags_test_and_set(int flag, struct mm_struct *mm) 933 { 934 return test_and_set_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); 935 } 936 937 static inline bool mm_flags_test_and_clear(int flag, struct mm_struct *mm) 938 { 939 return test_and_clear_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); 940 } 941 942 static inline void mm_flags_set(int flag, struct mm_struct *mm) 943 { 944 set_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); 945 } 946 947 static inline void mm_flags_clear(int flag, struct mm_struct *mm) 948 { 949 clear_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); 950 } 951 952 static inline void mm_flags_clear_all(struct mm_struct *mm) 953 { 954 bitmap_zero(ACCESS_PRIVATE(&mm->flags, __mm_flags), NUM_MM_FLAG_BITS); 955 } 956 957 extern const struct vm_operations_struct vma_dummy_vm_ops; 958 959 static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) 960 { 961 memset(vma, 0, sizeof(*vma)); 962 vma->vm_mm = mm; 963 vma->vm_ops = &vma_dummy_vm_ops; 964 INIT_LIST_HEAD(&vma->anon_vma_chain); 965 vma_lock_init(vma, false); 966 } 967 968 /* Use when VMA is not part of the VMA tree and needs no locking */ 969 static inline void vm_flags_init(struct vm_area_struct *vma, 970 vm_flags_t flags) 971 { 972 VM_WARN_ON_ONCE(!pgtable_supports_soft_dirty() && (flags & VM_SOFTDIRTY)); 973 vma_flags_clear_all(&vma->flags); 974 vma_flags_overwrite_word(&vma->flags, flags); 975 } 976 977 /* 978 * Use when VMA is part of the VMA tree and modifications need coordination 979 * Note: vm_flags_reset and vm_flags_reset_once do not lock the vma and 980 * it should be locked explicitly beforehand. 981 */ 982 static inline void vm_flags_reset(struct vm_area_struct *vma, 983 vm_flags_t flags) 984 { 985 VM_WARN_ON_ONCE(!pgtable_supports_soft_dirty() && (flags & VM_SOFTDIRTY)); 986 vma_assert_write_locked(vma); 987 vm_flags_init(vma, flags); 988 } 989 990 static inline void vm_flags_set(struct vm_area_struct *vma, 991 vm_flags_t flags) 992 { 993 vma_start_write(vma); 994 vma_flags_set_word(&vma->flags, flags); 995 } 996 997 static inline void vm_flags_clear(struct vm_area_struct *vma, 998 vm_flags_t flags) 999 { 1000 VM_WARN_ON_ONCE(!pgtable_supports_soft_dirty() && (flags & VM_SOFTDIRTY)); 1001 vma_start_write(vma); 1002 vma_flags_clear_word(&vma->flags, flags); 1003 } 1004 1005 /* 1006 * Use only if VMA is not part of the VMA tree or has no other users and 1007 * therefore needs no locking. 1008 */ 1009 static inline void __vm_flags_mod(struct vm_area_struct *vma, 1010 vm_flags_t set, vm_flags_t clear) 1011 { 1012 vm_flags_init(vma, (vma->vm_flags | set) & ~clear); 1013 } 1014 1015 /* 1016 * Use only when the order of set/clear operations is unimportant, otherwise 1017 * use vm_flags_{set|clear} explicitly. 1018 */ 1019 static inline void vm_flags_mod(struct vm_area_struct *vma, 1020 vm_flags_t set, vm_flags_t clear) 1021 { 1022 vma_start_write(vma); 1023 __vm_flags_mod(vma, set, clear); 1024 } 1025 1026 /* Set an individual VMA flag in flags, non-atomically. */ 1027 static __always_inline void vma_flags_set_flag(vma_flags_t *flags, 1028 vma_flag_t bit) 1029 { 1030 unsigned long *bitmap = flags->__vma_flags; 1031 1032 __set_bit((__force int)bit, bitmap); 1033 } 1034 1035 static __always_inline vma_flags_t __mk_vma_flags(vma_flags_t flags, 1036 size_t count, const vma_flag_t *bits) 1037 { 1038 int i; 1039 1040 for (i = 0; i < count; i++) 1041 vma_flags_set_flag(&flags, bits[i]); 1042 return flags; 1043 } 1044 1045 /* 1046 * Helper macro which bitwise-or combines the specified input flags into a 1047 * vma_flags_t bitmap value. E.g.: 1048 * 1049 * vma_flags_t flags = mk_vma_flags(VMA_IO_BIT, VMA_PFNMAP_BIT, 1050 * VMA_DONTEXPAND_BIT, VMA_DONTDUMP_BIT); 1051 * 1052 * The compiler cleverly optimises away all of the work and this ends up being 1053 * equivalent to aggregating the values manually. 1054 */ 1055 #define mk_vma_flags(...) __mk_vma_flags(EMPTY_VMA_FLAGS, \ 1056 COUNT_ARGS(__VA_ARGS__), (const vma_flag_t []){__VA_ARGS__}) 1057 1058 /* 1059 * Helper macro which acts like mk_vma_flags, only appending to a copy of the 1060 * specified flags rather than establishing new flags. E.g.: 1061 * 1062 * vma_flags_t flags = append_vma_flags(VMA_STACK_DEFAULT_FLAGS, VMA_STACK_BIT, 1063 * VMA_ACCOUNT_BIT); 1064 */ 1065 #define append_vma_flags(flags, ...) __mk_vma_flags(flags, \ 1066 COUNT_ARGS(__VA_ARGS__), (const vma_flag_t []){__VA_ARGS__}) 1067 1068 /* Calculates the number of set bits in the specified VMA flags. */ 1069 static __always_inline int vma_flags_count(const vma_flags_t *flags) 1070 { 1071 const unsigned long *bitmap = flags->__vma_flags; 1072 1073 return bitmap_weight(bitmap, NUM_VMA_FLAG_BITS); 1074 } 1075 1076 /* 1077 * Test whether a specific VMA flag is set, e.g.: 1078 * 1079 * if (vma_flags_test(flags, VMA_READ_BIT)) { ... } 1080 */ 1081 static __always_inline bool vma_flags_test(const vma_flags_t *flags, 1082 vma_flag_t bit) 1083 { 1084 const unsigned long *bitmap = flags->__vma_flags; 1085 1086 return test_bit((__force int)bit, bitmap); 1087 } 1088 1089 /* 1090 * Obtain a set of VMA flags which contain the overlapping flags contained 1091 * within flags and to_and. 1092 */ 1093 static __always_inline vma_flags_t vma_flags_and_mask(const vma_flags_t *flags, 1094 vma_flags_t to_and) 1095 { 1096 vma_flags_t dst; 1097 unsigned long *bitmap_dst = dst.__vma_flags; 1098 const unsigned long *bitmap = flags->__vma_flags; 1099 const unsigned long *bitmap_to_and = to_and.__vma_flags; 1100 1101 bitmap_and(bitmap_dst, bitmap, bitmap_to_and, NUM_VMA_FLAG_BITS); 1102 return dst; 1103 } 1104 1105 /* 1106 * Obtain a set of VMA flags which contains the specified overlapping flags, 1107 * e.g.: 1108 * 1109 * vma_flags_t read_flags = vma_flags_and(&flags, VMA_READ_BIT, 1110 * VMA_MAY_READ_BIT); 1111 */ 1112 #define vma_flags_and(flags, ...) \ 1113 vma_flags_and_mask(flags, mk_vma_flags(__VA_ARGS__)) 1114 1115 /* Test each of to_test flags in flags, non-atomically. */ 1116 static __always_inline bool vma_flags_test_any_mask(const vma_flags_t *flags, 1117 vma_flags_t to_test) 1118 { 1119 const unsigned long *bitmap = flags->__vma_flags; 1120 const unsigned long *bitmap_to_test = to_test.__vma_flags; 1121 1122 return bitmap_intersects(bitmap_to_test, bitmap, NUM_VMA_FLAG_BITS); 1123 } 1124 1125 /* 1126 * Test whether any specified VMA flag is set, e.g.: 1127 * 1128 * if (vma_flags_test_any(flags, VMA_READ_BIT, VMA_MAYREAD_BIT)) { ... } 1129 */ 1130 #define vma_flags_test_any(flags, ...) \ 1131 vma_flags_test_any_mask(flags, mk_vma_flags(__VA_ARGS__)) 1132 1133 /* Test that ALL of the to_test flags are set, non-atomically. */ 1134 static __always_inline bool vma_flags_test_all_mask(const vma_flags_t *flags, 1135 vma_flags_t to_test) 1136 { 1137 const unsigned long *bitmap = flags->__vma_flags; 1138 const unsigned long *bitmap_to_test = to_test.__vma_flags; 1139 1140 return bitmap_subset(bitmap_to_test, bitmap, NUM_VMA_FLAG_BITS); 1141 } 1142 1143 /* 1144 * Test whether ALL specified VMA flags are set, e.g.: 1145 * 1146 * if (vma_flags_test_all(flags, VMA_READ_BIT, VMA_MAYREAD_BIT)) { ... } 1147 */ 1148 #define vma_flags_test_all(flags, ...) \ 1149 vma_flags_test_all_mask(flags, mk_vma_flags(__VA_ARGS__)) 1150 1151 /* 1152 * Helper to test that a flag mask of type vma_flags_t has a SINGLE flag set 1153 * (returning false if flagmask has no flags set). 1154 * 1155 * This is defined to make the semantics clearer when testing an optionally 1156 * defined VMA flags mask, e.g.: 1157 * 1158 * if (vma_flags_test_single_mask(&flags, VMA_DROPPABLE)) { ... } 1159 * 1160 * When VMA_DROPPABLE is defined if available, or set to EMPTY_VMA_FLAGS 1161 * otherwise. 1162 */ 1163 static __always_inline bool vma_flags_test_single_mask(const vma_flags_t *flags, 1164 vma_flags_t flagmask) 1165 { 1166 VM_WARN_ON_ONCE(vma_flags_count(&flagmask) > 1); 1167 1168 return vma_flags_test_any_mask(flags, flagmask); 1169 } 1170 1171 /* Set each of the to_set flags in flags, non-atomically. */ 1172 static __always_inline void vma_flags_set_mask(vma_flags_t *flags, 1173 vma_flags_t to_set) 1174 { 1175 unsigned long *bitmap = flags->__vma_flags; 1176 const unsigned long *bitmap_to_set = to_set.__vma_flags; 1177 1178 bitmap_or(bitmap, bitmap, bitmap_to_set, NUM_VMA_FLAG_BITS); 1179 } 1180 1181 /* 1182 * Set all specified VMA flags, e.g.: 1183 * 1184 * vma_flags_set(&flags, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 1185 */ 1186 #define vma_flags_set(flags, ...) \ 1187 vma_flags_set_mask(flags, mk_vma_flags(__VA_ARGS__)) 1188 1189 static __always_inline vma_flags_t __mk_vma_flags_from_masks(size_t count, 1190 const vma_flags_t *masks) 1191 { 1192 vma_flags_t flags = EMPTY_VMA_FLAGS; 1193 size_t i; 1194 1195 for (i = 0; i < count; i++) 1196 vma_flags_set_mask(&flags, masks[i]); 1197 return flags; 1198 } 1199 1200 /* 1201 * Combine pre-computed vma_flags_t masks into one value, e.g.: 1202 * 1203 * vma_flags_t flags = mk_vma_flags_from_masks(VMA_UFFD_WP, VMA_UFFD_MINOR); 1204 * 1205 * Unlike mk_vma_flags(), which takes bit numbers, this takes whole masks -- 1206 * each of which may be EMPTY_VMA_FLAGS when its feature is unavailable -- so a 1207 * bit that does not exist on the current build is never materialised. 1208 */ 1209 #define mk_vma_flags_from_masks(...) \ 1210 __mk_vma_flags_from_masks(COUNT_ARGS(__VA_ARGS__), \ 1211 (const vma_flags_t []){__VA_ARGS__}) 1212 1213 /* Clear all of the to-clear flags in flags, non-atomically. */ 1214 static __always_inline void vma_flags_clear_mask(vma_flags_t *flags, 1215 vma_flags_t to_clear) 1216 { 1217 unsigned long *bitmap = flags->__vma_flags; 1218 const unsigned long *bitmap_to_clear = to_clear.__vma_flags; 1219 1220 bitmap_andnot(bitmap, bitmap, bitmap_to_clear, NUM_VMA_FLAG_BITS); 1221 } 1222 1223 /* 1224 * Clear all specified individual flags, e.g.: 1225 * 1226 * vma_flags_clear(&flags, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 1227 */ 1228 #define vma_flags_clear(flags, ...) \ 1229 vma_flags_clear_mask(flags, mk_vma_flags(__VA_ARGS__)) 1230 1231 /* 1232 * Obtain a VMA flags value containing those flags that are present in flags or 1233 * flags_other but not in both. 1234 */ 1235 static __always_inline vma_flags_t vma_flags_diff_pair(const vma_flags_t *flags, 1236 const vma_flags_t *flags_other) 1237 { 1238 vma_flags_t dst; 1239 const unsigned long *bitmap_other = flags_other->__vma_flags; 1240 const unsigned long *bitmap = flags->__vma_flags; 1241 unsigned long *bitmap_dst = dst.__vma_flags; 1242 1243 bitmap_xor(bitmap_dst, bitmap, bitmap_other, NUM_VMA_FLAG_BITS); 1244 return dst; 1245 } 1246 1247 /* 1248 * Determine if flags and flags_other have precisely the same flags set, 1249 * excluding atomic VMA flags. 1250 */ 1251 static __always_inline bool vma_flags_same_pair(const vma_flags_t *flags, 1252 const vma_flags_t *flags_other) 1253 { 1254 vma_flags_t diff = vma_flags_diff_pair(flags, flags_other); 1255 1256 /* 1257 * Atomic VMA flags may be set concurrently under the VMA read lock, so 1258 * are disregarded for equality testing purposes. 1259 */ 1260 vma_flags_clear_mask(&diff, VMA_ATOMIC_SET_ALLOWED); 1261 1262 return vma_flags_empty(&diff); 1263 } 1264 1265 /* 1266 * Test whether a specific flag in the VMA is set, e.g.: 1267 * 1268 * if (vma_test(vma, VMA_READ_BIT)) { ... } 1269 */ 1270 static __always_inline bool vma_test(const struct vm_area_struct *vma, 1271 vma_flag_t bit) 1272 { 1273 return vma_flags_test(&vma->flags, bit); 1274 } 1275 1276 /* Helper to test any VMA flags in a VMA . */ 1277 static __always_inline bool vma_test_any_mask(const struct vm_area_struct *vma, 1278 vma_flags_t flags) 1279 { 1280 return vma_flags_test_any_mask(&vma->flags, flags); 1281 } 1282 1283 /* 1284 * Helper macro for testing whether any VMA flags are set in a VMA, 1285 * e.g.: 1286 * 1287 * if (vma_test_any(vma, VMA_IO_BIT, VMA_PFNMAP_BIT, 1288 * VMA_DONTEXPAND_BIT, VMA_DONTDUMP_BIT)) { ... } 1289 */ 1290 #define vma_test_any(vma, ...) \ 1291 vma_test_any_mask(vma, mk_vma_flags(__VA_ARGS__)) 1292 1293 /* 1294 * Helper to test that ALL specified flags are set in a VMA. 1295 * 1296 * Note: appropriate locks must be held, this function does not acquire them for 1297 * you. 1298 */ 1299 static __always_inline bool vma_test_all_mask(const struct vm_area_struct *vma, 1300 vma_flags_t flags) 1301 { 1302 return vma_flags_test_all_mask(&vma->flags, flags); 1303 } 1304 1305 /* 1306 * Helper macro for checking that ALL specified flags are set in a VMA, e.g.: 1307 * 1308 * if (vma_test_all(vma, VMA_READ_BIT, VMA_MAYREAD_BIT) { ... } 1309 */ 1310 #define vma_test_all(vma, ...) \ 1311 vma_test_all_mask(vma, mk_vma_flags(__VA_ARGS__)) 1312 1313 /* 1314 * Helper to test that a flag mask of type vma_flags_t has a SINGLE flag set 1315 * (returning false if flagmask has no flags set). 1316 * 1317 * This is useful when a flag needs to be either defined or not depending upon 1318 * kernel configuration, e.g.: 1319 * 1320 * if (vma_test_single_mask(vma, VMA_DROPPABLE)) { ... } 1321 * 1322 * When VMA_DROPPABLE is defined if available, or set to EMPTY_VMA_FLAGS 1323 * otherwise. 1324 */ 1325 static __always_inline bool 1326 vma_test_single_mask(const struct vm_area_struct *vma, vma_flags_t flagmask) 1327 { 1328 return vma_flags_test_single_mask(&vma->flags, flagmask); 1329 } 1330 1331 /* 1332 * Helper to set all VMA flags in a VMA. 1333 * 1334 * Note: appropriate locks must be held, this function does not acquire them for 1335 * you. 1336 */ 1337 static __always_inline void vma_set_flags_mask(struct vm_area_struct *vma, 1338 vma_flags_t flags) 1339 { 1340 vma_flags_set_mask(&vma->flags, flags); 1341 } 1342 1343 /* 1344 * Helper macro for specifying VMA flags in a VMA, e.g.: 1345 * 1346 * vma_set_flags(vma, VMA_IO_BIT, VMA_PFNMAP_BIT, VMA_DONTEXPAND_BIT, 1347 * VMA_DONTDUMP_BIT); 1348 * 1349 * Note: appropriate locks must be held, this function does not acquire them for 1350 * you. 1351 */ 1352 #define vma_set_flags(vma, ...) \ 1353 vma_set_flags_mask(vma, mk_vma_flags(__VA_ARGS__)) 1354 1355 /* Helper to clear all VMA flags in a VMA. */ 1356 static __always_inline void vma_clear_flags_mask(struct vm_area_struct *vma, 1357 vma_flags_t flags) 1358 { 1359 vma_flags_clear_mask(&vma->flags, flags); 1360 } 1361 1362 /* 1363 * Helper macro for clearing VMA flags, e.g.: 1364 * 1365 * vma_clear_flags(vma, VMA_IO_BIT, VMA_PFNMAP_BIT, VMA_DONTEXPAND_BIT, 1366 * VMA_DONTDUMP_BIT); 1367 */ 1368 #define vma_clear_flags(vma, ...) \ 1369 vma_clear_flags_mask(vma, mk_vma_flags(__VA_ARGS__)) 1370 1371 static __always_inline bool __vma_atomic_valid_flag(struct vm_area_struct *vma, 1372 vma_flag_t bit) 1373 { 1374 const vma_flags_t mask = VMA_ATOMIC_SET_ALLOWED; 1375 1376 /* Is this flag allowed to be set atomically? */ 1377 if (WARN_ON_ONCE(!vma_flags_test(&mask, bit))) 1378 return false; 1379 1380 return true; 1381 } 1382 1383 /* 1384 * Set VMA flag atomically. Requires only VMA/mmap read lock. Only specific 1385 * valid flags are allowed to do this. 1386 */ 1387 static __always_inline void vma_set_atomic_flag(struct vm_area_struct *vma, 1388 vma_flag_t bit) 1389 { 1390 unsigned long *bitmap = vma->flags.__vma_flags; 1391 1392 vma_assert_stabilised(vma); 1393 if (__vma_atomic_valid_flag(vma, bit)) 1394 set_bit((__force int)bit, bitmap); 1395 } 1396 1397 /* 1398 * Test for VMA flag atomically. Requires no locks. Only specific valid flags 1399 * are allowed to do this. 1400 * 1401 * This is necessarily racey, so callers must ensure that serialisation is 1402 * achieved through some other means, or that races are permissible. 1403 */ 1404 static __always_inline bool vma_test_atomic_flag(struct vm_area_struct *vma, 1405 vma_flag_t bit) 1406 { 1407 if (__vma_atomic_valid_flag(vma, bit)) 1408 return test_bit((__force int)bit, &vma->vm_flags); 1409 1410 return false; 1411 } 1412 1413 /* 1414 * Overwrite the VMA's flags with new ones, preserving atomic VMA flags in the 1415 * process. The write is done once and once only. 1416 */ 1417 static inline void vma_overwrite_flags(struct vm_area_struct *vma, 1418 vma_flags_t new_flags) 1419 { 1420 vma_flags_t atomic; 1421 1422 vma_assert_write_locked(vma); 1423 1424 /* 1425 * Preserve the VMA's atomic flags, which are kept stable by the VMA 1426 * write lock. 1427 */ 1428 atomic = vma_flags_and_mask(&vma->flags, VMA_ATOMIC_SET_ALLOWED); 1429 vma_flags_clear_mask(&new_flags, VMA_ATOMIC_SET_ALLOWED); 1430 vma_flags_set_mask(&new_flags, atomic); 1431 1432 /* It is assumed only the first system word must be written once. */ 1433 vma_flags_overwrite_word_once(&vma->flags, new_flags.__vma_flags[0]); 1434 /* The remainder can be copied normally. */ 1435 if (NUM_VMA_FLAG_BITS > BITS_PER_LONG) { 1436 unsigned long *dst = &vma->flags.__vma_flags[1]; 1437 const unsigned long *src = &new_flags.__vma_flags[1]; 1438 1439 bitmap_copy(dst, src, NUM_VMA_FLAG_BITS - BITS_PER_LONG); 1440 } 1441 } 1442 1443 /* 1444 * Test whether a specific VMA flag is set in a VMA descriptor, e.g.: 1445 * 1446 * if (vma_desc_test(desc, VMA_READ_BIT)) { ... } 1447 */ 1448 static __always_inline bool vma_desc_test(const struct vm_area_desc *desc, 1449 vma_flag_t bit) 1450 { 1451 return vma_flags_test(&desc->vma_flags, bit); 1452 } 1453 1454 /* Helper to test any VMA flags in a VMA descriptor. */ 1455 static __always_inline bool vma_desc_test_any_mask(const struct vm_area_desc *desc, 1456 vma_flags_t flags) 1457 { 1458 return vma_flags_test_any_mask(&desc->vma_flags, flags); 1459 } 1460 1461 /* 1462 * Helper macro for testing whether any VMA flags are set in a VMA descriptor, 1463 * e.g.: 1464 * 1465 * if (vma_desc_test_any(desc, VMA_IO_BIT, VMA_PFNMAP_BIT, 1466 * VMA_DONTEXPAND_BIT, VMA_DONTDUMP_BIT)) { ... } 1467 */ 1468 #define vma_desc_test_any(desc, ...) \ 1469 vma_desc_test_any_mask(desc, mk_vma_flags(__VA_ARGS__)) 1470 1471 /* Helper to test all VMA flags in a VMA descriptor. */ 1472 static __always_inline bool vma_desc_test_all_mask(const struct vm_area_desc *desc, 1473 vma_flags_t flags) 1474 { 1475 return vma_flags_test_all_mask(&desc->vma_flags, flags); 1476 } 1477 1478 /* 1479 * Helper macro for testing whether ALL VMA flags are set in a VMA descriptor, 1480 * e.g.: 1481 * 1482 * if (vma_desc_test_all(desc, VMA_READ_BIT, VMA_MAYREAD_BIT)) { ... } 1483 */ 1484 #define vma_desc_test_all(desc, ...) \ 1485 vma_desc_test_all_mask(desc, mk_vma_flags(__VA_ARGS__)) 1486 1487 /* Helper to set all VMA flags in a VMA descriptor. */ 1488 static __always_inline void vma_desc_set_flags_mask(struct vm_area_desc *desc, 1489 vma_flags_t flags) 1490 { 1491 vma_flags_set_mask(&desc->vma_flags, flags); 1492 } 1493 1494 /* 1495 * Helper macro for specifying VMA flags for an input pointer to a struct 1496 * vm_area_desc object describing a proposed VMA, e.g.: 1497 * 1498 * vma_desc_set_flags(desc, VMA_IO_BIT, VMA_PFNMAP_BIT, VMA_DONTEXPAND_BIT, 1499 * VMA_DONTDUMP_BIT); 1500 */ 1501 #define vma_desc_set_flags(desc, ...) \ 1502 vma_desc_set_flags_mask(desc, mk_vma_flags(__VA_ARGS__)) 1503 1504 /* Helper to clear all VMA flags in a VMA descriptor. */ 1505 static __always_inline void vma_desc_clear_flags_mask(struct vm_area_desc *desc, 1506 vma_flags_t flags) 1507 { 1508 vma_flags_clear_mask(&desc->vma_flags, flags); 1509 } 1510 1511 /* 1512 * Helper macro for clearing VMA flags for an input pointer to a struct 1513 * vm_area_desc object describing a proposed VMA, e.g.: 1514 * 1515 * vma_desc_clear_flags(desc, VMA_IO_BIT, VMA_PFNMAP_BIT, VMA_DONTEXPAND_BIT, 1516 * VMA_DONTDUMP_BIT); 1517 */ 1518 #define vma_desc_clear_flags(desc, ...) \ 1519 vma_desc_clear_flags_mask(desc, mk_vma_flags(__VA_ARGS__)) 1520 1521 static inline void vma_set_anonymous(struct vm_area_struct *vma) 1522 { 1523 vma->vm_ops = NULL; 1524 } 1525 1526 static inline bool vma_is_anonymous(const struct vm_area_struct *vma) 1527 { 1528 return !vma->vm_ops; 1529 } 1530 1531 /* 1532 * Indicate if the VMA is a heap for the given task; for 1533 * /proc/PID/maps that is the heap of the main task. 1534 */ 1535 static inline bool vma_is_initial_heap(const struct vm_area_struct *vma) 1536 { 1537 return vma->vm_start < vma->vm_mm->brk && 1538 vma->vm_end > vma->vm_mm->start_brk; 1539 } 1540 1541 /* 1542 * Indicate if the VMA is a stack for the given task; for 1543 * /proc/PID/maps that is the stack of the main task. 1544 */ 1545 static inline bool vma_is_initial_stack(const struct vm_area_struct *vma) 1546 { 1547 /* 1548 * We make no effort to guess what a given thread considers to be 1549 * its "stack". It's not even well-defined for programs written 1550 * languages like Go. 1551 */ 1552 return vma->vm_start <= vma->vm_mm->start_stack && 1553 vma->vm_end >= vma->vm_mm->start_stack; 1554 } 1555 1556 static inline bool vma_flags_can_grow(const vma_flags_t *flags) 1557 { 1558 if (vma_flags_test_single_mask(flags, VMA_GROWSUP)) 1559 return true; 1560 if (vma_flags_test(flags, VMA_GROWSDOWN_BIT)) 1561 return true; 1562 1563 return false; 1564 } 1565 1566 static inline bool vma_can_grow(const struct vm_area_struct *vma) 1567 { 1568 return vma_flags_can_grow(&vma->flags); 1569 } 1570 1571 static inline bool vma_is_temporary_stack(const struct vm_area_struct *vma) 1572 { 1573 if (!vma_can_grow(vma)) 1574 return false; 1575 1576 if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) == 1577 VM_STACK_INCOMPLETE_SETUP) 1578 return true; 1579 1580 return false; 1581 } 1582 1583 static inline bool vma_is_foreign(const struct vm_area_struct *vma) 1584 { 1585 if (!current->mm) 1586 return true; 1587 1588 if (current->mm != vma->vm_mm) 1589 return true; 1590 1591 return false; 1592 } 1593 1594 static inline bool vma_is_accessible(const struct vm_area_struct *vma) 1595 { 1596 return vma->vm_flags & VM_ACCESS_FLAGS; 1597 } 1598 1599 static inline bool is_shared_maywrite(const vma_flags_t *flags) 1600 { 1601 return vma_flags_test_all(flags, VMA_SHARED_BIT, VMA_MAYWRITE_BIT); 1602 } 1603 1604 static inline bool vma_is_shared_maywrite(const struct vm_area_struct *vma) 1605 { 1606 return is_shared_maywrite(&vma->flags); 1607 } 1608 1609 /** 1610 * vma_flags_is_hugetlb() - Do the specified VMA flags indicate that the 1611 * VMA is a hugetlb mapping? 1612 * @flags: The VMA flags to test. 1613 * 1614 * Returns: true if the flags indicate a hugetlb mapping, false otherwise. 1615 */ 1616 static inline bool vma_flags_is_hugetlb(const vma_flags_t *flags) 1617 { 1618 return IS_ENABLED(CONFIG_HUGETLB_PAGE) && 1619 vma_flags_test(flags, VMA_HUGETLB_BIT); 1620 } 1621 1622 /** 1623 * vma_is_hugetlb() - Is @vma a hugetlb mapping? 1624 * @vma: The VMA to test. 1625 * 1626 * Returns: true if @vma is a hugetlb mapping, false otherwise. 1627 */ 1628 static inline bool vma_is_hugetlb(const struct vm_area_struct *vma) 1629 { 1630 return vma_flags_is_hugetlb(&vma->flags); 1631 } 1632 1633 /** 1634 * vma_flags_is_mm_managed() - Do the specified VMA flags indicate that the 1635 * contents of the VMA are managed by core mm? 1636 * @flags: The VMA flags to test. 1637 * 1638 * Such a mapping is one established by a simple mmap() or brk() with no driver 1639 * or other kernel component involved. 1640 * 1641 * They are rmappable, and core mm services rmap operations including reclaim, 1642 * as well as population, CoW and merge/split (if eligible). 1643 * 1644 * Mappings which fail this test are those where this is not the case - 1645 * e.g. MMIO, user-mapped kernel pages, udmabuf (pfnmap shmem), etc. 1646 * 1647 * Returns: true if the flags indicate an mm-managed mapping. 1648 */ 1649 static inline bool vma_flags_is_mm_managed(const vma_flags_t *flags) 1650 { 1651 return !vma_flags_test_any(flags, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT); 1652 } 1653 1654 /** 1655 * vma_is_mm_managed() - Are the contents of @vma managed by core mm? 1656 * @vma: The VMA to test. 1657 * 1658 * See vma_flags_is_mm_managed() for a description of this property. 1659 * 1660 * Returns: true if the VMA is mm-managed. 1661 */ 1662 static inline bool vma_is_mm_managed(const struct vm_area_struct *vma) 1663 { 1664 return vma_flags_is_mm_managed(&vma->flags); 1665 } 1666 1667 /** 1668 * vma_flags_is_fixed_mapping() - Do the specified VMA flags indicate that this 1669 * is a fixed mapping that cannot be expanded or merged? 1670 * @flags: The VMA flags to test. 1671 * 1672 * Fixed mappings are those whose size is set at the point of mmap (for 1673 * instance, a mapping of a fixed range of memory established by a driver), 1674 * and thus cannot be expanded or merged. 1675 * 1676 * Returns: true if the flags indicate a fixed mapping. 1677 */ 1678 static inline bool vma_flags_is_fixed_mapping(const vma_flags_t *flags) 1679 { 1680 /* 1681 * VMA_PFNMAP_BIT should imply VMA_DONTEXPAND_BIT, but some callers set 1682 * only the former. 1683 */ 1684 return vma_flags_test_any(flags, VMA_PFNMAP_BIT, VMA_DONTEXPAND_BIT); 1685 } 1686 1687 /** 1688 * vma_is_fixed_mapping() - Is this VMA a fixed mapping that cannot be 1689 * expanded or merged? 1690 * @vma: The VMA to test. 1691 * 1692 * See vma_flags_is_fixed_mapping() for a description of this property. 1693 * 1694 * Returns: true if the VMA maps a fixed mapping. 1695 */ 1696 static inline bool vma_is_fixed_mapping(const struct vm_area_struct *vma) 1697 { 1698 return vma_flags_is_fixed_mapping(&vma->flags); 1699 } 1700 1701 /** 1702 * vma_flags_is_mm_backed() - Do the specified VMA flags imply the mapping is 1703 * backed by core mm? 1704 * @flags: The VMA flags to test. 1705 * 1706 * An mm-backed mapping is one managed by core mm (see 1707 * vma_flags_is_mm_managed()) whose contents are established by core mm's own 1708 * fault path rather than a custom ->fault handler. 1709 * 1710 * Mappings not managed by core mm are populated by their owner, and fixed 1711 * mappings may be mm-managed but have their contents established by a custom 1712 * ->fault handler, so neither are mm-backed. hugetlb mappings are fixed, but 1713 * core mm owns them entirely, so they are. 1714 * 1715 * These are the only mappings which can sensibly be merged, locked, dumped or 1716 * be subject to uffd faulting, as all of these assume core mm and core mm 1717 * alone manages their contents. 1718 * 1719 * Returns: true if the flags imply an mm-backed mapping, otherwise false. 1720 */ 1721 static inline bool vma_flags_is_mm_backed(const vma_flags_t *flags) 1722 { 1723 /* hugetlb is a fixed mapping, but core mm owns it entirely. */ 1724 if (vma_flags_is_hugetlb(flags)) 1725 return true; 1726 /* 1727 * Mappings not managed by core mm are populated by their owner. 1728 * 1729 * Fixed mappings may be mm-managed, but their contents are established 1730 * by a custom ->fault handler, so core mm cannot assume ordinary fault 1731 * semantics over them. 1732 */ 1733 return vma_flags_is_mm_managed(flags) && !vma_flags_is_fixed_mapping(flags); 1734 } 1735 1736 /** 1737 * vma_is_mm_backed() - Is @vma backed by core mm? 1738 * @vma: The VMA to test. 1739 * 1740 * See vma_flags_is_mm_backed() for details. 1741 * 1742 * Returns: true if the VMA is mm-backed, otherwise false. 1743 */ 1744 static inline bool vma_is_mm_backed(const struct vm_area_struct *vma) 1745 { 1746 return vma_flags_is_mm_backed(&vma->flags); 1747 } 1748 1749 /** 1750 * vma_flags_can_merge() - Do the specified VMA flags permit the VMA to be 1751 * merged with another? 1752 * @flags: The VMA flags to test. 1753 * Returns: true if the flags permit merging, false otherwise. 1754 */ 1755 static inline bool vma_flags_can_merge(const vma_flags_t *flags) 1756 { 1757 /* 1758 * VMA merging assumes that a VMA's flags and fields completely describe 1759 * its state, which is only the case for mm-backed mappings. 1760 * 1761 * Mappings which are not mm-managed may have established state upon 1762 * mapping not embodied in any attribute of the VMA. 1763 * 1764 * Additionally, private (CoW) PFN maps encode the source PFN of the 1765 * range in vma->vm_pgoff, which may otherwise cause spurious merges. 1766 */ 1767 if (!vma_flags_is_mm_backed(flags)) 1768 return false; 1769 /* 1770 * hugetlb mappings contain per-VMA state, so their flags and fields do 1771 * not completely describe them. 1772 */ 1773 return !vma_flags_is_hugetlb(flags); 1774 } 1775 1776 /** 1777 * vma_can_merge() - Do @vma's flags permit it to be merged with another VMA? 1778 * @vma: The VMA to test. 1779 * Returns: true if the flags permit merging, otherwise false. 1780 */ 1781 static inline bool vma_can_merge(const struct vm_area_struct *vma) 1782 { 1783 return vma_flags_can_merge(&vma->flags); 1784 } 1785 1786 /** 1787 * vma_kernel_pagesize - Default page size granularity for this VMA. 1788 * @vma: The user mapping. 1789 * 1790 * The kernel page size specifies in which granularity VMA modifications 1791 * can be performed. Folios in this VMA will be aligned to, and at least 1792 * the size of the number of bytes returned by this function. 1793 * 1794 * The default kernel page size is not affected by Transparent Huge Pages 1795 * being in effect. 1796 * 1797 * Return: The default page size granularity for this VMA. 1798 */ 1799 static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 1800 { 1801 if (unlikely(vma->vm_ops && vma->vm_ops->pagesize)) 1802 return vma->vm_ops->pagesize(vma); 1803 return PAGE_SIZE; 1804 } 1805 1806 unsigned long vma_mmu_pagesize(struct vm_area_struct *vma); 1807 1808 static inline 1809 struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max) 1810 { 1811 return mas_find(&vmi->mas, max - 1); 1812 } 1813 1814 static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi) 1815 { 1816 /* 1817 * Uses mas_find() to get the first VMA when the iterator starts. 1818 * Calling mas_next() could skip the first entry. 1819 */ 1820 return mas_find(&vmi->mas, ULONG_MAX); 1821 } 1822 1823 static inline 1824 struct vm_area_struct *vma_iter_next_range(struct vma_iterator *vmi) 1825 { 1826 return mas_next_range(&vmi->mas, ULONG_MAX); 1827 } 1828 1829 1830 static inline struct vm_area_struct *vma_prev(struct vma_iterator *vmi) 1831 { 1832 return mas_prev(&vmi->mas, 0); 1833 } 1834 1835 static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, 1836 unsigned long start, unsigned long end, gfp_t gfp) 1837 { 1838 __mas_set_range(&vmi->mas, start, end - 1); 1839 mas_store_gfp(&vmi->mas, NULL, gfp); 1840 if (unlikely(mas_is_err(&vmi->mas))) 1841 return -ENOMEM; 1842 1843 return 0; 1844 } 1845 1846 /* Free any unused preallocations */ 1847 static inline void vma_iter_free(struct vma_iterator *vmi) 1848 { 1849 mas_destroy(&vmi->mas); 1850 } 1851 1852 static inline int vma_iter_bulk_store(struct vma_iterator *vmi, 1853 struct vm_area_struct *vma) 1854 { 1855 vmi->mas.index = vma->vm_start; 1856 vmi->mas.last = vma->vm_end - 1; 1857 mas_store(&vmi->mas, vma); 1858 if (unlikely(mas_is_err(&vmi->mas))) 1859 return -ENOMEM; 1860 1861 vma_mark_attached(vma); 1862 return 0; 1863 } 1864 1865 static inline void vma_iter_invalidate(struct vma_iterator *vmi) 1866 { 1867 mas_pause(&vmi->mas); 1868 } 1869 1870 static inline void vma_iter_set(struct vma_iterator *vmi, unsigned long addr) 1871 { 1872 mas_set(&vmi->mas, addr); 1873 } 1874 1875 #define for_each_vma(__vmi, __vma) \ 1876 while (((__vma) = vma_next(&(__vmi))) != NULL) 1877 1878 /* The MM code likes to work with exclusive end addresses */ 1879 #define for_each_vma_range(__vmi, __vma, __end) \ 1880 while (((__vma) = vma_find(&(__vmi), (__end))) != NULL) 1881 1882 #ifdef CONFIG_SHMEM 1883 /* 1884 * The vma_is_shmem is not inline because it is used only by slow 1885 * paths in userfault. 1886 */ 1887 bool vma_is_shmem(const struct vm_area_struct *vma); 1888 bool vma_is_anon_shmem(const struct vm_area_struct *vma); 1889 #else 1890 static inline bool vma_is_shmem(const struct vm_area_struct *vma) { return false; } 1891 static inline bool vma_is_anon_shmem(const struct vm_area_struct *vma) { return false; } 1892 #endif 1893 1894 int vma_is_stack_for_current(const struct vm_area_struct *vma); 1895 1896 /* flush_tlb_range() takes a vma, not a mm, and can care about flags */ 1897 #define TLB_FLUSH_VMA(mm,flags) { .vm_mm = (mm), .vm_flags = (flags) } 1898 1899 struct mmu_gather; 1900 struct inode; 1901 1902 extern void prep_compound_page(struct page *page, unsigned int order); 1903 1904 static inline unsigned int folio_large_order(const struct folio *folio) 1905 { 1906 return folio->_flags_1 & 0xff; 1907 } 1908 1909 #ifdef NR_PAGES_IN_LARGE_FOLIO 1910 static inline unsigned long folio_large_nr_pages(const struct folio *folio) 1911 { 1912 return folio->_nr_pages; 1913 } 1914 #else 1915 static inline unsigned long folio_large_nr_pages(const struct folio *folio) 1916 { 1917 return 1L << folio_large_order(folio); 1918 } 1919 #endif 1920 1921 /* 1922 * compound_order() can be called without holding a reference, which means 1923 * that niceties like page_folio() don't work. These callers should be 1924 * prepared to handle wild return values. For example, PG_head may be 1925 * set before the order is initialised, or this may be a tail page. 1926 * See compaction.c for some good examples. 1927 */ 1928 static inline unsigned int compound_order(const struct page *page) 1929 { 1930 const struct folio *folio = (struct folio *)page; 1931 1932 if (!test_bit(PG_head, &folio->flags.f)) 1933 return 0; 1934 return folio_large_order(folio); 1935 } 1936 1937 /** 1938 * folio_order - The allocation order of a folio. 1939 * @folio: The folio. 1940 * 1941 * A folio is composed of 2^order pages. See get_order() for the definition 1942 * of order. 1943 * 1944 * Return: The order of the folio. 1945 */ 1946 static inline unsigned int folio_order(const struct folio *folio) 1947 { 1948 if (!folio_test_large(folio)) 1949 return 0; 1950 return folio_large_order(folio); 1951 } 1952 1953 /** 1954 * folio_reset_order - Reset the folio order and derived _nr_pages 1955 * @folio: The folio. 1956 * 1957 * Reset the order and derived _nr_pages to 0. Must only be used in the 1958 * process of splitting large folios. 1959 */ 1960 static inline void folio_reset_order(struct folio *folio) 1961 { 1962 if (WARN_ON_ONCE(!folio_test_large(folio))) 1963 return; 1964 folio->_flags_1 &= ~0xffUL; 1965 #ifdef NR_PAGES_IN_LARGE_FOLIO 1966 folio->_nr_pages = 0; 1967 #endif 1968 } 1969 1970 #include <linux/huge_mm.h> 1971 1972 /* 1973 * Methods to modify the page usage count. 1974 * 1975 * What counts for a page usage: 1976 * - cache mapping (page->mapping) 1977 * - private data (page->private) 1978 * - page mapped in a task's page tables, each mapping 1979 * is counted separately 1980 * 1981 * Also, many kernel routines increase the page count before a critical 1982 * routine so they can be sure the page doesn't go away from under them. 1983 */ 1984 1985 /* 1986 * Drop a ref, return true if the refcount fell to zero (the page has no users) 1987 */ 1988 static inline int put_page_testzero(struct page *page) 1989 { 1990 VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); 1991 return page_ref_dec_and_test(page); 1992 } 1993 1994 static inline int folio_put_testzero(struct folio *folio) 1995 { 1996 return put_page_testzero(&folio->page); 1997 } 1998 1999 /* 2000 * Try to grab a ref unless the page has a refcount of zero, return false if 2001 * that is the case. 2002 * This can be called when MMU is off so it must not access 2003 * any of the virtual mappings. 2004 */ 2005 static inline bool get_page_unless_zero(struct page *page) 2006 { 2007 return page_ref_add_unless_zero(page, 1); 2008 } 2009 2010 static inline struct folio *folio_get_nontail_page(struct page *page) 2011 { 2012 if (unlikely(!get_page_unless_zero(page))) 2013 return NULL; 2014 return (struct folio *)page; 2015 } 2016 2017 extern int page_is_ram(unsigned long pfn); 2018 2019 enum { 2020 REGION_INTERSECTS, 2021 REGION_DISJOINT, 2022 REGION_MIXED, 2023 }; 2024 2025 int region_intersects(resource_size_t offset, size_t size, unsigned long flags, 2026 unsigned long desc); 2027 2028 /* Support for virtually mapped pages */ 2029 struct page *vmalloc_to_page(const void *addr); 2030 unsigned long vmalloc_to_pfn(const void *addr); 2031 2032 /* 2033 * Determine if an address is within the vmalloc range 2034 * 2035 * On nommu, vmalloc/vfree wrap through kmalloc/kfree directly, so there 2036 * is no special casing required. 2037 */ 2038 #ifdef CONFIG_MMU 2039 extern bool is_vmalloc_addr(const void *x); 2040 extern int is_vmalloc_or_module_addr(const void *x); 2041 #else 2042 static inline bool is_vmalloc_addr(const void *x) 2043 { 2044 return false; 2045 } 2046 static inline int is_vmalloc_or_module_addr(const void *x) 2047 { 2048 return 0; 2049 } 2050 #endif 2051 2052 /* 2053 * How many times the entire folio is mapped as a single unit (eg by a 2054 * PMD or PUD entry). This is probably not what you want, except for 2055 * debugging purposes or implementation of other core folio_*() primitives. 2056 */ 2057 static inline int folio_entire_mapcount(const struct folio *folio) 2058 { 2059 VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); 2060 if (!IS_ENABLED(CONFIG_64BIT) && unlikely(folio_large_order(folio) == 1)) 2061 return 0; 2062 return atomic_read(&folio->_entire_mapcount) + 1; 2063 } 2064 2065 static inline int folio_large_mapcount(const struct folio *folio) 2066 { 2067 VM_WARN_ON_FOLIO(!folio_test_large(folio), folio); 2068 return atomic_read(&folio->_large_mapcount) + 1; 2069 } 2070 2071 /** 2072 * folio_mapcount() - Number of mappings of this folio. 2073 * @folio: The folio. 2074 * 2075 * The folio mapcount corresponds to the number of present user page table 2076 * entries that reference any part of a folio. Each such present user page 2077 * table entry must be paired with exactly on folio reference. 2078 * 2079 * For ordindary folios, each user page table entry (PTE/PMD/PUD/...) counts 2080 * exactly once. 2081 * 2082 * For hugetlb folios, each abstracted "hugetlb" user page table entry that 2083 * references the entire folio counts exactly once, even when such special 2084 * page table entries are comprised of multiple ordinary page table entries. 2085 * 2086 * Will report 0 for pages which cannot be mapped into userspace, such as 2087 * slab, page tables and similar. 2088 * 2089 * Return: The number of times this folio is mapped. 2090 */ 2091 static inline int folio_mapcount(const struct folio *folio) 2092 { 2093 int mapcount; 2094 2095 if (likely(!folio_test_large(folio))) { 2096 mapcount = atomic_read(&folio->_mapcount) + 1; 2097 if (page_mapcount_is_type(mapcount)) 2098 mapcount = 0; 2099 return mapcount; 2100 } 2101 return folio_large_mapcount(folio); 2102 } 2103 2104 /** 2105 * folio_mapped - Is this folio mapped into userspace? 2106 * @folio: The folio. 2107 * 2108 * Return: True if any page in this folio is referenced by user page tables. 2109 */ 2110 static inline bool folio_mapped(const struct folio *folio) 2111 { 2112 return folio_mapcount(folio) >= 1; 2113 } 2114 2115 static inline struct page *virt_to_head_page(const void *x) 2116 { 2117 struct page *page = virt_to_page(x); 2118 2119 return compound_head(page); 2120 } 2121 2122 static inline struct folio *virt_to_folio(const void *x) 2123 { 2124 struct page *page = virt_to_page(x); 2125 2126 return page_folio(page); 2127 } 2128 2129 void __folio_put(struct folio *folio); 2130 2131 void split_page(struct page *page, unsigned int order); 2132 void folio_copy(struct folio *dst, struct folio *src); 2133 int folio_mc_copy(struct folio *dst, struct folio *src); 2134 2135 unsigned long nr_free_buffer_pages(void); 2136 2137 /* Returns the number of bytes in this potentially compound page. */ 2138 static inline unsigned long page_size(const struct page *page) 2139 { 2140 return PAGE_SIZE << compound_order(page); 2141 } 2142 2143 /* Returns the number of bits needed for the number of bytes in a page */ 2144 static inline unsigned int page_shift(struct page *page) 2145 { 2146 return PAGE_SHIFT + compound_order(page); 2147 } 2148 2149 /** 2150 * thp_order - Order of a transparent huge page. 2151 * @page: Head page of a transparent huge page. 2152 */ 2153 static inline unsigned int thp_order(struct page *page) 2154 { 2155 VM_BUG_ON_PGFLAGS(PageTail(page), page); 2156 return compound_order(page); 2157 } 2158 2159 /** 2160 * thp_size - Size of a transparent huge page. 2161 * @page: Head page of a transparent huge page. 2162 * 2163 * Return: Number of bytes in this page. 2164 */ 2165 static inline unsigned long thp_size(struct page *page) 2166 { 2167 return PAGE_SIZE << thp_order(page); 2168 } 2169 2170 #ifdef CONFIG_MMU 2171 /* 2172 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when 2173 * servicing faults for write access. In the normal case, do always want 2174 * pte_mkwrite. But get_user_pages can cause write faults for mappings 2175 * that do not have writing enabled, when used by access_process_vm. 2176 */ 2177 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) 2178 { 2179 if (likely(vma->vm_flags & VM_WRITE)) 2180 pte = pte_mkwrite(pte, vma); 2181 return pte; 2182 } 2183 2184 vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page); 2185 void set_pte_range(struct vm_fault *vmf, struct folio *folio, 2186 struct page *page, unsigned int nr, unsigned long addr); 2187 2188 vm_fault_t finish_fault(struct vm_fault *vmf); 2189 #endif 2190 2191 /* 2192 * Multiple processes may "see" the same page. E.g. for untouched 2193 * mappings of /dev/null, all processes see the same page full of 2194 * zeroes, and text pages of executables and shared libraries have 2195 * only one copy in memory, at most, normally. 2196 * 2197 * For the non-reserved pages, page_count(page) denotes a reference count. 2198 * page_count() == 0 means the page is free. page->lru is then used for 2199 * freelist management in the buddy allocator. 2200 * page_count() > 0 means the page has been allocated. 2201 * 2202 * Pages are allocated by the slab allocator in order to provide memory 2203 * to kmalloc and kmem_cache_alloc. In this case, the management of the 2204 * page, and the fields in 'struct page' are the responsibility of mm/slab.c 2205 * unless a particular usage is carefully commented. (the responsibility of 2206 * freeing the kmalloc memory is the caller's, of course). 2207 * 2208 * A page may be used by anyone else who does a __get_free_page(). 2209 * In this case, page_count still tracks the references, and should only 2210 * be used through the normal accessor functions. The top bits of page->flags 2211 * and page->virtual store page management information, but all other fields 2212 * are unused and could be used privately, carefully. The management of this 2213 * page is the responsibility of the one who allocated it, and those who have 2214 * subsequently been given references to it. 2215 * 2216 * The other pages (we may call them "pagecache pages") are completely 2217 * managed by the Linux memory manager: I/O, buffers, swapping etc. 2218 * The following discussion applies only to them. 2219 * 2220 * A pagecache page contains an opaque `private' member, which belongs to the 2221 * page's address_space. Usually, this is the address of a circular list of 2222 * the page's disk buffers. It tells the VM to call into the filesystem to 2223 * release these pages. 2224 * 2225 * A folio may belong to an inode's memory mapping. In this case, 2226 * folio->mapping points to the inode, and folio->index is the file 2227 * offset of the folio, in units of PAGE_SIZE. 2228 * 2229 * If pagecache folios are not associated with an inode, they are said to be 2230 * anonymous folios. These may become associated with the swapcache, and in that 2231 * case PG_swapcache is set, and folio->private is an offset into the swapcache. 2232 * 2233 * In either case (swapcache or inode backed), the pagecache itself holds one 2234 * reference to the folio. Attaching filesystem private data via 2235 * folio_attach_private() also increments the refcount. Each user mapping also 2236 * has a reference to the folio. 2237 * 2238 * The pagecache pages are stored in a per-mapping radix tree, which is 2239 * rooted at mapping->i_pages, and indexed by offset. 2240 * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space 2241 * lists, we instead now tag pages as dirty/writeback in the radix tree. 2242 * 2243 * All pagecache pages may be subject to I/O: 2244 * - inode pages may need to be read from disk, 2245 * - inode pages which have been modified and are MAP_SHARED may need 2246 * to be written back to the inode on disk, 2247 * - anonymous pages (including MAP_PRIVATE file mappings) which have been 2248 * modified may need to be swapped out to swap space and (later) to be read 2249 * back into memory. 2250 */ 2251 2252 /* Leave separate reserves for administrators and concurrent reference gains. */ 2253 #define FOLIO_REF_COUNT_LIMIT (INT_MAX - (1U << 26)) 2254 #define FOLIO_REF_COUNT_ADMIN_LIMIT (INT_MAX - (1U << 25)) 2255 #define FOLIO_REF_COUNT_FLUSH_LIMIT (INT_MAX - (1U << 27)) 2256 2257 bool folio_ref_count_over_limit_slowpath(const struct folio *folio, 2258 unsigned int ref_count); 2259 2260 /** 2261 * __folio_ref_count_over_limit - Check whether to refuse more folio references. 2262 * @folio: The folio. 2263 * @ref_count: The sampled folio reference count. 2264 * 2265 * Check before adding page table mappings or other significant refcount users 2266 * to pagecache folios (!hugetlb), either before acquiring references or 2267 * afterwards with rollback if the operation is refused. 2268 * 2269 * Also required when pinning folios without a dedicated pincount that use 2270 * GUP_PIN_COUNTING_BIAS. 2271 * 2272 * Checking other folios does no harm. 2273 * 2274 * This check is an approximation; we leave some headroom such that concurrent 2275 * increments, batches, temporary references, or other bounded refcount 2276 * consumers are not a problem. 2277 * 2278 * Return: True if the caller should refuse the operation, false otherwise. 2279 */ 2280 static inline bool __folio_ref_count_over_limit(const struct folio *folio, 2281 unsigned int ref_count) 2282 { 2283 if (likely(ref_count < FOLIO_REF_COUNT_LIMIT)) 2284 return false; 2285 return folio_ref_count_over_limit_slowpath(folio, ref_count); 2286 } 2287 2288 /** 2289 * folio_ref_count_over_limit - Check whether to refuse more folio references. 2290 * @folio: The folio. 2291 * 2292 * See __folio_ref_count_over_limit(). 2293 * 2294 * Return: True if the caller should refuse the operation, false otherwise. 2295 */ 2296 static inline bool folio_ref_count_over_limit(const struct folio *folio) 2297 { 2298 return __folio_ref_count_over_limit(folio, folio_ref_count(folio)); 2299 } 2300 2301 /** 2302 * folio_get - Increment the reference count on a folio. 2303 * @folio: The folio. 2304 * 2305 * Context: May be called in any context, as long as you know that 2306 * you have a refcount on the folio. If you do not already have one, 2307 * folio_try_get() may be the right interface for you to use. 2308 */ 2309 static inline void folio_get(struct folio *folio) 2310 { 2311 VM_WARN_ON_FOLIO(folio_ref_count(folio) <= 0, folio); 2312 folio_ref_inc(folio); 2313 } 2314 2315 static inline void get_page(struct page *page) 2316 { 2317 struct folio *folio = page_folio(page); 2318 if (WARN_ON_ONCE(folio_test_slab(folio))) 2319 return; 2320 if (WARN_ON_ONCE(folio_test_large_kmalloc(folio))) 2321 return; 2322 folio_get(folio); 2323 } 2324 2325 /** 2326 * folio_get_checked - Try to acquire a reference to a live folio. 2327 * @folio: The folio. 2328 * 2329 * The caller must already hold a reference to @folio. Warn and fail on a 2330 * non-positive refcount, and refuse additional references at the limit. 2331 * 2332 * Use folio_try_get() for speculative reference acquisition where the 2333 * refcount can legitimately be zero. 2334 * 2335 * Return: True if the refcount was incremented, false otherwise. 2336 */ 2337 static inline __must_check bool folio_get_checked(struct folio *folio) 2338 { 2339 const int ref_count = folio_ref_count(folio); 2340 2341 if (WARN_ON_ONCE(ref_count <= 0)) 2342 return false; 2343 if (unlikely(__folio_ref_count_over_limit(folio, ref_count))) 2344 return false; 2345 folio_ref_inc(folio); 2346 return true; 2347 } 2348 2349 static inline __must_check bool try_get_page(struct page *page) 2350 { 2351 return folio_get_checked(page_folio(page)); 2352 } 2353 2354 /** 2355 * folio_put - Decrement the reference count on a folio. 2356 * @folio: The folio. 2357 * 2358 * If the folio's reference count reaches zero, the memory will be 2359 * released back to the page allocator and may be used by another 2360 * allocation immediately. Do not access the memory or the struct folio 2361 * after calling folio_put() unless you can be sure that it wasn't the 2362 * last reference. 2363 * 2364 * Context: May be called in process or interrupt context, but not in NMI 2365 * context. May be called while holding a spinlock. 2366 */ 2367 static inline void folio_put(struct folio *folio) 2368 { 2369 if (folio_put_testzero(folio)) 2370 __folio_put(folio); 2371 } 2372 2373 /** 2374 * folio_put_refs - Reduce the reference count on a folio. 2375 * @folio: The folio. 2376 * @refs: The amount to subtract from the folio's reference count. 2377 * 2378 * If the folio's reference count reaches zero, the memory will be 2379 * released back to the page allocator and may be used by another 2380 * allocation immediately. Do not access the memory or the struct folio 2381 * after calling folio_put_refs() unless you can be sure that these weren't 2382 * the last references. 2383 * 2384 * Context: May be called in process or interrupt context, but not in NMI 2385 * context. May be called while holding a spinlock. 2386 */ 2387 static inline void folio_put_refs(struct folio *folio, int refs) 2388 { 2389 if (folio_ref_sub_and_test(folio, refs)) 2390 __folio_put(folio); 2391 } 2392 2393 void folios_put_refs(struct folio_batch *folios, unsigned int *refs); 2394 2395 /* 2396 * union release_pages_arg - an array of pages or folios 2397 * 2398 * release_pages() releases a simple array of multiple pages, and 2399 * accepts various different forms of said page array: either 2400 * a regular old boring array of pages, an array of folios, or 2401 * an array of encoded page pointers. 2402 * 2403 * The transparent union syntax for this kind of "any of these 2404 * argument types" is all kinds of ugly, so look away. 2405 */ 2406 typedef union { 2407 struct page **pages; 2408 struct folio **folios; 2409 struct encoded_page **encoded_pages; 2410 } release_pages_arg __attribute__ ((__transparent_union__)); 2411 2412 void release_pages(release_pages_arg, int nr); 2413 2414 /** 2415 * folios_put - Decrement the reference count on an array of folios. 2416 * @folios: The folios. 2417 * 2418 * Like folio_put(), but for a batch of folios. This is more efficient 2419 * than writing the loop yourself as it will optimise the locks which need 2420 * to be taken if the folios are freed. The folios batch is returned 2421 * empty and ready to be reused for another batch; there is no need to 2422 * reinitialise it. 2423 * 2424 * Context: May be called in process or interrupt context, but not in NMI 2425 * context. May be called while holding a spinlock. 2426 */ 2427 static inline void folios_put(struct folio_batch *folios) 2428 { 2429 folios_put_refs(folios, NULL); 2430 } 2431 2432 static inline void put_page(struct page *page) 2433 { 2434 struct folio *folio = page_folio(page); 2435 2436 if (folio_test_slab(folio) || folio_test_large_kmalloc(folio)) 2437 return; 2438 2439 folio_put(folio); 2440 } 2441 2442 /* 2443 * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload 2444 * the page's refcount so that two separate items are tracked: the original page 2445 * reference count, and also a new count of how many pin_user_pages() calls were 2446 * made against the page. ("gup-pinned" is another term for the latter). 2447 * 2448 * With this scheme, pin_user_pages() becomes special: such pages are marked as 2449 * distinct from normal pages. As such, the unpin_user_page() call (and its 2450 * variants) must be used in order to release gup-pinned pages. 2451 * 2452 * Choice of value: 2453 * 2454 * By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference 2455 * counts with respect to pin_user_pages() and unpin_user_page() becomes 2456 * simpler, due to the fact that adding an even power of two to the page 2457 * refcount has the effect of using only the upper N bits, for the code that 2458 * counts up using the bias value. This means that the lower bits are left for 2459 * the exclusive use of the original code that increments and decrements by one 2460 * (or at least, by much smaller values than the bias value). 2461 * 2462 * Of course, once the lower bits overflow into the upper bits (and this is 2463 * OK, because subtraction recovers the original values), then visual inspection 2464 * no longer suffices to directly view the separate counts. However, for normal 2465 * applications that don't have huge page reference counts, this won't be an 2466 * issue. 2467 * 2468 * Locking: the lockless algorithm described in folio_try_get_rcu() 2469 * provides safe operation for get_user_pages(), folio_mkclean() and 2470 * other calls that race to set up page table entries. 2471 */ 2472 #define GUP_PIN_COUNTING_BIAS (1U << 10) 2473 2474 void unpin_user_page(struct page *page); 2475 void unpin_folio(struct folio *folio); 2476 void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, 2477 bool make_dirty); 2478 void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages, 2479 bool make_dirty); 2480 void unpin_user_pages(struct page **pages, unsigned long npages); 2481 void unpin_user_folio(struct folio *folio, unsigned long npages); 2482 void unpin_folios(struct folio **folios, unsigned long nfolios); 2483 2484 /** 2485 * vma_flags_is_cow_mapping() - Do these VMA flags imply a CoW mapping? 2486 * @flags: The VMA flags to check. 2487 * 2488 * Mappings which could be CoW'd (subject to Copy-On-Write faults) are 2489 * described as CoW mappings. 2490 * 2491 * All mappings backed by anonymous folios (all anonymous mappings and most 2492 * MAP_PRIVATE-file backed ranges) are CoW mappings. 2493 * 2494 * All other mappings (including all MAP_SHARED mappings) are non-CoW. 2495 * 2496 * The criteria are !VMA_SHARED_BIT, VMA_MAYWRITE_BIT. 2497 * 2498 * VMA_MAYWRITE_BIT is checked instead of VMA_WRITE_BIT to account for both 2499 * future mprotect() calls which can render a read-only mapping writable, and 2500 * GUP with FOLL_FORCE (e.g. ptrace) which can CoW a read-only mapping. 2501 * 2502 * - No anonymous mapping can ever clear VMA_MAYWRITE_BIT. 2503 * 2504 * - Writes to anonymous mappings do not immediately result in CoW faults but 2505 * may do so after the process is forked or if a read is followed by a 2506 * write. 2507 * 2508 * - Writes to MAP_PRIVATE file-backed mappings result in CoW faults and may 2509 * do so again after fork. 2510 * 2511 * - MAP_SHARED mappings of a file opened read-only are transformed into 2512 * VMA_MAYSHARE_BIT, !VMA_SHARED_BIT, !VMA_MAYWRITE_BIT mappings, so remain 2513 * non-CoW. 2514 * 2515 * - Drivers may clear VMA_MAYWRITE_BIT but do so at mmap() time and cannot 2516 * mark themselves anonymous. Having cleared this flag it is not valid for 2517 * them to leave the VMA_WRITE_BIT flag set. 2518 * 2519 * As a consequence, the anonymous reverse mapping only tracks CoW mappings. 2520 * 2521 * Returns: true if the flags indicate a CoW mapping, otherwise false. 2522 */ 2523 static inline bool vma_flags_is_cow_mapping(const vma_flags_t *flags) 2524 { 2525 return vma_flags_test(flags, VMA_MAYWRITE_BIT) && 2526 !vma_flags_test(flags, VMA_SHARED_BIT); 2527 } 2528 2529 /** 2530 * vma_is_cow_mapping() - Is this VMA a CoW mapping? 2531 * @vma: The VMA to check. 2532 * 2533 * See vma_flags_is_cow_mapping() for details. 2534 * 2535 * Returns: true if the VMA is a CoW mapping, otherwise false. 2536 */ 2537 static inline bool vma_is_cow_mapping(const struct vm_area_struct *vma) 2538 { 2539 return vma_flags_is_cow_mapping(&vma->flags); 2540 } 2541 2542 /** 2543 * vma_desc_is_cow_mapping() - Is this VMA descriptor a CoW mapping? 2544 * @desc: The VMA descriptor to check. 2545 * 2546 * See vma_flags_is_cow_mapping() for details. 2547 * 2548 * Returns: true if the VMA descriptor describes a CoW mapping, otherwise 2549 * false. 2550 */ 2551 static inline bool vma_desc_is_cow_mapping(struct vm_area_desc *desc) 2552 { 2553 return vma_flags_is_cow_mapping(&desc->vma_flags); 2554 } 2555 2556 #ifndef CONFIG_MMU 2557 static inline bool is_nommu_shared_mapping(vm_flags_t flags) 2558 { 2559 /* 2560 * NOMMU shared mappings are ordinary MAP_SHARED mappings and selected 2561 * R/O MAP_PRIVATE file mappings that are an effective R/O overlay of 2562 * a file mapping. R/O MAP_PRIVATE mappings might still modify 2563 * underlying memory if ptrace is active, so this is only possible if 2564 * ptrace does not apply. Note that there is no mprotect() to upgrade 2565 * write permissions later. 2566 */ 2567 return flags & (VM_MAYSHARE | VM_MAYOVERLAY); 2568 } 2569 2570 static inline bool is_nommu_shared_vma_flags(const vma_flags_t *flags) 2571 { 2572 return vma_flags_test_any(flags, VMA_MAYSHARE_BIT, VMA_MAYOVERLAY_BIT); 2573 } 2574 #endif 2575 2576 #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP) 2577 #define SECTION_IN_PAGE_FLAGS 2578 #endif 2579 2580 /* 2581 * The identification function is mainly used by the buddy allocator for 2582 * determining if two pages could be buddies. We are not really identifying 2583 * the zone since we could be using the section number id if we do not have 2584 * node id available in page flags. 2585 * We only guarantee that it will return the same value for two combinable 2586 * pages in a zone. 2587 */ 2588 static inline int page_zone_id(struct page *page) 2589 { 2590 return (page->flags.f >> ZONEID_PGSHIFT) & ZONEID_MASK; 2591 } 2592 2593 #ifdef NODE_NOT_IN_PAGE_FLAGS 2594 int memdesc_nid(const memdesc_flags_t *mdf); 2595 #else 2596 #ifdef CONFIG_NUMA 2597 static inline int memdesc_nid(const memdesc_flags_t *mdf) 2598 { 2599 ASSERT_EXCLUSIVE_BITS(mdf->f, NODES_MASK << NODES_PGSHIFT); 2600 return (mdf->f >> NODES_PGSHIFT) & NODES_MASK; 2601 } 2602 #else 2603 static inline int memdesc_nid(const memdesc_flags_t *mdf) 2604 { 2605 return 0; 2606 } 2607 #endif 2608 #endif 2609 2610 static inline int page_to_nid(const struct page *page) 2611 { 2612 return memdesc_nid(&(PF_POISONED_CHECK(page)->flags)); 2613 } 2614 2615 static inline int folio_nid(const struct folio *folio) 2616 { 2617 return memdesc_nid(&folio->flags); 2618 } 2619 2620 #ifdef CONFIG_NUMA_BALANCING 2621 /* page access time bits needs to hold at least 4 seconds */ 2622 #define PAGE_ACCESS_TIME_MIN_BITS 12 2623 #if LAST_CPUPID_SHIFT < PAGE_ACCESS_TIME_MIN_BITS 2624 #define PAGE_ACCESS_TIME_BUCKETS \ 2625 (PAGE_ACCESS_TIME_MIN_BITS - LAST_CPUPID_SHIFT) 2626 #else 2627 #define PAGE_ACCESS_TIME_BUCKETS 0 2628 #endif 2629 2630 #define PAGE_ACCESS_TIME_MASK \ 2631 (LAST_CPUPID_MASK << PAGE_ACCESS_TIME_BUCKETS) 2632 2633 static inline int cpu_pid_to_cpupid(int cpu, int pid) 2634 { 2635 return ((cpu & LAST__CPU_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK); 2636 } 2637 2638 static inline int cpupid_to_pid(int cpupid) 2639 { 2640 return cpupid & LAST__PID_MASK; 2641 } 2642 2643 static inline int cpupid_to_cpu(int cpupid) 2644 { 2645 return (cpupid >> LAST__PID_SHIFT) & LAST__CPU_MASK; 2646 } 2647 2648 static inline int cpupid_to_nid(int cpupid) 2649 { 2650 return cpu_to_node(cpupid_to_cpu(cpupid)); 2651 } 2652 2653 static inline bool cpupid_pid_unset(int cpupid) 2654 { 2655 return cpupid_to_pid(cpupid) == (-1 & LAST__PID_MASK); 2656 } 2657 2658 static inline bool cpupid_cpu_unset(int cpupid) 2659 { 2660 return cpupid_to_cpu(cpupid) == (-1 & LAST__CPU_MASK); 2661 } 2662 2663 static inline bool __cpupid_match_pid(pid_t task_pid, int cpupid) 2664 { 2665 return (task_pid & LAST__PID_MASK) == cpupid_to_pid(cpupid); 2666 } 2667 2668 #define cpupid_match_pid(task, cpupid) __cpupid_match_pid(task->pid, cpupid) 2669 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 2670 static inline int folio_xchg_last_cpupid(struct folio *folio, int cpupid) 2671 { 2672 return xchg(&folio->_last_cpupid, cpupid & LAST_CPUPID_MASK); 2673 } 2674 2675 static inline int folio_last_cpupid(struct folio *folio) 2676 { 2677 return folio->_last_cpupid; 2678 } 2679 static inline void page_cpupid_reset_last(struct page *page) 2680 { 2681 page->_last_cpupid = -1 & LAST_CPUPID_MASK; 2682 } 2683 #else 2684 static inline int folio_last_cpupid(struct folio *folio) 2685 { 2686 return (folio->flags.f >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK; 2687 } 2688 2689 int folio_xchg_last_cpupid(struct folio *folio, int cpupid); 2690 2691 static inline void page_cpupid_reset_last(struct page *page) 2692 { 2693 page->flags.f |= LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT; 2694 } 2695 #endif /* LAST_CPUPID_NOT_IN_PAGE_FLAGS */ 2696 2697 static inline int folio_xchg_access_time(struct folio *folio, int time) 2698 { 2699 int last_time; 2700 2701 last_time = folio_xchg_last_cpupid(folio, 2702 time >> PAGE_ACCESS_TIME_BUCKETS); 2703 return last_time << PAGE_ACCESS_TIME_BUCKETS; 2704 } 2705 2706 static inline void vma_set_access_pid_bit(struct vm_area_struct *vma) 2707 { 2708 unsigned int pid_bit; 2709 2710 pid_bit = hash_32(current->pid, ilog2(BITS_PER_LONG)); 2711 if (vma->numab_state && !test_bit(pid_bit, &vma->numab_state->pids_active[1])) { 2712 __set_bit(pid_bit, &vma->numab_state->pids_active[1]); 2713 } 2714 } 2715 2716 bool folio_use_access_time(struct folio *folio); 2717 #else /* !CONFIG_NUMA_BALANCING */ 2718 static inline int folio_xchg_last_cpupid(struct folio *folio, int cpupid) 2719 { 2720 return folio_nid(folio); /* XXX */ 2721 } 2722 2723 static inline int folio_xchg_access_time(struct folio *folio, int time) 2724 { 2725 return 0; 2726 } 2727 2728 static inline int folio_last_cpupid(struct folio *folio) 2729 { 2730 return folio_nid(folio); /* XXX */ 2731 } 2732 2733 static inline int cpupid_to_nid(int cpupid) 2734 { 2735 return -1; 2736 } 2737 2738 static inline int cpupid_to_pid(int cpupid) 2739 { 2740 return -1; 2741 } 2742 2743 static inline int cpupid_to_cpu(int cpupid) 2744 { 2745 return -1; 2746 } 2747 2748 static inline int cpu_pid_to_cpupid(int nid, int pid) 2749 { 2750 return -1; 2751 } 2752 2753 static inline bool cpupid_pid_unset(int cpupid) 2754 { 2755 return true; 2756 } 2757 2758 static inline void page_cpupid_reset_last(struct page *page) 2759 { 2760 } 2761 2762 static inline bool cpupid_match_pid(struct task_struct *task, int cpupid) 2763 { 2764 return false; 2765 } 2766 2767 static inline void vma_set_access_pid_bit(struct vm_area_struct *vma) 2768 { 2769 } 2770 static inline bool folio_use_access_time(struct folio *folio) 2771 { 2772 return false; 2773 } 2774 #endif /* CONFIG_NUMA_BALANCING */ 2775 2776 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) 2777 2778 /* 2779 * KASAN per-page tags are stored xor'ed with 0xff. This allows to avoid 2780 * setting tags for all pages to native kernel tag value 0xff, as the default 2781 * value 0x00 maps to 0xff. 2782 */ 2783 2784 static inline u8 page_kasan_tag(const struct page *page) 2785 { 2786 u8 tag = KASAN_TAG_KERNEL; 2787 2788 if (kasan_enabled()) { 2789 tag = (page->flags.f >> KASAN_TAG_PGSHIFT) & KASAN_TAG_MASK; 2790 tag ^= 0xff; 2791 } 2792 2793 return tag; 2794 } 2795 2796 static inline void page_kasan_tag_set(struct page *page, u8 tag) 2797 { 2798 unsigned long old_flags, flags; 2799 2800 if (!kasan_enabled()) 2801 return; 2802 2803 tag ^= 0xff; 2804 old_flags = READ_ONCE(page->flags.f); 2805 do { 2806 flags = old_flags; 2807 flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT); 2808 flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT; 2809 } while (unlikely(!try_cmpxchg(&page->flags.f, &old_flags, flags))); 2810 } 2811 2812 static inline void page_kasan_tag_reset(struct page *page) 2813 { 2814 if (kasan_enabled()) 2815 page_kasan_tag_set(page, KASAN_TAG_KERNEL); 2816 } 2817 2818 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ 2819 2820 static inline u8 page_kasan_tag(const struct page *page) 2821 { 2822 return 0xff; 2823 } 2824 2825 static inline void page_kasan_tag_set(struct page *page, u8 tag) { } 2826 static inline void page_kasan_tag_reset(struct page *page) { } 2827 2828 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ 2829 2830 static inline struct zone *page_zone(const struct page *page) 2831 { 2832 return &NODE_DATA(page_to_nid(page))->node_zones[page_zonenum(page)]; 2833 } 2834 2835 static inline pg_data_t *page_pgdat(const struct page *page) 2836 { 2837 return NODE_DATA(page_to_nid(page)); 2838 } 2839 2840 static inline pg_data_t *folio_pgdat(const struct folio *folio) 2841 { 2842 return NODE_DATA(folio_nid(folio)); 2843 } 2844 2845 static inline struct zone *folio_zone(const struct folio *folio) 2846 { 2847 return &folio_pgdat(folio)->node_zones[folio_zonenum(folio)]; 2848 } 2849 2850 #ifdef SECTION_IN_PAGE_FLAGS 2851 static inline void set_page_section(struct page *page, unsigned long section) 2852 { 2853 page->flags.f &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT); 2854 page->flags.f |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT; 2855 } 2856 2857 static inline void set_page_section_from_pfn(struct page *page, 2858 unsigned long pfn) 2859 { 2860 set_page_section(page, pfn_to_section_nr(pfn)); 2861 } 2862 2863 static inline unsigned long memdesc_section(const memdesc_flags_t *mdf) 2864 { 2865 ASSERT_EXCLUSIVE_BITS(mdf->f, SECTIONS_MASK << SECTIONS_PGSHIFT); 2866 return (mdf->f >> SECTIONS_PGSHIFT) & SECTIONS_MASK; 2867 } 2868 #else /* !SECTION_IN_PAGE_FLAGS */ 2869 static inline void set_page_section_from_pfn(struct page *page, 2870 unsigned long pfn) 2871 { 2872 } 2873 2874 static inline unsigned long memdesc_section(const memdesc_flags_t *mdf) 2875 { 2876 return 0; 2877 } 2878 #endif /* SECTION_IN_PAGE_FLAGS */ 2879 2880 /** 2881 * folio_pfn - Return the Page Frame Number of a folio. 2882 * @folio: The folio. 2883 * 2884 * A folio may contain multiple pages. The pages have consecutive 2885 * Page Frame Numbers. 2886 * 2887 * Return: The Page Frame Number of the first page in the folio. 2888 */ 2889 static inline unsigned long folio_pfn(const struct folio *folio) 2890 { 2891 return page_to_pfn(&folio->page); 2892 } 2893 2894 static inline struct folio *pfn_folio(unsigned long pfn) 2895 { 2896 return page_folio(pfn_to_page(pfn)); 2897 } 2898 2899 #ifdef CONFIG_MMU 2900 static inline pte_t mk_pte(const struct page *page, pgprot_t pgprot) 2901 { 2902 return pfn_pte(page_to_pfn(page), pgprot); 2903 } 2904 2905 /** 2906 * folio_mk_pte - Create a PTE for this folio 2907 * @folio: The folio to create a PTE for 2908 * @pgprot: The page protection bits to use 2909 * 2910 * Create a page table entry for the first page of this folio. 2911 * This is suitable for passing to set_ptes(). 2912 * 2913 * Return: A page table entry suitable for mapping this folio. 2914 */ 2915 static inline pte_t folio_mk_pte(const struct folio *folio, pgprot_t pgprot) 2916 { 2917 return pfn_pte(folio_pfn(folio), pgprot); 2918 } 2919 2920 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2921 /** 2922 * folio_mk_pmd - Create a PMD for this folio 2923 * @folio: The folio to create a PMD for 2924 * @pgprot: The page protection bits to use 2925 * 2926 * Create a page table entry for the first page of this folio. 2927 * This is suitable for passing to set_pmd_at(). 2928 * 2929 * Return: A page table entry suitable for mapping this folio. 2930 */ 2931 static inline pmd_t folio_mk_pmd(const struct folio *folio, pgprot_t pgprot) 2932 { 2933 return pmd_mkhuge(pfn_pmd(folio_pfn(folio), pgprot)); 2934 } 2935 2936 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 2937 /** 2938 * folio_mk_pud - Create a PUD for this folio 2939 * @folio: The folio to create a PUD for 2940 * @pgprot: The page protection bits to use 2941 * 2942 * Create a page table entry for the first page of this folio. 2943 * This is suitable for passing to set_pud_at(). 2944 * 2945 * Return: A page table entry suitable for mapping this folio. 2946 */ 2947 static inline pud_t folio_mk_pud(const struct folio *folio, pgprot_t pgprot) 2948 { 2949 return pud_mkhuge(pfn_pud(folio_pfn(folio), pgprot)); 2950 } 2951 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 2952 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 2953 #endif /* CONFIG_MMU */ 2954 2955 static inline bool folio_has_pincount(const struct folio *folio) 2956 { 2957 if (IS_ENABLED(CONFIG_64BIT)) 2958 return folio_test_large(folio); 2959 return folio_order(folio) > 1; 2960 } 2961 2962 /** 2963 * folio_maybe_dma_pinned - Report if a folio may be pinned for DMA. 2964 * @folio: The folio. 2965 * 2966 * This function checks if a folio has been pinned via a call to 2967 * a function in the pin_user_pages() family. 2968 * 2969 * For small folios, the return value is partially fuzzy: false is not fuzzy, 2970 * because it means "definitely not pinned for DMA", but true means "probably 2971 * pinned for DMA, but possibly a false positive due to having at least 2972 * GUP_PIN_COUNTING_BIAS worth of normal folio references". 2973 * 2974 * False positives are OK, because: a) it's unlikely for a folio to 2975 * get that many refcounts, and b) all the callers of this routine are 2976 * expected to be able to deal gracefully with a false positive. 2977 * 2978 * For most large folios, the result will be exactly correct. That's because 2979 * we have more tracking data available: the _pincount field is used 2980 * instead of the GUP_PIN_COUNTING_BIAS scheme. 2981 * 2982 * For more information, please see Documentation/core-api/pin_user_pages.rst. 2983 * 2984 * Return: True, if it is likely that the folio has been "dma-pinned". 2985 * False, if the folio is definitely not dma-pinned. 2986 */ 2987 static inline bool folio_maybe_dma_pinned(struct folio *folio) 2988 { 2989 if (folio_has_pincount(folio)) 2990 return atomic_read(&folio->_pincount) > 0; 2991 2992 /* 2993 * Use unsigned math so even an unexpected negative refcount is 2994 * treated as possibly pinned. 2995 */ 2996 return ((unsigned int)folio_ref_count(folio)) >= 2997 GUP_PIN_COUNTING_BIAS; 2998 } 2999 3000 /* 3001 * This should most likely only be called during fork() to see whether we 3002 * should break the cow immediately for an anon page on the src mm. 3003 * 3004 * The caller has to hold the PT lock and the vma->vm_mm->->write_protect_seq. 3005 */ 3006 static inline bool folio_needs_cow_for_dma(struct vm_area_struct *vma, 3007 struct folio *folio) 3008 { 3009 VM_BUG_ON(!(raw_read_seqcount(&vma->vm_mm->write_protect_seq) & 1)); 3010 3011 if (!mm_flags_test(MMF_HAS_PINNED, vma->vm_mm)) 3012 return false; 3013 3014 return folio_maybe_dma_pinned(folio); 3015 } 3016 3017 /** 3018 * is_zero_page - Query if a page is a zero page 3019 * @page: The page to query 3020 * 3021 * This returns true if @page is one of the permanent zero pages. 3022 */ 3023 static inline bool is_zero_page(const struct page *page) 3024 { 3025 return is_zero_pfn(page_to_pfn(page)); 3026 } 3027 3028 /** 3029 * is_zero_folio - Query if a folio is a zero page 3030 * @folio: The folio to query 3031 * 3032 * This returns true if @folio is one of the permanent zero pages. 3033 */ 3034 static inline bool is_zero_folio(const struct folio *folio) 3035 { 3036 return is_zero_page(&folio->page); 3037 } 3038 3039 /* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */ 3040 #ifdef CONFIG_MIGRATION 3041 static inline bool folio_is_longterm_pinnable(struct folio *folio) 3042 { 3043 #ifdef CONFIG_CMA 3044 int mt = folio_migratetype(folio); 3045 3046 if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE) 3047 return false; 3048 #endif 3049 /* The zero page can be "pinned" but gets special handling. */ 3050 if (is_zero_folio(folio)) 3051 return true; 3052 3053 /* Coherent device memory must always allow eviction. */ 3054 if (folio_is_device_coherent(folio)) 3055 return false; 3056 3057 /* 3058 * Filesystems can only tolerate transient delays to truncate and 3059 * hole-punch operations 3060 */ 3061 if (folio_is_fsdax(folio)) 3062 return false; 3063 3064 /* Otherwise, non-movable zone folios can be pinned. */ 3065 return !folio_is_zone_movable(folio); 3066 3067 } 3068 #else 3069 static inline bool folio_is_longterm_pinnable(struct folio *folio) 3070 { 3071 return true; 3072 } 3073 #endif 3074 3075 static inline void set_page_zone(struct page *page, enum zone_type zone) 3076 { 3077 page->flags.f &= ~(ZONES_MASK << ZONES_PGSHIFT); 3078 page->flags.f |= (zone & ZONES_MASK) << ZONES_PGSHIFT; 3079 } 3080 3081 static inline void set_page_node(struct page *page, unsigned long node) 3082 { 3083 page->flags.f &= ~(NODES_MASK << NODES_PGSHIFT); 3084 page->flags.f |= (node & NODES_MASK) << NODES_PGSHIFT; 3085 } 3086 3087 static inline void set_page_links(struct page *page, enum zone_type zone, 3088 unsigned long node, unsigned long pfn) 3089 { 3090 set_page_zone(page, zone); 3091 set_page_node(page, node); 3092 set_page_section_from_pfn(page, pfn); 3093 } 3094 3095 /** 3096 * folio_nr_pages - The number of pages in the folio. 3097 * @folio: The folio. 3098 * 3099 * Return: A positive power of two. 3100 */ 3101 static inline unsigned long folio_nr_pages(const struct folio *folio) 3102 { 3103 if (!folio_test_large(folio)) 3104 return 1; 3105 return folio_large_nr_pages(folio); 3106 } 3107 3108 /* 3109 * compound_nr() returns the number of pages in this potentially compound 3110 * page. compound_nr() can be called on a tail page, and is defined to 3111 * return 1 in that case. 3112 */ 3113 static inline unsigned long compound_nr(const struct page *page) 3114 { 3115 const struct folio *folio = (struct folio *)page; 3116 3117 if (!test_bit(PG_head, &folio->flags.f)) 3118 return 1; 3119 return folio_large_nr_pages(folio); 3120 } 3121 3122 /** 3123 * folio_next - Move to the next physical folio. 3124 * @folio: The folio we're currently operating on. 3125 * 3126 * If you have physically contiguous memory which may span more than 3127 * one folio (eg a &struct bio_vec), use this function to move from one 3128 * folio to the next. Do not use it if the memory is only virtually 3129 * contiguous as the folios are almost certainly not adjacent to each 3130 * other. This is the folio equivalent to writing ``page++``. 3131 * 3132 * Context: We assume that the folios are refcounted and/or locked at a 3133 * higher level and do not adjust the reference counts. 3134 * Return: The next struct folio. 3135 */ 3136 static inline struct folio *folio_next(struct folio *folio) 3137 { 3138 return (struct folio *)folio_page(folio, folio_nr_pages(folio)); 3139 } 3140 3141 /** 3142 * folio_shift - The size of the memory described by this folio. 3143 * @folio: The folio. 3144 * 3145 * A folio represents a number of bytes which is a power-of-two in size. 3146 * This function tells you which power-of-two the folio is. See also 3147 * folio_size() and folio_order(). 3148 * 3149 * Context: The caller should have a reference on the folio to prevent 3150 * it from being split. It is not necessary for the folio to be locked. 3151 * Return: The base-2 logarithm of the size of this folio. 3152 */ 3153 static inline unsigned int folio_shift(const struct folio *folio) 3154 { 3155 return PAGE_SHIFT + folio_order(folio); 3156 } 3157 3158 /** 3159 * folio_size - The number of bytes in a folio. 3160 * @folio: The folio. 3161 * 3162 * Context: The caller should have a reference on the folio to prevent 3163 * it from being split. It is not necessary for the folio to be locked. 3164 * Return: The number of bytes in this folio. 3165 */ 3166 static inline size_t folio_size(const struct folio *folio) 3167 { 3168 return PAGE_SIZE << folio_order(folio); 3169 } 3170 3171 /** 3172 * folio_maybe_mapped_shared - Whether the folio is mapped into the page 3173 * tables of more than one MM 3174 * @folio: The folio. 3175 * 3176 * This function checks if the folio maybe currently mapped into more than one 3177 * MM ("maybe mapped shared"), or if the folio is certainly mapped into a single 3178 * MM ("mapped exclusively"). 3179 * 3180 * For KSM folios, this function also returns "mapped shared" when a folio is 3181 * mapped multiple times into the same MM, because the individual page mappings 3182 * are independent. 3183 * 3184 * For small anonymous folios and anonymous hugetlb folios, the return 3185 * value will be exactly correct: non-KSM folios can only be mapped at most once 3186 * into an MM, and they cannot be partially mapped. KSM folios are 3187 * considered shared even if mapped multiple times into the same MM. 3188 * 3189 * For other folios, the result can be fuzzy: 3190 * #. For partially-mappable large folios (THP), the return value can wrongly 3191 * indicate "mapped shared" (false positive) if a folio was mapped by 3192 * more than two MMs at one point in time. 3193 * #. For pagecache folios (including hugetlb), the return value can wrongly 3194 * indicate "mapped shared" (false positive) when two VMAs in the same MM 3195 * cover the same file range. 3196 * 3197 * Further, this function only considers current page table mappings that 3198 * are tracked using the folio mapcount(s). 3199 * 3200 * This function does not consider: 3201 * #. If the folio might get mapped in the (near) future (e.g., swapcache, 3202 * pagecache, temporary unmapping for migration). 3203 * #. If the folio is mapped differently (VM_PFNMAP). 3204 * #. If hugetlb page table sharing applies. Callers might want to check 3205 * hugetlb_pmd_shared(). 3206 * 3207 * Return: Whether the folio is estimated to be mapped into more than one MM. 3208 */ 3209 static inline bool folio_maybe_mapped_shared(struct folio *folio) 3210 { 3211 int mapcount = folio_mapcount(folio); 3212 3213 /* Only partially-mappable folios require more care. */ 3214 if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio))) 3215 return mapcount > 1; 3216 3217 /* 3218 * vm_insert_page() without CONFIG_TRANSPARENT_HUGEPAGE ... 3219 * simply assume "mapped shared", nobody should really care 3220 * about this for arbitrary kernel allocations. 3221 */ 3222 if (!IS_ENABLED(CONFIG_MM_ID)) 3223 return true; 3224 3225 /* 3226 * A single mapping implies "mapped exclusively", even if the 3227 * folio flag says something different: it's easier to handle this 3228 * case here instead of on the RMAP hot path. 3229 */ 3230 if (mapcount <= 1) 3231 return false; 3232 return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids); 3233 } 3234 3235 /** 3236 * folio_expected_ref_count - calculate the expected folio refcount 3237 * @folio: the folio 3238 * 3239 * Calculate the expected folio refcount, taking references from the pagecache, 3240 * swapcache, private data (folio->private != NULL) and page table mappings into 3241 * account. Useful in combination with folio_ref_count() to detect unexpected 3242 * references (e.g., GUP or other temporary references). 3243 * 3244 * Does currently not consider references from the LRU cache. If the folio 3245 * was isolated from the LRU (which is the case during migration or split), 3246 * the LRU cache does not apply. 3247 * 3248 * Calling this function on an unmapped folio -- !folio_mapped() -- that is 3249 * locked will return a stable result. 3250 * 3251 * Calling this function on a mapped folio will not result in a stable result, 3252 * because nothing stops additional page table mappings from coming (e.g., 3253 * fork()) or going (e.g., munmap()). 3254 * 3255 * Calling this function without the folio lock will also not result in a 3256 * stable result: for example, the folio might get dropped from the swapcache 3257 * concurrently. 3258 * 3259 * However, even when called without the folio lock or on a mapped folio, 3260 * this function can be used to detect unexpected references early (for example, 3261 * if it makes sense to even lock the folio and unmap it). 3262 * 3263 * The caller must add any reference (e.g., from folio_try_get()) it might be 3264 * holding itself to the result. 3265 * 3266 * Returns: the expected folio refcount. 3267 */ 3268 static inline int folio_expected_ref_count(const struct folio *folio) 3269 { 3270 const int order = folio_order(folio); 3271 int ref_count = 0; 3272 3273 if (WARN_ON_ONCE(page_has_type(&folio->page) && !folio_test_hugetlb(folio))) 3274 return 0; 3275 3276 /* One reference per page from the swapcache. */ 3277 ref_count += folio_test_swapcache(folio) << order; 3278 3279 if (!folio_test_anon(folio)) { 3280 /* 3281 * One reference per page from the pagecache. 3282 * Use data_race() since folio might not be locked. 3283 */ 3284 ref_count += !!data_race(folio->mapping) << order; 3285 /* 3286 * One reference from filesystem private data. 3287 */ 3288 ref_count += folio_has_attached_private(folio); 3289 } 3290 3291 /* One reference per page table mapping. */ 3292 return ref_count + folio_mapcount(folio); 3293 } 3294 3295 #ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE 3296 static inline int arch_make_folio_accessible(struct folio *folio) 3297 { 3298 return 0; 3299 } 3300 #endif 3301 3302 /* 3303 * Some inline functions in vmstat.h depend on page_zone() 3304 */ 3305 #include <linux/vmstat.h> 3306 3307 #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) 3308 #define HASHED_PAGE_VIRTUAL 3309 #endif 3310 3311 #if defined(WANT_PAGE_VIRTUAL) 3312 static inline void *page_address(const struct page *page) 3313 { 3314 return page->virtual; 3315 } 3316 static inline void set_page_address(struct page *page, void *address) 3317 { 3318 page->virtual = address; 3319 } 3320 #define page_address_init() do { } while(0) 3321 #endif 3322 3323 #if defined(HASHED_PAGE_VIRTUAL) 3324 void *page_address(const struct page *page); 3325 void set_page_address(struct page *page, void *virtual); 3326 void page_address_init(void); 3327 #endif 3328 3329 static __always_inline void *lowmem_page_address(const struct page *page) 3330 { 3331 return page_to_virt(page); 3332 } 3333 3334 #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL) 3335 #define page_address(page) lowmem_page_address(page) 3336 #define set_page_address(page, address) do { } while(0) 3337 #define page_address_init() do { } while(0) 3338 #endif 3339 3340 static inline void *folio_address(const struct folio *folio) 3341 { 3342 return page_address(&folio->page); 3343 } 3344 3345 /* 3346 * Return true only if the page has been allocated with 3347 * ALLOC_NO_WATERMARKS and the low watermark was not 3348 * met implying that the system is under some pressure. 3349 */ 3350 static inline bool page_is_pfmemalloc(const struct page *page) 3351 { 3352 /* 3353 * lru.next has bit 1 set if the page is allocated from the 3354 * pfmemalloc reserves. Callers may simply overwrite it if 3355 * they do not need to preserve that information. 3356 */ 3357 return (uintptr_t)page->lru.next & BIT(1); 3358 } 3359 3360 /* 3361 * Return true only if the folio has been allocated with 3362 * ALLOC_NO_WATERMARKS and the low watermark was not 3363 * met implying that the system is under some pressure. 3364 */ 3365 static inline bool folio_is_pfmemalloc(const struct folio *folio) 3366 { 3367 /* 3368 * lru.next has bit 1 set if the page is allocated from the 3369 * pfmemalloc reserves. Callers may simply overwrite it if 3370 * they do not need to preserve that information. 3371 */ 3372 return (uintptr_t)folio->lru.next & BIT(1); 3373 } 3374 3375 /* 3376 * Only to be called by the page allocator on a freshly allocated 3377 * page. 3378 */ 3379 static inline void set_page_pfmemalloc(struct page *page) 3380 { 3381 page->lru.next = (void *)BIT(1); 3382 } 3383 3384 static inline void clear_page_pfmemalloc(struct page *page) 3385 { 3386 page->lru.next = NULL; 3387 } 3388 3389 /* 3390 * Can be called by the pagefault handler when it gets a VM_FAULT_OOM. 3391 */ 3392 extern void pagefault_out_of_memory(void); 3393 3394 #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK) 3395 #define offset_in_folio(folio, p) ((unsigned long)(p) & (folio_size(folio) - 1)) 3396 3397 /* 3398 * Parameter block passed down to zap_pte_range in exceptional cases. 3399 */ 3400 struct zap_details { 3401 struct folio *single_folio; /* Locked folio to be unmapped */ 3402 bool skip_cows; /* Do not zap COWed private pages */ 3403 bool reclaim_pt; /* Need reclaim page tables? */ 3404 bool reaping; /* Reaping, do not block. */ 3405 zap_flags_t zap_flags; /* Extra flags for zapping */ 3406 }; 3407 3408 /* 3409 * Whether to drop the pte markers, for example, the uffd-wp information for 3410 * file-backed memory. This should only be specified when we will completely 3411 * drop the page in the mm, either by truncation or unmapping of the vma. By 3412 * default, the flag is not set. 3413 */ 3414 #define ZAP_FLAG_DROP_MARKER ((__force zap_flags_t) BIT(0)) 3415 /* Set in unmap_vmas() to indicate a final unmap call. Only used by hugetlb */ 3416 #define ZAP_FLAG_UNMAP ((__force zap_flags_t) BIT(1)) 3417 3418 #ifdef CONFIG_MMU 3419 extern bool can_do_mlock(void); 3420 #else 3421 static inline bool can_do_mlock(void) { return false; } 3422 #endif 3423 extern int user_shm_lock(size_t, struct ucounts *); 3424 extern void user_shm_unlock(size_t, struct ucounts *); 3425 3426 struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr, 3427 pte_t pte); 3428 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 3429 pte_t pte); 3430 struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, 3431 unsigned long addr, pmd_t pmd); 3432 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 3433 pmd_t pmd); 3434 struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr, 3435 pud_t pud); 3436 3437 void zap_special_vma_range(struct vm_area_struct *vma, unsigned long address, 3438 unsigned long size); 3439 void zap_vma_range(struct vm_area_struct *vma, unsigned long address, 3440 unsigned long size); 3441 /** 3442 * zap_vma - zap all page table entries in a vma 3443 * @vma: The vma to zap. 3444 */ 3445 static inline void zap_vma(struct vm_area_struct *vma) 3446 { 3447 zap_vma_range(vma, vma->vm_start, vma->vm_end - vma->vm_start); 3448 } 3449 struct mmu_notifier_range; 3450 3451 void free_pgd_range(struct mmu_gather *tlb, unsigned long addr, 3452 unsigned long end, unsigned long floor, unsigned long ceiling); 3453 int 3454 copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma); 3455 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 3456 void *buf, int len, int write); 3457 3458 struct follow_pfnmap_args { 3459 /** 3460 * Inputs: 3461 * @vma: Pointer to @vm_area_struct struct 3462 * @address: the virtual address to walk 3463 */ 3464 struct vm_area_struct *vma; 3465 unsigned long address; 3466 /** 3467 * Internals: 3468 * 3469 * The caller shouldn't touch any of these. 3470 */ 3471 spinlock_t *lock; 3472 pte_t *ptep; 3473 /** 3474 * Outputs: 3475 * 3476 * @pfn: the PFN of the address 3477 * @addr_mask: address mask covering pfn 3478 * @pgprot: the pgprot_t of the mapping 3479 * @writable: whether the mapping is writable 3480 * @special: whether the mapping is a special mapping (real PFN maps) 3481 */ 3482 unsigned long pfn; 3483 unsigned long addr_mask; 3484 pgprot_t pgprot; 3485 bool writable; 3486 bool special; 3487 }; 3488 int follow_pfnmap_start(struct follow_pfnmap_args *args); 3489 void follow_pfnmap_end(struct follow_pfnmap_args *args); 3490 3491 extern void truncate_pagecache(struct inode *inode, loff_t new); 3492 extern void truncate_setsize(struct inode *inode, loff_t newsize); 3493 void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to); 3494 void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end); 3495 int generic_error_remove_folio(struct address_space *mapping, 3496 struct folio *folio); 3497 3498 struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 3499 unsigned long address, struct pt_regs *regs); 3500 3501 #ifdef CONFIG_MMU 3502 extern vm_fault_t handle_mm_fault(struct vm_area_struct *vma, 3503 unsigned long address, unsigned int flags, 3504 struct pt_regs *regs); 3505 extern int fixup_user_fault(struct mm_struct *mm, 3506 unsigned long address, unsigned int fault_flags, 3507 bool *unlocked); 3508 void unmap_mapping_pages(struct address_space *mapping, 3509 pgoff_t start, pgoff_t nr, bool even_cows); 3510 void unmap_mapping_range(struct address_space *mapping, 3511 loff_t const holebegin, loff_t const holelen, int even_cows); 3512 #else 3513 static inline vm_fault_t handle_mm_fault(struct vm_area_struct *vma, 3514 unsigned long address, unsigned int flags, 3515 struct pt_regs *regs) 3516 { 3517 /* should never happen if there's no MMU */ 3518 BUG(); 3519 return VM_FAULT_SIGBUS; 3520 } 3521 static inline int fixup_user_fault(struct mm_struct *mm, unsigned long address, 3522 unsigned int fault_flags, bool *unlocked) 3523 { 3524 /* should never happen if there's no MMU */ 3525 BUG(); 3526 return -EFAULT; 3527 } 3528 static inline void unmap_mapping_pages(struct address_space *mapping, 3529 pgoff_t start, pgoff_t nr, bool even_cows) { } 3530 static inline void unmap_mapping_range(struct address_space *mapping, 3531 loff_t const holebegin, loff_t const holelen, int even_cows) { } 3532 #endif 3533 3534 static inline void unmap_shared_mapping_range(struct address_space *mapping, 3535 loff_t const holebegin, loff_t const holelen) 3536 { 3537 unmap_mapping_range(mapping, holebegin, holelen, 0); 3538 } 3539 3540 static inline struct vm_area_struct *vma_lookup(struct mm_struct *mm, 3541 unsigned long addr); 3542 3543 extern int access_process_vm(struct task_struct *tsk, unsigned long addr, 3544 void *buf, int len, unsigned int gup_flags); 3545 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr, 3546 void *buf, int len, unsigned int gup_flags); 3547 3548 int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, 3549 void *buf, int len, unsigned int gup_flags); 3550 int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, 3551 void *buf, int len, unsigned int gup_flags); 3552 3553 long get_user_pages_remote(struct mm_struct *mm, 3554 unsigned long start, unsigned long nr_pages, 3555 unsigned int gup_flags, struct page **pages, 3556 int *locked); 3557 long pin_user_pages_remote(struct mm_struct *mm, 3558 unsigned long start, unsigned long nr_pages, 3559 unsigned int gup_flags, struct page **pages, 3560 int *locked); 3561 3562 /* 3563 * Retrieves a single page alongside its VMA. Does not support FOLL_NOWAIT. 3564 */ 3565 static inline struct page *get_user_page_vma_remote(struct mm_struct *mm, 3566 unsigned long addr, 3567 int gup_flags, 3568 struct vm_area_struct **vmap) 3569 { 3570 struct page *page; 3571 struct vm_area_struct *vma; 3572 int got; 3573 3574 if (WARN_ON_ONCE(unlikely(gup_flags & FOLL_NOWAIT))) 3575 return ERR_PTR(-EINVAL); 3576 3577 got = get_user_pages_remote(mm, addr, 1, gup_flags, &page, NULL); 3578 3579 if (got < 0) 3580 return ERR_PTR(got); 3581 3582 vma = vma_lookup(mm, addr); 3583 if (WARN_ON_ONCE(!vma)) { 3584 put_page(page); 3585 return ERR_PTR(-EINVAL); 3586 } 3587 3588 *vmap = vma; 3589 return page; 3590 } 3591 3592 long get_user_pages(unsigned long start, unsigned long nr_pages, 3593 unsigned int gup_flags, struct page **pages); 3594 long pin_user_pages(unsigned long start, unsigned long nr_pages, 3595 unsigned int gup_flags, struct page **pages); 3596 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, 3597 struct page **pages, unsigned int gup_flags); 3598 long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, 3599 struct page **pages, unsigned int gup_flags); 3600 long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, 3601 struct folio **folios, unsigned int max_folios, 3602 pgoff_t *offset); 3603 int folio_add_pins(struct folio *folio, unsigned int pins); 3604 3605 int get_user_pages_fast(unsigned long start, int nr_pages, 3606 unsigned int gup_flags, struct page **pages); 3607 int pin_user_pages_fast(unsigned long start, int nr_pages, 3608 unsigned int gup_flags, struct page **pages); 3609 void folio_add_pin(struct folio *folio); 3610 3611 int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc); 3612 int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, 3613 const struct task_struct *task, bool bypass_rlim); 3614 3615 struct kvec; 3616 struct page *get_dump_page(unsigned long addr, int *locked); 3617 3618 bool folio_mark_dirty(struct folio *folio); 3619 bool folio_mark_dirty_lock(struct folio *folio); 3620 bool set_page_dirty(struct page *page); 3621 int set_page_dirty_lock(struct page *page); 3622 3623 int get_cmdline(struct task_struct *task, char *buffer, int buflen); 3624 3625 /* 3626 * Flags used by change_protection(). For now we make it a bitmap so 3627 * that we can pass in multiple flags just like parameters. However 3628 * for now all the callers are only use one of the flags at the same 3629 * time. 3630 */ 3631 /* 3632 * Whether we should manually check if we can map individual PTEs writable, 3633 * because something (e.g., COW, uffd-wp) blocks that from happening for all 3634 * PTEs automatically in a writable mapping. 3635 */ 3636 #define MM_CP_TRY_CHANGE_WRITABLE (1UL << 0) 3637 /* Whether this protection change is for NUMA hints */ 3638 #define MM_CP_PROT_NUMA (1UL << 1) 3639 /* Whether this change is for write protecting */ 3640 #define MM_CP_UFFD_WP (1UL << 2) /* do wp */ 3641 #define MM_CP_UFFD_WP_RESOLVE (1UL << 3) /* Resolve wp */ 3642 #define MM_CP_UFFD_WP_ALL (MM_CP_UFFD_WP | \ 3643 MM_CP_UFFD_WP_RESOLVE) 3644 /* Whether this change is for uffd RWP */ 3645 #define MM_CP_UFFD_RWP (1UL << 4) /* do rwp */ 3646 #define MM_CP_UFFD_RWP_RESOLVE (1UL << 5) /* resolve rwp */ 3647 #define MM_CP_UFFD_RWP_ALL (MM_CP_UFFD_RWP | \ 3648 MM_CP_UFFD_RWP_RESOLVE) 3649 3650 bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, 3651 pte_t pte); 3652 extern long change_protection(struct mmu_gather *tlb, 3653 struct vm_area_struct *vma, unsigned long start, 3654 unsigned long end, unsigned long cp_flags); 3655 extern int mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, 3656 struct vm_area_struct *vma, struct vm_area_struct **pprev, 3657 unsigned long start, unsigned long end, vm_flags_t newflags); 3658 3659 /* 3660 * doesn't attempt to fault and will return short. 3661 */ 3662 int get_user_pages_fast_only(unsigned long start, int nr_pages, 3663 unsigned int gup_flags, struct page **pages); 3664 3665 static inline bool get_user_page_fast_only(unsigned long addr, 3666 unsigned int gup_flags, struct page **pagep) 3667 { 3668 return get_user_pages_fast_only(addr, 1, gup_flags, pagep) == 1; 3669 } 3670 /* 3671 * per-process(per-mm_struct) statistics. 3672 */ 3673 static inline unsigned long get_mm_counter(struct mm_struct *mm, int member) 3674 { 3675 return percpu_counter_read_positive(&mm->rss_stat[member]); 3676 } 3677 3678 static inline unsigned long get_mm_counter_sum(struct mm_struct *mm, int member) 3679 { 3680 return percpu_counter_sum_positive(&mm->rss_stat[member]); 3681 } 3682 3683 void mm_trace_rss_stat(struct mm_struct *mm, int member); 3684 3685 static inline void add_mm_counter(struct mm_struct *mm, int member, long value) 3686 { 3687 percpu_counter_add(&mm->rss_stat[member], value); 3688 3689 mm_trace_rss_stat(mm, member); 3690 } 3691 3692 static inline void inc_mm_counter(struct mm_struct *mm, int member) 3693 { 3694 percpu_counter_inc(&mm->rss_stat[member]); 3695 3696 mm_trace_rss_stat(mm, member); 3697 } 3698 3699 static inline void dec_mm_counter(struct mm_struct *mm, int member) 3700 { 3701 percpu_counter_dec(&mm->rss_stat[member]); 3702 3703 mm_trace_rss_stat(mm, member); 3704 } 3705 3706 /* Optimized variant when folio is already known not to be anon */ 3707 static inline int mm_counter_file(struct folio *folio) 3708 { 3709 if (folio_test_swapbacked(folio)) 3710 return MM_SHMEMPAGES; 3711 return MM_FILEPAGES; 3712 } 3713 3714 static inline int mm_counter(struct folio *folio) 3715 { 3716 if (folio_test_anon(folio)) 3717 return MM_ANONPAGES; 3718 return mm_counter_file(folio); 3719 } 3720 3721 static inline unsigned long get_mm_rss(struct mm_struct *mm) 3722 { 3723 return get_mm_counter(mm, MM_FILEPAGES) + 3724 get_mm_counter(mm, MM_ANONPAGES) + 3725 get_mm_counter(mm, MM_SHMEMPAGES); 3726 } 3727 3728 static inline unsigned long get_mm_rss_sum(struct mm_struct *mm) 3729 { 3730 return get_mm_counter_sum(mm, MM_FILEPAGES) + 3731 get_mm_counter_sum(mm, MM_ANONPAGES) + 3732 get_mm_counter_sum(mm, MM_SHMEMPAGES); 3733 } 3734 3735 static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm) 3736 { 3737 return max(mm->hiwater_rss, get_mm_rss(mm)); 3738 } 3739 3740 static inline unsigned long get_mm_hiwater_vm(struct mm_struct *mm) 3741 { 3742 return max(mm->hiwater_vm, mm->total_vm); 3743 } 3744 3745 static inline void update_hiwater_rss(struct mm_struct *mm) 3746 { 3747 unsigned long _rss = get_mm_rss(mm); 3748 3749 if (data_race(mm->hiwater_rss) < _rss) 3750 data_race(mm->hiwater_rss = _rss); 3751 } 3752 3753 static inline void update_hiwater_vm(struct mm_struct *mm) 3754 { 3755 if (mm->hiwater_vm < mm->total_vm) 3756 mm->hiwater_vm = mm->total_vm; 3757 } 3758 3759 static inline void reset_mm_hiwater_rss(struct mm_struct *mm) 3760 { 3761 mm->hiwater_rss = get_mm_rss(mm); 3762 } 3763 3764 static inline void setmax_mm_hiwater_rss(unsigned long *maxrss, 3765 struct mm_struct *mm) 3766 { 3767 unsigned long hiwater_rss = get_mm_hiwater_rss(mm); 3768 3769 if (*maxrss < hiwater_rss) 3770 *maxrss = hiwater_rss; 3771 } 3772 3773 #ifndef CONFIG_ARCH_HAS_PTE_SPECIAL 3774 static inline int pte_special(pte_t pte) 3775 { 3776 return 0; 3777 } 3778 3779 static inline pte_t pte_mkspecial(pte_t pte) 3780 { 3781 return pte; 3782 } 3783 #endif 3784 3785 #ifndef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP 3786 static inline bool pmd_special(pmd_t pmd) 3787 { 3788 return false; 3789 } 3790 3791 static inline pmd_t pmd_mkspecial(pmd_t pmd) 3792 { 3793 return pmd; 3794 } 3795 #endif /* CONFIG_ARCH_SUPPORTS_PMD_PFNMAP */ 3796 3797 #ifndef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP 3798 static inline bool pud_special(pud_t pud) 3799 { 3800 return false; 3801 } 3802 3803 static inline pud_t pud_mkspecial(pud_t pud) 3804 { 3805 return pud; 3806 } 3807 #endif /* CONFIG_ARCH_SUPPORTS_PUD_PFNMAP */ 3808 3809 extern pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr, 3810 spinlock_t **ptl); 3811 3812 #ifdef __PAGETABLE_P4D_FOLDED 3813 static inline int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, 3814 unsigned long address) 3815 { 3816 return 0; 3817 } 3818 #else 3819 int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address); 3820 #endif 3821 3822 #if defined(__PAGETABLE_PUD_FOLDED) || !defined(CONFIG_MMU) 3823 static inline int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, 3824 unsigned long address) 3825 { 3826 return 0; 3827 } 3828 static inline void mm_inc_nr_puds(struct mm_struct *mm) {} 3829 static inline void mm_dec_nr_puds(struct mm_struct *mm) {} 3830 3831 #else 3832 int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address); 3833 3834 static inline void mm_inc_nr_puds(struct mm_struct *mm) 3835 { 3836 if (mm_pud_folded(mm)) 3837 return; 3838 atomic_long_add(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes); 3839 } 3840 3841 static inline void mm_dec_nr_puds(struct mm_struct *mm) 3842 { 3843 if (mm_pud_folded(mm)) 3844 return; 3845 atomic_long_sub(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes); 3846 } 3847 #endif 3848 3849 #if defined(__PAGETABLE_PMD_FOLDED) || !defined(CONFIG_MMU) 3850 static inline int __pmd_alloc(struct mm_struct *mm, pud_t *pud, 3851 unsigned long address) 3852 { 3853 return 0; 3854 } 3855 3856 static inline void mm_inc_nr_pmds(struct mm_struct *mm) {} 3857 static inline void mm_dec_nr_pmds(struct mm_struct *mm) {} 3858 3859 #else 3860 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address); 3861 3862 static inline void mm_inc_nr_pmds(struct mm_struct *mm) 3863 { 3864 if (mm_pmd_folded(mm)) 3865 return; 3866 atomic_long_add(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes); 3867 } 3868 3869 static inline void mm_dec_nr_pmds(struct mm_struct *mm) 3870 { 3871 if (mm_pmd_folded(mm)) 3872 return; 3873 atomic_long_sub(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes); 3874 } 3875 #endif 3876 3877 #ifdef CONFIG_MMU 3878 static inline void mm_pgtables_bytes_init(struct mm_struct *mm) 3879 { 3880 atomic_long_set(&mm->pgtables_bytes, 0); 3881 } 3882 3883 static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm) 3884 { 3885 return atomic_long_read(&mm->pgtables_bytes); 3886 } 3887 3888 static inline void mm_inc_nr_ptes(struct mm_struct *mm) 3889 { 3890 atomic_long_add(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes); 3891 } 3892 3893 static inline void mm_dec_nr_ptes(struct mm_struct *mm) 3894 { 3895 atomic_long_sub(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes); 3896 } 3897 #else 3898 3899 static inline void mm_pgtables_bytes_init(struct mm_struct *mm) {} 3900 static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm) 3901 { 3902 return 0; 3903 } 3904 3905 static inline void mm_inc_nr_ptes(struct mm_struct *mm) {} 3906 static inline void mm_dec_nr_ptes(struct mm_struct *mm) {} 3907 #endif 3908 3909 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd); 3910 int __pte_alloc_kernel(pmd_t *pmd); 3911 3912 #if defined(CONFIG_MMU) 3913 3914 static inline p4d_t *p4d_alloc(struct mm_struct *mm, pgd_t *pgd, 3915 unsigned long address) 3916 { 3917 return (unlikely(pgd_none(*pgd)) && __p4d_alloc(mm, pgd, address)) ? 3918 NULL : p4d_offset(pgd, address); 3919 } 3920 3921 static inline pud_t *pud_alloc(struct mm_struct *mm, p4d_t *p4d, 3922 unsigned long address) 3923 { 3924 return (unlikely(p4d_none(*p4d)) && __pud_alloc(mm, p4d, address)) ? 3925 NULL : pud_offset(p4d, address); 3926 } 3927 3928 static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 3929 { 3930 return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))? 3931 NULL: pmd_offset(pud, address); 3932 } 3933 #endif /* CONFIG_MMU */ 3934 3935 enum pt_flags { 3936 PT_kernel = PG_referenced, 3937 PT_reserved = PG_reserved, 3938 /* High bits are used for zone/node/section */ 3939 }; 3940 3941 static inline struct ptdesc *virt_to_ptdesc(const void *x) 3942 { 3943 return page_ptdesc(virt_to_page(x)); 3944 } 3945 3946 /** 3947 * ptdesc_address - Virtual address of page table. 3948 * @pt: Page table descriptor. 3949 * 3950 * Return: The first byte of the page table described by @pt. 3951 */ 3952 static inline void *ptdesc_address(const struct ptdesc *pt) 3953 { 3954 return folio_address(ptdesc_folio(pt)); 3955 } 3956 3957 static inline bool pagetable_is_reserved(struct ptdesc *pt) 3958 { 3959 return test_bit(PT_reserved, &pt->pt_flags.f); 3960 } 3961 3962 /** 3963 * ptdesc_set_kernel - Mark a ptdesc used to map the kernel 3964 * @ptdesc: The ptdesc to be marked 3965 * 3966 * Kernel page tables often need special handling. Set a flag so that 3967 * the handling code knows this ptdesc will not be used for userspace. 3968 */ 3969 static inline void ptdesc_set_kernel(struct ptdesc *ptdesc) 3970 { 3971 set_bit(PT_kernel, &ptdesc->pt_flags.f); 3972 } 3973 3974 /** 3975 * ptdesc_clear_kernel - Mark a ptdesc as no longer used to map the kernel 3976 * @ptdesc: The ptdesc to be unmarked 3977 * 3978 * Use when the ptdesc is no longer used to map the kernel and no longer 3979 * needs special handling. 3980 */ 3981 static inline void ptdesc_clear_kernel(struct ptdesc *ptdesc) 3982 { 3983 /* 3984 * Note: the 'PG_referenced' bit does not strictly need to be 3985 * cleared before freeing the page. But this is nice for 3986 * symmetry. 3987 */ 3988 clear_bit(PT_kernel, &ptdesc->pt_flags.f); 3989 } 3990 3991 /** 3992 * ptdesc_test_kernel - Check if a ptdesc is used to map the kernel 3993 * @ptdesc: The ptdesc being tested 3994 * 3995 * Call to tell if the ptdesc used to map the kernel. 3996 */ 3997 static inline bool ptdesc_test_kernel(const struct ptdesc *ptdesc) 3998 { 3999 return test_bit(PT_kernel, &ptdesc->pt_flags.f); 4000 } 4001 4002 /** 4003 * pagetable_alloc - Allocate pagetables 4004 * @gfp: GFP flags 4005 * @order: desired pagetable order 4006 * 4007 * pagetable_alloc allocates memory for page tables as well as a page table 4008 * descriptor to describe that memory. 4009 * 4010 * Return: The ptdesc describing the allocated page tables. 4011 */ 4012 static inline struct ptdesc *pagetable_alloc_noprof(gfp_t gfp, unsigned int order) 4013 { 4014 struct page *page = alloc_pages_noprof(gfp | __GFP_COMP, order); 4015 4016 return page_ptdesc(page); 4017 } 4018 #define pagetable_alloc(...) alloc_hooks(pagetable_alloc_noprof(__VA_ARGS__)) 4019 4020 static inline void __pagetable_free(struct ptdesc *pt) 4021 { 4022 struct page *page = ptdesc_page(pt); 4023 4024 __free_pages(page, compound_order(page)); 4025 } 4026 4027 #ifdef CONFIG_ASYNC_KERNEL_PGTABLE_FREE 4028 void pagetable_free_kernel(struct ptdesc *pt); 4029 #else 4030 static inline void pagetable_free_kernel(struct ptdesc *pt) 4031 { 4032 __pagetable_free(pt); 4033 } 4034 #endif 4035 #if defined(CONFIG_SPLIT_PTE_PTLOCKS) 4036 #if ALLOC_SPLIT_PTLOCKS 4037 void __init ptlock_cache_init(void); 4038 bool ptlock_alloc(struct ptdesc *ptdesc); 4039 void ptlock_free(struct ptdesc *ptdesc); 4040 4041 static inline spinlock_t *ptlock_ptr(struct ptdesc *ptdesc) 4042 { 4043 return ptdesc->ptl; 4044 } 4045 #else /* ALLOC_SPLIT_PTLOCKS */ 4046 static inline void ptlock_cache_init(void) 4047 { 4048 } 4049 4050 static inline bool ptlock_alloc(struct ptdesc *ptdesc) 4051 { 4052 return true; 4053 } 4054 4055 static inline void ptlock_free(struct ptdesc *ptdesc) 4056 { 4057 } 4058 4059 static inline spinlock_t *ptlock_ptr(struct ptdesc *ptdesc) 4060 { 4061 return &ptdesc->ptl; 4062 } 4063 #endif /* ALLOC_SPLIT_PTLOCKS */ 4064 4065 static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd) 4066 { 4067 return ptlock_ptr(page_ptdesc(pmd_page(*pmd))); 4068 } 4069 4070 static inline spinlock_t *ptep_lockptr(struct mm_struct *mm, pte_t *pte) 4071 { 4072 BUILD_BUG_ON(IS_ENABLED(CONFIG_HIGHPTE)); 4073 BUILD_BUG_ON(MAX_PTRS_PER_PTE * sizeof(pte_t) > PAGE_SIZE); 4074 return ptlock_ptr(virt_to_ptdesc(pte)); 4075 } 4076 4077 static inline bool ptlock_init(struct ptdesc *ptdesc) 4078 { 4079 /* 4080 * prep_new_page() initialize page->private (and therefore page->ptl) 4081 * with 0. Make sure nobody took it in use in between. 4082 * 4083 * It can happen if arch try to use slab for page table allocation: 4084 * slab code uses page->slab_cache, which share storage with page->ptl. 4085 */ 4086 VM_BUG_ON_PAGE(*(unsigned long *)&ptdesc->ptl, ptdesc_page(ptdesc)); 4087 if (!ptlock_alloc(ptdesc)) 4088 return false; 4089 spin_lock_init(ptlock_ptr(ptdesc)); 4090 return true; 4091 } 4092 4093 #else /* !defined(CONFIG_SPLIT_PTE_PTLOCKS) */ 4094 /* 4095 * We use mm->page_table_lock to guard all pagetable pages of the mm. 4096 */ 4097 static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd) 4098 { 4099 return &mm->page_table_lock; 4100 } 4101 static inline spinlock_t *ptep_lockptr(struct mm_struct *mm, pte_t *pte) 4102 { 4103 return &mm->page_table_lock; 4104 } 4105 static inline void ptlock_cache_init(void) {} 4106 static inline bool ptlock_init(struct ptdesc *ptdesc) { return true; } 4107 static inline void ptlock_free(struct ptdesc *ptdesc) {} 4108 #endif /* defined(CONFIG_SPLIT_PTE_PTLOCKS) */ 4109 4110 static inline void __pagetable_ctor(struct ptdesc *ptdesc) 4111 { 4112 struct folio *folio = ptdesc_folio(ptdesc); 4113 4114 __folio_set_pgtable(folio); 4115 lruvec_stat_add_folio(folio, NR_PAGETABLE); 4116 } 4117 4118 static inline void pagetable_dtor(struct ptdesc *ptdesc) 4119 { 4120 struct folio *folio = ptdesc_folio(ptdesc); 4121 4122 ptlock_free(ptdesc); 4123 __folio_clear_pgtable(folio); 4124 lruvec_stat_sub_folio(folio, NR_PAGETABLE); 4125 } 4126 4127 /** 4128 * pagetable_free - Free pagetables 4129 * @pt: The page table descriptor 4130 * 4131 * pagetable_free frees the memory of all page tables described by a page 4132 * table descriptor and the memory for the descriptor itself. 4133 */ 4134 static inline void pagetable_free(struct ptdesc *pt) 4135 { 4136 if (PageTable(ptdesc_page(pt))) 4137 pagetable_dtor(pt); 4138 4139 if (ptdesc_test_kernel(pt)) { 4140 ptdesc_clear_kernel(pt); 4141 pagetable_free_kernel(pt); 4142 } else { 4143 __pagetable_free(pt); 4144 } 4145 } 4146 4147 static inline void pagetable_dtor_free(struct ptdesc *ptdesc) 4148 { 4149 pagetable_dtor(ptdesc); 4150 pagetable_free(ptdesc); 4151 } 4152 4153 static inline bool pagetable_pte_ctor(struct mm_struct *mm, 4154 struct ptdesc *ptdesc) 4155 { 4156 if (mm != &init_mm && !ptlock_init(ptdesc)) 4157 return false; 4158 __pagetable_ctor(ptdesc); 4159 return true; 4160 } 4161 4162 pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp); 4163 4164 static inline pte_t *pte_offset_map(pmd_t *pmd, unsigned long addr) 4165 { 4166 return __pte_offset_map(pmd, addr, NULL); 4167 } 4168 4169 pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd, 4170 unsigned long addr, spinlock_t **ptlp); 4171 4172 pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd, 4173 unsigned long addr, spinlock_t **ptlp); 4174 pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd, 4175 unsigned long addr, pmd_t *pmdvalp, 4176 spinlock_t **ptlp); 4177 4178 #define pte_unmap_unlock(pte, ptl) do { \ 4179 spin_unlock(ptl); \ 4180 pte_unmap(pte); \ 4181 } while (0) 4182 4183 #define pte_alloc(mm, pmd) (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd)) 4184 4185 #define pte_alloc_map(mm, pmd, address) \ 4186 (pte_alloc(mm, pmd) ? NULL : pte_offset_map(pmd, address)) 4187 4188 #define pte_alloc_map_lock(mm, pmd, address, ptlp) \ 4189 (pte_alloc(mm, pmd) ? \ 4190 NULL : pte_offset_map_lock(mm, pmd, address, ptlp)) 4191 4192 #define pte_alloc_kernel(pmd, address) \ 4193 ((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd))? \ 4194 NULL: pte_offset_kernel(pmd, address)) 4195 4196 #if defined(CONFIG_SPLIT_PMD_PTLOCKS) 4197 4198 static inline struct page *pmd_pgtable_page(pmd_t *pmd) 4199 { 4200 unsigned long mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1); 4201 return virt_to_page((void *)((unsigned long) pmd & mask)); 4202 } 4203 4204 static inline struct ptdesc *pmd_ptdesc(pmd_t *pmd) 4205 { 4206 return page_ptdesc(pmd_pgtable_page(pmd)); 4207 } 4208 4209 static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd) 4210 { 4211 return ptlock_ptr(pmd_ptdesc(pmd)); 4212 } 4213 4214 static inline bool pmd_ptlock_init(struct ptdesc *ptdesc) 4215 { 4216 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4217 ptdesc->pmd_huge_pte = NULL; 4218 #endif 4219 return ptlock_init(ptdesc); 4220 } 4221 4222 #define pmd_huge_pte(mm, pmd) (pmd_ptdesc(pmd)->pmd_huge_pte) 4223 4224 #else 4225 4226 static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd) 4227 { 4228 return &mm->page_table_lock; 4229 } 4230 4231 static inline bool pmd_ptlock_init(struct ptdesc *ptdesc) { return true; } 4232 4233 #define pmd_huge_pte(mm, pmd) ((mm)->pmd_huge_pte) 4234 4235 #endif 4236 4237 static inline spinlock_t *pmd_lock(struct mm_struct *mm, pmd_t *pmd) 4238 { 4239 spinlock_t *ptl = pmd_lockptr(mm, pmd); 4240 spin_lock(ptl); 4241 return ptl; 4242 } 4243 4244 static inline bool pagetable_pmd_ctor(struct mm_struct *mm, 4245 struct ptdesc *ptdesc) 4246 { 4247 if (mm != &init_mm && !pmd_ptlock_init(ptdesc)) 4248 return false; 4249 ptdesc_pmd_pts_init(ptdesc); 4250 __pagetable_ctor(ptdesc); 4251 return true; 4252 } 4253 4254 /* 4255 * No scalability reason to split PUD locks yet, but follow the same pattern 4256 * as the PMD locks to make it easier if we decide to. The VM should not be 4257 * considered ready to switch to split PUD locks yet; there may be places 4258 * which need to be converted from page_table_lock. 4259 */ 4260 static inline spinlock_t *pud_lockptr(struct mm_struct *mm, pud_t *pud) 4261 { 4262 return &mm->page_table_lock; 4263 } 4264 4265 static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud) 4266 { 4267 spinlock_t *ptl = pud_lockptr(mm, pud); 4268 4269 spin_lock(ptl); 4270 return ptl; 4271 } 4272 4273 static inline void pagetable_pud_ctor(struct ptdesc *ptdesc) 4274 { 4275 __pagetable_ctor(ptdesc); 4276 } 4277 4278 static inline void pagetable_p4d_ctor(struct ptdesc *ptdesc) 4279 { 4280 __pagetable_ctor(ptdesc); 4281 } 4282 4283 static inline void pagetable_pgd_ctor(struct ptdesc *ptdesc) 4284 { 4285 __pagetable_ctor(ptdesc); 4286 } 4287 4288 extern void __init pagecache_init(void); 4289 extern void free_initmem(void); 4290 4291 /* 4292 * Free reserved pages within range [PAGE_ALIGN(start), end & PAGE_MASK) 4293 * into the buddy system. The freed pages will be poisoned with pattern 4294 * "poison" if it's within range [0, UCHAR_MAX]. 4295 * Return pages freed into the buddy system. 4296 */ 4297 extern unsigned long free_reserved_area(void *start, void *end, 4298 int poison, const char *s); 4299 4300 extern void adjust_managed_page_count(struct page *page, long count); 4301 4302 void free_reserved_pages(struct page *page, unsigned int order); 4303 4304 static inline void free_reserved_page(struct page *page) 4305 { 4306 free_reserved_pages(page, 0); 4307 } 4308 4309 static inline void free_reserved_ptdesc(struct ptdesc *pt) 4310 { 4311 free_reserved_page(ptdesc_page(pt)); 4312 } 4313 4314 /* 4315 * Default method to free all the __init memory into the buddy system. 4316 * The freed pages will be poisoned with pattern "poison" if it's within 4317 * range [0, UCHAR_MAX]. 4318 * Return pages freed into the buddy system. 4319 */ 4320 static inline unsigned long free_initmem_default(int poison) 4321 { 4322 extern char __init_begin[], __init_end[]; 4323 4324 return free_reserved_area(&__init_begin, &__init_end, 4325 poison, "unused kernel image (initmem)"); 4326 } 4327 4328 static inline unsigned long get_num_physpages(void) 4329 { 4330 int nid; 4331 unsigned long phys_pages = 0; 4332 4333 for_each_online_node(nid) 4334 phys_pages += node_present_pages(nid); 4335 4336 return phys_pages; 4337 } 4338 4339 /* 4340 * FIXME: Using memblock node mappings, an architecture may initialise its 4341 * zones, allocate the backing mem_map and account for memory holes in an 4342 * architecture independent manner. 4343 * 4344 * An architecture is expected to register range of page frames backed by 4345 * physical memory with memblock_add[_node]() before calling 4346 * free_area_init() passing in the PFN each zone ends at. At a basic 4347 * usage, an architecture is expected to do something like 4348 * 4349 * unsigned long max_zone_pfns[MAX_NR_ZONES] = {max_dma, max_normal_pfn, 4350 * max_highmem_pfn}; 4351 * for_each_valid_physical_page_range() 4352 * memblock_add_node(base, size, nid, MEMBLOCK_NONE) 4353 * free_area_init(max_zone_pfns); 4354 */ 4355 void arch_zone_limits_init(unsigned long *max_zone_pfn); 4356 unsigned long node_map_pfn_alignment(void); 4357 extern unsigned long absent_pages_in_range(unsigned long start_pfn, 4358 unsigned long end_pfn); 4359 extern void get_pfn_range_for_nid(unsigned int nid, 4360 unsigned long *start_pfn, unsigned long *end_pfn); 4361 4362 #ifndef CONFIG_NUMA 4363 static inline int early_pfn_to_nid(unsigned long pfn) 4364 { 4365 return 0; 4366 } 4367 #else 4368 /* please see mm/page_alloc.c */ 4369 extern int __meminit early_pfn_to_nid(unsigned long pfn); 4370 #endif 4371 4372 extern void mem_init(void); 4373 extern void __init mmap_init(void); 4374 4375 extern void __show_mem(unsigned int flags, const nodemask_t *nodemask, int max_zone_idx); 4376 static inline void show_mem(void) 4377 { 4378 __show_mem(0, NULL, MAX_NR_ZONES - 1); 4379 } 4380 extern long si_mem_available(void); 4381 extern void si_meminfo(struct sysinfo * val); 4382 extern void si_meminfo_node(struct sysinfo *val, int nid); 4383 4384 extern __printf(3, 4) 4385 void warn_alloc(gfp_t gfp_mask, const nodemask_t *nodemask, const char *fmt, ...); 4386 4387 extern void setup_per_cpu_pageset(void); 4388 4389 /* nommu.c */ 4390 extern atomic_long_t mmap_pages_allocated; 4391 extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t); 4392 4393 /* interval_tree.c */ 4394 void mapping_rmap_tree_insert(struct vm_area_struct *vma, 4395 struct address_space *mapping); 4396 void mapping_rmap_tree_insert_after(struct vm_area_struct *vma, 4397 struct vm_area_struct *prev, 4398 struct address_space *mapping); 4399 void mapping_rmap_tree_remove(struct vm_area_struct *vma, 4400 struct address_space *mapping); 4401 void mapping_rmap_tree_pre_update(struct vm_area_struct *vma, 4402 struct address_space *mapping, 4403 bool pgoff_unchanged); 4404 void mapping_rmap_tree_post_update(struct vm_area_struct *vma, 4405 struct address_space *mapping, 4406 bool pgoff_unchanged); 4407 struct vm_area_struct * 4408 mapping_rmap_tree_iter_first(struct address_space *mapping, 4409 pgoff_t pgoff_start, pgoff_t pgoff_last); 4410 struct vm_area_struct * 4411 mapping_rmap_tree_iter_next(struct vm_area_struct *vma, 4412 pgoff_t pgoff_start, pgoff_t pgoff_last); 4413 4414 #define mapping_rmap_tree_foreach(vma, mapping, pgoff_start, pgoff_last) \ 4415 for (vma = mapping_rmap_tree_iter_first(mapping, pgoff_start, \ 4416 pgoff_last); \ 4417 vma; vma = mapping_rmap_tree_iter_next(vma, pgoff_start, \ 4418 pgoff_last)) 4419 4420 void anon_rmap_tree_insert(struct anon_vma_chain *avc, 4421 struct anon_vma *anon_vma); 4422 void anon_rmap_tree_remove(struct anon_vma_chain *avc, 4423 struct anon_vma *anon_vma); 4424 void anon_rmap_tree_pre_update_vma(struct vm_area_struct *vma, 4425 bool anon_pgoff_unchanged); 4426 void anon_rmap_tree_post_update_vma(struct vm_area_struct *vma, 4427 bool anon_pgoff_unchanged); 4428 struct anon_vma_chain * 4429 anon_rmap_tree_iter_first(struct anon_vma *anon_vma, 4430 pgoff_t pgoff_start, pgoff_t pgoff_last); 4431 struct anon_vma_chain * 4432 anon_rmap_tree_iter_next(struct anon_vma_chain *avc, 4433 pgoff_t pgoff_start, pgoff_t pgoff_last); 4434 void anon_rmap_tree_verify(struct anon_vma_chain *avc); 4435 4436 #define anon_rmap_tree_foreach(avc, anon_vma, pgoff_start, pgoff_last) \ 4437 for (avc = anon_rmap_tree_iter_first(anon_vma, pgoff_start, pgoff_last); \ 4438 avc; avc = anon_rmap_tree_iter_next(avc, pgoff_start, pgoff_last)) 4439 4440 /* mmap.c */ 4441 extern int __vm_enough_memory(const struct mm_struct *mm, long pages, int cap_sys_admin); 4442 extern void exit_mmap(struct mm_struct *); 4443 bool mmap_read_lock_maybe_expand(struct mm_struct *mm, struct vm_area_struct *vma, 4444 unsigned long addr, bool write); 4445 4446 static inline int check_data_rlimit(unsigned long rlim, 4447 unsigned long new, 4448 unsigned long start, 4449 unsigned long end_data, 4450 unsigned long start_data) 4451 { 4452 if (rlim < RLIM_INFINITY) { 4453 if (((new - start) + (end_data - start_data)) > rlim) 4454 return -ENOSPC; 4455 } 4456 4457 return 0; 4458 } 4459 4460 extern int mm_take_all_locks(struct mm_struct *mm); 4461 extern void mm_drop_all_locks(struct mm_struct *mm); 4462 4463 extern int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file); 4464 extern int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file); 4465 extern struct file *get_mm_exe_file(struct mm_struct *mm); 4466 extern struct file *get_task_exe_file(struct task_struct *task); 4467 4468 extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages); 4469 4470 extern bool vma_is_special_mapping(const struct vm_area_struct *vma, 4471 const struct vm_special_mapping *sm); 4472 struct vm_area_struct *_install_special_mapping(struct mm_struct *mm, 4473 unsigned long addr, unsigned long len, 4474 vm_flags_t vm_flags, 4475 const struct vm_special_mapping *spec); 4476 4477 unsigned long randomize_stack_top(unsigned long stack_top); 4478 unsigned long randomize_page(unsigned long start, unsigned long range); 4479 4480 unsigned long 4481 __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 4482 unsigned long pgoff, unsigned long flags, 4483 vma_flags_t vma_flags); 4484 4485 static inline unsigned long 4486 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 4487 unsigned long pgoff, unsigned long flags) 4488 { 4489 return __get_unmapped_area(file, addr, len, pgoff, flags, 4490 EMPTY_VMA_FLAGS); 4491 } 4492 4493 unsigned long do_mmap(struct file *file, unsigned long addr, 4494 unsigned long len, unsigned long prot, unsigned long flags, 4495 vma_flags_t vma_flags, unsigned long pgoff, unsigned long *populate, 4496 struct list_head *uf); 4497 extern int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, 4498 unsigned long start, size_t len, struct list_head *uf, 4499 bool unlock); 4500 int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 4501 struct mm_struct *mm, unsigned long start, 4502 unsigned long end, struct list_head *uf, bool unlock); 4503 extern int do_munmap(struct mm_struct *, unsigned long, size_t, 4504 struct list_head *uf); 4505 extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior); 4506 4507 #ifdef CONFIG_MMU 4508 extern int __mm_populate(unsigned long addr, unsigned long len, 4509 int ignore_errors); 4510 static inline void mm_populate(unsigned long addr, unsigned long len) 4511 { 4512 /* Ignore errors */ 4513 (void) __mm_populate(addr, len, 1); 4514 } 4515 #else 4516 static inline void mm_populate(unsigned long addr, unsigned long len) {} 4517 #endif 4518 4519 /* This takes the mm semaphore itself */ 4520 int __must_check vm_brk_flags(unsigned long addr, unsigned long request, bool is_exec); 4521 int vm_munmap(unsigned long start, size_t len); 4522 unsigned long __must_check vm_mmap(struct file *file, unsigned long addr, 4523 unsigned long len, unsigned long prot, 4524 unsigned long flag, unsigned long offset); 4525 unsigned long __must_check vm_mmap_shadow_stack(unsigned long addr, 4526 unsigned long len, unsigned long flags); 4527 4528 struct vm_unmapped_area_info { 4529 #define VM_UNMAPPED_AREA_TOPDOWN 1 4530 unsigned long flags; 4531 unsigned long length; 4532 unsigned long low_limit; 4533 unsigned long high_limit; 4534 unsigned long align_mask; 4535 unsigned long align_offset; 4536 unsigned long start_gap; 4537 }; 4538 4539 extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info); 4540 4541 /* truncate.c */ 4542 void truncate_inode_pages(struct address_space *mapping, loff_t lstart); 4543 void truncate_inode_pages_range(struct address_space *mapping, loff_t lstart, 4544 uoff_t lend); 4545 void truncate_inode_pages_final(struct address_space *mapping); 4546 4547 /* generic vm_area_ops exported for stackable file systems */ 4548 extern vm_fault_t filemap_fault(struct vm_fault *vmf); 4549 extern vm_fault_t filemap_map_pages(struct vm_fault *vmf, 4550 pgoff_t start_pgoff, pgoff_t end_pgoff); 4551 extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf); 4552 4553 extern unsigned long stack_guard_gap; 4554 /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */ 4555 int expand_stack_locked(struct vm_area_struct *vma, unsigned long address); 4556 struct vm_area_struct *expand_stack(struct mm_struct * mm, unsigned long addr); 4557 4558 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ 4559 extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr); 4560 extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr, 4561 struct vm_area_struct **pprev); 4562 4563 /* 4564 * Look up the first VMA which intersects the interval [start_addr, end_addr) 4565 * NULL if none. Assume start_addr < end_addr. 4566 */ 4567 struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 4568 unsigned long start_addr, unsigned long end_addr); 4569 4570 /** 4571 * vma_lookup() - Find a VMA at a specific address 4572 * @mm: The process address space. 4573 * @addr: The user address. 4574 * 4575 * Return: The vm_area_struct at the given address, %NULL otherwise. 4576 */ 4577 static inline 4578 struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr) 4579 { 4580 return mtree_load(&mm->mm_mt, addr); 4581 } 4582 4583 static inline unsigned long stack_guard_start_gap(const struct vm_area_struct *vma) 4584 { 4585 if (vma->vm_flags & VM_GROWSDOWN) 4586 return stack_guard_gap; 4587 4588 /* See reasoning around the VM_SHADOW_STACK definition */ 4589 if (vma->vm_flags & VM_SHADOW_STACK) 4590 return PAGE_SIZE; 4591 4592 return 0; 4593 } 4594 4595 static inline unsigned long vm_start_gap(const struct vm_area_struct *vma) 4596 { 4597 unsigned long gap = stack_guard_start_gap(vma); 4598 unsigned long vm_start = vma->vm_start; 4599 4600 vm_start -= gap; 4601 if (vm_start > vma->vm_start) 4602 vm_start = 0; 4603 return vm_start; 4604 } 4605 4606 static inline unsigned long vm_end_gap(const struct vm_area_struct *vma) 4607 { 4608 unsigned long vm_end = vma->vm_end; 4609 4610 if (vma->vm_flags & VM_GROWSUP) { 4611 vm_end += stack_guard_gap; 4612 if (vm_end < vma->vm_end) 4613 vm_end = -PAGE_SIZE; 4614 } 4615 return vm_end; 4616 } 4617 4618 static inline unsigned long vma_pages(const struct vm_area_struct *vma) 4619 { 4620 return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 4621 } 4622 4623 /** 4624 * vma_start_pgoff() - Get the page offset of the start of @vma 4625 * @vma: The VMA whose page offset is required. 4626 * 4627 * If the VMA is file-backed, this is the page offset into the file. 4628 * 4629 * If @vma is anonymous, this is the virtual page offset of the start of the 4630 * VMA - if unfaulted, then vma->vm_start >> PAGE_SHIFT, if faulted then the 4631 * virtual page offset at the time of first fault. 4632 * 4633 * If @vma is a MAP_PRIVATE file-backed mapping, then this returns the 4634 * page offset within the file. 4635 * 4636 * Edge cases: nommu does not abide by these and CoW MAP_PRIVATE-pfnmap regions 4637 * have their page offset set to the first PFN in the range. 4638 * 4639 * Returns: The page offset of the start of @vma. 4640 */ 4641 static inline pgoff_t vma_start_pgoff(const struct vm_area_struct *vma) 4642 { 4643 return vma->vm_pgoff; 4644 } 4645 4646 /** 4647 * vma_end_pgoff() - Get the page offset of the exclusive end of @vma 4648 * @vma: The VMA whose end page offset is required. 4649 * 4650 * This returns the exclusive end page offset of @vma, which is useful for 4651 * expressing page offset ranges. 4652 * 4653 * See the description of vma_start_pgoff() for a description of VMA page 4654 * offsets. 4655 * 4656 * Returns: The exclusive end page offset of @vma. 4657 */ 4658 static inline pgoff_t vma_end_pgoff(const struct vm_area_struct *vma) 4659 { 4660 return vma_start_pgoff(vma) + vma_pages(vma); 4661 } 4662 4663 /** 4664 * vma_last_pgoff() - Get the page offset of the last page in @vma 4665 * @vma: The VMA whose last page offset is required. 4666 * 4667 * This returns the last page offset contained within @vma. 4668 * 4669 * See the description of vma_start_pgoff() for a description of VMA page 4670 * offsets. 4671 * 4672 * Returns: The last page offset of @vma. 4673 */ 4674 static inline pgoff_t vma_last_pgoff(const struct vm_area_struct *vma) 4675 { 4676 return vma_end_pgoff(vma) - 1; 4677 } 4678 4679 /** 4680 * vma_start_anon_pgoff() - Get the anonymous page offset of the start of @vma 4681 * @vma: The VMA whose anonymous page offset is required. 4682 * 4683 * If unfaulted, then this is vma->vm_start >> PAGE_SHIFT, if faulted then the 4684 * anonymous page offset at the time of first fault. 4685 * 4686 * If the VMA is anonymous, this returns the same value as vma_start_pgoff(). 4687 * 4688 * This value is used for tracking MAP_PRIVATE file-backed mappings by their 4689 * anonymous page offset. 4690 * 4691 * Returns: The anonymous page offset of the start of @vma. 4692 */ 4693 static inline pgoff_t vma_start_anon_pgoff(const struct vm_area_struct *vma) 4694 { 4695 pgoff_t pgoff = 0; 4696 4697 #ifdef CONFIG_64BIT 4698 pgoff += vma->__vm_anon_pgoff_hi; 4699 pgoff <<= 32; 4700 #endif 4701 pgoff += vma->__vm_anon_pgoff_lo; 4702 return pgoff; 4703 } 4704 4705 /** 4706 * vma_end_anon_pgoff() - Get the anonymous page offset of the exclusive end of 4707 * @vma. 4708 * @vma: The VMA whose end anonymous page offset is required. 4709 * 4710 * This returns the anonymous exclusive end page offset of @vma, which is useful 4711 * for expressing page offset ranges. 4712 * 4713 * See the description of vma_start_anon_pgoff() for a description of VMA 4714 * anonymous page offsets. 4715 * 4716 * Returns: The exclusive end anonymous page offset of @vma. 4717 */ 4718 static inline pgoff_t vma_end_anon_pgoff(const struct vm_area_struct *vma) 4719 { 4720 return vma_start_anon_pgoff(vma) + vma_pages(vma); 4721 } 4722 4723 /** 4724 * vma_last_anon_pgoff() - Get the anonymous page offset of the last page in 4725 * @vma. 4726 * @vma: The VMA whose last anonymous page offset is required. 4727 * 4728 * See the description of vma_start_anon_pgoff() for a description of VMA 4729 * anonymous page offsets. 4730 * 4731 * Returns: The last anonymous page offset of @vma. 4732 */ 4733 static inline pgoff_t vma_last_anon_pgoff(const struct vm_area_struct *vma) 4734 { 4735 return vma_end_anon_pgoff(vma) - 1; 4736 } 4737 4738 static inline unsigned long vma_desc_size(const struct vm_area_desc *desc) 4739 { 4740 return desc->end - desc->start; 4741 } 4742 4743 static inline unsigned long vma_desc_pages(const struct vm_area_desc *desc) 4744 { 4745 return vma_desc_size(desc) >> PAGE_SHIFT; 4746 } 4747 4748 /** 4749 * mmap_action_remap - helper for mmap_prepare hook to specify that a pure PFN 4750 * remap is required. 4751 * @desc: The VMA descriptor for the VMA requiring remap. 4752 * @start: The virtual address to start the remap from, must be within the VMA. 4753 * @start_pfn: The first PFN in the range to remap. 4754 * @size: The size of the range to remap, in bytes, at most spanning to the end 4755 * of the VMA. 4756 */ 4757 static inline void mmap_action_remap(struct vm_area_desc *desc, 4758 unsigned long start, 4759 unsigned long start_pfn, 4760 unsigned long size) 4761 { 4762 struct mmap_action *action = &desc->action; 4763 4764 /* [start, start + size) must be within the VMA. */ 4765 WARN_ON_ONCE(start < desc->start || start >= desc->end); 4766 WARN_ON_ONCE(start + size > desc->end); 4767 4768 action->type = MMAP_REMAP_PFN; 4769 action->remap.start = start; 4770 action->remap.start_pfn = start_pfn; 4771 action->remap.size = size; 4772 action->remap.pgprot = desc->page_prot; 4773 } 4774 4775 /** 4776 * mmap_action_remap_full - helper for mmap_prepare hook to specify that the 4777 * entirety of a VMA should be PFN remapped. 4778 * @desc: The VMA descriptor for the VMA requiring remap. 4779 * @start_pfn: The first PFN in the range to remap. 4780 */ 4781 static inline void mmap_action_remap_full(struct vm_area_desc *desc, 4782 unsigned long start_pfn) 4783 { 4784 mmap_action_remap(desc, desc->start, start_pfn, vma_desc_size(desc)); 4785 } 4786 4787 /** 4788 * mmap_action_ioremap - helper for mmap_prepare hook to specify that a pure PFN 4789 * I/O remap is required. 4790 * @desc: The VMA descriptor for the VMA requiring remap. 4791 * @start: The virtual address to start the remap from, must be within the VMA. 4792 * @start_pfn: The first PFN in the range to remap. 4793 * @size: The size of the range to remap, in bytes, at most spanning to the end 4794 * of the VMA. 4795 */ 4796 static inline void mmap_action_ioremap(struct vm_area_desc *desc, 4797 unsigned long start, 4798 unsigned long start_pfn, 4799 unsigned long size) 4800 { 4801 mmap_action_remap(desc, start, start_pfn, size); 4802 desc->action.type = MMAP_IO_REMAP_PFN; 4803 } 4804 4805 /** 4806 * mmap_action_ioremap_full - helper for mmap_prepare hook to specify that the 4807 * entirety of a VMA should be PFN I/O remapped. 4808 * @desc: The VMA descriptor for the VMA requiring remap. 4809 * @start_pfn: The first PFN in the range to remap. 4810 */ 4811 static inline void mmap_action_ioremap_full(struct vm_area_desc *desc, 4812 unsigned long start_pfn) 4813 { 4814 mmap_action_ioremap(desc, desc->start, start_pfn, vma_desc_size(desc)); 4815 } 4816 4817 /** 4818 * mmap_action_simple_ioremap - helper for mmap_prepare hook to specify that the 4819 * physical range in [start_phys_addr, start_phys_addr + size) should be I/O 4820 * remapped. 4821 * @desc: The VMA descriptor for the VMA requiring remap. 4822 * @start_phys_addr: Start of the physical memory to be mapped. 4823 * @size: Size of the area to map. 4824 * 4825 * NOTE: Some drivers might want to tweak desc->page_prot for purposes of 4826 * write-combine or similar. 4827 */ 4828 static inline void mmap_action_simple_ioremap(struct vm_area_desc *desc, 4829 phys_addr_t start_phys_addr, 4830 unsigned long size) 4831 { 4832 struct mmap_action *action = &desc->action; 4833 4834 action->simple_ioremap.start_phys_addr = start_phys_addr; 4835 action->simple_ioremap.size = size; 4836 action->type = MMAP_SIMPLE_IO_REMAP; 4837 } 4838 4839 /** 4840 * mmap_action_map_kernel_pages - helper for mmap_prepare hook to specify that 4841 * @num kernel pages contained in the @pages array should be mapped to userland 4842 * starting at virtual address @start. 4843 * @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. 4844 * @start: The virtual address from which to map them. 4845 * @pages: An array of struct page pointers describing the memory to map. 4846 * @nr_pages: The number of entries in the @pages array. 4847 */ 4848 static inline void mmap_action_map_kernel_pages(struct vm_area_desc *desc, 4849 unsigned long start, struct page **pages, 4850 unsigned long nr_pages) 4851 { 4852 struct mmap_action *action = &desc->action; 4853 4854 action->type = MMAP_KERNEL_PAGES; 4855 action->map_kernel.start = start; 4856 action->map_kernel.pages = pages; 4857 action->map_kernel.nr_pages = nr_pages; 4858 action->map_kernel.pgoff = desc->pgoff; 4859 } 4860 4861 /** 4862 * mmap_action_map_kernel_pages_full - helper for mmap_prepare hook to specify that 4863 * kernel pages contained in the @pages array should be mapped to userland 4864 * from @desc->start to @desc->end. 4865 * @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. 4866 * @pages: An array of struct page pointers describing the memory to map. 4867 * 4868 * The caller must ensure that @pages contains sufficient entries to cover the 4869 * entire range described by @desc. 4870 */ 4871 static inline void mmap_action_map_kernel_pages_full(struct vm_area_desc *desc, 4872 struct page **pages) 4873 { 4874 mmap_action_map_kernel_pages(desc, desc->start, pages, 4875 vma_desc_pages(desc)); 4876 } 4877 4878 static inline 4879 void mmap_action_map_discontig_kernel_pages(struct vm_area_desc *desc, 4880 void *init_private, const struct discontig_kernel_page_ops *ops) 4881 { 4882 struct mmap_action *action = &desc->action; 4883 4884 action->type = MMAP_DISCONTIG_KERNEL_PAGES; 4885 action->map_kernel_discontig.init_private = init_private; 4886 action->map_kernel_discontig.ops = ops; 4887 } 4888 4889 int mmap_action_prepare(struct vm_area_desc *desc); 4890 int mmap_action_complete(struct vm_area_struct *vma, 4891 struct mmap_action *action, bool is_compat); 4892 4893 static inline void 4894 discontig_kernel_map_abort(struct discontig_kernel_page_state *state) 4895 { 4896 state->action = DISCONTIG_KERNEL_PAGE_ABORT; 4897 } 4898 4899 static inline void 4900 discontig_kernel_map_page(struct discontig_kernel_page_state *state, 4901 struct page *page) 4902 { 4903 struct folio *folio = page_folio(page); 4904 unsigned long nr_pages = 1; 4905 4906 if (folio_test_large(folio)) { 4907 VM_WARN_ON_ONCE(page != folio_page(folio, 0)); 4908 nr_pages = folio_nr_pages(folio); 4909 } 4910 4911 state->action = DISCONTIG_KERNEL_PAGE_MAP_CONTIG_PAGES; 4912 state->__page = page; 4913 state->__nr_pages = min(nr_pages, state->nr_pages_remain); 4914 } 4915 4916 static inline void 4917 discontig_kernel_map_pages(struct discontig_kernel_page_state *state, 4918 struct page **page_arr, unsigned long nr_pages) 4919 { 4920 state->action = DISCONTIG_KERNEL_PAGE_MAP_PAGES; 4921 state->__page_arr = page_arr; 4922 state->__nr_pages = min(nr_pages, state->nr_pages_remain); 4923 } 4924 4925 /* Look up the first VMA which exactly match the interval vm_start ... vm_end */ 4926 static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm, 4927 unsigned long vm_start, unsigned long vm_end) 4928 { 4929 struct vm_area_struct *vma = vma_lookup(mm, vm_start); 4930 4931 if (vma && (vma->vm_start != vm_start || vma->vm_end != vm_end)) 4932 vma = NULL; 4933 4934 return vma; 4935 } 4936 4937 /** 4938 * range_is_subset - Is the specified inner range a subset of the outer range? 4939 * @outer_start: The start of the outer range. 4940 * @outer_end: The exclusive end of the outer range. 4941 * @inner_start: The start of the inner range. 4942 * @inner_end: The exclusive end of the inner range. 4943 * 4944 * Returns: %true if [inner_start, inner_end) is a subset of [outer_start, 4945 * outer_end), otherwise %false. 4946 */ 4947 static inline bool range_is_subset(unsigned long outer_start, 4948 unsigned long outer_end, 4949 unsigned long inner_start, 4950 unsigned long inner_end) 4951 { 4952 return outer_start <= inner_start && inner_end <= outer_end; 4953 } 4954 4955 /** 4956 * range_in_vma - is the specified [@start, @end) range a subset of the VMA? 4957 * @vma: The VMA against which we want to check [@start, @end). 4958 * @start: The start of the range we wish to check. 4959 * @end: The exclusive end of the range we wish to check. 4960 * 4961 * Returns: %true if [@start, @end) is a subset of [@vma->vm_start, 4962 * @vma->vm_end), %false otherwise. 4963 */ 4964 static inline bool range_in_vma(const struct vm_area_struct *vma, 4965 unsigned long start, unsigned long end) 4966 { 4967 if (!vma) 4968 return false; 4969 4970 return range_is_subset(vma->vm_start, vma->vm_end, start, end); 4971 } 4972 4973 /** 4974 * range_in_vma_desc - is the specified [@start, @end) range a subset of the VMA 4975 * described by @desc, a VMA descriptor? 4976 * @desc: The VMA descriptor against which we want to check [@start, @end). 4977 * @start: The start of the range we wish to check. 4978 * @end: The exclusive end of the range we wish to check. 4979 * 4980 * Returns: %true if [@start, @end) is a subset of [@desc->start, @desc->end), 4981 * %false otherwise. 4982 */ 4983 static inline bool range_in_vma_desc(const struct vm_area_desc *desc, 4984 unsigned long start, unsigned long end) 4985 { 4986 if (!desc) 4987 return false; 4988 4989 return range_is_subset(desc->start, desc->end, start, end); 4990 } 4991 4992 #ifdef CONFIG_MMU 4993 pgprot_t vm_get_page_prot(vm_flags_t vm_flags); 4994 4995 static inline pgprot_t vma_flags_to_page_prot(vma_flags_t vma_flags) 4996 { 4997 const vm_flags_t vm_flags = vma_flags_to_legacy(vma_flags); 4998 4999 return vm_get_page_prot(vm_flags); 5000 } 5001 5002 static inline pgprot_t vma_get_page_prot(const struct vm_area_struct *vma) 5003 { 5004 return vma_flags_to_page_prot(vma->flags); 5005 } 5006 5007 void vma_set_page_prot(struct vm_area_struct *vma); 5008 #else 5009 static inline pgprot_t vm_get_page_prot(vm_flags_t vm_flags) 5010 { 5011 return __pgprot(0); 5012 } 5013 static inline pgprot_t vma_flags_to_page_prot(vma_flags_t vma_flags) 5014 { 5015 return __pgprot(0); 5016 } 5017 static inline pgprot_t vma_get_page_prot(const struct vm_area_struct *vma) 5018 { 5019 return __pgprot(0); 5020 } 5021 static inline void vma_set_page_prot(struct vm_area_struct *vma) 5022 { 5023 vma->vm_page_prot = vma_get_page_prot(vma); 5024 } 5025 #endif 5026 5027 void vma_set_file(struct vm_area_struct *vma, struct file *file); 5028 5029 #ifdef CONFIG_NUMA_BALANCING 5030 unsigned long change_prot_numa(struct vm_area_struct *vma, 5031 unsigned long start, unsigned long end); 5032 #endif 5033 5034 struct vm_area_struct *find_extend_vma_locked(struct mm_struct *, 5035 unsigned long addr); 5036 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 5037 unsigned long pfn, unsigned long size, pgprot_t pgprot); 5038 5039 int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *); 5040 int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, 5041 struct page **pages, unsigned long *num); 5042 int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 5043 unsigned long num); 5044 int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 5045 unsigned long num); 5046 vm_fault_t vmf_insert_page_mkwrite(struct vm_fault *vmf, struct page *page, 5047 bool write); 5048 vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 5049 unsigned long pfn); 5050 vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 5051 unsigned long pfn, pgprot_t pgprot); 5052 vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 5053 unsigned long pfn); 5054 vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 5055 unsigned long addr, unsigned long pfn); 5056 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len); 5057 5058 static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma, 5059 unsigned long addr, struct page *page) 5060 { 5061 int err = vm_insert_page(vma, addr, page); 5062 5063 if (err == -ENOMEM) 5064 return VM_FAULT_OOM; 5065 if (err < 0 && err != -EBUSY) 5066 return VM_FAULT_SIGBUS; 5067 5068 return VM_FAULT_NOPAGE; 5069 } 5070 5071 #ifndef io_remap_pfn_range_pfn 5072 static inline unsigned long io_remap_pfn_range_pfn(unsigned long pfn, 5073 unsigned long size) 5074 { 5075 return pfn; 5076 } 5077 #endif 5078 5079 static inline int io_remap_pfn_range(struct vm_area_struct *vma, 5080 unsigned long addr, unsigned long orig_pfn, 5081 unsigned long size, pgprot_t orig_prot) 5082 { 5083 const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size); 5084 const pgprot_t prot = pgprot_decrypted(orig_prot); 5085 5086 return remap_pfn_range(vma, addr, pfn, size, prot); 5087 } 5088 5089 static inline vm_fault_t vmf_error(int err) 5090 { 5091 if (err == -ENOMEM) 5092 return VM_FAULT_OOM; 5093 else if (err == -EHWPOISON) 5094 return VM_FAULT_HWPOISON; 5095 return VM_FAULT_SIGBUS; 5096 } 5097 5098 /* 5099 * Convert errno to return value for ->page_mkwrite() calls. 5100 * 5101 * This should eventually be merged with vmf_error() above, but will need a 5102 * careful audit of all vmf_error() callers. 5103 */ 5104 static inline vm_fault_t vmf_fs_error(int err) 5105 { 5106 if (err == 0) 5107 return VM_FAULT_LOCKED; 5108 if (err == -EFAULT || err == -EAGAIN) 5109 return VM_FAULT_NOPAGE; 5110 if (err == -ENOMEM) 5111 return VM_FAULT_OOM; 5112 /* -ENOSPC, -EDQUOT, -EIO ... */ 5113 return VM_FAULT_SIGBUS; 5114 } 5115 5116 static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags) 5117 { 5118 if (vm_fault & VM_FAULT_OOM) 5119 return -ENOMEM; 5120 if (vm_fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) 5121 return (foll_flags & FOLL_HWPOISON) ? -EHWPOISON : -EFAULT; 5122 if (vm_fault & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) 5123 return -EFAULT; 5124 return 0; 5125 } 5126 5127 /* 5128 * Indicates whether GUP can follow a PROT_NONE mapped page, or whether 5129 * a (NUMA hinting or userfaultfd RWP) fault is required. 5130 */ 5131 static inline bool gup_can_follow_protnone(const struct vm_area_struct *vma, 5132 unsigned int flags) 5133 { 5134 /* 5135 * VM_UFFD_RWP uses protnone as an access-tracking marker, not for 5136 * NUMA hinting. GUP must always take a fault so the access is 5137 * delivered to userfaultfd, regardless of FOLL_HONOR_NUMA_FAULT. 5138 * 5139 * Only do so while the VMA is accessible. If it has been made 5140 * inaccessible (e.g. mprotect(PROT_NONE)), fall through to the guard 5141 * below: forcing a fault there would loop, as handle_mm_fault() makes 5142 * no progress on protnone in an inaccessible VMA, and the access is 5143 * denied regardless of RWP anyway. 5144 */ 5145 if (vma_test_single_mask(vma, VMA_UFFD_RWP) && vma_is_accessible(vma)) 5146 return false; 5147 5148 /* 5149 * If callers don't want to honor NUMA hinting faults, no need to 5150 * determine if we would actually have to trigger a NUMA hinting fault. 5151 */ 5152 if (!(flags & FOLL_HONOR_NUMA_FAULT)) 5153 return true; 5154 5155 /* 5156 * NUMA hinting faults don't apply in inaccessible (PROT_NONE) VMAs. 5157 * 5158 * Requiring a fault here even for inaccessible VMAs would mean that 5159 * FOLL_FORCE cannot make any progress, because handle_mm_fault() 5160 * refuses to process NUMA hinting faults in inaccessible VMAs. 5161 */ 5162 return !vma_is_accessible(vma); 5163 } 5164 5165 typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data); 5166 extern int apply_to_page_range(struct mm_struct *mm, unsigned long address, 5167 unsigned long size, pte_fn_t fn, void *data); 5168 extern int apply_to_existing_page_range(struct mm_struct *mm, 5169 unsigned long address, unsigned long size, 5170 pte_fn_t fn, void *data); 5171 5172 #ifdef CONFIG_PAGE_POISONING 5173 extern void __kernel_poison_pages(struct page *page, int numpages); 5174 extern void __kernel_unpoison_pages(struct page *page, int numpages); 5175 extern bool _page_poisoning_enabled_early; 5176 DECLARE_STATIC_KEY_FALSE(_page_poisoning_enabled); 5177 static inline bool page_poisoning_enabled(void) 5178 { 5179 return _page_poisoning_enabled_early; 5180 } 5181 /* 5182 * For use in fast paths after init_mem_debugging() has run, or when a 5183 * false negative result is not harmful when called too early. 5184 */ 5185 static inline bool page_poisoning_enabled_static(void) 5186 { 5187 return static_branch_unlikely(&_page_poisoning_enabled); 5188 } 5189 static inline void kernel_poison_pages(struct page *page, int numpages) 5190 { 5191 if (page_poisoning_enabled_static()) 5192 __kernel_poison_pages(page, numpages); 5193 } 5194 static inline void kernel_unpoison_pages(struct page *page, int numpages) 5195 { 5196 if (page_poisoning_enabled_static()) 5197 __kernel_unpoison_pages(page, numpages); 5198 } 5199 #else 5200 static inline bool page_poisoning_enabled(void) { return false; } 5201 static inline bool page_poisoning_enabled_static(void) { return false; } 5202 static inline void __kernel_poison_pages(struct page *page, int nunmpages) { } 5203 static inline void kernel_poison_pages(struct page *page, int numpages) { } 5204 static inline void kernel_unpoison_pages(struct page *page, int numpages) { } 5205 #endif 5206 5207 DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc); 5208 static inline bool want_init_on_alloc(gfp_t flags) 5209 { 5210 if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, 5211 &init_on_alloc)) 5212 return true; 5213 return flags & __GFP_ZERO; 5214 } 5215 5216 DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free); 5217 static inline bool want_init_on_free(void) 5218 { 5219 return static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON, 5220 &init_on_free); 5221 } 5222 5223 extern bool _debug_pagealloc_enabled_early; 5224 DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled); 5225 5226 static inline bool debug_pagealloc_enabled(void) 5227 { 5228 return IS_ENABLED(CONFIG_DEBUG_PAGEALLOC) && 5229 _debug_pagealloc_enabled_early; 5230 } 5231 5232 /* 5233 * For use in fast paths after mem_debugging_and_hardening_init() has run, 5234 * or when a false negative result is not harmful when called too early. 5235 */ 5236 static inline bool debug_pagealloc_enabled_static(void) 5237 { 5238 if (!IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) 5239 return false; 5240 5241 return static_branch_unlikely(&_debug_pagealloc_enabled); 5242 } 5243 5244 /* 5245 * To support DEBUG_PAGEALLOC architecture must ensure that 5246 * __kernel_map_pages() never fails 5247 */ 5248 extern void __kernel_map_pages(struct page *page, int numpages, int enable); 5249 #ifdef CONFIG_DEBUG_PAGEALLOC 5250 static inline void debug_pagealloc_map_pages(struct page *page, int numpages) 5251 { 5252 iommu_debug_check_unmapped(page, numpages); 5253 5254 if (debug_pagealloc_enabled_static()) 5255 __kernel_map_pages(page, numpages, 1); 5256 } 5257 5258 static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages) 5259 { 5260 iommu_debug_check_unmapped(page, numpages); 5261 5262 if (debug_pagealloc_enabled_static()) 5263 __kernel_map_pages(page, numpages, 0); 5264 } 5265 5266 extern unsigned int _debug_guardpage_minorder; 5267 DECLARE_STATIC_KEY_FALSE(_debug_guardpage_enabled); 5268 5269 static inline unsigned int debug_guardpage_minorder(void) 5270 { 5271 return _debug_guardpage_minorder; 5272 } 5273 5274 static inline bool debug_guardpage_enabled(void) 5275 { 5276 return static_branch_unlikely(&_debug_guardpage_enabled); 5277 } 5278 5279 static inline bool page_is_guard(const struct page *page) 5280 { 5281 if (!debug_guardpage_enabled()) 5282 return false; 5283 5284 return PageGuard(page); 5285 } 5286 5287 bool __set_page_guard(struct zone *zone, struct page *page, unsigned int order); 5288 static inline bool set_page_guard(struct zone *zone, struct page *page, 5289 unsigned int order) 5290 { 5291 if (!debug_guardpage_enabled()) 5292 return false; 5293 return __set_page_guard(zone, page, order); 5294 } 5295 5296 void __clear_page_guard(struct zone *zone, struct page *page, unsigned int order); 5297 static inline void clear_page_guard(struct zone *zone, struct page *page, 5298 unsigned int order) 5299 { 5300 if (!debug_guardpage_enabled()) 5301 return; 5302 __clear_page_guard(zone, page, order); 5303 } 5304 5305 #else /* CONFIG_DEBUG_PAGEALLOC */ 5306 static inline void debug_pagealloc_map_pages(struct page *page, int numpages) {} 5307 static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages) {} 5308 static inline unsigned int debug_guardpage_minorder(void) { return 0; } 5309 static inline bool debug_guardpage_enabled(void) { return false; } 5310 static inline bool page_is_guard(const struct page *page) { return false; } 5311 static inline bool set_page_guard(struct zone *zone, struct page *page, 5312 unsigned int order) { return false; } 5313 static inline void clear_page_guard(struct zone *zone, struct page *page, 5314 unsigned int order) {} 5315 #endif /* CONFIG_DEBUG_PAGEALLOC */ 5316 5317 #ifndef clear_pages 5318 /** 5319 * clear_pages() - clear a page range for kernel-internal use. 5320 * @addr: start address 5321 * @npages: number of pages 5322 * 5323 * Use clear_user_pages() instead when clearing a page range to be 5324 * mapped to user space. 5325 * 5326 * Does absolutely no exception handling. 5327 * 5328 * Note that even though the clearing operation is preemptible, clear_pages() 5329 * does not (and on architectures where it reduces to a few long-running 5330 * instructions, might not be able to) call cond_resched() to check if 5331 * rescheduling is required. 5332 * 5333 * When running under preemptible models this is not a problem. Under 5334 * cooperatively scheduled models, however, the caller is expected to 5335 * limit @npages to no more than PROCESS_PAGES_NON_PREEMPT_BATCH. 5336 */ 5337 static inline void clear_pages(void *addr, unsigned int npages) 5338 { 5339 do { 5340 clear_page(addr); 5341 addr += PAGE_SIZE; 5342 } while (--npages); 5343 } 5344 #endif 5345 5346 #ifndef PROCESS_PAGES_NON_PREEMPT_BATCH 5347 #ifdef clear_pages 5348 /* 5349 * The architecture defines clear_pages(), and we assume that it is 5350 * generally "fast". So choose a batch size large enough to allow the processor 5351 * headroom for optimizing the operation and yet small enough that we see 5352 * reasonable preemption latency for when this optimization is not possible 5353 * (ex. slow microarchitectures, memory bandwidth saturation.) 5354 * 5355 * With a value of 32MB and assuming a memory bandwidth of ~10GBps, this should 5356 * result in worst case preemption latency of around 3ms when clearing pages. 5357 * 5358 * (See comment above clear_pages() for why preemption latency is a concern 5359 * here.) 5360 */ 5361 #define PROCESS_PAGES_NON_PREEMPT_BATCH (SZ_32M >> PAGE_SHIFT) 5362 #else /* !clear_pages */ 5363 /* 5364 * The architecture does not provide a clear_pages() implementation. Assume 5365 * that clear_page() -- which clear_pages() will fallback to -- is relatively 5366 * slow and choose a small value for PROCESS_PAGES_NON_PREEMPT_BATCH. 5367 */ 5368 #define PROCESS_PAGES_NON_PREEMPT_BATCH 1 5369 #endif 5370 #endif 5371 5372 #ifdef __HAVE_ARCH_GATE_AREA 5373 extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm); 5374 extern int in_gate_area_no_mm(unsigned long addr); 5375 extern int in_gate_area(struct mm_struct *mm, unsigned long addr); 5376 #else 5377 static inline struct vm_area_struct *get_gate_vma(struct mm_struct *mm) 5378 { 5379 return NULL; 5380 } 5381 static inline int in_gate_area_no_mm(unsigned long addr) { return 0; } 5382 static inline int in_gate_area(struct mm_struct *mm, unsigned long addr) 5383 { 5384 return 0; 5385 } 5386 #endif /* __HAVE_ARCH_GATE_AREA */ 5387 5388 bool process_shares_mm(const struct task_struct *p, const struct mm_struct *mm); 5389 5390 void drop_slab(void); 5391 5392 #ifndef CONFIG_MMU 5393 #define randomize_va_space 0 5394 #else 5395 extern int randomize_va_space; 5396 #endif 5397 5398 const char * arch_vma_name(struct vm_area_struct *vma); 5399 #ifdef CONFIG_MMU 5400 void print_vma_addr(char *prefix, unsigned long rip); 5401 #else 5402 static inline void print_vma_addr(char *prefix, unsigned long rip) 5403 { 5404 } 5405 #endif 5406 5407 struct page * __populate_section_memmap(unsigned long pfn, 5408 unsigned long nr_pages, int nid, struct vmem_altmap *altmap, 5409 struct dev_pagemap *pgmap); 5410 void *vmemmap_alloc_block(unsigned long size, int node); 5411 struct vmem_altmap; 5412 void *vmemmap_alloc_block_buf(unsigned long size, int node, 5413 struct vmem_altmap *altmap); 5414 void vmemmap_verify(pte_t *, int, unsigned long, unsigned long); 5415 void vmemmap_set_pmd(pmd_t *pmd, void *p, int node, 5416 unsigned long addr, unsigned long next); 5417 int vmemmap_check_pmd(pmd_t *pmd, int node, 5418 unsigned long addr, unsigned long next); 5419 int vmemmap_populate_basepages(unsigned long start, unsigned long end, 5420 int node, struct vmem_altmap *altmap); 5421 int vmemmap_populate_hugepages(unsigned long start, unsigned long end, 5422 int node, struct vmem_altmap *altmap); 5423 int vmemmap_populate(unsigned long start, unsigned long end, int node, 5424 struct vmem_altmap *altmap); 5425 void vmemmap_wrprotect_hvo(unsigned long start, unsigned long end, int node, 5426 unsigned long headsize); 5427 void vmemmap_populate_print_last(void); 5428 #ifdef CONFIG_MEMORY_HOTPLUG 5429 void vmemmap_free(unsigned long start, unsigned long end, 5430 struct vmem_altmap *altmap); 5431 #endif 5432 5433 #ifdef CONFIG_SPARSEMEM_VMEMMAP 5434 static inline unsigned long vmem_altmap_offset(const struct vmem_altmap *altmap) 5435 { 5436 /* number of pfns from base where pfn_to_page() is valid */ 5437 if (altmap) 5438 return altmap->reserve + altmap->free; 5439 return 0; 5440 } 5441 5442 static inline void vmem_altmap_free(struct vmem_altmap *altmap, 5443 unsigned long nr_pfns) 5444 { 5445 altmap->alloc -= nr_pfns; 5446 } 5447 #else 5448 static inline unsigned long vmem_altmap_offset(const struct vmem_altmap *altmap) 5449 { 5450 return 0; 5451 } 5452 5453 static inline void vmem_altmap_free(struct vmem_altmap *altmap, 5454 unsigned long nr_pfns) 5455 { 5456 } 5457 #endif 5458 5459 #ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP 5460 static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap, 5461 struct dev_pagemap *pgmap) 5462 { 5463 unsigned long nr_pages; 5464 unsigned long nr_vmemmap_pages; 5465 5466 if (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION)) 5467 return false; 5468 5469 if (!pgmap || !is_power_of_2(sizeof(struct page))) 5470 return false; 5471 5472 nr_pages = pgmap_vmemmap_nr(pgmap); 5473 nr_vmemmap_pages = ((nr_pages * sizeof(struct page)) >> PAGE_SHIFT); 5474 /* 5475 * For vmemmap optimization with DAX we need minimum 2 vmemmap 5476 * pages. See layout diagram in Documentation/mm/vmemmap_dedup.rst 5477 */ 5478 return !altmap && (nr_vmemmap_pages > VMEMMAP_OPTIMIZATION_PAGES); 5479 } 5480 /* 5481 * If we don't have an architecture override, use the generic rule 5482 */ 5483 #ifndef vmemmap_can_optimize 5484 #define vmemmap_can_optimize __vmemmap_can_optimize 5485 #endif 5486 5487 #else 5488 static inline bool vmemmap_can_optimize(struct vmem_altmap *altmap, 5489 struct dev_pagemap *pgmap) 5490 { 5491 return false; 5492 } 5493 #endif 5494 5495 enum mf_flags { 5496 MF_COUNT_INCREASED = 1 << 0, 5497 MF_ACTION_REQUIRED = 1 << 1, 5498 MF_MUST_KILL = 1 << 2, 5499 MF_SOFT_OFFLINE = 1 << 3, 5500 MF_UNPOISON = 1 << 4, 5501 MF_SW_SIMULATED = 1 << 5, 5502 MF_NO_RETRY = 1 << 6, 5503 MF_MEM_PRE_REMOVE = 1 << 7, 5504 }; 5505 int mf_dax_kill_procs(struct address_space *mapping, pgoff_t index, 5506 unsigned long count, int mf_flags); 5507 extern int memory_failure(unsigned long pfn, int flags); 5508 extern int unpoison_memory(unsigned long pfn); 5509 extern atomic_long_t num_poisoned_pages __read_mostly; 5510 extern int soft_offline_page(unsigned long pfn, int flags); 5511 #ifdef CONFIG_MEMORY_FAILURE 5512 /* 5513 * Sysfs entries for memory failure handling statistics. 5514 */ 5515 extern const struct attribute_group memory_failure_attr_group; 5516 extern void memory_failure_queue(unsigned long pfn, int flags); 5517 void num_poisoned_pages_inc(unsigned long pfn); 5518 void num_poisoned_pages_sub(unsigned long pfn, long i); 5519 phys_addr_t range_first_hwpoison(phys_addr_t start, unsigned long size); 5520 phys_addr_t range_last_hwpoison(phys_addr_t start, unsigned long size); 5521 #else 5522 static inline void memory_failure_queue(unsigned long pfn, int flags) 5523 { 5524 } 5525 5526 static inline void num_poisoned_pages_inc(unsigned long pfn) 5527 { 5528 } 5529 5530 static inline void num_poisoned_pages_sub(unsigned long pfn, long i) 5531 { 5532 } 5533 5534 static inline phys_addr_t range_first_hwpoison(phys_addr_t start, 5535 unsigned long size) 5536 { 5537 return PHYS_ADDR_MAX; 5538 } 5539 5540 static inline phys_addr_t range_last_hwpoison(phys_addr_t start, 5541 unsigned long size) 5542 { 5543 return PHYS_ADDR_MAX; 5544 } 5545 #endif 5546 5547 #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG) 5548 extern void memblk_nr_poison_inc(unsigned long pfn); 5549 extern void memblk_nr_poison_sub(unsigned long pfn, long i); 5550 #else 5551 static inline void memblk_nr_poison_inc(unsigned long pfn) 5552 { 5553 } 5554 5555 static inline void memblk_nr_poison_sub(unsigned long pfn, long i) 5556 { 5557 } 5558 #endif 5559 5560 #ifndef arch_memory_failure 5561 static inline int arch_memory_failure(unsigned long pfn, int flags) 5562 { 5563 return -ENXIO; 5564 } 5565 #endif 5566 5567 #ifndef arch_is_platform_page 5568 static inline bool arch_is_platform_page(u64 paddr) 5569 { 5570 return false; 5571 } 5572 #endif 5573 5574 /* 5575 * Error handlers for various types of pages. 5576 */ 5577 enum mf_result { 5578 MF_IGNORED, /* Error: cannot be handled */ 5579 MF_FAILED, /* Error: handling failed */ 5580 MF_DELAYED, /* Will be handled later */ 5581 MF_RECOVERED, /* Successfully recovered */ 5582 }; 5583 5584 enum mf_action_page_type { 5585 MF_MSG_KERNEL, 5586 MF_MSG_KERNEL_HIGH_ORDER, 5587 MF_MSG_DIFFERENT_COMPOUND, 5588 MF_MSG_HUGE, 5589 MF_MSG_FREE_HUGE, 5590 MF_MSG_GET_HWPOISON, 5591 MF_MSG_UNMAP_FAILED, 5592 MF_MSG_DIRTY_SWAPCACHE, 5593 MF_MSG_CLEAN_SWAPCACHE, 5594 MF_MSG_DIRTY_MLOCKED_LRU, 5595 MF_MSG_CLEAN_MLOCKED_LRU, 5596 MF_MSG_DIRTY_UNEVICTABLE_LRU, 5597 MF_MSG_CLEAN_UNEVICTABLE_LRU, 5598 MF_MSG_DIRTY_LRU, 5599 MF_MSG_CLEAN_LRU, 5600 MF_MSG_TRUNCATED_LRU, 5601 MF_MSG_BUDDY, 5602 MF_MSG_DAX, 5603 MF_MSG_UNSPLIT_THP, 5604 MF_MSG_ALREADY_POISONED, 5605 MF_MSG_PFN_MAP, 5606 MF_MSG_UNKNOWN, 5607 }; 5608 5609 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 5610 void folio_zero_user(struct folio *folio, unsigned long addr_hint); 5611 int copy_user_large_folio(struct folio *dst, struct folio *src, 5612 unsigned long addr_hint, 5613 struct vm_area_struct *vma); 5614 long copy_folio_from_user(struct folio *dst_folio, 5615 const void __user *usr_src, 5616 bool allow_pagefault); 5617 5618 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 5619 5620 #if MAX_NUMNODES > 1 5621 void __init setup_nr_node_ids(void); 5622 #else 5623 static inline void setup_nr_node_ids(void) {} 5624 #endif 5625 5626 extern int memcmp_pages(struct page *page1, struct page *page2); 5627 5628 static inline int pages_identical(struct page *page1, struct page *page2) 5629 { 5630 return !memcmp_pages(page1, page2); 5631 } 5632 5633 #ifdef CONFIG_MAPPING_DIRTY_HELPERS 5634 unsigned long clean_record_shared_mapping_range(struct address_space *mapping, 5635 pgoff_t first_index, pgoff_t nr, 5636 pgoff_t bitmap_pgoff, 5637 unsigned long *bitmap, 5638 pgoff_t *start, 5639 pgoff_t *end); 5640 5641 unsigned long wp_shared_mapping_range(struct address_space *mapping, 5642 pgoff_t first_index, pgoff_t nr); 5643 #endif 5644 5645 #ifdef CONFIG_ANON_VMA_NAME 5646 int set_anon_vma_name(unsigned long addr, unsigned long size, 5647 const char __user *uname); 5648 #else 5649 static inline 5650 int set_anon_vma_name(unsigned long addr, unsigned long size, 5651 const char __user *uname) 5652 { 5653 return -EINVAL; 5654 } 5655 #endif 5656 5657 #ifdef CONFIG_UNACCEPTED_MEMORY 5658 5659 bool range_contains_unaccepted_memory(phys_addr_t start, unsigned long size); 5660 void accept_memory(phys_addr_t start, unsigned long size); 5661 5662 #else 5663 5664 static inline bool range_contains_unaccepted_memory(phys_addr_t start, 5665 unsigned long size) 5666 { 5667 return false; 5668 } 5669 5670 static inline void accept_memory(phys_addr_t start, unsigned long size) 5671 { 5672 } 5673 5674 #endif 5675 5676 static inline bool pfn_is_unaccepted_memory(unsigned long pfn) 5677 { 5678 return range_contains_unaccepted_memory(pfn << PAGE_SHIFT, PAGE_SIZE); 5679 } 5680 5681 void vma_pgtable_walk_begin(struct vm_area_struct *vma); 5682 void vma_pgtable_walk_end(struct vm_area_struct *vma); 5683 5684 int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size); 5685 int reserve_mem_release_by_name(const char *name); 5686 5687 #ifdef CONFIG_64BIT 5688 void mseal_mmap_page_zero(void); 5689 #else 5690 static inline void mseal_mmap_page_zero(void) {} 5691 #endif 5692 5693 /* 5694 * user_alloc_needs_zeroing checks if a user folio from page allocator needs to 5695 * be zeroed or not. 5696 */ 5697 static inline bool user_alloc_needs_zeroing(void) 5698 { 5699 /* 5700 * for user folios, arch with cache aliasing requires cache flush and 5701 * arc changes folio->flags to make icache coherent with dcache, so 5702 * always return false to make caller use 5703 * clear_user_page()/clear_user_highpage(). 5704 */ 5705 return cpu_dcache_is_aliasing() || cpu_icache_is_aliasing() || 5706 !static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, 5707 &init_on_alloc); 5708 } 5709 5710 int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); 5711 int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); 5712 int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); 5713 5714 /* 5715 * DMA mapping IDs for page_pool 5716 * 5717 * When DMA-mapping a page, page_pool allocates an ID (from an xarray) and 5718 * stashes it in the upper bits of page->pp_magic. We always want to be able to 5719 * unambiguously identify page pool pages (using page_pool_page_is_pp()). Non-PP 5720 * pages can have arbitrary kernel pointers stored in the same field as pp_magic 5721 * (since it overlaps with page->lru.next), so we must ensure that we cannot 5722 * mistake a valid kernel pointer with any of the values we write into this 5723 * field. 5724 * 5725 * On architectures that set POISON_POINTER_DELTA, this is already ensured, 5726 * since this value becomes part of PP_SIGNATURE; meaning we can just use the 5727 * space between the PP_SIGNATURE value (without POISON_POINTER_DELTA), and the 5728 * lowest bits of POISON_POINTER_DELTA. On arches where POISON_POINTER_DELTA is 5729 * 0, we use the lowest bit of PAGE_OFFSET as the boundary if that value is 5730 * known at compile-time. 5731 * 5732 * If the value of PAGE_OFFSET is not known at compile time, or if it is too 5733 * small to leave at least 8 bits available above PP_SIGNATURE, we define the 5734 * number of bits to be 0, which turns off the DMA index tracking altogether 5735 * (see page_pool_register_dma_index()). 5736 */ 5737 #define PP_DMA_INDEX_SHIFT (1 + __fls(PP_SIGNATURE - POISON_POINTER_DELTA)) 5738 #if POISON_POINTER_DELTA > 0 5739 /* PP_SIGNATURE includes POISON_POINTER_DELTA, so limit the size of the DMA 5740 * index to not overlap with that if set 5741 */ 5742 #define PP_DMA_INDEX_BITS MIN(32, __ffs(POISON_POINTER_DELTA) - PP_DMA_INDEX_SHIFT) 5743 #else 5744 /* Use the lowest bit of PAGE_OFFSET if there's at least 8 bits available; see above */ 5745 #define PP_DMA_INDEX_MIN_OFFSET (1 << (PP_DMA_INDEX_SHIFT + 8)) 5746 #define PP_DMA_INDEX_BITS ((__builtin_constant_p(PAGE_OFFSET) && \ 5747 PAGE_OFFSET >= PP_DMA_INDEX_MIN_OFFSET && \ 5748 !(PAGE_OFFSET & (PP_DMA_INDEX_MIN_OFFSET - 1))) ? \ 5749 MIN(32, __ffs(PAGE_OFFSET) - PP_DMA_INDEX_SHIFT) : 0) 5750 5751 #endif 5752 5753 #define PP_DMA_INDEX_MASK GENMASK(PP_DMA_INDEX_BITS + PP_DMA_INDEX_SHIFT - 1, \ 5754 PP_DMA_INDEX_SHIFT) 5755 5756 /* Mask used for checking in page_pool_page_is_pp() below. page->pp_magic is 5757 * OR'ed with PP_SIGNATURE after the allocation in order to preserve bit 0 for 5758 * the head page of compound page and bit 1 for pfmemalloc page, as well as the 5759 * bits used for the DMA index. page_is_pfmemalloc() is checked in 5760 * __page_pool_put_page() to avoid recycling the pfmemalloc page. 5761 */ 5762 #define PP_MAGIC_MASK ~(PP_DMA_INDEX_MASK | 0x3UL) 5763 5764 #ifdef CONFIG_PAGE_POOL 5765 static inline bool page_pool_page_is_pp(const struct page *page) 5766 { 5767 return (page->pp_magic & PP_MAGIC_MASK) == PP_SIGNATURE; 5768 } 5769 #else 5770 static inline bool page_pool_page_is_pp(const struct page *page) 5771 { 5772 return false; 5773 } 5774 #endif 5775 5776 #define PAGE_SNAPSHOT_FAITHFUL (1 << 0) 5777 #define PAGE_SNAPSHOT_PG_BUDDY (1 << 1) 5778 #define PAGE_SNAPSHOT_PG_IDLE (1 << 2) 5779 5780 struct page_snapshot { 5781 struct folio folio_snapshot; 5782 struct page page_snapshot; 5783 unsigned long pfn; 5784 unsigned long idx; 5785 unsigned long flags; 5786 }; 5787 5788 static inline bool snapshot_page_is_faithful(const struct page_snapshot *ps) 5789 { 5790 return ps->flags & PAGE_SNAPSHOT_FAITHFUL; 5791 } 5792 5793 void snapshot_page(struct page_snapshot *ps, const struct page *page); 5794 5795 void map_anon_folio_pte_nopf(struct folio *folio, pte_t *pte, 5796 struct vm_area_struct *vma, unsigned long addr, 5797 bool uffd_wp); 5798 5799 #endif /* _LINUX_MM_H */ 5800