• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MM_H
3 #define _LINUX_MM_H
4 
5 #include <linux/errno.h>
6 #include <linux/mmdebug.h>
7 #include <linux/gfp.h>
8 #include <linux/bug.h>
9 #include <linux/list.h>
10 #include <linux/mmzone.h>
11 #include <linux/rbtree.h>
12 #include <linux/atomic.h>
13 #include <linux/debug_locks.h>
14 #include <linux/mm_types.h>
15 #include <linux/mmap_lock.h>
16 #include <linux/range.h>
17 #include <linux/pfn.h>
18 #include <linux/percpu-refcount.h>
19 #include <linux/bit_spinlock.h>
20 #include <linux/shrinker.h>
21 #include <linux/resource.h>
22 #include <linux/page_ext.h>
23 #include <linux/err.h>
24 #include <linux/page-flags.h>
25 #include <linux/page_ref.h>
26 #include <linux/overflow.h>
27 #include <linux/sizes.h>
28 #include <linux/sched.h>
29 #include <linux/pgtable.h>
30 #include <linux/kasan.h>
31 #include <linux/page_pinner.h>
32 #include <linux/memremap.h>
33 #include <linux/android_kabi.h>
34 
35 struct mempolicy;
36 struct anon_vma;
37 struct anon_vma_chain;
38 struct user_struct;
39 struct pt_regs;
40 
41 extern int sysctl_page_lock_unfairness;
42 
43 void init_mm_internals(void);
44 
45 #ifndef CONFIG_NUMA		/* Don't use mapnrs, do it properly */
46 extern unsigned long max_mapnr;
47 
set_max_mapnr(unsigned long limit)48 static inline void set_max_mapnr(unsigned long limit)
49 {
50 	max_mapnr = limit;
51 }
52 #else
set_max_mapnr(unsigned long limit)53 static inline void set_max_mapnr(unsigned long limit) { }
54 #endif
55 
56 extern atomic_long_t _totalram_pages;
totalram_pages(void)57 static inline unsigned long totalram_pages(void)
58 {
59 	return (unsigned long)atomic_long_read(&_totalram_pages);
60 }
61 
totalram_pages_inc(void)62 static inline void totalram_pages_inc(void)
63 {
64 	atomic_long_inc(&_totalram_pages);
65 }
66 
totalram_pages_dec(void)67 static inline void totalram_pages_dec(void)
68 {
69 	atomic_long_dec(&_totalram_pages);
70 }
71 
totalram_pages_add(long count)72 static inline void totalram_pages_add(long count)
73 {
74 	atomic_long_add(count, &_totalram_pages);
75 }
76 
77 extern void * high_memory;
78 extern int page_cluster;
79 
80 #ifdef CONFIG_SYSCTL
81 extern int sysctl_legacy_va_layout;
82 #else
83 #define sysctl_legacy_va_layout 0
84 #endif
85 
86 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
87 extern const int mmap_rnd_bits_min;
88 extern const int mmap_rnd_bits_max;
89 extern int mmap_rnd_bits __read_mostly;
90 #endif
91 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
92 extern const int mmap_rnd_compat_bits_min;
93 extern const int mmap_rnd_compat_bits_max;
94 extern int mmap_rnd_compat_bits __read_mostly;
95 #endif
96 
97 #include <asm/page.h>
98 #include <asm/processor.h>
99 
100 /*
101  * Architectures that support memory tagging (assigning tags to memory regions,
102  * embedding these tags into addresses that point to these memory regions, and
103  * checking that the memory and the pointer tags match on memory accesses)
104  * redefine this macro to strip tags from pointers.
105  * It's defined as noop for architectures that don't support memory tagging.
106  */
107 #ifndef untagged_addr
108 #define untagged_addr(addr) (addr)
109 #endif
110 
111 #ifndef __pa_symbol
112 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
113 #endif
114 
115 #ifndef page_to_virt
116 #define page_to_virt(x)	__va(PFN_PHYS(page_to_pfn(x)))
117 #endif
118 
119 #ifndef lm_alias
120 #define lm_alias(x)	__va(__pa_symbol(x))
121 #endif
122 
123 /*
124  * To prevent common memory management code establishing
125  * a zero page mapping on a read fault.
126  * This macro should be defined within <asm/pgtable.h>.
127  * s390 does this to prevent multiplexing of hardware bits
128  * related to the physical page in case of virtualization.
129  */
130 #ifndef mm_forbids_zeropage
131 #define mm_forbids_zeropage(X)	(0)
132 #endif
133 
134 /*
135  * On some architectures it is expensive to call memset() for small sizes.
136  * If an architecture decides to implement their own version of
137  * mm_zero_struct_page they should wrap the defines below in a #ifndef and
138  * define their own version of this macro in <asm/pgtable.h>
139  */
140 #if BITS_PER_LONG == 64
141 /* This function must be updated when the size of struct page grows above 96
142  * or reduces below 56. The idea that compiler optimizes out switch()
143  * statement, and only leaves move/store instructions. Also the compiler can
144  * combine write statements if they are both assignments and can be reordered,
145  * this can result in several of the writes here being dropped.
146  */
147 #define	mm_zero_struct_page(pp) __mm_zero_struct_page(pp)
__mm_zero_struct_page(struct page * page)148 static inline void __mm_zero_struct_page(struct page *page)
149 {
150 	unsigned long *_pp = (void *)page;
151 
152 	 /* Check that struct page is either 56, 64, 72, 80, 88 or 96 bytes */
153 	BUILD_BUG_ON(sizeof(struct page) & 7);
154 	BUILD_BUG_ON(sizeof(struct page) < 56);
155 	BUILD_BUG_ON(sizeof(struct page) > 96);
156 
157 	switch (sizeof(struct page)) {
158 	case 96:
159 		_pp[11] = 0;
160 		fallthrough;
161 	case 88:
162 		_pp[10] = 0;
163 		fallthrough;
164 	case 80:
165 		_pp[9] = 0;
166 		fallthrough;
167 	case 72:
168 		_pp[8] = 0;
169 		fallthrough;
170 	case 64:
171 		_pp[7] = 0;
172 		fallthrough;
173 	case 56:
174 		_pp[6] = 0;
175 		_pp[5] = 0;
176 		_pp[4] = 0;
177 		_pp[3] = 0;
178 		_pp[2] = 0;
179 		_pp[1] = 0;
180 		_pp[0] = 0;
181 	}
182 }
183 #else
184 #define mm_zero_struct_page(pp)  ((void)memset((pp), 0, sizeof(struct page)))
185 #endif
186 
187 /*
188  * Default maximum number of active map areas, this limits the number of vmas
189  * per mm struct. Users can overwrite this number by sysctl but there is a
190  * problem.
191  *
192  * When a program's coredump is generated as ELF format, a section is created
193  * per a vma. In ELF, the number of sections is represented in unsigned short.
194  * This means the number of sections should be smaller than 65535 at coredump.
195  * Because the kernel adds some informative sections to a image of program at
196  * generating coredump, we need some margin. The number of extra sections is
197  * 1-3 now and depends on arch. We use "5" as safe margin, here.
198  *
199  * ELF extended numbering allows more than 65535 sections, so 16-bit bound is
200  * not a hard limit any more. Although some userspace tools can be surprised by
201  * that.
202  */
203 #define MAPCOUNT_ELF_CORE_MARGIN	(5)
204 #define DEFAULT_MAX_MAP_COUNT	(USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN)
205 
206 extern int sysctl_max_map_count;
207 
208 extern unsigned long sysctl_user_reserve_kbytes;
209 extern unsigned long sysctl_admin_reserve_kbytes;
210 
211 extern int sysctl_overcommit_memory;
212 extern int sysctl_overcommit_ratio;
213 extern unsigned long sysctl_overcommit_kbytes;
214 
215 int overcommit_ratio_handler(struct ctl_table *, int, void *, size_t *,
216 		loff_t *);
217 int overcommit_kbytes_handler(struct ctl_table *, int, void *, size_t *,
218 		loff_t *);
219 int overcommit_policy_handler(struct ctl_table *, int, void *, size_t *,
220 		loff_t *);
221 
222 #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
223 #define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
224 #define folio_page_idx(folio, p)	(page_to_pfn(p) - folio_pfn(folio))
225 #else
226 #define nth_page(page,n) ((page) + (n))
227 #define folio_page_idx(folio, p)	((p) - &(folio)->page)
228 #endif
229 
230 /* to align the pointer to the (next) page boundary */
231 #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
232 
233 /* to align the pointer to the (prev) page boundary */
234 #define PAGE_ALIGN_DOWN(addr) ALIGN_DOWN(addr, PAGE_SIZE)
235 
236 /* test whether an address (unsigned long or pointer) is aligned to PAGE_SIZE */
237 #define PAGE_ALIGNED(addr)	IS_ALIGNED((unsigned long)(addr), PAGE_SIZE)
238 
239 #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
lru_to_folio(struct list_head * head)240 static inline struct folio *lru_to_folio(struct list_head *head)
241 {
242 	return list_entry((head)->prev, struct folio, lru);
243 }
244 
245 void setup_initial_init_mm(void *start_code, void *end_code,
246 			   void *end_data, void *brk);
247 
248 /*
249  * Linux kernel virtual memory manager primitives.
250  * The idea being to have a "virtual" mm in the same way
251  * we have a virtual fs - giving a cleaner interface to the
252  * mm details, and allowing different kinds of memory mappings
253  * (from shared memory to executable loading to arbitrary
254  * mmap() functions).
255  */
256 
257 struct vm_area_struct *vm_area_alloc(struct mm_struct *);
258 struct vm_area_struct *vm_area_dup(struct vm_area_struct *);
259 void vm_area_free(struct vm_area_struct *);
260 /* Use only if VMA has no other users */
261 void __vm_area_free(struct vm_area_struct *vma);
262 
263 #ifndef CONFIG_MMU
264 extern struct rb_root nommu_region_tree;
265 extern struct rw_semaphore nommu_region_sem;
266 
267 extern unsigned int kobjsize(const void *objp);
268 #endif
269 
270 /*
271  * vm_flags in vm_area_struct, see mm_types.h.
272  * When changing, update also include/trace/events/mmflags.h
273  */
274 #define VM_NONE		0x00000000
275 
276 #define VM_READ		0x00000001	/* currently active flags */
277 #define VM_WRITE	0x00000002
278 #define VM_EXEC		0x00000004
279 #define VM_SHARED	0x00000008
280 
281 /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
282 #define VM_MAYREAD	0x00000010	/* limits for mprotect() etc */
283 #define VM_MAYWRITE	0x00000020
284 #define VM_MAYEXEC	0x00000040
285 #define VM_MAYSHARE	0x00000080
286 
287 #define VM_GROWSDOWN	0x00000100	/* general info on the segment */
288 #define VM_UFFD_MISSING	0x00000200	/* missing pages tracking */
289 #define VM_PFNMAP	0x00000400	/* Page-ranges managed without "struct page", just pure PFN */
290 #define VM_UFFD_WP	0x00001000	/* wrprotect pages tracking */
291 
292 #define VM_LOCKED	0x00002000
293 #define VM_IO           0x00004000	/* Memory mapped I/O or similar */
294 
295 					/* Used by sys_madvise() */
296 #define VM_SEQ_READ	0x00008000	/* App will access data sequentially */
297 #define VM_RAND_READ	0x00010000	/* App will not benefit from clustered reads */
298 
299 #define VM_DONTCOPY	0x00020000      /* Do not copy this vma on fork */
300 #define VM_DONTEXPAND	0x00040000	/* Cannot expand with mremap() */
301 #define VM_LOCKONFAULT	0x00080000	/* Lock the pages covered when they are faulted in */
302 #define VM_ACCOUNT	0x00100000	/* Is a VM accounted object */
303 #define VM_NORESERVE	0x00200000	/* should the VM suppress accounting */
304 #define VM_HUGETLB	0x00400000	/* Huge TLB Page VM */
305 #define VM_SYNC		0x00800000	/* Synchronous page faults */
306 #define VM_ARCH_1	0x01000000	/* Architecture-specific flag */
307 #define VM_WIPEONFORK	0x02000000	/* Wipe VMA contents in child. */
308 #define VM_DONTDUMP	0x04000000	/* Do not include in the core dump */
309 
310 #ifdef CONFIG_MEM_SOFT_DIRTY
311 # define VM_SOFTDIRTY	0x08000000	/* Not soft dirty clean area */
312 #else
313 # define VM_SOFTDIRTY	0
314 #endif
315 
316 #define VM_MIXEDMAP	0x10000000	/* Can contain "struct page" and pure PFN pages */
317 #define VM_HUGEPAGE	0x20000000	/* MADV_HUGEPAGE marked this vma */
318 #define VM_NOHUGEPAGE	0x40000000	/* MADV_NOHUGEPAGE marked this vma */
319 #define VM_MERGEABLE	0x80000000	/* KSM may merge identical pages */
320 
321 #ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS
322 #define VM_HIGH_ARCH_BIT_0	32	/* bit only usable on 64-bit architectures */
323 #define VM_HIGH_ARCH_BIT_1	33	/* bit only usable on 64-bit architectures */
324 #define VM_HIGH_ARCH_BIT_2	34	/* bit only usable on 64-bit architectures */
325 #define VM_HIGH_ARCH_BIT_3	35	/* bit only usable on 64-bit architectures */
326 #define VM_HIGH_ARCH_BIT_4	36	/* bit only usable on 64-bit architectures */
327 #define VM_HIGH_ARCH_0	BIT(VM_HIGH_ARCH_BIT_0)
328 #define VM_HIGH_ARCH_1	BIT(VM_HIGH_ARCH_BIT_1)
329 #define VM_HIGH_ARCH_2	BIT(VM_HIGH_ARCH_BIT_2)
330 #define VM_HIGH_ARCH_3	BIT(VM_HIGH_ARCH_BIT_3)
331 #define VM_HIGH_ARCH_4	BIT(VM_HIGH_ARCH_BIT_4)
332 #endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */
333 
334 #ifdef CONFIG_ARCH_HAS_PKEYS
335 # define VM_PKEY_SHIFT	VM_HIGH_ARCH_BIT_0
336 # define VM_PKEY_BIT0	VM_HIGH_ARCH_0	/* A protection key is a 4-bit value */
337 # define VM_PKEY_BIT1	VM_HIGH_ARCH_1	/* on x86 and 5-bit value on ppc64   */
338 # define VM_PKEY_BIT2	VM_HIGH_ARCH_2
339 # define VM_PKEY_BIT3	VM_HIGH_ARCH_3
340 #ifdef CONFIG_PPC
341 # define VM_PKEY_BIT4  VM_HIGH_ARCH_4
342 #else
343 # define VM_PKEY_BIT4  0
344 #endif
345 #endif /* CONFIG_ARCH_HAS_PKEYS */
346 
347 #if defined(CONFIG_X86)
348 # define VM_PAT		VM_ARCH_1	/* PAT reserves whole VMA at once (x86) */
349 #elif defined(CONFIG_PPC)
350 # define VM_SAO		VM_ARCH_1	/* Strong Access Ordering (powerpc) */
351 #elif defined(CONFIG_PARISC)
352 # define VM_GROWSUP	VM_ARCH_1
353 #elif defined(CONFIG_IA64)
354 # define VM_GROWSUP	VM_ARCH_1
355 #elif defined(CONFIG_SPARC64)
356 # define VM_SPARC_ADI	VM_ARCH_1	/* Uses ADI tag for access control */
357 # define VM_ARCH_CLEAR	VM_SPARC_ADI
358 #elif defined(CONFIG_ARM64)
359 # define VM_ARM64_BTI	VM_ARCH_1	/* BTI guarded page, a.k.a. GP bit */
360 # define VM_ARCH_CLEAR	VM_ARM64_BTI
361 #elif !defined(CONFIG_MMU)
362 # define VM_MAPPED_COPY	VM_ARCH_1	/* T if mapped copy of data (nommu mmap) */
363 #endif
364 
365 #if defined(CONFIG_ARM64_MTE)
366 # define VM_MTE		VM_HIGH_ARCH_0	/* Use Tagged memory for access control */
367 # define VM_MTE_ALLOWED	VM_HIGH_ARCH_1	/* Tagged memory permitted */
368 #else
369 # define VM_MTE		VM_NONE
370 # define VM_MTE_ALLOWED	VM_NONE
371 #endif
372 
373 #ifndef VM_GROWSUP
374 # define VM_GROWSUP	VM_NONE
375 #endif
376 
377 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
378 # define VM_UFFD_MINOR_BIT	37
379 # define VM_UFFD_MINOR		BIT(VM_UFFD_MINOR_BIT)	/* UFFD minor faults */
380 #else /* !CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
381 # define VM_UFFD_MINOR		VM_NONE
382 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
383 
384 /* Bits set in the VMA until the stack is in its final location */
385 #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ | VM_STACK_EARLY)
386 
387 #define TASK_EXEC ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0)
388 
389 /* Common data flag combinations */
390 #define VM_DATA_FLAGS_TSK_EXEC	(VM_READ | VM_WRITE | TASK_EXEC | \
391 				 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
392 #define VM_DATA_FLAGS_NON_EXEC	(VM_READ | VM_WRITE | VM_MAYREAD | \
393 				 VM_MAYWRITE | VM_MAYEXEC)
394 #define VM_DATA_FLAGS_EXEC	(VM_READ | VM_WRITE | VM_EXEC | \
395 				 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
396 
397 #ifndef VM_DATA_DEFAULT_FLAGS		/* arch can override this */
398 #define VM_DATA_DEFAULT_FLAGS  VM_DATA_FLAGS_EXEC
399 #endif
400 
401 #ifndef VM_STACK_DEFAULT_FLAGS		/* arch can override this */
402 #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
403 #endif
404 
405 #ifdef CONFIG_STACK_GROWSUP
406 #define VM_STACK	VM_GROWSUP
407 #define VM_STACK_EARLY	VM_GROWSDOWN
408 #else
409 #define VM_STACK	VM_GROWSDOWN
410 #define VM_STACK_EARLY	0
411 #endif
412 
413 #define VM_STACK_FLAGS	(VM_STACK | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
414 
415 /* VMA basic access permission flags */
416 #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
417 
418 
419 /*
420  * Special vmas that are non-mergable, non-mlock()able.
421  */
422 #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP)
423 
424 /* This mask prevents VMA from being scanned with khugepaged */
425 #define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB)
426 
427 /* This mask defines which mm->def_flags a process can inherit its parent */
428 #define VM_INIT_DEF_MASK	VM_NOHUGEPAGE
429 
430 /* This mask represents all the VMA flag bits used by mlock */
431 #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
432 
433 /* Arch-specific flags to clear when updating VM flags on protection change */
434 #ifndef VM_ARCH_CLEAR
435 # define VM_ARCH_CLEAR	VM_NONE
436 #endif
437 #define VM_FLAGS_CLEAR	(ARCH_VM_PKEY_FLAGS | VM_ARCH_CLEAR)
438 
439 /*
440  * mapping from the currently active vm_flags protection bits (the
441  * low four bits) to a page protection mask..
442  */
443 
444 /*
445  * The default fault flags that should be used by most of the
446  * arch-specific page fault handlers.
447  */
448 #define FAULT_FLAG_DEFAULT  (FAULT_FLAG_ALLOW_RETRY | \
449 			     FAULT_FLAG_KILLABLE | \
450 			     FAULT_FLAG_INTERRUPTIBLE)
451 
452 /**
453  * fault_flag_allow_retry_first - check ALLOW_RETRY the first time
454  * @flags: Fault flags.
455  *
456  * This is mostly used for places where we want to try to avoid taking
457  * the mmap_lock for too long a time when waiting for another condition
458  * to change, in which case we can try to be polite to release the
459  * mmap_lock in the first round to avoid potential starvation of other
460  * processes that would also want the mmap_lock.
461  *
462  * Return: true if the page fault allows retry and this is the first
463  * attempt of the fault handling; false otherwise.
464  */
fault_flag_allow_retry_first(enum fault_flag flags)465 static inline bool fault_flag_allow_retry_first(enum fault_flag flags)
466 {
467 	return (flags & FAULT_FLAG_ALLOW_RETRY) &&
468 	    (!(flags & FAULT_FLAG_TRIED));
469 }
470 
471 #define FAULT_FLAG_TRACE \
472 	{ FAULT_FLAG_WRITE,		"WRITE" }, \
473 	{ FAULT_FLAG_MKWRITE,		"MKWRITE" }, \
474 	{ FAULT_FLAG_ALLOW_RETRY,	"ALLOW_RETRY" }, \
475 	{ FAULT_FLAG_RETRY_NOWAIT,	"RETRY_NOWAIT" }, \
476 	{ FAULT_FLAG_KILLABLE,		"KILLABLE" }, \
477 	{ FAULT_FLAG_TRIED,		"TRIED" }, \
478 	{ FAULT_FLAG_USER,		"USER" }, \
479 	{ FAULT_FLAG_REMOTE,		"REMOTE" }, \
480 	{ FAULT_FLAG_INSTRUCTION,	"INSTRUCTION" }, \
481 	{ FAULT_FLAG_INTERRUPTIBLE,	"INTERRUPTIBLE" }, \
482 	{ FAULT_FLAG_VMA_LOCK,		"VMA_LOCK" }
483 
484 /*
485  * vm_fault is filled by the pagefault handler and passed to the vma's
486  * ->fault function. The vma's ->fault is responsible for returning a bitmask
487  * of VM_FAULT_xxx flags that give details about how the fault was handled.
488  *
489  * MM layer fills up gfp_mask for page allocations but fault handler might
490  * alter it if its implementation requires a different allocation context.
491  *
492  * pgoff should be used in favour of virtual_address, if possible.
493  */
494 struct vm_fault {
495 	const struct {
496 		struct vm_area_struct *vma;	/* Target VMA */
497 		gfp_t gfp_mask;			/* gfp mask to be used for allocations */
498 		pgoff_t pgoff;			/* Logical page offset based on vma */
499 		unsigned long address;		/* Faulting virtual address - masked */
500 		unsigned long real_address;	/* Faulting virtual address - unmasked */
501 	};
502 	enum fault_flag flags;		/* FAULT_FLAG_xxx flags
503 					 * XXX: should really be 'const' */
504 	pmd_t *pmd;			/* Pointer to pmd entry matching
505 					 * the 'address' */
506 	pud_t *pud;			/* Pointer to pud entry matching
507 					 * the 'address'
508 					 */
509 	union {
510 		pte_t orig_pte;		/* Value of PTE at the time of fault */
511 		pmd_t orig_pmd;		/* Value of PMD at the time of fault,
512 					 * used by PMD fault only.
513 					 */
514 	};
515 
516 	struct page *cow_page;		/* Page handler may use for COW fault */
517 	struct page *page;		/* ->fault handlers should return a
518 					 * page here, unless VM_FAULT_NOPAGE
519 					 * is set (which is also implied by
520 					 * VM_FAULT_ERROR).
521 					 */
522 	/* These three entries are valid only while holding ptl lock */
523 	pte_t *pte;			/* Pointer to pte entry matching
524 					 * the 'address'. NULL if the page
525 					 * table hasn't been allocated.
526 					 */
527 	spinlock_t *ptl;		/* Page table lock.
528 					 * Protects pte page table if 'pte'
529 					 * is not NULL, otherwise pmd.
530 					 */
531 	pgtable_t prealloc_pte;		/* Pre-allocated pte page table.
532 					 * vm_ops->map_pages() sets up a page
533 					 * table from atomic context.
534 					 * do_fault_around() pre-allocates
535 					 * page table to avoid allocation from
536 					 * atomic context.
537 					 */
538 };
539 
540 /* page entry size for vm->huge_fault() */
541 enum page_entry_size {
542 	PE_SIZE_PTE = 0,
543 	PE_SIZE_PMD,
544 	PE_SIZE_PUD,
545 };
546 
547 /*
548  * These are the virtual MM functions - opening of an area, closing and
549  * unmapping it (needed to keep files on disk up-to-date etc), pointer
550  * to the functions called when a no-page or a wp-page exception occurs.
551  */
552 struct vm_operations_struct {
553 	void (*open)(struct vm_area_struct * area);
554 	/**
555 	 * @close: Called when the VMA is being removed from the MM.
556 	 * Context: User context.  May sleep.  Caller holds mmap_lock.
557 	 */
558 	void (*close)(struct vm_area_struct * area);
559 	/* Called any time before splitting to check if it's allowed */
560 	int (*may_split)(struct vm_area_struct *area, unsigned long addr);
561 	int (*mremap)(struct vm_area_struct *area);
562 	/*
563 	 * Called by mprotect() to make driver-specific permission
564 	 * checks before mprotect() is finalised.   The VMA must not
565 	 * be modified.  Returns 0 if eprotect() can proceed.
566 	 */
567 	int (*mprotect)(struct vm_area_struct *vma, unsigned long start,
568 			unsigned long end, unsigned long newflags);
569 	vm_fault_t (*fault)(struct vm_fault *vmf);
570 	vm_fault_t (*huge_fault)(struct vm_fault *vmf,
571 			enum page_entry_size pe_size);
572 	vm_fault_t (*map_pages)(struct vm_fault *vmf,
573 			pgoff_t start_pgoff, pgoff_t end_pgoff);
574 	unsigned long (*pagesize)(struct vm_area_struct * area);
575 
576 	/* notification that a previously read-only page is about to become
577 	 * writable, if an error is returned it will cause a SIGBUS */
578 	vm_fault_t (*page_mkwrite)(struct vm_fault *vmf);
579 
580 	/* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */
581 	vm_fault_t (*pfn_mkwrite)(struct vm_fault *vmf);
582 
583 	/* called by access_process_vm when get_user_pages() fails, typically
584 	 * for use by special VMAs. See also generic_access_phys() for a generic
585 	 * implementation useful for any iomem mapping.
586 	 */
587 	int (*access)(struct vm_area_struct *vma, unsigned long addr,
588 		      void *buf, int len, int write);
589 
590 	/* Called by the /proc/PID/maps code to ask the vma whether it
591 	 * has a special name.  Returning non-NULL will also cause this
592 	 * vma to be dumped unconditionally. */
593 	const char *(*name)(struct vm_area_struct *vma);
594 
595 #ifdef CONFIG_NUMA
596 	/*
597 	 * set_policy() op must add a reference to any non-NULL @new mempolicy
598 	 * to hold the policy upon return.  Caller should pass NULL @new to
599 	 * remove a policy and fall back to surrounding context--i.e. do not
600 	 * install a MPOL_DEFAULT policy, nor the task or system default
601 	 * mempolicy.
602 	 */
603 	int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
604 
605 	/*
606 	 * get_policy() op must add reference [mpol_get()] to any policy at
607 	 * (vma,addr) marked as MPOL_SHARED.  The shared policy infrastructure
608 	 * in mm/mempolicy.c will do this automatically.
609 	 * get_policy() must NOT add a ref if the policy at (vma,addr) is not
610 	 * marked as MPOL_SHARED. vma policies are protected by the mmap_lock.
611 	 * If no [shared/vma] mempolicy exists at the addr, get_policy() op
612 	 * must return NULL--i.e., do not "fallback" to task or system default
613 	 * policy.
614 	 */
615 	struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
616 					unsigned long addr);
617 #endif
618 	/*
619 	 * Called by vm_normal_page() for special PTEs to find the
620 	 * page for @addr.  This is useful if the default behavior
621 	 * (using pte_page()) would not find the correct page.
622 	 */
623 	struct page *(*find_special_page)(struct vm_area_struct *vma,
624 					  unsigned long addr);
625 
626 	ANDROID_KABI_RESERVE(1);
627 	ANDROID_KABI_RESERVE(2);
628 	ANDROID_KABI_RESERVE(3);
629 	ANDROID_KABI_RESERVE(4);
630 };
631 
632 #ifdef CONFIG_PER_VMA_LOCK
633 /*
634  * Try to read-lock a vma. The function is allowed to occasionally yield false
635  * locked result to avoid performance overhead, in which case we fall back to
636  * using mmap_lock. The function should never yield false unlocked result.
637  */
vma_start_read(struct vm_area_struct * vma)638 static inline bool vma_start_read(struct vm_area_struct *vma)
639 {
640 	/*
641 	 * Check before locking. A race might cause false locked result.
642 	 * We can use READ_ONCE() for the mm_lock_seq here, and don't need
643 	 * ACQUIRE semantics, because this is just a lockless check whose result
644 	 * we don't rely on for anything - the mm_lock_seq read against which we
645 	 * need ordering is below.
646 	 */
647 	if (READ_ONCE(vma->vm_lock_seq) == READ_ONCE(vma->vm_mm->mm_lock_seq))
648 		return false;
649 
650 	if (unlikely(down_read_trylock(&vma->vm_lock->lock) == 0))
651 		return false;
652 
653 	/*
654 	 * Overflow might produce false locked result.
655 	 * False unlocked result is impossible because we modify and check
656 	 * vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq
657 	 * modification invalidates all existing locks.
658 	 *
659 	 * We must use ACQUIRE semantics for the mm_lock_seq so that if we are
660 	 * racing with vma_end_write_all(), we only start reading from the VMA
661 	 * after it has been unlocked.
662 	 * This pairs with RELEASE semantics in vma_end_write_all().
663 	 */
664 	if (unlikely(vma->vm_lock_seq == smp_load_acquire(&vma->vm_mm->mm_lock_seq))) {
665 		up_read(&vma->vm_lock->lock);
666 		return false;
667 	}
668 	return true;
669 }
670 
vma_end_read(struct vm_area_struct * vma)671 static inline void vma_end_read(struct vm_area_struct *vma)
672 {
673 	rcu_read_lock(); /* keeps vma alive till the end of up_read */
674 	up_read(&vma->vm_lock->lock);
675 	rcu_read_unlock();
676 }
677 
678 /* WARNING! Can only be used if mmap_lock is expected to be write-locked */
__is_vma_write_locked(struct vm_area_struct * vma,int * mm_lock_seq)679 static bool __is_vma_write_locked(struct vm_area_struct *vma, int *mm_lock_seq)
680 {
681 	mmap_assert_write_locked(vma->vm_mm);
682 
683 	/*
684 	 * current task is holding mmap_write_lock, both vma->vm_lock_seq and
685 	 * mm->mm_lock_seq can't be concurrently modified.
686 	 */
687 	*mm_lock_seq = vma->vm_mm->mm_lock_seq;
688 	return (vma->vm_lock_seq == *mm_lock_seq);
689 }
690 
691 /*
692  * Begin writing to a VMA.
693  * Exclude concurrent readers under the per-VMA lock until the currently
694  * write-locked mmap_lock is dropped or downgraded.
695  */
vma_start_write(struct vm_area_struct * vma)696 static inline void vma_start_write(struct vm_area_struct *vma)
697 {
698 	int mm_lock_seq;
699 
700 	if (__is_vma_write_locked(vma, &mm_lock_seq))
701 		return;
702 
703 	down_write(&vma->vm_lock->lock);
704 	/*
705 	 * We should use WRITE_ONCE() here because we can have concurrent reads
706 	 * from the early lockless pessimistic check in vma_start_read().
707 	 * We don't really care about the correctness of that early check, but
708 	 * we should use WRITE_ONCE() for cleanliness and to keep KCSAN happy.
709 	 */
710 	WRITE_ONCE(vma->vm_lock_seq, mm_lock_seq);
711 	up_write(&vma->vm_lock->lock);
712 }
713 
vma_try_start_write(struct vm_area_struct * vma)714 static inline bool vma_try_start_write(struct vm_area_struct *vma)
715 {
716 	int mm_lock_seq;
717 
718 	if (__is_vma_write_locked(vma, &mm_lock_seq))
719 		return true;
720 
721 	if (!down_write_trylock(&vma->vm_lock->lock))
722 		return false;
723 
724 	WRITE_ONCE(vma->vm_lock_seq, mm_lock_seq);
725 	up_write(&vma->vm_lock->lock);
726 	return true;
727 }
728 
vma_assert_write_locked(struct vm_area_struct * vma)729 static inline void vma_assert_write_locked(struct vm_area_struct *vma)
730 {
731 	int mm_lock_seq;
732 
733 	VM_BUG_ON_VMA(!__is_vma_write_locked(vma, &mm_lock_seq), vma);
734 }
735 
vma_assert_locked(struct vm_area_struct * vma)736 static inline void vma_assert_locked(struct vm_area_struct *vma)
737 {
738 	if (!rwsem_is_locked(&vma->vm_lock->lock))
739 		vma_assert_write_locked(vma);
740 }
741 
vma_mark_detached(struct vm_area_struct * vma,bool detached)742 static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached)
743 {
744 	/* When detaching vma should be write-locked */
745 	if (detached)
746 		vma_assert_write_locked(vma);
747 	vma->detached = detached;
748 }
749 
release_fault_lock(struct vm_fault * vmf)750 static inline void release_fault_lock(struct vm_fault *vmf)
751 {
752 	if (vmf->flags & FAULT_FLAG_VMA_LOCK)
753 		vma_end_read(vmf->vma);
754 	else
755 		mmap_read_unlock(vmf->vma->vm_mm);
756 }
757 
assert_fault_locked(struct vm_fault * vmf)758 static inline void assert_fault_locked(struct vm_fault *vmf)
759 {
760 	if (vmf->flags & FAULT_FLAG_VMA_LOCK)
761 		vma_assert_locked(vmf->vma);
762 	else
763 		mmap_assert_locked(vmf->vma->vm_mm);
764 }
765 
766 struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
767 					  unsigned long address);
768 
769 #else /* CONFIG_PER_VMA_LOCK */
770 
vma_init_lock(struct vm_area_struct * vma)771 static inline void vma_init_lock(struct vm_area_struct *vma) {}
vma_start_read(struct vm_area_struct * vma)772 static inline bool vma_start_read(struct vm_area_struct *vma)
773 		{ return false; }
vma_end_read(struct vm_area_struct * vma)774 static inline void vma_end_read(struct vm_area_struct *vma) {}
vma_start_write(struct vm_area_struct * vma)775 static inline void vma_start_write(struct vm_area_struct *vma) {}
vma_try_start_write(struct vm_area_struct * vma)776 static inline bool vma_try_start_write(struct vm_area_struct *vma)
777 		{ return true; }
vma_assert_write_locked(struct vm_area_struct * vma)778 static inline void vma_assert_write_locked(struct vm_area_struct *vma)
779 		{ mmap_assert_write_locked(vma->vm_mm); }
vma_mark_detached(struct vm_area_struct * vma,bool detached)780 static inline void vma_mark_detached(struct vm_area_struct *vma,
781 				     bool detached) {}
782 
release_fault_lock(struct vm_fault * vmf)783 static inline void release_fault_lock(struct vm_fault *vmf)
784 {
785 	mmap_read_unlock(vmf->vma->vm_mm);
786 }
787 
assert_fault_locked(struct vm_fault * vmf)788 static inline void assert_fault_locked(struct vm_fault *vmf)
789 {
790 	mmap_assert_locked(vmf->vma->vm_mm);
791 }
792 
lock_vma_under_rcu(struct mm_struct * mm,unsigned long address)793 static inline struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
794 		unsigned long address)
795 {
796 	return NULL;
797 }
798 
799 #endif /* CONFIG_PER_VMA_LOCK */
800 
801 /*
802  * WARNING: vma_init does not initialize vma->vm_lock.
803  * Use vm_area_alloc()/vm_area_free() if vma needs locking.
804  */
vma_init(struct vm_area_struct * vma,struct mm_struct * mm)805 static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)
806 {
807 	static const struct vm_operations_struct dummy_vm_ops = {};
808 
809 	memset(vma, 0, sizeof(*vma));
810 	vma->vm_mm = mm;
811 	vma->vm_ops = &dummy_vm_ops;
812 	INIT_LIST_HEAD(&vma->anon_vma_chain);
813 	vma_mark_detached(vma, false);
814 }
815 
816 /* Use when VMA is not part of the VMA tree and needs no locking */
vm_flags_init(struct vm_area_struct * vma,vm_flags_t flags)817 static inline void vm_flags_init(struct vm_area_struct *vma,
818 				 vm_flags_t flags)
819 {
820 	ACCESS_PRIVATE(vma, __vm_flags) = flags;
821 }
822 
823 /*
824  * Use when VMA is part of the VMA tree and modifications need coordination
825  * Note: vm_flags_reset and vm_flags_reset_once do not lock the vma and
826  * it should be locked explicitly beforehand.
827  */
vm_flags_reset(struct vm_area_struct * vma,vm_flags_t flags)828 static inline void vm_flags_reset(struct vm_area_struct *vma,
829 				  vm_flags_t flags)
830 {
831 	vma_assert_write_locked(vma);
832 	vm_flags_init(vma, flags);
833 }
834 
vm_flags_reset_once(struct vm_area_struct * vma,vm_flags_t flags)835 static inline void vm_flags_reset_once(struct vm_area_struct *vma,
836 				       vm_flags_t flags)
837 {
838 	vma_assert_write_locked(vma);
839 	WRITE_ONCE(ACCESS_PRIVATE(vma, __vm_flags), flags);
840 }
841 
vm_flags_set(struct vm_area_struct * vma,vm_flags_t flags)842 static inline void vm_flags_set(struct vm_area_struct *vma,
843 				vm_flags_t flags)
844 {
845 	vma_start_write(vma);
846 	ACCESS_PRIVATE(vma, __vm_flags) |= flags;
847 }
848 
vm_flags_clear(struct vm_area_struct * vma,vm_flags_t flags)849 static inline void vm_flags_clear(struct vm_area_struct *vma,
850 				  vm_flags_t flags)
851 {
852 	vma_start_write(vma);
853 	ACCESS_PRIVATE(vma, __vm_flags) &= ~flags;
854 }
855 
856 /*
857  * Use only if VMA is not part of the VMA tree or has no other users and
858  * therefore needs no locking.
859  */
__vm_flags_mod(struct vm_area_struct * vma,vm_flags_t set,vm_flags_t clear)860 static inline void __vm_flags_mod(struct vm_area_struct *vma,
861 				  vm_flags_t set, vm_flags_t clear)
862 {
863 	vm_flags_init(vma, (vma->vm_flags | set) & ~clear);
864 }
865 
866 /*
867  * Use only when the order of set/clear operations is unimportant, otherwise
868  * use vm_flags_{set|clear} explicitly.
869  */
vm_flags_mod(struct vm_area_struct * vma,vm_flags_t set,vm_flags_t clear)870 static inline void vm_flags_mod(struct vm_area_struct *vma,
871 				vm_flags_t set, vm_flags_t clear)
872 {
873 	vma_start_write(vma);
874 	__vm_flags_mod(vma, set, clear);
875 }
876 
vma_set_anonymous(struct vm_area_struct * vma)877 static inline void vma_set_anonymous(struct vm_area_struct *vma)
878 {
879 	vma->vm_ops = NULL;
880 }
881 
vma_is_anonymous(struct vm_area_struct * vma)882 static inline bool vma_is_anonymous(struct vm_area_struct *vma)
883 {
884 	return !vma->vm_ops;
885 }
886 
vma_is_temporary_stack(struct vm_area_struct * vma)887 static inline bool vma_is_temporary_stack(struct vm_area_struct *vma)
888 {
889 	int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);
890 
891 	if (!maybe_stack)
892 		return false;
893 
894 	if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) ==
895 						VM_STACK_INCOMPLETE_SETUP)
896 		return true;
897 
898 	return false;
899 }
900 
vma_is_foreign(struct vm_area_struct * vma)901 static inline bool vma_is_foreign(struct vm_area_struct *vma)
902 {
903 	if (!current->mm)
904 		return true;
905 
906 	if (current->mm != vma->vm_mm)
907 		return true;
908 
909 	return false;
910 }
911 
vma_is_accessible(struct vm_area_struct * vma)912 static inline bool vma_is_accessible(struct vm_area_struct *vma)
913 {
914 	return vma->vm_flags & VM_ACCESS_FLAGS;
915 }
916 
917 static inline
vma_find(struct vma_iterator * vmi,unsigned long max)918 struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max)
919 {
920 	return mas_find(&vmi->mas, max);
921 }
922 
vma_next(struct vma_iterator * vmi)923 static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi)
924 {
925 	/*
926 	 * Uses vma_find() to get the first VMA when the iterator starts.
927 	 * Calling mas_next() could skip the first entry.
928 	 */
929 	return vma_find(vmi, ULONG_MAX);
930 }
931 
vma_prev(struct vma_iterator * vmi)932 static inline struct vm_area_struct *vma_prev(struct vma_iterator *vmi)
933 {
934 	return mas_prev(&vmi->mas, 0);
935 }
936 
vma_iter_addr(struct vma_iterator * vmi)937 static inline unsigned long vma_iter_addr(struct vma_iterator *vmi)
938 {
939 	return vmi->mas.index;
940 }
941 
942 #define for_each_vma(__vmi, __vma)					\
943 	while (((__vma) = vma_next(&(__vmi))) != NULL)
944 
945 /* The MM code likes to work with exclusive end addresses */
946 #define for_each_vma_range(__vmi, __vma, __end)				\
947 	while (((__vma) = vma_find(&(__vmi), (__end) - 1)) != NULL)
948 
949 #ifdef CONFIG_SHMEM
950 /*
951  * The vma_is_shmem is not inline because it is used only by slow
952  * paths in userfault.
953  */
954 bool vma_is_shmem(struct vm_area_struct *vma);
955 #else
vma_is_shmem(struct vm_area_struct * vma)956 static inline bool vma_is_shmem(struct vm_area_struct *vma) { return false; }
957 #endif
958 
959 int vma_is_stack_for_current(struct vm_area_struct *vma);
960 
961 /* flush_tlb_range() takes a vma, not a mm, and can care about flags */
962 #define TLB_FLUSH_VMA(mm,flags) { .vm_mm = (mm), .vm_flags = (flags) }
963 
964 struct mmu_gather;
965 struct inode;
966 
compound_order(struct page * page)967 static inline unsigned int compound_order(struct page *page)
968 {
969 	if (!PageHead(page))
970 		return 0;
971 	return page[1].compound_order;
972 }
973 
974 /**
975  * folio_order - The allocation order of a folio.
976  * @folio: The folio.
977  *
978  * A folio is composed of 2^order pages.  See get_order() for the definition
979  * of order.
980  *
981  * Return: The order of the folio.
982  */
folio_order(struct folio * folio)983 static inline unsigned int folio_order(struct folio *folio)
984 {
985 	if (!folio_test_large(folio))
986 		return 0;
987 	return folio->_folio_order;
988 }
989 
990 #include <linux/huge_mm.h>
991 
992 /*
993  * Methods to modify the page usage count.
994  *
995  * What counts for a page usage:
996  * - cache mapping   (page->mapping)
997  * - private data    (page->private)
998  * - page mapped in a task's page tables, each mapping
999  *   is counted separately
1000  *
1001  * Also, many kernel routines increase the page count before a critical
1002  * routine so they can be sure the page doesn't go away from under them.
1003  */
1004 
1005 /*
1006  * Drop a ref, return true if the refcount fell to zero (the page has no users)
1007  */
put_page_testzero(struct page * page)1008 static inline int put_page_testzero(struct page *page)
1009 {
1010 	int ret;
1011 
1012 	VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
1013 	ret = page_ref_dec_and_test(page);
1014 	page_pinner_put_page(page);
1015 
1016 	return ret;
1017 }
1018 
folio_put_testzero(struct folio * folio)1019 static inline int folio_put_testzero(struct folio *folio)
1020 {
1021 	return put_page_testzero(&folio->page);
1022 }
1023 
1024 /*
1025  * Try to grab a ref unless the page has a refcount of zero, return false if
1026  * that is the case.
1027  * This can be called when MMU is off so it must not access
1028  * any of the virtual mappings.
1029  */
get_page_unless_zero(struct page * page)1030 static inline bool get_page_unless_zero(struct page *page)
1031 {
1032 	return page_ref_add_unless(page, 1, 0);
1033 }
1034 
1035 extern int page_is_ram(unsigned long pfn);
1036 
1037 enum {
1038 	REGION_INTERSECTS,
1039 	REGION_DISJOINT,
1040 	REGION_MIXED,
1041 };
1042 
1043 int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
1044 		      unsigned long desc);
1045 
1046 /* Support for virtually mapped pages */
1047 struct page *vmalloc_to_page(const void *addr);
1048 unsigned long vmalloc_to_pfn(const void *addr);
1049 
1050 /*
1051  * Determine if an address is within the vmalloc range
1052  *
1053  * On nommu, vmalloc/vfree wrap through kmalloc/kfree directly, so there
1054  * is no special casing required.
1055  */
1056 
1057 #ifndef is_ioremap_addr
1058 #define is_ioremap_addr(x) is_vmalloc_addr(x)
1059 #endif
1060 
1061 #ifdef CONFIG_MMU
1062 extern bool is_vmalloc_addr(const void *x);
1063 extern int is_vmalloc_or_module_addr(const void *x);
1064 #else
is_vmalloc_addr(const void * x)1065 static inline bool is_vmalloc_addr(const void *x)
1066 {
1067 	return false;
1068 }
is_vmalloc_or_module_addr(const void * x)1069 static inline int is_vmalloc_or_module_addr(const void *x)
1070 {
1071 	return 0;
1072 }
1073 #endif
1074 
1075 /*
1076  * How many times the entire folio is mapped as a single unit (eg by a
1077  * PMD or PUD entry).  This is probably not what you want, except for
1078  * debugging purposes; look at folio_mapcount() or page_mapcount()
1079  * instead.
1080  */
folio_entire_mapcount(struct folio * folio)1081 static inline int folio_entire_mapcount(struct folio *folio)
1082 {
1083 	VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
1084 	return atomic_read(folio_mapcount_ptr(folio)) + 1;
1085 }
1086 
1087 /*
1088  * Mapcount of compound page as a whole, does not include mapped sub-pages.
1089  *
1090  * Must be called only for compound pages.
1091  */
compound_mapcount(struct page * page)1092 static inline int compound_mapcount(struct page *page)
1093 {
1094 	return folio_entire_mapcount(page_folio(page));
1095 }
1096 
1097 /*
1098  * The atomic page->_mapcount, starts from -1: so that transitions
1099  * both from it and to it can be tracked, using atomic_inc_and_test
1100  * and atomic_add_negative(-1).
1101  */
page_mapcount_reset(struct page * page)1102 static inline void page_mapcount_reset(struct page *page)
1103 {
1104 	atomic_set(&(page)->_mapcount, -1);
1105 }
1106 
1107 int __page_mapcount(struct page *page);
1108 
1109 /*
1110  * Mapcount of 0-order page; when compound sub-page, includes
1111  * compound_mapcount().
1112  *
1113  * Result is undefined for pages which cannot be mapped into userspace.
1114  * For example SLAB or special types of pages. See function page_has_type().
1115  * They use this place in struct page differently.
1116  */
page_mapcount(struct page * page)1117 static inline int page_mapcount(struct page *page)
1118 {
1119 	if (unlikely(PageCompound(page)))
1120 		return __page_mapcount(page);
1121 	return atomic_read(&page->_mapcount) + 1;
1122 }
1123 
1124 int folio_mapcount(struct folio *folio);
1125 
1126 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
total_mapcount(struct page * page)1127 static inline int total_mapcount(struct page *page)
1128 {
1129 	return folio_mapcount(page_folio(page));
1130 }
1131 
1132 #else
total_mapcount(struct page * page)1133 static inline int total_mapcount(struct page *page)
1134 {
1135 	return page_mapcount(page);
1136 }
1137 #endif
1138 
virt_to_head_page(const void * x)1139 static inline struct page *virt_to_head_page(const void *x)
1140 {
1141 	struct page *page = virt_to_page(x);
1142 
1143 	return compound_head(page);
1144 }
1145 
virt_to_folio(const void * x)1146 static inline struct folio *virt_to_folio(const void *x)
1147 {
1148 	struct page *page = virt_to_page(x);
1149 
1150 	return page_folio(page);
1151 }
1152 
1153 void __folio_put(struct folio *folio);
1154 
1155 void put_pages_list(struct list_head *pages);
1156 
1157 void split_page(struct page *page, unsigned int order);
1158 void folio_copy(struct folio *dst, struct folio *src);
1159 
1160 unsigned long nr_free_buffer_pages(void);
1161 
1162 /*
1163  * Compound pages have a destructor function.  Provide a
1164  * prototype for that function and accessor functions.
1165  * These are _only_ valid on the head of a compound page.
1166  */
1167 typedef void compound_page_dtor(struct page *);
1168 
1169 /* Keep the enum in sync with compound_page_dtors array in mm/page_alloc.c */
1170 enum compound_dtor_id {
1171 	NULL_COMPOUND_DTOR,
1172 	COMPOUND_PAGE_DTOR,
1173 #ifdef CONFIG_HUGETLB_PAGE
1174 	HUGETLB_PAGE_DTOR,
1175 #endif
1176 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1177 	TRANSHUGE_PAGE_DTOR,
1178 #endif
1179 	NR_COMPOUND_DTORS,
1180 };
1181 extern compound_page_dtor * const compound_page_dtors[NR_COMPOUND_DTORS];
1182 
set_compound_page_dtor(struct page * page,enum compound_dtor_id compound_dtor)1183 static inline void set_compound_page_dtor(struct page *page,
1184 		enum compound_dtor_id compound_dtor)
1185 {
1186 	VM_BUG_ON_PAGE(compound_dtor >= NR_COMPOUND_DTORS, page);
1187 	page[1].compound_dtor = compound_dtor;
1188 }
1189 
1190 void destroy_large_folio(struct folio *folio);
1191 
head_compound_pincount(struct page * head)1192 static inline int head_compound_pincount(struct page *head)
1193 {
1194 	return atomic_read(compound_pincount_ptr(head));
1195 }
1196 
set_compound_order(struct page * page,unsigned int order)1197 static inline void set_compound_order(struct page *page, unsigned int order)
1198 {
1199 	page[1].compound_order = order;
1200 #ifdef CONFIG_64BIT
1201 	page[1].compound_nr = 1U << order;
1202 #endif
1203 }
1204 
1205 /* Returns the number of pages in this potentially compound page. */
compound_nr(struct page * page)1206 static inline unsigned long compound_nr(struct page *page)
1207 {
1208 	if (!PageHead(page))
1209 		return 1;
1210 #ifdef CONFIG_64BIT
1211 	return page[1].compound_nr;
1212 #else
1213 	return 1UL << compound_order(page);
1214 #endif
1215 }
1216 
1217 /* Returns the number of bytes in this potentially compound page. */
page_size(struct page * page)1218 static inline unsigned long page_size(struct page *page)
1219 {
1220 	return PAGE_SIZE << compound_order(page);
1221 }
1222 
1223 /* Returns the number of bits needed for the number of bytes in a page */
page_shift(struct page * page)1224 static inline unsigned int page_shift(struct page *page)
1225 {
1226 	return PAGE_SHIFT + compound_order(page);
1227 }
1228 
1229 /**
1230  * thp_order - Order of a transparent huge page.
1231  * @page: Head page of a transparent huge page.
1232  */
thp_order(struct page * page)1233 static inline unsigned int thp_order(struct page *page)
1234 {
1235 	VM_BUG_ON_PGFLAGS(PageTail(page), page);
1236 	return compound_order(page);
1237 }
1238 
1239 /**
1240  * thp_nr_pages - The number of regular pages in this huge page.
1241  * @page: The head page of a huge page.
1242  */
thp_nr_pages(struct page * page)1243 static inline int thp_nr_pages(struct page *page)
1244 {
1245 	VM_BUG_ON_PGFLAGS(PageTail(page), page);
1246 	return compound_nr(page);
1247 }
1248 
1249 /**
1250  * thp_size - Size of a transparent huge page.
1251  * @page: Head page of a transparent huge page.
1252  *
1253  * Return: Number of bytes in this page.
1254  */
thp_size(struct page * page)1255 static inline unsigned long thp_size(struct page *page)
1256 {
1257 	return PAGE_SIZE << thp_order(page);
1258 }
1259 
1260 void free_compound_page(struct page *page);
1261 
1262 #ifdef CONFIG_MMU
1263 /*
1264  * Do pte_mkwrite, but only if the vma says VM_WRITE.  We do this when
1265  * servicing faults for write access.  In the normal case, do always want
1266  * pte_mkwrite.  But get_user_pages can cause write faults for mappings
1267  * that do not have writing enabled, when used by access_process_vm.
1268  */
maybe_mkwrite(pte_t pte,struct vm_area_struct * vma)1269 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1270 {
1271 	if (likely(vma->vm_flags & VM_WRITE))
1272 		pte = pte_mkwrite(pte);
1273 	return pte;
1274 }
1275 
1276 vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page);
1277 void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr);
1278 
1279 vm_fault_t finish_fault(struct vm_fault *vmf);
1280 vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf);
1281 #endif
1282 
1283 /*
1284  * Multiple processes may "see" the same page. E.g. for untouched
1285  * mappings of /dev/null, all processes see the same page full of
1286  * zeroes, and text pages of executables and shared libraries have
1287  * only one copy in memory, at most, normally.
1288  *
1289  * For the non-reserved pages, page_count(page) denotes a reference count.
1290  *   page_count() == 0 means the page is free. page->lru is then used for
1291  *   freelist management in the buddy allocator.
1292  *   page_count() > 0  means the page has been allocated.
1293  *
1294  * Pages are allocated by the slab allocator in order to provide memory
1295  * to kmalloc and kmem_cache_alloc. In this case, the management of the
1296  * page, and the fields in 'struct page' are the responsibility of mm/slab.c
1297  * unless a particular usage is carefully commented. (the responsibility of
1298  * freeing the kmalloc memory is the caller's, of course).
1299  *
1300  * A page may be used by anyone else who does a __get_free_page().
1301  * In this case, page_count still tracks the references, and should only
1302  * be used through the normal accessor functions. The top bits of page->flags
1303  * and page->virtual store page management information, but all other fields
1304  * are unused and could be used privately, carefully. The management of this
1305  * page is the responsibility of the one who allocated it, and those who have
1306  * subsequently been given references to it.
1307  *
1308  * The other pages (we may call them "pagecache pages") are completely
1309  * managed by the Linux memory manager: I/O, buffers, swapping etc.
1310  * The following discussion applies only to them.
1311  *
1312  * A pagecache page contains an opaque `private' member, which belongs to the
1313  * page's address_space. Usually, this is the address of a circular list of
1314  * the page's disk buffers. PG_private must be set to tell the VM to call
1315  * into the filesystem to release these pages.
1316  *
1317  * A page may belong to an inode's memory mapping. In this case, page->mapping
1318  * is the pointer to the inode, and page->index is the file offset of the page,
1319  * in units of PAGE_SIZE.
1320  *
1321  * If pagecache pages are not associated with an inode, they are said to be
1322  * anonymous pages. These may become associated with the swapcache, and in that
1323  * case PG_swapcache is set, and page->private is an offset into the swapcache.
1324  *
1325  * In either case (swapcache or inode backed), the pagecache itself holds one
1326  * reference to the page. Setting PG_private should also increment the
1327  * refcount. The each user mapping also has a reference to the page.
1328  *
1329  * The pagecache pages are stored in a per-mapping radix tree, which is
1330  * rooted at mapping->i_pages, and indexed by offset.
1331  * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space
1332  * lists, we instead now tag pages as dirty/writeback in the radix tree.
1333  *
1334  * All pagecache pages may be subject to I/O:
1335  * - inode pages may need to be read from disk,
1336  * - inode pages which have been modified and are MAP_SHARED may need
1337  *   to be written back to the inode on disk,
1338  * - anonymous pages (including MAP_PRIVATE file mappings) which have been
1339  *   modified may need to be swapped out to swap space and (later) to be read
1340  *   back into memory.
1341  */
1342 
1343 #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_FS_DAX)
1344 DECLARE_STATIC_KEY_FALSE(devmap_managed_key);
1345 
1346 bool __put_devmap_managed_page_refs(struct page *page, int refs);
put_devmap_managed_page_refs(struct page * page,int refs)1347 static inline bool put_devmap_managed_page_refs(struct page *page, int refs)
1348 {
1349 	if (!static_branch_unlikely(&devmap_managed_key))
1350 		return false;
1351 	if (!is_zone_device_page(page))
1352 		return false;
1353 	return __put_devmap_managed_page_refs(page, refs);
1354 }
1355 #else /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */
put_devmap_managed_page_refs(struct page * page,int refs)1356 static inline bool put_devmap_managed_page_refs(struct page *page, int refs)
1357 {
1358 	return false;
1359 }
1360 #endif /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */
1361 
put_devmap_managed_page(struct page * page)1362 static inline bool put_devmap_managed_page(struct page *page)
1363 {
1364 	return put_devmap_managed_page_refs(page, 1);
1365 }
1366 
1367 /* 127: arbitrary random number, small enough to assemble well */
1368 #define folio_ref_zero_or_close_to_overflow(folio) \
1369 	((unsigned int) folio_ref_count(folio) + 127u <= 127u)
1370 
1371 /**
1372  * folio_get - Increment the reference count on a folio.
1373  * @folio: The folio.
1374  *
1375  * Context: May be called in any context, as long as you know that
1376  * you have a refcount on the folio.  If you do not already have one,
1377  * folio_try_get() may be the right interface for you to use.
1378  */
folio_get(struct folio * folio)1379 static inline void folio_get(struct folio *folio)
1380 {
1381 	VM_BUG_ON_FOLIO(folio_ref_zero_or_close_to_overflow(folio), folio);
1382 	folio_ref_inc(folio);
1383 }
1384 
get_page(struct page * page)1385 static inline void get_page(struct page *page)
1386 {
1387 	folio_get(page_folio(page));
1388 }
1389 
1390 bool __must_check try_grab_page(struct page *page, unsigned int flags);
1391 
try_get_page(struct page * page)1392 static inline __must_check bool try_get_page(struct page *page)
1393 {
1394 	page = compound_head(page);
1395 	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
1396 		return false;
1397 	page_ref_inc(page);
1398 	return true;
1399 }
1400 
1401 /**
1402  * folio_put - Decrement the reference count on a folio.
1403  * @folio: The folio.
1404  *
1405  * If the folio's reference count reaches zero, the memory will be
1406  * released back to the page allocator and may be used by another
1407  * allocation immediately.  Do not access the memory or the struct folio
1408  * after calling folio_put() unless you can be sure that it wasn't the
1409  * last reference.
1410  *
1411  * Context: May be called in process or interrupt context, but not in NMI
1412  * context.  May be called while holding a spinlock.
1413  */
folio_put(struct folio * folio)1414 static inline void folio_put(struct folio *folio)
1415 {
1416 	if (folio_put_testzero(folio))
1417 		__folio_put(folio);
1418 }
1419 
1420 /**
1421  * folio_put_refs - Reduce the reference count on a folio.
1422  * @folio: The folio.
1423  * @refs: The amount to subtract from the folio's reference count.
1424  *
1425  * If the folio's reference count reaches zero, the memory will be
1426  * released back to the page allocator and may be used by another
1427  * allocation immediately.  Do not access the memory or the struct folio
1428  * after calling folio_put_refs() unless you can be sure that these weren't
1429  * the last references.
1430  *
1431  * Context: May be called in process or interrupt context, but not in NMI
1432  * context.  May be called while holding a spinlock.
1433  */
folio_put_refs(struct folio * folio,int refs)1434 static inline void folio_put_refs(struct folio *folio, int refs)
1435 {
1436 	if (folio_ref_sub_and_test(folio, refs))
1437 		__folio_put(folio);
1438 }
1439 
1440 void release_pages(struct page **pages, int nr);
1441 
1442 /**
1443  * folios_put - Decrement the reference count on an array of folios.
1444  * @folios: The folios.
1445  * @nr: How many folios there are.
1446  *
1447  * Like folio_put(), but for an array of folios.  This is more efficient
1448  * than writing the loop yourself as it will optimise the locks which
1449  * need to be taken if the folios are freed.
1450  *
1451  * Context: May be called in process or interrupt context, but not in NMI
1452  * context.  May be called while holding a spinlock.
1453  */
folios_put(struct folio ** folios,unsigned int nr)1454 static inline void folios_put(struct folio **folios, unsigned int nr)
1455 {
1456 	release_pages((struct page **)folios, nr);
1457 }
1458 
put_page(struct page * page)1459 static inline void put_page(struct page *page)
1460 {
1461 	struct folio *folio = page_folio(page);
1462 
1463 	/*
1464 	 * For some devmap managed pages we need to catch refcount transition
1465 	 * from 2 to 1:
1466 	 */
1467 	if (put_devmap_managed_page(&folio->page))
1468 		return;
1469 	folio_put(folio);
1470 }
1471 
1472 /*
1473  * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload
1474  * the page's refcount so that two separate items are tracked: the original page
1475  * reference count, and also a new count of how many pin_user_pages() calls were
1476  * made against the page. ("gup-pinned" is another term for the latter).
1477  *
1478  * With this scheme, pin_user_pages() becomes special: such pages are marked as
1479  * distinct from normal pages. As such, the unpin_user_page() call (and its
1480  * variants) must be used in order to release gup-pinned pages.
1481  *
1482  * Choice of value:
1483  *
1484  * By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference
1485  * counts with respect to pin_user_pages() and unpin_user_page() becomes
1486  * simpler, due to the fact that adding an even power of two to the page
1487  * refcount has the effect of using only the upper N bits, for the code that
1488  * counts up using the bias value. This means that the lower bits are left for
1489  * the exclusive use of the original code that increments and decrements by one
1490  * (or at least, by much smaller values than the bias value).
1491  *
1492  * Of course, once the lower bits overflow into the upper bits (and this is
1493  * OK, because subtraction recovers the original values), then visual inspection
1494  * no longer suffices to directly view the separate counts. However, for normal
1495  * applications that don't have huge page reference counts, this won't be an
1496  * issue.
1497  *
1498  * Locking: the lockless algorithm described in folio_try_get_rcu()
1499  * provides safe operation for get_user_pages(), page_mkclean() and
1500  * other calls that race to set up page table entries.
1501  */
1502 #define GUP_PIN_COUNTING_BIAS (1U << 10)
1503 
1504 void unpin_user_page(struct page *page);
1505 void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
1506 				 bool make_dirty);
1507 void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
1508 				      bool make_dirty);
1509 void unpin_user_pages(struct page **pages, unsigned long npages);
1510 
is_cow_mapping(vm_flags_t flags)1511 static inline bool is_cow_mapping(vm_flags_t flags)
1512 {
1513 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
1514 }
1515 
1516 #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
1517 #define SECTION_IN_PAGE_FLAGS
1518 #endif
1519 
1520 /*
1521  * The identification function is mainly used by the buddy allocator for
1522  * determining if two pages could be buddies. We are not really identifying
1523  * the zone since we could be using the section number id if we do not have
1524  * node id available in page flags.
1525  * We only guarantee that it will return the same value for two combinable
1526  * pages in a zone.
1527  */
page_zone_id(struct page * page)1528 static inline int page_zone_id(struct page *page)
1529 {
1530 	return (page->flags >> ZONEID_PGSHIFT) & ZONEID_MASK;
1531 }
1532 
1533 #ifdef NODE_NOT_IN_PAGE_FLAGS
1534 extern int page_to_nid(const struct page *page);
1535 #else
page_to_nid(const struct page * page)1536 static inline int page_to_nid(const struct page *page)
1537 {
1538 	struct page *p = (struct page *)page;
1539 
1540 	return (PF_POISONED_CHECK(p)->flags >> NODES_PGSHIFT) & NODES_MASK;
1541 }
1542 #endif
1543 
folio_nid(const struct folio * folio)1544 static inline int folio_nid(const struct folio *folio)
1545 {
1546 	return page_to_nid(&folio->page);
1547 }
1548 
1549 #ifdef CONFIG_NUMA_BALANCING
1550 /* page access time bits needs to hold at least 4 seconds */
1551 #define PAGE_ACCESS_TIME_MIN_BITS	12
1552 #if LAST_CPUPID_SHIFT < PAGE_ACCESS_TIME_MIN_BITS
1553 #define PAGE_ACCESS_TIME_BUCKETS				\
1554 	(PAGE_ACCESS_TIME_MIN_BITS - LAST_CPUPID_SHIFT)
1555 #else
1556 #define PAGE_ACCESS_TIME_BUCKETS	0
1557 #endif
1558 
1559 #define PAGE_ACCESS_TIME_MASK				\
1560 	(LAST_CPUPID_MASK << PAGE_ACCESS_TIME_BUCKETS)
1561 
cpu_pid_to_cpupid(int cpu,int pid)1562 static inline int cpu_pid_to_cpupid(int cpu, int pid)
1563 {
1564 	return ((cpu & LAST__CPU_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK);
1565 }
1566 
cpupid_to_pid(int cpupid)1567 static inline int cpupid_to_pid(int cpupid)
1568 {
1569 	return cpupid & LAST__PID_MASK;
1570 }
1571 
cpupid_to_cpu(int cpupid)1572 static inline int cpupid_to_cpu(int cpupid)
1573 {
1574 	return (cpupid >> LAST__PID_SHIFT) & LAST__CPU_MASK;
1575 }
1576 
cpupid_to_nid(int cpupid)1577 static inline int cpupid_to_nid(int cpupid)
1578 {
1579 	return cpu_to_node(cpupid_to_cpu(cpupid));
1580 }
1581 
cpupid_pid_unset(int cpupid)1582 static inline bool cpupid_pid_unset(int cpupid)
1583 {
1584 	return cpupid_to_pid(cpupid) == (-1 & LAST__PID_MASK);
1585 }
1586 
cpupid_cpu_unset(int cpupid)1587 static inline bool cpupid_cpu_unset(int cpupid)
1588 {
1589 	return cpupid_to_cpu(cpupid) == (-1 & LAST__CPU_MASK);
1590 }
1591 
__cpupid_match_pid(pid_t task_pid,int cpupid)1592 static inline bool __cpupid_match_pid(pid_t task_pid, int cpupid)
1593 {
1594 	return (task_pid & LAST__PID_MASK) == cpupid_to_pid(cpupid);
1595 }
1596 
1597 #define cpupid_match_pid(task, cpupid) __cpupid_match_pid(task->pid, cpupid)
1598 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
page_cpupid_xchg_last(struct page * page,int cpupid)1599 static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
1600 {
1601 	return xchg(&page->_last_cpupid, cpupid & LAST_CPUPID_MASK);
1602 }
1603 
page_cpupid_last(struct page * page)1604 static inline int page_cpupid_last(struct page *page)
1605 {
1606 	return page->_last_cpupid;
1607 }
page_cpupid_reset_last(struct page * page)1608 static inline void page_cpupid_reset_last(struct page *page)
1609 {
1610 	page->_last_cpupid = -1 & LAST_CPUPID_MASK;
1611 }
1612 #else
page_cpupid_last(struct page * page)1613 static inline int page_cpupid_last(struct page *page)
1614 {
1615 	return (page->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
1616 }
1617 
1618 extern int page_cpupid_xchg_last(struct page *page, int cpupid);
1619 
page_cpupid_reset_last(struct page * page)1620 static inline void page_cpupid_reset_last(struct page *page)
1621 {
1622 	page->flags |= LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT;
1623 }
1624 #endif /* LAST_CPUPID_NOT_IN_PAGE_FLAGS */
1625 
xchg_page_access_time(struct page * page,int time)1626 static inline int xchg_page_access_time(struct page *page, int time)
1627 {
1628 	int last_time;
1629 
1630 	last_time = page_cpupid_xchg_last(page, time >> PAGE_ACCESS_TIME_BUCKETS);
1631 	return last_time << PAGE_ACCESS_TIME_BUCKETS;
1632 }
1633 #else /* !CONFIG_NUMA_BALANCING */
page_cpupid_xchg_last(struct page * page,int cpupid)1634 static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
1635 {
1636 	return page_to_nid(page); /* XXX */
1637 }
1638 
xchg_page_access_time(struct page * page,int time)1639 static inline int xchg_page_access_time(struct page *page, int time)
1640 {
1641 	return 0;
1642 }
1643 
page_cpupid_last(struct page * page)1644 static inline int page_cpupid_last(struct page *page)
1645 {
1646 	return page_to_nid(page); /* XXX */
1647 }
1648 
cpupid_to_nid(int cpupid)1649 static inline int cpupid_to_nid(int cpupid)
1650 {
1651 	return -1;
1652 }
1653 
cpupid_to_pid(int cpupid)1654 static inline int cpupid_to_pid(int cpupid)
1655 {
1656 	return -1;
1657 }
1658 
cpupid_to_cpu(int cpupid)1659 static inline int cpupid_to_cpu(int cpupid)
1660 {
1661 	return -1;
1662 }
1663 
cpu_pid_to_cpupid(int nid,int pid)1664 static inline int cpu_pid_to_cpupid(int nid, int pid)
1665 {
1666 	return -1;
1667 }
1668 
cpupid_pid_unset(int cpupid)1669 static inline bool cpupid_pid_unset(int cpupid)
1670 {
1671 	return true;
1672 }
1673 
page_cpupid_reset_last(struct page * page)1674 static inline void page_cpupid_reset_last(struct page *page)
1675 {
1676 }
1677 
cpupid_match_pid(struct task_struct * task,int cpupid)1678 static inline bool cpupid_match_pid(struct task_struct *task, int cpupid)
1679 {
1680 	return false;
1681 }
1682 #endif /* CONFIG_NUMA_BALANCING */
1683 
1684 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
1685 
1686 /*
1687  * KASAN per-page tags are stored xor'ed with 0xff. This allows to avoid
1688  * setting tags for all pages to native kernel tag value 0xff, as the default
1689  * value 0x00 maps to 0xff.
1690  */
1691 
page_kasan_tag(const struct page * page)1692 static inline u8 page_kasan_tag(const struct page *page)
1693 {
1694 	u8 tag = 0xff;
1695 
1696 	if (kasan_enabled()) {
1697 		tag = (page->flags >> KASAN_TAG_PGSHIFT) & KASAN_TAG_MASK;
1698 		tag ^= 0xff;
1699 	}
1700 
1701 	return tag;
1702 }
1703 
page_kasan_tag_set(struct page * page,u8 tag)1704 static inline void page_kasan_tag_set(struct page *page, u8 tag)
1705 {
1706 	unsigned long old_flags, flags;
1707 
1708 	if (!kasan_enabled())
1709 		return;
1710 
1711 	tag ^= 0xff;
1712 	old_flags = READ_ONCE(page->flags);
1713 	do {
1714 		flags = old_flags;
1715 		flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
1716 		flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
1717 	} while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags)));
1718 }
1719 
page_kasan_tag_reset(struct page * page)1720 static inline void page_kasan_tag_reset(struct page *page)
1721 {
1722 	if (kasan_enabled())
1723 		page_kasan_tag_set(page, 0xff);
1724 }
1725 
1726 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
1727 
page_kasan_tag(const struct page * page)1728 static inline u8 page_kasan_tag(const struct page *page)
1729 {
1730 	return 0xff;
1731 }
1732 
page_kasan_tag_set(struct page * page,u8 tag)1733 static inline void page_kasan_tag_set(struct page *page, u8 tag) { }
page_kasan_tag_reset(struct page * page)1734 static inline void page_kasan_tag_reset(struct page *page) { }
1735 
1736 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
1737 
page_zone(const struct page * page)1738 static inline struct zone *page_zone(const struct page *page)
1739 {
1740 	return &NODE_DATA(page_to_nid(page))->node_zones[page_zonenum(page)];
1741 }
1742 
page_pgdat(const struct page * page)1743 static inline pg_data_t *page_pgdat(const struct page *page)
1744 {
1745 	return NODE_DATA(page_to_nid(page));
1746 }
1747 
folio_zone(const struct folio * folio)1748 static inline struct zone *folio_zone(const struct folio *folio)
1749 {
1750 	return page_zone(&folio->page);
1751 }
1752 
folio_pgdat(const struct folio * folio)1753 static inline pg_data_t *folio_pgdat(const struct folio *folio)
1754 {
1755 	return page_pgdat(&folio->page);
1756 }
1757 
1758 #ifdef SECTION_IN_PAGE_FLAGS
set_page_section(struct page * page,unsigned long section)1759 static inline void set_page_section(struct page *page, unsigned long section)
1760 {
1761 	page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT);
1762 	page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
1763 }
1764 
page_to_section(const struct page * page)1765 static inline unsigned long page_to_section(const struct page *page)
1766 {
1767 	return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
1768 }
1769 #endif
1770 
1771 /**
1772  * folio_pfn - Return the Page Frame Number of a folio.
1773  * @folio: The folio.
1774  *
1775  * A folio may contain multiple pages.  The pages have consecutive
1776  * Page Frame Numbers.
1777  *
1778  * Return: The Page Frame Number of the first page in the folio.
1779  */
folio_pfn(struct folio * folio)1780 static inline unsigned long folio_pfn(struct folio *folio)
1781 {
1782 	return page_to_pfn(&folio->page);
1783 }
1784 
pfn_folio(unsigned long pfn)1785 static inline struct folio *pfn_folio(unsigned long pfn)
1786 {
1787 	return page_folio(pfn_to_page(pfn));
1788 }
1789 
folio_pincount_ptr(struct folio * folio)1790 static inline atomic_t *folio_pincount_ptr(struct folio *folio)
1791 {
1792 	return &folio_page(folio, 1)->compound_pincount;
1793 }
1794 
1795 /**
1796  * folio_maybe_dma_pinned - Report if a folio may be pinned for DMA.
1797  * @folio: The folio.
1798  *
1799  * This function checks if a folio has been pinned via a call to
1800  * a function in the pin_user_pages() family.
1801  *
1802  * For small folios, the return value is partially fuzzy: false is not fuzzy,
1803  * because it means "definitely not pinned for DMA", but true means "probably
1804  * pinned for DMA, but possibly a false positive due to having at least
1805  * GUP_PIN_COUNTING_BIAS worth of normal folio references".
1806  *
1807  * False positives are OK, because: a) it's unlikely for a folio to
1808  * get that many refcounts, and b) all the callers of this routine are
1809  * expected to be able to deal gracefully with a false positive.
1810  *
1811  * For large folios, the result will be exactly correct. That's because
1812  * we have more tracking data available: the compound_pincount is used
1813  * instead of the GUP_PIN_COUNTING_BIAS scheme.
1814  *
1815  * For more information, please see Documentation/core-api/pin_user_pages.rst.
1816  *
1817  * Return: True, if it is likely that the page has been "dma-pinned".
1818  * False, if the page is definitely not dma-pinned.
1819  */
folio_maybe_dma_pinned(struct folio * folio)1820 static inline bool folio_maybe_dma_pinned(struct folio *folio)
1821 {
1822 	if (folio_test_large(folio))
1823 		return atomic_read(folio_pincount_ptr(folio)) > 0;
1824 
1825 	/*
1826 	 * folio_ref_count() is signed. If that refcount overflows, then
1827 	 * folio_ref_count() returns a negative value, and callers will avoid
1828 	 * further incrementing the refcount.
1829 	 *
1830 	 * Here, for that overflow case, use the sign bit to count a little
1831 	 * bit higher via unsigned math, and thus still get an accurate result.
1832 	 */
1833 	return ((unsigned int)folio_ref_count(folio)) >=
1834 		GUP_PIN_COUNTING_BIAS;
1835 }
1836 
page_maybe_dma_pinned(struct page * page)1837 static inline bool page_maybe_dma_pinned(struct page *page)
1838 {
1839 	return folio_maybe_dma_pinned(page_folio(page));
1840 }
1841 
1842 /*
1843  * This should most likely only be called during fork() to see whether we
1844  * should break the cow immediately for an anon page on the src mm.
1845  *
1846  * The caller has to hold the PT lock and the vma->vm_mm->->write_protect_seq.
1847  */
page_needs_cow_for_dma(struct vm_area_struct * vma,struct page * page)1848 static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma,
1849 					  struct page *page)
1850 {
1851 	VM_BUG_ON(!(raw_read_seqcount(&vma->vm_mm->write_protect_seq) & 1));
1852 
1853 	if (!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))
1854 		return false;
1855 
1856 	return page_maybe_dma_pinned(page);
1857 }
1858 
1859 /* MIGRATE_CMA and ZONE_MOVABLE do not allow pin pages */
1860 #ifdef CONFIG_MIGRATION
is_longterm_pinnable_page(struct page * page)1861 static inline bool is_longterm_pinnable_page(struct page *page)
1862 {
1863 #ifdef CONFIG_CMA
1864 	int mt = get_pageblock_migratetype(page);
1865 
1866 	if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
1867 		return false;
1868 #endif
1869 	/* The zero page may always be pinned */
1870 	if (is_zero_pfn(page_to_pfn(page)))
1871 		return true;
1872 
1873 	/* Coherent device memory must always allow eviction. */
1874 	if (is_device_coherent_page(page))
1875 		return false;
1876 
1877 	/* Otherwise, non-movable zone pages can be pinned. */
1878 	return !is_zone_movable_page(page);
1879 }
1880 #else
is_longterm_pinnable_page(struct page * page)1881 static inline bool is_longterm_pinnable_page(struct page *page)
1882 {
1883 	return true;
1884 }
1885 #endif
1886 
folio_is_longterm_pinnable(struct folio * folio)1887 static inline bool folio_is_longterm_pinnable(struct folio *folio)
1888 {
1889 	return is_longterm_pinnable_page(&folio->page);
1890 }
1891 
set_page_zone(struct page * page,enum zone_type zone)1892 static inline void set_page_zone(struct page *page, enum zone_type zone)
1893 {
1894 	page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT);
1895 	page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT;
1896 }
1897 
set_page_node(struct page * page,unsigned long node)1898 static inline void set_page_node(struct page *page, unsigned long node)
1899 {
1900 	page->flags &= ~(NODES_MASK << NODES_PGSHIFT);
1901 	page->flags |= (node & NODES_MASK) << NODES_PGSHIFT;
1902 }
1903 
set_page_links(struct page * page,enum zone_type zone,unsigned long node,unsigned long pfn)1904 static inline void set_page_links(struct page *page, enum zone_type zone,
1905 	unsigned long node, unsigned long pfn)
1906 {
1907 	set_page_zone(page, zone);
1908 	set_page_node(page, node);
1909 #ifdef SECTION_IN_PAGE_FLAGS
1910 	set_page_section(page, pfn_to_section_nr(pfn));
1911 #endif
1912 }
1913 
1914 /**
1915  * folio_nr_pages - The number of pages in the folio.
1916  * @folio: The folio.
1917  *
1918  * Return: A positive power of two.
1919  */
folio_nr_pages(struct folio * folio)1920 static inline long folio_nr_pages(struct folio *folio)
1921 {
1922 	if (!folio_test_large(folio))
1923 		return 1;
1924 #ifdef CONFIG_64BIT
1925 	return folio->_folio_nr_pages;
1926 #else
1927 	return 1L << folio->_folio_order;
1928 #endif
1929 }
1930 
1931 /**
1932  * folio_next - Move to the next physical folio.
1933  * @folio: The folio we're currently operating on.
1934  *
1935  * If you have physically contiguous memory which may span more than
1936  * one folio (eg a &struct bio_vec), use this function to move from one
1937  * folio to the next.  Do not use it if the memory is only virtually
1938  * contiguous as the folios are almost certainly not adjacent to each
1939  * other.  This is the folio equivalent to writing ``page++``.
1940  *
1941  * Context: We assume that the folios are refcounted and/or locked at a
1942  * higher level and do not adjust the reference counts.
1943  * Return: The next struct folio.
1944  */
folio_next(struct folio * folio)1945 static inline struct folio *folio_next(struct folio *folio)
1946 {
1947 	return (struct folio *)folio_page(folio, folio_nr_pages(folio));
1948 }
1949 
1950 /**
1951  * folio_shift - The size of the memory described by this folio.
1952  * @folio: The folio.
1953  *
1954  * A folio represents a number of bytes which is a power-of-two in size.
1955  * This function tells you which power-of-two the folio is.  See also
1956  * folio_size() and folio_order().
1957  *
1958  * Context: The caller should have a reference on the folio to prevent
1959  * it from being split.  It is not necessary for the folio to be locked.
1960  * Return: The base-2 logarithm of the size of this folio.
1961  */
folio_shift(struct folio * folio)1962 static inline unsigned int folio_shift(struct folio *folio)
1963 {
1964 	return PAGE_SHIFT + folio_order(folio);
1965 }
1966 
1967 /**
1968  * folio_size - The number of bytes in a folio.
1969  * @folio: The folio.
1970  *
1971  * Context: The caller should have a reference on the folio to prevent
1972  * it from being split.  It is not necessary for the folio to be locked.
1973  * Return: The number of bytes in this folio.
1974  */
folio_size(struct folio * folio)1975 static inline size_t folio_size(struct folio *folio)
1976 {
1977 	return PAGE_SIZE << folio_order(folio);
1978 }
1979 
1980 /**
1981  * folio_estimated_sharers - Estimate the number of sharers of a folio.
1982  * @folio: The folio.
1983  *
1984  * folio_estimated_sharers() aims to serve as a function to efficiently
1985  * estimate the number of processes sharing a folio. This is done by
1986  * looking at the precise mapcount of the first subpage in the folio, and
1987  * assuming the other subpages are the same. This may not be true for large
1988  * folios. If you want exact mapcounts for exact calculations, look at
1989  * page_mapcount() or folio_total_mapcount().
1990  *
1991  * Return: The estimated number of processes sharing a folio.
1992  */
folio_estimated_sharers(struct folio * folio)1993 static inline int folio_estimated_sharers(struct folio *folio)
1994 {
1995 	return page_mapcount(folio_page(folio, 0));
1996 }
1997 
1998 
1999 #ifndef HAVE_ARCH_MAKE_PAGE_ACCESSIBLE
arch_make_page_accessible(struct page * page)2000 static inline int arch_make_page_accessible(struct page *page)
2001 {
2002 	return 0;
2003 }
2004 #endif
2005 
2006 #ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE
arch_make_folio_accessible(struct folio * folio)2007 static inline int arch_make_folio_accessible(struct folio *folio)
2008 {
2009 	int ret;
2010 	long i, nr = folio_nr_pages(folio);
2011 
2012 	for (i = 0; i < nr; i++) {
2013 		ret = arch_make_page_accessible(folio_page(folio, i));
2014 		if (ret)
2015 			break;
2016 	}
2017 
2018 	return ret;
2019 }
2020 #endif
2021 
2022 /*
2023  * Some inline functions in vmstat.h depend on page_zone()
2024  */
2025 #include <linux/vmstat.h>
2026 
lowmem_page_address(const struct page * page)2027 static __always_inline void *lowmem_page_address(const struct page *page)
2028 {
2029 	return page_to_virt(page);
2030 }
2031 
2032 #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
2033 #define HASHED_PAGE_VIRTUAL
2034 #endif
2035 
2036 #if defined(WANT_PAGE_VIRTUAL)
page_address(const struct page * page)2037 static inline void *page_address(const struct page *page)
2038 {
2039 	return page->virtual;
2040 }
set_page_address(struct page * page,void * address)2041 static inline void set_page_address(struct page *page, void *address)
2042 {
2043 	page->virtual = address;
2044 }
2045 #define page_address_init()  do { } while(0)
2046 #endif
2047 
2048 #if defined(HASHED_PAGE_VIRTUAL)
2049 void *page_address(const struct page *page);
2050 void set_page_address(struct page *page, void *virtual);
2051 void page_address_init(void);
2052 #endif
2053 
2054 #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
2055 #define page_address(page) lowmem_page_address(page)
2056 #define set_page_address(page, address)  do { } while(0)
2057 #define page_address_init()  do { } while(0)
2058 #endif
2059 
folio_address(const struct folio * folio)2060 static inline void *folio_address(const struct folio *folio)
2061 {
2062 	return page_address(&folio->page);
2063 }
2064 
2065 extern void *page_rmapping(struct page *page);
2066 extern pgoff_t __page_file_index(struct page *page);
2067 
2068 /*
2069  * Return the pagecache index of the passed page.  Regular pagecache pages
2070  * use ->index whereas swapcache pages use swp_offset(->private)
2071  */
page_index(struct page * page)2072 static inline pgoff_t page_index(struct page *page)
2073 {
2074 	if (unlikely(PageSwapCache(page)))
2075 		return __page_file_index(page);
2076 	return page->index;
2077 }
2078 
2079 bool page_mapped(struct page *page);
2080 bool folio_mapped(struct folio *folio);
2081 
2082 /*
2083  * Return true only if the page has been allocated with
2084  * ALLOC_NO_WATERMARKS and the low watermark was not
2085  * met implying that the system is under some pressure.
2086  */
page_is_pfmemalloc(const struct page * page)2087 static inline bool page_is_pfmemalloc(const struct page *page)
2088 {
2089 	/*
2090 	 * lru.next has bit 1 set if the page is allocated from the
2091 	 * pfmemalloc reserves.  Callers may simply overwrite it if
2092 	 * they do not need to preserve that information.
2093 	 */
2094 	return (uintptr_t)page->lru.next & BIT(1);
2095 }
2096 
2097 /*
2098  * Only to be called by the page allocator on a freshly allocated
2099  * page.
2100  */
set_page_pfmemalloc(struct page * page)2101 static inline void set_page_pfmemalloc(struct page *page)
2102 {
2103 	page->lru.next = (void *)BIT(1);
2104 }
2105 
clear_page_pfmemalloc(struct page * page)2106 static inline void clear_page_pfmemalloc(struct page *page)
2107 {
2108 	page->lru.next = NULL;
2109 }
2110 
2111 /*
2112  * Can be called by the pagefault handler when it gets a VM_FAULT_OOM.
2113  */
2114 extern void pagefault_out_of_memory(void);
2115 
2116 #define offset_in_page(p)	((unsigned long)(p) & ~PAGE_MASK)
2117 #define offset_in_thp(page, p)	((unsigned long)(p) & (thp_size(page) - 1))
2118 #define offset_in_folio(folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))
2119 
2120 /*
2121  * Flags passed to show_mem() and show_free_areas() to suppress output in
2122  * various contexts.
2123  */
2124 #define SHOW_MEM_FILTER_NODES		(0x0001u)	/* disallowed nodes */
2125 
2126 extern void __show_free_areas(unsigned int flags, nodemask_t *nodemask, int max_zone_idx);
show_free_areas(unsigned int flags,nodemask_t * nodemask)2127 static void __maybe_unused show_free_areas(unsigned int flags, nodemask_t *nodemask)
2128 {
2129 	__show_free_areas(flags, nodemask, MAX_NR_ZONES - 1);
2130 }
2131 
2132 /*
2133  * Parameter block passed down to zap_pte_range in exceptional cases.
2134  */
2135 struct zap_details {
2136 	struct folio *single_folio;	/* Locked folio to be unmapped */
2137 	bool even_cows;			/* Zap COWed private pages too? */
2138 	zap_flags_t zap_flags;		/* Extra flags for zapping */
2139 };
2140 
2141 /*
2142  * Whether to drop the pte markers, for example, the uffd-wp information for
2143  * file-backed memory.  This should only be specified when we will completely
2144  * drop the page in the mm, either by truncation or unmapping of the vma.  By
2145  * default, the flag is not set.
2146  */
2147 #define  ZAP_FLAG_DROP_MARKER        ((__force zap_flags_t) BIT(0))
2148 /* Set in unmap_vmas() to indicate a final unmap call.  Only used by hugetlb */
2149 #define  ZAP_FLAG_UNMAP              ((__force zap_flags_t) BIT(1))
2150 
2151 #ifdef CONFIG_MMU
2152 extern bool can_do_mlock(void);
2153 #else
can_do_mlock(void)2154 static inline bool can_do_mlock(void) { return false; }
2155 #endif
2156 extern int user_shm_lock(size_t, struct ucounts *);
2157 extern void user_shm_unlock(size_t, struct ucounts *);
2158 
2159 struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr,
2160 			     pte_t pte);
2161 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
2162 			     pte_t pte);
2163 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
2164 				pmd_t pmd);
2165 
2166 void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
2167 		  unsigned long size);
2168 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
2169 		    unsigned long size);
2170 void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
2171 			   unsigned long size, struct zap_details *details);
2172 void unmap_vmas(struct mmu_gather *tlb, struct maple_tree *mt,
2173 		struct vm_area_struct *start_vma, unsigned long start,
2174 		unsigned long end, unsigned long start_t,
2175 		unsigned long end_t, bool mm_wr_locked);
2176 
2177 struct mmu_notifier_range;
2178 
2179 void free_pgd_range(struct mmu_gather *tlb, unsigned long addr,
2180 		unsigned long end, unsigned long floor, unsigned long ceiling);
2181 int
2182 copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
2183 int follow_pte(struct mm_struct *mm, unsigned long address,
2184 	       pte_t **ptepp, spinlock_t **ptlp);
2185 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
2186 	unsigned long *pfn);
2187 int follow_phys(struct vm_area_struct *vma, unsigned long address,
2188 		unsigned int flags, unsigned long *prot, resource_size_t *phys);
2189 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
2190 			void *buf, int len, int write);
2191 
2192 extern void truncate_pagecache(struct inode *inode, loff_t new);
2193 extern void truncate_setsize(struct inode *inode, loff_t newsize);
2194 void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to);
2195 void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end);
2196 int generic_error_remove_page(struct address_space *mapping, struct page *page);
2197 
2198 #ifdef CONFIG_MMU
2199 extern vm_fault_t handle_mm_fault(struct vm_area_struct *vma,
2200 				  unsigned long address, unsigned int flags,
2201 				  struct pt_regs *regs);
2202 extern int fixup_user_fault(struct mm_struct *mm,
2203 			    unsigned long address, unsigned int fault_flags,
2204 			    bool *unlocked);
2205 void unmap_mapping_pages(struct address_space *mapping,
2206 		pgoff_t start, pgoff_t nr, bool even_cows);
2207 void unmap_mapping_range(struct address_space *mapping,
2208 		loff_t const holebegin, loff_t const holelen, int even_cows);
2209 struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,
2210 		unsigned long address, struct pt_regs *regs);
2211 #else
handle_mm_fault(struct vm_area_struct * vma,unsigned long address,unsigned int flags,struct pt_regs * regs)2212 static inline vm_fault_t handle_mm_fault(struct vm_area_struct *vma,
2213 					 unsigned long address, unsigned int flags,
2214 					 struct pt_regs *regs)
2215 {
2216 	/* should never happen if there's no MMU */
2217 	BUG();
2218 	return VM_FAULT_SIGBUS;
2219 }
fixup_user_fault(struct mm_struct * mm,unsigned long address,unsigned int fault_flags,bool * unlocked)2220 static inline int fixup_user_fault(struct mm_struct *mm, unsigned long address,
2221 		unsigned int fault_flags, bool *unlocked)
2222 {
2223 	/* should never happen if there's no MMU */
2224 	BUG();
2225 	return -EFAULT;
2226 }
unmap_mapping_pages(struct address_space * mapping,pgoff_t start,pgoff_t nr,bool even_cows)2227 static inline void unmap_mapping_pages(struct address_space *mapping,
2228 		pgoff_t start, pgoff_t nr, bool even_cows) { }
unmap_mapping_range(struct address_space * mapping,loff_t const holebegin,loff_t const holelen,int even_cows)2229 static inline void unmap_mapping_range(struct address_space *mapping,
2230 		loff_t const holebegin, loff_t const holelen, int even_cows) { }
2231 #endif
2232 
unmap_shared_mapping_range(struct address_space * mapping,loff_t const holebegin,loff_t const holelen)2233 static inline void unmap_shared_mapping_range(struct address_space *mapping,
2234 		loff_t const holebegin, loff_t const holelen)
2235 {
2236 	unmap_mapping_range(mapping, holebegin, holelen, 0);
2237 }
2238 
2239 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
2240 		void *buf, int len, unsigned int gup_flags);
2241 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2242 		void *buf, int len, unsigned int gup_flags);
2243 extern int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
2244 			      void *buf, int len, unsigned int gup_flags);
2245 
2246 long get_user_pages_remote(struct mm_struct *mm,
2247 			    unsigned long start, unsigned long nr_pages,
2248 			    unsigned int gup_flags, struct page **pages,
2249 			    struct vm_area_struct **vmas, int *locked);
2250 long pin_user_pages_remote(struct mm_struct *mm,
2251 			   unsigned long start, unsigned long nr_pages,
2252 			   unsigned int gup_flags, struct page **pages,
2253 			   struct vm_area_struct **vmas, int *locked);
2254 long get_user_pages(unsigned long start, unsigned long nr_pages,
2255 			    unsigned int gup_flags, struct page **pages,
2256 			    struct vm_area_struct **vmas);
2257 long pin_user_pages(unsigned long start, unsigned long nr_pages,
2258 		    unsigned int gup_flags, struct page **pages,
2259 		    struct vm_area_struct **vmas);
2260 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2261 		    struct page **pages, unsigned int gup_flags);
2262 long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2263 		    struct page **pages, unsigned int gup_flags);
2264 
2265 int get_user_pages_fast(unsigned long start, int nr_pages,
2266 			unsigned int gup_flags, struct page **pages);
2267 int pin_user_pages_fast(unsigned long start, int nr_pages,
2268 			unsigned int gup_flags, struct page **pages);
2269 
2270 int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc);
2271 int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc,
2272 			struct task_struct *task, bool bypass_rlim);
2273 
2274 struct kvec;
2275 int get_kernel_pages(const struct kvec *iov, int nr_pages, int write,
2276 			struct page **pages);
2277 struct page *get_dump_page(unsigned long addr);
2278 
2279 bool folio_mark_dirty(struct folio *folio);
2280 bool set_page_dirty(struct page *page);
2281 int set_page_dirty_lock(struct page *page);
2282 
2283 int get_cmdline(struct task_struct *task, char *buffer, int buflen);
2284 
2285 extern unsigned long move_page_tables(struct vm_area_struct *vma,
2286 		unsigned long old_addr, struct vm_area_struct *new_vma,
2287 		unsigned long new_addr, unsigned long len,
2288 		bool need_rmap_locks);
2289 
2290 /*
2291  * Flags used by change_protection().  For now we make it a bitmap so
2292  * that we can pass in multiple flags just like parameters.  However
2293  * for now all the callers are only use one of the flags at the same
2294  * time.
2295  */
2296 /*
2297  * Whether we should manually check if we can map individual PTEs writable,
2298  * because something (e.g., COW, uffd-wp) blocks that from happening for all
2299  * PTEs automatically in a writable mapping.
2300  */
2301 #define  MM_CP_TRY_CHANGE_WRITABLE	   (1UL << 0)
2302 /* Whether this protection change is for NUMA hints */
2303 #define  MM_CP_PROT_NUMA                   (1UL << 1)
2304 /* Whether this change is for write protecting */
2305 #define  MM_CP_UFFD_WP                     (1UL << 2) /* do wp */
2306 #define  MM_CP_UFFD_WP_RESOLVE             (1UL << 3) /* Resolve wp */
2307 #define  MM_CP_UFFD_WP_ALL                 (MM_CP_UFFD_WP | \
2308 					    MM_CP_UFFD_WP_RESOLVE)
2309 
2310 extern unsigned long change_protection(struct mmu_gather *tlb,
2311 			      struct vm_area_struct *vma, unsigned long start,
2312 			      unsigned long end, pgprot_t newprot,
2313 			      unsigned long cp_flags);
2314 extern int mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
2315 			  struct vm_area_struct **pprev, unsigned long start,
2316 			  unsigned long end, unsigned long newflags);
2317 
2318 /*
2319  * doesn't attempt to fault and will return short.
2320  */
2321 int get_user_pages_fast_only(unsigned long start, int nr_pages,
2322 			     unsigned int gup_flags, struct page **pages);
2323 int pin_user_pages_fast_only(unsigned long start, int nr_pages,
2324 			     unsigned int gup_flags, struct page **pages);
2325 
get_user_page_fast_only(unsigned long addr,unsigned int gup_flags,struct page ** pagep)2326 static inline bool get_user_page_fast_only(unsigned long addr,
2327 			unsigned int gup_flags, struct page **pagep)
2328 {
2329 	return get_user_pages_fast_only(addr, 1, gup_flags, pagep) == 1;
2330 }
2331 /*
2332  * per-process(per-mm_struct) statistics.
2333  */
get_mm_counter(struct mm_struct * mm,int member)2334 static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
2335 {
2336 	long val = atomic_long_read(&mm->rss_stat.count[member]);
2337 
2338 #ifdef SPLIT_RSS_COUNTING
2339 	/*
2340 	 * counter is updated in asynchronous manner and may go to minus.
2341 	 * But it's never be expected number for users.
2342 	 */
2343 	if (val < 0)
2344 		val = 0;
2345 #endif
2346 	return (unsigned long)val;
2347 }
2348 
2349 void mm_trace_rss_stat(struct mm_struct *mm, int member, long count);
2350 
add_mm_counter(struct mm_struct * mm,int member,long value)2351 static inline void add_mm_counter(struct mm_struct *mm, int member, long value)
2352 {
2353 	long count = atomic_long_add_return(value, &mm->rss_stat.count[member]);
2354 
2355 	mm_trace_rss_stat(mm, member, count);
2356 }
2357 
inc_mm_counter(struct mm_struct * mm,int member)2358 static inline void inc_mm_counter(struct mm_struct *mm, int member)
2359 {
2360 	long count = atomic_long_inc_return(&mm->rss_stat.count[member]);
2361 
2362 	mm_trace_rss_stat(mm, member, count);
2363 }
2364 
dec_mm_counter(struct mm_struct * mm,int member)2365 static inline void dec_mm_counter(struct mm_struct *mm, int member)
2366 {
2367 	long count = atomic_long_dec_return(&mm->rss_stat.count[member]);
2368 
2369 	mm_trace_rss_stat(mm, member, count);
2370 }
2371 
2372 /* Optimized variant when page is already known not to be PageAnon */
mm_counter_file(struct page * page)2373 static inline int mm_counter_file(struct page *page)
2374 {
2375 	if (PageSwapBacked(page))
2376 		return MM_SHMEMPAGES;
2377 	return MM_FILEPAGES;
2378 }
2379 
mm_counter(struct page * page)2380 static inline int mm_counter(struct page *page)
2381 {
2382 	if (PageAnon(page))
2383 		return MM_ANONPAGES;
2384 	return mm_counter_file(page);
2385 }
2386 
get_mm_rss(struct mm_struct * mm)2387 static inline unsigned long get_mm_rss(struct mm_struct *mm)
2388 {
2389 	return get_mm_counter(mm, MM_FILEPAGES) +
2390 		get_mm_counter(mm, MM_ANONPAGES) +
2391 		get_mm_counter(mm, MM_SHMEMPAGES);
2392 }
2393 
get_mm_hiwater_rss(struct mm_struct * mm)2394 static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
2395 {
2396 	return max(mm->hiwater_rss, get_mm_rss(mm));
2397 }
2398 
get_mm_hiwater_vm(struct mm_struct * mm)2399 static inline unsigned long get_mm_hiwater_vm(struct mm_struct *mm)
2400 {
2401 	return max(mm->hiwater_vm, mm->total_vm);
2402 }
2403 
update_hiwater_rss(struct mm_struct * mm)2404 static inline void update_hiwater_rss(struct mm_struct *mm)
2405 {
2406 	unsigned long _rss = get_mm_rss(mm);
2407 
2408 	if ((mm)->hiwater_rss < _rss)
2409 		(mm)->hiwater_rss = _rss;
2410 }
2411 
update_hiwater_vm(struct mm_struct * mm)2412 static inline void update_hiwater_vm(struct mm_struct *mm)
2413 {
2414 	if (mm->hiwater_vm < mm->total_vm)
2415 		mm->hiwater_vm = mm->total_vm;
2416 }
2417 
reset_mm_hiwater_rss(struct mm_struct * mm)2418 static inline void reset_mm_hiwater_rss(struct mm_struct *mm)
2419 {
2420 	mm->hiwater_rss = get_mm_rss(mm);
2421 }
2422 
setmax_mm_hiwater_rss(unsigned long * maxrss,struct mm_struct * mm)2423 static inline void setmax_mm_hiwater_rss(unsigned long *maxrss,
2424 					 struct mm_struct *mm)
2425 {
2426 	unsigned long hiwater_rss = get_mm_hiwater_rss(mm);
2427 
2428 	if (*maxrss < hiwater_rss)
2429 		*maxrss = hiwater_rss;
2430 }
2431 
2432 #if defined(SPLIT_RSS_COUNTING)
2433 void sync_mm_rss(struct mm_struct *mm);
2434 #else
sync_mm_rss(struct mm_struct * mm)2435 static inline void sync_mm_rss(struct mm_struct *mm)
2436 {
2437 }
2438 #endif
2439 
2440 #ifndef CONFIG_ARCH_HAS_PTE_SPECIAL
pte_special(pte_t pte)2441 static inline int pte_special(pte_t pte)
2442 {
2443 	return 0;
2444 }
2445 
pte_mkspecial(pte_t pte)2446 static inline pte_t pte_mkspecial(pte_t pte)
2447 {
2448 	return pte;
2449 }
2450 #endif
2451 
2452 #ifndef CONFIG_ARCH_HAS_PTE_DEVMAP
pte_devmap(pte_t pte)2453 static inline int pte_devmap(pte_t pte)
2454 {
2455 	return 0;
2456 }
2457 #endif
2458 
2459 int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot);
2460 
2461 extern pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
2462 			       spinlock_t **ptl);
get_locked_pte(struct mm_struct * mm,unsigned long addr,spinlock_t ** ptl)2463 static inline pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,
2464 				    spinlock_t **ptl)
2465 {
2466 	pte_t *ptep;
2467 	__cond_lock(*ptl, ptep = __get_locked_pte(mm, addr, ptl));
2468 	return ptep;
2469 }
2470 
2471 #ifdef __PAGETABLE_P4D_FOLDED
__p4d_alloc(struct mm_struct * mm,pgd_t * pgd,unsigned long address)2472 static inline int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
2473 						unsigned long address)
2474 {
2475 	return 0;
2476 }
2477 #else
2478 int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address);
2479 #endif
2480 
2481 #if defined(__PAGETABLE_PUD_FOLDED) || !defined(CONFIG_MMU)
__pud_alloc(struct mm_struct * mm,p4d_t * p4d,unsigned long address)2482 static inline int __pud_alloc(struct mm_struct *mm, p4d_t *p4d,
2483 						unsigned long address)
2484 {
2485 	return 0;
2486 }
mm_inc_nr_puds(struct mm_struct * mm)2487 static inline void mm_inc_nr_puds(struct mm_struct *mm) {}
mm_dec_nr_puds(struct mm_struct * mm)2488 static inline void mm_dec_nr_puds(struct mm_struct *mm) {}
2489 
2490 #else
2491 int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address);
2492 
mm_inc_nr_puds(struct mm_struct * mm)2493 static inline void mm_inc_nr_puds(struct mm_struct *mm)
2494 {
2495 	if (mm_pud_folded(mm))
2496 		return;
2497 	atomic_long_add(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes);
2498 }
2499 
mm_dec_nr_puds(struct mm_struct * mm)2500 static inline void mm_dec_nr_puds(struct mm_struct *mm)
2501 {
2502 	if (mm_pud_folded(mm))
2503 		return;
2504 	atomic_long_sub(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes);
2505 }
2506 #endif
2507 
2508 #if defined(__PAGETABLE_PMD_FOLDED) || !defined(CONFIG_MMU)
__pmd_alloc(struct mm_struct * mm,pud_t * pud,unsigned long address)2509 static inline int __pmd_alloc(struct mm_struct *mm, pud_t *pud,
2510 						unsigned long address)
2511 {
2512 	return 0;
2513 }
2514 
mm_inc_nr_pmds(struct mm_struct * mm)2515 static inline void mm_inc_nr_pmds(struct mm_struct *mm) {}
mm_dec_nr_pmds(struct mm_struct * mm)2516 static inline void mm_dec_nr_pmds(struct mm_struct *mm) {}
2517 
2518 #else
2519 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address);
2520 
mm_inc_nr_pmds(struct mm_struct * mm)2521 static inline void mm_inc_nr_pmds(struct mm_struct *mm)
2522 {
2523 	if (mm_pmd_folded(mm))
2524 		return;
2525 	atomic_long_add(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes);
2526 }
2527 
mm_dec_nr_pmds(struct mm_struct * mm)2528 static inline void mm_dec_nr_pmds(struct mm_struct *mm)
2529 {
2530 	if (mm_pmd_folded(mm))
2531 		return;
2532 	atomic_long_sub(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes);
2533 }
2534 #endif
2535 
2536 #ifdef CONFIG_MMU
mm_pgtables_bytes_init(struct mm_struct * mm)2537 static inline void mm_pgtables_bytes_init(struct mm_struct *mm)
2538 {
2539 	atomic_long_set(&mm->pgtables_bytes, 0);
2540 }
2541 
mm_pgtables_bytes(const struct mm_struct * mm)2542 static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm)
2543 {
2544 	return atomic_long_read(&mm->pgtables_bytes);
2545 }
2546 
mm_inc_nr_ptes(struct mm_struct * mm)2547 static inline void mm_inc_nr_ptes(struct mm_struct *mm)
2548 {
2549 	atomic_long_add(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes);
2550 }
2551 
mm_dec_nr_ptes(struct mm_struct * mm)2552 static inline void mm_dec_nr_ptes(struct mm_struct *mm)
2553 {
2554 	atomic_long_sub(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes);
2555 }
2556 #else
2557 
mm_pgtables_bytes_init(struct mm_struct * mm)2558 static inline void mm_pgtables_bytes_init(struct mm_struct *mm) {}
mm_pgtables_bytes(const struct mm_struct * mm)2559 static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm)
2560 {
2561 	return 0;
2562 }
2563 
mm_inc_nr_ptes(struct mm_struct * mm)2564 static inline void mm_inc_nr_ptes(struct mm_struct *mm) {}
mm_dec_nr_ptes(struct mm_struct * mm)2565 static inline void mm_dec_nr_ptes(struct mm_struct *mm) {}
2566 #endif
2567 
2568 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd);
2569 int __pte_alloc_kernel(pmd_t *pmd);
2570 
2571 #if defined(CONFIG_MMU)
2572 
p4d_alloc(struct mm_struct * mm,pgd_t * pgd,unsigned long address)2573 static inline p4d_t *p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
2574 		unsigned long address)
2575 {
2576 	return (unlikely(pgd_none(*pgd)) && __p4d_alloc(mm, pgd, address)) ?
2577 		NULL : p4d_offset(pgd, address);
2578 }
2579 
pud_alloc(struct mm_struct * mm,p4d_t * p4d,unsigned long address)2580 static inline pud_t *pud_alloc(struct mm_struct *mm, p4d_t *p4d,
2581 		unsigned long address)
2582 {
2583 	return (unlikely(p4d_none(*p4d)) && __pud_alloc(mm, p4d, address)) ?
2584 		NULL : pud_offset(p4d, address);
2585 }
2586 
pmd_alloc(struct mm_struct * mm,pud_t * pud,unsigned long address)2587 static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2588 {
2589 	return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))?
2590 		NULL: pmd_offset(pud, address);
2591 }
2592 #endif /* CONFIG_MMU */
2593 
2594 #if USE_SPLIT_PTE_PTLOCKS
2595 #if ALLOC_SPLIT_PTLOCKS
2596 void __init ptlock_cache_init(void);
2597 extern bool ptlock_alloc(struct page *page);
2598 extern void ptlock_free(struct page *page);
2599 
ptlock_ptr(struct page * page)2600 static inline spinlock_t *ptlock_ptr(struct page *page)
2601 {
2602 	return page->ptl;
2603 }
2604 #else /* ALLOC_SPLIT_PTLOCKS */
ptlock_cache_init(void)2605 static inline void ptlock_cache_init(void)
2606 {
2607 }
2608 
ptlock_alloc(struct page * page)2609 static inline bool ptlock_alloc(struct page *page)
2610 {
2611 	return true;
2612 }
2613 
ptlock_free(struct page * page)2614 static inline void ptlock_free(struct page *page)
2615 {
2616 }
2617 
ptlock_ptr(struct page * page)2618 static inline spinlock_t *ptlock_ptr(struct page *page)
2619 {
2620 	return &page->ptl;
2621 }
2622 #endif /* ALLOC_SPLIT_PTLOCKS */
2623 
pte_lockptr(struct mm_struct * mm,pmd_t * pmd)2624 static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
2625 {
2626 	return ptlock_ptr(pmd_page(*pmd));
2627 }
2628 
ptlock_init(struct page * page)2629 static inline bool ptlock_init(struct page *page)
2630 {
2631 	/*
2632 	 * prep_new_page() initialize page->private (and therefore page->ptl)
2633 	 * with 0. Make sure nobody took it in use in between.
2634 	 *
2635 	 * It can happen if arch try to use slab for page table allocation:
2636 	 * slab code uses page->slab_cache, which share storage with page->ptl.
2637 	 */
2638 	VM_BUG_ON_PAGE(*(unsigned long *)&page->ptl, page);
2639 	if (!ptlock_alloc(page))
2640 		return false;
2641 	spin_lock_init(ptlock_ptr(page));
2642 	return true;
2643 }
2644 
2645 #else	/* !USE_SPLIT_PTE_PTLOCKS */
2646 /*
2647  * We use mm->page_table_lock to guard all pagetable pages of the mm.
2648  */
pte_lockptr(struct mm_struct * mm,pmd_t * pmd)2649 static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
2650 {
2651 	return &mm->page_table_lock;
2652 }
ptlock_cache_init(void)2653 static inline void ptlock_cache_init(void) {}
ptlock_init(struct page * page)2654 static inline bool ptlock_init(struct page *page) { return true; }
ptlock_free(struct page * page)2655 static inline void ptlock_free(struct page *page) {}
2656 #endif /* USE_SPLIT_PTE_PTLOCKS */
2657 
pgtable_init(void)2658 static inline void pgtable_init(void)
2659 {
2660 	ptlock_cache_init();
2661 	pgtable_cache_init();
2662 }
2663 
pgtable_pte_page_ctor(struct page * page)2664 static inline bool pgtable_pte_page_ctor(struct page *page)
2665 {
2666 	if (!ptlock_init(page))
2667 		return false;
2668 	__SetPageTable(page);
2669 	inc_lruvec_page_state(page, NR_PAGETABLE);
2670 	return true;
2671 }
2672 
pgtable_pte_page_dtor(struct page * page)2673 static inline void pgtable_pte_page_dtor(struct page *page)
2674 {
2675 	ptlock_free(page);
2676 	__ClearPageTable(page);
2677 	dec_lruvec_page_state(page, NR_PAGETABLE);
2678 }
2679 
2680 #define pte_offset_map_lock(mm, pmd, address, ptlp)	\
2681 ({							\
2682 	spinlock_t *__ptl = pte_lockptr(mm, pmd);	\
2683 	pte_t *__pte = pte_offset_map(pmd, address);	\
2684 	*(ptlp) = __ptl;				\
2685 	spin_lock(__ptl);				\
2686 	__pte;						\
2687 })
2688 
2689 #define pte_unmap_unlock(pte, ptl)	do {		\
2690 	spin_unlock(ptl);				\
2691 	pte_unmap(pte);					\
2692 } while (0)
2693 
2694 #define pte_alloc(mm, pmd) (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd))
2695 
2696 #define pte_alloc_map(mm, pmd, address)			\
2697 	(pte_alloc(mm, pmd) ? NULL : pte_offset_map(pmd, address))
2698 
2699 #define pte_alloc_map_lock(mm, pmd, address, ptlp)	\
2700 	(pte_alloc(mm, pmd) ?			\
2701 		 NULL : pte_offset_map_lock(mm, pmd, address, ptlp))
2702 
2703 #define pte_alloc_kernel(pmd, address)			\
2704 	((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd))? \
2705 		NULL: pte_offset_kernel(pmd, address))
2706 
2707 #if USE_SPLIT_PMD_PTLOCKS
2708 
pmd_to_page(pmd_t * pmd)2709 static struct page *pmd_to_page(pmd_t *pmd)
2710 {
2711 	unsigned long mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
2712 	return virt_to_page((void *)((unsigned long) pmd & mask));
2713 }
2714 
pmd_lockptr(struct mm_struct * mm,pmd_t * pmd)2715 static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
2716 {
2717 	return ptlock_ptr(pmd_to_page(pmd));
2718 }
2719 
pmd_ptlock_init(struct page * page)2720 static inline bool pmd_ptlock_init(struct page *page)
2721 {
2722 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2723 	page->pmd_huge_pte = NULL;
2724 #endif
2725 	return ptlock_init(page);
2726 }
2727 
pmd_ptlock_free(struct page * page)2728 static inline void pmd_ptlock_free(struct page *page)
2729 {
2730 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2731 	VM_BUG_ON_PAGE(page->pmd_huge_pte, page);
2732 #endif
2733 	ptlock_free(page);
2734 }
2735 
2736 #define pmd_huge_pte(mm, pmd) (pmd_to_page(pmd)->pmd_huge_pte)
2737 
2738 #else
2739 
pmd_lockptr(struct mm_struct * mm,pmd_t * pmd)2740 static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
2741 {
2742 	return &mm->page_table_lock;
2743 }
2744 
pmd_ptlock_init(struct page * page)2745 static inline bool pmd_ptlock_init(struct page *page) { return true; }
pmd_ptlock_free(struct page * page)2746 static inline void pmd_ptlock_free(struct page *page) {}
2747 
2748 #define pmd_huge_pte(mm, pmd) ((mm)->pmd_huge_pte)
2749 
2750 #endif
2751 
pmd_lock(struct mm_struct * mm,pmd_t * pmd)2752 static inline spinlock_t *pmd_lock(struct mm_struct *mm, pmd_t *pmd)
2753 {
2754 	spinlock_t *ptl = pmd_lockptr(mm, pmd);
2755 	spin_lock(ptl);
2756 	return ptl;
2757 }
2758 
pgtable_pmd_page_ctor(struct page * page)2759 static inline bool pgtable_pmd_page_ctor(struct page *page)
2760 {
2761 	if (!pmd_ptlock_init(page))
2762 		return false;
2763 	__SetPageTable(page);
2764 	inc_lruvec_page_state(page, NR_PAGETABLE);
2765 	return true;
2766 }
2767 
pgtable_pmd_page_dtor(struct page * page)2768 static inline void pgtable_pmd_page_dtor(struct page *page)
2769 {
2770 	pmd_ptlock_free(page);
2771 	__ClearPageTable(page);
2772 	dec_lruvec_page_state(page, NR_PAGETABLE);
2773 }
2774 
2775 /*
2776  * No scalability reason to split PUD locks yet, but follow the same pattern
2777  * as the PMD locks to make it easier if we decide to.  The VM should not be
2778  * considered ready to switch to split PUD locks yet; there may be places
2779  * which need to be converted from page_table_lock.
2780  */
pud_lockptr(struct mm_struct * mm,pud_t * pud)2781 static inline spinlock_t *pud_lockptr(struct mm_struct *mm, pud_t *pud)
2782 {
2783 	return &mm->page_table_lock;
2784 }
2785 
pud_lock(struct mm_struct * mm,pud_t * pud)2786 static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud)
2787 {
2788 	spinlock_t *ptl = pud_lockptr(mm, pud);
2789 
2790 	spin_lock(ptl);
2791 	return ptl;
2792 }
2793 
2794 extern void __init pagecache_init(void);
2795 extern void free_initmem(void);
2796 
2797 /*
2798  * Free reserved pages within range [PAGE_ALIGN(start), end & PAGE_MASK)
2799  * into the buddy system. The freed pages will be poisoned with pattern
2800  * "poison" if it's within range [0, UCHAR_MAX].
2801  * Return pages freed into the buddy system.
2802  */
2803 extern unsigned long free_reserved_area(void *start, void *end,
2804 					int poison, const char *s);
2805 
2806 extern void adjust_managed_page_count(struct page *page, long count);
2807 extern void mem_init_print_info(void);
2808 
2809 extern void reserve_bootmem_region(phys_addr_t start, phys_addr_t end);
2810 
2811 /* Free the reserved page into the buddy system, so it gets managed. */
free_reserved_page(struct page * page)2812 static inline void free_reserved_page(struct page *page)
2813 {
2814 	ClearPageReserved(page);
2815 	init_page_count(page);
2816 	__free_page(page);
2817 	adjust_managed_page_count(page, 1);
2818 }
2819 #define free_highmem_page(page) free_reserved_page(page)
2820 
mark_page_reserved(struct page * page)2821 static inline void mark_page_reserved(struct page *page)
2822 {
2823 	SetPageReserved(page);
2824 	adjust_managed_page_count(page, -1);
2825 }
2826 
2827 /*
2828  * Default method to free all the __init memory into the buddy system.
2829  * The freed pages will be poisoned with pattern "poison" if it's within
2830  * range [0, UCHAR_MAX].
2831  * Return pages freed into the buddy system.
2832  */
free_initmem_default(int poison)2833 static inline unsigned long free_initmem_default(int poison)
2834 {
2835 	extern char __init_begin[], __init_end[];
2836 
2837 	return free_reserved_area(&__init_begin, &__init_end,
2838 				  poison, "unused kernel image (initmem)");
2839 }
2840 
get_num_physpages(void)2841 static inline unsigned long get_num_physpages(void)
2842 {
2843 	int nid;
2844 	unsigned long phys_pages = 0;
2845 
2846 	for_each_online_node(nid)
2847 		phys_pages += node_present_pages(nid);
2848 
2849 	return phys_pages;
2850 }
2851 
2852 /*
2853  * Using memblock node mappings, an architecture may initialise its
2854  * zones, allocate the backing mem_map and account for memory holes in an
2855  * architecture independent manner.
2856  *
2857  * An architecture is expected to register range of page frames backed by
2858  * physical memory with memblock_add[_node]() before calling
2859  * free_area_init() passing in the PFN each zone ends at. At a basic
2860  * usage, an architecture is expected to do something like
2861  *
2862  * unsigned long max_zone_pfns[MAX_NR_ZONES] = {max_dma, max_normal_pfn,
2863  * 							 max_highmem_pfn};
2864  * for_each_valid_physical_page_range()
2865  *	memblock_add_node(base, size, nid, MEMBLOCK_NONE)
2866  * free_area_init(max_zone_pfns);
2867  */
2868 void free_area_init(unsigned long *max_zone_pfn);
2869 unsigned long node_map_pfn_alignment(void);
2870 unsigned long __absent_pages_in_range(int nid, unsigned long start_pfn,
2871 						unsigned long end_pfn);
2872 extern unsigned long absent_pages_in_range(unsigned long start_pfn,
2873 						unsigned long end_pfn);
2874 extern void get_pfn_range_for_nid(unsigned int nid,
2875 			unsigned long *start_pfn, unsigned long *end_pfn);
2876 
2877 #ifndef CONFIG_NUMA
early_pfn_to_nid(unsigned long pfn)2878 static inline int early_pfn_to_nid(unsigned long pfn)
2879 {
2880 	return 0;
2881 }
2882 #else
2883 /* please see mm/page_alloc.c */
2884 extern int __meminit early_pfn_to_nid(unsigned long pfn);
2885 #endif
2886 
2887 extern void set_dma_reserve(unsigned long new_dma_reserve);
2888 extern void memmap_init_range(unsigned long, int, unsigned long,
2889 		unsigned long, unsigned long, enum meminit_context,
2890 		struct vmem_altmap *, int migratetype);
2891 extern void setup_per_zone_wmarks(void);
2892 extern void calculate_min_free_kbytes(void);
2893 extern int __meminit init_per_zone_wmark_min(void);
2894 extern void mem_init(void);
2895 extern void __init mmap_init(void);
2896 
2897 extern void __show_mem(unsigned int flags, nodemask_t *nodemask, int max_zone_idx);
show_mem(unsigned int flags,nodemask_t * nodemask)2898 static inline void show_mem(unsigned int flags, nodemask_t *nodemask)
2899 {
2900 	__show_mem(flags, nodemask, MAX_NR_ZONES - 1);
2901 }
2902 extern long si_mem_available(void);
2903 extern void si_meminfo(struct sysinfo * val);
2904 extern void si_meminfo_node(struct sysinfo *val, int nid);
2905 #ifdef __HAVE_ARCH_RESERVED_KERNEL_PAGES
2906 extern unsigned long arch_reserved_kernel_pages(void);
2907 #endif
2908 
2909 extern __printf(3, 4)
2910 void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...);
2911 
2912 extern void setup_per_cpu_pageset(void);
2913 
2914 /* page_alloc.c */
2915 extern int min_free_kbytes;
2916 extern int watermark_boost_factor;
2917 extern int watermark_scale_factor;
2918 extern bool arch_has_descending_max_zone_pfns(void);
2919 
2920 /* nommu.c */
2921 extern atomic_long_t mmap_pages_allocated;
2922 extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t);
2923 
2924 /* interval_tree.c */
2925 void vma_interval_tree_insert(struct vm_area_struct *node,
2926 			      struct rb_root_cached *root);
2927 void vma_interval_tree_insert_after(struct vm_area_struct *node,
2928 				    struct vm_area_struct *prev,
2929 				    struct rb_root_cached *root);
2930 void vma_interval_tree_remove(struct vm_area_struct *node,
2931 			      struct rb_root_cached *root);
2932 struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root,
2933 				unsigned long start, unsigned long last);
2934 struct vm_area_struct *vma_interval_tree_iter_next(struct vm_area_struct *node,
2935 				unsigned long start, unsigned long last);
2936 
2937 #define vma_interval_tree_foreach(vma, root, start, last)		\
2938 	for (vma = vma_interval_tree_iter_first(root, start, last);	\
2939 	     vma; vma = vma_interval_tree_iter_next(vma, start, last))
2940 
2941 void anon_vma_interval_tree_insert(struct anon_vma_chain *node,
2942 				   struct rb_root_cached *root);
2943 void anon_vma_interval_tree_remove(struct anon_vma_chain *node,
2944 				   struct rb_root_cached *root);
2945 struct anon_vma_chain *
2946 anon_vma_interval_tree_iter_first(struct rb_root_cached *root,
2947 				  unsigned long start, unsigned long last);
2948 struct anon_vma_chain *anon_vma_interval_tree_iter_next(
2949 	struct anon_vma_chain *node, unsigned long start, unsigned long last);
2950 #ifdef CONFIG_DEBUG_VM_RB
2951 void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
2952 #endif
2953 
2954 #define anon_vma_interval_tree_foreach(avc, root, start, last)		 \
2955 	for (avc = anon_vma_interval_tree_iter_first(root, start, last); \
2956 	     avc; avc = anon_vma_interval_tree_iter_next(avc, start, last))
2957 
2958 /* mmap.c */
2959 extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin);
2960 extern int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
2961 	unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
2962 	struct vm_area_struct *expand);
vma_adjust(struct vm_area_struct * vma,unsigned long start,unsigned long end,pgoff_t pgoff,struct vm_area_struct * insert)2963 static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start,
2964 	unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
2965 {
2966 	return __vma_adjust(vma, start, end, pgoff, insert, NULL);
2967 }
2968 extern struct vm_area_struct *vma_merge(struct mm_struct *,
2969 	struct vm_area_struct *prev, unsigned long addr, unsigned long end,
2970 	unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
2971 	struct mempolicy *, struct vm_userfaultfd_ctx, struct anon_vma_name *);
2972 extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
2973 extern int __split_vma(struct mm_struct *, struct vm_area_struct *,
2974 	unsigned long addr, int new_below);
2975 extern int split_vma(struct mm_struct *, struct vm_area_struct *,
2976 	unsigned long addr, int new_below);
2977 extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
2978 extern void unlink_file_vma(struct vm_area_struct *);
2979 extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
2980 	unsigned long addr, unsigned long len, pgoff_t pgoff,
2981 	bool *need_rmap_locks);
2982 extern void exit_mmap(struct mm_struct *);
2983 
2984 void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas);
2985 void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas);
2986 
check_data_rlimit(unsigned long rlim,unsigned long new,unsigned long start,unsigned long end_data,unsigned long start_data)2987 static inline int check_data_rlimit(unsigned long rlim,
2988 				    unsigned long new,
2989 				    unsigned long start,
2990 				    unsigned long end_data,
2991 				    unsigned long start_data)
2992 {
2993 	if (rlim < RLIM_INFINITY) {
2994 		if (((new - start) + (end_data - start_data)) > rlim)
2995 			return -ENOSPC;
2996 	}
2997 
2998 	return 0;
2999 }
3000 
3001 extern int mm_take_all_locks(struct mm_struct *mm);
3002 extern void mm_drop_all_locks(struct mm_struct *mm);
3003 
3004 extern int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
3005 extern int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
3006 extern struct file *get_mm_exe_file(struct mm_struct *mm);
3007 extern struct file *get_task_exe_file(struct task_struct *task);
3008 
3009 extern bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long npages);
3010 extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages);
3011 
3012 extern bool vma_is_special_mapping(const struct vm_area_struct *vma,
3013 				   const struct vm_special_mapping *sm);
3014 extern struct vm_area_struct *_install_special_mapping(struct mm_struct *mm,
3015 				   unsigned long addr, unsigned long len,
3016 				   unsigned long flags,
3017 				   const struct vm_special_mapping *spec);
3018 /* This is an obsolete alternative to _install_special_mapping. */
3019 extern int install_special_mapping(struct mm_struct *mm,
3020 				   unsigned long addr, unsigned long len,
3021 				   unsigned long flags, struct page **pages);
3022 
3023 unsigned long randomize_stack_top(unsigned long stack_top);
3024 unsigned long randomize_page(unsigned long start, unsigned long range);
3025 
3026 extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
3027 
3028 extern unsigned long mmap_region(struct file *file, unsigned long addr,
3029 	unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
3030 	struct list_head *uf);
3031 extern unsigned long do_mmap(struct file *file, unsigned long addr,
3032 	unsigned long len, unsigned long prot, unsigned long flags,
3033 	unsigned long pgoff, unsigned long *populate, struct list_head *uf);
3034 extern int do_mas_munmap(struct ma_state *mas, struct mm_struct *mm,
3035 			 unsigned long start, size_t len, struct list_head *uf,
3036 			 bool downgrade);
3037 extern int do_munmap(struct mm_struct *, unsigned long, size_t,
3038 		     struct list_head *uf);
3039 extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior);
3040 
3041 #ifdef CONFIG_MMU
3042 extern int __mm_populate(unsigned long addr, unsigned long len,
3043 			 int ignore_errors);
mm_populate(unsigned long addr,unsigned long len)3044 static inline void mm_populate(unsigned long addr, unsigned long len)
3045 {
3046 	/* Ignore errors */
3047 	(void) __mm_populate(addr, len, 1);
3048 }
3049 #else
mm_populate(unsigned long addr,unsigned long len)3050 static inline void mm_populate(unsigned long addr, unsigned long len) {}
3051 #endif
3052 
3053 /* These take the mm semaphore themselves */
3054 extern int __must_check vm_brk(unsigned long, unsigned long);
3055 extern int __must_check vm_brk_flags(unsigned long, unsigned long, unsigned long);
3056 extern int vm_munmap(unsigned long, size_t);
3057 extern unsigned long __must_check vm_mmap(struct file *, unsigned long,
3058         unsigned long, unsigned long,
3059         unsigned long, unsigned long);
3060 
3061 struct vm_unmapped_area_info {
3062 #define VM_UNMAPPED_AREA_TOPDOWN 1
3063 	unsigned long flags;
3064 	unsigned long length;
3065 	unsigned long low_limit;
3066 	unsigned long high_limit;
3067 	unsigned long align_mask;
3068 	unsigned long align_offset;
3069 };
3070 
3071 extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info);
3072 
3073 /* truncate.c */
3074 extern void truncate_inode_pages(struct address_space *, loff_t);
3075 extern void truncate_inode_pages_range(struct address_space *,
3076 				       loff_t lstart, loff_t lend);
3077 extern void truncate_inode_pages_final(struct address_space *);
3078 
3079 /* generic vm_area_ops exported for stackable file systems */
3080 extern vm_fault_t filemap_fault(struct vm_fault *vmf);
3081 extern vm_fault_t filemap_map_pages(struct vm_fault *vmf,
3082 		pgoff_t start_pgoff, pgoff_t end_pgoff);
3083 extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf);
3084 
3085 extern unsigned long stack_guard_gap;
3086 /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
3087 int expand_stack_locked(struct vm_area_struct *vma, unsigned long address);
3088 struct vm_area_struct *expand_stack(struct mm_struct * mm, unsigned long addr);
3089 
3090 /* CONFIG_STACK_GROWSUP still needs to grow downwards at some places */
3091 int expand_downwards(struct vm_area_struct *vma, unsigned long address);
3092 
3093 /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
3094 extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
3095 extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
3096 					     struct vm_area_struct **pprev);
3097 
3098 /*
3099  * Look up the first VMA which intersects the interval [start_addr, end_addr)
3100  * NULL if none.  Assume start_addr < end_addr.
3101  */
3102 struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
3103 			unsigned long start_addr, unsigned long end_addr);
3104 
3105 /**
3106  * vma_lookup() - Find a VMA at a specific address
3107  * @mm: The process address space.
3108  * @addr: The user address.
3109  *
3110  * Return: The vm_area_struct at the given address, %NULL otherwise.
3111  */
3112 static inline
vma_lookup(struct mm_struct * mm,unsigned long addr)3113 struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr)
3114 {
3115 	return mtree_load(&mm->mm_mt, addr);
3116 }
3117 
vm_start_gap(struct vm_area_struct * vma)3118 static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
3119 {
3120 	unsigned long vm_start = vma->vm_start;
3121 
3122 	if (vma->vm_flags & VM_GROWSDOWN) {
3123 		vm_start -= stack_guard_gap;
3124 		if (vm_start > vma->vm_start)
3125 			vm_start = 0;
3126 	}
3127 	return vm_start;
3128 }
3129 
vm_end_gap(struct vm_area_struct * vma)3130 static inline unsigned long vm_end_gap(struct vm_area_struct *vma)
3131 {
3132 	unsigned long vm_end = vma->vm_end;
3133 
3134 	if (vma->vm_flags & VM_GROWSUP) {
3135 		vm_end += stack_guard_gap;
3136 		if (vm_end < vma->vm_end)
3137 			vm_end = -PAGE_SIZE;
3138 	}
3139 	return vm_end;
3140 }
3141 
vma_pages(struct vm_area_struct * vma)3142 static inline unsigned long vma_pages(struct vm_area_struct *vma)
3143 {
3144 	return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
3145 }
3146 
3147 /* Look up the first VMA which exactly match the interval vm_start ... vm_end */
find_exact_vma(struct mm_struct * mm,unsigned long vm_start,unsigned long vm_end)3148 static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,
3149 				unsigned long vm_start, unsigned long vm_end)
3150 {
3151 	struct vm_area_struct *vma = vma_lookup(mm, vm_start);
3152 
3153 	if (vma && (vma->vm_start != vm_start || vma->vm_end != vm_end))
3154 		vma = NULL;
3155 
3156 	return vma;
3157 }
3158 
range_in_vma(struct vm_area_struct * vma,unsigned long start,unsigned long end)3159 static inline bool range_in_vma(struct vm_area_struct *vma,
3160 				unsigned long start, unsigned long end)
3161 {
3162 	return (vma && vma->vm_start <= start && end <= vma->vm_end);
3163 }
3164 
3165 #ifdef CONFIG_MMU
3166 pgprot_t vm_get_page_prot(unsigned long vm_flags);
3167 void vma_set_page_prot(struct vm_area_struct *vma);
3168 #else
vm_get_page_prot(unsigned long vm_flags)3169 static inline pgprot_t vm_get_page_prot(unsigned long vm_flags)
3170 {
3171 	return __pgprot(0);
3172 }
vma_set_page_prot(struct vm_area_struct * vma)3173 static inline void vma_set_page_prot(struct vm_area_struct *vma)
3174 {
3175 	vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
3176 }
3177 #endif
3178 
3179 void vma_set_file(struct vm_area_struct *vma, struct file *file);
3180 
3181 #ifdef CONFIG_NUMA_BALANCING
3182 unsigned long change_prot_numa(struct vm_area_struct *vma,
3183 			unsigned long start, unsigned long end);
3184 #endif
3185 
3186 struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
3187 struct vm_area_struct *find_extend_vma_locked(struct mm_struct *,
3188 		unsigned long addr);
3189 int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
3190 			unsigned long pfn, unsigned long size, pgprot_t);
3191 int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
3192 		unsigned long pfn, unsigned long size, pgprot_t prot);
3193 int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
3194 int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
3195 			struct page **pages, unsigned long *num);
3196 int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
3197 				unsigned long num);
3198 int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
3199 				unsigned long num);
3200 vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
3201 			unsigned long pfn);
3202 vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
3203 			unsigned long pfn, pgprot_t pgprot);
3204 vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
3205 			pfn_t pfn);
3206 vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
3207 			pfn_t pfn, pgprot_t pgprot);
3208 vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
3209 		unsigned long addr, pfn_t pfn);
3210 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len);
3211 
vmf_insert_page(struct vm_area_struct * vma,unsigned long addr,struct page * page)3212 static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma,
3213 				unsigned long addr, struct page *page)
3214 {
3215 	int err = vm_insert_page(vma, addr, page);
3216 
3217 	if (err == -ENOMEM)
3218 		return VM_FAULT_OOM;
3219 	if (err < 0 && err != -EBUSY)
3220 		return VM_FAULT_SIGBUS;
3221 
3222 	return VM_FAULT_NOPAGE;
3223 }
3224 
3225 #ifndef io_remap_pfn_range
io_remap_pfn_range(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn,unsigned long size,pgprot_t prot)3226 static inline int io_remap_pfn_range(struct vm_area_struct *vma,
3227 				     unsigned long addr, unsigned long pfn,
3228 				     unsigned long size, pgprot_t prot)
3229 {
3230 	return remap_pfn_range(vma, addr, pfn, size, pgprot_decrypted(prot));
3231 }
3232 #endif
3233 
vmf_error(int err)3234 static inline vm_fault_t vmf_error(int err)
3235 {
3236 	if (err == -ENOMEM)
3237 		return VM_FAULT_OOM;
3238 	return VM_FAULT_SIGBUS;
3239 }
3240 
3241 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
3242 			 unsigned int foll_flags);
3243 
3244 #define FOLL_WRITE	0x01	/* check pte is writable */
3245 #define FOLL_TOUCH	0x02	/* mark page accessed */
3246 #define FOLL_GET	0x04	/* do get_page on page */
3247 #define FOLL_DUMP	0x08	/* give error on hole if it would be zero */
3248 #define FOLL_FORCE	0x10	/* get_user_pages read/write w/o permission */
3249 #define FOLL_NOWAIT	0x20	/* if a disk transfer is needed, start the IO
3250 				 * and return without waiting upon it */
3251 #define FOLL_NOFAULT	0x80	/* do not fault in pages */
3252 #define FOLL_HWPOISON	0x100	/* check page is hwpoisoned */
3253 #define FOLL_MIGRATION	0x400	/* wait for page to replace migration entry */
3254 #define FOLL_TRIED	0x800	/* a retry, previous pass started an IO */
3255 #define FOLL_REMOTE	0x2000	/* we are working on non-current tsk/mm */
3256 #define FOLL_ANON	0x8000	/* don't do file mappings */
3257 #define FOLL_LONGTERM	0x10000	/* mapping lifetime is indefinite: see below */
3258 #define FOLL_SPLIT_PMD	0x20000	/* split huge pmd before returning */
3259 #define FOLL_PIN	0x40000	/* pages must be released via unpin_user_page */
3260 #define FOLL_FAST_ONLY	0x80000	/* gup_fast: prevent fall-back to slow gup */
3261 
3262 /*
3263  * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
3264  * other. Here is what they mean, and how to use them:
3265  *
3266  * FOLL_LONGTERM indicates that the page will be held for an indefinite time
3267  * period _often_ under userspace control.  This is in contrast to
3268  * iov_iter_get_pages(), whose usages are transient.
3269  *
3270  * FIXME: For pages which are part of a filesystem, mappings are subject to the
3271  * lifetime enforced by the filesystem and we need guarantees that longterm
3272  * users like RDMA and V4L2 only establish mappings which coordinate usage with
3273  * the filesystem.  Ideas for this coordination include revoking the longterm
3274  * pin, delaying writeback, bounce buffer page writeback, etc.  As FS DAX was
3275  * added after the problem with filesystems was found FS DAX VMAs are
3276  * specifically failed.  Filesystem pages are still subject to bugs and use of
3277  * FOLL_LONGTERM should be avoided on those pages.
3278  *
3279  * FIXME: Also NOTE that FOLL_LONGTERM is not supported in every GUP call.
3280  * Currently only get_user_pages() and get_user_pages_fast() support this flag
3281  * and calls to get_user_pages_[un]locked are specifically not allowed.  This
3282  * is due to an incompatibility with the FS DAX check and
3283  * FAULT_FLAG_ALLOW_RETRY.
3284  *
3285  * In the CMA case: long term pins in a CMA region would unnecessarily fragment
3286  * that region.  And so, CMA attempts to migrate the page before pinning, when
3287  * FOLL_LONGTERM is specified.
3288  *
3289  * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
3290  * but an additional pin counting system) will be invoked. This is intended for
3291  * anything that gets a page reference and then touches page data (for example,
3292  * Direct IO). This lets the filesystem know that some non-file-system entity is
3293  * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
3294  * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
3295  * a call to unpin_user_page().
3296  *
3297  * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
3298  * and separate refcounting mechanisms, however, and that means that each has
3299  * its own acquire and release mechanisms:
3300  *
3301  *     FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
3302  *
3303  *     FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
3304  *
3305  * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
3306  * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
3307  * calls applied to them, and that's perfectly OK. This is a constraint on the
3308  * callers, not on the pages.)
3309  *
3310  * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
3311  * directly by the caller. That's in order to help avoid mismatches when
3312  * releasing pages: get_user_pages*() pages must be released via put_page(),
3313  * while pin_user_pages*() pages must be released via unpin_user_page().
3314  *
3315  * Please see Documentation/core-api/pin_user_pages.rst for more information.
3316  */
3317 
vm_fault_to_errno(vm_fault_t vm_fault,int foll_flags)3318 static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags)
3319 {
3320 	if (vm_fault & VM_FAULT_OOM)
3321 		return -ENOMEM;
3322 	if (vm_fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
3323 		return (foll_flags & FOLL_HWPOISON) ? -EHWPOISON : -EFAULT;
3324 	if (vm_fault & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
3325 		return -EFAULT;
3326 	return 0;
3327 }
3328 
3329 /*
3330  * Indicates for which pages that are write-protected in the page table,
3331  * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
3332  * GUP pin will remain consistent with the pages mapped into the page tables
3333  * of the MM.
3334  *
3335  * Temporary unmapping of PageAnonExclusive() pages or clearing of
3336  * PageAnonExclusive() has to protect against concurrent GUP:
3337  * * Ordinary GUP: Using the PT lock
3338  * * GUP-fast and fork(): mm->write_protect_seq
3339  * * GUP-fast and KSM or temporary unmapping (swap, migration): see
3340  *    page_try_share_anon_rmap()
3341  *
3342  * Must be called with the (sub)page that's actually referenced via the
3343  * page table entry, which might not necessarily be the head page for a
3344  * PTE-mapped THP.
3345  */
gup_must_unshare(unsigned int flags,struct page * page)3346 static inline bool gup_must_unshare(unsigned int flags, struct page *page)
3347 {
3348 	/*
3349 	 * FOLL_WRITE is implicitly handled correctly as the page table entry
3350 	 * has to be writable -- and if it references (part of) an anonymous
3351 	 * folio, that part is required to be marked exclusive.
3352 	 */
3353 	if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
3354 		return false;
3355 	/*
3356 	 * Note: PageAnon(page) is stable until the page is actually getting
3357 	 * freed.
3358 	 */
3359 	if (!PageAnon(page))
3360 		return false;
3361 
3362 	/* Paired with a memory barrier in page_try_share_anon_rmap(). */
3363 	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
3364 		smp_rmb();
3365 
3366 	/*
3367 	 * During GUP-fast we might not get called on the head page for a
3368 	 * hugetlb page that is mapped using cont-PTE, because GUP-fast does
3369 	 * not work with the abstracted hugetlb PTEs that always point at the
3370 	 * head page. For hugetlb, PageAnonExclusive only applies on the head
3371 	 * page (as it cannot be partially COW-shared), so lookup the head page.
3372 	 */
3373 	if (unlikely(!PageHead(page) && PageHuge(page)))
3374 		page = compound_head(page);
3375 
3376 	/*
3377 	 * Note that PageKsm() pages cannot be exclusive, and consequently,
3378 	 * cannot get pinned.
3379 	 */
3380 	return !PageAnonExclusive(page);
3381 }
3382 
3383 /*
3384  * Indicates whether GUP can follow a PROT_NONE mapped page, or whether
3385  * a (NUMA hinting) fault is required.
3386  */
gup_can_follow_protnone(unsigned int flags)3387 static inline bool gup_can_follow_protnone(unsigned int flags)
3388 {
3389 	/*
3390 	 * FOLL_FORCE has to be able to make progress even if the VMA is
3391 	 * inaccessible. Further, FOLL_FORCE access usually does not represent
3392 	 * application behaviour and we should avoid triggering NUMA hinting
3393 	 * faults.
3394 	 */
3395 	return flags & FOLL_FORCE;
3396 }
3397 
3398 typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data);
3399 extern int apply_to_page_range(struct mm_struct *mm, unsigned long address,
3400 			       unsigned long size, pte_fn_t fn, void *data);
3401 extern int apply_to_existing_page_range(struct mm_struct *mm,
3402 				   unsigned long address, unsigned long size,
3403 				   pte_fn_t fn, void *data);
3404 
3405 extern void __init init_mem_debugging_and_hardening(void);
3406 #ifdef CONFIG_PAGE_POISONING
3407 extern void __kernel_poison_pages(struct page *page, int numpages);
3408 extern void __kernel_unpoison_pages(struct page *page, int numpages);
3409 extern bool _page_poisoning_enabled_early;
3410 DECLARE_STATIC_KEY_FALSE(_page_poisoning_enabled);
page_poisoning_enabled(void)3411 static inline bool page_poisoning_enabled(void)
3412 {
3413 	return _page_poisoning_enabled_early;
3414 }
3415 /*
3416  * For use in fast paths after init_mem_debugging() has run, or when a
3417  * false negative result is not harmful when called too early.
3418  */
page_poisoning_enabled_static(void)3419 static inline bool page_poisoning_enabled_static(void)
3420 {
3421 	return static_branch_unlikely(&_page_poisoning_enabled);
3422 }
kernel_poison_pages(struct page * page,int numpages)3423 static inline void kernel_poison_pages(struct page *page, int numpages)
3424 {
3425 	if (page_poisoning_enabled_static())
3426 		__kernel_poison_pages(page, numpages);
3427 }
kernel_unpoison_pages(struct page * page,int numpages)3428 static inline void kernel_unpoison_pages(struct page *page, int numpages)
3429 {
3430 	if (page_poisoning_enabled_static())
3431 		__kernel_unpoison_pages(page, numpages);
3432 }
3433 #else
page_poisoning_enabled(void)3434 static inline bool page_poisoning_enabled(void) { return false; }
page_poisoning_enabled_static(void)3435 static inline bool page_poisoning_enabled_static(void) { return false; }
__kernel_poison_pages(struct page * page,int nunmpages)3436 static inline void __kernel_poison_pages(struct page *page, int nunmpages) { }
kernel_poison_pages(struct page * page,int numpages)3437 static inline void kernel_poison_pages(struct page *page, int numpages) { }
kernel_unpoison_pages(struct page * page,int numpages)3438 static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
3439 #endif
3440 
3441 DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc);
want_init_on_alloc(gfp_t flags)3442 static inline bool want_init_on_alloc(gfp_t flags)
3443 {
3444 	if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
3445 				&init_on_alloc))
3446 		return true;
3447 	return flags & __GFP_ZERO;
3448 }
3449 
3450 DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free);
want_init_on_free(void)3451 static inline bool want_init_on_free(void)
3452 {
3453 	return static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
3454 				   &init_on_free);
3455 }
3456 
3457 extern bool _debug_pagealloc_enabled_early;
3458 DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
3459 
debug_pagealloc_enabled(void)3460 static inline bool debug_pagealloc_enabled(void)
3461 {
3462 	return IS_ENABLED(CONFIG_DEBUG_PAGEALLOC) &&
3463 		_debug_pagealloc_enabled_early;
3464 }
3465 
3466 /*
3467  * For use in fast paths after init_debug_pagealloc() has run, or when a
3468  * false negative result is not harmful when called too early.
3469  */
debug_pagealloc_enabled_static(void)3470 static inline bool debug_pagealloc_enabled_static(void)
3471 {
3472 	if (!IS_ENABLED(CONFIG_DEBUG_PAGEALLOC))
3473 		return false;
3474 
3475 	return static_branch_unlikely(&_debug_pagealloc_enabled);
3476 }
3477 
3478 #ifdef CONFIG_DEBUG_PAGEALLOC
3479 /*
3480  * To support DEBUG_PAGEALLOC architecture must ensure that
3481  * __kernel_map_pages() never fails
3482  */
3483 extern void __kernel_map_pages(struct page *page, int numpages, int enable);
3484 
debug_pagealloc_map_pages(struct page * page,int numpages)3485 static inline void debug_pagealloc_map_pages(struct page *page, int numpages)
3486 {
3487 	if (debug_pagealloc_enabled_static())
3488 		__kernel_map_pages(page, numpages, 1);
3489 }
3490 
debug_pagealloc_unmap_pages(struct page * page,int numpages)3491 static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages)
3492 {
3493 	if (debug_pagealloc_enabled_static())
3494 		__kernel_map_pages(page, numpages, 0);
3495 }
3496 #else	/* CONFIG_DEBUG_PAGEALLOC */
debug_pagealloc_map_pages(struct page * page,int numpages)3497 static inline void debug_pagealloc_map_pages(struct page *page, int numpages) {}
debug_pagealloc_unmap_pages(struct page * page,int numpages)3498 static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages) {}
3499 #endif	/* CONFIG_DEBUG_PAGEALLOC */
3500 
3501 #ifdef __HAVE_ARCH_GATE_AREA
3502 extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
3503 extern int in_gate_area_no_mm(unsigned long addr);
3504 extern int in_gate_area(struct mm_struct *mm, unsigned long addr);
3505 #else
get_gate_vma(struct mm_struct * mm)3506 static inline struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
3507 {
3508 	return NULL;
3509 }
in_gate_area_no_mm(unsigned long addr)3510 static inline int in_gate_area_no_mm(unsigned long addr) { return 0; }
in_gate_area(struct mm_struct * mm,unsigned long addr)3511 static inline int in_gate_area(struct mm_struct *mm, unsigned long addr)
3512 {
3513 	return 0;
3514 }
3515 #endif	/* __HAVE_ARCH_GATE_AREA */
3516 
3517 extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm);
3518 
3519 #ifdef CONFIG_SYSCTL
3520 extern int sysctl_drop_caches;
3521 int drop_caches_sysctl_handler(struct ctl_table *, int, void *, size_t *,
3522 		loff_t *);
3523 #endif
3524 
3525 void drop_slab(void);
3526 
3527 #ifndef CONFIG_MMU
3528 #define randomize_va_space 0
3529 #else
3530 extern int randomize_va_space;
3531 #endif
3532 
3533 const char * arch_vma_name(struct vm_area_struct *vma);
3534 #ifdef CONFIG_MMU
3535 void print_vma_addr(char *prefix, unsigned long rip);
3536 #else
print_vma_addr(char * prefix,unsigned long rip)3537 static inline void print_vma_addr(char *prefix, unsigned long rip)
3538 {
3539 }
3540 #endif
3541 
3542 void *sparse_buffer_alloc(unsigned long size);
3543 struct page * __populate_section_memmap(unsigned long pfn,
3544 		unsigned long nr_pages, int nid, struct vmem_altmap *altmap,
3545 		struct dev_pagemap *pgmap);
3546 pgd_t *vmemmap_pgd_populate(unsigned long addr, int node);
3547 p4d_t *vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node);
3548 pud_t *vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node);
3549 pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node);
3550 pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
3551 			    struct vmem_altmap *altmap, struct page *reuse);
3552 void *vmemmap_alloc_block(unsigned long size, int node);
3553 struct vmem_altmap;
3554 void *vmemmap_alloc_block_buf(unsigned long size, int node,
3555 			      struct vmem_altmap *altmap);
3556 void vmemmap_verify(pte_t *, int, unsigned long, unsigned long);
3557 int vmemmap_populate_basepages(unsigned long start, unsigned long end,
3558 			       int node, struct vmem_altmap *altmap);
3559 int vmemmap_populate(unsigned long start, unsigned long end, int node,
3560 		struct vmem_altmap *altmap);
3561 void vmemmap_populate_print_last(void);
3562 #ifdef CONFIG_MEMORY_HOTPLUG
3563 void vmemmap_free(unsigned long start, unsigned long end,
3564 		struct vmem_altmap *altmap);
3565 #endif
3566 void register_page_bootmem_memmap(unsigned long section_nr, struct page *map,
3567 				  unsigned long nr_pages);
3568 
3569 enum mf_flags {
3570 	MF_COUNT_INCREASED = 1 << 0,
3571 	MF_ACTION_REQUIRED = 1 << 1,
3572 	MF_MUST_KILL = 1 << 2,
3573 	MF_SOFT_OFFLINE = 1 << 3,
3574 	MF_UNPOISON = 1 << 4,
3575 	MF_SW_SIMULATED = 1 << 5,
3576 	MF_NO_RETRY = 1 << 6,
3577 };
3578 int mf_dax_kill_procs(struct address_space *mapping, pgoff_t index,
3579 		      unsigned long count, int mf_flags);
3580 extern int memory_failure(unsigned long pfn, int flags);
3581 extern void memory_failure_queue_kick(int cpu);
3582 extern int unpoison_memory(unsigned long pfn);
3583 extern int sysctl_memory_failure_early_kill;
3584 extern int sysctl_memory_failure_recovery;
3585 extern void shake_page(struct page *p);
3586 extern atomic_long_t num_poisoned_pages __read_mostly;
3587 extern int soft_offline_page(unsigned long pfn, int flags);
3588 #ifdef CONFIG_MEMORY_FAILURE
3589 extern void memory_failure_queue(unsigned long pfn, int flags);
3590 extern int __get_huge_page_for_hwpoison(unsigned long pfn, int flags);
3591 #else
memory_failure_queue(unsigned long pfn,int flags)3592 static inline void memory_failure_queue(unsigned long pfn, int flags)
3593 {
3594 }
__get_huge_page_for_hwpoison(unsigned long pfn,int flags)3595 static inline int __get_huge_page_for_hwpoison(unsigned long pfn, int flags)
3596 {
3597 	return 0;
3598 }
3599 #endif
3600 
3601 #ifndef arch_memory_failure
arch_memory_failure(unsigned long pfn,int flags)3602 static inline int arch_memory_failure(unsigned long pfn, int flags)
3603 {
3604 	return -ENXIO;
3605 }
3606 #endif
3607 
3608 #ifndef arch_is_platform_page
arch_is_platform_page(u64 paddr)3609 static inline bool arch_is_platform_page(u64 paddr)
3610 {
3611 	return false;
3612 }
3613 #endif
3614 
3615 /*
3616  * Error handlers for various types of pages.
3617  */
3618 enum mf_result {
3619 	MF_IGNORED,	/* Error: cannot be handled */
3620 	MF_FAILED,	/* Error: handling failed */
3621 	MF_DELAYED,	/* Will be handled later */
3622 	MF_RECOVERED,	/* Successfully recovered */
3623 };
3624 
3625 enum mf_action_page_type {
3626 	MF_MSG_KERNEL,
3627 	MF_MSG_KERNEL_HIGH_ORDER,
3628 	MF_MSG_SLAB,
3629 	MF_MSG_DIFFERENT_COMPOUND,
3630 	MF_MSG_HUGE,
3631 	MF_MSG_FREE_HUGE,
3632 	MF_MSG_UNMAP_FAILED,
3633 	MF_MSG_DIRTY_SWAPCACHE,
3634 	MF_MSG_CLEAN_SWAPCACHE,
3635 	MF_MSG_DIRTY_MLOCKED_LRU,
3636 	MF_MSG_CLEAN_MLOCKED_LRU,
3637 	MF_MSG_DIRTY_UNEVICTABLE_LRU,
3638 	MF_MSG_CLEAN_UNEVICTABLE_LRU,
3639 	MF_MSG_DIRTY_LRU,
3640 	MF_MSG_CLEAN_LRU,
3641 	MF_MSG_TRUNCATED_LRU,
3642 	MF_MSG_BUDDY,
3643 	MF_MSG_DAX,
3644 	MF_MSG_UNSPLIT_THP,
3645 	MF_MSG_UNKNOWN,
3646 };
3647 
3648 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
3649 extern void clear_huge_page(struct page *page,
3650 			    unsigned long addr_hint,
3651 			    unsigned int pages_per_huge_page);
3652 extern void copy_user_huge_page(struct page *dst, struct page *src,
3653 				unsigned long addr_hint,
3654 				struct vm_area_struct *vma,
3655 				unsigned int pages_per_huge_page);
3656 extern long copy_huge_page_from_user(struct page *dst_page,
3657 				const void __user *usr_src,
3658 				unsigned int pages_per_huge_page,
3659 				bool allow_pagefault);
3660 
3661 /**
3662  * vma_is_special_huge - Are transhuge page-table entries considered special?
3663  * @vma: Pointer to the struct vm_area_struct to consider
3664  *
3665  * Whether transhuge page-table entries are considered "special" following
3666  * the definition in vm_normal_page().
3667  *
3668  * Return: true if transhuge page-table entries should be considered special,
3669  * false otherwise.
3670  */
vma_is_special_huge(const struct vm_area_struct * vma)3671 static inline bool vma_is_special_huge(const struct vm_area_struct *vma)
3672 {
3673 	return vma_is_dax(vma) || (vma->vm_file &&
3674 				   (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)));
3675 }
3676 
3677 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
3678 
3679 #ifdef CONFIG_DEBUG_PAGEALLOC
3680 extern unsigned int _debug_guardpage_minorder;
3681 DECLARE_STATIC_KEY_FALSE(_debug_guardpage_enabled);
3682 
debug_guardpage_minorder(void)3683 static inline unsigned int debug_guardpage_minorder(void)
3684 {
3685 	return _debug_guardpage_minorder;
3686 }
3687 
debug_guardpage_enabled(void)3688 static inline bool debug_guardpage_enabled(void)
3689 {
3690 	return static_branch_unlikely(&_debug_guardpage_enabled);
3691 }
3692 
page_is_guard(struct page * page)3693 static inline bool page_is_guard(struct page *page)
3694 {
3695 	if (!debug_guardpage_enabled())
3696 		return false;
3697 
3698 	return PageGuard(page);
3699 }
3700 #else
debug_guardpage_minorder(void)3701 static inline unsigned int debug_guardpage_minorder(void) { return 0; }
debug_guardpage_enabled(void)3702 static inline bool debug_guardpage_enabled(void) { return false; }
page_is_guard(struct page * page)3703 static inline bool page_is_guard(struct page *page) { return false; }
3704 #endif /* CONFIG_DEBUG_PAGEALLOC */
3705 
3706 #if MAX_NUMNODES > 1
3707 void __init setup_nr_node_ids(void);
3708 #else
setup_nr_node_ids(void)3709 static inline void setup_nr_node_ids(void) {}
3710 #endif
3711 
3712 extern int memcmp_pages(struct page *page1, struct page *page2);
3713 
pages_identical(struct page * page1,struct page * page2)3714 static inline int pages_identical(struct page *page1, struct page *page2)
3715 {
3716 	return !memcmp_pages(page1, page2);
3717 }
3718 
3719 #ifdef CONFIG_MAPPING_DIRTY_HELPERS
3720 unsigned long clean_record_shared_mapping_range(struct address_space *mapping,
3721 						pgoff_t first_index, pgoff_t nr,
3722 						pgoff_t bitmap_pgoff,
3723 						unsigned long *bitmap,
3724 						pgoff_t *start,
3725 						pgoff_t *end);
3726 
3727 unsigned long wp_shared_mapping_range(struct address_space *mapping,
3728 				      pgoff_t first_index, pgoff_t nr);
3729 #endif
3730 
3731 extern int sysctl_nr_trim_pages;
3732 extern int reclaim_shmem_address_space(struct address_space *mapping);
3733 
3734 #ifdef CONFIG_PRINTK
3735 void mem_dump_obj(void *object);
3736 #else
mem_dump_obj(void * object)3737 static inline void mem_dump_obj(void *object) {}
3738 #endif
3739 
3740 /**
3741  * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it
3742  * @seals: the seals to check
3743  * @vma: the vma to operate on
3744  *
3745  * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on
3746  * the vma flags.  Return 0 if check pass, or <0 for errors.
3747  */
seal_check_future_write(int seals,struct vm_area_struct * vma)3748 static inline int seal_check_future_write(int seals, struct vm_area_struct *vma)
3749 {
3750 	if (seals & F_SEAL_FUTURE_WRITE) {
3751 		/*
3752 		 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
3753 		 * "future write" seal active.
3754 		 */
3755 		if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
3756 			return -EPERM;
3757 
3758 		/*
3759 		 * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as
3760 		 * MAP_SHARED and read-only, take care to not allow mprotect to
3761 		 * revert protections on such mappings. Do this only for shared
3762 		 * mappings. For private mappings, don't need to mask
3763 		 * VM_MAYWRITE as we still want them to be COW-writable.
3764 		 */
3765 		if (vma->vm_flags & VM_SHARED)
3766 			vm_flags_clear(vma, VM_MAYWRITE);
3767 	}
3768 
3769 	return 0;
3770 }
3771 
3772 #ifdef CONFIG_ANON_VMA_NAME
3773 int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
3774 			  unsigned long len_in,
3775 			  struct anon_vma_name *anon_name);
3776 #else
3777 static inline int
madvise_set_anon_name(struct mm_struct * mm,unsigned long start,unsigned long len_in,struct anon_vma_name * anon_name)3778 madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
3779 		      unsigned long len_in, struct anon_vma_name *anon_name) {
3780 	return 0;
3781 }
3782 #endif
3783 
3784 #endif /* _LINUX_MM_H */
3785