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