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