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