1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MM_TYPES_H
3 #define _LINUX_MM_TYPES_H
5 #include <linux/mm_types_task.h>
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>
20 #ifndef AT_VECTOR_SIZE_ARCH
21 #define AT_VECTOR_SIZE_ARCH 0
23 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
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
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
42 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
43 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
45 #define _struct_page_alignment
49 /* First double word block */
50 unsigned long flags
; /* Atomic flags, some possibly
51 * updated asynchronously */
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
62 void *s_mem
; /* slab first object */
63 atomic_t compound_mapcount
; /* first tail page */
64 /* page_deferred_list().next -- second tail page */
67 /* Second double word */
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 */
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
;
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.
87 unsigned int active
; /* SLAB */
95 struct { /* Page cache */
97 * Count of ptes mapped in mms, to show when
98 * page is mapped & limit reverse map searches.
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.
108 * Usage count, *USE WRAPPER FUNCTION* when manual
109 * accounting. See page_ref.h
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().
121 struct list_head lru
; /* Pageout list, eg. active_list
122 * protected by zone_lru_lock !
123 * Can be used as a generic list
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.
131 struct { /* slub per cpu partial pages */
132 struct page
*next
; /* Next partial slab */
134 int pages
; /* Nr of partial slabs left */
135 int pobjects
; /* Approximate # of objects */
142 struct rcu_head rcu_head
; /* Used by SLAB
143 * when destroying via RCU
145 /* Tail pages of compound page */
147 unsigned long compound_head
; /* If bit zero is set */
149 /* First tail page only */
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.
157 unsigned int compound_dtor
;
158 unsigned int compound_order
;
160 unsigned short int compound_dtor
;
161 unsigned short int compound_order
;
165 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && USE_SPLIT_PMD_PTLOCKS
167 unsigned long __pad
; /* do not overlay pmd_huge_pte
168 * with compound_head to avoid
169 * possible bit 0 collision.
171 pgtable_t pmd_huge_pte
; /* protected by page->ptl */
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.
184 #if USE_SPLIT_PTE_PTLOCKS
185 #if ALLOC_SPLIT_PTLOCKS
191 struct kmem_cache
*slab_cache
; /* SL[AU]B: Pointer to slab */
195 struct mem_cgroup
*mem_cgroup
;
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 ... ;)
205 * Architectures with slow multiplication can define
206 * WANT_PAGE_VIRTUAL in asm/page.h
208 #if defined(WANT_PAGE_VIRTUAL)
209 void *virtual; /* Kernel virtual address (NULL if
210 not kmapped, ie. highmem) */
211 #endif /* WANT_PAGE_VIRTUAL */
213 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
216 } _struct_page_alignment
;
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)
221 struct page_frag_cache
{
223 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
229 /* we maintain a pagecount bias, so that we dont dirty cache line
230 * containing page->_refcount every time we allocate a fragment.
232 unsigned int pagecnt_bias
;
236 typedef unsigned long vm_flags_t
;
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
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 */
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
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
;
262 #else /* CONFIG_USERFAULTFD */
263 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
264 struct vm_userfaultfd_ctx
{};
265 #endif /* CONFIG_USERFAULTFD */
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).
273 struct vm_area_struct
{
274 /* The first cache line has the info for VMA tree walking. */
276 unsigned long vm_start
; /* Our start address within vm_mm. */
277 unsigned long vm_end
; /* The first byte after our end address
280 /* linked list of VM areas per task, sorted by address */
281 struct vm_area_struct
*vm_next
, *vm_prev
;
283 struct rb_node vm_rb
;
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.
291 unsigned long rb_subtree_gap
;
293 /* Second cache line starts here. */
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. */
300 * For areas with an address space and backing store,
301 * linkage into the address_space->i_mmap interval tree.
305 unsigned long rb_subtree_last
;
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.
314 struct list_head anon_vma_chain
; /* Serialized by mmap_sem &
316 struct anon_vma
*anon_vma
; /* Serialized by page_table_lock */
318 /* Function pointers to deal with this struct. */
319 const struct vm_operations_struct
*vm_ops
;
321 /* Information about our backing store: */
322 unsigned long vm_pgoff
; /* Offset (within vm_file) in PAGE_SIZE
324 struct file
* vm_file
; /* File we map to (can be NULL). */
325 void * vm_private_data
; /* was vm_pte (shared mem) */
327 atomic_long_t swap_readahead_info
;
329 struct vm_region
*vm_region
; /* NOMMU mapping region */
332 struct mempolicy
*vm_policy
; /* NUMA policy for the VMA */
334 struct vm_userfaultfd_ctx vm_userfaultfd_ctx
;
335 } __randomize_layout
;
338 struct task_struct
*task
;
339 struct core_thread
*next
;
344 struct core_thread dumper
;
345 struct completion startup
;
350 struct vm_area_struct
*mmap
; /* list of VMAs */
351 struct rb_root mm_rb
;
352 u32 vmacache_seqnum
; /* per-thread vmacache */
354 unsigned long (*get_unmapped_area
) (struct file
*filp
,
355 unsigned long addr
, unsigned long len
,
356 unsigned long pgoff
, unsigned long flags
);
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
;
365 unsigned long task_size
; /* size of task vm space */
366 unsigned long highest_vm_end
; /* highest vma end address */
370 * @mm_users: The number of users including userspace.
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
381 * @mm_count: The number of references to &struct mm_struct
382 * (@mm_users count as 1).
384 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the
385 * &struct mm_struct is freed.
390 atomic_long_t pgtables_bytes
; /* PTE page table pages */
392 int map_count
; /* number of VMAs */
394 spinlock_t page_table_lock
; /* Protects page tables and some counters */
395 struct rw_semaphore mmap_sem
;
397 struct list_head mmlist
; /* List of maybe swapped mm's. These are globally strung
398 * together off init_mm.mmlist, and are protected
403 unsigned long hiwater_rss
; /* High-watermark of RSS usage */
404 unsigned long hiwater_vm
; /* High-water virtual memory usage */
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
;
417 unsigned long saved_auxv
[AT_VECTOR_SIZE
]; /* for /proc/PID/auxv */
420 * Special counters, in some configurations protected by the
421 * page_table_lock, in other configurations by being atomic.
423 struct mm_rss_stat rss_stat
;
425 struct linux_binfmt
*binfmt
;
427 cpumask_var_t cpu_vm_mask_var
;
429 /* Architecture-specific MM context */
430 mm_context_t context
;
432 unsigned long flags
; /* Must use atomic bitops to access the bits */
434 struct core_state
*core_state
; /* coredumping support */
435 #ifdef CONFIG_MEMBARRIER
436 atomic_t membarrier_state
;
439 spinlock_t ioctx_lock
;
440 struct kioctx_table __rcu
*ioctx_table
;
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:
448 * current == mm->owner
450 * new_owner->mm == mm
451 * new_owner->alloc_lock is held
453 struct task_struct __rcu
*owner
;
455 struct user_namespace
*user_ns
;
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
;
462 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
463 pgtable_t pmd_huge_pte
; /* protected by page_table_lock */
465 #ifdef CONFIG_CPUMASK_OFFSTACK
466 struct cpumask cpumask_allocation
;
468 #ifdef CONFIG_NUMA_BALANCING
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.
474 unsigned long numa_next_scan
;
476 /* Restart point for scanning and setting pte_numa */
477 unsigned long numa_scan_offset
;
479 /* numa_scan_seq prevents two threads setting pte_numa */
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.
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
;
492 struct uprobes_state uprobes_state
;
493 #ifdef CONFIG_HUGETLB_PAGE
494 atomic_long_t hugetlb_usage
;
496 struct work_struct async_put_work
;
498 #if IS_ENABLED(CONFIG_HMM)
499 /* HMM needs to track a few things per mm */
502 } __randomize_layout
;
504 extern struct mm_struct init_mm
;
506 static inline void mm_init_cpumask(struct mm_struct
*mm
)
508 #ifdef CONFIG_CPUMASK_OFFSTACK
509 mm
->cpu_vm_mask_var
= &mm
->cpumask_allocation
;
511 cpumask_clear(mm
->cpu_vm_mask_var
);
514 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
515 static inline cpumask_t
*mm_cpumask(struct mm_struct
*mm
)
517 return mm
->cpu_vm_mask_var
;
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
);
526 static inline void init_tlb_flush_pending(struct mm_struct
*mm
)
528 atomic_set(&mm
->tlb_flush_pending
, 0);
531 static inline void inc_tlb_flush_pending(struct mm_struct
*mm
)
533 atomic_inc(&mm
->tlb_flush_pending
);
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
539 * So the ordering here is:
541 * atomic_inc(&mm->tlb_flush_pending);
548 * mm_tlb_flush_pending();
553 * atomic_dec(&mm->tlb_flush_pending);
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.
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
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
572 static inline void dec_tlb_flush_pending(struct mm_struct
*mm
)
575 * See inc_tlb_flush_pending().
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.
580 * Therefore we must rely on tlb_flush_*() to guarantee order.
582 atomic_dec(&mm
->tlb_flush_pending
);
585 static inline bool mm_tlb_flush_pending(struct mm_struct
*mm
)
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().
592 * That is, it only guarantees to return true if there is a flush
593 * pending for _this_ PTL.
595 return atomic_read(&mm
->tlb_flush_pending
);
598 static inline bool mm_tlb_flush_nested(struct mm_struct
*mm
)
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.
605 * (no requirement on actually still holding the PTL, that is irrelevant)
607 return atomic_read(&mm
->tlb_flush_pending
) > 1;
612 struct vm_special_mapping
{
613 const char *name
; /* The name, e.g. "[vdso]". */
616 * If .fault is not provided, this points to a
617 * NULL-terminated array of pages that back the special mapping.
619 * This must not be NULL unless .fault is provided.
624 * If non-NULL, then this is called to resolve page faults
625 * on the special mapping. If used, .pages is not checked.
627 int (*fault
)(const struct vm_special_mapping
*sm
,
628 struct vm_area_struct
*vma
,
629 struct vm_fault
*vmf
);
631 int (*mremap
)(const struct vm_special_mapping
*sm
,
632 struct vm_area_struct
*new_vma
);
635 enum tlb_flush_reason
{
636 TLB_FLUSH_ON_TASK_SWITCH
,
637 TLB_REMOTE_SHOOTDOWN
,
639 TLB_LOCAL_MM_SHOOTDOWN
,
641 NR_TLB_FLUSH_REASONS
,
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.
652 #endif /* _LINUX_MM_TYPES_H */