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