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. 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, it 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 /* 954 * Can only be written (using WRITE_ONCE()) while holding both: 955 * - mmap_lock (in write mode) 956 * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_FLAG is set 957 * Can be read reliably while holding one of: 958 * - mmap_lock (in read or write mode) 959 * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_BIT is set or vm_refcnt > 1 960 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout 961 * while holding nothing (except RCU to keep the VMA struct allocated). 962 * 963 * This sequence counter is explicitly allowed to overflow; sequence 964 * counter reuse can only lead to occasional unnecessary use of the 965 * slowpath. 966 */ 967 unsigned int vm_lock_seq; 968 969 /* 970 * Low 32-bits of anonymous page offset. 971 * See vma_start_anon_pgoff() comment for details. 972 */ 973 unsigned int __vm_anon_pgoff_lo; 974 /* 975 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma 976 * list, after a COW of one of the file pages. A MAP_SHARED vma 977 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack 978 * or brk vma (with NULL file) can only be in an anon_vma list. 979 */ 980 struct list_head anon_vma_chain; /* Serialized by mmap_lock & 981 * page_table_lock */ 982 struct anon_vma *anon_vma; /* Serialized by page_table_lock */ 983 984 /* Function pointers to deal with this struct. */ 985 const struct vm_operations_struct *vm_ops; 986 987 /* Information about our backing store: */ 988 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE 989 units */ 990 struct file * vm_file; /* File we map to (can be NULL). */ 991 void * vm_private_data; /* was vm_pte (shared mem) */ 992 993 #ifdef CONFIG_SWAP 994 atomic_long_t swap_readahead_info; 995 #endif 996 #ifndef CONFIG_MMU 997 struct vm_region *vm_region; /* NOMMU mapping region */ 998 #endif 999 #ifdef CONFIG_NUMA 1000 struct mempolicy *vm_policy; /* NUMA policy for the VMA */ 1001 #endif 1002 #ifdef CONFIG_NUMA_BALANCING 1003 struct vma_numab_state *numab_state; /* NUMA Balancing state */ 1004 #endif 1005 /* 1006 * Used to keep track of firstly, whether the VMA is attached, secondly, 1007 * if attached, how many read locks are taken, and thirdly, if the 1008 * VM_REFCNT_EXCLUDE_READERS_FLAG is set, whether any read locks held 1009 * are currently in the process of being excluded. 1010 * 1011 * This value can be equal to: 1012 * 1013 * 0 - Detached. IMPORTANT: when the refcnt is zero, readers cannot 1014 * increment it. 1015 * 1016 * 1 - Attached and either unlocked or write-locked. Write locks are 1017 * identified via __is_vma_write_locked() which checks for equality of 1018 * vma->vm_lock_seq and mm->mm_lock_seq. 1019 * 1020 * >1, < VM_REFCNT_EXCLUDE_READERS_FLAG - Read-locked or (unlikely) 1021 * write-locked with other threads having temporarily incremented the 1022 * reference count prior to determining it is write-locked and 1023 * decrementing it again. 1024 * 1025 * VM_REFCNT_EXCLUDE_READERS_FLAG - Detached, pending 1026 * __vma_end_exclude_readers() completion which will decrement the 1027 * reference count to zero. IMPORTANT - at this stage no further readers 1028 * can increment the reference count. It can only be reduced. 1029 * 1030 * VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either write-locking 1031 * an attached VMA and has yet to invoke __vma_end_exclude_readers(), 1032 * OR a thread is detaching a VMA and is waiting on a single spurious 1033 * reader in order to decrement the reference count. IMPORTANT - as 1034 * above, no further readers can increment the reference count. 1035 * 1036 * > VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either 1037 * write-locking or detaching a VMA is waiting on readers to 1038 * exit. IMPORTANT - as above, no further readers can increment the 1039 * reference count. 1040 * 1041 * NOTE: Unstable RCU readers are allowed to read this. 1042 */ 1043 refcount_t vm_refcnt ____cacheline_aligned_in_smp; 1044 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1045 struct lockdep_map vmlock_dep_map; 1046 #endif 1047 #ifdef CONFIG_64BIT 1048 /* 1049 * High 32-bits of anonymous page offset. 1050 * See vma_start_anon_pgoff() comment for details. 1051 */ 1052 unsigned int __vm_anon_pgoff_hi; 1053 #endif 1054 /* 1055 * For areas with an address space and backing store, 1056 * linkage into the address_space->i_mmap interval tree. 1057 * 1058 */ 1059 struct { 1060 struct rb_node rb; 1061 unsigned long rb_subtree_last; 1062 } shared; 1063 #ifdef CONFIG_ANON_VMA_NAME 1064 /* 1065 * For private and shared anonymous mappings, a pointer to a null 1066 * terminated string containing the name given to the vma, or NULL if 1067 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access. 1068 */ 1069 struct anon_vma_name *anon_name; 1070 #endif 1071 struct vm_userfaultfd_ctx vm_userfaultfd_ctx; 1072 #ifdef __HAVE_PFNMAP_TRACKING 1073 struct pfnmap_track_ctx *pfnmap_track_ctx; 1074 #endif 1075 } __randomize_layout; 1076 1077 /* Clears all bits in the VMA flags bitmap, non-atomically. */ 1078 static __always_inline void vma_flags_clear_all(vma_flags_t *flags) 1079 { 1080 bitmap_zero(flags->__vma_flags, NUM_VMA_FLAG_BITS); 1081 } 1082 1083 /* 1084 * Helper function which converts a vma_flags_t value to a legacy vm_flags_t 1085 * value. This is only valid if the input flags value can be expressed in a 1086 * system word. 1087 * 1088 * Will be removed once the conversion to VMA flags is complete. 1089 */ 1090 static __always_inline vm_flags_t vma_flags_to_legacy(vma_flags_t flags) 1091 { 1092 return (vm_flags_t)flags.__vma_flags[0]; 1093 } 1094 1095 /* 1096 * Copy value to the first system word of VMA flags, non-atomically. 1097 * 1098 * IMPORTANT: This does not overwrite bytes past the first system word. The 1099 * caller must account for this. 1100 */ 1101 static __always_inline void vma_flags_overwrite_word(vma_flags_t *flags, 1102 unsigned long value) 1103 { 1104 unsigned long *bitmap = flags->__vma_flags; 1105 1106 bitmap[0] = value; 1107 } 1108 1109 /* 1110 * Helper function which converts a legacy vm_flags_t value to a vma_flags_t 1111 * value. 1112 * 1113 * Will be removed once the conversion to VMA flags is complete. 1114 */ 1115 static __always_inline vma_flags_t legacy_to_vma_flags(vm_flags_t flags) 1116 { 1117 vma_flags_t ret = EMPTY_VMA_FLAGS; 1118 1119 vma_flags_overwrite_word(&ret, flags); 1120 return ret; 1121 } 1122 1123 /* 1124 * Copy value to the first system word of VMA flags ONCE, non-atomically. 1125 * 1126 * IMPORTANT: This does not overwrite bytes past the first system word. The 1127 * caller must account for this. 1128 */ 1129 static __always_inline void vma_flags_overwrite_word_once(vma_flags_t *flags, 1130 unsigned long value) 1131 { 1132 unsigned long *bitmap = flags->__vma_flags; 1133 1134 WRITE_ONCE(*bitmap, value); 1135 } 1136 1137 /* Update the first system word of VMA flags setting bits, non-atomically. */ 1138 static __always_inline void vma_flags_set_word(vma_flags_t *flags, 1139 unsigned long value) 1140 { 1141 unsigned long *bitmap = flags->__vma_flags; 1142 1143 *bitmap |= value; 1144 } 1145 1146 /* Update the first system word of VMA flags clearing bits, non-atomically. */ 1147 static __always_inline void vma_flags_clear_word(vma_flags_t *flags, 1148 unsigned long value) 1149 { 1150 unsigned long *bitmap = flags->__vma_flags; 1151 1152 *bitmap &= ~value; 1153 } 1154 1155 #ifdef CONFIG_NUMA 1156 #define vma_policy(vma) ((vma)->vm_policy) 1157 #else 1158 #define vma_policy(vma) NULL 1159 #endif 1160 1161 /* 1162 * Opaque type representing current mm_struct flag state. Must be accessed via 1163 * mm_flags_xxx() helper functions. 1164 */ 1165 #define NUM_MM_FLAG_BITS (64) 1166 typedef struct { 1167 DECLARE_BITMAP(__mm_flags, NUM_MM_FLAG_BITS); 1168 } __private mm_flags_t; 1169 1170 struct kioctx_table; 1171 struct iommu_mm_data; 1172 struct mm_struct { 1173 struct { 1174 /* 1175 * Fields which are often written to are placed in a separate 1176 * cache line. 1177 */ 1178 struct { 1179 /** 1180 * @mm_count: The number of references to &struct 1181 * mm_struct (@mm_users count as 1). 1182 * 1183 * Use mmgrab()/mmdrop() to modify. When this drops to 1184 * 0, the &struct mm_struct is freed. 1185 */ 1186 atomic_t mm_count; 1187 } ____cacheline_aligned_in_smp; 1188 1189 struct maple_tree mm_mt; 1190 1191 unsigned long mmap_base; /* base of mmap area */ 1192 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */ 1193 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES 1194 /* Base addresses for compatible mmap() */ 1195 unsigned long mmap_compat_base; 1196 unsigned long mmap_compat_legacy_base; 1197 #endif 1198 unsigned long task_size; /* size of task vm space */ 1199 pgd_t * pgd; 1200 1201 #ifdef CONFIG_MEMBARRIER 1202 /** 1203 * @membarrier_state: Flags controlling membarrier behavior. 1204 * 1205 * This field is close to @pgd to hopefully fit in the same 1206 * cache-line, which needs to be touched by switch_mm(). 1207 */ 1208 atomic_t membarrier_state; 1209 #endif 1210 1211 /** 1212 * @mm_users: The number of users including userspace. 1213 * 1214 * Use mmget()/mmget_not_zero()/mmput() to modify. When this 1215 * drops to 0 (i.e. when the task exits and there are no other 1216 * temporary reference holders), we also release a reference on 1217 * @mm_count (which may then free the &struct mm_struct if 1218 * @mm_count also drops to 0). 1219 */ 1220 atomic_t mm_users; 1221 1222 /* MM CID related storage */ 1223 struct mm_mm_cid mm_cid; 1224 1225 /* sched_cache related statistics */ 1226 struct sched_cache_group *sched_cache_grp; 1227 #ifdef CONFIG_MMU 1228 atomic_long_t pgtables_bytes; /* size of all page tables */ 1229 #endif 1230 int map_count; /* number of VMAs */ 1231 1232 spinlock_t page_table_lock; /* Protects page tables and some 1233 * counters 1234 */ 1235 /* 1236 * Typically the current mmap_lock's offset is 56 bytes from 1237 * the last cacheline boundary, which is very optimal, as 1238 * its two hot fields 'count' and 'owner' sit in 2 different 1239 * cachelines, and when mmap_lock is highly contended, both 1240 * of the 2 fields will be accessed frequently, current layout 1241 * will help to reduce cache bouncing. 1242 * 1243 * So please be careful with adding new fields before 1244 * mmap_lock, which can easily push the 2 fields into one 1245 * cacheline. 1246 */ 1247 struct rw_semaphore mmap_lock; 1248 1249 struct list_head mmlist; /* List of maybe swapped mm's. These 1250 * are globally strung together off 1251 * init_mm.mmlist, and are protected 1252 * by mmlist_lock 1253 */ 1254 struct rcuwait vma_writer_wait; 1255 /* 1256 * This field has lock-like semantics, meaning it is sometimes 1257 * accessed with ACQUIRE/RELEASE semantics. 1258 * Roughly speaking, incrementing the sequence number is 1259 * equivalent to releasing locks on VMAs; reading the sequence 1260 * number can be part of taking a read lock on a VMA. 1261 * Incremented every time mmap_lock is write-locked/unlocked. 1262 * Initialized to 0, therefore odd values indicate mmap_lock 1263 * is write-locked and even values that it's released. 1264 * 1265 * Can be modified under write mmap_lock using RELEASE 1266 * semantics. 1267 * Can be read with no other protection when holding write 1268 * mmap_lock. 1269 * Can be read with ACQUIRE semantics if not holding write 1270 * mmap_lock. 1271 */ 1272 seqcount_t mm_lock_seq; 1273 1274 struct futex_mm_data futex; 1275 1276 unsigned long hiwater_rss; /* High-watermark of RSS usage */ 1277 unsigned long hiwater_vm; /* High-water virtual memory usage */ 1278 1279 unsigned long total_vm; /* Total pages mapped */ 1280 unsigned long locked_vm; /* Pages that have PG_mlocked set */ 1281 atomic64_t pinned_vm; /* Refcount permanently increased */ 1282 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */ 1283 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */ 1284 unsigned long stack_vm; /* VM_STACK */ 1285 union { 1286 /* Temporary while VMA flags are being converted. */ 1287 vm_flags_t def_flags; 1288 vma_flags_t def_vma_flags; 1289 }; 1290 1291 /** 1292 * @write_protect_seq: Locked when any thread is write 1293 * protecting pages mapped by this mm to enforce a later COW, 1294 * for instance during page table copying for fork(). 1295 */ 1296 seqcount_t write_protect_seq; 1297 1298 spinlock_t arg_lock; /* protect the below fields */ 1299 1300 unsigned long start_code, end_code, start_data, end_data; 1301 unsigned long start_brk, brk, start_stack; 1302 unsigned long arg_start, arg_end, env_start, env_end; 1303 1304 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */ 1305 1306 #ifdef CONFIG_ARCH_HAS_ELF_CORE_EFLAGS 1307 /* the ABI-related flags from the ELF header. Used for core dump */ 1308 unsigned long saved_e_flags; 1309 #endif 1310 1311 struct percpu_counter rss_stat[NR_MM_COUNTERS]; 1312 1313 struct linux_binfmt *binfmt; 1314 1315 /* Architecture-specific MM context */ 1316 mm_context_t context; 1317 1318 mm_flags_t flags; /* Must use mm_flags_* hlpers to access */ 1319 1320 #ifdef CONFIG_AIO 1321 spinlock_t ioctx_lock; 1322 struct kioctx_table __rcu *ioctx_table; 1323 #endif 1324 #ifdef CONFIG_MEMCG 1325 /* 1326 * "owner" points to a task that is regarded as the canonical 1327 * user/owner of this mm. All of the following must be true in 1328 * order for it to be changed: 1329 * 1330 * current == mm->owner 1331 * current->mm != mm 1332 * new_owner->mm == mm 1333 * new_owner->alloc_lock is held 1334 */ 1335 struct task_struct __rcu *owner; 1336 #endif 1337 1338 /* store ref to file /proc/<pid>/exe symlink points to */ 1339 struct file __rcu *exe_file; 1340 #ifdef CONFIG_MMU_NOTIFIER 1341 struct mmu_notifier_subscriptions *notifier_subscriptions; 1342 #endif 1343 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS) 1344 pgtable_t pmd_huge_pte; /* protected by page_table_lock */ 1345 #endif 1346 #ifdef CONFIG_NUMA_BALANCING 1347 /* 1348 * numa_next_scan is the next time that PTEs will be remapped 1349 * PROT_NONE to trigger NUMA hinting faults; such faults gather 1350 * statistics and migrate pages to new nodes if necessary. 1351 */ 1352 unsigned long numa_next_scan; 1353 1354 /* Restart point for scanning and remapping PTEs. */ 1355 unsigned long numa_scan_offset; 1356 1357 /* numa_scan_seq prevents two threads remapping PTEs. */ 1358 int numa_scan_seq; 1359 #endif 1360 /* 1361 * An operation with batched TLB flushing is going on. Anything 1362 * that can move process memory needs to flush the TLB when 1363 * moving a PROT_NONE mapped page. 1364 */ 1365 atomic_t tlb_flush_pending; 1366 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 1367 /* See flush_tlb_batched_pending() */ 1368 atomic_t tlb_flush_batched; 1369 #endif 1370 struct uprobes_state uprobes_state; 1371 #ifdef CONFIG_PREEMPT_RT 1372 struct rcu_head delayed_drop; 1373 #endif 1374 #ifdef CONFIG_HUGETLB_PAGE 1375 atomic_long_t hugetlb_usage; 1376 #endif 1377 struct work_struct async_put_work; 1378 1379 #ifdef CONFIG_IOMMU_MM_DATA 1380 struct iommu_mm_data *iommu_mm; 1381 #endif 1382 #ifdef CONFIG_KSM 1383 /* 1384 * Represent how many pages of this process are involved in KSM 1385 * merging (not including ksm_zero_pages). 1386 */ 1387 unsigned long ksm_merging_pages; 1388 /* 1389 * Represent how many pages are checked for ksm merging 1390 * including merged and not merged. 1391 */ 1392 unsigned long ksm_rmap_items; 1393 /* 1394 * Represent how many empty pages are merged with kernel zero 1395 * pages when enabling KSM use_zero_pages. 1396 */ 1397 atomic_long_t ksm_zero_pages; 1398 #endif /* CONFIG_KSM */ 1399 #ifdef CONFIG_LRU_GEN_WALKS_MMU 1400 struct { 1401 /* this mm_struct is on lru_gen_mm_list */ 1402 struct list_head list; 1403 /* 1404 * Set when switching to this mm_struct, as a hint of 1405 * whether it has been used since the last time per-node 1406 * page table walkers cleared the corresponding bits. 1407 */ 1408 unsigned long bitmap; 1409 #ifdef CONFIG_MEMCG 1410 /* points to the memcg of "owner" above */ 1411 struct mem_cgroup *memcg; 1412 #endif 1413 } lru_gen; 1414 #endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1415 #ifdef CONFIG_MM_ID 1416 mm_id_t mm_id; 1417 #endif /* CONFIG_MM_ID */ 1418 } __randomize_layout; 1419 1420 /* 1421 * The mm_cpumask needs to be at the end of mm_struct, because it 1422 * is dynamically sized based on nr_cpu_ids. 1423 */ 1424 char flexible_array[] __aligned(__alignof__(unsigned long)); 1425 }; 1426 1427 /* Copy value to the first system word of mm flags, non-atomically. */ 1428 static inline void __mm_flags_overwrite_word(struct mm_struct *mm, unsigned long value) 1429 { 1430 *ACCESS_PRIVATE(&mm->flags, __mm_flags) = value; 1431 } 1432 1433 /* Obtain a read-only view of the mm flags bitmap. */ 1434 static inline const unsigned long *__mm_flags_get_bitmap(const struct mm_struct *mm) 1435 { 1436 return (const unsigned long *)ACCESS_PRIVATE(&mm->flags, __mm_flags); 1437 } 1438 1439 /* Read the first system word of mm flags, non-atomically. */ 1440 static inline unsigned long __mm_flags_get_word(const struct mm_struct *mm) 1441 { 1442 return *__mm_flags_get_bitmap(mm); 1443 } 1444 1445 /* 1446 * Update the first system word of mm flags ONLY, applying the specified mask to 1447 * it, then setting all flags specified by bits. 1448 */ 1449 static inline void __mm_flags_set_mask_bits_word(struct mm_struct *mm, 1450 unsigned long mask, unsigned long bits) 1451 { 1452 unsigned long *bitmap = ACCESS_PRIVATE(&mm->flags, __mm_flags); 1453 1454 set_mask_bits(bitmap, mask, bits); 1455 } 1456 1457 #define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \ 1458 MT_FLAGS_USE_RCU) 1459 extern struct mm_struct init_mm; 1460 1461 #define MM_STRUCT_FLEXIBLE_ARRAY_INIT \ 1462 { \ 1463 [0 ... sizeof(cpumask_t) + MM_CID_STATIC_SIZE - 1] = 0 \ 1464 } 1465 1466 /* Pointer magic because the dynamic array size confuses some compilers. */ 1467 static inline void mm_init_cpumask(struct mm_struct *mm) 1468 { 1469 unsigned long cpu_bitmap = (unsigned long)mm; 1470 1471 cpu_bitmap += offsetof(struct mm_struct, flexible_array); 1472 cpumask_clear((struct cpumask *)cpu_bitmap); 1473 } 1474 1475 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */ 1476 static inline cpumask_t *mm_cpumask(struct mm_struct *mm) 1477 { 1478 return (struct cpumask *)&mm->flexible_array; 1479 } 1480 1481 #ifdef CONFIG_LRU_GEN 1482 1483 struct lru_gen_mm_list { 1484 /* mm_struct list for page table walkers */ 1485 struct list_head fifo; 1486 /* protects the list above */ 1487 spinlock_t lock; 1488 }; 1489 1490 #endif /* CONFIG_LRU_GEN */ 1491 1492 #ifdef CONFIG_LRU_GEN_WALKS_MMU 1493 1494 void lru_gen_add_mm(struct mm_struct *mm); 1495 void lru_gen_del_mm(struct mm_struct *mm); 1496 void lru_gen_migrate_mm(struct mm_struct *mm); 1497 1498 static inline void lru_gen_init_mm(struct mm_struct *mm) 1499 { 1500 INIT_LIST_HEAD(&mm->lru_gen.list); 1501 mm->lru_gen.bitmap = 0; 1502 #ifdef CONFIG_MEMCG 1503 mm->lru_gen.memcg = NULL; 1504 #endif 1505 } 1506 1507 static inline void lru_gen_use_mm(struct mm_struct *mm) 1508 { 1509 /* 1510 * When the bitmap is set, page reclaim knows this mm_struct has been 1511 * used since the last time it cleared the bitmap. So it might be worth 1512 * walking the page tables of this mm_struct to clear the accessed bit. 1513 */ 1514 WRITE_ONCE(mm->lru_gen.bitmap, -1); 1515 } 1516 1517 #else /* !CONFIG_LRU_GEN_WALKS_MMU */ 1518 1519 static inline void lru_gen_add_mm(struct mm_struct *mm) 1520 { 1521 } 1522 1523 static inline void lru_gen_del_mm(struct mm_struct *mm) 1524 { 1525 } 1526 1527 static inline void lru_gen_migrate_mm(struct mm_struct *mm) 1528 { 1529 } 1530 1531 static inline void lru_gen_init_mm(struct mm_struct *mm) 1532 { 1533 } 1534 1535 static inline void lru_gen_use_mm(struct mm_struct *mm) 1536 { 1537 } 1538 1539 #endif /* CONFIG_LRU_GEN_WALKS_MMU */ 1540 1541 struct vma_iterator { 1542 struct ma_state mas; 1543 }; 1544 1545 #define VMA_ITERATOR(name, __mm, __addr) \ 1546 struct vma_iterator name = { \ 1547 .mas = { \ 1548 .tree = &(__mm)->mm_mt, \ 1549 .index = __addr, \ 1550 .node = NULL, \ 1551 .status = ma_start, \ 1552 }, \ 1553 } 1554 1555 static inline void vma_iter_init(struct vma_iterator *vmi, 1556 struct mm_struct *mm, unsigned long addr) 1557 { 1558 mas_init(&vmi->mas, &mm->mm_mt, addr); 1559 } 1560 1561 #ifdef CONFIG_SCHED_MM_CID 1562 /* 1563 * mm_cpus_allowed: Union of all mm's threads allowed CPUs. 1564 */ 1565 static inline cpumask_t *mm_cpus_allowed(struct mm_struct *mm) 1566 { 1567 unsigned long bitmap = (unsigned long)mm; 1568 1569 bitmap += offsetof(struct mm_struct, flexible_array); 1570 /* Skip cpu_bitmap */ 1571 bitmap += cpumask_size(); 1572 return (struct cpumask *)bitmap; 1573 } 1574 1575 /* Accessor for struct mm_struct's cidmask. */ 1576 static inline unsigned long *mm_cidmask(struct mm_struct *mm) 1577 { 1578 unsigned long cid_bitmap = (unsigned long)mm_cpus_allowed(mm); 1579 1580 /* Skip mm_cpus_allowed */ 1581 cid_bitmap += cpumask_size(); 1582 return (unsigned long *)cid_bitmap; 1583 } 1584 1585 void mm_init_cid(struct mm_struct *mm, struct task_struct *p); 1586 1587 static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p) 1588 { 1589 mm->mm_cid.pcpu = alloc_percpu_noprof(struct mm_cid_pcpu); 1590 if (!mm->mm_cid.pcpu) 1591 return -ENOMEM; 1592 mm_init_cid(mm, p); 1593 return 0; 1594 } 1595 # define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__)) 1596 1597 static inline void mm_destroy_cid(struct mm_struct *mm) 1598 { 1599 free_percpu(mm->mm_cid.pcpu); 1600 mm->mm_cid.pcpu = NULL; 1601 } 1602 1603 static inline unsigned int mm_cid_size(void) 1604 { 1605 /* mm_cpus_allowed(), mm_cidmask(). */ 1606 return cpumask_size() + bitmap_size(num_possible_cpus()); 1607 } 1608 1609 /* Use 2 * NR_CPUS as worse case for static allocation. */ 1610 # define MM_CID_STATIC_SIZE (2 * sizeof(cpumask_t)) 1611 #else /* CONFIG_SCHED_MM_CID */ 1612 static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { } 1613 static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; } 1614 static inline void mm_destroy_cid(struct mm_struct *mm) { } 1615 static inline unsigned int mm_cid_size(void) 1616 { 1617 return 0; 1618 } 1619 # define MM_CID_STATIC_SIZE 0 1620 #endif /* CONFIG_SCHED_MM_CID */ 1621 1622 #ifdef CONFIG_SCHED_CACHE 1623 int mm_init_sched(struct mm_struct *mm, 1624 struct sched_cache_time __percpu *pcpu_sched); 1625 void mm_destroy_sched(struct mm_struct *mm); 1626 1627 static inline int mm_alloc_sched_noprof(struct mm_struct *mm) 1628 { 1629 struct sched_cache_time __percpu *pcpu_sched = 1630 alloc_percpu_noprof(struct sched_cache_time); 1631 1632 if (!pcpu_sched) 1633 return -ENOMEM; 1634 1635 return mm_init_sched(mm, pcpu_sched); 1636 } 1637 1638 #define mm_alloc_sched(...) alloc_hooks(mm_alloc_sched_noprof(__VA_ARGS__)) 1639 1640 #else /* !CONFIG_SCHED_CACHE */ 1641 1642 static inline int mm_alloc_sched(struct mm_struct *mm) { return 0; } 1643 static inline void mm_destroy_sched(struct mm_struct *mm) { } 1644 1645 #endif /* CONFIG_SCHED_CACHE */ 1646 1647 struct mmu_gather; 1648 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm); 1649 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm); 1650 void tlb_gather_mmu_vma(struct mmu_gather *tlb, struct vm_area_struct *vma); 1651 extern void tlb_finish_mmu(struct mmu_gather *tlb); 1652 1653 struct vm_fault; 1654 1655 /** 1656 * typedef vm_fault_t - Return type for page fault handlers. 1657 * 1658 * Page fault handlers return a bitmask of %VM_FAULT values. 1659 */ 1660 typedef __bitwise unsigned int vm_fault_t; 1661 1662 /** 1663 * enum vm_fault_reason - Page fault handlers return a bitmask of 1664 * these values to tell the core VM what happened when handling the 1665 * fault. Used to decide whether a process gets delivered SIGBUS or 1666 * just gets major/minor fault counters bumped up. 1667 * 1668 * @VM_FAULT_OOM: Out Of Memory 1669 * @VM_FAULT_SIGBUS: Bad access 1670 * @VM_FAULT_MAJOR: Page read from storage 1671 * @VM_FAULT_HWPOISON: Hit poisoned small page 1672 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded 1673 * in upper bits 1674 * @VM_FAULT_SIGSEGV: segmentation fault 1675 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page 1676 * @VM_FAULT_LOCKED: ->fault locked the returned page 1677 * @VM_FAULT_RETRY: ->fault blocked, must retry 1678 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small 1679 * @VM_FAULT_DONE_COW: ->fault has fully handled COW 1680 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs 1681 * fsync() to complete (for synchronous page faults 1682 * in DAX) 1683 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released 1684 * @VM_FAULT_HINDEX_MASK: mask HINDEX value 1685 * 1686 */ 1687 enum vm_fault_reason { 1688 VM_FAULT_OOM = (__force vm_fault_t)0x000001, 1689 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002, 1690 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004, 1691 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010, 1692 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020, 1693 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040, 1694 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100, 1695 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200, 1696 VM_FAULT_RETRY = (__force vm_fault_t)0x000400, 1697 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800, 1698 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, 1699 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, 1700 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000, 1701 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, 1702 }; 1703 1704 /* Encode hstate index for a hwpoisoned large page */ 1705 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16)) 1706 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf) 1707 1708 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \ 1709 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \ 1710 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) 1711 1712 #define VM_FAULT_RESULT_TRACE \ 1713 { (__force u32)VM_FAULT_OOM, "OOM" }, \ 1714 { (__force u32)VM_FAULT_SIGBUS, "SIGBUS" }, \ 1715 { (__force u32)VM_FAULT_MAJOR, "MAJOR" }, \ 1716 { (__force u32)VM_FAULT_HWPOISON, "HWPOISON" }, \ 1717 { (__force u32)VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \ 1718 { (__force u32)VM_FAULT_SIGSEGV, "SIGSEGV" }, \ 1719 { (__force u32)VM_FAULT_NOPAGE, "NOPAGE" }, \ 1720 { (__force u32)VM_FAULT_LOCKED, "LOCKED" }, \ 1721 { (__force u32)VM_FAULT_RETRY, "RETRY" }, \ 1722 { (__force u32)VM_FAULT_FALLBACK, "FALLBACK" }, \ 1723 { (__force u32)VM_FAULT_DONE_COW, "DONE_COW" }, \ 1724 { (__force u32)VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ 1725 { (__force u32)VM_FAULT_COMPLETED, "COMPLETED" } 1726 1727 struct vm_special_mapping { 1728 const char *name; /* The name, e.g. "[vdso]". */ 1729 1730 /* 1731 * If .fault is not provided, this points to a 1732 * NULL-terminated array of pages that back the special mapping. 1733 * 1734 * This must not be NULL unless .fault is provided. 1735 */ 1736 struct page **pages; 1737 1738 /* 1739 * If non-NULL, then this is called to resolve page faults 1740 * on the special mapping. If used, .pages is not checked. 1741 */ 1742 vm_fault_t (*fault)(const struct vm_special_mapping *sm, 1743 struct vm_area_struct *vma, 1744 struct vm_fault *vmf); 1745 1746 int (*mremap)(const struct vm_special_mapping *sm, 1747 struct vm_area_struct *new_vma); 1748 1749 void (*close)(const struct vm_special_mapping *sm, 1750 struct vm_area_struct *vma); 1751 }; 1752 1753 enum tlb_flush_reason { 1754 TLB_FLUSH_ON_TASK_SWITCH, 1755 TLB_REMOTE_SHOOTDOWN, 1756 TLB_LOCAL_SHOOTDOWN, 1757 TLB_LOCAL_MM_SHOOTDOWN, 1758 TLB_REMOTE_SEND_IPI, 1759 TLB_REMOTE_WRONG_CPU, 1760 }; 1761 1762 /** 1763 * enum fault_flag - Fault flag definitions. 1764 * @FAULT_FLAG_WRITE: Fault was a write fault. 1765 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE. 1766 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked. 1767 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying. 1768 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region. 1769 * @FAULT_FLAG_TRIED: The fault has been tried once. 1770 * @FAULT_FLAG_USER: The fault originated in userspace. 1771 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm. 1772 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. 1773 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. 1774 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a 1775 * COW mapping, making sure that an exclusive anon page is 1776 * mapped after the fault. 1777 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. 1778 * We should only access orig_pte if this flag set. 1779 * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock. 1780 * 1781 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify 1782 * whether we would allow page faults to retry by specifying these two 1783 * fault flags correctly. Currently there can be three legal combinations: 1784 * 1785 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and 1786 * this is the first try 1787 * 1788 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and 1789 * we've already tried at least once 1790 * 1791 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry 1792 * 1793 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never 1794 * be used. Note that page faults can be allowed to retry for multiple times, 1795 * in which case we'll have an initial fault with flags (a) then later on 1796 * continuous faults with flags (b). We should always try to detect pending 1797 * signals before a retry to make sure the continuous page faults can still be 1798 * interrupted if necessary. 1799 * 1800 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal. 1801 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when 1802 * applied to mappings that are not COW mappings. 1803 */ 1804 enum fault_flag { 1805 FAULT_FLAG_WRITE = 1 << 0, 1806 FAULT_FLAG_MKWRITE = 1 << 1, 1807 FAULT_FLAG_ALLOW_RETRY = 1 << 2, 1808 FAULT_FLAG_RETRY_NOWAIT = 1 << 3, 1809 FAULT_FLAG_KILLABLE = 1 << 4, 1810 FAULT_FLAG_TRIED = 1 << 5, 1811 FAULT_FLAG_USER = 1 << 6, 1812 FAULT_FLAG_REMOTE = 1 << 7, 1813 FAULT_FLAG_INSTRUCTION = 1 << 8, 1814 FAULT_FLAG_INTERRUPTIBLE = 1 << 9, 1815 FAULT_FLAG_UNSHARE = 1 << 10, 1816 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11, 1817 FAULT_FLAG_VMA_LOCK = 1 << 12, 1818 }; 1819 1820 typedef unsigned int __bitwise zap_flags_t; 1821 1822 /* Flags for clear_young_dirty_ptes(). */ 1823 typedef int __bitwise cydp_t; 1824 1825 /* Clear the access bit */ 1826 #define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0)) 1827 1828 /* Clear the dirty bit */ 1829 #define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1)) 1830 1831 /* 1832 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each 1833 * other. Here is what they mean, and how to use them: 1834 * 1835 * 1836 * FIXME: For pages which are part of a filesystem, mappings are subject to the 1837 * lifetime enforced by the filesystem and we need guarantees that longterm 1838 * users like RDMA and V4L2 only establish mappings which coordinate usage with 1839 * the filesystem. Ideas for this coordination include revoking the longterm 1840 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was 1841 * added after the problem with filesystems was found FS DAX VMAs are 1842 * specifically failed. Filesystem pages are still subject to bugs and use of 1843 * FOLL_LONGTERM should be avoided on those pages. 1844 * 1845 * In the CMA case: long term pins in a CMA region would unnecessarily fragment 1846 * that region. And so, CMA attempts to migrate the page before pinning, when 1847 * FOLL_LONGTERM is specified. 1848 * 1849 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount, 1850 * but an additional pin counting system) will be invoked. This is intended for 1851 * anything that gets a page reference and then touches page data (for example, 1852 * Direct IO). This lets the filesystem know that some non-file-system entity is 1853 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages 1854 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by 1855 * a call to unpin_user_page(). 1856 * 1857 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different 1858 * and separate refcounting mechanisms, however, and that means that each has 1859 * its own acquire and release mechanisms: 1860 * 1861 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release. 1862 * 1863 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release. 1864 * 1865 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call. 1866 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based 1867 * calls applied to them, and that's perfectly OK. This is a constraint on the 1868 * callers, not on the pages.) 1869 * 1870 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never 1871 * directly by the caller. That's in order to help avoid mismatches when 1872 * releasing pages: get_user_pages*() pages must be released via put_page(), 1873 * while pin_user_pages*() pages must be released via unpin_user_page(). 1874 * 1875 * Please see Documentation/core-api/pin_user_pages.rst for more information. 1876 */ 1877 1878 enum { 1879 /* check pte is writable */ 1880 FOLL_WRITE = 1 << 0, 1881 /* do get_page on page */ 1882 FOLL_GET = 1 << 1, 1883 /* give error on hole if it would be zero */ 1884 FOLL_DUMP = 1 << 2, 1885 /* get_user_pages read/write w/o permission */ 1886 FOLL_FORCE = 1 << 3, 1887 /* 1888 * if a disk transfer is needed, start the IO and return without waiting 1889 * upon it 1890 */ 1891 FOLL_NOWAIT = 1 << 4, 1892 /* do not fault in pages */ 1893 FOLL_NOFAULT = 1 << 5, 1894 /* check page is hwpoisoned */ 1895 FOLL_HWPOISON = 1 << 6, 1896 /* don't do file mappings */ 1897 FOLL_ANON = 1 << 7, 1898 /* 1899 * FOLL_LONGTERM indicates that the page will be held for an indefinite 1900 * time period _often_ under userspace control. This is in contrast to 1901 * iov_iter_get_pages(), whose usages are transient. 1902 */ 1903 FOLL_LONGTERM = 1 << 8, 1904 /* split huge pmd before returning */ 1905 FOLL_SPLIT_PMD = 1 << 9, 1906 /* allow returning PCI P2PDMA pages */ 1907 FOLL_PCI_P2PDMA = 1 << 10, 1908 /* allow interrupts from generic signals */ 1909 FOLL_INTERRUPTIBLE = 1 << 11, 1910 /* 1911 * Always honor (trigger) NUMA hinting faults. 1912 * 1913 * FOLL_WRITE implicitly honors NUMA hinting faults because a 1914 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE 1915 * apply). get_user_pages_fast_only() always implicitly honors NUMA 1916 * hinting faults. 1917 */ 1918 FOLL_HONOR_NUMA_FAULT = 1 << 12, 1919 1920 /* See also internal only FOLL flags in mm/internal.h */ 1921 }; 1922 1923 /* mm flags */ 1924 1925 /* 1926 * Bits 0 and 1 were dumpability; that moved to task->exec_state. Reserve 1927 * the bits so MMF_DUMP_FILTER_* positions stay stable for the 1928 * /proc/<pid>/coredump_filter ABI. 1929 */ 1930 #define MMF_DUMPABLE_BITS 2 1931 /* coredump filter bits */ 1932 #define MMF_DUMP_ANON_PRIVATE 2 1933 #define MMF_DUMP_ANON_SHARED 3 1934 #define MMF_DUMP_MAPPED_PRIVATE 4 1935 #define MMF_DUMP_MAPPED_SHARED 5 1936 #define MMF_DUMP_ELF_HEADERS 6 1937 #define MMF_DUMP_HUGETLB_PRIVATE 7 1938 #define MMF_DUMP_HUGETLB_SHARED 8 1939 #define MMF_DUMP_DAX_PRIVATE 9 1940 #define MMF_DUMP_DAX_SHARED 10 1941 1942 #define MMF_DUMP_FILTER_SHIFT MMF_DUMPABLE_BITS 1943 #define MMF_DUMP_FILTER_BITS 9 1944 #define MMF_DUMP_FILTER_MASK \ 1945 ((BIT(MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT) 1946 #define MMF_DUMP_FILTER_DEFAULT \ 1947 (BIT(MMF_DUMP_ANON_PRIVATE) | BIT(MMF_DUMP_ANON_SHARED) | \ 1948 BIT(MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF) 1949 1950 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS 1951 # define MMF_DUMP_MASK_DEFAULT_ELF BIT(MMF_DUMP_ELF_HEADERS) 1952 #else 1953 # define MMF_DUMP_MASK_DEFAULT_ELF 0 1954 #endif 1955 /* leave room for more dump flags */ 1956 #define MMF_VM_MERGEABLE 16 /* KSM may merge identical pages */ 1957 #define MMF_VM_HUGEPAGE 17 /* set when mm is available for khugepaged */ 1958 1959 #define MMF_HUGE_ZERO_FOLIO 18 /* mm has ever used the global huge zero folio */ 1960 1961 #define MMF_HAS_UPROBES 19 /* has uprobes */ 1962 #define MMF_RECALC_UPROBES 20 /* MMF_HAS_UPROBES can be wrong */ 1963 #define MMF_OOM_SKIP 21 /* mm is of no interest for the OOM killer */ 1964 #define MMF_UNSTABLE 22 /* mm is unstable for copy_from_user */ 1965 #define MMF_DISABLE_THP_EXCEPT_ADVISED 23 /* no THP except when advised (e.g., VM_HUGEPAGE) */ 1966 #define MMF_DISABLE_THP_COMPLETELY 24 /* no THP for all VMAs */ 1967 #define MMF_DISABLE_THP_MASK (BIT(MMF_DISABLE_THP_COMPLETELY) | \ 1968 BIT(MMF_DISABLE_THP_EXCEPT_ADVISED)) 1969 #define MMF_OOM_REAP_QUEUED 25 /* mm was queued for oom_reaper */ 1970 #define MMF_MULTIPROCESS 26 /* mm is shared between processes */ 1971 /* 1972 * MMF_HAS_PINNED: Whether this mm has pinned any pages. This can be either 1973 * replaced in the future by mm.pinned_vm when it becomes stable, or grow into 1974 * a counter on its own. We're aggresive on this bit for now: even if the 1975 * pinned pages were unpinned later on, we'll still keep this bit set for the 1976 * lifecycle of this mm, just for simplicity. 1977 */ 1978 #define MMF_HAS_PINNED 27 /* FOLL_PIN has run, never cleared */ 1979 1980 #define MMF_HAS_MDWE 28 1981 #define MMF_HAS_MDWE_MASK BIT(MMF_HAS_MDWE) 1982 1983 #define MMF_HAS_MDWE_NO_INHERIT 29 1984 1985 #define MMF_VM_MERGE_ANY 30 1986 #define MMF_VM_MERGE_ANY_MASK BIT(MMF_VM_MERGE_ANY) 1987 1988 #define MMF_TOPDOWN 31 /* mm searches top down by default */ 1989 #define MMF_TOPDOWN_MASK BIT(MMF_TOPDOWN) 1990 1991 #define MMF_INIT_LEGACY_MASK (MMF_DUMP_FILTER_MASK |\ 1992 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ 1993 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK) 1994 1995 /* Legacy flags must fit within 32 bits. */ 1996 static_assert((u64)MMF_INIT_LEGACY_MASK <= (u64)UINT_MAX); 1997 1998 /* 1999 * Initialise legacy flags according to masks, propagating selected flags on 2000 * fork. Further flag manipulation can be performed by the caller. 2001 */ 2002 static inline unsigned long mmf_init_legacy_flags(unsigned long flags) 2003 { 2004 if (flags & (1UL << MMF_HAS_MDWE_NO_INHERIT)) 2005 flags &= ~((1UL << MMF_HAS_MDWE) | 2006 (1UL << MMF_HAS_MDWE_NO_INHERIT)); 2007 return flags & MMF_INIT_LEGACY_MASK; 2008 } 2009 2010 #endif /* _LINUX_MM_TYPES_H */ 2011