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 #include <linux/types.h> 23 #include <linux/futex_types.h> 24 #include <linux/rseq_types.h> 25 #include <linux/bitmap.h> 26 27 #include <asm/mmu.h> 28 29 #ifndef AT_VECTOR_SIZE_ARCH 30 #define AT_VECTOR_SIZE_ARCH 0 31 #endif 32 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1)) 33 34 35 struct address_space; 36 struct futex_private_hash; 37 struct mem_cgroup; 38 39 typedef struct { 40 unsigned long f; 41 } memdesc_flags_t; 42 43 /* 44 * Each physical page in the system has a struct page associated with 45 * it to keep track of whatever it is we are using the page for at the 46 * moment. Note that we have no way to track which tasks are using 47 * a page, though if it is a pagecache page, rmap structures can tell us 48 * who is mapping it. 49 * 50 * If you allocate the page using alloc_pages(), you can use some of the 51 * space in struct page for your own purposes. The five words in the main 52 * union are available, except for bit 0 of the first word which must be 53 * kept clear. Many users use this word to store a pointer to an object 54 * which is guaranteed to be aligned. If you use the same storage as 55 * page->mapping, you must restore it to NULL before freeing the page. 56 * 57 * The mapcount field must not be used for own purposes. 58 * 59 * If you want to use the refcount field, it must be used in such a way 60 * that other CPUs temporarily incrementing and then decrementing the 61 * refcount does not cause problems. On receiving the page from 62 * alloc_pages(), the refcount will be positive. 63 * 64 * If you allocate pages of order > 0, you can use some of the fields 65 * in each subpage, but you may need to restore some of their values 66 * afterwards. 67 * 68 * SLUB uses cmpxchg_double() to atomically update its freelist and counters. 69 * That requires that freelist & counters in struct slab be adjacent and 70 * double-word aligned. Because struct slab currently just reinterprets the 71 * bits of struct page, we align all struct pages to double-word boundaries, 72 * and ensure that 'freelist' is aligned within struct slab. 73 */ 74 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE 75 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long)) 76 #else 77 #define _struct_page_alignment __aligned(sizeof(unsigned long)) 78 #endif 79 80 struct page { 81 memdesc_flags_t flags; /* Atomic flags, some possibly 82 * updated asynchronously */ 83 /* 84 * Five words (20/40 bytes) are available in this union. 85 * WARNING: bit 0 of the first word is used for PageTail(). That 86 * means the other users of this union MUST NOT use the bit to 87 * avoid collision and false-positive PageTail(). 88 */ 89 union { 90 struct { /* Page cache and anonymous pages */ 91 /** 92 * @lru: Pageout list, eg. active_list protected by 93 * lruvec->lru_lock. Sometimes used as a generic list 94 * by the page owner. 95 */ 96 union { 97 struct list_head lru; 98 99 /* Or, free page */ 100 struct list_head buddy_list; 101 struct list_head pcp_list; 102 struct llist_node pcp_llist; 103 }; 104 struct address_space *mapping; 105 union { 106 pgoff_t __folio_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 * or on pcp_llist. 115 */ 116 unsigned long private; 117 }; 118 struct { /* page_pool used by netstack */ 119 /** 120 * @pp_magic: magic value to avoid recycling non 121 * page_pool allocated pages. 122 */ 123 unsigned long pp_magic; 124 struct page_pool *pp; 125 unsigned long _pp_mapping_pad; 126 unsigned long dma_addr; 127 atomic_long_t pp_ref_count; 128 }; 129 struct { /* Tail pages of compound page */ 130 unsigned long compound_info; /* Bit zero is set */ 131 }; 132 struct { /* ZONE_DEVICE pages */ 133 /* 134 * The first word is used for compound_info or folio 135 * pgmap 136 */ 137 void *_unused_pgmap_compound_info; 138 void *zone_device_data; 139 /* 140 * ZONE_DEVICE private pages are counted as being 141 * mapped so the next 3 words hold the mapping, index, 142 * and private fields from the source anonymous or 143 * page cache page while the page is migrated to device 144 * private memory. 145 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also 146 * use the mapping, index, and private fields when 147 * pmem backed DAX files are mapped. 148 */ 149 }; 150 151 /** @rcu_head: You can use this to free a page by RCU. */ 152 struct rcu_head rcu_head; 153 }; 154 155 union { /* This union is 4 bytes in size. */ 156 /* 157 * For head pages of typed folios, the value stored here 158 * allows for determining what this page is used for. The 159 * tail pages of typed folios will not store a type 160 * (page_type == _mapcount == -1). 161 * 162 * See page-flags.h for a list of page types which are currently 163 * stored here. 164 * 165 * Owners of typed folios may reuse the lower 16 bit of the 166 * head page page_type field after setting the page type, 167 * but must reset these 16 bit to -1 before clearing the 168 * page type. 169 */ 170 unsigned int page_type; 171 172 /* 173 * For pages that are part of non-typed folios for which mappings 174 * are tracked via the RMAP, encodes the number of times this page 175 * is directly referenced by a page table. 176 * 177 * Note that the mapcount is always initialized to -1, so that 178 * transitions both from it and to it can be tracked, using 179 * atomic_inc_and_test() and atomic_add_negative(-1). 180 */ 181 atomic_t _mapcount; 182 }; 183 184 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */ 185 atomic_t _refcount; 186 187 #ifdef CONFIG_MEMCG 188 unsigned long memcg_data; 189 #elif defined(CONFIG_SLAB_OBJ_EXT) 190 unsigned long _unused_slab_obj_exts; 191 #endif 192 193 /* 194 * On machines where all RAM is mapped into kernel address space, 195 * we can simply calculate the virtual address. On machines with 196 * highmem some memory is mapped into kernel virtual memory 197 * dynamically, so we need a place to store that address. 198 * Note that this field could be 16 bits on x86 ... ;) 199 * 200 * Architectures with slow multiplication can define 201 * WANT_PAGE_VIRTUAL in asm/page.h 202 */ 203 #if defined(WANT_PAGE_VIRTUAL) 204 void *virtual; /* Kernel virtual address (NULL if 205 not kmapped, ie. highmem) */ 206 #endif /* WANT_PAGE_VIRTUAL */ 207 208 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 209 int _last_cpupid; 210 #endif 211 212 #ifdef CONFIG_KMSAN 213 /* 214 * KMSAN metadata for this page: 215 * - shadow page: every bit indicates whether the corresponding 216 * bit of the original page is initialized (0) or not (1); 217 * - origin page: every 4 bytes contain an id of the stack trace 218 * where the uninitialized value was created. 219 */ 220 struct page *kmsan_shadow; 221 struct page *kmsan_origin; 222 #endif 223 } _struct_page_alignment; 224 225 /* 226 * struct encoded_page - a nonexistent type marking this pointer 227 * 228 * An 'encoded_page' pointer is a pointer to a regular 'struct page', but 229 * with the low bits of the pointer indicating extra context-dependent 230 * information. Only used in mmu_gather handling, and this acts as a type 231 * system check on that use. 232 * 233 * We only really have two guaranteed bits in general, although you could 234 * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE) 235 * for more. 236 * 237 * Use the supplied helper functions to endcode/decode the pointer and bits. 238 */ 239 struct encoded_page; 240 241 #define ENCODED_PAGE_BITS 3ul 242 243 /* Perform rmap removal after we have flushed the TLB. */ 244 #define ENCODED_PAGE_BIT_DELAY_RMAP 1ul 245 246 /* 247 * The next item in an encoded_page array is the "nr_pages" argument, specifying 248 * the number of consecutive pages starting from this page, that all belong to 249 * the same folio. For example, "nr_pages" corresponds to the number of folio 250 * references that must be dropped. If this bit is not set, "nr_pages" is 251 * implicitly 1. 252 */ 253 #define ENCODED_PAGE_BIT_NR_PAGES_NEXT 2ul 254 255 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags) 256 { 257 BUILD_BUG_ON(flags > ENCODED_PAGE_BITS); 258 return (struct encoded_page *)(flags | (unsigned long)page); 259 } 260 261 static inline unsigned long encoded_page_flags(struct encoded_page *page) 262 { 263 return ENCODED_PAGE_BITS & (unsigned long)page; 264 } 265 266 static inline struct page *encoded_page_ptr(struct encoded_page *page) 267 { 268 return (struct page *)(~ENCODED_PAGE_BITS & (unsigned long)page); 269 } 270 271 static __always_inline struct encoded_page *encode_nr_pages(unsigned long nr) 272 { 273 VM_WARN_ON_ONCE((nr << 2) >> 2 != nr); 274 return (struct encoded_page *)(nr << 2); 275 } 276 277 static __always_inline unsigned long encoded_nr_pages(struct encoded_page *page) 278 { 279 return ((unsigned long)page) >> 2; 280 } 281 282 /* 283 * A swap entry has to fit into a "unsigned long", as the entry is hidden 284 * in the "index" field of the swapper address space. 285 */ 286 typedef struct { 287 unsigned long val; 288 } swp_entry_t; 289 290 /** 291 * typedef softleaf_t - Describes a page table software leaf entry, abstracted 292 * from its architecture-specific encoding. 293 * 294 * Page table leaf entries are those which do not reference any descendent page 295 * tables but rather either reference a data page, are an empty (or 'none' 296 * entry), or contain a non-present entry. 297 * 298 * If referencing another page table or a data page then the page table entry is 299 * pertinent to hardware - that is it tells the hardware how to decode the page 300 * table entry. 301 * 302 * Otherwise it is a software-defined leaf page table entry, which this type 303 * describes. See leafops.h and specifically @softleaf_type for a list of all 304 * possible kinds of software leaf entry. 305 * 306 * A softleaf_t entry is abstracted from the hardware page table entry, so is 307 * not architecture-specific. 308 * 309 * NOTE: While we transition from the confusing swp_entry_t type used for this 310 * purpose, we simply alias this type. This will be removed once the 311 * transition is complete. 312 */ 313 typedef swp_entry_t softleaf_t; 314 315 #if defined(CONFIG_MEMCG) || defined(CONFIG_SLAB_OBJ_EXT) 316 /* We have some extra room after the refcount in tail pages. */ 317 #define NR_PAGES_IN_LARGE_FOLIO 318 #endif 319 320 /* 321 * On 32bit, we can cut the required metadata in half, because: 322 * (a) PID_MAX_LIMIT implicitly limits the number of MMs we could ever have, 323 * so we can limit MM IDs to 15 bit (32767). 324 * (b) We don't expect folios where even a single complete PTE mapping by 325 * one MM would exceed 15 bits (order-15). 326 */ 327 #ifdef CONFIG_64BIT 328 typedef int mm_id_mapcount_t; 329 #define MM_ID_MAPCOUNT_MAX INT_MAX 330 typedef unsigned int mm_id_t; 331 #else /* !CONFIG_64BIT */ 332 typedef short mm_id_mapcount_t; 333 #define MM_ID_MAPCOUNT_MAX SHRT_MAX 334 typedef unsigned short mm_id_t; 335 #endif /* CONFIG_64BIT */ 336 337 /* We implicitly use the dummy ID for init-mm etc. where we never rmap pages. */ 338 #define MM_ID_DUMMY 0 339 #define MM_ID_MIN (MM_ID_DUMMY + 1) 340 341 /* 342 * We leave the highest bit of each MM id unused, so we can store a flag 343 * in the highest bit of each folio->_mm_id[]. 344 */ 345 #define MM_ID_BITS ((sizeof(mm_id_t) * BITS_PER_BYTE) - 1) 346 #define MM_ID_MASK ((1U << MM_ID_BITS) - 1) 347 #define MM_ID_MAX MM_ID_MASK 348 349 /* 350 * In order to use bit_spin_lock(), which requires an unsigned long, we 351 * operate on folio->_mm_ids when working on flags. 352 */ 353 #define FOLIO_MM_IDS_LOCK_BITNUM MM_ID_BITS 354 #define FOLIO_MM_IDS_LOCK_BIT BIT(FOLIO_MM_IDS_LOCK_BITNUM) 355 #define FOLIO_MM_IDS_SHARED_BITNUM (2 * MM_ID_BITS + 1) 356 #define FOLIO_MM_IDS_SHARED_BIT BIT(FOLIO_MM_IDS_SHARED_BITNUM) 357 358 /** 359 * struct folio - Represents a contiguous set of bytes. 360 * @flags: Identical to the page flags. 361 * @lru: Least Recently Used list; tracks how recently this folio was used. 362 * @mlock_count: Number of times this folio has been pinned by mlock(). 363 * @mapping: The file this page belongs to, or refers to the anon_vma for 364 * anonymous memory. 365 * @index: Offset within the file, in units of pages. For anonymous memory, 366 * this is the index from the beginning of the mmap. 367 * @share: number of DAX mappings that reference this folio. See 368 * dax_associate_entry. 369 * @private: Filesystem per-folio data (see folio_attach_private()). 370 * @swap: Used for swp_entry_t if folio_test_swapcache(). 371 * @migrate_info: Stores migration state (anon_vma pointer and 372 * FOLIO_WAS_* markers). 373 * @_mapcount: Do not access this member directly. Use folio_mapcount() to 374 * find out how many times this folio is mapped by userspace. 375 * @_refcount: Do not access this member directly. Use folio_ref_count() 376 * to find how many references there are to this folio. 377 * @memcg_data: Memory Control Group data. 378 * @pgmap: Metadata for ZONE_DEVICE mappings 379 * @virtual: Virtual address in the kernel direct map. 380 * @_last_cpupid: IDs of last CPU and last process that accessed the folio. 381 * @_entire_mapcount: Do not use directly, call folio_entire_mapcount(). 382 * @_large_mapcount: Do not use directly, call folio_mapcount(). 383 * @_nr_pages_mapped: Do not use outside of rmap and debug code. 384 * @_pincount: Do not use directly, call folio_maybe_dma_pinned(). 385 * @_nr_pages: Do not use directly, call folio_nr_pages(). 386 * @_mm_id: Do not use outside of rmap code. 387 * @_mm_ids: Do not use outside of rmap code. 388 * @_mm_id_mapcount: Do not use outside of rmap code. 389 * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h. 390 * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h. 391 * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h. 392 * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head(). 393 * @_deferred_list: Folios to be split under memory pressure. 394 * @_unused_slab_obj_exts: Placeholder to match obj_exts in struct slab. 395 * 396 * A folio is a physically, virtually and logically contiguous set 397 * of bytes. It is a power-of-two in size, and it is aligned to that 398 * same power-of-two. It is at least as large as %PAGE_SIZE. If it is 399 * in the page cache, it is at a file offset which is a multiple of that 400 * power-of-two. It may be mapped into userspace at an address which is 401 * at an arbitrary page offset, but its kernel virtual address is aligned 402 * to its size. 403 */ 404 struct folio { 405 /* private: don't document the anon union */ 406 union { 407 struct { 408 /* public: */ 409 memdesc_flags_t flags; 410 union { 411 struct list_head lru; 412 /* private: avoid cluttering the output */ 413 /* For the Unevictable "LRU list" slot */ 414 struct { 415 /* Avoid compound_info */ 416 void *__filler; 417 /* public: */ 418 unsigned int mlock_count; 419 /* private: */ 420 }; 421 /* public: */ 422 struct dev_pagemap *pgmap; 423 }; 424 struct address_space *mapping; 425 union { 426 pgoff_t index; 427 unsigned long share; 428 }; 429 union { 430 void *private; 431 swp_entry_t swap; 432 unsigned long migrate_info; 433 }; 434 atomic_t _mapcount; 435 atomic_t _refcount; 436 #ifdef CONFIG_MEMCG 437 unsigned long memcg_data; 438 #elif defined(CONFIG_SLAB_OBJ_EXT) 439 unsigned long _unused_slab_obj_exts; 440 #endif 441 #if defined(WANT_PAGE_VIRTUAL) 442 void *virtual; 443 #endif 444 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 445 int _last_cpupid; 446 #endif 447 /* private: the union with struct page is transitional */ 448 }; 449 struct page page; 450 }; 451 union { 452 struct { 453 unsigned long _flags_1; 454 unsigned long _head_1; 455 union { 456 struct { 457 /* public: */ 458 atomic_t _large_mapcount; 459 atomic_t _nr_pages_mapped; 460 #ifdef CONFIG_64BIT 461 atomic_t _entire_mapcount; 462 atomic_t _pincount; 463 #endif /* CONFIG_64BIT */ 464 mm_id_mapcount_t _mm_id_mapcount[2]; 465 union { 466 mm_id_t _mm_id[2]; 467 unsigned long _mm_ids; 468 }; 469 /* private: the union with struct page is transitional */ 470 }; 471 unsigned long _usable_1[4]; 472 }; 473 atomic_t _mapcount_1; 474 atomic_t _refcount_1; 475 /* public: */ 476 #ifdef NR_PAGES_IN_LARGE_FOLIO 477 unsigned int _nr_pages; 478 #endif /* NR_PAGES_IN_LARGE_FOLIO */ 479 /* private: the union with struct page is transitional */ 480 }; 481 struct page __page_1; 482 }; 483 union { 484 struct { 485 unsigned long _flags_2; 486 unsigned long _head_2; 487 /* public: */ 488 struct list_head _deferred_list; 489 #ifndef CONFIG_64BIT 490 atomic_t _entire_mapcount; 491 atomic_t _pincount; 492 #endif /* !CONFIG_64BIT */ 493 /* private: the union with struct page is transitional */ 494 }; 495 struct page __page_2; 496 }; 497 union { 498 struct { 499 unsigned long _flags_3; 500 unsigned long _head_3; 501 /* public: */ 502 void *_hugetlb_subpool; 503 void *_hugetlb_cgroup; 504 void *_hugetlb_cgroup_rsvd; 505 void *_hugetlb_hwpoison; 506 /* private: the union with struct page is transitional */ 507 }; 508 struct page __page_3; 509 }; 510 }; 511 512 #define FOLIO_MATCH(pg, fl) \ 513 static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl)) 514 FOLIO_MATCH(flags, flags); 515 FOLIO_MATCH(lru, lru); 516 FOLIO_MATCH(mapping, mapping); 517 FOLIO_MATCH(compound_info, lru); 518 FOLIO_MATCH(__folio_index, index); 519 FOLIO_MATCH(private, private); 520 FOLIO_MATCH(_mapcount, _mapcount); 521 FOLIO_MATCH(_refcount, _refcount); 522 #ifdef CONFIG_MEMCG 523 FOLIO_MATCH(memcg_data, memcg_data); 524 #endif 525 #if defined(WANT_PAGE_VIRTUAL) 526 FOLIO_MATCH(virtual, virtual); 527 #endif 528 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 529 FOLIO_MATCH(_last_cpupid, _last_cpupid); 530 #endif 531 #undef FOLIO_MATCH 532 #define FOLIO_MATCH(pg, fl) \ 533 static_assert(offsetof(struct folio, fl) == \ 534 offsetof(struct page, pg) + sizeof(struct page)) 535 FOLIO_MATCH(flags, _flags_1); 536 FOLIO_MATCH(compound_info, _head_1); 537 FOLIO_MATCH(_mapcount, _mapcount_1); 538 FOLIO_MATCH(_refcount, _refcount_1); 539 #undef FOLIO_MATCH 540 #define FOLIO_MATCH(pg, fl) \ 541 static_assert(offsetof(struct folio, fl) == \ 542 offsetof(struct page, pg) + 2 * sizeof(struct page)) 543 FOLIO_MATCH(flags, _flags_2); 544 FOLIO_MATCH(compound_info, _head_2); 545 #undef FOLIO_MATCH 546 #define FOLIO_MATCH(pg, fl) \ 547 static_assert(offsetof(struct folio, fl) == \ 548 offsetof(struct page, pg) + 3 * sizeof(struct page)) 549 FOLIO_MATCH(flags, _flags_3); 550 FOLIO_MATCH(compound_info, _head_3); 551 #undef FOLIO_MATCH 552 553 /** 554 * struct ptdesc - Memory descriptor for page tables. 555 * @pt_flags: enum pt_flags plus zone/node/section. 556 * @pt_rcu_head: For freeing page table pages. 557 * @pt_list: List of used page tables. Used for s390 gmap shadow pages 558 * (which are not linked into the user page tables) and x86 559 * pgds. 560 * @_pt_pad_1: Padding that aliases with page's compound head. 561 * @pmd_huge_pte: Protected by ptdesc->ptl, used for THPs. 562 * @__page_mapping: Aliases with page->mapping. Unused for page tables. 563 * @pt_index: Used for s390 gmap. 564 * @pt_mm: Used for x86 pgds. 565 * @pt_frag_refcount: For fragmented page table tracking. Powerpc only. 566 * @pt_share_count: Used for HugeTLB PMD page table share count. 567 * @_pt_pad_2: Padding to ensure proper alignment. 568 * @ptl: Lock for the page table. 569 * @__page_type: Same as page->page_type. Unused for page tables. 570 * @__page_refcount: Same as page refcount. 571 * @pt_memcg_data: Memcg data. Tracked for page tables here. 572 * 573 * This struct overlays struct page for now. Do not modify without a good 574 * understanding of the issues. 575 */ 576 struct ptdesc { 577 memdesc_flags_t pt_flags; 578 579 union { 580 struct rcu_head pt_rcu_head; 581 struct list_head pt_list; 582 struct { 583 unsigned long _pt_pad_1; 584 pgtable_t pmd_huge_pte; 585 }; 586 }; 587 unsigned long __page_mapping; 588 589 union { 590 pgoff_t pt_index; 591 struct mm_struct *pt_mm; 592 atomic_t pt_frag_refcount; 593 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 594 atomic_t pt_share_count; 595 #endif 596 }; 597 598 union { 599 unsigned long _pt_pad_2; 600 #if ALLOC_SPLIT_PTLOCKS 601 spinlock_t *ptl; 602 #else 603 spinlock_t ptl; 604 #endif 605 }; 606 unsigned int __page_type; 607 atomic_t __page_refcount; 608 #ifdef CONFIG_MEMCG 609 unsigned long pt_memcg_data; 610 #endif 611 }; 612 613 #define TABLE_MATCH(pg, pt) \ 614 static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt)) 615 TABLE_MATCH(flags, pt_flags); 616 TABLE_MATCH(compound_info, pt_list); 617 TABLE_MATCH(compound_info, _pt_pad_1); 618 TABLE_MATCH(mapping, __page_mapping); 619 TABLE_MATCH(__folio_index, pt_index); 620 TABLE_MATCH(rcu_head, pt_rcu_head); 621 TABLE_MATCH(page_type, __page_type); 622 TABLE_MATCH(_refcount, __page_refcount); 623 #ifdef CONFIG_MEMCG 624 TABLE_MATCH(memcg_data, pt_memcg_data); 625 #endif 626 #undef TABLE_MATCH 627 static_assert(sizeof(struct ptdesc) <= sizeof(struct page)); 628 629 #define ptdesc_page(pt) (_Generic((pt), \ 630 const struct ptdesc *: (const struct page *)(pt), \ 631 struct ptdesc *: (struct page *)(pt))) 632 633 #define ptdesc_folio(pt) (_Generic((pt), \ 634 const struct ptdesc *: (const struct folio *)(pt), \ 635 struct ptdesc *: (struct folio *)(pt))) 636 637 #define page_ptdesc(p) (_Generic((p), \ 638 const struct page *: (const struct ptdesc *)(p), \ 639 struct page *: (struct ptdesc *)(p))) 640 641 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 642 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc) 643 { 644 atomic_set(&ptdesc->pt_share_count, 0); 645 } 646 647 static inline void ptdesc_pmd_pts_inc(struct ptdesc *ptdesc) 648 { 649 atomic_inc(&ptdesc->pt_share_count); 650 } 651 652 static inline void ptdesc_pmd_pts_dec(struct ptdesc *ptdesc) 653 { 654 atomic_dec(&ptdesc->pt_share_count); 655 } 656 657 static inline int ptdesc_pmd_pts_count(const struct ptdesc *ptdesc) 658 { 659 return atomic_read(&ptdesc->pt_share_count); 660 } 661 662 static inline bool ptdesc_pmd_is_shared(struct ptdesc *ptdesc) 663 { 664 return !!ptdesc_pmd_pts_count(ptdesc); 665 } 666 #else 667 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc) 668 { 669 } 670 #endif 671 672 /* 673 * Used for sizing the vmemmap region on some architectures 674 */ 675 #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) 676 677 /* 678 * page_private can be used on tail pages. However, PagePrivate is only 679 * checked by the VM on the head page. So page_private on the tail pages 680 * should be used for data that's ancillary to the head page (eg attaching 681 * buffer heads to tail pages after attaching buffer heads to the head page) 682 */ 683 #define page_private(page) ((page)->private) 684 685 static inline void set_page_private(struct page *page, unsigned long private) 686 { 687 page->private = private; 688 } 689 690 static inline void *folio_get_private(const struct folio *folio) 691 { 692 return folio->private; 693 } 694 695 typedef unsigned long vm_flags_t; 696 697 /* 698 * freeptr_t represents a SLUB freelist pointer, which might be encoded 699 * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. 700 */ 701 typedef struct { unsigned long v; } freeptr_t; 702 703 /* 704 * A region containing a mapping of a non-memory backed file under NOMMU 705 * conditions. These are held in a global tree and are pinned by the VMAs that 706 * map parts of them. 707 */ 708 struct vm_region { 709 struct rb_node vm_rb; /* link in global region tree */ 710 vm_flags_t vm_flags; /* VMA vm_flags */ 711 unsigned long vm_start; /* start address of region */ 712 unsigned long vm_end; /* region initialised to here */ 713 unsigned long vm_top; /* region allocated to here */ 714 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */ 715 struct file *vm_file; /* the backing file or NULL */ 716 717 int vm_usage; /* region usage count (access under nommu_region_sem) */ 718 bool vm_icache_flushed : 1; /* true if the icache has been flushed for 719 * this region */ 720 }; 721 722 #ifdef CONFIG_USERFAULTFD 723 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, }) 724 struct vm_userfaultfd_ctx { 725 struct userfaultfd_ctx *ctx; 726 }; 727 #else /* CONFIG_USERFAULTFD */ 728 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {}) 729 struct vm_userfaultfd_ctx {}; 730 #endif /* CONFIG_USERFAULTFD */ 731 732 struct anon_vma_name { 733 struct kref kref; 734 /* The name needs to be at the end because it is dynamically sized. */ 735 char name[]; 736 }; 737 738 #ifdef CONFIG_ANON_VMA_NAME 739 /* 740 * mmap_lock should be read-locked when calling anon_vma_name(). Caller should 741 * either keep holding the lock while using the returned pointer or it should 742 * raise anon_vma_name refcount before releasing the lock. 743 */ 744 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma); 745 struct anon_vma_name *anon_vma_name_alloc(const char *name); 746 void anon_vma_name_free(struct kref *kref); 747 #else /* CONFIG_ANON_VMA_NAME */ 748 static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma) 749 { 750 return NULL; 751 } 752 753 static inline struct anon_vma_name *anon_vma_name_alloc(const char *name) 754 { 755 return NULL; 756 } 757 #endif 758 759 /* 760 * While __vma_enter_locked() is working to ensure are no read-locks held on a 761 * VMA (either while acquiring a VMA write lock or marking a VMA detached) we 762 * set the VM_REFCNT_EXCLUDE_READERS_FLAG in vma->vm_refcnt to indiciate to 763 * vma_start_read() that the reference count should be left alone. 764 * 765 * See the comment describing vm_refcnt in vm_area_struct for details as to 766 * which values the VMA reference count can be. 767 */ 768 #define VM_REFCNT_EXCLUDE_READERS_BIT (30) 769 #define VM_REFCNT_EXCLUDE_READERS_FLAG (1U << VM_REFCNT_EXCLUDE_READERS_BIT) 770 #define VM_REFCNT_LIMIT (VM_REFCNT_EXCLUDE_READERS_FLAG - 1) 771 772 struct vma_numab_state { 773 /* 774 * Initialised as time in 'jiffies' after which VMA 775 * should be scanned. Delays first scan of new VMA by at 776 * least sysctl_numa_balancing_scan_delay: 777 */ 778 unsigned long next_scan; 779 780 /* 781 * Time in jiffies when pids_active[] is reset to 782 * detect phase change behaviour: 783 */ 784 unsigned long pids_active_reset; 785 786 /* 787 * Approximate tracking of PIDs that trapped a NUMA hinting 788 * fault. May produce false positives due to hash collisions. 789 * 790 * [0] Previous PID tracking 791 * [1] Current PID tracking 792 * 793 * Window moves after next_pid_reset has expired approximately 794 * every VMA_PID_RESET_PERIOD jiffies: 795 */ 796 unsigned long pids_active[2]; 797 798 /* MM scan sequence ID when scan first started after VMA creation */ 799 int start_scan_seq; 800 801 /* 802 * MM scan sequence ID when the VMA was last completely scanned. 803 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq 804 */ 805 int prev_scan_seq; 806 }; 807 808 #ifdef __HAVE_PFNMAP_TRACKING 809 struct pfnmap_track_ctx { 810 struct kref kref; 811 unsigned long pfn; 812 unsigned long size; /* in bytes */ 813 }; 814 #endif 815 816 /* What action should be taken after an .mmap_prepare call is complete? */ 817 enum mmap_action_type { 818 MMAP_NOTHING, /* Mapping is complete, no further action. */ 819 MMAP_REMAP_PFN, /* Remap PFN range. */ 820 MMAP_IO_REMAP_PFN, /* I/O remap PFN range. */ 821 MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */ 822 MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from array. */ 823 }; 824 825 /* 826 * Describes an action an mmap_prepare hook can instruct to be taken to complete 827 * the mapping of a VMA. Specified in vm_area_desc. 828 */ 829 struct mmap_action { 830 union { 831 struct { 832 unsigned long start; 833 unsigned long start_pfn; 834 unsigned long size; 835 pgprot_t pgprot; 836 } remap; 837 struct { 838 phys_addr_t start_phys_addr; 839 unsigned long size; 840 } simple_ioremap; 841 struct { 842 unsigned long start; 843 struct page **pages; 844 unsigned long nr_pages; 845 pgoff_t pgoff; 846 } map_kernel; 847 }; 848 enum mmap_action_type type; 849 850 /* 851 * If non-zero, replace errors that arise from mmap actions with this 852 * value instead. Only valid error codes may be specified. 853 */ 854 int error_override; 855 856 /* 857 * This should be set in rare instances where the operation required 858 * that the rmap should not be able to access the VMA until 859 * completely set up. 860 */ 861 bool hide_from_rmap_until_complete :1; 862 }; 863 864 /* 865 * Opaque type representing current VMA (vm_area_struct) flag state. Must be 866 * accessed via vma_flags_xxx() helper functions. 867 */ 868 #define NUM_VMA_FLAG_BITS BITS_PER_LONG 869 typedef struct { 870 DECLARE_BITMAP(__vma_flags, NUM_VMA_FLAG_BITS); 871 } vma_flags_t; 872 873 #define EMPTY_VMA_FLAGS ((vma_flags_t){ }) 874 875 /* Are no flags set in the specified VMA flags? */ 876 static __always_inline bool vma_flags_empty(const vma_flags_t *flags) 877 { 878 const unsigned long *bitmap = flags->__vma_flags; 879 880 return bitmap_empty(bitmap, NUM_VMA_FLAG_BITS); 881 } 882 883 /* 884 * Describes a VMA that is about to be mmap()'ed. Drivers may choose to 885 * manipulate mutable fields which will cause those fields to be updated in the 886 * resultant VMA. 887 * 888 * Helper functions are not required for manipulating any field. 889 */ 890 struct vm_area_desc { 891 /* Immutable state. */ 892 struct mm_struct *mm; 893 struct file *file; /* May vary from vm_file in stacked callers. */ 894 unsigned long start; 895 unsigned long end; 896 897 /* Mutable fields. Populated with initial state. */ 898 pgoff_t pgoff; 899 struct file *vm_file; 900 vma_flags_t vma_flags; 901 pgprot_t page_prot; 902 903 /* Write-only fields. */ 904 const struct vm_operations_struct *vm_ops; 905 void *private_data; 906 907 /* Take further action? */ 908 struct mmap_action action; 909 }; 910 911 /* 912 * This struct describes a virtual memory area. There is one of these 913 * per VM-area/task. A VM area is any part of the process virtual memory 914 * space that has a special rule for the page-fault handlers (ie a shared 915 * library, the executable area etc). 916 * 917 * Only explicitly marked struct members may be accessed by RCU readers before 918 * getting a stable reference. 919 * 920 * WARNING: when adding new members, please update vm_area_init_from() to copy 921 * them during vm_area_struct content duplication. 922 */ 923 struct vm_area_struct { 924 /* The first cache line has the info for VMA tree walking. */ 925 926 union { 927 struct { 928 /* VMA covers [vm_start; vm_end) addresses within mm */ 929 unsigned long vm_start; 930 unsigned long vm_end; 931 }; 932 freeptr_t vm_freeptr; /* Pointer used by SLAB_TYPESAFE_BY_RCU */ 933 }; 934 935 /* 936 * The address space we belong to. 937 * Unstable RCU readers are allowed to read this. 938 */ 939 struct mm_struct *vm_mm; 940 pgprot_t vm_page_prot; /* Access permissions of this VMA. */ 941 942 /* 943 * Flags, see mm.h. 944 * To modify use vm_flags_{init|reset|set|clear|mod} functions. 945 * Preferably, use vma_flags_xxx() functions. 946 */ 947 union { 948 /* Temporary while VMA flags are being converted. */ 949 const vm_flags_t vm_flags; 950 vma_flags_t flags; 951 }; 952 953 #ifdef CONFIG_PER_VMA_LOCK 954 /* 955 * Can only be written (using WRITE_ONCE()) while holding both: 956 * - mmap_lock (in write mode) 957 * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_FLAG is set 958 * Can be read reliably while holding one of: 959 * - mmap_lock (in read or write mode) 960 * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_BIT is set or vm_refcnt > 1 961 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout 962 * while holding nothing (except RCU to keep the VMA struct allocated). 963 * 964 * This sequence counter is explicitly allowed to overflow; sequence 965 * counter reuse can only lead to occasional unnecessary use of the 966 * slowpath. 967 */ 968 unsigned int vm_lock_seq; 969 #endif 970 /* 971 * Low 32-bits of anonymous page offset. 972 * See vma_start_anon_pgoff() comment for details. 973 */ 974 unsigned int __vm_anon_pgoff_lo; 975 /* 976 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma 977 * list, after a COW of one of the file pages. A MAP_SHARED vma 978 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack 979 * or brk vma (with NULL file) can only be in an anon_vma list. 980 */ 981 struct list_head anon_vma_chain; /* Serialized by mmap_lock & 982 * page_table_lock */ 983 struct anon_vma *anon_vma; /* Serialized by page_table_lock */ 984 985 /* Function pointers to deal with this struct. */ 986 const struct vm_operations_struct *vm_ops; 987 988 /* Information about our backing store: */ 989 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE 990 units */ 991 struct file * vm_file; /* File we map to (can be NULL). */ 992 void * vm_private_data; /* was vm_pte (shared mem) */ 993 994 #ifdef CONFIG_SWAP 995 atomic_long_t swap_readahead_info; 996 #endif 997 #ifndef CONFIG_MMU 998 struct vm_region *vm_region; /* NOMMU mapping region */ 999 #endif 1000 #ifdef CONFIG_NUMA 1001 struct mempolicy *vm_policy; /* NUMA policy for the VMA */ 1002 #endif 1003 #ifdef CONFIG_NUMA_BALANCING 1004 struct vma_numab_state *numab_state; /* NUMA Balancing state */ 1005 #endif 1006 #ifdef CONFIG_PER_VMA_LOCK 1007 /* 1008 * Used to keep track of firstly, whether the VMA is attached, secondly, 1009 * if attached, how many read locks are taken, and thirdly, if the 1010 * VM_REFCNT_EXCLUDE_READERS_FLAG is set, whether any read locks held 1011 * are currently in the process of being excluded. 1012 * 1013 * This value can be equal to: 1014 * 1015 * 0 - Detached. IMPORTANT: when the refcnt is zero, readers cannot 1016 * increment it. 1017 * 1018 * 1 - Attached and either unlocked or write-locked. Write locks are 1019 * identified via __is_vma_write_locked() which checks for equality of 1020 * vma->vm_lock_seq and mm->mm_lock_seq. 1021 * 1022 * >1, < VM_REFCNT_EXCLUDE_READERS_FLAG - Read-locked or (unlikely) 1023 * write-locked with other threads having temporarily incremented the 1024 * reference count prior to determining it is write-locked and 1025 * decrementing it again. 1026 * 1027 * VM_REFCNT_EXCLUDE_READERS_FLAG - Detached, pending 1028 * __vma_end_exclude_readers() completion which will decrement the 1029 * reference count to zero. IMPORTANT - at this stage no further readers 1030 * can increment the reference count. It can only be reduced. 1031 * 1032 * VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either write-locking 1033 * an attached VMA and has yet to invoke __vma_end_exclude_readers(), 1034 * OR a thread is detaching a VMA and is waiting on a single spurious 1035 * reader in order to decrement the reference count. IMPORTANT - as 1036 * above, no further readers can increment the reference count. 1037 * 1038 * > VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either 1039 * write-locking or detaching a VMA is waiting on readers to 1040 * exit. IMPORTANT - as above, no further readers can increment the 1041 * reference count. 1042 * 1043 * NOTE: Unstable RCU readers are allowed to read this. 1044 */ 1045 refcount_t vm_refcnt ____cacheline_aligned_in_smp; 1046 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1047 struct lockdep_map vmlock_dep_map; 1048 #endif 1049 #endif 1050 #ifdef CONFIG_64BIT 1051 /* 1052 * High 32-bits of anonymous page offset. 1053 * See vma_start_anon_pgoff() comment for details. 1054 */ 1055 unsigned int __vm_anon_pgoff_hi; 1056 #endif 1057 /* 1058 * For areas with an address space and backing store, 1059 * linkage into the address_space->i_mmap interval tree. 1060 * 1061 */ 1062 struct { 1063 struct rb_node rb; 1064 unsigned long rb_subtree_last; 1065 } shared; 1066 #ifdef CONFIG_ANON_VMA_NAME 1067 /* 1068 * For private and shared anonymous mappings, a pointer to a null 1069 * terminated string containing the name given to the vma, or NULL if 1070 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access. 1071 */ 1072 struct anon_vma_name *anon_name; 1073 #endif 1074 struct vm_userfaultfd_ctx vm_userfaultfd_ctx; 1075 #ifdef __HAVE_PFNMAP_TRACKING 1076 struct pfnmap_track_ctx *pfnmap_track_ctx; 1077 #endif 1078 } __randomize_layout; 1079 1080 /* Clears all bits in the VMA flags bitmap, non-atomically. */ 1081 static __always_inline void vma_flags_clear_all(vma_flags_t *flags) 1082 { 1083 bitmap_zero(flags->__vma_flags, NUM_VMA_FLAG_BITS); 1084 } 1085 1086 /* 1087 * Helper function which converts a vma_flags_t value to a legacy vm_flags_t 1088 * value. This is only valid if the input flags value can be expressed in a 1089 * system word. 1090 * 1091 * Will be removed once the conversion to VMA flags is complete. 1092 */ 1093 static __always_inline vm_flags_t vma_flags_to_legacy(vma_flags_t flags) 1094 { 1095 return (vm_flags_t)flags.__vma_flags[0]; 1096 } 1097 1098 /* 1099 * Copy value to the first system word of VMA flags, non-atomically. 1100 * 1101 * IMPORTANT: This does not overwrite bytes past the first system word. The 1102 * caller must account for this. 1103 */ 1104 static __always_inline void vma_flags_overwrite_word(vma_flags_t *flags, 1105 unsigned long value) 1106 { 1107 unsigned long *bitmap = flags->__vma_flags; 1108 1109 bitmap[0] = value; 1110 } 1111 1112 /* 1113 * Helper function which converts a legacy vm_flags_t value to a vma_flags_t 1114 * value. 1115 * 1116 * Will be removed once the conversion to VMA flags is complete. 1117 */ 1118 static __always_inline vma_flags_t legacy_to_vma_flags(vm_flags_t flags) 1119 { 1120 vma_flags_t ret = EMPTY_VMA_FLAGS; 1121 1122 vma_flags_overwrite_word(&ret, flags); 1123 return ret; 1124 } 1125 1126 /* 1127 * Copy value to the first system word of VMA flags ONCE, non-atomically. 1128 * 1129 * IMPORTANT: This does not overwrite bytes past the first system word. The 1130 * caller must account for this. 1131 */ 1132 static __always_inline void vma_flags_overwrite_word_once(vma_flags_t *flags, 1133 unsigned long value) 1134 { 1135 unsigned long *bitmap = flags->__vma_flags; 1136 1137 WRITE_ONCE(*bitmap, value); 1138 } 1139 1140 /* Update the first system word of VMA flags setting bits, non-atomically. */ 1141 static __always_inline void vma_flags_set_word(vma_flags_t *flags, 1142 unsigned long value) 1143 { 1144 unsigned long *bitmap = flags->__vma_flags; 1145 1146 *bitmap |= value; 1147 } 1148 1149 /* Update the first system word of VMA flags clearing bits, non-atomically. */ 1150 static __always_inline void vma_flags_clear_word(vma_flags_t *flags, 1151 unsigned long value) 1152 { 1153 unsigned long *bitmap = flags->__vma_flags; 1154 1155 *bitmap &= ~value; 1156 } 1157 1158 #ifdef CONFIG_NUMA 1159 #define vma_policy(vma) ((vma)->vm_policy) 1160 #else 1161 #define vma_policy(vma) NULL 1162 #endif 1163 1164 /* 1165 * Opaque type representing current mm_struct flag state. Must be accessed via 1166 * mm_flags_xxx() helper functions. 1167 */ 1168 #define NUM_MM_FLAG_BITS (64) 1169 typedef struct { 1170 DECLARE_BITMAP(__mm_flags, NUM_MM_FLAG_BITS); 1171 } __private mm_flags_t; 1172 1173 struct kioctx_table; 1174 struct iommu_mm_data; 1175 struct mm_struct { 1176 struct { 1177 /* 1178 * Fields which are often written to are placed in a separate 1179 * cache line. 1180 */ 1181 struct { 1182 /** 1183 * @mm_count: The number of references to &struct 1184 * mm_struct (@mm_users count as 1). 1185 * 1186 * Use mmgrab()/mmdrop() to modify. When this drops to 1187 * 0, the &struct mm_struct is freed. 1188 */ 1189 atomic_t mm_count; 1190 } ____cacheline_aligned_in_smp; 1191 1192 struct maple_tree mm_mt; 1193 1194 unsigned long mmap_base; /* base of mmap area */ 1195 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */ 1196 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES 1197 /* Base addresses for compatible mmap() */ 1198 unsigned long mmap_compat_base; 1199 unsigned long mmap_compat_legacy_base; 1200 #endif 1201 unsigned long task_size; /* size of task vm space */ 1202 pgd_t * pgd; 1203 1204 #ifdef CONFIG_MEMBARRIER 1205 /** 1206 * @membarrier_state: Flags controlling membarrier behavior. 1207 * 1208 * This field is close to @pgd to hopefully fit in the same 1209 * cache-line, which needs to be touched by switch_mm(). 1210 */ 1211 atomic_t membarrier_state; 1212 #endif 1213 1214 /** 1215 * @mm_users: The number of users including userspace. 1216 * 1217 * Use mmget()/mmget_not_zero()/mmput() to modify. When this 1218 * drops to 0 (i.e. when the task exits and there are no other 1219 * temporary reference holders), we also release a reference on 1220 * @mm_count (which may then free the &struct mm_struct if 1221 * @mm_count also drops to 0). 1222 */ 1223 atomic_t mm_users; 1224 1225 /* MM CID related storage */ 1226 struct mm_mm_cid mm_cid; 1227 1228 /* sched_cache related statistics */ 1229 struct sched_cache_group *sched_cache_grp; 1230 #ifdef CONFIG_MMU 1231 atomic_long_t pgtables_bytes; /* size of all page tables */ 1232 #endif 1233 int map_count; /* number of VMAs */ 1234 1235 spinlock_t page_table_lock; /* Protects page tables and some 1236 * counters 1237 */ 1238 /* 1239 * Typically the current mmap_lock's offset is 56 bytes from 1240 * the last cacheline boundary, which is very optimal, as 1241 * its two hot fields 'count' and 'owner' sit in 2 different 1242 * cachelines, and when mmap_lock is highly contended, both 1243 * of the 2 fields will be accessed frequently, current layout 1244 * will help to reduce cache bouncing. 1245 * 1246 * So please be careful with adding new fields before 1247 * mmap_lock, which can easily push the 2 fields into one 1248 * cacheline. 1249 */ 1250 struct rw_semaphore mmap_lock; 1251 1252 struct list_head mmlist; /* List of maybe swapped mm's. These 1253 * are globally strung together off 1254 * init_mm.mmlist, and are protected 1255 * by mmlist_lock 1256 */ 1257 #ifdef CONFIG_PER_VMA_LOCK 1258 struct rcuwait vma_writer_wait; 1259 /* 1260 * This field has lock-like semantics, meaning it is sometimes 1261 * accessed with ACQUIRE/RELEASE semantics. 1262 * Roughly speaking, incrementing the sequence number is 1263 * equivalent to releasing locks on VMAs; reading the sequence 1264 * number can be part of taking a read lock on a VMA. 1265 * Incremented every time mmap_lock is write-locked/unlocked. 1266 * Initialized to 0, therefore odd values indicate mmap_lock 1267 * is write-locked and even values that it's released. 1268 * 1269 * Can be modified under write mmap_lock using RELEASE 1270 * semantics. 1271 * Can be read with no other protection when holding write 1272 * mmap_lock. 1273 * Can be read with ACQUIRE semantics if not holding write 1274 * mmap_lock. 1275 */ 1276 seqcount_t mm_lock_seq; 1277 #endif 1278 struct futex_mm_data futex; 1279 1280 unsigned long hiwater_rss; /* High-watermark of RSS usage */ 1281 unsigned long hiwater_vm; /* High-water virtual memory usage */ 1282 1283 unsigned long total_vm; /* Total pages mapped */ 1284 unsigned long locked_vm; /* Pages that have PG_mlocked set */ 1285 atomic64_t pinned_vm; /* Refcount permanently increased */ 1286 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */ 1287 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */ 1288 unsigned long stack_vm; /* VM_STACK */ 1289 union { 1290 /* Temporary while VMA flags are being converted. */ 1291 vm_flags_t def_flags; 1292 vma_flags_t def_vma_flags; 1293 }; 1294 1295 /** 1296 * @write_protect_seq: Locked when any thread is write 1297 * protecting pages mapped by this mm to enforce a later COW, 1298 * for instance during page table copying for fork(). 1299 */ 1300 seqcount_t write_protect_seq; 1301 1302 spinlock_t arg_lock; /* protect the below fields */ 1303 1304 unsigned long start_code, end_code, start_data, end_data; 1305 unsigned long start_brk, brk, start_stack; 1306 unsigned long arg_start, arg_end, env_start, env_end; 1307 1308 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */ 1309 1310 #ifdef CONFIG_ARCH_HAS_ELF_CORE_EFLAGS 1311 /* the ABI-related flags from the ELF header. Used for core dump */ 1312 unsigned long saved_e_flags; 1313 #endif 1314 1315 struct percpu_counter rss_stat[NR_MM_COUNTERS]; 1316 1317 struct linux_binfmt *binfmt; 1318 1319 /* Architecture-specific MM context */ 1320 mm_context_t context; 1321 1322 mm_flags_t flags; /* Must use mm_flags_* hlpers to access */ 1323 1324 #ifdef CONFIG_AIO 1325 spinlock_t ioctx_lock; 1326 struct kioctx_table __rcu *ioctx_table; 1327 #endif 1328 #ifdef CONFIG_MEMCG 1329 /* 1330 * "owner" points to a task that is regarded as the canonical 1331 * user/owner of this mm. All of the following must be true in 1332 * order for it to be changed: 1333 * 1334 * current == mm->owner 1335 * current->mm != mm 1336 * new_owner->mm == mm 1337 * new_owner->alloc_lock is held 1338 */ 1339 struct task_struct __rcu *owner; 1340 #endif 1341 1342 /* store ref to file /proc/<pid>/exe symlink points to */ 1343 struct file __rcu *exe_file; 1344 #ifdef CONFIG_MMU_NOTIFIER 1345 struct mmu_notifier_subscriptions *notifier_subscriptions; 1346 #endif 1347 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS) 1348 pgtable_t pmd_huge_pte; /* protected by page_table_lock */ 1349 #endif 1350 #ifdef CONFIG_NUMA_BALANCING 1351 /* 1352 * numa_next_scan is the next time that PTEs will be remapped 1353 * PROT_NONE to trigger NUMA hinting faults; such faults gather 1354 * statistics and migrate pages to new nodes if necessary. 1355 */ 1356 unsigned long numa_next_scan; 1357 1358 /* Restart point for scanning and remapping PTEs. */ 1359 unsigned long numa_scan_offset; 1360 1361 /* numa_scan_seq prevents two threads remapping PTEs. */ 1362 int numa_scan_seq; 1363 #endif 1364 /* 1365 * An operation with batched TLB flushing is going on. Anything 1366 * that can move process memory needs to flush the TLB when 1367 * moving a PROT_NONE mapped page. 1368 */ 1369 atomic_t tlb_flush_pending; 1370 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1371 /* See flush_tlb_batched_pending() */ 1372 atomic_t tlb_flush_batched; 1373 #endif 1374 struct uprobes_state uprobes_state; 1375 #ifdef CONFIG_PREEMPT_RT 1376 struct rcu_head delayed_drop; 1377 #endif 1378 #ifdef CONFIG_HUGETLB_PAGE 1379 atomic_long_t hugetlb_usage; 1380 #endif 1381 struct work_struct async_put_work; 1382 1383 #ifdef CONFIG_IOMMU_MM_DATA 1384 struct iommu_mm_data *iommu_mm; 1385 #endif 1386 #ifdef CONFIG_KSM 1387 /* 1388 * Represent how many pages of this process are involved in KSM 1389 * merging (not including ksm_zero_pages). 1390 */ 1391 unsigned long ksm_merging_pages; 1392 /* 1393 * Represent how many pages are checked for ksm merging 1394 * including merged and not merged. 1395 */ 1396 unsigned long ksm_rmap_items; 1397 /* 1398 * Represent how many empty pages are merged with kernel zero 1399 * pages when enabling KSM use_zero_pages. 1400 */ 1401 atomic_long_t ksm_zero_pages; 1402 #endif /* CONFIG_KSM */ 1403 #ifdef CONFIG_LRU_GEN_WALKS_MMU 1404 struct { 1405 /* this mm_struct is on lru_gen_mm_list */ 1406 struct list_head list; 1407 /* 1408 * Set when switching to this mm_struct, as a hint of 1409 * whether it has been used since the last time per-node 1410 * page table walkers cleared the corresponding bits. 1411 */ 1412 unsigned long bitmap; 1413 #ifdef CONFIG_MEMCG 1414 /* points to the memcg of "owner" above */ 1415 struct mem_cgroup *memcg; 1416 #endif 1417 } lru_gen; 1418 #endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1419 #ifdef CONFIG_MM_ID 1420 mm_id_t mm_id; 1421 #endif /* CONFIG_MM_ID */ 1422 } __randomize_layout; 1423 1424 /* 1425 * The mm_cpumask needs to be at the end of mm_struct, because it 1426 * is dynamically sized based on nr_cpu_ids. 1427 */ 1428 char flexible_array[] __aligned(__alignof__(unsigned long)); 1429 }; 1430 1431 /* Copy value to the first system word of mm flags, non-atomically. */ 1432 static inline void __mm_flags_overwrite_word(struct mm_struct *mm, unsigned long value) 1433 { 1434 *ACCESS_PRIVATE(&mm->flags, __mm_flags) = value; 1435 } 1436 1437 /* Obtain a read-only view of the mm flags bitmap. */ 1438 static inline const unsigned long *__mm_flags_get_bitmap(const struct mm_struct *mm) 1439 { 1440 return (const unsigned long *)ACCESS_PRIVATE(&mm->flags, __mm_flags); 1441 } 1442 1443 /* Read the first system word of mm flags, non-atomically. */ 1444 static inline unsigned long __mm_flags_get_word(const struct mm_struct *mm) 1445 { 1446 return *__mm_flags_get_bitmap(mm); 1447 } 1448 1449 /* 1450 * Update the first system word of mm flags ONLY, applying the specified mask to 1451 * it, then setting all flags specified by bits. 1452 */ 1453 static inline void __mm_flags_set_mask_bits_word(struct mm_struct *mm, 1454 unsigned long mask, unsigned long bits) 1455 { 1456 unsigned long *bitmap = ACCESS_PRIVATE(&mm->flags, __mm_flags); 1457 1458 set_mask_bits(bitmap, mask, bits); 1459 } 1460 1461 #define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \ 1462 MT_FLAGS_USE_RCU) 1463 extern struct mm_struct init_mm; 1464 1465 #define MM_STRUCT_FLEXIBLE_ARRAY_INIT \ 1466 { \ 1467 [0 ... sizeof(cpumask_t) + MM_CID_STATIC_SIZE - 1] = 0 \ 1468 } 1469 1470 /* Pointer magic because the dynamic array size confuses some compilers. */ 1471 static inline void mm_init_cpumask(struct mm_struct *mm) 1472 { 1473 unsigned long cpu_bitmap = (unsigned long)mm; 1474 1475 cpu_bitmap += offsetof(struct mm_struct, flexible_array); 1476 cpumask_clear((struct cpumask *)cpu_bitmap); 1477 } 1478 1479 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */ 1480 static inline cpumask_t *mm_cpumask(struct mm_struct *mm) 1481 { 1482 return (struct cpumask *)&mm->flexible_array; 1483 } 1484 1485 #ifdef CONFIG_LRU_GEN 1486 1487 struct lru_gen_mm_list { 1488 /* mm_struct list for page table walkers */ 1489 struct list_head fifo; 1490 /* protects the list above */ 1491 spinlock_t lock; 1492 }; 1493 1494 #endif /* CONFIG_LRU_GEN */ 1495 1496 #ifdef CONFIG_LRU_GEN_WALKS_MMU 1497 1498 void lru_gen_add_mm(struct mm_struct *mm); 1499 void lru_gen_del_mm(struct mm_struct *mm); 1500 void lru_gen_migrate_mm(struct mm_struct *mm); 1501 1502 static inline void lru_gen_init_mm(struct mm_struct *mm) 1503 { 1504 INIT_LIST_HEAD(&mm->lru_gen.list); 1505 mm->lru_gen.bitmap = 0; 1506 #ifdef CONFIG_MEMCG 1507 mm->lru_gen.memcg = NULL; 1508 #endif 1509 } 1510 1511 static inline void lru_gen_use_mm(struct mm_struct *mm) 1512 { 1513 /* 1514 * When the bitmap is set, page reclaim knows this mm_struct has been 1515 * used since the last time it cleared the bitmap. So it might be worth 1516 * walking the page tables of this mm_struct to clear the accessed bit. 1517 */ 1518 WRITE_ONCE(mm->lru_gen.bitmap, -1); 1519 } 1520 1521 #else /* !CONFIG_LRU_GEN_WALKS_MMU */ 1522 1523 static inline void lru_gen_add_mm(struct mm_struct *mm) 1524 { 1525 } 1526 1527 static inline void lru_gen_del_mm(struct mm_struct *mm) 1528 { 1529 } 1530 1531 static inline void lru_gen_migrate_mm(struct mm_struct *mm) 1532 { 1533 } 1534 1535 static inline void lru_gen_init_mm(struct mm_struct *mm) 1536 { 1537 } 1538 1539 static inline void lru_gen_use_mm(struct mm_struct *mm) 1540 { 1541 } 1542 1543 #endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1544 1545 struct vma_iterator { 1546 struct ma_state mas; 1547 }; 1548 1549 #define VMA_ITERATOR(name, __mm, __addr) \ 1550 struct vma_iterator name = { \ 1551 .mas = { \ 1552 .tree = &(__mm)->mm_mt, \ 1553 .index = __addr, \ 1554 .node = NULL, \ 1555 .status = ma_start, \ 1556 }, \ 1557 } 1558 1559 static inline void vma_iter_init(struct vma_iterator *vmi, 1560 struct mm_struct *mm, unsigned long addr) 1561 { 1562 mas_init(&vmi->mas, &mm->mm_mt, addr); 1563 } 1564 1565 #ifdef CONFIG_SCHED_MM_CID 1566 /* 1567 * mm_cpus_allowed: Union of all mm's threads allowed CPUs. 1568 */ 1569 static inline cpumask_t *mm_cpus_allowed(struct mm_struct *mm) 1570 { 1571 unsigned long bitmap = (unsigned long)mm; 1572 1573 bitmap += offsetof(struct mm_struct, flexible_array); 1574 /* Skip cpu_bitmap */ 1575 bitmap += cpumask_size(); 1576 return (struct cpumask *)bitmap; 1577 } 1578 1579 /* Accessor for struct mm_struct's cidmask. */ 1580 static inline unsigned long *mm_cidmask(struct mm_struct *mm) 1581 { 1582 unsigned long cid_bitmap = (unsigned long)mm_cpus_allowed(mm); 1583 1584 /* Skip mm_cpus_allowed */ 1585 cid_bitmap += cpumask_size(); 1586 return (unsigned long *)cid_bitmap; 1587 } 1588 1589 void mm_init_cid(struct mm_struct *mm, struct task_struct *p); 1590 1591 static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p) 1592 { 1593 mm->mm_cid.pcpu = alloc_percpu_noprof(struct mm_cid_pcpu); 1594 if (!mm->mm_cid.pcpu) 1595 return -ENOMEM; 1596 mm_init_cid(mm, p); 1597 return 0; 1598 } 1599 # define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__)) 1600 1601 static inline void mm_destroy_cid(struct mm_struct *mm) 1602 { 1603 free_percpu(mm->mm_cid.pcpu); 1604 mm->mm_cid.pcpu = NULL; 1605 } 1606 1607 static inline unsigned int mm_cid_size(void) 1608 { 1609 /* mm_cpus_allowed(), mm_cidmask(). */ 1610 return cpumask_size() + bitmap_size(num_possible_cpus()); 1611 } 1612 1613 /* Use 2 * NR_CPUS as worse case for static allocation. */ 1614 # define MM_CID_STATIC_SIZE (2 * sizeof(cpumask_t)) 1615 #else /* CONFIG_SCHED_MM_CID */ 1616 static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { } 1617 static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; } 1618 static inline void mm_destroy_cid(struct mm_struct *mm) { } 1619 static inline unsigned int mm_cid_size(void) 1620 { 1621 return 0; 1622 } 1623 # define MM_CID_STATIC_SIZE 0 1624 #endif /* CONFIG_SCHED_MM_CID */ 1625 1626 #ifdef CONFIG_SCHED_CACHE 1627 int mm_init_sched(struct mm_struct *mm, 1628 struct sched_cache_time __percpu *pcpu_sched); 1629 void mm_destroy_sched(struct mm_struct *mm); 1630 1631 static inline int mm_alloc_sched_noprof(struct mm_struct *mm) 1632 { 1633 struct sched_cache_time __percpu *pcpu_sched = 1634 alloc_percpu_noprof(struct sched_cache_time); 1635 1636 if (!pcpu_sched) 1637 return -ENOMEM; 1638 1639 return mm_init_sched(mm, pcpu_sched); 1640 } 1641 1642 #define mm_alloc_sched(...) alloc_hooks(mm_alloc_sched_noprof(__VA_ARGS__)) 1643 1644 #else /* !CONFIG_SCHED_CACHE */ 1645 1646 static inline int mm_alloc_sched(struct mm_struct *mm) { return 0; } 1647 static inline void mm_destroy_sched(struct mm_struct *mm) { } 1648 1649 #endif /* CONFIG_SCHED_CACHE */ 1650 1651 struct mmu_gather; 1652 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm); 1653 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm); 1654 void tlb_gather_mmu_vma(struct mmu_gather *tlb, struct vm_area_struct *vma); 1655 extern void tlb_finish_mmu(struct mmu_gather *tlb); 1656 1657 struct vm_fault; 1658 1659 /** 1660 * typedef vm_fault_t - Return type for page fault handlers. 1661 * 1662 * Page fault handlers return a bitmask of %VM_FAULT values. 1663 */ 1664 typedef __bitwise unsigned int vm_fault_t; 1665 1666 /** 1667 * enum vm_fault_reason - Page fault handlers return a bitmask of 1668 * these values to tell the core VM what happened when handling the 1669 * fault. Used to decide whether a process gets delivered SIGBUS or 1670 * just gets major/minor fault counters bumped up. 1671 * 1672 * @VM_FAULT_OOM: Out Of Memory 1673 * @VM_FAULT_SIGBUS: Bad access 1674 * @VM_FAULT_MAJOR: Page read from storage 1675 * @VM_FAULT_HWPOISON: Hit poisoned small page 1676 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded 1677 * in upper bits 1678 * @VM_FAULT_SIGSEGV: segmentation fault 1679 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page 1680 * @VM_FAULT_LOCKED: ->fault locked the returned page 1681 * @VM_FAULT_RETRY: ->fault blocked, must retry 1682 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small 1683 * @VM_FAULT_DONE_COW: ->fault has fully handled COW 1684 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs 1685 * fsync() to complete (for synchronous page faults 1686 * in DAX) 1687 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released 1688 * @VM_FAULT_HINDEX_MASK: mask HINDEX value 1689 * 1690 */ 1691 enum vm_fault_reason { 1692 VM_FAULT_OOM = (__force vm_fault_t)0x000001, 1693 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002, 1694 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004, 1695 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010, 1696 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020, 1697 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040, 1698 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100, 1699 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200, 1700 VM_FAULT_RETRY = (__force vm_fault_t)0x000400, 1701 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800, 1702 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, 1703 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, 1704 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000, 1705 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, 1706 }; 1707 1708 /* Encode hstate index for a hwpoisoned large page */ 1709 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16)) 1710 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf) 1711 1712 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \ 1713 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \ 1714 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) 1715 1716 #define VM_FAULT_RESULT_TRACE \ 1717 { (__force u32)VM_FAULT_OOM, "OOM" }, \ 1718 { (__force u32)VM_FAULT_SIGBUS, "SIGBUS" }, \ 1719 { (__force u32)VM_FAULT_MAJOR, "MAJOR" }, \ 1720 { (__force u32)VM_FAULT_HWPOISON, "HWPOISON" }, \ 1721 { (__force u32)VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \ 1722 { (__force u32)VM_FAULT_SIGSEGV, "SIGSEGV" }, \ 1723 { (__force u32)VM_FAULT_NOPAGE, "NOPAGE" }, \ 1724 { (__force u32)VM_FAULT_LOCKED, "LOCKED" }, \ 1725 { (__force u32)VM_FAULT_RETRY, "RETRY" }, \ 1726 { (__force u32)VM_FAULT_FALLBACK, "FALLBACK" }, \ 1727 { (__force u32)VM_FAULT_DONE_COW, "DONE_COW" }, \ 1728 { (__force u32)VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ 1729 { (__force u32)VM_FAULT_COMPLETED, "COMPLETED" } 1730 1731 struct vm_special_mapping { 1732 const char *name; /* The name, e.g. "[vdso]". */ 1733 1734 /* 1735 * If .fault is not provided, this points to a 1736 * NULL-terminated array of pages that back the special mapping. 1737 * 1738 * This must not be NULL unless .fault is provided. 1739 */ 1740 struct page **pages; 1741 1742 /* 1743 * If non-NULL, then this is called to resolve page faults 1744 * on the special mapping. If used, .pages is not checked. 1745 */ 1746 vm_fault_t (*fault)(const struct vm_special_mapping *sm, 1747 struct vm_area_struct *vma, 1748 struct vm_fault *vmf); 1749 1750 int (*mremap)(const struct vm_special_mapping *sm, 1751 struct vm_area_struct *new_vma); 1752 1753 void (*close)(const struct vm_special_mapping *sm, 1754 struct vm_area_struct *vma); 1755 }; 1756 1757 enum tlb_flush_reason { 1758 TLB_FLUSH_ON_TASK_SWITCH, 1759 TLB_REMOTE_SHOOTDOWN, 1760 TLB_LOCAL_SHOOTDOWN, 1761 TLB_LOCAL_MM_SHOOTDOWN, 1762 TLB_REMOTE_SEND_IPI, 1763 TLB_REMOTE_WRONG_CPU, 1764 }; 1765 1766 /** 1767 * enum fault_flag - Fault flag definitions. 1768 * @FAULT_FLAG_WRITE: Fault was a write fault. 1769 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE. 1770 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked. 1771 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying. 1772 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region. 1773 * @FAULT_FLAG_TRIED: The fault has been tried once. 1774 * @FAULT_FLAG_USER: The fault originated in userspace. 1775 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm. 1776 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. 1777 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. 1778 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a 1779 * COW mapping, making sure that an exclusive anon page is 1780 * mapped after the fault. 1781 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. 1782 * We should only access orig_pte if this flag set. 1783 * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock. 1784 * 1785 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify 1786 * whether we would allow page faults to retry by specifying these two 1787 * fault flags correctly. Currently there can be three legal combinations: 1788 * 1789 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and 1790 * this is the first try 1791 * 1792 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and 1793 * we've already tried at least once 1794 * 1795 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry 1796 * 1797 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never 1798 * be used. Note that page faults can be allowed to retry for multiple times, 1799 * in which case we'll have an initial fault with flags (a) then later on 1800 * continuous faults with flags (b). We should always try to detect pending 1801 * signals before a retry to make sure the continuous page faults can still be 1802 * interrupted if necessary. 1803 * 1804 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal. 1805 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when 1806 * applied to mappings that are not COW mappings. 1807 */ 1808 enum fault_flag { 1809 FAULT_FLAG_WRITE = 1 << 0, 1810 FAULT_FLAG_MKWRITE = 1 << 1, 1811 FAULT_FLAG_ALLOW_RETRY = 1 << 2, 1812 FAULT_FLAG_RETRY_NOWAIT = 1 << 3, 1813 FAULT_FLAG_KILLABLE = 1 << 4, 1814 FAULT_FLAG_TRIED = 1 << 5, 1815 FAULT_FLAG_USER = 1 << 6, 1816 FAULT_FLAG_REMOTE = 1 << 7, 1817 FAULT_FLAG_INSTRUCTION = 1 << 8, 1818 FAULT_FLAG_INTERRUPTIBLE = 1 << 9, 1819 FAULT_FLAG_UNSHARE = 1 << 10, 1820 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11, 1821 FAULT_FLAG_VMA_LOCK = 1 << 12, 1822 }; 1823 1824 typedef unsigned int __bitwise zap_flags_t; 1825 1826 /* Flags for clear_young_dirty_ptes(). */ 1827 typedef int __bitwise cydp_t; 1828 1829 /* Clear the access bit */ 1830 #define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0)) 1831 1832 /* Clear the dirty bit */ 1833 #define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1)) 1834 1835 /* 1836 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each 1837 * other. Here is what they mean, and how to use them: 1838 * 1839 * 1840 * FIXME: For pages which are part of a filesystem, mappings are subject to the 1841 * lifetime enforced by the filesystem and we need guarantees that longterm 1842 * users like RDMA and V4L2 only establish mappings which coordinate usage with 1843 * the filesystem. Ideas for this coordination include revoking the longterm 1844 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was 1845 * added after the problem with filesystems was found FS DAX VMAs are 1846 * specifically failed. Filesystem pages are still subject to bugs and use of 1847 * FOLL_LONGTERM should be avoided on those pages. 1848 * 1849 * In the CMA case: long term pins in a CMA region would unnecessarily fragment 1850 * that region. And so, CMA attempts to migrate the page before pinning, when 1851 * FOLL_LONGTERM is specified. 1852 * 1853 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount, 1854 * but an additional pin counting system) will be invoked. This is intended for 1855 * anything that gets a page reference and then touches page data (for example, 1856 * Direct IO). This lets the filesystem know that some non-file-system entity is 1857 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages 1858 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by 1859 * a call to unpin_user_page(). 1860 * 1861 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different 1862 * and separate refcounting mechanisms, however, and that means that each has 1863 * its own acquire and release mechanisms: 1864 * 1865 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release. 1866 * 1867 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release. 1868 * 1869 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call. 1870 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based 1871 * calls applied to them, and that's perfectly OK. This is a constraint on the 1872 * callers, not on the pages.) 1873 * 1874 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never 1875 * directly by the caller. That's in order to help avoid mismatches when 1876 * releasing pages: get_user_pages*() pages must be released via put_page(), 1877 * while pin_user_pages*() pages must be released via unpin_user_page(). 1878 * 1879 * Please see Documentation/core-api/pin_user_pages.rst for more information. 1880 */ 1881 1882 enum { 1883 /* check pte is writable */ 1884 FOLL_WRITE = 1 << 0, 1885 /* do get_page on page */ 1886 FOLL_GET = 1 << 1, 1887 /* give error on hole if it would be zero */ 1888 FOLL_DUMP = 1 << 2, 1889 /* get_user_pages read/write w/o permission */ 1890 FOLL_FORCE = 1 << 3, 1891 /* 1892 * if a disk transfer is needed, start the IO and return without waiting 1893 * upon it 1894 */ 1895 FOLL_NOWAIT = 1 << 4, 1896 /* do not fault in pages */ 1897 FOLL_NOFAULT = 1 << 5, 1898 /* check page is hwpoisoned */ 1899 FOLL_HWPOISON = 1 << 6, 1900 /* don't do file mappings */ 1901 FOLL_ANON = 1 << 7, 1902 /* 1903 * FOLL_LONGTERM indicates that the page will be held for an indefinite 1904 * time period _often_ under userspace control. This is in contrast to 1905 * iov_iter_get_pages(), whose usages are transient. 1906 */ 1907 FOLL_LONGTERM = 1 << 8, 1908 /* split huge pmd before returning */ 1909 FOLL_SPLIT_PMD = 1 << 9, 1910 /* allow returning PCI P2PDMA pages */ 1911 FOLL_PCI_P2PDMA = 1 << 10, 1912 /* allow interrupts from generic signals */ 1913 FOLL_INTERRUPTIBLE = 1 << 11, 1914 /* 1915 * Always honor (trigger) NUMA hinting faults. 1916 * 1917 * FOLL_WRITE implicitly honors NUMA hinting faults because a 1918 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE 1919 * apply). get_user_pages_fast_only() always implicitly honors NUMA 1920 * hinting faults. 1921 */ 1922 FOLL_HONOR_NUMA_FAULT = 1 << 12, 1923 1924 /* See also internal only FOLL flags in mm/internal.h */ 1925 }; 1926 1927 /* mm flags */ 1928 1929 /* 1930 * Bits 0 and 1 were dumpability; that moved to task->exec_state. Reserve 1931 * the bits so MMF_DUMP_FILTER_* positions stay stable for the 1932 * /proc/<pid>/coredump_filter ABI. 1933 */ 1934 #define MMF_DUMPABLE_BITS 2 1935 /* coredump filter bits */ 1936 #define MMF_DUMP_ANON_PRIVATE 2 1937 #define MMF_DUMP_ANON_SHARED 3 1938 #define MMF_DUMP_MAPPED_PRIVATE 4 1939 #define MMF_DUMP_MAPPED_SHARED 5 1940 #define MMF_DUMP_ELF_HEADERS 6 1941 #define MMF_DUMP_HUGETLB_PRIVATE 7 1942 #define MMF_DUMP_HUGETLB_SHARED 8 1943 #define MMF_DUMP_DAX_PRIVATE 9 1944 #define MMF_DUMP_DAX_SHARED 10 1945 1946 #define MMF_DUMP_FILTER_SHIFT MMF_DUMPABLE_BITS 1947 #define MMF_DUMP_FILTER_BITS 9 1948 #define MMF_DUMP_FILTER_MASK \ 1949 ((BIT(MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT) 1950 #define MMF_DUMP_FILTER_DEFAULT \ 1951 (BIT(MMF_DUMP_ANON_PRIVATE) | BIT(MMF_DUMP_ANON_SHARED) | \ 1952 BIT(MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF) 1953 1954 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS 1955 # define MMF_DUMP_MASK_DEFAULT_ELF BIT(MMF_DUMP_ELF_HEADERS) 1956 #else 1957 # define MMF_DUMP_MASK_DEFAULT_ELF 0 1958 #endif 1959 /* leave room for more dump flags */ 1960 #define MMF_VM_MERGEABLE 16 /* KSM may merge identical pages */ 1961 #define MMF_VM_HUGEPAGE 17 /* set when mm is available for khugepaged */ 1962 1963 #define MMF_HUGE_ZERO_FOLIO 18 /* mm has ever used the global huge zero folio */ 1964 1965 #define MMF_HAS_UPROBES 19 /* has uprobes */ 1966 #define MMF_RECALC_UPROBES 20 /* MMF_HAS_UPROBES can be wrong */ 1967 #define MMF_OOM_SKIP 21 /* mm is of no interest for the OOM killer */ 1968 #define MMF_UNSTABLE 22 /* mm is unstable for copy_from_user */ 1969 #define MMF_DISABLE_THP_EXCEPT_ADVISED 23 /* no THP except when advised (e.g., VM_HUGEPAGE) */ 1970 #define MMF_DISABLE_THP_COMPLETELY 24 /* no THP for all VMAs */ 1971 #define MMF_DISABLE_THP_MASK (BIT(MMF_DISABLE_THP_COMPLETELY) | \ 1972 BIT(MMF_DISABLE_THP_EXCEPT_ADVISED)) 1973 #define MMF_OOM_REAP_QUEUED 25 /* mm was queued for oom_reaper */ 1974 #define MMF_MULTIPROCESS 26 /* mm is shared between processes */ 1975 /* 1976 * MMF_HAS_PINNED: Whether this mm has pinned any pages. This can be either 1977 * replaced in the future by mm.pinned_vm when it becomes stable, or grow into 1978 * a counter on its own. We're aggresive on this bit for now: even if the 1979 * pinned pages were unpinned later on, we'll still keep this bit set for the 1980 * lifecycle of this mm, just for simplicity. 1981 */ 1982 #define MMF_HAS_PINNED 27 /* FOLL_PIN has run, never cleared */ 1983 1984 #define MMF_HAS_MDWE 28 1985 #define MMF_HAS_MDWE_MASK BIT(MMF_HAS_MDWE) 1986 1987 #define MMF_HAS_MDWE_NO_INHERIT 29 1988 1989 #define MMF_VM_MERGE_ANY 30 1990 #define MMF_VM_MERGE_ANY_MASK BIT(MMF_VM_MERGE_ANY) 1991 1992 #define MMF_TOPDOWN 31 /* mm searches top down by default */ 1993 #define MMF_TOPDOWN_MASK BIT(MMF_TOPDOWN) 1994 1995 #define MMF_INIT_LEGACY_MASK (MMF_DUMP_FILTER_MASK |\ 1996 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ 1997 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK) 1998 1999 /* Legacy flags must fit within 32 bits. */ 2000 static_assert((u64)MMF_INIT_LEGACY_MASK <= (u64)UINT_MAX); 2001 2002 /* 2003 * Initialise legacy flags according to masks, propagating selected flags on 2004 * fork. Further flag manipulation can be performed by the caller. 2005 */ 2006 static inline unsigned long mmf_init_legacy_flags(unsigned long flags) 2007 { 2008 if (flags & (1UL << MMF_HAS_MDWE_NO_INHERIT)) 2009 flags &= ~((1UL << MMF_HAS_MDWE) | 2010 (1UL << MMF_HAS_MDWE_NO_INHERIT)); 2011 return flags & MMF_INIT_LEGACY_MASK; 2012 } 2013 2014 #endif /* _LINUX_MM_TYPES_H */ 2015