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