1 #ifndef _LINUX_MM_TYPES_H
2 #define _LINUX_MM_TYPES_H
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/rcupdate.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))
28 #define USE_SPLIT_PTE_PTLOCKS (NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS)
29 #define USE_SPLIT_PMD_PTLOCKS (USE_SPLIT_PTE_PTLOCKS && \
30 IS_ENABLED(CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK))
31 #define ALLOC_SPLIT_PTLOCKS (SPINLOCK_SIZE > BITS_PER_LONG/8)
34 * Each physical page in the system has a struct page associated with
35 * it to keep track of whatever it is we are using the page for at the
36 * moment. Note that we have no way to track which tasks are using
37 * a page, though if it is a pagecache page, rmap structures can tell us
40 * The objects in struct page are organized in double word blocks in
41 * order to allows us to use atomic double word operations on portions
42 * of struct page. That is currently only used by slub but the arrangement
43 * allows the use of atomic double word operations on the flags/mapping
44 * and lru list pointers also.
47 /* First double word block */
48 unsigned long flags; /* Atomic flags, some possibly
49 * updated asynchronously */
51 struct address_space *mapping; /* If low bit clear, points to
52 * inode address_space, or NULL.
53 * If page mapped as anonymous
54 * memory, low bit is set, and
55 * it points to anon_vma object:
56 * see PAGE_MAPPING_ANON below.
58 void *s_mem; /* slab first object */
59 atomic_t compound_mapcount; /* first tail page */
60 /* page_deferred_list().next -- second tail page */
63 /* Second double word */
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 */
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;
77 * Keep _refcount separate from slub cmpxchg_double data.
78 * As the rest of the double word is protected by slab_lock
79 * but _refcount is not.
87 * Count of ptes mapped in mms, to show when
88 * page is mapped & limit reverse map searches.
90 * Extra information about page type may be
91 * stored here for pages that are never mapped,
92 * in which case the value MUST BE <= -2.
93 * See page-flags.h for more details.
97 unsigned int active; /* SLAB */
103 int units; /* SLOB */
106 * Usage count, *USE WRAPPER FUNCTION* when manual
107 * accounting. See page_ref.h
114 * Third double word block
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 */
176 /* Remainder is not double word aligned */
178 unsigned long private; /* Mapping-private opaque data:
179 * usually used for buffer_heads
180 * if PagePrivate set; used for
181 * swp_entry_t if PageSwapCache;
182 * indicates order in the buddy
183 * system if PG_buddy is set.
185 #if USE_SPLIT_PTE_PTLOCKS
186 #if ALLOC_SPLIT_PTLOCKS
192 struct kmem_cache *slab_cache; /* SL[AU]B: Pointer to slab */
196 struct mem_cgroup *mem_cgroup;
200 * On machines where all RAM is mapped into kernel address space,
201 * we can simply calculate the virtual address. On machines with
202 * highmem some memory is mapped into kernel virtual memory
203 * dynamically, so we need a place to store that address.
204 * Note that this field could be 16 bits on x86 ... ;)
206 * Architectures with slow multiplication can define
207 * WANT_PAGE_VIRTUAL in asm/page.h
209 #if defined(WANT_PAGE_VIRTUAL)
210 void *virtual; /* Kernel virtual address (NULL if
211 not kmapped, ie. highmem) */
212 #endif /* WANT_PAGE_VIRTUAL */
214 #ifdef CONFIG_KMEMCHECK
216 * kmemcheck wants to track the status of each byte in a page; this
217 * is a pointer to such a status block. NULL if not tracked.
222 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
227 * The struct page can be forced to be double word aligned so that atomic ops
228 * on double words work. The SLUB allocator can make use of such a feature.
230 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
231 __aligned(2 * sizeof(unsigned long))
237 #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
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)
249 struct page_frag_cache {
251 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
257 /* we maintain a pagecount bias, so that we dont dirty cache line
258 * containing page->_refcount every time we allocate a fragment.
260 unsigned int pagecnt_bias;
264 typedef unsigned long vm_flags_t;
267 * A region containing a mapping of a non-memory backed file under NOMMU
268 * conditions. These are held in a global tree and are pinned by the VMAs that
272 struct rb_node vm_rb; /* link in global region tree */
273 vm_flags_t vm_flags; /* VMA vm_flags */
274 unsigned long vm_start; /* start address of region */
275 unsigned long vm_end; /* region initialised to here */
276 unsigned long vm_top; /* region allocated to here */
277 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
278 struct file *vm_file; /* the backing file or NULL */
280 int vm_usage; /* region usage count (access under nommu_region_sem) */
281 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
285 #ifdef CONFIG_USERFAULTFD
286 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
287 struct vm_userfaultfd_ctx {
288 struct userfaultfd_ctx *ctx;
290 #else /* CONFIG_USERFAULTFD */
291 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
292 struct vm_userfaultfd_ctx {};
293 #endif /* CONFIG_USERFAULTFD */
296 * This struct defines a memory VMM memory area. There is one of these
297 * per VM-area/task. A VM area is any part of the process virtual memory
298 * space that has a special rule for the page-fault handlers (ie a shared
299 * library, the executable area etc).
301 struct vm_area_struct {
302 /* The first cache line has the info for VMA tree walking. */
304 unsigned long vm_start; /* Our start address within vm_mm. */
305 unsigned long vm_end; /* The first byte after our end address
308 /* linked list of VM areas per task, sorted by address */
309 struct vm_area_struct *vm_next, *vm_prev;
311 struct rb_node vm_rb;
314 * Largest free memory gap in bytes to the left of this VMA.
315 * Either between this VMA and vma->vm_prev, or between one of the
316 * VMAs below us in the VMA rbtree and its ->vm_prev. This helps
317 * get_unmapped_area find a free area of the right size.
319 unsigned long rb_subtree_gap;
321 /* Second cache line starts here. */
323 struct mm_struct *vm_mm; /* The address space we belong to. */
324 pgprot_t vm_page_prot; /* Access permissions of this VMA. */
325 unsigned long vm_flags; /* Flags, see mm.h. */
328 * For areas with an address space and backing store,
329 * linkage into the address_space->i_mmap interval tree.
333 unsigned long rb_subtree_last;
337 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
338 * list, after a COW of one of the file pages. A MAP_SHARED vma
339 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
340 * or brk vma (with NULL file) can only be in an anon_vma list.
342 struct list_head anon_vma_chain; /* Serialized by mmap_sem &
344 struct anon_vma *anon_vma; /* Serialized by page_table_lock */
346 /* Function pointers to deal with this struct. */
347 const struct vm_operations_struct *vm_ops;
349 /* Information about our backing store: */
350 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
352 struct file * vm_file; /* File we map to (can be NULL). */
353 void * vm_private_data; /* was vm_pte (shared mem) */
356 struct vm_region *vm_region; /* NOMMU mapping region */
359 struct mempolicy *vm_policy; /* NUMA policy for the VMA */
361 struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
365 struct task_struct *task;
366 struct core_thread *next;
371 struct core_thread dumper;
372 struct completion startup;
376 MM_FILEPAGES, /* Resident file mapping pages */
377 MM_ANONPAGES, /* Resident anonymous pages */
378 MM_SWAPENTS, /* Anonymous swap entries */
379 MM_SHMEMPAGES, /* Resident shared memory pages */
383 #if USE_SPLIT_PTE_PTLOCKS && defined(CONFIG_MMU)
384 #define SPLIT_RSS_COUNTING
385 /* per-thread cached information, */
386 struct task_rss_stat {
387 int events; /* for synchronization threshold */
388 int count[NR_MM_COUNTERS];
390 #endif /* USE_SPLIT_PTE_PTLOCKS */
393 atomic_long_t count[NR_MM_COUNTERS];
398 struct vm_area_struct *mmap; /* list of VMAs */
399 struct rb_root mm_rb;
400 u32 vmacache_seqnum; /* per-thread vmacache */
402 unsigned long (*get_unmapped_area) (struct file *filp,
403 unsigned long addr, unsigned long len,
404 unsigned long pgoff, unsigned long flags);
406 unsigned long mmap_base; /* base of mmap area */
407 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
408 unsigned long task_size; /* size of task vm space */
409 unsigned long highest_vm_end; /* highest vma end address */
411 atomic_t mm_users; /* How many users with user space? */
412 atomic_t mm_count; /* How many references to "struct mm_struct" (users count as 1) */
413 atomic_long_t nr_ptes; /* PTE page table pages */
414 #if CONFIG_PGTABLE_LEVELS > 2
415 atomic_long_t nr_pmds; /* PMD page table pages */
417 int map_count; /* number of VMAs */
419 spinlock_t page_table_lock; /* Protects page tables and some counters */
420 struct rw_semaphore mmap_sem;
422 struct list_head mmlist; /* List of maybe swapped mm's. These are globally strung
423 * together off init_mm.mmlist, and are protected
428 unsigned long hiwater_rss; /* High-watermark of RSS usage */
429 unsigned long hiwater_vm; /* High-water virtual memory usage */
431 unsigned long total_vm; /* Total pages mapped */
432 unsigned long locked_vm; /* Pages that have PG_mlocked set */
433 unsigned long pinned_vm; /* Refcount permanently increased */
434 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
435 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
436 unsigned long stack_vm; /* VM_STACK */
437 unsigned long def_flags;
438 unsigned long start_code, end_code, start_data, end_data;
439 unsigned long start_brk, brk, start_stack;
440 unsigned long arg_start, arg_end, env_start, env_end;
442 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
445 * Special counters, in some configurations protected by the
446 * page_table_lock, in other configurations by being atomic.
448 struct mm_rss_stat rss_stat;
450 struct linux_binfmt *binfmt;
452 cpumask_var_t cpu_vm_mask_var;
454 /* Architecture-specific MM context */
455 mm_context_t context;
457 unsigned long flags; /* Must use atomic bitops to access the bits */
459 struct core_state *core_state; /* coredumping support */
461 spinlock_t ioctx_lock;
462 struct kioctx_table __rcu *ioctx_table;
466 * "owner" points to a task that is regarded as the canonical
467 * user/owner of this mm. All of the following must be true in
468 * order for it to be changed:
470 * current == mm->owner
472 * new_owner->mm == mm
473 * new_owner->alloc_lock is held
475 struct task_struct __rcu *owner;
478 /* store ref to file /proc/<pid>/exe symlink points to */
479 struct file __rcu *exe_file;
480 #ifdef CONFIG_MMU_NOTIFIER
481 struct mmu_notifier_mm *mmu_notifier_mm;
483 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
484 pgtable_t pmd_huge_pte; /* protected by page_table_lock */
486 #ifdef CONFIG_CPUMASK_OFFSTACK
487 struct cpumask cpumask_allocation;
489 #ifdef CONFIG_NUMA_BALANCING
491 * numa_next_scan is the next time that the PTEs will be marked
492 * pte_numa. NUMA hinting faults will gather statistics and migrate
493 * pages to new nodes if necessary.
495 unsigned long numa_next_scan;
497 /* Restart point for scanning and setting pte_numa */
498 unsigned long numa_scan_offset;
500 /* numa_scan_seq prevents two threads setting pte_numa */
503 #if defined(CONFIG_NUMA_BALANCING) || defined(CONFIG_COMPACTION)
505 * An operation with batched TLB flushing is going on. Anything that
506 * can move process memory needs to flush the TLB when moving a
507 * PROT_NONE or PROT_NUMA mapped page.
509 bool tlb_flush_pending;
511 struct uprobes_state uprobes_state;
512 #ifdef CONFIG_PREEMPT_RT_BASE
513 struct rcu_head delayed_drop;
515 #ifdef CONFIG_X86_INTEL_MPX
516 /* address of the bounds directory */
517 void __user *bd_addr;
519 #ifdef CONFIG_HUGETLB_PAGE
520 atomic_long_t hugetlb_usage;
522 struct work_struct async_put_work;
525 static inline void mm_init_cpumask(struct mm_struct *mm)
527 #ifdef CONFIG_CPUMASK_OFFSTACK
528 mm->cpu_vm_mask_var = &mm->cpumask_allocation;
530 cpumask_clear(mm->cpu_vm_mask_var);
533 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
534 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
536 return mm->cpu_vm_mask_var;
539 #if defined(CONFIG_NUMA_BALANCING) || defined(CONFIG_COMPACTION)
541 * Memory barriers to keep this state in sync are graciously provided by
542 * the page table locks, outside of which no page table modifications happen.
543 * The barriers below prevent the compiler from re-ordering the instructions
544 * around the memory barriers that are already present in the code.
546 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
549 return mm->tlb_flush_pending;
551 static inline void set_tlb_flush_pending(struct mm_struct *mm)
553 mm->tlb_flush_pending = true;
556 * Guarantee that the tlb_flush_pending store does not leak into the
557 * critical section updating the page tables
559 smp_mb__before_spinlock();
561 /* Clearing is done after a TLB flush, which also provides a barrier. */
562 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
565 mm->tlb_flush_pending = false;
568 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
572 static inline void set_tlb_flush_pending(struct mm_struct *mm)
575 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
582 struct vm_special_mapping {
583 const char *name; /* The name, e.g. "[vdso]". */
586 * If .fault is not provided, this points to a
587 * NULL-terminated array of pages that back the special mapping.
589 * This must not be NULL unless .fault is provided.
594 * If non-NULL, then this is called to resolve page faults
595 * on the special mapping. If used, .pages is not checked.
597 int (*fault)(const struct vm_special_mapping *sm,
598 struct vm_area_struct *vma,
599 struct vm_fault *vmf);
601 int (*mremap)(const struct vm_special_mapping *sm,
602 struct vm_area_struct *new_vma);
605 enum tlb_flush_reason {
606 TLB_FLUSH_ON_TASK_SWITCH,
607 TLB_REMOTE_SHOOTDOWN,
609 TLB_LOCAL_MM_SHOOTDOWN,
611 NR_TLB_FLUSH_REASONS,
615 * A swap entry has to fit into a "unsigned long", as the entry is hidden
616 * in the "index" field of the swapper address space.
622 #endif /* _LINUX_MM_TYPES_H */