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