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