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