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