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