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;
130 * dma_addr_upper: might require a 64-bit
131 * value on 32-bit architectures.
133 unsigned long dma_addr_upper;
135 * For frag page support, not supported in
136 * 32-bit architectures with 64-bit DMA.
138 atomic_long_t pp_frag_count;
141 struct { /* Tail pages of compound page */
142 unsigned long compound_head; /* Bit zero is set */
144 struct { /* Page table pages */
145 unsigned long _pt_pad_1; /* compound_head */
146 pgtable_t pmd_huge_pte; /* protected by page->ptl */
147 unsigned long _pt_pad_2; /* mapping */
149 struct mm_struct *pt_mm; /* x86 pgds only */
150 atomic_t pt_frag_refcount; /* powerpc */
152 #if ALLOC_SPLIT_PTLOCKS
158 struct { /* ZONE_DEVICE pages */
159 /** @pgmap: Points to the hosting device page map. */
160 struct dev_pagemap *pgmap;
161 void *zone_device_data;
163 * ZONE_DEVICE private pages are counted as being
164 * mapped so the next 3 words hold the mapping, index,
165 * and private fields from the source anonymous or
166 * page cache page while the page is migrated to device
168 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
169 * use the mapping, index, and private fields when
170 * pmem backed DAX files are mapped.
174 /** @rcu_head: You can use this to free a page by RCU. */
175 struct rcu_head rcu_head;
178 union { /* This union is 4 bytes in size. */
180 * If the page can be mapped to userspace, encodes the number
181 * of times this page is referenced by a page table.
186 * If the page is neither PageSlab nor mappable to userspace,
187 * the value stored here may help determine what this page
188 * is used for. See page-flags.h for a list of page types
189 * which are currently stored here.
191 unsigned int page_type;
194 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
198 unsigned long memcg_data;
202 * On machines where all RAM is mapped into kernel address space,
203 * we can simply calculate the virtual address. On machines with
204 * highmem some memory is mapped into kernel virtual memory
205 * dynamically, so we need a place to store that address.
206 * Note that this field could be 16 bits on x86 ... ;)
208 * Architectures with slow multiplication can define
209 * WANT_PAGE_VIRTUAL in asm/page.h
211 #if defined(WANT_PAGE_VIRTUAL)
212 void *virtual; /* Kernel virtual address (NULL if
213 not kmapped, ie. highmem) */
214 #endif /* WANT_PAGE_VIRTUAL */
218 * KMSAN metadata for this page:
219 * - shadow page: every bit indicates whether the corresponding
220 * bit of the original page is initialized (0) or not (1);
221 * - origin page: every 4 bytes contain an id of the stack trace
222 * where the uninitialized value was created.
224 struct page *kmsan_shadow;
225 struct page *kmsan_origin;
228 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
231 } _struct_page_alignment;
234 * struct encoded_page - a nonexistent type marking this pointer
236 * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
237 * with the low bits of the pointer indicating extra context-dependent
238 * information. Not super-common, but happens in mmu_gather and mlock
239 * handling, and this acts as a type system check on that use.
241 * We only really have two guaranteed bits in general, although you could
242 * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
245 * Use the supplied helper functions to endcode/decode the pointer and bits.
248 #define ENCODE_PAGE_BITS 3ul
249 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
251 BUILD_BUG_ON(flags > ENCODE_PAGE_BITS);
252 return (struct encoded_page *)(flags | (unsigned long)page);
255 static inline unsigned long encoded_page_flags(struct encoded_page *page)
257 return ENCODE_PAGE_BITS & (unsigned long)page;
260 static inline struct page *encoded_page_ptr(struct encoded_page *page)
262 return (struct page *)(~ENCODE_PAGE_BITS & (unsigned long)page);
266 * struct folio - Represents a contiguous set of bytes.
267 * @flags: Identical to the page flags.
268 * @lru: Least Recently Used list; tracks how recently this folio was used.
269 * @mlock_count: Number of times this folio has been pinned by mlock().
270 * @mapping: The file this page belongs to, or refers to the anon_vma for
272 * @index: Offset within the file, in units of pages. For anonymous memory,
273 * this is the index from the beginning of the mmap.
274 * @private: Filesystem per-folio data (see folio_attach_private()).
275 * Used for swp_entry_t if folio_test_swapcache().
276 * @_mapcount: Do not access this member directly. Use folio_mapcount() to
277 * find out how many times this folio is mapped by userspace.
278 * @_refcount: Do not access this member directly. Use folio_ref_count()
279 * to find how many references there are to this folio.
280 * @memcg_data: Memory Control Group data.
281 * @_folio_dtor: Which destructor to use for this folio.
282 * @_folio_order: Do not use directly, call folio_order().
283 * @_entire_mapcount: Do not use directly, call folio_entire_mapcount().
284 * @_nr_pages_mapped: Do not use directly, call folio_mapcount().
285 * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
286 * @_folio_nr_pages: Do not use directly, call folio_nr_pages().
287 * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
288 * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
289 * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
290 * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
291 * @_deferred_list: Folios to be split under memory pressure.
293 * A folio is a physically, virtually and logically contiguous set
294 * of bytes. It is a power-of-two in size, and it is aligned to that
295 * same power-of-two. It is at least as large as %PAGE_SIZE. If it is
296 * in the page cache, it is at a file offset which is a multiple of that
297 * power-of-two. It may be mapped into userspace at an address which is
298 * at an arbitrary page offset, but its kernel virtual address is aligned
302 /* private: don't document the anon union */
308 struct list_head lru;
309 /* private: avoid cluttering the output */
313 unsigned int mlock_count;
318 struct address_space *mapping;
324 unsigned long memcg_data;
326 /* private: the union with struct page is transitional */
332 unsigned long _flags_1;
333 unsigned long _head_1;
335 unsigned char _folio_dtor;
336 unsigned char _folio_order;
337 atomic_t _entire_mapcount;
338 atomic_t _nr_pages_mapped;
341 unsigned int _folio_nr_pages;
343 /* private: the union with struct page is transitional */
345 struct page __page_1;
349 unsigned long _flags_2;
350 unsigned long _head_2;
352 void *_hugetlb_subpool;
353 void *_hugetlb_cgroup;
354 void *_hugetlb_cgroup_rsvd;
355 void *_hugetlb_hwpoison;
356 /* private: the union with struct page is transitional */
359 unsigned long _flags_2a;
360 unsigned long _head_2a;
362 struct list_head _deferred_list;
363 /* private: the union with struct page is transitional */
365 struct page __page_2;
369 #define FOLIO_MATCH(pg, fl) \
370 static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
371 FOLIO_MATCH(flags, flags);
372 FOLIO_MATCH(lru, lru);
373 FOLIO_MATCH(mapping, mapping);
374 FOLIO_MATCH(compound_head, lru);
375 FOLIO_MATCH(index, index);
376 FOLIO_MATCH(private, private);
377 FOLIO_MATCH(_mapcount, _mapcount);
378 FOLIO_MATCH(_refcount, _refcount);
380 FOLIO_MATCH(memcg_data, memcg_data);
383 #define FOLIO_MATCH(pg, fl) \
384 static_assert(offsetof(struct folio, fl) == \
385 offsetof(struct page, pg) + sizeof(struct page))
386 FOLIO_MATCH(flags, _flags_1);
387 FOLIO_MATCH(compound_head, _head_1);
389 #define FOLIO_MATCH(pg, fl) \
390 static_assert(offsetof(struct folio, fl) == \
391 offsetof(struct page, pg) + 2 * sizeof(struct page))
392 FOLIO_MATCH(flags, _flags_2);
393 FOLIO_MATCH(compound_head, _head_2);
397 * Used for sizing the vmemmap region on some architectures
399 #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page)))
401 #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK)
402 #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE)
405 * page_private can be used on tail pages. However, PagePrivate is only
406 * checked by the VM on the head page. So page_private on the tail pages
407 * should be used for data that's ancillary to the head page (eg attaching
408 * buffer heads to tail pages after attaching buffer heads to the head page)
410 #define page_private(page) ((page)->private)
412 static inline void set_page_private(struct page *page, unsigned long private)
414 page->private = private;
417 static inline void *folio_get_private(struct folio *folio)
419 return folio->private;
422 struct page_frag_cache {
424 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
430 /* we maintain a pagecount bias, so that we dont dirty cache line
431 * containing page->_refcount every time we allocate a fragment.
433 unsigned int pagecnt_bias;
437 typedef unsigned long vm_flags_t;
440 * A region containing a mapping of a non-memory backed file under NOMMU
441 * conditions. These are held in a global tree and are pinned by the VMAs that
445 struct rb_node vm_rb; /* link in global region tree */
446 vm_flags_t vm_flags; /* VMA vm_flags */
447 unsigned long vm_start; /* start address of region */
448 unsigned long vm_end; /* region initialised to here */
449 unsigned long vm_top; /* region allocated to here */
450 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
451 struct file *vm_file; /* the backing file or NULL */
453 int vm_usage; /* region usage count (access under nommu_region_sem) */
454 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
458 #ifdef CONFIG_USERFAULTFD
459 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
460 struct vm_userfaultfd_ctx {
461 struct userfaultfd_ctx *ctx;
463 #else /* CONFIG_USERFAULTFD */
464 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
465 struct vm_userfaultfd_ctx {};
466 #endif /* CONFIG_USERFAULTFD */
468 struct anon_vma_name {
470 /* The name needs to be at the end because it is dynamically sized. */
475 * This struct describes a virtual memory area. There is one of these
476 * per VM-area/task. A VM area is any part of the process virtual memory
477 * space that has a special rule for the page-fault handlers (ie a shared
478 * library, the executable area etc).
480 struct vm_area_struct {
481 /* The first cache line has the info for VMA tree walking. */
483 unsigned long vm_start; /* Our start address within vm_mm. */
484 unsigned long vm_end; /* The first byte after our end address
487 struct mm_struct *vm_mm; /* The address space we belong to. */
490 * Access permissions of this VMA.
491 * See vmf_insert_mixed_prot() for discussion.
493 pgprot_t vm_page_prot;
497 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
500 const vm_flags_t vm_flags;
501 vm_flags_t __private __vm_flags;
505 * For areas with an address space and backing store,
506 * linkage into the address_space->i_mmap interval tree.
511 unsigned long rb_subtree_last;
515 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
516 * list, after a COW of one of the file pages. A MAP_SHARED vma
517 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
518 * or brk vma (with NULL file) can only be in an anon_vma list.
520 struct list_head anon_vma_chain; /* Serialized by mmap_lock &
522 struct anon_vma *anon_vma; /* Serialized by page_table_lock */
524 /* Function pointers to deal with this struct. */
525 const struct vm_operations_struct *vm_ops;
527 /* Information about our backing store: */
528 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
530 struct file * vm_file; /* File we map to (can be NULL). */
531 void * vm_private_data; /* was vm_pte (shared mem) */
533 #ifdef CONFIG_ANON_VMA_NAME
535 * For private and shared anonymous mappings, a pointer to a null
536 * terminated string containing the name given to the vma, or NULL if
537 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
539 struct anon_vma_name *anon_name;
542 atomic_long_t swap_readahead_info;
545 struct vm_region *vm_region; /* NOMMU mapping region */
548 struct mempolicy *vm_policy; /* NUMA policy for the VMA */
550 struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
551 } __randomize_layout;
553 #ifdef CONFIG_SCHED_MM_CID
563 struct maple_tree mm_mt;
565 unsigned long (*get_unmapped_area) (struct file *filp,
566 unsigned long addr, unsigned long len,
567 unsigned long pgoff, unsigned long flags);
569 unsigned long mmap_base; /* base of mmap area */
570 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
571 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
572 /* Base addresses for compatible mmap() */
573 unsigned long mmap_compat_base;
574 unsigned long mmap_compat_legacy_base;
576 unsigned long task_size; /* size of task vm space */
579 #ifdef CONFIG_MEMBARRIER
581 * @membarrier_state: Flags controlling membarrier behavior.
583 * This field is close to @pgd to hopefully fit in the same
584 * cache-line, which needs to be touched by switch_mm().
586 atomic_t membarrier_state;
590 * @mm_users: The number of users including userspace.
592 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
593 * drops to 0 (i.e. when the task exits and there are no other
594 * temporary reference holders), we also release a reference on
595 * @mm_count (which may then free the &struct mm_struct if
596 * @mm_count also drops to 0).
601 * @mm_count: The number of references to &struct mm_struct
602 * (@mm_users count as 1).
604 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the
605 * &struct mm_struct is freed.
608 #ifdef CONFIG_SCHED_MM_CID
610 * @pcpu_cid: Per-cpu current cid.
612 * Keep track of the currently allocated mm_cid for each cpu.
613 * The per-cpu mm_cid values are serialized by their respective
616 struct mm_cid __percpu *pcpu_cid;
618 * @mm_cid_next_scan: Next mm_cid scan (in jiffies).
620 * When the next mm_cid scan is due (in jiffies).
622 unsigned long mm_cid_next_scan;
625 atomic_long_t pgtables_bytes; /* size of all page tables */
627 int map_count; /* number of VMAs */
629 spinlock_t page_table_lock; /* Protects page tables and some
633 * With some kernel config, the current mmap_lock's offset
634 * inside 'mm_struct' is at 0x120, which is very optimal, as
635 * its two hot fields 'count' and 'owner' sit in 2 different
636 * cachelines, and when mmap_lock is highly contended, both
637 * of the 2 fields will be accessed frequently, current layout
638 * will help to reduce cache bouncing.
640 * So please be careful with adding new fields before
641 * mmap_lock, which can easily push the 2 fields into one
644 struct rw_semaphore mmap_lock;
646 struct list_head mmlist; /* List of maybe swapped mm's. These
647 * are globally strung together off
648 * init_mm.mmlist, and are protected
653 unsigned long hiwater_rss; /* High-watermark of RSS usage */
654 unsigned long hiwater_vm; /* High-water virtual memory usage */
656 unsigned long total_vm; /* Total pages mapped */
657 unsigned long locked_vm; /* Pages that have PG_mlocked set */
658 atomic64_t pinned_vm; /* Refcount permanently increased */
659 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
660 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
661 unsigned long stack_vm; /* VM_STACK */
662 unsigned long def_flags;
665 * @write_protect_seq: Locked when any thread is write
666 * protecting pages mapped by this mm to enforce a later COW,
667 * for instance during page table copying for fork().
669 seqcount_t write_protect_seq;
671 spinlock_t arg_lock; /* protect the below fields */
673 unsigned long start_code, end_code, start_data, end_data;
674 unsigned long start_brk, brk, start_stack;
675 unsigned long arg_start, arg_end, env_start, env_end;
677 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
679 struct percpu_counter rss_stat[NR_MM_COUNTERS];
681 struct linux_binfmt *binfmt;
683 /* Architecture-specific MM context */
684 mm_context_t context;
686 unsigned long flags; /* Must use atomic bitops to access */
689 spinlock_t ioctx_lock;
690 struct kioctx_table __rcu *ioctx_table;
694 * "owner" points to a task that is regarded as the canonical
695 * user/owner of this mm. All of the following must be true in
696 * order for it to be changed:
698 * current == mm->owner
700 * new_owner->mm == mm
701 * new_owner->alloc_lock is held
703 struct task_struct __rcu *owner;
705 struct user_namespace *user_ns;
707 /* store ref to file /proc/<pid>/exe symlink points to */
708 struct file __rcu *exe_file;
709 #ifdef CONFIG_MMU_NOTIFIER
710 struct mmu_notifier_subscriptions *notifier_subscriptions;
712 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
713 pgtable_t pmd_huge_pte; /* protected by page_table_lock */
715 #ifdef CONFIG_NUMA_BALANCING
717 * numa_next_scan is the next time that PTEs will be remapped
718 * PROT_NONE to trigger NUMA hinting faults; such faults gather
719 * statistics and migrate pages to new nodes if necessary.
721 unsigned long numa_next_scan;
723 /* Restart point for scanning and remapping PTEs. */
724 unsigned long numa_scan_offset;
726 /* numa_scan_seq prevents two threads remapping PTEs. */
730 * An operation with batched TLB flushing is going on. Anything
731 * that can move process memory needs to flush the TLB when
732 * moving a PROT_NONE mapped page.
734 atomic_t tlb_flush_pending;
735 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
736 /* See flush_tlb_batched_pending() */
737 atomic_t tlb_flush_batched;
739 struct uprobes_state uprobes_state;
740 #ifdef CONFIG_PREEMPT_RT
741 struct rcu_head delayed_drop;
743 #ifdef CONFIG_HUGETLB_PAGE
744 atomic_long_t hugetlb_usage;
746 struct work_struct async_put_work;
748 #ifdef CONFIG_IOMMU_SVA
753 * Represent how many pages of this process are involved in KSM
756 unsigned long ksm_merging_pages;
758 * Represent how many pages are checked for ksm merging
759 * including merged and not merged.
761 unsigned long ksm_rmap_items;
763 #ifdef CONFIG_LRU_GEN
765 /* this mm_struct is on lru_gen_mm_list */
766 struct list_head list;
768 * Set when switching to this mm_struct, as a hint of
769 * whether it has been used since the last time per-node
770 * page table walkers cleared the corresponding bits.
772 unsigned long bitmap;
774 /* points to the memcg of "owner" above */
775 struct mem_cgroup *memcg;
778 #endif /* CONFIG_LRU_GEN */
779 } __randomize_layout;
782 * The mm_cpumask needs to be at the end of mm_struct, because it
783 * is dynamically sized based on nr_cpu_ids.
785 unsigned long cpu_bitmap[];
788 #define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
790 extern struct mm_struct init_mm;
792 /* Pointer magic because the dynamic array size confuses some compilers. */
793 static inline void mm_init_cpumask(struct mm_struct *mm)
795 unsigned long cpu_bitmap = (unsigned long)mm;
797 cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
798 cpumask_clear((struct cpumask *)cpu_bitmap);
801 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
802 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
804 return (struct cpumask *)&mm->cpu_bitmap;
807 #ifdef CONFIG_LRU_GEN
809 struct lru_gen_mm_list {
810 /* mm_struct list for page table walkers */
811 struct list_head fifo;
812 /* protects the list above */
816 void lru_gen_add_mm(struct mm_struct *mm);
817 void lru_gen_del_mm(struct mm_struct *mm);
819 void lru_gen_migrate_mm(struct mm_struct *mm);
822 static inline void lru_gen_init_mm(struct mm_struct *mm)
824 INIT_LIST_HEAD(&mm->lru_gen.list);
825 mm->lru_gen.bitmap = 0;
827 mm->lru_gen.memcg = NULL;
831 static inline void lru_gen_use_mm(struct mm_struct *mm)
834 * When the bitmap is set, page reclaim knows this mm_struct has been
835 * used since the last time it cleared the bitmap. So it might be worth
836 * walking the page tables of this mm_struct to clear the accessed bit.
838 WRITE_ONCE(mm->lru_gen.bitmap, -1);
841 #else /* !CONFIG_LRU_GEN */
843 static inline void lru_gen_add_mm(struct mm_struct *mm)
847 static inline void lru_gen_del_mm(struct mm_struct *mm)
852 static inline void lru_gen_migrate_mm(struct mm_struct *mm)
857 static inline void lru_gen_init_mm(struct mm_struct *mm)
861 static inline void lru_gen_use_mm(struct mm_struct *mm)
865 #endif /* CONFIG_LRU_GEN */
867 struct vma_iterator {
871 #define VMA_ITERATOR(name, __mm, __addr) \
872 struct vma_iterator name = { \
874 .tree = &(__mm)->mm_mt, \
880 static inline void vma_iter_init(struct vma_iterator *vmi,
881 struct mm_struct *mm, unsigned long addr)
883 mas_init(&vmi->mas, &mm->mm_mt, addr);
886 #ifdef CONFIG_SCHED_MM_CID
889 MM_CID_UNSET = -1U, /* Unset state has lazy_put flag set. */
890 MM_CID_LAZY_PUT = (1U << 31),
893 static inline bool mm_cid_is_unset(int cid)
895 return cid == MM_CID_UNSET;
898 static inline bool mm_cid_is_lazy_put(int cid)
900 return !mm_cid_is_unset(cid) && (cid & MM_CID_LAZY_PUT);
903 static inline bool mm_cid_is_valid(int cid)
905 return !(cid & MM_CID_LAZY_PUT);
908 static inline int mm_cid_set_lazy_put(int cid)
910 return cid | MM_CID_LAZY_PUT;
913 static inline int mm_cid_clear_lazy_put(int cid)
915 return cid & ~MM_CID_LAZY_PUT;
918 /* Accessor for struct mm_struct's cidmask. */
919 static inline cpumask_t *mm_cidmask(struct mm_struct *mm)
921 unsigned long cid_bitmap = (unsigned long)mm;
923 cid_bitmap += offsetof(struct mm_struct, cpu_bitmap);
924 /* Skip cpu_bitmap */
925 cid_bitmap += cpumask_size();
926 return (struct cpumask *)cid_bitmap;
929 static inline void mm_init_cid(struct mm_struct *mm)
933 for_each_possible_cpu(i) {
934 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, i);
936 pcpu_cid->cid = MM_CID_UNSET;
939 cpumask_clear(mm_cidmask(mm));
942 static inline int mm_alloc_cid(struct mm_struct *mm)
944 mm->pcpu_cid = alloc_percpu(struct mm_cid);
951 static inline void mm_destroy_cid(struct mm_struct *mm)
953 free_percpu(mm->pcpu_cid);
957 static inline unsigned int mm_cid_size(void)
959 return cpumask_size();
961 #else /* CONFIG_SCHED_MM_CID */
962 static inline void mm_init_cid(struct mm_struct *mm) { }
963 static inline int mm_alloc_cid(struct mm_struct *mm) { return 0; }
964 static inline void mm_destroy_cid(struct mm_struct *mm) { }
965 static inline unsigned int mm_cid_size(void)
969 #endif /* CONFIG_SCHED_MM_CID */
972 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
973 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
974 extern void tlb_finish_mmu(struct mmu_gather *tlb);
979 * typedef vm_fault_t - Return type for page fault handlers.
981 * Page fault handlers return a bitmask of %VM_FAULT values.
983 typedef __bitwise unsigned int vm_fault_t;
986 * enum vm_fault_reason - Page fault handlers return a bitmask of
987 * these values to tell the core VM what happened when handling the
988 * fault. Used to decide whether a process gets delivered SIGBUS or
989 * just gets major/minor fault counters bumped up.
991 * @VM_FAULT_OOM: Out Of Memory
992 * @VM_FAULT_SIGBUS: Bad access
993 * @VM_FAULT_MAJOR: Page read from storage
994 * @VM_FAULT_HWPOISON: Hit poisoned small page
995 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded
997 * @VM_FAULT_SIGSEGV: segmentation fault
998 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page
999 * @VM_FAULT_LOCKED: ->fault locked the returned page
1000 * @VM_FAULT_RETRY: ->fault blocked, must retry
1001 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small
1002 * @VM_FAULT_DONE_COW: ->fault has fully handled COW
1003 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs
1004 * fsync() to complete (for synchronous page faults
1006 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released
1007 * @VM_FAULT_HINDEX_MASK: mask HINDEX value
1010 enum vm_fault_reason {
1011 VM_FAULT_OOM = (__force vm_fault_t)0x000001,
1012 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002,
1013 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004,
1014 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010,
1015 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
1016 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040,
1017 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100,
1018 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200,
1019 VM_FAULT_RETRY = (__force vm_fault_t)0x000400,
1020 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800,
1021 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000,
1022 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000,
1023 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000,
1024 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000,
1027 /* Encode hstate index for a hwpoisoned large page */
1028 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
1029 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
1031 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \
1032 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \
1033 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
1035 #define VM_FAULT_RESULT_TRACE \
1036 { VM_FAULT_OOM, "OOM" }, \
1037 { VM_FAULT_SIGBUS, "SIGBUS" }, \
1038 { VM_FAULT_MAJOR, "MAJOR" }, \
1039 { VM_FAULT_HWPOISON, "HWPOISON" }, \
1040 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \
1041 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \
1042 { VM_FAULT_NOPAGE, "NOPAGE" }, \
1043 { VM_FAULT_LOCKED, "LOCKED" }, \
1044 { VM_FAULT_RETRY, "RETRY" }, \
1045 { VM_FAULT_FALLBACK, "FALLBACK" }, \
1046 { VM_FAULT_DONE_COW, "DONE_COW" }, \
1047 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }
1049 struct vm_special_mapping {
1050 const char *name; /* The name, e.g. "[vdso]". */
1053 * If .fault is not provided, this points to a
1054 * NULL-terminated array of pages that back the special mapping.
1056 * This must not be NULL unless .fault is provided.
1058 struct page **pages;
1061 * If non-NULL, then this is called to resolve page faults
1062 * on the special mapping. If used, .pages is not checked.
1064 vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1065 struct vm_area_struct *vma,
1066 struct vm_fault *vmf);
1068 int (*mremap)(const struct vm_special_mapping *sm,
1069 struct vm_area_struct *new_vma);
1072 enum tlb_flush_reason {
1073 TLB_FLUSH_ON_TASK_SWITCH,
1074 TLB_REMOTE_SHOOTDOWN,
1075 TLB_LOCAL_SHOOTDOWN,
1076 TLB_LOCAL_MM_SHOOTDOWN,
1077 TLB_REMOTE_SEND_IPI,
1078 NR_TLB_FLUSH_REASONS,
1082 * A swap entry has to fit into a "unsigned long", as the entry is hidden
1083 * in the "index" field of the swapper address space.
1090 * enum fault_flag - Fault flag definitions.
1091 * @FAULT_FLAG_WRITE: Fault was a write fault.
1092 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
1093 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
1094 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
1095 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
1096 * @FAULT_FLAG_TRIED: The fault has been tried once.
1097 * @FAULT_FLAG_USER: The fault originated in userspace.
1098 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
1099 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
1100 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
1101 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
1102 * COW mapping, making sure that an exclusive anon page is
1103 * mapped after the fault.
1104 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
1105 * We should only access orig_pte if this flag set.
1107 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
1108 * whether we would allow page faults to retry by specifying these two
1109 * fault flags correctly. Currently there can be three legal combinations:
1111 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and
1112 * this is the first try
1114 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and
1115 * we've already tried at least once
1117 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
1119 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
1120 * be used. Note that page faults can be allowed to retry for multiple times,
1121 * in which case we'll have an initial fault with flags (a) then later on
1122 * continuous faults with flags (b). We should always try to detect pending
1123 * signals before a retry to make sure the continuous page faults can still be
1124 * interrupted if necessary.
1126 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
1127 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
1128 * applied to mappings that are not COW mappings.
1131 FAULT_FLAG_WRITE = 1 << 0,
1132 FAULT_FLAG_MKWRITE = 1 << 1,
1133 FAULT_FLAG_ALLOW_RETRY = 1 << 2,
1134 FAULT_FLAG_RETRY_NOWAIT = 1 << 3,
1135 FAULT_FLAG_KILLABLE = 1 << 4,
1136 FAULT_FLAG_TRIED = 1 << 5,
1137 FAULT_FLAG_USER = 1 << 6,
1138 FAULT_FLAG_REMOTE = 1 << 7,
1139 FAULT_FLAG_INSTRUCTION = 1 << 8,
1140 FAULT_FLAG_INTERRUPTIBLE = 1 << 9,
1141 FAULT_FLAG_UNSHARE = 1 << 10,
1142 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11,
1145 typedef unsigned int __bitwise zap_flags_t;
1148 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
1149 * other. Here is what they mean, and how to use them:
1152 * FIXME: For pages which are part of a filesystem, mappings are subject to the
1153 * lifetime enforced by the filesystem and we need guarantees that longterm
1154 * users like RDMA and V4L2 only establish mappings which coordinate usage with
1155 * the filesystem. Ideas for this coordination include revoking the longterm
1156 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was
1157 * added after the problem with filesystems was found FS DAX VMAs are
1158 * specifically failed. Filesystem pages are still subject to bugs and use of
1159 * FOLL_LONGTERM should be avoided on those pages.
1161 * In the CMA case: long term pins in a CMA region would unnecessarily fragment
1162 * that region. And so, CMA attempts to migrate the page before pinning, when
1163 * FOLL_LONGTERM is specified.
1165 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
1166 * but an additional pin counting system) will be invoked. This is intended for
1167 * anything that gets a page reference and then touches page data (for example,
1168 * Direct IO). This lets the filesystem know that some non-file-system entity is
1169 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
1170 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
1171 * a call to unpin_user_page().
1173 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
1174 * and separate refcounting mechanisms, however, and that means that each has
1175 * its own acquire and release mechanisms:
1177 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
1179 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
1181 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
1182 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
1183 * calls applied to them, and that's perfectly OK. This is a constraint on the
1184 * callers, not on the pages.)
1186 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
1187 * directly by the caller. That's in order to help avoid mismatches when
1188 * releasing pages: get_user_pages*() pages must be released via put_page(),
1189 * while pin_user_pages*() pages must be released via unpin_user_page().
1191 * Please see Documentation/core-api/pin_user_pages.rst for more information.
1195 /* check pte is writable */
1196 FOLL_WRITE = 1 << 0,
1197 /* do get_page on page */
1199 /* give error on hole if it would be zero */
1201 /* get_user_pages read/write w/o permission */
1202 FOLL_FORCE = 1 << 3,
1204 * if a disk transfer is needed, start the IO and return without waiting
1207 FOLL_NOWAIT = 1 << 4,
1208 /* do not fault in pages */
1209 FOLL_NOFAULT = 1 << 5,
1210 /* check page is hwpoisoned */
1211 FOLL_HWPOISON = 1 << 6,
1212 /* don't do file mappings */
1215 * FOLL_LONGTERM indicates that the page will be held for an indefinite
1216 * time period _often_ under userspace control. This is in contrast to
1217 * iov_iter_get_pages(), whose usages are transient.
1219 FOLL_LONGTERM = 1 << 8,
1220 /* split huge pmd before returning */
1221 FOLL_SPLIT_PMD = 1 << 9,
1222 /* allow returning PCI P2PDMA pages */
1223 FOLL_PCI_P2PDMA = 1 << 10,
1224 /* allow interrupts from generic signals */
1225 FOLL_INTERRUPTIBLE = 1 << 11,
1227 /* See also internal only FOLL flags in mm/internal.h */
1230 #endif /* _LINUX_MM_TYPES_H */