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