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