xref: /linux-6.15/include/linux/mm_types.h (revision 9e2b3e83)
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/list.h>
9 #include <linux/spinlock.h>
10 #include <linux/rbtree.h>
11 #include <linux/rwsem.h>
12 #include <linux/completion.h>
13 #include <linux/cpumask.h>
14 #include <linux/uprobes.h>
15 #include <linux/rcupdate.h>
16 #include <linux/page-flags-layout.h>
17 #include <linux/workqueue.h>
18 #include <linux/seqlock.h>
19 
20 #include <asm/mmu.h>
21 
22 #ifndef AT_VECTOR_SIZE_ARCH
23 #define AT_VECTOR_SIZE_ARCH 0
24 #endif
25 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
26 
27 #define INIT_PASID	0
28 
29 struct address_space;
30 struct mem_cgroup;
31 
32 /*
33  * Each physical page in the system has a struct page associated with
34  * it to keep track of whatever it is we are using the page for at the
35  * moment. Note that we have no way to track which tasks are using
36  * a page, though if it is a pagecache page, rmap structures can tell us
37  * who is mapping it.
38  *
39  * If you allocate the page using alloc_pages(), you can use some of the
40  * space in struct page for your own purposes.  The five words in the main
41  * union are available, except for bit 0 of the first word which must be
42  * kept clear.  Many users use this word to store a pointer to an object
43  * which is guaranteed to be aligned.  If you use the same storage as
44  * page->mapping, you must restore it to NULL before freeing the page.
45  *
46  * If your page will not be mapped to userspace, you can also use the four
47  * bytes in the mapcount union, but you must call page_mapcount_reset()
48  * before freeing it.
49  *
50  * If you want to use the refcount field, it must be used in such a way
51  * that other CPUs temporarily incrementing and then decrementing the
52  * refcount does not cause problems.  On receiving the page from
53  * alloc_pages(), the refcount will be positive.
54  *
55  * If you allocate pages of order > 0, you can use some of the fields
56  * in each subpage, but you may need to restore some of their values
57  * afterwards.
58  *
59  * SLUB uses cmpxchg_double() to atomically update its freelist and
60  * counters.  That requires that freelist & counters be adjacent and
61  * double-word aligned.  We align all struct pages to double-word
62  * boundaries, and ensure that 'freelist' is aligned within the
63  * struct.
64  */
65 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
66 #define _struct_page_alignment	__aligned(2 * sizeof(unsigned long))
67 #else
68 #define _struct_page_alignment
69 #endif
70 
71 struct page {
72 	unsigned long flags;		/* Atomic flags, some possibly
73 					 * updated asynchronously */
74 	/*
75 	 * Five words (20/40 bytes) are available in this union.
76 	 * WARNING: bit 0 of the first word is used for PageTail(). That
77 	 * means the other users of this union MUST NOT use the bit to
78 	 * avoid collision and false-positive PageTail().
79 	 */
80 	union {
81 		struct {	/* Page cache and anonymous pages */
82 			/**
83 			 * @lru: Pageout list, eg. active_list protected by
84 			 * lruvec->lru_lock.  Sometimes used as a generic list
85 			 * by the page owner.
86 			 */
87 			struct list_head lru;
88 			/* See page-flags.h for PAGE_MAPPING_FLAGS */
89 			struct address_space *mapping;
90 			pgoff_t index;		/* Our offset within mapping. */
91 			/**
92 			 * @private: Mapping-private opaque data.
93 			 * Usually used for buffer_heads if PagePrivate.
94 			 * Used for swp_entry_t if PageSwapCache.
95 			 * Indicates order in the buddy system if PageBuddy.
96 			 */
97 			unsigned long private;
98 		};
99 		struct {	/* page_pool used by netstack */
100 			/**
101 			 * @pp_magic: magic value to avoid recycling non
102 			 * page_pool allocated pages.
103 			 */
104 			unsigned long pp_magic;
105 			struct page_pool *pp;
106 			unsigned long _pp_mapping_pad;
107 			unsigned long dma_addr;
108 			union {
109 				/**
110 				 * dma_addr_upper: might require a 64-bit
111 				 * value on 32-bit architectures.
112 				 */
113 				unsigned long dma_addr_upper;
114 				/**
115 				 * For frag page support, not supported in
116 				 * 32-bit architectures with 64-bit DMA.
117 				 */
118 				atomic_long_t pp_frag_count;
119 			};
120 		};
121 		struct {	/* slab, slob and slub */
122 			union {
123 				struct list_head slab_list;
124 				struct {	/* Partial pages */
125 					struct page *next;
126 #ifdef CONFIG_64BIT
127 					int pages;	/* Nr of pages left */
128 					int pobjects;	/* Approximate count */
129 #else
130 					short int pages;
131 					short int pobjects;
132 #endif
133 				};
134 			};
135 			struct kmem_cache *slab_cache; /* not slob */
136 			/* Double-word boundary */
137 			void *freelist;		/* first free object */
138 			union {
139 				void *s_mem;	/* slab: first object */
140 				unsigned long counters;		/* SLUB */
141 				struct {			/* SLUB */
142 					unsigned inuse:16;
143 					unsigned objects:15;
144 					unsigned frozen:1;
145 				};
146 			};
147 		};
148 		struct {	/* Tail pages of compound page */
149 			unsigned long compound_head;	/* Bit zero is set */
150 
151 			/* First tail page only */
152 			unsigned char compound_dtor;
153 			unsigned char compound_order;
154 			atomic_t compound_mapcount;
155 			unsigned int compound_nr; /* 1 << compound_order */
156 		};
157 		struct {	/* Second tail page of compound page */
158 			unsigned long _compound_pad_1;	/* compound_head */
159 			atomic_t hpage_pinned_refcount;
160 			/* For both global and memcg */
161 			struct list_head deferred_list;
162 		};
163 		struct {	/* Page table pages */
164 			unsigned long _pt_pad_1;	/* compound_head */
165 			pgtable_t pmd_huge_pte; /* protected by page->ptl */
166 			unsigned long _pt_pad_2;	/* mapping */
167 			union {
168 				struct mm_struct *pt_mm; /* x86 pgds only */
169 				atomic_t pt_frag_refcount; /* powerpc */
170 			};
171 #if ALLOC_SPLIT_PTLOCKS
172 			spinlock_t *ptl;
173 #else
174 			spinlock_t ptl;
175 #endif
176 		};
177 		struct {	/* ZONE_DEVICE pages */
178 			/** @pgmap: Points to the hosting device page map. */
179 			struct dev_pagemap *pgmap;
180 			void *zone_device_data;
181 			/*
182 			 * ZONE_DEVICE private pages are counted as being
183 			 * mapped so the next 3 words hold the mapping, index,
184 			 * and private fields from the source anonymous or
185 			 * page cache page while the page is migrated to device
186 			 * private memory.
187 			 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
188 			 * use the mapping, index, and private fields when
189 			 * pmem backed DAX files are mapped.
190 			 */
191 		};
192 
193 		/** @rcu_head: You can use this to free a page by RCU. */
194 		struct rcu_head rcu_head;
195 	};
196 
197 	union {		/* This union is 4 bytes in size. */
198 		/*
199 		 * If the page can be mapped to userspace, encodes the number
200 		 * of times this page is referenced by a page table.
201 		 */
202 		atomic_t _mapcount;
203 
204 		/*
205 		 * If the page is neither PageSlab nor mappable to userspace,
206 		 * the value stored here may help determine what this page
207 		 * is used for.  See page-flags.h for a list of page types
208 		 * which are currently stored here.
209 		 */
210 		unsigned int page_type;
211 
212 		unsigned int active;		/* SLAB */
213 		int units;			/* SLOB */
214 	};
215 
216 	/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
217 	atomic_t _refcount;
218 
219 #ifdef CONFIG_MEMCG
220 	unsigned long memcg_data;
221 #endif
222 
223 	/*
224 	 * On machines where all RAM is mapped into kernel address space,
225 	 * we can simply calculate the virtual address. On machines with
226 	 * highmem some memory is mapped into kernel virtual memory
227 	 * dynamically, so we need a place to store that address.
228 	 * Note that this field could be 16 bits on x86 ... ;)
229 	 *
230 	 * Architectures with slow multiplication can define
231 	 * WANT_PAGE_VIRTUAL in asm/page.h
232 	 */
233 #if defined(WANT_PAGE_VIRTUAL)
234 	void *virtual;			/* Kernel virtual address (NULL if
235 					   not kmapped, ie. highmem) */
236 #endif /* WANT_PAGE_VIRTUAL */
237 
238 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
239 	int _last_cpupid;
240 #endif
241 } _struct_page_alignment;
242 
243 /**
244  * struct folio - Represents a contiguous set of bytes.
245  * @flags: Identical to the page flags.
246  * @lru: Least Recently Used list; tracks how recently this folio was used.
247  * @mapping: The file this page belongs to, or refers to the anon_vma for
248  *    anonymous memory.
249  * @index: Offset within the file, in units of pages.  For anonymous memory,
250  *    this is the index from the beginning of the mmap.
251  * @private: Filesystem per-folio data (see folio_attach_private()).
252  *    Used for swp_entry_t if folio_test_swapcache().
253  * @_mapcount: Do not access this member directly.  Use folio_mapcount() to
254  *    find out how many times this folio is mapped by userspace.
255  * @_refcount: Do not access this member directly.  Use folio_ref_count()
256  *    to find how many references there are to this folio.
257  * @memcg_data: Memory Control Group data.
258  *
259  * A folio is a physically, virtually and logically contiguous set
260  * of bytes.  It is a power-of-two in size, and it is aligned to that
261  * same power-of-two.  It is at least as large as %PAGE_SIZE.  If it is
262  * in the page cache, it is at a file offset which is a multiple of that
263  * power-of-two.  It may be mapped into userspace at an address which is
264  * at an arbitrary page offset, but its kernel virtual address is aligned
265  * to its size.
266  */
267 struct folio {
268 	/* private: don't document the anon union */
269 	union {
270 		struct {
271 	/* public: */
272 			unsigned long flags;
273 			struct list_head lru;
274 			struct address_space *mapping;
275 			pgoff_t index;
276 			void *private;
277 			atomic_t _mapcount;
278 			atomic_t _refcount;
279 #ifdef CONFIG_MEMCG
280 			unsigned long memcg_data;
281 #endif
282 	/* private: the union with struct page is transitional */
283 		};
284 		struct page page;
285 	};
286 };
287 
288 static_assert(sizeof(struct page) == sizeof(struct folio));
289 #define FOLIO_MATCH(pg, fl)						\
290 	static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
291 FOLIO_MATCH(flags, flags);
292 FOLIO_MATCH(lru, lru);
293 FOLIO_MATCH(compound_head, lru);
294 FOLIO_MATCH(index, index);
295 FOLIO_MATCH(private, private);
296 FOLIO_MATCH(_mapcount, _mapcount);
297 FOLIO_MATCH(_refcount, _refcount);
298 #ifdef CONFIG_MEMCG
299 FOLIO_MATCH(memcg_data, memcg_data);
300 #endif
301 #undef FOLIO_MATCH
302 
303 static inline atomic_t *folio_mapcount_ptr(struct folio *folio)
304 {
305 	struct page *tail = &folio->page + 1;
306 	return &tail->compound_mapcount;
307 }
308 
309 static inline atomic_t *compound_mapcount_ptr(struct page *page)
310 {
311 	return &page[1].compound_mapcount;
312 }
313 
314 static inline atomic_t *compound_pincount_ptr(struct page *page)
315 {
316 	return &page[2].hpage_pinned_refcount;
317 }
318 
319 /*
320  * Used for sizing the vmemmap region on some architectures
321  */
322 #define STRUCT_PAGE_MAX_SHIFT	(order_base_2(sizeof(struct page)))
323 
324 #define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK)
325 #define PAGE_FRAG_CACHE_MAX_ORDER	get_order(PAGE_FRAG_CACHE_MAX_SIZE)
326 
327 /*
328  * page_private can be used on tail pages.  However, PagePrivate is only
329  * checked by the VM on the head page.  So page_private on the tail pages
330  * should be used for data that's ancillary to the head page (eg attaching
331  * buffer heads to tail pages after attaching buffer heads to the head page)
332  */
333 #define page_private(page)		((page)->private)
334 
335 static inline void set_page_private(struct page *page, unsigned long private)
336 {
337 	page->private = private;
338 }
339 
340 static inline void *folio_get_private(struct folio *folio)
341 {
342 	return folio->private;
343 }
344 
345 struct page_frag_cache {
346 	void * va;
347 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
348 	__u16 offset;
349 	__u16 size;
350 #else
351 	__u32 offset;
352 #endif
353 	/* we maintain a pagecount bias, so that we dont dirty cache line
354 	 * containing page->_refcount every time we allocate a fragment.
355 	 */
356 	unsigned int		pagecnt_bias;
357 	bool pfmemalloc;
358 };
359 
360 typedef unsigned long vm_flags_t;
361 
362 /*
363  * A region containing a mapping of a non-memory backed file under NOMMU
364  * conditions.  These are held in a global tree and are pinned by the VMAs that
365  * map parts of them.
366  */
367 struct vm_region {
368 	struct rb_node	vm_rb;		/* link in global region tree */
369 	vm_flags_t	vm_flags;	/* VMA vm_flags */
370 	unsigned long	vm_start;	/* start address of region */
371 	unsigned long	vm_end;		/* region initialised to here */
372 	unsigned long	vm_top;		/* region allocated to here */
373 	unsigned long	vm_pgoff;	/* the offset in vm_file corresponding to vm_start */
374 	struct file	*vm_file;	/* the backing file or NULL */
375 
376 	int		vm_usage;	/* region usage count (access under nommu_region_sem) */
377 	bool		vm_icache_flushed : 1; /* true if the icache has been flushed for
378 						* this region */
379 };
380 
381 #ifdef CONFIG_USERFAULTFD
382 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
383 struct vm_userfaultfd_ctx {
384 	struct userfaultfd_ctx *ctx;
385 };
386 #else /* CONFIG_USERFAULTFD */
387 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
388 struct vm_userfaultfd_ctx {};
389 #endif /* CONFIG_USERFAULTFD */
390 
391 /*
392  * This struct describes a virtual memory area. There is one of these
393  * per VM-area/task. A VM area is any part of the process virtual memory
394  * space that has a special rule for the page-fault handlers (ie a shared
395  * library, the executable area etc).
396  */
397 struct vm_area_struct {
398 	/* The first cache line has the info for VMA tree walking. */
399 
400 	unsigned long vm_start;		/* Our start address within vm_mm. */
401 	unsigned long vm_end;		/* The first byte after our end address
402 					   within vm_mm. */
403 
404 	/* linked list of VM areas per task, sorted by address */
405 	struct vm_area_struct *vm_next, *vm_prev;
406 
407 	struct rb_node vm_rb;
408 
409 	/*
410 	 * Largest free memory gap in bytes to the left of this VMA.
411 	 * Either between this VMA and vma->vm_prev, or between one of the
412 	 * VMAs below us in the VMA rbtree and its ->vm_prev. This helps
413 	 * get_unmapped_area find a free area of the right size.
414 	 */
415 	unsigned long rb_subtree_gap;
416 
417 	/* Second cache line starts here. */
418 
419 	struct mm_struct *vm_mm;	/* The address space we belong to. */
420 
421 	/*
422 	 * Access permissions of this VMA.
423 	 * See vmf_insert_mixed_prot() for discussion.
424 	 */
425 	pgprot_t vm_page_prot;
426 	unsigned long vm_flags;		/* Flags, see mm.h. */
427 
428 	/*
429 	 * For areas with an address space and backing store,
430 	 * linkage into the address_space->i_mmap interval tree.
431 	 */
432 	struct {
433 		struct rb_node rb;
434 		unsigned long rb_subtree_last;
435 	} shared;
436 
437 	/*
438 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
439 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
440 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
441 	 * or brk vma (with NULL file) can only be in an anon_vma list.
442 	 */
443 	struct list_head anon_vma_chain; /* Serialized by mmap_lock &
444 					  * page_table_lock */
445 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
446 
447 	/* Function pointers to deal with this struct. */
448 	const struct vm_operations_struct *vm_ops;
449 
450 	/* Information about our backing store: */
451 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
452 					   units */
453 	struct file * vm_file;		/* File we map to (can be NULL). */
454 	void * vm_private_data;		/* was vm_pte (shared mem) */
455 
456 #ifdef CONFIG_SWAP
457 	atomic_long_t swap_readahead_info;
458 #endif
459 #ifndef CONFIG_MMU
460 	struct vm_region *vm_region;	/* NOMMU mapping region */
461 #endif
462 #ifdef CONFIG_NUMA
463 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
464 #endif
465 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
466 } __randomize_layout;
467 
468 struct core_thread {
469 	struct task_struct *task;
470 	struct core_thread *next;
471 };
472 
473 struct core_state {
474 	atomic_t nr_threads;
475 	struct core_thread dumper;
476 	struct completion startup;
477 };
478 
479 struct kioctx_table;
480 struct mm_struct {
481 	struct {
482 		struct vm_area_struct *mmap;		/* list of VMAs */
483 		struct rb_root mm_rb;
484 		u64 vmacache_seqnum;                   /* per-thread vmacache */
485 #ifdef CONFIG_MMU
486 		unsigned long (*get_unmapped_area) (struct file *filp,
487 				unsigned long addr, unsigned long len,
488 				unsigned long pgoff, unsigned long flags);
489 #endif
490 		unsigned long mmap_base;	/* base of mmap area */
491 		unsigned long mmap_legacy_base;	/* base of mmap area in bottom-up allocations */
492 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
493 		/* Base addresses for compatible mmap() */
494 		unsigned long mmap_compat_base;
495 		unsigned long mmap_compat_legacy_base;
496 #endif
497 		unsigned long task_size;	/* size of task vm space */
498 		unsigned long highest_vm_end;	/* highest vma end address */
499 		pgd_t * pgd;
500 
501 #ifdef CONFIG_MEMBARRIER
502 		/**
503 		 * @membarrier_state: Flags controlling membarrier behavior.
504 		 *
505 		 * This field is close to @pgd to hopefully fit in the same
506 		 * cache-line, which needs to be touched by switch_mm().
507 		 */
508 		atomic_t membarrier_state;
509 #endif
510 
511 		/**
512 		 * @mm_users: The number of users including userspace.
513 		 *
514 		 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
515 		 * drops to 0 (i.e. when the task exits and there are no other
516 		 * temporary reference holders), we also release a reference on
517 		 * @mm_count (which may then free the &struct mm_struct if
518 		 * @mm_count also drops to 0).
519 		 */
520 		atomic_t mm_users;
521 
522 		/**
523 		 * @mm_count: The number of references to &struct mm_struct
524 		 * (@mm_users count as 1).
525 		 *
526 		 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the
527 		 * &struct mm_struct is freed.
528 		 */
529 		atomic_t mm_count;
530 
531 #ifdef CONFIG_MMU
532 		atomic_long_t pgtables_bytes;	/* PTE page table pages */
533 #endif
534 		int map_count;			/* number of VMAs */
535 
536 		spinlock_t page_table_lock; /* Protects page tables and some
537 					     * counters
538 					     */
539 		/*
540 		 * With some kernel config, the current mmap_lock's offset
541 		 * inside 'mm_struct' is at 0x120, which is very optimal, as
542 		 * its two hot fields 'count' and 'owner' sit in 2 different
543 		 * cachelines,  and when mmap_lock is highly contended, both
544 		 * of the 2 fields will be accessed frequently, current layout
545 		 * will help to reduce cache bouncing.
546 		 *
547 		 * So please be careful with adding new fields before
548 		 * mmap_lock, which can easily push the 2 fields into one
549 		 * cacheline.
550 		 */
551 		struct rw_semaphore mmap_lock;
552 
553 		struct list_head mmlist; /* List of maybe swapped mm's.	These
554 					  * are globally strung together off
555 					  * init_mm.mmlist, and are protected
556 					  * by mmlist_lock
557 					  */
558 
559 
560 		unsigned long hiwater_rss; /* High-watermark of RSS usage */
561 		unsigned long hiwater_vm;  /* High-water virtual memory usage */
562 
563 		unsigned long total_vm;	   /* Total pages mapped */
564 		unsigned long locked_vm;   /* Pages that have PG_mlocked set */
565 		atomic64_t    pinned_vm;   /* Refcount permanently increased */
566 		unsigned long data_vm;	   /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
567 		unsigned long exec_vm;	   /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
568 		unsigned long stack_vm;	   /* VM_STACK */
569 		unsigned long def_flags;
570 
571 		/**
572 		 * @write_protect_seq: Locked when any thread is write
573 		 * protecting pages mapped by this mm to enforce a later COW,
574 		 * for instance during page table copying for fork().
575 		 */
576 		seqcount_t write_protect_seq;
577 
578 		spinlock_t arg_lock; /* protect the below fields */
579 
580 		unsigned long start_code, end_code, start_data, end_data;
581 		unsigned long start_brk, brk, start_stack;
582 		unsigned long arg_start, arg_end, env_start, env_end;
583 
584 		unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
585 
586 		/*
587 		 * Special counters, in some configurations protected by the
588 		 * page_table_lock, in other configurations by being atomic.
589 		 */
590 		struct mm_rss_stat rss_stat;
591 
592 		struct linux_binfmt *binfmt;
593 
594 		/* Architecture-specific MM context */
595 		mm_context_t context;
596 
597 		unsigned long flags; /* Must use atomic bitops to access */
598 
599 		struct core_state *core_state; /* coredumping support */
600 
601 #ifdef CONFIG_AIO
602 		spinlock_t			ioctx_lock;
603 		struct kioctx_table __rcu	*ioctx_table;
604 #endif
605 #ifdef CONFIG_MEMCG
606 		/*
607 		 * "owner" points to a task that is regarded as the canonical
608 		 * user/owner of this mm. All of the following must be true in
609 		 * order for it to be changed:
610 		 *
611 		 * current == mm->owner
612 		 * current->mm != mm
613 		 * new_owner->mm == mm
614 		 * new_owner->alloc_lock is held
615 		 */
616 		struct task_struct __rcu *owner;
617 #endif
618 		struct user_namespace *user_ns;
619 
620 		/* store ref to file /proc/<pid>/exe symlink points to */
621 		struct file __rcu *exe_file;
622 #ifdef CONFIG_MMU_NOTIFIER
623 		struct mmu_notifier_subscriptions *notifier_subscriptions;
624 #endif
625 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
626 		pgtable_t pmd_huge_pte; /* protected by page_table_lock */
627 #endif
628 #ifdef CONFIG_NUMA_BALANCING
629 		/*
630 		 * numa_next_scan is the next time that the PTEs will be marked
631 		 * pte_numa. NUMA hinting faults will gather statistics and
632 		 * migrate pages to new nodes if necessary.
633 		 */
634 		unsigned long numa_next_scan;
635 
636 		/* Restart point for scanning and setting pte_numa */
637 		unsigned long numa_scan_offset;
638 
639 		/* numa_scan_seq prevents two threads setting pte_numa */
640 		int numa_scan_seq;
641 #endif
642 		/*
643 		 * An operation with batched TLB flushing is going on. Anything
644 		 * that can move process memory needs to flush the TLB when
645 		 * moving a PROT_NONE or PROT_NUMA mapped page.
646 		 */
647 		atomic_t tlb_flush_pending;
648 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
649 		/* See flush_tlb_batched_pending() */
650 		bool tlb_flush_batched;
651 #endif
652 		struct uprobes_state uprobes_state;
653 #ifdef CONFIG_PREEMPT_RT
654 		struct rcu_head delayed_drop;
655 #endif
656 #ifdef CONFIG_HUGETLB_PAGE
657 		atomic_long_t hugetlb_usage;
658 #endif
659 		struct work_struct async_put_work;
660 
661 #ifdef CONFIG_IOMMU_SUPPORT
662 		u32 pasid;
663 #endif
664 	} __randomize_layout;
665 
666 	/*
667 	 * The mm_cpumask needs to be at the end of mm_struct, because it
668 	 * is dynamically sized based on nr_cpu_ids.
669 	 */
670 	unsigned long cpu_bitmap[];
671 };
672 
673 extern struct mm_struct init_mm;
674 
675 /* Pointer magic because the dynamic array size confuses some compilers. */
676 static inline void mm_init_cpumask(struct mm_struct *mm)
677 {
678 	unsigned long cpu_bitmap = (unsigned long)mm;
679 
680 	cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
681 	cpumask_clear((struct cpumask *)cpu_bitmap);
682 }
683 
684 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
685 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
686 {
687 	return (struct cpumask *)&mm->cpu_bitmap;
688 }
689 
690 struct mmu_gather;
691 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
692 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
693 extern void tlb_finish_mmu(struct mmu_gather *tlb);
694 
695 static inline void init_tlb_flush_pending(struct mm_struct *mm)
696 {
697 	atomic_set(&mm->tlb_flush_pending, 0);
698 }
699 
700 static inline void inc_tlb_flush_pending(struct mm_struct *mm)
701 {
702 	atomic_inc(&mm->tlb_flush_pending);
703 	/*
704 	 * The only time this value is relevant is when there are indeed pages
705 	 * to flush. And we'll only flush pages after changing them, which
706 	 * requires the PTL.
707 	 *
708 	 * So the ordering here is:
709 	 *
710 	 *	atomic_inc(&mm->tlb_flush_pending);
711 	 *	spin_lock(&ptl);
712 	 *	...
713 	 *	set_pte_at();
714 	 *	spin_unlock(&ptl);
715 	 *
716 	 *				spin_lock(&ptl)
717 	 *				mm_tlb_flush_pending();
718 	 *				....
719 	 *				spin_unlock(&ptl);
720 	 *
721 	 *	flush_tlb_range();
722 	 *	atomic_dec(&mm->tlb_flush_pending);
723 	 *
724 	 * Where the increment if constrained by the PTL unlock, it thus
725 	 * ensures that the increment is visible if the PTE modification is
726 	 * visible. After all, if there is no PTE modification, nobody cares
727 	 * about TLB flushes either.
728 	 *
729 	 * This very much relies on users (mm_tlb_flush_pending() and
730 	 * mm_tlb_flush_nested()) only caring about _specific_ PTEs (and
731 	 * therefore specific PTLs), because with SPLIT_PTE_PTLOCKS and RCpc
732 	 * locks (PPC) the unlock of one doesn't order against the lock of
733 	 * another PTL.
734 	 *
735 	 * The decrement is ordered by the flush_tlb_range(), such that
736 	 * mm_tlb_flush_pending() will not return false unless all flushes have
737 	 * completed.
738 	 */
739 }
740 
741 static inline void dec_tlb_flush_pending(struct mm_struct *mm)
742 {
743 	/*
744 	 * See inc_tlb_flush_pending().
745 	 *
746 	 * This cannot be smp_mb__before_atomic() because smp_mb() simply does
747 	 * not order against TLB invalidate completion, which is what we need.
748 	 *
749 	 * Therefore we must rely on tlb_flush_*() to guarantee order.
750 	 */
751 	atomic_dec(&mm->tlb_flush_pending);
752 }
753 
754 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
755 {
756 	/*
757 	 * Must be called after having acquired the PTL; orders against that
758 	 * PTLs release and therefore ensures that if we observe the modified
759 	 * PTE we must also observe the increment from inc_tlb_flush_pending().
760 	 *
761 	 * That is, it only guarantees to return true if there is a flush
762 	 * pending for _this_ PTL.
763 	 */
764 	return atomic_read(&mm->tlb_flush_pending);
765 }
766 
767 static inline bool mm_tlb_flush_nested(struct mm_struct *mm)
768 {
769 	/*
770 	 * Similar to mm_tlb_flush_pending(), we must have acquired the PTL
771 	 * for which there is a TLB flush pending in order to guarantee
772 	 * we've seen both that PTE modification and the increment.
773 	 *
774 	 * (no requirement on actually still holding the PTL, that is irrelevant)
775 	 */
776 	return atomic_read(&mm->tlb_flush_pending) > 1;
777 }
778 
779 struct vm_fault;
780 
781 /**
782  * typedef vm_fault_t - Return type for page fault handlers.
783  *
784  * Page fault handlers return a bitmask of %VM_FAULT values.
785  */
786 typedef __bitwise unsigned int vm_fault_t;
787 
788 /**
789  * enum vm_fault_reason - Page fault handlers return a bitmask of
790  * these values to tell the core VM what happened when handling the
791  * fault. Used to decide whether a process gets delivered SIGBUS or
792  * just gets major/minor fault counters bumped up.
793  *
794  * @VM_FAULT_OOM:		Out Of Memory
795  * @VM_FAULT_SIGBUS:		Bad access
796  * @VM_FAULT_MAJOR:		Page read from storage
797  * @VM_FAULT_WRITE:		Special case for get_user_pages
798  * @VM_FAULT_HWPOISON:		Hit poisoned small page
799  * @VM_FAULT_HWPOISON_LARGE:	Hit poisoned large page. Index encoded
800  *				in upper bits
801  * @VM_FAULT_SIGSEGV:		segmentation fault
802  * @VM_FAULT_NOPAGE:		->fault installed the pte, not return page
803  * @VM_FAULT_LOCKED:		->fault locked the returned page
804  * @VM_FAULT_RETRY:		->fault blocked, must retry
805  * @VM_FAULT_FALLBACK:		huge page fault failed, fall back to small
806  * @VM_FAULT_DONE_COW:		->fault has fully handled COW
807  * @VM_FAULT_NEEDDSYNC:		->fault did not modify page tables and needs
808  *				fsync() to complete (for synchronous page faults
809  *				in DAX)
810  * @VM_FAULT_HINDEX_MASK:	mask HINDEX value
811  *
812  */
813 enum vm_fault_reason {
814 	VM_FAULT_OOM            = (__force vm_fault_t)0x000001,
815 	VM_FAULT_SIGBUS         = (__force vm_fault_t)0x000002,
816 	VM_FAULT_MAJOR          = (__force vm_fault_t)0x000004,
817 	VM_FAULT_WRITE          = (__force vm_fault_t)0x000008,
818 	VM_FAULT_HWPOISON       = (__force vm_fault_t)0x000010,
819 	VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
820 	VM_FAULT_SIGSEGV        = (__force vm_fault_t)0x000040,
821 	VM_FAULT_NOPAGE         = (__force vm_fault_t)0x000100,
822 	VM_FAULT_LOCKED         = (__force vm_fault_t)0x000200,
823 	VM_FAULT_RETRY          = (__force vm_fault_t)0x000400,
824 	VM_FAULT_FALLBACK       = (__force vm_fault_t)0x000800,
825 	VM_FAULT_DONE_COW       = (__force vm_fault_t)0x001000,
826 	VM_FAULT_NEEDDSYNC      = (__force vm_fault_t)0x002000,
827 	VM_FAULT_HINDEX_MASK    = (__force vm_fault_t)0x0f0000,
828 };
829 
830 /* Encode hstate index for a hwpoisoned large page */
831 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
832 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
833 
834 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS |	\
835 			VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON |	\
836 			VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
837 
838 #define VM_FAULT_RESULT_TRACE \
839 	{ VM_FAULT_OOM,                 "OOM" },	\
840 	{ VM_FAULT_SIGBUS,              "SIGBUS" },	\
841 	{ VM_FAULT_MAJOR,               "MAJOR" },	\
842 	{ VM_FAULT_WRITE,               "WRITE" },	\
843 	{ VM_FAULT_HWPOISON,            "HWPOISON" },	\
844 	{ VM_FAULT_HWPOISON_LARGE,      "HWPOISON_LARGE" },	\
845 	{ VM_FAULT_SIGSEGV,             "SIGSEGV" },	\
846 	{ VM_FAULT_NOPAGE,              "NOPAGE" },	\
847 	{ VM_FAULT_LOCKED,              "LOCKED" },	\
848 	{ VM_FAULT_RETRY,               "RETRY" },	\
849 	{ VM_FAULT_FALLBACK,            "FALLBACK" },	\
850 	{ VM_FAULT_DONE_COW,            "DONE_COW" },	\
851 	{ VM_FAULT_NEEDDSYNC,           "NEEDDSYNC" }
852 
853 struct vm_special_mapping {
854 	const char *name;	/* The name, e.g. "[vdso]". */
855 
856 	/*
857 	 * If .fault is not provided, this points to a
858 	 * NULL-terminated array of pages that back the special mapping.
859 	 *
860 	 * This must not be NULL unless .fault is provided.
861 	 */
862 	struct page **pages;
863 
864 	/*
865 	 * If non-NULL, then this is called to resolve page faults
866 	 * on the special mapping.  If used, .pages is not checked.
867 	 */
868 	vm_fault_t (*fault)(const struct vm_special_mapping *sm,
869 				struct vm_area_struct *vma,
870 				struct vm_fault *vmf);
871 
872 	int (*mremap)(const struct vm_special_mapping *sm,
873 		     struct vm_area_struct *new_vma);
874 };
875 
876 enum tlb_flush_reason {
877 	TLB_FLUSH_ON_TASK_SWITCH,
878 	TLB_REMOTE_SHOOTDOWN,
879 	TLB_LOCAL_SHOOTDOWN,
880 	TLB_LOCAL_MM_SHOOTDOWN,
881 	TLB_REMOTE_SEND_IPI,
882 	NR_TLB_FLUSH_REASONS,
883 };
884 
885  /*
886   * A swap entry has to fit into a "unsigned long", as the entry is hidden
887   * in the "index" field of the swapper address space.
888   */
889 typedef struct {
890 	unsigned long val;
891 } swp_entry_t;
892 
893 #endif /* _LINUX_MM_TYPES_H */
894