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