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