xref: /linux/include/linux/mm_types.h (revision 7135a05838e85845e1bbf6caa6596a7e3b791a40)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MM_TYPES_H
3 #define _LINUX_MM_TYPES_H
4 
5 #include <linux/mm_types_task.h>
6 
7 #include <linux/auxvec.h>
8 #include <linux/kref.h>
9 #include <linux/list.h>
10 #include <linux/spinlock.h>
11 #include <linux/rbtree.h>
12 #include <linux/maple_tree.h>
13 #include <linux/rwsem.h>
14 #include <linux/completion.h>
15 #include <linux/cpumask.h>
16 #include <linux/uprobes.h>
17 #include <linux/rcupdate.h>
18 #include <linux/page-flags-layout.h>
19 #include <linux/workqueue.h>
20 #include <linux/seqlock.h>
21 #include <linux/percpu_counter.h>
22 #include <linux/types.h>
23 #include <linux/futex_types.h>
24 #include <linux/rseq_types.h>
25 #include <linux/bitmap.h>
26 
27 #include <asm/mmu.h>
28 
29 #ifndef AT_VECTOR_SIZE_ARCH
30 #define AT_VECTOR_SIZE_ARCH 0
31 #endif
32 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
33 
34 
35 struct address_space;
36 struct futex_private_hash;
37 struct mem_cgroup;
38 
39 typedef struct {
40 	unsigned long f;
41 } memdesc_flags_t;
42 
43 /*
44  * Each physical page in the system has a struct page associated with
45  * it to keep track of whatever it is we are using the page for at the
46  * moment. Note that we have no way to track which tasks are using
47  * a page, though if it is a pagecache page, rmap structures can tell us
48  * who is mapping it.
49  *
50  * If you allocate the page using alloc_pages(), you can use some of the
51  * space in struct page for your own purposes.  The five words in the main
52  * union are available, except for bit 0 of the first word which must be
53  * kept clear.  Many users use this word to store a pointer to an object
54  * which is guaranteed to be aligned.  If you use the same storage as
55  * page->mapping, you must restore it to NULL before freeing the page.
56  *
57  * The mapcount field must not be used for own purposes.
58  *
59  * If you want to use the refcount field, it must be used in such a way
60  * that other CPUs temporarily incrementing and then decrementing the
61  * refcount does not cause problems.  On receiving the page from
62  * alloc_pages(), the refcount will be positive.
63  *
64  * If you allocate pages of order > 0, you can use some of the fields
65  * in each subpage, but you may need to restore some of their values
66  * afterwards.
67  *
68  * SLUB uses cmpxchg_double() to atomically update its freelist and counters.
69  * That requires that freelist & counters in struct slab be adjacent and
70  * double-word aligned. Because struct slab currently just reinterprets the
71  * bits of struct page, we align all struct pages to double-word boundaries,
72  * and ensure that 'freelist' is aligned within struct slab.
73  */
74 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
75 #define _struct_page_alignment	__aligned(2 * sizeof(unsigned long))
76 #else
77 #define _struct_page_alignment	__aligned(sizeof(unsigned long))
78 #endif
79 
80 struct page {
81 	memdesc_flags_t flags;		/* Atomic flags, some possibly
82 					 * updated asynchronously */
83 	/*
84 	 * Five words (20/40 bytes) are available in this union.
85 	 * WARNING: bit 0 of the first word is used for PageTail(). That
86 	 * means the other users of this union MUST NOT use the bit to
87 	 * avoid collision and false-positive PageTail().
88 	 */
89 	union {
90 		struct {	/* Page cache and anonymous pages */
91 			/**
92 			 * @lru: Pageout list, eg. active_list protected by
93 			 * lruvec->lru_lock.  Sometimes used as a generic list
94 			 * by the page owner.
95 			 */
96 			union {
97 				struct list_head lru;
98 
99 				/* Or, free page */
100 				struct list_head buddy_list;
101 				struct list_head pcp_list;
102 				struct llist_node pcp_llist;
103 			};
104 			struct address_space *mapping;
105 			union {
106 				pgoff_t __folio_index;		/* Our offset within mapping. */
107 				unsigned long share;	/* share count for fsdax */
108 			};
109 			/**
110 			 * @private: Mapping-private opaque data.
111 			 * Usually used for buffer_heads.
112 			 * Used for swp_entry_t if swapcache flag set.
113 			 * Indicates order in the buddy system if PageBuddy
114 			 * or on pcp_llist.
115 			 */
116 			unsigned long private;
117 		};
118 		struct {	/* page_pool used by netstack */
119 			/**
120 			 * @pp_magic: magic value to avoid recycling non
121 			 * page_pool allocated pages.
122 			 */
123 			unsigned long pp_magic;
124 			struct page_pool *pp;
125 			unsigned long _pp_mapping_pad;
126 			unsigned long dma_addr;
127 			atomic_long_t pp_ref_count;
128 		};
129 		struct {	/* Tail pages of compound page */
130 			unsigned long compound_info;	/* Bit zero is set */
131 		};
132 		struct {	/* ZONE_DEVICE pages */
133 			/*
134 			 * The first word is used for compound_info or folio
135 			 * pgmap
136 			 */
137 			void *_unused_pgmap_compound_info;
138 			void *zone_device_data;
139 			/*
140 			 * ZONE_DEVICE private pages are counted as being
141 			 * mapped so the next 3 words hold the mapping, index,
142 			 * and private fields from the source anonymous or
143 			 * page cache page while the page is migrated to device
144 			 * private memory.
145 			 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
146 			 * use the mapping, index, and private fields when
147 			 * pmem backed DAX files are mapped.
148 			 */
149 		};
150 
151 		/** @rcu_head: You can use this to free a page by RCU. */
152 		struct rcu_head rcu_head;
153 	};
154 
155 	union {		/* This union is 4 bytes in size. */
156 		/*
157 		 * For head pages of typed folios, the value stored here
158 		 * allows for determining what this page is used for. The
159 		 * tail pages of typed folios will not store a type
160 		 * (page_type == _mapcount == -1).
161 		 *
162 		 * See page-flags.h for a list of page types which are currently
163 		 * stored here.
164 		 *
165 		 * Owners of typed folios may reuse the lower 16 bit of the
166 		 * head page page_type field after setting the page type,
167 		 * but must reset these 16 bit to -1 before clearing the
168 		 * page type.
169 		 */
170 		unsigned int page_type;
171 
172 		/*
173 		 * For pages that are part of non-typed folios for which mappings
174 		 * are tracked via the RMAP, encodes the number of times this page
175 		 * is directly referenced by a page table.
176 		 *
177 		 * Note that the mapcount is always initialized to -1, so that
178 		 * transitions both from it and to it can be tracked, using
179 		 * atomic_inc_and_test() and atomic_add_negative(-1).
180 		 */
181 		atomic_t _mapcount;
182 	};
183 
184 	/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
185 	atomic_t _refcount;
186 
187 #ifdef CONFIG_MEMCG
188 	unsigned long memcg_data;
189 #elif defined(CONFIG_SLAB_OBJ_EXT)
190 	unsigned long _unused_slab_obj_exts;
191 #endif
192 
193 	/*
194 	 * On machines where all RAM is mapped into kernel address space,
195 	 * we can simply calculate the virtual address. On machines with
196 	 * highmem some memory is mapped into kernel virtual memory
197 	 * dynamically, so we need a place to store that address.
198 	 * Note that this field could be 16 bits on x86 ... ;)
199 	 *
200 	 * Architectures with slow multiplication can define
201 	 * WANT_PAGE_VIRTUAL in asm/page.h
202 	 */
203 #if defined(WANT_PAGE_VIRTUAL)
204 	void *virtual;			/* Kernel virtual address (NULL if
205 					   not kmapped, ie. highmem) */
206 #endif /* WANT_PAGE_VIRTUAL */
207 
208 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
209 	int _last_cpupid;
210 #endif
211 
212 #ifdef CONFIG_KMSAN
213 	/*
214 	 * KMSAN metadata for this page:
215 	 *  - shadow page: every bit indicates whether the corresponding
216 	 *    bit of the original page is initialized (0) or not (1);
217 	 *  - origin page: every 4 bytes contain an id of the stack trace
218 	 *    where the uninitialized value was created.
219 	 */
220 	struct page *kmsan_shadow;
221 	struct page *kmsan_origin;
222 #endif
223 } _struct_page_alignment;
224 
225 /*
226  * struct encoded_page - a nonexistent type marking this pointer
227  *
228  * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
229  * with the low bits of the pointer indicating extra context-dependent
230  * information. Only used in mmu_gather handling, and this acts as a type
231  * system check on that use.
232  *
233  * We only really have two guaranteed bits in general, although you could
234  * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
235  * for more.
236  *
237  * Use the supplied helper functions to endcode/decode the pointer and bits.
238  */
239 struct encoded_page;
240 
241 #define ENCODED_PAGE_BITS			3ul
242 
243 /* Perform rmap removal after we have flushed the TLB. */
244 #define ENCODED_PAGE_BIT_DELAY_RMAP		1ul
245 
246 /*
247  * The next item in an encoded_page array is the "nr_pages" argument, specifying
248  * the number of consecutive pages starting from this page, that all belong to
249  * the same folio. For example, "nr_pages" corresponds to the number of folio
250  * references that must be dropped. If this bit is not set, "nr_pages" is
251  * implicitly 1.
252  */
253 #define ENCODED_PAGE_BIT_NR_PAGES_NEXT		2ul
254 
255 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
256 {
257 	BUILD_BUG_ON(flags > ENCODED_PAGE_BITS);
258 	return (struct encoded_page *)(flags | (unsigned long)page);
259 }
260 
261 static inline unsigned long encoded_page_flags(struct encoded_page *page)
262 {
263 	return ENCODED_PAGE_BITS & (unsigned long)page;
264 }
265 
266 static inline struct page *encoded_page_ptr(struct encoded_page *page)
267 {
268 	return (struct page *)(~ENCODED_PAGE_BITS & (unsigned long)page);
269 }
270 
271 static __always_inline struct encoded_page *encode_nr_pages(unsigned long nr)
272 {
273 	VM_WARN_ON_ONCE((nr << 2) >> 2 != nr);
274 	return (struct encoded_page *)(nr << 2);
275 }
276 
277 static __always_inline unsigned long encoded_nr_pages(struct encoded_page *page)
278 {
279 	return ((unsigned long)page) >> 2;
280 }
281 
282 /*
283  * A swap entry has to fit into a "unsigned long", as the entry is hidden
284  * in the "index" field of the swapper address space.
285  */
286 typedef struct {
287 	unsigned long val;
288 } swp_entry_t;
289 
290 /**
291  * typedef softleaf_t - Describes a page table software leaf entry, abstracted
292  * from its architecture-specific encoding.
293  *
294  * Page table leaf entries are those which do not reference any descendent page
295  * tables but rather either reference a data page, are an empty (or 'none'
296  * entry), or contain a non-present entry.
297  *
298  * If referencing another page table or a data page then the page table entry is
299  * pertinent to hardware - that is it tells the hardware how to decode the page
300  * table entry.
301  *
302  * Otherwise it is a software-defined leaf page table entry, which this type
303  * describes. See leafops.h and specifically @softleaf_type for a list of all
304  * possible kinds of software leaf entry.
305  *
306  * A softleaf_t entry is abstracted from the hardware page table entry, so is
307  * not architecture-specific.
308  *
309  * NOTE: While we transition from the confusing swp_entry_t type used for this
310  *       purpose, we simply alias this type. This will be removed once the
311  *       transition is complete.
312  */
313 typedef swp_entry_t softleaf_t;
314 
315 #if defined(CONFIG_MEMCG) || defined(CONFIG_SLAB_OBJ_EXT)
316 /* We have some extra room after the refcount in tail pages. */
317 #define NR_PAGES_IN_LARGE_FOLIO
318 #endif
319 
320 /*
321  * On 32bit, we can cut the required metadata in half, because:
322  * (a) PID_MAX_LIMIT implicitly limits the number of MMs we could ever have,
323  *     so we can limit MM IDs to 15 bit (32767).
324  * (b) We don't expect folios where even a single complete PTE mapping by
325  *     one MM would exceed 15 bits (order-15).
326  */
327 #ifdef CONFIG_64BIT
328 typedef int mm_id_mapcount_t;
329 #define MM_ID_MAPCOUNT_MAX		INT_MAX
330 typedef unsigned int mm_id_t;
331 #else /* !CONFIG_64BIT */
332 typedef short mm_id_mapcount_t;
333 #define MM_ID_MAPCOUNT_MAX		SHRT_MAX
334 typedef unsigned short mm_id_t;
335 #endif /* CONFIG_64BIT */
336 
337 /* We implicitly use the dummy ID for init-mm etc. where we never rmap pages. */
338 #define MM_ID_DUMMY			0
339 #define MM_ID_MIN			(MM_ID_DUMMY + 1)
340 
341 /*
342  * We leave the highest bit of each MM id unused, so we can store a flag
343  * in the highest bit of each folio->_mm_id[].
344  */
345 #define MM_ID_BITS			((sizeof(mm_id_t) * BITS_PER_BYTE) - 1)
346 #define MM_ID_MASK			((1U << MM_ID_BITS) - 1)
347 #define MM_ID_MAX			MM_ID_MASK
348 
349 /*
350  * In order to use bit_spin_lock(), which requires an unsigned long, we
351  * operate on folio->_mm_ids when working on flags.
352  */
353 #define FOLIO_MM_IDS_LOCK_BITNUM	MM_ID_BITS
354 #define FOLIO_MM_IDS_LOCK_BIT		BIT(FOLIO_MM_IDS_LOCK_BITNUM)
355 #define FOLIO_MM_IDS_SHARED_BITNUM	(2 * MM_ID_BITS + 1)
356 #define FOLIO_MM_IDS_SHARED_BIT		BIT(FOLIO_MM_IDS_SHARED_BITNUM)
357 
358 /**
359  * struct folio - Represents a contiguous set of bytes.
360  * @flags: Identical to the page flags.
361  * @lru: Least Recently Used list; tracks how recently this folio was used.
362  * @mlock_count: Number of times this folio has been pinned by mlock().
363  * @mapping: The file this page belongs to, or refers to the anon_vma for
364  *    anonymous memory.
365  * @index: Offset within the file, in units of pages.  For anonymous memory,
366  *    this is the index from the beginning of the mmap.
367  * @share: number of DAX mappings that reference this folio. See
368  *    dax_associate_entry.
369  * @private: Filesystem per-folio data (see folio_attach_private()).
370  * @swap: Used for swp_entry_t if folio_test_swapcache().
371  * @migrate_info: Stores migration state (anon_vma pointer and
372  *    FOLIO_WAS_* markers).
373  * @_mapcount: Do not access this member directly.  Use folio_mapcount() to
374  *    find out how many times this folio is mapped by userspace.
375  * @_refcount: Do not access this member directly.  Use folio_ref_count()
376  *    to find how many references there are to this folio.
377  * @memcg_data: Memory Control Group data.
378  * @pgmap: Metadata for ZONE_DEVICE mappings
379  * @virtual: Virtual address in the kernel direct map.
380  * @_last_cpupid: IDs of last CPU and last process that accessed the folio.
381  * @_entire_mapcount: Do not use directly, call folio_entire_mapcount().
382  * @_large_mapcount: Do not use directly, call folio_mapcount().
383  * @_nr_pages_mapped: Do not use outside of rmap and debug code.
384  * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
385  * @_nr_pages: Do not use directly, call folio_nr_pages().
386  * @_mm_id: Do not use outside of rmap code.
387  * @_mm_ids: Do not use outside of rmap code.
388  * @_mm_id_mapcount: Do not use outside of rmap code.
389  * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
390  * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
391  * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
392  * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
393  * @_deferred_list: Folios to be split under memory pressure.
394  * @_unused_slab_obj_exts: Placeholder to match obj_exts in struct slab.
395  *
396  * A folio is a physically, virtually and logically contiguous set
397  * of bytes.  It is a power-of-two in size, and it is aligned to that
398  * same power-of-two.  It is at least as large as %PAGE_SIZE.  If it is
399  * in the page cache, it is at a file offset which is a multiple of that
400  * power-of-two.  It may be mapped into userspace at an address which is
401  * at an arbitrary page offset, but its kernel virtual address is aligned
402  * to its size.
403  */
404 struct folio {
405 	/* private: don't document the anon union */
406 	union {
407 		struct {
408 	/* public: */
409 			memdesc_flags_t flags;
410 			union {
411 				struct list_head lru;
412 	/* private: avoid cluttering the output */
413 				/* For the Unevictable "LRU list" slot */
414 				struct {
415 					/* Avoid compound_info */
416 					void *__filler;
417 	/* public: */
418 					unsigned int mlock_count;
419 	/* private: */
420 				};
421 	/* public: */
422 				struct dev_pagemap *pgmap;
423 			};
424 			struct address_space *mapping;
425 			union {
426 				pgoff_t index;
427 				unsigned long share;
428 			};
429 			union {
430 				void *private;
431 				swp_entry_t swap;
432 				unsigned long migrate_info;
433 			};
434 			atomic_t _mapcount;
435 			atomic_t _refcount;
436 #ifdef CONFIG_MEMCG
437 			unsigned long memcg_data;
438 #elif defined(CONFIG_SLAB_OBJ_EXT)
439 			unsigned long _unused_slab_obj_exts;
440 #endif
441 #if defined(WANT_PAGE_VIRTUAL)
442 			void *virtual;
443 #endif
444 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
445 			int _last_cpupid;
446 #endif
447 	/* private: the union with struct page is transitional */
448 		};
449 		struct page page;
450 	};
451 	union {
452 		struct {
453 			unsigned long _flags_1;
454 			unsigned long _head_1;
455 			union {
456 				struct {
457 	/* public: */
458 					atomic_t _large_mapcount;
459 					atomic_t _nr_pages_mapped;
460 #ifdef CONFIG_64BIT
461 					atomic_t _entire_mapcount;
462 					atomic_t _pincount;
463 #endif /* CONFIG_64BIT */
464 					mm_id_mapcount_t _mm_id_mapcount[2];
465 					union {
466 						mm_id_t _mm_id[2];
467 						unsigned long _mm_ids;
468 					};
469 	/* private: the union with struct page is transitional */
470 				};
471 				unsigned long _usable_1[4];
472 			};
473 			atomic_t _mapcount_1;
474 			atomic_t _refcount_1;
475 	/* public: */
476 #ifdef NR_PAGES_IN_LARGE_FOLIO
477 			unsigned int _nr_pages;
478 #endif /* NR_PAGES_IN_LARGE_FOLIO */
479 	/* private: the union with struct page is transitional */
480 		};
481 		struct page __page_1;
482 	};
483 	union {
484 		struct {
485 			unsigned long _flags_2;
486 			unsigned long _head_2;
487 	/* public: */
488 			struct list_head _deferred_list;
489 #ifndef CONFIG_64BIT
490 			atomic_t _entire_mapcount;
491 			atomic_t _pincount;
492 #endif /* !CONFIG_64BIT */
493 	/* private: the union with struct page is transitional */
494 		};
495 		struct page __page_2;
496 	};
497 	union {
498 		struct {
499 			unsigned long _flags_3;
500 			unsigned long _head_3;
501 	/* public: */
502 			void *_hugetlb_subpool;
503 			void *_hugetlb_cgroup;
504 			void *_hugetlb_cgroup_rsvd;
505 			void *_hugetlb_hwpoison;
506 	/* private: the union with struct page is transitional */
507 		};
508 		struct page __page_3;
509 	};
510 };
511 
512 #define FOLIO_MATCH(pg, fl)						\
513 	static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
514 FOLIO_MATCH(flags, flags);
515 FOLIO_MATCH(lru, lru);
516 FOLIO_MATCH(mapping, mapping);
517 FOLIO_MATCH(compound_info, lru);
518 FOLIO_MATCH(__folio_index, index);
519 FOLIO_MATCH(private, private);
520 FOLIO_MATCH(_mapcount, _mapcount);
521 FOLIO_MATCH(_refcount, _refcount);
522 #ifdef CONFIG_MEMCG
523 FOLIO_MATCH(memcg_data, memcg_data);
524 #endif
525 #if defined(WANT_PAGE_VIRTUAL)
526 FOLIO_MATCH(virtual, virtual);
527 #endif
528 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
529 FOLIO_MATCH(_last_cpupid, _last_cpupid);
530 #endif
531 #undef FOLIO_MATCH
532 #define FOLIO_MATCH(pg, fl)						\
533 	static_assert(offsetof(struct folio, fl) ==			\
534 			offsetof(struct page, pg) + sizeof(struct page))
535 FOLIO_MATCH(flags, _flags_1);
536 FOLIO_MATCH(compound_info, _head_1);
537 FOLIO_MATCH(_mapcount, _mapcount_1);
538 FOLIO_MATCH(_refcount, _refcount_1);
539 #undef FOLIO_MATCH
540 #define FOLIO_MATCH(pg, fl)						\
541 	static_assert(offsetof(struct folio, fl) ==			\
542 			offsetof(struct page, pg) + 2 * sizeof(struct page))
543 FOLIO_MATCH(flags, _flags_2);
544 FOLIO_MATCH(compound_info, _head_2);
545 #undef FOLIO_MATCH
546 #define FOLIO_MATCH(pg, fl)						\
547 	static_assert(offsetof(struct folio, fl) ==			\
548 			offsetof(struct page, pg) + 3 * sizeof(struct page))
549 FOLIO_MATCH(flags, _flags_3);
550 FOLIO_MATCH(compound_info, _head_3);
551 #undef FOLIO_MATCH
552 
553 /**
554  * struct ptdesc -    Memory descriptor for page tables.
555  * @pt_flags: enum pt_flags plus zone/node/section.
556  * @pt_rcu_head:      For freeing page table pages.
557  * @pt_list:          List of used page tables. Used for s390 gmap shadow pages
558  *                    (which are not linked into the user page tables) and x86
559  *                    pgds.
560  * @_pt_pad_1:        Padding that aliases with page's compound head.
561  * @pmd_huge_pte:     Protected by ptdesc->ptl, used for THPs.
562  * @__page_mapping:   Aliases with page->mapping. Unused for page tables.
563  * @pt_index:         Used for s390 gmap.
564  * @pt_mm:            Used for x86 pgds.
565  * @pt_frag_refcount: For fragmented page table tracking. Powerpc only.
566  * @pt_share_count:   Used for HugeTLB PMD page table share count.
567  * @_pt_pad_2:        Padding to ensure proper alignment.
568  * @ptl:              Lock for the page table.
569  * @__page_type:      Same as page->page_type. Unused for page tables.
570  * @__page_refcount:  Same as page refcount.
571  * @pt_memcg_data:    Memcg data. Tracked for page tables here.
572  *
573  * This struct overlays struct page for now. Do not modify without a good
574  * understanding of the issues.
575  */
576 struct ptdesc {
577 	memdesc_flags_t pt_flags;
578 
579 	union {
580 		struct rcu_head pt_rcu_head;
581 		struct list_head pt_list;
582 		struct {
583 			unsigned long _pt_pad_1;
584 			pgtable_t pmd_huge_pte;
585 		};
586 	};
587 	unsigned long __page_mapping;
588 
589 	union {
590 		pgoff_t pt_index;
591 		struct mm_struct *pt_mm;
592 		atomic_t pt_frag_refcount;
593 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
594 		atomic_t pt_share_count;
595 #endif
596 	};
597 
598 	union {
599 		unsigned long _pt_pad_2;
600 #if ALLOC_SPLIT_PTLOCKS
601 		spinlock_t *ptl;
602 #else
603 		spinlock_t ptl;
604 #endif
605 	};
606 	unsigned int __page_type;
607 	atomic_t __page_refcount;
608 #ifdef CONFIG_MEMCG
609 	unsigned long pt_memcg_data;
610 #endif
611 };
612 
613 #define TABLE_MATCH(pg, pt)						\
614 	static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt))
615 TABLE_MATCH(flags, pt_flags);
616 TABLE_MATCH(compound_info, pt_list);
617 TABLE_MATCH(compound_info, _pt_pad_1);
618 TABLE_MATCH(mapping, __page_mapping);
619 TABLE_MATCH(__folio_index, pt_index);
620 TABLE_MATCH(rcu_head, pt_rcu_head);
621 TABLE_MATCH(page_type, __page_type);
622 TABLE_MATCH(_refcount, __page_refcount);
623 #ifdef CONFIG_MEMCG
624 TABLE_MATCH(memcg_data, pt_memcg_data);
625 #endif
626 #undef TABLE_MATCH
627 static_assert(sizeof(struct ptdesc) <= sizeof(struct page));
628 
629 #define ptdesc_page(pt)			(_Generic((pt),			\
630 	const struct ptdesc *:		(const struct page *)(pt),	\
631 	struct ptdesc *:		(struct page *)(pt)))
632 
633 #define ptdesc_folio(pt)		(_Generic((pt),			\
634 	const struct ptdesc *:		(const struct folio *)(pt),	\
635 	struct ptdesc *:		(struct folio *)(pt)))
636 
637 #define page_ptdesc(p)			(_Generic((p),			\
638 	const struct page *:		(const struct ptdesc *)(p),	\
639 	struct page *:			(struct ptdesc *)(p)))
640 
641 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
642 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
643 {
644 	atomic_set(&ptdesc->pt_share_count, 0);
645 }
646 
647 static inline void ptdesc_pmd_pts_inc(struct ptdesc *ptdesc)
648 {
649 	atomic_inc(&ptdesc->pt_share_count);
650 }
651 
652 static inline void ptdesc_pmd_pts_dec(struct ptdesc *ptdesc)
653 {
654 	atomic_dec(&ptdesc->pt_share_count);
655 }
656 
657 static inline int ptdesc_pmd_pts_count(const struct ptdesc *ptdesc)
658 {
659 	return atomic_read(&ptdesc->pt_share_count);
660 }
661 
662 static inline bool ptdesc_pmd_is_shared(struct ptdesc *ptdesc)
663 {
664 	return !!ptdesc_pmd_pts_count(ptdesc);
665 }
666 #else
667 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
668 {
669 }
670 #endif
671 
672 /*
673  * Used for sizing the vmemmap region on some architectures
674  */
675 #define STRUCT_PAGE_MAX_SHIFT	(order_base_2(sizeof(struct page)))
676 
677 /*
678  * page_private can be used on tail pages.  However, it is only
679  * checked by the VM on the head page.  So page_private on the tail pages
680  * should be used for data that's ancillary to the head page (eg attaching
681  * buffer heads to tail pages after attaching buffer heads to the head page)
682  */
683 #define page_private(page)		((page)->private)
684 
685 static inline void set_page_private(struct page *page, unsigned long private)
686 {
687 	page->private = private;
688 }
689 
690 static inline void *folio_get_private(const struct folio *folio)
691 {
692 	return folio->private;
693 }
694 
695 typedef unsigned long vm_flags_t;
696 
697 /*
698  * freeptr_t represents a SLUB freelist pointer, which might be encoded
699  * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled.
700  */
701 typedef struct { unsigned long v; } freeptr_t;
702 
703 /*
704  * A region containing a mapping of a non-memory backed file under NOMMU
705  * conditions.  These are held in a global tree and are pinned by the VMAs that
706  * map parts of them.
707  */
708 struct vm_region {
709 	struct rb_node	vm_rb;		/* link in global region tree */
710 	vm_flags_t	vm_flags;	/* VMA vm_flags */
711 	unsigned long	vm_start;	/* start address of region */
712 	unsigned long	vm_end;		/* region initialised to here */
713 	unsigned long	vm_top;		/* region allocated to here */
714 	unsigned long	vm_pgoff;	/* the offset in vm_file corresponding to vm_start */
715 	struct file	*vm_file;	/* the backing file or NULL */
716 
717 	int		vm_usage;	/* region usage count (access under nommu_region_sem) */
718 	bool		vm_icache_flushed : 1; /* true if the icache has been flushed for
719 						* this region */
720 };
721 
722 #ifdef CONFIG_USERFAULTFD
723 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
724 struct vm_userfaultfd_ctx {
725 	struct userfaultfd_ctx *ctx;
726 };
727 #else /* CONFIG_USERFAULTFD */
728 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
729 struct vm_userfaultfd_ctx {};
730 #endif /* CONFIG_USERFAULTFD */
731 
732 struct anon_vma_name {
733 	struct kref kref;
734 	/* The name needs to be at the end because it is dynamically sized. */
735 	char name[];
736 };
737 
738 #ifdef CONFIG_ANON_VMA_NAME
739 /*
740  * mmap_lock should be read-locked when calling anon_vma_name(). Caller should
741  * either keep holding the lock while using the returned pointer or it should
742  * raise anon_vma_name refcount before releasing the lock.
743  */
744 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma);
745 struct anon_vma_name *anon_vma_name_alloc(const char *name);
746 void anon_vma_name_free(struct kref *kref);
747 #else /* CONFIG_ANON_VMA_NAME */
748 static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
749 {
750 	return NULL;
751 }
752 
753 static inline struct anon_vma_name *anon_vma_name_alloc(const char *name)
754 {
755 	return NULL;
756 }
757 #endif
758 
759 /*
760  * While __vma_enter_locked() is working to ensure are no read-locks held on a
761  * VMA (either while acquiring a VMA write lock or marking a VMA detached) we
762  * set the VM_REFCNT_EXCLUDE_READERS_FLAG in vma->vm_refcnt to indicate to
763  * vma_start_read() that the reference count should be left alone.
764  *
765  * See the comment describing vm_refcnt in vm_area_struct for details as to
766  * which values the VMA reference count can be.
767  */
768 #define VM_REFCNT_EXCLUDE_READERS_BIT	(30)
769 #define VM_REFCNT_EXCLUDE_READERS_FLAG	(1U << VM_REFCNT_EXCLUDE_READERS_BIT)
770 #define VM_REFCNT_LIMIT			(VM_REFCNT_EXCLUDE_READERS_FLAG - 1)
771 
772 struct vma_numab_state {
773 	/*
774 	 * Initialised as time in 'jiffies' after which VMA
775 	 * should be scanned.  Delays first scan of new VMA by at
776 	 * least sysctl_numa_balancing_scan_delay:
777 	 */
778 	unsigned long next_scan;
779 
780 	/*
781 	 * Time in jiffies when pids_active[] is reset to
782 	 * detect phase change behaviour:
783 	 */
784 	unsigned long pids_active_reset;
785 
786 	/*
787 	 * Approximate tracking of PIDs that trapped a NUMA hinting
788 	 * fault. May produce false positives due to hash collisions.
789 	 *
790 	 *   [0] Previous PID tracking
791 	 *   [1] Current PID tracking
792 	 *
793 	 * Window moves after next_pid_reset has expired approximately
794 	 * every VMA_PID_RESET_PERIOD jiffies:
795 	 */
796 	unsigned long pids_active[2];
797 
798 	/* MM scan sequence ID when scan first started after VMA creation */
799 	int start_scan_seq;
800 
801 	/*
802 	 * MM scan sequence ID when the VMA was last completely scanned.
803 	 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq
804 	 */
805 	int prev_scan_seq;
806 };
807 
808 #ifdef __HAVE_PFNMAP_TRACKING
809 struct pfnmap_track_ctx {
810 	struct kref kref;
811 	unsigned long pfn;
812 	unsigned long size;	/* in bytes */
813 };
814 #endif
815 
816 /* What action should be taken after an .mmap_prepare call is complete? */
817 enum mmap_action_type {
818 	MMAP_NOTHING,
819 	MMAP_REMAP_PFN,
820 	MMAP_IO_REMAP_PFN,
821 	MMAP_SIMPLE_IO_REMAP,		/* I/O remap with guardrails. */
822 	MMAP_KERNEL_PAGES,		/* Map kernel page range from array. */
823 	MMAP_DISCONTIG_KERNEL_PAGES,	/* Map kernel discontig page range. */
824 };
825 
826 enum discontig_kernel_page_action {
827 	DISCONTIG_KERNEL_PAGE_ABORT,
828 	DISCONTIG_KERNEL_PAGE_MAP_PAGES,	/* Array of individual pages. */
829 	DISCONTIG_KERNEL_PAGE_MAP_CONTIG_PAGES,	/* 1..N contiguous pages. */
830 };
831 
832 struct discontig_kernel_page_state {
833 	/* Map state. */
834 	const unsigned long start;	/* Start address of VMA. */
835 	const unsigned long end;	/* End address of VMA. */
836 	unsigned long addr;		/* The current address to be mapped. */
837 	pgoff_t pgoff;			/* The current pgoff to be mapped. */
838 	unsigned long nr_pages_mapped;	/* The number of pages mapped. */
839 	unsigned long nr_pages_remain;	/* The number of pages remaining. */
840 
841 	/* User-defined state. */
842 	void *vm_private_data;		/* VMA private data. */
843 	void *private;			/* Mapping private data. */
844 
845 	/* Users should not touch these, use discontig_kernel_map_*() helpers. */
846 	enum discontig_kernel_page_action action;
847 	union {
848 		struct page *__page;
849 		struct page **__page_arr;
850 	};
851 	unsigned long __nr_pages;
852 };
853 
854 struct discontig_kernel_page_ops {
855 	int (*init)(void *vm_private_data, void **private);
856 	int (*get)(struct discontig_kernel_page_state *state);
857 };
858 
859 /*
860  * Describes an action an mmap_prepare hook can instruct to be taken to complete
861  * the mapping of a VMA. Specified in vm_area_desc.
862  */
863 struct mmap_action {
864 	union {
865 		struct {
866 			unsigned long start;
867 			unsigned long start_pfn;
868 			unsigned long size;
869 			pgprot_t pgprot;
870 		} remap;
871 		struct {
872 			phys_addr_t start_phys_addr;
873 			unsigned long size;
874 		} simple_ioremap;
875 		struct {
876 			unsigned long start;
877 			struct page **pages;
878 			unsigned long nr_pages;
879 			pgoff_t pgoff;
880 		} map_kernel;
881 		struct {
882 			void *init_private;
883 			const struct discontig_kernel_page_ops *ops;
884 		} map_kernel_discontig;
885 	};
886 	enum mmap_action_type type;
887 
888 	/*
889 	 * If non-zero, replace errors that arise from mmap actions with this
890 	 * value instead. Only valid error codes may be specified.
891 	 */
892 	int error_override;
893 
894 	/*
895 	 * This should be set in rare instances where the operation required
896 	 * that the rmap should not be able to access the VMA until
897 	 * completely set up.
898 	 */
899 	bool hide_from_rmap_until_complete :1;
900 };
901 
902 /*
903  * Opaque type representing current VMA (vm_area_struct) flag state. Must be
904  * accessed via vma_flags_xxx() helper functions.
905  */
906 #define NUM_VMA_FLAG_BITS BITS_PER_LONG
907 typedef struct {
908 	DECLARE_BITMAP(__vma_flags, NUM_VMA_FLAG_BITS);
909 } vma_flags_t;
910 
911 #define EMPTY_VMA_FLAGS ((vma_flags_t){ })
912 
913 /* Are no flags set in the specified VMA flags? */
914 static __always_inline bool vma_flags_empty(const vma_flags_t *flags)
915 {
916 	const unsigned long *bitmap = flags->__vma_flags;
917 
918 	return bitmap_empty(bitmap, NUM_VMA_FLAG_BITS);
919 }
920 
921 /*
922  * Describes a VMA that is about to be mmap()'ed. Drivers may choose to
923  * manipulate mutable fields which will cause those fields to be updated in the
924  * resultant VMA.
925  *
926  * Helper functions are not required for manipulating any field.
927  */
928 struct vm_area_desc {
929 	/* Immutable state. */
930 	struct mm_struct *mm;
931 	struct file *file; /* May vary from vm_file in stacked callers. */
932 	unsigned long start;
933 	unsigned long end;
934 
935 	/* Mutable fields. Populated with initial state. */
936 	pgoff_t pgoff;
937 	struct file *vm_file;
938 	vma_flags_t vma_flags;
939 	pgprot_t page_prot;
940 
941 	/* Write-only fields. */
942 	const struct vm_operations_struct *vm_ops;
943 	void *private_data;
944 
945 	/* Take further action? */
946 	struct mmap_action action;
947 };
948 
949 /*
950  * This struct describes a virtual memory area. There is one of these
951  * per VM-area/task. A VM area is any part of the process virtual memory
952  * space that has a special rule for the page-fault handlers (ie a shared
953  * library, the executable area etc).
954  *
955  * Only explicitly marked struct members may be accessed by RCU readers before
956  * getting a stable reference.
957  *
958  * WARNING: when adding new members, please update vm_area_init_from() to copy
959  * them during vm_area_struct content duplication.
960  */
961 struct vm_area_struct {
962 	/* The first cache line has the info for VMA tree walking. */
963 
964 	union {
965 		struct {
966 			/* VMA covers [vm_start; vm_end) addresses within mm */
967 			unsigned long vm_start;
968 			unsigned long vm_end;
969 		};
970 		freeptr_t vm_freeptr; /* Pointer used by SLAB_TYPESAFE_BY_RCU */
971 	};
972 
973 	/*
974 	 * The address space we belong to.
975 	 * Unstable RCU readers are allowed to read this.
976 	 */
977 	struct mm_struct *vm_mm;
978 	pgprot_t vm_page_prot;          /* Access permissions of this VMA. */
979 
980 	/*
981 	 * Flags, see mm.h.
982 	 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
983 	 * Preferably, use vma_flags_xxx() functions.
984 	 */
985 	union {
986 		/* Temporary while VMA flags are being converted. */
987 		const vm_flags_t vm_flags;
988 		vma_flags_t flags;
989 	};
990 
991 	/*
992 	 * Can only be written (using WRITE_ONCE()) while holding both:
993 	 *  - mmap_lock (in write mode)
994 	 *  - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_FLAG is set
995 	 * The one exception is __mm_reset_vma_seqcounts(), which stores zero
996 	 * (an even value, which can never indicate a write lock) under the
997 	 * mmap write lock alone once the mm seqcount has wrapped, so a reader
998 	 * holding only vm_refcnt may observe the value change from odd to zero.
999 	 * Can be read reliably while holding one of:
1000 	 *  - mmap_lock (in read or write mode)
1001 	 *  - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_BIT is set
1002 	 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
1003 	 * while holding nothing (except RCU to keep the VMA struct allocated).
1004 	 *
1005 	 * Assigned the (odd) mm seqcount on write lock and not updated on
1006 	 * unlock, so a stale value would alias the mm seqcount again 2^31 write
1007 	 * lock/unlock cycles later. The reset described above prevents this.
1008 	 */
1009 	unsigned int vm_lock_seq;
1010 
1011 	/*
1012 	 * Low 32-bits of anonymous page offset.
1013 	 * See vma_start_anon_pgoff() comment for details.
1014 	 */
1015 	unsigned int __vm_anon_pgoff_lo;
1016 	/*
1017 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
1018 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
1019 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
1020 	 * or brk vma (with NULL file) can only be in an anon_vma list.
1021 	 */
1022 	struct list_head anon_vma_chain; /* Serialized by mmap_lock &
1023 					  * page_table_lock */
1024 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
1025 
1026 	/* Function pointers to deal with this struct. */
1027 	const struct vm_operations_struct *vm_ops;
1028 
1029 	/* Information about our backing store: */
1030 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
1031 					   units */
1032 	struct file * vm_file;		/* File we map to (can be NULL). */
1033 	void * vm_private_data;		/* was vm_pte (shared mem) */
1034 
1035 #ifdef CONFIG_SWAP
1036 	atomic_long_t swap_readahead_info;
1037 #endif
1038 #ifndef CONFIG_MMU
1039 	struct vm_region *vm_region;	/* NOMMU mapping region */
1040 #endif
1041 #ifdef CONFIG_NUMA
1042 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
1043 #endif
1044 #ifdef CONFIG_NUMA_BALANCING
1045 	struct vma_numab_state *numab_state;	/* NUMA Balancing state */
1046 #endif
1047 	/*
1048 	 * Used to keep track of firstly, whether the VMA is attached, secondly,
1049 	 * if attached, how many read locks are taken, and thirdly, if the
1050 	 * VM_REFCNT_EXCLUDE_READERS_FLAG is set, whether any read locks held
1051 	 * are currently in the process of being excluded.
1052 	 *
1053 	 * This value can be equal to:
1054 	 *
1055 	 * 0 - Detached. IMPORTANT: when the refcnt is zero, readers cannot
1056 	 * increment it.
1057 	 *
1058 	 * 1 - Attached and either unlocked or write-locked. Write locks are
1059 	 * identified via __is_vma_write_locked() which checks for equality of
1060 	 * vma->vm_lock_seq and mm->mm_lock_seq.
1061 	 *
1062 	 * >1, < VM_REFCNT_EXCLUDE_READERS_FLAG - Read-locked or (unlikely)
1063 	 * write-locked with other threads having temporarily incremented the
1064 	 * reference count prior to determining it is write-locked and
1065 	 * decrementing it again.
1066 	 *
1067 	 * VM_REFCNT_EXCLUDE_READERS_FLAG - Detached, pending
1068 	 * __vma_end_exclude_readers() completion which will decrement the
1069 	 * reference count to zero. IMPORTANT - at this stage no further readers
1070 	 * can increment the reference count. It can only be reduced.
1071 	 *
1072 	 * VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either write-locking
1073 	 * an attached VMA and has yet to invoke __vma_end_exclude_readers(),
1074 	 * OR a thread is detaching a VMA and is waiting on a single spurious
1075 	 * reader in order to decrement the reference count. IMPORTANT - as
1076 	 * above, no further readers can increment the reference count.
1077 	 *
1078 	 * > VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either
1079 	 * write-locking or detaching a VMA is waiting on readers to
1080 	 * exit. IMPORTANT - as above, no further readers can increment the
1081 	 * reference count.
1082 	 *
1083 	 * NOTE: Unstable RCU readers are allowed to read this.
1084 	 */
1085 	refcount_t vm_refcnt ____cacheline_aligned_in_smp;
1086 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1087 	struct lockdep_map vmlock_dep_map;
1088 #endif
1089 #ifdef CONFIG_64BIT
1090 	/*
1091 	 * High 32-bits of anonymous page offset.
1092 	 * See vma_start_anon_pgoff() comment for details.
1093 	 */
1094 	unsigned int __vm_anon_pgoff_hi;
1095 #endif
1096 	/*
1097 	 * For areas with an address space and backing store,
1098 	 * linkage into the address_space->i_mmap interval tree.
1099 	 *
1100 	 */
1101 	struct {
1102 		struct rb_node rb;
1103 		unsigned long rb_subtree_last;
1104 	} shared;
1105 #ifdef CONFIG_ANON_VMA_NAME
1106 	/*
1107 	 * For private and shared anonymous mappings, a pointer to a null
1108 	 * terminated string containing the name given to the vma, or NULL if
1109 	 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
1110 	 */
1111 	struct anon_vma_name *anon_name;
1112 #endif
1113 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
1114 #ifdef __HAVE_PFNMAP_TRACKING
1115 	struct pfnmap_track_ctx *pfnmap_track_ctx;
1116 #endif
1117 } __randomize_layout;
1118 
1119 /* Clears all bits in the VMA flags bitmap, non-atomically. */
1120 static __always_inline void vma_flags_clear_all(vma_flags_t *flags)
1121 {
1122 	bitmap_zero(flags->__vma_flags, NUM_VMA_FLAG_BITS);
1123 }
1124 
1125 /*
1126  * Helper function which converts a vma_flags_t value to a legacy vm_flags_t
1127  * value. This is only valid if the input flags value can be expressed in a
1128  * system word.
1129  *
1130  * Will be removed once the conversion to VMA flags is complete.
1131  */
1132 static __always_inline vm_flags_t vma_flags_to_legacy(vma_flags_t flags)
1133 {
1134 	return (vm_flags_t)flags.__vma_flags[0];
1135 }
1136 
1137 /*
1138  * Copy value to the first system word of VMA flags, non-atomically.
1139  *
1140  * IMPORTANT: This does not overwrite bytes past the first system word. The
1141  * caller must account for this.
1142  */
1143 static __always_inline void vma_flags_overwrite_word(vma_flags_t *flags,
1144 		unsigned long value)
1145 {
1146 	unsigned long *bitmap = flags->__vma_flags;
1147 
1148 	bitmap[0] = value;
1149 }
1150 
1151 /*
1152  * Helper function which converts a legacy vm_flags_t value to a vma_flags_t
1153  * value.
1154  *
1155  * Will be removed once the conversion to VMA flags is complete.
1156  */
1157 static __always_inline vma_flags_t legacy_to_vma_flags(vm_flags_t flags)
1158 {
1159 	vma_flags_t ret = EMPTY_VMA_FLAGS;
1160 
1161 	vma_flags_overwrite_word(&ret, flags);
1162 	return ret;
1163 }
1164 
1165 /*
1166  * Copy value to the first system word of VMA flags ONCE, non-atomically.
1167  *
1168  * IMPORTANT: This does not overwrite bytes past the first system word. The
1169  * caller must account for this.
1170  */
1171 static __always_inline void vma_flags_overwrite_word_once(vma_flags_t *flags,
1172 		unsigned long value)
1173 {
1174 	unsigned long *bitmap = flags->__vma_flags;
1175 
1176 	WRITE_ONCE(*bitmap, value);
1177 }
1178 
1179 /* Update the first system word of VMA flags setting bits, non-atomically. */
1180 static __always_inline void vma_flags_set_word(vma_flags_t *flags,
1181 		unsigned long value)
1182 {
1183 	unsigned long *bitmap = flags->__vma_flags;
1184 
1185 	*bitmap |= value;
1186 }
1187 
1188 /* Update the first system word of VMA flags clearing bits, non-atomically. */
1189 static __always_inline void vma_flags_clear_word(vma_flags_t *flags,
1190 		unsigned long value)
1191 {
1192 	unsigned long *bitmap = flags->__vma_flags;
1193 
1194 	*bitmap &= ~value;
1195 }
1196 
1197 #ifdef CONFIG_NUMA
1198 #define vma_policy(vma) ((vma)->vm_policy)
1199 #else
1200 #define vma_policy(vma) NULL
1201 #endif
1202 
1203 /*
1204  * Opaque type representing current mm_struct flag state. Must be accessed via
1205  * mm_flags_xxx() helper functions.
1206  */
1207 #define NUM_MM_FLAG_BITS (64)
1208 typedef struct {
1209 	DECLARE_BITMAP(__mm_flags, NUM_MM_FLAG_BITS);
1210 } __private mm_flags_t;
1211 
1212 struct kioctx_table;
1213 struct iommu_mm_data;
1214 struct mm_struct {
1215 	struct {
1216 		/*
1217 		 * Fields which are often written to are placed in a separate
1218 		 * cache line.
1219 		 */
1220 		struct {
1221 			/**
1222 			 * @mm_count: The number of references to &struct
1223 			 * mm_struct (@mm_users count as 1).
1224 			 *
1225 			 * Use mmgrab()/mmdrop() to modify. When this drops to
1226 			 * 0, the &struct mm_struct is freed.
1227 			 */
1228 			atomic_t mm_count;
1229 		} ____cacheline_aligned_in_smp;
1230 
1231 		struct maple_tree mm_mt;
1232 
1233 		unsigned long mmap_base;	/* base of mmap area */
1234 		unsigned long mmap_legacy_base;	/* base of mmap area in bottom-up allocations */
1235 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
1236 		/* Base addresses for compatible mmap() */
1237 		unsigned long mmap_compat_base;
1238 		unsigned long mmap_compat_legacy_base;
1239 #endif
1240 		unsigned long task_size;	/* size of task vm space */
1241 		pgd_t * pgd;
1242 
1243 #ifdef CONFIG_MEMBARRIER
1244 		/**
1245 		 * @membarrier_state: Flags controlling membarrier behavior.
1246 		 *
1247 		 * This field is close to @pgd to hopefully fit in the same
1248 		 * cache-line, which needs to be touched by switch_mm().
1249 		 */
1250 		atomic_t membarrier_state;
1251 #endif
1252 
1253 		/**
1254 		 * @mm_users: The number of users including userspace.
1255 		 *
1256 		 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
1257 		 * drops to 0 (i.e. when the task exits and there are no other
1258 		 * temporary reference holders), we also release a reference on
1259 		 * @mm_count (which may then free the &struct mm_struct if
1260 		 * @mm_count also drops to 0).
1261 		 */
1262 		atomic_t mm_users;
1263 
1264 		/* MM CID related storage */
1265 		struct mm_mm_cid mm_cid;
1266 
1267 		/* sched_cache related statistics */
1268 		struct sched_cache_group *sched_cache_grp;
1269 #ifdef CONFIG_MMU
1270 		atomic_long_t pgtables_bytes;	/* size of all page tables */
1271 #endif
1272 		int map_count;			/* number of VMAs */
1273 
1274 		spinlock_t page_table_lock; /* Protects page tables and some
1275 					     * counters
1276 					     */
1277 		/*
1278 		 * Typically the current mmap_lock's offset is 56 bytes from
1279 		 * the last cacheline boundary, which is very optimal, as
1280 		 * its two hot fields 'count' and 'owner' sit in 2 different
1281 		 * cachelines, and when mmap_lock is highly contended, both
1282 		 * of the 2 fields will be accessed frequently, current layout
1283 		 * will help to reduce cache bouncing.
1284 		 *
1285 		 * So please be careful with adding new fields before
1286 		 * mmap_lock, which can easily push the 2 fields into one
1287 		 * cacheline.
1288 		 */
1289 		struct rw_semaphore mmap_lock;
1290 
1291 		struct list_head mmlist; /* List of maybe swapped mm's.	These
1292 					  * are globally strung together off
1293 					  * init_mm.mmlist, and are protected
1294 					  * by mmlist_lock
1295 					  */
1296 		struct rcuwait vma_writer_wait;
1297 		/*
1298 		 * This field has lock-like semantics, meaning it is sometimes
1299 		 * accessed with ACQUIRE/RELEASE semantics.
1300 		 * Roughly speaking, incrementing the sequence number is
1301 		 * equivalent to releasing locks on VMAs; reading the sequence
1302 		 * number can be part of taking a read lock on a VMA.
1303 		 * Incremented every time mmap_lock is write-locked/unlocked.
1304 		 * Initialized to 0, therefore odd values indicate mmap_lock
1305 		 * is write-locked and even values that it's released.
1306 		 *
1307 		 * Can be modified under write mmap_lock using RELEASE
1308 		 * semantics.
1309 		 * Can be read with no other protection when holding write
1310 		 * mmap_lock.
1311 		 * Can be read with ACQUIRE semantics if not holding write
1312 		 * mmap_lock.
1313 		 */
1314 		seqcount_t mm_lock_seq;
1315 
1316 		struct futex_mm_data	futex;
1317 
1318 		unsigned long hiwater_rss; /* High-watermark of RSS usage */
1319 		unsigned long hiwater_vm;  /* High-water virtual memory usage */
1320 
1321 		unsigned long total_vm;	   /* Total pages mapped */
1322 		unsigned long locked_vm;   /* Pages that have PG_mlocked set */
1323 		atomic64_t    pinned_vm;   /* Refcount permanently increased */
1324 		unsigned long data_vm;	   /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
1325 		unsigned long exec_vm;	   /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
1326 		unsigned long stack_vm;	   /* VM_STACK */
1327 		union {
1328 			/* Temporary while VMA flags are being converted. */
1329 			vm_flags_t def_flags;
1330 			vma_flags_t def_vma_flags;
1331 		};
1332 
1333 		/**
1334 		 * @write_protect_seq: Locked when any thread is write
1335 		 * protecting pages mapped by this mm to enforce a later COW,
1336 		 * for instance during page table copying for fork().
1337 		 */
1338 		seqcount_t write_protect_seq;
1339 
1340 		spinlock_t arg_lock; /* protect the below fields */
1341 
1342 		unsigned long start_code, end_code, start_data, end_data;
1343 		unsigned long start_brk, brk, start_stack;
1344 		unsigned long arg_start, arg_end, env_start, env_end;
1345 
1346 		unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
1347 
1348 #ifdef CONFIG_ARCH_HAS_ELF_CORE_EFLAGS
1349 		/* the ABI-related flags from the ELF header. Used for core dump */
1350 		unsigned long saved_e_flags;
1351 #endif
1352 
1353 		struct percpu_counter rss_stat[NR_MM_COUNTERS];
1354 
1355 		struct linux_binfmt *binfmt;
1356 
1357 		/* Architecture-specific MM context */
1358 		mm_context_t context;
1359 
1360 		mm_flags_t flags; /* Must use mm_flags_* hlpers to access */
1361 
1362 #ifdef CONFIG_AIO
1363 		spinlock_t			ioctx_lock;
1364 		struct kioctx_table __rcu	*ioctx_table;
1365 #endif
1366 #ifdef CONFIG_MEMCG
1367 		/*
1368 		 * "owner" points to a task that is regarded as the canonical
1369 		 * user/owner of this mm. All of the following must be true in
1370 		 * order for it to be changed:
1371 		 *
1372 		 * current == mm->owner
1373 		 * current->mm != mm
1374 		 * new_owner->mm == mm
1375 		 * new_owner->alloc_lock is held
1376 		 */
1377 		struct task_struct __rcu *owner;
1378 #endif
1379 
1380 		/* store ref to file /proc/<pid>/exe symlink points to */
1381 		struct file __rcu *exe_file;
1382 #ifdef CONFIG_MMU_NOTIFIER
1383 		struct mmu_notifier_subscriptions *notifier_subscriptions;
1384 #endif
1385 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
1386 		pgtable_t pmd_huge_pte; /* protected by page_table_lock */
1387 #endif
1388 #ifdef CONFIG_NUMA_BALANCING
1389 		/*
1390 		 * numa_next_scan is the next time that PTEs will be remapped
1391 		 * PROT_NONE to trigger NUMA hinting faults; such faults gather
1392 		 * statistics and migrate pages to new nodes if necessary.
1393 		 */
1394 		unsigned long numa_next_scan;
1395 
1396 		/* Restart point for scanning and remapping PTEs. */
1397 		unsigned long numa_scan_offset;
1398 
1399 		/* numa_scan_seq prevents two threads remapping PTEs. */
1400 		int numa_scan_seq;
1401 #endif
1402 		/*
1403 		 * An operation with batched TLB flushing is going on. Anything
1404 		 * that can move process memory needs to flush the TLB when
1405 		 * moving a PROT_NONE mapped page.
1406 		 */
1407 		atomic_t tlb_flush_pending;
1408 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
1409 		/* See flush_tlb_batched_pending() */
1410 		atomic_t tlb_flush_batched;
1411 #endif
1412 		struct uprobes_state uprobes_state;
1413 #ifdef CONFIG_PREEMPT_RT
1414 		struct rcu_head delayed_drop;
1415 #endif
1416 #ifdef CONFIG_HUGETLB_PAGE
1417 		atomic_long_t hugetlb_usage;
1418 #endif
1419 		struct work_struct async_put_work;
1420 
1421 #ifdef CONFIG_IOMMU_MM_DATA
1422 		struct iommu_mm_data *iommu_mm;
1423 #endif
1424 #ifdef CONFIG_KSM
1425 		/*
1426 		 * Represent how many pages of this process are involved in KSM
1427 		 * merging (not including ksm_zero_pages).
1428 		 */
1429 		unsigned long ksm_merging_pages;
1430 		/*
1431 		 * Represent how many pages are checked for ksm merging
1432 		 * including merged and not merged.
1433 		 */
1434 		unsigned long ksm_rmap_items;
1435 		/*
1436 		 * Represent how many empty pages are merged with kernel zero
1437 		 * pages when enabling KSM use_zero_pages.
1438 		 */
1439 		atomic_long_t ksm_zero_pages;
1440 #endif /* CONFIG_KSM */
1441 #ifdef CONFIG_LRU_GEN_WALKS_MMU
1442 		struct {
1443 			/* this mm_struct is on lru_gen_mm_list */
1444 			struct list_head list;
1445 			/*
1446 			 * Set when switching to this mm_struct, as a hint of
1447 			 * whether it has been used since the last time per-node
1448 			 * page table walkers cleared the corresponding bits.
1449 			 */
1450 			unsigned long bitmap;
1451 #ifdef CONFIG_MEMCG
1452 			/* points to the memcg of "owner" above */
1453 			struct mem_cgroup *memcg;
1454 #endif
1455 		} lru_gen;
1456 #endif /* CONFIG_LRU_GEN_WALKS_MMU */
1457 #ifdef CONFIG_MM_ID
1458 		mm_id_t mm_id;
1459 #endif /* CONFIG_MM_ID */
1460 	} __randomize_layout;
1461 
1462 	/*
1463 	 * The mm_cpumask needs to be at the end of mm_struct, because it
1464 	 * is dynamically sized based on nr_cpu_ids.
1465 	 */
1466 	char flexible_array[] __aligned(__alignof__(unsigned long));
1467 };
1468 
1469 /* Copy value to the first system word of mm flags, non-atomically. */
1470 static inline void __mm_flags_overwrite_word(struct mm_struct *mm, unsigned long value)
1471 {
1472 	*ACCESS_PRIVATE(&mm->flags, __mm_flags) = value;
1473 }
1474 
1475 /* Obtain a read-only view of the mm flags bitmap. */
1476 static inline const unsigned long *__mm_flags_get_bitmap(const struct mm_struct *mm)
1477 {
1478 	return (const unsigned long *)ACCESS_PRIVATE(&mm->flags, __mm_flags);
1479 }
1480 
1481 /* Read the first system word of mm flags, non-atomically. */
1482 static inline unsigned long __mm_flags_get_word(const struct mm_struct *mm)
1483 {
1484 	return *__mm_flags_get_bitmap(mm);
1485 }
1486 
1487 /*
1488  * Update the first system word of mm flags ONLY, applying the specified mask to
1489  * it, then setting all flags specified by bits.
1490  */
1491 static inline void __mm_flags_set_mask_bits_word(struct mm_struct *mm,
1492 		unsigned long mask, unsigned long bits)
1493 {
1494 	unsigned long *bitmap = ACCESS_PRIVATE(&mm->flags, __mm_flags);
1495 
1496 	set_mask_bits(bitmap, mask, bits);
1497 }
1498 
1499 #define MM_MT_FLAGS	(MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
1500 			 MT_FLAGS_USE_RCU)
1501 extern struct mm_struct init_mm;
1502 
1503 #define MM_STRUCT_FLEXIBLE_ARRAY_INIT				\
1504 {								\
1505 	[0 ... sizeof(cpumask_t) + MM_CID_STATIC_SIZE - 1] = 0	\
1506 }
1507 
1508 /* Pointer magic because the dynamic array size confuses some compilers. */
1509 static inline void mm_init_cpumask(struct mm_struct *mm)
1510 {
1511 	unsigned long cpu_bitmap = (unsigned long)mm;
1512 
1513 	cpu_bitmap += offsetof(struct mm_struct, flexible_array);
1514 	cpumask_clear((struct cpumask *)cpu_bitmap);
1515 }
1516 
1517 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
1518 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
1519 {
1520 	return (struct cpumask *)&mm->flexible_array;
1521 }
1522 
1523 #ifdef CONFIG_LRU_GEN
1524 
1525 struct lru_gen_mm_list {
1526 	/* mm_struct list for page table walkers */
1527 	struct list_head fifo;
1528 	/* protects the list above */
1529 	spinlock_t lock;
1530 };
1531 
1532 #endif /* CONFIG_LRU_GEN */
1533 
1534 #ifdef CONFIG_LRU_GEN_WALKS_MMU
1535 
1536 void lru_gen_add_mm(struct mm_struct *mm);
1537 void lru_gen_del_mm(struct mm_struct *mm);
1538 void lru_gen_migrate_mm(struct mm_struct *mm);
1539 
1540 static inline void lru_gen_init_mm(struct mm_struct *mm)
1541 {
1542 	INIT_LIST_HEAD(&mm->lru_gen.list);
1543 	mm->lru_gen.bitmap = 0;
1544 #ifdef CONFIG_MEMCG
1545 	mm->lru_gen.memcg = NULL;
1546 #endif
1547 }
1548 
1549 static inline void lru_gen_use_mm(struct mm_struct *mm)
1550 {
1551 	/*
1552 	 * When the bitmap is set, page reclaim knows this mm_struct has been
1553 	 * used since the last time it cleared the bitmap. So it might be worth
1554 	 * walking the page tables of this mm_struct to clear the accessed bit.
1555 	 */
1556 	WRITE_ONCE(mm->lru_gen.bitmap, -1);
1557 }
1558 
1559 #else /* !CONFIG_LRU_GEN_WALKS_MMU */
1560 
1561 static inline void lru_gen_add_mm(struct mm_struct *mm)
1562 {
1563 }
1564 
1565 static inline void lru_gen_del_mm(struct mm_struct *mm)
1566 {
1567 }
1568 
1569 static inline void lru_gen_migrate_mm(struct mm_struct *mm)
1570 {
1571 }
1572 
1573 static inline void lru_gen_init_mm(struct mm_struct *mm)
1574 {
1575 }
1576 
1577 static inline void lru_gen_use_mm(struct mm_struct *mm)
1578 {
1579 }
1580 
1581 #endif /* CONFIG_LRU_GEN_WALKS_MMU */
1582 
1583 struct vma_iterator {
1584 	struct ma_state mas;
1585 };
1586 
1587 #define VMA_ITERATOR(name, __mm, __addr)				\
1588 	struct vma_iterator name = {					\
1589 		.mas = {						\
1590 			.tree = &(__mm)->mm_mt,				\
1591 			.index = __addr,				\
1592 			.node = NULL,					\
1593 			.status = ma_start,				\
1594 		},							\
1595 	}
1596 
1597 static inline void vma_iter_init(struct vma_iterator *vmi,
1598 		struct mm_struct *mm, unsigned long addr)
1599 {
1600 	mas_init(&vmi->mas, &mm->mm_mt, addr);
1601 }
1602 
1603 #ifdef CONFIG_SCHED_MM_CID
1604 /*
1605  * mm_cpus_allowed: Union of all mm's threads allowed CPUs.
1606  */
1607 static inline cpumask_t *mm_cpus_allowed(struct mm_struct *mm)
1608 {
1609 	unsigned long bitmap = (unsigned long)mm;
1610 
1611 	bitmap += offsetof(struct mm_struct, flexible_array);
1612 	/* Skip cpu_bitmap */
1613 	bitmap += cpumask_size();
1614 	return (struct cpumask *)bitmap;
1615 }
1616 
1617 /* Accessor for struct mm_struct's cidmask. */
1618 static inline unsigned long *mm_cidmask(struct mm_struct *mm)
1619 {
1620 	unsigned long cid_bitmap = (unsigned long)mm_cpus_allowed(mm);
1621 
1622 	/* Skip mm_cpus_allowed */
1623 	cid_bitmap += cpumask_size();
1624 	return (unsigned long *)cid_bitmap;
1625 }
1626 
1627 void mm_init_cid(struct mm_struct *mm, struct task_struct *p);
1628 
1629 static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p)
1630 {
1631 	mm->mm_cid.pcpu = alloc_percpu_noprof(struct mm_cid_pcpu);
1632 	if (!mm->mm_cid.pcpu)
1633 		return -ENOMEM;
1634 	mm_init_cid(mm, p);
1635 	return 0;
1636 }
1637 # define mm_alloc_cid(...)	alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__))
1638 
1639 static inline void mm_destroy_cid(struct mm_struct *mm)
1640 {
1641 	free_percpu(mm->mm_cid.pcpu);
1642 	mm->mm_cid.pcpu = NULL;
1643 }
1644 
1645 static inline unsigned int mm_cid_size(void)
1646 {
1647 	/* mm_cpus_allowed(), mm_cidmask(). */
1648 	return cpumask_size() + bitmap_size(num_possible_cpus());
1649 }
1650 
1651 /* Use 2 * NR_CPUS as worse case for static allocation. */
1652 # define MM_CID_STATIC_SIZE	(2 * sizeof(cpumask_t))
1653 #else /* CONFIG_SCHED_MM_CID */
1654 static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { }
1655 static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; }
1656 static inline void mm_destroy_cid(struct mm_struct *mm) { }
1657 static inline unsigned int mm_cid_size(void)
1658 {
1659 	return 0;
1660 }
1661 # define MM_CID_STATIC_SIZE	0
1662 #endif /* CONFIG_SCHED_MM_CID */
1663 
1664 #ifdef CONFIG_SCHED_CACHE
1665 int mm_init_sched(struct mm_struct *mm,
1666 		  struct sched_cache_time __percpu *pcpu_sched);
1667 void mm_destroy_sched(struct mm_struct *mm);
1668 
1669 static inline int mm_alloc_sched_noprof(struct mm_struct *mm)
1670 {
1671 	struct sched_cache_time __percpu *pcpu_sched =
1672 		alloc_percpu_noprof(struct sched_cache_time);
1673 
1674 	if (!pcpu_sched)
1675 		return -ENOMEM;
1676 
1677 	return mm_init_sched(mm, pcpu_sched);
1678 }
1679 
1680 #define mm_alloc_sched(...)	alloc_hooks(mm_alloc_sched_noprof(__VA_ARGS__))
1681 
1682 #else /* !CONFIG_SCHED_CACHE */
1683 
1684 static inline int mm_alloc_sched(struct mm_struct *mm) { return 0; }
1685 static inline void mm_destroy_sched(struct mm_struct *mm) { }
1686 
1687 #endif /* CONFIG_SCHED_CACHE */
1688 
1689 struct mmu_gather;
1690 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
1691 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
1692 void tlb_gather_mmu_vma(struct mmu_gather *tlb, struct vm_area_struct *vma);
1693 extern void tlb_finish_mmu(struct mmu_gather *tlb);
1694 
1695 struct vm_fault;
1696 
1697 /**
1698  * typedef vm_fault_t - Return type for page fault handlers.
1699  *
1700  * Page fault handlers return a bitmask of %VM_FAULT values.
1701  */
1702 typedef __bitwise unsigned int vm_fault_t;
1703 
1704 /**
1705  * enum vm_fault_reason - Page fault handlers return a bitmask of
1706  * these values to tell the core VM what happened when handling the
1707  * fault. Used to decide whether a process gets delivered SIGBUS or
1708  * just gets major/minor fault counters bumped up.
1709  *
1710  * @VM_FAULT_OOM:		Out Of Memory
1711  * @VM_FAULT_SIGBUS:		Bad access
1712  * @VM_FAULT_MAJOR:		Page read from storage
1713  * @VM_FAULT_HWPOISON:		Hit poisoned small page
1714  * @VM_FAULT_HWPOISON_LARGE:	Hit poisoned large page. Index encoded
1715  *				in upper bits
1716  * @VM_FAULT_SIGSEGV:		segmentation fault
1717  * @VM_FAULT_NOPAGE:		->fault installed the pte, not return page
1718  * @VM_FAULT_LOCKED:		->fault locked the returned page
1719  * @VM_FAULT_RETRY:		->fault blocked, must retry
1720  * @VM_FAULT_FALLBACK:		huge page fault failed, fall back to small
1721  * @VM_FAULT_DONE_COW:		->fault has fully handled COW
1722  * @VM_FAULT_NEEDDSYNC:		->fault did not modify page tables and needs
1723  *				fsync() to complete (for synchronous page faults
1724  *				in DAX)
1725  * @VM_FAULT_COMPLETED:		->fault completed, meanwhile mmap lock released
1726  * @VM_FAULT_HINDEX_MASK:	mask HINDEX value
1727  *
1728  */
1729 enum vm_fault_reason {
1730 	VM_FAULT_OOM            = (__force vm_fault_t)0x000001,
1731 	VM_FAULT_SIGBUS         = (__force vm_fault_t)0x000002,
1732 	VM_FAULT_MAJOR          = (__force vm_fault_t)0x000004,
1733 	VM_FAULT_HWPOISON       = (__force vm_fault_t)0x000010,
1734 	VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
1735 	VM_FAULT_SIGSEGV        = (__force vm_fault_t)0x000040,
1736 	VM_FAULT_NOPAGE         = (__force vm_fault_t)0x000100,
1737 	VM_FAULT_LOCKED         = (__force vm_fault_t)0x000200,
1738 	VM_FAULT_RETRY          = (__force vm_fault_t)0x000400,
1739 	VM_FAULT_FALLBACK       = (__force vm_fault_t)0x000800,
1740 	VM_FAULT_DONE_COW       = (__force vm_fault_t)0x001000,
1741 	VM_FAULT_NEEDDSYNC      = (__force vm_fault_t)0x002000,
1742 	VM_FAULT_COMPLETED      = (__force vm_fault_t)0x004000,
1743 	VM_FAULT_HINDEX_MASK    = (__force vm_fault_t)0x0f0000,
1744 };
1745 
1746 /* Encode hstate index for a hwpoisoned large page */
1747 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
1748 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
1749 
1750 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS |	\
1751 			VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON |	\
1752 			VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
1753 
1754 #define VM_FAULT_RESULT_TRACE						\
1755 	{ (__force u32)VM_FAULT_OOM,                 "OOM" },		\
1756 	{ (__force u32)VM_FAULT_SIGBUS,              "SIGBUS" },	\
1757 	{ (__force u32)VM_FAULT_MAJOR,               "MAJOR" },		\
1758 	{ (__force u32)VM_FAULT_HWPOISON,            "HWPOISON" },	\
1759 	{ (__force u32)VM_FAULT_HWPOISON_LARGE,      "HWPOISON_LARGE" }, \
1760 	{ (__force u32)VM_FAULT_SIGSEGV,             "SIGSEGV" },	\
1761 	{ (__force u32)VM_FAULT_NOPAGE,              "NOPAGE" },	\
1762 	{ (__force u32)VM_FAULT_LOCKED,              "LOCKED" },	\
1763 	{ (__force u32)VM_FAULT_RETRY,               "RETRY" },		\
1764 	{ (__force u32)VM_FAULT_FALLBACK,            "FALLBACK" },	\
1765 	{ (__force u32)VM_FAULT_DONE_COW,            "DONE_COW" },	\
1766 	{ (__force u32)VM_FAULT_NEEDDSYNC,           "NEEDDSYNC" },	\
1767 	{ (__force u32)VM_FAULT_COMPLETED,           "COMPLETED" }
1768 
1769 struct vm_special_mapping {
1770 	const char *name;	/* The name, e.g. "[vdso]". */
1771 
1772 	/*
1773 	 * If .fault is not provided, this points to a
1774 	 * NULL-terminated array of pages that back the special mapping.
1775 	 *
1776 	 * This must not be NULL unless .fault is provided.
1777 	 */
1778 	struct page **pages;
1779 
1780 	/*
1781 	 * If non-NULL, then this is called to resolve page faults
1782 	 * on the special mapping.  If used, .pages is not checked.
1783 	 */
1784 	vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1785 				struct vm_area_struct *vma,
1786 				struct vm_fault *vmf);
1787 
1788 	int (*mremap)(const struct vm_special_mapping *sm,
1789 		     struct vm_area_struct *new_vma);
1790 
1791 	void (*close)(const struct vm_special_mapping *sm,
1792 		      struct vm_area_struct *vma);
1793 };
1794 
1795 enum tlb_flush_reason {
1796 	TLB_FLUSH_ON_TASK_SWITCH,
1797 	TLB_REMOTE_SHOOTDOWN,
1798 	TLB_LOCAL_SHOOTDOWN,
1799 	TLB_LOCAL_MM_SHOOTDOWN,
1800 	TLB_REMOTE_SEND_IPI,
1801 	TLB_REMOTE_WRONG_CPU,
1802 };
1803 
1804 /**
1805  * enum fault_flag - Fault flag definitions.
1806  * @FAULT_FLAG_WRITE: Fault was a write fault.
1807  * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
1808  * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
1809  * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
1810  * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
1811  * @FAULT_FLAG_TRIED: The fault has been tried once.
1812  * @FAULT_FLAG_USER: The fault originated in userspace.
1813  * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
1814  * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
1815  * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
1816  * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
1817  *                      COW mapping, making sure that an exclusive anon page is
1818  *                      mapped after the fault.
1819  * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
1820  *                        We should only access orig_pte if this flag set.
1821  * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock.
1822  *
1823  * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
1824  * whether we would allow page faults to retry by specifying these two
1825  * fault flags correctly.  Currently there can be three legal combinations:
1826  *
1827  * (a) ALLOW_RETRY and !TRIED:  this means the page fault allows retry, and
1828  *                              this is the first try
1829  *
1830  * (b) ALLOW_RETRY and TRIED:   this means the page fault allows retry, and
1831  *                              we've already tried at least once
1832  *
1833  * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
1834  *
1835  * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
1836  * be used.  Note that page faults can be allowed to retry for multiple times,
1837  * in which case we'll have an initial fault with flags (a) then later on
1838  * continuous faults with flags (b).  We should always try to detect pending
1839  * signals before a retry to make sure the continuous page faults can still be
1840  * interrupted if necessary.
1841  *
1842  * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
1843  * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
1844  * applied to mappings that are not COW mappings.
1845  */
1846 enum fault_flag {
1847 	FAULT_FLAG_WRITE =		1 << 0,
1848 	FAULT_FLAG_MKWRITE =		1 << 1,
1849 	FAULT_FLAG_ALLOW_RETRY =	1 << 2,
1850 	FAULT_FLAG_RETRY_NOWAIT = 	1 << 3,
1851 	FAULT_FLAG_KILLABLE =		1 << 4,
1852 	FAULT_FLAG_TRIED = 		1 << 5,
1853 	FAULT_FLAG_USER =		1 << 6,
1854 	FAULT_FLAG_REMOTE =		1 << 7,
1855 	FAULT_FLAG_INSTRUCTION =	1 << 8,
1856 	FAULT_FLAG_INTERRUPTIBLE =	1 << 9,
1857 	FAULT_FLAG_UNSHARE =		1 << 10,
1858 	FAULT_FLAG_ORIG_PTE_VALID =	1 << 11,
1859 	FAULT_FLAG_VMA_LOCK =		1 << 12,
1860 };
1861 
1862 typedef unsigned int __bitwise zap_flags_t;
1863 
1864 /* Flags for clear_young_dirty_ptes(). */
1865 typedef int __bitwise cydp_t;
1866 
1867 /* Clear the access bit */
1868 #define CYDP_CLEAR_YOUNG		((__force cydp_t)BIT(0))
1869 
1870 /* Clear the dirty bit */
1871 #define CYDP_CLEAR_DIRTY		((__force cydp_t)BIT(1))
1872 
1873 /*
1874  * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
1875  * other. Here is what they mean, and how to use them:
1876  *
1877  *
1878  * FIXME: For pages which are part of a filesystem, mappings are subject to the
1879  * lifetime enforced by the filesystem and we need guarantees that longterm
1880  * users like RDMA and V4L2 only establish mappings which coordinate usage with
1881  * the filesystem.  Ideas for this coordination include revoking the longterm
1882  * pin, delaying writeback, bounce buffer page writeback, etc.  As FS DAX was
1883  * added after the problem with filesystems was found FS DAX VMAs are
1884  * specifically failed.  Filesystem pages are still subject to bugs and use of
1885  * FOLL_LONGTERM should be avoided on those pages.
1886  *
1887  * In the CMA case: long term pins in a CMA region would unnecessarily fragment
1888  * that region.  And so, CMA attempts to migrate the page before pinning, when
1889  * FOLL_LONGTERM is specified.
1890  *
1891  * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
1892  * but an additional pin counting system) will be invoked. This is intended for
1893  * anything that gets a page reference and then touches page data (for example,
1894  * Direct IO). This lets the filesystem know that some non-file-system entity is
1895  * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
1896  * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
1897  * a call to unpin_user_page().
1898  *
1899  * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
1900  * and separate refcounting mechanisms, however, and that means that each has
1901  * its own acquire and release mechanisms:
1902  *
1903  *     FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
1904  *
1905  *     FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
1906  *
1907  * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
1908  * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
1909  * calls applied to them, and that's perfectly OK. This is a constraint on the
1910  * callers, not on the pages.)
1911  *
1912  * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
1913  * directly by the caller. That's in order to help avoid mismatches when
1914  * releasing pages: get_user_pages*() pages must be released via put_page(),
1915  * while pin_user_pages*() pages must be released via unpin_user_page().
1916  *
1917  * Please see Documentation/core-api/pin_user_pages.rst for more information.
1918  */
1919 
1920 enum {
1921 	/* check pte is writable */
1922 	FOLL_WRITE = 1 << 0,
1923 	/* do get_page on page */
1924 	FOLL_GET = 1 << 1,
1925 	/* give error on hole if it would be zero */
1926 	FOLL_DUMP = 1 << 2,
1927 	/* get_user_pages read/write w/o permission */
1928 	FOLL_FORCE = 1 << 3,
1929 	/*
1930 	 * if a disk transfer is needed, start the IO and return without waiting
1931 	 * upon it
1932 	 */
1933 	FOLL_NOWAIT = 1 << 4,
1934 	/* do not fault in pages */
1935 	FOLL_NOFAULT = 1 << 5,
1936 	/* check page is hwpoisoned */
1937 	FOLL_HWPOISON = 1 << 6,
1938 	/* don't do file mappings */
1939 	FOLL_ANON = 1 << 7,
1940 	/*
1941 	 * FOLL_LONGTERM indicates that the page will be held for an indefinite
1942 	 * time period _often_ under userspace control.  This is in contrast to
1943 	 * iov_iter_get_pages(), whose usages are transient.
1944 	 */
1945 	FOLL_LONGTERM = 1 << 8,
1946 	/* split huge pmd before returning */
1947 	FOLL_SPLIT_PMD = 1 << 9,
1948 	/* allow returning PCI P2PDMA pages */
1949 	FOLL_PCI_P2PDMA = 1 << 10,
1950 	/* allow interrupts from generic signals */
1951 	FOLL_INTERRUPTIBLE = 1 << 11,
1952 	/*
1953 	 * Always honor (trigger) NUMA hinting faults.
1954 	 *
1955 	 * FOLL_WRITE implicitly honors NUMA hinting faults because a
1956 	 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE
1957 	 * apply). get_user_pages_fast_only() always implicitly honors NUMA
1958 	 * hinting faults.
1959 	 */
1960 	FOLL_HONOR_NUMA_FAULT = 1 << 12,
1961 
1962 	/* See also internal only FOLL flags in mm/internal.h */
1963 };
1964 
1965 /* mm flags */
1966 
1967 /*
1968  * Bits 0 and 1 were dumpability; that moved to task->exec_state.  Reserve
1969  * the bits so MMF_DUMP_FILTER_* positions stay stable for the
1970  * /proc/<pid>/coredump_filter ABI.
1971  */
1972 #define MMF_DUMPABLE_BITS 2
1973 /* coredump filter bits */
1974 #define MMF_DUMP_ANON_PRIVATE	2
1975 #define MMF_DUMP_ANON_SHARED	3
1976 #define MMF_DUMP_MAPPED_PRIVATE	4
1977 #define MMF_DUMP_MAPPED_SHARED	5
1978 #define MMF_DUMP_ELF_HEADERS	6
1979 #define MMF_DUMP_HUGETLB_PRIVATE 7
1980 #define MMF_DUMP_HUGETLB_SHARED  8
1981 #define MMF_DUMP_DAX_PRIVATE	9
1982 #define MMF_DUMP_DAX_SHARED	10
1983 
1984 #define MMF_DUMP_FILTER_SHIFT	MMF_DUMPABLE_BITS
1985 #define MMF_DUMP_FILTER_BITS	9
1986 #define MMF_DUMP_FILTER_MASK \
1987 	((BIT(MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
1988 #define MMF_DUMP_FILTER_DEFAULT \
1989 	(BIT(MMF_DUMP_ANON_PRIVATE) | BIT(MMF_DUMP_ANON_SHARED) | \
1990 	 BIT(MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
1991 
1992 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
1993 # define MMF_DUMP_MASK_DEFAULT_ELF	BIT(MMF_DUMP_ELF_HEADERS)
1994 #else
1995 # define MMF_DUMP_MASK_DEFAULT_ELF	0
1996 #endif
1997 					/* leave room for more dump flags */
1998 #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
1999 #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
2000 
2001 #define MMF_HUGE_ZERO_FOLIO	18      /* mm has ever used the global huge zero folio */
2002 
2003 #define MMF_HAS_UPROBES		19	/* has uprobes */
2004 #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
2005 #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
2006 #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
2007 #define MMF_DISABLE_THP_EXCEPT_ADVISED	23	/* no THP except when advised (e.g., VM_HUGEPAGE) */
2008 #define MMF_DISABLE_THP_COMPLETELY	24	/* no THP for all VMAs */
2009 #define MMF_DISABLE_THP_MASK	(BIT(MMF_DISABLE_THP_COMPLETELY) | \
2010 				 BIT(MMF_DISABLE_THP_EXCEPT_ADVISED))
2011 #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
2012 #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
2013 /*
2014  * MMF_HAS_PINNED: Whether this mm has pinned any pages.  This can be either
2015  * replaced in the future by mm.pinned_vm when it becomes stable, or grow into
2016  * a counter on its own. We're aggressive on this bit for now: even if the
2017  * pinned pages were unpinned later on, we'll still keep this bit set for the
2018  * lifecycle of this mm, just for simplicity.
2019  */
2020 #define MMF_HAS_PINNED		27	/* FOLL_PIN has run, never cleared */
2021 
2022 #define MMF_HAS_MDWE		28
2023 #define MMF_HAS_MDWE_MASK	BIT(MMF_HAS_MDWE)
2024 
2025 #define MMF_HAS_MDWE_NO_INHERIT	29
2026 
2027 #define MMF_VM_MERGE_ANY	30
2028 #define MMF_VM_MERGE_ANY_MASK	BIT(MMF_VM_MERGE_ANY)
2029 
2030 #define MMF_TOPDOWN		31	/* mm searches top down by default */
2031 #define MMF_TOPDOWN_MASK	BIT(MMF_TOPDOWN)
2032 
2033 #define MMF_INIT_LEGACY_MASK	(MMF_DUMP_FILTER_MASK |\
2034 				 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\
2035 				 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK)
2036 
2037 /* Legacy flags must fit within 32 bits. */
2038 static_assert((u64)MMF_INIT_LEGACY_MASK <= (u64)UINT_MAX);
2039 
2040 /*
2041  * Initialise legacy flags according to masks, propagating selected flags on
2042  * fork. Further flag manipulation can be performed by the caller.
2043  */
2044 static inline unsigned long mmf_init_legacy_flags(unsigned long flags)
2045 {
2046 	if (flags & (1UL << MMF_HAS_MDWE_NO_INHERIT))
2047 		flags &= ~((1UL << MMF_HAS_MDWE) |
2048 			   (1UL << MMF_HAS_MDWE_NO_INHERIT));
2049 	return flags & MMF_INIT_LEGACY_MASK;
2050 }
2051 
2052 #endif /* _LINUX_MM_TYPES_H */
2053