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/maple_tree.h>
13 #include <linux/rwsem.h>
14 #include <linux/completion.h>
15 #include <linux/cpumask.h>
16 #include <linux/uprobes.h>
17 #include <linux/rcupdate.h>
18 #include <linux/page-flags-layout.h>
19 #include <linux/workqueue.h>
20 #include <linux/seqlock.h>
21 #include <linux/percpu_counter.h>
22
23 #include <asm/mmu.h>
24
25 #ifndef AT_VECTOR_SIZE_ARCH
26 #define AT_VECTOR_SIZE_ARCH 0
27 #endif
28 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
29
30 #define INIT_PASID 0
31
32 struct address_space;
33 struct mem_cgroup;
34
35 /*
36 * Each physical page in the system has a struct page associated with
37 * it to keep track of whatever it is we are using the page for at the
38 * moment. Note that we have no way to track which tasks are using
39 * a page, though if it is a pagecache page, rmap structures can tell us
40 * who is mapping it.
41 *
42 * If you allocate the page using alloc_pages(), you can use some of the
43 * space in struct page for your own purposes. The five words in the main
44 * union are available, except for bit 0 of the first word which must be
45 * kept clear. Many users use this word to store a pointer to an object
46 * which is guaranteed to be aligned. If you use the same storage as
47 * page->mapping, you must restore it to NULL before freeing the page.
48 *
49 * If your page will not be mapped to userspace, you can also use the four
50 * bytes in the mapcount union, but you must call page_mapcount_reset()
51 * before freeing it.
52 *
53 * If you want to use the refcount field, it must be used in such a way
54 * that other CPUs temporarily incrementing and then decrementing the
55 * refcount does not cause problems. On receiving the page from
56 * alloc_pages(), the refcount will be positive.
57 *
58 * If you allocate pages of order > 0, you can use some of the fields
59 * in each subpage, but you may need to restore some of their values
60 * afterwards.
61 *
62 * SLUB uses cmpxchg_double() to atomically update its freelist and counters.
63 * That requires that freelist & counters in struct slab be adjacent and
64 * double-word aligned. Because struct slab currently just reinterprets the
65 * bits of struct page, we align all struct pages to double-word boundaries,
66 * and ensure that 'freelist' is aligned within struct slab.
67 */
68 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
69 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
70 #else
71 #define _struct_page_alignment __aligned(sizeof(unsigned long))
72 #endif
73
74 struct page {
75 unsigned long flags; /* Atomic flags, some possibly
76 * updated asynchronously */
77 /*
78 * Five words (20/40 bytes) are available in this union.
79 * WARNING: bit 0 of the first word is used for PageTail(). That
80 * means the other users of this union MUST NOT use the bit to
81 * avoid collision and false-positive PageTail().
82 */
83 union {
84 struct { /* Page cache and anonymous pages */
85 /**
86 * @lru: Pageout list, eg. active_list protected by
87 * lruvec->lru_lock. Sometimes used as a generic list
88 * by the page owner.
89 */
90 union {
91 struct list_head lru;
92
93 /* Or, for the Unevictable "LRU list" slot */
94 struct {
95 /* Always even, to negate PageTail */
96 void *__filler;
97 /* Count page's or folio's mlocks */
98 unsigned int mlock_count;
99 };
100
101 /* Or, free page */
102 struct list_head buddy_list;
103 struct list_head pcp_list;
104 };
105 /* See page-flags.h for PAGE_MAPPING_FLAGS */
106 struct address_space *mapping;
107 union {
108 pgoff_t index; /* Our offset within mapping. */
109 unsigned long share; /* share count for fsdax */
110 };
111 /**
112 * @private: Mapping-private opaque data.
113 * Usually used for buffer_heads if PagePrivate.
114 * Used for swp_entry_t if PageSwapCache.
115 * Indicates order in the buddy system if PageBuddy.
116 */
117 unsigned long private;
118 };
119 struct { /* page_pool used by netstack */
120 /**
121 * @pp_magic: magic value to avoid recycling non
122 * page_pool allocated pages.
123 */
124 unsigned long pp_magic;
125 struct page_pool *pp;
126 unsigned long _pp_mapping_pad;
127 unsigned long dma_addr;
128 union {
129 /**
130 * dma_addr_upper: might require a 64-bit
131 * value on 32-bit architectures.
132 */
133 unsigned long dma_addr_upper;
134 /**
135 * For frag page support, not supported in
136 * 32-bit architectures with 64-bit DMA.
137 */
138 atomic_long_t pp_frag_count;
139 };
140 };
141 struct { /* Tail pages of compound page */
142 unsigned long compound_head; /* Bit zero is set */
143 };
144 struct { /* ZONE_DEVICE pages */
145 /** @pgmap: Points to the hosting device page map. */
146 struct dev_pagemap *pgmap;
147 void *zone_device_data;
148 /*
149 * ZONE_DEVICE private pages are counted as being
150 * mapped so the next 3 words hold the mapping, index,
151 * and private fields from the source anonymous or
152 * page cache page while the page is migrated to device
153 * private memory.
154 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
155 * use the mapping, index, and private fields when
156 * pmem backed DAX files are mapped.
157 */
158 };
159
160 /** @rcu_head: You can use this to free a page by RCU. */
161 struct rcu_head rcu_head;
162 };
163
164 union { /* This union is 4 bytes in size. */
165 /*
166 * If the page can be mapped to userspace, encodes the number
167 * of times this page is referenced by a page table.
168 */
169 atomic_t _mapcount;
170
171 /*
172 * If the page is neither PageSlab nor mappable to userspace,
173 * the value stored here may help determine what this page
174 * is used for. See page-flags.h for a list of page types
175 * which are currently stored here.
176 */
177 unsigned int page_type;
178 };
179
180 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
181 atomic_t _refcount;
182
183 #ifdef CONFIG_MEMCG
184 unsigned long memcg_data;
185 #endif
186
187 /*
188 * On machines where all RAM is mapped into kernel address space,
189 * we can simply calculate the virtual address. On machines with
190 * highmem some memory is mapped into kernel virtual memory
191 * dynamically, so we need a place to store that address.
192 * Note that this field could be 16 bits on x86 ... ;)
193 *
194 * Architectures with slow multiplication can define
195 * WANT_PAGE_VIRTUAL in asm/page.h
196 */
197 #if defined(WANT_PAGE_VIRTUAL)
198 void *virtual; /* Kernel virtual address (NULL if
199 not kmapped, ie. highmem) */
200 #endif /* WANT_PAGE_VIRTUAL */
201
202 #ifdef CONFIG_KMSAN
203 /*
204 * KMSAN metadata for this page:
205 * - shadow page: every bit indicates whether the corresponding
206 * bit of the original page is initialized (0) or not (1);
207 * - origin page: every 4 bytes contain an id of the stack trace
208 * where the uninitialized value was created.
209 */
210 struct page *kmsan_shadow;
211 struct page *kmsan_origin;
212 #endif
213
214 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
215 int _last_cpupid;
216 #endif
217 } _struct_page_alignment;
218
219 /*
220 * struct encoded_page - a nonexistent type marking this pointer
221 *
222 * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
223 * with the low bits of the pointer indicating extra context-dependent
224 * information. Not super-common, but happens in mmu_gather and mlock
225 * handling, and this acts as a type system check on that use.
226 *
227 * We only really have two guaranteed bits in general, although you could
228 * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
229 * for more.
230 *
231 * Use the supplied helper functions to endcode/decode the pointer and bits.
232 */
233 struct encoded_page;
234 #define ENCODE_PAGE_BITS 3ul
encode_page(struct page * page,unsigned long flags)235 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
236 {
237 BUILD_BUG_ON(flags > ENCODE_PAGE_BITS);
238 return (struct encoded_page *)(flags | (unsigned long)page);
239 }
240
encoded_page_flags(struct encoded_page * page)241 static inline unsigned long encoded_page_flags(struct encoded_page *page)
242 {
243 return ENCODE_PAGE_BITS & (unsigned long)page;
244 }
245
encoded_page_ptr(struct encoded_page * page)246 static inline struct page *encoded_page_ptr(struct encoded_page *page)
247 {
248 return (struct page *)(~ENCODE_PAGE_BITS & (unsigned long)page);
249 }
250
251 /*
252 * A swap entry has to fit into a "unsigned long", as the entry is hidden
253 * in the "index" field of the swapper address space.
254 */
255 typedef struct {
256 unsigned long val;
257 } swp_entry_t;
258
259 /**
260 * struct folio - Represents a contiguous set of bytes.
261 * @flags: Identical to the page flags.
262 * @lru: Least Recently Used list; tracks how recently this folio was used.
263 * @mlock_count: Number of times this folio has been pinned by mlock().
264 * @mapping: The file this page belongs to, or refers to the anon_vma for
265 * anonymous memory.
266 * @index: Offset within the file, in units of pages. For anonymous memory,
267 * this is the index from the beginning of the mmap.
268 * @private: Filesystem per-folio data (see folio_attach_private()).
269 * @swap: Used for swp_entry_t if folio_test_swapcache().
270 * @_mapcount: Do not access this member directly. Use folio_mapcount() to
271 * find out how many times this folio is mapped by userspace.
272 * @_refcount: Do not access this member directly. Use folio_ref_count()
273 * to find how many references there are to this folio.
274 * @memcg_data: Memory Control Group data.
275 * @_entire_mapcount: Do not use directly, call folio_entire_mapcount().
276 * @_nr_pages_mapped: Do not use directly, call folio_mapcount().
277 * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
278 * @_folio_nr_pages: Do not use directly, call folio_nr_pages().
279 * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
280 * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
281 * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
282 * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
283 * @_deferred_list: Folios to be split under memory pressure.
284 *
285 * A folio is a physically, virtually and logically contiguous set
286 * of bytes. It is a power-of-two in size, and it is aligned to that
287 * same power-of-two. It is at least as large as %PAGE_SIZE. If it is
288 * in the page cache, it is at a file offset which is a multiple of that
289 * power-of-two. It may be mapped into userspace at an address which is
290 * at an arbitrary page offset, but its kernel virtual address is aligned
291 * to its size.
292 */
293 struct folio {
294 /* private: don't document the anon union */
295 union {
296 struct {
297 /* public: */
298 unsigned long flags;
299 union {
300 struct list_head lru;
301 /* private: avoid cluttering the output */
302 struct {
303 void *__filler;
304 /* public: */
305 unsigned int mlock_count;
306 /* private: */
307 };
308 /* public: */
309 };
310 struct address_space *mapping;
311 pgoff_t index;
312 union {
313 void *private;
314 swp_entry_t swap;
315 };
316 atomic_t _mapcount;
317 atomic_t _refcount;
318 #ifdef CONFIG_MEMCG
319 unsigned long memcg_data;
320 #endif
321 /* private: the union with struct page is transitional */
322 };
323 struct page page;
324 };
325 union {
326 struct {
327 unsigned long _flags_1;
328 unsigned long _head_1;
329 unsigned long _folio_avail;
330 /* public: */
331 atomic_t _entire_mapcount;
332 atomic_t _nr_pages_mapped;
333 atomic_t _pincount;
334 #ifdef CONFIG_64BIT
335 unsigned int _folio_nr_pages;
336 #endif
337 /* private: the union with struct page is transitional */
338 };
339 struct page __page_1;
340 };
341 union {
342 struct {
343 unsigned long _flags_2;
344 unsigned long _head_2;
345 /* public: */
346 void *_hugetlb_subpool;
347 void *_hugetlb_cgroup;
348 void *_hugetlb_cgroup_rsvd;
349 void *_hugetlb_hwpoison;
350 /* private: the union with struct page is transitional */
351 };
352 struct {
353 unsigned long _flags_2a;
354 unsigned long _head_2a;
355 /* public: */
356 struct list_head _deferred_list;
357 /* private: the union with struct page is transitional */
358 };
359 struct page __page_2;
360 };
361 };
362
363 #define FOLIO_MATCH(pg, fl) \
364 static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
365 FOLIO_MATCH(flags, flags);
366 FOLIO_MATCH(lru, lru);
367 FOLIO_MATCH(mapping, mapping);
368 FOLIO_MATCH(compound_head, lru);
369 FOLIO_MATCH(index, index);
370 FOLIO_MATCH(private, private);
371 FOLIO_MATCH(_mapcount, _mapcount);
372 FOLIO_MATCH(_refcount, _refcount);
373 #ifdef CONFIG_MEMCG
374 FOLIO_MATCH(memcg_data, memcg_data);
375 #endif
376 #undef FOLIO_MATCH
377 #define FOLIO_MATCH(pg, fl) \
378 static_assert(offsetof(struct folio, fl) == \
379 offsetof(struct page, pg) + sizeof(struct page))
380 FOLIO_MATCH(flags, _flags_1);
381 FOLIO_MATCH(compound_head, _head_1);
382 #undef FOLIO_MATCH
383 #define FOLIO_MATCH(pg, fl) \
384 static_assert(offsetof(struct folio, fl) == \
385 offsetof(struct page, pg) + 2 * sizeof(struct page))
386 FOLIO_MATCH(flags, _flags_2);
387 FOLIO_MATCH(compound_head, _head_2);
388 FOLIO_MATCH(flags, _flags_2a);
389 FOLIO_MATCH(compound_head, _head_2a);
390 #undef FOLIO_MATCH
391
392 /**
393 * struct ptdesc - Memory descriptor for page tables.
394 * @__page_flags: Same as page flags. Unused for page tables.
395 * @pt_rcu_head: For freeing page table pages.
396 * @pt_list: List of used page tables. Used for s390 and x86.
397 * @_pt_pad_1: Padding that aliases with page's compound head.
398 * @pmd_huge_pte: Protected by ptdesc->ptl, used for THPs.
399 * @__page_mapping: Aliases with page->mapping. Unused for page tables.
400 * @pt_mm: Used for x86 pgds.
401 * @pt_frag_refcount: For fragmented page table tracking. Powerpc only.
402 * @pt_share_count: Used for HugeTLB PMD page table share count.
403 * @_pt_pad_2: Padding to ensure proper alignment.
404 * @ptl: Lock for the page table.
405 * @__page_type: Same as page->page_type. Unused for page tables.
406 * @_refcount: Same as page refcount.
407 * @pt_memcg_data: Memcg data. Tracked for page tables here.
408 *
409 * This struct overlays struct page for now. Do not modify without a good
410 * understanding of the issues.
411 */
412 struct ptdesc {
413 unsigned long __page_flags;
414
415 union {
416 struct rcu_head pt_rcu_head;
417 struct list_head pt_list;
418 struct {
419 unsigned long _pt_pad_1;
420 pgtable_t pmd_huge_pte;
421 };
422 };
423 unsigned long __page_mapping;
424
425 union {
426 struct mm_struct *pt_mm;
427 atomic_t pt_frag_refcount;
428 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
429 atomic_t pt_share_count;
430 #endif
431 };
432
433 union {
434 unsigned long _pt_pad_2;
435 #if ALLOC_SPLIT_PTLOCKS
436 spinlock_t *ptl;
437 #else
438 spinlock_t ptl;
439 #endif
440 };
441 unsigned int __page_type;
442 atomic_t _refcount;
443 #ifdef CONFIG_MEMCG
444 unsigned long pt_memcg_data;
445 #endif
446 };
447
448 #define TABLE_MATCH(pg, pt) \
449 static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt))
450 TABLE_MATCH(flags, __page_flags);
451 TABLE_MATCH(compound_head, pt_list);
452 TABLE_MATCH(compound_head, _pt_pad_1);
453 TABLE_MATCH(mapping, __page_mapping);
454 TABLE_MATCH(rcu_head, pt_rcu_head);
455 TABLE_MATCH(page_type, __page_type);
456 TABLE_MATCH(_refcount, _refcount);
457 #ifdef CONFIG_MEMCG
458 TABLE_MATCH(memcg_data, pt_memcg_data);
459 #endif
460 #undef TABLE_MATCH
461 static_assert(sizeof(struct ptdesc) <= sizeof(struct page));
462
463 #define ptdesc_page(pt) (_Generic((pt), \
464 const struct ptdesc *: (const struct page *)(pt), \
465 struct ptdesc *: (struct page *)(pt)))
466
467 #define ptdesc_folio(pt) (_Generic((pt), \
468 const struct ptdesc *: (const struct folio *)(pt), \
469 struct ptdesc *: (struct folio *)(pt)))
470
471 #define page_ptdesc(p) (_Generic((p), \
472 const struct page *: (const struct ptdesc *)(p), \
473 struct page *: (struct ptdesc *)(p)))
474
475 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
ptdesc_pmd_pts_init(struct ptdesc * ptdesc)476 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
477 {
478 atomic_set(&ptdesc->pt_share_count, 0);
479 }
480
ptdesc_pmd_pts_inc(struct ptdesc * ptdesc)481 static inline void ptdesc_pmd_pts_inc(struct ptdesc *ptdesc)
482 {
483 atomic_inc(&ptdesc->pt_share_count);
484 }
485
ptdesc_pmd_pts_dec(struct ptdesc * ptdesc)486 static inline void ptdesc_pmd_pts_dec(struct ptdesc *ptdesc)
487 {
488 atomic_dec(&ptdesc->pt_share_count);
489 }
490
ptdesc_pmd_pts_count(struct ptdesc * ptdesc)491 static inline int ptdesc_pmd_pts_count(struct ptdesc *ptdesc)
492 {
493 return atomic_read(&ptdesc->pt_share_count);
494 }
495 #else
ptdesc_pmd_pts_init(struct ptdesc * ptdesc)496 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
497 {
498 }
499 #endif
500
501 /*
502 * Used for sizing the vmemmap region on some architectures
503 */
504 #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page)))
505
506 #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK)
507 #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE)
508
509 /*
510 * page_private can be used on tail pages. However, PagePrivate is only
511 * checked by the VM on the head page. So page_private on the tail pages
512 * should be used for data that's ancillary to the head page (eg attaching
513 * buffer heads to tail pages after attaching buffer heads to the head page)
514 */
515 #define page_private(page) ((page)->private)
516
set_page_private(struct page * page,unsigned long private)517 static inline void set_page_private(struct page *page, unsigned long private)
518 {
519 page->private = private;
520 }
521
folio_get_private(struct folio * folio)522 static inline void *folio_get_private(struct folio *folio)
523 {
524 return folio->private;
525 }
526
527 struct page_frag_cache {
528 void * va;
529 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
530 __u16 offset;
531 __u16 size;
532 #else
533 __u32 offset;
534 #endif
535 /* we maintain a pagecount bias, so that we dont dirty cache line
536 * containing page->_refcount every time we allocate a fragment.
537 */
538 unsigned int pagecnt_bias;
539 bool pfmemalloc;
540 };
541
542 typedef unsigned long vm_flags_t;
543
544 /*
545 * A region containing a mapping of a non-memory backed file under NOMMU
546 * conditions. These are held in a global tree and are pinned by the VMAs that
547 * map parts of them.
548 */
549 struct vm_region {
550 struct rb_node vm_rb; /* link in global region tree */
551 vm_flags_t vm_flags; /* VMA vm_flags */
552 unsigned long vm_start; /* start address of region */
553 unsigned long vm_end; /* region initialised to here */
554 unsigned long vm_top; /* region allocated to here */
555 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
556 struct file *vm_file; /* the backing file or NULL */
557
558 int vm_usage; /* region usage count (access under nommu_region_sem) */
559 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
560 * this region */
561 };
562
563 #ifdef CONFIG_USERFAULTFD
564 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
565 struct vm_userfaultfd_ctx {
566 struct userfaultfd_ctx *ctx;
567 };
568 #else /* CONFIG_USERFAULTFD */
569 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
570 struct vm_userfaultfd_ctx {};
571 #endif /* CONFIG_USERFAULTFD */
572
573 struct anon_vma_name {
574 struct kref kref;
575 /* The name needs to be at the end because it is dynamically sized. */
576 char name[];
577 };
578
579 struct vma_lock {
580 struct rw_semaphore lock;
581 };
582
583 struct vma_numab_state {
584 /*
585 * Initialised as time in 'jiffies' after which VMA
586 * should be scanned. Delays first scan of new VMA by at
587 * least sysctl_numa_balancing_scan_delay:
588 */
589 unsigned long next_scan;
590
591 /*
592 * Time in jiffies when pids_active[] is reset to
593 * detect phase change behaviour:
594 */
595 unsigned long pids_active_reset;
596
597 /*
598 * Approximate tracking of PIDs that trapped a NUMA hinting
599 * fault. May produce false positives due to hash collisions.
600 *
601 * [0] Previous PID tracking
602 * [1] Current PID tracking
603 *
604 * Window moves after next_pid_reset has expired approximately
605 * every VMA_PID_RESET_PERIOD jiffies:
606 */
607 unsigned long pids_active[2];
608
609 /* MM scan sequence ID when scan first started after VMA creation */
610 int start_scan_seq;
611
612 /*
613 * MM scan sequence ID when the VMA was last completely scanned.
614 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq
615 */
616 int prev_scan_seq;
617 };
618
619 /*
620 * This struct describes a virtual memory area. There is one of these
621 * per VM-area/task. A VM area is any part of the process virtual memory
622 * space that has a special rule for the page-fault handlers (ie a shared
623 * library, the executable area etc).
624 */
625 struct vm_area_struct {
626 /* The first cache line has the info for VMA tree walking. */
627
628 union {
629 struct {
630 /* VMA covers [vm_start; vm_end) addresses within mm */
631 unsigned long vm_start;
632 unsigned long vm_end;
633 };
634 #ifdef CONFIG_PER_VMA_LOCK
635 struct rcu_head vm_rcu; /* Used for deferred freeing. */
636 #endif
637 };
638
639 struct mm_struct *vm_mm; /* The address space we belong to. */
640 pgprot_t vm_page_prot; /* Access permissions of this VMA. */
641
642 /*
643 * Flags, see mm.h.
644 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
645 */
646 union {
647 const vm_flags_t vm_flags;
648 vm_flags_t __private __vm_flags;
649 };
650
651 #ifdef CONFIG_PER_VMA_LOCK
652 /*
653 * Can only be written (using WRITE_ONCE()) while holding both:
654 * - mmap_lock (in write mode)
655 * - vm_lock->lock (in write mode)
656 * Can be read reliably while holding one of:
657 * - mmap_lock (in read or write mode)
658 * - vm_lock->lock (in read or write mode)
659 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
660 * while holding nothing (except RCU to keep the VMA struct allocated).
661 *
662 * This sequence counter is explicitly allowed to overflow; sequence
663 * counter reuse can only lead to occasional unnecessary use of the
664 * slowpath.
665 */
666 int vm_lock_seq;
667 struct vma_lock *vm_lock;
668
669 /* Flag to indicate areas detached from the mm->mm_mt tree */
670 bool detached;
671 #endif
672
673 /*
674 * For areas with an address space and backing store,
675 * linkage into the address_space->i_mmap interval tree.
676 *
677 */
678 struct {
679 struct rb_node rb;
680 unsigned long rb_subtree_last;
681 } shared;
682
683 /*
684 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
685 * list, after a COW of one of the file pages. A MAP_SHARED vma
686 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
687 * or brk vma (with NULL file) can only be in an anon_vma list.
688 */
689 struct list_head anon_vma_chain; /* Serialized by mmap_lock &
690 * page_table_lock */
691 struct anon_vma *anon_vma; /* Serialized by page_table_lock */
692
693 /* Function pointers to deal with this struct. */
694 const struct vm_operations_struct *vm_ops;
695
696 /* Information about our backing store: */
697 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
698 units */
699 struct file * vm_file; /* File we map to (can be NULL). */
700 void * vm_private_data; /* was vm_pte (shared mem) */
701
702 #ifdef CONFIG_ANON_VMA_NAME
703 /*
704 * For private and shared anonymous mappings, a pointer to a null
705 * terminated string containing the name given to the vma, or NULL if
706 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
707 */
708 struct anon_vma_name *anon_name;
709 #endif
710 #ifdef CONFIG_SWAP
711 atomic_long_t swap_readahead_info;
712 #endif
713 #ifndef CONFIG_MMU
714 struct vm_region *vm_region; /* NOMMU mapping region */
715 #endif
716 #ifdef CONFIG_NUMA
717 struct mempolicy *vm_policy; /* NUMA policy for the VMA */
718 #endif
719 #ifdef CONFIG_NUMA_BALANCING
720 struct vma_numab_state *numab_state; /* NUMA Balancing state */
721 #endif
722 struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
723 } __randomize_layout;
724
725 #ifdef CONFIG_SCHED_MM_CID
726 struct mm_cid {
727 u64 time;
728 int cid;
729 };
730 #endif
731
732 struct kioctx_table;
733 struct mm_struct {
734 struct {
735 /*
736 * Fields which are often written to are placed in a separate
737 * cache line.
738 */
739 struct {
740 /**
741 * @mm_count: The number of references to &struct
742 * mm_struct (@mm_users count as 1).
743 *
744 * Use mmgrab()/mmdrop() to modify. When this drops to
745 * 0, the &struct mm_struct is freed.
746 */
747 atomic_t mm_count;
748 } ____cacheline_aligned_in_smp;
749
750 struct maple_tree mm_mt;
751 #ifdef CONFIG_MMU
752 unsigned long (*get_unmapped_area) (struct file *filp,
753 unsigned long addr, unsigned long len,
754 unsigned long pgoff, unsigned long flags);
755 #endif
756 unsigned long mmap_base; /* base of mmap area */
757 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
758 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
759 /* Base addresses for compatible mmap() */
760 unsigned long mmap_compat_base;
761 unsigned long mmap_compat_legacy_base;
762 #endif
763 unsigned long task_size; /* size of task vm space */
764 pgd_t * pgd;
765
766 #ifdef CONFIG_MEMBARRIER
767 /**
768 * @membarrier_state: Flags controlling membarrier behavior.
769 *
770 * This field is close to @pgd to hopefully fit in the same
771 * cache-line, which needs to be touched by switch_mm().
772 */
773 atomic_t membarrier_state;
774 #endif
775
776 /**
777 * @mm_users: The number of users including userspace.
778 *
779 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
780 * drops to 0 (i.e. when the task exits and there are no other
781 * temporary reference holders), we also release a reference on
782 * @mm_count (which may then free the &struct mm_struct if
783 * @mm_count also drops to 0).
784 */
785 atomic_t mm_users;
786
787 #ifdef CONFIG_SCHED_MM_CID
788 /**
789 * @pcpu_cid: Per-cpu current cid.
790 *
791 * Keep track of the currently allocated mm_cid for each cpu.
792 * The per-cpu mm_cid values are serialized by their respective
793 * runqueue locks.
794 */
795 struct mm_cid __percpu *pcpu_cid;
796 /*
797 * @mm_cid_next_scan: Next mm_cid scan (in jiffies).
798 *
799 * When the next mm_cid scan is due (in jiffies).
800 */
801 unsigned long mm_cid_next_scan;
802 #endif
803 #ifdef CONFIG_MMU
804 atomic_long_t pgtables_bytes; /* size of all page tables */
805 #endif
806 int map_count; /* number of VMAs */
807
808 spinlock_t page_table_lock; /* Protects page tables and some
809 * counters
810 */
811 /*
812 * With some kernel config, the current mmap_lock's offset
813 * inside 'mm_struct' is at 0x120, which is very optimal, as
814 * its two hot fields 'count' and 'owner' sit in 2 different
815 * cachelines, and when mmap_lock is highly contended, both
816 * of the 2 fields will be accessed frequently, current layout
817 * will help to reduce cache bouncing.
818 *
819 * So please be careful with adding new fields before
820 * mmap_lock, which can easily push the 2 fields into one
821 * cacheline.
822 */
823 struct rw_semaphore mmap_lock;
824
825 struct list_head mmlist; /* List of maybe swapped mm's. These
826 * are globally strung together off
827 * init_mm.mmlist, and are protected
828 * by mmlist_lock
829 */
830 #ifdef CONFIG_PER_VMA_LOCK
831 /*
832 * This field has lock-like semantics, meaning it is sometimes
833 * accessed with ACQUIRE/RELEASE semantics.
834 * Roughly speaking, incrementing the sequence number is
835 * equivalent to releasing locks on VMAs; reading the sequence
836 * number can be part of taking a read lock on a VMA.
837 *
838 * Can be modified under write mmap_lock using RELEASE
839 * semantics.
840 * Can be read with no other protection when holding write
841 * mmap_lock.
842 * Can be read with ACQUIRE semantics if not holding write
843 * mmap_lock.
844 */
845 int mm_lock_seq;
846 #endif
847
848
849 unsigned long hiwater_rss; /* High-watermark of RSS usage */
850 unsigned long hiwater_vm; /* High-water virtual memory usage */
851
852 unsigned long total_vm; /* Total pages mapped */
853 unsigned long locked_vm; /* Pages that have PG_mlocked set */
854 atomic64_t pinned_vm; /* Refcount permanently increased */
855 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
856 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
857 unsigned long stack_vm; /* VM_STACK */
858 unsigned long def_flags;
859
860 /**
861 * @write_protect_seq: Locked when any thread is write
862 * protecting pages mapped by this mm to enforce a later COW,
863 * for instance during page table copying for fork().
864 */
865 seqcount_t write_protect_seq;
866
867 spinlock_t arg_lock; /* protect the below fields */
868
869 unsigned long start_code, end_code, start_data, end_data;
870 unsigned long start_brk, brk, start_stack;
871 unsigned long arg_start, arg_end, env_start, env_end;
872
873 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
874
875 struct percpu_counter rss_stat[NR_MM_COUNTERS];
876
877 struct linux_binfmt *binfmt;
878
879 /* Architecture-specific MM context */
880 mm_context_t context;
881
882 unsigned long flags; /* Must use atomic bitops to access */
883
884 #ifdef CONFIG_AIO
885 spinlock_t ioctx_lock;
886 struct kioctx_table __rcu *ioctx_table;
887 #endif
888 #ifdef CONFIG_MEMCG
889 /*
890 * "owner" points to a task that is regarded as the canonical
891 * user/owner of this mm. All of the following must be true in
892 * order for it to be changed:
893 *
894 * current == mm->owner
895 * current->mm != mm
896 * new_owner->mm == mm
897 * new_owner->alloc_lock is held
898 */
899 struct task_struct __rcu *owner;
900 #endif
901 struct user_namespace *user_ns;
902
903 /* store ref to file /proc/<pid>/exe symlink points to */
904 struct file __rcu *exe_file;
905 #ifdef CONFIG_MMU_NOTIFIER
906 struct mmu_notifier_subscriptions *notifier_subscriptions;
907 #endif
908 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
909 pgtable_t pmd_huge_pte; /* protected by page_table_lock */
910 #endif
911 #ifdef CONFIG_NUMA_BALANCING
912 /*
913 * numa_next_scan is the next time that PTEs will be remapped
914 * PROT_NONE to trigger NUMA hinting faults; such faults gather
915 * statistics and migrate pages to new nodes if necessary.
916 */
917 unsigned long numa_next_scan;
918
919 /* Restart point for scanning and remapping PTEs. */
920 unsigned long numa_scan_offset;
921
922 /* numa_scan_seq prevents two threads remapping PTEs. */
923 int numa_scan_seq;
924 #endif
925 /*
926 * An operation with batched TLB flushing is going on. Anything
927 * that can move process memory needs to flush the TLB when
928 * moving a PROT_NONE mapped page.
929 */
930 atomic_t tlb_flush_pending;
931 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
932 /* See flush_tlb_batched_pending() */
933 atomic_t tlb_flush_batched;
934 #endif
935 struct uprobes_state uprobes_state;
936 #ifdef CONFIG_PREEMPT_RT
937 struct rcu_head delayed_drop;
938 #endif
939 #ifdef CONFIG_HUGETLB_PAGE
940 atomic_long_t hugetlb_usage;
941 #endif
942 struct work_struct async_put_work;
943
944 #ifdef CONFIG_IOMMU_SVA
945 u32 pasid;
946 #endif
947 #ifdef CONFIG_KSM
948 /*
949 * Represent how many pages of this process are involved in KSM
950 * merging (not including ksm_zero_pages).
951 */
952 unsigned long ksm_merging_pages;
953 /*
954 * Represent how many pages are checked for ksm merging
955 * including merged and not merged.
956 */
957 unsigned long ksm_rmap_items;
958 /*
959 * Represent how many empty pages are merged with kernel zero
960 * pages when enabling KSM use_zero_pages.
961 */
962 atomic_long_t ksm_zero_pages;
963 #endif /* CONFIG_KSM */
964 #ifdef CONFIG_LRU_GEN
965 struct {
966 /* this mm_struct is on lru_gen_mm_list */
967 struct list_head list;
968 /*
969 * Set when switching to this mm_struct, as a hint of
970 * whether it has been used since the last time per-node
971 * page table walkers cleared the corresponding bits.
972 */
973 unsigned long bitmap;
974 #ifdef CONFIG_MEMCG
975 /* points to the memcg of "owner" above */
976 struct mem_cgroup *memcg;
977 #endif
978 } lru_gen;
979 #endif /* CONFIG_LRU_GEN */
980 } __randomize_layout;
981
982 /*
983 * The mm_cpumask needs to be at the end of mm_struct, because it
984 * is dynamically sized based on nr_cpu_ids.
985 */
986 unsigned long cpu_bitmap[];
987 };
988
989 #define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
990 MT_FLAGS_USE_RCU)
991 extern struct mm_struct init_mm;
992
993 /* Pointer magic because the dynamic array size confuses some compilers. */
mm_init_cpumask(struct mm_struct * mm)994 static inline void mm_init_cpumask(struct mm_struct *mm)
995 {
996 unsigned long cpu_bitmap = (unsigned long)mm;
997
998 cpu_bitmap += offsetof(struct mm_struct, cpu_bitmap);
999 cpumask_clear((struct cpumask *)cpu_bitmap);
1000 }
1001
1002 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
mm_cpumask(struct mm_struct * mm)1003 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
1004 {
1005 return (struct cpumask *)&mm->cpu_bitmap;
1006 }
1007
1008 #ifdef CONFIG_LRU_GEN
1009
1010 struct lru_gen_mm_list {
1011 /* mm_struct list for page table walkers */
1012 struct list_head fifo;
1013 /* protects the list above */
1014 spinlock_t lock;
1015 };
1016
1017 void lru_gen_add_mm(struct mm_struct *mm);
1018 void lru_gen_del_mm(struct mm_struct *mm);
1019 #ifdef CONFIG_MEMCG
1020 void lru_gen_migrate_mm(struct mm_struct *mm);
1021 #endif
1022
lru_gen_init_mm(struct mm_struct * mm)1023 static inline void lru_gen_init_mm(struct mm_struct *mm)
1024 {
1025 INIT_LIST_HEAD(&mm->lru_gen.list);
1026 mm->lru_gen.bitmap = 0;
1027 #ifdef CONFIG_MEMCG
1028 mm->lru_gen.memcg = NULL;
1029 #endif
1030 }
1031
lru_gen_use_mm(struct mm_struct * mm)1032 static inline void lru_gen_use_mm(struct mm_struct *mm)
1033 {
1034 /*
1035 * When the bitmap is set, page reclaim knows this mm_struct has been
1036 * used since the last time it cleared the bitmap. So it might be worth
1037 * walking the page tables of this mm_struct to clear the accessed bit.
1038 */
1039 WRITE_ONCE(mm->lru_gen.bitmap, -1);
1040 }
1041
1042 #else /* !CONFIG_LRU_GEN */
1043
lru_gen_add_mm(struct mm_struct * mm)1044 static inline void lru_gen_add_mm(struct mm_struct *mm)
1045 {
1046 }
1047
lru_gen_del_mm(struct mm_struct * mm)1048 static inline void lru_gen_del_mm(struct mm_struct *mm)
1049 {
1050 }
1051
1052 #ifdef CONFIG_MEMCG
lru_gen_migrate_mm(struct mm_struct * mm)1053 static inline void lru_gen_migrate_mm(struct mm_struct *mm)
1054 {
1055 }
1056 #endif
1057
lru_gen_init_mm(struct mm_struct * mm)1058 static inline void lru_gen_init_mm(struct mm_struct *mm)
1059 {
1060 }
1061
lru_gen_use_mm(struct mm_struct * mm)1062 static inline void lru_gen_use_mm(struct mm_struct *mm)
1063 {
1064 }
1065
1066 #endif /* CONFIG_LRU_GEN */
1067
1068 struct vma_iterator {
1069 struct ma_state mas;
1070 };
1071
1072 #define VMA_ITERATOR(name, __mm, __addr) \
1073 struct vma_iterator name = { \
1074 .mas = { \
1075 .tree = &(__mm)->mm_mt, \
1076 .index = __addr, \
1077 .node = MAS_START, \
1078 }, \
1079 }
1080
vma_iter_init(struct vma_iterator * vmi,struct mm_struct * mm,unsigned long addr)1081 static inline void vma_iter_init(struct vma_iterator *vmi,
1082 struct mm_struct *mm, unsigned long addr)
1083 {
1084 mas_init(&vmi->mas, &mm->mm_mt, addr);
1085 }
1086
1087 #ifdef CONFIG_SCHED_MM_CID
1088
1089 enum mm_cid_state {
1090 MM_CID_UNSET = -1U, /* Unset state has lazy_put flag set. */
1091 MM_CID_LAZY_PUT = (1U << 31),
1092 };
1093
mm_cid_is_unset(int cid)1094 static inline bool mm_cid_is_unset(int cid)
1095 {
1096 return cid == MM_CID_UNSET;
1097 }
1098
mm_cid_is_lazy_put(int cid)1099 static inline bool mm_cid_is_lazy_put(int cid)
1100 {
1101 return !mm_cid_is_unset(cid) && (cid & MM_CID_LAZY_PUT);
1102 }
1103
mm_cid_is_valid(int cid)1104 static inline bool mm_cid_is_valid(int cid)
1105 {
1106 return !(cid & MM_CID_LAZY_PUT);
1107 }
1108
mm_cid_set_lazy_put(int cid)1109 static inline int mm_cid_set_lazy_put(int cid)
1110 {
1111 return cid | MM_CID_LAZY_PUT;
1112 }
1113
mm_cid_clear_lazy_put(int cid)1114 static inline int mm_cid_clear_lazy_put(int cid)
1115 {
1116 return cid & ~MM_CID_LAZY_PUT;
1117 }
1118
1119 /* Accessor for struct mm_struct's cidmask. */
mm_cidmask(struct mm_struct * mm)1120 static inline cpumask_t *mm_cidmask(struct mm_struct *mm)
1121 {
1122 unsigned long cid_bitmap = (unsigned long)mm;
1123
1124 cid_bitmap += offsetof(struct mm_struct, cpu_bitmap);
1125 /* Skip cpu_bitmap */
1126 cid_bitmap += cpumask_size();
1127 return (struct cpumask *)cid_bitmap;
1128 }
1129
mm_init_cid(struct mm_struct * mm)1130 static inline void mm_init_cid(struct mm_struct *mm)
1131 {
1132 int i;
1133
1134 for_each_possible_cpu(i) {
1135 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, i);
1136
1137 pcpu_cid->cid = MM_CID_UNSET;
1138 pcpu_cid->time = 0;
1139 }
1140 cpumask_clear(mm_cidmask(mm));
1141 }
1142
mm_alloc_cid(struct mm_struct * mm)1143 static inline int mm_alloc_cid(struct mm_struct *mm)
1144 {
1145 mm->pcpu_cid = alloc_percpu(struct mm_cid);
1146 if (!mm->pcpu_cid)
1147 return -ENOMEM;
1148 mm_init_cid(mm);
1149 return 0;
1150 }
1151
mm_destroy_cid(struct mm_struct * mm)1152 static inline void mm_destroy_cid(struct mm_struct *mm)
1153 {
1154 free_percpu(mm->pcpu_cid);
1155 mm->pcpu_cid = NULL;
1156 }
1157
mm_cid_size(void)1158 static inline unsigned int mm_cid_size(void)
1159 {
1160 return cpumask_size();
1161 }
1162 #else /* CONFIG_SCHED_MM_CID */
mm_init_cid(struct mm_struct * mm)1163 static inline void mm_init_cid(struct mm_struct *mm) { }
mm_alloc_cid(struct mm_struct * mm)1164 static inline int mm_alloc_cid(struct mm_struct *mm) { return 0; }
mm_destroy_cid(struct mm_struct * mm)1165 static inline void mm_destroy_cid(struct mm_struct *mm) { }
mm_cid_size(void)1166 static inline unsigned int mm_cid_size(void)
1167 {
1168 return 0;
1169 }
1170 #endif /* CONFIG_SCHED_MM_CID */
1171
1172 struct mmu_gather;
1173 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
1174 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
1175 extern void tlb_finish_mmu(struct mmu_gather *tlb);
1176
1177 struct vm_fault;
1178
1179 /**
1180 * typedef vm_fault_t - Return type for page fault handlers.
1181 *
1182 * Page fault handlers return a bitmask of %VM_FAULT values.
1183 */
1184 typedef __bitwise unsigned int vm_fault_t;
1185
1186 /**
1187 * enum vm_fault_reason - Page fault handlers return a bitmask of
1188 * these values to tell the core VM what happened when handling the
1189 * fault. Used to decide whether a process gets delivered SIGBUS or
1190 * just gets major/minor fault counters bumped up.
1191 *
1192 * @VM_FAULT_OOM: Out Of Memory
1193 * @VM_FAULT_SIGBUS: Bad access
1194 * @VM_FAULT_MAJOR: Page read from storage
1195 * @VM_FAULT_HWPOISON: Hit poisoned small page
1196 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded
1197 * in upper bits
1198 * @VM_FAULT_SIGSEGV: segmentation fault
1199 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page
1200 * @VM_FAULT_LOCKED: ->fault locked the returned page
1201 * @VM_FAULT_RETRY: ->fault blocked, must retry
1202 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small
1203 * @VM_FAULT_DONE_COW: ->fault has fully handled COW
1204 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs
1205 * fsync() to complete (for synchronous page faults
1206 * in DAX)
1207 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released
1208 * @VM_FAULT_HINDEX_MASK: mask HINDEX value
1209 *
1210 */
1211 enum vm_fault_reason {
1212 VM_FAULT_OOM = (__force vm_fault_t)0x000001,
1213 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002,
1214 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004,
1215 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010,
1216 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
1217 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040,
1218 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100,
1219 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200,
1220 VM_FAULT_RETRY = (__force vm_fault_t)0x000400,
1221 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800,
1222 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000,
1223 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000,
1224 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000,
1225 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000,
1226 };
1227
1228 /* Encode hstate index for a hwpoisoned large page */
1229 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
1230 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
1231
1232 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \
1233 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \
1234 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
1235
1236 #define VM_FAULT_RESULT_TRACE \
1237 { VM_FAULT_OOM, "OOM" }, \
1238 { VM_FAULT_SIGBUS, "SIGBUS" }, \
1239 { VM_FAULT_MAJOR, "MAJOR" }, \
1240 { VM_FAULT_HWPOISON, "HWPOISON" }, \
1241 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \
1242 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \
1243 { VM_FAULT_NOPAGE, "NOPAGE" }, \
1244 { VM_FAULT_LOCKED, "LOCKED" }, \
1245 { VM_FAULT_RETRY, "RETRY" }, \
1246 { VM_FAULT_FALLBACK, "FALLBACK" }, \
1247 { VM_FAULT_DONE_COW, "DONE_COW" }, \
1248 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \
1249 { VM_FAULT_COMPLETED, "COMPLETED" }
1250
1251 struct vm_special_mapping {
1252 const char *name; /* The name, e.g. "[vdso]". */
1253
1254 /*
1255 * If .fault is not provided, this points to a
1256 * NULL-terminated array of pages that back the special mapping.
1257 *
1258 * This must not be NULL unless .fault is provided.
1259 */
1260 struct page **pages;
1261
1262 /*
1263 * If non-NULL, then this is called to resolve page faults
1264 * on the special mapping. If used, .pages is not checked.
1265 */
1266 vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1267 struct vm_area_struct *vma,
1268 struct vm_fault *vmf);
1269
1270 int (*mremap)(const struct vm_special_mapping *sm,
1271 struct vm_area_struct *new_vma);
1272 };
1273
1274 enum tlb_flush_reason {
1275 TLB_FLUSH_ON_TASK_SWITCH,
1276 TLB_REMOTE_SHOOTDOWN,
1277 TLB_LOCAL_SHOOTDOWN,
1278 TLB_LOCAL_MM_SHOOTDOWN,
1279 TLB_REMOTE_SEND_IPI,
1280 NR_TLB_FLUSH_REASONS,
1281 };
1282
1283 /**
1284 * enum fault_flag - Fault flag definitions.
1285 * @FAULT_FLAG_WRITE: Fault was a write fault.
1286 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
1287 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
1288 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
1289 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
1290 * @FAULT_FLAG_TRIED: The fault has been tried once.
1291 * @FAULT_FLAG_USER: The fault originated in userspace.
1292 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
1293 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
1294 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
1295 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
1296 * COW mapping, making sure that an exclusive anon page is
1297 * mapped after the fault.
1298 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
1299 * We should only access orig_pte if this flag set.
1300 * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock.
1301 *
1302 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
1303 * whether we would allow page faults to retry by specifying these two
1304 * fault flags correctly. Currently there can be three legal combinations:
1305 *
1306 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and
1307 * this is the first try
1308 *
1309 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and
1310 * we've already tried at least once
1311 *
1312 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
1313 *
1314 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
1315 * be used. Note that page faults can be allowed to retry for multiple times,
1316 * in which case we'll have an initial fault with flags (a) then later on
1317 * continuous faults with flags (b). We should always try to detect pending
1318 * signals before a retry to make sure the continuous page faults can still be
1319 * interrupted if necessary.
1320 *
1321 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
1322 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
1323 * applied to mappings that are not COW mappings.
1324 */
1325 enum fault_flag {
1326 FAULT_FLAG_WRITE = 1 << 0,
1327 FAULT_FLAG_MKWRITE = 1 << 1,
1328 FAULT_FLAG_ALLOW_RETRY = 1 << 2,
1329 FAULT_FLAG_RETRY_NOWAIT = 1 << 3,
1330 FAULT_FLAG_KILLABLE = 1 << 4,
1331 FAULT_FLAG_TRIED = 1 << 5,
1332 FAULT_FLAG_USER = 1 << 6,
1333 FAULT_FLAG_REMOTE = 1 << 7,
1334 FAULT_FLAG_INSTRUCTION = 1 << 8,
1335 FAULT_FLAG_INTERRUPTIBLE = 1 << 9,
1336 FAULT_FLAG_UNSHARE = 1 << 10,
1337 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11,
1338 FAULT_FLAG_VMA_LOCK = 1 << 12,
1339 };
1340
1341 typedef unsigned int __bitwise zap_flags_t;
1342
1343 /*
1344 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
1345 * other. Here is what they mean, and how to use them:
1346 *
1347 *
1348 * FIXME: For pages which are part of a filesystem, mappings are subject to the
1349 * lifetime enforced by the filesystem and we need guarantees that longterm
1350 * users like RDMA and V4L2 only establish mappings which coordinate usage with
1351 * the filesystem. Ideas for this coordination include revoking the longterm
1352 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was
1353 * added after the problem with filesystems was found FS DAX VMAs are
1354 * specifically failed. Filesystem pages are still subject to bugs and use of
1355 * FOLL_LONGTERM should be avoided on those pages.
1356 *
1357 * In the CMA case: long term pins in a CMA region would unnecessarily fragment
1358 * that region. And so, CMA attempts to migrate the page before pinning, when
1359 * FOLL_LONGTERM is specified.
1360 *
1361 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
1362 * but an additional pin counting system) will be invoked. This is intended for
1363 * anything that gets a page reference and then touches page data (for example,
1364 * Direct IO). This lets the filesystem know that some non-file-system entity is
1365 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
1366 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
1367 * a call to unpin_user_page().
1368 *
1369 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
1370 * and separate refcounting mechanisms, however, and that means that each has
1371 * its own acquire and release mechanisms:
1372 *
1373 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
1374 *
1375 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
1376 *
1377 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
1378 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
1379 * calls applied to them, and that's perfectly OK. This is a constraint on the
1380 * callers, not on the pages.)
1381 *
1382 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
1383 * directly by the caller. That's in order to help avoid mismatches when
1384 * releasing pages: get_user_pages*() pages must be released via put_page(),
1385 * while pin_user_pages*() pages must be released via unpin_user_page().
1386 *
1387 * Please see Documentation/core-api/pin_user_pages.rst for more information.
1388 */
1389
1390 enum {
1391 /* check pte is writable */
1392 FOLL_WRITE = 1 << 0,
1393 /* do get_page on page */
1394 FOLL_GET = 1 << 1,
1395 /* give error on hole if it would be zero */
1396 FOLL_DUMP = 1 << 2,
1397 /* get_user_pages read/write w/o permission */
1398 FOLL_FORCE = 1 << 3,
1399 /*
1400 * if a disk transfer is needed, start the IO and return without waiting
1401 * upon it
1402 */
1403 FOLL_NOWAIT = 1 << 4,
1404 /* do not fault in pages */
1405 FOLL_NOFAULT = 1 << 5,
1406 /* check page is hwpoisoned */
1407 FOLL_HWPOISON = 1 << 6,
1408 /* don't do file mappings */
1409 FOLL_ANON = 1 << 7,
1410 /*
1411 * FOLL_LONGTERM indicates that the page will be held for an indefinite
1412 * time period _often_ under userspace control. This is in contrast to
1413 * iov_iter_get_pages(), whose usages are transient.
1414 */
1415 FOLL_LONGTERM = 1 << 8,
1416 /* split huge pmd before returning */
1417 FOLL_SPLIT_PMD = 1 << 9,
1418 /* allow returning PCI P2PDMA pages */
1419 FOLL_PCI_P2PDMA = 1 << 10,
1420 /* allow interrupts from generic signals */
1421 FOLL_INTERRUPTIBLE = 1 << 11,
1422 /*
1423 * Always honor (trigger) NUMA hinting faults.
1424 *
1425 * FOLL_WRITE implicitly honors NUMA hinting faults because a
1426 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE
1427 * apply). get_user_pages_fast_only() always implicitly honors NUMA
1428 * hinting faults.
1429 */
1430 FOLL_HONOR_NUMA_FAULT = 1 << 12,
1431
1432 /* See also internal only FOLL flags in mm/internal.h */
1433 };
1434
1435 #endif /* _LINUX_MM_TYPES_H */
1436