]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/mm_types.h
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[mirror_ubuntu-jammy-kernel.git] / include / linux / mm_types.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
5b99cd0e
HC
2#ifndef _LINUX_MM_TYPES_H
3#define _LINUX_MM_TYPES_H
4
2e58f173
IM
5#include <linux/mm_types_task.h>
6
4f9a58d7 7#include <linux/auxvec.h>
5b99cd0e
HC
8#include <linux/list.h>
9#include <linux/spinlock.h>
c92ff1bd
MS
10#include <linux/rbtree.h>
11#include <linux/rwsem.h>
12#include <linux/completion.h>
cddb8a5c 13#include <linux/cpumask.h>
d4b3b638 14#include <linux/uprobes.h>
bbeae5b0 15#include <linux/page-flags-layout.h>
ec8d7c14 16#include <linux/workqueue.h>
2e58f173 17
c92ff1bd 18#include <asm/mmu.h>
5b99cd0e 19
4f9a58d7
OH
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
1c8f4220 25
5b99cd0e 26struct address_space;
1306a85a 27struct mem_cgroup;
5b99cd0e
HC
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
97b4a671 34 * who is mapping it.
be50015d 35 *
97b4a671
MW
36 * If you allocate the page using alloc_pages(), you can use some of the
37 * space in struct page for your own purposes. The five words in the main
38 * union are available, except for bit 0 of the first word which must be
39 * kept clear. Many users use this word to store a pointer to an object
40 * which is guaranteed to be aligned. If you use the same storage as
41 * page->mapping, you must restore it to NULL before freeing the page.
be50015d 42 *
97b4a671
MW
43 * If your page will not be mapped to userspace, you can also use the four
44 * bytes in the mapcount union, but you must call page_mapcount_reset()
45 * before freeing it.
46 *
47 * If you want to use the refcount field, it must be used in such a way
48 * that other CPUs temporarily incrementing and then decrementing the
49 * refcount does not cause problems. On receiving the page from
50 * alloc_pages(), the refcount will be positive.
51 *
52 * If you allocate pages of order > 0, you can use some of the fields
53 * in each subpage, but you may need to restore some of their values
54 * afterwards.
fc9bb8c7 55 *
4cf7c8bf
MW
56 * SLUB uses cmpxchg_double() to atomically update its freelist and
57 * counters. That requires that freelist & counters be adjacent and
58 * double-word aligned. We align all struct pages to double-word
59 * boundaries, and ensure that 'freelist' is aligned within the
60 * struct.
5b99cd0e 61 */
e20df2c6
MW
62#ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
63#define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
64#else
0dd4da5b 65#define _struct_page_alignment
7d27a04b 66#endif
e20df2c6 67
5b99cd0e
HC
68struct page {
69 unsigned long flags; /* Atomic flags, some possibly
70 * updated asynchronously */
b7ccc7f8 71 /*
4da1984e
MW
72 * Five words (20/40 bytes) are available in this union.
73 * WARNING: bit 0 of the first word is used for PageTail(). That
74 * means the other users of this union MUST NOT use the bit to
b7ccc7f8
MW
75 * avoid collision and false-positive PageTail().
76 */
8456a648 77 union {
66a6ffd2 78 struct { /* Page cache and anonymous pages */
4da1984e
MW
79 /**
80 * @lru: Pageout list, eg. active_list protected by
f4b7e272 81 * pgdat->lru_lock. Sometimes used as a generic list
4da1984e
MW
82 * by the page owner.
83 */
84 struct list_head lru;
66a6ffd2
MW
85 /* See page-flags.h for PAGE_MAPPING_FLAGS */
86 struct address_space *mapping;
87 pgoff_t index; /* Our offset within mapping. */
88 /**
89 * @private: Mapping-private opaque data.
90 * Usually used for buffer_heads if PagePrivate.
91 * Used for swp_entry_t if PageSwapCache.
92 * Indicates order in the buddy system if PageBuddy.
93 */
94 unsigned long private;
95 };
c25fff71
JDB
96 struct { /* page_pool used by netstack */
97 /**
98 * @dma_addr: might require a 64-bit value even on
99 * 32-bit architectures.
100 */
101 dma_addr_t dma_addr;
102 };
66a6ffd2 103 struct { /* slab, slob and slub */
4da1984e 104 union {
3e05617c 105 struct list_head slab_list;
4da1984e
MW
106 struct { /* Partial pages */
107 struct page *next;
108#ifdef CONFIG_64BIT
109 int pages; /* Nr of pages left */
110 int pobjects; /* Approximate count */
111#else
112 short int pages;
113 short int pobjects;
114#endif
115 };
116 };
66a6ffd2
MW
117 struct kmem_cache *slab_cache; /* not slob */
118 /* Double-word boundary */
119 void *freelist; /* first free object */
120 union {
121 void *s_mem; /* slab: first object */
122 unsigned long counters; /* SLUB */
123 struct { /* SLUB */
124 unsigned inuse:16;
125 unsigned objects:15;
126 unsigned frozen:1;
127 };
128 };
129 };
4da1984e
MW
130 struct { /* Tail pages of compound page */
131 unsigned long compound_head; /* Bit zero is set */
132
133 /* First tail page only */
134 unsigned char compound_dtor;
135 unsigned char compound_order;
136 atomic_t compound_mapcount;
1378a5ee 137 unsigned int compound_nr; /* 1 << compound_order */
4da1984e
MW
138 };
139 struct { /* Second tail page of compound page */
140 unsigned long _compound_pad_1; /* compound_head */
47e29d32 141 atomic_t hpage_pinned_refcount;
87eaceb3 142 /* For both global and memcg */
4da1984e
MW
143 struct list_head deferred_list;
144 };
66a6ffd2 145 struct { /* Page table pages */
4da1984e
MW
146 unsigned long _pt_pad_1; /* compound_head */
147 pgtable_t pmd_huge_pte; /* protected by page->ptl */
66a6ffd2 148 unsigned long _pt_pad_2; /* mapping */
4231aba0
NP
149 union {
150 struct mm_struct *pt_mm; /* x86 pgds only */
151 atomic_t pt_frag_refcount; /* powerpc */
152 };
7d27a04b 153#if ALLOC_SPLIT_PTLOCKS
66a6ffd2 154 spinlock_t *ptl;
7d27a04b 155#else
66a6ffd2 156 spinlock_t ptl;
7d27a04b 157#endif
7d27a04b 158 };
50e7fbc3
MW
159 struct { /* ZONE_DEVICE pages */
160 /** @pgmap: Points to the hosting device page map. */
161 struct dev_pagemap *pgmap;
8a164fef 162 void *zone_device_data;
76470ccd
RC
163 /*
164 * ZONE_DEVICE private pages are counted as being
165 * mapped so the next 3 words hold the mapping, index,
166 * and private fields from the source anonymous or
167 * page cache page while the page is migrated to device
168 * private memory.
169 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
170 * use the mapping, index, and private fields when
171 * pmem backed DAX files are mapped.
172 */
50e7fbc3 173 };
4da1984e
MW
174
175 /** @rcu_head: You can use this to free a page by RCU. */
176 struct rcu_head rcu_head;
7d27a04b
MW
177 };
178
b21999da
MW
179 union { /* This union is 4 bytes in size. */
180 /*
181 * If the page can be mapped to userspace, encodes the number
182 * of times this page is referenced by a page table.
183 */
184 atomic_t _mapcount;
185
6e292b9b
MW
186 /*
187 * If the page is neither PageSlab nor mappable to userspace,
188 * the value stored here may help determine what this page
189 * is used for. See page-flags.h for a list of page types
190 * which are currently stored here.
191 */
192 unsigned int page_type;
193
ca9c88c7 194 unsigned int active; /* SLAB */
ca9c88c7 195 int units; /* SLOB */
81819f0f 196 };
fc9bb8c7 197
b21999da
MW
198 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
199 atomic_t _refcount;
200
1306a85a 201#ifdef CONFIG_MEMCG
286e04b8
RG
202 union {
203 struct mem_cgroup *mem_cgroup;
204 struct obj_cgroup **obj_cgroups;
205 };
1306a85a
JW
206#endif
207
5b99cd0e
HC
208 /*
209 * On machines where all RAM is mapped into kernel address space,
210 * we can simply calculate the virtual address. On machines with
211 * highmem some memory is mapped into kernel virtual memory
212 * dynamically, so we need a place to store that address.
213 * Note that this field could be 16 bits on x86 ... ;)
214 *
215 * Architectures with slow multiplication can define
216 * WANT_PAGE_VIRTUAL in asm/page.h
217 */
218#if defined(WANT_PAGE_VIRTUAL)
219 void *virtual; /* Kernel virtual address (NULL if
220 not kmapped, ie. highmem) */
221#endif /* WANT_PAGE_VIRTUAL */
dfec072e 222
90572890
PZ
223#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
224 int _last_cpupid;
57e0a030 225#endif
e20df2c6 226} _struct_page_alignment;
5b99cd0e 227
169226f7
YS
228static inline atomic_t *compound_mapcount_ptr(struct page *page)
229{
230 return &page[1].compound_mapcount;
231}
232
47e29d32
JH
233static inline atomic_t *compound_pincount_ptr(struct page *page)
234{
235 return &page[2].hpage_pinned_refcount;
236}
237
d1402fc7
LG
238/*
239 * Used for sizing the vmemmap region on some architectures
240 */
241#define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page)))
242
b63ae8ca
AD
243#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK)
244#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE)
245
b03641af 246#define page_private(page) ((page)->private)
60e65a6f
GJ
247
248static inline void set_page_private(struct page *page, unsigned long private)
249{
250 page->private = private;
251}
b03641af 252
b63ae8ca
AD
253struct page_frag_cache {
254 void * va;
255#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
256 __u16 offset;
257 __u16 size;
258#else
259 __u32 offset;
260#endif
261 /* we maintain a pagecount bias, so that we dont dirty cache line
0139aa7b 262 * containing page->_refcount every time we allocate a fragment.
b63ae8ca
AD
263 */
264 unsigned int pagecnt_bias;
265 bool pfmemalloc;
266};
267
64b990d2 268typedef unsigned long vm_flags_t;
ca16d140 269
8feae131
DH
270/*
271 * A region containing a mapping of a non-memory backed file under NOMMU
272 * conditions. These are held in a global tree and are pinned by the VMAs that
273 * map parts of them.
274 */
275struct vm_region {
276 struct rb_node vm_rb; /* link in global region tree */
ca16d140 277 vm_flags_t vm_flags; /* VMA vm_flags */
8feae131
DH
278 unsigned long vm_start; /* start address of region */
279 unsigned long vm_end; /* region initialised to here */
dd8632a1 280 unsigned long vm_top; /* region allocated to here */
8feae131
DH
281 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
282 struct file *vm_file; /* the backing file or NULL */
283
1e2ae599 284 int vm_usage; /* region usage count (access under nommu_region_sem) */
cfe79c00
MF
285 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
286 * this region */
8feae131
DH
287};
288
745f234b
AA
289#ifdef CONFIG_USERFAULTFD
290#define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
291struct vm_userfaultfd_ctx {
292 struct userfaultfd_ctx *ctx;
293};
294#else /* CONFIG_USERFAULTFD */
295#define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
296struct vm_userfaultfd_ctx {};
297#endif /* CONFIG_USERFAULTFD */
298
c92ff1bd 299/*
552657b7 300 * This struct describes a virtual memory area. There is one of these
301 * per VM-area/task. A VM area is any part of the process virtual memory
c92ff1bd
MS
302 * space that has a special rule for the page-fault handlers (ie a shared
303 * library, the executable area etc).
304 */
305struct vm_area_struct {
e4c6bfd2
RR
306 /* The first cache line has the info for VMA tree walking. */
307
c92ff1bd
MS
308 unsigned long vm_start; /* Our start address within vm_mm. */
309 unsigned long vm_end; /* The first byte after our end address
310 within vm_mm. */
311
312 /* linked list of VM areas per task, sorted by address */
297c5eee 313 struct vm_area_struct *vm_next, *vm_prev;
c92ff1bd 314
c92ff1bd
MS
315 struct rb_node vm_rb;
316
d3737187
ML
317 /*
318 * Largest free memory gap in bytes to the left of this VMA.
319 * Either between this VMA and vma->vm_prev, or between one of the
320 * VMAs below us in the VMA rbtree and its ->vm_prev. This helps
321 * get_unmapped_area find a free area of the right size.
322 */
323 unsigned long rb_subtree_gap;
324
e4c6bfd2
RR
325 /* Second cache line starts here. */
326
327 struct mm_struct *vm_mm; /* The address space we belong to. */
574c5b3d
TH
328
329 /*
330 * Access permissions of this VMA.
331 * See vmf_insert_mixed_prot() for discussion.
332 */
333 pgprot_t vm_page_prot;
e4c6bfd2
RR
334 unsigned long vm_flags; /* Flags, see mm.h. */
335
c92ff1bd
MS
336 /*
337 * For areas with an address space and backing store,
27ba0644 338 * linkage into the address_space->i_mmap interval tree.
c92ff1bd 339 */
ac51b934
KS
340 struct {
341 struct rb_node rb;
342 unsigned long rb_subtree_last;
c92ff1bd
MS
343 } shared;
344
345 /*
346 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
347 * list, after a COW of one of the file pages. A MAP_SHARED vma
348 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
349 * or brk vma (with NULL file) can only be in an anon_vma list.
350 */
c1e8d7c6 351 struct list_head anon_vma_chain; /* Serialized by mmap_lock &
5beb4930 352 * page_table_lock */
c92ff1bd
MS
353 struct anon_vma *anon_vma; /* Serialized by page_table_lock */
354
355 /* Function pointers to deal with this struct. */
f0f37e2f 356 const struct vm_operations_struct *vm_ops;
c92ff1bd
MS
357
358 /* Information about our backing store: */
359 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
ea1754a0 360 units */
c92ff1bd
MS
361 struct file * vm_file; /* File we map to (can be NULL). */
362 void * vm_private_data; /* was vm_pte (shared mem) */
c92ff1bd 363
219f8a2e 364#ifdef CONFIG_SWAP
ec560175 365 atomic_long_t swap_readahead_info;
219f8a2e 366#endif
c92ff1bd 367#ifndef CONFIG_MMU
8feae131 368 struct vm_region *vm_region; /* NOMMU mapping region */
c92ff1bd
MS
369#endif
370#ifdef CONFIG_NUMA
371 struct mempolicy *vm_policy; /* NUMA policy for the VMA */
372#endif
745f234b 373 struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
3859a271 374} __randomize_layout;
c92ff1bd 375
b564daf8
ON
376struct core_thread {
377 struct task_struct *task;
378 struct core_thread *next;
379};
380
32ecb1f2 381struct core_state {
c5f1cc8c 382 atomic_t nr_threads;
b564daf8 383 struct core_thread dumper;
32ecb1f2
ON
384 struct completion startup;
385};
386
db446a08 387struct kioctx_table;
c92ff1bd 388struct mm_struct {
c1a2f7f0
RR
389 struct {
390 struct vm_area_struct *mmap; /* list of VMAs */
391 struct rb_root mm_rb;
7a9cdebd 392 u64 vmacache_seqnum; /* per-thread vmacache */
efc1a3b1 393#ifdef CONFIG_MMU
c1a2f7f0 394 unsigned long (*get_unmapped_area) (struct file *filp,
c92ff1bd
MS
395 unsigned long addr, unsigned long len,
396 unsigned long pgoff, unsigned long flags);
efc1a3b1 397#endif
c1a2f7f0
RR
398 unsigned long mmap_base; /* base of mmap area */
399 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
1b028f78 400#ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
c1a2f7f0
RR
401 /* Base adresses for compatible mmap() */
402 unsigned long mmap_compat_base;
403 unsigned long mmap_compat_legacy_base;
1b028f78 404#endif
c1a2f7f0
RR
405 unsigned long task_size; /* size of task vm space */
406 unsigned long highest_vm_end; /* highest vma end address */
407 pgd_t * pgd;
408
227a4aad
MD
409#ifdef CONFIG_MEMBARRIER
410 /**
411 * @membarrier_state: Flags controlling membarrier behavior.
412 *
413 * This field is close to @pgd to hopefully fit in the same
414 * cache-line, which needs to be touched by switch_mm().
415 */
416 atomic_t membarrier_state;
417#endif
418
c1a2f7f0
RR
419 /**
420 * @mm_users: The number of users including userspace.
421 *
422 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
423 * drops to 0 (i.e. when the task exits and there are no other
424 * temporary reference holders), we also release a reference on
425 * @mm_count (which may then free the &struct mm_struct if
426 * @mm_count also drops to 0).
427 */
428 atomic_t mm_users;
429
430 /**
431 * @mm_count: The number of references to &struct mm_struct
432 * (@mm_users count as 1).
433 *
434 * Use mmgrab()/mmdrop() to modify. When this drops to 0, the
435 * &struct mm_struct is freed.
436 */
437 atomic_t mm_count;
b279ddc3 438
c4812909 439#ifdef CONFIG_MMU
c1a2f7f0 440 atomic_long_t pgtables_bytes; /* PTE page table pages */
5a3fbef3 441#endif
c1a2f7f0 442 int map_count; /* number of VMAs */
481b4bb5 443
c1a2f7f0
RR
444 spinlock_t page_table_lock; /* Protects page tables and some
445 * counters
446 */
da1c55f1 447 struct rw_semaphore mmap_lock;
c92ff1bd 448
c1a2f7f0
RR
449 struct list_head mmlist; /* List of maybe swapped mm's. These
450 * are globally strung together off
451 * init_mm.mmlist, and are protected
452 * by mmlist_lock
453 */
c92ff1bd 454
c92ff1bd 455
c1a2f7f0
RR
456 unsigned long hiwater_rss; /* High-watermark of RSS usage */
457 unsigned long hiwater_vm; /* High-water virtual memory usage */
c92ff1bd 458
c1a2f7f0
RR
459 unsigned long total_vm; /* Total pages mapped */
460 unsigned long locked_vm; /* Pages that have PG_mlocked set */
70f8a3ca 461 atomic64_t pinned_vm; /* Refcount permanently increased */
c1a2f7f0
RR
462 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
463 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
464 unsigned long stack_vm; /* VM_STACK */
465 unsigned long def_flags;
88aa7cc6 466
c1a2f7f0
RR
467 spinlock_t arg_lock; /* protect the below fields */
468 unsigned long start_code, end_code, start_data, end_data;
469 unsigned long start_brk, brk, start_stack;
470 unsigned long arg_start, arg_end, env_start, env_end;
c92ff1bd 471
c1a2f7f0 472 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
c92ff1bd 473
c1a2f7f0
RR
474 /*
475 * Special counters, in some configurations protected by the
476 * page_table_lock, in other configurations by being atomic.
477 */
478 struct mm_rss_stat rss_stat;
801460d0 479
c1a2f7f0 480 struct linux_binfmt *binfmt;
6345d24d 481
c1a2f7f0
RR
482 /* Architecture-specific MM context */
483 mm_context_t context;
c92ff1bd 484
c1a2f7f0 485 unsigned long flags; /* Must use atomic bitops to access */
c92ff1bd 486
c1a2f7f0 487 struct core_state *core_state; /* coredumping support */
227a4aad 488
858f0993 489#ifdef CONFIG_AIO
c1a2f7f0
RR
490 spinlock_t ioctx_lock;
491 struct kioctx_table __rcu *ioctx_table;
858f0993 492#endif
f98bafa0 493#ifdef CONFIG_MEMCG
c1a2f7f0
RR
494 /*
495 * "owner" points to a task that is regarded as the canonical
496 * user/owner of this mm. All of the following must be true in
497 * order for it to be changed:
498 *
499 * current == mm->owner
500 * current->mm != mm
501 * new_owner->mm == mm
502 * new_owner->alloc_lock is held
503 */
504 struct task_struct __rcu *owner;
78fb7466 505#endif
c1a2f7f0 506 struct user_namespace *user_ns;
925d1c40 507
c1a2f7f0
RR
508 /* store ref to file /proc/<pid>/exe symlink points to */
509 struct file __rcu *exe_file;
cddb8a5c 510#ifdef CONFIG_MMU_NOTIFIER
984cfe4e 511 struct mmu_notifier_subscriptions *notifier_subscriptions;
e7a00c45 512#endif
e009bb30 513#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
c1a2f7f0 514 pgtable_t pmd_huge_pte; /* protected by page_table_lock */
cbee9f88
PZ
515#endif
516#ifdef CONFIG_NUMA_BALANCING
c1a2f7f0
RR
517 /*
518 * numa_next_scan is the next time that the PTEs will be marked
519 * pte_numa. NUMA hinting faults will gather statistics and
520 * migrate pages to new nodes if necessary.
521 */
522 unsigned long numa_next_scan;
cbee9f88 523
c1a2f7f0
RR
524 /* Restart point for scanning and setting pte_numa */
525 unsigned long numa_scan_offset;
6e5fb223 526
c1a2f7f0
RR
527 /* numa_scan_seq prevents two threads setting pte_numa */
528 int numa_scan_seq;
20841405 529#endif
c1a2f7f0
RR
530 /*
531 * An operation with batched TLB flushing is going on. Anything
532 * that can move process memory needs to flush the TLB when
533 * moving a PROT_NONE or PROT_NUMA mapped page.
534 */
535 atomic_t tlb_flush_pending;
3ea27719 536#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
c1a2f7f0
RR
537 /* See flush_tlb_batched_pending() */
538 bool tlb_flush_batched;
6345d24d 539#endif
c1a2f7f0 540 struct uprobes_state uprobes_state;
5d317b2b 541#ifdef CONFIG_HUGETLB_PAGE
c1a2f7f0 542 atomic_long_t hugetlb_usage;
5d317b2b 543#endif
c1a2f7f0 544 struct work_struct async_put_work;
c1a2f7f0
RR
545 } __randomize_layout;
546
547 /*
548 * The mm_cpumask needs to be at the end of mm_struct, because it
549 * is dynamically sized based on nr_cpu_ids.
550 */
551 unsigned long cpu_bitmap[];
552};
c92ff1bd 553
abe722a1
IM
554extern struct mm_struct init_mm;
555
c1a2f7f0 556/* Pointer magic because the dynamic array size confuses some compilers. */
6345d24d
LT
557static inline void mm_init_cpumask(struct mm_struct *mm)
558{
c1a2f7f0
RR
559 unsigned long cpu_bitmap = (unsigned long)mm;
560
561 cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
562 cpumask_clear((struct cpumask *)cpu_bitmap);
6345d24d
LT
563}
564
45e575ab 565/* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
de03c72c
KM
566static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
567{
c1a2f7f0 568 return (struct cpumask *)&mm->cpu_bitmap;
de03c72c 569}
45e575ab 570
56236a59
MK
571struct mmu_gather;
572extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
573 unsigned long start, unsigned long end);
574extern void tlb_finish_mmu(struct mmu_gather *tlb,
575 unsigned long start, unsigned long end);
576
16af97dc 577static inline void init_tlb_flush_pending(struct mm_struct *mm)
20841405 578{
16af97dc 579 atomic_set(&mm->tlb_flush_pending, 0);
20841405 580}
16af97dc
NA
581
582static inline void inc_tlb_flush_pending(struct mm_struct *mm)
20841405 583{
16af97dc 584 atomic_inc(&mm->tlb_flush_pending);
af2c1401 585 /*
8b1b436d
PZ
586 * The only time this value is relevant is when there are indeed pages
587 * to flush. And we'll only flush pages after changing them, which
588 * requires the PTL.
589 *
590 * So the ordering here is:
591 *
040cca3a 592 * atomic_inc(&mm->tlb_flush_pending);
8b1b436d
PZ
593 * spin_lock(&ptl);
594 * ...
595 * set_pte_at();
596 * spin_unlock(&ptl);
597 *
598 * spin_lock(&ptl)
599 * mm_tlb_flush_pending();
600 * ....
601 * spin_unlock(&ptl);
602 *
603 * flush_tlb_range();
040cca3a 604 * atomic_dec(&mm->tlb_flush_pending);
8b1b436d 605 *
0e709703
PZ
606 * Where the increment if constrained by the PTL unlock, it thus
607 * ensures that the increment is visible if the PTE modification is
608 * visible. After all, if there is no PTE modification, nobody cares
609 * about TLB flushes either.
610 *
611 * This very much relies on users (mm_tlb_flush_pending() and
612 * mm_tlb_flush_nested()) only caring about _specific_ PTEs (and
613 * therefore specific PTLs), because with SPLIT_PTE_PTLOCKS and RCpc
614 * locks (PPC) the unlock of one doesn't order against the lock of
615 * another PTL.
616 *
617 * The decrement is ordered by the flush_tlb_range(), such that
618 * mm_tlb_flush_pending() will not return false unless all flushes have
619 * completed.
af2c1401 620 */
20841405 621}
16af97dc 622
16af97dc 623static inline void dec_tlb_flush_pending(struct mm_struct *mm)
20841405 624{
0a2c4048 625 /*
0e709703
PZ
626 * See inc_tlb_flush_pending().
627 *
628 * This cannot be smp_mb__before_atomic() because smp_mb() simply does
629 * not order against TLB invalidate completion, which is what we need.
630 *
631 * Therefore we must rely on tlb_flush_*() to guarantee order.
0a2c4048 632 */
16af97dc 633 atomic_dec(&mm->tlb_flush_pending);
20841405 634}
20841405 635
0e709703
PZ
636static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
637{
638 /*
639 * Must be called after having acquired the PTL; orders against that
640 * PTLs release and therefore ensures that if we observe the modified
641 * PTE we must also observe the increment from inc_tlb_flush_pending().
642 *
643 * That is, it only guarantees to return true if there is a flush
644 * pending for _this_ PTL.
645 */
646 return atomic_read(&mm->tlb_flush_pending);
647}
648
649static inline bool mm_tlb_flush_nested(struct mm_struct *mm)
650{
651 /*
652 * Similar to mm_tlb_flush_pending(), we must have acquired the PTL
653 * for which there is a TLB flush pending in order to guarantee
654 * we've seen both that PTE modification and the increment.
655 *
656 * (no requirement on actually still holding the PTL, that is irrelevant)
657 */
658 return atomic_read(&mm->tlb_flush_pending) > 1;
659}
660
f872f540
AL
661struct vm_fault;
662
3d353901
SJ
663/**
664 * typedef vm_fault_t - Return type for page fault handlers.
665 *
666 * Page fault handlers return a bitmask of %VM_FAULT values.
667 */
668typedef __bitwise unsigned int vm_fault_t;
669
670/**
671 * enum vm_fault_reason - Page fault handlers return a bitmask of
672 * these values to tell the core VM what happened when handling the
673 * fault. Used to decide whether a process gets delivered SIGBUS or
674 * just gets major/minor fault counters bumped up.
675 *
676 * @VM_FAULT_OOM: Out Of Memory
677 * @VM_FAULT_SIGBUS: Bad access
678 * @VM_FAULT_MAJOR: Page read from storage
679 * @VM_FAULT_WRITE: Special case for get_user_pages
680 * @VM_FAULT_HWPOISON: Hit poisoned small page
681 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded
682 * in upper bits
683 * @VM_FAULT_SIGSEGV: segmentation fault
684 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page
685 * @VM_FAULT_LOCKED: ->fault locked the returned page
686 * @VM_FAULT_RETRY: ->fault blocked, must retry
687 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small
688 * @VM_FAULT_DONE_COW: ->fault has fully handled COW
689 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs
690 * fsync() to complete (for synchronous page faults
691 * in DAX)
692 * @VM_FAULT_HINDEX_MASK: mask HINDEX value
693 *
694 */
695enum vm_fault_reason {
696 VM_FAULT_OOM = (__force vm_fault_t)0x000001,
697 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002,
698 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004,
699 VM_FAULT_WRITE = (__force vm_fault_t)0x000008,
700 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010,
701 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
702 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040,
703 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100,
704 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200,
705 VM_FAULT_RETRY = (__force vm_fault_t)0x000400,
706 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800,
707 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000,
708 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000,
709 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000,
710};
711
712/* Encode hstate index for a hwpoisoned large page */
713#define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
fcae96ff 714#define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
3d353901
SJ
715
716#define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \
717 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \
718 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
719
720#define VM_FAULT_RESULT_TRACE \
721 { VM_FAULT_OOM, "OOM" }, \
722 { VM_FAULT_SIGBUS, "SIGBUS" }, \
723 { VM_FAULT_MAJOR, "MAJOR" }, \
724 { VM_FAULT_WRITE, "WRITE" }, \
725 { VM_FAULT_HWPOISON, "HWPOISON" }, \
726 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \
727 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \
728 { VM_FAULT_NOPAGE, "NOPAGE" }, \
729 { VM_FAULT_LOCKED, "LOCKED" }, \
730 { VM_FAULT_RETRY, "RETRY" }, \
731 { VM_FAULT_FALLBACK, "FALLBACK" }, \
732 { VM_FAULT_DONE_COW, "DONE_COW" }, \
733 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }
734
f872f540
AL
735struct vm_special_mapping {
736 const char *name; /* The name, e.g. "[vdso]". */
737
738 /*
739 * If .fault is not provided, this points to a
740 * NULL-terminated array of pages that back the special mapping.
741 *
742 * This must not be NULL unless .fault is provided.
743 */
a62c34bd 744 struct page **pages;
f872f540
AL
745
746 /*
747 * If non-NULL, then this is called to resolve page faults
748 * on the special mapping. If used, .pages is not checked.
749 */
b3ec9f33
SJ
750 vm_fault_t (*fault)(const struct vm_special_mapping *sm,
751 struct vm_area_struct *vma,
752 struct vm_fault *vmf);
b059a453
DS
753
754 int (*mremap)(const struct vm_special_mapping *sm,
755 struct vm_area_struct *new_vma);
a62c34bd
AL
756};
757
d17d8f9d
DH
758enum tlb_flush_reason {
759 TLB_FLUSH_ON_TASK_SWITCH,
760 TLB_REMOTE_SHOOTDOWN,
761 TLB_LOCAL_SHOOTDOWN,
762 TLB_LOCAL_MM_SHOOTDOWN,
5b74283a 763 TLB_REMOTE_SEND_IPI,
d17d8f9d
DH
764 NR_TLB_FLUSH_REASONS,
765};
766
bd6dace7
TH
767 /*
768 * A swap entry has to fit into a "unsigned long", as the entry is hidden
769 * in the "index" field of the swapper address space.
770 */
771typedef struct {
772 unsigned long val;
773} swp_entry_t;
774
5b99cd0e 775#endif /* _LINUX_MM_TYPES_H */