xref: /openbmc/linux/include/linux/mm_types.h (revision f0702555)
1 #ifndef _LINUX_MM_TYPES_H
2 #define _LINUX_MM_TYPES_H
3 
4 #include <linux/auxvec.h>
5 #include <linux/types.h>
6 #include <linux/threads.h>
7 #include <linux/list.h>
8 #include <linux/spinlock.h>
9 #include <linux/rbtree.h>
10 #include <linux/rwsem.h>
11 #include <linux/completion.h>
12 #include <linux/cpumask.h>
13 #include <linux/uprobes.h>
14 #include <linux/page-flags-layout.h>
15 #include <linux/workqueue.h>
16 #include <asm/page.h>
17 #include <asm/mmu.h>
18 
19 #ifndef AT_VECTOR_SIZE_ARCH
20 #define AT_VECTOR_SIZE_ARCH 0
21 #endif
22 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
23 
24 struct address_space;
25 struct mem_cgroup;
26 
27 #define USE_SPLIT_PTE_PTLOCKS	(NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS)
28 #define USE_SPLIT_PMD_PTLOCKS	(USE_SPLIT_PTE_PTLOCKS && \
29 		IS_ENABLED(CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK))
30 #define ALLOC_SPLIT_PTLOCKS	(SPINLOCK_SIZE > BITS_PER_LONG/8)
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  * The objects in struct page are organized in double word blocks in
40  * order to allows us to use atomic double word operations on portions
41  * of struct page. That is currently only used by slub but the arrangement
42  * allows the use of atomic double word operations on the flags/mapping
43  * and lru list pointers also.
44  */
45 struct page {
46 	/* First double word block */
47 	unsigned long flags;		/* Atomic flags, some possibly
48 					 * updated asynchronously */
49 	union {
50 		struct address_space *mapping;	/* If low bit clear, points to
51 						 * inode address_space, or NULL.
52 						 * If page mapped as anonymous
53 						 * memory, low bit is set, and
54 						 * it points to anon_vma object:
55 						 * see PAGE_MAPPING_ANON below.
56 						 */
57 		void *s_mem;			/* slab first object */
58 		atomic_t compound_mapcount;	/* first tail page */
59 		/* page_deferred_list().next	 -- second tail page */
60 	};
61 
62 	/* Second double word */
63 	struct {
64 		union {
65 			pgoff_t index;		/* Our offset within mapping. */
66 			void *freelist;		/* sl[aou]b first free object */
67 			/* page_deferred_list().prev	-- second tail page */
68 		};
69 
70 		union {
71 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
72 	defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
73 			/* Used for cmpxchg_double in slub */
74 			unsigned long counters;
75 #else
76 			/*
77 			 * Keep _refcount separate from slub cmpxchg_double
78 			 * data.  As the rest of the double word is protected by
79 			 * slab_lock but _refcount is not.
80 			 */
81 			unsigned counters;
82 #endif
83 
84 			struct {
85 
86 				union {
87 					/*
88 					 * Count of ptes mapped in mms, to show
89 					 * when page is mapped & limit reverse
90 					 * map searches.
91 					 */
92 					atomic_t _mapcount;
93 
94 					struct { /* SLUB */
95 						unsigned inuse:16;
96 						unsigned objects:15;
97 						unsigned frozen:1;
98 					};
99 					int units;	/* SLOB */
100 				};
101 				/*
102 				 * Usage count, *USE WRAPPER FUNCTION*
103 				 * when manual accounting. See page_ref.h
104 				 */
105 				atomic_t _refcount;
106 			};
107 			unsigned int active;	/* SLAB */
108 		};
109 	};
110 
111 	/*
112 	 * Third double word block
113 	 *
114 	 * WARNING: bit 0 of the first word encode PageTail(). That means
115 	 * the rest users of the storage space MUST NOT use the bit to
116 	 * avoid collision and false-positive PageTail().
117 	 */
118 	union {
119 		struct list_head lru;	/* Pageout list, eg. active_list
120 					 * protected by zone->lru_lock !
121 					 * Can be used as a generic list
122 					 * by the page owner.
123 					 */
124 		struct dev_pagemap *pgmap; /* ZONE_DEVICE pages are never on an
125 					    * lru or handled by a slab
126 					    * allocator, this points to the
127 					    * hosting device page map.
128 					    */
129 		struct {		/* slub per cpu partial pages */
130 			struct page *next;	/* Next partial slab */
131 #ifdef CONFIG_64BIT
132 			int pages;	/* Nr of partial slabs left */
133 			int pobjects;	/* Approximate # of objects */
134 #else
135 			short int pages;
136 			short int pobjects;
137 #endif
138 		};
139 
140 		struct rcu_head rcu_head;	/* Used by SLAB
141 						 * when destroying via RCU
142 						 */
143 		/* Tail pages of compound page */
144 		struct {
145 			unsigned long compound_head; /* If bit zero is set */
146 
147 			/* First tail page only */
148 #ifdef CONFIG_64BIT
149 			/*
150 			 * On 64 bit system we have enough space in struct page
151 			 * to encode compound_dtor and compound_order with
152 			 * unsigned int. It can help compiler generate better or
153 			 * smaller code on some archtectures.
154 			 */
155 			unsigned int compound_dtor;
156 			unsigned int compound_order;
157 #else
158 			unsigned short int compound_dtor;
159 			unsigned short int compound_order;
160 #endif
161 		};
162 
163 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && USE_SPLIT_PMD_PTLOCKS
164 		struct {
165 			unsigned long __pad;	/* do not overlay pmd_huge_pte
166 						 * with compound_head to avoid
167 						 * possible bit 0 collision.
168 						 */
169 			pgtable_t pmd_huge_pte; /* protected by page->ptl */
170 		};
171 #endif
172 	};
173 
174 	/* Remainder is not double word aligned */
175 	union {
176 		unsigned long private;		/* Mapping-private opaque data:
177 					 	 * usually used for buffer_heads
178 						 * if PagePrivate set; used for
179 						 * swp_entry_t if PageSwapCache;
180 						 * indicates order in the buddy
181 						 * system if PG_buddy is set.
182 						 */
183 #if USE_SPLIT_PTE_PTLOCKS
184 #if ALLOC_SPLIT_PTLOCKS
185 		spinlock_t *ptl;
186 #else
187 		spinlock_t ptl;
188 #endif
189 #endif
190 		struct kmem_cache *slab_cache;	/* SL[AU]B: Pointer to slab */
191 	};
192 
193 #ifdef CONFIG_MEMCG
194 	struct mem_cgroup *mem_cgroup;
195 #endif
196 
197 	/*
198 	 * On machines where all RAM is mapped into kernel address space,
199 	 * we can simply calculate the virtual address. On machines with
200 	 * highmem some memory is mapped into kernel virtual memory
201 	 * dynamically, so we need a place to store that address.
202 	 * Note that this field could be 16 bits on x86 ... ;)
203 	 *
204 	 * Architectures with slow multiplication can define
205 	 * WANT_PAGE_VIRTUAL in asm/page.h
206 	 */
207 #if defined(WANT_PAGE_VIRTUAL)
208 	void *virtual;			/* Kernel virtual address (NULL if
209 					   not kmapped, ie. highmem) */
210 #endif /* WANT_PAGE_VIRTUAL */
211 
212 #ifdef CONFIG_KMEMCHECK
213 	/*
214 	 * kmemcheck wants to track the status of each byte in a page; this
215 	 * is a pointer to such a status block. NULL if not tracked.
216 	 */
217 	void *shadow;
218 #endif
219 
220 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
221 	int _last_cpupid;
222 #endif
223 }
224 /*
225  * The struct page can be forced to be double word aligned so that atomic ops
226  * on double words work. The SLUB allocator can make use of such a feature.
227  */
228 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
229 	__aligned(2 * sizeof(unsigned long))
230 #endif
231 ;
232 
233 struct page_frag {
234 	struct page *page;
235 #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
236 	__u32 offset;
237 	__u32 size;
238 #else
239 	__u16 offset;
240 	__u16 size;
241 #endif
242 };
243 
244 #define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK)
245 #define PAGE_FRAG_CACHE_MAX_ORDER	get_order(PAGE_FRAG_CACHE_MAX_SIZE)
246 
247 struct page_frag_cache {
248 	void * va;
249 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
250 	__u16 offset;
251 	__u16 size;
252 #else
253 	__u32 offset;
254 #endif
255 	/* we maintain a pagecount bias, so that we dont dirty cache line
256 	 * containing page->_refcount every time we allocate a fragment.
257 	 */
258 	unsigned int		pagecnt_bias;
259 	bool pfmemalloc;
260 };
261 
262 typedef unsigned long vm_flags_t;
263 
264 /*
265  * A region containing a mapping of a non-memory backed file under NOMMU
266  * conditions.  These are held in a global tree and are pinned by the VMAs that
267  * map parts of them.
268  */
269 struct vm_region {
270 	struct rb_node	vm_rb;		/* link in global region tree */
271 	vm_flags_t	vm_flags;	/* VMA vm_flags */
272 	unsigned long	vm_start;	/* start address of region */
273 	unsigned long	vm_end;		/* region initialised to here */
274 	unsigned long	vm_top;		/* region allocated to here */
275 	unsigned long	vm_pgoff;	/* the offset in vm_file corresponding to vm_start */
276 	struct file	*vm_file;	/* the backing file or NULL */
277 
278 	int		vm_usage;	/* region usage count (access under nommu_region_sem) */
279 	bool		vm_icache_flushed : 1; /* true if the icache has been flushed for
280 						* this region */
281 };
282 
283 #ifdef CONFIG_USERFAULTFD
284 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
285 struct vm_userfaultfd_ctx {
286 	struct userfaultfd_ctx *ctx;
287 };
288 #else /* CONFIG_USERFAULTFD */
289 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
290 struct vm_userfaultfd_ctx {};
291 #endif /* CONFIG_USERFAULTFD */
292 
293 /*
294  * This struct defines a memory VMM memory area. There is one of these
295  * per VM-area/task.  A VM area is any part of the process virtual memory
296  * space that has a special rule for the page-fault handlers (ie a shared
297  * library, the executable area etc).
298  */
299 struct vm_area_struct {
300 	/* The first cache line has the info for VMA tree walking. */
301 
302 	unsigned long vm_start;		/* Our start address within vm_mm. */
303 	unsigned long vm_end;		/* The first byte after our end address
304 					   within vm_mm. */
305 
306 	/* linked list of VM areas per task, sorted by address */
307 	struct vm_area_struct *vm_next, *vm_prev;
308 
309 	struct rb_node vm_rb;
310 
311 	/*
312 	 * Largest free memory gap in bytes to the left of this VMA.
313 	 * Either between this VMA and vma->vm_prev, or between one of the
314 	 * VMAs below us in the VMA rbtree and its ->vm_prev. This helps
315 	 * get_unmapped_area find a free area of the right size.
316 	 */
317 	unsigned long rb_subtree_gap;
318 
319 	/* Second cache line starts here. */
320 
321 	struct mm_struct *vm_mm;	/* The address space we belong to. */
322 	pgprot_t vm_page_prot;		/* Access permissions of this VMA. */
323 	unsigned long vm_flags;		/* Flags, see mm.h. */
324 
325 	/*
326 	 * For areas with an address space and backing store,
327 	 * linkage into the address_space->i_mmap interval tree.
328 	 */
329 	struct {
330 		struct rb_node rb;
331 		unsigned long rb_subtree_last;
332 	} shared;
333 
334 	/*
335 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
336 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
337 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
338 	 * or brk vma (with NULL file) can only be in an anon_vma list.
339 	 */
340 	struct list_head anon_vma_chain; /* Serialized by mmap_sem &
341 					  * page_table_lock */
342 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
343 
344 	/* Function pointers to deal with this struct. */
345 	const struct vm_operations_struct *vm_ops;
346 
347 	/* Information about our backing store: */
348 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
349 					   units */
350 	struct file * vm_file;		/* File we map to (can be NULL). */
351 	void * vm_private_data;		/* was vm_pte (shared mem) */
352 
353 #ifndef CONFIG_MMU
354 	struct vm_region *vm_region;	/* NOMMU mapping region */
355 #endif
356 #ifdef CONFIG_NUMA
357 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
358 #endif
359 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
360 };
361 
362 struct core_thread {
363 	struct task_struct *task;
364 	struct core_thread *next;
365 };
366 
367 struct core_state {
368 	atomic_t nr_threads;
369 	struct core_thread dumper;
370 	struct completion startup;
371 };
372 
373 enum {
374 	MM_FILEPAGES,	/* Resident file mapping pages */
375 	MM_ANONPAGES,	/* Resident anonymous pages */
376 	MM_SWAPENTS,	/* Anonymous swap entries */
377 	MM_SHMEMPAGES,	/* Resident shared memory pages */
378 	NR_MM_COUNTERS
379 };
380 
381 #if USE_SPLIT_PTE_PTLOCKS && defined(CONFIG_MMU)
382 #define SPLIT_RSS_COUNTING
383 /* per-thread cached information, */
384 struct task_rss_stat {
385 	int events;	/* for synchronization threshold */
386 	int count[NR_MM_COUNTERS];
387 };
388 #endif /* USE_SPLIT_PTE_PTLOCKS */
389 
390 struct mm_rss_stat {
391 	atomic_long_t count[NR_MM_COUNTERS];
392 };
393 
394 struct kioctx_table;
395 struct mm_struct {
396 	struct vm_area_struct *mmap;		/* list of VMAs */
397 	struct rb_root mm_rb;
398 	u32 vmacache_seqnum;                   /* per-thread vmacache */
399 #ifdef CONFIG_MMU
400 	unsigned long (*get_unmapped_area) (struct file *filp,
401 				unsigned long addr, unsigned long len,
402 				unsigned long pgoff, unsigned long flags);
403 #endif
404 	unsigned long mmap_base;		/* base of mmap area */
405 	unsigned long mmap_legacy_base;         /* base of mmap area in bottom-up allocations */
406 	unsigned long task_size;		/* size of task vm space */
407 	unsigned long highest_vm_end;		/* highest vma end address */
408 	pgd_t * pgd;
409 	atomic_t mm_users;			/* How many users with user space? */
410 	atomic_t mm_count;			/* How many references to "struct mm_struct" (users count as 1) */
411 	atomic_long_t nr_ptes;			/* PTE page table pages */
412 #if CONFIG_PGTABLE_LEVELS > 2
413 	atomic_long_t nr_pmds;			/* PMD page table pages */
414 #endif
415 	int map_count;				/* number of VMAs */
416 
417 	spinlock_t page_table_lock;		/* Protects page tables and some counters */
418 	struct rw_semaphore mmap_sem;
419 
420 	struct list_head mmlist;		/* List of maybe swapped mm's.	These are globally strung
421 						 * together off init_mm.mmlist, and are protected
422 						 * by mmlist_lock
423 						 */
424 
425 
426 	unsigned long hiwater_rss;	/* High-watermark of RSS usage */
427 	unsigned long hiwater_vm;	/* High-water virtual memory usage */
428 
429 	unsigned long total_vm;		/* Total pages mapped */
430 	unsigned long locked_vm;	/* Pages that have PG_mlocked set */
431 	unsigned long pinned_vm;	/* Refcount permanently increased */
432 	unsigned long data_vm;		/* VM_WRITE & ~VM_SHARED & ~VM_STACK */
433 	unsigned long exec_vm;		/* VM_EXEC & ~VM_WRITE & ~VM_STACK */
434 	unsigned long stack_vm;		/* VM_STACK */
435 	unsigned long def_flags;
436 	unsigned long start_code, end_code, start_data, end_data;
437 	unsigned long start_brk, brk, start_stack;
438 	unsigned long arg_start, arg_end, env_start, env_end;
439 
440 	unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
441 
442 	/*
443 	 * Special counters, in some configurations protected by the
444 	 * page_table_lock, in other configurations by being atomic.
445 	 */
446 	struct mm_rss_stat rss_stat;
447 
448 	struct linux_binfmt *binfmt;
449 
450 	cpumask_var_t cpu_vm_mask_var;
451 
452 	/* Architecture-specific MM context */
453 	mm_context_t context;
454 
455 	unsigned long flags; /* Must use atomic bitops to access the bits */
456 
457 	struct core_state *core_state; /* coredumping support */
458 #ifdef CONFIG_AIO
459 	spinlock_t			ioctx_lock;
460 	struct kioctx_table __rcu	*ioctx_table;
461 #endif
462 #ifdef CONFIG_MEMCG
463 	/*
464 	 * "owner" points to a task that is regarded as the canonical
465 	 * user/owner of this mm. All of the following must be true in
466 	 * order for it to be changed:
467 	 *
468 	 * current == mm->owner
469 	 * current->mm != mm
470 	 * new_owner->mm == mm
471 	 * new_owner->alloc_lock is held
472 	 */
473 	struct task_struct __rcu *owner;
474 #endif
475 
476 	/* store ref to file /proc/<pid>/exe symlink points to */
477 	struct file __rcu *exe_file;
478 #ifdef CONFIG_MMU_NOTIFIER
479 	struct mmu_notifier_mm *mmu_notifier_mm;
480 #endif
481 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
482 	pgtable_t pmd_huge_pte; /* protected by page_table_lock */
483 #endif
484 #ifdef CONFIG_CPUMASK_OFFSTACK
485 	struct cpumask cpumask_allocation;
486 #endif
487 #ifdef CONFIG_NUMA_BALANCING
488 	/*
489 	 * numa_next_scan is the next time that the PTEs will be marked
490 	 * pte_numa. NUMA hinting faults will gather statistics and migrate
491 	 * pages to new nodes if necessary.
492 	 */
493 	unsigned long numa_next_scan;
494 
495 	/* Restart point for scanning and setting pte_numa */
496 	unsigned long numa_scan_offset;
497 
498 	/* numa_scan_seq prevents two threads setting pte_numa */
499 	int numa_scan_seq;
500 #endif
501 #if defined(CONFIG_NUMA_BALANCING) || defined(CONFIG_COMPACTION)
502 	/*
503 	 * An operation with batched TLB flushing is going on. Anything that
504 	 * can move process memory needs to flush the TLB when moving a
505 	 * PROT_NONE or PROT_NUMA mapped page.
506 	 */
507 	bool tlb_flush_pending;
508 #endif
509 	struct uprobes_state uprobes_state;
510 #ifdef CONFIG_X86_INTEL_MPX
511 	/* address of the bounds directory */
512 	void __user *bd_addr;
513 #endif
514 #ifdef CONFIG_HUGETLB_PAGE
515 	atomic_long_t hugetlb_usage;
516 #endif
517 #ifdef CONFIG_MMU
518 	struct work_struct async_put_work;
519 #endif
520 };
521 
522 static inline void mm_init_cpumask(struct mm_struct *mm)
523 {
524 #ifdef CONFIG_CPUMASK_OFFSTACK
525 	mm->cpu_vm_mask_var = &mm->cpumask_allocation;
526 #endif
527 	cpumask_clear(mm->cpu_vm_mask_var);
528 }
529 
530 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
531 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
532 {
533 	return mm->cpu_vm_mask_var;
534 }
535 
536 #if defined(CONFIG_NUMA_BALANCING) || defined(CONFIG_COMPACTION)
537 /*
538  * Memory barriers to keep this state in sync are graciously provided by
539  * the page table locks, outside of which no page table modifications happen.
540  * The barriers below prevent the compiler from re-ordering the instructions
541  * around the memory barriers that are already present in the code.
542  */
543 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
544 {
545 	barrier();
546 	return mm->tlb_flush_pending;
547 }
548 static inline void set_tlb_flush_pending(struct mm_struct *mm)
549 {
550 	mm->tlb_flush_pending = true;
551 
552 	/*
553 	 * Guarantee that the tlb_flush_pending store does not leak into the
554 	 * critical section updating the page tables
555 	 */
556 	smp_mb__before_spinlock();
557 }
558 /* Clearing is done after a TLB flush, which also provides a barrier. */
559 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
560 {
561 	barrier();
562 	mm->tlb_flush_pending = false;
563 }
564 #else
565 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
566 {
567 	return false;
568 }
569 static inline void set_tlb_flush_pending(struct mm_struct *mm)
570 {
571 }
572 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
573 {
574 }
575 #endif
576 
577 struct vm_fault;
578 
579 struct vm_special_mapping {
580 	const char *name;	/* The name, e.g. "[vdso]". */
581 
582 	/*
583 	 * If .fault is not provided, this points to a
584 	 * NULL-terminated array of pages that back the special mapping.
585 	 *
586 	 * This must not be NULL unless .fault is provided.
587 	 */
588 	struct page **pages;
589 
590 	/*
591 	 * If non-NULL, then this is called to resolve page faults
592 	 * on the special mapping.  If used, .pages is not checked.
593 	 */
594 	int (*fault)(const struct vm_special_mapping *sm,
595 		     struct vm_area_struct *vma,
596 		     struct vm_fault *vmf);
597 };
598 
599 enum tlb_flush_reason {
600 	TLB_FLUSH_ON_TASK_SWITCH,
601 	TLB_REMOTE_SHOOTDOWN,
602 	TLB_LOCAL_SHOOTDOWN,
603 	TLB_LOCAL_MM_SHOOTDOWN,
604 	TLB_REMOTE_SEND_IPI,
605 	NR_TLB_FLUSH_REASONS,
606 };
607 
608  /*
609   * A swap entry has to fit into a "unsigned long", as the entry is hidden
610   * in the "index" field of the swapper address space.
611   */
612 typedef struct {
613 	unsigned long val;
614 } swp_entry_t;
615 
616 #endif /* _LINUX_MM_TYPES_H */
617