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