1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MM_TYPES_H
3 #define _LINUX_MM_TYPES_H
5 #include <linux/mm_types_task.h>
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>
25 #ifndef AT_VECTOR_SIZE_ARCH
26 #define AT_VECTOR_SIZE_ARCH 0
28 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
36 * Each physical page in the system has a struct page associated with
37 * it to keep track of whatever it is we are using the page for at the
38 * moment. Note that we have no way to track which tasks are using
39 * a page, though if it is a pagecache page, rmap structures can tell us
42 * If you allocate the page using alloc_pages(), you can use some of the
43 * space in struct page for your own purposes. The five words in the main
44 * union are available, except for bit 0 of the first word which must be
45 * kept clear. Many users use this word to store a pointer to an object
46 * which is guaranteed to be aligned. If you use the same storage as
47 * page->mapping, you must restore it to NULL before freeing the page.
49 * If your page will not be mapped to userspace, you can also use the four
50 * bytes in the mapcount union, but you must call page_mapcount_reset()
53 * If you want to use the refcount field, it must be used in such a way
54 * that other CPUs temporarily incrementing and then decrementing the
55 * refcount does not cause problems. On receiving the page from
56 * alloc_pages(), the refcount will be positive.
58 * If you allocate pages of order > 0, you can use some of the fields
59 * in each subpage, but you may need to restore some of their values
62 * SLUB uses cmpxchg_double() to atomically update its freelist and counters.
63 * That requires that freelist & counters in struct slab be adjacent and
64 * double-word aligned. Because struct slab currently just reinterprets the
65 * bits of struct page, we align all struct pages to double-word boundaries,
66 * and ensure that 'freelist' is aligned within struct slab.
68 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
69 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
71 #define _struct_page_alignment __aligned(sizeof(unsigned long))
75 unsigned long flags; /* Atomic flags, some possibly
76 * updated asynchronously */
78 * Five words (20/40 bytes) are available in this union.
79 * WARNING: bit 0 of the first word is used for PageTail(). That
80 * means the other users of this union MUST NOT use the bit to
81 * avoid collision and false-positive PageTail().
84 struct { /* Page cache and anonymous pages */
86 * @lru: Pageout list, eg. active_list protected by
87 * lruvec->lru_lock. Sometimes used as a generic list
93 /* Or, for the Unevictable "LRU list" slot */
95 /* Always even, to negate PageTail */
97 /* Count page's or folio's mlocks */
98 unsigned int mlock_count;
102 struct list_head buddy_list;
103 struct list_head pcp_list;
105 /* See page-flags.h for PAGE_MAPPING_FLAGS */
106 struct address_space *mapping;
108 pgoff_t index; /* Our offset within mapping. */
109 unsigned long share; /* share count for fsdax */
112 * @private: Mapping-private opaque data.
113 * Usually used for buffer_heads if PagePrivate.
114 * Used for swp_entry_t if PageSwapCache.
115 * Indicates order in the buddy system if PageBuddy.
117 unsigned long private;
119 struct { /* page_pool used by netstack */
121 * @pp_magic: magic value to avoid recycling non
122 * page_pool allocated pages.
124 unsigned long pp_magic;
125 struct page_pool *pp;
126 unsigned long _pp_mapping_pad;
127 unsigned long dma_addr;
128 atomic_long_t pp_frag_count;
130 struct { /* Tail pages of compound page */
131 unsigned long compound_head; /* Bit zero is set */
133 struct { /* ZONE_DEVICE pages */
134 /** @pgmap: Points to the hosting device page map. */
135 struct dev_pagemap *pgmap;
136 void *zone_device_data;
138 * ZONE_DEVICE private pages are counted as being
139 * mapped so the next 3 words hold the mapping, index,
140 * and private fields from the source anonymous or
141 * page cache page while the page is migrated to device
143 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
144 * use the mapping, index, and private fields when
145 * pmem backed DAX files are mapped.
149 /** @rcu_head: You can use this to free a page by RCU. */
150 struct rcu_head rcu_head;
153 union { /* This union is 4 bytes in size. */
155 * If the page can be mapped to userspace, encodes the number
156 * of times this page is referenced by a page table.
161 * If the page is neither PageSlab nor mappable to userspace,
162 * the value stored here may help determine what this page
163 * is used for. See page-flags.h for a list of page types
164 * which are currently stored here.
166 unsigned int page_type;
169 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
173 unsigned long memcg_data;
177 * On machines where all RAM is mapped into kernel address space,
178 * we can simply calculate the virtual address. On machines with
179 * highmem some memory is mapped into kernel virtual memory
180 * dynamically, so we need a place to store that address.
181 * Note that this field could be 16 bits on x86 ... ;)
183 * Architectures with slow multiplication can define
184 * WANT_PAGE_VIRTUAL in asm/page.h
186 #if defined(WANT_PAGE_VIRTUAL)
187 void *virtual; /* Kernel virtual address (NULL if
188 not kmapped, ie. highmem) */
189 #endif /* WANT_PAGE_VIRTUAL */
191 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
197 * KMSAN metadata for this page:
198 * - shadow page: every bit indicates whether the corresponding
199 * bit of the original page is initialized (0) or not (1);
200 * - origin page: every 4 bytes contain an id of the stack trace
201 * where the uninitialized value was created.
203 struct page *kmsan_shadow;
204 struct page *kmsan_origin;
206 } _struct_page_alignment;
209 * struct encoded_page - a nonexistent type marking this pointer
211 * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
212 * with the low bits of the pointer indicating extra context-dependent
213 * information. Not super-common, but happens in mmu_gather and mlock
214 * handling, and this acts as a type system check on that use.
216 * We only really have two guaranteed bits in general, although you could
217 * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
220 * Use the supplied helper functions to endcode/decode the pointer and bits.
223 #define ENCODE_PAGE_BITS 3ul
224 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
226 BUILD_BUG_ON(flags > ENCODE_PAGE_BITS);
227 return (struct encoded_page *)(flags | (unsigned long)page);
230 static inline unsigned long encoded_page_flags(struct encoded_page *page)
232 return ENCODE_PAGE_BITS & (unsigned long)page;
235 static inline struct page *encoded_page_ptr(struct encoded_page *page)
237 return (struct page *)(~ENCODE_PAGE_BITS & (unsigned long)page);
241 * A swap entry has to fit into a "unsigned long", as the entry is hidden
242 * in the "index" field of the swapper address space.
249 * struct folio - Represents a contiguous set of bytes.
250 * @flags: Identical to the page flags.
251 * @lru: Least Recently Used list; tracks how recently this folio was used.
252 * @mlock_count: Number of times this folio has been pinned by mlock().
253 * @mapping: The file this page belongs to, or refers to the anon_vma for
255 * @index: Offset within the file, in units of pages. For anonymous memory,
256 * this is the index from the beginning of the mmap.
257 * @private: Filesystem per-folio data (see folio_attach_private()).
258 * @swap: Used for swp_entry_t if folio_test_swapcache().
259 * @_mapcount: Do not access this member directly. Use folio_mapcount() to
260 * find out how many times this folio is mapped by userspace.
261 * @_refcount: Do not access this member directly. Use folio_ref_count()
262 * to find how many references there are to this folio.
263 * @memcg_data: Memory Control Group data.
264 * @virtual: Virtual address in the kernel direct map.
265 * @_last_cpupid: IDs of last CPU and last process that accessed the folio.
266 * @_entire_mapcount: Do not use directly, call folio_entire_mapcount().
267 * @_nr_pages_mapped: Do not use directly, call folio_mapcount().
268 * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
269 * @_folio_nr_pages: Do not use directly, call folio_nr_pages().
270 * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
271 * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
272 * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
273 * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
274 * @_deferred_list: Folios to be split under memory pressure.
276 * A folio is a physically, virtually and logically contiguous set
277 * of bytes. It is a power-of-two in size, and it is aligned to that
278 * same power-of-two. It is at least as large as %PAGE_SIZE. If it is
279 * in the page cache, it is at a file offset which is a multiple of that
280 * power-of-two. It may be mapped into userspace at an address which is
281 * at an arbitrary page offset, but its kernel virtual address is aligned
285 /* private: don't document the anon union */
291 struct list_head lru;
292 /* private: avoid cluttering the output */
296 unsigned int mlock_count;
301 struct address_space *mapping;
310 unsigned long memcg_data;
312 #if defined(WANT_PAGE_VIRTUAL)
315 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
318 /* private: the union with struct page is transitional */
324 unsigned long _flags_1;
325 unsigned long _head_1;
326 unsigned long _folio_avail;
328 atomic_t _entire_mapcount;
329 atomic_t _nr_pages_mapped;
332 unsigned int _folio_nr_pages;
334 /* private: the union with struct page is transitional */
336 struct page __page_1;
340 unsigned long _flags_2;
341 unsigned long _head_2;
343 void *_hugetlb_subpool;
344 void *_hugetlb_cgroup;
345 void *_hugetlb_cgroup_rsvd;
346 void *_hugetlb_hwpoison;
347 /* private: the union with struct page is transitional */
350 unsigned long _flags_2a;
351 unsigned long _head_2a;
353 struct list_head _deferred_list;
354 /* private: the union with struct page is transitional */
356 struct page __page_2;
360 #define FOLIO_MATCH(pg, fl) \
361 static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
362 FOLIO_MATCH(flags, flags);
363 FOLIO_MATCH(lru, lru);
364 FOLIO_MATCH(mapping, mapping);
365 FOLIO_MATCH(compound_head, lru);
366 FOLIO_MATCH(index, index);
367 FOLIO_MATCH(private, private);
368 FOLIO_MATCH(_mapcount, _mapcount);
369 FOLIO_MATCH(_refcount, _refcount);
371 FOLIO_MATCH(memcg_data, memcg_data);
373 #if defined(WANT_PAGE_VIRTUAL)
374 FOLIO_MATCH(virtual, virtual);
376 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
377 FOLIO_MATCH(_last_cpupid, _last_cpupid);
380 #define FOLIO_MATCH(pg, fl) \
381 static_assert(offsetof(struct folio, fl) == \
382 offsetof(struct page, pg) + sizeof(struct page))
383 FOLIO_MATCH(flags, _flags_1);
384 FOLIO_MATCH(compound_head, _head_1);
386 #define FOLIO_MATCH(pg, fl) \
387 static_assert(offsetof(struct folio, fl) == \
388 offsetof(struct page, pg) + 2 * sizeof(struct page))
389 FOLIO_MATCH(flags, _flags_2);
390 FOLIO_MATCH(compound_head, _head_2);
391 FOLIO_MATCH(flags, _flags_2a);
392 FOLIO_MATCH(compound_head, _head_2a);
396 * struct ptdesc - Memory descriptor for page tables.
397 * @__page_flags: Same as page flags. Unused for page tables.
398 * @pt_rcu_head: For freeing page table pages.
399 * @pt_list: List of used page tables. Used for s390 and x86.
400 * @_pt_pad_1: Padding that aliases with page's compound head.
401 * @pmd_huge_pte: Protected by ptdesc->ptl, used for THPs.
402 * @__page_mapping: Aliases with page->mapping. Unused for page tables.
403 * @pt_mm: Used for x86 pgds.
404 * @pt_frag_refcount: For fragmented page table tracking. Powerpc and s390 only.
405 * @_pt_pad_2: Padding to ensure proper alignment.
406 * @ptl: Lock for the page table.
407 * @__page_type: Same as page->page_type. Unused for page tables.
408 * @_refcount: Same as page refcount. Used for s390 page tables.
409 * @pt_memcg_data: Memcg data. Tracked for page tables here.
411 * This struct overlays struct page for now. Do not modify without a good
412 * understanding of the issues.
415 unsigned long __page_flags;
418 struct rcu_head pt_rcu_head;
419 struct list_head pt_list;
421 unsigned long _pt_pad_1;
422 pgtable_t pmd_huge_pte;
425 unsigned long __page_mapping;
428 struct mm_struct *pt_mm;
429 atomic_t pt_frag_refcount;
433 unsigned long _pt_pad_2;
434 #if ALLOC_SPLIT_PTLOCKS
440 unsigned int __page_type;
443 unsigned long pt_memcg_data;
447 #define TABLE_MATCH(pg, pt) \
448 static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt))
449 TABLE_MATCH(flags, __page_flags);
450 TABLE_MATCH(compound_head, pt_list);
451 TABLE_MATCH(compound_head, _pt_pad_1);
452 TABLE_MATCH(mapping, __page_mapping);
453 TABLE_MATCH(rcu_head, pt_rcu_head);
454 TABLE_MATCH(page_type, __page_type);
455 TABLE_MATCH(_refcount, _refcount);
457 TABLE_MATCH(memcg_data, pt_memcg_data);
460 static_assert(sizeof(struct ptdesc) <= sizeof(struct page));
462 #define ptdesc_page(pt) (_Generic((pt), \
463 const struct ptdesc *: (const struct page *)(pt), \
464 struct ptdesc *: (struct page *)(pt)))
466 #define ptdesc_folio(pt) (_Generic((pt), \
467 const struct ptdesc *: (const struct folio *)(pt), \
468 struct ptdesc *: (struct folio *)(pt)))
470 #define page_ptdesc(p) (_Generic((p), \
471 const struct page *: (const struct ptdesc *)(p), \
472 struct page *: (struct ptdesc *)(p)))
475 * Used for sizing the vmemmap region on some architectures
477 #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page)))
479 #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK)
480 #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE)
483 * page_private can be used on tail pages. However, PagePrivate is only
484 * checked by the VM on the head page. So page_private on the tail pages
485 * should be used for data that's ancillary to the head page (eg attaching
486 * buffer heads to tail pages after attaching buffer heads to the head page)
488 #define page_private(page) ((page)->private)
490 static inline void set_page_private(struct page *page, unsigned long private)
492 page->private = private;
495 static inline void *folio_get_private(struct folio *folio)
497 return folio->private;
500 struct page_frag_cache {
502 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
508 /* we maintain a pagecount bias, so that we dont dirty cache line
509 * containing page->_refcount every time we allocate a fragment.
511 unsigned int pagecnt_bias;
515 typedef unsigned long vm_flags_t;
518 * A region containing a mapping of a non-memory backed file under NOMMU
519 * conditions. These are held in a global tree and are pinned by the VMAs that
523 struct rb_node vm_rb; /* link in global region tree */
524 vm_flags_t vm_flags; /* VMA vm_flags */
525 unsigned long vm_start; /* start address of region */
526 unsigned long vm_end; /* region initialised to here */
527 unsigned long vm_top; /* region allocated to here */
528 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
529 struct file *vm_file; /* the backing file or NULL */
531 int vm_usage; /* region usage count (access under nommu_region_sem) */
532 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
536 #ifdef CONFIG_USERFAULTFD
537 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
538 struct vm_userfaultfd_ctx {
539 struct userfaultfd_ctx *ctx;
541 #else /* CONFIG_USERFAULTFD */
542 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
543 struct vm_userfaultfd_ctx {};
544 #endif /* CONFIG_USERFAULTFD */
546 struct anon_vma_name {
548 /* The name needs to be at the end because it is dynamically sized. */
552 #ifdef CONFIG_ANON_VMA_NAME
554 * mmap_lock should be read-locked when calling anon_vma_name(). Caller should
555 * either keep holding the lock while using the returned pointer or it should
556 * raise anon_vma_name refcount before releasing the lock.
558 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma);
559 struct anon_vma_name *anon_vma_name_alloc(const char *name);
560 void anon_vma_name_free(struct kref *kref);
561 #else /* CONFIG_ANON_VMA_NAME */
562 static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
567 static inline struct anon_vma_name *anon_vma_name_alloc(const char *name)
574 struct rw_semaphore lock;
577 struct vma_numab_state {
579 * Initialised as time in 'jiffies' after which VMA
580 * should be scanned. Delays first scan of new VMA by at
581 * least sysctl_numa_balancing_scan_delay:
583 unsigned long next_scan;
586 * Time in jiffies when pids_active[] is reset to
587 * detect phase change behaviour:
589 unsigned long pids_active_reset;
592 * Approximate tracking of PIDs that trapped a NUMA hinting
593 * fault. May produce false positives due to hash collisions.
595 * [0] Previous PID tracking
596 * [1] Current PID tracking
598 * Window moves after next_pid_reset has expired approximately
599 * every VMA_PID_RESET_PERIOD jiffies:
601 unsigned long pids_active[2];
604 * MM scan sequence ID when the VMA was last completely scanned.
605 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq
611 * This struct describes a virtual memory area. There is one of these
612 * per VM-area/task. A VM area is any part of the process virtual memory
613 * space that has a special rule for the page-fault handlers (ie a shared
614 * library, the executable area etc).
616 struct vm_area_struct {
617 /* The first cache line has the info for VMA tree walking. */
621 /* VMA covers [vm_start; vm_end) addresses within mm */
622 unsigned long vm_start;
623 unsigned long vm_end;
625 #ifdef CONFIG_PER_VMA_LOCK
626 struct rcu_head vm_rcu; /* Used for deferred freeing. */
630 struct mm_struct *vm_mm; /* The address space we belong to. */
631 pgprot_t vm_page_prot; /* Access permissions of this VMA. */
635 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
638 const vm_flags_t vm_flags;
639 vm_flags_t __private __vm_flags;
642 #ifdef CONFIG_PER_VMA_LOCK
644 * Can only be written (using WRITE_ONCE()) while holding both:
645 * - mmap_lock (in write mode)
646 * - vm_lock->lock (in write mode)
647 * Can be read reliably while holding one of:
648 * - mmap_lock (in read or write mode)
649 * - vm_lock->lock (in read or write mode)
650 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
651 * while holding nothing (except RCU to keep the VMA struct allocated).
653 * This sequence counter is explicitly allowed to overflow; sequence
654 * counter reuse can only lead to occasional unnecessary use of the
658 struct vma_lock *vm_lock;
660 /* Flag to indicate areas detached from the mm->mm_mt tree */
665 * For areas with an address space and backing store,
666 * linkage into the address_space->i_mmap interval tree.
671 unsigned long rb_subtree_last;
675 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
676 * list, after a COW of one of the file pages. A MAP_SHARED vma
677 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
678 * or brk vma (with NULL file) can only be in an anon_vma list.
680 struct list_head anon_vma_chain; /* Serialized by mmap_lock &
682 struct anon_vma *anon_vma; /* Serialized by page_table_lock */
684 /* Function pointers to deal with this struct. */
685 const struct vm_operations_struct *vm_ops;
687 /* Information about our backing store: */
688 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
690 struct file * vm_file; /* File we map to (can be NULL). */
691 void * vm_private_data; /* was vm_pte (shared mem) */
693 #ifdef CONFIG_ANON_VMA_NAME
695 * For private and shared anonymous mappings, a pointer to a null
696 * terminated string containing the name given to the vma, or NULL if
697 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
699 struct anon_vma_name *anon_name;
702 atomic_long_t swap_readahead_info;
705 struct vm_region *vm_region; /* NOMMU mapping region */
708 struct mempolicy *vm_policy; /* NUMA policy for the VMA */
710 #ifdef CONFIG_NUMA_BALANCING
711 struct vma_numab_state *numab_state; /* NUMA Balancing state */
713 struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
714 } __randomize_layout;
717 #define vma_policy(vma) ((vma)->vm_policy)
719 #define vma_policy(vma) NULL
722 #ifdef CONFIG_SCHED_MM_CID
733 * Fields which are often written to are placed in a separate
738 * @mm_count: The number of references to &struct
739 * mm_struct (@mm_users count as 1).
741 * Use mmgrab()/mmdrop() to modify. When this drops to
742 * 0, the &struct mm_struct is freed.
745 } ____cacheline_aligned_in_smp;
747 struct maple_tree mm_mt;
749 unsigned long (*get_unmapped_area) (struct file *filp,
750 unsigned long addr, unsigned long len,
751 unsigned long pgoff, unsigned long flags);
753 unsigned long mmap_base; /* base of mmap area */
754 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
755 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
756 /* Base addresses for compatible mmap() */
757 unsigned long mmap_compat_base;
758 unsigned long mmap_compat_legacy_base;
760 unsigned long task_size; /* size of task vm space */
763 #ifdef CONFIG_MEMBARRIER
765 * @membarrier_state: Flags controlling membarrier behavior.
767 * This field is close to @pgd to hopefully fit in the same
768 * cache-line, which needs to be touched by switch_mm().
770 atomic_t membarrier_state;
774 * @mm_users: The number of users including userspace.
776 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
777 * drops to 0 (i.e. when the task exits and there are no other
778 * temporary reference holders), we also release a reference on
779 * @mm_count (which may then free the &struct mm_struct if
780 * @mm_count also drops to 0).
784 #ifdef CONFIG_SCHED_MM_CID
786 * @pcpu_cid: Per-cpu current cid.
788 * Keep track of the currently allocated mm_cid for each cpu.
789 * The per-cpu mm_cid values are serialized by their respective
792 struct mm_cid __percpu *pcpu_cid;
794 * @mm_cid_next_scan: Next mm_cid scan (in jiffies).
796 * When the next mm_cid scan is due (in jiffies).
798 unsigned long mm_cid_next_scan;
801 atomic_long_t pgtables_bytes; /* size of all page tables */
803 int map_count; /* number of VMAs */
805 spinlock_t page_table_lock; /* Protects page tables and some
809 * With some kernel config, the current mmap_lock's offset
810 * inside 'mm_struct' is at 0x120, which is very optimal, as
811 * its two hot fields 'count' and 'owner' sit in 2 different
812 * cachelines, and when mmap_lock is highly contended, both
813 * of the 2 fields will be accessed frequently, current layout
814 * will help to reduce cache bouncing.
816 * So please be careful with adding new fields before
817 * mmap_lock, which can easily push the 2 fields into one
820 struct rw_semaphore mmap_lock;
822 struct list_head mmlist; /* List of maybe swapped mm's. These
823 * are globally strung together off
824 * init_mm.mmlist, and are protected
827 #ifdef CONFIG_PER_VMA_LOCK
829 * This field has lock-like semantics, meaning it is sometimes
830 * accessed with ACQUIRE/RELEASE semantics.
831 * Roughly speaking, incrementing the sequence number is
832 * equivalent to releasing locks on VMAs; reading the sequence
833 * number can be part of taking a read lock on a VMA.
835 * Can be modified under write mmap_lock using RELEASE
837 * Can be read with no other protection when holding write
839 * Can be read with ACQUIRE semantics if not holding write
846 unsigned long hiwater_rss; /* High-watermark of RSS usage */
847 unsigned long hiwater_vm; /* High-water virtual memory usage */
849 unsigned long total_vm; /* Total pages mapped */
850 unsigned long locked_vm; /* Pages that have PG_mlocked set */
851 atomic64_t pinned_vm; /* Refcount permanently increased */
852 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
853 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
854 unsigned long stack_vm; /* VM_STACK */
855 unsigned long def_flags;
858 * @write_protect_seq: Locked when any thread is write
859 * protecting pages mapped by this mm to enforce a later COW,
860 * for instance during page table copying for fork().
862 seqcount_t write_protect_seq;
864 spinlock_t arg_lock; /* protect the below fields */
866 unsigned long start_code, end_code, start_data, end_data;
867 unsigned long start_brk, brk, start_stack;
868 unsigned long arg_start, arg_end, env_start, env_end;
870 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
872 struct percpu_counter rss_stat[NR_MM_COUNTERS];
874 struct linux_binfmt *binfmt;
876 /* Architecture-specific MM context */
877 mm_context_t context;
879 unsigned long flags; /* Must use atomic bitops to access */
882 spinlock_t ioctx_lock;
883 struct kioctx_table __rcu *ioctx_table;
887 * "owner" points to a task that is regarded as the canonical
888 * user/owner of this mm. All of the following must be true in
889 * order for it to be changed:
891 * current == mm->owner
893 * new_owner->mm == mm
894 * new_owner->alloc_lock is held
896 struct task_struct __rcu *owner;
898 struct user_namespace *user_ns;
900 /* store ref to file /proc/<pid>/exe symlink points to */
901 struct file __rcu *exe_file;
902 #ifdef CONFIG_MMU_NOTIFIER
903 struct mmu_notifier_subscriptions *notifier_subscriptions;
905 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
906 pgtable_t pmd_huge_pte; /* protected by page_table_lock */
908 #ifdef CONFIG_NUMA_BALANCING
910 * numa_next_scan is the next time that PTEs will be remapped
911 * PROT_NONE to trigger NUMA hinting faults; such faults gather
912 * statistics and migrate pages to new nodes if necessary.
914 unsigned long numa_next_scan;
916 /* Restart point for scanning and remapping PTEs. */
917 unsigned long numa_scan_offset;
919 /* numa_scan_seq prevents two threads remapping PTEs. */
923 * An operation with batched TLB flushing is going on. Anything
924 * that can move process memory needs to flush the TLB when
925 * moving a PROT_NONE mapped page.
927 atomic_t tlb_flush_pending;
928 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
929 /* See flush_tlb_batched_pending() */
930 atomic_t tlb_flush_batched;
932 struct uprobes_state uprobes_state;
933 #ifdef CONFIG_PREEMPT_RT
934 struct rcu_head delayed_drop;
936 #ifdef CONFIG_HUGETLB_PAGE
937 atomic_long_t hugetlb_usage;
939 struct work_struct async_put_work;
941 #ifdef CONFIG_IOMMU_SVA
946 * Represent how many pages of this process are involved in KSM
947 * merging (not including ksm_zero_pages).
949 unsigned long ksm_merging_pages;
951 * Represent how many pages are checked for ksm merging
952 * including merged and not merged.
954 unsigned long ksm_rmap_items;
956 * Represent how many empty pages are merged with kernel zero
957 * pages when enabling KSM use_zero_pages.
959 unsigned long ksm_zero_pages;
960 #endif /* CONFIG_KSM */
961 #ifdef CONFIG_LRU_GEN
963 /* this mm_struct is on lru_gen_mm_list */
964 struct list_head list;
966 * Set when switching to this mm_struct, as a hint of
967 * whether it has been used since the last time per-node
968 * page table walkers cleared the corresponding bits.
970 unsigned long bitmap;
972 /* points to the memcg of "owner" above */
973 struct mem_cgroup *memcg;
976 #endif /* CONFIG_LRU_GEN */
977 } __randomize_layout;
980 * The mm_cpumask needs to be at the end of mm_struct, because it
981 * is dynamically sized based on nr_cpu_ids.
983 unsigned long cpu_bitmap[];
986 #define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
988 extern struct mm_struct init_mm;
990 /* Pointer magic because the dynamic array size confuses some compilers. */
991 static inline void mm_init_cpumask(struct mm_struct *mm)
993 unsigned long cpu_bitmap = (unsigned long)mm;
995 cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
996 cpumask_clear((struct cpumask *)cpu_bitmap);
999 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
1000 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
1002 return (struct cpumask *)&mm->cpu_bitmap;
1005 #ifdef CONFIG_LRU_GEN
1007 struct lru_gen_mm_list {
1008 /* mm_struct list for page table walkers */
1009 struct list_head fifo;
1010 /* protects the list above */
1014 void lru_gen_add_mm(struct mm_struct *mm);
1015 void lru_gen_del_mm(struct mm_struct *mm);
1017 void lru_gen_migrate_mm(struct mm_struct *mm);
1020 static inline void lru_gen_init_mm(struct mm_struct *mm)
1022 INIT_LIST_HEAD(&mm->lru_gen.list);
1023 mm->lru_gen.bitmap = 0;
1025 mm->lru_gen.memcg = NULL;
1029 static inline void lru_gen_use_mm(struct mm_struct *mm)
1032 * When the bitmap is set, page reclaim knows this mm_struct has been
1033 * used since the last time it cleared the bitmap. So it might be worth
1034 * walking the page tables of this mm_struct to clear the accessed bit.
1036 WRITE_ONCE(mm->lru_gen.bitmap, -1);
1039 #else /* !CONFIG_LRU_GEN */
1041 static inline void lru_gen_add_mm(struct mm_struct *mm)
1045 static inline void lru_gen_del_mm(struct mm_struct *mm)
1050 static inline void lru_gen_migrate_mm(struct mm_struct *mm)
1055 static inline void lru_gen_init_mm(struct mm_struct *mm)
1059 static inline void lru_gen_use_mm(struct mm_struct *mm)
1063 #endif /* CONFIG_LRU_GEN */
1065 struct vma_iterator {
1066 struct ma_state mas;
1069 #define VMA_ITERATOR(name, __mm, __addr) \
1070 struct vma_iterator name = { \
1072 .tree = &(__mm)->mm_mt, \
1074 .node = MAS_START, \
1078 static inline void vma_iter_init(struct vma_iterator *vmi,
1079 struct mm_struct *mm, unsigned long addr)
1081 mas_init(&vmi->mas, &mm->mm_mt, addr);
1084 #ifdef CONFIG_SCHED_MM_CID
1087 MM_CID_UNSET = -1U, /* Unset state has lazy_put flag set. */
1088 MM_CID_LAZY_PUT = (1U << 31),
1091 static inline bool mm_cid_is_unset(int cid)
1093 return cid == MM_CID_UNSET;
1096 static inline bool mm_cid_is_lazy_put(int cid)
1098 return !mm_cid_is_unset(cid) && (cid & MM_CID_LAZY_PUT);
1101 static inline bool mm_cid_is_valid(int cid)
1103 return !(cid & MM_CID_LAZY_PUT);
1106 static inline int mm_cid_set_lazy_put(int cid)
1108 return cid | MM_CID_LAZY_PUT;
1111 static inline int mm_cid_clear_lazy_put(int cid)
1113 return cid & ~MM_CID_LAZY_PUT;
1116 /* Accessor for struct mm_struct's cidmask. */
1117 static inline cpumask_t *mm_cidmask(struct mm_struct *mm)
1119 unsigned long cid_bitmap = (unsigned long)mm;
1121 cid_bitmap += offsetof(struct mm_struct, cpu_bitmap);
1122 /* Skip cpu_bitmap */
1123 cid_bitmap += cpumask_size();
1124 return (struct cpumask *)cid_bitmap;
1127 static inline void mm_init_cid(struct mm_struct *mm)
1131 for_each_possible_cpu(i) {
1132 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, i);
1134 pcpu_cid->cid = MM_CID_UNSET;
1137 cpumask_clear(mm_cidmask(mm));
1140 static inline int mm_alloc_cid(struct mm_struct *mm)
1142 mm->pcpu_cid = alloc_percpu(struct mm_cid);
1149 static inline void mm_destroy_cid(struct mm_struct *mm)
1151 free_percpu(mm->pcpu_cid);
1152 mm->pcpu_cid = NULL;
1155 static inline unsigned int mm_cid_size(void)
1157 return cpumask_size();
1159 #else /* CONFIG_SCHED_MM_CID */
1160 static inline void mm_init_cid(struct mm_struct *mm) { }
1161 static inline int mm_alloc_cid(struct mm_struct *mm) { return 0; }
1162 static inline void mm_destroy_cid(struct mm_struct *mm) { }
1163 static inline unsigned int mm_cid_size(void)
1167 #endif /* CONFIG_SCHED_MM_CID */
1170 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
1171 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
1172 extern void tlb_finish_mmu(struct mmu_gather *tlb);
1177 * typedef vm_fault_t - Return type for page fault handlers.
1179 * Page fault handlers return a bitmask of %VM_FAULT values.
1181 typedef __bitwise unsigned int vm_fault_t;
1184 * enum vm_fault_reason - Page fault handlers return a bitmask of
1185 * these values to tell the core VM what happened when handling the
1186 * fault. Used to decide whether a process gets delivered SIGBUS or
1187 * just gets major/minor fault counters bumped up.
1189 * @VM_FAULT_OOM: Out Of Memory
1190 * @VM_FAULT_SIGBUS: Bad access
1191 * @VM_FAULT_MAJOR: Page read from storage
1192 * @VM_FAULT_HWPOISON: Hit poisoned small page
1193 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded
1195 * @VM_FAULT_SIGSEGV: segmentation fault
1196 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page
1197 * @VM_FAULT_LOCKED: ->fault locked the returned page
1198 * @VM_FAULT_RETRY: ->fault blocked, must retry
1199 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small
1200 * @VM_FAULT_DONE_COW: ->fault has fully handled COW
1201 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs
1202 * fsync() to complete (for synchronous page faults
1204 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released
1205 * @VM_FAULT_HINDEX_MASK: mask HINDEX value
1208 enum vm_fault_reason {
1209 VM_FAULT_OOM = (__force vm_fault_t)0x000001,
1210 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002,
1211 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004,
1212 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010,
1213 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
1214 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040,
1215 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100,
1216 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200,
1217 VM_FAULT_RETRY = (__force vm_fault_t)0x000400,
1218 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800,
1219 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000,
1220 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000,
1221 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000,
1222 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000,
1225 /* Encode hstate index for a hwpoisoned large page */
1226 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
1227 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
1229 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \
1230 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \
1231 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
1233 #define VM_FAULT_RESULT_TRACE \
1234 { VM_FAULT_OOM, "OOM" }, \
1235 { VM_FAULT_SIGBUS, "SIGBUS" }, \
1236 { VM_FAULT_MAJOR, "MAJOR" }, \
1237 { VM_FAULT_HWPOISON, "HWPOISON" }, \
1238 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \
1239 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \
1240 { VM_FAULT_NOPAGE, "NOPAGE" }, \
1241 { VM_FAULT_LOCKED, "LOCKED" }, \
1242 { VM_FAULT_RETRY, "RETRY" }, \
1243 { VM_FAULT_FALLBACK, "FALLBACK" }, \
1244 { VM_FAULT_DONE_COW, "DONE_COW" }, \
1245 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \
1246 { VM_FAULT_COMPLETED, "COMPLETED" }
1248 struct vm_special_mapping {
1249 const char *name; /* The name, e.g. "[vdso]". */
1252 * If .fault is not provided, this points to a
1253 * NULL-terminated array of pages that back the special mapping.
1255 * This must not be NULL unless .fault is provided.
1257 struct page **pages;
1260 * If non-NULL, then this is called to resolve page faults
1261 * on the special mapping. If used, .pages is not checked.
1263 vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1264 struct vm_area_struct *vma,
1265 struct vm_fault *vmf);
1267 int (*mremap)(const struct vm_special_mapping *sm,
1268 struct vm_area_struct *new_vma);
1271 enum tlb_flush_reason {
1272 TLB_FLUSH_ON_TASK_SWITCH,
1273 TLB_REMOTE_SHOOTDOWN,
1274 TLB_LOCAL_SHOOTDOWN,
1275 TLB_LOCAL_MM_SHOOTDOWN,
1276 TLB_REMOTE_SEND_IPI,
1277 NR_TLB_FLUSH_REASONS,
1281 * enum fault_flag - Fault flag definitions.
1282 * @FAULT_FLAG_WRITE: Fault was a write fault.
1283 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
1284 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
1285 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
1286 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
1287 * @FAULT_FLAG_TRIED: The fault has been tried once.
1288 * @FAULT_FLAG_USER: The fault originated in userspace.
1289 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
1290 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
1291 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
1292 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
1293 * COW mapping, making sure that an exclusive anon page is
1294 * mapped after the fault.
1295 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
1296 * We should only access orig_pte if this flag set.
1297 * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock.
1299 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
1300 * whether we would allow page faults to retry by specifying these two
1301 * fault flags correctly. Currently there can be three legal combinations:
1303 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and
1304 * this is the first try
1306 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and
1307 * we've already tried at least once
1309 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
1311 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
1312 * be used. Note that page faults can be allowed to retry for multiple times,
1313 * in which case we'll have an initial fault with flags (a) then later on
1314 * continuous faults with flags (b). We should always try to detect pending
1315 * signals before a retry to make sure the continuous page faults can still be
1316 * interrupted if necessary.
1318 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
1319 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
1320 * applied to mappings that are not COW mappings.
1323 FAULT_FLAG_WRITE = 1 << 0,
1324 FAULT_FLAG_MKWRITE = 1 << 1,
1325 FAULT_FLAG_ALLOW_RETRY = 1 << 2,
1326 FAULT_FLAG_RETRY_NOWAIT = 1 << 3,
1327 FAULT_FLAG_KILLABLE = 1 << 4,
1328 FAULT_FLAG_TRIED = 1 << 5,
1329 FAULT_FLAG_USER = 1 << 6,
1330 FAULT_FLAG_REMOTE = 1 << 7,
1331 FAULT_FLAG_INSTRUCTION = 1 << 8,
1332 FAULT_FLAG_INTERRUPTIBLE = 1 << 9,
1333 FAULT_FLAG_UNSHARE = 1 << 10,
1334 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11,
1335 FAULT_FLAG_VMA_LOCK = 1 << 12,
1338 typedef unsigned int __bitwise zap_flags_t;
1341 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
1342 * other. Here is what they mean, and how to use them:
1345 * FIXME: For pages which are part of a filesystem, mappings are subject to the
1346 * lifetime enforced by the filesystem and we need guarantees that longterm
1347 * users like RDMA and V4L2 only establish mappings which coordinate usage with
1348 * the filesystem. Ideas for this coordination include revoking the longterm
1349 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was
1350 * added after the problem with filesystems was found FS DAX VMAs are
1351 * specifically failed. Filesystem pages are still subject to bugs and use of
1352 * FOLL_LONGTERM should be avoided on those pages.
1354 * In the CMA case: long term pins in a CMA region would unnecessarily fragment
1355 * that region. And so, CMA attempts to migrate the page before pinning, when
1356 * FOLL_LONGTERM is specified.
1358 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
1359 * but an additional pin counting system) will be invoked. This is intended for
1360 * anything that gets a page reference and then touches page data (for example,
1361 * Direct IO). This lets the filesystem know that some non-file-system entity is
1362 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
1363 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
1364 * a call to unpin_user_page().
1366 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
1367 * and separate refcounting mechanisms, however, and that means that each has
1368 * its own acquire and release mechanisms:
1370 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
1372 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
1374 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
1375 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
1376 * calls applied to them, and that's perfectly OK. This is a constraint on the
1377 * callers, not on the pages.)
1379 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
1380 * directly by the caller. That's in order to help avoid mismatches when
1381 * releasing pages: get_user_pages*() pages must be released via put_page(),
1382 * while pin_user_pages*() pages must be released via unpin_user_page().
1384 * Please see Documentation/core-api/pin_user_pages.rst for more information.
1388 /* check pte is writable */
1389 FOLL_WRITE = 1 << 0,
1390 /* do get_page on page */
1392 /* give error on hole if it would be zero */
1394 /* get_user_pages read/write w/o permission */
1395 FOLL_FORCE = 1 << 3,
1397 * if a disk transfer is needed, start the IO and return without waiting
1400 FOLL_NOWAIT = 1 << 4,
1401 /* do not fault in pages */
1402 FOLL_NOFAULT = 1 << 5,
1403 /* check page is hwpoisoned */
1404 FOLL_HWPOISON = 1 << 6,
1405 /* don't do file mappings */
1408 * FOLL_LONGTERM indicates that the page will be held for an indefinite
1409 * time period _often_ under userspace control. This is in contrast to
1410 * iov_iter_get_pages(), whose usages are transient.
1412 FOLL_LONGTERM = 1 << 8,
1413 /* split huge pmd before returning */
1414 FOLL_SPLIT_PMD = 1 << 9,
1415 /* allow returning PCI P2PDMA pages */
1416 FOLL_PCI_P2PDMA = 1 << 10,
1417 /* allow interrupts from generic signals */
1418 FOLL_INTERRUPTIBLE = 1 << 11,
1420 * Always honor (trigger) NUMA hinting faults.
1422 * FOLL_WRITE implicitly honors NUMA hinting faults because a
1423 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE
1424 * apply). get_user_pages_fast_only() always implicitly honors NUMA
1427 FOLL_HONOR_NUMA_FAULT = 1 << 12,
1429 /* See also internal only FOLL flags in mm/internal.h */
1432 #endif /* _LINUX_MM_TYPES_H */