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