]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/linux/mm_types.h
mm: remove misleading alignment claims
[mirror_ubuntu-jammy-kernel.git] / include / linux / mm_types.h
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
18 #include <asm/mmu.h>
19
20 #ifndef AT_VECTOR_SIZE_ARCH
21 #define AT_VECTOR_SIZE_ARCH 0
22 #endif
23 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
24
25 struct address_space;
26 struct mem_cgroup;
27 struct hmm;
28
29 /*
30 * Each physical page in the system has a struct page associated with
31 * it to keep track of whatever it is we are using the page for at the
32 * moment. Note that we have no way to track which tasks are using
33 * a page, though if it is a pagecache page, rmap structures can tell us
34 * who is mapping it.
35 *
36 * SLUB uses cmpxchg_double() to atomically update its freelist and
37 * counters. That requires that freelist & counters be adjacent and
38 * double-word aligned. We align all struct pages to double-word
39 * boundaries, and ensure that 'freelist' is aligned within the
40 * struct.
41 */
42 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
43 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
44 #else
45 #define _struct_page_alignment
46 #endif
47
48 struct page {
49 /* First double word block */
50 unsigned long flags; /* Atomic flags, some possibly
51 * updated asynchronously */
52 union {
53 struct address_space *mapping; /* If low bit clear, points to
54 * inode address_space, or NULL.
55 * If page mapped as anonymous
56 * memory, low bit is set, and
57 * it points to anon_vma object
58 * or KSM private structure. See
59 * PAGE_MAPPING_ANON and
60 * PAGE_MAPPING_KSM.
61 */
62 void *s_mem; /* slab first object */
63 atomic_t compound_mapcount; /* first tail page */
64 /* page_deferred_list().next -- second tail page */
65 };
66
67 /* Second double word */
68 union {
69 pgoff_t index; /* Our offset within mapping. */
70 void *freelist; /* sl[aou]b first free object */
71 /* page_deferred_list().prev -- second tail page */
72 };
73
74 union {
75 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
76 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
77 /* Used for cmpxchg_double in slub */
78 unsigned long counters;
79 #else
80 /*
81 * Keep _refcount separate from slub cmpxchg_double data.
82 * As the rest of the double word is protected by slab_lock
83 * but _refcount is not.
84 */
85 unsigned counters;
86 #endif
87 unsigned int active; /* SLAB */
88 struct { /* SLUB */
89 unsigned inuse:16;
90 unsigned objects:15;
91 unsigned frozen:1;
92 };
93 int units; /* SLOB */
94
95 struct { /* Page cache */
96 /*
97 * Count of ptes mapped in mms, to show when
98 * page is mapped & limit reverse map searches.
99 *
100 * Extra information about page type may be
101 * stored here for pages that are never mapped,
102 * in which case the value MUST BE <= -2.
103 * See page-flags.h for more details.
104 */
105 atomic_t _mapcount;
106
107 /*
108 * Usage count, *USE WRAPPER FUNCTION* when manual
109 * accounting. See page_ref.h
110 */
111 atomic_t _refcount;
112 };
113 };
114
115 /*
116 * WARNING: bit 0 of the first word encode PageTail(). That means
117 * the rest users of the storage space MUST NOT use the bit to
118 * avoid collision and false-positive PageTail().
119 */
120 union {
121 struct list_head lru; /* Pageout list, eg. active_list
122 * protected by zone_lru_lock !
123 * Can be used as a generic list
124 * by the page owner.
125 */
126 struct dev_pagemap *pgmap; /* ZONE_DEVICE pages are never on an
127 * lru or handled by a slab
128 * allocator, this points to the
129 * hosting device page map.
130 */
131 struct { /* slub per cpu partial pages */
132 struct page *next; /* Next partial slab */
133 #ifdef CONFIG_64BIT
134 int pages; /* Nr of partial slabs left */
135 int pobjects; /* Approximate # of objects */
136 #else
137 short int pages;
138 short int pobjects;
139 #endif
140 };
141
142 struct rcu_head rcu_head; /* Used by SLAB
143 * when destroying via RCU
144 */
145 /* Tail pages of compound page */
146 struct {
147 unsigned long compound_head; /* If bit zero is set */
148
149 /* First tail page only */
150 #ifdef CONFIG_64BIT
151 /*
152 * On 64 bit system we have enough space in struct page
153 * to encode compound_dtor and compound_order with
154 * unsigned int. It can help compiler generate better or
155 * smaller code on some archtectures.
156 */
157 unsigned int compound_dtor;
158 unsigned int compound_order;
159 #else
160 unsigned short int compound_dtor;
161 unsigned short int compound_order;
162 #endif
163 };
164
165 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && USE_SPLIT_PMD_PTLOCKS
166 struct {
167 unsigned long __pad; /* do not overlay pmd_huge_pte
168 * with compound_head to avoid
169 * possible bit 0 collision.
170 */
171 pgtable_t pmd_huge_pte; /* protected by page->ptl */
172 };
173 #endif
174 };
175
176 union {
177 unsigned long private; /* Mapping-private opaque data:
178 * usually used for buffer_heads
179 * if PagePrivate set; used for
180 * swp_entry_t if PageSwapCache;
181 * indicates order in the buddy
182 * system if PG_buddy is set.
183 */
184 #if USE_SPLIT_PTE_PTLOCKS
185 #if ALLOC_SPLIT_PTLOCKS
186 spinlock_t *ptl;
187 #else
188 spinlock_t ptl;
189 #endif
190 #endif
191 struct kmem_cache *slab_cache; /* SL[AU]B: Pointer to slab */
192 };
193
194 #ifdef CONFIG_MEMCG
195 struct mem_cgroup *mem_cgroup;
196 #endif
197
198 /*
199 * On machines where all RAM is mapped into kernel address space,
200 * we can simply calculate the virtual address. On machines with
201 * highmem some memory is mapped into kernel virtual memory
202 * dynamically, so we need a place to store that address.
203 * Note that this field could be 16 bits on x86 ... ;)
204 *
205 * Architectures with slow multiplication can define
206 * WANT_PAGE_VIRTUAL in asm/page.h
207 */
208 #if defined(WANT_PAGE_VIRTUAL)
209 void *virtual; /* Kernel virtual address (NULL if
210 not kmapped, ie. highmem) */
211 #endif /* WANT_PAGE_VIRTUAL */
212
213 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
214 int _last_cpupid;
215 #endif
216 } _struct_page_alignment;
217
218 #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK)
219 #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE)
220
221 struct page_frag_cache {
222 void * va;
223 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
224 __u16 offset;
225 __u16 size;
226 #else
227 __u32 offset;
228 #endif
229 /* we maintain a pagecount bias, so that we dont dirty cache line
230 * containing page->_refcount every time we allocate a fragment.
231 */
232 unsigned int pagecnt_bias;
233 bool pfmemalloc;
234 };
235
236 typedef unsigned long vm_flags_t;
237
238 /*
239 * A region containing a mapping of a non-memory backed file under NOMMU
240 * conditions. These are held in a global tree and are pinned by the VMAs that
241 * map parts of them.
242 */
243 struct vm_region {
244 struct rb_node vm_rb; /* link in global region tree */
245 vm_flags_t vm_flags; /* VMA vm_flags */
246 unsigned long vm_start; /* start address of region */
247 unsigned long vm_end; /* region initialised to here */
248 unsigned long vm_top; /* region allocated to here */
249 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
250 struct file *vm_file; /* the backing file or NULL */
251
252 int vm_usage; /* region usage count (access under nommu_region_sem) */
253 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
254 * this region */
255 };
256
257 #ifdef CONFIG_USERFAULTFD
258 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
259 struct vm_userfaultfd_ctx {
260 struct userfaultfd_ctx *ctx;
261 };
262 #else /* CONFIG_USERFAULTFD */
263 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
264 struct vm_userfaultfd_ctx {};
265 #endif /* CONFIG_USERFAULTFD */
266
267 /*
268 * This struct defines a memory VMM memory area. There is one of these
269 * per VM-area/task. A VM area is any part of the process virtual memory
270 * space that has a special rule for the page-fault handlers (ie a shared
271 * library, the executable area etc).
272 */
273 struct vm_area_struct {
274 /* The first cache line has the info for VMA tree walking. */
275
276 unsigned long vm_start; /* Our start address within vm_mm. */
277 unsigned long vm_end; /* The first byte after our end address
278 within vm_mm. */
279
280 /* linked list of VM areas per task, sorted by address */
281 struct vm_area_struct *vm_next, *vm_prev;
282
283 struct rb_node vm_rb;
284
285 /*
286 * Largest free memory gap in bytes to the left of this VMA.
287 * Either between this VMA and vma->vm_prev, or between one of the
288 * VMAs below us in the VMA rbtree and its ->vm_prev. This helps
289 * get_unmapped_area find a free area of the right size.
290 */
291 unsigned long rb_subtree_gap;
292
293 /* Second cache line starts here. */
294
295 struct mm_struct *vm_mm; /* The address space we belong to. */
296 pgprot_t vm_page_prot; /* Access permissions of this VMA. */
297 unsigned long vm_flags; /* Flags, see mm.h. */
298
299 /*
300 * For areas with an address space and backing store,
301 * linkage into the address_space->i_mmap interval tree.
302 */
303 struct {
304 struct rb_node rb;
305 unsigned long rb_subtree_last;
306 } shared;
307
308 /*
309 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
310 * list, after a COW of one of the file pages. A MAP_SHARED vma
311 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
312 * or brk vma (with NULL file) can only be in an anon_vma list.
313 */
314 struct list_head anon_vma_chain; /* Serialized by mmap_sem &
315 * page_table_lock */
316 struct anon_vma *anon_vma; /* Serialized by page_table_lock */
317
318 /* Function pointers to deal with this struct. */
319 const struct vm_operations_struct *vm_ops;
320
321 /* Information about our backing store: */
322 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
323 units */
324 struct file * vm_file; /* File we map to (can be NULL). */
325 void * vm_private_data; /* was vm_pte (shared mem) */
326
327 atomic_long_t swap_readahead_info;
328 #ifndef CONFIG_MMU
329 struct vm_region *vm_region; /* NOMMU mapping region */
330 #endif
331 #ifdef CONFIG_NUMA
332 struct mempolicy *vm_policy; /* NUMA policy for the VMA */
333 #endif
334 struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
335 } __randomize_layout;
336
337 struct core_thread {
338 struct task_struct *task;
339 struct core_thread *next;
340 };
341
342 struct core_state {
343 atomic_t nr_threads;
344 struct core_thread dumper;
345 struct completion startup;
346 };
347
348 struct kioctx_table;
349 struct mm_struct {
350 struct vm_area_struct *mmap; /* list of VMAs */
351 struct rb_root mm_rb;
352 u32 vmacache_seqnum; /* per-thread vmacache */
353 #ifdef CONFIG_MMU
354 unsigned long (*get_unmapped_area) (struct file *filp,
355 unsigned long addr, unsigned long len,
356 unsigned long pgoff, unsigned long flags);
357 #endif
358 unsigned long mmap_base; /* base of mmap area */
359 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
360 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
361 /* Base adresses for compatible mmap() */
362 unsigned long mmap_compat_base;
363 unsigned long mmap_compat_legacy_base;
364 #endif
365 unsigned long task_size; /* size of task vm space */
366 unsigned long highest_vm_end; /* highest vma end address */
367 pgd_t * pgd;
368
369 /**
370 * @mm_users: The number of users including userspace.
371 *
372 * Use mmget()/mmget_not_zero()/mmput() to modify. When this drops
373 * to 0 (i.e. when the task exits and there are no other temporary
374 * reference holders), we also release a reference on @mm_count
375 * (which may then free the &struct mm_struct if @mm_count also
376 * drops to 0).
377 */
378 atomic_t mm_users;
379
380 /**
381 * @mm_count: The number of references to &struct mm_struct
382 * (@mm_users count as 1).
383 *
384 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the
385 * &struct mm_struct is freed.
386 */
387 atomic_t mm_count;
388
389 #ifdef CONFIG_MMU
390 atomic_long_t pgtables_bytes; /* PTE page table pages */
391 #endif
392 int map_count; /* number of VMAs */
393
394 spinlock_t page_table_lock; /* Protects page tables and some counters */
395 struct rw_semaphore mmap_sem;
396
397 struct list_head mmlist; /* List of maybe swapped mm's. These are globally strung
398 * together off init_mm.mmlist, and are protected
399 * by mmlist_lock
400 */
401
402
403 unsigned long hiwater_rss; /* High-watermark of RSS usage */
404 unsigned long hiwater_vm; /* High-water virtual memory usage */
405
406 unsigned long total_vm; /* Total pages mapped */
407 unsigned long locked_vm; /* Pages that have PG_mlocked set */
408 unsigned long pinned_vm; /* Refcount permanently increased */
409 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
410 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
411 unsigned long stack_vm; /* VM_STACK */
412 unsigned long def_flags;
413 unsigned long start_code, end_code, start_data, end_data;
414 unsigned long start_brk, brk, start_stack;
415 unsigned long arg_start, arg_end, env_start, env_end;
416
417 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
418
419 /*
420 * Special counters, in some configurations protected by the
421 * page_table_lock, in other configurations by being atomic.
422 */
423 struct mm_rss_stat rss_stat;
424
425 struct linux_binfmt *binfmt;
426
427 cpumask_var_t cpu_vm_mask_var;
428
429 /* Architecture-specific MM context */
430 mm_context_t context;
431
432 unsigned long flags; /* Must use atomic bitops to access the bits */
433
434 struct core_state *core_state; /* coredumping support */
435 #ifdef CONFIG_MEMBARRIER
436 atomic_t membarrier_state;
437 #endif
438 #ifdef CONFIG_AIO
439 spinlock_t ioctx_lock;
440 struct kioctx_table __rcu *ioctx_table;
441 #endif
442 #ifdef CONFIG_MEMCG
443 /*
444 * "owner" points to a task that is regarded as the canonical
445 * user/owner of this mm. All of the following must be true in
446 * order for it to be changed:
447 *
448 * current == mm->owner
449 * current->mm != mm
450 * new_owner->mm == mm
451 * new_owner->alloc_lock is held
452 */
453 struct task_struct __rcu *owner;
454 #endif
455 struct user_namespace *user_ns;
456
457 /* store ref to file /proc/<pid>/exe symlink points to */
458 struct file __rcu *exe_file;
459 #ifdef CONFIG_MMU_NOTIFIER
460 struct mmu_notifier_mm *mmu_notifier_mm;
461 #endif
462 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
463 pgtable_t pmd_huge_pte; /* protected by page_table_lock */
464 #endif
465 #ifdef CONFIG_CPUMASK_OFFSTACK
466 struct cpumask cpumask_allocation;
467 #endif
468 #ifdef CONFIG_NUMA_BALANCING
469 /*
470 * numa_next_scan is the next time that the PTEs will be marked
471 * pte_numa. NUMA hinting faults will gather statistics and migrate
472 * pages to new nodes if necessary.
473 */
474 unsigned long numa_next_scan;
475
476 /* Restart point for scanning and setting pte_numa */
477 unsigned long numa_scan_offset;
478
479 /* numa_scan_seq prevents two threads setting pte_numa */
480 int numa_scan_seq;
481 #endif
482 /*
483 * An operation with batched TLB flushing is going on. Anything that
484 * can move process memory needs to flush the TLB when moving a
485 * PROT_NONE or PROT_NUMA mapped page.
486 */
487 atomic_t tlb_flush_pending;
488 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
489 /* See flush_tlb_batched_pending() */
490 bool tlb_flush_batched;
491 #endif
492 struct uprobes_state uprobes_state;
493 #ifdef CONFIG_HUGETLB_PAGE
494 atomic_long_t hugetlb_usage;
495 #endif
496 struct work_struct async_put_work;
497
498 #if IS_ENABLED(CONFIG_HMM)
499 /* HMM needs to track a few things per mm */
500 struct hmm *hmm;
501 #endif
502 } __randomize_layout;
503
504 extern struct mm_struct init_mm;
505
506 static inline void mm_init_cpumask(struct mm_struct *mm)
507 {
508 #ifdef CONFIG_CPUMASK_OFFSTACK
509 mm->cpu_vm_mask_var = &mm->cpumask_allocation;
510 #endif
511 cpumask_clear(mm->cpu_vm_mask_var);
512 }
513
514 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
515 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
516 {
517 return mm->cpu_vm_mask_var;
518 }
519
520 struct mmu_gather;
521 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
522 unsigned long start, unsigned long end);
523 extern void tlb_finish_mmu(struct mmu_gather *tlb,
524 unsigned long start, unsigned long end);
525
526 static inline void init_tlb_flush_pending(struct mm_struct *mm)
527 {
528 atomic_set(&mm->tlb_flush_pending, 0);
529 }
530
531 static inline void inc_tlb_flush_pending(struct mm_struct *mm)
532 {
533 atomic_inc(&mm->tlb_flush_pending);
534 /*
535 * The only time this value is relevant is when there are indeed pages
536 * to flush. And we'll only flush pages after changing them, which
537 * requires the PTL.
538 *
539 * So the ordering here is:
540 *
541 * atomic_inc(&mm->tlb_flush_pending);
542 * spin_lock(&ptl);
543 * ...
544 * set_pte_at();
545 * spin_unlock(&ptl);
546 *
547 * spin_lock(&ptl)
548 * mm_tlb_flush_pending();
549 * ....
550 * spin_unlock(&ptl);
551 *
552 * flush_tlb_range();
553 * atomic_dec(&mm->tlb_flush_pending);
554 *
555 * Where the increment if constrained by the PTL unlock, it thus
556 * ensures that the increment is visible if the PTE modification is
557 * visible. After all, if there is no PTE modification, nobody cares
558 * about TLB flushes either.
559 *
560 * This very much relies on users (mm_tlb_flush_pending() and
561 * mm_tlb_flush_nested()) only caring about _specific_ PTEs (and
562 * therefore specific PTLs), because with SPLIT_PTE_PTLOCKS and RCpc
563 * locks (PPC) the unlock of one doesn't order against the lock of
564 * another PTL.
565 *
566 * The decrement is ordered by the flush_tlb_range(), such that
567 * mm_tlb_flush_pending() will not return false unless all flushes have
568 * completed.
569 */
570 }
571
572 static inline void dec_tlb_flush_pending(struct mm_struct *mm)
573 {
574 /*
575 * See inc_tlb_flush_pending().
576 *
577 * This cannot be smp_mb__before_atomic() because smp_mb() simply does
578 * not order against TLB invalidate completion, which is what we need.
579 *
580 * Therefore we must rely on tlb_flush_*() to guarantee order.
581 */
582 atomic_dec(&mm->tlb_flush_pending);
583 }
584
585 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
586 {
587 /*
588 * Must be called after having acquired the PTL; orders against that
589 * PTLs release and therefore ensures that if we observe the modified
590 * PTE we must also observe the increment from inc_tlb_flush_pending().
591 *
592 * That is, it only guarantees to return true if there is a flush
593 * pending for _this_ PTL.
594 */
595 return atomic_read(&mm->tlb_flush_pending);
596 }
597
598 static inline bool mm_tlb_flush_nested(struct mm_struct *mm)
599 {
600 /*
601 * Similar to mm_tlb_flush_pending(), we must have acquired the PTL
602 * for which there is a TLB flush pending in order to guarantee
603 * we've seen both that PTE modification and the increment.
604 *
605 * (no requirement on actually still holding the PTL, that is irrelevant)
606 */
607 return atomic_read(&mm->tlb_flush_pending) > 1;
608 }
609
610 struct vm_fault;
611
612 struct vm_special_mapping {
613 const char *name; /* The name, e.g. "[vdso]". */
614
615 /*
616 * If .fault is not provided, this points to a
617 * NULL-terminated array of pages that back the special mapping.
618 *
619 * This must not be NULL unless .fault is provided.
620 */
621 struct page **pages;
622
623 /*
624 * If non-NULL, then this is called to resolve page faults
625 * on the special mapping. If used, .pages is not checked.
626 */
627 int (*fault)(const struct vm_special_mapping *sm,
628 struct vm_area_struct *vma,
629 struct vm_fault *vmf);
630
631 int (*mremap)(const struct vm_special_mapping *sm,
632 struct vm_area_struct *new_vma);
633 };
634
635 enum tlb_flush_reason {
636 TLB_FLUSH_ON_TASK_SWITCH,
637 TLB_REMOTE_SHOOTDOWN,
638 TLB_LOCAL_SHOOTDOWN,
639 TLB_LOCAL_MM_SHOOTDOWN,
640 TLB_REMOTE_SEND_IPI,
641 NR_TLB_FLUSH_REASONS,
642 };
643
644 /*
645 * A swap entry has to fit into a "unsigned long", as the entry is hidden
646 * in the "index" field of the swapper address space.
647 */
648 typedef struct {
649 unsigned long val;
650 } swp_entry_t;
651
652 #endif /* _LINUX_MM_TYPES_H */