xref: /linux-6.15/include/linux/mm_types.h (revision e80a48ba)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MM_TYPES_H
3 #define _LINUX_MM_TYPES_H
4 
5 #include <linux/mm_types_task.h>
6 
7 #include <linux/auxvec.h>
8 #include <linux/kref.h>
9 #include <linux/list.h>
10 #include <linux/spinlock.h>
11 #include <linux/rbtree.h>
12 #include <linux/maple_tree.h>
13 #include <linux/rwsem.h>
14 #include <linux/completion.h>
15 #include <linux/cpumask.h>
16 #include <linux/uprobes.h>
17 #include <linux/rcupdate.h>
18 #include <linux/page-flags-layout.h>
19 #include <linux/workqueue.h>
20 #include <linux/seqlock.h>
21 #include <linux/percpu_counter.h>
22 
23 #include <asm/mmu.h>
24 
25 #ifndef AT_VECTOR_SIZE_ARCH
26 #define AT_VECTOR_SIZE_ARCH 0
27 #endif
28 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
29 
30 #define INIT_PASID	0
31 
32 struct address_space;
33 struct mem_cgroup;
34 
35 /*
36  * Each physical page in the system has a struct page associated with
37  * it to keep track of whatever it is we are using the page for at the
38  * moment. Note that we have no way to track which tasks are using
39  * a page, though if it is a pagecache page, rmap structures can tell us
40  * who is mapping it.
41  *
42  * If you allocate the page using alloc_pages(), you can use some of the
43  * space in struct page for your own purposes.  The five words in the main
44  * union are available, except for bit 0 of the first word which must be
45  * kept clear.  Many users use this word to store a pointer to an object
46  * which is guaranteed to be aligned.  If you use the same storage as
47  * page->mapping, you must restore it to NULL before freeing the page.
48  *
49  * If your page will not be mapped to userspace, you can also use the four
50  * bytes in the mapcount union, but you must call page_mapcount_reset()
51  * before freeing it.
52  *
53  * If you want to use the refcount field, it must be used in such a way
54  * that other CPUs temporarily incrementing and then decrementing the
55  * refcount does not cause problems.  On receiving the page from
56  * alloc_pages(), the refcount will be positive.
57  *
58  * If you allocate pages of order > 0, you can use some of the fields
59  * in each subpage, but you may need to restore some of their values
60  * afterwards.
61  *
62  * SLUB uses cmpxchg_double() to atomically update its freelist and counters.
63  * That requires that freelist & counters in struct slab be adjacent and
64  * double-word aligned. Because struct slab currently just reinterprets the
65  * bits of struct page, we align all struct pages to double-word boundaries,
66  * and ensure that 'freelist' is aligned within struct slab.
67  */
68 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
69 #define _struct_page_alignment	__aligned(2 * sizeof(unsigned long))
70 #else
71 #define _struct_page_alignment	__aligned(sizeof(unsigned long))
72 #endif
73 
74 struct page {
75 	unsigned long flags;		/* Atomic flags, some possibly
76 					 * updated asynchronously */
77 	/*
78 	 * Five words (20/40 bytes) are available in this union.
79 	 * WARNING: bit 0 of the first word is used for PageTail(). That
80 	 * means the other users of this union MUST NOT use the bit to
81 	 * avoid collision and false-positive PageTail().
82 	 */
83 	union {
84 		struct {	/* Page cache and anonymous pages */
85 			/**
86 			 * @lru: Pageout list, eg. active_list protected by
87 			 * lruvec->lru_lock.  Sometimes used as a generic list
88 			 * by the page owner.
89 			 */
90 			union {
91 				struct list_head lru;
92 
93 				/* Or, for the Unevictable "LRU list" slot */
94 				struct {
95 					/* Always even, to negate PageTail */
96 					void *__filler;
97 					/* Count page's or folio's mlocks */
98 					unsigned int mlock_count;
99 				};
100 
101 				/* Or, free page */
102 				struct list_head buddy_list;
103 				struct list_head pcp_list;
104 			};
105 			/* See page-flags.h for PAGE_MAPPING_FLAGS */
106 			struct address_space *mapping;
107 			union {
108 				pgoff_t index;		/* Our offset within mapping. */
109 				unsigned long share;	/* share count for fsdax */
110 			};
111 			/**
112 			 * @private: Mapping-private opaque data.
113 			 * Usually used for buffer_heads if PagePrivate.
114 			 * Used for swp_entry_t if PageSwapCache.
115 			 * Indicates order in the buddy system if PageBuddy.
116 			 */
117 			unsigned long private;
118 		};
119 		struct {	/* page_pool used by netstack */
120 			/**
121 			 * @pp_magic: magic value to avoid recycling non
122 			 * page_pool allocated pages.
123 			 */
124 			unsigned long pp_magic;
125 			struct page_pool *pp;
126 			unsigned long _pp_mapping_pad;
127 			unsigned long dma_addr;
128 			union {
129 				/**
130 				 * dma_addr_upper: might require a 64-bit
131 				 * value on 32-bit architectures.
132 				 */
133 				unsigned long dma_addr_upper;
134 				/**
135 				 * For frag page support, not supported in
136 				 * 32-bit architectures with 64-bit DMA.
137 				 */
138 				atomic_long_t pp_frag_count;
139 			};
140 		};
141 		struct {	/* Tail pages of compound page */
142 			unsigned long compound_head;	/* Bit zero is set */
143 
144 			/* First tail page only */
145 			unsigned char compound_dtor;
146 			unsigned char compound_order;
147 			atomic_t compound_mapcount;
148 			atomic_t subpages_mapcount;
149 			atomic_t compound_pincount;
150 #ifdef CONFIG_64BIT
151 			unsigned int compound_nr; /* 1 << compound_order */
152 #endif
153 		};
154 		struct {	/* Second tail page of transparent huge page */
155 			unsigned long _compound_pad_1;	/* compound_head */
156 			unsigned long _compound_pad_2;
157 			/* For both global and memcg */
158 			struct list_head deferred_list;
159 		};
160 		struct {	/* Second tail page of hugetlb page */
161 			unsigned long _hugetlb_pad_1;	/* compound_head */
162 			void *hugetlb_subpool;
163 			void *hugetlb_cgroup;
164 			void *hugetlb_cgroup_rsvd;
165 			void *hugetlb_hwpoison;
166 			/* No more space on 32-bit: use third tail if more */
167 		};
168 		struct {	/* Page table pages */
169 			unsigned long _pt_pad_1;	/* compound_head */
170 			pgtable_t pmd_huge_pte; /* protected by page->ptl */
171 			unsigned long _pt_pad_2;	/* mapping */
172 			union {
173 				struct mm_struct *pt_mm; /* x86 pgds only */
174 				atomic_t pt_frag_refcount; /* powerpc */
175 			};
176 #if ALLOC_SPLIT_PTLOCKS
177 			spinlock_t *ptl;
178 #else
179 			spinlock_t ptl;
180 #endif
181 		};
182 		struct {	/* ZONE_DEVICE pages */
183 			/** @pgmap: Points to the hosting device page map. */
184 			struct dev_pagemap *pgmap;
185 			void *zone_device_data;
186 			/*
187 			 * ZONE_DEVICE private pages are counted as being
188 			 * mapped so the next 3 words hold the mapping, index,
189 			 * and private fields from the source anonymous or
190 			 * page cache page while the page is migrated to device
191 			 * private memory.
192 			 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
193 			 * use the mapping, index, and private fields when
194 			 * pmem backed DAX files are mapped.
195 			 */
196 		};
197 
198 		/** @rcu_head: You can use this to free a page by RCU. */
199 		struct rcu_head rcu_head;
200 	};
201 
202 	union {		/* This union is 4 bytes in size. */
203 		/*
204 		 * If the page can be mapped to userspace, encodes the number
205 		 * of times this page is referenced by a page table.
206 		 */
207 		atomic_t _mapcount;
208 
209 		/*
210 		 * If the page is neither PageSlab nor mappable to userspace,
211 		 * the value stored here may help determine what this page
212 		 * is used for.  See page-flags.h for a list of page types
213 		 * which are currently stored here.
214 		 */
215 		unsigned int page_type;
216 	};
217 
218 	/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
219 	atomic_t _refcount;
220 
221 #ifdef CONFIG_MEMCG
222 	unsigned long memcg_data;
223 #endif
224 
225 	/*
226 	 * On machines where all RAM is mapped into kernel address space,
227 	 * we can simply calculate the virtual address. On machines with
228 	 * highmem some memory is mapped into kernel virtual memory
229 	 * dynamically, so we need a place to store that address.
230 	 * Note that this field could be 16 bits on x86 ... ;)
231 	 *
232 	 * Architectures with slow multiplication can define
233 	 * WANT_PAGE_VIRTUAL in asm/page.h
234 	 */
235 #if defined(WANT_PAGE_VIRTUAL)
236 	void *virtual;			/* Kernel virtual address (NULL if
237 					   not kmapped, ie. highmem) */
238 #endif /* WANT_PAGE_VIRTUAL */
239 
240 #ifdef CONFIG_KMSAN
241 	/*
242 	 * KMSAN metadata for this page:
243 	 *  - shadow page: every bit indicates whether the corresponding
244 	 *    bit of the original page is initialized (0) or not (1);
245 	 *  - origin page: every 4 bytes contain an id of the stack trace
246 	 *    where the uninitialized value was created.
247 	 */
248 	struct page *kmsan_shadow;
249 	struct page *kmsan_origin;
250 #endif
251 
252 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
253 	int _last_cpupid;
254 #endif
255 } _struct_page_alignment;
256 
257 /*
258  * struct encoded_page - a nonexistent type marking this pointer
259  *
260  * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
261  * with the low bits of the pointer indicating extra context-dependent
262  * information. Not super-common, but happens in mmu_gather and mlock
263  * handling, and this acts as a type system check on that use.
264  *
265  * We only really have two guaranteed bits in general, although you could
266  * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
267  * for more.
268  *
269  * Use the supplied helper functions to endcode/decode the pointer and bits.
270  */
271 struct encoded_page;
272 #define ENCODE_PAGE_BITS 3ul
273 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
274 {
275 	BUILD_BUG_ON(flags > ENCODE_PAGE_BITS);
276 	return (struct encoded_page *)(flags | (unsigned long)page);
277 }
278 
279 static inline unsigned long encoded_page_flags(struct encoded_page *page)
280 {
281 	return ENCODE_PAGE_BITS & (unsigned long)page;
282 }
283 
284 static inline struct page *encoded_page_ptr(struct encoded_page *page)
285 {
286 	return (struct page *)(~ENCODE_PAGE_BITS & (unsigned long)page);
287 }
288 
289 /**
290  * struct folio - Represents a contiguous set of bytes.
291  * @flags: Identical to the page flags.
292  * @lru: Least Recently Used list; tracks how recently this folio was used.
293  * @mlock_count: Number of times this folio has been pinned by mlock().
294  * @mapping: The file this page belongs to, or refers to the anon_vma for
295  *    anonymous memory.
296  * @index: Offset within the file, in units of pages.  For anonymous memory,
297  *    this is the index from the beginning of the mmap.
298  * @private: Filesystem per-folio data (see folio_attach_private()).
299  *    Used for swp_entry_t if folio_test_swapcache().
300  * @_mapcount: Do not access this member directly.  Use folio_mapcount() to
301  *    find out how many times this folio is mapped by userspace.
302  * @_refcount: Do not access this member directly.  Use folio_ref_count()
303  *    to find how many references there are to this folio.
304  * @memcg_data: Memory Control Group data.
305  * @_flags_1: For large folios, additional page flags.
306  * @_head_1: Points to the folio.  Do not use.
307  * @_folio_dtor: Which destructor to use for this folio.
308  * @_folio_order: Do not use directly, call folio_order().
309  * @_compound_mapcount: Do not use directly, call folio_entire_mapcount().
310  * @_subpages_mapcount: Do not use directly, call folio_mapcount().
311  * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
312  * @_folio_nr_pages: Do not use directly, call folio_nr_pages().
313  * @_flags_2: For alignment.  Do not use.
314  * @_head_2: Points to the folio.  Do not use.
315  * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
316  * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
317  * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
318  * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
319  *
320  * A folio is a physically, virtually and logically contiguous set
321  * of bytes.  It is a power-of-two in size, and it is aligned to that
322  * same power-of-two.  It is at least as large as %PAGE_SIZE.  If it is
323  * in the page cache, it is at a file offset which is a multiple of that
324  * power-of-two.  It may be mapped into userspace at an address which is
325  * at an arbitrary page offset, but its kernel virtual address is aligned
326  * to its size.
327  */
328 struct folio {
329 	/* private: don't document the anon union */
330 	union {
331 		struct {
332 	/* public: */
333 			unsigned long flags;
334 			union {
335 				struct list_head lru;
336 	/* private: avoid cluttering the output */
337 				struct {
338 					void *__filler;
339 	/* public: */
340 					unsigned int mlock_count;
341 	/* private: */
342 				};
343 	/* public: */
344 			};
345 			struct address_space *mapping;
346 			pgoff_t index;
347 			void *private;
348 			atomic_t _mapcount;
349 			atomic_t _refcount;
350 #ifdef CONFIG_MEMCG
351 			unsigned long memcg_data;
352 #endif
353 	/* private: the union with struct page is transitional */
354 		};
355 		struct page page;
356 	};
357 	union {
358 		struct {
359 			unsigned long _flags_1;
360 			unsigned long _head_1;
361 			unsigned char _folio_dtor;
362 			unsigned char _folio_order;
363 			atomic_t _compound_mapcount;
364 			atomic_t _subpages_mapcount;
365 			atomic_t _pincount;
366 #ifdef CONFIG_64BIT
367 			unsigned int _folio_nr_pages;
368 #endif
369 		};
370 		struct page __page_1;
371 	};
372 	union {
373 		struct {
374 			unsigned long _flags_2;
375 			unsigned long _head_2;
376 			void *_hugetlb_subpool;
377 			void *_hugetlb_cgroup;
378 			void *_hugetlb_cgroup_rsvd;
379 			void *_hugetlb_hwpoison;
380 		};
381 		struct page __page_2;
382 	};
383 };
384 
385 #define FOLIO_MATCH(pg, fl)						\
386 	static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
387 FOLIO_MATCH(flags, flags);
388 FOLIO_MATCH(lru, lru);
389 FOLIO_MATCH(mapping, mapping);
390 FOLIO_MATCH(compound_head, lru);
391 FOLIO_MATCH(index, index);
392 FOLIO_MATCH(private, private);
393 FOLIO_MATCH(_mapcount, _mapcount);
394 FOLIO_MATCH(_refcount, _refcount);
395 #ifdef CONFIG_MEMCG
396 FOLIO_MATCH(memcg_data, memcg_data);
397 #endif
398 #undef FOLIO_MATCH
399 #define FOLIO_MATCH(pg, fl)						\
400 	static_assert(offsetof(struct folio, fl) ==			\
401 			offsetof(struct page, pg) + sizeof(struct page))
402 FOLIO_MATCH(flags, _flags_1);
403 FOLIO_MATCH(compound_head, _head_1);
404 FOLIO_MATCH(compound_dtor, _folio_dtor);
405 FOLIO_MATCH(compound_order, _folio_order);
406 FOLIO_MATCH(compound_mapcount, _compound_mapcount);
407 FOLIO_MATCH(subpages_mapcount, _subpages_mapcount);
408 FOLIO_MATCH(compound_pincount, _pincount);
409 #ifdef CONFIG_64BIT
410 FOLIO_MATCH(compound_nr, _folio_nr_pages);
411 #endif
412 #undef FOLIO_MATCH
413 #define FOLIO_MATCH(pg, fl)						\
414 	static_assert(offsetof(struct folio, fl) ==			\
415 			offsetof(struct page, pg) + 2 * sizeof(struct page))
416 FOLIO_MATCH(flags, _flags_2);
417 FOLIO_MATCH(compound_head, _head_2);
418 FOLIO_MATCH(hugetlb_subpool, _hugetlb_subpool);
419 FOLIO_MATCH(hugetlb_cgroup, _hugetlb_cgroup);
420 FOLIO_MATCH(hugetlb_cgroup_rsvd, _hugetlb_cgroup_rsvd);
421 FOLIO_MATCH(hugetlb_hwpoison, _hugetlb_hwpoison);
422 #undef FOLIO_MATCH
423 
424 static inline atomic_t *folio_mapcount_ptr(struct folio *folio)
425 {
426 	struct page *tail = &folio->page + 1;
427 	return &tail->compound_mapcount;
428 }
429 
430 static inline atomic_t *folio_subpages_mapcount_ptr(struct folio *folio)
431 {
432 	struct page *tail = &folio->page + 1;
433 	return &tail->subpages_mapcount;
434 }
435 
436 static inline atomic_t *compound_mapcount_ptr(struct page *page)
437 {
438 	return &page[1].compound_mapcount;
439 }
440 
441 static inline atomic_t *subpages_mapcount_ptr(struct page *page)
442 {
443 	return &page[1].subpages_mapcount;
444 }
445 
446 static inline atomic_t *compound_pincount_ptr(struct page *page)
447 {
448 	return &page[1].compound_pincount;
449 }
450 
451 /*
452  * Used for sizing the vmemmap region on some architectures
453  */
454 #define STRUCT_PAGE_MAX_SHIFT	(order_base_2(sizeof(struct page)))
455 
456 #define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK)
457 #define PAGE_FRAG_CACHE_MAX_ORDER	get_order(PAGE_FRAG_CACHE_MAX_SIZE)
458 
459 /*
460  * page_private can be used on tail pages.  However, PagePrivate is only
461  * checked by the VM on the head page.  So page_private on the tail pages
462  * should be used for data that's ancillary to the head page (eg attaching
463  * buffer heads to tail pages after attaching buffer heads to the head page)
464  */
465 #define page_private(page)		((page)->private)
466 
467 static inline void set_page_private(struct page *page, unsigned long private)
468 {
469 	page->private = private;
470 }
471 
472 static inline void *folio_get_private(struct folio *folio)
473 {
474 	return folio->private;
475 }
476 
477 struct page_frag_cache {
478 	void * va;
479 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
480 	__u16 offset;
481 	__u16 size;
482 #else
483 	__u32 offset;
484 #endif
485 	/* we maintain a pagecount bias, so that we dont dirty cache line
486 	 * containing page->_refcount every time we allocate a fragment.
487 	 */
488 	unsigned int		pagecnt_bias;
489 	bool pfmemalloc;
490 };
491 
492 typedef unsigned long vm_flags_t;
493 
494 /*
495  * A region containing a mapping of a non-memory backed file under NOMMU
496  * conditions.  These are held in a global tree and are pinned by the VMAs that
497  * map parts of them.
498  */
499 struct vm_region {
500 	struct rb_node	vm_rb;		/* link in global region tree */
501 	vm_flags_t	vm_flags;	/* VMA vm_flags */
502 	unsigned long	vm_start;	/* start address of region */
503 	unsigned long	vm_end;		/* region initialised to here */
504 	unsigned long	vm_top;		/* region allocated to here */
505 	unsigned long	vm_pgoff;	/* the offset in vm_file corresponding to vm_start */
506 	struct file	*vm_file;	/* the backing file or NULL */
507 
508 	int		vm_usage;	/* region usage count (access under nommu_region_sem) */
509 	bool		vm_icache_flushed : 1; /* true if the icache has been flushed for
510 						* this region */
511 };
512 
513 #ifdef CONFIG_USERFAULTFD
514 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
515 struct vm_userfaultfd_ctx {
516 	struct userfaultfd_ctx *ctx;
517 };
518 #else /* CONFIG_USERFAULTFD */
519 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
520 struct vm_userfaultfd_ctx {};
521 #endif /* CONFIG_USERFAULTFD */
522 
523 struct anon_vma_name {
524 	struct kref kref;
525 	/* The name needs to be at the end because it is dynamically sized. */
526 	char name[];
527 };
528 
529 /*
530  * This struct describes a virtual memory area. There is one of these
531  * per VM-area/task. A VM area is any part of the process virtual memory
532  * space that has a special rule for the page-fault handlers (ie a shared
533  * library, the executable area etc).
534  */
535 struct vm_area_struct {
536 	/* The first cache line has the info for VMA tree walking. */
537 
538 	unsigned long vm_start;		/* Our start address within vm_mm. */
539 	unsigned long vm_end;		/* The first byte after our end address
540 					   within vm_mm. */
541 
542 	struct mm_struct *vm_mm;	/* The address space we belong to. */
543 
544 	/*
545 	 * Access permissions of this VMA.
546 	 * See vmf_insert_mixed_prot() for discussion.
547 	 */
548 	pgprot_t vm_page_prot;
549 	unsigned long vm_flags;		/* Flags, see mm.h. */
550 
551 	/*
552 	 * For areas with an address space and backing store,
553 	 * linkage into the address_space->i_mmap interval tree.
554 	 *
555 	 */
556 	struct {
557 		struct rb_node rb;
558 		unsigned long rb_subtree_last;
559 	} shared;
560 
561 	/*
562 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
563 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
564 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
565 	 * or brk vma (with NULL file) can only be in an anon_vma list.
566 	 */
567 	struct list_head anon_vma_chain; /* Serialized by mmap_lock &
568 					  * page_table_lock */
569 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
570 
571 	/* Function pointers to deal with this struct. */
572 	const struct vm_operations_struct *vm_ops;
573 
574 	/* Information about our backing store: */
575 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
576 					   units */
577 	struct file * vm_file;		/* File we map to (can be NULL). */
578 	void * vm_private_data;		/* was vm_pte (shared mem) */
579 
580 #ifdef CONFIG_ANON_VMA_NAME
581 	/*
582 	 * For private and shared anonymous mappings, a pointer to a null
583 	 * terminated string containing the name given to the vma, or NULL if
584 	 * unnamed. Serialized by mmap_sem. Use anon_vma_name to access.
585 	 */
586 	struct anon_vma_name *anon_name;
587 #endif
588 #ifdef CONFIG_SWAP
589 	atomic_long_t swap_readahead_info;
590 #endif
591 #ifndef CONFIG_MMU
592 	struct vm_region *vm_region;	/* NOMMU mapping region */
593 #endif
594 #ifdef CONFIG_NUMA
595 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
596 #endif
597 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
598 } __randomize_layout;
599 
600 struct kioctx_table;
601 struct mm_struct {
602 	struct {
603 		struct maple_tree mm_mt;
604 #ifdef CONFIG_MMU
605 		unsigned long (*get_unmapped_area) (struct file *filp,
606 				unsigned long addr, unsigned long len,
607 				unsigned long pgoff, unsigned long flags);
608 #endif
609 		unsigned long mmap_base;	/* base of mmap area */
610 		unsigned long mmap_legacy_base;	/* base of mmap area in bottom-up allocations */
611 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
612 		/* Base addresses for compatible mmap() */
613 		unsigned long mmap_compat_base;
614 		unsigned long mmap_compat_legacy_base;
615 #endif
616 		unsigned long task_size;	/* size of task vm space */
617 		pgd_t * pgd;
618 
619 #ifdef CONFIG_MEMBARRIER
620 		/**
621 		 * @membarrier_state: Flags controlling membarrier behavior.
622 		 *
623 		 * This field is close to @pgd to hopefully fit in the same
624 		 * cache-line, which needs to be touched by switch_mm().
625 		 */
626 		atomic_t membarrier_state;
627 #endif
628 
629 		/**
630 		 * @mm_users: The number of users including userspace.
631 		 *
632 		 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
633 		 * drops to 0 (i.e. when the task exits and there are no other
634 		 * temporary reference holders), we also release a reference on
635 		 * @mm_count (which may then free the &struct mm_struct if
636 		 * @mm_count also drops to 0).
637 		 */
638 		atomic_t mm_users;
639 
640 		/**
641 		 * @mm_count: The number of references to &struct mm_struct
642 		 * (@mm_users count as 1).
643 		 *
644 		 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the
645 		 * &struct mm_struct is freed.
646 		 */
647 		atomic_t mm_count;
648 #ifdef CONFIG_SCHED_MM_CID
649 		/**
650 		 * @cid_lock: Protect cid bitmap updates vs lookups.
651 		 *
652 		 * Prevent situations where updates to the cid bitmap happen
653 		 * concurrently with lookups. Those can lead to situations
654 		 * where a lookup cannot find a free bit simply because it was
655 		 * unlucky enough to load, non-atomically, bitmap words as they
656 		 * were being concurrently updated by the updaters.
657 		 */
658 		raw_spinlock_t cid_lock;
659 #endif
660 #ifdef CONFIG_MMU
661 		atomic_long_t pgtables_bytes;	/* PTE page table pages */
662 #endif
663 		int map_count;			/* number of VMAs */
664 
665 		spinlock_t page_table_lock; /* Protects page tables and some
666 					     * counters
667 					     */
668 		/*
669 		 * With some kernel config, the current mmap_lock's offset
670 		 * inside 'mm_struct' is at 0x120, which is very optimal, as
671 		 * its two hot fields 'count' and 'owner' sit in 2 different
672 		 * cachelines,  and when mmap_lock is highly contended, both
673 		 * of the 2 fields will be accessed frequently, current layout
674 		 * will help to reduce cache bouncing.
675 		 *
676 		 * So please be careful with adding new fields before
677 		 * mmap_lock, which can easily push the 2 fields into one
678 		 * cacheline.
679 		 */
680 		struct rw_semaphore mmap_lock;
681 
682 		struct list_head mmlist; /* List of maybe swapped mm's.	These
683 					  * are globally strung together off
684 					  * init_mm.mmlist, and are protected
685 					  * by mmlist_lock
686 					  */
687 
688 
689 		unsigned long hiwater_rss; /* High-watermark of RSS usage */
690 		unsigned long hiwater_vm;  /* High-water virtual memory usage */
691 
692 		unsigned long total_vm;	   /* Total pages mapped */
693 		unsigned long locked_vm;   /* Pages that have PG_mlocked set */
694 		atomic64_t    pinned_vm;   /* Refcount permanently increased */
695 		unsigned long data_vm;	   /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
696 		unsigned long exec_vm;	   /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
697 		unsigned long stack_vm;	   /* VM_STACK */
698 		unsigned long def_flags;
699 
700 		/**
701 		 * @write_protect_seq: Locked when any thread is write
702 		 * protecting pages mapped by this mm to enforce a later COW,
703 		 * for instance during page table copying for fork().
704 		 */
705 		seqcount_t write_protect_seq;
706 
707 		spinlock_t arg_lock; /* protect the below fields */
708 
709 		unsigned long start_code, end_code, start_data, end_data;
710 		unsigned long start_brk, brk, start_stack;
711 		unsigned long arg_start, arg_end, env_start, env_end;
712 
713 		unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
714 
715 		struct percpu_counter rss_stat[NR_MM_COUNTERS];
716 
717 		struct linux_binfmt *binfmt;
718 
719 		/* Architecture-specific MM context */
720 		mm_context_t context;
721 
722 		unsigned long flags; /* Must use atomic bitops to access */
723 
724 #ifdef CONFIG_AIO
725 		spinlock_t			ioctx_lock;
726 		struct kioctx_table __rcu	*ioctx_table;
727 #endif
728 #ifdef CONFIG_MEMCG
729 		/*
730 		 * "owner" points to a task that is regarded as the canonical
731 		 * user/owner of this mm. All of the following must be true in
732 		 * order for it to be changed:
733 		 *
734 		 * current == mm->owner
735 		 * current->mm != mm
736 		 * new_owner->mm == mm
737 		 * new_owner->alloc_lock is held
738 		 */
739 		struct task_struct __rcu *owner;
740 #endif
741 		struct user_namespace *user_ns;
742 
743 		/* store ref to file /proc/<pid>/exe symlink points to */
744 		struct file __rcu *exe_file;
745 #ifdef CONFIG_MMU_NOTIFIER
746 		struct mmu_notifier_subscriptions *notifier_subscriptions;
747 #endif
748 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
749 		pgtable_t pmd_huge_pte; /* protected by page_table_lock */
750 #endif
751 #ifdef CONFIG_NUMA_BALANCING
752 		/*
753 		 * numa_next_scan is the next time that PTEs will be remapped
754 		 * PROT_NONE to trigger NUMA hinting faults; such faults gather
755 		 * statistics and migrate pages to new nodes if necessary.
756 		 */
757 		unsigned long numa_next_scan;
758 
759 		/* Restart point for scanning and remapping PTEs. */
760 		unsigned long numa_scan_offset;
761 
762 		/* numa_scan_seq prevents two threads remapping PTEs. */
763 		int numa_scan_seq;
764 #endif
765 		/*
766 		 * An operation with batched TLB flushing is going on. Anything
767 		 * that can move process memory needs to flush the TLB when
768 		 * moving a PROT_NONE mapped page.
769 		 */
770 		atomic_t tlb_flush_pending;
771 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
772 		/* See flush_tlb_batched_pending() */
773 		atomic_t tlb_flush_batched;
774 #endif
775 		struct uprobes_state uprobes_state;
776 #ifdef CONFIG_PREEMPT_RT
777 		struct rcu_head delayed_drop;
778 #endif
779 #ifdef CONFIG_HUGETLB_PAGE
780 		atomic_long_t hugetlb_usage;
781 #endif
782 		struct work_struct async_put_work;
783 
784 #ifdef CONFIG_IOMMU_SVA
785 		u32 pasid;
786 #endif
787 #ifdef CONFIG_KSM
788 		/*
789 		 * Represent how many pages of this process are involved in KSM
790 		 * merging.
791 		 */
792 		unsigned long ksm_merging_pages;
793 		/*
794 		 * Represent how many pages are checked for ksm merging
795 		 * including merged and not merged.
796 		 */
797 		unsigned long ksm_rmap_items;
798 #endif
799 #ifdef CONFIG_LRU_GEN
800 		struct {
801 			/* this mm_struct is on lru_gen_mm_list */
802 			struct list_head list;
803 			/*
804 			 * Set when switching to this mm_struct, as a hint of
805 			 * whether it has been used since the last time per-node
806 			 * page table walkers cleared the corresponding bits.
807 			 */
808 			unsigned long bitmap;
809 #ifdef CONFIG_MEMCG
810 			/* points to the memcg of "owner" above */
811 			struct mem_cgroup *memcg;
812 #endif
813 		} lru_gen;
814 #endif /* CONFIG_LRU_GEN */
815 	} __randomize_layout;
816 
817 	/*
818 	 * The mm_cpumask needs to be at the end of mm_struct, because it
819 	 * is dynamically sized based on nr_cpu_ids.
820 	 */
821 	unsigned long cpu_bitmap[];
822 };
823 
824 #define MM_MT_FLAGS	(MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN)
825 extern struct mm_struct init_mm;
826 
827 /* Pointer magic because the dynamic array size confuses some compilers. */
828 static inline void mm_init_cpumask(struct mm_struct *mm)
829 {
830 	unsigned long cpu_bitmap = (unsigned long)mm;
831 
832 	cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
833 	cpumask_clear((struct cpumask *)cpu_bitmap);
834 }
835 
836 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
837 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
838 {
839 	return (struct cpumask *)&mm->cpu_bitmap;
840 }
841 
842 #ifdef CONFIG_LRU_GEN
843 
844 struct lru_gen_mm_list {
845 	/* mm_struct list for page table walkers */
846 	struct list_head fifo;
847 	/* protects the list above */
848 	spinlock_t lock;
849 };
850 
851 void lru_gen_add_mm(struct mm_struct *mm);
852 void lru_gen_del_mm(struct mm_struct *mm);
853 #ifdef CONFIG_MEMCG
854 void lru_gen_migrate_mm(struct mm_struct *mm);
855 #endif
856 
857 static inline void lru_gen_init_mm(struct mm_struct *mm)
858 {
859 	INIT_LIST_HEAD(&mm->lru_gen.list);
860 	mm->lru_gen.bitmap = 0;
861 #ifdef CONFIG_MEMCG
862 	mm->lru_gen.memcg = NULL;
863 #endif
864 }
865 
866 static inline void lru_gen_use_mm(struct mm_struct *mm)
867 {
868 	/*
869 	 * When the bitmap is set, page reclaim knows this mm_struct has been
870 	 * used since the last time it cleared the bitmap. So it might be worth
871 	 * walking the page tables of this mm_struct to clear the accessed bit.
872 	 */
873 	WRITE_ONCE(mm->lru_gen.bitmap, -1);
874 }
875 
876 #else /* !CONFIG_LRU_GEN */
877 
878 static inline void lru_gen_add_mm(struct mm_struct *mm)
879 {
880 }
881 
882 static inline void lru_gen_del_mm(struct mm_struct *mm)
883 {
884 }
885 
886 #ifdef CONFIG_MEMCG
887 static inline void lru_gen_migrate_mm(struct mm_struct *mm)
888 {
889 }
890 #endif
891 
892 static inline void lru_gen_init_mm(struct mm_struct *mm)
893 {
894 }
895 
896 static inline void lru_gen_use_mm(struct mm_struct *mm)
897 {
898 }
899 
900 #endif /* CONFIG_LRU_GEN */
901 
902 struct vma_iterator {
903 	struct ma_state mas;
904 };
905 
906 #define VMA_ITERATOR(name, __mm, __addr)				\
907 	struct vma_iterator name = {					\
908 		.mas = {						\
909 			.tree = &(__mm)->mm_mt,				\
910 			.index = __addr,				\
911 			.node = MAS_START,				\
912 		},							\
913 	}
914 
915 static inline void vma_iter_init(struct vma_iterator *vmi,
916 		struct mm_struct *mm, unsigned long addr)
917 {
918 	vmi->mas.tree = &mm->mm_mt;
919 	vmi->mas.index = addr;
920 	vmi->mas.node = MAS_START;
921 }
922 
923 #ifdef CONFIG_SCHED_MM_CID
924 /* Accessor for struct mm_struct's cidmask. */
925 static inline cpumask_t *mm_cidmask(struct mm_struct *mm)
926 {
927 	unsigned long cid_bitmap = (unsigned long)mm;
928 
929 	cid_bitmap += offsetof(struct mm_struct, cpu_bitmap);
930 	/* Skip cpu_bitmap */
931 	cid_bitmap += cpumask_size();
932 	return (struct cpumask *)cid_bitmap;
933 }
934 
935 static inline void mm_init_cid(struct mm_struct *mm)
936 {
937 	raw_spin_lock_init(&mm->cid_lock);
938 	cpumask_clear(mm_cidmask(mm));
939 }
940 
941 static inline unsigned int mm_cid_size(void)
942 {
943 	return cpumask_size();
944 }
945 #else /* CONFIG_SCHED_MM_CID */
946 static inline void mm_init_cid(struct mm_struct *mm) { }
947 static inline unsigned int mm_cid_size(void)
948 {
949 	return 0;
950 }
951 #endif /* CONFIG_SCHED_MM_CID */
952 
953 struct mmu_gather;
954 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
955 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
956 extern void tlb_finish_mmu(struct mmu_gather *tlb);
957 
958 struct vm_fault;
959 
960 /**
961  * typedef vm_fault_t - Return type for page fault handlers.
962  *
963  * Page fault handlers return a bitmask of %VM_FAULT values.
964  */
965 typedef __bitwise unsigned int vm_fault_t;
966 
967 /**
968  * enum vm_fault_reason - Page fault handlers return a bitmask of
969  * these values to tell the core VM what happened when handling the
970  * fault. Used to decide whether a process gets delivered SIGBUS or
971  * just gets major/minor fault counters bumped up.
972  *
973  * @VM_FAULT_OOM:		Out Of Memory
974  * @VM_FAULT_SIGBUS:		Bad access
975  * @VM_FAULT_MAJOR:		Page read from storage
976  * @VM_FAULT_HWPOISON:		Hit poisoned small page
977  * @VM_FAULT_HWPOISON_LARGE:	Hit poisoned large page. Index encoded
978  *				in upper bits
979  * @VM_FAULT_SIGSEGV:		segmentation fault
980  * @VM_FAULT_NOPAGE:		->fault installed the pte, not return page
981  * @VM_FAULT_LOCKED:		->fault locked the returned page
982  * @VM_FAULT_RETRY:		->fault blocked, must retry
983  * @VM_FAULT_FALLBACK:		huge page fault failed, fall back to small
984  * @VM_FAULT_DONE_COW:		->fault has fully handled COW
985  * @VM_FAULT_NEEDDSYNC:		->fault did not modify page tables and needs
986  *				fsync() to complete (for synchronous page faults
987  *				in DAX)
988  * @VM_FAULT_COMPLETED:		->fault completed, meanwhile mmap lock released
989  * @VM_FAULT_HINDEX_MASK:	mask HINDEX value
990  *
991  */
992 enum vm_fault_reason {
993 	VM_FAULT_OOM            = (__force vm_fault_t)0x000001,
994 	VM_FAULT_SIGBUS         = (__force vm_fault_t)0x000002,
995 	VM_FAULT_MAJOR          = (__force vm_fault_t)0x000004,
996 	VM_FAULT_HWPOISON       = (__force vm_fault_t)0x000010,
997 	VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
998 	VM_FAULT_SIGSEGV        = (__force vm_fault_t)0x000040,
999 	VM_FAULT_NOPAGE         = (__force vm_fault_t)0x000100,
1000 	VM_FAULT_LOCKED         = (__force vm_fault_t)0x000200,
1001 	VM_FAULT_RETRY          = (__force vm_fault_t)0x000400,
1002 	VM_FAULT_FALLBACK       = (__force vm_fault_t)0x000800,
1003 	VM_FAULT_DONE_COW       = (__force vm_fault_t)0x001000,
1004 	VM_FAULT_NEEDDSYNC      = (__force vm_fault_t)0x002000,
1005 	VM_FAULT_COMPLETED      = (__force vm_fault_t)0x004000,
1006 	VM_FAULT_HINDEX_MASK    = (__force vm_fault_t)0x0f0000,
1007 };
1008 
1009 /* Encode hstate index for a hwpoisoned large page */
1010 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
1011 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
1012 
1013 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS |	\
1014 			VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON |	\
1015 			VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
1016 
1017 #define VM_FAULT_RESULT_TRACE \
1018 	{ VM_FAULT_OOM,                 "OOM" },	\
1019 	{ VM_FAULT_SIGBUS,              "SIGBUS" },	\
1020 	{ VM_FAULT_MAJOR,               "MAJOR" },	\
1021 	{ VM_FAULT_HWPOISON,            "HWPOISON" },	\
1022 	{ VM_FAULT_HWPOISON_LARGE,      "HWPOISON_LARGE" },	\
1023 	{ VM_FAULT_SIGSEGV,             "SIGSEGV" },	\
1024 	{ VM_FAULT_NOPAGE,              "NOPAGE" },	\
1025 	{ VM_FAULT_LOCKED,              "LOCKED" },	\
1026 	{ VM_FAULT_RETRY,               "RETRY" },	\
1027 	{ VM_FAULT_FALLBACK,            "FALLBACK" },	\
1028 	{ VM_FAULT_DONE_COW,            "DONE_COW" },	\
1029 	{ VM_FAULT_NEEDDSYNC,           "NEEDDSYNC" }
1030 
1031 struct vm_special_mapping {
1032 	const char *name;	/* The name, e.g. "[vdso]". */
1033 
1034 	/*
1035 	 * If .fault is not provided, this points to a
1036 	 * NULL-terminated array of pages that back the special mapping.
1037 	 *
1038 	 * This must not be NULL unless .fault is provided.
1039 	 */
1040 	struct page **pages;
1041 
1042 	/*
1043 	 * If non-NULL, then this is called to resolve page faults
1044 	 * on the special mapping.  If used, .pages is not checked.
1045 	 */
1046 	vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1047 				struct vm_area_struct *vma,
1048 				struct vm_fault *vmf);
1049 
1050 	int (*mremap)(const struct vm_special_mapping *sm,
1051 		     struct vm_area_struct *new_vma);
1052 };
1053 
1054 enum tlb_flush_reason {
1055 	TLB_FLUSH_ON_TASK_SWITCH,
1056 	TLB_REMOTE_SHOOTDOWN,
1057 	TLB_LOCAL_SHOOTDOWN,
1058 	TLB_LOCAL_MM_SHOOTDOWN,
1059 	TLB_REMOTE_SEND_IPI,
1060 	NR_TLB_FLUSH_REASONS,
1061 };
1062 
1063  /*
1064   * A swap entry has to fit into a "unsigned long", as the entry is hidden
1065   * in the "index" field of the swapper address space.
1066   */
1067 typedef struct {
1068 	unsigned long val;
1069 } swp_entry_t;
1070 
1071 /**
1072  * enum fault_flag - Fault flag definitions.
1073  * @FAULT_FLAG_WRITE: Fault was a write fault.
1074  * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
1075  * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
1076  * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
1077  * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
1078  * @FAULT_FLAG_TRIED: The fault has been tried once.
1079  * @FAULT_FLAG_USER: The fault originated in userspace.
1080  * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
1081  * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
1082  * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
1083  * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
1084  *                      COW mapping, making sure that an exclusive anon page is
1085  *                      mapped after the fault.
1086  * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
1087  *                        We should only access orig_pte if this flag set.
1088  *
1089  * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
1090  * whether we would allow page faults to retry by specifying these two
1091  * fault flags correctly.  Currently there can be three legal combinations:
1092  *
1093  * (a) ALLOW_RETRY and !TRIED:  this means the page fault allows retry, and
1094  *                              this is the first try
1095  *
1096  * (b) ALLOW_RETRY and TRIED:   this means the page fault allows retry, and
1097  *                              we've already tried at least once
1098  *
1099  * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
1100  *
1101  * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
1102  * be used.  Note that page faults can be allowed to retry for multiple times,
1103  * in which case we'll have an initial fault with flags (a) then later on
1104  * continuous faults with flags (b).  We should always try to detect pending
1105  * signals before a retry to make sure the continuous page faults can still be
1106  * interrupted if necessary.
1107  *
1108  * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
1109  * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
1110  * applied to mappings that are not COW mappings.
1111  */
1112 enum fault_flag {
1113 	FAULT_FLAG_WRITE =		1 << 0,
1114 	FAULT_FLAG_MKWRITE =		1 << 1,
1115 	FAULT_FLAG_ALLOW_RETRY =	1 << 2,
1116 	FAULT_FLAG_RETRY_NOWAIT = 	1 << 3,
1117 	FAULT_FLAG_KILLABLE =		1 << 4,
1118 	FAULT_FLAG_TRIED = 		1 << 5,
1119 	FAULT_FLAG_USER =		1 << 6,
1120 	FAULT_FLAG_REMOTE =		1 << 7,
1121 	FAULT_FLAG_INSTRUCTION =	1 << 8,
1122 	FAULT_FLAG_INTERRUPTIBLE =	1 << 9,
1123 	FAULT_FLAG_UNSHARE =		1 << 10,
1124 	FAULT_FLAG_ORIG_PTE_VALID =	1 << 11,
1125 };
1126 
1127 typedef unsigned int __bitwise zap_flags_t;
1128 
1129 #endif /* _LINUX_MM_TYPES_H */
1130