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