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 #include <linux/types.h>
23 #include <linux/rseq_types.h>
24 #include <linux/bitmap.h>
25
26 #include <asm/mmu.h>
27
28 #ifndef AT_VECTOR_SIZE_ARCH
29 #define AT_VECTOR_SIZE_ARCH 0
30 #endif
31 #define AT_VECTOR_SIZE (2*(AT_VECTOR_SIZE_ARCH + AT_VECTOR_SIZE_BASE + 1))
32
33
34 struct address_space;
35 struct futex_private_hash;
36 struct mem_cgroup;
37
38 typedef struct {
39 unsigned long f;
40 } memdesc_flags_t;
41
42 /*
43 * Each physical page in the system has a struct page associated with
44 * it to keep track of whatever it is we are using the page for at the
45 * moment. Note that we have no way to track which tasks are using
46 * a page, though if it is a pagecache page, rmap structures can tell us
47 * who is mapping it.
48 *
49 * If you allocate the page using alloc_pages(), you can use some of the
50 * space in struct page for your own purposes. The five words in the main
51 * union are available, except for bit 0 of the first word which must be
52 * kept clear. Many users use this word to store a pointer to an object
53 * which is guaranteed to be aligned. If you use the same storage as
54 * page->mapping, you must restore it to NULL before freeing the page.
55 *
56 * The mapcount field must not be used for own purposes.
57 *
58 * If you want to use the refcount field, it must be used in such a way
59 * that other CPUs temporarily incrementing and then decrementing the
60 * refcount does not cause problems. On receiving the page from
61 * alloc_pages(), the refcount will be positive.
62 *
63 * If you allocate pages of order > 0, you can use some of the fields
64 * in each subpage, but you may need to restore some of their values
65 * afterwards.
66 *
67 * SLUB uses cmpxchg_double() to atomically update its freelist and counters.
68 * That requires that freelist & counters in struct slab be adjacent and
69 * double-word aligned. Because struct slab currently just reinterprets the
70 * bits of struct page, we align all struct pages to double-word boundaries,
71 * and ensure that 'freelist' is aligned within struct slab.
72 */
73 #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE
74 #define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
75 #else
76 #define _struct_page_alignment __aligned(sizeof(unsigned long))
77 #endif
78
79 struct page {
80 memdesc_flags_t flags; /* Atomic flags, some possibly
81 * updated asynchronously */
82 /*
83 * Five words (20/40 bytes) are available in this union.
84 * WARNING: bit 0 of the first word is used for PageTail(). That
85 * means the other users of this union MUST NOT use the bit to
86 * avoid collision and false-positive PageTail().
87 */
88 union {
89 struct { /* Page cache and anonymous pages */
90 /**
91 * @lru: Pageout list, eg. active_list protected by
92 * lruvec->lru_lock. Sometimes used as a generic list
93 * by the page owner.
94 */
95 union {
96 struct list_head lru;
97
98 /* Or, free page */
99 struct list_head buddy_list;
100 struct list_head pcp_list;
101 struct llist_node pcp_llist;
102 };
103 struct address_space *mapping;
104 union {
105 pgoff_t __folio_index; /* Our offset within mapping. */
106 unsigned long share; /* share count for fsdax */
107 };
108 /**
109 * @private: Mapping-private opaque data.
110 * Usually used for buffer_heads if PagePrivate.
111 * Used for swp_entry_t if swapcache flag set.
112 * Indicates order in the buddy system if PageBuddy
113 * or on pcp_llist.
114 */
115 unsigned long private;
116 };
117 struct { /* page_pool used by netstack */
118 /**
119 * @pp_magic: magic value to avoid recycling non
120 * page_pool allocated pages.
121 */
122 unsigned long pp_magic;
123 struct page_pool *pp;
124 unsigned long _pp_mapping_pad;
125 unsigned long dma_addr;
126 atomic_long_t pp_ref_count;
127 };
128 struct { /* Tail pages of compound page */
129 unsigned long compound_head; /* Bit zero is set */
130 };
131 struct { /* ZONE_DEVICE pages */
132 /*
133 * The first word is used for compound_head or folio
134 * pgmap
135 */
136 void *_unused_pgmap_compound_head;
137 void *zone_device_data;
138 /*
139 * ZONE_DEVICE private pages are counted as being
140 * mapped so the next 3 words hold the mapping, index,
141 * and private fields from the source anonymous or
142 * page cache page while the page is migrated to device
143 * private memory.
144 * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
145 * use the mapping, index, and private fields when
146 * pmem backed DAX files are mapped.
147 */
148 };
149
150 /** @rcu_head: You can use this to free a page by RCU. */
151 struct rcu_head rcu_head;
152 };
153
154 union { /* This union is 4 bytes in size. */
155 /*
156 * For head pages of typed folios, the value stored here
157 * allows for determining what this page is used for. The
158 * tail pages of typed folios will not store a type
159 * (page_type == _mapcount == -1).
160 *
161 * See page-flags.h for a list of page types which are currently
162 * stored here.
163 *
164 * Owners of typed folios may reuse the lower 16 bit of the
165 * head page page_type field after setting the page type,
166 * but must reset these 16 bit to -1 before clearing the
167 * page type.
168 */
169 unsigned int page_type;
170
171 /*
172 * For pages that are part of non-typed folios for which mappings
173 * are tracked via the RMAP, encodes the number of times this page
174 * is directly referenced by a page table.
175 *
176 * Note that the mapcount is always initialized to -1, so that
177 * transitions both from it and to it can be tracked, using
178 * atomic_inc_and_test() and atomic_add_negative(-1).
179 */
180 atomic_t _mapcount;
181 };
182
183 /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
184 atomic_t _refcount;
185
186 #ifdef CONFIG_MEMCG
187 unsigned long memcg_data;
188 #elif defined(CONFIG_SLAB_OBJ_EXT)
189 unsigned long _unused_slab_obj_exts;
190 #endif
191
192 /*
193 * On machines where all RAM is mapped into kernel address space,
194 * we can simply calculate the virtual address. On machines with
195 * highmem some memory is mapped into kernel virtual memory
196 * dynamically, so we need a place to store that address.
197 * Note that this field could be 16 bits on x86 ... ;)
198 *
199 * Architectures with slow multiplication can define
200 * WANT_PAGE_VIRTUAL in asm/page.h
201 */
202 #if defined(WANT_PAGE_VIRTUAL)
203 void *virtual; /* Kernel virtual address (NULL if
204 not kmapped, ie. highmem) */
205 #endif /* WANT_PAGE_VIRTUAL */
206
207 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
208 int _last_cpupid;
209 #endif
210
211 #ifdef CONFIG_KMSAN
212 /*
213 * KMSAN metadata for this page:
214 * - shadow page: every bit indicates whether the corresponding
215 * bit of the original page is initialized (0) or not (1);
216 * - origin page: every 4 bytes contain an id of the stack trace
217 * where the uninitialized value was created.
218 */
219 struct page *kmsan_shadow;
220 struct page *kmsan_origin;
221 #endif
222 } _struct_page_alignment;
223
224 /*
225 * struct encoded_page - a nonexistent type marking this pointer
226 *
227 * An 'encoded_page' pointer is a pointer to a regular 'struct page', but
228 * with the low bits of the pointer indicating extra context-dependent
229 * information. Only used in mmu_gather handling, and this acts as a type
230 * system check on that use.
231 *
232 * We only really have two guaranteed bits in general, although you could
233 * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
234 * for more.
235 *
236 * Use the supplied helper functions to endcode/decode the pointer and bits.
237 */
238 struct encoded_page;
239
240 #define ENCODED_PAGE_BITS 3ul
241
242 /* Perform rmap removal after we have flushed the TLB. */
243 #define ENCODED_PAGE_BIT_DELAY_RMAP 1ul
244
245 /*
246 * The next item in an encoded_page array is the "nr_pages" argument, specifying
247 * the number of consecutive pages starting from this page, that all belong to
248 * the same folio. For example, "nr_pages" corresponds to the number of folio
249 * references that must be dropped. If this bit is not set, "nr_pages" is
250 * implicitly 1.
251 */
252 #define ENCODED_PAGE_BIT_NR_PAGES_NEXT 2ul
253
encode_page(struct page * page,unsigned long flags)254 static __always_inline struct encoded_page *encode_page(struct page *page, unsigned long flags)
255 {
256 BUILD_BUG_ON(flags > ENCODED_PAGE_BITS);
257 return (struct encoded_page *)(flags | (unsigned long)page);
258 }
259
encoded_page_flags(struct encoded_page * page)260 static inline unsigned long encoded_page_flags(struct encoded_page *page)
261 {
262 return ENCODED_PAGE_BITS & (unsigned long)page;
263 }
264
encoded_page_ptr(struct encoded_page * page)265 static inline struct page *encoded_page_ptr(struct encoded_page *page)
266 {
267 return (struct page *)(~ENCODED_PAGE_BITS & (unsigned long)page);
268 }
269
encode_nr_pages(unsigned long nr)270 static __always_inline struct encoded_page *encode_nr_pages(unsigned long nr)
271 {
272 VM_WARN_ON_ONCE((nr << 2) >> 2 != nr);
273 return (struct encoded_page *)(nr << 2);
274 }
275
encoded_nr_pages(struct encoded_page * page)276 static __always_inline unsigned long encoded_nr_pages(struct encoded_page *page)
277 {
278 return ((unsigned long)page) >> 2;
279 }
280
281 /*
282 * A swap entry has to fit into a "unsigned long", as the entry is hidden
283 * in the "index" field of the swapper address space.
284 */
285 typedef struct {
286 unsigned long val;
287 } swp_entry_t;
288
289 /**
290 * typedef softleaf_t - Describes a page table software leaf entry, abstracted
291 * from its architecture-specific encoding.
292 *
293 * Page table leaf entries are those which do not reference any descendent page
294 * tables but rather either reference a data page, are an empty (or 'none'
295 * entry), or contain a non-present entry.
296 *
297 * If referencing another page table or a data page then the page table entry is
298 * pertinent to hardware - that is it tells the hardware how to decode the page
299 * table entry.
300 *
301 * Otherwise it is a software-defined leaf page table entry, which this type
302 * describes. See leafops.h and specifically @softleaf_type for a list of all
303 * possible kinds of software leaf entry.
304 *
305 * A softleaf_t entry is abstracted from the hardware page table entry, so is
306 * not architecture-specific.
307 *
308 * NOTE: While we transition from the confusing swp_entry_t type used for this
309 * purpose, we simply alias this type. This will be removed once the
310 * transition is complete.
311 */
312 typedef swp_entry_t softleaf_t;
313
314 #if defined(CONFIG_MEMCG) || defined(CONFIG_SLAB_OBJ_EXT)
315 /* We have some extra room after the refcount in tail pages. */
316 #define NR_PAGES_IN_LARGE_FOLIO
317 #endif
318
319 /*
320 * On 32bit, we can cut the required metadata in half, because:
321 * (a) PID_MAX_LIMIT implicitly limits the number of MMs we could ever have,
322 * so we can limit MM IDs to 15 bit (32767).
323 * (b) We don't expect folios where even a single complete PTE mapping by
324 * one MM would exceed 15 bits (order-15).
325 */
326 #ifdef CONFIG_64BIT
327 typedef int mm_id_mapcount_t;
328 #define MM_ID_MAPCOUNT_MAX INT_MAX
329 typedef unsigned int mm_id_t;
330 #else /* !CONFIG_64BIT */
331 typedef short mm_id_mapcount_t;
332 #define MM_ID_MAPCOUNT_MAX SHRT_MAX
333 typedef unsigned short mm_id_t;
334 #endif /* CONFIG_64BIT */
335
336 /* We implicitly use the dummy ID for init-mm etc. where we never rmap pages. */
337 #define MM_ID_DUMMY 0
338 #define MM_ID_MIN (MM_ID_DUMMY + 1)
339
340 /*
341 * We leave the highest bit of each MM id unused, so we can store a flag
342 * in the highest bit of each folio->_mm_id[].
343 */
344 #define MM_ID_BITS ((sizeof(mm_id_t) * BITS_PER_BYTE) - 1)
345 #define MM_ID_MASK ((1U << MM_ID_BITS) - 1)
346 #define MM_ID_MAX MM_ID_MASK
347
348 /*
349 * In order to use bit_spin_lock(), which requires an unsigned long, we
350 * operate on folio->_mm_ids when working on flags.
351 */
352 #define FOLIO_MM_IDS_LOCK_BITNUM MM_ID_BITS
353 #define FOLIO_MM_IDS_LOCK_BIT BIT(FOLIO_MM_IDS_LOCK_BITNUM)
354 #define FOLIO_MM_IDS_SHARED_BITNUM (2 * MM_ID_BITS + 1)
355 #define FOLIO_MM_IDS_SHARED_BIT BIT(FOLIO_MM_IDS_SHARED_BITNUM)
356
357 /**
358 * struct folio - Represents a contiguous set of bytes.
359 * @flags: Identical to the page flags.
360 * @lru: Least Recently Used list; tracks how recently this folio was used.
361 * @mlock_count: Number of times this folio has been pinned by mlock().
362 * @mapping: The file this page belongs to, or refers to the anon_vma for
363 * anonymous memory.
364 * @index: Offset within the file, in units of pages. For anonymous memory,
365 * this is the index from the beginning of the mmap.
366 * @share: number of DAX mappings that reference this folio. See
367 * dax_associate_entry.
368 * @private: Filesystem per-folio data (see folio_attach_private()).
369 * @swap: Used for swp_entry_t if folio_test_swapcache().
370 * @_mapcount: Do not access this member directly. Use folio_mapcount() to
371 * find out how many times this folio is mapped by userspace.
372 * @_refcount: Do not access this member directly. Use folio_ref_count()
373 * to find how many references there are to this folio.
374 * @memcg_data: Memory Control Group data.
375 * @pgmap: Metadata for ZONE_DEVICE mappings
376 * @virtual: Virtual address in the kernel direct map.
377 * @_last_cpupid: IDs of last CPU and last process that accessed the folio.
378 * @_entire_mapcount: Do not use directly, call folio_entire_mapcount().
379 * @_large_mapcount: Do not use directly, call folio_mapcount().
380 * @_nr_pages_mapped: Do not use outside of rmap and debug code.
381 * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
382 * @_nr_pages: Do not use directly, call folio_nr_pages().
383 * @_mm_id: Do not use outside of rmap code.
384 * @_mm_ids: Do not use outside of rmap code.
385 * @_mm_id_mapcount: Do not use outside of rmap code.
386 * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
387 * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
388 * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
389 * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
390 * @_deferred_list: Folios to be split under memory pressure.
391 * @_unused_slab_obj_exts: Placeholder to match obj_exts in struct slab.
392 *
393 * A folio is a physically, virtually and logically contiguous set
394 * of bytes. It is a power-of-two in size, and it is aligned to that
395 * same power-of-two. It is at least as large as %PAGE_SIZE. If it is
396 * in the page cache, it is at a file offset which is a multiple of that
397 * power-of-two. It may be mapped into userspace at an address which is
398 * at an arbitrary page offset, but its kernel virtual address is aligned
399 * to its size.
400 */
401 struct folio {
402 /* private: don't document the anon union */
403 union {
404 struct {
405 /* public: */
406 memdesc_flags_t flags;
407 union {
408 struct list_head lru;
409 /* private: avoid cluttering the output */
410 /* For the Unevictable "LRU list" slot */
411 struct {
412 /* Avoid compound_head */
413 void *__filler;
414 /* public: */
415 unsigned int mlock_count;
416 /* private: */
417 };
418 /* public: */
419 struct dev_pagemap *pgmap;
420 };
421 struct address_space *mapping;
422 union {
423 pgoff_t index;
424 unsigned long share;
425 };
426 union {
427 void *private;
428 swp_entry_t swap;
429 };
430 atomic_t _mapcount;
431 atomic_t _refcount;
432 #ifdef CONFIG_MEMCG
433 unsigned long memcg_data;
434 #elif defined(CONFIG_SLAB_OBJ_EXT)
435 unsigned long _unused_slab_obj_exts;
436 #endif
437 #if defined(WANT_PAGE_VIRTUAL)
438 void *virtual;
439 #endif
440 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
441 int _last_cpupid;
442 #endif
443 /* private: the union with struct page is transitional */
444 };
445 struct page page;
446 };
447 union {
448 struct {
449 unsigned long _flags_1;
450 unsigned long _head_1;
451 union {
452 struct {
453 /* public: */
454 atomic_t _large_mapcount;
455 atomic_t _nr_pages_mapped;
456 #ifdef CONFIG_64BIT
457 atomic_t _entire_mapcount;
458 atomic_t _pincount;
459 #endif /* CONFIG_64BIT */
460 mm_id_mapcount_t _mm_id_mapcount[2];
461 union {
462 mm_id_t _mm_id[2];
463 unsigned long _mm_ids;
464 };
465 /* private: the union with struct page is transitional */
466 };
467 unsigned long _usable_1[4];
468 };
469 atomic_t _mapcount_1;
470 atomic_t _refcount_1;
471 /* public: */
472 #ifdef NR_PAGES_IN_LARGE_FOLIO
473 unsigned int _nr_pages;
474 #endif /* NR_PAGES_IN_LARGE_FOLIO */
475 /* private: the union with struct page is transitional */
476 };
477 struct page __page_1;
478 };
479 union {
480 struct {
481 unsigned long _flags_2;
482 unsigned long _head_2;
483 /* public: */
484 struct list_head _deferred_list;
485 #ifndef CONFIG_64BIT
486 atomic_t _entire_mapcount;
487 atomic_t _pincount;
488 #endif /* !CONFIG_64BIT */
489 /* private: the union with struct page is transitional */
490 };
491 struct page __page_2;
492 };
493 union {
494 struct {
495 unsigned long _flags_3;
496 unsigned long _head_3;
497 /* public: */
498 void *_hugetlb_subpool;
499 void *_hugetlb_cgroup;
500 void *_hugetlb_cgroup_rsvd;
501 void *_hugetlb_hwpoison;
502 /* private: the union with struct page is transitional */
503 };
504 struct page __page_3;
505 };
506 };
507
508 #define FOLIO_MATCH(pg, fl) \
509 static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))
510 FOLIO_MATCH(flags, flags);
511 FOLIO_MATCH(lru, lru);
512 FOLIO_MATCH(mapping, mapping);
513 FOLIO_MATCH(compound_head, lru);
514 FOLIO_MATCH(__folio_index, index);
515 FOLIO_MATCH(private, private);
516 FOLIO_MATCH(_mapcount, _mapcount);
517 FOLIO_MATCH(_refcount, _refcount);
518 #ifdef CONFIG_MEMCG
519 FOLIO_MATCH(memcg_data, memcg_data);
520 #endif
521 #if defined(WANT_PAGE_VIRTUAL)
522 FOLIO_MATCH(virtual, virtual);
523 #endif
524 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
525 FOLIO_MATCH(_last_cpupid, _last_cpupid);
526 #endif
527 #undef FOLIO_MATCH
528 #define FOLIO_MATCH(pg, fl) \
529 static_assert(offsetof(struct folio, fl) == \
530 offsetof(struct page, pg) + sizeof(struct page))
531 FOLIO_MATCH(flags, _flags_1);
532 FOLIO_MATCH(compound_head, _head_1);
533 FOLIO_MATCH(_mapcount, _mapcount_1);
534 FOLIO_MATCH(_refcount, _refcount_1);
535 #undef FOLIO_MATCH
536 #define FOLIO_MATCH(pg, fl) \
537 static_assert(offsetof(struct folio, fl) == \
538 offsetof(struct page, pg) + 2 * sizeof(struct page))
539 FOLIO_MATCH(flags, _flags_2);
540 FOLIO_MATCH(compound_head, _head_2);
541 #undef FOLIO_MATCH
542 #define FOLIO_MATCH(pg, fl) \
543 static_assert(offsetof(struct folio, fl) == \
544 offsetof(struct page, pg) + 3 * sizeof(struct page))
545 FOLIO_MATCH(flags, _flags_3);
546 FOLIO_MATCH(compound_head, _head_3);
547 #undef FOLIO_MATCH
548
549 /**
550 * struct ptdesc - Memory descriptor for page tables.
551 * @pt_flags: enum pt_flags plus zone/node/section.
552 * @pt_rcu_head: For freeing page table pages.
553 * @pt_list: List of used page tables. Used for s390 gmap shadow pages
554 * (which are not linked into the user page tables) and x86
555 * pgds.
556 * @_pt_pad_1: Padding that aliases with page's compound head.
557 * @pmd_huge_pte: Protected by ptdesc->ptl, used for THPs.
558 * @__page_mapping: Aliases with page->mapping. Unused for page tables.
559 * @pt_index: Used for s390 gmap.
560 * @pt_mm: Used for x86 pgds.
561 * @pt_frag_refcount: For fragmented page table tracking. Powerpc only.
562 * @pt_share_count: Used for HugeTLB PMD page table share count.
563 * @_pt_pad_2: Padding to ensure proper alignment.
564 * @ptl: Lock for the page table.
565 * @__page_type: Same as page->page_type. Unused for page tables.
566 * @__page_refcount: Same as page refcount.
567 * @pt_memcg_data: Memcg data. Tracked for page tables here.
568 *
569 * This struct overlays struct page for now. Do not modify without a good
570 * understanding of the issues.
571 */
572 struct ptdesc {
573 memdesc_flags_t pt_flags;
574
575 union {
576 struct rcu_head pt_rcu_head;
577 struct list_head pt_list;
578 struct {
579 unsigned long _pt_pad_1;
580 pgtable_t pmd_huge_pte;
581 };
582 };
583 unsigned long __page_mapping;
584
585 union {
586 pgoff_t pt_index;
587 struct mm_struct *pt_mm;
588 atomic_t pt_frag_refcount;
589 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
590 atomic_t pt_share_count;
591 #endif
592 };
593
594 union {
595 unsigned long _pt_pad_2;
596 #if ALLOC_SPLIT_PTLOCKS
597 spinlock_t *ptl;
598 #else
599 spinlock_t ptl;
600 #endif
601 };
602 unsigned int __page_type;
603 atomic_t __page_refcount;
604 #ifdef CONFIG_MEMCG
605 unsigned long pt_memcg_data;
606 #endif
607 };
608
609 #define TABLE_MATCH(pg, pt) \
610 static_assert(offsetof(struct page, pg) == offsetof(struct ptdesc, pt))
611 TABLE_MATCH(flags, pt_flags);
612 TABLE_MATCH(compound_head, pt_list);
613 TABLE_MATCH(compound_head, _pt_pad_1);
614 TABLE_MATCH(mapping, __page_mapping);
615 TABLE_MATCH(__folio_index, pt_index);
616 TABLE_MATCH(rcu_head, pt_rcu_head);
617 TABLE_MATCH(page_type, __page_type);
618 TABLE_MATCH(_refcount, __page_refcount);
619 #ifdef CONFIG_MEMCG
620 TABLE_MATCH(memcg_data, pt_memcg_data);
621 #endif
622 #undef TABLE_MATCH
623 static_assert(sizeof(struct ptdesc) <= sizeof(struct page));
624
625 #define ptdesc_page(pt) (_Generic((pt), \
626 const struct ptdesc *: (const struct page *)(pt), \
627 struct ptdesc *: (struct page *)(pt)))
628
629 #define ptdesc_folio(pt) (_Generic((pt), \
630 const struct ptdesc *: (const struct folio *)(pt), \
631 struct ptdesc *: (struct folio *)(pt)))
632
633 #define page_ptdesc(p) (_Generic((p), \
634 const struct page *: (const struct ptdesc *)(p), \
635 struct page *: (struct ptdesc *)(p)))
636
637 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
ptdesc_pmd_pts_init(struct ptdesc * ptdesc)638 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
639 {
640 atomic_set(&ptdesc->pt_share_count, 0);
641 }
642
ptdesc_pmd_pts_inc(struct ptdesc * ptdesc)643 static inline void ptdesc_pmd_pts_inc(struct ptdesc *ptdesc)
644 {
645 atomic_inc(&ptdesc->pt_share_count);
646 }
647
ptdesc_pmd_pts_dec(struct ptdesc * ptdesc)648 static inline void ptdesc_pmd_pts_dec(struct ptdesc *ptdesc)
649 {
650 atomic_dec(&ptdesc->pt_share_count);
651 }
652
ptdesc_pmd_pts_count(const struct ptdesc * ptdesc)653 static inline int ptdesc_pmd_pts_count(const struct ptdesc *ptdesc)
654 {
655 return atomic_read(&ptdesc->pt_share_count);
656 }
657
ptdesc_pmd_is_shared(struct ptdesc * ptdesc)658 static inline bool ptdesc_pmd_is_shared(struct ptdesc *ptdesc)
659 {
660 return !!ptdesc_pmd_pts_count(ptdesc);
661 }
662 #else
ptdesc_pmd_pts_init(struct ptdesc * ptdesc)663 static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
664 {
665 }
666 #endif
667
668 /*
669 * Used for sizing the vmemmap region on some architectures
670 */
671 #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page)))
672
673 /*
674 * page_private can be used on tail pages. However, PagePrivate is only
675 * checked by the VM on the head page. So page_private on the tail pages
676 * should be used for data that's ancillary to the head page (eg attaching
677 * buffer heads to tail pages after attaching buffer heads to the head page)
678 */
679 #define page_private(page) ((page)->private)
680
set_page_private(struct page * page,unsigned long private)681 static inline void set_page_private(struct page *page, unsigned long private)
682 {
683 page->private = private;
684 }
685
folio_get_private(const struct folio * folio)686 static inline void *folio_get_private(const struct folio *folio)
687 {
688 return folio->private;
689 }
690
691 typedef unsigned long vm_flags_t;
692
693 /*
694 * freeptr_t represents a SLUB freelist pointer, which might be encoded
695 * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled.
696 */
697 typedef struct { unsigned long v; } freeptr_t;
698
699 /*
700 * A region containing a mapping of a non-memory backed file under NOMMU
701 * conditions. These are held in a global tree and are pinned by the VMAs that
702 * map parts of them.
703 */
704 struct vm_region {
705 struct rb_node vm_rb; /* link in global region tree */
706 vm_flags_t vm_flags; /* VMA vm_flags */
707 unsigned long vm_start; /* start address of region */
708 unsigned long vm_end; /* region initialised to here */
709 unsigned long vm_top; /* region allocated to here */
710 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
711 struct file *vm_file; /* the backing file or NULL */
712
713 int vm_usage; /* region usage count (access under nommu_region_sem) */
714 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
715 * this region */
716 };
717
718 #ifdef CONFIG_USERFAULTFD
719 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) { NULL, })
720 struct vm_userfaultfd_ctx {
721 struct userfaultfd_ctx *ctx;
722 };
723 #else /* CONFIG_USERFAULTFD */
724 #define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {})
725 struct vm_userfaultfd_ctx {};
726 #endif /* CONFIG_USERFAULTFD */
727
728 struct anon_vma_name {
729 struct kref kref;
730 /* The name needs to be at the end because it is dynamically sized. */
731 char name[];
732 };
733
734 #ifdef CONFIG_ANON_VMA_NAME
735 /*
736 * mmap_lock should be read-locked when calling anon_vma_name(). Caller should
737 * either keep holding the lock while using the returned pointer or it should
738 * raise anon_vma_name refcount before releasing the lock.
739 */
740 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma);
741 struct anon_vma_name *anon_vma_name_alloc(const char *name);
742 void anon_vma_name_free(struct kref *kref);
743 #else /* CONFIG_ANON_VMA_NAME */
anon_vma_name(struct vm_area_struct * vma)744 static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
745 {
746 return NULL;
747 }
748
anon_vma_name_alloc(const char * name)749 static inline struct anon_vma_name *anon_vma_name_alloc(const char *name)
750 {
751 return NULL;
752 }
753 #endif
754
755 /*
756 * While __vma_enter_locked() is working to ensure are no read-locks held on a
757 * VMA (either while acquiring a VMA write lock or marking a VMA detached) we
758 * set the VM_REFCNT_EXCLUDE_READERS_FLAG in vma->vm_refcnt to indiciate to
759 * vma_start_read() that the reference count should be left alone.
760 *
761 * See the comment describing vm_refcnt in vm_area_struct for details as to
762 * which values the VMA reference count can be.
763 */
764 #define VM_REFCNT_EXCLUDE_READERS_BIT (30)
765 #define VM_REFCNT_EXCLUDE_READERS_FLAG (1U << VM_REFCNT_EXCLUDE_READERS_BIT)
766 #define VM_REFCNT_LIMIT (VM_REFCNT_EXCLUDE_READERS_FLAG - 1)
767
768 struct vma_numab_state {
769 /*
770 * Initialised as time in 'jiffies' after which VMA
771 * should be scanned. Delays first scan of new VMA by at
772 * least sysctl_numa_balancing_scan_delay:
773 */
774 unsigned long next_scan;
775
776 /*
777 * Time in jiffies when pids_active[] is reset to
778 * detect phase change behaviour:
779 */
780 unsigned long pids_active_reset;
781
782 /*
783 * Approximate tracking of PIDs that trapped a NUMA hinting
784 * fault. May produce false positives due to hash collisions.
785 *
786 * [0] Previous PID tracking
787 * [1] Current PID tracking
788 *
789 * Window moves after next_pid_reset has expired approximately
790 * every VMA_PID_RESET_PERIOD jiffies:
791 */
792 unsigned long pids_active[2];
793
794 /* MM scan sequence ID when scan first started after VMA creation */
795 int start_scan_seq;
796
797 /*
798 * MM scan sequence ID when the VMA was last completely scanned.
799 * A VMA is not eligible for scanning if prev_scan_seq == numa_scan_seq
800 */
801 int prev_scan_seq;
802 };
803
804 #ifdef __HAVE_PFNMAP_TRACKING
805 struct pfnmap_track_ctx {
806 struct kref kref;
807 unsigned long pfn;
808 unsigned long size; /* in bytes */
809 };
810 #endif
811
812 /* What action should be taken after an .mmap_prepare call is complete? */
813 enum mmap_action_type {
814 MMAP_NOTHING, /* Mapping is complete, no further action. */
815 MMAP_REMAP_PFN, /* Remap PFN range. */
816 MMAP_IO_REMAP_PFN, /* I/O remap PFN range. */
817 };
818
819 /*
820 * Describes an action an mmap_prepare hook can instruct to be taken to complete
821 * the mapping of a VMA. Specified in vm_area_desc.
822 */
823 struct mmap_action {
824 union {
825 /* Remap range. */
826 struct {
827 unsigned long start;
828 unsigned long start_pfn;
829 unsigned long size;
830 pgprot_t pgprot;
831 } remap;
832 };
833 enum mmap_action_type type;
834
835 /*
836 * If specified, this hook is invoked after the selected action has been
837 * successfully completed. Note that the VMA write lock still held.
838 *
839 * The absolute minimum ought to be done here.
840 *
841 * Returns 0 on success, or an error code.
842 */
843 int (*success_hook)(const struct vm_area_struct *vma);
844
845 /*
846 * If specified, this hook is invoked when an error occurred when
847 * attempting the selected action.
848 *
849 * The hook can return an error code in order to filter the error, but
850 * it is not valid to clear the error here.
851 */
852 int (*error_hook)(int err);
853
854 /*
855 * This should be set in rare instances where the operation required
856 * that the rmap should not be able to access the VMA until
857 * completely set up.
858 */
859 bool hide_from_rmap_until_complete :1;
860 };
861
862 /*
863 * Opaque type representing current VMA (vm_area_struct) flag state. Must be
864 * accessed via vma_flags_xxx() helper functions.
865 */
866 #define NUM_VMA_FLAG_BITS BITS_PER_LONG
867 typedef struct {
868 DECLARE_BITMAP(__vma_flags, NUM_VMA_FLAG_BITS);
869 } vma_flags_t;
870
871 #define EMPTY_VMA_FLAGS ((vma_flags_t){ })
872
873 /*
874 * Describes a VMA that is about to be mmap()'ed. Drivers may choose to
875 * manipulate mutable fields which will cause those fields to be updated in the
876 * resultant VMA.
877 *
878 * Helper functions are not required for manipulating any field.
879 */
880 struct vm_area_desc {
881 /* Immutable state. */
882 const struct mm_struct *const mm;
883 struct file *const file; /* May vary from vm_file in stacked callers. */
884 unsigned long start;
885 unsigned long end;
886
887 /* Mutable fields. Populated with initial state. */
888 pgoff_t pgoff;
889 struct file *vm_file;
890 vma_flags_t vma_flags;
891 pgprot_t page_prot;
892
893 /* Write-only fields. */
894 const struct vm_operations_struct *vm_ops;
895 void *private_data;
896
897 /* Take further action? */
898 struct mmap_action action;
899 };
900
901 /*
902 * This struct describes a virtual memory area. There is one of these
903 * per VM-area/task. A VM area is any part of the process virtual memory
904 * space that has a special rule for the page-fault handlers (ie a shared
905 * library, the executable area etc).
906 *
907 * Only explicitly marked struct members may be accessed by RCU readers before
908 * getting a stable reference.
909 *
910 * WARNING: when adding new members, please update vm_area_init_from() to copy
911 * them during vm_area_struct content duplication.
912 */
913 struct vm_area_struct {
914 /* The first cache line has the info for VMA tree walking. */
915
916 union {
917 struct {
918 /* VMA covers [vm_start; vm_end) addresses within mm */
919 unsigned long vm_start;
920 unsigned long vm_end;
921 };
922 freeptr_t vm_freeptr; /* Pointer used by SLAB_TYPESAFE_BY_RCU */
923 };
924
925 /*
926 * The address space we belong to.
927 * Unstable RCU readers are allowed to read this.
928 */
929 struct mm_struct *vm_mm;
930 pgprot_t vm_page_prot; /* Access permissions of this VMA. */
931
932 /*
933 * Flags, see mm.h.
934 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
935 * Preferably, use vma_flags_xxx() functions.
936 */
937 union {
938 /* Temporary while VMA flags are being converted. */
939 const vm_flags_t vm_flags;
940 vma_flags_t flags;
941 };
942
943 #ifdef CONFIG_PER_VMA_LOCK
944 /*
945 * Can only be written (using WRITE_ONCE()) while holding both:
946 * - mmap_lock (in write mode)
947 * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_FLAG is set
948 * Can be read reliably while holding one of:
949 * - mmap_lock (in read or write mode)
950 * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_BIT is set or vm_refcnt > 1
951 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
952 * while holding nothing (except RCU to keep the VMA struct allocated).
953 *
954 * This sequence counter is explicitly allowed to overflow; sequence
955 * counter reuse can only lead to occasional unnecessary use of the
956 * slowpath.
957 */
958 unsigned int vm_lock_seq;
959 #endif
960 /*
961 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
962 * list, after a COW of one of the file pages. A MAP_SHARED vma
963 * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
964 * or brk vma (with NULL file) can only be in an anon_vma list.
965 */
966 struct list_head anon_vma_chain; /* Serialized by mmap_lock &
967 * page_table_lock */
968 struct anon_vma *anon_vma; /* Serialized by page_table_lock */
969
970 /* Function pointers to deal with this struct. */
971 const struct vm_operations_struct *vm_ops;
972
973 /* Information about our backing store: */
974 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
975 units */
976 struct file * vm_file; /* File we map to (can be NULL). */
977 void * vm_private_data; /* was vm_pte (shared mem) */
978
979 #ifdef CONFIG_SWAP
980 atomic_long_t swap_readahead_info;
981 #endif
982 #ifndef CONFIG_MMU
983 struct vm_region *vm_region; /* NOMMU mapping region */
984 #endif
985 #ifdef CONFIG_NUMA
986 struct mempolicy *vm_policy; /* NUMA policy for the VMA */
987 #endif
988 #ifdef CONFIG_NUMA_BALANCING
989 struct vma_numab_state *numab_state; /* NUMA Balancing state */
990 #endif
991 #ifdef CONFIG_PER_VMA_LOCK
992 /*
993 * Used to keep track of firstly, whether the VMA is attached, secondly,
994 * if attached, how many read locks are taken, and thirdly, if the
995 * VM_REFCNT_EXCLUDE_READERS_FLAG is set, whether any read locks held
996 * are currently in the process of being excluded.
997 *
998 * This value can be equal to:
999 *
1000 * 0 - Detached. IMPORTANT: when the refcnt is zero, readers cannot
1001 * increment it.
1002 *
1003 * 1 - Attached and either unlocked or write-locked. Write locks are
1004 * identified via __is_vma_write_locked() which checks for equality of
1005 * vma->vm_lock_seq and mm->mm_lock_seq.
1006 *
1007 * >1, < VM_REFCNT_EXCLUDE_READERS_FLAG - Read-locked or (unlikely)
1008 * write-locked with other threads having temporarily incremented the
1009 * reference count prior to determining it is write-locked and
1010 * decrementing it again.
1011 *
1012 * VM_REFCNT_EXCLUDE_READERS_FLAG - Detached, pending
1013 * __vma_end_exclude_readers() completion which will decrement the
1014 * reference count to zero. IMPORTANT - at this stage no further readers
1015 * can increment the reference count. It can only be reduced.
1016 *
1017 * VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either write-locking
1018 * an attached VMA and has yet to invoke __vma_end_exclude_readers(),
1019 * OR a thread is detaching a VMA and is waiting on a single spurious
1020 * reader in order to decrement the reference count. IMPORTANT - as
1021 * above, no further readers can increment the reference count.
1022 *
1023 * > VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either
1024 * write-locking or detaching a VMA is waiting on readers to
1025 * exit. IMPORTANT - as above, no further readers can increment the
1026 * reference count.
1027 *
1028 * NOTE: Unstable RCU readers are allowed to read this.
1029 */
1030 refcount_t vm_refcnt ____cacheline_aligned_in_smp;
1031 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1032 struct lockdep_map vmlock_dep_map;
1033 #endif
1034 #endif
1035 /*
1036 * For areas with an address space and backing store,
1037 * linkage into the address_space->i_mmap interval tree.
1038 *
1039 */
1040 struct {
1041 struct rb_node rb;
1042 unsigned long rb_subtree_last;
1043 } shared;
1044 #ifdef CONFIG_ANON_VMA_NAME
1045 /*
1046 * For private and shared anonymous mappings, a pointer to a null
1047 * terminated string containing the name given to the vma, or NULL if
1048 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
1049 */
1050 struct anon_vma_name *anon_name;
1051 #endif
1052 struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
1053 #ifdef __HAVE_PFNMAP_TRACKING
1054 struct pfnmap_track_ctx *pfnmap_track_ctx;
1055 #endif
1056 } __randomize_layout;
1057
1058 /* Clears all bits in the VMA flags bitmap, non-atomically. */
vma_flags_clear_all(vma_flags_t * flags)1059 static inline void vma_flags_clear_all(vma_flags_t *flags)
1060 {
1061 bitmap_zero(flags->__vma_flags, NUM_VMA_FLAG_BITS);
1062 }
1063
1064 /*
1065 * Copy value to the first system word of VMA flags, non-atomically.
1066 *
1067 * IMPORTANT: This does not overwrite bytes past the first system word. The
1068 * caller must account for this.
1069 */
vma_flags_overwrite_word(vma_flags_t * flags,unsigned long value)1070 static inline void vma_flags_overwrite_word(vma_flags_t *flags, unsigned long value)
1071 {
1072 unsigned long *bitmap = flags->__vma_flags;
1073
1074 bitmap[0] = value;
1075 }
1076
1077 /*
1078 * Copy value to the first system word of VMA flags ONCE, non-atomically.
1079 *
1080 * IMPORTANT: This does not overwrite bytes past the first system word. The
1081 * caller must account for this.
1082 */
vma_flags_overwrite_word_once(vma_flags_t * flags,unsigned long value)1083 static inline void vma_flags_overwrite_word_once(vma_flags_t *flags, unsigned long value)
1084 {
1085 unsigned long *bitmap = flags->__vma_flags;
1086
1087 WRITE_ONCE(*bitmap, value);
1088 }
1089
1090 /* Update the first system word of VMA flags setting bits, non-atomically. */
vma_flags_set_word(vma_flags_t * flags,unsigned long value)1091 static inline void vma_flags_set_word(vma_flags_t *flags, unsigned long value)
1092 {
1093 unsigned long *bitmap = flags->__vma_flags;
1094
1095 *bitmap |= value;
1096 }
1097
1098 /* Update the first system word of VMA flags clearing bits, non-atomically. */
vma_flags_clear_word(vma_flags_t * flags,unsigned long value)1099 static inline void vma_flags_clear_word(vma_flags_t *flags, unsigned long value)
1100 {
1101 unsigned long *bitmap = flags->__vma_flags;
1102
1103 *bitmap &= ~value;
1104 }
1105
1106 #ifdef CONFIG_NUMA
1107 #define vma_policy(vma) ((vma)->vm_policy)
1108 #else
1109 #define vma_policy(vma) NULL
1110 #endif
1111
1112 /*
1113 * Opaque type representing current mm_struct flag state. Must be accessed via
1114 * mm_flags_xxx() helper functions.
1115 */
1116 #define NUM_MM_FLAG_BITS (64)
1117 typedef struct {
1118 DECLARE_BITMAP(__mm_flags, NUM_MM_FLAG_BITS);
1119 } __private mm_flags_t;
1120
1121 struct kioctx_table;
1122 struct iommu_mm_data;
1123 struct mm_struct {
1124 struct {
1125 /*
1126 * Fields which are often written to are placed in a separate
1127 * cache line.
1128 */
1129 struct {
1130 /**
1131 * @mm_count: The number of references to &struct
1132 * mm_struct (@mm_users count as 1).
1133 *
1134 * Use mmgrab()/mmdrop() to modify. When this drops to
1135 * 0, the &struct mm_struct is freed.
1136 */
1137 atomic_t mm_count;
1138 } ____cacheline_aligned_in_smp;
1139
1140 struct maple_tree mm_mt;
1141
1142 unsigned long mmap_base; /* base of mmap area */
1143 unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
1144 #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES
1145 /* Base addresses for compatible mmap() */
1146 unsigned long mmap_compat_base;
1147 unsigned long mmap_compat_legacy_base;
1148 #endif
1149 unsigned long task_size; /* size of task vm space */
1150 pgd_t * pgd;
1151
1152 #ifdef CONFIG_MEMBARRIER
1153 /**
1154 * @membarrier_state: Flags controlling membarrier behavior.
1155 *
1156 * This field is close to @pgd to hopefully fit in the same
1157 * cache-line, which needs to be touched by switch_mm().
1158 */
1159 atomic_t membarrier_state;
1160 #endif
1161
1162 /**
1163 * @mm_users: The number of users including userspace.
1164 *
1165 * Use mmget()/mmget_not_zero()/mmput() to modify. When this
1166 * drops to 0 (i.e. when the task exits and there are no other
1167 * temporary reference holders), we also release a reference on
1168 * @mm_count (which may then free the &struct mm_struct if
1169 * @mm_count also drops to 0).
1170 */
1171 atomic_t mm_users;
1172
1173 /* MM CID related storage */
1174 struct mm_mm_cid mm_cid;
1175
1176 #ifdef CONFIG_MMU
1177 atomic_long_t pgtables_bytes; /* size of all page tables */
1178 #endif
1179 int map_count; /* number of VMAs */
1180
1181 spinlock_t page_table_lock; /* Protects page tables and some
1182 * counters
1183 */
1184 /*
1185 * Typically the current mmap_lock's offset is 56 bytes from
1186 * the last cacheline boundary, which is very optimal, as
1187 * its two hot fields 'count' and 'owner' sit in 2 different
1188 * cachelines, and when mmap_lock is highly contended, both
1189 * of the 2 fields will be accessed frequently, current layout
1190 * will help to reduce cache bouncing.
1191 *
1192 * So please be careful with adding new fields before
1193 * mmap_lock, which can easily push the 2 fields into one
1194 * cacheline.
1195 */
1196 struct rw_semaphore mmap_lock;
1197
1198 struct list_head mmlist; /* List of maybe swapped mm's. These
1199 * are globally strung together off
1200 * init_mm.mmlist, and are protected
1201 * by mmlist_lock
1202 */
1203 #ifdef CONFIG_PER_VMA_LOCK
1204 struct rcuwait vma_writer_wait;
1205 /*
1206 * This field has lock-like semantics, meaning it is sometimes
1207 * accessed with ACQUIRE/RELEASE semantics.
1208 * Roughly speaking, incrementing the sequence number is
1209 * equivalent to releasing locks on VMAs; reading the sequence
1210 * number can be part of taking a read lock on a VMA.
1211 * Incremented every time mmap_lock is write-locked/unlocked.
1212 * Initialized to 0, therefore odd values indicate mmap_lock
1213 * is write-locked and even values that it's released.
1214 *
1215 * Can be modified under write mmap_lock using RELEASE
1216 * semantics.
1217 * Can be read with no other protection when holding write
1218 * mmap_lock.
1219 * Can be read with ACQUIRE semantics if not holding write
1220 * mmap_lock.
1221 */
1222 seqcount_t mm_lock_seq;
1223 #endif
1224 #ifdef CONFIG_FUTEX_PRIVATE_HASH
1225 struct mutex futex_hash_lock;
1226 struct futex_private_hash __rcu *futex_phash;
1227 struct futex_private_hash *futex_phash_new;
1228 /* futex-ref */
1229 unsigned long futex_batches;
1230 struct rcu_head futex_rcu;
1231 atomic_long_t futex_atomic;
1232 unsigned int __percpu *futex_ref;
1233 #endif
1234
1235 unsigned long hiwater_rss; /* High-watermark of RSS usage */
1236 unsigned long hiwater_vm; /* High-water virtual memory usage */
1237
1238 unsigned long total_vm; /* Total pages mapped */
1239 unsigned long locked_vm; /* Pages that have PG_mlocked set */
1240 atomic64_t pinned_vm; /* Refcount permanently increased */
1241 unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
1242 unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
1243 unsigned long stack_vm; /* VM_STACK */
1244 vm_flags_t def_flags;
1245
1246 /**
1247 * @write_protect_seq: Locked when any thread is write
1248 * protecting pages mapped by this mm to enforce a later COW,
1249 * for instance during page table copying for fork().
1250 */
1251 seqcount_t write_protect_seq;
1252
1253 spinlock_t arg_lock; /* protect the below fields */
1254
1255 unsigned long start_code, end_code, start_data, end_data;
1256 unsigned long start_brk, brk, start_stack;
1257 unsigned long arg_start, arg_end, env_start, env_end;
1258
1259 unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
1260
1261 #ifdef CONFIG_ARCH_HAS_ELF_CORE_EFLAGS
1262 /* the ABI-related flags from the ELF header. Used for core dump */
1263 unsigned long saved_e_flags;
1264 #endif
1265
1266 struct percpu_counter rss_stat[NR_MM_COUNTERS];
1267
1268 struct linux_binfmt *binfmt;
1269
1270 /* Architecture-specific MM context */
1271 mm_context_t context;
1272
1273 mm_flags_t flags; /* Must use mm_flags_* hlpers to access */
1274
1275 #ifdef CONFIG_AIO
1276 spinlock_t ioctx_lock;
1277 struct kioctx_table __rcu *ioctx_table;
1278 #endif
1279 #ifdef CONFIG_MEMCG
1280 /*
1281 * "owner" points to a task that is regarded as the canonical
1282 * user/owner of this mm. All of the following must be true in
1283 * order for it to be changed:
1284 *
1285 * current == mm->owner
1286 * current->mm != mm
1287 * new_owner->mm == mm
1288 * new_owner->alloc_lock is held
1289 */
1290 struct task_struct __rcu *owner;
1291 #endif
1292 struct user_namespace *user_ns;
1293
1294 /* store ref to file /proc/<pid>/exe symlink points to */
1295 struct file __rcu *exe_file;
1296 #ifdef CONFIG_MMU_NOTIFIER
1297 struct mmu_notifier_subscriptions *notifier_subscriptions;
1298 #endif
1299 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
1300 pgtable_t pmd_huge_pte; /* protected by page_table_lock */
1301 #endif
1302 #ifdef CONFIG_NUMA_BALANCING
1303 /*
1304 * numa_next_scan is the next time that PTEs will be remapped
1305 * PROT_NONE to trigger NUMA hinting faults; such faults gather
1306 * statistics and migrate pages to new nodes if necessary.
1307 */
1308 unsigned long numa_next_scan;
1309
1310 /* Restart point for scanning and remapping PTEs. */
1311 unsigned long numa_scan_offset;
1312
1313 /* numa_scan_seq prevents two threads remapping PTEs. */
1314 int numa_scan_seq;
1315 #endif
1316 /*
1317 * An operation with batched TLB flushing is going on. Anything
1318 * that can move process memory needs to flush the TLB when
1319 * moving a PROT_NONE mapped page.
1320 */
1321 atomic_t tlb_flush_pending;
1322 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
1323 /* See flush_tlb_batched_pending() */
1324 atomic_t tlb_flush_batched;
1325 #endif
1326 struct uprobes_state uprobes_state;
1327 #ifdef CONFIG_PREEMPT_RT
1328 struct rcu_head delayed_drop;
1329 #endif
1330 #ifdef CONFIG_HUGETLB_PAGE
1331 atomic_long_t hugetlb_usage;
1332 #endif
1333 struct work_struct async_put_work;
1334
1335 #ifdef CONFIG_IOMMU_MM_DATA
1336 struct iommu_mm_data *iommu_mm;
1337 #endif
1338 #ifdef CONFIG_KSM
1339 /*
1340 * Represent how many pages of this process are involved in KSM
1341 * merging (not including ksm_zero_pages).
1342 */
1343 unsigned long ksm_merging_pages;
1344 /*
1345 * Represent how many pages are checked for ksm merging
1346 * including merged and not merged.
1347 */
1348 unsigned long ksm_rmap_items;
1349 /*
1350 * Represent how many empty pages are merged with kernel zero
1351 * pages when enabling KSM use_zero_pages.
1352 */
1353 atomic_long_t ksm_zero_pages;
1354 #endif /* CONFIG_KSM */
1355 #ifdef CONFIG_LRU_GEN_WALKS_MMU
1356 struct {
1357 /* this mm_struct is on lru_gen_mm_list */
1358 struct list_head list;
1359 /*
1360 * Set when switching to this mm_struct, as a hint of
1361 * whether it has been used since the last time per-node
1362 * page table walkers cleared the corresponding bits.
1363 */
1364 unsigned long bitmap;
1365 #ifdef CONFIG_MEMCG
1366 /* points to the memcg of "owner" above */
1367 struct mem_cgroup *memcg;
1368 #endif
1369 } lru_gen;
1370 #endif /* CONFIG_LRU_GEN_WALKS_MMU */
1371 #ifdef CONFIG_MM_ID
1372 mm_id_t mm_id;
1373 #endif /* CONFIG_MM_ID */
1374 } __randomize_layout;
1375
1376 /*
1377 * The mm_cpumask needs to be at the end of mm_struct, because it
1378 * is dynamically sized based on nr_cpu_ids.
1379 */
1380 char flexible_array[] __aligned(__alignof__(unsigned long));
1381 };
1382
1383 /* Copy value to the first system word of mm flags, non-atomically. */
__mm_flags_overwrite_word(struct mm_struct * mm,unsigned long value)1384 static inline void __mm_flags_overwrite_word(struct mm_struct *mm, unsigned long value)
1385 {
1386 *ACCESS_PRIVATE(&mm->flags, __mm_flags) = value;
1387 }
1388
1389 /* Obtain a read-only view of the mm flags bitmap. */
__mm_flags_get_bitmap(const struct mm_struct * mm)1390 static inline const unsigned long *__mm_flags_get_bitmap(const struct mm_struct *mm)
1391 {
1392 return (const unsigned long *)ACCESS_PRIVATE(&mm->flags, __mm_flags);
1393 }
1394
1395 /* Read the first system word of mm flags, non-atomically. */
__mm_flags_get_word(const struct mm_struct * mm)1396 static inline unsigned long __mm_flags_get_word(const struct mm_struct *mm)
1397 {
1398 return *__mm_flags_get_bitmap(mm);
1399 }
1400
1401 /*
1402 * Update the first system word of mm flags ONLY, applying the specified mask to
1403 * it, then setting all flags specified by bits.
1404 */
__mm_flags_set_mask_bits_word(struct mm_struct * mm,unsigned long mask,unsigned long bits)1405 static inline void __mm_flags_set_mask_bits_word(struct mm_struct *mm,
1406 unsigned long mask, unsigned long bits)
1407 {
1408 unsigned long *bitmap = ACCESS_PRIVATE(&mm->flags, __mm_flags);
1409
1410 set_mask_bits(bitmap, mask, bits);
1411 }
1412
1413 #define MM_MT_FLAGS (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
1414 MT_FLAGS_USE_RCU)
1415 extern struct mm_struct init_mm;
1416
1417 #define MM_STRUCT_FLEXIBLE_ARRAY_INIT \
1418 { \
1419 [0 ... sizeof(cpumask_t) + MM_CID_STATIC_SIZE - 1] = 0 \
1420 }
1421
1422 /* Pointer magic because the dynamic array size confuses some compilers. */
mm_init_cpumask(struct mm_struct * mm)1423 static inline void mm_init_cpumask(struct mm_struct *mm)
1424 {
1425 unsigned long cpu_bitmap = (unsigned long)mm;
1426
1427 cpu_bitmap += offsetof(struct mm_struct, flexible_array);
1428 cpumask_clear((struct cpumask *)cpu_bitmap);
1429 }
1430
1431 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
mm_cpumask(struct mm_struct * mm)1432 static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
1433 {
1434 return (struct cpumask *)&mm->flexible_array;
1435 }
1436
1437 #ifdef CONFIG_LRU_GEN
1438
1439 struct lru_gen_mm_list {
1440 /* mm_struct list for page table walkers */
1441 struct list_head fifo;
1442 /* protects the list above */
1443 spinlock_t lock;
1444 };
1445
1446 #endif /* CONFIG_LRU_GEN */
1447
1448 #ifdef CONFIG_LRU_GEN_WALKS_MMU
1449
1450 void lru_gen_add_mm(struct mm_struct *mm);
1451 void lru_gen_del_mm(struct mm_struct *mm);
1452 void lru_gen_migrate_mm(struct mm_struct *mm);
1453
lru_gen_init_mm(struct mm_struct * mm)1454 static inline void lru_gen_init_mm(struct mm_struct *mm)
1455 {
1456 INIT_LIST_HEAD(&mm->lru_gen.list);
1457 mm->lru_gen.bitmap = 0;
1458 #ifdef CONFIG_MEMCG
1459 mm->lru_gen.memcg = NULL;
1460 #endif
1461 }
1462
lru_gen_use_mm(struct mm_struct * mm)1463 static inline void lru_gen_use_mm(struct mm_struct *mm)
1464 {
1465 /*
1466 * When the bitmap is set, page reclaim knows this mm_struct has been
1467 * used since the last time it cleared the bitmap. So it might be worth
1468 * walking the page tables of this mm_struct to clear the accessed bit.
1469 */
1470 WRITE_ONCE(mm->lru_gen.bitmap, -1);
1471 }
1472
1473 #else /* !CONFIG_LRU_GEN_WALKS_MMU */
1474
lru_gen_add_mm(struct mm_struct * mm)1475 static inline void lru_gen_add_mm(struct mm_struct *mm)
1476 {
1477 }
1478
lru_gen_del_mm(struct mm_struct * mm)1479 static inline void lru_gen_del_mm(struct mm_struct *mm)
1480 {
1481 }
1482
lru_gen_migrate_mm(struct mm_struct * mm)1483 static inline void lru_gen_migrate_mm(struct mm_struct *mm)
1484 {
1485 }
1486
lru_gen_init_mm(struct mm_struct * mm)1487 static inline void lru_gen_init_mm(struct mm_struct *mm)
1488 {
1489 }
1490
lru_gen_use_mm(struct mm_struct * mm)1491 static inline void lru_gen_use_mm(struct mm_struct *mm)
1492 {
1493 }
1494
1495 #endif /* CONFIG_LRU_GEN_WALKS_MMU */
1496
1497 struct vma_iterator {
1498 struct ma_state mas;
1499 };
1500
1501 #define VMA_ITERATOR(name, __mm, __addr) \
1502 struct vma_iterator name = { \
1503 .mas = { \
1504 .tree = &(__mm)->mm_mt, \
1505 .index = __addr, \
1506 .node = NULL, \
1507 .status = ma_start, \
1508 }, \
1509 }
1510
vma_iter_init(struct vma_iterator * vmi,struct mm_struct * mm,unsigned long addr)1511 static inline void vma_iter_init(struct vma_iterator *vmi,
1512 struct mm_struct *mm, unsigned long addr)
1513 {
1514 mas_init(&vmi->mas, &mm->mm_mt, addr);
1515 }
1516
1517 #ifdef CONFIG_SCHED_MM_CID
1518 /*
1519 * mm_cpus_allowed: Union of all mm's threads allowed CPUs.
1520 */
mm_cpus_allowed(struct mm_struct * mm)1521 static inline cpumask_t *mm_cpus_allowed(struct mm_struct *mm)
1522 {
1523 unsigned long bitmap = (unsigned long)mm;
1524
1525 bitmap += offsetof(struct mm_struct, flexible_array);
1526 /* Skip cpu_bitmap */
1527 bitmap += cpumask_size();
1528 return (struct cpumask *)bitmap;
1529 }
1530
1531 /* Accessor for struct mm_struct's cidmask. */
mm_cidmask(struct mm_struct * mm)1532 static inline unsigned long *mm_cidmask(struct mm_struct *mm)
1533 {
1534 unsigned long cid_bitmap = (unsigned long)mm_cpus_allowed(mm);
1535
1536 /* Skip mm_cpus_allowed */
1537 cid_bitmap += cpumask_size();
1538 return (unsigned long *)cid_bitmap;
1539 }
1540
1541 void mm_init_cid(struct mm_struct *mm, struct task_struct *p);
1542
mm_alloc_cid_noprof(struct mm_struct * mm,struct task_struct * p)1543 static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p)
1544 {
1545 mm->mm_cid.pcpu = alloc_percpu_noprof(struct mm_cid_pcpu);
1546 if (!mm->mm_cid.pcpu)
1547 return -ENOMEM;
1548 mm_init_cid(mm, p);
1549 return 0;
1550 }
1551 # define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__))
1552
mm_destroy_cid(struct mm_struct * mm)1553 static inline void mm_destroy_cid(struct mm_struct *mm)
1554 {
1555 free_percpu(mm->mm_cid.pcpu);
1556 mm->mm_cid.pcpu = NULL;
1557 }
1558
mm_cid_size(void)1559 static inline unsigned int mm_cid_size(void)
1560 {
1561 /* mm_cpus_allowed(), mm_cidmask(). */
1562 return cpumask_size() + bitmap_size(num_possible_cpus());
1563 }
1564
1565 /* Use 2 * NR_CPUS as worse case for static allocation. */
1566 # define MM_CID_STATIC_SIZE (2 * sizeof(cpumask_t))
1567 #else /* CONFIG_SCHED_MM_CID */
mm_init_cid(struct mm_struct * mm,struct task_struct * p)1568 static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { }
mm_alloc_cid(struct mm_struct * mm,struct task_struct * p)1569 static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; }
mm_destroy_cid(struct mm_struct * mm)1570 static inline void mm_destroy_cid(struct mm_struct *mm) { }
mm_cid_size(void)1571 static inline unsigned int mm_cid_size(void)
1572 {
1573 return 0;
1574 }
1575 # define MM_CID_STATIC_SIZE 0
1576 #endif /* CONFIG_SCHED_MM_CID */
1577
1578 struct mmu_gather;
1579 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
1580 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
1581 void tlb_gather_mmu_vma(struct mmu_gather *tlb, struct vm_area_struct *vma);
1582 extern void tlb_finish_mmu(struct mmu_gather *tlb);
1583
1584 struct vm_fault;
1585
1586 /**
1587 * typedef vm_fault_t - Return type for page fault handlers.
1588 *
1589 * Page fault handlers return a bitmask of %VM_FAULT values.
1590 */
1591 typedef __bitwise unsigned int vm_fault_t;
1592
1593 /**
1594 * enum vm_fault_reason - Page fault handlers return a bitmask of
1595 * these values to tell the core VM what happened when handling the
1596 * fault. Used to decide whether a process gets delivered SIGBUS or
1597 * just gets major/minor fault counters bumped up.
1598 *
1599 * @VM_FAULT_OOM: Out Of Memory
1600 * @VM_FAULT_SIGBUS: Bad access
1601 * @VM_FAULT_MAJOR: Page read from storage
1602 * @VM_FAULT_HWPOISON: Hit poisoned small page
1603 * @VM_FAULT_HWPOISON_LARGE: Hit poisoned large page. Index encoded
1604 * in upper bits
1605 * @VM_FAULT_SIGSEGV: segmentation fault
1606 * @VM_FAULT_NOPAGE: ->fault installed the pte, not return page
1607 * @VM_FAULT_LOCKED: ->fault locked the returned page
1608 * @VM_FAULT_RETRY: ->fault blocked, must retry
1609 * @VM_FAULT_FALLBACK: huge page fault failed, fall back to small
1610 * @VM_FAULT_DONE_COW: ->fault has fully handled COW
1611 * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs
1612 * fsync() to complete (for synchronous page faults
1613 * in DAX)
1614 * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released
1615 * @VM_FAULT_HINDEX_MASK: mask HINDEX value
1616 *
1617 */
1618 enum vm_fault_reason {
1619 VM_FAULT_OOM = (__force vm_fault_t)0x000001,
1620 VM_FAULT_SIGBUS = (__force vm_fault_t)0x000002,
1621 VM_FAULT_MAJOR = (__force vm_fault_t)0x000004,
1622 VM_FAULT_HWPOISON = (__force vm_fault_t)0x000010,
1623 VM_FAULT_HWPOISON_LARGE = (__force vm_fault_t)0x000020,
1624 VM_FAULT_SIGSEGV = (__force vm_fault_t)0x000040,
1625 VM_FAULT_NOPAGE = (__force vm_fault_t)0x000100,
1626 VM_FAULT_LOCKED = (__force vm_fault_t)0x000200,
1627 VM_FAULT_RETRY = (__force vm_fault_t)0x000400,
1628 VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800,
1629 VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000,
1630 VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000,
1631 VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000,
1632 VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000,
1633 };
1634
1635 /* Encode hstate index for a hwpoisoned large page */
1636 #define VM_FAULT_SET_HINDEX(x) ((__force vm_fault_t)((x) << 16))
1637 #define VM_FAULT_GET_HINDEX(x) (((__force unsigned int)(x) >> 16) & 0xf)
1638
1639 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | \
1640 VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \
1641 VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
1642
1643 #define VM_FAULT_RESULT_TRACE \
1644 { VM_FAULT_OOM, "OOM" }, \
1645 { VM_FAULT_SIGBUS, "SIGBUS" }, \
1646 { VM_FAULT_MAJOR, "MAJOR" }, \
1647 { VM_FAULT_HWPOISON, "HWPOISON" }, \
1648 { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \
1649 { VM_FAULT_SIGSEGV, "SIGSEGV" }, \
1650 { VM_FAULT_NOPAGE, "NOPAGE" }, \
1651 { VM_FAULT_LOCKED, "LOCKED" }, \
1652 { VM_FAULT_RETRY, "RETRY" }, \
1653 { VM_FAULT_FALLBACK, "FALLBACK" }, \
1654 { VM_FAULT_DONE_COW, "DONE_COW" }, \
1655 { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \
1656 { VM_FAULT_COMPLETED, "COMPLETED" }
1657
1658 struct vm_special_mapping {
1659 const char *name; /* The name, e.g. "[vdso]". */
1660
1661 /*
1662 * If .fault is not provided, this points to a
1663 * NULL-terminated array of pages that back the special mapping.
1664 *
1665 * This must not be NULL unless .fault is provided.
1666 */
1667 struct page **pages;
1668
1669 /*
1670 * If non-NULL, then this is called to resolve page faults
1671 * on the special mapping. If used, .pages is not checked.
1672 */
1673 vm_fault_t (*fault)(const struct vm_special_mapping *sm,
1674 struct vm_area_struct *vma,
1675 struct vm_fault *vmf);
1676
1677 int (*mremap)(const struct vm_special_mapping *sm,
1678 struct vm_area_struct *new_vma);
1679
1680 void (*close)(const struct vm_special_mapping *sm,
1681 struct vm_area_struct *vma);
1682 };
1683
1684 enum tlb_flush_reason {
1685 TLB_FLUSH_ON_TASK_SWITCH,
1686 TLB_REMOTE_SHOOTDOWN,
1687 TLB_LOCAL_SHOOTDOWN,
1688 TLB_LOCAL_MM_SHOOTDOWN,
1689 TLB_REMOTE_SEND_IPI,
1690 TLB_REMOTE_WRONG_CPU,
1691 };
1692
1693 /**
1694 * enum fault_flag - Fault flag definitions.
1695 * @FAULT_FLAG_WRITE: Fault was a write fault.
1696 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
1697 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
1698 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
1699 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
1700 * @FAULT_FLAG_TRIED: The fault has been tried once.
1701 * @FAULT_FLAG_USER: The fault originated in userspace.
1702 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
1703 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
1704 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
1705 * @FAULT_FLAG_UNSHARE: The fault is an unsharing request to break COW in a
1706 * COW mapping, making sure that an exclusive anon page is
1707 * mapped after the fault.
1708 * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached.
1709 * We should only access orig_pte if this flag set.
1710 * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock.
1711 *
1712 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
1713 * whether we would allow page faults to retry by specifying these two
1714 * fault flags correctly. Currently there can be three legal combinations:
1715 *
1716 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and
1717 * this is the first try
1718 *
1719 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and
1720 * we've already tried at least once
1721 *
1722 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
1723 *
1724 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
1725 * be used. Note that page faults can be allowed to retry for multiple times,
1726 * in which case we'll have an initial fault with flags (a) then later on
1727 * continuous faults with flags (b). We should always try to detect pending
1728 * signals before a retry to make sure the continuous page faults can still be
1729 * interrupted if necessary.
1730 *
1731 * The combination FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE is illegal.
1732 * FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault when
1733 * applied to mappings that are not COW mappings.
1734 */
1735 enum fault_flag {
1736 FAULT_FLAG_WRITE = 1 << 0,
1737 FAULT_FLAG_MKWRITE = 1 << 1,
1738 FAULT_FLAG_ALLOW_RETRY = 1 << 2,
1739 FAULT_FLAG_RETRY_NOWAIT = 1 << 3,
1740 FAULT_FLAG_KILLABLE = 1 << 4,
1741 FAULT_FLAG_TRIED = 1 << 5,
1742 FAULT_FLAG_USER = 1 << 6,
1743 FAULT_FLAG_REMOTE = 1 << 7,
1744 FAULT_FLAG_INSTRUCTION = 1 << 8,
1745 FAULT_FLAG_INTERRUPTIBLE = 1 << 9,
1746 FAULT_FLAG_UNSHARE = 1 << 10,
1747 FAULT_FLAG_ORIG_PTE_VALID = 1 << 11,
1748 FAULT_FLAG_VMA_LOCK = 1 << 12,
1749 };
1750
1751 typedef unsigned int __bitwise zap_flags_t;
1752
1753 /* Flags for clear_young_dirty_ptes(). */
1754 typedef int __bitwise cydp_t;
1755
1756 /* Clear the access bit */
1757 #define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0))
1758
1759 /* Clear the dirty bit */
1760 #define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1))
1761
1762 /*
1763 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
1764 * other. Here is what they mean, and how to use them:
1765 *
1766 *
1767 * FIXME: For pages which are part of a filesystem, mappings are subject to the
1768 * lifetime enforced by the filesystem and we need guarantees that longterm
1769 * users like RDMA and V4L2 only establish mappings which coordinate usage with
1770 * the filesystem. Ideas for this coordination include revoking the longterm
1771 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was
1772 * added after the problem with filesystems was found FS DAX VMAs are
1773 * specifically failed. Filesystem pages are still subject to bugs and use of
1774 * FOLL_LONGTERM should be avoided on those pages.
1775 *
1776 * In the CMA case: long term pins in a CMA region would unnecessarily fragment
1777 * that region. And so, CMA attempts to migrate the page before pinning, when
1778 * FOLL_LONGTERM is specified.
1779 *
1780 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
1781 * but an additional pin counting system) will be invoked. This is intended for
1782 * anything that gets a page reference and then touches page data (for example,
1783 * Direct IO). This lets the filesystem know that some non-file-system entity is
1784 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
1785 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
1786 * a call to unpin_user_page().
1787 *
1788 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
1789 * and separate refcounting mechanisms, however, and that means that each has
1790 * its own acquire and release mechanisms:
1791 *
1792 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
1793 *
1794 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
1795 *
1796 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
1797 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
1798 * calls applied to them, and that's perfectly OK. This is a constraint on the
1799 * callers, not on the pages.)
1800 *
1801 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
1802 * directly by the caller. That's in order to help avoid mismatches when
1803 * releasing pages: get_user_pages*() pages must be released via put_page(),
1804 * while pin_user_pages*() pages must be released via unpin_user_page().
1805 *
1806 * Please see Documentation/core-api/pin_user_pages.rst for more information.
1807 */
1808
1809 enum {
1810 /* check pte is writable */
1811 FOLL_WRITE = 1 << 0,
1812 /* do get_page on page */
1813 FOLL_GET = 1 << 1,
1814 /* give error on hole if it would be zero */
1815 FOLL_DUMP = 1 << 2,
1816 /* get_user_pages read/write w/o permission */
1817 FOLL_FORCE = 1 << 3,
1818 /*
1819 * if a disk transfer is needed, start the IO and return without waiting
1820 * upon it
1821 */
1822 FOLL_NOWAIT = 1 << 4,
1823 /* do not fault in pages */
1824 FOLL_NOFAULT = 1 << 5,
1825 /* check page is hwpoisoned */
1826 FOLL_HWPOISON = 1 << 6,
1827 /* don't do file mappings */
1828 FOLL_ANON = 1 << 7,
1829 /*
1830 * FOLL_LONGTERM indicates that the page will be held for an indefinite
1831 * time period _often_ under userspace control. This is in contrast to
1832 * iov_iter_get_pages(), whose usages are transient.
1833 */
1834 FOLL_LONGTERM = 1 << 8,
1835 /* split huge pmd before returning */
1836 FOLL_SPLIT_PMD = 1 << 9,
1837 /* allow returning PCI P2PDMA pages */
1838 FOLL_PCI_P2PDMA = 1 << 10,
1839 /* allow interrupts from generic signals */
1840 FOLL_INTERRUPTIBLE = 1 << 11,
1841 /*
1842 * Always honor (trigger) NUMA hinting faults.
1843 *
1844 * FOLL_WRITE implicitly honors NUMA hinting faults because a
1845 * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE
1846 * apply). get_user_pages_fast_only() always implicitly honors NUMA
1847 * hinting faults.
1848 */
1849 FOLL_HONOR_NUMA_FAULT = 1 << 12,
1850
1851 /* See also internal only FOLL flags in mm/internal.h */
1852 };
1853
1854 /* mm flags */
1855
1856 /*
1857 * The first two bits represent core dump modes for set-user-ID,
1858 * the modes are SUID_DUMP_* defined in linux/sched/coredump.h
1859 */
1860 #define MMF_DUMPABLE_BITS 2
1861 #define MMF_DUMPABLE_MASK (BIT(MMF_DUMPABLE_BITS) - 1)
1862 /* coredump filter bits */
1863 #define MMF_DUMP_ANON_PRIVATE 2
1864 #define MMF_DUMP_ANON_SHARED 3
1865 #define MMF_DUMP_MAPPED_PRIVATE 4
1866 #define MMF_DUMP_MAPPED_SHARED 5
1867 #define MMF_DUMP_ELF_HEADERS 6
1868 #define MMF_DUMP_HUGETLB_PRIVATE 7
1869 #define MMF_DUMP_HUGETLB_SHARED 8
1870 #define MMF_DUMP_DAX_PRIVATE 9
1871 #define MMF_DUMP_DAX_SHARED 10
1872
1873 #define MMF_DUMP_FILTER_SHIFT MMF_DUMPABLE_BITS
1874 #define MMF_DUMP_FILTER_BITS 9
1875 #define MMF_DUMP_FILTER_MASK \
1876 ((BIT(MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
1877 #define MMF_DUMP_FILTER_DEFAULT \
1878 (BIT(MMF_DUMP_ANON_PRIVATE) | BIT(MMF_DUMP_ANON_SHARED) | \
1879 BIT(MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
1880
1881 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
1882 # define MMF_DUMP_MASK_DEFAULT_ELF BIT(MMF_DUMP_ELF_HEADERS)
1883 #else
1884 # define MMF_DUMP_MASK_DEFAULT_ELF 0
1885 #endif
1886 /* leave room for more dump flags */
1887 #define MMF_VM_MERGEABLE 16 /* KSM may merge identical pages */
1888 #define MMF_VM_HUGEPAGE 17 /* set when mm is available for khugepaged */
1889
1890 #define MMF_HUGE_ZERO_FOLIO 18 /* mm has ever used the global huge zero folio */
1891
1892 #define MMF_HAS_UPROBES 19 /* has uprobes */
1893 #define MMF_RECALC_UPROBES 20 /* MMF_HAS_UPROBES can be wrong */
1894 #define MMF_OOM_SKIP 21 /* mm is of no interest for the OOM killer */
1895 #define MMF_UNSTABLE 22 /* mm is unstable for copy_from_user */
1896 #define MMF_DISABLE_THP_EXCEPT_ADVISED 23 /* no THP except when advised (e.g., VM_HUGEPAGE) */
1897 #define MMF_DISABLE_THP_COMPLETELY 24 /* no THP for all VMAs */
1898 #define MMF_DISABLE_THP_MASK (BIT(MMF_DISABLE_THP_COMPLETELY) | \
1899 BIT(MMF_DISABLE_THP_EXCEPT_ADVISED))
1900 #define MMF_OOM_REAP_QUEUED 25 /* mm was queued for oom_reaper */
1901 #define MMF_MULTIPROCESS 26 /* mm is shared between processes */
1902 /*
1903 * MMF_HAS_PINNED: Whether this mm has pinned any pages. This can be either
1904 * replaced in the future by mm.pinned_vm when it becomes stable, or grow into
1905 * a counter on its own. We're aggresive on this bit for now: even if the
1906 * pinned pages were unpinned later on, we'll still keep this bit set for the
1907 * lifecycle of this mm, just for simplicity.
1908 */
1909 #define MMF_HAS_PINNED 27 /* FOLL_PIN has run, never cleared */
1910
1911 #define MMF_HAS_MDWE 28
1912 #define MMF_HAS_MDWE_MASK BIT(MMF_HAS_MDWE)
1913
1914 #define MMF_HAS_MDWE_NO_INHERIT 29
1915
1916 #define MMF_VM_MERGE_ANY 30
1917 #define MMF_VM_MERGE_ANY_MASK BIT(MMF_VM_MERGE_ANY)
1918
1919 #define MMF_TOPDOWN 31 /* mm searches top down by default */
1920 #define MMF_TOPDOWN_MASK BIT(MMF_TOPDOWN)
1921
1922 #define MMF_INIT_LEGACY_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
1923 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\
1924 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK)
1925
1926 /* Legacy flags must fit within 32 bits. */
1927 static_assert((u64)MMF_INIT_LEGACY_MASK <= (u64)UINT_MAX);
1928
1929 /*
1930 * Initialise legacy flags according to masks, propagating selected flags on
1931 * fork. Further flag manipulation can be performed by the caller.
1932 */
mmf_init_legacy_flags(unsigned long flags)1933 static inline unsigned long mmf_init_legacy_flags(unsigned long flags)
1934 {
1935 if (flags & (1UL << MMF_HAS_MDWE_NO_INHERIT))
1936 flags &= ~((1UL << MMF_HAS_MDWE) |
1937 (1UL << MMF_HAS_MDWE_NO_INHERIT));
1938 return flags & MMF_INIT_LEGACY_MASK;
1939 }
1940
1941 #endif /* _LINUX_MM_TYPES_H */
1942