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