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