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