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