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/list.h> 9 #include <linux/spinlock.h> 10 #include <linux/rbtree.h> 11 #include <linux/rwsem.h> 12 #include <linux/completion.h> 13 #include <linux/cpumask.h> 14 #include <linux/uprobes.h> 15 #include <linux/rcupdate.h> 16 #include <linux/page-flags-layout.h> 17 #include <linux/workqueue.h> 18 #include <linux/seqlock.h> 19 20 #include <asm/mmu.h> 21 22 #ifndef AT_VECTOR_SIZE_ARCH 23 #define AT_VECTOR_SIZE_ARCH 0 24 #endif 25 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1)) 26 27 #define INIT_PASID 0 28 29 struct address_space; 30 struct mem_cgroup; 31 32 /* 33 * Each physical page in the system has a struct page associated with 34 * it to keep track of whatever it is we are using the page for at the 35 * moment. Note that we have no way to track which tasks are using 36 * a page, though if it is a pagecache page, rmap structures can tell us 37 * who is mapping it. 38 * 39 * If you allocate the page using alloc_pages(), you can use some of the 40 * space in struct page for your own purposes. The five words in the main 41 * union are available, except for bit 0 of the first word which must be 42 * kept clear. Many users use this word to store a pointer to an object 43 * which is guaranteed to be aligned. If you use the same storage as 44 * page->mapping, you must restore it to NULL before freeing the page. 45 * 46 * If your page will not be mapped to userspace, you can also use the four 47 * bytes in the mapcount union, but you must call page_mapcount_reset() 48 * before freeing it. 49 * 50 * If you want to use the refcount field, it must be used in such a way 51 * that other CPUs temporarily incrementing and then decrementing the 52 * refcount does not cause problems. On receiving the page from 53 * alloc_pages(), the refcount will be positive. 54 * 55 * If you allocate pages of order > 0, you can use some of the fields 56 * in each subpage, but you may need to restore some of their values 57 * afterwards. 58 * 59 * SLUB uses cmpxchg_double() to atomically update its freelist and 60 * counters. That requires that freelist & counters be adjacent and 61 * double-word aligned. We align all struct pages to double-word 62 * boundaries, and ensure that 'freelist' is aligned within the 63 * struct. 64 */ 65 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE 66 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long)) 67 #else 68 #define _struct_page_alignment 69 #endif 70 71 struct page { 72 unsigned long flags; /* Atomic flags, some possibly 73 * updated asynchronously */ 74 /* 75 * Five words (20/40 bytes) are available in this union. 76 * WARNING: bit 0 of the first word is used for PageTail(). That 77 * means the other users of this union MUST NOT use the bit to 78 * avoid collision and false-positive PageTail(). 79 */ 80 union { 81 struct { /* Page cache and anonymous pages */ 82 /** 83 * @lru: Pageout list, eg. active_list protected by 84 * lruvec->lru_lock. Sometimes used as a generic list 85 * by the page owner. 86 */ 87 struct list_head lru; 88 /* See page-flags.h for PAGE_MAPPING_FLAGS */ 89 struct address_space *mapping; 90 pgoff_t index; /* Our offset within mapping. */ 91 /** 92 * @private: Mapping-private opaque data. 93 * Usually used for buffer_heads if PagePrivate. 94 * Used for swp_entry_t if PageSwapCache. 95 * Indicates order in the buddy system if PageBuddy. 96 */ 97 unsigned long private; 98 }; 99 struct { /* page_pool used by netstack */ 100 /** 101 * @pp_magic: magic value to avoid recycling non 102 * page_pool allocated pages. 103 */ 104 unsigned long pp_magic; 105 struct page_pool *pp; 106 unsigned long _pp_mapping_pad; 107 unsigned long dma_addr; 108 atomic_long_t pp_frag_count; 109 }; 110 struct { /* slab, slob and slub */ 111 union { 112 struct list_head slab_list; 113 struct { /* Partial pages */ 114 struct page *next; 115 #ifdef CONFIG_64BIT 116 int pages; /* Nr of pages left */ 117 int pobjects; /* Approximate count */ 118 #else 119 short int pages; 120 short int pobjects; 121 #endif 122 }; 123 }; 124 struct kmem_cache *slab_cache; /* not slob */ 125 /* Double-word boundary */ 126 void *freelist; /* first free object */ 127 union { 128 void *s_mem; /* slab: first object */ 129 unsigned long counters; /* SLUB */ 130 struct { /* SLUB */ 131 unsigned inuse:16; 132 unsigned objects:15; 133 unsigned frozen:1; 134 }; 135 }; 136 }; 137 struct { /* Tail pages of compound page */ 138 unsigned long compound_head; /* Bit zero is set */ 139 140 /* First tail page only */ 141 unsigned char compound_dtor; 142 unsigned char compound_order; 143 atomic_t compound_mapcount; 144 unsigned int compound_nr; /* 1 << compound_order */ 145 }; 146 struct { /* Second tail page of compound page */ 147 unsigned long _compound_pad_1; /* compound_head */ 148 atomic_t hpage_pinned_refcount; 149 /* For both global and memcg */ 150 struct list_head deferred_list; 151 }; 152 struct { /* Page table pages */ 153 unsigned long _pt_pad_1; /* compound_head */ 154 pgtable_t pmd_huge_pte; /* protected by page->ptl */ 155 unsigned long _pt_pad_2; /* mapping */ 156 union { 157 struct mm_struct *pt_mm; /* x86 pgds only */ 158 atomic_t pt_frag_refcount; /* powerpc */ 159 }; 160 #if ALLOC_SPLIT_PTLOCKS 161 spinlock_t *ptl; 162 #else 163 spinlock_t ptl; 164 #endif 165 }; 166 struct { /* ZONE_DEVICE pages */ 167 /** @pgmap: Points to the hosting device page map. */ 168 struct dev_pagemap *pgmap; 169 void *zone_device_data; 170 /* 171 * ZONE_DEVICE private pages are counted as being 172 * mapped so the next 3 words hold the mapping, index, 173 * and private fields from the source anonymous or 174 * page cache page while the page is migrated to device 175 * private memory. 176 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also 177 * use the mapping, index, and private fields when 178 * pmem backed DAX files are mapped. 179 */ 180 }; 181 182 /** @rcu_head: You can use this to free a page by RCU. */ 183 struct rcu_head rcu_head; 184 }; 185 186 union { /* This union is 4 bytes in size. */ 187 /* 188 * If the page can be mapped to userspace, encodes the number 189 * of times this page is referenced by a page table. 190 */ 191 atomic_t _mapcount; 192 193 /* 194 * If the page is neither PageSlab nor mappable to userspace, 195 * the value stored here may help determine what this page 196 * is used for. See page-flags.h for a list of page types 197 * which are currently stored here. 198 */ 199 unsigned int page_type; 200 201 unsigned int active; /* SLAB */ 202 int units; /* SLOB */ 203 }; 204 205 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */ 206 atomic_t _refcount; 207 208 #ifdef CONFIG_MEMCG 209 unsigned long memcg_data; 210 #endif 211 212 /* 213 * On machines where all RAM is mapped into kernel address space, 214 * we can simply calculate the virtual address. On machines with 215 * highmem some memory is mapped into kernel virtual memory 216 * dynamically, so we need a place to store that address. 217 * Note that this field could be 16 bits on x86 ... ;) 218 * 219 * Architectures with slow multiplication can define 220 * WANT_PAGE_VIRTUAL in asm/page.h 221 */ 222 #if defined(WANT_PAGE_VIRTUAL) 223 void *virtual; /* Kernel virtual address (NULL if 224 not kmapped, ie. highmem) */ 225 #endif /* WANT_PAGE_VIRTUAL */ 226 227 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 228 int _last_cpupid; 229 #endif 230 } _struct_page_alignment; 231 232 /** 233 * struct folio - Represents a contiguous set of bytes. 234 * @flags: Identical to the page flags. 235 * @lru: Least Recently Used list; tracks how recently this folio was used. 236 * @mapping: The file this page belongs to, or refers to the anon_vma for 237 * anonymous memory. 238 * @index: Offset within the file, in units of pages. For anonymous memory, 239 * this is the index from the beginning of the mmap. 240 * @private: Filesystem per-folio data (see folio_attach_private()). 241 * Used for swp_entry_t if folio_test_swapcache(). 242 * @_mapcount: Do not access this member directly. Use folio_mapcount() to 243 * find out how many times this folio is mapped by userspace. 244 * @_refcount: Do not access this member directly. Use folio_ref_count() 245 * to find how many references there are to this folio. 246 * @memcg_data: Memory Control Group data. 247 * 248 * A folio is a physically, virtually and logically contiguous set 249 * of bytes. It is a power-of-two in size, and it is aligned to that 250 * same power-of-two. It is at least as large as %PAGE_SIZE. If it is 251 * in the page cache, it is at a file offset which is a multiple of that 252 * power-of-two. It may be mapped into userspace at an address which is 253 * at an arbitrary page offset, but its kernel virtual address is aligned 254 * to its size. 255 */ 256 struct folio { 257 /* private: don't document the anon union */ 258 union { 259 struct { 260 /* public: */ 261 unsigned long flags; 262 struct list_head lru; 263 struct address_space *mapping; 264 pgoff_t index; 265 void *private; 266 atomic_t _mapcount; 267 atomic_t _refcount; 268 #ifdef CONFIG_MEMCG 269 unsigned long memcg_data; 270 #endif 271 /* private: the union with struct page is transitional */ 272 }; 273 struct page page; 274 }; 275 }; 276 277 static_assert(sizeof(struct page) == sizeof(struct folio)); 278 #define FOLIO_MATCH(pg, fl) \ 279 static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl)) 280 FOLIO_MATCH(flags, flags); 281 FOLIO_MATCH(lru, lru); 282 FOLIO_MATCH(compound_head, lru); 283 FOLIO_MATCH(index, index); 284 FOLIO_MATCH(private, private); 285 FOLIO_MATCH(_mapcount, _mapcount); 286 FOLIO_MATCH(_refcount, _refcount); 287 #ifdef CONFIG_MEMCG 288 FOLIO_MATCH(memcg_data, memcg_data); 289 #endif 290 #undef FOLIO_MATCH 291 292 static inline atomic_t *folio_mapcount_ptr(struct folio *folio) 293 { 294 struct page *tail = &folio->page + 1; 295 return &tail->compound_mapcount; 296 } 297 298 static inline atomic_t *compound_mapcount_ptr(struct page *page) 299 { 300 return &page[1].compound_mapcount; 301 } 302 303 static inline atomic_t *compound_pincount_ptr(struct page *page) 304 { 305 return &page[2].hpage_pinned_refcount; 306 } 307 308 /* 309 * Used for sizing the vmemmap region on some architectures 310 */ 311 #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) 312 313 #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) 314 #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) 315 316 /* 317 * page_private can be used on tail pages. However, PagePrivate is only 318 * checked by the VM on the head page. So page_private on the tail pages 319 * should be used for data that's ancillary to the head page (eg attaching 320 * buffer heads to tail pages after attaching buffer heads to the head page) 321 */ 322 #define page_private(page) ((page)->private) 323 324 static inline void set_page_private(struct page *page, unsigned long private) 325 { 326 page->private = private; 327 } 328 329 static inline void *folio_get_private(struct folio *folio) 330 { 331 return folio->private; 332 } 333 334 struct page_frag_cache { 335 void * va; 336 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) 337 __u16 offset; 338 __u16 size; 339 #else 340 __u32 offset; 341 #endif 342 /* we maintain a pagecount bias, so that we dont dirty cache line 343 * containing page->_refcount every time we allocate a fragment. 344 */ 345 unsigned int pagecnt_bias; 346 bool pfmemalloc; 347 }; 348 349 typedef unsigned long vm_flags_t; 350 351 /* 352 * A region containing a mapping of a non-memory backed file under NOMMU 353 * conditions. These are held in a global tree and are pinned by the VMAs that 354 * map parts of them. 355 */ 356 struct vm_region { 357 struct rb_node vm_rb; /* link in global region tree */ 358 vm_flags_t vm_flags; /* VMA vm_flags */ 359 unsigned long vm_start; /* start address of region */ 360 unsigned long vm_end; /* region initialised to here */ 361 unsigned long vm_top; /* region allocated to here */ 362 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */ 363 struct file *vm_file; /* the backing file or NULL */ 364 365 int vm_usage; /* region usage count (access under nommu_region_sem) */ 366 bool vm_icache_flushed : 1; /* true if the icache has been flushed for 367 * this region */ 368 }; 369 370 #ifdef CONFIG_USERFAULTFD 371 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, }) 372 struct vm_userfaultfd_ctx { 373 struct userfaultfd_ctx *ctx; 374 }; 375 #else /* CONFIG_USERFAULTFD */ 376 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {}) 377 struct vm_userfaultfd_ctx {}; 378 #endif /* CONFIG_USERFAULTFD */ 379 380 /* 381 * This struct describes a virtual memory area. There is one of these 382 * per VM-area/task. A VM area is any part of the process virtual memory 383 * space that has a special rule for the page-fault handlers (ie a shared 384 * library, the executable area etc). 385 */ 386 struct vm_area_struct { 387 /* The first cache line has the info for VMA tree walking. */ 388 389 unsigned long vm_start; /* Our start address within vm_mm. */ 390 unsigned long vm_end; /* The first byte after our end address 391 within vm_mm. */ 392 393 /* linked list of VM areas per task, sorted by address */ 394 struct vm_area_struct *vm_next, *vm_prev; 395 396 struct rb_node vm_rb; 397 398 /* 399 * Largest free memory gap in bytes to the left of this VMA. 400 * Either between this VMA and vma->vm_prev, or between one of the 401 * VMAs below us in the VMA rbtree and its ->vm_prev. This helps 402 * get_unmapped_area find a free area of the right size. 403 */ 404 unsigned long rb_subtree_gap; 405 406 /* Second cache line starts here. */ 407 408 struct mm_struct *vm_mm; /* The address space we belong to. */ 409 410 /* 411 * Access permissions of this VMA. 412 * See vmf_insert_mixed_prot() for discussion. 413 */ 414 pgprot_t vm_page_prot; 415 unsigned long vm_flags; /* Flags, see mm.h. */ 416 417 /* 418 * For areas with an address space and backing store, 419 * linkage into the address_space->i_mmap interval tree. 420 */ 421 struct { 422 struct rb_node rb; 423 unsigned long rb_subtree_last; 424 } shared; 425 426 /* 427 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma 428 * list, after a COW of one of the file pages. A MAP_SHARED vma 429 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack 430 * or brk vma (with NULL file) can only be in an anon_vma list. 431 */ 432 struct list_head anon_vma_chain; /* Serialized by mmap_lock & 433 * page_table_lock */ 434 struct anon_vma *anon_vma; /* Serialized by page_table_lock */ 435 436 /* Function pointers to deal with this struct. */ 437 const struct vm_operations_struct *vm_ops; 438 439 /* Information about our backing store: */ 440 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE 441 units */ 442 struct file * vm_file; /* File we map to (can be NULL). */ 443 void * vm_private_data; /* was vm_pte (shared mem) */ 444 445 #ifdef CONFIG_SWAP 446 atomic_long_t swap_readahead_info; 447 #endif 448 #ifndef CONFIG_MMU 449 struct vm_region *vm_region; /* NOMMU mapping region */ 450 #endif 451 #ifdef CONFIG_NUMA 452 struct mempolicy *vm_policy; /* NUMA policy for the VMA */ 453 #endif 454 struct vm_userfaultfd_ctx vm_userfaultfd_ctx; 455 } __randomize_layout; 456 457 struct kioctx_table; 458 struct mm_struct { 459 struct { 460 struct vm_area_struct *mmap; /* list of VMAs */ 461 struct rb_root mm_rb; 462 u64 vmacache_seqnum; /* per-thread vmacache */ 463 #ifdef CONFIG_MMU 464 unsigned long (*get_unmapped_area) (struct file *filp, 465 unsigned long addr, unsigned long len, 466 unsigned long pgoff, unsigned long flags); 467 #endif 468 unsigned long mmap_base; /* base of mmap area */ 469 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */ 470 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES 471 /* Base addresses for compatible mmap() */ 472 unsigned long mmap_compat_base; 473 unsigned long mmap_compat_legacy_base; 474 #endif 475 unsigned long task_size; /* size of task vm space */ 476 unsigned long highest_vm_end; /* highest vma end address */ 477 pgd_t * pgd; 478 479 #ifdef CONFIG_MEMBARRIER 480 /** 481 * @membarrier_state: Flags controlling membarrier behavior. 482 * 483 * This field is close to @pgd to hopefully fit in the same 484 * cache-line, which needs to be touched by switch_mm(). 485 */ 486 atomic_t membarrier_state; 487 #endif 488 489 /** 490 * @mm_users: The number of users including userspace. 491 * 492 * Use mmget()/mmget_not_zero()/mmput() to modify. When this 493 * drops to 0 (i.e. when the task exits and there are no other 494 * temporary reference holders), we also release a reference on 495 * @mm_count (which may then free the &struct mm_struct if 496 * @mm_count also drops to 0). 497 */ 498 atomic_t mm_users; 499 500 /** 501 * @mm_count: The number of references to &struct mm_struct 502 * (@mm_users count as 1). 503 * 504 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the 505 * &struct mm_struct is freed. 506 */ 507 atomic_t mm_count; 508 509 #ifdef CONFIG_MMU 510 atomic_long_t pgtables_bytes; /* PTE page table pages */ 511 #endif 512 int map_count; /* number of VMAs */ 513 514 spinlock_t page_table_lock; /* Protects page tables and some 515 * counters 516 */ 517 /* 518 * With some kernel config, the current mmap_lock's offset 519 * inside 'mm_struct' is at 0x120, which is very optimal, as 520 * its two hot fields 'count' and 'owner' sit in 2 different 521 * cachelines, and when mmap_lock is highly contended, both 522 * of the 2 fields will be accessed frequently, current layout 523 * will help to reduce cache bouncing. 524 * 525 * So please be careful with adding new fields before 526 * mmap_lock, which can easily push the 2 fields into one 527 * cacheline. 528 */ 529 struct rw_semaphore mmap_lock; 530 531 struct list_head mmlist; /* List of maybe swapped mm's. These 532 * are globally strung together off 533 * init_mm.mmlist, and are protected 534 * by mmlist_lock 535 */ 536 537 538 unsigned long hiwater_rss; /* High-watermark of RSS usage */ 539 unsigned long hiwater_vm; /* High-water virtual memory usage */ 540 541 unsigned long total_vm; /* Total pages mapped */ 542 unsigned long locked_vm; /* Pages that have PG_mlocked set */ 543 atomic64_t pinned_vm; /* Refcount permanently increased */ 544 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */ 545 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */ 546 unsigned long stack_vm; /* VM_STACK */ 547 unsigned long def_flags; 548 549 /** 550 * @write_protect_seq: Locked when any thread is write 551 * protecting pages mapped by this mm to enforce a later COW, 552 * for instance during page table copying for fork(). 553 */ 554 seqcount_t write_protect_seq; 555 556 spinlock_t arg_lock; /* protect the below fields */ 557 558 unsigned long start_code, end_code, start_data, end_data; 559 unsigned long start_brk, brk, start_stack; 560 unsigned long arg_start, arg_end, env_start, env_end; 561 562 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */ 563 564 /* 565 * Special counters, in some configurations protected by the 566 * page_table_lock, in other configurations by being atomic. 567 */ 568 struct mm_rss_stat rss_stat; 569 570 struct linux_binfmt *binfmt; 571 572 /* Architecture-specific MM context */ 573 mm_context_t context; 574 575 unsigned long flags; /* Must use atomic bitops to access */ 576 577 #ifdef CONFIG_AIO 578 spinlock_t ioctx_lock; 579 struct kioctx_table __rcu *ioctx_table; 580 #endif 581 #ifdef CONFIG_MEMCG 582 /* 583 * "owner" points to a task that is regarded as the canonical 584 * user/owner of this mm. All of the following must be true in 585 * order for it to be changed: 586 * 587 * current == mm->owner 588 * current->mm != mm 589 * new_owner->mm == mm 590 * new_owner->alloc_lock is held 591 */ 592 struct task_struct __rcu *owner; 593 #endif 594 struct user_namespace *user_ns; 595 596 /* store ref to file /proc/<pid>/exe symlink points to */ 597 struct file __rcu *exe_file; 598 #ifdef CONFIG_MMU_NOTIFIER 599 struct mmu_notifier_subscriptions *notifier_subscriptions; 600 #endif 601 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 602 pgtable_t pmd_huge_pte; /* protected by page_table_lock */ 603 #endif 604 #ifdef CONFIG_NUMA_BALANCING 605 /* 606 * numa_next_scan is the next time that the PTEs will be marked 607 * pte_numa. NUMA hinting faults will gather statistics and 608 * migrate pages to new nodes if necessary. 609 */ 610 unsigned long numa_next_scan; 611 612 /* Restart point for scanning and setting pte_numa */ 613 unsigned long numa_scan_offset; 614 615 /* numa_scan_seq prevents two threads setting pte_numa */ 616 int numa_scan_seq; 617 #endif 618 /* 619 * An operation with batched TLB flushing is going on. Anything 620 * that can move process memory needs to flush the TLB when 621 * moving a PROT_NONE or PROT_NUMA mapped page. 622 */ 623 atomic_t tlb_flush_pending; 624 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 625 /* See flush_tlb_batched_pending() */ 626 bool tlb_flush_batched; 627 #endif 628 struct uprobes_state uprobes_state; 629 #ifdef CONFIG_PREEMPT_RT 630 struct rcu_head delayed_drop; 631 #endif 632 #ifdef CONFIG_HUGETLB_PAGE 633 atomic_long_t hugetlb_usage; 634 #endif 635 struct work_struct async_put_work; 636 637 #ifdef CONFIG_IOMMU_SUPPORT 638 u32 pasid; 639 #endif 640 } __randomize_layout; 641 642 /* 643 * The mm_cpumask needs to be at the end of mm_struct, because it 644 * is dynamically sized based on nr_cpu_ids. 645 */ 646 unsigned long cpu_bitmap[]; 647 }; 648 649 extern struct mm_struct init_mm; 650 651 /* Pointer magic because the dynamic array size confuses some compilers. */ 652 static inline void mm_init_cpumask(struct mm_struct *mm) 653 { 654 unsigned long cpu_bitmap = (unsigned long)mm; 655 656 cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap); 657 cpumask_clear((struct cpumask *)cpu_bitmap); 658 } 659 660 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */ 661 static inline cpumask_t *mm_cpumask(struct mm_struct *mm) 662 { 663 return (struct cpumask *)&mm->cpu_bitmap; 664 } 665 666 struct mmu_gather; 667 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm); 668 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm); 669 extern void tlb_finish_mmu(struct mmu_gather *tlb); 670 671 static inline void init_tlb_flush_pending(struct mm_struct *mm) 672 { 673 atomic_set(&mm->tlb_flush_pending, 0); 674 } 675 676 static inline void inc_tlb_flush_pending(struct mm_struct *mm) 677 { 678 atomic_inc(&mm->tlb_flush_pending); 679 /* 680 * The only time this value is relevant is when there are indeed pages 681 * to flush. And we'll only flush pages after changing them, which 682 * requires the PTL. 683 * 684 * So the ordering here is: 685 * 686 * atomic_inc(&mm->tlb_flush_pending); 687 * spin_lock(&ptl); 688 * ... 689 * set_pte_at(); 690 * spin_unlock(&ptl); 691 * 692 * spin_lock(&ptl) 693 * mm_tlb_flush_pending(); 694 * .... 695 * spin_unlock(&ptl); 696 * 697 * flush_tlb_range(); 698 * atomic_dec(&mm->tlb_flush_pending); 699 * 700 * Where the increment if constrained by the PTL unlock, it thus 701 * ensures that the increment is visible if the PTE modification is 702 * visible. After all, if there is no PTE modification, nobody cares 703 * about TLB flushes either. 704 * 705 * This very much relies on users (mm_tlb_flush_pending() and 706 * mm_tlb_flush_nested()) only caring about _specific_ PTEs (and 707 * therefore specific PTLs), because with SPLIT_PTE_PTLOCKS and RCpc 708 * locks (PPC) the unlock of one doesn't order against the lock of 709 * another PTL. 710 * 711 * The decrement is ordered by the flush_tlb_range(), such that 712 * mm_tlb_flush_pending() will not return false unless all flushes have 713 * completed. 714 */ 715 } 716 717 static inline void dec_tlb_flush_pending(struct mm_struct *mm) 718 { 719 /* 720 * See inc_tlb_flush_pending(). 721 * 722 * This cannot be smp_mb__before_atomic() because smp_mb() simply does 723 * not order against TLB invalidate completion, which is what we need. 724 * 725 * Therefore we must rely on tlb_flush_*() to guarantee order. 726 */ 727 atomic_dec(&mm->tlb_flush_pending); 728 } 729 730 static inline bool mm_tlb_flush_pending(struct mm_struct *mm) 731 { 732 /* 733 * Must be called after having acquired the PTL; orders against that 734 * PTLs release and therefore ensures that if we observe the modified 735 * PTE we must also observe the increment from inc_tlb_flush_pending(). 736 * 737 * That is, it only guarantees to return true if there is a flush 738 * pending for _this_ PTL. 739 */ 740 return atomic_read(&mm->tlb_flush_pending); 741 } 742 743 static inline bool mm_tlb_flush_nested(struct mm_struct *mm) 744 { 745 /* 746 * Similar to mm_tlb_flush_pending(), we must have acquired the PTL 747 * for which there is a TLB flush pending in order to guarantee 748 * we've seen both that PTE modification and the increment. 749 * 750 * (no requirement on actually still holding the PTL, that is irrelevant) 751 */ 752 return atomic_read(&mm->tlb_flush_pending) > 1; 753 } 754 755 struct vm_fault; 756 757 /** 758 * typedef vm_fault_t - Return type for page fault handlers. 759 * 760 * Page fault handlers return a bitmask of %VM_FAULT values. 761 */ 762 typedef __bitwise unsigned int vm_fault_t; 763 764 /** 765 * enum vm_fault_reason - Page fault handlers return a bitmask of 766 * these values to tell the core VM what happened when handling the 767 * fault. Used to decide whether a process gets delivered SIGBUS or 768 * just gets major/minor fault counters bumped up. 769 * 770 * @VM_FAULT_OOM: Out Of Memory 771 * @VM_FAULT_SIGBUS: Bad access 772 * @VM_FAULT_MAJOR: Page read from storage 773 * @VM_FAULT_WRITE: Special case for get_user_pages 774 * @VM_FAULT_HWPOISON: Hit poisoned small page 775 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded 776 * in upper bits 777 * @VM_FAULT_SIGSEGV: segmentation fault 778 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page 779 * @VM_FAULT_LOCKED: ->fault locked the returned page 780 * @VM_FAULT_RETRY: ->fault blocked, must retry 781 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small 782 * @VM_FAULT_DONE_COW: ->fault has fully handled COW 783 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs 784 * fsync() to complete (for synchronous page faults 785 * in DAX) 786 * @VM_FAULT_HINDEX_MASK: mask HINDEX value 787 * 788 */ 789 enum vm_fault_reason { 790 VM_FAULT_OOM = (__force vm_fault_t)0x000001, 791 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002, 792 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004, 793 VM_FAULT_WRITE = (__force vm_fault_t)0x000008, 794 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010, 795 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020, 796 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040, 797 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100, 798 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200, 799 VM_FAULT_RETRY = (__force vm_fault_t)0x000400, 800 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800, 801 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, 802 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, 803 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, 804 }; 805 806 /* Encode hstate index for a hwpoisoned large page */ 807 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16)) 808 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf) 809 810 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \ 811 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \ 812 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) 813 814 #define VM_FAULT_RESULT_TRACE \ 815 { VM_FAULT_OOM, "OOM" }, \ 816 { VM_FAULT_SIGBUS, "SIGBUS" }, \ 817 { VM_FAULT_MAJOR, "MAJOR" }, \ 818 { VM_FAULT_WRITE, "WRITE" }, \ 819 { VM_FAULT_HWPOISON, "HWPOISON" }, \ 820 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \ 821 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \ 822 { VM_FAULT_NOPAGE, "NOPAGE" }, \ 823 { VM_FAULT_LOCKED, "LOCKED" }, \ 824 { VM_FAULT_RETRY, "RETRY" }, \ 825 { VM_FAULT_FALLBACK, "FALLBACK" }, \ 826 { VM_FAULT_DONE_COW, "DONE_COW" }, \ 827 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" } 828 829 struct vm_special_mapping { 830 const char *name; /* The name, e.g. "[vdso]". */ 831 832 /* 833 * If .fault is not provided, this points to a 834 * NULL-terminated array of pages that back the special mapping. 835 * 836 * This must not be NULL unless .fault is provided. 837 */ 838 struct page **pages; 839 840 /* 841 * If non-NULL, then this is called to resolve page faults 842 * on the special mapping. If used, .pages is not checked. 843 */ 844 vm_fault_t (*fault)(const struct vm_special_mapping *sm, 845 struct vm_area_struct *vma, 846 struct vm_fault *vmf); 847 848 int (*mremap)(const struct vm_special_mapping *sm, 849 struct vm_area_struct *new_vma); 850 }; 851 852 enum tlb_flush_reason { 853 TLB_FLUSH_ON_TASK_SWITCH, 854 TLB_REMOTE_SHOOTDOWN, 855 TLB_LOCAL_SHOOTDOWN, 856 TLB_LOCAL_MM_SHOOTDOWN, 857 TLB_REMOTE_SEND_IPI, 858 NR_TLB_FLUSH_REASONS, 859 }; 860 861 /* 862 * A swap entry has to fit into a "unsigned long", as the entry is hidden 863 * in the "index" field of the swapper address space. 864 */ 865 typedef struct { 866 unsigned long val; 867 } swp_entry_t; 868 869 #endif /* _LINUX_MM_TYPES_H */ 870