xref: /linux-6.15/include/linux/mm_types.h (revision 6af8cb80)
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>
22f35ab95cSSuren Baghdasaryan #include <linux/types.h>
232e58f173SIngo Molnar 
24c92ff1bdSMartin Schwidefsky #include <asm/mmu.h>
255b99cd0eSHeiko Carstens 
264f9a58d7SOlaf Hering #ifndef AT_VECTOR_SIZE_ARCH
274f9a58d7SOlaf Hering #define AT_VECTOR_SIZE_ARCH 0
284f9a58d7SOlaf Hering #endif
294f9a58d7SOlaf Hering #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
304f9a58d7SOlaf Hering 
3182e69a12SFenghua Yu #define INIT_PASID	0
321c8f4220SSouptick Joarder 
335b99cd0eSHeiko Carstens struct address_space;
341306a85aSJohannes Weiner struct mem_cgroup;
355b99cd0eSHeiko Carstens 
365b99cd0eSHeiko Carstens /*
375b99cd0eSHeiko Carstens  * Each physical page in the system has a struct page associated with
385b99cd0eSHeiko Carstens  * it to keep track of whatever it is we are using the page for at the
395b99cd0eSHeiko Carstens  * moment. Note that we have no way to track which tasks are using
405b99cd0eSHeiko Carstens  * a page, though if it is a pagecache page, rmap structures can tell us
4197b4a671SMatthew Wilcox  * who is mapping it.
42be50015dSMatthew Wilcox  *
4397b4a671SMatthew Wilcox  * If you allocate the page using alloc_pages(), you can use some of the
4497b4a671SMatthew Wilcox  * space in struct page for your own purposes.  The five words in the main
4597b4a671SMatthew Wilcox  * union are available, except for bit 0 of the first word which must be
4697b4a671SMatthew Wilcox  * kept clear.  Many users use this word to store a pointer to an object
4797b4a671SMatthew Wilcox  * which is guaranteed to be aligned.  If you use the same storage as
4897b4a671SMatthew Wilcox  * page->mapping, you must restore it to NULL before freeing the page.
4997b4a671SMatthew Wilcox  *
506d21dde7SDavid Hildenbrand  * The mapcount field must not be used for own purposes.
5197b4a671SMatthew Wilcox  *
5297b4a671SMatthew Wilcox  * If you want to use the refcount field, it must be used in such a way
5397b4a671SMatthew Wilcox  * that other CPUs temporarily incrementing and then decrementing the
54be50015dSMatthew Wilcox  * refcount does not cause problems.  On receiving the page from
55be50015dSMatthew Wilcox  * alloc_pages(), the refcount will be positive.
56be50015dSMatthew Wilcox  *
5797b4a671SMatthew Wilcox  * If you allocate pages of order > 0, you can use some of the fields
5897b4a671SMatthew Wilcox  * in each subpage, but you may need to restore some of their values
5997b4a671SMatthew Wilcox  * afterwards.
60fc9bb8c7SChristoph Lameter  *
61d122019bSMatthew Wilcox (Oracle)  * SLUB uses cmpxchg_double() to atomically update its freelist and counters.
62d122019bSMatthew Wilcox (Oracle)  * That requires that freelist & counters in struct slab be adjacent and
63d122019bSMatthew Wilcox (Oracle)  * double-word aligned. Because struct slab currently just reinterprets the
64d122019bSMatthew Wilcox (Oracle)  * bits of struct page, we align all struct pages to double-word boundaries,
65d122019bSMatthew Wilcox (Oracle)  * and ensure that 'freelist' is aligned within struct slab.
665b99cd0eSHeiko Carstens  */
67e20df2c6SMatthew Wilcox #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
68e20df2c6SMatthew Wilcox #define _struct_page_alignment	__aligned(2 * sizeof(unsigned long))
69e20df2c6SMatthew Wilcox #else
7070fb4fdfSLinus Torvalds #define _struct_page_alignment	__aligned(sizeof(unsigned long))
717d27a04bSMatthew Wilcox #endif
72e20df2c6SMatthew Wilcox 
735b99cd0eSHeiko Carstens struct page {
745b99cd0eSHeiko Carstens 	unsigned long flags;		/* Atomic flags, some possibly
755b99cd0eSHeiko Carstens 					 * updated asynchronously */
76b7ccc7f8SMatthew Wilcox 	/*
774da1984eSMatthew Wilcox 	 * Five words (20/40 bytes) are available in this union.
784da1984eSMatthew Wilcox 	 * WARNING: bit 0 of the first word is used for PageTail(). That
794da1984eSMatthew Wilcox 	 * means the other users of this union MUST NOT use the bit to
80b7ccc7f8SMatthew Wilcox 	 * avoid collision and false-positive PageTail().
81b7ccc7f8SMatthew Wilcox 	 */
82b7ccc7f8SMatthew Wilcox 	union {
834da1984eSMatthew Wilcox 		struct {	/* Page cache and anonymous pages */
844da1984eSMatthew Wilcox 			/**
854da1984eSMatthew Wilcox 			 * @lru: Pageout list, eg. active_list protected by
8615b44736SHugh Dickins 			 * lruvec->lru_lock.  Sometimes used as a generic list
87b7ccc7f8SMatthew Wilcox 			 * by the page owner.
88b7ccc7f8SMatthew Wilcox 			 */
8907ca7606SHugh Dickins 			union {
904da1984eSMatthew Wilcox 				struct list_head lru;
91bf75f200SMel Gorman 
9207ca7606SHugh Dickins 				/* Or, for the Unevictable "LRU list" slot */
9307ca7606SHugh Dickins 				struct {
9407ca7606SHugh Dickins 					/* Always even, to negate PageTail */
9507ca7606SHugh Dickins 					void *__filler;
9607ca7606SHugh Dickins 					/* Count page's or folio's mlocks */
9707ca7606SHugh Dickins 					unsigned int mlock_count;
9807ca7606SHugh Dickins 				};
99bf75f200SMel Gorman 
100bf75f200SMel Gorman 				/* Or, free page */
101bf75f200SMel Gorman 				struct list_head buddy_list;
102bf75f200SMel Gorman 				struct list_head pcp_list;
10307ca7606SHugh Dickins 				struct {
10466a6ffd2SMatthew Wilcox 					struct llist_node pcp_llist;
105b26435a0SMatthew Wilcox 					unsigned int order;
10616900426SShiyang Ruan 				};
107fc9bb8c7SChristoph Lameter 			};
10816900426SShiyang Ruan 			/* See page-flags.h for PAGE_MAPPING_FLAGS */
10916900426SShiyang Ruan 			struct address_space *mapping;
11066a6ffd2SMatthew Wilcox 			union {
11166a6ffd2SMatthew Wilcox 				pgoff_t index;		/* Our offset within mapping. */
11266a6ffd2SMatthew Wilcox 				unsigned long share;	/* share count for fsdax */
11332f51eadSMatthew Wilcox (Oracle) 			};
11466a6ffd2SMatthew Wilcox 			/**
1157d27a04bSMatthew Wilcox 			 * @private: Mapping-private opaque data.
1167d27a04bSMatthew Wilcox 			 * Usually used for buffer_heads if PagePrivate.
11766a6ffd2SMatthew Wilcox 			 * Used for swp_entry_t if swapcache flag set.
118c25fff71SJesper Dangaard Brouer 			 * Indicates order in the buddy system if PageBuddy.
119c25fff71SJesper Dangaard Brouer 			 */
120c07aea3eSMatteo Croce 			unsigned long private;
121c07aea3eSMatteo Croce 		};
122c07aea3eSMatteo Croce 		struct {	/* page_pool used by netstack */
123c07aea3eSMatteo Croce 			/**
124c07aea3eSMatteo Croce 			 * @pp_magic: magic value to avoid recycling non
125c07aea3eSMatteo Croce 			 * page_pool allocated pages.
1260e9d2a0aSYunsheng Lin 			 */
1270a149ab7SLiang Chen 			unsigned long pp_magic;
1280e9d2a0aSYunsheng Lin 			struct page_pool *pp;
1294da1984eSMatthew Wilcox 			unsigned long _pp_mapping_pad;
1304da1984eSMatthew Wilcox 			unsigned long dma_addr;
131dad6a5ebSHugh Dickins 			atomic_long_t pp_ref_count;
13250e7fbc3SMatthew Wilcox 		};
13382ba975eSAlistair Popple 		struct {	/* Tail pages of compound page */
13482ba975eSAlistair Popple 			unsigned long compound_head;	/* Bit zero is set */
13582ba975eSAlistair Popple 		};
13682ba975eSAlistair Popple 		struct {	/* ZONE_DEVICE pages */
13782ba975eSAlistair Popple 			/*
1388a164fefSChristoph Hellwig 			 * The first word is used for compound_head or folio
13976470ccdSRalph Campbell 			 * pgmap
14076470ccdSRalph Campbell 			 */
14176470ccdSRalph Campbell 			void *_unused_pgmap_compound_head;
14276470ccdSRalph Campbell 			void *zone_device_data;
14376470ccdSRalph Campbell 			/*
14476470ccdSRalph Campbell 			 * ZONE_DEVICE private pages are counted as being
14576470ccdSRalph Campbell 			 * mapped so the next 3 words hold the mapping, index,
14676470ccdSRalph Campbell 			 * and private fields from the source anonymous or
14776470ccdSRalph Campbell 			 * page cache page while the page is migrated to device
14876470ccdSRalph Campbell 			 * private memory.
14950e7fbc3SMatthew Wilcox 			 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
1504da1984eSMatthew Wilcox 			 * use the mapping, index, and private fields when
1514da1984eSMatthew Wilcox 			 * pmem backed DAX files are mapped.
1524da1984eSMatthew Wilcox 			 */
15366a6ffd2SMatthew Wilcox 		};
1547d27a04bSMatthew Wilcox 
155b21999daSMatthew Wilcox 		/** @rcu_head: You can use this to free a page by RCU. */
156b21999daSMatthew Wilcox 		struct rcu_head rcu_head;
1576d21dde7SDavid Hildenbrand 	};
1586d21dde7SDavid Hildenbrand 
1596d21dde7SDavid Hildenbrand 	union {		/* This union is 4 bytes in size. */
1606d21dde7SDavid Hildenbrand 		/*
1616d21dde7SDavid Hildenbrand 		 * For head pages of typed folios, the value stored here
1626d21dde7SDavid Hildenbrand 		 * allows for determining what this page is used for. The
1636d21dde7SDavid Hildenbrand 		 * tail pages of typed folios will not store a type
1648db00ad5SDavid Hildenbrand 		 * (page_type == _mapcount == -1).
1658db00ad5SDavid Hildenbrand 		 *
1668db00ad5SDavid Hildenbrand 		 * See page-flags.h for a list of page types which are currently
1678db00ad5SDavid Hildenbrand 		 * stored here.
1688db00ad5SDavid Hildenbrand 		 *
1696e292b9bSMatthew Wilcox 		 * Owners of typed folios may reuse the lower 16 bit of the
1706e292b9bSMatthew Wilcox 		 * head page page_type field after setting the page type,
1716d21dde7SDavid Hildenbrand 		 * but must reset these 16 bit to -1 before clearing the
1726d21dde7SDavid Hildenbrand 		 * page type.
1736d21dde7SDavid Hildenbrand 		 */
1746d21dde7SDavid Hildenbrand 		unsigned int page_type;
1756d21dde7SDavid Hildenbrand 
1766d21dde7SDavid Hildenbrand 		/*
1776d21dde7SDavid Hildenbrand 		 * For pages that are part of non-typed folios for which mappings
1786d21dde7SDavid Hildenbrand 		 * are tracked via the RMAP, encodes the number of times this page
1796d21dde7SDavid Hildenbrand 		 * is directly referenced by a page table.
1806d21dde7SDavid Hildenbrand 		 *
1816d21dde7SDavid Hildenbrand 		 * Note that the mapcount is always initialized to -1, so that
182b21999daSMatthew Wilcox 		 * transitions both from it and to it can be tracked, using
183013e8963SChristoph Lameter 		 * atomic_inc_and_test() and atomic_add_negative(-1).
184b21999daSMatthew Wilcox 		 */
1850139aa7bSJoonsoo Kim 		atomic_t _mapcount;
186fc9bb8c7SChristoph Lameter 	};
187a52c6330SAlex Shi (Tencent) 
188bcfe06bfSRoman Gushchin 	/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
189a52c6330SAlex Shi (Tencent) 	atomic_t _refcount;
190a52c6330SAlex Shi (Tencent) 
1911306a85aSJohannes Weiner #ifdef CONFIG_MEMCG
1921306a85aSJohannes Weiner 	unsigned long memcg_data;
1935b99cd0eSHeiko Carstens #elif defined(CONFIG_SLAB_OBJ_EXT)
1945b99cd0eSHeiko Carstens 	unsigned long _unused_slab_obj_exts;
1955b99cd0eSHeiko Carstens #endif
1965b99cd0eSHeiko Carstens 
1975b99cd0eSHeiko Carstens 	/*
1985b99cd0eSHeiko Carstens 	 * On machines where all RAM is mapped into kernel address space,
1995b99cd0eSHeiko Carstens 	 * we can simply calculate the virtual address. On machines with
2005b99cd0eSHeiko Carstens 	 * highmem some memory is mapped into kernel virtual memory
2015b99cd0eSHeiko Carstens 	 * dynamically, so we need a place to store that address.
2025b99cd0eSHeiko Carstens 	 * Note that this field could be 16 bits on x86 ... ;)
2035b99cd0eSHeiko Carstens 	 *
2045b99cd0eSHeiko Carstens 	 * Architectures with slow multiplication can define
2055b99cd0eSHeiko Carstens 	 * WANT_PAGE_VIRTUAL in asm/page.h
2065b99cd0eSHeiko Carstens 	 */
207dfec072eSVegard Nossum #if defined(WANT_PAGE_VIRTUAL)
2081d44f2e6SKefeng Wang 	void *virtual;			/* Kernel virtual address (NULL if
2091d44f2e6SKefeng Wang 					   not kmapped, ie. highmem) */
2101d44f2e6SKefeng Wang #endif /* WANT_PAGE_VIRTUAL */
2111d44f2e6SKefeng Wang 
212f80be457SAlexander Potapenko #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
213f80be457SAlexander Potapenko 	int _last_cpupid;
214f80be457SAlexander Potapenko #endif
215f80be457SAlexander Potapenko 
216f80be457SAlexander Potapenko #ifdef CONFIG_KMSAN
217f80be457SAlexander Potapenko 	/*
218f80be457SAlexander Potapenko 	 * KMSAN metadata for this page:
219f80be457SAlexander Potapenko 	 *  - shadow page: every bit indicates whether the corresponding
220f80be457SAlexander Potapenko 	 *    bit of the original page is initialized (0) or not (1);
221f80be457SAlexander Potapenko 	 *  - origin page: every 4 bytes contain an id of the stack trace
222f80be457SAlexander Potapenko 	 *    where the uninitialized value was created.
223e20df2c6SMatthew Wilcox 	 */
2245b99cd0eSHeiko Carstens 	struct page *kmsan_shadow;
22570fb4fdfSLinus Torvalds 	struct page *kmsan_origin;
22670fb4fdfSLinus Torvalds #endif
22770fb4fdfSLinus Torvalds } _struct_page_alignment;
22870fb4fdfSLinus Torvalds 
22970fb4fdfSLinus Torvalds /*
230da510964SDavid Hildenbrand  * struct encoded_page - a nonexistent type marking this pointer
231da510964SDavid Hildenbrand  *
23270fb4fdfSLinus Torvalds  * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
23370fb4fdfSLinus Torvalds  * with the low bits of the pointer indicating extra context-dependent
23470fb4fdfSLinus Torvalds  * information. Only used in mmu_gather handling, and this acts as a type
23570fb4fdfSLinus Torvalds  * system check on that use.
23670fb4fdfSLinus Torvalds  *
23770fb4fdfSLinus Torvalds  * We only really have two guaranteed bits in general, although you could
23870fb4fdfSLinus Torvalds  * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
23970fb4fdfSLinus Torvalds  * for more.
240da510964SDavid Hildenbrand  *
241da510964SDavid Hildenbrand  * Use the supplied helper functions to endcode/decode the pointer and bits.
242da510964SDavid Hildenbrand  */
243da510964SDavid Hildenbrand struct encoded_page;
244da510964SDavid Hildenbrand 
245da510964SDavid Hildenbrand #define ENCODED_PAGE_BITS			3ul
246d7f861b9SDavid Hildenbrand 
247d7f861b9SDavid Hildenbrand /* Perform rmap removal after we have flushed the TLB. */
248d7f861b9SDavid Hildenbrand #define ENCODED_PAGE_BIT_DELAY_RMAP		1ul
249d7f861b9SDavid Hildenbrand 
250d7f861b9SDavid Hildenbrand /*
251d7f861b9SDavid Hildenbrand  * The next item in an encoded_page array is the "nr_pages" argument, specifying
252d7f861b9SDavid Hildenbrand  * the number of consecutive pages starting from this page, that all belong to
253d7f861b9SDavid Hildenbrand  * the same folio. For example, "nr_pages" corresponds to the number of folio
254d7f861b9SDavid Hildenbrand  * references that must be dropped. If this bit is not set, "nr_pages" is
25570fb4fdfSLinus Torvalds  * implicitly 1.
25670fb4fdfSLinus Torvalds  */
257da510964SDavid Hildenbrand #define ENCODED_PAGE_BIT_NR_PAGES_NEXT		2ul
25870fb4fdfSLinus Torvalds 
encode_page(struct page * page,unsigned long flags)25970fb4fdfSLinus Torvalds static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
26070fb4fdfSLinus Torvalds {
26170fb4fdfSLinus Torvalds 	BUILD_BUG_ON(flags > ENCODED_PAGE_BITS);
26270fb4fdfSLinus Torvalds 	return (struct encoded_page *)(flags | (unsigned long)page);
263da510964SDavid Hildenbrand }
26470fb4fdfSLinus Torvalds 
encoded_page_flags(struct encoded_page * page)26570fb4fdfSLinus Torvalds static inline unsigned long encoded_page_flags(struct encoded_page *page)
26670fb4fdfSLinus Torvalds {
26770fb4fdfSLinus Torvalds 	return ENCODED_PAGE_BITS & (unsigned long)page;
268da510964SDavid Hildenbrand }
26970fb4fdfSLinus Torvalds 
encoded_page_ptr(struct encoded_page * page)27070fb4fdfSLinus Torvalds static inline struct page *encoded_page_ptr(struct encoded_page *page)
271d7f861b9SDavid Hildenbrand {
272d7f861b9SDavid Hildenbrand 	return (struct page *)(~ENCODED_PAGE_BITS & (unsigned long)page);
273d7f861b9SDavid Hildenbrand }
274d7f861b9SDavid Hildenbrand 
encode_nr_pages(unsigned long nr)275d7f861b9SDavid Hildenbrand static __always_inline struct encoded_page *encode_nr_pages(unsigned long nr)
276d7f861b9SDavid Hildenbrand {
277d7f861b9SDavid Hildenbrand 	VM_WARN_ON_ONCE((nr << 2) >> 2 != nr);
278d7f861b9SDavid Hildenbrand 	return (struct encoded_page *)(nr << 2);
279d7f861b9SDavid Hildenbrand }
280d7f861b9SDavid Hildenbrand 
encoded_nr_pages(struct encoded_page * page)281d7f861b9SDavid Hildenbrand static __always_inline unsigned long encoded_nr_pages(struct encoded_page *page)
28285a13334SMatthew Wilcox {
28385a13334SMatthew Wilcox 	return ((unsigned long)page) >> 2;
28485a13334SMatthew Wilcox }
28585a13334SMatthew Wilcox 
28685a13334SMatthew Wilcox /*
28785a13334SMatthew Wilcox  * A swap entry has to fit into a "unsigned long", as the entry is hidden
28885a13334SMatthew Wilcox  * in the "index" field of the swapper address space.
28985a13334SMatthew Wilcox  */
2904996fc54SDavid Hildenbrand typedef struct {
2914996fc54SDavid Hildenbrand 	unsigned long val;
2924996fc54SDavid Hildenbrand } swp_entry_t;
2934996fc54SDavid Hildenbrand 
2944996fc54SDavid Hildenbrand #if defined(CONFIG_MEMCG) || defined(CONFIG_SLAB_OBJ_EXT)
295*6af8cb80SDavid Hildenbrand /* We have some extra room after the refcount in tail pages. */
296*6af8cb80SDavid Hildenbrand #define NR_PAGES_IN_LARGE_FOLIO
297*6af8cb80SDavid Hildenbrand #endif
298*6af8cb80SDavid Hildenbrand 
299*6af8cb80SDavid Hildenbrand /*
300*6af8cb80SDavid Hildenbrand  * On 32bit, we can cut the required metadata in half, because:
301*6af8cb80SDavid Hildenbrand  * (a) PID_MAX_LIMIT implicitly limits the number of MMs we could ever have,
302*6af8cb80SDavid Hildenbrand  *     so we can limit MM IDs to 15 bit (32767).
303*6af8cb80SDavid Hildenbrand  * (b) We don't expect folios where even a single complete PTE mapping by
304*6af8cb80SDavid Hildenbrand  *     one MM would exceed 15 bits (order-15).
305*6af8cb80SDavid Hildenbrand  */
306*6af8cb80SDavid Hildenbrand #ifdef CONFIG_64BIT
307*6af8cb80SDavid Hildenbrand typedef int mm_id_mapcount_t;
308*6af8cb80SDavid Hildenbrand #define MM_ID_MAPCOUNT_MAX		INT_MAX
309*6af8cb80SDavid Hildenbrand typedef unsigned int mm_id_t;
310*6af8cb80SDavid Hildenbrand #else /* !CONFIG_64BIT */
311*6af8cb80SDavid Hildenbrand typedef short mm_id_mapcount_t;
312*6af8cb80SDavid Hildenbrand #define MM_ID_MAPCOUNT_MAX		SHRT_MAX
313*6af8cb80SDavid Hildenbrand typedef unsigned short mm_id_t;
314*6af8cb80SDavid Hildenbrand #endif /* CONFIG_64BIT */
315*6af8cb80SDavid Hildenbrand 
316*6af8cb80SDavid Hildenbrand /* We implicitly use the dummy ID for init-mm etc. where we never rmap pages. */
317*6af8cb80SDavid Hildenbrand #define MM_ID_DUMMY			0
318*6af8cb80SDavid Hildenbrand #define MM_ID_MIN			(MM_ID_DUMMY + 1)
319*6af8cb80SDavid Hildenbrand 
320*6af8cb80SDavid Hildenbrand /*
321*6af8cb80SDavid Hildenbrand  * We leave the highest bit of each MM id unused, so we can store a flag
322*6af8cb80SDavid Hildenbrand  * in the highest bit of each folio->_mm_id[].
323*6af8cb80SDavid Hildenbrand  */
324*6af8cb80SDavid Hildenbrand #define MM_ID_BITS			((sizeof(mm_id_t) * BITS_PER_BYTE) - 1)
325*6af8cb80SDavid Hildenbrand #define MM_ID_MASK			((1U << MM_ID_BITS) - 1)
326*6af8cb80SDavid Hildenbrand #define MM_ID_MAX			MM_ID_MASK
327*6af8cb80SDavid Hildenbrand 
328*6af8cb80SDavid Hildenbrand /*
329*6af8cb80SDavid Hildenbrand  * In order to use bit_spin_lock(), which requires an unsigned long, we
330*6af8cb80SDavid Hildenbrand  * operate on folio->_mm_ids when working on flags.
331*6af8cb80SDavid Hildenbrand  */
332*6af8cb80SDavid Hildenbrand #define FOLIO_MM_IDS_LOCK_BITNUM	MM_ID_BITS
3337b230db3SMatthew Wilcox (Oracle) #define FOLIO_MM_IDS_LOCK_BIT		BIT(FOLIO_MM_IDS_LOCK_BITNUM)
3347b230db3SMatthew Wilcox (Oracle) #define FOLIO_MM_IDS_SHARED_BITNUM	(2 * MM_ID_BITS + 1)
3357b230db3SMatthew Wilcox (Oracle) #define FOLIO_MM_IDS_SHARED_BIT		BIT(FOLIO_MM_IDS_SHARED_BITNUM)
3367b230db3SMatthew Wilcox (Oracle) 
337334f6f53SMatthew Wilcox (Oracle) /**
3387b230db3SMatthew Wilcox (Oracle)  * struct folio - Represents a contiguous set of bytes.
3397b230db3SMatthew Wilcox (Oracle)  * @flags: Identical to the page flags.
3407b230db3SMatthew Wilcox (Oracle)  * @lru: Least Recently Used list; tracks how recently this folio was used.
3417b230db3SMatthew Wilcox (Oracle)  * @mlock_count: Number of times this folio has been pinned by mlock().
34238607c62SAlistair Popple  * @mapping: The file this page belongs to, or refers to the anon_vma for
34338607c62SAlistair Popple  *    anonymous memory.
3447b230db3SMatthew Wilcox (Oracle)  * @index: Offset within the file, in units of pages.  For anonymous memory,
34585a13334SMatthew Wilcox  *    this is the index from the beginning of the mmap.
3467b230db3SMatthew Wilcox (Oracle)  * @share: number of DAX mappings that reference this folio. See
3477b230db3SMatthew Wilcox (Oracle)  *    dax_associate_entry.
3487b230db3SMatthew Wilcox (Oracle)  * @private: Filesystem per-folio data (see folio_attach_private()).
3497b230db3SMatthew Wilcox (Oracle)  * @swap: Used for swp_entry_t if folio_test_swapcache().
3507b230db3SMatthew Wilcox (Oracle)  * @_mapcount: Do not access this member directly.  Use folio_mapcount() to
35182ba975eSAlistair Popple  *    find out how many times this folio is mapped by userspace.
3521d44f2e6SKefeng Wang  * @_refcount: Do not access this member directly.  Use folio_ref_count()
3531d44f2e6SKefeng Wang  *    to find how many references there are to this folio.
354b14224fbSMatthew Wilcox (Oracle)  * @memcg_data: Memory Control Group data.
35505c5323bSDavid Hildenbrand  * @pgmap: Metadata for ZONE_DEVICE mappings
35605c5323bSDavid Hildenbrand  * @virtual: Virtual address in the kernel direct map.
357379708ffSMatthew Wilcox (Oracle)  * @_last_cpupid: IDs of last CPU and last process that accessed the folio.
3584996fc54SDavid Hildenbrand  * @_entire_mapcount: Do not use directly, call folio_entire_mapcount().
359*6af8cb80SDavid Hildenbrand  * @_large_mapcount: Do not use directly, call folio_mapcount().
360*6af8cb80SDavid Hildenbrand  * @_nr_pages_mapped: Do not use outside of rmap and debug code.
361*6af8cb80SDavid Hildenbrand  * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
362dad6a5ebSHugh Dickins  * @_nr_pages: Do not use directly, call folio_nr_pages().
363dad6a5ebSHugh Dickins  * @_mm_id: Do not use outside of rmap code.
364dad6a5ebSHugh Dickins  * @_mm_ids: Do not use outside of rmap code.
365dad6a5ebSHugh Dickins  * @_mm_id_mapcount: Do not use outside of rmap code.
3664375a553SMatthew Wilcox (Oracle)  * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
367a52c6330SAlex Shi (Tencent)  * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
3687b230db3SMatthew Wilcox (Oracle)  * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
3697b230db3SMatthew Wilcox (Oracle)  * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
3707b230db3SMatthew Wilcox (Oracle)  * @_deferred_list: Folios to be split under memory pressure.
3717b230db3SMatthew Wilcox (Oracle)  * @_unused_slab_obj_exts: Placeholder to match obj_exts in struct slab.
3727b230db3SMatthew Wilcox (Oracle)  *
3737b230db3SMatthew Wilcox (Oracle)  * A folio is a physically, virtually and logically contiguous set
3747b230db3SMatthew Wilcox (Oracle)  * of bytes.  It is a power-of-two in size, and it is aligned to that
3757b230db3SMatthew Wilcox (Oracle)  * same power-of-two.  It is at least as large as %PAGE_SIZE.  If it is
3767b230db3SMatthew Wilcox (Oracle)  * in the page cache, it is at a file offset which is a multiple of that
3777b230db3SMatthew Wilcox (Oracle)  * power-of-two.  It may be mapped into userspace at an address which is
3787b230db3SMatthew Wilcox (Oracle)  * at an arbitrary page offset, but its kernel virtual address is aligned
3797b230db3SMatthew Wilcox (Oracle)  * to its size.
3807b230db3SMatthew Wilcox (Oracle)  */
3817b230db3SMatthew Wilcox (Oracle) struct folio {
3827b230db3SMatthew Wilcox (Oracle) 	/* private: don't document the anon union */
38307ca7606SHugh Dickins 	union {
3847b230db3SMatthew Wilcox (Oracle) 		struct {
385334f6f53SMatthew Wilcox (Oracle) 	/* public: */
38607ca7606SHugh Dickins 			unsigned long flags;
38707ca7606SHugh Dickins 			union {
388334f6f53SMatthew Wilcox (Oracle) 				struct list_head lru;
38907ca7606SHugh Dickins 	/* private: avoid cluttering the output */
390334f6f53SMatthew Wilcox (Oracle) 				struct {
39107ca7606SHugh Dickins 					void *__filler;
392334f6f53SMatthew Wilcox (Oracle) 	/* public: */
39382ba975eSAlistair Popple 					unsigned int mlock_count;
39407ca7606SHugh Dickins 	/* private: */
3957b230db3SMatthew Wilcox (Oracle) 				};
39638607c62SAlistair Popple 	/* public: */
3977b230db3SMatthew Wilcox (Oracle) 				struct dev_pagemap *pgmap;
39838607c62SAlistair Popple 			};
39938607c62SAlistair Popple 			struct address_space *mapping;
40085a13334SMatthew Wilcox 			union {
4017b230db3SMatthew Wilcox (Oracle) 				pgoff_t index;
40285a13334SMatthew Wilcox 				unsigned long share;
40385a13334SMatthew Wilcox 			};
4047b230db3SMatthew Wilcox (Oracle) 			union {
4057b230db3SMatthew Wilcox (Oracle) 				void *private;
406a52c6330SAlex Shi (Tencent) 				swp_entry_t swap;
4077b230db3SMatthew Wilcox (Oracle) 			};
408a52c6330SAlex Shi (Tencent) 			atomic_t _mapcount;
409a52c6330SAlex Shi (Tencent) 			atomic_t _refcount;
4107b230db3SMatthew Wilcox (Oracle) #ifdef CONFIG_MEMCG
4111d44f2e6SKefeng Wang 			unsigned long memcg_data;
4121d44f2e6SKefeng Wang #elif defined(CONFIG_SLAB_OBJ_EXT)
4131d44f2e6SKefeng Wang 			unsigned long _unused_slab_obj_exts;
4141d44f2e6SKefeng Wang #endif
4151d44f2e6SKefeng Wang #if defined(WANT_PAGE_VIRTUAL)
4161d44f2e6SKefeng Wang 			void *virtual;
4177b230db3SMatthew Wilcox (Oracle) #endif
4187b230db3SMatthew Wilcox (Oracle) #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
4197b230db3SMatthew Wilcox (Oracle) 			int _last_cpupid;
4207b230db3SMatthew Wilcox (Oracle) #endif
421dad6a5ebSHugh Dickins 	/* private: the union with struct page is transitional */
422dad6a5ebSHugh Dickins 		};
423379708ffSMatthew Wilcox (Oracle) 		struct page page;
424dad6a5ebSHugh Dickins 	};
4254996fc54SDavid Hildenbrand 	union {
4264996fc54SDavid Hildenbrand 		struct {
427a8d55327SMatthew Wilcox (Oracle) 			unsigned long _flags_1;
42805c5323bSDavid Hildenbrand 			unsigned long _head_1;
429eec20426SMatthew Wilcox (Oracle) 			union {
43031a31da8SDavid Hildenbrand 				struct {
431845d2be6SDavid Hildenbrand 	/* public: */
432379708ffSMatthew Wilcox (Oracle) 					atomic_t _large_mapcount;
43331a31da8SDavid Hildenbrand 					atomic_t _nr_pages_mapped;
434*6af8cb80SDavid Hildenbrand #ifdef CONFIG_64BIT
435*6af8cb80SDavid Hildenbrand 					atomic_t _entire_mapcount;
436*6af8cb80SDavid Hildenbrand 					atomic_t _pincount;
437*6af8cb80SDavid Hildenbrand #endif /* CONFIG_64BIT */
438*6af8cb80SDavid Hildenbrand 					mm_id_mapcount_t _mm_id_mapcount[2];
4394996fc54SDavid Hildenbrand 					union {
4404996fc54SDavid Hildenbrand 						mm_id_t _mm_id[2];
4414996fc54SDavid Hildenbrand 						unsigned long _mm_ids;
4424996fc54SDavid Hildenbrand 					};
4434996fc54SDavid Hildenbrand 	/* private: the union with struct page is transitional */
4444996fc54SDavid Hildenbrand 				};
4454996fc54SDavid Hildenbrand 				unsigned long _usable_1[4];
4464996fc54SDavid Hildenbrand 			};
4474996fc54SDavid Hildenbrand 			atomic_t _mapcount_1;
4484996fc54SDavid Hildenbrand 			atomic_t _refcount_1;
449cfeed8ffSDavid Hildenbrand 	/* public: */
450dad6a5ebSHugh Dickins #ifdef NR_PAGES_IN_LARGE_FOLIO
451dad6a5ebSHugh Dickins 			unsigned int _nr_pages;
452dad6a5ebSHugh Dickins #endif /* NR_PAGES_IN_LARGE_FOLIO */
453dad6a5ebSHugh Dickins 	/* private: the union with struct page is transitional */
454dad6a5ebSHugh Dickins 		};
455dad6a5ebSHugh Dickins 		struct page __page_1;
456dad6a5ebSHugh Dickins 	};
457a8d55327SMatthew Wilcox (Oracle) 	union {
4584eeec8c8SDavid Hildenbrand 		struct {
45931a31da8SDavid Hildenbrand 			unsigned long _flags_2;
460845d2be6SDavid Hildenbrand 			unsigned long _head_2;
46131a31da8SDavid Hildenbrand 	/* public: */
46231a31da8SDavid Hildenbrand 			struct list_head _deferred_list;
4634eeec8c8SDavid Hildenbrand #ifndef CONFIG_64BIT
4644eeec8c8SDavid Hildenbrand 			atomic_t _entire_mapcount;
4654eeec8c8SDavid Hildenbrand 			atomic_t _pincount;
4664eeec8c8SDavid Hildenbrand #endif /* !CONFIG_64BIT */
4674eeec8c8SDavid Hildenbrand 	/* private: the union with struct page is transitional */
4684eeec8c8SDavid Hildenbrand 		};
4694eeec8c8SDavid Hildenbrand 		struct page __page_2;
4704eeec8c8SDavid Hildenbrand 	};
4714eeec8c8SDavid Hildenbrand 	union {
472dad6a5ebSHugh Dickins 		struct {
473dad6a5ebSHugh Dickins 			unsigned long _flags_3;
474dad6a5ebSHugh Dickins 			unsigned long _head_3;
475dad6a5ebSHugh Dickins 	/* public: */
476a8d55327SMatthew Wilcox (Oracle) 			void *_hugetlb_subpool;
477dad6a5ebSHugh Dickins 			void *_hugetlb_cgroup;
4784eeec8c8SDavid Hildenbrand 			void *_hugetlb_cgroup_rsvd;
479dad6a5ebSHugh Dickins 			void *_hugetlb_hwpoison;
4807b230db3SMatthew Wilcox (Oracle) 	/* private: the union with struct page is transitional */
4817b230db3SMatthew Wilcox (Oracle) 		};
4827b230db3SMatthew Wilcox (Oracle) 		struct page __page_3;
4837b230db3SMatthew Wilcox (Oracle) 	};
4847b230db3SMatthew Wilcox (Oracle) };
4857b230db3SMatthew Wilcox (Oracle) 
486536f4217SWei Yang #define FOLIO_MATCH(pg, fl)						\
4877b230db3SMatthew Wilcox (Oracle) 	static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
4887b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(flags, flags);
4897b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(lru, lru);
4907b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(mapping, mapping);
4917b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(compound_head, lru);
4927b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(index, index);
4937b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(private, private);
4947b230db3SMatthew Wilcox (Oracle) FOLIO_MATCH(_mapcount, _mapcount);
4951d44f2e6SKefeng Wang FOLIO_MATCH(_refcount, _refcount);
4961d44f2e6SKefeng Wang #ifdef CONFIG_MEMCG
4971d44f2e6SKefeng Wang FOLIO_MATCH(memcg_data, memcg_data);
4981d44f2e6SKefeng Wang #endif
4991d44f2e6SKefeng Wang #if defined(WANT_PAGE_VIRTUAL)
5001d44f2e6SKefeng Wang FOLIO_MATCH(virtual, virtual);
5017b230db3SMatthew Wilcox (Oracle) #endif
502379708ffSMatthew Wilcox (Oracle) #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
503379708ffSMatthew Wilcox (Oracle) FOLIO_MATCH(_last_cpupid, _last_cpupid);
504379708ffSMatthew Wilcox (Oracle) #endif
505379708ffSMatthew Wilcox (Oracle) #undef FOLIO_MATCH
506dad6a5ebSHugh Dickins #define FOLIO_MATCH(pg, fl)						\
5074996fc54SDavid Hildenbrand 	static_assert(offsetof(struct folio, fl) ==			\
5084996fc54SDavid Hildenbrand 			offsetof(struct page, pg) + sizeof(struct page))
509379708ffSMatthew Wilcox (Oracle) FOLIO_MATCH(flags, _flags_1);
510dad6a5ebSHugh Dickins FOLIO_MATCH(compound_head, _head_1);
511dad6a5ebSHugh Dickins FOLIO_MATCH(_mapcount, _mapcount_1);
512dad6a5ebSHugh Dickins FOLIO_MATCH(_refcount, _refcount_1);
513dad6a5ebSHugh Dickins #undef FOLIO_MATCH
514dad6a5ebSHugh Dickins #define FOLIO_MATCH(pg, fl)						\
5154eeec8c8SDavid Hildenbrand 	static_assert(offsetof(struct folio, fl) ==			\
5164eeec8c8SDavid Hildenbrand 			offsetof(struct page, pg) + 2 * sizeof(struct page))
5174eeec8c8SDavid Hildenbrand FOLIO_MATCH(flags, _flags_2);
5184eeec8c8SDavid Hildenbrand FOLIO_MATCH(compound_head, _head_2);
5194eeec8c8SDavid Hildenbrand #undef FOLIO_MATCH
5204eeec8c8SDavid Hildenbrand #define FOLIO_MATCH(pg, fl)						\
521dad6a5ebSHugh Dickins 	static_assert(offsetof(struct folio, fl) ==			\
5227b230db3SMatthew Wilcox (Oracle) 			offsetof(struct page, pg) + 3 * sizeof(struct page))
5239a35de4fSVishal Moola (Oracle) FOLIO_MATCH(flags, _flags_3);
5249a35de4fSVishal Moola (Oracle) FOLIO_MATCH(compound_head, _head_3);
52522beb471SQi Zheng #undef FOLIO_MATCH
5269a35de4fSVishal Moola (Oracle) 
527718b1386SQi Zheng /**
528718b1386SQi Zheng  * struct ptdesc -    Memory descriptor for page tables.
529718b1386SQi Zheng  * @__page_flags:     Same as page flags. Powerpc only.
5309a35de4fSVishal Moola (Oracle)  * @pt_rcu_head:      For freeing page table pages.
5319a35de4fSVishal Moola (Oracle)  * @pt_list:          List of used page tables. Used for s390 gmap shadow pages
5329a35de4fSVishal Moola (Oracle)  *                    (which are not linked into the user page tables) and x86
533ea919671SQi Zheng  *                    pgds.
5349a35de4fSVishal Moola (Oracle)  * @_pt_pad_1:        Padding that aliases with page's compound head.
53538ca8a18SAlexander Gordeev  * @pmd_huge_pte:     Protected by ptdesc->ptl, used for THPs.
53659d9094dSLiu Shixin  * @__page_mapping:   Aliases with page->mapping. Unused for page tables.
5379a35de4fSVishal Moola (Oracle)  * @pt_index:         Used for s390 gmap.
5389a35de4fSVishal Moola (Oracle)  * @pt_mm:            Used for x86 pgds.
5399a35de4fSVishal Moola (Oracle)  * @pt_frag_refcount: For fragmented page table tracking. Powerpc only.
540f7dd74acSAlexander Gordeev  * @pt_share_count:   Used for HugeTLB PMD page table share count.
5419a35de4fSVishal Moola (Oracle)  * @_pt_pad_2:        Padding to ensure proper alignment.
5429a35de4fSVishal Moola (Oracle)  * @ptl:              Lock for the page table.
5439a35de4fSVishal Moola (Oracle)  * @__page_type:      Same as page->page_type. Unused for page tables.
5449a35de4fSVishal Moola (Oracle)  * @__page_refcount:  Same as page refcount.
5459a35de4fSVishal Moola (Oracle)  * @pt_memcg_data:    Memcg data. Tracked for page tables here.
5469a35de4fSVishal Moola (Oracle)  *
5479a35de4fSVishal Moola (Oracle)  * This struct overlays struct page for now. Do not modify without a good
5489a35de4fSVishal Moola (Oracle)  * understanding of the issues.
5499a35de4fSVishal Moola (Oracle)  */
5509a35de4fSVishal Moola (Oracle) struct ptdesc {
5519a35de4fSVishal Moola (Oracle) 	unsigned long __page_flags;
5529a35de4fSVishal Moola (Oracle) 
5539a35de4fSVishal Moola (Oracle) 	union {
5549a35de4fSVishal Moola (Oracle) 		struct rcu_head pt_rcu_head;
5559a35de4fSVishal Moola (Oracle) 		struct list_head pt_list;
5569a35de4fSVishal Moola (Oracle) 		struct {
5579a35de4fSVishal Moola (Oracle) 			unsigned long _pt_pad_1;
5589a35de4fSVishal Moola (Oracle) 			pgtable_t pmd_huge_pte;
5599a35de4fSVishal Moola (Oracle) 		};
560ea919671SQi Zheng 	};
5619a35de4fSVishal Moola (Oracle) 	unsigned long __page_mapping;
5629a35de4fSVishal Moola (Oracle) 
56359d9094dSLiu Shixin 	union {
56459d9094dSLiu Shixin 		pgoff_t pt_index;
56559d9094dSLiu Shixin 		struct mm_struct *pt_mm;
5669a35de4fSVishal Moola (Oracle) 		atomic_t pt_frag_refcount;
5679a35de4fSVishal Moola (Oracle) #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
5689a35de4fSVishal Moola (Oracle) 		atomic_t pt_share_count;
5699a35de4fSVishal Moola (Oracle) #endif
5709a35de4fSVishal Moola (Oracle) 	};
5719a35de4fSVishal Moola (Oracle) 
5729a35de4fSVishal Moola (Oracle) 	union {
5739a35de4fSVishal Moola (Oracle) 		unsigned long _pt_pad_2;
5749a35de4fSVishal Moola (Oracle) #if ALLOC_SPLIT_PTLOCKS
5759a35de4fSVishal Moola (Oracle) 		spinlock_t *ptl;
5769a35de4fSVishal Moola (Oracle) #else
577f7dd74acSAlexander Gordeev 		spinlock_t ptl;
5789a35de4fSVishal Moola (Oracle) #endif
5799a35de4fSVishal Moola (Oracle) 	};
5809a35de4fSVishal Moola (Oracle) 	unsigned int __page_type;
5819a35de4fSVishal Moola (Oracle) 	atomic_t __page_refcount;
5829a35de4fSVishal Moola (Oracle) #ifdef CONFIG_MEMCG
5839a35de4fSVishal Moola (Oracle) 	unsigned long pt_memcg_data;
5849a35de4fSVishal Moola (Oracle) #endif
5859a35de4fSVishal Moola (Oracle) };
5869a35de4fSVishal Moola (Oracle) 
5879a35de4fSVishal Moola (Oracle) #define TABLE_MATCH(pg, pt)						\
5889a35de4fSVishal Moola (Oracle) 	static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt))
589ea919671SQi Zheng TABLE_MATCH(flags, __page_flags);
5909a35de4fSVishal Moola (Oracle) TABLE_MATCH(compound_head, pt_list);
5919a35de4fSVishal Moola (Oracle) TABLE_MATCH(compound_head, _pt_pad_1);
592f7dd74acSAlexander Gordeev TABLE_MATCH(mapping, __page_mapping);
5939a35de4fSVishal Moola (Oracle) TABLE_MATCH(index, pt_index);
5949a35de4fSVishal Moola (Oracle) TABLE_MATCH(rcu_head, pt_rcu_head);
5959a35de4fSVishal Moola (Oracle) TABLE_MATCH(page_type, __page_type);
5969a35de4fSVishal Moola (Oracle) TABLE_MATCH(_refcount, __page_refcount);
5979a35de4fSVishal Moola (Oracle) #ifdef CONFIG_MEMCG
5989a35de4fSVishal Moola (Oracle) TABLE_MATCH(memcg_data, pt_memcg_data);
599bf2d4334SVishal Moola (Oracle) #endif
600bf2d4334SVishal Moola (Oracle) #undef TABLE_MATCH
601bf2d4334SVishal Moola (Oracle) static_assert(sizeof(struct ptdesc) <= sizeof(struct page));
602bf2d4334SVishal Moola (Oracle) 
603bf2d4334SVishal Moola (Oracle) #define ptdesc_page(pt)			(_Generic((pt),			\
604bf2d4334SVishal Moola (Oracle) 	const struct ptdesc *:		(const struct page *)(pt),	\
605bf2d4334SVishal Moola (Oracle) 	struct ptdesc *:		(struct page *)(pt)))
606bf2d4334SVishal Moola (Oracle) 
607bf2d4334SVishal Moola (Oracle) #define ptdesc_folio(pt)		(_Generic((pt),			\
608bf2d4334SVishal Moola (Oracle) 	const struct ptdesc *:		(const struct folio *)(pt),	\
609bf2d4334SVishal Moola (Oracle) 	struct ptdesc *:		(struct folio *)(pt)))
610bf2d4334SVishal Moola (Oracle) 
61159d9094dSLiu Shixin #define page_ptdesc(p)			(_Generic((p),			\
61259d9094dSLiu Shixin 	const struct page *:		(const struct ptdesc *)(p),	\
61359d9094dSLiu Shixin 	struct page *:			(struct ptdesc *)(p)))
61459d9094dSLiu Shixin 
61559d9094dSLiu Shixin #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
ptdesc_pmd_pts_init(struct ptdesc * ptdesc)61659d9094dSLiu Shixin static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
61759d9094dSLiu Shixin {
61859d9094dSLiu Shixin 	atomic_set(&ptdesc->pt_share_count, 0);
61959d9094dSLiu Shixin }
62059d9094dSLiu Shixin 
ptdesc_pmd_pts_inc(struct ptdesc * ptdesc)62159d9094dSLiu Shixin static inline void ptdesc_pmd_pts_inc(struct ptdesc *ptdesc)
62259d9094dSLiu Shixin {
62359d9094dSLiu Shixin 	atomic_inc(&ptdesc->pt_share_count);
62459d9094dSLiu Shixin }
62559d9094dSLiu Shixin 
ptdesc_pmd_pts_dec(struct ptdesc * ptdesc)62659d9094dSLiu Shixin static inline void ptdesc_pmd_pts_dec(struct ptdesc *ptdesc)
62759d9094dSLiu Shixin {
62859d9094dSLiu Shixin 	atomic_dec(&ptdesc->pt_share_count);
62959d9094dSLiu Shixin }
63059d9094dSLiu Shixin 
ptdesc_pmd_pts_count(struct ptdesc * ptdesc)63159d9094dSLiu Shixin static inline int ptdesc_pmd_pts_count(struct ptdesc *ptdesc)
63259d9094dSLiu Shixin {
63359d9094dSLiu Shixin 	return atomic_read(&ptdesc->pt_share_count);
63459d9094dSLiu Shixin }
63559d9094dSLiu Shixin #else
ptdesc_pmd_pts_init(struct ptdesc * ptdesc)63659d9094dSLiu Shixin static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
637d1402fc7SLogan Gunthorpe {
638d1402fc7SLogan Gunthorpe }
639d1402fc7SLogan Gunthorpe #endif
640d1402fc7SLogan Gunthorpe 
641d1402fc7SLogan Gunthorpe /*
64285d0a2edSMatthew Wilcox (Oracle)  * Used for sizing the vmemmap region on some architectures
64385d0a2edSMatthew Wilcox (Oracle)  */
64485d0a2edSMatthew Wilcox (Oracle) #define STRUCT_PAGE_MAX_SHIFT	(order_base_2(sizeof(struct page)))
64585d0a2edSMatthew Wilcox (Oracle) 
64685d0a2edSMatthew Wilcox (Oracle) /*
64785d0a2edSMatthew Wilcox (Oracle)  * page_private can be used on tail pages.  However, PagePrivate is only
648b03641afSDan Williams  * checked by the VM on the head page.  So page_private on the tail pages
64960e65a6fSGuoqing Jiang  * should be used for data that's ancillary to the head page (eg attaching
65060e65a6fSGuoqing Jiang  * buffer heads to tail pages after attaching buffer heads to the head page)
65160e65a6fSGuoqing Jiang  */
65260e65a6fSGuoqing Jiang #define page_private(page)		((page)->private)
65360e65a6fSGuoqing Jiang 
set_page_private(struct page * page,unsigned long private)654b03641afSDan Williams static inline void set_page_private(struct page *page, unsigned long private)
65585d0a2edSMatthew Wilcox (Oracle) {
65685d0a2edSMatthew Wilcox (Oracle) 	page->private = private;
65785d0a2edSMatthew Wilcox (Oracle) }
65885d0a2edSMatthew Wilcox (Oracle) 
folio_get_private(struct folio * folio)65985d0a2edSMatthew Wilcox (Oracle) static inline void *folio_get_private(struct folio *folio)
66064b990d2SKirill A. Shutemov {
661ca16d140SKOSAKI Motohiro 	return folio->private;
662c92ff1bdSMartin Schwidefsky }
66331041385SSuren Baghdasaryan 
66431041385SSuren Baghdasaryan typedef unsigned long vm_flags_t;
66531041385SSuren Baghdasaryan 
66631041385SSuren Baghdasaryan /*
66731041385SSuren Baghdasaryan  * freeptr_t represents a SLUB freelist pointer, which might be encoded
66831041385SSuren Baghdasaryan  * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled.
6698feae131SDavid Howells  */
6708feae131SDavid Howells typedef struct { unsigned long v; } freeptr_t;
6718feae131SDavid Howells 
6728feae131SDavid Howells /*
6738feae131SDavid Howells  * A region containing a mapping of a non-memory backed file under NOMMU
6748feae131SDavid Howells  * conditions.  These are held in a global tree and are pinned by the VMAs that
675ca16d140SKOSAKI Motohiro  * map parts of them.
6768feae131SDavid Howells  */
6778feae131SDavid Howells struct vm_region {
678dd8632a1SPaul Mundt 	struct rb_node	vm_rb;		/* link in global region tree */
6798feae131SDavid Howells 	vm_flags_t	vm_flags;	/* VMA vm_flags */
6808feae131SDavid Howells 	unsigned long	vm_start;	/* start address of region */
6818feae131SDavid Howells 	unsigned long	vm_end;		/* region initialised to here */
6821e2ae599SDavid Howells 	unsigned long	vm_top;		/* region allocated to here */
683cfe79c00SMike Frysinger 	unsigned long	vm_pgoff;	/* the offset in vm_file corresponding to vm_start */
684cfe79c00SMike Frysinger 	struct file	*vm_file;	/* the backing file or NULL */
6858feae131SDavid Howells 
6868feae131SDavid Howells 	int		vm_usage;	/* region usage count (access under nommu_region_sem) */
687745f234bSAndrea Arcangeli 	bool		vm_icache_flushed : 1; /* true if the icache has been flushed for
688745f234bSAndrea Arcangeli 						* this region */
689745f234bSAndrea Arcangeli };
690745f234bSAndrea Arcangeli 
691745f234bSAndrea Arcangeli #ifdef CONFIG_USERFAULTFD
692745f234bSAndrea Arcangeli #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
693745f234bSAndrea Arcangeli struct vm_userfaultfd_ctx {
694745f234bSAndrea Arcangeli 	struct userfaultfd_ctx *ctx;
695745f234bSAndrea Arcangeli };
696745f234bSAndrea Arcangeli #else /* CONFIG_USERFAULTFD */
69778db3412SSuren Baghdasaryan #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
69878db3412SSuren Baghdasaryan struct vm_userfaultfd_ctx {};
69978db3412SSuren Baghdasaryan #endif /* CONFIG_USERFAULTFD */
70078db3412SSuren Baghdasaryan 
70178db3412SSuren Baghdasaryan struct anon_vma_name {
70278db3412SSuren Baghdasaryan 	struct kref kref;
7033657fdc2SLorenzo Stoakes 	/* The name needs to be at the end because it is dynamically sized. */
7043657fdc2SLorenzo Stoakes 	char name[];
7053657fdc2SLorenzo Stoakes };
7063657fdc2SLorenzo Stoakes 
7073657fdc2SLorenzo Stoakes #ifdef CONFIG_ANON_VMA_NAME
7083657fdc2SLorenzo Stoakes /*
7093657fdc2SLorenzo Stoakes  * mmap_lock should be read-locked when calling anon_vma_name(). Caller should
7103657fdc2SLorenzo Stoakes  * either keep holding the lock while using the returned pointer or it should
7113657fdc2SLorenzo Stoakes  * raise anon_vma_name refcount before releasing the lock.
7123657fdc2SLorenzo Stoakes  */
7133657fdc2SLorenzo Stoakes struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma);
7143657fdc2SLorenzo Stoakes struct anon_vma_name *anon_vma_name_alloc(const char *name);
7153657fdc2SLorenzo Stoakes void anon_vma_name_free(struct kref *kref);
7163657fdc2SLorenzo Stoakes #else /* CONFIG_ANON_VMA_NAME */
anon_vma_name(struct vm_area_struct * vma)7173657fdc2SLorenzo Stoakes static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
7183657fdc2SLorenzo Stoakes {
7193657fdc2SLorenzo Stoakes 	return NULL;
7203657fdc2SLorenzo Stoakes }
7213657fdc2SLorenzo Stoakes 
anon_vma_name_alloc(const char * name)7223657fdc2SLorenzo Stoakes static inline struct anon_vma_name *anon_vma_name_alloc(const char *name)
7233657fdc2SLorenzo Stoakes {
724f35ab95cSSuren Baghdasaryan 	return NULL;
725f35ab95cSSuren Baghdasaryan }
726c7f8f31cSSuren Baghdasaryan #endif
727ef6a22b7SMel Gorman 
7289ae5c00eSMel Gorman #define VMA_LOCK_OFFSET	0x40000000
7299ae5c00eSMel Gorman #define VMA_REF_LIMIT	(VMA_LOCK_OFFSET - 1)
7309ae5c00eSMel Gorman 
7319ae5c00eSMel Gorman struct vma_numab_state {
7329ae5c00eSMel Gorman 	/*
733ef6a22b7SMel Gorman 	 * Initialised as time in 'jiffies' after which VMA
7349ae5c00eSMel Gorman 	 * should be scanned.  Delays first scan of new VMA by at
7359ae5c00eSMel Gorman 	 * least sysctl_numa_balancing_scan_delay:
736f3a6c979SMel Gorman 	 */
7379ae5c00eSMel Gorman 	unsigned long next_scan;
7389ae5c00eSMel Gorman 
739f3a6c979SMel Gorman 	/*
7409ae5c00eSMel Gorman 	 * Time in jiffies when pids_active[] is reset to
7419ae5c00eSMel Gorman 	 * detect phase change behaviour:
7429ae5c00eSMel Gorman 	 */
7439ae5c00eSMel Gorman 	unsigned long pids_active_reset;
7449ae5c00eSMel Gorman 
7459ae5c00eSMel Gorman 	/*
7469ae5c00eSMel Gorman 	 * Approximate tracking of PIDs that trapped a NUMA hinting
7479ae5c00eSMel Gorman 	 * fault. May produce false positives due to hash collisions.
7489ae5c00eSMel Gorman 	 *
7499ae5c00eSMel Gorman 	 *   [0] Previous PID tracking
7509ae5c00eSMel Gorman 	 *   [1] Current PID tracking
751f3a6c979SMel Gorman 	 *
752f169c62fSMel Gorman 	 * Window moves after next_pid_reset has expired approximately
75384db47caSRaghavendra K T 	 * every VMA_PID_RESET_PERIOD jiffies:
75484db47caSRaghavendra K T 	 */
75584db47caSRaghavendra K T 	unsigned long pids_active[2];
756f169c62fSMel Gorman 
757f169c62fSMel Gorman 	/* MM scan sequence ID when scan first started after VMA creation */
758f169c62fSMel Gorman 	int start_scan_seq;
759f169c62fSMel Gorman 
760f169c62fSMel Gorman 	/*
761ef6a22b7SMel Gorman 	 * MM scan sequence ID when the VMA was last completely scanned.
762ef6a22b7SMel Gorman 	 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq
7638feae131SDavid Howells 	 */
764552657b7Schenqiwu 	int prev_scan_seq;
765c92ff1bdSMartin Schwidefsky };
766c92ff1bdSMartin Schwidefsky 
767c92ff1bdSMartin Schwidefsky /*
76817fe833bSJann Horn  * This struct describes a virtual memory area. There is one of these
76917fe833bSJann Horn  * per VM-area/task. A VM area is any part of the process virtual memory
77017fe833bSJann Horn  * space that has a special rule for the page-fault handlers (ie a shared
77131041385SSuren Baghdasaryan  * library, the executable area etc).
77231041385SSuren Baghdasaryan  *
77331041385SSuren Baghdasaryan  * Only explicitly marked struct members may be accessed by RCU readers before
774c92ff1bdSMartin Schwidefsky  * getting a stable reference.
775c92ff1bdSMartin Schwidefsky  *
776e4c6bfd2SRik van Riel  * WARNING: when adding new members, please update vm_area_init_from() to copy
777e4c6bfd2SRik van Riel  * them during vm_area_struct content duplication.
77820cce633SMichel Lespinasse  */
77920cce633SMichel Lespinasse struct vm_area_struct {
78020cce633SMichel Lespinasse 	/* The first cache line has the info for VMA tree walking. */
78120cce633SMichel Lespinasse 
78220cce633SMichel Lespinasse 	union {
78320cce633SMichel Lespinasse 		struct {
78431041385SSuren Baghdasaryan 			/* VMA covers [vm_start; vm_end) addresses within mm */
78520cce633SMichel Lespinasse 			unsigned long vm_start;
786c92ff1bdSMartin Schwidefsky 			unsigned long vm_end;
78717fe833bSJann Horn 		};
78817fe833bSJann Horn 		freeptr_t vm_freeptr; /* Pointer used by SLAB_TYPESAFE_BY_RCU */
78917fe833bSJann Horn 	};
79017fe833bSJann Horn 
79117fe833bSJann Horn 	/*
79228d8b812SLorenzo Stoakes 	 * The address space we belong to.
793bc292ab0SSuren Baghdasaryan 	 * Unstable RCU readers are allowed to read this.
794bc292ab0SSuren Baghdasaryan 	 */
795bc292ab0SSuren Baghdasaryan 	struct mm_struct *vm_mm;
796bc292ab0SSuren Baghdasaryan 	pgprot_t vm_page_prot;          /* Access permissions of this VMA. */
797bc292ab0SSuren Baghdasaryan 
798bc292ab0SSuren Baghdasaryan 	/*
799bc292ab0SSuren Baghdasaryan 	 * Flags, see mm.h.
800bc292ab0SSuren Baghdasaryan 	 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
801bc292ab0SSuren Baghdasaryan 	 */
802e4c6bfd2SRik van Riel 	union {
8035e31275cSSuren Baghdasaryan 		const vm_flags_t vm_flags;
80417fe833bSJann Horn 		vm_flags_t __private __vm_flags;
805b1f02b95SJann Horn 	};
806b1f02b95SJann Horn 
807f35ab95cSSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK
808b1f02b95SJann Horn 	/*
809b1f02b95SJann Horn 	 * Can only be written (using WRITE_ONCE()) while holding both:
810f35ab95cSSuren Baghdasaryan 	 *  - mmap_lock (in write mode)
811b1f02b95SJann Horn 	 *  - vm_refcnt bit at VMA_LOCK_OFFSET is set
812b1f02b95SJann Horn 	 * Can be read reliably while holding one of:
813b1f02b95SJann Horn 	 *  - mmap_lock (in read or write mode)
814b1f02b95SJann Horn 	 *  - vm_refcnt bit at VMA_LOCK_OFFSET is set or vm_refcnt > 1
815b1f02b95SJann Horn 	 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
816b1f02b95SJann Horn 	 * while holding nothing (except RCU to keep the VMA struct allocated).
817b1f02b95SJann Horn 	 *
818e5e7fb27SSuren Baghdasaryan 	 * This sequence counter is explicitly allowed to overflow; sequence
8195e31275cSSuren Baghdasaryan 	 * counter reuse can only lead to occasional unnecessary use of the
820c92ff1bdSMartin Schwidefsky 	 * slowpath.
821c92ff1bdSMartin Schwidefsky 	 */
822c92ff1bdSMartin Schwidefsky 	unsigned int vm_lock_seq;
823c92ff1bdSMartin Schwidefsky #endif
824c92ff1bdSMartin Schwidefsky 	/*
825c92ff1bdSMartin Schwidefsky 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
826c1e8d7c6SMichel Lespinasse 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
8275beb4930SRik van Riel 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
828c92ff1bdSMartin Schwidefsky 	 * or brk vma (with NULL file) can only be in an anon_vma list.
829c92ff1bdSMartin Schwidefsky 	 */
830c92ff1bdSMartin Schwidefsky 	struct list_head anon_vma_chain; /* Serialized by mmap_lock &
831f0f37e2fSAlexey Dobriyan 					  * page_table_lock */
832c92ff1bdSMartin Schwidefsky 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
833c92ff1bdSMartin Schwidefsky 
834c92ff1bdSMartin Schwidefsky 	/* Function pointers to deal with this struct. */
835ea1754a0SKirill A. Shutemov 	const struct vm_operations_struct *vm_ops;
836c92ff1bdSMartin Schwidefsky 
837c92ff1bdSMartin Schwidefsky 	/* Information about our backing store: */
838c92ff1bdSMartin Schwidefsky 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
839219f8a2eSAlexey Dobriyan 					   units */
840ec560175SHuang Ying 	struct file * vm_file;		/* File we map to (can be NULL). */
841219f8a2eSAlexey Dobriyan 	void * vm_private_data;		/* was vm_pte (shared mem) */
842c92ff1bdSMartin Schwidefsky 
8438feae131SDavid Howells #ifdef CONFIG_SWAP
844c92ff1bdSMartin Schwidefsky 	atomic_long_t swap_readahead_info;
845c92ff1bdSMartin Schwidefsky #endif
846c92ff1bdSMartin Schwidefsky #ifndef CONFIG_MMU
847c92ff1bdSMartin Schwidefsky 	struct vm_region *vm_region;	/* NOMMU mapping region */
848ef6a22b7SMel Gorman #endif
849ef6a22b7SMel Gorman #ifdef CONFIG_NUMA
850ef6a22b7SMel Gorman 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
8517b6218aeSSuren Baghdasaryan #endif
8527b6218aeSSuren Baghdasaryan #ifdef CONFIG_NUMA_BALANCING
853f35ab95cSSuren Baghdasaryan 	struct vma_numab_state *numab_state;	/* NUMA Balancing state */
854f35ab95cSSuren Baghdasaryan #endif
855f35ab95cSSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK
856f35ab95cSSuren Baghdasaryan 	/* Unstable RCU readers are allowed to read this. */
8577b6218aeSSuren Baghdasaryan 	refcount_t vm_refcnt ____cacheline_aligned_in_smp;
8586bef4c2fSSuren Baghdasaryan #ifdef CONFIG_DEBUG_LOCK_ALLOC
8596bef4c2fSSuren Baghdasaryan 	struct lockdep_map vmlock_dep_map;
8606bef4c2fSSuren Baghdasaryan #endif
8616bef4c2fSSuren Baghdasaryan #endif
8626bef4c2fSSuren Baghdasaryan 	/*
8636bef4c2fSSuren Baghdasaryan 	 * For areas with an address space and backing store,
8646bef4c2fSSuren Baghdasaryan 	 * linkage into the address_space->i_mmap interval tree.
8656bef4c2fSSuren Baghdasaryan 	 *
8666bef4c2fSSuren Baghdasaryan 	 */
8676bef4c2fSSuren Baghdasaryan 	struct {
8686bef4c2fSSuren Baghdasaryan 		struct rb_node rb;
8696bef4c2fSSuren Baghdasaryan 		unsigned long rb_subtree_last;
8706bef4c2fSSuren Baghdasaryan 	} shared;
8716bef4c2fSSuren Baghdasaryan #ifdef CONFIG_ANON_VMA_NAME
8726bef4c2fSSuren Baghdasaryan 	/*
8736bef4c2fSSuren Baghdasaryan 	 * For private and shared anonymous mappings, a pointer to a null
8746bef4c2fSSuren Baghdasaryan 	 * terminated string containing the name given to the vma, or NULL if
8756bef4c2fSSuren Baghdasaryan 	 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
8763859a271SKees Cook 	 */
877c92ff1bdSMartin Schwidefsky 	struct anon_vma_name *anon_name;
8783657fdc2SLorenzo Stoakes #endif
8793657fdc2SLorenzo Stoakes 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
8803657fdc2SLorenzo Stoakes } __randomize_layout;
8813657fdc2SLorenzo Stoakes 
8823657fdc2SLorenzo Stoakes #ifdef CONFIG_NUMA
8833657fdc2SLorenzo Stoakes #define vma_policy(vma) ((vma)->vm_policy)
884223baf9dSMathieu Desnoyers #else
885223baf9dSMathieu Desnoyers #define vma_policy(vma) NULL
886223baf9dSMathieu Desnoyers #endif
887223baf9dSMathieu Desnoyers 
8887e019dccSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID
889223baf9dSMathieu Desnoyers struct mm_cid {
890223baf9dSMathieu Desnoyers 	u64 time;
891223baf9dSMathieu Desnoyers 	int cid;
892db446a08SBenjamin LaHaise 	int recent_cid;
893541a3e25STina Zhang };
894c92ff1bdSMartin Schwidefsky #endif
895c1a2f7f0SRik van Riel 
896c1753fd0SMathieu Desnoyers struct kioctx_table;
897c1753fd0SMathieu Desnoyers struct iommu_mm_data;
898c1753fd0SMathieu Desnoyers struct mm_struct {
899c1753fd0SMathieu Desnoyers 	struct {
900c1753fd0SMathieu Desnoyers 		/*
901c1753fd0SMathieu Desnoyers 		 * Fields which are often written to are placed in a separate
902c1753fd0SMathieu Desnoyers 		 * cache line.
903c1753fd0SMathieu Desnoyers 		 */
904c1753fd0SMathieu Desnoyers 		struct {
905c1753fd0SMathieu Desnoyers 			/**
906c1753fd0SMathieu Desnoyers 			 * @mm_count: The number of references to &struct
907c1753fd0SMathieu Desnoyers 			 * mm_struct (@mm_users count as 1).
908c1753fd0SMathieu Desnoyers 			 *
909c1753fd0SMathieu Desnoyers 			 * Use mmgrab()/mmdrop() to modify. When this drops to
910c1753fd0SMathieu Desnoyers 			 * 0, the &struct mm_struct is freed.
911d4af56c5SLiam R. Howlett 			 */
912529ce23aSRick Edgecombe 			atomic_t mm_count;
913c92ff1bdSMartin Schwidefsky 		} ____cacheline_aligned_in_smp;
91441aacc1eSRadu Caragea 
9151b028f78SDmitry Safonov 		struct maple_tree mm_mt;
916041711ceSZhen Lei 
9171b028f78SDmitry Safonov 		unsigned long mmap_base;	/* base of mmap area */
9181b028f78SDmitry Safonov 		unsigned long mmap_legacy_base;	/* base of mmap area in bottom-up allocations */
9191b028f78SDmitry Safonov #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
920c92ff1bdSMartin Schwidefsky 		/* Base addresses for compatible mmap() */
921c92ff1bdSMartin Schwidefsky 		unsigned long mmap_compat_base;
922b279ddc3SVegard Nossum 		unsigned long mmap_compat_legacy_base;
923227a4aadSMathieu Desnoyers #endif
924227a4aadSMathieu Desnoyers 		unsigned long task_size;	/* size of task vm space */
925227a4aadSMathieu Desnoyers 		pgd_t * pgd;
926227a4aadSMathieu Desnoyers 
927227a4aadSMathieu Desnoyers #ifdef CONFIG_MEMBARRIER
928227a4aadSMathieu Desnoyers 		/**
929227a4aadSMathieu Desnoyers 		 * @membarrier_state: Flags controlling membarrier behavior.
930227a4aadSMathieu Desnoyers 		 *
931227a4aadSMathieu Desnoyers 		 * This field is close to @pgd to hopefully fit in the same
932227a4aadSMathieu Desnoyers 		 * cache-line, which needs to be touched by switch_mm().
933b279ddc3SVegard Nossum 		 */
934b279ddc3SVegard Nossum 		atomic_t membarrier_state;
935b279ddc3SVegard Nossum #endif
936c1a2f7f0SRik van Riel 
937c1a2f7f0SRik van Riel 		/**
938c1a2f7f0SRik van Riel 		 * @mm_users: The number of users including userspace.
939c1a2f7f0SRik van Riel 		 *
940c1a2f7f0SRik van Riel 		 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
941b279ddc3SVegard Nossum 		 * drops to 0 (i.e. when the task exits and there are no other
942b279ddc3SVegard Nossum 		 * temporary reference holders), we also release a reference on
943b279ddc3SVegard Nossum 		 * @mm_count (which may then free the &struct mm_struct if
944af7f588dSMathieu Desnoyers 		 * @mm_count also drops to 0).
945af7f588dSMathieu Desnoyers 		 */
946223baf9dSMathieu Desnoyers 		atomic_t mm_users;
947af7f588dSMathieu Desnoyers 
948223baf9dSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID
949223baf9dSMathieu Desnoyers 		/**
950223baf9dSMathieu Desnoyers 		 * @pcpu_cid: Per-cpu current cid.
951af7f588dSMathieu Desnoyers 		 *
952223baf9dSMathieu Desnoyers 		 * Keep track of the currently allocated mm_cid for each cpu.
953223baf9dSMathieu Desnoyers 		 * The per-cpu mm_cid values are serialized by their respective
954223baf9dSMathieu Desnoyers 		 * runqueue locks.
955223baf9dSMathieu Desnoyers 		 */
956223baf9dSMathieu Desnoyers 		struct mm_cid __percpu *pcpu_cid;
957223baf9dSMathieu Desnoyers 		/*
958223baf9dSMathieu Desnoyers 		 * @mm_cid_next_scan: Next mm_cid scan (in jiffies).
9597e019dccSMathieu Desnoyers 		 *
9607e019dccSMathieu Desnoyers 		 * When the next mm_cid scan is due (in jiffies).
9617e019dccSMathieu Desnoyers 		 */
9627e019dccSMathieu Desnoyers 		unsigned long mm_cid_next_scan;
9637e019dccSMathieu Desnoyers 		/**
9647e019dccSMathieu Desnoyers 		 * @nr_cpus_allowed: Number of CPUs allowed for mm.
9657e019dccSMathieu Desnoyers 		 *
9667e019dccSMathieu Desnoyers 		 * Number of CPUs allowed in the union of all mm's
96702d954c0SMathieu Desnoyers 		 * threads allowed CPUs.
96802d954c0SMathieu Desnoyers 		 */
9697e019dccSMathieu Desnoyers 		unsigned int nr_cpus_allowed;
97002d954c0SMathieu Desnoyers 		/**
97102d954c0SMathieu Desnoyers 		 * @max_nr_cid: Maximum number of allowed concurrency
9727e019dccSMathieu Desnoyers 		 *              IDs allocated.
9737e019dccSMathieu Desnoyers 		 *
9747e019dccSMathieu Desnoyers 		 * Track the highest number of allowed concurrency IDs
9757e019dccSMathieu Desnoyers 		 * allocated for the mm.
9767e019dccSMathieu Desnoyers 		 */
9777e019dccSMathieu Desnoyers 		atomic_t max_nr_cid;
9787e019dccSMathieu Desnoyers 		/**
9797e019dccSMathieu Desnoyers 		 * @cpus_allowed_lock: Lock protecting mm cpus_allowed.
9807e019dccSMathieu Desnoyers 		 *
981af7f588dSMathieu Desnoyers 		 * Provide mutual exclusion for mm cpus_allowed and
982c4812909SKirill A. Shutemov 		 * mm nr_cpus_allowed updates.
9833783e172SKele Huang 		 */
984b4e98d9aSKirill A. Shutemov 		raw_spinlock_t cpus_allowed_lock;
985c92ff1bdSMartin Schwidefsky #endif
986481b4bb5SRichard Kennedy #ifdef CONFIG_MMU
987c1a2f7f0SRik van Riel 		atomic_long_t pgtables_bytes;	/* size of all page tables */
988c1a2f7f0SRik van Riel #endif
989c1a2f7f0SRik van Riel 		int map_count;			/* number of VMAs */
9902e302543SFeng Tang 
9912e302543SFeng Tang 		spinlock_t page_table_lock; /* Protects page tables and some
9922e302543SFeng Tang 					     * counters
9932e302543SFeng Tang 					     */
9942e302543SFeng Tang 		/*
9952e302543SFeng Tang 		 * With some kernel config, the current mmap_lock's offset
9962e302543SFeng Tang 		 * inside 'mm_struct' is at 0x120, which is very optimal, as
9972e302543SFeng Tang 		 * its two hot fields 'count' and 'owner' sit in 2 different
9982e302543SFeng Tang 		 * cachelines,  and when mmap_lock is highly contended, both
9992e302543SFeng Tang 		 * of the 2 fields will be accessed frequently, current layout
10002e302543SFeng Tang 		 * will help to reduce cache bouncing.
10012e302543SFeng Tang 		 *
1002da1c55f1SMichel Lespinasse 		 * So please be careful with adding new fields before
1003c92ff1bdSMartin Schwidefsky 		 * mmap_lock, which can easily push the 2 fields into one
1004c1a2f7f0SRik van Riel 		 * cacheline.
1005c1a2f7f0SRik van Riel 		 */
1006c1a2f7f0SRik van Riel 		struct rw_semaphore mmap_lock;
1007c92ff1bdSMartin Schwidefsky 
1008c92ff1bdSMartin Schwidefsky 		struct list_head mmlist; /* List of maybe swapped mm's.	These
10095e31275cSSuren Baghdasaryan 					  * are globally strung together off
1010f35ab95cSSuren Baghdasaryan 					  * init_mm.mmlist, and are protected
1011b1f02b95SJann Horn 					  * by mmlist_lock
1012b1f02b95SJann Horn 					  */
1013b1f02b95SJann Horn #ifdef CONFIG_PER_VMA_LOCK
1014b1f02b95SJann Horn 		struct rcuwait vma_writer_wait;
1015b1f02b95SJann Horn 		/*
1016b1f02b95SJann Horn 		 * This field has lock-like semantics, meaning it is sometimes
1017e5e7fb27SSuren Baghdasaryan 		 * accessed with ACQUIRE/RELEASE semantics.
1018e5e7fb27SSuren Baghdasaryan 		 * Roughly speaking, incrementing the sequence number is
1019e5e7fb27SSuren Baghdasaryan 		 * equivalent to releasing locks on VMAs; reading the sequence
1020b1f02b95SJann Horn 		 * number can be part of taking a read lock on a VMA.
1021b1f02b95SJann Horn 		 * Incremented every time mmap_lock is write-locked/unlocked.
1022b1f02b95SJann Horn 		 * Initialized to 0, therefore odd values indicate mmap_lock
1023b1f02b95SJann Horn 		 * is write-locked and even values that it's released.
1024b1f02b95SJann Horn 		 *
1025b1f02b95SJann Horn 		 * Can be modified under write mmap_lock using RELEASE
1026b1f02b95SJann Horn 		 * semantics.
1027b1f02b95SJann Horn 		 * Can be read with no other protection when holding write
1028e5e7fb27SSuren Baghdasaryan 		 * mmap_lock.
10295e31275cSSuren Baghdasaryan 		 * Can be read with ACQUIRE semantics if not holding write
1030c92ff1bdSMartin Schwidefsky 		 * mmap_lock.
1031c92ff1bdSMartin Schwidefsky 		 */
1032c92ff1bdSMartin Schwidefsky 		seqcount_t mm_lock_seq;
1033c92ff1bdSMartin Schwidefsky #endif
1034c92ff1bdSMartin Schwidefsky 
1035e10d59f2SChristoph Lameter 
1036e10d59f2SChristoph Lameter 		unsigned long hiwater_rss; /* High-watermark of RSS usage */
103770f8a3caSDavidlohr Bueso 		unsigned long hiwater_vm;  /* High-water virtual memory usage */
103830bdbb78SKonstantin Khlebnikov 
103930bdbb78SKonstantin Khlebnikov 		unsigned long total_vm;	   /* Total pages mapped */
104030bdbb78SKonstantin Khlebnikov 		unsigned long locked_vm;   /* Pages that have PG_mlocked set */
1041e10d59f2SChristoph Lameter 		atomic64_t    pinned_vm;   /* Refcount permanently increased */
104288aa7cc6SYang Shi 		unsigned long data_vm;	   /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
10432e302543SFeng Tang 		unsigned long exec_vm;	   /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
10442e302543SFeng Tang 		unsigned long stack_vm;	   /* VM_STACK */
10452e302543SFeng Tang 		unsigned long def_flags;
10462e302543SFeng Tang 
10472e302543SFeng Tang 		/**
10482e302543SFeng Tang 		 * @write_protect_seq: Locked when any thread is write
10492e302543SFeng Tang 		 * protecting pages mapped by this mm to enforce a later COW,
105088aa7cc6SYang Shi 		 * for instance during page table copying for fork().
10512e302543SFeng Tang 		 */
1052c92ff1bdSMartin Schwidefsky 		seqcount_t write_protect_seq;
1053c92ff1bdSMartin Schwidefsky 
1054c92ff1bdSMartin Schwidefsky 		spinlock_t arg_lock; /* protect the below fields */
1055c92ff1bdSMartin Schwidefsky 
1056c92ff1bdSMartin Schwidefsky 		unsigned long start_code, end_code, start_data, end_data;
1057c92ff1bdSMartin Schwidefsky 		unsigned long start_brk, brk, start_stack;
1058f1a79412SShakeel Butt 		unsigned long arg_start, arg_end, env_start, env_end;
1059d559db08SKAMEZAWA Hiroyuki 
1060801460d0SHiroshi Shimamoto 		unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
1061801460d0SHiroshi Shimamoto 
1062c92ff1bdSMartin Schwidefsky 		struct percpu_counter rss_stat[NR_MM_COUNTERS];
1063c92ff1bdSMartin Schwidefsky 
1064c92ff1bdSMartin Schwidefsky 		struct linux_binfmt *binfmt;
1065c1a2f7f0SRik van Riel 
1066c92ff1bdSMartin Schwidefsky 		/* Architecture-specific MM context */
1067858f0993SAlexey Dobriyan 		mm_context_t context;
1068abf137ddSJens Axboe 
1069db446a08SBenjamin LaHaise 		unsigned long flags; /* Must use atomic bitops to access */
1070858f0993SAlexey Dobriyan 
1071f98bafa0SOleg Nesterov #ifdef CONFIG_AIO
10724cd1a8fcSKOSAKI Motohiro 		spinlock_t			ioctx_lock;
10734cd1a8fcSKOSAKI Motohiro 		struct kioctx_table __rcu	*ioctx_table;
10744cd1a8fcSKOSAKI Motohiro #endif
10754cd1a8fcSKOSAKI Motohiro #ifdef CONFIG_MEMCG
10764cd1a8fcSKOSAKI Motohiro 		/*
10774cd1a8fcSKOSAKI Motohiro 		 * "owner" points to a task that is regarded as the canonical
10784cd1a8fcSKOSAKI Motohiro 		 * user/owner of this mm. All of the following must be true in
10794cd1a8fcSKOSAKI Motohiro 		 * order for it to be changed:
10804cd1a8fcSKOSAKI Motohiro 		 *
10814cd1a8fcSKOSAKI Motohiro 		 * current == mm->owner
10824d2deb40SArnd Bergmann 		 * current->mm != mm
108378fb7466SPavel Emelianov 		 * new_owner->mm == mm
1084bfedb589SEric W. Biederman 		 * new_owner->alloc_lock is held
1085925d1c40SMatt Helsley 		 */
1086925d1c40SMatt Helsley 		struct task_struct __rcu *owner;
108790f31d0eSKonstantin Khlebnikov #endif
1088cddb8a5cSAndrea Arcangeli 		struct user_namespace *user_ns;
1089984cfe4eSJason Gunthorpe 
1090cddb8a5cSAndrea Arcangeli 		/* store ref to file /proc/<pid>/exe symlink points to */
1091394290cbSDavid Hildenbrand 		struct file __rcu *exe_file;
1092e7a00c45SAndrea Arcangeli #ifdef CONFIG_MMU_NOTIFIER
1093e7a00c45SAndrea Arcangeli 		struct mmu_notifier_subscriptions *notifier_subscriptions;
1094cbee9f88SPeter Zijlstra #endif
1095cbee9f88SPeter Zijlstra #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
10967014887aSDavid Hildenbrand 		pgtable_t pmd_huge_pte; /* protected by page_table_lock */
10977014887aSDavid Hildenbrand #endif
10987014887aSDavid Hildenbrand #ifdef CONFIG_NUMA_BALANCING
1099cbee9f88SPeter Zijlstra 		/*
1100cbee9f88SPeter Zijlstra 		 * numa_next_scan is the next time that PTEs will be remapped
1101cbee9f88SPeter Zijlstra 		 * PROT_NONE to trigger NUMA hinting faults; such faults gather
11027014887aSDavid Hildenbrand 		 * statistics and migrate pages to new nodes if necessary.
11036e5fb223SPeter Zijlstra 		 */
11046e5fb223SPeter Zijlstra 		unsigned long numa_next_scan;
11057014887aSDavid Hildenbrand 
1106cbee9f88SPeter Zijlstra 		/* Restart point for scanning and remapping PTEs. */
1107cbee9f88SPeter Zijlstra 		unsigned long numa_scan_offset;
110820841405SRik van Riel 
1109c1a2f7f0SRik van Riel 		/* numa_scan_seq prevents two threads remapping PTEs. */
1110c1a2f7f0SRik van Riel 		int numa_scan_seq;
11117014887aSDavid Hildenbrand #endif
111220841405SRik van Riel 		/*
111316af97dcSNadav Amit 		 * An operation with batched TLB flushing is going on. Anything
11143ea27719SMel Gorman 		 * that can move process memory needs to flush the TLB when
11153ea27719SMel Gorman 		 * moving a PROT_NONE mapped page.
11165ee2fa2fSHuang Ying 		 */
11173ea27719SMel Gorman 		atomic_t tlb_flush_pending;
1118d4b3b638SSrikar Dronamraju #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
11198d491de6SThomas Gleixner 		/* See flush_tlb_batched_pending() */
11208d491de6SThomas Gleixner 		atomic_t tlb_flush_batched;
11218d491de6SThomas Gleixner #endif
11225d317b2bSNaoya Horiguchi 		struct uprobes_state uprobes_state;
11235d317b2bSNaoya Horiguchi #ifdef CONFIG_PREEMPT_RT
11245d317b2bSNaoya Horiguchi 		struct rcu_head delayed_drop;
1125ec8d7c14SMichal Hocko #endif
112652ad9bc6SFenghua Yu #ifdef CONFIG_HUGETLB_PAGE
11278f23f5dbSJason Gunthorpe 		atomic_long_t hugetlb_usage;
1128541a3e25STina Zhang #endif
112952ad9bc6SFenghua Yu 		struct work_struct async_put_work;
113076093853Sxu xin 
113176093853Sxu xin #ifdef CONFIG_IOMMU_MM_DATA
113276093853Sxu xin 		struct iommu_mm_data *iommu_mm;
11336080d19fSxu xin #endif
113476093853Sxu xin #ifdef CONFIG_KSM
113576093853Sxu xin 		/*
1136cb4df4caSxu xin 		 * Represent how many pages of this process are involved in KSM
1137cb4df4caSxu xin 		 * merging (not including ksm_zero_pages).
1138cb4df4caSxu xin 		 */
1139cb4df4caSxu xin 		unsigned long ksm_merging_pages;
1140cb4df4caSxu xin 		/*
11416080d19fSxu xin 		 * Represent how many pages are checked for ksm merging
11426080d19fSxu xin 		 * including merged and not merged.
11436080d19fSxu xin 		 */
11446080d19fSxu xin 		unsigned long ksm_rmap_items;
1145c2dc78b8SChengming Zhou 		/*
11466080d19fSxu xin 		 * Represent how many empty pages are merged with kernel zero
114761dd3f24SKinsey Ho 		 * pages when enabling KSM use_zero_pages.
1148bd74fdaeSYu Zhao 		 */
1149bd74fdaeSYu Zhao 		atomic_long_t ksm_zero_pages;
1150bd74fdaeSYu Zhao #endif /* CONFIG_KSM */
1151bd74fdaeSYu Zhao #ifdef CONFIG_LRU_GEN_WALKS_MMU
1152bd74fdaeSYu Zhao 		struct {
1153bd74fdaeSYu Zhao 			/* this mm_struct is on lru_gen_mm_list */
1154bd74fdaeSYu Zhao 			struct list_head list;
1155bd74fdaeSYu Zhao 			/*
1156bd74fdaeSYu Zhao 			 * Set when switching to this mm_struct, as a hint of
1157bd74fdaeSYu Zhao 			 * whether it has been used since the last time per-node
1158bd74fdaeSYu Zhao 			 * page table walkers cleared the corresponding bits.
1159bd74fdaeSYu Zhao 			 */
1160bd74fdaeSYu Zhao 			unsigned long bitmap;
1161bd74fdaeSYu Zhao #ifdef CONFIG_MEMCG
116261dd3f24SKinsey Ho 			/* points to the memcg of "owner" above */
1163*6af8cb80SDavid Hildenbrand 			struct mem_cgroup *memcg;
1164*6af8cb80SDavid Hildenbrand #endif
1165*6af8cb80SDavid Hildenbrand 		} lru_gen;
11663859a271SKees Cook #endif /* CONFIG_LRU_GEN_WALKS_MMU */
1167c92ff1bdSMartin Schwidefsky #ifdef CONFIG_MM_ID
1168c1a2f7f0SRik van Riel 		mm_id_t mm_id;
1169c1a2f7f0SRik van Riel #endif /* CONFIG_MM_ID */
1170c1a2f7f0SRik van Riel 	} __randomize_layout;
1171c1a2f7f0SRik van Riel 
1172c1a2f7f0SRik van Riel 	/*
1173c1a2f7f0SRik van Riel 	 * The mm_cpumask needs to be at the end of mm_struct, because it
1174c1a2f7f0SRik van Riel 	 * is dynamically sized based on nr_cpu_ids.
11753dd44325SLiam R. Howlett 	 */
11763dd44325SLiam R. Howlett 	unsigned long cpu_bitmap[];
1177abe722a1SIngo Molnar };
1178abe722a1SIngo Molnar 
1179c1a2f7f0SRik van Riel #define MM_MT_FLAGS	(MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
11806345d24dSLinus Torvalds 			 MT_FLAGS_USE_RCU)
11816345d24dSLinus Torvalds extern struct mm_struct init_mm;
1182c1a2f7f0SRik van Riel 
1183c1a2f7f0SRik van Riel /* Pointer magic because the dynamic array size confuses some compilers. */
mm_init_cpumask(struct mm_struct * mm)1184c1a2f7f0SRik van Riel static inline void mm_init_cpumask(struct mm_struct *mm)
1185c1a2f7f0SRik van Riel {
11866345d24dSLinus Torvalds 	unsigned long cpu_bitmap = (unsigned long)mm;
11876345d24dSLinus Torvalds 
118845e575abSRusty Russell 	cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
1189de03c72cSKOSAKI Motohiro 	cpumask_clear((struct cpumask *)cpu_bitmap);
1190de03c72cSKOSAKI Motohiro }
1191c1a2f7f0SRik van Riel 
1192de03c72cSKOSAKI Motohiro /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
mm_cpumask(struct mm_struct * mm)119345e575abSRusty Russell static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
1194bd74fdaeSYu Zhao {
1195bd74fdaeSYu Zhao 	return (struct cpumask *)&mm->cpu_bitmap;
1196bd74fdaeSYu Zhao }
1197bd74fdaeSYu Zhao 
1198bd74fdaeSYu Zhao #ifdef CONFIG_LRU_GEN
1199bd74fdaeSYu Zhao 
1200bd74fdaeSYu Zhao struct lru_gen_mm_list {
1201bd74fdaeSYu Zhao 	/* mm_struct list for page table walkers */
1202bd74fdaeSYu Zhao 	struct list_head fifo;
120361dd3f24SKinsey Ho 	/* protects the list above */
120461dd3f24SKinsey Ho 	spinlock_t lock;
120561dd3f24SKinsey Ho };
120661dd3f24SKinsey Ho 
1207bd74fdaeSYu Zhao #endif /* CONFIG_LRU_GEN */
1208bd74fdaeSYu Zhao 
1209bd74fdaeSYu Zhao #ifdef CONFIG_LRU_GEN_WALKS_MMU
1210bd74fdaeSYu Zhao 
1211bd74fdaeSYu Zhao void lru_gen_add_mm(struct mm_struct *mm);
1212bd74fdaeSYu Zhao void lru_gen_del_mm(struct mm_struct *mm);
1213bd74fdaeSYu Zhao void lru_gen_migrate_mm(struct mm_struct *mm);
1214bd74fdaeSYu Zhao 
lru_gen_init_mm(struct mm_struct * mm)1215bd74fdaeSYu Zhao static inline void lru_gen_init_mm(struct mm_struct *mm)
1216bd74fdaeSYu Zhao {
1217bd74fdaeSYu Zhao 	INIT_LIST_HEAD(&mm->lru_gen.list);
1218bd74fdaeSYu Zhao 	mm->lru_gen.bitmap = 0;
1219bd74fdaeSYu Zhao #ifdef CONFIG_MEMCG
1220bd74fdaeSYu Zhao 	mm->lru_gen.memcg = NULL;
1221bd74fdaeSYu Zhao #endif
1222bd74fdaeSYu Zhao }
1223bd74fdaeSYu Zhao 
lru_gen_use_mm(struct mm_struct * mm)1224bd74fdaeSYu Zhao static inline void lru_gen_use_mm(struct mm_struct *mm)
1225bd74fdaeSYu Zhao {
1226bd74fdaeSYu Zhao 	/*
1227bd74fdaeSYu Zhao 	 * When the bitmap is set, page reclaim knows this mm_struct has been
1228bd74fdaeSYu Zhao 	 * used since the last time it cleared the bitmap. So it might be worth
1229bd74fdaeSYu Zhao 	 * walking the page tables of this mm_struct to clear the accessed bit.
123061dd3f24SKinsey Ho 	 */
1231bd74fdaeSYu Zhao 	WRITE_ONCE(mm->lru_gen.bitmap, -1);
1232bd74fdaeSYu Zhao }
1233bd74fdaeSYu Zhao 
1234bd74fdaeSYu Zhao #else /* !CONFIG_LRU_GEN_WALKS_MMU */
1235bd74fdaeSYu Zhao 
lru_gen_add_mm(struct mm_struct * mm)1236bd74fdaeSYu Zhao static inline void lru_gen_add_mm(struct mm_struct *mm)
1237bd74fdaeSYu Zhao {
1238bd74fdaeSYu Zhao }
1239bd74fdaeSYu Zhao 
lru_gen_del_mm(struct mm_struct * mm)1240bd74fdaeSYu Zhao static inline void lru_gen_del_mm(struct mm_struct *mm)
1241bd74fdaeSYu Zhao {
1242bd74fdaeSYu Zhao }
1243bd74fdaeSYu Zhao 
lru_gen_migrate_mm(struct mm_struct * mm)1244bd74fdaeSYu Zhao static inline void lru_gen_migrate_mm(struct mm_struct *mm)
1245bd74fdaeSYu Zhao {
1246bd74fdaeSYu Zhao }
1247bd74fdaeSYu Zhao 
lru_gen_init_mm(struct mm_struct * mm)1248bd74fdaeSYu Zhao static inline void lru_gen_init_mm(struct mm_struct *mm)
1249bd74fdaeSYu Zhao {
1250bd74fdaeSYu Zhao }
1251bd74fdaeSYu Zhao 
lru_gen_use_mm(struct mm_struct * mm)125261dd3f24SKinsey Ho static inline void lru_gen_use_mm(struct mm_struct *mm)
1253bd74fdaeSYu Zhao {
1254f39af059SMatthew Wilcox (Oracle) }
1255f39af059SMatthew Wilcox (Oracle) 
1256f39af059SMatthew Wilcox (Oracle) #endif /* CONFIG_LRU_GEN_WALKS_MMU */
1257f39af059SMatthew Wilcox (Oracle) 
1258f39af059SMatthew Wilcox (Oracle) struct vma_iterator {
1259f39af059SMatthew Wilcox (Oracle) 	struct ma_state mas;
1260f39af059SMatthew Wilcox (Oracle) };
1261f39af059SMatthew Wilcox (Oracle) 
1262f39af059SMatthew Wilcox (Oracle) #define VMA_ITERATOR(name, __mm, __addr)				\
1263067311d3SLiam R. Howlett 	struct vma_iterator name = {					\
1264067311d3SLiam R. Howlett 		.mas = {						\
1265f39af059SMatthew Wilcox (Oracle) 			.tree = &(__mm)->mm_mt,				\
1266f39af059SMatthew Wilcox (Oracle) 			.index = __addr,				\
1267f39af059SMatthew Wilcox (Oracle) 			.node = NULL,					\
1268f39af059SMatthew Wilcox (Oracle) 			.status = ma_start,				\
1269f39af059SMatthew Wilcox (Oracle) 		},							\
1270f39af059SMatthew Wilcox (Oracle) 	}
1271b62b633eSLiam R. Howlett 
vma_iter_init(struct vma_iterator * vmi,struct mm_struct * mm,unsigned long addr)1272f39af059SMatthew Wilcox (Oracle) static inline void vma_iter_init(struct vma_iterator *vmi,
1273f39af059SMatthew Wilcox (Oracle) 		struct mm_struct *mm, unsigned long addr)
1274af7f588dSMathieu Desnoyers {
1275223baf9dSMathieu Desnoyers 	mas_init(&vmi->mas, &mm->mm_mt, addr);
1276223baf9dSMathieu Desnoyers }
1277223baf9dSMathieu Desnoyers 
1278223baf9dSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID
1279223baf9dSMathieu Desnoyers 
1280223baf9dSMathieu Desnoyers enum mm_cid_state {
1281223baf9dSMathieu Desnoyers 	MM_CID_UNSET = -1U,		/* Unset state has lazy_put flag set. */
1282223baf9dSMathieu Desnoyers 	MM_CID_LAZY_PUT = (1U << 31),
1283223baf9dSMathieu Desnoyers };
1284223baf9dSMathieu Desnoyers 
mm_cid_is_unset(int cid)1285223baf9dSMathieu Desnoyers static inline bool mm_cid_is_unset(int cid)
1286223baf9dSMathieu Desnoyers {
1287223baf9dSMathieu Desnoyers 	return cid == MM_CID_UNSET;
1288223baf9dSMathieu Desnoyers }
1289223baf9dSMathieu Desnoyers 
mm_cid_is_lazy_put(int cid)1290223baf9dSMathieu Desnoyers static inline bool mm_cid_is_lazy_put(int cid)
1291223baf9dSMathieu Desnoyers {
1292223baf9dSMathieu Desnoyers 	return !mm_cid_is_unset(cid) && (cid & MM_CID_LAZY_PUT);
1293223baf9dSMathieu Desnoyers }
1294223baf9dSMathieu Desnoyers 
mm_cid_is_valid(int cid)1295223baf9dSMathieu Desnoyers static inline bool mm_cid_is_valid(int cid)
1296223baf9dSMathieu Desnoyers {
1297223baf9dSMathieu Desnoyers 	return !(cid & MM_CID_LAZY_PUT);
1298223baf9dSMathieu Desnoyers }
1299223baf9dSMathieu Desnoyers 
mm_cid_set_lazy_put(int cid)1300223baf9dSMathieu Desnoyers static inline int mm_cid_set_lazy_put(int cid)
1301223baf9dSMathieu Desnoyers {
1302223baf9dSMathieu Desnoyers 	return cid | MM_CID_LAZY_PUT;
1303223baf9dSMathieu Desnoyers }
1304223baf9dSMathieu Desnoyers 
mm_cid_clear_lazy_put(int cid)1305223baf9dSMathieu Desnoyers static inline int mm_cid_clear_lazy_put(int cid)
13067e019dccSMathieu Desnoyers {
13077e019dccSMathieu Desnoyers 	return cid & ~MM_CID_LAZY_PUT;
13087e019dccSMathieu Desnoyers }
13097e019dccSMathieu Desnoyers 
13107e019dccSMathieu Desnoyers /*
13117e019dccSMathieu Desnoyers  * mm_cpus_allowed: Union of all mm's threads allowed CPUs.
13127e019dccSMathieu Desnoyers  */
mm_cpus_allowed(struct mm_struct * mm)13137e019dccSMathieu Desnoyers static inline cpumask_t *mm_cpus_allowed(struct mm_struct *mm)
13147e019dccSMathieu Desnoyers {
13157e019dccSMathieu Desnoyers 	unsigned long bitmap = (unsigned long)mm;
13167e019dccSMathieu Desnoyers 
13177e019dccSMathieu Desnoyers 	bitmap += offsetof(struct mm_struct, cpu_bitmap);
13187e019dccSMathieu Desnoyers 	/* Skip cpu_bitmap */
1319af7f588dSMathieu Desnoyers 	bitmap += cpumask_size();
1320af7f588dSMathieu Desnoyers 	return (struct cpumask *)bitmap;
1321af7f588dSMathieu Desnoyers }
13227e019dccSMathieu Desnoyers 
1323af7f588dSMathieu Desnoyers /* Accessor for struct mm_struct's cidmask. */
mm_cidmask(struct mm_struct * mm)13247e019dccSMathieu Desnoyers static inline cpumask_t *mm_cidmask(struct mm_struct *mm)
1325af7f588dSMathieu Desnoyers {
1326af7f588dSMathieu Desnoyers 	unsigned long cid_bitmap = (unsigned long)mm_cpus_allowed(mm);
1327af7f588dSMathieu Desnoyers 
1328af7f588dSMathieu Desnoyers 	/* Skip mm_cpus_allowed */
13297e019dccSMathieu Desnoyers 	cid_bitmap += cpumask_size();
1330af7f588dSMathieu Desnoyers 	return (struct cpumask *)cid_bitmap;
1331223baf9dSMathieu Desnoyers }
1332223baf9dSMathieu Desnoyers 
mm_init_cid(struct mm_struct * mm,struct task_struct * p)1333223baf9dSMathieu Desnoyers static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p)
1334223baf9dSMathieu Desnoyers {
1335223baf9dSMathieu Desnoyers 	int i;
1336223baf9dSMathieu Desnoyers 
13377e019dccSMathieu Desnoyers 	for_each_possible_cpu(i) {
1338223baf9dSMathieu Desnoyers 		struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, i);
1339223baf9dSMathieu Desnoyers 
13407e019dccSMathieu Desnoyers 		pcpu_cid->cid = MM_CID_UNSET;
13417e019dccSMathieu Desnoyers 		pcpu_cid->recent_cid = MM_CID_UNSET;
13427e019dccSMathieu Desnoyers 		pcpu_cid->time = 0;
13437e019dccSMathieu Desnoyers 	}
1344af7f588dSMathieu Desnoyers 	mm->nr_cpus_allowed = p->nr_cpus_allowed;
1345af7f588dSMathieu Desnoyers 	atomic_set(&mm->max_nr_cid, 0);
1346af7f588dSMathieu Desnoyers 	raw_spin_lock_init(&mm->cpus_allowed_lock);
13477e019dccSMathieu Desnoyers 	cpumask_copy(mm_cpus_allowed(mm), &p->cpus_mask);
1348223baf9dSMathieu Desnoyers 	cpumask_clear(mm_cidmask(mm));
13492c321f3fSSuren Baghdasaryan }
1350223baf9dSMathieu Desnoyers 
mm_alloc_cid_noprof(struct mm_struct * mm,struct task_struct * p)1351223baf9dSMathieu Desnoyers static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p)
13527e019dccSMathieu Desnoyers {
1353223baf9dSMathieu Desnoyers 	mm->pcpu_cid = alloc_percpu_noprof(struct mm_cid);
1354223baf9dSMathieu Desnoyers 	if (!mm->pcpu_cid)
13552c321f3fSSuren Baghdasaryan 		return -ENOMEM;
1356223baf9dSMathieu Desnoyers 	mm_init_cid(mm, p);
1357223baf9dSMathieu Desnoyers 	return 0;
1358223baf9dSMathieu Desnoyers }
1359223baf9dSMathieu Desnoyers #define mm_alloc_cid(...)	alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__))
1360223baf9dSMathieu Desnoyers 
mm_destroy_cid(struct mm_struct * mm)1361223baf9dSMathieu Desnoyers static inline void mm_destroy_cid(struct mm_struct *mm)
1362223baf9dSMathieu Desnoyers {
1363af7f588dSMathieu Desnoyers 	free_percpu(mm->pcpu_cid);
1364af7f588dSMathieu Desnoyers 	mm->pcpu_cid = NULL;
13657e019dccSMathieu Desnoyers }
13667e019dccSMathieu Desnoyers 
mm_cid_size(void)13677e019dccSMathieu Desnoyers static inline unsigned int mm_cid_size(void)
13687e019dccSMathieu Desnoyers {
13697e019dccSMathieu Desnoyers 	return 2 * cpumask_size();	/* mm_cpus_allowed(), mm_cidmask(). */
13707e019dccSMathieu Desnoyers }
13717e019dccSMathieu Desnoyers 
mm_set_cpus_allowed(struct mm_struct * mm,const struct cpumask * cpumask)13727e019dccSMathieu Desnoyers static inline void mm_set_cpus_allowed(struct mm_struct *mm, const struct cpumask *cpumask)
13737e019dccSMathieu Desnoyers {
13747e019dccSMathieu Desnoyers 	struct cpumask *mm_allowed = mm_cpus_allowed(mm);
13757e019dccSMathieu Desnoyers 
13767e019dccSMathieu Desnoyers 	if (!mm)
13777e019dccSMathieu Desnoyers 		return;
13787e019dccSMathieu Desnoyers 	/* The mm_cpus_allowed is the union of each thread allowed CPUs masks. */
1379af7f588dSMathieu Desnoyers 	raw_spin_lock(&mm->cpus_allowed_lock);
1380af7f588dSMathieu Desnoyers 	cpumask_or(mm_allowed, mm_allowed, cpumask);
13817e019dccSMathieu Desnoyers 	WRITE_ONCE(mm->nr_cpus_allowed, cpumask_weight(mm_allowed));
13827e019dccSMathieu Desnoyers 	raw_spin_unlock(&mm->cpus_allowed_lock);
1383223baf9dSMathieu Desnoyers }
13847e019dccSMathieu Desnoyers #else /* CONFIG_SCHED_MM_CID */
mm_init_cid(struct mm_struct * mm,struct task_struct * p)1385af7f588dSMathieu Desnoyers static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { }
mm_alloc_cid(struct mm_struct * mm,struct task_struct * p)1386af7f588dSMathieu Desnoyers static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; }
mm_destroy_cid(struct mm_struct * mm)1387af7f588dSMathieu Desnoyers static inline void mm_destroy_cid(struct mm_struct *mm) { }
1388af7f588dSMathieu Desnoyers 
mm_cid_size(void)13897e019dccSMathieu Desnoyers static inline unsigned int mm_cid_size(void)
1390af7f588dSMathieu Desnoyers {
1391af7f588dSMathieu Desnoyers 	return 0;
139256236a59SMinchan Kim }
mm_set_cpus_allowed(struct mm_struct * mm,const struct cpumask * cpumask)1393a72afd87SWill Deacon static inline void mm_set_cpus_allowed(struct mm_struct *mm, const struct cpumask *cpumask) { }
1394d8b45053SWill Deacon #endif /* CONFIG_SCHED_MM_CID */
1395ae8eba8bSWill Deacon 
139656236a59SMinchan Kim struct mmu_gather;
1397f872f540SAndy Lutomirski extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
1398f872f540SAndy Lutomirski extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
13993d353901SSouptick Joarder extern void tlb_finish_mmu(struct mmu_gather *tlb);
14003d353901SSouptick Joarder 
14013d353901SSouptick Joarder struct vm_fault;
14023d353901SSouptick Joarder 
14033d353901SSouptick Joarder /**
14043d353901SSouptick Joarder  * typedef vm_fault_t - Return type for page fault handlers.
14053d353901SSouptick Joarder  *
14063d353901SSouptick Joarder  * Page fault handlers return a bitmask of %VM_FAULT values.
14073d353901SSouptick Joarder  */
14083d353901SSouptick Joarder typedef __bitwise unsigned int vm_fault_t;
14093d353901SSouptick Joarder 
14103d353901SSouptick Joarder /**
14113d353901SSouptick Joarder  * enum vm_fault_reason - Page fault handlers return a bitmask of
14123d353901SSouptick Joarder  * these values to tell the core VM what happened when handling the
14133d353901SSouptick Joarder  * fault. Used to decide whether a process gets delivered SIGBUS or
14143d353901SSouptick Joarder  * just gets major/minor fault counters bumped up.
14153d353901SSouptick Joarder  *
14163d353901SSouptick Joarder  * @VM_FAULT_OOM:		Out Of Memory
14173d353901SSouptick Joarder  * @VM_FAULT_SIGBUS:		Bad access
14183d353901SSouptick Joarder  * @VM_FAULT_MAJOR:		Page read from storage
14193d353901SSouptick Joarder  * @VM_FAULT_HWPOISON:		Hit poisoned small page
14203d353901SSouptick Joarder  * @VM_FAULT_HWPOISON_LARGE:	Hit poisoned large page. Index encoded
14213d353901SSouptick Joarder  *				in upper bits
14223d353901SSouptick Joarder  * @VM_FAULT_SIGSEGV:		segmentation fault
14233d353901SSouptick Joarder  * @VM_FAULT_NOPAGE:		->fault installed the pte, not return page
14243d353901SSouptick Joarder  * @VM_FAULT_LOCKED:		->fault locked the returned page
14253d353901SSouptick Joarder  * @VM_FAULT_RETRY:		->fault blocked, must retry
14263d353901SSouptick Joarder  * @VM_FAULT_FALLBACK:		huge page fault failed, fall back to small
1427d9272525SPeter Xu  * @VM_FAULT_DONE_COW:		->fault has fully handled COW
14283d353901SSouptick Joarder  * @VM_FAULT_NEEDDSYNC:		->fault did not modify page tables and needs
14293d353901SSouptick Joarder  *				fsync() to complete (for synchronous page faults
14303d353901SSouptick Joarder  *				in DAX)
14313d353901SSouptick Joarder  * @VM_FAULT_COMPLETED:		->fault completed, meanwhile mmap lock released
14323d353901SSouptick Joarder  * @VM_FAULT_HINDEX_MASK:	mask HINDEX value
14333d353901SSouptick Joarder  *
14343d353901SSouptick Joarder  */
14353d353901SSouptick Joarder enum vm_fault_reason {
14363d353901SSouptick Joarder 	VM_FAULT_OOM            = (__force vm_fault_t)0x000001,
14373d353901SSouptick Joarder 	VM_FAULT_SIGBUS         = (__force vm_fault_t)0x000002,
14383d353901SSouptick Joarder 	VM_FAULT_MAJOR          = (__force vm_fault_t)0x000004,
14393d353901SSouptick Joarder 	VM_FAULT_HWPOISON       = (__force vm_fault_t)0x000010,
14403d353901SSouptick Joarder 	VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
14413d353901SSouptick Joarder 	VM_FAULT_SIGSEGV        = (__force vm_fault_t)0x000040,
14423d353901SSouptick Joarder 	VM_FAULT_NOPAGE         = (__force vm_fault_t)0x000100,
14433d353901SSouptick Joarder 	VM_FAULT_LOCKED         = (__force vm_fault_t)0x000200,
1444d9272525SPeter Xu 	VM_FAULT_RETRY          = (__force vm_fault_t)0x000400,
14453d353901SSouptick Joarder 	VM_FAULT_FALLBACK       = (__force vm_fault_t)0x000800,
14463d353901SSouptick Joarder 	VM_FAULT_DONE_COW       = (__force vm_fault_t)0x001000,
14473d353901SSouptick Joarder 	VM_FAULT_NEEDDSYNC      = (__force vm_fault_t)0x002000,
14483d353901SSouptick Joarder 	VM_FAULT_COMPLETED      = (__force vm_fault_t)0x004000,
14493d353901SSouptick Joarder 	VM_FAULT_HINDEX_MASK    = (__force vm_fault_t)0x0f0000,
1450fcae96ffSJann Horn };
14513d353901SSouptick Joarder 
14523d353901SSouptick Joarder /* Encode hstate index for a hwpoisoned large page */
14533d353901SSouptick Joarder #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
14543d353901SSouptick Joarder #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
14553d353901SSouptick Joarder 
14563d353901SSouptick Joarder #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS |	\
14573d353901SSouptick Joarder 			VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON |	\
14583d353901SSouptick Joarder 			VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
14593d353901SSouptick Joarder 
14603d353901SSouptick Joarder #define VM_FAULT_RESULT_TRACE \
14613d353901SSouptick Joarder 	{ VM_FAULT_OOM,                 "OOM" },	\
14623d353901SSouptick Joarder 	{ VM_FAULT_SIGBUS,              "SIGBUS" },	\
14633d353901SSouptick Joarder 	{ VM_FAULT_MAJOR,               "MAJOR" },	\
14643d353901SSouptick Joarder 	{ VM_FAULT_HWPOISON,            "HWPOISON" },	\
14653d353901SSouptick Joarder 	{ VM_FAULT_HWPOISON_LARGE,      "HWPOISON_LARGE" },	\
14663d353901SSouptick Joarder 	{ VM_FAULT_SIGSEGV,             "SIGSEGV" },	\
14673d353901SSouptick Joarder 	{ VM_FAULT_NOPAGE,              "NOPAGE" },	\
14687a32b58bSSuren Baghdasaryan 	{ VM_FAULT_LOCKED,              "LOCKED" },	\
14697a32b58bSSuren Baghdasaryan 	{ VM_FAULT_RETRY,               "RETRY" },	\
14703d353901SSouptick Joarder 	{ VM_FAULT_FALLBACK,            "FALLBACK" },	\
1471f872f540SAndy Lutomirski 	{ VM_FAULT_DONE_COW,            "DONE_COW" },	\
1472f872f540SAndy Lutomirski 	{ VM_FAULT_NEEDDSYNC,           "NEEDDSYNC" },	\
1473f872f540SAndy Lutomirski 	{ VM_FAULT_COMPLETED,           "COMPLETED" }
1474f872f540SAndy Lutomirski 
1475f872f540SAndy Lutomirski struct vm_special_mapping {
1476f872f540SAndy Lutomirski 	const char *name;	/* The name, e.g. "[vdso]". */
1477f872f540SAndy Lutomirski 
1478f872f540SAndy Lutomirski 	/*
1479f872f540SAndy Lutomirski 	 * If .fault is not provided, this points to a
1480a62c34bdSAndy Lutomirski 	 * NULL-terminated array of pages that back the special mapping.
1481f872f540SAndy Lutomirski 	 *
1482f872f540SAndy Lutomirski 	 * This must not be NULL unless .fault is provided.
1483f872f540SAndy Lutomirski 	 */
1484f872f540SAndy Lutomirski 	struct page **pages;
1485f872f540SAndy Lutomirski 
1486b3ec9f33SSouptick Joarder 	/*
1487f872f540SAndy Lutomirski 	 * If non-NULL, then this is called to resolve page faults
1488f872f540SAndy Lutomirski 	 * on the special mapping.  If used, .pages is not checked.
1489b059a453SDmitry Safonov 	 */
1490b059a453SDmitry Safonov 	vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1491b059a453SDmitry Safonov 				struct vm_area_struct *vma,
1492223febc6SMichael Ellerman 				struct vm_fault *vmf);
1493223febc6SMichael Ellerman 
1494223febc6SMichael Ellerman 	int (*mremap)(const struct vm_special_mapping *sm,
1495a62c34bdSAndy Lutomirski 		     struct vm_area_struct *new_vma);
1496a62c34bdSAndy Lutomirski 
1497d17d8f9dSDave Hansen 	void (*close)(const struct vm_special_mapping *sm,
1498d17d8f9dSDave Hansen 		      struct vm_area_struct *vma);
1499d17d8f9dSDave Hansen };
1500d17d8f9dSDave Hansen 
1501d17d8f9dSDave Hansen enum tlb_flush_reason {
15025b74283aSMel Gorman 	TLB_FLUSH_ON_TASK_SWITCH,
15032815a56eSRik van Riel 	TLB_REMOTE_SHOOTDOWN,
1504d17d8f9dSDave Hansen 	TLB_LOCAL_SHOOTDOWN,
1505d17d8f9dSDave Hansen 	TLB_LOCAL_MM_SHOOTDOWN,
1506d17d8f9dSDave Hansen 	TLB_REMOTE_SEND_IPI,
150736090defSArnd Bergmann 	TLB_REMOTE_WRONG_CPU,
150836090defSArnd Bergmann 	NR_TLB_FLUSH_REASONS,
150936090defSArnd Bergmann };
151036090defSArnd Bergmann 
151136090defSArnd Bergmann /**
151236090defSArnd Bergmann  * enum fault_flag - Fault flag definitions.
151336090defSArnd Bergmann  * @FAULT_FLAG_WRITE: Fault was a write fault.
151436090defSArnd Bergmann  * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
151536090defSArnd Bergmann  * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
151636090defSArnd Bergmann  * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
151736090defSArnd Bergmann  * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
151836090defSArnd Bergmann  * @FAULT_FLAG_TRIED: The fault has been tried once.
15198d6a0ac0SDavid Hildenbrand  * @FAULT_FLAG_USER: The fault originated in userspace.
15208d6a0ac0SDavid Hildenbrand  * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
15218d6a0ac0SDavid Hildenbrand  * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
1522f46f2adeSPeter Xu  * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
1523f46f2adeSPeter Xu  * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
152455324e46SSuren Baghdasaryan  *                      COW mapping, making sure that an exclusive anon page is
152536090defSArnd Bergmann  *                      mapped after the fault.
152636090defSArnd Bergmann  * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
152736090defSArnd Bergmann  *                        We should only access orig_pte if this flag set.
152836090defSArnd Bergmann  * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock.
152936090defSArnd Bergmann  *
153036090defSArnd Bergmann  * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
153136090defSArnd Bergmann  * whether we would allow page faults to retry by specifying these two
153236090defSArnd Bergmann  * fault flags correctly.  Currently there can be three legal combinations:
153336090defSArnd Bergmann  *
153436090defSArnd Bergmann  * (a) ALLOW_RETRY and !TRIED:  this means the page fault allows retry, and
153536090defSArnd Bergmann  *                              this is the first try
153636090defSArnd Bergmann  *
153736090defSArnd Bergmann  * (b) ALLOW_RETRY and TRIED:   this means the page fault allows retry, and
153836090defSArnd Bergmann  *                              we've already tried at least once
153936090defSArnd Bergmann  *
154036090defSArnd Bergmann  * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
154136090defSArnd Bergmann  *
154236090defSArnd Bergmann  * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
154336090defSArnd Bergmann  * be used.  Note that page faults can be allowed to retry for multiple times,
1544c89357e2SDavid Hildenbrand  * in which case we'll have an initial fault with flags (a) then later on
1545c89357e2SDavid Hildenbrand  * continuous faults with flags (b).  We should always try to detect pending
1546c89357e2SDavid Hildenbrand  * signals before a retry to make sure the continuous page faults can still be
15478d6a0ac0SDavid Hildenbrand  * interrupted if necessary.
154836090defSArnd Bergmann  *
154936090defSArnd Bergmann  * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
155036090defSArnd Bergmann  * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
155136090defSArnd Bergmann  * applied to mappings that are not COW mappings.
155236090defSArnd Bergmann  */
155336090defSArnd Bergmann enum fault_flag {
155436090defSArnd Bergmann 	FAULT_FLAG_WRITE =		1 << 0,
155536090defSArnd Bergmann 	FAULT_FLAG_MKWRITE =		1 << 1,
155636090defSArnd Bergmann 	FAULT_FLAG_ALLOW_RETRY =	1 << 2,
155736090defSArnd Bergmann 	FAULT_FLAG_RETRY_NOWAIT = 	1 << 3,
155836090defSArnd Bergmann 	FAULT_FLAG_KILLABLE =		1 << 4,
155936090defSArnd Bergmann 	FAULT_FLAG_TRIED = 		1 << 5,
1560c89357e2SDavid Hildenbrand 	FAULT_FLAG_USER =		1 << 6,
1561f46f2adeSPeter Xu 	FAULT_FLAG_REMOTE =		1 << 7,
156255324e46SSuren Baghdasaryan 	FAULT_FLAG_INSTRUCTION =	1 << 8,
156336090defSArnd Bergmann 	FAULT_FLAG_INTERRUPTIBLE =	1 << 9,
156436090defSArnd Bergmann 	FAULT_FLAG_UNSHARE =		1 << 10,
156505e90bd0SPeter Xu 	FAULT_FLAG_ORIG_PTE_VALID =	1 << 11,
156605e90bd0SPeter Xu 	FAULT_FLAG_VMA_LOCK =		1 << 12,
15671b68112cSLance Yang };
15681b68112cSLance Yang 
15691b68112cSLance Yang typedef unsigned int __bitwise zap_flags_t;
15701b68112cSLance Yang 
15711b68112cSLance Yang /* Flags for clear_young_dirty_ptes(). */
15721b68112cSLance Yang typedef int __bitwise cydp_t;
15731b68112cSLance Yang 
15741b68112cSLance Yang /* Clear the access bit */
15751b68112cSLance Yang #define CYDP_CLEAR_YOUNG		((__force cydp_t)BIT(0))
1576b5054174SDavid Howells 
1577b5054174SDavid Howells /* Clear the dirty bit */
1578b5054174SDavid Howells #define CYDP_CLEAR_DIRTY		((__force cydp_t)BIT(1))
1579b5054174SDavid Howells 
1580b5054174SDavid Howells /*
1581b5054174SDavid Howells  * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
1582b5054174SDavid Howells  * other. Here is what they mean, and how to use them:
1583b5054174SDavid Howells  *
1584b5054174SDavid Howells  *
1585b5054174SDavid Howells  * FIXME: For pages which are part of a filesystem, mappings are subject to the
1586b5054174SDavid Howells  * lifetime enforced by the filesystem and we need guarantees that longterm
1587b5054174SDavid Howells  * users like RDMA and V4L2 only establish mappings which coordinate usage with
1588b5054174SDavid Howells  * the filesystem.  Ideas for this coordination include revoking the longterm
1589b5054174SDavid Howells  * pin, delaying writeback, bounce buffer page writeback, etc.  As FS DAX was
1590b5054174SDavid Howells  * added after the problem with filesystems was found FS DAX VMAs are
1591b5054174SDavid Howells  * specifically failed.  Filesystem pages are still subject to bugs and use of
1592b5054174SDavid Howells  * FOLL_LONGTERM should be avoided on those pages.
1593b5054174SDavid Howells  *
1594b5054174SDavid Howells  * In the CMA case: long term pins in a CMA region would unnecessarily fragment
1595b5054174SDavid Howells  * that region.  And so, CMA attempts to migrate the page before pinning, when
1596b5054174SDavid Howells  * FOLL_LONGTERM is specified.
1597b5054174SDavid Howells  *
1598b5054174SDavid Howells  * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
1599b5054174SDavid Howells  * but an additional pin counting system) will be invoked. This is intended for
1600b5054174SDavid Howells  * anything that gets a page reference and then touches page data (for example,
1601b5054174SDavid Howells  * Direct IO). This lets the filesystem know that some non-file-system entity is
1602b5054174SDavid Howells  * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
1603b5054174SDavid Howells  * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
1604b5054174SDavid Howells  * a call to unpin_user_page().
1605b5054174SDavid Howells  *
1606b5054174SDavid Howells  * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
1607b5054174SDavid Howells  * and separate refcounting mechanisms, however, and that means that each has
1608b5054174SDavid Howells  * its own acquire and release mechanisms:
1609b5054174SDavid Howells  *
1610b5054174SDavid Howells  *     FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
1611b5054174SDavid Howells  *
1612b5054174SDavid Howells  *     FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
1613b5054174SDavid Howells  *
1614b5054174SDavid Howells  * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
1615b5054174SDavid Howells  * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
1616b5054174SDavid Howells  * calls applied to them, and that's perfectly OK. This is a constraint on the
1617b5054174SDavid Howells  * callers, not on the pages.)
1618b5054174SDavid Howells  *
1619b5054174SDavid Howells  * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
1620b5054174SDavid Howells  * directly by the caller. That's in order to help avoid mismatches when
1621b5054174SDavid Howells  * releasing pages: get_user_pages*() pages must be released via put_page(),
1622b5054174SDavid Howells  * while pin_user_pages*() pages must be released via unpin_user_page().
16232c224108SJason Gunthorpe  *
16242c224108SJason Gunthorpe  * Please see Documentation/core-api/pin_user_pages.rst for more information.
16252c224108SJason Gunthorpe  */
16262c224108SJason Gunthorpe 
16272c224108SJason Gunthorpe enum {
16282c224108SJason Gunthorpe 	/* check pte is writable */
16292c224108SJason Gunthorpe 	FOLL_WRITE = 1 << 0,
16302c224108SJason Gunthorpe 	/* do get_page on page */
16312c224108SJason Gunthorpe 	FOLL_GET = 1 << 1,
16322c224108SJason Gunthorpe 	/* give error on hole if it would be zero */
16332c224108SJason Gunthorpe 	FOLL_DUMP = 1 << 2,
16342c224108SJason Gunthorpe 	/* get_user_pages read/write w/o permission */
16352c224108SJason Gunthorpe 	FOLL_FORCE = 1 << 3,
16362c224108SJason Gunthorpe 	/*
16372c224108SJason Gunthorpe 	 * if a disk transfer is needed, start the IO and return without waiting
16382c224108SJason Gunthorpe 	 * upon it
16392c224108SJason Gunthorpe 	 */
16402c224108SJason Gunthorpe 	FOLL_NOWAIT = 1 << 4,
16412c224108SJason Gunthorpe 	/* do not fault in pages */
16422c224108SJason Gunthorpe 	FOLL_NOFAULT = 1 << 5,
16432c224108SJason Gunthorpe 	/* check page is hwpoisoned */
16442c224108SJason Gunthorpe 	FOLL_HWPOISON = 1 << 6,
16452c224108SJason Gunthorpe 	/* don't do file mappings */
16462c224108SJason Gunthorpe 	FOLL_ANON = 1 << 7,
16472c224108SJason Gunthorpe 	/*
16482c224108SJason Gunthorpe 	 * FOLL_LONGTERM indicates that the page will be held for an indefinite
16492c224108SJason Gunthorpe 	 * time period _often_ under userspace control.  This is in contrast to
16502c224108SJason Gunthorpe 	 * iov_iter_get_pages(), whose usages are transient.
16512c224108SJason Gunthorpe 	 */
16522c224108SJason Gunthorpe 	FOLL_LONGTERM = 1 << 8,
16532c224108SJason Gunthorpe 	/* split huge pmd before returning */
16542c224108SJason Gunthorpe 	FOLL_SPLIT_PMD = 1 << 9,
1655d74943a2SDavid Hildenbrand 	/* allow returning PCI P2PDMA pages */
1656d74943a2SDavid Hildenbrand 	FOLL_PCI_P2PDMA = 1 << 10,
1657d74943a2SDavid Hildenbrand 	/* allow interrupts from generic signals */
1658d74943a2SDavid Hildenbrand 	FOLL_INTERRUPTIBLE = 1 << 11,
1659d74943a2SDavid Hildenbrand 	/*
1660d74943a2SDavid Hildenbrand 	 * Always honor (trigger) NUMA hinting faults.
1661d74943a2SDavid Hildenbrand 	 *
1662d74943a2SDavid Hildenbrand 	 * FOLL_WRITE implicitly honors NUMA hinting faults because a
1663d74943a2SDavid Hildenbrand 	 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE
16642c224108SJason Gunthorpe 	 * apply). get_user_pages_fast_only() always implicitly honors NUMA
16652c224108SJason Gunthorpe 	 * hinting faults.
16662c224108SJason Gunthorpe 	 */
1667b5054174SDavid Howells 	FOLL_HONOR_NUMA_FAULT = 1 << 12,
1668f2f48408SNanyong Sun 
1669f2f48408SNanyong Sun 	/* See also internal only FOLL flags in mm/internal.h */
1670f2f48408SNanyong Sun };
1671f2f48408SNanyong Sun 
1672f2f48408SNanyong Sun /* mm flags */
1673f2f48408SNanyong Sun 
1674f2f48408SNanyong Sun /*
1675f2f48408SNanyong Sun  * The first two bits represent core dump modes for set-user-ID,
1676f2f48408SNanyong Sun  * the modes are SUID_DUMP_* defined in linux/sched/coredump.h
1677f2f48408SNanyong Sun  */
1678f2f48408SNanyong Sun #define MMF_DUMPABLE_BITS 2
1679f2f48408SNanyong Sun #define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1)
1680f2f48408SNanyong Sun /* coredump filter bits */
1681f2f48408SNanyong Sun #define MMF_DUMP_ANON_PRIVATE	2
1682f2f48408SNanyong Sun #define MMF_DUMP_ANON_SHARED	3
1683f2f48408SNanyong Sun #define MMF_DUMP_MAPPED_PRIVATE	4
1684f2f48408SNanyong Sun #define MMF_DUMP_MAPPED_SHARED	5
1685f2f48408SNanyong Sun #define MMF_DUMP_ELF_HEADERS	6
1686f2f48408SNanyong Sun #define MMF_DUMP_HUGETLB_PRIVATE 7
1687f2f48408SNanyong Sun #define MMF_DUMP_HUGETLB_SHARED  8
1688f2f48408SNanyong Sun #define MMF_DUMP_DAX_PRIVATE	9
1689f2f48408SNanyong Sun #define MMF_DUMP_DAX_SHARED	10
1690f2f48408SNanyong Sun 
1691f2f48408SNanyong Sun #define MMF_DUMP_FILTER_SHIFT	MMF_DUMPABLE_BITS
1692f2f48408SNanyong Sun #define MMF_DUMP_FILTER_BITS	9
1693f2f48408SNanyong Sun #define MMF_DUMP_FILTER_MASK \
1694f2f48408SNanyong Sun 	(((1 << MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
1695f2f48408SNanyong Sun #define MMF_DUMP_FILTER_DEFAULT \
1696f2f48408SNanyong Sun 	((1 << MMF_DUMP_ANON_PRIVATE) |	(1 << MMF_DUMP_ANON_SHARED) |\
1697f2f48408SNanyong Sun 	 (1 << MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
1698f2f48408SNanyong Sun 
1699f2f48408SNanyong Sun #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
1700f2f48408SNanyong Sun # define MMF_DUMP_MASK_DEFAULT_ELF	(1 << MMF_DUMP_ELF_HEADERS)
1701f2f48408SNanyong Sun #else
1702f2f48408SNanyong Sun # define MMF_DUMP_MASK_DEFAULT_ELF	0
1703f2f48408SNanyong Sun #endif
1704f2f48408SNanyong Sun 					/* leave room for more dump flags */
1705f2f48408SNanyong Sun #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
1706f2f48408SNanyong Sun #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
1707f2f48408SNanyong Sun 
1708f2f48408SNanyong Sun /*
1709f2f48408SNanyong Sun  * This one-shot flag is dropped due to necessity of changing exe once again
1710f2f48408SNanyong Sun  * on NFS restore
1711f2f48408SNanyong Sun  */
1712f2f48408SNanyong Sun //#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
1713f2f48408SNanyong Sun 
1714f2f48408SNanyong Sun #define MMF_HAS_UPROBES		19	/* has uprobes */
1715f2f48408SNanyong Sun #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
1716f2f48408SNanyong Sun #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
1717f2f48408SNanyong Sun #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
1718f2f48408SNanyong Sun #define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
1719f2f48408SNanyong Sun #define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
1720f2f48408SNanyong Sun #define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
1721f2f48408SNanyong Sun #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
1722f2f48408SNanyong Sun #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
1723f2f48408SNanyong Sun /*
1724f2f48408SNanyong Sun  * MMF_HAS_PINNED: Whether this mm has pinned any pages.  This can be either
1725f2f48408SNanyong Sun  * replaced in the future by mm.pinned_vm when it becomes stable, or grow into
1726f2f48408SNanyong Sun  * a counter on its own. We're aggresive on this bit for now: even if the
1727f2f48408SNanyong Sun  * pinned pages were unpinned later on, we'll still keep this bit set for the
1728f2f48408SNanyong Sun  * lifecycle of this mm, just for simplicity.
1729f2f48408SNanyong Sun  */
1730f2f48408SNanyong Sun #define MMF_HAS_PINNED		27	/* FOLL_PIN has run, never cleared */
1731f2f48408SNanyong Sun 
1732f2f48408SNanyong Sun #define MMF_HAS_MDWE		28
1733f2f48408SNanyong Sun #define MMF_HAS_MDWE_MASK	(1 << MMF_HAS_MDWE)
1734f2f48408SNanyong Sun 
1735f2f48408SNanyong Sun 
1736f2f48408SNanyong Sun #define MMF_HAS_MDWE_NO_INHERIT	29
1737f2f48408SNanyong Sun 
1738f2f48408SNanyong Sun #define MMF_VM_MERGE_ANY	30
1739f2f48408SNanyong Sun #define MMF_VM_MERGE_ANY_MASK	(1 << MMF_VM_MERGE_ANY)
1740f2f48408SNanyong Sun 
1741f2f48408SNanyong Sun #define MMF_TOPDOWN		31	/* mm searches top down by default */
1742f2f48408SNanyong Sun #define MMF_TOPDOWN_MASK	(1 << MMF_TOPDOWN)
1743f2f48408SNanyong Sun 
1744f2f48408SNanyong Sun #define MMF_INIT_MASK		(MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
1745f2f48408SNanyong Sun 				 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\
1746f2f48408SNanyong Sun 				 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK)
1747f2f48408SNanyong Sun 
mmf_init_flags(unsigned long flags)1748f2f48408SNanyong Sun static inline unsigned long mmf_init_flags(unsigned long flags)
1749f2f48408SNanyong Sun {
1750f2f48408SNanyong Sun 	if (flags & (1UL << MMF_HAS_MDWE_NO_INHERIT))
1751f2f48408SNanyong Sun 		flags &= ~((1UL << MMF_HAS_MDWE) |
17525b99cd0eSHeiko Carstens 			   (1UL << MMF_HAS_MDWE_NO_INHERIT));
1753 	return flags & MMF_INIT_MASK;
1754 }
1755 
1756 #endif /* _LINUX_MM_TYPES_H */
1757