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))
25 typedef int vm_fault_t
;
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
38 * If you allocate the page using alloc_pages(), you can use some of the
39 * space in struct page for your own purposes. The five words in the main
40 * union are available, except for bit 0 of the first word which must be
41 * kept clear. Many users use this word to store a pointer to an object
42 * which is guaranteed to be aligned. If you use the same storage as
43 * page->mapping, you must restore it to NULL before freeing the page.
45 * If your page will not be mapped to userspace, you can also use the four
46 * bytes in the mapcount union, but you must call page_mapcount_reset()
49 * If you want to use the refcount field, it must be used in such a way
50 * that other CPUs temporarily incrementing and then decrementing the
51 * refcount does not cause problems. On receiving the page from
52 * alloc_pages(), the refcount will be positive.
54 * If you allocate pages of order > 0, you can use some of the fields
55 * in each subpage, but you may need to restore some of their values
58 * SLUB uses cmpxchg_double() to atomically update its freelist and
59 * counters. That requires that freelist & counters be adjacent and
60 * double-word aligned. We align all struct pages to double-word
61 * boundaries, and ensure that 'freelist' is aligned within the
64 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
65 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
67 #define _struct_page_alignment
71 unsigned long flags
; /* Atomic flags, some possibly
72 * updated asynchronously */
74 * Five words (20/40 bytes) are available in this union.
75 * WARNING: bit 0 of the first word is used for PageTail(). That
76 * means the other users of this union MUST NOT use the bit to
77 * avoid collision and false-positive PageTail().
80 struct { /* Page cache and anonymous pages */
82 * @lru: Pageout list, eg. active_list protected by
83 * zone_lru_lock. Sometimes used as a generic list
87 /* See page-flags.h for PAGE_MAPPING_FLAGS */
88 struct address_space
*mapping
;
89 pgoff_t index
; /* Our offset within mapping. */
91 * @private: Mapping-private opaque data.
92 * Usually used for buffer_heads if PagePrivate.
93 * Used for swp_entry_t if PageSwapCache.
94 * Indicates order in the buddy system if PageBuddy.
96 unsigned long private;
98 struct { /* slab, slob and slub */
100 struct list_head slab_list
; /* uses lru */
101 struct { /* Partial pages */
104 int pages
; /* Nr of pages left */
105 int pobjects
; /* Approximate count */
112 struct kmem_cache
*slab_cache
; /* not slob */
113 /* Double-word boundary */
114 void *freelist
; /* first free object */
116 void *s_mem
; /* slab: first object */
117 unsigned long counters
; /* SLUB */
125 struct { /* Tail pages of compound page */
126 unsigned long compound_head
; /* Bit zero is set */
128 /* First tail page only */
129 unsigned char compound_dtor
;
130 unsigned char compound_order
;
131 atomic_t compound_mapcount
;
133 struct { /* Second tail page of compound page */
134 unsigned long _compound_pad_1
; /* compound_head */
135 unsigned long _compound_pad_2
;
136 struct list_head deferred_list
;
138 struct { /* Page table pages */
139 unsigned long _pt_pad_1
; /* compound_head */
140 pgtable_t pmd_huge_pte
; /* protected by page->ptl */
141 unsigned long _pt_pad_2
; /* mapping */
143 struct mm_struct
*pt_mm
; /* x86 pgds only */
144 atomic_t pt_frag_refcount
; /* powerpc */
146 #if ALLOC_SPLIT_PTLOCKS
152 struct { /* ZONE_DEVICE pages */
153 /** @pgmap: Points to the hosting device page map. */
154 struct dev_pagemap
*pgmap
;
155 unsigned long hmm_data
;
156 unsigned long _zd_pad_1
; /* uses mapping */
159 /** @rcu_head: You can use this to free a page by RCU. */
160 struct rcu_head rcu_head
;
163 union { /* This union is 4 bytes in size. */
165 * If the page can be mapped to userspace, encodes the number
166 * of times this page is referenced by a page table.
171 * If the page is neither PageSlab nor mappable to userspace,
172 * the value stored here may help determine what this page
173 * is used for. See page-flags.h for a list of page types
174 * which are currently stored here.
176 unsigned int page_type
;
178 unsigned int active
; /* SLAB */
179 int units
; /* SLOB */
182 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
186 struct mem_cgroup
*mem_cgroup
;
190 * On machines where all RAM is mapped into kernel address space,
191 * we can simply calculate the virtual address. On machines with
192 * highmem some memory is mapped into kernel virtual memory
193 * dynamically, so we need a place to store that address.
194 * Note that this field could be 16 bits on x86 ... ;)
196 * Architectures with slow multiplication can define
197 * WANT_PAGE_VIRTUAL in asm/page.h
199 #if defined(WANT_PAGE_VIRTUAL)
200 void *virtual; /* Kernel virtual address (NULL if
201 not kmapped, ie. highmem) */
202 #endif /* WANT_PAGE_VIRTUAL */
204 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
207 } _struct_page_alignment
;
209 #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK)
210 #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE)
212 struct page_frag_cache
{
214 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
220 /* we maintain a pagecount bias, so that we dont dirty cache line
221 * containing page->_refcount every time we allocate a fragment.
223 unsigned int pagecnt_bias
;
227 typedef unsigned long vm_flags_t
;
230 * A region containing a mapping of a non-memory backed file under NOMMU
231 * conditions. These are held in a global tree and are pinned by the VMAs that
235 struct rb_node vm_rb
; /* link in global region tree */
236 vm_flags_t vm_flags
; /* VMA vm_flags */
237 unsigned long vm_start
; /* start address of region */
238 unsigned long vm_end
; /* region initialised to here */
239 unsigned long vm_top
; /* region allocated to here */
240 unsigned long vm_pgoff
; /* the offset in vm_file corresponding to vm_start */
241 struct file
*vm_file
; /* the backing file or NULL */
243 int vm_usage
; /* region usage count (access under nommu_region_sem) */
244 bool vm_icache_flushed
: 1; /* true if the icache has been flushed for
248 #ifdef CONFIG_USERFAULTFD
249 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
250 struct vm_userfaultfd_ctx
{
251 struct userfaultfd_ctx
*ctx
;
253 #else /* CONFIG_USERFAULTFD */
254 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
255 struct vm_userfaultfd_ctx
{};
256 #endif /* CONFIG_USERFAULTFD */
259 * This struct defines a memory VMM memory area. There is one of these
260 * per VM-area/task. A VM area is any part of the process virtual memory
261 * space that has a special rule for the page-fault handlers (ie a shared
262 * library, the executable area etc).
264 struct vm_area_struct
{
265 /* The first cache line has the info for VMA tree walking. */
267 unsigned long vm_start
; /* Our start address within vm_mm. */
268 unsigned long vm_end
; /* The first byte after our end address
271 /* linked list of VM areas per task, sorted by address */
272 struct vm_area_struct
*vm_next
, *vm_prev
;
274 struct rb_node vm_rb
;
277 * Largest free memory gap in bytes to the left of this VMA.
278 * Either between this VMA and vma->vm_prev, or between one of the
279 * VMAs below us in the VMA rbtree and its ->vm_prev. This helps
280 * get_unmapped_area find a free area of the right size.
282 unsigned long rb_subtree_gap
;
284 /* Second cache line starts here. */
286 struct mm_struct
*vm_mm
; /* The address space we belong to. */
287 pgprot_t vm_page_prot
; /* Access permissions of this VMA. */
288 unsigned long vm_flags
; /* Flags, see mm.h. */
291 * For areas with an address space and backing store,
292 * linkage into the address_space->i_mmap interval tree.
296 unsigned long rb_subtree_last
;
300 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
301 * list, after a COW of one of the file pages. A MAP_SHARED vma
302 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
303 * or brk vma (with NULL file) can only be in an anon_vma list.
305 struct list_head anon_vma_chain
; /* Serialized by mmap_sem &
307 struct anon_vma
*anon_vma
; /* Serialized by page_table_lock */
309 /* Function pointers to deal with this struct. */
310 const struct vm_operations_struct
*vm_ops
;
312 /* Information about our backing store: */
313 unsigned long vm_pgoff
; /* Offset (within vm_file) in PAGE_SIZE
315 struct file
* vm_file
; /* File we map to (can be NULL). */
316 void * vm_private_data
; /* was vm_pte (shared mem) */
318 atomic_long_t swap_readahead_info
;
320 struct vm_region
*vm_region
; /* NOMMU mapping region */
323 struct mempolicy
*vm_policy
; /* NUMA policy for the VMA */
325 struct vm_userfaultfd_ctx vm_userfaultfd_ctx
;
326 } __randomize_layout
;
329 struct task_struct
*task
;
330 struct core_thread
*next
;
335 struct core_thread dumper
;
336 struct completion startup
;
342 struct vm_area_struct
*mmap
; /* list of VMAs */
343 struct rb_root mm_rb
;
344 u64 vmacache_seqnum
; /* per-thread vmacache */
346 unsigned long (*get_unmapped_area
) (struct file
*filp
,
347 unsigned long addr
, unsigned long len
,
348 unsigned long pgoff
, unsigned long flags
);
350 unsigned long mmap_base
; /* base of mmap area */
351 unsigned long mmap_legacy_base
; /* base of mmap area in bottom-up allocations */
352 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
353 /* Base adresses for compatible mmap() */
354 unsigned long mmap_compat_base
;
355 unsigned long mmap_compat_legacy_base
;
357 unsigned long task_size
; /* size of task vm space */
358 unsigned long highest_vm_end
; /* highest vma end address */
362 * @mm_users: The number of users including userspace.
364 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
365 * drops to 0 (i.e. when the task exits and there are no other
366 * temporary reference holders), we also release a reference on
367 * @mm_count (which may then free the &struct mm_struct if
368 * @mm_count also drops to 0).
373 * @mm_count: The number of references to &struct mm_struct
374 * (@mm_users count as 1).
376 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the
377 * &struct mm_struct is freed.
382 atomic_long_t pgtables_bytes
; /* PTE page table pages */
384 int map_count
; /* number of VMAs */
386 spinlock_t page_table_lock
; /* Protects page tables and some
389 struct rw_semaphore mmap_sem
;
391 struct list_head mmlist
; /* List of maybe swapped mm's. These
392 * are globally strung together off
393 * init_mm.mmlist, and are protected
398 unsigned long hiwater_rss
; /* High-watermark of RSS usage */
399 unsigned long hiwater_vm
; /* High-water virtual memory usage */
401 unsigned long total_vm
; /* Total pages mapped */
402 unsigned long locked_vm
; /* Pages that have PG_mlocked set */
403 unsigned long pinned_vm
; /* Refcount permanently increased */
404 unsigned long data_vm
; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
405 unsigned long exec_vm
; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
406 unsigned long stack_vm
; /* VM_STACK */
407 unsigned long def_flags
;
409 spinlock_t arg_lock
; /* protect the below fields */
410 unsigned long start_code
, end_code
, start_data
, end_data
;
411 unsigned long start_brk
, brk
, start_stack
;
412 unsigned long arg_start
, arg_end
, env_start
, env_end
;
414 unsigned long saved_auxv
[AT_VECTOR_SIZE
]; /* for /proc/PID/auxv */
417 * Special counters, in some configurations protected by the
418 * page_table_lock, in other configurations by being atomic.
420 struct mm_rss_stat rss_stat
;
422 struct linux_binfmt
*binfmt
;
424 /* Architecture-specific MM context */
425 mm_context_t context
;
427 unsigned long flags
; /* Must use atomic bitops to access */
429 struct core_state
*core_state
; /* coredumping support */
430 #ifdef CONFIG_MEMBARRIER
431 atomic_t membarrier_state
;
434 spinlock_t ioctx_lock
;
435 struct kioctx_table __rcu
*ioctx_table
;
439 * "owner" points to a task that is regarded as the canonical
440 * user/owner of this mm. All of the following must be true in
441 * order for it to be changed:
443 * current == mm->owner
445 * new_owner->mm == mm
446 * new_owner->alloc_lock is held
448 struct task_struct __rcu
*owner
;
450 struct user_namespace
*user_ns
;
452 /* store ref to file /proc/<pid>/exe symlink points to */
453 struct file __rcu
*exe_file
;
454 #ifdef CONFIG_MMU_NOTIFIER
455 struct mmu_notifier_mm
*mmu_notifier_mm
;
457 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
458 pgtable_t pmd_huge_pte
; /* protected by page_table_lock */
460 #ifdef CONFIG_NUMA_BALANCING
462 * numa_next_scan is the next time that the PTEs will be marked
463 * pte_numa. NUMA hinting faults will gather statistics and
464 * migrate pages to new nodes if necessary.
466 unsigned long numa_next_scan
;
468 /* Restart point for scanning and setting pte_numa */
469 unsigned long numa_scan_offset
;
471 /* numa_scan_seq prevents two threads setting pte_numa */
475 * An operation with batched TLB flushing is going on. Anything
476 * that can move process memory needs to flush the TLB when
477 * moving a PROT_NONE or PROT_NUMA mapped page.
479 atomic_t tlb_flush_pending
;
480 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
481 /* See flush_tlb_batched_pending() */
482 bool tlb_flush_batched
;
484 struct uprobes_state uprobes_state
;
485 #ifdef CONFIG_HUGETLB_PAGE
486 atomic_long_t hugetlb_usage
;
488 struct work_struct async_put_work
;
490 #if IS_ENABLED(CONFIG_HMM)
491 /* HMM needs to track a few things per mm */
494 } __randomize_layout
;
497 * The mm_cpumask needs to be at the end of mm_struct, because it
498 * is dynamically sized based on nr_cpu_ids.
500 unsigned long cpu_bitmap
[];
503 extern struct mm_struct init_mm
;
505 /* Pointer magic because the dynamic array size confuses some compilers. */
506 static inline void mm_init_cpumask(struct mm_struct
*mm
)
508 unsigned long cpu_bitmap
= (unsigned long)mm
;
510 cpu_bitmap
+= offsetof(struct mm_struct
, cpu_bitmap
);
511 cpumask_clear((struct cpumask
*)cpu_bitmap
);
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 (struct cpumask
*)&mm
->cpu_bitmap
;
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 vm_fault_t (*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 */