1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_MM_TYPES_H 3 #define _LINUX_MM_TYPES_H 4 5 #include <linux/mm_types_task.h> 6 7 #include <linux/auxvec.h> 8 #include <linux/kref.h> 9 #include <linux/list.h> 10 #include <linux/spinlock.h> 11 #include <linux/rbtree.h> 12 #include <linux/maple_tree.h> 13 #include <linux/rwsem.h> 14 #include <linux/completion.h> 15 #include <linux/cpumask.h> 16 #include <linux/uprobes.h> 17 #include <linux/rcupdate.h> 18 #include <linux/page-flags-layout.h> 19 #include <linux/workqueue.h> 20 #include <linux/seqlock.h> 21 #include <linux/percpu_counter.h> 22 23 #include <asm/mmu.h> 24 25 #ifndef AT_VECTOR_SIZE_ARCH 26 #define AT_VECTOR_SIZE_ARCH 0 27 #endif 28 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1)) 29 30 #define INIT_PASID 0 31 32 struct address_space; 33 struct mem_cgroup; 34 35 /* 36 * Each physical page in the system has a struct page associated with 37 * it to keep track of whatever it is we are using the page for at the 38 * moment. Note that we have no way to track which tasks are using 39 * a page, though if it is a pagecache page, rmap structures can tell us 40 * who is mapping it. 41 * 42 * If you allocate the page using alloc_pages(), you can use some of the 43 * space in struct page for your own purposes. The five words in the main 44 * union are available, except for bit 0 of the first word which must be 45 * kept clear. Many users use this word to store a pointer to an object 46 * which is guaranteed to be aligned. If you use the same storage as 47 * page->mapping, you must restore it to NULL before freeing the page. 48 * 49 * The mapcount field must not be used for own purposes. 50 * 51 * If you want to use the refcount field, it must be used in such a way 52 * that other CPUs temporarily incrementing and then decrementing the 53 * refcount does not cause problems. On receiving the page from 54 * alloc_pages(), the refcount will be positive. 55 * 56 * If you allocate pages of order > 0, you can use some of the fields 57 * in each subpage, but you may need to restore some of their values 58 * afterwards. 59 * 60 * SLUB uses cmpxchg_double() to atomically update its freelist and counters. 61 * That requires that freelist & counters in struct slab be adjacent and 62 * double-word aligned. Because struct slab currently just reinterprets the 63 * bits of struct page, we align all struct pages to double-word boundaries, 64 * and ensure that 'freelist' is aligned within struct slab. 65 */ 66 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE 67 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long)) 68 #else 69 #define _struct_page_alignment __aligned(sizeof(unsigned long)) 70 #endif 71 72 struct page { 73 unsigned long flags; /* Atomic flags, some possibly 74 * updated asynchronously */ 75 /* 76 * Five words (20/40 bytes) are available in this union. 77 * WARNING: bit 0 of the first word is used for PageTail(). That 78 * means the other users of this union MUST NOT use the bit to 79 * avoid collision and false-positive PageTail(). 80 */ 81 union { 82 struct { /* Page cache and anonymous pages */ 83 /** 84 * @lru: Pageout list, eg. active_list protected by 85 * lruvec->lru_lock. Sometimes used as a generic list 86 * by the page owner. 87 */ 88 union { 89 struct list_head lru; 90 91 /* Or, for the Unevictable "LRU list" slot */ 92 struct { 93 /* Always even, to negate PageTail */ 94 void *__filler; 95 /* Count page's or folio's mlocks */ 96 unsigned int mlock_count; 97 }; 98 99 /* Or, free page */ 100 struct list_head buddy_list; 101 struct list_head pcp_list; 102 }; 103 /* See page-flags.h for PAGE_MAPPING_FLAGS */ 104 struct address_space *mapping; 105 union { 106 pgoff_t index; /* Our offset within mapping. */ 107 unsigned long share; /* share count for fsdax */ 108 }; 109 /** 110 * @private: Mapping-private opaque data. 111 * Usually used for buffer_heads if PagePrivate. 112 * Used for swp_entry_t if swapcache flag set. 113 * Indicates order in the buddy system if PageBuddy. 114 */ 115 unsigned long private; 116 }; 117 struct { /* page_pool used by netstack */ 118 /** 119 * @pp_magic: magic value to avoid recycling non 120 * page_pool allocated pages. 121 */ 122 unsigned long pp_magic; 123 struct page_pool *pp; 124 unsigned long _pp_mapping_pad; 125 unsigned long dma_addr; 126 atomic_long_t pp_ref_count; 127 }; 128 struct { /* Tail pages of compound page */ 129 unsigned long compound_head; /* Bit zero is set */ 130 }; 131 struct { /* ZONE_DEVICE pages */ 132 /** @pgmap: Points to the hosting device page map. */ 133 struct dev_pagemap *pgmap; 134 void *zone_device_data; 135 /* 136 * ZONE_DEVICE private pages are counted as being 137 * mapped so the next 3 words hold the mapping, index, 138 * and private fields from the source anonymous or 139 * page cache page while the page is migrated to device 140 * private memory. 141 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also 142 * use the mapping, index, and private fields when 143 * pmem backed DAX files are mapped. 144 */ 145 }; 146 147 /** @rcu_head: You can use this to free a page by RCU. */ 148 struct rcu_head rcu_head; 149 }; 150 151 union { /* This union is 4 bytes in size. */ 152 /* 153 * For head pages of typed folios, the value stored here 154 * allows for determining what this page is used for. The 155 * tail pages of typed folios will not store a type 156 * (page_type == _mapcount == -1). 157 * 158 * See page-flags.h for a list of page types which are currently 159 * stored here. 160 * 161 * Owners of typed folios may reuse the lower 16 bit of the 162 * head page page_type field after setting the page type, 163 * but must reset these 16 bit to -1 before clearing the 164 * page type. 165 */ 166 unsigned int page_type; 167 168 /* 169 * For pages that are part of non-typed folios for which mappings 170 * are tracked via the RMAP, encodes the number of times this page 171 * is directly referenced by a page table. 172 * 173 * Note that the mapcount is always initialized to -1, so that 174 * transitions both from it and to it can be tracked, using 175 * atomic_inc_and_test() and atomic_add_negative(-1). 176 */ 177 atomic_t _mapcount; 178 }; 179 180 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */ 181 atomic_t _refcount; 182 183 #ifdef CONFIG_MEMCG 184 unsigned long memcg_data; 185 #elif defined(CONFIG_SLAB_OBJ_EXT) 186 unsigned long _unused_slab_obj_exts; 187 #endif 188 189 /* 190 * On machines where all RAM is mapped into kernel address space, 191 * we can simply calculate the virtual address. On machines with 192 * highmem some memory is mapped into kernel virtual memory 193 * dynamically, so we need a place to store that address. 194 * Note that this field could be 16 bits on x86 ... ;) 195 * 196 * Architectures with slow multiplication can define 197 * WANT_PAGE_VIRTUAL in asm/page.h 198 */ 199 #if defined(WANT_PAGE_VIRTUAL) 200 void *virtual; /* Kernel virtual address (NULL if 201 not kmapped, ie. highmem) */ 202 #endif /* WANT_PAGE_VIRTUAL */ 203 204 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 205 int _last_cpupid; 206 #endif 207 208 #ifdef CONFIG_KMSAN 209 /* 210 * KMSAN metadata for this page: 211 * - shadow page: every bit indicates whether the corresponding 212 * bit of the original page is initialized (0) or not (1); 213 * - origin page: every 4 bytes contain an id of the stack trace 214 * where the uninitialized value was created. 215 */ 216 struct page *kmsan_shadow; 217 struct page *kmsan_origin; 218 #endif 219 } _struct_page_alignment; 220 221 /* 222 * struct encoded_page - a nonexistent type marking this pointer 223 * 224 * An 'encoded_page' pointer is a pointer to a regular 'struct page', but 225 * with the low bits of the pointer indicating extra context-dependent 226 * information. Only used in mmu_gather handling, and this acts as a type 227 * system check on that use. 228 * 229 * We only really have two guaranteed bits in general, although you could 230 * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE) 231 * for more. 232 * 233 * Use the supplied helper functions to endcode/decode the pointer and bits. 234 */ 235 struct encoded_page; 236 237 #define ENCODED_PAGE_BITS 3ul 238 239 /* Perform rmap removal after we have flushed the TLB. */ 240 #define ENCODED_PAGE_BIT_DELAY_RMAP 1ul 241 242 /* 243 * The next item in an encoded_page array is the "nr_pages" argument, specifying 244 * the number of consecutive pages starting from this page, that all belong to 245 * the same folio. For example, "nr_pages" corresponds to the number of folio 246 * references that must be dropped. If this bit is not set, "nr_pages" is 247 * implicitly 1. 248 */ 249 #define ENCODED_PAGE_BIT_NR_PAGES_NEXT 2ul 250 251 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags) 252 { 253 BUILD_BUG_ON(flags > ENCODED_PAGE_BITS); 254 return (struct encoded_page *)(flags | (unsigned long)page); 255 } 256 257 static inline unsigned long encoded_page_flags(struct encoded_page *page) 258 { 259 return ENCODED_PAGE_BITS & (unsigned long)page; 260 } 261 262 static inline struct page *encoded_page_ptr(struct encoded_page *page) 263 { 264 return (struct page *)(~ENCODED_PAGE_BITS & (unsigned long)page); 265 } 266 267 static __always_inline struct encoded_page *encode_nr_pages(unsigned long nr) 268 { 269 VM_WARN_ON_ONCE((nr << 2) >> 2 != nr); 270 return (struct encoded_page *)(nr << 2); 271 } 272 273 static __always_inline unsigned long encoded_nr_pages(struct encoded_page *page) 274 { 275 return ((unsigned long)page) >> 2; 276 } 277 278 /* 279 * A swap entry has to fit into a "unsigned long", as the entry is hidden 280 * in the "index" field of the swapper address space. 281 */ 282 typedef struct { 283 unsigned long val; 284 } swp_entry_t; 285 286 /** 287 * struct folio - Represents a contiguous set of bytes. 288 * @flags: Identical to the page flags. 289 * @lru: Least Recently Used list; tracks how recently this folio was used. 290 * @mlock_count: Number of times this folio has been pinned by mlock(). 291 * @mapping: The file this page belongs to, or refers to the anon_vma for 292 * anonymous memory. 293 * @index: Offset within the file, in units of pages. For anonymous memory, 294 * this is the index from the beginning of the mmap. 295 * @private: Filesystem per-folio data (see folio_attach_private()). 296 * @swap: Used for swp_entry_t if folio_test_swapcache(). 297 * @_mapcount: Do not access this member directly. Use folio_mapcount() to 298 * find out how many times this folio is mapped by userspace. 299 * @_refcount: Do not access this member directly. Use folio_ref_count() 300 * to find how many references there are to this folio. 301 * @memcg_data: Memory Control Group data. 302 * @virtual: Virtual address in the kernel direct map. 303 * @_last_cpupid: IDs of last CPU and last process that accessed the folio. 304 * @_entire_mapcount: Do not use directly, call folio_entire_mapcount(). 305 * @_large_mapcount: Do not use directly, call folio_mapcount(). 306 * @_nr_pages_mapped: Do not use outside of rmap and debug code. 307 * @_pincount: Do not use directly, call folio_maybe_dma_pinned(). 308 * @_folio_nr_pages: Do not use directly, call folio_nr_pages(). 309 * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h. 310 * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h. 311 * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h. 312 * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head(). 313 * @_deferred_list: Folios to be split under memory pressure. 314 * @_unused_slab_obj_exts: Placeholder to match obj_exts in struct slab. 315 * 316 * A folio is a physically, virtually and logically contiguous set 317 * of bytes. It is a power-of-two in size, and it is aligned to that 318 * same power-of-two. It is at least as large as %PAGE_SIZE. If it is 319 * in the page cache, it is at a file offset which is a multiple of that 320 * power-of-two. It may be mapped into userspace at an address which is 321 * at an arbitrary page offset, but its kernel virtual address is aligned 322 * to its size. 323 */ 324 struct folio { 325 /* private: don't document the anon union */ 326 union { 327 struct { 328 /* public: */ 329 unsigned long flags; 330 union { 331 struct list_head lru; 332 /* private: avoid cluttering the output */ 333 struct { 334 void *__filler; 335 /* public: */ 336 unsigned int mlock_count; 337 /* private: */ 338 }; 339 /* public: */ 340 }; 341 struct address_space *mapping; 342 pgoff_t index; 343 union { 344 void *private; 345 swp_entry_t swap; 346 }; 347 atomic_t _mapcount; 348 atomic_t _refcount; 349 #ifdef CONFIG_MEMCG 350 unsigned long memcg_data; 351 #elif defined(CONFIG_SLAB_OBJ_EXT) 352 unsigned long _unused_slab_obj_exts; 353 #endif 354 #if defined(WANT_PAGE_VIRTUAL) 355 void *virtual; 356 #endif 357 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 358 int _last_cpupid; 359 #endif 360 /* private: the union with struct page is transitional */ 361 }; 362 struct page page; 363 }; 364 union { 365 struct { 366 unsigned long _flags_1; 367 unsigned long _head_1; 368 /* public: */ 369 atomic_t _large_mapcount; 370 atomic_t _entire_mapcount; 371 atomic_t _nr_pages_mapped; 372 atomic_t _pincount; 373 #ifdef CONFIG_64BIT 374 unsigned int _folio_nr_pages; 375 #endif 376 /* private: the union with struct page is transitional */ 377 }; 378 struct page __page_1; 379 }; 380 union { 381 struct { 382 unsigned long _flags_2; 383 unsigned long _head_2; 384 /* public: */ 385 void *_hugetlb_subpool; 386 void *_hugetlb_cgroup; 387 void *_hugetlb_cgroup_rsvd; 388 void *_hugetlb_hwpoison; 389 /* private: the union with struct page is transitional */ 390 }; 391 struct { 392 unsigned long _flags_2a; 393 unsigned long _head_2a; 394 /* public: */ 395 struct list_head _deferred_list; 396 /* private: the union with struct page is transitional */ 397 }; 398 struct page __page_2; 399 }; 400 }; 401 402 #define FOLIO_MATCH(pg, fl) \ 403 static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl)) 404 FOLIO_MATCH(flags, flags); 405 FOLIO_MATCH(lru, lru); 406 FOLIO_MATCH(mapping, mapping); 407 FOLIO_MATCH(compound_head, lru); 408 FOLIO_MATCH(index, index); 409 FOLIO_MATCH(private, private); 410 FOLIO_MATCH(_mapcount, _mapcount); 411 FOLIO_MATCH(_refcount, _refcount); 412 #ifdef CONFIG_MEMCG 413 FOLIO_MATCH(memcg_data, memcg_data); 414 #endif 415 #if defined(WANT_PAGE_VIRTUAL) 416 FOLIO_MATCH(virtual, virtual); 417 #endif 418 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 419 FOLIO_MATCH(_last_cpupid, _last_cpupid); 420 #endif 421 #undef FOLIO_MATCH 422 #define FOLIO_MATCH(pg, fl) \ 423 static_assert(offsetof(struct folio, fl) == \ 424 offsetof(struct page, pg) + sizeof(struct page)) 425 FOLIO_MATCH(flags, _flags_1); 426 FOLIO_MATCH(compound_head, _head_1); 427 #undef FOLIO_MATCH 428 #define FOLIO_MATCH(pg, fl) \ 429 static_assert(offsetof(struct folio, fl) == \ 430 offsetof(struct page, pg) + 2 * sizeof(struct page)) 431 FOLIO_MATCH(flags, _flags_2); 432 FOLIO_MATCH(compound_head, _head_2); 433 FOLIO_MATCH(flags, _flags_2a); 434 FOLIO_MATCH(compound_head, _head_2a); 435 #undef FOLIO_MATCH 436 437 /** 438 * struct ptdesc - Memory descriptor for page tables. 439 * @__page_flags: Same as page flags. Powerpc only. 440 * @pt_rcu_head: For freeing page table pages. 441 * @pt_list: List of used page tables. Used for s390 and x86. 442 * @_pt_pad_1: Padding that aliases with page's compound head. 443 * @pmd_huge_pte: Protected by ptdesc->ptl, used for THPs. 444 * @__page_mapping: Aliases with page->mapping. Unused for page tables. 445 * @pt_index: Used for s390 gmap. 446 * @pt_mm: Used for x86 pgds. 447 * @pt_frag_refcount: For fragmented page table tracking. Powerpc only. 448 * @_pt_pad_2: Padding to ensure proper alignment. 449 * @ptl: Lock for the page table. 450 * @__page_type: Same as page->page_type. Unused for page tables. 451 * @__page_refcount: Same as page refcount. 452 * @pt_memcg_data: Memcg data. Tracked for page tables here. 453 * 454 * This struct overlays struct page for now. Do not modify without a good 455 * understanding of the issues. 456 */ 457 struct ptdesc { 458 unsigned long __page_flags; 459 460 union { 461 struct rcu_head pt_rcu_head; 462 struct list_head pt_list; 463 struct { 464 unsigned long _pt_pad_1; 465 pgtable_t pmd_huge_pte; 466 }; 467 }; 468 unsigned long __page_mapping; 469 470 union { 471 pgoff_t pt_index; 472 struct mm_struct *pt_mm; 473 atomic_t pt_frag_refcount; 474 }; 475 476 union { 477 unsigned long _pt_pad_2; 478 #if ALLOC_SPLIT_PTLOCKS 479 spinlock_t *ptl; 480 #else 481 spinlock_t ptl; 482 #endif 483 }; 484 unsigned int __page_type; 485 atomic_t __page_refcount; 486 #ifdef CONFIG_MEMCG 487 unsigned long pt_memcg_data; 488 #endif 489 }; 490 491 #define TABLE_MATCH(pg, pt) \ 492 static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt)) 493 TABLE_MATCH(flags, __page_flags); 494 TABLE_MATCH(compound_head, pt_list); 495 TABLE_MATCH(compound_head, _pt_pad_1); 496 TABLE_MATCH(mapping, __page_mapping); 497 TABLE_MATCH(index, pt_index); 498 TABLE_MATCH(rcu_head, pt_rcu_head); 499 TABLE_MATCH(page_type, __page_type); 500 TABLE_MATCH(_refcount, __page_refcount); 501 #ifdef CONFIG_MEMCG 502 TABLE_MATCH(memcg_data, pt_memcg_data); 503 #endif 504 #undef TABLE_MATCH 505 static_assert(sizeof(struct ptdesc) <= sizeof(struct page)); 506 507 #define ptdesc_page(pt) (_Generic((pt), \ 508 const struct ptdesc *: (const struct page *)(pt), \ 509 struct ptdesc *: (struct page *)(pt))) 510 511 #define ptdesc_folio(pt) (_Generic((pt), \ 512 const struct ptdesc *: (const struct folio *)(pt), \ 513 struct ptdesc *: (struct folio *)(pt))) 514 515 #define page_ptdesc(p) (_Generic((p), \ 516 const struct page *: (const struct ptdesc *)(p), \ 517 struct page *: (struct ptdesc *)(p))) 518 519 /* 520 * Used for sizing the vmemmap region on some architectures 521 */ 522 #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) 523 524 /* 525 * page_private can be used on tail pages. However, PagePrivate is only 526 * checked by the VM on the head page. So page_private on the tail pages 527 * should be used for data that's ancillary to the head page (eg attaching 528 * buffer heads to tail pages after attaching buffer heads to the head page) 529 */ 530 #define page_private(page) ((page)->private) 531 532 static inline void set_page_private(struct page *page, unsigned long private) 533 { 534 page->private = private; 535 } 536 537 static inline void *folio_get_private(struct folio *folio) 538 { 539 return folio->private; 540 } 541 542 typedef unsigned long vm_flags_t; 543 544 /* 545 * A region containing a mapping of a non-memory backed file under NOMMU 546 * conditions. These are held in a global tree and are pinned by the VMAs that 547 * map parts of them. 548 */ 549 struct vm_region { 550 struct rb_node vm_rb; /* link in global region tree */ 551 vm_flags_t vm_flags; /* VMA vm_flags */ 552 unsigned long vm_start; /* start address of region */ 553 unsigned long vm_end; /* region initialised to here */ 554 unsigned long vm_top; /* region allocated to here */ 555 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */ 556 struct file *vm_file; /* the backing file or NULL */ 557 558 int vm_usage; /* region usage count (access under nommu_region_sem) */ 559 bool vm_icache_flushed : 1; /* true if the icache has been flushed for 560 * this region */ 561 }; 562 563 #ifdef CONFIG_USERFAULTFD 564 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, }) 565 struct vm_userfaultfd_ctx { 566 struct userfaultfd_ctx *ctx; 567 }; 568 #else /* CONFIG_USERFAULTFD */ 569 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {}) 570 struct vm_userfaultfd_ctx {}; 571 #endif /* CONFIG_USERFAULTFD */ 572 573 struct anon_vma_name { 574 struct kref kref; 575 /* The name needs to be at the end because it is dynamically sized. */ 576 char name[]; 577 }; 578 579 #ifdef CONFIG_ANON_VMA_NAME 580 /* 581 * mmap_lock should be read-locked when calling anon_vma_name(). Caller should 582 * either keep holding the lock while using the returned pointer or it should 583 * raise anon_vma_name refcount before releasing the lock. 584 */ 585 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma); 586 struct anon_vma_name *anon_vma_name_alloc(const char *name); 587 void anon_vma_name_free(struct kref *kref); 588 #else /* CONFIG_ANON_VMA_NAME */ 589 static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma) 590 { 591 return NULL; 592 } 593 594 static inline struct anon_vma_name *anon_vma_name_alloc(const char *name) 595 { 596 return NULL; 597 } 598 #endif 599 600 struct vma_lock { 601 struct rw_semaphore lock; 602 }; 603 604 struct vma_numab_state { 605 /* 606 * Initialised as time in 'jiffies' after which VMA 607 * should be scanned. Delays first scan of new VMA by at 608 * least sysctl_numa_balancing_scan_delay: 609 */ 610 unsigned long next_scan; 611 612 /* 613 * Time in jiffies when pids_active[] is reset to 614 * detect phase change behaviour: 615 */ 616 unsigned long pids_active_reset; 617 618 /* 619 * Approximate tracking of PIDs that trapped a NUMA hinting 620 * fault. May produce false positives due to hash collisions. 621 * 622 * [0] Previous PID tracking 623 * [1] Current PID tracking 624 * 625 * Window moves after next_pid_reset has expired approximately 626 * every VMA_PID_RESET_PERIOD jiffies: 627 */ 628 unsigned long pids_active[2]; 629 630 /* MM scan sequence ID when scan first started after VMA creation */ 631 int start_scan_seq; 632 633 /* 634 * MM scan sequence ID when the VMA was last completely scanned. 635 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq 636 */ 637 int prev_scan_seq; 638 }; 639 640 /* 641 * This struct describes a virtual memory area. There is one of these 642 * per VM-area/task. A VM area is any part of the process virtual memory 643 * space that has a special rule for the page-fault handlers (ie a shared 644 * library, the executable area etc). 645 * 646 * Only explicitly marked struct members may be accessed by RCU readers before 647 * getting a stable reference. 648 */ 649 struct vm_area_struct { 650 /* The first cache line has the info for VMA tree walking. */ 651 652 union { 653 struct { 654 /* VMA covers [vm_start; vm_end) addresses within mm */ 655 unsigned long vm_start; 656 unsigned long vm_end; 657 }; 658 #ifdef CONFIG_PER_VMA_LOCK 659 struct rcu_head vm_rcu; /* Used for deferred freeing. */ 660 #endif 661 }; 662 663 /* 664 * The address space we belong to. 665 * Unstable RCU readers are allowed to read this. 666 */ 667 struct mm_struct *vm_mm; 668 pgprot_t vm_page_prot; /* Access permissions of this VMA. */ 669 670 /* 671 * Flags, see mm.h. 672 * To modify use vm_flags_{init|reset|set|clear|mod} functions. 673 */ 674 union { 675 const vm_flags_t vm_flags; 676 vm_flags_t __private __vm_flags; 677 }; 678 679 #ifdef CONFIG_PER_VMA_LOCK 680 /* 681 * Flag to indicate areas detached from the mm->mm_mt tree. 682 * Unstable RCU readers are allowed to read this. 683 */ 684 bool detached; 685 686 /* 687 * Can only be written (using WRITE_ONCE()) while holding both: 688 * - mmap_lock (in write mode) 689 * - vm_lock->lock (in write mode) 690 * Can be read reliably while holding one of: 691 * - mmap_lock (in read or write mode) 692 * - vm_lock->lock (in read or write mode) 693 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout 694 * while holding nothing (except RCU to keep the VMA struct allocated). 695 * 696 * This sequence counter is explicitly allowed to overflow; sequence 697 * counter reuse can only lead to occasional unnecessary use of the 698 * slowpath. 699 */ 700 int vm_lock_seq; 701 /* Unstable RCU readers are allowed to read this. */ 702 struct vma_lock *vm_lock; 703 #endif 704 705 /* 706 * For areas with an address space and backing store, 707 * linkage into the address_space->i_mmap interval tree. 708 * 709 */ 710 struct { 711 struct rb_node rb; 712 unsigned long rb_subtree_last; 713 } shared; 714 715 /* 716 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma 717 * list, after a COW of one of the file pages. A MAP_SHARED vma 718 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack 719 * or brk vma (with NULL file) can only be in an anon_vma list. 720 */ 721 struct list_head anon_vma_chain; /* Serialized by mmap_lock & 722 * page_table_lock */ 723 struct anon_vma *anon_vma; /* Serialized by page_table_lock */ 724 725 /* Function pointers to deal with this struct. */ 726 const struct vm_operations_struct *vm_ops; 727 728 /* Information about our backing store: */ 729 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE 730 units */ 731 struct file * vm_file; /* File we map to (can be NULL). */ 732 void * vm_private_data; /* was vm_pte (shared mem) */ 733 734 #ifdef CONFIG_ANON_VMA_NAME 735 /* 736 * For private and shared anonymous mappings, a pointer to a null 737 * terminated string containing the name given to the vma, or NULL if 738 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access. 739 */ 740 struct anon_vma_name *anon_name; 741 #endif 742 #ifdef CONFIG_SWAP 743 atomic_long_t swap_readahead_info; 744 #endif 745 #ifndef CONFIG_MMU 746 struct vm_region *vm_region; /* NOMMU mapping region */ 747 #endif 748 #ifdef CONFIG_NUMA 749 struct mempolicy *vm_policy; /* NUMA policy for the VMA */ 750 #endif 751 #ifdef CONFIG_NUMA_BALANCING 752 struct vma_numab_state *numab_state; /* NUMA Balancing state */ 753 #endif 754 struct vm_userfaultfd_ctx vm_userfaultfd_ctx; 755 } __randomize_layout; 756 757 #ifdef CONFIG_NUMA 758 #define vma_policy(vma) ((vma)->vm_policy) 759 #else 760 #define vma_policy(vma) NULL 761 #endif 762 763 #ifdef CONFIG_SCHED_MM_CID 764 struct mm_cid { 765 u64 time; 766 int cid; 767 }; 768 #endif 769 770 struct kioctx_table; 771 struct iommu_mm_data; 772 struct mm_struct { 773 struct { 774 /* 775 * Fields which are often written to are placed in a separate 776 * cache line. 777 */ 778 struct { 779 /** 780 * @mm_count: The number of references to &struct 781 * mm_struct (@mm_users count as 1). 782 * 783 * Use mmgrab()/mmdrop() to modify. When this drops to 784 * 0, the &struct mm_struct is freed. 785 */ 786 atomic_t mm_count; 787 } ____cacheline_aligned_in_smp; 788 789 struct maple_tree mm_mt; 790 791 unsigned long mmap_base; /* base of mmap area */ 792 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */ 793 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES 794 /* Base addresses for compatible mmap() */ 795 unsigned long mmap_compat_base; 796 unsigned long mmap_compat_legacy_base; 797 #endif 798 unsigned long task_size; /* size of task vm space */ 799 pgd_t * pgd; 800 801 #ifdef CONFIG_MEMBARRIER 802 /** 803 * @membarrier_state: Flags controlling membarrier behavior. 804 * 805 * This field is close to @pgd to hopefully fit in the same 806 * cache-line, which needs to be touched by switch_mm(). 807 */ 808 atomic_t membarrier_state; 809 #endif 810 811 /** 812 * @mm_users: The number of users including userspace. 813 * 814 * Use mmget()/mmget_not_zero()/mmput() to modify. When this 815 * drops to 0 (i.e. when the task exits and there are no other 816 * temporary reference holders), we also release a reference on 817 * @mm_count (which may then free the &struct mm_struct if 818 * @mm_count also drops to 0). 819 */ 820 atomic_t mm_users; 821 822 #ifdef CONFIG_SCHED_MM_CID 823 /** 824 * @pcpu_cid: Per-cpu current cid. 825 * 826 * Keep track of the currently allocated mm_cid for each cpu. 827 * The per-cpu mm_cid values are serialized by their respective 828 * runqueue locks. 829 */ 830 struct mm_cid __percpu *pcpu_cid; 831 /* 832 * @mm_cid_next_scan: Next mm_cid scan (in jiffies). 833 * 834 * When the next mm_cid scan is due (in jiffies). 835 */ 836 unsigned long mm_cid_next_scan; 837 #endif 838 #ifdef CONFIG_MMU 839 atomic_long_t pgtables_bytes; /* size of all page tables */ 840 #endif 841 int map_count; /* number of VMAs */ 842 843 spinlock_t page_table_lock; /* Protects page tables and some 844 * counters 845 */ 846 /* 847 * With some kernel config, the current mmap_lock's offset 848 * inside 'mm_struct' is at 0x120, which is very optimal, as 849 * its two hot fields 'count' and 'owner' sit in 2 different 850 * cachelines, and when mmap_lock is highly contended, both 851 * of the 2 fields will be accessed frequently, current layout 852 * will help to reduce cache bouncing. 853 * 854 * So please be careful with adding new fields before 855 * mmap_lock, which can easily push the 2 fields into one 856 * cacheline. 857 */ 858 struct rw_semaphore mmap_lock; 859 860 struct list_head mmlist; /* List of maybe swapped mm's. These 861 * are globally strung together off 862 * init_mm.mmlist, and are protected 863 * by mmlist_lock 864 */ 865 #ifdef CONFIG_PER_VMA_LOCK 866 /* 867 * This field has lock-like semantics, meaning it is sometimes 868 * accessed with ACQUIRE/RELEASE semantics. 869 * Roughly speaking, incrementing the sequence number is 870 * equivalent to releasing locks on VMAs; reading the sequence 871 * number can be part of taking a read lock on a VMA. 872 * 873 * Can be modified under write mmap_lock using RELEASE 874 * semantics. 875 * Can be read with no other protection when holding write 876 * mmap_lock. 877 * Can be read with ACQUIRE semantics if not holding write 878 * mmap_lock. 879 */ 880 int mm_lock_seq; 881 #endif 882 883 884 unsigned long hiwater_rss; /* High-watermark of RSS usage */ 885 unsigned long hiwater_vm; /* High-water virtual memory usage */ 886 887 unsigned long total_vm; /* Total pages mapped */ 888 unsigned long locked_vm; /* Pages that have PG_mlocked set */ 889 atomic64_t pinned_vm; /* Refcount permanently increased */ 890 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */ 891 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */ 892 unsigned long stack_vm; /* VM_STACK */ 893 unsigned long def_flags; 894 895 /** 896 * @write_protect_seq: Locked when any thread is write 897 * protecting pages mapped by this mm to enforce a later COW, 898 * for instance during page table copying for fork(). 899 */ 900 seqcount_t write_protect_seq; 901 902 spinlock_t arg_lock; /* protect the below fields */ 903 904 unsigned long start_code, end_code, start_data, end_data; 905 unsigned long start_brk, brk, start_stack; 906 unsigned long arg_start, arg_end, env_start, env_end; 907 908 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */ 909 910 struct percpu_counter rss_stat[NR_MM_COUNTERS]; 911 912 struct linux_binfmt *binfmt; 913 914 /* Architecture-specific MM context */ 915 mm_context_t context; 916 917 unsigned long flags; /* Must use atomic bitops to access */ 918 919 #ifdef CONFIG_AIO 920 spinlock_t ioctx_lock; 921 struct kioctx_table __rcu *ioctx_table; 922 #endif 923 #ifdef CONFIG_MEMCG 924 /* 925 * "owner" points to a task that is regarded as the canonical 926 * user/owner of this mm. All of the following must be true in 927 * order for it to be changed: 928 * 929 * current == mm->owner 930 * current->mm != mm 931 * new_owner->mm == mm 932 * new_owner->alloc_lock is held 933 */ 934 struct task_struct __rcu *owner; 935 #endif 936 struct user_namespace *user_ns; 937 938 /* store ref to file /proc/<pid>/exe symlink points to */ 939 struct file __rcu *exe_file; 940 #ifdef CONFIG_MMU_NOTIFIER 941 struct mmu_notifier_subscriptions *notifier_subscriptions; 942 #endif 943 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS) 944 pgtable_t pmd_huge_pte; /* protected by page_table_lock */ 945 #endif 946 #ifdef CONFIG_NUMA_BALANCING 947 /* 948 * numa_next_scan is the next time that PTEs will be remapped 949 * PROT_NONE to trigger NUMA hinting faults; such faults gather 950 * statistics and migrate pages to new nodes if necessary. 951 */ 952 unsigned long numa_next_scan; 953 954 /* Restart point for scanning and remapping PTEs. */ 955 unsigned long numa_scan_offset; 956 957 /* numa_scan_seq prevents two threads remapping PTEs. */ 958 int numa_scan_seq; 959 #endif 960 /* 961 * An operation with batched TLB flushing is going on. Anything 962 * that can move process memory needs to flush the TLB when 963 * moving a PROT_NONE mapped page. 964 */ 965 atomic_t tlb_flush_pending; 966 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 967 /* See flush_tlb_batched_pending() */ 968 atomic_t tlb_flush_batched; 969 #endif 970 struct uprobes_state uprobes_state; 971 #ifdef CONFIG_PREEMPT_RT 972 struct rcu_head delayed_drop; 973 #endif 974 #ifdef CONFIG_HUGETLB_PAGE 975 atomic_long_t hugetlb_usage; 976 #endif 977 struct work_struct async_put_work; 978 979 #ifdef CONFIG_IOMMU_MM_DATA 980 struct iommu_mm_data *iommu_mm; 981 #endif 982 #ifdef CONFIG_KSM 983 /* 984 * Represent how many pages of this process are involved in KSM 985 * merging (not including ksm_zero_pages). 986 */ 987 unsigned long ksm_merging_pages; 988 /* 989 * Represent how many pages are checked for ksm merging 990 * including merged and not merged. 991 */ 992 unsigned long ksm_rmap_items; 993 /* 994 * Represent how many empty pages are merged with kernel zero 995 * pages when enabling KSM use_zero_pages. 996 */ 997 atomic_long_t ksm_zero_pages; 998 #endif /* CONFIG_KSM */ 999 #ifdef CONFIG_LRU_GEN_WALKS_MMU 1000 struct { 1001 /* this mm_struct is on lru_gen_mm_list */ 1002 struct list_head list; 1003 /* 1004 * Set when switching to this mm_struct, as a hint of 1005 * whether it has been used since the last time per-node 1006 * page table walkers cleared the corresponding bits. 1007 */ 1008 unsigned long bitmap; 1009 #ifdef CONFIG_MEMCG 1010 /* points to the memcg of "owner" above */ 1011 struct mem_cgroup *memcg; 1012 #endif 1013 } lru_gen; 1014 #endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1015 } __randomize_layout; 1016 1017 /* 1018 * The mm_cpumask needs to be at the end of mm_struct, because it 1019 * is dynamically sized based on nr_cpu_ids. 1020 */ 1021 unsigned long cpu_bitmap[]; 1022 }; 1023 1024 #define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \ 1025 MT_FLAGS_USE_RCU) 1026 extern struct mm_struct init_mm; 1027 1028 /* Pointer magic because the dynamic array size confuses some compilers. */ 1029 static inline void mm_init_cpumask(struct mm_struct *mm) 1030 { 1031 unsigned long cpu_bitmap = (unsigned long)mm; 1032 1033 cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap); 1034 cpumask_clear((struct cpumask *)cpu_bitmap); 1035 } 1036 1037 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */ 1038 static inline cpumask_t *mm_cpumask(struct mm_struct *mm) 1039 { 1040 return (struct cpumask *)&mm->cpu_bitmap; 1041 } 1042 1043 #ifdef CONFIG_LRU_GEN 1044 1045 struct lru_gen_mm_list { 1046 /* mm_struct list for page table walkers */ 1047 struct list_head fifo; 1048 /* protects the list above */ 1049 spinlock_t lock; 1050 }; 1051 1052 #endif /* CONFIG_LRU_GEN */ 1053 1054 #ifdef CONFIG_LRU_GEN_WALKS_MMU 1055 1056 void lru_gen_add_mm(struct mm_struct *mm); 1057 void lru_gen_del_mm(struct mm_struct *mm); 1058 void lru_gen_migrate_mm(struct mm_struct *mm); 1059 1060 static inline void lru_gen_init_mm(struct mm_struct *mm) 1061 { 1062 INIT_LIST_HEAD(&mm->lru_gen.list); 1063 mm->lru_gen.bitmap = 0; 1064 #ifdef CONFIG_MEMCG 1065 mm->lru_gen.memcg = NULL; 1066 #endif 1067 } 1068 1069 static inline void lru_gen_use_mm(struct mm_struct *mm) 1070 { 1071 /* 1072 * When the bitmap is set, page reclaim knows this mm_struct has been 1073 * used since the last time it cleared the bitmap. So it might be worth 1074 * walking the page tables of this mm_struct to clear the accessed bit. 1075 */ 1076 WRITE_ONCE(mm->lru_gen.bitmap, -1); 1077 } 1078 1079 #else /* !CONFIG_LRU_GEN_WALKS_MMU */ 1080 1081 static inline void lru_gen_add_mm(struct mm_struct *mm) 1082 { 1083 } 1084 1085 static inline void lru_gen_del_mm(struct mm_struct *mm) 1086 { 1087 } 1088 1089 static inline void lru_gen_migrate_mm(struct mm_struct *mm) 1090 { 1091 } 1092 1093 static inline void lru_gen_init_mm(struct mm_struct *mm) 1094 { 1095 } 1096 1097 static inline void lru_gen_use_mm(struct mm_struct *mm) 1098 { 1099 } 1100 1101 #endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1102 1103 struct vma_iterator { 1104 struct ma_state mas; 1105 }; 1106 1107 #define VMA_ITERATOR(name, __mm, __addr) \ 1108 struct vma_iterator name = { \ 1109 .mas = { \ 1110 .tree = &(__mm)->mm_mt, \ 1111 .index = __addr, \ 1112 .node = NULL, \ 1113 .status = ma_start, \ 1114 }, \ 1115 } 1116 1117 static inline void vma_iter_init(struct vma_iterator *vmi, 1118 struct mm_struct *mm, unsigned long addr) 1119 { 1120 mas_init(&vmi->mas, &mm->mm_mt, addr); 1121 } 1122 1123 #ifdef CONFIG_SCHED_MM_CID 1124 1125 enum mm_cid_state { 1126 MM_CID_UNSET = -1U, /* Unset state has lazy_put flag set. */ 1127 MM_CID_LAZY_PUT = (1U << 31), 1128 }; 1129 1130 static inline bool mm_cid_is_unset(int cid) 1131 { 1132 return cid == MM_CID_UNSET; 1133 } 1134 1135 static inline bool mm_cid_is_lazy_put(int cid) 1136 { 1137 return !mm_cid_is_unset(cid) && (cid & MM_CID_LAZY_PUT); 1138 } 1139 1140 static inline bool mm_cid_is_valid(int cid) 1141 { 1142 return !(cid & MM_CID_LAZY_PUT); 1143 } 1144 1145 static inline int mm_cid_set_lazy_put(int cid) 1146 { 1147 return cid | MM_CID_LAZY_PUT; 1148 } 1149 1150 static inline int mm_cid_clear_lazy_put(int cid) 1151 { 1152 return cid & ~MM_CID_LAZY_PUT; 1153 } 1154 1155 /* Accessor for struct mm_struct's cidmask. */ 1156 static inline cpumask_t *mm_cidmask(struct mm_struct *mm) 1157 { 1158 unsigned long cid_bitmap = (unsigned long)mm; 1159 1160 cid_bitmap += offsetof(struct mm_struct, cpu_bitmap); 1161 /* Skip cpu_bitmap */ 1162 cid_bitmap += cpumask_size(); 1163 return (struct cpumask *)cid_bitmap; 1164 } 1165 1166 static inline void mm_init_cid(struct mm_struct *mm) 1167 { 1168 int i; 1169 1170 for_each_possible_cpu(i) { 1171 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, i); 1172 1173 pcpu_cid->cid = MM_CID_UNSET; 1174 pcpu_cid->time = 0; 1175 } 1176 cpumask_clear(mm_cidmask(mm)); 1177 } 1178 1179 static inline int mm_alloc_cid_noprof(struct mm_struct *mm) 1180 { 1181 mm->pcpu_cid = alloc_percpu_noprof(struct mm_cid); 1182 if (!mm->pcpu_cid) 1183 return -ENOMEM; 1184 mm_init_cid(mm); 1185 return 0; 1186 } 1187 #define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__)) 1188 1189 static inline void mm_destroy_cid(struct mm_struct *mm) 1190 { 1191 free_percpu(mm->pcpu_cid); 1192 mm->pcpu_cid = NULL; 1193 } 1194 1195 static inline unsigned int mm_cid_size(void) 1196 { 1197 return cpumask_size(); 1198 } 1199 #else /* CONFIG_SCHED_MM_CID */ 1200 static inline void mm_init_cid(struct mm_struct *mm) { } 1201 static inline int mm_alloc_cid(struct mm_struct *mm) { return 0; } 1202 static inline void mm_destroy_cid(struct mm_struct *mm) { } 1203 static inline unsigned int mm_cid_size(void) 1204 { 1205 return 0; 1206 } 1207 #endif /* CONFIG_SCHED_MM_CID */ 1208 1209 struct mmu_gather; 1210 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm); 1211 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm); 1212 extern void tlb_finish_mmu(struct mmu_gather *tlb); 1213 1214 struct vm_fault; 1215 1216 /** 1217 * typedef vm_fault_t - Return type for page fault handlers. 1218 * 1219 * Page fault handlers return a bitmask of %VM_FAULT values. 1220 */ 1221 typedef __bitwise unsigned int vm_fault_t; 1222 1223 /** 1224 * enum vm_fault_reason - Page fault handlers return a bitmask of 1225 * these values to tell the core VM what happened when handling the 1226 * fault. Used to decide whether a process gets delivered SIGBUS or 1227 * just gets major/minor fault counters bumped up. 1228 * 1229 * @VM_FAULT_OOM: Out Of Memory 1230 * @VM_FAULT_SIGBUS: Bad access 1231 * @VM_FAULT_MAJOR: Page read from storage 1232 * @VM_FAULT_HWPOISON: Hit poisoned small page 1233 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded 1234 * in upper bits 1235 * @VM_FAULT_SIGSEGV: segmentation fault 1236 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page 1237 * @VM_FAULT_LOCKED: ->fault locked the returned page 1238 * @VM_FAULT_RETRY: ->fault blocked, must retry 1239 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small 1240 * @VM_FAULT_DONE_COW: ->fault has fully handled COW 1241 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs 1242 * fsync() to complete (for synchronous page faults 1243 * in DAX) 1244 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released 1245 * @VM_FAULT_HINDEX_MASK: mask HINDEX value 1246 * 1247 */ 1248 enum vm_fault_reason { 1249 VM_FAULT_OOM = (__force vm_fault_t)0x000001, 1250 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002, 1251 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004, 1252 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010, 1253 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020, 1254 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040, 1255 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100, 1256 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200, 1257 VM_FAULT_RETRY = (__force vm_fault_t)0x000400, 1258 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800, 1259 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, 1260 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, 1261 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000, 1262 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, 1263 }; 1264 1265 /* Encode hstate index for a hwpoisoned large page */ 1266 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16)) 1267 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf) 1268 1269 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \ 1270 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \ 1271 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) 1272 1273 #define VM_FAULT_RESULT_TRACE \ 1274 { VM_FAULT_OOM, "OOM" }, \ 1275 { VM_FAULT_SIGBUS, "SIGBUS" }, \ 1276 { VM_FAULT_MAJOR, "MAJOR" }, \ 1277 { VM_FAULT_HWPOISON, "HWPOISON" }, \ 1278 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \ 1279 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \ 1280 { VM_FAULT_NOPAGE, "NOPAGE" }, \ 1281 { VM_FAULT_LOCKED, "LOCKED" }, \ 1282 { VM_FAULT_RETRY, "RETRY" }, \ 1283 { VM_FAULT_FALLBACK, "FALLBACK" }, \ 1284 { VM_FAULT_DONE_COW, "DONE_COW" }, \ 1285 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ 1286 { VM_FAULT_COMPLETED, "COMPLETED" } 1287 1288 struct vm_special_mapping { 1289 const char *name; /* The name, e.g. "[vdso]". */ 1290 1291 /* 1292 * If .fault is not provided, this points to a 1293 * NULL-terminated array of pages that back the special mapping. 1294 * 1295 * This must not be NULL unless .fault is provided. 1296 */ 1297 struct page **pages; 1298 1299 /* 1300 * If non-NULL, then this is called to resolve page faults 1301 * on the special mapping. If used, .pages is not checked. 1302 */ 1303 vm_fault_t (*fault)(const struct vm_special_mapping *sm, 1304 struct vm_area_struct *vma, 1305 struct vm_fault *vmf); 1306 1307 int (*mremap)(const struct vm_special_mapping *sm, 1308 struct vm_area_struct *new_vma); 1309 1310 void (*close)(const struct vm_special_mapping *sm, 1311 struct vm_area_struct *vma); 1312 }; 1313 1314 enum tlb_flush_reason { 1315 TLB_FLUSH_ON_TASK_SWITCH, 1316 TLB_REMOTE_SHOOTDOWN, 1317 TLB_LOCAL_SHOOTDOWN, 1318 TLB_LOCAL_MM_SHOOTDOWN, 1319 TLB_REMOTE_SEND_IPI, 1320 NR_TLB_FLUSH_REASONS, 1321 }; 1322 1323 /** 1324 * enum fault_flag - Fault flag definitions. 1325 * @FAULT_FLAG_WRITE: Fault was a write fault. 1326 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE. 1327 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked. 1328 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying. 1329 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region. 1330 * @FAULT_FLAG_TRIED: The fault has been tried once. 1331 * @FAULT_FLAG_USER: The fault originated in userspace. 1332 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm. 1333 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. 1334 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. 1335 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a 1336 * COW mapping, making sure that an exclusive anon page is 1337 * mapped after the fault. 1338 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. 1339 * We should only access orig_pte if this flag set. 1340 * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock. 1341 * 1342 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify 1343 * whether we would allow page faults to retry by specifying these two 1344 * fault flags correctly. Currently there can be three legal combinations: 1345 * 1346 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and 1347 * this is the first try 1348 * 1349 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and 1350 * we've already tried at least once 1351 * 1352 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry 1353 * 1354 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never 1355 * be used. Note that page faults can be allowed to retry for multiple times, 1356 * in which case we'll have an initial fault with flags (a) then later on 1357 * continuous faults with flags (b). We should always try to detect pending 1358 * signals before a retry to make sure the continuous page faults can still be 1359 * interrupted if necessary. 1360 * 1361 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal. 1362 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when 1363 * applied to mappings that are not COW mappings. 1364 */ 1365 enum fault_flag { 1366 FAULT_FLAG_WRITE = 1 << 0, 1367 FAULT_FLAG_MKWRITE = 1 << 1, 1368 FAULT_FLAG_ALLOW_RETRY = 1 << 2, 1369 FAULT_FLAG_RETRY_NOWAIT = 1 << 3, 1370 FAULT_FLAG_KILLABLE = 1 << 4, 1371 FAULT_FLAG_TRIED = 1 << 5, 1372 FAULT_FLAG_USER = 1 << 6, 1373 FAULT_FLAG_REMOTE = 1 << 7, 1374 FAULT_FLAG_INSTRUCTION = 1 << 8, 1375 FAULT_FLAG_INTERRUPTIBLE = 1 << 9, 1376 FAULT_FLAG_UNSHARE = 1 << 10, 1377 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11, 1378 FAULT_FLAG_VMA_LOCK = 1 << 12, 1379 }; 1380 1381 typedef unsigned int __bitwise zap_flags_t; 1382 1383 /* Flags for clear_young_dirty_ptes(). */ 1384 typedef int __bitwise cydp_t; 1385 1386 /* Clear the access bit */ 1387 #define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0)) 1388 1389 /* Clear the dirty bit */ 1390 #define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1)) 1391 1392 /* 1393 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each 1394 * other. Here is what they mean, and how to use them: 1395 * 1396 * 1397 * FIXME: For pages which are part of a filesystem, mappings are subject to the 1398 * lifetime enforced by the filesystem and we need guarantees that longterm 1399 * users like RDMA and V4L2 only establish mappings which coordinate usage with 1400 * the filesystem. Ideas for this coordination include revoking the longterm 1401 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was 1402 * added after the problem with filesystems was found FS DAX VMAs are 1403 * specifically failed. Filesystem pages are still subject to bugs and use of 1404 * FOLL_LONGTERM should be avoided on those pages. 1405 * 1406 * In the CMA case: long term pins in a CMA region would unnecessarily fragment 1407 * that region. And so, CMA attempts to migrate the page before pinning, when 1408 * FOLL_LONGTERM is specified. 1409 * 1410 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount, 1411 * but an additional pin counting system) will be invoked. This is intended for 1412 * anything that gets a page reference and then touches page data (for example, 1413 * Direct IO). This lets the filesystem know that some non-file-system entity is 1414 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages 1415 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by 1416 * a call to unpin_user_page(). 1417 * 1418 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different 1419 * and separate refcounting mechanisms, however, and that means that each has 1420 * its own acquire and release mechanisms: 1421 * 1422 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release. 1423 * 1424 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release. 1425 * 1426 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call. 1427 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based 1428 * calls applied to them, and that's perfectly OK. This is a constraint on the 1429 * callers, not on the pages.) 1430 * 1431 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never 1432 * directly by the caller. That's in order to help avoid mismatches when 1433 * releasing pages: get_user_pages*() pages must be released via put_page(), 1434 * while pin_user_pages*() pages must be released via unpin_user_page(). 1435 * 1436 * Please see Documentation/core-api/pin_user_pages.rst for more information. 1437 */ 1438 1439 enum { 1440 /* check pte is writable */ 1441 FOLL_WRITE = 1 << 0, 1442 /* do get_page on page */ 1443 FOLL_GET = 1 << 1, 1444 /* give error on hole if it would be zero */ 1445 FOLL_DUMP = 1 << 2, 1446 /* get_user_pages read/write w/o permission */ 1447 FOLL_FORCE = 1 << 3, 1448 /* 1449 * if a disk transfer is needed, start the IO and return without waiting 1450 * upon it 1451 */ 1452 FOLL_NOWAIT = 1 << 4, 1453 /* do not fault in pages */ 1454 FOLL_NOFAULT = 1 << 5, 1455 /* check page is hwpoisoned */ 1456 FOLL_HWPOISON = 1 << 6, 1457 /* don't do file mappings */ 1458 FOLL_ANON = 1 << 7, 1459 /* 1460 * FOLL_LONGTERM indicates that the page will be held for an indefinite 1461 * time period _often_ under userspace control. This is in contrast to 1462 * iov_iter_get_pages(), whose usages are transient. 1463 */ 1464 FOLL_LONGTERM = 1 << 8, 1465 /* split huge pmd before returning */ 1466 FOLL_SPLIT_PMD = 1 << 9, 1467 /* allow returning PCI P2PDMA pages */ 1468 FOLL_PCI_P2PDMA = 1 << 10, 1469 /* allow interrupts from generic signals */ 1470 FOLL_INTERRUPTIBLE = 1 << 11, 1471 /* 1472 * Always honor (trigger) NUMA hinting faults. 1473 * 1474 * FOLL_WRITE implicitly honors NUMA hinting faults because a 1475 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE 1476 * apply). get_user_pages_fast_only() always implicitly honors NUMA 1477 * hinting faults. 1478 */ 1479 FOLL_HONOR_NUMA_FAULT = 1 << 12, 1480 1481 /* See also internal only FOLL flags in mm/internal.h */ 1482 }; 1483 1484 #endif /* _LINUX_MM_TYPES_H */ 1485