at v6.14 53 kB view raw
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 32struct address_space; 33struct 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 72struct 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 */ 235struct 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 251static __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 257static inline unsigned long encoded_page_flags(struct encoded_page *page) 258{ 259 return ENCODED_PAGE_BITS & (unsigned long)page; 260} 261 262static inline struct page *encoded_page_ptr(struct encoded_page *page) 263{ 264 return (struct page *)(~ENCODED_PAGE_BITS & (unsigned long)page); 265} 266 267static __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 273static __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 */ 282typedef 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 */ 324struct 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)) 404FOLIO_MATCH(flags, flags); 405FOLIO_MATCH(lru, lru); 406FOLIO_MATCH(mapping, mapping); 407FOLIO_MATCH(compound_head, lru); 408FOLIO_MATCH(index, index); 409FOLIO_MATCH(private, private); 410FOLIO_MATCH(_mapcount, _mapcount); 411FOLIO_MATCH(_refcount, _refcount); 412#ifdef CONFIG_MEMCG 413FOLIO_MATCH(memcg_data, memcg_data); 414#endif 415#if defined(WANT_PAGE_VIRTUAL) 416FOLIO_MATCH(virtual, virtual); 417#endif 418#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 419FOLIO_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)) 425FOLIO_MATCH(flags, _flags_1); 426FOLIO_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)) 431FOLIO_MATCH(flags, _flags_2); 432FOLIO_MATCH(compound_head, _head_2); 433FOLIO_MATCH(flags, _flags_2a); 434FOLIO_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 gmap shadow pages 442 * (which are not linked into the user page tables) and x86 443 * pgds. 444 * @_pt_pad_1: Padding that aliases with page's compound head. 445 * @pmd_huge_pte: Protected by ptdesc->ptl, used for THPs. 446 * @__page_mapping: Aliases with page->mapping. Unused for page tables. 447 * @pt_index: Used for s390 gmap. 448 * @pt_mm: Used for x86 pgds. 449 * @pt_frag_refcount: For fragmented page table tracking. Powerpc only. 450 * @pt_share_count: Used for HugeTLB PMD page table share count. 451 * @_pt_pad_2: Padding to ensure proper alignment. 452 * @ptl: Lock for the page table. 453 * @__page_type: Same as page->page_type. Unused for page tables. 454 * @__page_refcount: Same as page refcount. 455 * @pt_memcg_data: Memcg data. Tracked for page tables here. 456 * 457 * This struct overlays struct page for now. Do not modify without a good 458 * understanding of the issues. 459 */ 460struct ptdesc { 461 unsigned long __page_flags; 462 463 union { 464 struct rcu_head pt_rcu_head; 465 struct list_head pt_list; 466 struct { 467 unsigned long _pt_pad_1; 468 pgtable_t pmd_huge_pte; 469 }; 470 }; 471 unsigned long __page_mapping; 472 473 union { 474 pgoff_t pt_index; 475 struct mm_struct *pt_mm; 476 atomic_t pt_frag_refcount; 477#ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 478 atomic_t pt_share_count; 479#endif 480 }; 481 482 union { 483 unsigned long _pt_pad_2; 484#if ALLOC_SPLIT_PTLOCKS 485 spinlock_t *ptl; 486#else 487 spinlock_t ptl; 488#endif 489 }; 490 unsigned int __page_type; 491 atomic_t __page_refcount; 492#ifdef CONFIG_MEMCG 493 unsigned long pt_memcg_data; 494#endif 495}; 496 497#define TABLE_MATCH(pg, pt) \ 498 static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt)) 499TABLE_MATCH(flags, __page_flags); 500TABLE_MATCH(compound_head, pt_list); 501TABLE_MATCH(compound_head, _pt_pad_1); 502TABLE_MATCH(mapping, __page_mapping); 503TABLE_MATCH(index, pt_index); 504TABLE_MATCH(rcu_head, pt_rcu_head); 505TABLE_MATCH(page_type, __page_type); 506TABLE_MATCH(_refcount, __page_refcount); 507#ifdef CONFIG_MEMCG 508TABLE_MATCH(memcg_data, pt_memcg_data); 509#endif 510#undef TABLE_MATCH 511static_assert(sizeof(struct ptdesc) <= sizeof(struct page)); 512 513#define ptdesc_page(pt) (_Generic((pt), \ 514 const struct ptdesc *: (const struct page *)(pt), \ 515 struct ptdesc *: (struct page *)(pt))) 516 517#define ptdesc_folio(pt) (_Generic((pt), \ 518 const struct ptdesc *: (const struct folio *)(pt), \ 519 struct ptdesc *: (struct folio *)(pt))) 520 521#define page_ptdesc(p) (_Generic((p), \ 522 const struct page *: (const struct ptdesc *)(p), \ 523 struct page *: (struct ptdesc *)(p))) 524 525#ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 526static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc) 527{ 528 atomic_set(&ptdesc->pt_share_count, 0); 529} 530 531static inline void ptdesc_pmd_pts_inc(struct ptdesc *ptdesc) 532{ 533 atomic_inc(&ptdesc->pt_share_count); 534} 535 536static inline void ptdesc_pmd_pts_dec(struct ptdesc *ptdesc) 537{ 538 atomic_dec(&ptdesc->pt_share_count); 539} 540 541static inline int ptdesc_pmd_pts_count(struct ptdesc *ptdesc) 542{ 543 return atomic_read(&ptdesc->pt_share_count); 544} 545#else 546static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc) 547{ 548} 549#endif 550 551/* 552 * Used for sizing the vmemmap region on some architectures 553 */ 554#define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) 555 556/* 557 * page_private can be used on tail pages. However, PagePrivate is only 558 * checked by the VM on the head page. So page_private on the tail pages 559 * should be used for data that's ancillary to the head page (eg attaching 560 * buffer heads to tail pages after attaching buffer heads to the head page) 561 */ 562#define page_private(page) ((page)->private) 563 564static inline void set_page_private(struct page *page, unsigned long private) 565{ 566 page->private = private; 567} 568 569static inline void *folio_get_private(struct folio *folio) 570{ 571 return folio->private; 572} 573 574typedef unsigned long vm_flags_t; 575 576/* 577 * A region containing a mapping of a non-memory backed file under NOMMU 578 * conditions. These are held in a global tree and are pinned by the VMAs that 579 * map parts of them. 580 */ 581struct vm_region { 582 struct rb_node vm_rb; /* link in global region tree */ 583 vm_flags_t vm_flags; /* VMA vm_flags */ 584 unsigned long vm_start; /* start address of region */ 585 unsigned long vm_end; /* region initialised to here */ 586 unsigned long vm_top; /* region allocated to here */ 587 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */ 588 struct file *vm_file; /* the backing file or NULL */ 589 590 int vm_usage; /* region usage count (access under nommu_region_sem) */ 591 bool vm_icache_flushed : 1; /* true if the icache has been flushed for 592 * this region */ 593}; 594 595#ifdef CONFIG_USERFAULTFD 596#define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, }) 597struct vm_userfaultfd_ctx { 598 struct userfaultfd_ctx *ctx; 599}; 600#else /* CONFIG_USERFAULTFD */ 601#define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {}) 602struct vm_userfaultfd_ctx {}; 603#endif /* CONFIG_USERFAULTFD */ 604 605struct anon_vma_name { 606 struct kref kref; 607 /* The name needs to be at the end because it is dynamically sized. */ 608 char name[]; 609}; 610 611#ifdef CONFIG_ANON_VMA_NAME 612/* 613 * mmap_lock should be read-locked when calling anon_vma_name(). Caller should 614 * either keep holding the lock while using the returned pointer or it should 615 * raise anon_vma_name refcount before releasing the lock. 616 */ 617struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma); 618struct anon_vma_name *anon_vma_name_alloc(const char *name); 619void anon_vma_name_free(struct kref *kref); 620#else /* CONFIG_ANON_VMA_NAME */ 621static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma) 622{ 623 return NULL; 624} 625 626static inline struct anon_vma_name *anon_vma_name_alloc(const char *name) 627{ 628 return NULL; 629} 630#endif 631 632struct vma_lock { 633 struct rw_semaphore lock; 634}; 635 636struct vma_numab_state { 637 /* 638 * Initialised as time in 'jiffies' after which VMA 639 * should be scanned. Delays first scan of new VMA by at 640 * least sysctl_numa_balancing_scan_delay: 641 */ 642 unsigned long next_scan; 643 644 /* 645 * Time in jiffies when pids_active[] is reset to 646 * detect phase change behaviour: 647 */ 648 unsigned long pids_active_reset; 649 650 /* 651 * Approximate tracking of PIDs that trapped a NUMA hinting 652 * fault. May produce false positives due to hash collisions. 653 * 654 * [0] Previous PID tracking 655 * [1] Current PID tracking 656 * 657 * Window moves after next_pid_reset has expired approximately 658 * every VMA_PID_RESET_PERIOD jiffies: 659 */ 660 unsigned long pids_active[2]; 661 662 /* MM scan sequence ID when scan first started after VMA creation */ 663 int start_scan_seq; 664 665 /* 666 * MM scan sequence ID when the VMA was last completely scanned. 667 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq 668 */ 669 int prev_scan_seq; 670}; 671 672/* 673 * This struct describes a virtual memory area. There is one of these 674 * per VM-area/task. A VM area is any part of the process virtual memory 675 * space that has a special rule for the page-fault handlers (ie a shared 676 * library, the executable area etc). 677 * 678 * Only explicitly marked struct members may be accessed by RCU readers before 679 * getting a stable reference. 680 */ 681struct vm_area_struct { 682 /* The first cache line has the info for VMA tree walking. */ 683 684 union { 685 struct { 686 /* VMA covers [vm_start; vm_end) addresses within mm */ 687 unsigned long vm_start; 688 unsigned long vm_end; 689 }; 690#ifdef CONFIG_PER_VMA_LOCK 691 struct rcu_head vm_rcu; /* Used for deferred freeing. */ 692#endif 693 }; 694 695 /* 696 * The address space we belong to. 697 * Unstable RCU readers are allowed to read this. 698 */ 699 struct mm_struct *vm_mm; 700 pgprot_t vm_page_prot; /* Access permissions of this VMA. */ 701 702 /* 703 * Flags, see mm.h. 704 * To modify use vm_flags_{init|reset|set|clear|mod} functions. 705 */ 706 union { 707 const vm_flags_t vm_flags; 708 vm_flags_t __private __vm_flags; 709 }; 710 711#ifdef CONFIG_PER_VMA_LOCK 712 /* 713 * Flag to indicate areas detached from the mm->mm_mt tree. 714 * Unstable RCU readers are allowed to read this. 715 */ 716 bool detached; 717 718 /* 719 * Can only be written (using WRITE_ONCE()) while holding both: 720 * - mmap_lock (in write mode) 721 * - vm_lock->lock (in write mode) 722 * Can be read reliably while holding one of: 723 * - mmap_lock (in read or write mode) 724 * - vm_lock->lock (in read or write mode) 725 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout 726 * while holding nothing (except RCU to keep the VMA struct allocated). 727 * 728 * This sequence counter is explicitly allowed to overflow; sequence 729 * counter reuse can only lead to occasional unnecessary use of the 730 * slowpath. 731 */ 732 unsigned int vm_lock_seq; 733 /* Unstable RCU readers are allowed to read this. */ 734 struct vma_lock *vm_lock; 735#endif 736 737 /* 738 * For areas with an address space and backing store, 739 * linkage into the address_space->i_mmap interval tree. 740 * 741 */ 742 struct { 743 struct rb_node rb; 744 unsigned long rb_subtree_last; 745 } shared; 746 747 /* 748 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma 749 * list, after a COW of one of the file pages. A MAP_SHARED vma 750 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack 751 * or brk vma (with NULL file) can only be in an anon_vma list. 752 */ 753 struct list_head anon_vma_chain; /* Serialized by mmap_lock & 754 * page_table_lock */ 755 struct anon_vma *anon_vma; /* Serialized by page_table_lock */ 756 757 /* Function pointers to deal with this struct. */ 758 const struct vm_operations_struct *vm_ops; 759 760 /* Information about our backing store: */ 761 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE 762 units */ 763 struct file * vm_file; /* File we map to (can be NULL). */ 764 void * vm_private_data; /* was vm_pte (shared mem) */ 765 766#ifdef CONFIG_ANON_VMA_NAME 767 /* 768 * For private and shared anonymous mappings, a pointer to a null 769 * terminated string containing the name given to the vma, or NULL if 770 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access. 771 */ 772 struct anon_vma_name *anon_name; 773#endif 774#ifdef CONFIG_SWAP 775 atomic_long_t swap_readahead_info; 776#endif 777#ifndef CONFIG_MMU 778 struct vm_region *vm_region; /* NOMMU mapping region */ 779#endif 780#ifdef CONFIG_NUMA 781 struct mempolicy *vm_policy; /* NUMA policy for the VMA */ 782#endif 783#ifdef CONFIG_NUMA_BALANCING 784 struct vma_numab_state *numab_state; /* NUMA Balancing state */ 785#endif 786 struct vm_userfaultfd_ctx vm_userfaultfd_ctx; 787} __randomize_layout; 788 789#ifdef CONFIG_NUMA 790#define vma_policy(vma) ((vma)->vm_policy) 791#else 792#define vma_policy(vma) NULL 793#endif 794 795#ifdef CONFIG_SCHED_MM_CID 796struct mm_cid { 797 u64 time; 798 int cid; 799 int recent_cid; 800}; 801#endif 802 803struct kioctx_table; 804struct iommu_mm_data; 805struct mm_struct { 806 struct { 807 /* 808 * Fields which are often written to are placed in a separate 809 * cache line. 810 */ 811 struct { 812 /** 813 * @mm_count: The number of references to &struct 814 * mm_struct (@mm_users count as 1). 815 * 816 * Use mmgrab()/mmdrop() to modify. When this drops to 817 * 0, the &struct mm_struct is freed. 818 */ 819 atomic_t mm_count; 820 } ____cacheline_aligned_in_smp; 821 822 struct maple_tree mm_mt; 823 824 unsigned long mmap_base; /* base of mmap area */ 825 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */ 826#ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES 827 /* Base addresses for compatible mmap() */ 828 unsigned long mmap_compat_base; 829 unsigned long mmap_compat_legacy_base; 830#endif 831 unsigned long task_size; /* size of task vm space */ 832 pgd_t * pgd; 833 834#ifdef CONFIG_MEMBARRIER 835 /** 836 * @membarrier_state: Flags controlling membarrier behavior. 837 * 838 * This field is close to @pgd to hopefully fit in the same 839 * cache-line, which needs to be touched by switch_mm(). 840 */ 841 atomic_t membarrier_state; 842#endif 843 844 /** 845 * @mm_users: The number of users including userspace. 846 * 847 * Use mmget()/mmget_not_zero()/mmput() to modify. When this 848 * drops to 0 (i.e. when the task exits and there are no other 849 * temporary reference holders), we also release a reference on 850 * @mm_count (which may then free the &struct mm_struct if 851 * @mm_count also drops to 0). 852 */ 853 atomic_t mm_users; 854 855#ifdef CONFIG_SCHED_MM_CID 856 /** 857 * @pcpu_cid: Per-cpu current cid. 858 * 859 * Keep track of the currently allocated mm_cid for each cpu. 860 * The per-cpu mm_cid values are serialized by their respective 861 * runqueue locks. 862 */ 863 struct mm_cid __percpu *pcpu_cid; 864 /* 865 * @mm_cid_next_scan: Next mm_cid scan (in jiffies). 866 * 867 * When the next mm_cid scan is due (in jiffies). 868 */ 869 unsigned long mm_cid_next_scan; 870 /** 871 * @nr_cpus_allowed: Number of CPUs allowed for mm. 872 * 873 * Number of CPUs allowed in the union of all mm's 874 * threads allowed CPUs. 875 */ 876 unsigned int nr_cpus_allowed; 877 /** 878 * @max_nr_cid: Maximum number of allowed concurrency 879 * IDs allocated. 880 * 881 * Track the highest number of allowed concurrency IDs 882 * allocated for the mm. 883 */ 884 atomic_t max_nr_cid; 885 /** 886 * @cpus_allowed_lock: Lock protecting mm cpus_allowed. 887 * 888 * Provide mutual exclusion for mm cpus_allowed and 889 * mm nr_cpus_allowed updates. 890 */ 891 raw_spinlock_t cpus_allowed_lock; 892#endif 893#ifdef CONFIG_MMU 894 atomic_long_t pgtables_bytes; /* size of all page tables */ 895#endif 896 int map_count; /* number of VMAs */ 897 898 spinlock_t page_table_lock; /* Protects page tables and some 899 * counters 900 */ 901 /* 902 * With some kernel config, the current mmap_lock's offset 903 * inside 'mm_struct' is at 0x120, which is very optimal, as 904 * its two hot fields 'count' and 'owner' sit in 2 different 905 * cachelines, and when mmap_lock is highly contended, both 906 * of the 2 fields will be accessed frequently, current layout 907 * will help to reduce cache bouncing. 908 * 909 * So please be careful with adding new fields before 910 * mmap_lock, which can easily push the 2 fields into one 911 * cacheline. 912 */ 913 struct rw_semaphore mmap_lock; 914 915 struct list_head mmlist; /* List of maybe swapped mm's. These 916 * are globally strung together off 917 * init_mm.mmlist, and are protected 918 * by mmlist_lock 919 */ 920#ifdef CONFIG_PER_VMA_LOCK 921 /* 922 * This field has lock-like semantics, meaning it is sometimes 923 * accessed with ACQUIRE/RELEASE semantics. 924 * Roughly speaking, incrementing the sequence number is 925 * equivalent to releasing locks on VMAs; reading the sequence 926 * number can be part of taking a read lock on a VMA. 927 * Incremented every time mmap_lock is write-locked/unlocked. 928 * Initialized to 0, therefore odd values indicate mmap_lock 929 * is write-locked and even values that it's released. 930 * 931 * Can be modified under write mmap_lock using RELEASE 932 * semantics. 933 * Can be read with no other protection when holding write 934 * mmap_lock. 935 * Can be read with ACQUIRE semantics if not holding write 936 * mmap_lock. 937 */ 938 seqcount_t mm_lock_seq; 939#endif 940 941 942 unsigned long hiwater_rss; /* High-watermark of RSS usage */ 943 unsigned long hiwater_vm; /* High-water virtual memory usage */ 944 945 unsigned long total_vm; /* Total pages mapped */ 946 unsigned long locked_vm; /* Pages that have PG_mlocked set */ 947 atomic64_t pinned_vm; /* Refcount permanently increased */ 948 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */ 949 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */ 950 unsigned long stack_vm; /* VM_STACK */ 951 unsigned long def_flags; 952 953 /** 954 * @write_protect_seq: Locked when any thread is write 955 * protecting pages mapped by this mm to enforce a later COW, 956 * for instance during page table copying for fork(). 957 */ 958 seqcount_t write_protect_seq; 959 960 spinlock_t arg_lock; /* protect the below fields */ 961 962 unsigned long start_code, end_code, start_data, end_data; 963 unsigned long start_brk, brk, start_stack; 964 unsigned long arg_start, arg_end, env_start, env_end; 965 966 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */ 967 968 struct percpu_counter rss_stat[NR_MM_COUNTERS]; 969 970 struct linux_binfmt *binfmt; 971 972 /* Architecture-specific MM context */ 973 mm_context_t context; 974 975 unsigned long flags; /* Must use atomic bitops to access */ 976 977#ifdef CONFIG_AIO 978 spinlock_t ioctx_lock; 979 struct kioctx_table __rcu *ioctx_table; 980#endif 981#ifdef CONFIG_MEMCG 982 /* 983 * "owner" points to a task that is regarded as the canonical 984 * user/owner of this mm. All of the following must be true in 985 * order for it to be changed: 986 * 987 * current == mm->owner 988 * current->mm != mm 989 * new_owner->mm == mm 990 * new_owner->alloc_lock is held 991 */ 992 struct task_struct __rcu *owner; 993#endif 994 struct user_namespace *user_ns; 995 996 /* store ref to file /proc/<pid>/exe symlink points to */ 997 struct file __rcu *exe_file; 998#ifdef CONFIG_MMU_NOTIFIER 999 struct mmu_notifier_subscriptions *notifier_subscriptions; 1000#endif 1001#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS) 1002 pgtable_t pmd_huge_pte; /* protected by page_table_lock */ 1003#endif 1004#ifdef CONFIG_NUMA_BALANCING 1005 /* 1006 * numa_next_scan is the next time that PTEs will be remapped 1007 * PROT_NONE to trigger NUMA hinting faults; such faults gather 1008 * statistics and migrate pages to new nodes if necessary. 1009 */ 1010 unsigned long numa_next_scan; 1011 1012 /* Restart point for scanning and remapping PTEs. */ 1013 unsigned long numa_scan_offset; 1014 1015 /* numa_scan_seq prevents two threads remapping PTEs. */ 1016 int numa_scan_seq; 1017#endif 1018 /* 1019 * An operation with batched TLB flushing is going on. Anything 1020 * that can move process memory needs to flush the TLB when 1021 * moving a PROT_NONE mapped page. 1022 */ 1023 atomic_t tlb_flush_pending; 1024#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1025 /* See flush_tlb_batched_pending() */ 1026 atomic_t tlb_flush_batched; 1027#endif 1028 struct uprobes_state uprobes_state; 1029#ifdef CONFIG_PREEMPT_RT 1030 struct rcu_head delayed_drop; 1031#endif 1032#ifdef CONFIG_HUGETLB_PAGE 1033 atomic_long_t hugetlb_usage; 1034#endif 1035 struct work_struct async_put_work; 1036 1037#ifdef CONFIG_IOMMU_MM_DATA 1038 struct iommu_mm_data *iommu_mm; 1039#endif 1040#ifdef CONFIG_KSM 1041 /* 1042 * Represent how many pages of this process are involved in KSM 1043 * merging (not including ksm_zero_pages). 1044 */ 1045 unsigned long ksm_merging_pages; 1046 /* 1047 * Represent how many pages are checked for ksm merging 1048 * including merged and not merged. 1049 */ 1050 unsigned long ksm_rmap_items; 1051 /* 1052 * Represent how many empty pages are merged with kernel zero 1053 * pages when enabling KSM use_zero_pages. 1054 */ 1055 atomic_long_t ksm_zero_pages; 1056#endif /* CONFIG_KSM */ 1057#ifdef CONFIG_LRU_GEN_WALKS_MMU 1058 struct { 1059 /* this mm_struct is on lru_gen_mm_list */ 1060 struct list_head list; 1061 /* 1062 * Set when switching to this mm_struct, as a hint of 1063 * whether it has been used since the last time per-node 1064 * page table walkers cleared the corresponding bits. 1065 */ 1066 unsigned long bitmap; 1067#ifdef CONFIG_MEMCG 1068 /* points to the memcg of "owner" above */ 1069 struct mem_cgroup *memcg; 1070#endif 1071 } lru_gen; 1072#endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1073 } __randomize_layout; 1074 1075 /* 1076 * The mm_cpumask needs to be at the end of mm_struct, because it 1077 * is dynamically sized based on nr_cpu_ids. 1078 */ 1079 unsigned long cpu_bitmap[]; 1080}; 1081 1082#define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \ 1083 MT_FLAGS_USE_RCU) 1084extern struct mm_struct init_mm; 1085 1086/* Pointer magic because the dynamic array size confuses some compilers. */ 1087static inline void mm_init_cpumask(struct mm_struct *mm) 1088{ 1089 unsigned long cpu_bitmap = (unsigned long)mm; 1090 1091 cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap); 1092 cpumask_clear((struct cpumask *)cpu_bitmap); 1093} 1094 1095/* Future-safe accessor for struct mm_struct's cpu_vm_mask. */ 1096static inline cpumask_t *mm_cpumask(struct mm_struct *mm) 1097{ 1098 return (struct cpumask *)&mm->cpu_bitmap; 1099} 1100 1101#ifdef CONFIG_LRU_GEN 1102 1103struct lru_gen_mm_list { 1104 /* mm_struct list for page table walkers */ 1105 struct list_head fifo; 1106 /* protects the list above */ 1107 spinlock_t lock; 1108}; 1109 1110#endif /* CONFIG_LRU_GEN */ 1111 1112#ifdef CONFIG_LRU_GEN_WALKS_MMU 1113 1114void lru_gen_add_mm(struct mm_struct *mm); 1115void lru_gen_del_mm(struct mm_struct *mm); 1116void lru_gen_migrate_mm(struct mm_struct *mm); 1117 1118static inline void lru_gen_init_mm(struct mm_struct *mm) 1119{ 1120 INIT_LIST_HEAD(&mm->lru_gen.list); 1121 mm->lru_gen.bitmap = 0; 1122#ifdef CONFIG_MEMCG 1123 mm->lru_gen.memcg = NULL; 1124#endif 1125} 1126 1127static inline void lru_gen_use_mm(struct mm_struct *mm) 1128{ 1129 /* 1130 * When the bitmap is set, page reclaim knows this mm_struct has been 1131 * used since the last time it cleared the bitmap. So it might be worth 1132 * walking the page tables of this mm_struct to clear the accessed bit. 1133 */ 1134 WRITE_ONCE(mm->lru_gen.bitmap, -1); 1135} 1136 1137#else /* !CONFIG_LRU_GEN_WALKS_MMU */ 1138 1139static inline void lru_gen_add_mm(struct mm_struct *mm) 1140{ 1141} 1142 1143static inline void lru_gen_del_mm(struct mm_struct *mm) 1144{ 1145} 1146 1147static inline void lru_gen_migrate_mm(struct mm_struct *mm) 1148{ 1149} 1150 1151static inline void lru_gen_init_mm(struct mm_struct *mm) 1152{ 1153} 1154 1155static inline void lru_gen_use_mm(struct mm_struct *mm) 1156{ 1157} 1158 1159#endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1160 1161struct vma_iterator { 1162 struct ma_state mas; 1163}; 1164 1165#define VMA_ITERATOR(name, __mm, __addr) \ 1166 struct vma_iterator name = { \ 1167 .mas = { \ 1168 .tree = &(__mm)->mm_mt, \ 1169 .index = __addr, \ 1170 .node = NULL, \ 1171 .status = ma_start, \ 1172 }, \ 1173 } 1174 1175static inline void vma_iter_init(struct vma_iterator *vmi, 1176 struct mm_struct *mm, unsigned long addr) 1177{ 1178 mas_init(&vmi->mas, &mm->mm_mt, addr); 1179} 1180 1181#ifdef CONFIG_SCHED_MM_CID 1182 1183enum mm_cid_state { 1184 MM_CID_UNSET = -1U, /* Unset state has lazy_put flag set. */ 1185 MM_CID_LAZY_PUT = (1U << 31), 1186}; 1187 1188static inline bool mm_cid_is_unset(int cid) 1189{ 1190 return cid == MM_CID_UNSET; 1191} 1192 1193static inline bool mm_cid_is_lazy_put(int cid) 1194{ 1195 return !mm_cid_is_unset(cid) && (cid & MM_CID_LAZY_PUT); 1196} 1197 1198static inline bool mm_cid_is_valid(int cid) 1199{ 1200 return !(cid & MM_CID_LAZY_PUT); 1201} 1202 1203static inline int mm_cid_set_lazy_put(int cid) 1204{ 1205 return cid | MM_CID_LAZY_PUT; 1206} 1207 1208static inline int mm_cid_clear_lazy_put(int cid) 1209{ 1210 return cid & ~MM_CID_LAZY_PUT; 1211} 1212 1213/* 1214 * mm_cpus_allowed: Union of all mm's threads allowed CPUs. 1215 */ 1216static inline cpumask_t *mm_cpus_allowed(struct mm_struct *mm) 1217{ 1218 unsigned long bitmap = (unsigned long)mm; 1219 1220 bitmap += offsetof(struct mm_struct, cpu_bitmap); 1221 /* Skip cpu_bitmap */ 1222 bitmap += cpumask_size(); 1223 return (struct cpumask *)bitmap; 1224} 1225 1226/* Accessor for struct mm_struct's cidmask. */ 1227static inline cpumask_t *mm_cidmask(struct mm_struct *mm) 1228{ 1229 unsigned long cid_bitmap = (unsigned long)mm_cpus_allowed(mm); 1230 1231 /* Skip mm_cpus_allowed */ 1232 cid_bitmap += cpumask_size(); 1233 return (struct cpumask *)cid_bitmap; 1234} 1235 1236static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) 1237{ 1238 int i; 1239 1240 for_each_possible_cpu(i) { 1241 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, i); 1242 1243 pcpu_cid->cid = MM_CID_UNSET; 1244 pcpu_cid->recent_cid = MM_CID_UNSET; 1245 pcpu_cid->time = 0; 1246 } 1247 mm->nr_cpus_allowed = p->nr_cpus_allowed; 1248 atomic_set(&mm->max_nr_cid, 0); 1249 raw_spin_lock_init(&mm->cpus_allowed_lock); 1250 cpumask_copy(mm_cpus_allowed(mm), &p->cpus_mask); 1251 cpumask_clear(mm_cidmask(mm)); 1252} 1253 1254static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p) 1255{ 1256 mm->pcpu_cid = alloc_percpu_noprof(struct mm_cid); 1257 if (!mm->pcpu_cid) 1258 return -ENOMEM; 1259 mm_init_cid(mm, p); 1260 return 0; 1261} 1262#define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__)) 1263 1264static inline void mm_destroy_cid(struct mm_struct *mm) 1265{ 1266 free_percpu(mm->pcpu_cid); 1267 mm->pcpu_cid = NULL; 1268} 1269 1270static inline unsigned int mm_cid_size(void) 1271{ 1272 return 2 * cpumask_size(); /* mm_cpus_allowed(), mm_cidmask(). */ 1273} 1274 1275static inline void mm_set_cpus_allowed(struct mm_struct *mm, const struct cpumask *cpumask) 1276{ 1277 struct cpumask *mm_allowed = mm_cpus_allowed(mm); 1278 1279 if (!mm) 1280 return; 1281 /* The mm_cpus_allowed is the union of each thread allowed CPUs masks. */ 1282 raw_spin_lock(&mm->cpus_allowed_lock); 1283 cpumask_or(mm_allowed, mm_allowed, cpumask); 1284 WRITE_ONCE(mm->nr_cpus_allowed, cpumask_weight(mm_allowed)); 1285 raw_spin_unlock(&mm->cpus_allowed_lock); 1286} 1287#else /* CONFIG_SCHED_MM_CID */ 1288static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { } 1289static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; } 1290static inline void mm_destroy_cid(struct mm_struct *mm) { } 1291 1292static inline unsigned int mm_cid_size(void) 1293{ 1294 return 0; 1295} 1296static inline void mm_set_cpus_allowed(struct mm_struct *mm, const struct cpumask *cpumask) { } 1297#endif /* CONFIG_SCHED_MM_CID */ 1298 1299struct mmu_gather; 1300extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm); 1301extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm); 1302extern void tlb_finish_mmu(struct mmu_gather *tlb); 1303 1304struct vm_fault; 1305 1306/** 1307 * typedef vm_fault_t - Return type for page fault handlers. 1308 * 1309 * Page fault handlers return a bitmask of %VM_FAULT values. 1310 */ 1311typedef __bitwise unsigned int vm_fault_t; 1312 1313/** 1314 * enum vm_fault_reason - Page fault handlers return a bitmask of 1315 * these values to tell the core VM what happened when handling the 1316 * fault. Used to decide whether a process gets delivered SIGBUS or 1317 * just gets major/minor fault counters bumped up. 1318 * 1319 * @VM_FAULT_OOM: Out Of Memory 1320 * @VM_FAULT_SIGBUS: Bad access 1321 * @VM_FAULT_MAJOR: Page read from storage 1322 * @VM_FAULT_HWPOISON: Hit poisoned small page 1323 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded 1324 * in upper bits 1325 * @VM_FAULT_SIGSEGV: segmentation fault 1326 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page 1327 * @VM_FAULT_LOCKED: ->fault locked the returned page 1328 * @VM_FAULT_RETRY: ->fault blocked, must retry 1329 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small 1330 * @VM_FAULT_DONE_COW: ->fault has fully handled COW 1331 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs 1332 * fsync() to complete (for synchronous page faults 1333 * in DAX) 1334 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released 1335 * @VM_FAULT_HINDEX_MASK: mask HINDEX value 1336 * 1337 */ 1338enum vm_fault_reason { 1339 VM_FAULT_OOM = (__force vm_fault_t)0x000001, 1340 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002, 1341 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004, 1342 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010, 1343 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020, 1344 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040, 1345 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100, 1346 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200, 1347 VM_FAULT_RETRY = (__force vm_fault_t)0x000400, 1348 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800, 1349 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, 1350 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, 1351 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000, 1352 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, 1353}; 1354 1355/* Encode hstate index for a hwpoisoned large page */ 1356#define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16)) 1357#define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf) 1358 1359#define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \ 1360 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \ 1361 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) 1362 1363#define VM_FAULT_RESULT_TRACE \ 1364 { VM_FAULT_OOM, "OOM" }, \ 1365 { VM_FAULT_SIGBUS, "SIGBUS" }, \ 1366 { VM_FAULT_MAJOR, "MAJOR" }, \ 1367 { VM_FAULT_HWPOISON, "HWPOISON" }, \ 1368 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \ 1369 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \ 1370 { VM_FAULT_NOPAGE, "NOPAGE" }, \ 1371 { VM_FAULT_LOCKED, "LOCKED" }, \ 1372 { VM_FAULT_RETRY, "RETRY" }, \ 1373 { VM_FAULT_FALLBACK, "FALLBACK" }, \ 1374 { VM_FAULT_DONE_COW, "DONE_COW" }, \ 1375 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ 1376 { VM_FAULT_COMPLETED, "COMPLETED" } 1377 1378struct vm_special_mapping { 1379 const char *name; /* The name, e.g. "[vdso]". */ 1380 1381 /* 1382 * If .fault is not provided, this points to a 1383 * NULL-terminated array of pages that back the special mapping. 1384 * 1385 * This must not be NULL unless .fault is provided. 1386 */ 1387 struct page **pages; 1388 1389 /* 1390 * If non-NULL, then this is called to resolve page faults 1391 * on the special mapping. If used, .pages is not checked. 1392 */ 1393 vm_fault_t (*fault)(const struct vm_special_mapping *sm, 1394 struct vm_area_struct *vma, 1395 struct vm_fault *vmf); 1396 1397 int (*mremap)(const struct vm_special_mapping *sm, 1398 struct vm_area_struct *new_vma); 1399 1400 void (*close)(const struct vm_special_mapping *sm, 1401 struct vm_area_struct *vma); 1402}; 1403 1404enum tlb_flush_reason { 1405 TLB_FLUSH_ON_TASK_SWITCH, 1406 TLB_REMOTE_SHOOTDOWN, 1407 TLB_LOCAL_SHOOTDOWN, 1408 TLB_LOCAL_MM_SHOOTDOWN, 1409 TLB_REMOTE_SEND_IPI, 1410 TLB_REMOTE_WRONG_CPU, 1411 NR_TLB_FLUSH_REASONS, 1412}; 1413 1414/** 1415 * enum fault_flag - Fault flag definitions. 1416 * @FAULT_FLAG_WRITE: Fault was a write fault. 1417 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE. 1418 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked. 1419 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying. 1420 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region. 1421 * @FAULT_FLAG_TRIED: The fault has been tried once. 1422 * @FAULT_FLAG_USER: The fault originated in userspace. 1423 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm. 1424 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. 1425 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. 1426 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a 1427 * COW mapping, making sure that an exclusive anon page is 1428 * mapped after the fault. 1429 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. 1430 * We should only access orig_pte if this flag set. 1431 * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock. 1432 * 1433 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify 1434 * whether we would allow page faults to retry by specifying these two 1435 * fault flags correctly. Currently there can be three legal combinations: 1436 * 1437 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and 1438 * this is the first try 1439 * 1440 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and 1441 * we've already tried at least once 1442 * 1443 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry 1444 * 1445 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never 1446 * be used. Note that page faults can be allowed to retry for multiple times, 1447 * in which case we'll have an initial fault with flags (a) then later on 1448 * continuous faults with flags (b). We should always try to detect pending 1449 * signals before a retry to make sure the continuous page faults can still be 1450 * interrupted if necessary. 1451 * 1452 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal. 1453 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when 1454 * applied to mappings that are not COW mappings. 1455 */ 1456enum fault_flag { 1457 FAULT_FLAG_WRITE = 1 << 0, 1458 FAULT_FLAG_MKWRITE = 1 << 1, 1459 FAULT_FLAG_ALLOW_RETRY = 1 << 2, 1460 FAULT_FLAG_RETRY_NOWAIT = 1 << 3, 1461 FAULT_FLAG_KILLABLE = 1 << 4, 1462 FAULT_FLAG_TRIED = 1 << 5, 1463 FAULT_FLAG_USER = 1 << 6, 1464 FAULT_FLAG_REMOTE = 1 << 7, 1465 FAULT_FLAG_INSTRUCTION = 1 << 8, 1466 FAULT_FLAG_INTERRUPTIBLE = 1 << 9, 1467 FAULT_FLAG_UNSHARE = 1 << 10, 1468 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11, 1469 FAULT_FLAG_VMA_LOCK = 1 << 12, 1470}; 1471 1472typedef unsigned int __bitwise zap_flags_t; 1473 1474/* Flags for clear_young_dirty_ptes(). */ 1475typedef int __bitwise cydp_t; 1476 1477/* Clear the access bit */ 1478#define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0)) 1479 1480/* Clear the dirty bit */ 1481#define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1)) 1482 1483/* 1484 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each 1485 * other. Here is what they mean, and how to use them: 1486 * 1487 * 1488 * FIXME: For pages which are part of a filesystem, mappings are subject to the 1489 * lifetime enforced by the filesystem and we need guarantees that longterm 1490 * users like RDMA and V4L2 only establish mappings which coordinate usage with 1491 * the filesystem. Ideas for this coordination include revoking the longterm 1492 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was 1493 * added after the problem with filesystems was found FS DAX VMAs are 1494 * specifically failed. Filesystem pages are still subject to bugs and use of 1495 * FOLL_LONGTERM should be avoided on those pages. 1496 * 1497 * In the CMA case: long term pins in a CMA region would unnecessarily fragment 1498 * that region. And so, CMA attempts to migrate the page before pinning, when 1499 * FOLL_LONGTERM is specified. 1500 * 1501 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount, 1502 * but an additional pin counting system) will be invoked. This is intended for 1503 * anything that gets a page reference and then touches page data (for example, 1504 * Direct IO). This lets the filesystem know that some non-file-system entity is 1505 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages 1506 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by 1507 * a call to unpin_user_page(). 1508 * 1509 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different 1510 * and separate refcounting mechanisms, however, and that means that each has 1511 * its own acquire and release mechanisms: 1512 * 1513 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release. 1514 * 1515 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release. 1516 * 1517 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call. 1518 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based 1519 * calls applied to them, and that's perfectly OK. This is a constraint on the 1520 * callers, not on the pages.) 1521 * 1522 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never 1523 * directly by the caller. That's in order to help avoid mismatches when 1524 * releasing pages: get_user_pages*() pages must be released via put_page(), 1525 * while pin_user_pages*() pages must be released via unpin_user_page(). 1526 * 1527 * Please see Documentation/core-api/pin_user_pages.rst for more information. 1528 */ 1529 1530enum { 1531 /* check pte is writable */ 1532 FOLL_WRITE = 1 << 0, 1533 /* do get_page on page */ 1534 FOLL_GET = 1 << 1, 1535 /* give error on hole if it would be zero */ 1536 FOLL_DUMP = 1 << 2, 1537 /* get_user_pages read/write w/o permission */ 1538 FOLL_FORCE = 1 << 3, 1539 /* 1540 * if a disk transfer is needed, start the IO and return without waiting 1541 * upon it 1542 */ 1543 FOLL_NOWAIT = 1 << 4, 1544 /* do not fault in pages */ 1545 FOLL_NOFAULT = 1 << 5, 1546 /* check page is hwpoisoned */ 1547 FOLL_HWPOISON = 1 << 6, 1548 /* don't do file mappings */ 1549 FOLL_ANON = 1 << 7, 1550 /* 1551 * FOLL_LONGTERM indicates that the page will be held for an indefinite 1552 * time period _often_ under userspace control. This is in contrast to 1553 * iov_iter_get_pages(), whose usages are transient. 1554 */ 1555 FOLL_LONGTERM = 1 << 8, 1556 /* split huge pmd before returning */ 1557 FOLL_SPLIT_PMD = 1 << 9, 1558 /* allow returning PCI P2PDMA pages */ 1559 FOLL_PCI_P2PDMA = 1 << 10, 1560 /* allow interrupts from generic signals */ 1561 FOLL_INTERRUPTIBLE = 1 << 11, 1562 /* 1563 * Always honor (trigger) NUMA hinting faults. 1564 * 1565 * FOLL_WRITE implicitly honors NUMA hinting faults because a 1566 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE 1567 * apply). get_user_pages_fast_only() always implicitly honors NUMA 1568 * hinting faults. 1569 */ 1570 FOLL_HONOR_NUMA_FAULT = 1 << 12, 1571 1572 /* See also internal only FOLL flags in mm/internal.h */ 1573}; 1574 1575/* mm flags */ 1576 1577/* 1578 * The first two bits represent core dump modes for set-user-ID, 1579 * the modes are SUID_DUMP_* defined in linux/sched/coredump.h 1580 */ 1581#define MMF_DUMPABLE_BITS 2 1582#define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1) 1583/* coredump filter bits */ 1584#define MMF_DUMP_ANON_PRIVATE 2 1585#define MMF_DUMP_ANON_SHARED 3 1586#define MMF_DUMP_MAPPED_PRIVATE 4 1587#define MMF_DUMP_MAPPED_SHARED 5 1588#define MMF_DUMP_ELF_HEADERS 6 1589#define MMF_DUMP_HUGETLB_PRIVATE 7 1590#define MMF_DUMP_HUGETLB_SHARED 8 1591#define MMF_DUMP_DAX_PRIVATE 9 1592#define MMF_DUMP_DAX_SHARED 10 1593 1594#define MMF_DUMP_FILTER_SHIFT MMF_DUMPABLE_BITS 1595#define MMF_DUMP_FILTER_BITS 9 1596#define MMF_DUMP_FILTER_MASK \ 1597 (((1 << MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT) 1598#define MMF_DUMP_FILTER_DEFAULT \ 1599 ((1 << MMF_DUMP_ANON_PRIVATE) | (1 << MMF_DUMP_ANON_SHARED) |\ 1600 (1 << MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF) 1601 1602#ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS 1603# define MMF_DUMP_MASK_DEFAULT_ELF (1 << MMF_DUMP_ELF_HEADERS) 1604#else 1605# define MMF_DUMP_MASK_DEFAULT_ELF 0 1606#endif 1607 /* leave room for more dump flags */ 1608#define MMF_VM_MERGEABLE 16 /* KSM may merge identical pages */ 1609#define MMF_VM_HUGEPAGE 17 /* set when mm is available for khugepaged */ 1610 1611/* 1612 * This one-shot flag is dropped due to necessity of changing exe once again 1613 * on NFS restore 1614 */ 1615//#define MMF_EXE_FILE_CHANGED 18 /* see prctl_set_mm_exe_file() */ 1616 1617#define MMF_HAS_UPROBES 19 /* has uprobes */ 1618#define MMF_RECALC_UPROBES 20 /* MMF_HAS_UPROBES can be wrong */ 1619#define MMF_OOM_SKIP 21 /* mm is of no interest for the OOM killer */ 1620#define MMF_UNSTABLE 22 /* mm is unstable for copy_from_user */ 1621#define MMF_HUGE_ZERO_PAGE 23 /* mm has ever used the global huge zero page */ 1622#define MMF_DISABLE_THP 24 /* disable THP for all VMAs */ 1623#define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) 1624#define MMF_OOM_REAP_QUEUED 25 /* mm was queued for oom_reaper */ 1625#define MMF_MULTIPROCESS 26 /* mm is shared between processes */ 1626/* 1627 * MMF_HAS_PINNED: Whether this mm has pinned any pages. This can be either 1628 * replaced in the future by mm.pinned_vm when it becomes stable, or grow into 1629 * a counter on its own. We're aggresive on this bit for now: even if the 1630 * pinned pages were unpinned later on, we'll still keep this bit set for the 1631 * lifecycle of this mm, just for simplicity. 1632 */ 1633#define MMF_HAS_PINNED 27 /* FOLL_PIN has run, never cleared */ 1634 1635#define MMF_HAS_MDWE 28 1636#define MMF_HAS_MDWE_MASK (1 << MMF_HAS_MDWE) 1637 1638 1639#define MMF_HAS_MDWE_NO_INHERIT 29 1640 1641#define MMF_VM_MERGE_ANY 30 1642#define MMF_VM_MERGE_ANY_MASK (1 << MMF_VM_MERGE_ANY) 1643 1644#define MMF_TOPDOWN 31 /* mm searches top down by default */ 1645#define MMF_TOPDOWN_MASK (1 << MMF_TOPDOWN) 1646 1647#define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ 1648 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ 1649 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK) 1650 1651static inline unsigned long mmf_init_flags(unsigned long flags) 1652{ 1653 if (flags & (1UL << MMF_HAS_MDWE_NO_INHERIT)) 1654 flags &= ~((1UL << MMF_HAS_MDWE) | 1655 (1UL << MMF_HAS_MDWE_NO_INHERIT)); 1656 return flags & MMF_INIT_MASK; 1657} 1658 1659#endif /* _LINUX_MM_TYPES_H */