xref: /openbmc/linux/include/linux/mm_types.h (revision 85a13334)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25b99cd0eSHeiko Carstens #ifndef _LINUX_MM_TYPES_H
35b99cd0eSHeiko Carstens #define _LINUX_MM_TYPES_H
45b99cd0eSHeiko Carstens 
52e58f173SIngo Molnar #include <linux/mm_types_task.h>
62e58f173SIngo Molnar 
74f9a58d7SOlaf Hering #include <linux/auxvec.h>
878db3412SSuren Baghdasaryan #include <linux/kref.h>
95b99cd0eSHeiko Carstens #include <linux/list.h>
105b99cd0eSHeiko Carstens #include <linux/spinlock.h>
11c92ff1bdSMartin Schwidefsky #include <linux/rbtree.h>
12d4af56c5SLiam R. Howlett #include <linux/maple_tree.h>
13c92ff1bdSMartin Schwidefsky #include <linux/rwsem.h>
14c92ff1bdSMartin Schwidefsky #include <linux/completion.h>
15cddb8a5cSAndrea Arcangeli #include <linux/cpumask.h>
16d4b3b638SSrikar Dronamraju #include <linux/uprobes.h>
178d491de6SThomas Gleixner #include <linux/rcupdate.h>
18bbeae5b0SPeter Zijlstra #include <linux/page-flags-layout.h>
19ec8d7c14SMichal Hocko #include <linux/workqueue.h>
2057efa1feSJason Gunthorpe #include <linux/seqlock.h>
21f1a79412SShakeel Butt #include <linux/percpu_counter.h>
222e58f173SIngo Molnar 
23c92ff1bdSMartin Schwidefsky #include <asm/mmu.h>
245b99cd0eSHeiko Carstens 
254f9a58d7SOlaf Hering #ifndef AT_VECTOR_SIZE_ARCH
264f9a58d7SOlaf Hering #define AT_VECTOR_SIZE_ARCH 0
274f9a58d7SOlaf Hering #endif
284f9a58d7SOlaf Hering #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
294f9a58d7SOlaf Hering 
3082e69a12SFenghua Yu #define INIT_PASID	0
311c8f4220SSouptick Joarder 
325b99cd0eSHeiko Carstens struct address_space;
331306a85aSJohannes Weiner struct mem_cgroup;
345b99cd0eSHeiko Carstens 
355b99cd0eSHeiko Carstens /*
365b99cd0eSHeiko Carstens  * Each physical page in the system has a struct page associated with
375b99cd0eSHeiko Carstens  * it to keep track of whatever it is we are using the page for at the
385b99cd0eSHeiko Carstens  * moment. Note that we have no way to track which tasks are using
395b99cd0eSHeiko Carstens  * a page, though if it is a pagecache page, rmap structures can tell us
4097b4a671SMatthew Wilcox  * who is mapping it.
41be50015dSMatthew Wilcox  *
4297b4a671SMatthew Wilcox  * If you allocate the page using alloc_pages(), you can use some of the
4397b4a671SMatthew Wilcox  * space in struct page for your own purposes.  The five words in the main
4497b4a671SMatthew Wilcox  * union are available, except for bit 0 of the first word which must be
4597b4a671SMatthew Wilcox  * kept clear.  Many users use this word to store a pointer to an object
4697b4a671SMatthew Wilcox  * which is guaranteed to be aligned.  If you use the same storage as
4797b4a671SMatthew Wilcox  * page->mapping, you must restore it to NULL before freeing the page.
4897b4a671SMatthew Wilcox  *
4997b4a671SMatthew Wilcox  * If your page will not be mapped to userspace, you can also use the four
5097b4a671SMatthew Wilcox  * bytes in the mapcount union, but you must call page_mapcount_reset()
5197b4a671SMatthew Wilcox  * before freeing it.
5297b4a671SMatthew Wilcox  *
5397b4a671SMatthew Wilcox  * If you want to use the refcount field, it must be used in such a way
5497b4a671SMatthew Wilcox  * that other CPUs temporarily incrementing and then decrementing the
55be50015dSMatthew Wilcox  * refcount does not cause problems.  On receiving the page from
56be50015dSMatthew Wilcox  * alloc_pages(), the refcount will be positive.
57be50015dSMatthew Wilcox  *
5897b4a671SMatthew Wilcox  * If you allocate pages of order > 0, you can use some of the fields
5997b4a671SMatthew Wilcox  * in each subpage, but you may need to restore some of their values
6097b4a671SMatthew Wilcox  * afterwards.
61fc9bb8c7SChristoph Lameter  *
62d122019bSMatthew Wilcox (Oracle)  * SLUB uses cmpxchg_double() to atomically update its freelist and counters.
63d122019bSMatthew Wilcox (Oracle)  * That requires that freelist & counters in struct slab be adjacent and
64d122019bSMatthew Wilcox (Oracle)  * double-word aligned. Because struct slab currently just reinterprets the
65d122019bSMatthew Wilcox (Oracle)  * bits of struct page, we align all struct pages to double-word boundaries,
66d122019bSMatthew Wilcox (Oracle)  * and ensure that 'freelist' is aligned within struct slab.
675b99cd0eSHeiko Carstens  */
68e20df2c6SMatthew Wilcox #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
69e20df2c6SMatthew Wilcox #define _struct_page_alignment	__aligned(2 * sizeof(unsigned long))
70e20df2c6SMatthew Wilcox #else
7170fb4fdfSLinus Torvalds #define _struct_page_alignment	__aligned(sizeof(unsigned long))
727d27a04bSMatthew Wilcox #endif
73e20df2c6SMatthew Wilcox 
745b99cd0eSHeiko Carstens struct page {
755b99cd0eSHeiko Carstens 	unsigned long flags;		/* Atomic flags, some possibly
765b99cd0eSHeiko Carstens 					 * updated asynchronously */
77b7ccc7f8SMatthew Wilcox 	/*
784da1984eSMatthew Wilcox 	 * Five words (20/40 bytes) are available in this union.
794da1984eSMatthew Wilcox 	 * WARNING: bit 0 of the first word is used for PageTail(). That
804da1984eSMatthew Wilcox 	 * means the other users of this union MUST NOT use the bit to
81b7ccc7f8SMatthew Wilcox 	 * avoid collision and false-positive PageTail().
82b7ccc7f8SMatthew Wilcox 	 */
83b7ccc7f8SMatthew Wilcox 	union {
844da1984eSMatthew Wilcox 		struct {	/* Page cache and anonymous pages */
854da1984eSMatthew Wilcox 			/**
864da1984eSMatthew Wilcox 			 * @lru: Pageout list, eg. active_list protected by
8715b44736SHugh Dickins 			 * lruvec->lru_lock.  Sometimes used as a generic list
88b7ccc7f8SMatthew Wilcox 			 * by the page owner.
89b7ccc7f8SMatthew Wilcox 			 */
9007ca7606SHugh Dickins 			union {
914da1984eSMatthew Wilcox 				struct list_head lru;
92bf75f200SMel Gorman 
9307ca7606SHugh Dickins 				/* Or, for the Unevictable "LRU list" slot */
9407ca7606SHugh Dickins 				struct {
9507ca7606SHugh Dickins 					/* Always even, to negate PageTail */
9607ca7606SHugh Dickins 					void *__filler;
9707ca7606SHugh Dickins 					/* Count page's or folio's mlocks */
9807ca7606SHugh Dickins 					unsigned int mlock_count;
9907ca7606SHugh Dickins 				};
100bf75f200SMel Gorman 
101bf75f200SMel Gorman 				/* Or, free page */
102bf75f200SMel Gorman 				struct list_head buddy_list;
103bf75f200SMel Gorman 				struct list_head pcp_list;
10407ca7606SHugh Dickins 			};
10566a6ffd2SMatthew Wilcox 			/* See page-flags.h for PAGE_MAPPING_FLAGS */
106b26435a0SMatthew Wilcox 			struct address_space *mapping;
10716900426SShiyang Ruan 			union {
108fc9bb8c7SChristoph Lameter 				pgoff_t index;		/* Our offset within mapping. */
10916900426SShiyang Ruan 				unsigned long share;	/* share count for fsdax */
11016900426SShiyang Ruan 			};
11166a6ffd2SMatthew Wilcox 			/**
11266a6ffd2SMatthew Wilcox 			 * @private: Mapping-private opaque data.
11366a6ffd2SMatthew Wilcox 			 * Usually used for buffer_heads if PagePrivate.
11466a6ffd2SMatthew Wilcox 			 * Used for swp_entry_t if PageSwapCache.
11566a6ffd2SMatthew Wilcox 			 * Indicates order in the buddy system if PageBuddy.
1167d27a04bSMatthew Wilcox 			 */
1177d27a04bSMatthew Wilcox 			unsigned long private;
11866a6ffd2SMatthew Wilcox 		};
119c25fff71SJesper Dangaard Brouer 		struct {	/* page_pool used by netstack */
120c25fff71SJesper Dangaard Brouer 			/**
121c07aea3eSMatteo Croce 			 * @pp_magic: magic value to avoid recycling non
122c07aea3eSMatteo Croce 			 * page_pool allocated pages.
123c07aea3eSMatteo Croce 			 */
124c07aea3eSMatteo Croce 			unsigned long pp_magic;
125c07aea3eSMatteo Croce 			struct page_pool *pp;
126c07aea3eSMatteo Croce 			unsigned long _pp_mapping_pad;
1270e9d2a0aSYunsheng Lin 			unsigned long dma_addr;
128f915b75bSYunsheng Lin 			union {
129f915b75bSYunsheng Lin 				/**
130f915b75bSYunsheng Lin 				 * dma_addr_upper: might require a 64-bit
131f915b75bSYunsheng Lin 				 * value on 32-bit architectures.
132f915b75bSYunsheng Lin 				 */
133f915b75bSYunsheng Lin 				unsigned long dma_addr_upper;
134f915b75bSYunsheng Lin 				/**
135f915b75bSYunsheng Lin 				 * For frag page support, not supported in
136f915b75bSYunsheng Lin 				 * 32-bit architectures with 64-bit DMA.
137f915b75bSYunsheng Lin 				 */
1380e9d2a0aSYunsheng Lin 				atomic_long_t pp_frag_count;
1390e9d2a0aSYunsheng Lin 			};
140f915b75bSYunsheng Lin 		};
1414da1984eSMatthew Wilcox 		struct {	/* Tail pages of compound page */
1424da1984eSMatthew Wilcox 			unsigned long compound_head;	/* Bit zero is set */
143dad6a5ebSHugh Dickins 		};
14450e7fbc3SMatthew Wilcox 		struct {	/* ZONE_DEVICE pages */
14550e7fbc3SMatthew Wilcox 			/** @pgmap: Points to the hosting device page map. */
14650e7fbc3SMatthew Wilcox 			struct dev_pagemap *pgmap;
1478a164fefSChristoph Hellwig 			void *zone_device_data;
14876470ccdSRalph Campbell 			/*
14976470ccdSRalph Campbell 			 * ZONE_DEVICE private pages are counted as being
15076470ccdSRalph Campbell 			 * mapped so the next 3 words hold the mapping, index,
15176470ccdSRalph Campbell 			 * and private fields from the source anonymous or
15276470ccdSRalph Campbell 			 * page cache page while the page is migrated to device
15376470ccdSRalph Campbell 			 * private memory.
15476470ccdSRalph Campbell 			 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
15576470ccdSRalph Campbell 			 * use the mapping, index, and private fields when
15676470ccdSRalph Campbell 			 * pmem backed DAX files are mapped.
15776470ccdSRalph Campbell 			 */
15850e7fbc3SMatthew Wilcox 		};
1594da1984eSMatthew Wilcox 
1604da1984eSMatthew Wilcox 		/** @rcu_head: You can use this to free a page by RCU. */
1614da1984eSMatthew Wilcox 		struct rcu_head rcu_head;
16266a6ffd2SMatthew Wilcox 	};
1637d27a04bSMatthew Wilcox 
164b21999daSMatthew Wilcox 	union {		/* This union is 4 bytes in size. */
165b21999daSMatthew Wilcox 		/*
166b21999daSMatthew Wilcox 		 * If the page can be mapped to userspace, encodes the number
167b21999daSMatthew Wilcox 		 * of times this page is referenced by a page table.
168b21999daSMatthew Wilcox 		 */
169b21999daSMatthew Wilcox 		atomic_t _mapcount;
170b21999daSMatthew Wilcox 
1717d27a04bSMatthew Wilcox 		/*
1726e292b9bSMatthew Wilcox 		 * If the page is neither PageSlab nor mappable to userspace,
1736e292b9bSMatthew Wilcox 		 * the value stored here may help determine what this page
1746e292b9bSMatthew Wilcox 		 * is used for.  See page-flags.h for a list of page types
1756e292b9bSMatthew Wilcox 		 * which are currently stored here.
1766e292b9bSMatthew Wilcox 		 */
1776e292b9bSMatthew Wilcox 		unsigned int page_type;
178b21999daSMatthew Wilcox 	};
179013e8963SChristoph Lameter 
180b21999daSMatthew Wilcox 	/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
1810139aa7bSJoonsoo Kim 	atomic_t _refcount;
182fc9bb8c7SChristoph Lameter 
1831306a85aSJohannes Weiner #ifdef CONFIG_MEMCG
184bcfe06bfSRoman Gushchin 	unsigned long memcg_data;
1851306a85aSJohannes Weiner #endif
1861306a85aSJohannes Weiner 
1875b99cd0eSHeiko Carstens 	/*
1885b99cd0eSHeiko Carstens 	 * On machines where all RAM is mapped into kernel address space,
1895b99cd0eSHeiko Carstens 	 * we can simply calculate the virtual address. On machines with
1905b99cd0eSHeiko Carstens 	 * highmem some memory is mapped into kernel virtual memory
1915b99cd0eSHeiko Carstens 	 * dynamically, so we need a place to store that address.
1925b99cd0eSHeiko Carstens 	 * Note that this field could be 16 bits on x86 ... ;)
1935b99cd0eSHeiko Carstens 	 *
1945b99cd0eSHeiko Carstens 	 * Architectures with slow multiplication can define
1955b99cd0eSHeiko Carstens 	 * WANT_PAGE_VIRTUAL in asm/page.h
1965b99cd0eSHeiko Carstens 	 */
1975b99cd0eSHeiko Carstens #if defined(WANT_PAGE_VIRTUAL)
1985b99cd0eSHeiko Carstens 	void *virtual;			/* Kernel virtual address (NULL if
1995b99cd0eSHeiko Carstens 					   not kmapped, ie. highmem) */
2005b99cd0eSHeiko Carstens #endif /* WANT_PAGE_VIRTUAL */
201dfec072eSVegard Nossum 
202f80be457SAlexander Potapenko #ifdef CONFIG_KMSAN
203f80be457SAlexander Potapenko 	/*
204f80be457SAlexander Potapenko 	 * KMSAN metadata for this page:
205f80be457SAlexander Potapenko 	 *  - shadow page: every bit indicates whether the corresponding
206f80be457SAlexander Potapenko 	 *    bit of the original page is initialized (0) or not (1);
207f80be457SAlexander Potapenko 	 *  - origin page: every 4 bytes contain an id of the stack trace
208f80be457SAlexander Potapenko 	 *    where the uninitialized value was created.
209f80be457SAlexander Potapenko 	 */
210f80be457SAlexander Potapenko 	struct page *kmsan_shadow;
211f80be457SAlexander Potapenko 	struct page *kmsan_origin;
212f80be457SAlexander Potapenko #endif
213f80be457SAlexander Potapenko 
21490572890SPeter Zijlstra #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
21590572890SPeter Zijlstra 	int _last_cpupid;
21657e0a030SMel Gorman #endif
217e20df2c6SMatthew Wilcox } _struct_page_alignment;
2185b99cd0eSHeiko Carstens 
21970fb4fdfSLinus Torvalds /*
22070fb4fdfSLinus Torvalds  * struct encoded_page - a nonexistent type marking this pointer
22170fb4fdfSLinus Torvalds  *
22270fb4fdfSLinus Torvalds  * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
22370fb4fdfSLinus Torvalds  * with the low bits of the pointer indicating extra context-dependent
22470fb4fdfSLinus Torvalds  * information. Not super-common, but happens in mmu_gather and mlock
22570fb4fdfSLinus Torvalds  * handling, and this acts as a type system check on that use.
22670fb4fdfSLinus Torvalds  *
22770fb4fdfSLinus Torvalds  * We only really have two guaranteed bits in general, although you could
22870fb4fdfSLinus Torvalds  * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
22970fb4fdfSLinus Torvalds  * for more.
23070fb4fdfSLinus Torvalds  *
23170fb4fdfSLinus Torvalds  * Use the supplied helper functions to endcode/decode the pointer and bits.
23270fb4fdfSLinus Torvalds  */
23370fb4fdfSLinus Torvalds struct encoded_page;
23470fb4fdfSLinus Torvalds #define ENCODE_PAGE_BITS 3ul
encode_page(struct page * page,unsigned long flags)23570fb4fdfSLinus Torvalds static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
23670fb4fdfSLinus Torvalds {
23770fb4fdfSLinus Torvalds 	BUILD_BUG_ON(flags > ENCODE_PAGE_BITS);
23870fb4fdfSLinus Torvalds 	return (struct encoded_page *)(flags | (unsigned long)page);
23970fb4fdfSLinus Torvalds }
24070fb4fdfSLinus Torvalds 
encoded_page_flags(struct encoded_page * page)24170fb4fdfSLinus Torvalds static inline unsigned long encoded_page_flags(struct encoded_page *page)
24270fb4fdfSLinus Torvalds {
24370fb4fdfSLinus Torvalds 	return ENCODE_PAGE_BITS & (unsigned long)page;
24470fb4fdfSLinus Torvalds }
24570fb4fdfSLinus Torvalds 
encoded_page_ptr(struct encoded_page * page)24670fb4fdfSLinus Torvalds static inline struct page *encoded_page_ptr(struct encoded_page *page)
24770fb4fdfSLinus Torvalds {
24870fb4fdfSLinus Torvalds 	return (struct page *)(~ENCODE_PAGE_BITS & (unsigned long)page);
24970fb4fdfSLinus Torvalds }
25070fb4fdfSLinus Torvalds 
251*85a13334SMatthew Wilcox /*
252*85a13334SMatthew Wilcox  * A swap entry has to fit into a "unsigned long", as the entry is hidden
253*85a13334SMatthew Wilcox  * in the "index" field of the swapper address space.
254*85a13334SMatthew Wilcox  */
255*85a13334SMatthew Wilcox typedef struct {
256*85a13334SMatthew Wilcox 	unsigned long val;
257*85a13334SMatthew Wilcox } swp_entry_t;
258*85a13334SMatthew Wilcox 
2597b230db3SMatthew Wilcox (Oracle) /**
2607b230db3SMatthew Wilcox (Oracle)  * struct folio - Represents a contiguous set of bytes.
2617b230db3SMatthew Wilcox (Oracle)  * @flags: Identical to the page flags.
2627b230db3SMatthew Wilcox (Oracle)  * @lru: Least Recently Used list; tracks how recently this folio was used.
263334f6f53SMatthew Wilcox (Oracle)  * @mlock_count: Number of times this folio has been pinned by mlock().
2647b230db3SMatthew Wilcox (Oracle)  * @mapping: The file this page belongs to, or refers to the anon_vma for
2657b230db3SMatthew Wilcox (Oracle)  *    anonymous memory.
2667b230db3SMatthew Wilcox (Oracle)  * @index: Offset within the file, in units of pages.  For anonymous memory,
2677b230db3SMatthew Wilcox (Oracle)  *    this is the index from the beginning of the mmap.
2687b230db3SMatthew Wilcox (Oracle)  * @private: Filesystem per-folio data (see folio_attach_private()).
269*85a13334SMatthew Wilcox  * @swap: Used for swp_entry_t if folio_test_swapcache().
2707b230db3SMatthew Wilcox (Oracle)  * @_mapcount: Do not access this member directly.  Use folio_mapcount() to
2717b230db3SMatthew Wilcox (Oracle)  *    find out how many times this folio is mapped by userspace.
2727b230db3SMatthew Wilcox (Oracle)  * @_refcount: Do not access this member directly.  Use folio_ref_count()
2737b230db3SMatthew Wilcox (Oracle)  *    to find how many references there are to this folio.
2747b230db3SMatthew Wilcox (Oracle)  * @memcg_data: Memory Control Group data.
275b14224fbSMatthew Wilcox (Oracle)  * @_entire_mapcount: Do not use directly, call folio_entire_mapcount().
276eec20426SMatthew Wilcox (Oracle)  * @_nr_pages_mapped: Do not use directly, call folio_mapcount().
277379708ffSMatthew Wilcox (Oracle)  * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
278379708ffSMatthew Wilcox (Oracle)  * @_folio_nr_pages: Do not use directly, call folio_nr_pages().
279dad6a5ebSHugh Dickins  * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
280dad6a5ebSHugh Dickins  * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
281dad6a5ebSHugh Dickins  * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
282dad6a5ebSHugh Dickins  * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
2834375a553SMatthew Wilcox (Oracle)  * @_deferred_list: Folios to be split under memory pressure.
2847b230db3SMatthew Wilcox (Oracle)  *
2857b230db3SMatthew Wilcox (Oracle)  * A folio is a physically, virtually and logically contiguous set
2867b230db3SMatthew Wilcox (Oracle)  * of bytes.  It is a power-of-two in size, and it is aligned to that
2877b230db3SMatthew Wilcox (Oracle)  * same power-of-two.  It is at least as large as %PAGE_SIZE.  If it is
2887b230db3SMatthew Wilcox (Oracle)  * in the page cache, it is at a file offset which is a multiple of that
2897b230db3SMatthew Wilcox (Oracle)  * power-of-two.  It may be mapped into userspace at an address which is
2907b230db3SMatthew Wilcox (Oracle)  * at an arbitrary page offset, but its kernel virtual address is aligned
2917b230db3SMatthew Wilcox (Oracle)  * to its size.
2927b230db3SMatthew Wilcox (Oracle)  */
2937b230db3SMatthew Wilcox (Oracle) struct folio {
2947b230db3SMatthew Wilcox (Oracle) 	/* private: don't document the anon union */
2957b230db3SMatthew Wilcox (Oracle) 	union {
2967b230db3SMatthew Wilcox (Oracle) 		struct {
2977b230db3SMatthew Wilcox (Oracle) 	/* public: */
2987b230db3SMatthew Wilcox (Oracle) 			unsigned long flags;
29907ca7606SHugh Dickins 			union {
3007b230db3SMatthew Wilcox (Oracle) 				struct list_head lru;
301334f6f53SMatthew Wilcox (Oracle) 	/* private: avoid cluttering the output */
30207ca7606SHugh Dickins 				struct {
30307ca7606SHugh Dickins 					void *__filler;
304334f6f53SMatthew Wilcox (Oracle) 	/* public: */
30507ca7606SHugh Dickins 					unsigned int mlock_count;
306334f6f53SMatthew Wilcox (Oracle) 	/* private: */
30707ca7606SHugh Dickins 				};
308334f6f53SMatthew Wilcox (Oracle) 	/* public: */
30907ca7606SHugh Dickins 			};
3107b230db3SMatthew Wilcox (Oracle) 			struct address_space *mapping;
3117b230db3SMatthew Wilcox (Oracle) 			pgoff_t index;
312*85a13334SMatthew Wilcox 			union {
3137b230db3SMatthew Wilcox (Oracle) 				void *private;
314*85a13334SMatthew Wilcox 				swp_entry_t swap;
315*85a13334SMatthew Wilcox 			};
3167b230db3SMatthew Wilcox (Oracle) 			atomic_t _mapcount;
3177b230db3SMatthew Wilcox (Oracle) 			atomic_t _refcount;
3187b230db3SMatthew Wilcox (Oracle) #ifdef CONFIG_MEMCG
3197b230db3SMatthew Wilcox (Oracle) 			unsigned long memcg_data;
3207b230db3SMatthew Wilcox (Oracle) #endif
3217b230db3SMatthew Wilcox (Oracle) 	/* private: the union with struct page is transitional */
3227b230db3SMatthew Wilcox (Oracle) 		};
3237b230db3SMatthew Wilcox (Oracle) 		struct page page;
3247b230db3SMatthew Wilcox (Oracle) 	};
325dad6a5ebSHugh Dickins 	union {
326dad6a5ebSHugh Dickins 		struct {
327379708ffSMatthew Wilcox (Oracle) 			unsigned long _flags_1;
328dad6a5ebSHugh Dickins 			unsigned long _head_1;
329ebc1baf5SMatthew Wilcox (Oracle) 			unsigned long _folio_avail;
330a8d55327SMatthew Wilcox (Oracle) 	/* public: */
331b14224fbSMatthew Wilcox (Oracle) 			atomic_t _entire_mapcount;
332eec20426SMatthew Wilcox (Oracle) 			atomic_t _nr_pages_mapped;
333379708ffSMatthew Wilcox (Oracle) 			atomic_t _pincount;
334379708ffSMatthew Wilcox (Oracle) #ifdef CONFIG_64BIT
335379708ffSMatthew Wilcox (Oracle) 			unsigned int _folio_nr_pages;
336b10ff04dSMatthew Wilcox (Oracle) #endif
337cfeed8ffSDavid Hildenbrand 	/* private: the union with struct page is transitional */
338dad6a5ebSHugh Dickins 		};
339dad6a5ebSHugh Dickins 		struct page __page_1;
340dad6a5ebSHugh Dickins 	};
341dad6a5ebSHugh Dickins 	union {
342dad6a5ebSHugh Dickins 		struct {
343dad6a5ebSHugh Dickins 			unsigned long _flags_2;
344dad6a5ebSHugh Dickins 			unsigned long _head_2;
345a8d55327SMatthew Wilcox (Oracle) 	/* public: */
346dad6a5ebSHugh Dickins 			void *_hugetlb_subpool;
347dad6a5ebSHugh Dickins 			void *_hugetlb_cgroup;
348dad6a5ebSHugh Dickins 			void *_hugetlb_cgroup_rsvd;
349dad6a5ebSHugh Dickins 			void *_hugetlb_hwpoison;
350a8d55327SMatthew Wilcox (Oracle) 	/* private: the union with struct page is transitional */
351dad6a5ebSHugh Dickins 		};
3524375a553SMatthew Wilcox (Oracle) 		struct {
3534375a553SMatthew Wilcox (Oracle) 			unsigned long _flags_2a;
3544375a553SMatthew Wilcox (Oracle) 			unsigned long _head_2a;
3554375a553SMatthew Wilcox (Oracle) 	/* public: */
3564375a553SMatthew Wilcox (Oracle) 			struct list_head _deferred_list;
3574375a553SMatthew Wilcox (Oracle) 	/* private: the union with struct page is transitional */
358dad6a5ebSHugh Dickins 		};
359dad6a5ebSHugh Dickins 		struct page __page_2;
360dad6a5ebSHugh Dickins 	};
3617b230db3SMatthew Wilcox (Oracle) };
3627b230db3SMatthew Wilcox (Oracle) 
3637b230db3SMatthew Wilcox (Oracle) #define FOLIO_MATCH(pg, fl)						\
3647b230db3SMatthew Wilcox (Oracle) 	static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
3657b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(flags, flags);
3667b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(lru, lru);
367536f4217SWei Yang FOLIO_MATCH(mapping, mapping);
3687b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(compound_head, lru);
3697b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(index, index);
3707b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(private, private);
3717b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(_mapcount, _mapcount);
3727b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(_refcount, _refcount);
3737b230db3SMatthew Wilcox (Oracle) #ifdef CONFIG_MEMCG
3747b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(memcg_data, memcg_data);
3757b230db3SMatthew Wilcox (Oracle) #endif
3767b230db3SMatthew Wilcox (Oracle) #undef FOLIO_MATCH
377379708ffSMatthew Wilcox (Oracle) #define FOLIO_MATCH(pg, fl)						\
378379708ffSMatthew Wilcox (Oracle) 	static_assert(offsetof(struct folio, fl) ==			\
379379708ffSMatthew Wilcox (Oracle) 			offsetof(struct page, pg) + sizeof(struct page))
380379708ffSMatthew Wilcox (Oracle) FOLIO_MATCH(flags, _flags_1);
381dad6a5ebSHugh Dickins FOLIO_MATCH(compound_head, _head_1);
382379708ffSMatthew Wilcox (Oracle) #undef FOLIO_MATCH
383dad6a5ebSHugh Dickins #define FOLIO_MATCH(pg, fl)						\
384dad6a5ebSHugh Dickins 	static_assert(offsetof(struct folio, fl) ==			\
385dad6a5ebSHugh Dickins 			offsetof(struct page, pg) + 2 * sizeof(struct page))
386dad6a5ebSHugh Dickins FOLIO_MATCH(flags, _flags_2);
387dad6a5ebSHugh Dickins FOLIO_MATCH(compound_head, _head_2);
388b10ff04dSMatthew Wilcox (Oracle) FOLIO_MATCH(flags, _flags_2a);
389b10ff04dSMatthew Wilcox (Oracle) FOLIO_MATCH(compound_head, _head_2a);
390dad6a5ebSHugh Dickins #undef FOLIO_MATCH
3917b230db3SMatthew Wilcox (Oracle) 
3929a35de4fSVishal Moola (Oracle) /**
3939a35de4fSVishal Moola (Oracle)  * struct ptdesc -    Memory descriptor for page tables.
3949a35de4fSVishal Moola (Oracle)  * @__page_flags:     Same as page flags. Unused for page tables.
3959a35de4fSVishal Moola (Oracle)  * @pt_rcu_head:      For freeing page table pages.
3969a35de4fSVishal Moola (Oracle)  * @pt_list:          List of used page tables. Used for s390 and x86.
3979a35de4fSVishal Moola (Oracle)  * @_pt_pad_1:        Padding that aliases with page's compound head.
3989a35de4fSVishal Moola (Oracle)  * @pmd_huge_pte:     Protected by ptdesc->ptl, used for THPs.
3999a35de4fSVishal Moola (Oracle)  * @__page_mapping:   Aliases with page->mapping. Unused for page tables.
4009a35de4fSVishal Moola (Oracle)  * @pt_mm:            Used for x86 pgds.
4019a35de4fSVishal Moola (Oracle)  * @pt_frag_refcount: For fragmented page table tracking. Powerpc and s390 only.
4029a35de4fSVishal Moola (Oracle)  * @_pt_pad_2:        Padding to ensure proper alignment.
4039a35de4fSVishal Moola (Oracle)  * @ptl:              Lock for the page table.
4049a35de4fSVishal Moola (Oracle)  * @__page_type:      Same as page->page_type. Unused for page tables.
4059a35de4fSVishal Moola (Oracle)  * @_refcount:        Same as page refcount. Used for s390 page tables.
4069a35de4fSVishal Moola (Oracle)  * @pt_memcg_data:    Memcg data. Tracked for page tables here.
4079a35de4fSVishal Moola (Oracle)  *
4089a35de4fSVishal Moola (Oracle)  * This struct overlays struct page for now. Do not modify without a good
4099a35de4fSVishal Moola (Oracle)  * understanding of the issues.
4109a35de4fSVishal Moola (Oracle)  */
4119a35de4fSVishal Moola (Oracle) struct ptdesc {
4129a35de4fSVishal Moola (Oracle) 	unsigned long __page_flags;
4139a35de4fSVishal Moola (Oracle) 
4149a35de4fSVishal Moola (Oracle) 	union {
4159a35de4fSVishal Moola (Oracle) 		struct rcu_head pt_rcu_head;
4169a35de4fSVishal Moola (Oracle) 		struct list_head pt_list;
4179a35de4fSVishal Moola (Oracle) 		struct {
4189a35de4fSVishal Moola (Oracle) 			unsigned long _pt_pad_1;
4199a35de4fSVishal Moola (Oracle) 			pgtable_t pmd_huge_pte;
4209a35de4fSVishal Moola (Oracle) 		};
4219a35de4fSVishal Moola (Oracle) 	};
4229a35de4fSVishal Moola (Oracle) 	unsigned long __page_mapping;
4239a35de4fSVishal Moola (Oracle) 
4249a35de4fSVishal Moola (Oracle) 	union {
4259a35de4fSVishal Moola (Oracle) 		struct mm_struct *pt_mm;
4269a35de4fSVishal Moola (Oracle) 		atomic_t pt_frag_refcount;
4279a35de4fSVishal Moola (Oracle) 	};
4289a35de4fSVishal Moola (Oracle) 
4299a35de4fSVishal Moola (Oracle) 	union {
4309a35de4fSVishal Moola (Oracle) 		unsigned long _pt_pad_2;
4319a35de4fSVishal Moola (Oracle) #if ALLOC_SPLIT_PTLOCKS
4329a35de4fSVishal Moola (Oracle) 		spinlock_t *ptl;
4339a35de4fSVishal Moola (Oracle) #else
4349a35de4fSVishal Moola (Oracle) 		spinlock_t ptl;
4359a35de4fSVishal Moola (Oracle) #endif
4369a35de4fSVishal Moola (Oracle) 	};
4379a35de4fSVishal Moola (Oracle) 	unsigned int __page_type;
4389a35de4fSVishal Moola (Oracle) 	atomic_t _refcount;
4399a35de4fSVishal Moola (Oracle) #ifdef CONFIG_MEMCG
4409a35de4fSVishal Moola (Oracle) 	unsigned long pt_memcg_data;
4419a35de4fSVishal Moola (Oracle) #endif
4429a35de4fSVishal Moola (Oracle) };
4439a35de4fSVishal Moola (Oracle) 
4449a35de4fSVishal Moola (Oracle) #define TABLE_MATCH(pg, pt)						\
4459a35de4fSVishal Moola (Oracle) 	static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt))
4469a35de4fSVishal Moola (Oracle) TABLE_MATCH(flags, __page_flags);
4479a35de4fSVishal Moola (Oracle) TABLE_MATCH(compound_head, pt_list);
4489a35de4fSVishal Moola (Oracle) TABLE_MATCH(compound_head, _pt_pad_1);
4499a35de4fSVishal Moola (Oracle) TABLE_MATCH(mapping, __page_mapping);
4509a35de4fSVishal Moola (Oracle) TABLE_MATCH(rcu_head, pt_rcu_head);
4519a35de4fSVishal Moola (Oracle) TABLE_MATCH(page_type, __page_type);
4529a35de4fSVishal Moola (Oracle) TABLE_MATCH(_refcount, _refcount);
4539a35de4fSVishal Moola (Oracle) #ifdef CONFIG_MEMCG
4549a35de4fSVishal Moola (Oracle) TABLE_MATCH(memcg_data, pt_memcg_data);
4559a35de4fSVishal Moola (Oracle) #endif
4569a35de4fSVishal Moola (Oracle) #undef TABLE_MATCH
4579a35de4fSVishal Moola (Oracle) static_assert(sizeof(struct ptdesc) <= sizeof(struct page));
4589a35de4fSVishal Moola (Oracle) 
459bf2d4334SVishal Moola (Oracle) #define ptdesc_page(pt)			(_Generic((pt),			\
460bf2d4334SVishal Moola (Oracle) 	const struct ptdesc *:		(const struct page *)(pt),	\
461bf2d4334SVishal Moola (Oracle) 	struct ptdesc *:		(struct page *)(pt)))
462bf2d4334SVishal Moola (Oracle) 
463bf2d4334SVishal Moola (Oracle) #define ptdesc_folio(pt)		(_Generic((pt),			\
464bf2d4334SVishal Moola (Oracle) 	const struct ptdesc *:		(const struct folio *)(pt),	\
465bf2d4334SVishal Moola (Oracle) 	struct ptdesc *:		(struct folio *)(pt)))
466bf2d4334SVishal Moola (Oracle) 
467bf2d4334SVishal Moola (Oracle) #define page_ptdesc(p)			(_Generic((p),			\
468bf2d4334SVishal Moola (Oracle) 	const struct page *:		(const struct ptdesc *)(p),	\
469bf2d4334SVishal Moola (Oracle) 	struct page *:			(struct ptdesc *)(p)))
470bf2d4334SVishal Moola (Oracle) 
471d1402fc7SLogan Gunthorpe /*
472d1402fc7SLogan Gunthorpe  * Used for sizing the vmemmap region on some architectures
473d1402fc7SLogan Gunthorpe  */
474d1402fc7SLogan Gunthorpe #define STRUCT_PAGE_MAX_SHIFT	(order_base_2(sizeof(struct page)))
475d1402fc7SLogan Gunthorpe 
476b63ae8caSAlexander Duyck #define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK)
477b63ae8caSAlexander Duyck #define PAGE_FRAG_CACHE_MAX_ORDER	get_order(PAGE_FRAG_CACHE_MAX_SIZE)
478b63ae8caSAlexander Duyck 
47985d0a2edSMatthew Wilcox (Oracle) /*
48085d0a2edSMatthew Wilcox (Oracle)  * page_private can be used on tail pages.  However, PagePrivate is only
48185d0a2edSMatthew Wilcox (Oracle)  * checked by the VM on the head page.  So page_private on the tail pages
48285d0a2edSMatthew Wilcox (Oracle)  * should be used for data that's ancillary to the head page (eg attaching
48385d0a2edSMatthew Wilcox (Oracle)  * buffer heads to tail pages after attaching buffer heads to the head page)
48485d0a2edSMatthew Wilcox (Oracle)  */
485b03641afSDan Williams #define page_private(page)		((page)->private)
48660e65a6fSGuoqing Jiang 
set_page_private(struct page * page,unsigned long private)48760e65a6fSGuoqing Jiang static inline void set_page_private(struct page *page, unsigned long private)
48860e65a6fSGuoqing Jiang {
48960e65a6fSGuoqing Jiang 	page->private = private;
49060e65a6fSGuoqing Jiang }
491b03641afSDan Williams 
folio_get_private(struct folio * folio)49285d0a2edSMatthew Wilcox (Oracle) static inline void *folio_get_private(struct folio *folio)
49385d0a2edSMatthew Wilcox (Oracle) {
49485d0a2edSMatthew Wilcox (Oracle) 	return folio->private;
49585d0a2edSMatthew Wilcox (Oracle) }
49685d0a2edSMatthew Wilcox (Oracle) 
497b63ae8caSAlexander Duyck struct page_frag_cache {
498b63ae8caSAlexander Duyck 	void * va;
499b63ae8caSAlexander Duyck #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
500b63ae8caSAlexander Duyck 	__u16 offset;
501b63ae8caSAlexander Duyck 	__u16 size;
502b63ae8caSAlexander Duyck #else
503b63ae8caSAlexander Duyck 	__u32 offset;
504b63ae8caSAlexander Duyck #endif
505b63ae8caSAlexander Duyck 	/* we maintain a pagecount bias, so that we dont dirty cache line
5060139aa7bSJoonsoo Kim 	 * containing page->_refcount every time we allocate a fragment.
507b63ae8caSAlexander Duyck 	 */
508b63ae8caSAlexander Duyck 	unsigned int		pagecnt_bias;
509b63ae8caSAlexander Duyck 	bool pfmemalloc;
510b63ae8caSAlexander Duyck };
511b63ae8caSAlexander Duyck 
51264b990d2SKirill A. Shutemov typedef unsigned long vm_flags_t;
513ca16d140SKOSAKI Motohiro 
514c92ff1bdSMartin Schwidefsky /*
5158feae131SDavid Howells  * A region containing a mapping of a non-memory backed file under NOMMU
5168feae131SDavid Howells  * conditions.  These are held in a global tree and are pinned by the VMAs that
5178feae131SDavid Howells  * map parts of them.
5188feae131SDavid Howells  */
5198feae131SDavid Howells struct vm_region {
5208feae131SDavid Howells 	struct rb_node	vm_rb;		/* link in global region tree */
521ca16d140SKOSAKI Motohiro 	vm_flags_t	vm_flags;	/* VMA vm_flags */
5228feae131SDavid Howells 	unsigned long	vm_start;	/* start address of region */
5238feae131SDavid Howells 	unsigned long	vm_end;		/* region initialised to here */
524dd8632a1SPaul Mundt 	unsigned long	vm_top;		/* region allocated to here */
5258feae131SDavid Howells 	unsigned long	vm_pgoff;	/* the offset in vm_file corresponding to vm_start */
5268feae131SDavid Howells 	struct file	*vm_file;	/* the backing file or NULL */
5278feae131SDavid Howells 
5281e2ae599SDavid Howells 	int		vm_usage;	/* region usage count (access under nommu_region_sem) */
529cfe79c00SMike Frysinger 	bool		vm_icache_flushed : 1; /* true if the icache has been flushed for
530cfe79c00SMike Frysinger 						* this region */
5318feae131SDavid Howells };
5328feae131SDavid Howells 
533745f234bSAndrea Arcangeli #ifdef CONFIG_USERFAULTFD
534745f234bSAndrea Arcangeli #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
535745f234bSAndrea Arcangeli struct vm_userfaultfd_ctx {
536745f234bSAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
537745f234bSAndrea Arcangeli };
538745f234bSAndrea Arcangeli #else /* CONFIG_USERFAULTFD */
539745f234bSAndrea Arcangeli #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
540745f234bSAndrea Arcangeli struct vm_userfaultfd_ctx {};
541745f234bSAndrea Arcangeli #endif /* CONFIG_USERFAULTFD */
542745f234bSAndrea Arcangeli 
54378db3412SSuren Baghdasaryan struct anon_vma_name {
54478db3412SSuren Baghdasaryan 	struct kref kref;
54578db3412SSuren Baghdasaryan 	/* The name needs to be at the end because it is dynamically sized. */
54678db3412SSuren Baghdasaryan 	char name[];
54778db3412SSuren Baghdasaryan };
54878db3412SSuren Baghdasaryan 
549c7f8f31cSSuren Baghdasaryan struct vma_lock {
550c7f8f31cSSuren Baghdasaryan 	struct rw_semaphore lock;
551c7f8f31cSSuren Baghdasaryan };
552c7f8f31cSSuren Baghdasaryan 
553ef6a22b7SMel Gorman struct vma_numab_state {
554ef6a22b7SMel Gorman 	unsigned long next_scan;
55520f58648SRaghavendra K T 	unsigned long next_pid_reset;
55620f58648SRaghavendra K T 	unsigned long access_pids[2];
557ef6a22b7SMel Gorman };
558ef6a22b7SMel Gorman 
5598feae131SDavid Howells /*
560552657b7Schenqiwu  * This struct describes a virtual memory area. There is one of these
561c92ff1bdSMartin Schwidefsky  * per VM-area/task. A VM area is any part of the process virtual memory
562c92ff1bdSMartin Schwidefsky  * space that has a special rule for the page-fault handlers (ie a shared
563c92ff1bdSMartin Schwidefsky  * library, the executable area etc).
564c92ff1bdSMartin Schwidefsky  */
565c92ff1bdSMartin Schwidefsky struct vm_area_struct {
566e4c6bfd2SRik van Riel 	/* The first cache line has the info for VMA tree walking. */
567e4c6bfd2SRik van Riel 
56820cce633SMichel Lespinasse 	union {
56920cce633SMichel Lespinasse 		struct {
57020cce633SMichel Lespinasse 			/* VMA covers [vm_start; vm_end) addresses within mm */
57120cce633SMichel Lespinasse 			unsigned long vm_start;
57220cce633SMichel Lespinasse 			unsigned long vm_end;
57320cce633SMichel Lespinasse 		};
57420cce633SMichel Lespinasse #ifdef CONFIG_PER_VMA_LOCK
57520cce633SMichel Lespinasse 		struct rcu_head vm_rcu;	/* Used for deferred freeing. */
57620cce633SMichel Lespinasse #endif
57720cce633SMichel Lespinasse 	};
578c92ff1bdSMartin Schwidefsky 
579e4c6bfd2SRik van Riel 	struct mm_struct *vm_mm;	/* The address space we belong to. */
58028d8b812SLorenzo Stoakes 	pgprot_t vm_page_prot;          /* Access permissions of this VMA. */
581bc292ab0SSuren Baghdasaryan 
582bc292ab0SSuren Baghdasaryan 	/*
583bc292ab0SSuren Baghdasaryan 	 * Flags, see mm.h.
584bc292ab0SSuren Baghdasaryan 	 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
585bc292ab0SSuren Baghdasaryan 	 */
586bc292ab0SSuren Baghdasaryan 	union {
587bc292ab0SSuren Baghdasaryan 		const vm_flags_t vm_flags;
588bc292ab0SSuren Baghdasaryan 		vm_flags_t __private __vm_flags;
589bc292ab0SSuren Baghdasaryan 	};
590e4c6bfd2SRik van Riel 
5915e31275cSSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK
592b1f02b95SJann Horn 	/*
593b1f02b95SJann Horn 	 * Can only be written (using WRITE_ONCE()) while holding both:
594b1f02b95SJann Horn 	 *  - mmap_lock (in write mode)
595b1f02b95SJann Horn 	 *  - vm_lock->lock (in write mode)
596b1f02b95SJann Horn 	 * Can be read reliably while holding one of:
597b1f02b95SJann Horn 	 *  - mmap_lock (in read or write mode)
598b1f02b95SJann Horn 	 *  - vm_lock->lock (in read or write mode)
599b1f02b95SJann Horn 	 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
600b1f02b95SJann Horn 	 * while holding nothing (except RCU to keep the VMA struct allocated).
601b1f02b95SJann Horn 	 *
602b1f02b95SJann Horn 	 * This sequence counter is explicitly allowed to overflow; sequence
603b1f02b95SJann Horn 	 * counter reuse can only lead to occasional unnecessary use of the
604b1f02b95SJann Horn 	 * slowpath.
605b1f02b95SJann Horn 	 */
6065e31275cSSuren Baghdasaryan 	int vm_lock_seq;
607c7f8f31cSSuren Baghdasaryan 	struct vma_lock *vm_lock;
608457f67beSSuren Baghdasaryan 
609457f67beSSuren Baghdasaryan 	/* Flag to indicate areas detached from the mm->mm_mt tree */
610457f67beSSuren Baghdasaryan 	bool detached;
6115e31275cSSuren Baghdasaryan #endif
6125e31275cSSuren Baghdasaryan 
613d3737187SMichel Lespinasse 	/*
614c92ff1bdSMartin Schwidefsky 	 * For areas with an address space and backing store,
61527ba0644SKirill A. Shutemov 	 * linkage into the address_space->i_mmap interval tree.
6169a10064fSColin Cross 	 *
617c92ff1bdSMartin Schwidefsky 	 */
618c92ff1bdSMartin Schwidefsky 	struct {
6196b2dbba8SMichel Lespinasse 		struct rb_node rb;
6206b2dbba8SMichel Lespinasse 		unsigned long rb_subtree_last;
621c92ff1bdSMartin Schwidefsky 	} shared;
622c92ff1bdSMartin Schwidefsky 
623c92ff1bdSMartin Schwidefsky 	/*
624c92ff1bdSMartin Schwidefsky 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
625c92ff1bdSMartin Schwidefsky 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
626c92ff1bdSMartin Schwidefsky 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
627c92ff1bdSMartin Schwidefsky 	 * or brk vma (with NULL file) can only be in an anon_vma list.
628c92ff1bdSMartin Schwidefsky 	 */
629c1e8d7c6SMichel Lespinasse 	struct list_head anon_vma_chain; /* Serialized by mmap_lock &
6305beb4930SRik van Riel 					  * page_table_lock */
631c92ff1bdSMartin Schwidefsky 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
632c92ff1bdSMartin Schwidefsky 
633c92ff1bdSMartin Schwidefsky 	/* Function pointers to deal with this struct. */
634f0f37e2fSAlexey Dobriyan 	const struct vm_operations_struct *vm_ops;
635c92ff1bdSMartin Schwidefsky 
636c92ff1bdSMartin Schwidefsky 	/* Information about our backing store: */
637c92ff1bdSMartin Schwidefsky 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
638ea1754a0SKirill A. Shutemov 					   units */
639c92ff1bdSMartin Schwidefsky 	struct file * vm_file;		/* File we map to (can be NULL). */
640c92ff1bdSMartin Schwidefsky 	void * vm_private_data;		/* was vm_pte (shared mem) */
641c92ff1bdSMartin Schwidefsky 
642d09e8ca6SPasha Tatashin #ifdef CONFIG_ANON_VMA_NAME
643d09e8ca6SPasha Tatashin 	/*
644d09e8ca6SPasha Tatashin 	 * For private and shared anonymous mappings, a pointer to a null
645d09e8ca6SPasha Tatashin 	 * terminated string containing the name given to the vma, or NULL if
6468651a137SLorenzo Stoakes 	 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
647d09e8ca6SPasha Tatashin 	 */
648d09e8ca6SPasha Tatashin 	struct anon_vma_name *anon_name;
649d09e8ca6SPasha Tatashin #endif
650219f8a2eSAlexey Dobriyan #ifdef CONFIG_SWAP
651ec560175SHuang Ying 	atomic_long_t swap_readahead_info;
652219f8a2eSAlexey Dobriyan #endif
653c92ff1bdSMartin Schwidefsky #ifndef CONFIG_MMU
6548feae131SDavid Howells 	struct vm_region *vm_region;	/* NOMMU mapping region */
655c92ff1bdSMartin Schwidefsky #endif
656c92ff1bdSMartin Schwidefsky #ifdef CONFIG_NUMA
657c92ff1bdSMartin Schwidefsky 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
658c92ff1bdSMartin Schwidefsky #endif
659ef6a22b7SMel Gorman #ifdef CONFIG_NUMA_BALANCING
660ef6a22b7SMel Gorman 	struct vma_numab_state *numab_state;	/* NUMA Balancing state */
661ef6a22b7SMel Gorman #endif
662745f234bSAndrea Arcangeli 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
6633859a271SKees Cook } __randomize_layout;
664c92ff1bdSMartin Schwidefsky 
665223baf9dSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID
666223baf9dSMathieu Desnoyers struct mm_cid {
667223baf9dSMathieu Desnoyers 	u64 time;
668223baf9dSMathieu Desnoyers 	int cid;
669223baf9dSMathieu Desnoyers };
670223baf9dSMathieu Desnoyers #endif
671223baf9dSMathieu Desnoyers 
672db446a08SBenjamin LaHaise struct kioctx_table;
673c92ff1bdSMartin Schwidefsky struct mm_struct {
674c1a2f7f0SRik van Riel 	struct {
675c1753fd0SMathieu Desnoyers 		/*
676c1753fd0SMathieu Desnoyers 		 * Fields which are often written to are placed in a separate
677c1753fd0SMathieu Desnoyers 		 * cache line.
678c1753fd0SMathieu Desnoyers 		 */
679c1753fd0SMathieu Desnoyers 		struct {
680c1753fd0SMathieu Desnoyers 			/**
681c1753fd0SMathieu Desnoyers 			 * @mm_count: The number of references to &struct
682c1753fd0SMathieu Desnoyers 			 * mm_struct (@mm_users count as 1).
683c1753fd0SMathieu Desnoyers 			 *
684c1753fd0SMathieu Desnoyers 			 * Use mmgrab()/mmdrop() to modify. When this drops to
685c1753fd0SMathieu Desnoyers 			 * 0, the &struct mm_struct is freed.
686c1753fd0SMathieu Desnoyers 			 */
687c1753fd0SMathieu Desnoyers 			atomic_t mm_count;
688c1753fd0SMathieu Desnoyers 		} ____cacheline_aligned_in_smp;
689c1753fd0SMathieu Desnoyers 
690d4af56c5SLiam R. Howlett 		struct maple_tree mm_mt;
691efc1a3b1SDavid Howells #ifdef CONFIG_MMU
692c92ff1bdSMartin Schwidefsky 		unsigned long (*get_unmapped_area) (struct file *filp,
693c92ff1bdSMartin Schwidefsky 				unsigned long addr, unsigned long len,
694c92ff1bdSMartin Schwidefsky 				unsigned long pgoff, unsigned long flags);
695efc1a3b1SDavid Howells #endif
696c92ff1bdSMartin Schwidefsky 		unsigned long mmap_base;	/* base of mmap area */
69741aacc1eSRadu Caragea 		unsigned long mmap_legacy_base;	/* base of mmap area in bottom-up allocations */
6981b028f78SDmitry Safonov #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
699041711ceSZhen Lei 		/* Base addresses for compatible mmap() */
7001b028f78SDmitry Safonov 		unsigned long mmap_compat_base;
7011b028f78SDmitry Safonov 		unsigned long mmap_compat_legacy_base;
7021b028f78SDmitry Safonov #endif
703c92ff1bdSMartin Schwidefsky 		unsigned long task_size;	/* size of task vm space */
704c92ff1bdSMartin Schwidefsky 		pgd_t * pgd;
705b279ddc3SVegard Nossum 
706227a4aadSMathieu Desnoyers #ifdef CONFIG_MEMBARRIER
707227a4aadSMathieu Desnoyers 		/**
708227a4aadSMathieu Desnoyers 		 * @membarrier_state: Flags controlling membarrier behavior.
709227a4aadSMathieu Desnoyers 		 *
710227a4aadSMathieu Desnoyers 		 * This field is close to @pgd to hopefully fit in the same
711227a4aadSMathieu Desnoyers 		 * cache-line, which needs to be touched by switch_mm().
712227a4aadSMathieu Desnoyers 		 */
713227a4aadSMathieu Desnoyers 		atomic_t membarrier_state;
714227a4aadSMathieu Desnoyers #endif
715227a4aadSMathieu Desnoyers 
716b279ddc3SVegard Nossum 		/**
717b279ddc3SVegard Nossum 		 * @mm_users: The number of users including userspace.
718b279ddc3SVegard Nossum 		 *
719c1a2f7f0SRik van Riel 		 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
720c1a2f7f0SRik van Riel 		 * drops to 0 (i.e. when the task exits and there are no other
721c1a2f7f0SRik van Riel 		 * temporary reference holders), we also release a reference on
722c1a2f7f0SRik van Riel 		 * @mm_count (which may then free the &struct mm_struct if
723c1a2f7f0SRik van Riel 		 * @mm_count also drops to 0).
724b279ddc3SVegard Nossum 		 */
725b279ddc3SVegard Nossum 		atomic_t mm_users;
726b279ddc3SVegard Nossum 
727af7f588dSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID
728af7f588dSMathieu Desnoyers 		/**
729223baf9dSMathieu Desnoyers 		 * @pcpu_cid: Per-cpu current cid.
730af7f588dSMathieu Desnoyers 		 *
731223baf9dSMathieu Desnoyers 		 * Keep track of the currently allocated mm_cid for each cpu.
732223baf9dSMathieu Desnoyers 		 * The per-cpu mm_cid values are serialized by their respective
733223baf9dSMathieu Desnoyers 		 * runqueue locks.
734af7f588dSMathieu Desnoyers 		 */
735223baf9dSMathieu Desnoyers 		struct mm_cid __percpu *pcpu_cid;
736223baf9dSMathieu Desnoyers 		/*
737223baf9dSMathieu Desnoyers 		 * @mm_cid_next_scan: Next mm_cid scan (in jiffies).
738223baf9dSMathieu Desnoyers 		 *
739223baf9dSMathieu Desnoyers 		 * When the next mm_cid scan is due (in jiffies).
740223baf9dSMathieu Desnoyers 		 */
741223baf9dSMathieu Desnoyers 		unsigned long mm_cid_next_scan;
742af7f588dSMathieu Desnoyers #endif
743c4812909SKirill A. Shutemov #ifdef CONFIG_MMU
7443783e172SKele Huang 		atomic_long_t pgtables_bytes;	/* size of all page tables */
745b4e98d9aSKirill A. Shutemov #endif
746c92ff1bdSMartin Schwidefsky 		int map_count;			/* number of VMAs */
747481b4bb5SRichard Kennedy 
748c1a2f7f0SRik van Riel 		spinlock_t page_table_lock; /* Protects page tables and some
749c1a2f7f0SRik van Riel 					     * counters
750c1a2f7f0SRik van Riel 					     */
7512e302543SFeng Tang 		/*
7522e302543SFeng Tang 		 * With some kernel config, the current mmap_lock's offset
7532e302543SFeng Tang 		 * inside 'mm_struct' is at 0x120, which is very optimal, as
7542e302543SFeng Tang 		 * its two hot fields 'count' and 'owner' sit in 2 different
7552e302543SFeng Tang 		 * cachelines,  and when mmap_lock is highly contended, both
7562e302543SFeng Tang 		 * of the 2 fields will be accessed frequently, current layout
7572e302543SFeng Tang 		 * will help to reduce cache bouncing.
7582e302543SFeng Tang 		 *
7592e302543SFeng Tang 		 * So please be careful with adding new fields before
7602e302543SFeng Tang 		 * mmap_lock, which can easily push the 2 fields into one
7612e302543SFeng Tang 		 * cacheline.
7622e302543SFeng Tang 		 */
763da1c55f1SMichel Lespinasse 		struct rw_semaphore mmap_lock;
764c92ff1bdSMartin Schwidefsky 
765c1a2f7f0SRik van Riel 		struct list_head mmlist; /* List of maybe swapped mm's.	These
766c1a2f7f0SRik van Riel 					  * are globally strung together off
767c1a2f7f0SRik van Riel 					  * init_mm.mmlist, and are protected
768c92ff1bdSMartin Schwidefsky 					  * by mmlist_lock
769c92ff1bdSMartin Schwidefsky 					  */
7705e31275cSSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK
771b1f02b95SJann Horn 		/*
772b1f02b95SJann Horn 		 * This field has lock-like semantics, meaning it is sometimes
773b1f02b95SJann Horn 		 * accessed with ACQUIRE/RELEASE semantics.
774b1f02b95SJann Horn 		 * Roughly speaking, incrementing the sequence number is
775b1f02b95SJann Horn 		 * equivalent to releasing locks on VMAs; reading the sequence
776b1f02b95SJann Horn 		 * number can be part of taking a read lock on a VMA.
777b1f02b95SJann Horn 		 *
778b1f02b95SJann Horn 		 * Can be modified under write mmap_lock using RELEASE
779b1f02b95SJann Horn 		 * semantics.
780b1f02b95SJann Horn 		 * Can be read with no other protection when holding write
781b1f02b95SJann Horn 		 * mmap_lock.
782b1f02b95SJann Horn 		 * Can be read with ACQUIRE semantics if not holding write
783b1f02b95SJann Horn 		 * mmap_lock.
784b1f02b95SJann Horn 		 */
7855e31275cSSuren Baghdasaryan 		int mm_lock_seq;
7865e31275cSSuren Baghdasaryan #endif
787c92ff1bdSMartin Schwidefsky 
788c92ff1bdSMartin Schwidefsky 
789c92ff1bdSMartin Schwidefsky 		unsigned long hiwater_rss; /* High-watermark of RSS usage */
790c92ff1bdSMartin Schwidefsky 		unsigned long hiwater_vm;  /* High-water virtual memory usage */
791c92ff1bdSMartin Schwidefsky 
792e10d59f2SChristoph Lameter 		unsigned long total_vm;	   /* Total pages mapped */
793e10d59f2SChristoph Lameter 		unsigned long locked_vm;   /* Pages that have PG_mlocked set */
79470f8a3caSDavidlohr Bueso 		atomic64_t    pinned_vm;   /* Refcount permanently increased */
79530bdbb78SKonstantin Khlebnikov 		unsigned long data_vm;	   /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
79630bdbb78SKonstantin Khlebnikov 		unsigned long exec_vm;	   /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
79730bdbb78SKonstantin Khlebnikov 		unsigned long stack_vm;	   /* VM_STACK */
798e10d59f2SChristoph Lameter 		unsigned long def_flags;
79988aa7cc6SYang Shi 
8002e302543SFeng Tang 		/**
8012e302543SFeng Tang 		 * @write_protect_seq: Locked when any thread is write
8022e302543SFeng Tang 		 * protecting pages mapped by this mm to enforce a later COW,
8032e302543SFeng Tang 		 * for instance during page table copying for fork().
8042e302543SFeng Tang 		 */
8052e302543SFeng Tang 		seqcount_t write_protect_seq;
8062e302543SFeng Tang 
80788aa7cc6SYang Shi 		spinlock_t arg_lock; /* protect the below fields */
8082e302543SFeng Tang 
809c92ff1bdSMartin Schwidefsky 		unsigned long start_code, end_code, start_data, end_data;
810c92ff1bdSMartin Schwidefsky 		unsigned long start_brk, brk, start_stack;
811c92ff1bdSMartin Schwidefsky 		unsigned long arg_start, arg_end, env_start, env_end;
812c92ff1bdSMartin Schwidefsky 
813c92ff1bdSMartin Schwidefsky 		unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
814c92ff1bdSMartin Schwidefsky 
815f1a79412SShakeel Butt 		struct percpu_counter rss_stat[NR_MM_COUNTERS];
816d559db08SKAMEZAWA Hiroyuki 
817801460d0SHiroshi Shimamoto 		struct linux_binfmt *binfmt;
818801460d0SHiroshi Shimamoto 
819c92ff1bdSMartin Schwidefsky 		/* Architecture-specific MM context */
820c92ff1bdSMartin Schwidefsky 		mm_context_t context;
821c92ff1bdSMartin Schwidefsky 
822c1a2f7f0SRik van Riel 		unsigned long flags; /* Must use atomic bitops to access */
823c92ff1bdSMartin Schwidefsky 
824858f0993SAlexey Dobriyan #ifdef CONFIG_AIO
825abf137ddSJens Axboe 		spinlock_t			ioctx_lock;
826db446a08SBenjamin LaHaise 		struct kioctx_table __rcu	*ioctx_table;
827858f0993SAlexey Dobriyan #endif
828f98bafa0SOleg Nesterov #ifdef CONFIG_MEMCG
8294cd1a8fcSKOSAKI Motohiro 		/*
8304cd1a8fcSKOSAKI Motohiro 		 * "owner" points to a task that is regarded as the canonical
8314cd1a8fcSKOSAKI Motohiro 		 * user/owner of this mm. All of the following must be true in
8324cd1a8fcSKOSAKI Motohiro 		 * order for it to be changed:
8334cd1a8fcSKOSAKI Motohiro 		 *
8344cd1a8fcSKOSAKI Motohiro 		 * current == mm->owner
8354cd1a8fcSKOSAKI Motohiro 		 * current->mm != mm
8364cd1a8fcSKOSAKI Motohiro 		 * new_owner->mm == mm
8374cd1a8fcSKOSAKI Motohiro 		 * new_owner->alloc_lock is held
8384cd1a8fcSKOSAKI Motohiro 		 */
8394d2deb40SArnd Bergmann 		struct task_struct __rcu *owner;
84078fb7466SPavel Emelianov #endif
841bfedb589SEric W. Biederman 		struct user_namespace *user_ns;
842925d1c40SMatt Helsley 
843925d1c40SMatt Helsley 		/* store ref to file /proc/<pid>/exe symlink points to */
84490f31d0eSKonstantin Khlebnikov 		struct file __rcu *exe_file;
845cddb8a5cSAndrea Arcangeli #ifdef CONFIG_MMU_NOTIFIER
846984cfe4eSJason Gunthorpe 		struct mmu_notifier_subscriptions *notifier_subscriptions;
847cddb8a5cSAndrea Arcangeli #endif
848e009bb30SKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
849e7a00c45SAndrea Arcangeli 		pgtable_t pmd_huge_pte; /* protected by page_table_lock */
850e7a00c45SAndrea Arcangeli #endif
851cbee9f88SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING
852cbee9f88SPeter Zijlstra 		/*
8537014887aSDavid Hildenbrand 		 * numa_next_scan is the next time that PTEs will be remapped
8547014887aSDavid Hildenbrand 		 * PROT_NONE to trigger NUMA hinting faults; such faults gather
8557014887aSDavid Hildenbrand 		 * statistics and migrate pages to new nodes if necessary.
856cbee9f88SPeter Zijlstra 		 */
857cbee9f88SPeter Zijlstra 		unsigned long numa_next_scan;
858cbee9f88SPeter Zijlstra 
8597014887aSDavid Hildenbrand 		/* Restart point for scanning and remapping PTEs. */
8606e5fb223SPeter Zijlstra 		unsigned long numa_scan_offset;
8616e5fb223SPeter Zijlstra 
8627014887aSDavid Hildenbrand 		/* numa_scan_seq prevents two threads remapping PTEs. */
863cbee9f88SPeter Zijlstra 		int numa_scan_seq;
864cbee9f88SPeter Zijlstra #endif
86520841405SRik van Riel 		/*
866c1a2f7f0SRik van Riel 		 * An operation with batched TLB flushing is going on. Anything
867c1a2f7f0SRik van Riel 		 * that can move process memory needs to flush the TLB when
8687014887aSDavid Hildenbrand 		 * moving a PROT_NONE mapped page.
86920841405SRik van Riel 		 */
87016af97dcSNadav Amit 		atomic_t tlb_flush_pending;
8713ea27719SMel Gorman #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
8723ea27719SMel Gorman 		/* See flush_tlb_batched_pending() */
8735ee2fa2fSHuang Ying 		atomic_t tlb_flush_batched;
8743ea27719SMel Gorman #endif
875d4b3b638SSrikar Dronamraju 		struct uprobes_state uprobes_state;
8768d491de6SThomas Gleixner #ifdef CONFIG_PREEMPT_RT
8778d491de6SThomas Gleixner 		struct rcu_head delayed_drop;
8788d491de6SThomas Gleixner #endif
8795d317b2bSNaoya Horiguchi #ifdef CONFIG_HUGETLB_PAGE
8805d317b2bSNaoya Horiguchi 		atomic_long_t hugetlb_usage;
8815d317b2bSNaoya Horiguchi #endif
882ec8d7c14SMichal Hocko 		struct work_struct async_put_work;
88352ad9bc6SFenghua Yu 
8847a853c2dSFenghua Yu #ifdef CONFIG_IOMMU_SVA
88552ad9bc6SFenghua Yu 		u32 pasid;
88652ad9bc6SFenghua Yu #endif
88776093853Sxu xin #ifdef CONFIG_KSM
88876093853Sxu xin 		/*
88976093853Sxu xin 		 * Represent how many pages of this process are involved in KSM
8906080d19fSxu xin 		 * merging (not including ksm_zero_pages).
89176093853Sxu xin 		 */
89276093853Sxu xin 		unsigned long ksm_merging_pages;
893cb4df4caSxu xin 		/*
894cb4df4caSxu xin 		 * Represent how many pages are checked for ksm merging
895cb4df4caSxu xin 		 * including merged and not merged.
896cb4df4caSxu xin 		 */
897cb4df4caSxu xin 		unsigned long ksm_rmap_items;
8986080d19fSxu xin 		/*
8996080d19fSxu xin 		 * Represent how many empty pages are merged with kernel zero
9006080d19fSxu xin 		 * pages when enabling KSM use_zero_pages.
9016080d19fSxu xin 		 */
9026080d19fSxu xin 		unsigned long ksm_zero_pages;
9036080d19fSxu xin #endif /* CONFIG_KSM */
904bd74fdaeSYu Zhao #ifdef CONFIG_LRU_GEN
905bd74fdaeSYu Zhao 		struct {
906bd74fdaeSYu Zhao 			/* this mm_struct is on lru_gen_mm_list */
907bd74fdaeSYu Zhao 			struct list_head list;
908bd74fdaeSYu Zhao 			/*
909bd74fdaeSYu Zhao 			 * Set when switching to this mm_struct, as a hint of
910bd74fdaeSYu Zhao 			 * whether it has been used since the last time per-node
911bd74fdaeSYu Zhao 			 * page table walkers cleared the corresponding bits.
912bd74fdaeSYu Zhao 			 */
913bd74fdaeSYu Zhao 			unsigned long bitmap;
914bd74fdaeSYu Zhao #ifdef CONFIG_MEMCG
915bd74fdaeSYu Zhao 			/* points to the memcg of "owner" above */
916bd74fdaeSYu Zhao 			struct mem_cgroup *memcg;
917bd74fdaeSYu Zhao #endif
918bd74fdaeSYu Zhao 		} lru_gen;
919bd74fdaeSYu Zhao #endif /* CONFIG_LRU_GEN */
9203859a271SKees Cook 	} __randomize_layout;
921c92ff1bdSMartin Schwidefsky 
922c1a2f7f0SRik van Riel 	/*
923c1a2f7f0SRik van Riel 	 * The mm_cpumask needs to be at the end of mm_struct, because it
924c1a2f7f0SRik van Riel 	 * is dynamically sized based on nr_cpu_ids.
925c1a2f7f0SRik van Riel 	 */
926c1a2f7f0SRik van Riel 	unsigned long cpu_bitmap[];
927c1a2f7f0SRik van Riel };
928c1a2f7f0SRik van Riel 
9293dd44325SLiam R. Howlett #define MM_MT_FLAGS	(MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
9303dd44325SLiam R. Howlett 			 MT_FLAGS_USE_RCU)
931abe722a1SIngo Molnar extern struct mm_struct init_mm;
932abe722a1SIngo Molnar 
933c1a2f7f0SRik van Riel /* Pointer magic because the dynamic array size confuses some compilers. */
mm_init_cpumask(struct mm_struct * mm)9346345d24dSLinus Torvalds static inline void mm_init_cpumask(struct mm_struct *mm)
9356345d24dSLinus Torvalds {
936c1a2f7f0SRik van Riel 	unsigned long cpu_bitmap = (unsigned long)mm;
937c1a2f7f0SRik van Riel 
938c1a2f7f0SRik van Riel 	cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
939c1a2f7f0SRik van Riel 	cpumask_clear((struct cpumask *)cpu_bitmap);
9406345d24dSLinus Torvalds }
9416345d24dSLinus Torvalds 
94245e575abSRusty Russell /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
mm_cpumask(struct mm_struct * mm)943de03c72cSKOSAKI Motohiro static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
944de03c72cSKOSAKI Motohiro {
945c1a2f7f0SRik van Riel 	return (struct cpumask *)&mm->cpu_bitmap;
946de03c72cSKOSAKI Motohiro }
94745e575abSRusty Russell 
948bd74fdaeSYu Zhao #ifdef CONFIG_LRU_GEN
949bd74fdaeSYu Zhao 
950bd74fdaeSYu Zhao struct lru_gen_mm_list {
951bd74fdaeSYu Zhao 	/* mm_struct list for page table walkers */
952bd74fdaeSYu Zhao 	struct list_head fifo;
953bd74fdaeSYu Zhao 	/* protects the list above */
954bd74fdaeSYu Zhao 	spinlock_t lock;
955bd74fdaeSYu Zhao };
956bd74fdaeSYu Zhao 
957bd74fdaeSYu Zhao void lru_gen_add_mm(struct mm_struct *mm);
958bd74fdaeSYu Zhao void lru_gen_del_mm(struct mm_struct *mm);
959bd74fdaeSYu Zhao #ifdef CONFIG_MEMCG
960bd74fdaeSYu Zhao void lru_gen_migrate_mm(struct mm_struct *mm);
961bd74fdaeSYu Zhao #endif
962bd74fdaeSYu Zhao 
lru_gen_init_mm(struct mm_struct * mm)963bd74fdaeSYu Zhao static inline void lru_gen_init_mm(struct mm_struct *mm)
964bd74fdaeSYu Zhao {
965bd74fdaeSYu Zhao 	INIT_LIST_HEAD(&mm->lru_gen.list);
966bd74fdaeSYu Zhao 	mm->lru_gen.bitmap = 0;
967bd74fdaeSYu Zhao #ifdef CONFIG_MEMCG
968bd74fdaeSYu Zhao 	mm->lru_gen.memcg = NULL;
969bd74fdaeSYu Zhao #endif
970bd74fdaeSYu Zhao }
971bd74fdaeSYu Zhao 
lru_gen_use_mm(struct mm_struct * mm)972bd74fdaeSYu Zhao static inline void lru_gen_use_mm(struct mm_struct *mm)
973bd74fdaeSYu Zhao {
974bd74fdaeSYu Zhao 	/*
975bd74fdaeSYu Zhao 	 * When the bitmap is set, page reclaim knows this mm_struct has been
976bd74fdaeSYu Zhao 	 * used since the last time it cleared the bitmap. So it might be worth
977bd74fdaeSYu Zhao 	 * walking the page tables of this mm_struct to clear the accessed bit.
978bd74fdaeSYu Zhao 	 */
979bd74fdaeSYu Zhao 	WRITE_ONCE(mm->lru_gen.bitmap, -1);
980bd74fdaeSYu Zhao }
981bd74fdaeSYu Zhao 
982bd74fdaeSYu Zhao #else /* !CONFIG_LRU_GEN */
983bd74fdaeSYu Zhao 
lru_gen_add_mm(struct mm_struct * mm)984bd74fdaeSYu Zhao static inline void lru_gen_add_mm(struct mm_struct *mm)
985bd74fdaeSYu Zhao {
986bd74fdaeSYu Zhao }
987bd74fdaeSYu Zhao 
lru_gen_del_mm(struct mm_struct * mm)988bd74fdaeSYu Zhao static inline void lru_gen_del_mm(struct mm_struct *mm)
989bd74fdaeSYu Zhao {
990bd74fdaeSYu Zhao }
991bd74fdaeSYu Zhao 
992bd74fdaeSYu Zhao #ifdef CONFIG_MEMCG
lru_gen_migrate_mm(struct mm_struct * mm)993bd74fdaeSYu Zhao static inline void lru_gen_migrate_mm(struct mm_struct *mm)
994bd74fdaeSYu Zhao {
995bd74fdaeSYu Zhao }
996bd74fdaeSYu Zhao #endif
997bd74fdaeSYu Zhao 
lru_gen_init_mm(struct mm_struct * mm)998bd74fdaeSYu Zhao static inline void lru_gen_init_mm(struct mm_struct *mm)
999bd74fdaeSYu Zhao {
1000bd74fdaeSYu Zhao }
1001bd74fdaeSYu Zhao 
lru_gen_use_mm(struct mm_struct * mm)1002bd74fdaeSYu Zhao static inline void lru_gen_use_mm(struct mm_struct *mm)
1003bd74fdaeSYu Zhao {
1004bd74fdaeSYu Zhao }
1005bd74fdaeSYu Zhao 
1006bd74fdaeSYu Zhao #endif /* CONFIG_LRU_GEN */
1007bd74fdaeSYu Zhao 
1008f39af059SMatthew Wilcox (Oracle) struct vma_iterator {
1009f39af059SMatthew Wilcox (Oracle) 	struct ma_state mas;
1010f39af059SMatthew Wilcox (Oracle) };
1011f39af059SMatthew Wilcox (Oracle) 
1012f39af059SMatthew Wilcox (Oracle) #define VMA_ITERATOR(name, __mm, __addr)				\
1013f39af059SMatthew Wilcox (Oracle) 	struct vma_iterator name = {					\
1014f39af059SMatthew Wilcox (Oracle) 		.mas = {						\
1015f39af059SMatthew Wilcox (Oracle) 			.tree = &(__mm)->mm_mt,				\
1016f39af059SMatthew Wilcox (Oracle) 			.index = __addr,				\
1017f39af059SMatthew Wilcox (Oracle) 			.node = MAS_START,				\
1018f39af059SMatthew Wilcox (Oracle) 		},							\
1019f39af059SMatthew Wilcox (Oracle) 	}
1020f39af059SMatthew Wilcox (Oracle) 
vma_iter_init(struct vma_iterator * vmi,struct mm_struct * mm,unsigned long addr)1021f39af059SMatthew Wilcox (Oracle) static inline void vma_iter_init(struct vma_iterator *vmi,
1022f39af059SMatthew Wilcox (Oracle) 		struct mm_struct *mm, unsigned long addr)
1023f39af059SMatthew Wilcox (Oracle) {
1024b62b633eSLiam R. Howlett 	mas_init(&vmi->mas, &mm->mm_mt, addr);
1025f39af059SMatthew Wilcox (Oracle) }
1026f39af059SMatthew Wilcox (Oracle) 
1027af7f588dSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID
1028223baf9dSMathieu Desnoyers 
1029223baf9dSMathieu Desnoyers enum mm_cid_state {
1030223baf9dSMathieu Desnoyers 	MM_CID_UNSET = -1U,		/* Unset state has lazy_put flag set. */
1031223baf9dSMathieu Desnoyers 	MM_CID_LAZY_PUT = (1U << 31),
1032223baf9dSMathieu Desnoyers };
1033223baf9dSMathieu Desnoyers 
mm_cid_is_unset(int cid)1034223baf9dSMathieu Desnoyers static inline bool mm_cid_is_unset(int cid)
1035223baf9dSMathieu Desnoyers {
1036223baf9dSMathieu Desnoyers 	return cid == MM_CID_UNSET;
1037223baf9dSMathieu Desnoyers }
1038223baf9dSMathieu Desnoyers 
mm_cid_is_lazy_put(int cid)1039223baf9dSMathieu Desnoyers static inline bool mm_cid_is_lazy_put(int cid)
1040223baf9dSMathieu Desnoyers {
1041223baf9dSMathieu Desnoyers 	return !mm_cid_is_unset(cid) && (cid & MM_CID_LAZY_PUT);
1042223baf9dSMathieu Desnoyers }
1043223baf9dSMathieu Desnoyers 
mm_cid_is_valid(int cid)1044223baf9dSMathieu Desnoyers static inline bool mm_cid_is_valid(int cid)
1045223baf9dSMathieu Desnoyers {
1046223baf9dSMathieu Desnoyers 	return !(cid & MM_CID_LAZY_PUT);
1047223baf9dSMathieu Desnoyers }
1048223baf9dSMathieu Desnoyers 
mm_cid_set_lazy_put(int cid)1049223baf9dSMathieu Desnoyers static inline int mm_cid_set_lazy_put(int cid)
1050223baf9dSMathieu Desnoyers {
1051223baf9dSMathieu Desnoyers 	return cid | MM_CID_LAZY_PUT;
1052223baf9dSMathieu Desnoyers }
1053223baf9dSMathieu Desnoyers 
mm_cid_clear_lazy_put(int cid)1054223baf9dSMathieu Desnoyers static inline int mm_cid_clear_lazy_put(int cid)
1055223baf9dSMathieu Desnoyers {
1056223baf9dSMathieu Desnoyers 	return cid & ~MM_CID_LAZY_PUT;
1057223baf9dSMathieu Desnoyers }
1058223baf9dSMathieu Desnoyers 
1059af7f588dSMathieu Desnoyers /* Accessor for struct mm_struct's cidmask. */
mm_cidmask(struct mm_struct * mm)1060af7f588dSMathieu Desnoyers static inline cpumask_t *mm_cidmask(struct mm_struct *mm)
1061af7f588dSMathieu Desnoyers {
1062af7f588dSMathieu Desnoyers 	unsigned long cid_bitmap = (unsigned long)mm;
1063af7f588dSMathieu Desnoyers 
1064af7f588dSMathieu Desnoyers 	cid_bitmap += offsetof(struct mm_struct, cpu_bitmap);
1065af7f588dSMathieu Desnoyers 	/* Skip cpu_bitmap */
1066af7f588dSMathieu Desnoyers 	cid_bitmap += cpumask_size();
1067af7f588dSMathieu Desnoyers 	return (struct cpumask *)cid_bitmap;
1068af7f588dSMathieu Desnoyers }
1069af7f588dSMathieu Desnoyers 
mm_init_cid(struct mm_struct * mm)1070af7f588dSMathieu Desnoyers static inline void mm_init_cid(struct mm_struct *mm)
1071af7f588dSMathieu Desnoyers {
1072223baf9dSMathieu Desnoyers 	int i;
1073223baf9dSMathieu Desnoyers 
1074223baf9dSMathieu Desnoyers 	for_each_possible_cpu(i) {
1075223baf9dSMathieu Desnoyers 		struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, i);
1076223baf9dSMathieu Desnoyers 
1077223baf9dSMathieu Desnoyers 		pcpu_cid->cid = MM_CID_UNSET;
1078223baf9dSMathieu Desnoyers 		pcpu_cid->time = 0;
1079223baf9dSMathieu Desnoyers 	}
1080af7f588dSMathieu Desnoyers 	cpumask_clear(mm_cidmask(mm));
1081af7f588dSMathieu Desnoyers }
1082af7f588dSMathieu Desnoyers 
mm_alloc_cid(struct mm_struct * mm)1083223baf9dSMathieu Desnoyers static inline int mm_alloc_cid(struct mm_struct *mm)
1084223baf9dSMathieu Desnoyers {
1085223baf9dSMathieu Desnoyers 	mm->pcpu_cid = alloc_percpu(struct mm_cid);
1086223baf9dSMathieu Desnoyers 	if (!mm->pcpu_cid)
1087223baf9dSMathieu Desnoyers 		return -ENOMEM;
1088223baf9dSMathieu Desnoyers 	mm_init_cid(mm);
1089223baf9dSMathieu Desnoyers 	return 0;
1090223baf9dSMathieu Desnoyers }
1091223baf9dSMathieu Desnoyers 
mm_destroy_cid(struct mm_struct * mm)1092223baf9dSMathieu Desnoyers static inline void mm_destroy_cid(struct mm_struct *mm)
1093223baf9dSMathieu Desnoyers {
1094223baf9dSMathieu Desnoyers 	free_percpu(mm->pcpu_cid);
1095223baf9dSMathieu Desnoyers 	mm->pcpu_cid = NULL;
1096223baf9dSMathieu Desnoyers }
1097223baf9dSMathieu Desnoyers 
mm_cid_size(void)1098af7f588dSMathieu Desnoyers static inline unsigned int mm_cid_size(void)
1099af7f588dSMathieu Desnoyers {
1100af7f588dSMathieu Desnoyers 	return cpumask_size();
1101af7f588dSMathieu Desnoyers }
1102af7f588dSMathieu Desnoyers #else /* CONFIG_SCHED_MM_CID */
mm_init_cid(struct mm_struct * mm)1103af7f588dSMathieu Desnoyers static inline void mm_init_cid(struct mm_struct *mm) { }
mm_alloc_cid(struct mm_struct * mm)1104223baf9dSMathieu Desnoyers static inline int mm_alloc_cid(struct mm_struct *mm) { return 0; }
mm_destroy_cid(struct mm_struct * mm)1105223baf9dSMathieu Desnoyers static inline void mm_destroy_cid(struct mm_struct *mm) { }
mm_cid_size(void)1106af7f588dSMathieu Desnoyers static inline unsigned int mm_cid_size(void)
1107af7f588dSMathieu Desnoyers {
1108af7f588dSMathieu Desnoyers 	return 0;
1109af7f588dSMathieu Desnoyers }
1110af7f588dSMathieu Desnoyers #endif /* CONFIG_SCHED_MM_CID */
1111af7f588dSMathieu Desnoyers 
111256236a59SMinchan Kim struct mmu_gather;
1113a72afd87SWill Deacon extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
1114d8b45053SWill Deacon extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
1115ae8eba8bSWill Deacon extern void tlb_finish_mmu(struct mmu_gather *tlb);
111656236a59SMinchan Kim 
1117f872f540SAndy Lutomirski struct vm_fault;
1118f872f540SAndy Lutomirski 
11193d353901SSouptick Joarder /**
11203d353901SSouptick Joarder  * typedef vm_fault_t - Return type for page fault handlers.
11213d353901SSouptick Joarder  *
11223d353901SSouptick Joarder  * Page fault handlers return a bitmask of %VM_FAULT values.
11233d353901SSouptick Joarder  */
11243d353901SSouptick Joarder typedef __bitwise unsigned int vm_fault_t;
11253d353901SSouptick Joarder 
11263d353901SSouptick Joarder /**
11273d353901SSouptick Joarder  * enum vm_fault_reason - Page fault handlers return a bitmask of
11283d353901SSouptick Joarder  * these values to tell the core VM what happened when handling the
11293d353901SSouptick Joarder  * fault. Used to decide whether a process gets delivered SIGBUS or
11303d353901SSouptick Joarder  * just gets major/minor fault counters bumped up.
11313d353901SSouptick Joarder  *
11323d353901SSouptick Joarder  * @VM_FAULT_OOM:		Out Of Memory
11333d353901SSouptick Joarder  * @VM_FAULT_SIGBUS:		Bad access
11343d353901SSouptick Joarder  * @VM_FAULT_MAJOR:		Page read from storage
11353d353901SSouptick Joarder  * @VM_FAULT_HWPOISON:		Hit poisoned small page
11363d353901SSouptick Joarder  * @VM_FAULT_HWPOISON_LARGE:	Hit poisoned large page. Index encoded
11373d353901SSouptick Joarder  *				in upper bits
11383d353901SSouptick Joarder  * @VM_FAULT_SIGSEGV:		segmentation fault
11393d353901SSouptick Joarder  * @VM_FAULT_NOPAGE:		->fault installed the pte, not return page
11403d353901SSouptick Joarder  * @VM_FAULT_LOCKED:		->fault locked the returned page
11413d353901SSouptick Joarder  * @VM_FAULT_RETRY:		->fault blocked, must retry
11423d353901SSouptick Joarder  * @VM_FAULT_FALLBACK:		huge page fault failed, fall back to small
11433d353901SSouptick Joarder  * @VM_FAULT_DONE_COW:		->fault has fully handled COW
11443d353901SSouptick Joarder  * @VM_FAULT_NEEDDSYNC:		->fault did not modify page tables and needs
11453d353901SSouptick Joarder  *				fsync() to complete (for synchronous page faults
11463d353901SSouptick Joarder  *				in DAX)
1147d9272525SPeter Xu  * @VM_FAULT_COMPLETED:		->fault completed, meanwhile mmap lock released
11483d353901SSouptick Joarder  * @VM_FAULT_HINDEX_MASK:	mask HINDEX value
11493d353901SSouptick Joarder  *
11503d353901SSouptick Joarder  */
11513d353901SSouptick Joarder enum vm_fault_reason {
11523d353901SSouptick Joarder 	VM_FAULT_OOM            = (__force vm_fault_t)0x000001,
11533d353901SSouptick Joarder 	VM_FAULT_SIGBUS         = (__force vm_fault_t)0x000002,
11543d353901SSouptick Joarder 	VM_FAULT_MAJOR          = (__force vm_fault_t)0x000004,
11553d353901SSouptick Joarder 	VM_FAULT_HWPOISON       = (__force vm_fault_t)0x000010,
11563d353901SSouptick Joarder 	VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
11573d353901SSouptick Joarder 	VM_FAULT_SIGSEGV        = (__force vm_fault_t)0x000040,
11583d353901SSouptick Joarder 	VM_FAULT_NOPAGE         = (__force vm_fault_t)0x000100,
11593d353901SSouptick Joarder 	VM_FAULT_LOCKED         = (__force vm_fault_t)0x000200,
11603d353901SSouptick Joarder 	VM_FAULT_RETRY          = (__force vm_fault_t)0x000400,
11613d353901SSouptick Joarder 	VM_FAULT_FALLBACK       = (__force vm_fault_t)0x000800,
11623d353901SSouptick Joarder 	VM_FAULT_DONE_COW       = (__force vm_fault_t)0x001000,
11633d353901SSouptick Joarder 	VM_FAULT_NEEDDSYNC      = (__force vm_fault_t)0x002000,
1164d9272525SPeter Xu 	VM_FAULT_COMPLETED      = (__force vm_fault_t)0x004000,
11653d353901SSouptick Joarder 	VM_FAULT_HINDEX_MASK    = (__force vm_fault_t)0x0f0000,
11663d353901SSouptick Joarder };
11673d353901SSouptick Joarder 
11683d353901SSouptick Joarder /* Encode hstate index for a hwpoisoned large page */
11693d353901SSouptick Joarder #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
1170fcae96ffSJann Horn #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
11713d353901SSouptick Joarder 
11723d353901SSouptick Joarder #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS |	\
11733d353901SSouptick Joarder 			VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON |	\
11743d353901SSouptick Joarder 			VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
11753d353901SSouptick Joarder 
11763d353901SSouptick Joarder #define VM_FAULT_RESULT_TRACE \
11773d353901SSouptick Joarder 	{ VM_FAULT_OOM,                 "OOM" },	\
11783d353901SSouptick Joarder 	{ VM_FAULT_SIGBUS,              "SIGBUS" },	\
11793d353901SSouptick Joarder 	{ VM_FAULT_MAJOR,               "MAJOR" },	\
11803d353901SSouptick Joarder 	{ VM_FAULT_HWPOISON,            "HWPOISON" },	\
11813d353901SSouptick Joarder 	{ VM_FAULT_HWPOISON_LARGE,      "HWPOISON_LARGE" },	\
11823d353901SSouptick Joarder 	{ VM_FAULT_SIGSEGV,             "SIGSEGV" },	\
11833d353901SSouptick Joarder 	{ VM_FAULT_NOPAGE,              "NOPAGE" },	\
11843d353901SSouptick Joarder 	{ VM_FAULT_LOCKED,              "LOCKED" },	\
11853d353901SSouptick Joarder 	{ VM_FAULT_RETRY,               "RETRY" },	\
11863d353901SSouptick Joarder 	{ VM_FAULT_FALLBACK,            "FALLBACK" },	\
11873d353901SSouptick Joarder 	{ VM_FAULT_DONE_COW,            "DONE_COW" },	\
11887a32b58bSSuren Baghdasaryan 	{ VM_FAULT_NEEDDSYNC,           "NEEDDSYNC" },	\
11897a32b58bSSuren Baghdasaryan 	{ VM_FAULT_COMPLETED,           "COMPLETED" }
11903d353901SSouptick Joarder 
1191f872f540SAndy Lutomirski struct vm_special_mapping {
1192f872f540SAndy Lutomirski 	const char *name;	/* The name, e.g. "[vdso]". */
1193f872f540SAndy Lutomirski 
1194f872f540SAndy Lutomirski 	/*
1195f872f540SAndy Lutomirski 	 * If .fault is not provided, this points to a
1196f872f540SAndy Lutomirski 	 * NULL-terminated array of pages that back the special mapping.
1197f872f540SAndy Lutomirski 	 *
1198f872f540SAndy Lutomirski 	 * This must not be NULL unless .fault is provided.
1199f872f540SAndy Lutomirski 	 */
1200a62c34bdSAndy Lutomirski 	struct page **pages;
1201f872f540SAndy Lutomirski 
1202f872f540SAndy Lutomirski 	/*
1203f872f540SAndy Lutomirski 	 * If non-NULL, then this is called to resolve page faults
1204f872f540SAndy Lutomirski 	 * on the special mapping.  If used, .pages is not checked.
1205f872f540SAndy Lutomirski 	 */
1206b3ec9f33SSouptick Joarder 	vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1207f872f540SAndy Lutomirski 				struct vm_area_struct *vma,
1208f872f540SAndy Lutomirski 				struct vm_fault *vmf);
1209b059a453SDmitry Safonov 
1210b059a453SDmitry Safonov 	int (*mremap)(const struct vm_special_mapping *sm,
1211b059a453SDmitry Safonov 		     struct vm_area_struct *new_vma);
1212a62c34bdSAndy Lutomirski };
1213a62c34bdSAndy Lutomirski 
1214d17d8f9dSDave Hansen enum tlb_flush_reason {
1215d17d8f9dSDave Hansen 	TLB_FLUSH_ON_TASK_SWITCH,
1216d17d8f9dSDave Hansen 	TLB_REMOTE_SHOOTDOWN,
1217d17d8f9dSDave Hansen 	TLB_LOCAL_SHOOTDOWN,
1218d17d8f9dSDave Hansen 	TLB_LOCAL_MM_SHOOTDOWN,
12195b74283aSMel Gorman 	TLB_REMOTE_SEND_IPI,
1220d17d8f9dSDave Hansen 	NR_TLB_FLUSH_REASONS,
1221d17d8f9dSDave Hansen };
1222d17d8f9dSDave Hansen 
122336090defSArnd Bergmann /**
122436090defSArnd Bergmann  * enum fault_flag - Fault flag definitions.
122536090defSArnd Bergmann  * @FAULT_FLAG_WRITE: Fault was a write fault.
122636090defSArnd Bergmann  * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
122736090defSArnd Bergmann  * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
122836090defSArnd Bergmann  * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
122936090defSArnd Bergmann  * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
123036090defSArnd Bergmann  * @FAULT_FLAG_TRIED: The fault has been tried once.
123136090defSArnd Bergmann  * @FAULT_FLAG_USER: The fault originated in userspace.
123236090defSArnd Bergmann  * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
123336090defSArnd Bergmann  * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
123436090defSArnd Bergmann  * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
12358d6a0ac0SDavid Hildenbrand  * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
12368d6a0ac0SDavid Hildenbrand  *                      COW mapping, making sure that an exclusive anon page is
12378d6a0ac0SDavid Hildenbrand  *                      mapped after the fault.
1238f46f2adeSPeter Xu  * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
1239f46f2adeSPeter Xu  *                        We should only access orig_pte if this flag set.
124055324e46SSuren Baghdasaryan  * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock.
124136090defSArnd Bergmann  *
124236090defSArnd Bergmann  * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
124336090defSArnd Bergmann  * whether we would allow page faults to retry by specifying these two
124436090defSArnd Bergmann  * fault flags correctly.  Currently there can be three legal combinations:
124536090defSArnd Bergmann  *
124636090defSArnd Bergmann  * (a) ALLOW_RETRY and !TRIED:  this means the page fault allows retry, and
124736090defSArnd Bergmann  *                              this is the first try
124836090defSArnd Bergmann  *
124936090defSArnd Bergmann  * (b) ALLOW_RETRY and TRIED:   this means the page fault allows retry, and
125036090defSArnd Bergmann  *                              we've already tried at least once
125136090defSArnd Bergmann  *
125236090defSArnd Bergmann  * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
125336090defSArnd Bergmann  *
125436090defSArnd Bergmann  * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
125536090defSArnd Bergmann  * be used.  Note that page faults can be allowed to retry for multiple times,
125636090defSArnd Bergmann  * in which case we'll have an initial fault with flags (a) then later on
125736090defSArnd Bergmann  * continuous faults with flags (b).  We should always try to detect pending
125836090defSArnd Bergmann  * signals before a retry to make sure the continuous page faults can still be
125936090defSArnd Bergmann  * interrupted if necessary.
1260c89357e2SDavid Hildenbrand  *
1261c89357e2SDavid Hildenbrand  * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
1262c89357e2SDavid Hildenbrand  * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
12638d6a0ac0SDavid Hildenbrand  * applied to mappings that are not COW mappings.
126436090defSArnd Bergmann  */
126536090defSArnd Bergmann enum fault_flag {
126636090defSArnd Bergmann 	FAULT_FLAG_WRITE =		1 << 0,
126736090defSArnd Bergmann 	FAULT_FLAG_MKWRITE =		1 << 1,
126836090defSArnd Bergmann 	FAULT_FLAG_ALLOW_RETRY =	1 << 2,
126936090defSArnd Bergmann 	FAULT_FLAG_RETRY_NOWAIT = 	1 << 3,
127036090defSArnd Bergmann 	FAULT_FLAG_KILLABLE =		1 << 4,
127136090defSArnd Bergmann 	FAULT_FLAG_TRIED = 		1 << 5,
127236090defSArnd Bergmann 	FAULT_FLAG_USER =		1 << 6,
127336090defSArnd Bergmann 	FAULT_FLAG_REMOTE =		1 << 7,
127436090defSArnd Bergmann 	FAULT_FLAG_INSTRUCTION =	1 << 8,
127536090defSArnd Bergmann 	FAULT_FLAG_INTERRUPTIBLE =	1 << 9,
1276c89357e2SDavid Hildenbrand 	FAULT_FLAG_UNSHARE =		1 << 10,
1277f46f2adeSPeter Xu 	FAULT_FLAG_ORIG_PTE_VALID =	1 << 11,
127855324e46SSuren Baghdasaryan 	FAULT_FLAG_VMA_LOCK =		1 << 12,
127936090defSArnd Bergmann };
128036090defSArnd Bergmann 
128105e90bd0SPeter Xu typedef unsigned int __bitwise zap_flags_t;
128205e90bd0SPeter Xu 
1283b5054174SDavid Howells /*
1284b5054174SDavid Howells  * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
1285b5054174SDavid Howells  * other. Here is what they mean, and how to use them:
1286b5054174SDavid Howells  *
1287b5054174SDavid Howells  *
1288b5054174SDavid Howells  * FIXME: For pages which are part of a filesystem, mappings are subject to the
1289b5054174SDavid Howells  * lifetime enforced by the filesystem and we need guarantees that longterm
1290b5054174SDavid Howells  * users like RDMA and V4L2 only establish mappings which coordinate usage with
1291b5054174SDavid Howells  * the filesystem.  Ideas for this coordination include revoking the longterm
1292b5054174SDavid Howells  * pin, delaying writeback, bounce buffer page writeback, etc.  As FS DAX was
1293b5054174SDavid Howells  * added after the problem with filesystems was found FS DAX VMAs are
1294b5054174SDavid Howells  * specifically failed.  Filesystem pages are still subject to bugs and use of
1295b5054174SDavid Howells  * FOLL_LONGTERM should be avoided on those pages.
1296b5054174SDavid Howells  *
1297b5054174SDavid Howells  * In the CMA case: long term pins in a CMA region would unnecessarily fragment
1298b5054174SDavid Howells  * that region.  And so, CMA attempts to migrate the page before pinning, when
1299b5054174SDavid Howells  * FOLL_LONGTERM is specified.
1300b5054174SDavid Howells  *
1301b5054174SDavid Howells  * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
1302b5054174SDavid Howells  * but an additional pin counting system) will be invoked. This is intended for
1303b5054174SDavid Howells  * anything that gets a page reference and then touches page data (for example,
1304b5054174SDavid Howells  * Direct IO). This lets the filesystem know that some non-file-system entity is
1305b5054174SDavid Howells  * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
1306b5054174SDavid Howells  * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
1307b5054174SDavid Howells  * a call to unpin_user_page().
1308b5054174SDavid Howells  *
1309b5054174SDavid Howells  * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
1310b5054174SDavid Howells  * and separate refcounting mechanisms, however, and that means that each has
1311b5054174SDavid Howells  * its own acquire and release mechanisms:
1312b5054174SDavid Howells  *
1313b5054174SDavid Howells  *     FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
1314b5054174SDavid Howells  *
1315b5054174SDavid Howells  *     FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
1316b5054174SDavid Howells  *
1317b5054174SDavid Howells  * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
1318b5054174SDavid Howells  * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
1319b5054174SDavid Howells  * calls applied to them, and that's perfectly OK. This is a constraint on the
1320b5054174SDavid Howells  * callers, not on the pages.)
1321b5054174SDavid Howells  *
1322b5054174SDavid Howells  * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
1323b5054174SDavid Howells  * directly by the caller. That's in order to help avoid mismatches when
1324b5054174SDavid Howells  * releasing pages: get_user_pages*() pages must be released via put_page(),
1325b5054174SDavid Howells  * while pin_user_pages*() pages must be released via unpin_user_page().
1326b5054174SDavid Howells  *
1327b5054174SDavid Howells  * Please see Documentation/core-api/pin_user_pages.rst for more information.
1328b5054174SDavid Howells  */
1329b5054174SDavid Howells 
13302c224108SJason Gunthorpe enum {
13312c224108SJason Gunthorpe 	/* check pte is writable */
13322c224108SJason Gunthorpe 	FOLL_WRITE = 1 << 0,
13332c224108SJason Gunthorpe 	/* do get_page on page */
13342c224108SJason Gunthorpe 	FOLL_GET = 1 << 1,
13352c224108SJason Gunthorpe 	/* give error on hole if it would be zero */
13362c224108SJason Gunthorpe 	FOLL_DUMP = 1 << 2,
13372c224108SJason Gunthorpe 	/* get_user_pages read/write w/o permission */
13382c224108SJason Gunthorpe 	FOLL_FORCE = 1 << 3,
13392c224108SJason Gunthorpe 	/*
13402c224108SJason Gunthorpe 	 * if a disk transfer is needed, start the IO and return without waiting
13412c224108SJason Gunthorpe 	 * upon it
13422c224108SJason Gunthorpe 	 */
13432c224108SJason Gunthorpe 	FOLL_NOWAIT = 1 << 4,
13442c224108SJason Gunthorpe 	/* do not fault in pages */
13452c224108SJason Gunthorpe 	FOLL_NOFAULT = 1 << 5,
13462c224108SJason Gunthorpe 	/* check page is hwpoisoned */
13472c224108SJason Gunthorpe 	FOLL_HWPOISON = 1 << 6,
13482c224108SJason Gunthorpe 	/* don't do file mappings */
13492c224108SJason Gunthorpe 	FOLL_ANON = 1 << 7,
13502c224108SJason Gunthorpe 	/*
13512c224108SJason Gunthorpe 	 * FOLL_LONGTERM indicates that the page will be held for an indefinite
13522c224108SJason Gunthorpe 	 * time period _often_ under userspace control.  This is in contrast to
13532c224108SJason Gunthorpe 	 * iov_iter_get_pages(), whose usages are transient.
13542c224108SJason Gunthorpe 	 */
13552c224108SJason Gunthorpe 	FOLL_LONGTERM = 1 << 8,
13562c224108SJason Gunthorpe 	/* split huge pmd before returning */
13572c224108SJason Gunthorpe 	FOLL_SPLIT_PMD = 1 << 9,
13582c224108SJason Gunthorpe 	/* allow returning PCI P2PDMA pages */
13592c224108SJason Gunthorpe 	FOLL_PCI_P2PDMA = 1 << 10,
13602c224108SJason Gunthorpe 	/* allow interrupts from generic signals */
13612c224108SJason Gunthorpe 	FOLL_INTERRUPTIBLE = 1 << 11,
1362d74943a2SDavid Hildenbrand 	/*
1363d74943a2SDavid Hildenbrand 	 * Always honor (trigger) NUMA hinting faults.
1364d74943a2SDavid Hildenbrand 	 *
1365d74943a2SDavid Hildenbrand 	 * FOLL_WRITE implicitly honors NUMA hinting faults because a
1366d74943a2SDavid Hildenbrand 	 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE
1367d74943a2SDavid Hildenbrand 	 * apply). get_user_pages_fast_only() always implicitly honors NUMA
1368d74943a2SDavid Hildenbrand 	 * hinting faults.
1369d74943a2SDavid Hildenbrand 	 */
1370d74943a2SDavid Hildenbrand 	FOLL_HONOR_NUMA_FAULT = 1 << 12,
13712c224108SJason Gunthorpe 
13722c224108SJason Gunthorpe 	/* See also internal only FOLL flags in mm/internal.h */
13732c224108SJason Gunthorpe };
1374b5054174SDavid Howells 
13755b99cd0eSHeiko Carstens #endif /* _LINUX_MM_TYPES_H */
1376