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