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