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