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