blob: ff79e257a861e62239053a5c96bc1ebb9afc4620 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_MM_H
3#define _LINUX_MM_H
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/errno.h>
6
7#ifdef __KERNEL__
8
Sasha Levin309381fea2014-01-23 15:52:54 -08009#include <linux/mmdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/gfp.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050011#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/list.h>
13#include <linux/mmzone.h>
14#include <linux/rbtree.h>
Konstantin Khlebnikov83aeead2011-12-08 14:33:54 -080015#include <linux/atomic.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070016#include <linux/debug_locks.h>
Heiko Carstens5b99cd02006-09-27 01:50:01 -070017#include <linux/mm_types.h>
Michel Lespinasse9740ca42020-06-08 21:33:14 -070018#include <linux/mmap_lock.h>
Yinghai Lu08677212010-02-10 01:20:20 -080019#include <linux/range.h>
Chris Metcalfc6f6b592010-05-24 14:32:53 -070020#include <linux/pfn.h>
Dan Williams3565fce2016-01-15 16:56:55 -080021#include <linux/percpu-refcount.h>
Andrea Arcangelie9da73d2011-01-13 15:46:32 -080022#include <linux/bit_spinlock.h>
Dave Chinnerb0d40c92011-07-08 14:14:42 +100023#include <linux/shrinker.h>
Cyrill Gorcunov9c599022014-10-09 15:27:29 -070024#include <linux/resource.h>
Joonsoo Kime30825f2014-12-12 16:55:49 -080025#include <linux/page_ext.h>
Jan Kara8025e5d2015-07-13 11:55:44 -030026#include <linux/err.h>
Matthew Wilcox (Oracle)41901562020-08-14 17:30:26 -070027#include <linux/page-flags.h>
Joonsoo Kimfe896d12016-03-17 14:19:26 -070028#include <linux/page_ref.h>
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070029#include <linux/memremap.h>
Kees Cook3b3b1a29e2018-05-08 12:55:26 -070030#include <linux/overflow.h>
Nikolay Borisovb5420232019-03-11 23:28:13 -070031#include <linux/sizes.h>
Anshuman Khandual7969f222020-04-01 21:07:49 -070032#include <linux/sched.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070033#include <linux/pgtable.h>
Andrey Konovalov4e35a812020-12-22 12:03:10 -080034#include <linux/kasan.h>
Minchan Kim6e12c5b2021-03-18 09:56:10 -070035#include <linux/page_pinner.h>
Greg Kroah-Hartman8faaa072020-05-02 09:41:26 +020036#include <linux/android_kabi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct mempolicy;
39struct anon_vma;
Michel Lespinassebf181b92012-10-08 16:31:39 -070040struct anon_vma_chain;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040041struct file_ra_state;
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040042struct user_struct;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040043struct writeback_control;
Tejun Heo682aa8e2015-05-28 14:50:53 -040044struct bdi_writeback;
Peter Xubce617e2020-08-11 18:37:44 -070045struct pt_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds5ef64cc2020-09-13 14:05:35 -070047extern int sysctl_page_lock_unfairness;
48
Michal Hocko597b7302017-03-31 15:11:47 -070049void init_mm_internals(void);
50
Jiang Liufccc9982013-07-03 15:04:23 -070051#ifndef CONFIG_NEED_MULTIPLE_NODES /* Don't use mapnrs, do it properly */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052extern unsigned long max_mapnr;
Jiang Liufccc9982013-07-03 15:04:23 -070053
54static inline void set_max_mapnr(unsigned long limit)
55{
56 max_mapnr = limit;
57}
58#else
59static inline void set_max_mapnr(unsigned long limit) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
Arun KSca79b0c2018-12-28 00:34:29 -080062extern atomic_long_t _totalram_pages;
63static inline unsigned long totalram_pages(void)
64{
65 return (unsigned long)atomic_long_read(&_totalram_pages);
66}
67
68static inline void totalram_pages_inc(void)
69{
70 atomic_long_inc(&_totalram_pages);
71}
72
73static inline void totalram_pages_dec(void)
74{
75 atomic_long_dec(&_totalram_pages);
76}
77
78static inline void totalram_pages_add(long count)
79{
80 atomic_long_add(count, &_totalram_pages);
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083extern void * high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084extern int page_cluster;
85
86#ifdef CONFIG_SYSCTL
87extern int sysctl_legacy_va_layout;
88#else
89#define sysctl_legacy_va_layout 0
90#endif
91
Daniel Cashmand07e2252016-01-14 15:19:53 -080092#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
93extern const int mmap_rnd_bits_min;
94extern const int mmap_rnd_bits_max;
95extern int mmap_rnd_bits __read_mostly;
96#endif
97#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
98extern const int mmap_rnd_compat_bits_min;
99extern const int mmap_rnd_compat_bits_max;
100extern int mmap_rnd_compat_bits __read_mostly;
101#endif
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Andrey Konovalovd9344522019-06-04 14:04:47 +0200106/*
107 * Architectures that support memory tagging (assigning tags to memory regions,
108 * embedding these tags into addresses that point to these memory regions, and
109 * checking that the memory and the pointer tags match on memory accesses)
110 * redefine this macro to strip tags from pointers.
111 * It's defined as noop for arcitectures that don't support memory tagging.
112 */
113#ifndef untagged_addr
114#define untagged_addr(addr) (addr)
115#endif
116
Tang Chen79442ed2013-11-12 15:07:59 -0800117#ifndef __pa_symbol
118#define __pa_symbol(x) __pa(RELOC_HIDE((unsigned long)(x), 0))
119#endif
120
Sami Tolvanen6010ce342020-09-10 15:33:56 -0700121#ifndef __va_function
122#define __va_function(x) (x)
123#endif
124
125#ifndef __pa_function
126#define __pa_function(x) __pa_symbol(x)
127#endif
128
Ard Biesheuvel1dff8082016-04-18 18:04:57 +0200129#ifndef page_to_virt
130#define page_to_virt(x) __va(PFN_PHYS(page_to_pfn(x)))
131#endif
132
Laura Abbott568c5fe2017-01-10 13:35:42 -0800133#ifndef lm_alias
134#define lm_alias(x) __va(__pa_symbol(x))
135#endif
136
Dominik Dingel593befa2014-10-23 12:07:44 +0200137/*
138 * To prevent common memory management code establishing
139 * a zero page mapping on a read fault.
140 * This macro should be defined within <asm/pgtable.h>.
141 * s390 does this to prevent multiplexing of hardware bits
142 * related to the physical page in case of virtualization.
143 */
144#ifndef mm_forbids_zeropage
145#define mm_forbids_zeropage(X) (0)
146#endif
147
Andrey Ryabininea606cf2016-03-17 14:18:48 -0700148/*
Pavel Tatashina4a3ede2017-11-15 17:36:31 -0800149 * On some architectures it is expensive to call memset() for small sizes.
Alexander Duyck5470dea2019-05-13 17:21:10 -0700150 * If an architecture decides to implement their own version of
151 * mm_zero_struct_page they should wrap the defines below in a #ifndef and
152 * define their own version of this macro in <asm/pgtable.h>
Pavel Tatashina4a3ede2017-11-15 17:36:31 -0800153 */
Alexander Duyck5470dea2019-05-13 17:21:10 -0700154#if BITS_PER_LONG == 64
155/* This function must be updated when the size of struct page grows above 80
156 * or reduces below 56. The idea that compiler optimizes out switch()
157 * statement, and only leaves move/store instructions. Also the compiler can
158 * combine write statments if they are both assignments and can be reordered,
159 * this can result in several of the writes here being dropped.
160 */
161#define mm_zero_struct_page(pp) __mm_zero_struct_page(pp)
162static inline void __mm_zero_struct_page(struct page *page)
163{
164 unsigned long *_pp = (void *)page;
165
166 /* Check that struct page is either 56, 64, 72, or 80 bytes */
167 BUILD_BUG_ON(sizeof(struct page) & 7);
168 BUILD_BUG_ON(sizeof(struct page) < 56);
169 BUILD_BUG_ON(sizeof(struct page) > 80);
170
171 switch (sizeof(struct page)) {
172 case 80:
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500173 _pp[9] = 0;
174 fallthrough;
Alexander Duyck5470dea2019-05-13 17:21:10 -0700175 case 72:
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500176 _pp[8] = 0;
177 fallthrough;
Alexander Duyck5470dea2019-05-13 17:21:10 -0700178 case 64:
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500179 _pp[7] = 0;
180 fallthrough;
Alexander Duyck5470dea2019-05-13 17:21:10 -0700181 case 56:
182 _pp[6] = 0;
183 _pp[5] = 0;
184 _pp[4] = 0;
185 _pp[3] = 0;
186 _pp[2] = 0;
187 _pp[1] = 0;
188 _pp[0] = 0;
189 }
190}
191#else
Pavel Tatashina4a3ede2017-11-15 17:36:31 -0800192#define mm_zero_struct_page(pp) ((void)memset((pp), 0, sizeof(struct page)))
193#endif
194
195/*
Andrey Ryabininea606cf2016-03-17 14:18:48 -0700196 * Default maximum number of active map areas, this limits the number of vmas
197 * per mm struct. Users can overwrite this number by sysctl but there is a
198 * problem.
199 *
200 * When a program's coredump is generated as ELF format, a section is created
201 * per a vma. In ELF, the number of sections is represented in unsigned short.
202 * This means the number of sections should be smaller than 65535 at coredump.
203 * Because the kernel adds some informative sections to a image of program at
204 * generating coredump, we need some margin. The number of extra sections is
205 * 1-3 now and depends on arch. We use "5" as safe margin, here.
206 *
207 * ELF extended numbering allows more than 65535 sections, so 16-bit bound is
208 * not a hard limit any more. Although some userspace tools can be surprised by
209 * that.
210 */
211#define MAPCOUNT_ELF_CORE_MARGIN (5)
212#define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN)
213
214extern int sysctl_max_map_count;
215
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -0700216extern unsigned long sysctl_user_reserve_kbytes;
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700217extern unsigned long sysctl_admin_reserve_kbytes;
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -0700218
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800219extern int sysctl_overcommit_memory;
220extern int sysctl_overcommit_ratio;
221extern unsigned long sysctl_overcommit_kbytes;
222
Christoph Hellwig32927392020-04-24 08:43:38 +0200223int overcommit_ratio_handler(struct ctl_table *, int, void *, size_t *,
224 loff_t *);
225int overcommit_kbytes_handler(struct ctl_table *, int, void *, size_t *,
226 loff_t *);
Feng Tang56f35472020-08-06 23:23:15 -0700227int overcommit_policy_handler(struct ctl_table *, int, void *, size_t *,
228 loff_t *);
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
231
Andrea Righi27ac7922008-07-23 21:28:13 -0700232/* to align the pointer to the (next) page boundary */
233#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
234
Andrew Morton0fa73b82013-07-03 15:02:11 -0700235/* test whether an address (unsigned long or pointer) is aligned to PAGE_SIZE */
zijun_hu1061b0d22016-10-07 17:02:04 -0700236#define PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), PAGE_SIZE)
Andrew Morton0fa73b82013-07-03 15:02:11 -0700237
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800238#define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Linux kernel virtual memory manager primitives.
242 * The idea being to have a "virtual" mm in the same way
243 * we have a virtual fs - giving a cleaner interface to the
244 * mm details, and allowing different kinds of memory mappings
245 * (from shared memory to executable loading to arbitrary
246 * mmap() functions).
247 */
248
Linus Torvalds490fc052018-07-21 15:24:03 -0700249struct vm_area_struct *vm_area_alloc(struct mm_struct *);
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700250struct vm_area_struct *vm_area_dup(struct vm_area_struct *);
251void vm_area_free(struct vm_area_struct *);
Christoph Lameterc43692e2006-12-06 20:32:48 -0800252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#ifndef CONFIG_MMU
David Howells8feae132009-01-08 12:04:47 +0000254extern struct rb_root nommu_region_tree;
255extern struct rw_semaphore nommu_region_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257extern unsigned int kobjsize(const void *objp);
258#endif
259
260/*
Hugh Dickins605d9282008-08-16 11:07:21 +0100261 * vm_flags in vm_area_struct, see mm_types.h.
Kirill A. Shutemovbcf66912016-03-17 14:18:53 -0700262 * When changing, update also include/trace/events/mmflags.h
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Konstantin Khlebnikovcc2383e2012-10-08 16:28:37 -0700264#define VM_NONE 0x00000000
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#define VM_READ 0x00000001 /* currently active flags */
267#define VM_WRITE 0x00000002
268#define VM_EXEC 0x00000004
269#define VM_SHARED 0x00000008
270
Paolo 'Blaisorblade' Giarrusso7e2cff42005-09-21 09:55:39 -0700271/* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
273#define VM_MAYWRITE 0x00000020
274#define VM_MAYEXEC 0x00000040
275#define VM_MAYSHARE 0x00000080
276
277#define VM_GROWSDOWN 0x00000100 /* general info on the segment */
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700278#define VM_UFFD_MISSING 0x00000200 /* missing pages tracking */
Linus Torvalds6aab3412005-11-28 14:34:23 -0800279#define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700281#define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#define VM_LOCKED 0x00002000
284#define VM_IO 0x00004000 /* Memory mapped I/O or similar */
285
286 /* Used by sys_madvise() */
287#define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
288#define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
289
290#define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */
291#define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800292#define VM_LOCKONFAULT 0x00080000 /* Lock the pages covered when they are faulted in */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */
Andy Whitcroftcdfd4322008-07-23 21:27:28 -0700294#define VM_NORESERVE 0x00200000 /* should the VM suppress accounting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
Jan Karab6fb2932017-11-01 16:36:41 +0100296#define VM_SYNC 0x00800000 /* Synchronous page faults */
Konstantin Khlebnikovcc2383e2012-10-08 16:28:37 -0700297#define VM_ARCH_1 0x01000000 /* Architecture-specific flag */
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700298#define VM_WIPEONFORK 0x02000000 /* Wipe VMA contents in child. */
Konstantin Khlebnikov0103bd12012-10-08 16:28:59 -0700299#define VM_DONTDUMP 0x04000000 /* Do not include in the core dump */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -0700301#ifdef CONFIG_MEM_SOFT_DIRTY
302# define VM_SOFTDIRTY 0x08000000 /* Not soft dirty clean area */
303#else
304# define VM_SOFTDIRTY 0
305#endif
306
Jared Hulbertb379d792008-04-28 02:12:58 -0700307#define VM_MIXEDMAP 0x10000000 /* Can contain "struct page" and pure PFN pages */
Konstantin Khlebnikovcc2383e2012-10-08 16:28:37 -0700308#define VM_HUGEPAGE 0x20000000 /* MADV_HUGEPAGE marked this vma */
309#define VM_NOHUGEPAGE 0x40000000 /* MADV_NOHUGEPAGE marked this vma */
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700310#define VM_MERGEABLE 0x80000000 /* KSM may merge identical pages */
Nick Piggind00806b2007-07-19 01:46:57 -0700311
Dave Hansen63c17fb2016-02-12 13:02:08 -0800312#ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS
313#define VM_HIGH_ARCH_BIT_0 32 /* bit only usable on 64-bit architectures */
314#define VM_HIGH_ARCH_BIT_1 33 /* bit only usable on 64-bit architectures */
315#define VM_HIGH_ARCH_BIT_2 34 /* bit only usable on 64-bit architectures */
316#define VM_HIGH_ARCH_BIT_3 35 /* bit only usable on 64-bit architectures */
Rik van Rieldf3735c2017-09-06 16:25:11 -0700317#define VM_HIGH_ARCH_BIT_4 36 /* bit only usable on 64-bit architectures */
Dave Hansen63c17fb2016-02-12 13:02:08 -0800318#define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0)
319#define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1)
320#define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2)
321#define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3)
Rik van Rieldf3735c2017-09-06 16:25:11 -0700322#define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4)
Dave Hansen63c17fb2016-02-12 13:02:08 -0800323#endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */
324
Ram Pai52122132018-03-27 02:09:26 -0700325#ifdef CONFIG_ARCH_HAS_PKEYS
Dave Hansen8f62c882016-02-12 13:02:10 -0800326# define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0
327# define VM_PKEY_BIT0 VM_HIGH_ARCH_0 /* A protection key is a 4-bit value */
Ram Pai2c9e0a62018-03-27 02:09:27 -0700328# define VM_PKEY_BIT1 VM_HIGH_ARCH_1 /* on x86 and 5-bit value on ppc64 */
Dave Hansen8f62c882016-02-12 13:02:10 -0800329# define VM_PKEY_BIT2 VM_HIGH_ARCH_2
330# define VM_PKEY_BIT3 VM_HIGH_ARCH_3
Ram Pai2c9e0a62018-03-27 02:09:27 -0700331#ifdef CONFIG_PPC
332# define VM_PKEY_BIT4 VM_HIGH_ARCH_4
333#else
334# define VM_PKEY_BIT4 0
Dave Hansen8f62c882016-02-12 13:02:10 -0800335#endif
Ram Pai52122132018-03-27 02:09:26 -0700336#endif /* CONFIG_ARCH_HAS_PKEYS */
337
338#if defined(CONFIG_X86)
339# define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */
Shawn Anastasio12564482020-08-21 13:55:56 -0500340#elif defined(CONFIG_PPC)
341# define VM_SAO VM_ARCH_1 /* Strong Access Ordering (powerpc) */
Konstantin Khlebnikovcc2383e2012-10-08 16:28:37 -0700342#elif defined(CONFIG_PARISC)
343# define VM_GROWSUP VM_ARCH_1
344#elif defined(CONFIG_IA64)
345# define VM_GROWSUP VM_ARCH_1
Khalid Aziz74a04962018-02-23 15:46:41 -0700346#elif defined(CONFIG_SPARC64)
347# define VM_SPARC_ADI VM_ARCH_1 /* Uses ADI tag for access control */
348# define VM_ARCH_CLEAR VM_SPARC_ADI
Dave Martin8ef8f3602020-03-16 16:50:45 +0000349#elif defined(CONFIG_ARM64)
350# define VM_ARM64_BTI VM_ARCH_1 /* BTI guarded page, a.k.a. GP bit */
351# define VM_ARCH_CLEAR VM_ARM64_BTI
Konstantin Khlebnikovcc2383e2012-10-08 16:28:37 -0700352#elif !defined(CONFIG_MMU)
353# define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */
354#endif
355
Catalin Marinas9f341932019-11-27 10:00:27 +0000356#if defined(CONFIG_ARM64_MTE)
357# define VM_MTE VM_HIGH_ARCH_0 /* Use Tagged memory for access control */
358# define VM_MTE_ALLOWED VM_HIGH_ARCH_1 /* Tagged memory permitted */
359#else
360# define VM_MTE VM_NONE
361# define VM_MTE_ALLOWED VM_NONE
362#endif
363
Konstantin Khlebnikovcc2383e2012-10-08 16:28:37 -0700364#ifndef VM_GROWSUP
365# define VM_GROWSUP VM_NONE
366#endif
367
Axel Rasmussen4d3dd332021-03-18 17:01:50 +1100368#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
369# define VM_UFFD_MINOR_BIT 37
370# define VM_UFFD_MINOR BIT(VM_UFFD_MINOR_BIT) /* UFFD minor faults */
371#else /* !CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
372# define VM_UFFD_MINOR VM_NONE
373#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
374
Mel Gormana8bef8ff2010-05-24 14:32:24 -0700375/* Bits set in the VMA until the stack is in its final location */
376#define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ)
377
Anshuman Khandualc62da0c2020-04-10 14:33:05 -0700378#define TASK_EXEC ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0)
379
380/* Common data flag combinations */
381#define VM_DATA_FLAGS_TSK_EXEC (VM_READ | VM_WRITE | TASK_EXEC | \
382 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
383#define VM_DATA_FLAGS_NON_EXEC (VM_READ | VM_WRITE | VM_MAYREAD | \
384 VM_MAYWRITE | VM_MAYEXEC)
385#define VM_DATA_FLAGS_EXEC (VM_READ | VM_WRITE | VM_EXEC | \
386 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
387
388#ifndef VM_DATA_DEFAULT_FLAGS /* arch can override this */
389#define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_EXEC
390#endif
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */
393#define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
394#endif
395
396#ifdef CONFIG_STACK_GROWSUP
Konstantin Khlebnikov30bdbb72016-02-02 16:57:46 -0800397#define VM_STACK VM_GROWSUP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#else
Konstantin Khlebnikov30bdbb72016-02-02 16:57:46 -0800399#define VM_STACK VM_GROWSDOWN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#endif
401
Konstantin Khlebnikov30bdbb72016-02-02 16:57:46 -0800402#define VM_STACK_FLAGS (VM_STACK | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
403
Anshuman Khandual6cb4d9a2020-04-10 14:33:09 -0700404/* VMA basic access permission flags */
405#define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
406
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
Andrea Arcangeli78f11a22011-04-27 15:26:45 -0700409 * Special vmas that are non-mergable, non-mlock()able.
Nick Pigginb291f002008-10-18 20:26:44 -0700410 */
Vlastimil Babka9050d7e2014-03-03 15:38:27 -0800411#define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP)
Nick Pigginb291f002008-10-18 20:26:44 -0700412
Anshuman Khandualb4443772020-04-01 21:07:45 -0700413/* This mask prevents VMA from being scanned with khugepaged */
414#define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB)
415
Alex Thorltona0715cc2014-04-07 15:37:10 -0700416/* This mask defines which mm->def_flags a process can inherit its parent */
417#define VM_INIT_DEF_MASK VM_NOHUGEPAGE
418
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800419/* This mask is used to clear all the VMA flags used by mlock */
420#define VM_LOCKED_CLEAR_MASK (~(VM_LOCKED | VM_LOCKONFAULT))
421
Khalid Aziz2c2d57b2018-02-21 10:15:50 -0700422/* Arch-specific flags to clear when updating VM flags on protection change */
423#ifndef VM_ARCH_CLEAR
424# define VM_ARCH_CLEAR VM_NONE
425#endif
426#define VM_FLAGS_CLEAR (ARCH_VM_PKEY_FLAGS | VM_ARCH_CLEAR)
427
Nick Pigginb291f002008-10-18 20:26:44 -0700428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * mapping from the currently active vm_flags protection bits (the
430 * low four bits) to a page protection mask..
431 */
432extern pgprot_t protection_map[16];
433
Peter Xuc270a7e2020-04-01 21:08:41 -0700434/**
435 * Fault flag definitions.
436 *
437 * @FAULT_FLAG_WRITE: Fault was a write fault.
438 * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE.
439 * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700440 * @FAULT_FLAG_RETRY_NOWAIT: Don't drop mmap_lock and wait when retrying.
Peter Xuc270a7e2020-04-01 21:08:41 -0700441 * @FAULT_FLAG_KILLABLE: The fault task is in SIGKILL killable region.
442 * @FAULT_FLAG_TRIED: The fault has been tried once.
443 * @FAULT_FLAG_USER: The fault originated in userspace.
444 * @FAULT_FLAG_REMOTE: The fault is not for current task/mm.
445 * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch.
446 * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals.
Peter Xu4064b982020-04-01 21:08:45 -0700447 *
448 * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify
449 * whether we would allow page faults to retry by specifying these two
450 * fault flags correctly. Currently there can be three legal combinations:
451 *
452 * (a) ALLOW_RETRY and !TRIED: this means the page fault allows retry, and
453 * this is the first try
454 *
455 * (b) ALLOW_RETRY and TRIED: this means the page fault allows retry, and
456 * we've already tried at least once
457 *
458 * (c) !ALLOW_RETRY and !TRIED: this means the page fault does not allow retry
459 *
460 * The unlisted combination (!ALLOW_RETRY && TRIED) is illegal and should never
461 * be used. Note that page faults can be allowed to retry for multiple times,
462 * in which case we'll have an initial fault with flags (a) then later on
463 * continuous faults with flags (b). We should always try to detect pending
464 * signals before a retry to make sure the continuous page faults can still be
465 * interrupted if necessary.
Peter Xuc270a7e2020-04-01 21:08:41 -0700466 */
467#define FAULT_FLAG_WRITE 0x01
468#define FAULT_FLAG_MKWRITE 0x02
469#define FAULT_FLAG_ALLOW_RETRY 0x04
470#define FAULT_FLAG_RETRY_NOWAIT 0x08
471#define FAULT_FLAG_KILLABLE 0x10
472#define FAULT_FLAG_TRIED 0x20
473#define FAULT_FLAG_USER 0x40
474#define FAULT_FLAG_REMOTE 0x80
475#define FAULT_FLAG_INSTRUCTION 0x100
476#define FAULT_FLAG_INTERRUPTIBLE 0x200
Peter Zijlstra1c537172018-04-17 16:33:24 +0200477/* Speculative fault, not holding mmap_sem */
Will Deacondb6cf102021-01-14 15:24:19 +0000478#define FAULT_FLAG_SPECULATIVE 0x400
Nick Piggind0217ac2007-07-19 01:47:03 -0700479
Peter Xudde16072020-04-01 21:08:37 -0700480/*
481 * The default fault flags that should be used by most of the
482 * arch-specific page fault handlers.
483 */
484#define FAULT_FLAG_DEFAULT (FAULT_FLAG_ALLOW_RETRY | \
Peter Xuc270a7e2020-04-01 21:08:41 -0700485 FAULT_FLAG_KILLABLE | \
486 FAULT_FLAG_INTERRUPTIBLE)
Peter Xudde16072020-04-01 21:08:37 -0700487
Peter Xu4064b982020-04-01 21:08:45 -0700488/**
489 * fault_flag_allow_retry_first - check ALLOW_RETRY the first time
490 *
491 * This is mostly used for places where we want to try to avoid taking
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700492 * the mmap_lock for too long a time when waiting for another condition
Peter Xu4064b982020-04-01 21:08:45 -0700493 * to change, in which case we can try to be polite to release the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700494 * mmap_lock in the first round to avoid potential starvation of other
495 * processes that would also want the mmap_lock.
Peter Xu4064b982020-04-01 21:08:45 -0700496 *
497 * Return: true if the page fault allows retry and this is the first
498 * attempt of the fault handling; false otherwise.
499 */
500static inline bool fault_flag_allow_retry_first(unsigned int flags)
501{
502 return (flags & FAULT_FLAG_ALLOW_RETRY) &&
503 (!(flags & FAULT_FLAG_TRIED));
504}
Nick Piggind0217ac2007-07-19 01:47:03 -0700505
Ross Zwisler282a8e02017-02-22 15:39:50 -0800506#define FAULT_FLAG_TRACE \
507 { FAULT_FLAG_WRITE, "WRITE" }, \
508 { FAULT_FLAG_MKWRITE, "MKWRITE" }, \
509 { FAULT_FLAG_ALLOW_RETRY, "ALLOW_RETRY" }, \
510 { FAULT_FLAG_RETRY_NOWAIT, "RETRY_NOWAIT" }, \
511 { FAULT_FLAG_KILLABLE, "KILLABLE" }, \
512 { FAULT_FLAG_TRIED, "TRIED" }, \
513 { FAULT_FLAG_USER, "USER" }, \
514 { FAULT_FLAG_REMOTE, "REMOTE" }, \
Peter Xuc270a7e2020-04-01 21:08:41 -0700515 { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \
Will Deacondb6cf102021-01-14 15:24:19 +0000516 { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }
Ross Zwisler282a8e02017-02-22 15:39:50 -0800517
venkatesh.pallipadi@intel.com6bd9cd52008-12-19 13:47:26 -0800518/*
Randy Dunlap111923372020-08-11 18:32:30 -0700519 * vm_fault is filled by the pagefault handler and passed to the vma's
Nick Piggin83c54072007-07-19 01:47:05 -0700520 * ->fault function. The vma's ->fault is responsible for returning a bitmask
521 * of VM_FAULT_xxx flags that give details about how the fault was handled.
Nick Piggin54cb8822007-07-19 01:46:59 -0700522 *
Michal Hockoc20cd452016-01-14 15:20:12 -0800523 * MM layer fills up gfp_mask for page allocations but fault handler might
524 * alter it if its implementation requires a different allocation context.
525 *
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -0800526 * pgoff should be used in favour of virtual_address, if possible.
Nick Piggin54cb8822007-07-19 01:46:59 -0700527 */
Nick Piggind0217ac2007-07-19 01:47:03 -0700528struct vm_fault {
Peter Zijlstra1c537172018-04-17 16:33:24 +0200529#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
530 unsigned int sequence;
531 pmd_t orig_pmd; /* value of PMD at the time of fault */
532#endif
Will Deaconccdd6172021-01-14 15:44:09 +0000533 const struct {
Will Deacon249439d2021-01-20 14:34:23 +0000534 struct vm_area_struct *vma; /* Target VMA */
535 gfp_t gfp_mask; /* gfp mask to be used for allocations */
536 pgoff_t pgoff; /* Logical page offset based on vma */
537 unsigned long address; /* Faulting virtual address */
538 };
539 unsigned int flags; /* FAULT_FLAG_xxx flags
540 * XXX: should really be 'const' */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800541 pmd_t *pmd; /* Pointer to pmd entry matching
Jan Kara29943022016-12-14 15:07:16 -0800542 * the 'address' */
Dave Jianga2d58162017-02-24 14:56:59 -0800543 pud_t *pud; /* Pointer to pud entry matching
544 * the 'address'
545 */
Jan Kara29943022016-12-14 15:07:16 -0800546 pte_t orig_pte; /* Value of PTE at the time of fault */
Nick Piggind0217ac2007-07-19 01:47:03 -0700547
Jan Kara39170482016-12-14 15:07:18 -0800548 struct page *cow_page; /* Page handler may use for COW fault */
Nick Piggind0217ac2007-07-19 01:47:03 -0700549 struct page *page; /* ->fault handlers should return a
Nick Piggin83c54072007-07-19 01:47:05 -0700550 * page here, unless VM_FAULT_NOPAGE
Nick Piggind0217ac2007-07-19 01:47:03 -0700551 * is set (which is also implied by
Nick Piggin83c54072007-07-19 01:47:05 -0700552 * VM_FAULT_ERROR).
Nick Piggind0217ac2007-07-19 01:47:03 -0700553 */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800554 /* These three entries are valid only while holding ptl lock */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700555 pte_t *pte; /* Pointer to pte entry matching
556 * the 'address'. NULL if the page
557 * table hasn't been allocated.
558 */
559 spinlock_t *ptl; /* Page table lock.
560 * Protects pte page table if 'pte'
561 * is not NULL, otherwise pmd.
562 */
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -0700563 pgtable_t prealloc_pte; /* Pre-allocated pte page table.
Kirill A. Shutemov0aa300a2020-12-19 15:19:23 +0300564 * vm_ops->map_pages() sets up a page
565 * table from atomic context.
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -0700566 * do_fault_around() pre-allocates
567 * page table to avoid allocation from
568 * atomic context.
569 */
Laurent Dufour32507b62018-04-17 16:33:18 +0200570 /*
571 * These entries are required when handling speculative page fault.
572 * This way the page handling is done using consistent field values.
573 */
574 unsigned long vma_flags;
575 pgprot_t vma_page_prot;
Liangliang Lida33f6f2021-06-08 10:00:02 +0800576 ANDROID_OEM_DATA_ARRAY(1, 2);
Nick Piggin54cb8822007-07-19 01:46:59 -0700577};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Jiangc791ace2017-02-24 14:57:08 -0800579/* page entry size for vm->huge_fault() */
580enum page_entry_size {
581 PE_SIZE_PTE = 0,
582 PE_SIZE_PMD,
583 PE_SIZE_PUD,
584};
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * These are the virtual MM functions - opening of an area, closing and
588 * unmapping it (needed to keep files on disk up-to-date etc), pointer
Leon Romanovsky27d036e2018-05-29 15:14:07 +0300589 * to the functions called when a no-page or a wp-page exception occurs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591struct vm_operations_struct {
592 void (*open)(struct vm_area_struct * area);
593 void (*close)(struct vm_area_struct * area);
Dan Williams31383c62017-11-29 16:10:28 -0800594 int (*split)(struct vm_area_struct * area, unsigned long addr);
Oleg Nesterov5477e702015-09-04 15:48:04 -0700595 int (*mremap)(struct vm_area_struct * area);
Souptick Joarder1c8f4222018-04-05 16:25:23 -0700596 vm_fault_t (*fault)(struct vm_fault *vmf);
597 vm_fault_t (*huge_fault)(struct vm_fault *vmf,
598 enum page_entry_size pe_size);
Kirill A. Shutemov0aa300a2020-12-19 15:19:23 +0300599 vm_fault_t (*map_pages)(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700600 pgoff_t start_pgoff, pgoff_t end_pgoff);
Dan Williams05ea8862018-04-05 16:24:25 -0700601 unsigned long (*pagesize)(struct vm_area_struct * area);
David Howells9637a5e2006-06-23 02:03:43 -0700602
603 /* notification that a previously read-only page is about to become
604 * writable, if an error is returned it will cause a SIGBUS */
Souptick Joarder1c8f4222018-04-05 16:25:23 -0700605 vm_fault_t (*page_mkwrite)(struct vm_fault *vmf);
Rik van Riel28b2ee22008-07-23 21:27:05 -0700606
Boaz Harroshdd906182015-04-15 16:15:11 -0700607 /* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */
Souptick Joarder1c8f4222018-04-05 16:25:23 -0700608 vm_fault_t (*pfn_mkwrite)(struct vm_fault *vmf);
Boaz Harroshdd906182015-04-15 16:15:11 -0700609
Rik van Riel28b2ee22008-07-23 21:27:05 -0700610 /* called by access_process_vm when get_user_pages() fails, typically
611 * for use by special VMAs that can switch between memory and hardware
612 */
613 int (*access)(struct vm_area_struct *vma, unsigned long addr,
614 void *buf, int len, int write);
Andy Lutomirski78d683e2014-05-19 15:58:32 -0700615
616 /* Called by the /proc/PID/maps code to ask the vma whether it
617 * has a special name. Returning non-NULL will also cause this
618 * vma to be dumped unconditionally. */
619 const char *(*name)(struct vm_area_struct *vma);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#ifdef CONFIG_NUMA
Lee Schermerhorna6020ed2008-04-28 02:13:14 -0700622 /*
623 * set_policy() op must add a reference to any non-NULL @new mempolicy
624 * to hold the policy upon return. Caller should pass NULL @new to
625 * remove a policy and fall back to surrounding context--i.e. do not
626 * install a MPOL_DEFAULT policy, nor the task or system default
627 * mempolicy.
628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
Lee Schermerhorna6020ed2008-04-28 02:13:14 -0700630
631 /*
632 * get_policy() op must add reference [mpol_get()] to any policy at
633 * (vma,addr) marked as MPOL_SHARED. The shared policy infrastructure
634 * in mm/mempolicy.c will do this automatically.
635 * get_policy() must NOT add a ref if the policy at (vma,addr) is not
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700636 * marked as MPOL_SHARED. vma policies are protected by the mmap_lock.
Lee Schermerhorna6020ed2008-04-28 02:13:14 -0700637 * If no [shared/vma] mempolicy exists at the addr, get_policy() op
638 * must return NULL--i.e., do not "fallback" to task or system default
639 * policy.
640 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
642 unsigned long addr);
643#endif
David Vrabel667a0a02014-12-18 14:48:15 +0000644 /*
645 * Called by vm_normal_page() for special PTEs to find the
646 * page for @addr. This is useful if the default behavior
647 * (using pte_page()) would not find the correct page.
648 */
649 struct page *(*find_special_page)(struct vm_area_struct *vma,
650 unsigned long addr);
Greg Kroah-Hartman8faaa072020-05-02 09:41:26 +0200651
Vinayak Menon35eacb52021-03-18 15:20:17 +0530652#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
653 bool (*allow_speculation)(void);
654#endif
655
Greg Kroah-Hartman8faaa072020-05-02 09:41:26 +0200656 ANDROID_KABI_RESERVE(1);
657 ANDROID_KABI_RESERVE(2);
658 ANDROID_KABI_RESERVE(3);
659 ANDROID_KABI_RESERVE(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660};
661
Laurent Dufour00766002018-04-17 16:33:13 +0200662static inline void INIT_VMA(struct vm_area_struct *vma)
663{
664 INIT_LIST_HEAD(&vma->anon_vma_chain);
Peter Zijlstra2ce6b112018-04-17 16:33:14 +0200665#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
666 seqcount_init(&vma->vm_sequence);
Laurent Dufour6d8fc362018-04-17 16:33:23 +0200667 atomic_set(&vma->vm_ref_count, 1);
Peter Zijlstra2ce6b112018-04-17 16:33:14 +0200668#endif
Laurent Dufour00766002018-04-17 16:33:13 +0200669}
670
Kirill A. Shutemov027232d2018-07-26 16:37:25 -0700671static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)
672{
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -0700673 static const struct vm_operations_struct dummy_vm_ops = {};
674
Andrew Mortona6704682018-08-21 21:53:06 -0700675 memset(vma, 0, sizeof(*vma));
Kirill A. Shutemov027232d2018-07-26 16:37:25 -0700676 vma->vm_mm = mm;
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -0700677 vma->vm_ops = &dummy_vm_ops;
Laurent Dufour00766002018-04-17 16:33:13 +0200678 INIT_VMA(vma);
Kirill A. Shutemov027232d2018-07-26 16:37:25 -0700679}
680
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -0700681static inline void vma_set_anonymous(struct vm_area_struct *vma)
682{
683 vma->vm_ops = NULL;
684}
685
Yang Shi43675e62019-07-18 15:57:24 -0700686static inline bool vma_is_anonymous(struct vm_area_struct *vma)
687{
688 return !vma->vm_ops;
689}
690
Anshuman Khandual222100e2020-04-01 21:07:52 -0700691static inline bool vma_is_temporary_stack(struct vm_area_struct *vma)
692{
693 int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);
694
695 if (!maybe_stack)
696 return false;
697
698 if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) ==
699 VM_STACK_INCOMPLETE_SETUP)
700 return true;
701
702 return false;
703}
704
Anshuman Khandual7969f222020-04-01 21:07:49 -0700705static inline bool vma_is_foreign(struct vm_area_struct *vma)
706{
707 if (!current->mm)
708 return true;
709
710 if (current->mm != vma->vm_mm)
711 return true;
712
713 return false;
714}
Anshuman Khandual3122e802020-04-06 20:03:47 -0700715
716static inline bool vma_is_accessible(struct vm_area_struct *vma)
717{
Anshuman Khandual6cb4d9a2020-04-10 14:33:09 -0700718 return vma->vm_flags & VM_ACCESS_FLAGS;
Anshuman Khandual3122e802020-04-06 20:03:47 -0700719}
720
Yang Shi43675e62019-07-18 15:57:24 -0700721#ifdef CONFIG_SHMEM
722/*
723 * The vma_is_shmem is not inline because it is used only by slow
724 * paths in userfault.
725 */
726bool vma_is_shmem(struct vm_area_struct *vma);
727#else
728static inline bool vma_is_shmem(struct vm_area_struct *vma) { return false; }
729#endif
730
731int vma_is_stack_for_current(struct vm_area_struct *vma);
732
Linus Torvalds8b11ec12018-08-01 13:43:38 -0700733/* flush_tlb_range() takes a vma, not a mm, and can care about flags */
734#define TLB_FLUSH_VMA(mm,flags) { .vm_mm = (mm), .vm_flags = (flags) }
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736struct mmu_gather;
737struct inode;
738
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800739#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741/*
742 * Methods to modify the page usage count.
743 *
744 * What counts for a page usage:
745 * - cache mapping (page->mapping)
746 * - private data (page->private)
747 * - page mapped in a task's page tables, each mapping
748 * is counted separately
749 *
750 * Also, many kernel routines increase the page count before a critical
751 * routine so they can be sure the page doesn't go away from under them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
753
754/*
Nick Pigginda6052f2006-09-25 23:31:35 -0700755 * Drop a ref, return true if the refcount fell to zero (the page has no users)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800757static inline int put_page_testzero(struct page *page)
758{
Minchan Kim470cce52021-07-08 11:12:29 -0700759 int ret;
760
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700761 VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
Minchan Kim470cce52021-07-08 11:12:29 -0700762 ret = page_ref_dec_and_test(page);
763 page_pinner_put_page(page);
764
765 return ret;
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800766}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768/*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800769 * Try to grab a ref unless the page has a refcount of zero, return false if
770 * that is the case.
Alexey Kardashevskiy8e0861fa2013-08-28 18:37:42 +1000771 * This can be called when MMU is off so it must not access
772 * any of the virtual mappings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800774static inline int get_page_unless_zero(struct page *page)
775{
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700776 return page_ref_add_unless(page, 1, 0);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800777}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Wu Fengguang53df8fd2010-01-27 11:06:39 +0800779extern int page_is_ram(unsigned long pfn);
Dan Williams124fe202015-08-10 23:07:05 -0400780
781enum {
782 REGION_INTERSECTS,
783 REGION_DISJOINT,
784 REGION_MIXED,
785};
786
Toshi Kani1c29f252016-01-26 21:57:28 +0100787int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
788 unsigned long desc);
Wu Fengguang53df8fd2010-01-27 11:06:39 +0800789
Christoph Lameter48667e72008-02-04 22:28:31 -0800790/* Support for virtually mapped pages */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800791struct page *vmalloc_to_page(const void *addr);
792unsigned long vmalloc_to_pfn(const void *addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800793
Paul Mundt0738c4b2008-03-12 16:51:31 +0900794/*
795 * Determine if an address is within the vmalloc range
796 *
797 * On nommu, vmalloc/vfree wrap through kmalloc/kfree directly, so there
798 * is no special casing required.
799 */
Aneesh Kumar K.V9bd3bb62019-07-11 20:52:08 -0700800
801#ifndef is_ioremap_addr
802#define is_ioremap_addr(x) is_vmalloc_addr(x)
803#endif
804
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700805#ifdef CONFIG_MMU
Ingo Molnar186525b2019-11-29 08:17:25 +0100806extern bool is_vmalloc_addr(const void *x);
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700807extern int is_vmalloc_or_module_addr(const void *x);
808#else
Ingo Molnar186525b2019-11-29 08:17:25 +0100809static inline bool is_vmalloc_addr(const void *x)
810{
811 return false;
812}
David Howells934831d2009-09-24 12:33:32 +0100813static inline int is_vmalloc_or_module_addr(const void *x)
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700814{
815 return 0;
816}
817#endif
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800818
Michal Hockoa7c3e902017-05-08 15:57:09 -0700819extern void *kvmalloc_node(size_t size, gfp_t flags, int node);
820static inline void *kvmalloc(size_t size, gfp_t flags)
821{
822 return kvmalloc_node(size, flags, NUMA_NO_NODE);
823}
824static inline void *kvzalloc_node(size_t size, gfp_t flags, int node)
825{
826 return kvmalloc_node(size, flags | __GFP_ZERO, node);
827}
828static inline void *kvzalloc(size_t size, gfp_t flags)
829{
830 return kvmalloc(size, flags | __GFP_ZERO);
831}
832
Michal Hocko752ade62017-05-08 15:57:27 -0700833static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags)
834{
Kees Cook3b3b1a29e2018-05-08 12:55:26 -0700835 size_t bytes;
836
837 if (unlikely(check_mul_overflow(n, size, &bytes)))
Michal Hocko752ade62017-05-08 15:57:27 -0700838 return NULL;
839
Kees Cook3b3b1a29e2018-05-08 12:55:26 -0700840 return kvmalloc(bytes, flags);
Michal Hocko752ade62017-05-08 15:57:27 -0700841}
842
Kees Cook1c542f32018-06-11 14:35:55 -0700843static inline void *kvcalloc(size_t n, size_t size, gfp_t flags)
844{
845 return kvmalloc_array(n, size, flags | __GFP_ZERO);
846}
847
Al Viro39f1f782014-05-06 14:02:53 -0400848extern void kvfree(const void *addr);
Waiman Longd4eaa282020-06-04 16:48:21 -0700849extern void kvfree_sensitive(const void *addr, size_t len);
Al Viro39f1f782014-05-06 14:02:53 -0400850
John Hubbardbac3cf42020-10-13 16:51:14 -0700851static inline int head_compound_mapcount(struct page *head)
John Hubbard6dc5ea12020-08-06 23:19:51 -0700852{
853 return atomic_read(compound_mapcount_ptr(head)) + 1;
854}
855
Konstantin Khlebnikov6988f312020-05-27 22:20:47 -0700856/*
857 * Mapcount of compound page as a whole, does not include mapped sub-pages.
858 *
859 * Must be called only for compound pages or any their tail sub-pages.
860 */
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800861static inline int compound_mapcount(struct page *page)
862{
Andrea Arcangeli5f527c22016-05-20 16:58:24 -0700863 VM_BUG_ON_PAGE(!PageCompound(page), page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800864 page = compound_head(page);
John Hubbardbac3cf42020-10-13 16:51:14 -0700865 return head_compound_mapcount(page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800866}
867
Joonsoo Kimccaafd72015-02-10 14:09:35 -0800868/*
Andrea Arcangeli70b50f92011-11-02 13:36:59 -0700869 * The atomic page->_mapcount, starts from -1: so that transitions
870 * both from it and to it can be tracked, using atomic_inc_and_test
871 * and atomic_add_negative(-1).
872 */
Mel Gorman22b751c2013-02-22 16:34:59 -0800873static inline void page_mapcount_reset(struct page *page)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -0700874{
875 atomic_set(&(page)->_mapcount, -1);
876}
877
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800878int __page_mapcount(struct page *page);
879
Konstantin Khlebnikov6988f312020-05-27 22:20:47 -0700880/*
881 * Mapcount of 0-order page; when compound sub-page, includes
882 * compound_mapcount().
883 *
884 * Result is undefined for pages which cannot be mapped into userspace.
885 * For example SLAB or special types of pages. See function page_has_type().
886 * They use this place in struct page differently.
887 */
Andrea Arcangeli70b50f92011-11-02 13:36:59 -0700888static inline int page_mapcount(struct page *page)
889{
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800890 if (unlikely(PageCompound(page)))
891 return __page_mapcount(page);
892 return atomic_read(&page->_mapcount) + 1;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -0700893}
894
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800895#ifdef CONFIG_TRANSPARENT_HUGEPAGE
896int total_mapcount(struct page *page);
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -0700897int page_trans_huge_mapcount(struct page *page, int *total_mapcount);
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800898#else
899static inline int total_mapcount(struct page *page)
900{
901 return page_mapcount(page);
902}
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -0700903static inline int page_trans_huge_mapcount(struct page *page,
904 int *total_mapcount)
905{
906 int mapcount = page_mapcount(page);
907 if (total_mapcount)
908 *total_mapcount = mapcount;
909 return mapcount;
910}
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800911#endif
912
Christoph Lameterb49af682007-05-06 14:49:41 -0700913static inline struct page *virt_to_head_page(const void *x)
914{
915 struct page *page = virt_to_page(x);
Joonsoo Kimccaafd72015-02-10 14:09:35 -0800916
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800917 return compound_head(page);
Christoph Lameterb49af682007-05-06 14:49:41 -0700918}
919
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800920void __put_page(struct page *page);
921
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700922void put_pages_list(struct list_head *pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800924void split_page(struct page *page, unsigned int order);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800927 * Compound pages have a destructor function. Provide a
928 * prototype for that function and accessor functions.
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800929 * These are _only_ valid on the head of a compound page.
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800930 */
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800931typedef void compound_page_dtor(struct page *);
932
933/* Keep the enum in sync with compound_page_dtors array in mm/page_alloc.c */
934enum compound_dtor_id {
935 NULL_COMPOUND_DTOR,
936 COMPOUND_PAGE_DTOR,
937#ifdef CONFIG_HUGETLB_PAGE
938 HUGETLB_PAGE_DTOR,
939#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800940#ifdef CONFIG_TRANSPARENT_HUGEPAGE
941 TRANSHUGE_PAGE_DTOR,
942#endif
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800943 NR_COMPOUND_DTORS,
944};
Anshuman Khandualae70edd2020-06-03 15:59:17 -0700945extern compound_page_dtor * const compound_page_dtors[NR_COMPOUND_DTORS];
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800946
947static inline void set_compound_page_dtor(struct page *page,
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800948 enum compound_dtor_id compound_dtor)
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800949{
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800950 VM_BUG_ON_PAGE(compound_dtor >= NR_COMPOUND_DTORS, page);
951 page[1].compound_dtor = compound_dtor;
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800952}
953
Matthew Wilcox (Oracle)ff45fc32020-06-03 16:01:09 -0700954static inline void destroy_compound_page(struct page *page)
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800955{
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800956 VM_BUG_ON_PAGE(page[1].compound_dtor >= NR_COMPOUND_DTORS, page);
Matthew Wilcox (Oracle)ff45fc32020-06-03 16:01:09 -0700957 compound_page_dtors[page[1].compound_dtor](page);
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800958}
959
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -0800960static inline unsigned int compound_order(struct page *page)
Christoph Lameterd85f3382007-05-06 14:49:39 -0700961{
Christoph Lameter6d777952007-05-06 14:49:40 -0700962 if (!PageHead(page))
Christoph Lameterd85f3382007-05-06 14:49:39 -0700963 return 0;
Kirill A. Shutemove4b294c2015-02-11 15:24:46 -0800964 return page[1].compound_order;
Christoph Lameterd85f3382007-05-06 14:49:39 -0700965}
966
John Hubbard47e29d32020-04-01 21:05:33 -0700967static inline bool hpage_pincount_available(struct page *page)
968{
969 /*
970 * Can the page->hpage_pinned_refcount field be used? That field is in
971 * the 3rd page of the compound page, so the smallest (2-page) compound
972 * pages cannot support it.
973 */
974 page = compound_head(page);
975 return PageCompound(page) && compound_order(page) > 1;
976}
977
John Hubbardbac3cf42020-10-13 16:51:14 -0700978static inline int head_compound_pincount(struct page *head)
John Hubbard6dc5ea12020-08-06 23:19:51 -0700979{
980 return atomic_read(compound_pincount_ptr(head));
981}
982
John Hubbard47e29d32020-04-01 21:05:33 -0700983static inline int compound_pincount(struct page *page)
984{
985 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
986 page = compound_head(page);
John Hubbardbac3cf42020-10-13 16:51:14 -0700987 return head_compound_pincount(page);
John Hubbard47e29d32020-04-01 21:05:33 -0700988}
989
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800990static inline void set_compound_order(struct page *page, unsigned int order)
Christoph Lameterd85f3382007-05-06 14:49:39 -0700991{
Kirill A. Shutemove4b294c2015-02-11 15:24:46 -0800992 page[1].compound_order = order;
Matthew Wilcox (Oracle)1378a5e2020-08-14 17:30:23 -0700993 page[1].compound_nr = 1U << order;
Christoph Lameterd85f3382007-05-06 14:49:39 -0700994}
995
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700996/* Returns the number of pages in this potentially compound page. */
997static inline unsigned long compound_nr(struct page *page)
998{
Matthew Wilcox (Oracle)1378a5e2020-08-14 17:30:23 -0700999 if (!PageHead(page))
1000 return 1;
1001 return page[1].compound_nr;
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001002}
1003
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07001004/* Returns the number of bytes in this potentially compound page. */
1005static inline unsigned long page_size(struct page *page)
1006{
1007 return PAGE_SIZE << compound_order(page);
1008}
1009
Matthew Wilcox (Oracle)94ad9332019-09-23 15:34:28 -07001010/* Returns the number of bits needed for the number of bytes in a page */
1011static inline unsigned int page_shift(struct page *page)
1012{
1013 return PAGE_SHIFT + compound_order(page);
1014}
1015
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001016void free_compound_page(struct page *page);
1017
Michal Simek3dece372011-01-21 08:49:56 +01001018#ifdef CONFIG_MMU
Andy Whitcroft33f2ef82006-12-06 20:33:32 -08001019/*
Andrea Arcangeli14fd4032011-01-13 15:46:37 -08001020 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1021 * servicing faults for write access. In the normal case, do always want
1022 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1023 * that do not have writing enabled, when used by access_process_vm.
1024 */
Laurent Dufour32507b62018-04-17 16:33:18 +02001025static inline pte_t maybe_mkwrite(pte_t pte, unsigned long vma_flags)
Andrea Arcangeli14fd4032011-01-13 15:46:37 -08001026{
Laurent Dufour32507b62018-04-17 16:33:18 +02001027 if (likely(vma_flags & VM_WRITE))
Andrea Arcangeli14fd4032011-01-13 15:46:37 -08001028 pte = pte_mkwrite(pte);
1029 return pte;
1030}
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07001031
Kirill A. Shutemov0aa300a2020-12-19 15:19:23 +03001032vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page);
Will Deacondb6cf102021-01-14 15:24:19 +00001033void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr);
Kirill A. Shutemov0aa300a2020-12-19 15:19:23 +03001034
Souptick Joarder2b740302018-08-23 17:01:36 -07001035vm_fault_t finish_fault(struct vm_fault *vmf);
1036vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf);
Michal Simek3dece372011-01-21 08:49:56 +01001037#endif
Andrea Arcangeli14fd4032011-01-13 15:46:37 -08001038
1039/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 * Multiple processes may "see" the same page. E.g. for untouched
1041 * mappings of /dev/null, all processes see the same page full of
1042 * zeroes, and text pages of executables and shared libraries have
1043 * only one copy in memory, at most, normally.
1044 *
1045 * For the non-reserved pages, page_count(page) denotes a reference count.
Paolo 'Blaisorblade' Giarrusso7e871b62005-09-21 09:55:38 -07001046 * page_count() == 0 means the page is free. page->lru is then used for
1047 * freelist management in the buddy allocator.
Nick Pigginda6052f2006-09-25 23:31:35 -07001048 * page_count() > 0 means the page has been allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001050 * Pages are allocated by the slab allocator in order to provide memory
1051 * to kmalloc and kmem_cache_alloc. In this case, the management of the
1052 * page, and the fields in 'struct page' are the responsibility of mm/slab.c
1053 * unless a particular usage is carefully commented. (the responsibility of
1054 * freeing the kmalloc memory is the caller's, of course).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001056 * A page may be used by anyone else who does a __get_free_page().
1057 * In this case, page_count still tracks the references, and should only
1058 * be used through the normal accessor functions. The top bits of page->flags
1059 * and page->virtual store page management information, but all other fields
1060 * are unused and could be used privately, carefully. The management of this
1061 * page is the responsibility of the one who allocated it, and those who have
1062 * subsequently been given references to it.
1063 *
1064 * The other pages (we may call them "pagecache pages") are completely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * managed by the Linux memory manager: I/O, buffers, swapping etc.
1066 * The following discussion applies only to them.
1067 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001068 * A pagecache page contains an opaque `private' member, which belongs to the
1069 * page's address_space. Usually, this is the address of a circular list of
1070 * the page's disk buffers. PG_private must be set to tell the VM to call
1071 * into the filesystem to release these pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001073 * A page may belong to an inode's memory mapping. In this case, page->mapping
1074 * is the pointer to the inode, and page->index is the file offset of the page,
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03001075 * in units of PAGE_SIZE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001077 * If pagecache pages are not associated with an inode, they are said to be
1078 * anonymous pages. These may become associated with the swapcache, and in that
1079 * case PG_swapcache is set, and page->private is an offset into the swapcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001081 * In either case (swapcache or inode backed), the pagecache itself holds one
1082 * reference to the page. Setting PG_private should also increment the
1083 * refcount. The each user mapping also has a reference to the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001085 * The pagecache pages are stored in a per-mapping radix tree, which is
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001086 * rooted at mapping->i_pages, and indexed by offset.
Nick Pigginda6052f2006-09-25 23:31:35 -07001087 * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space
1088 * lists, we instead now tag pages as dirty/writeback in the radix tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 *
Nick Pigginda6052f2006-09-25 23:31:35 -07001090 * All pagecache pages may be subject to I/O:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * - inode pages may need to be read from disk,
1092 * - inode pages which have been modified and are MAP_SHARED may need
Nick Pigginda6052f2006-09-25 23:31:35 -07001093 * to be written back to the inode on disk,
1094 * - anonymous pages (including MAP_PRIVATE file mappings) which have been
1095 * modified may need to be swapped out to swap space and (later) to be read
1096 * back into memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
1098
1099/*
1100 * The zone field is never updated after free_area_init_core()
1101 * sets it, so none of the operations on it need to be atomic.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 */
Dave Hansen348f8b62005-06-23 00:07:40 -07001103
Peter Zijlstra90572892013-10-07 11:29:20 +01001104/* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_CPUPID] | ... | FLAGS | */
Andi Kleen07808b72005-11-05 17:25:53 +01001105#define SECTIONS_PGOFF ((sizeof(unsigned long)*8) - SECTIONS_WIDTH)
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001106#define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH)
1107#define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH)
Peter Zijlstra90572892013-10-07 11:29:20 +01001108#define LAST_CPUPID_PGOFF (ZONES_PGOFF - LAST_CPUPID_WIDTH)
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001109#define KASAN_TAG_PGOFF (LAST_CPUPID_PGOFF - KASAN_TAG_WIDTH)
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001110
1111/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001112 * Define the bit shifts to access each section. For non-existent
Dave Hansen348f8b62005-06-23 00:07:40 -07001113 * sections we define the shift as 0; that plus a 0 mask ensures
1114 * the compiler will optimise away reference to them.
1115 */
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001116#define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0))
1117#define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0))
1118#define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0))
Peter Zijlstra90572892013-10-07 11:29:20 +01001119#define LAST_CPUPID_PGSHIFT (LAST_CPUPID_PGOFF * (LAST_CPUPID_WIDTH != 0))
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001120#define KASAN_TAG_PGSHIFT (KASAN_TAG_PGOFF * (KASAN_TAG_WIDTH != 0))
Dave Hansen348f8b62005-06-23 00:07:40 -07001121
Will Deaconbce54bb2010-10-26 14:21:37 -07001122/* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */
1123#ifdef NODE_NOT_IN_PAGE_FLAGS
Christoph Lameter89689ae2006-12-06 20:31:45 -08001124#define ZONEID_SHIFT (SECTIONS_SHIFT + ZONES_SHIFT)
Andy Whitcroftbd8029b2007-02-10 01:43:14 -08001125#define ZONEID_PGOFF ((SECTIONS_PGOFF < ZONES_PGOFF)? \
1126 SECTIONS_PGOFF : ZONES_PGOFF)
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001127#else
Christoph Lameter89689ae2006-12-06 20:31:45 -08001128#define ZONEID_SHIFT (NODES_SHIFT + ZONES_SHIFT)
Andy Whitcroftbd8029b2007-02-10 01:43:14 -08001129#define ZONEID_PGOFF ((NODES_PGOFF < ZONES_PGOFF)? \
1130 NODES_PGOFF : ZONES_PGOFF)
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001131#endif
Christoph Lameter89689ae2006-12-06 20:31:45 -08001132
Andy Whitcroftbd8029b2007-02-10 01:43:14 -08001133#define ZONEID_PGSHIFT (ZONEID_PGOFF * (ZONEID_SHIFT != 0))
Dave Hansen348f8b62005-06-23 00:07:40 -07001134
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001135#define ZONES_MASK ((1UL << ZONES_WIDTH) - 1)
1136#define NODES_MASK ((1UL << NODES_WIDTH) - 1)
1137#define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1)
Srikar Dronamraju834a9642014-04-07 15:37:57 -07001138#define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1)
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001139#define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1)
Christoph Lameter89689ae2006-12-06 20:31:45 -08001140#define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1)
Dave Hansen348f8b62005-06-23 00:07:40 -07001141
Ian Campbell33dd4e02011-07-25 17:11:51 -07001142static inline enum zone_type page_zonenum(const struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Qian Caic403f6a2020-08-14 17:31:53 -07001144 ASSERT_EXCLUSIVE_BITS(page->flags, ZONES_MASK << ZONES_PGSHIFT);
Dave Hansen348f8b62005-06-23 00:07:40 -07001145 return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Dan Williams260ae3f2016-01-15 16:56:17 -08001148#ifdef CONFIG_ZONE_DEVICE
1149static inline bool is_zone_device_page(const struct page *page)
1150{
1151 return page_zonenum(page) == ZONE_DEVICE;
1152}
Alexander Duyck966cf442018-10-26 15:07:52 -07001153extern void memmap_init_zone_device(struct zone *, unsigned long,
1154 unsigned long, struct dev_pagemap *);
Dan Williams260ae3f2016-01-15 16:56:17 -08001155#else
1156static inline bool is_zone_device_page(const struct page *page)
1157{
1158 return false;
1159}
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -07001160#endif
Jérôme Glisse5042db42017-09-08 16:11:43 -07001161
Dan Williamse76384882018-05-16 11:46:08 -07001162#ifdef CONFIG_DEV_PAGEMAP_OPS
John Hubbard07d80262020-01-30 22:12:28 -08001163void free_devmap_managed_page(struct page *page);
Dan Williamse76384882018-05-16 11:46:08 -07001164DECLARE_STATIC_KEY_FALSE(devmap_managed_key);
John Hubbard07d80262020-01-30 22:12:28 -08001165
1166static inline bool page_is_devmap_managed(struct page *page)
Dan Williamse76384882018-05-16 11:46:08 -07001167{
1168 if (!static_branch_unlikely(&devmap_managed_key))
1169 return false;
1170 if (!is_zone_device_page(page))
1171 return false;
1172 switch (page->pgmap->type) {
1173 case MEMORY_DEVICE_PRIVATE:
Dan Williamse76384882018-05-16 11:46:08 -07001174 case MEMORY_DEVICE_FS_DAX:
Dan Williamse76384882018-05-16 11:46:08 -07001175 return true;
1176 default:
1177 break;
1178 }
1179 return false;
1180}
1181
John Hubbard07d80262020-01-30 22:12:28 -08001182void put_devmap_managed_page(struct page *page);
1183
Dan Williamse76384882018-05-16 11:46:08 -07001184#else /* CONFIG_DEV_PAGEMAP_OPS */
John Hubbard07d80262020-01-30 22:12:28 -08001185static inline bool page_is_devmap_managed(struct page *page)
Dan Williamse76384882018-05-16 11:46:08 -07001186{
1187 return false;
1188}
John Hubbard07d80262020-01-30 22:12:28 -08001189
1190static inline void put_devmap_managed_page(struct page *page)
1191{
1192}
Robin Murphy7588adf2019-07-16 16:30:44 -07001193#endif /* CONFIG_DEV_PAGEMAP_OPS */
Dan Williamse76384882018-05-16 11:46:08 -07001194
Jérôme Glisse6b368cd2017-09-08 16:12:32 -07001195static inline bool is_device_private_page(const struct page *page)
1196{
Robin Murphy7588adf2019-07-16 16:30:44 -07001197 return IS_ENABLED(CONFIG_DEV_PAGEMAP_OPS) &&
1198 IS_ENABLED(CONFIG_DEVICE_PRIVATE) &&
1199 is_zone_device_page(page) &&
1200 page->pgmap->type == MEMORY_DEVICE_PRIVATE;
Jérôme Glisse6b368cd2017-09-08 16:12:32 -07001201}
Logan Gunthorpe52916982018-10-04 15:27:35 -06001202
1203static inline bool is_pci_p2pdma_page(const struct page *page)
1204{
Robin Murphy7588adf2019-07-16 16:30:44 -07001205 return IS_ENABLED(CONFIG_DEV_PAGEMAP_OPS) &&
1206 IS_ENABLED(CONFIG_PCI_P2PDMA) &&
1207 is_zone_device_page(page) &&
1208 page->pgmap->type == MEMORY_DEVICE_PCI_P2PDMA;
Logan Gunthorpe52916982018-10-04 15:27:35 -06001209}
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -07001210
Linus Torvaldsf958d7b2019-04-11 10:06:20 -07001211/* 127: arbitrary random number, small enough to assemble well */
1212#define page_ref_zero_or_close_to_overflow(page) \
1213 ((unsigned int) page_ref_count(page) + 127u <= 127u)
1214
Dan Williams3565fce2016-01-15 16:56:55 -08001215static inline void get_page(struct page *page)
1216{
1217 page = compound_head(page);
1218 /*
1219 * Getting a normal page or the head of a compound page
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001220 * requires to already have an elevated page->_refcount.
Dan Williams3565fce2016-01-15 16:56:55 -08001221 */
Linus Torvaldsf958d7b2019-04-11 10:06:20 -07001222 VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07001223 page_ref_inc(page);
Dan Williams3565fce2016-01-15 16:56:55 -08001224}
1225
John Hubbard3faa52c2020-04-01 21:05:29 -07001226bool __must_check try_grab_page(struct page *page, unsigned int flags);
1227
Linus Torvalds88b1a172019-04-11 10:14:59 -07001228static inline __must_check bool try_get_page(struct page *page)
1229{
1230 page = compound_head(page);
1231 if (WARN_ON_ONCE(page_ref_count(page) <= 0))
1232 return false;
1233 page_ref_inc(page);
1234 return true;
1235}
1236
Dan Williams3565fce2016-01-15 16:56:55 -08001237static inline void put_page(struct page *page)
1238{
1239 page = compound_head(page);
1240
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -07001241 /*
Dan Williamse76384882018-05-16 11:46:08 -07001242 * For devmap managed pages we need to catch refcount transition from
1243 * 2 to 1, when refcount reach one it means the page is free and we
1244 * need to inform the device driver through callback. See
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -07001245 * include/linux/memremap.h and HMM for details.
1246 */
John Hubbard07d80262020-01-30 22:12:28 -08001247 if (page_is_devmap_managed(page)) {
1248 put_devmap_managed_page(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -07001249 return;
John Hubbard07d80262020-01-30 22:12:28 -08001250 }
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -07001251
Dan Williams3565fce2016-01-15 16:56:55 -08001252 if (put_page_testzero(page))
1253 __put_page(page);
Dan Williams3565fce2016-01-15 16:56:55 -08001254}
1255
John Hubbard3faa52c2020-04-01 21:05:29 -07001256/*
1257 * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload
1258 * the page's refcount so that two separate items are tracked: the original page
1259 * reference count, and also a new count of how many pin_user_pages() calls were
1260 * made against the page. ("gup-pinned" is another term for the latter).
John Hubbardfc1d8e72019-05-13 17:19:08 -07001261 *
John Hubbard3faa52c2020-04-01 21:05:29 -07001262 * With this scheme, pin_user_pages() becomes special: such pages are marked as
1263 * distinct from normal pages. As such, the unpin_user_page() call (and its
1264 * variants) must be used in order to release gup-pinned pages.
John Hubbardfc1d8e72019-05-13 17:19:08 -07001265 *
John Hubbard3faa52c2020-04-01 21:05:29 -07001266 * Choice of value:
1267 *
1268 * By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference
1269 * counts with respect to pin_user_pages() and unpin_user_page() becomes
1270 * simpler, due to the fact that adding an even power of two to the page
1271 * refcount has the effect of using only the upper N bits, for the code that
1272 * counts up using the bias value. This means that the lower bits are left for
1273 * the exclusive use of the original code that increments and decrements by one
1274 * (or at least, by much smaller values than the bias value).
1275 *
1276 * Of course, once the lower bits overflow into the upper bits (and this is
1277 * OK, because subtraction recovers the original values), then visual inspection
1278 * no longer suffices to directly view the separate counts. However, for normal
1279 * applications that don't have huge page reference counts, this won't be an
1280 * issue.
1281 *
1282 * Locking: the lockless algorithm described in page_cache_get_speculative()
1283 * and page_cache_gup_pin_speculative() provides safe operation for
1284 * get_user_pages and page_mkclean and other calls that race to set up page
1285 * table entries.
John Hubbardfc1d8e72019-05-13 17:19:08 -07001286 */
John Hubbard3faa52c2020-04-01 21:05:29 -07001287#define GUP_PIN_COUNTING_BIAS (1U << 10)
John Hubbardfc1d8e72019-05-13 17:19:08 -07001288
Minchan Kim6e12c5b2021-03-18 09:56:10 -07001289void put_user_page(struct page *page);
John Hubbard3faa52c2020-04-01 21:05:29 -07001290void unpin_user_page(struct page *page);
John Hubbardf1f6a7d2020-01-30 22:13:35 -08001291void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
1292 bool make_dirty);
John Hubbardf1f6a7d2020-01-30 22:13:35 -08001293void unpin_user_pages(struct page **pages, unsigned long npages);
John Hubbardfc1d8e72019-05-13 17:19:08 -07001294
John Hubbard3faa52c2020-04-01 21:05:29 -07001295/**
1296 * page_maybe_dma_pinned() - report if a page is pinned for DMA.
1297 *
1298 * This function checks if a page has been pinned via a call to
1299 * pin_user_pages*().
1300 *
1301 * For non-huge pages, the return value is partially fuzzy: false is not fuzzy,
1302 * because it means "definitely not pinned for DMA", but true means "probably
1303 * pinned for DMA, but possibly a false positive due to having at least
1304 * GUP_PIN_COUNTING_BIAS worth of normal page references".
1305 *
1306 * False positives are OK, because: a) it's unlikely for a page to get that many
1307 * refcounts, and b) all the callers of this routine are expected to be able to
1308 * deal gracefully with a false positive.
1309 *
John Hubbard47e29d32020-04-01 21:05:33 -07001310 * For huge pages, the result will be exactly correct. That's because we have
1311 * more tracking data available: the 3rd struct page in the compound page is
1312 * used to track the pincount (instead using of the GUP_PIN_COUNTING_BIAS
1313 * scheme).
1314 *
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02001315 * For more information, please see Documentation/core-api/pin_user_pages.rst.
John Hubbard3faa52c2020-04-01 21:05:29 -07001316 *
1317 * @page: pointer to page to be queried.
1318 * @Return: True, if it is likely that the page has been "dma-pinned".
1319 * False, if the page is definitely not dma-pinned.
1320 */
1321static inline bool page_maybe_dma_pinned(struct page *page)
1322{
John Hubbard47e29d32020-04-01 21:05:33 -07001323 if (hpage_pincount_available(page))
1324 return compound_pincount(page) > 0;
1325
John Hubbard3faa52c2020-04-01 21:05:29 -07001326 /*
1327 * page_ref_count() is signed. If that refcount overflows, then
1328 * page_ref_count() returns a negative value, and callers will avoid
1329 * further incrementing the refcount.
1330 *
1331 * Here, for that overflow case, use the signed bit to count a little
1332 * bit higher via unsigned math, and thus still get an accurate result.
1333 */
1334 return ((unsigned int)page_ref_count(compound_head(page))) >=
1335 GUP_PIN_COUNTING_BIAS;
1336}
1337
Cody P Schafer9127ab42013-02-22 16:35:21 -08001338#if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
1339#define SECTION_IN_PAGE_FLAGS
1340#endif
1341
Christoph Lameter89689ae2006-12-06 20:31:45 -08001342/*
Vlastimil Babka7a8010c2013-09-11 14:22:35 -07001343 * The identification function is mainly used by the buddy allocator for
1344 * determining if two pages could be buddies. We are not really identifying
1345 * the zone since we could be using the section number id if we do not have
1346 * node id available in page flags.
1347 * We only guarantee that it will return the same value for two combinable
1348 * pages in a zone.
Christoph Lameter89689ae2006-12-06 20:31:45 -08001349 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -07001350static inline int page_zone_id(struct page *page)
1351{
Christoph Lameter89689ae2006-12-06 20:31:45 -08001352 return (page->flags >> ZONEID_PGSHIFT) & ZONEID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Christoph Lameter89689ae2006-12-06 20:31:45 -08001355#ifdef NODE_NOT_IN_PAGE_FLAGS
Ian Campbell33dd4e02011-07-25 17:11:51 -07001356extern int page_to_nid(const struct page *page);
Christoph Lameter89689ae2006-12-06 20:31:45 -08001357#else
Ian Campbell33dd4e02011-07-25 17:11:51 -07001358static inline int page_to_nid(const struct page *page)
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001359{
Pavel Tatashinf165b372018-04-05 16:22:47 -07001360 struct page *p = (struct page *)page;
1361
1362 return (PF_POISONED_CHECK(p)->flags >> NODES_PGSHIFT) & NODES_MASK;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001363}
Christoph Lameter89689ae2006-12-06 20:31:45 -08001364#endif
1365
Mel Gorman57e0a032012-11-12 09:06:20 +00001366#ifdef CONFIG_NUMA_BALANCING
Peter Zijlstra90572892013-10-07 11:29:20 +01001367static inline int cpu_pid_to_cpupid(int cpu, int pid)
Mel Gorman57e0a032012-11-12 09:06:20 +00001368{
Peter Zijlstra90572892013-10-07 11:29:20 +01001369 return ((cpu & LAST__CPU_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK);
Mel Gorman57e0a032012-11-12 09:06:20 +00001370}
1371
Peter Zijlstra90572892013-10-07 11:29:20 +01001372static inline int cpupid_to_pid(int cpupid)
Mel Gorman57e0a032012-11-12 09:06:20 +00001373{
Peter Zijlstra90572892013-10-07 11:29:20 +01001374 return cpupid & LAST__PID_MASK;
Mel Gorman57e0a032012-11-12 09:06:20 +00001375}
Mel Gormanb7958542013-10-07 11:29:07 +01001376
Peter Zijlstra90572892013-10-07 11:29:20 +01001377static inline int cpupid_to_cpu(int cpupid)
Mel Gorman57e0a032012-11-12 09:06:20 +00001378{
Peter Zijlstra90572892013-10-07 11:29:20 +01001379 return (cpupid >> LAST__PID_SHIFT) & LAST__CPU_MASK;
Mel Gormanb7958542013-10-07 11:29:07 +01001380}
1381
Peter Zijlstra90572892013-10-07 11:29:20 +01001382static inline int cpupid_to_nid(int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001383{
Peter Zijlstra90572892013-10-07 11:29:20 +01001384 return cpu_to_node(cpupid_to_cpu(cpupid));
Mel Gormanb7958542013-10-07 11:29:07 +01001385}
1386
Peter Zijlstra90572892013-10-07 11:29:20 +01001387static inline bool cpupid_pid_unset(int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001388{
Peter Zijlstra90572892013-10-07 11:29:20 +01001389 return cpupid_to_pid(cpupid) == (-1 & LAST__PID_MASK);
Mel Gormanb7958542013-10-07 11:29:07 +01001390}
1391
Peter Zijlstra90572892013-10-07 11:29:20 +01001392static inline bool cpupid_cpu_unset(int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001393{
Peter Zijlstra90572892013-10-07 11:29:20 +01001394 return cpupid_to_cpu(cpupid) == (-1 & LAST__CPU_MASK);
Mel Gormanb7958542013-10-07 11:29:07 +01001395}
1396
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001397static inline bool __cpupid_match_pid(pid_t task_pid, int cpupid)
1398{
1399 return (task_pid & LAST__PID_MASK) == cpupid_to_pid(cpupid);
1400}
1401
1402#define cpupid_match_pid(task, cpupid) __cpupid_match_pid(task->pid, cpupid)
Peter Zijlstra90572892013-10-07 11:29:20 +01001403#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
1404static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001405{
Liu Ping Fan1ae71d02014-03-03 15:38:39 -08001406 return xchg(&page->_last_cpupid, cpupid & LAST_CPUPID_MASK);
Mel Gormanb7958542013-10-07 11:29:07 +01001407}
Peter Zijlstra90572892013-10-07 11:29:20 +01001408
1409static inline int page_cpupid_last(struct page *page)
Mel Gormanb7958542013-10-07 11:29:07 +01001410{
Peter Zijlstra90572892013-10-07 11:29:20 +01001411 return page->_last_cpupid;
1412}
1413static inline void page_cpupid_reset_last(struct page *page)
1414{
Liu Ping Fan1ae71d02014-03-03 15:38:39 -08001415 page->_last_cpupid = -1 & LAST_CPUPID_MASK;
Mel Gorman57e0a032012-11-12 09:06:20 +00001416}
1417#else
Peter Zijlstra90572892013-10-07 11:29:20 +01001418static inline int page_cpupid_last(struct page *page)
Peter Zijlstra75980e92013-02-22 16:34:32 -08001419{
Peter Zijlstra90572892013-10-07 11:29:20 +01001420 return (page->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
Peter Zijlstra75980e92013-02-22 16:34:32 -08001421}
1422
Peter Zijlstra90572892013-10-07 11:29:20 +01001423extern int page_cpupid_xchg_last(struct page *page, int cpupid);
Peter Zijlstra75980e92013-02-22 16:34:32 -08001424
Peter Zijlstra90572892013-10-07 11:29:20 +01001425static inline void page_cpupid_reset_last(struct page *page)
Peter Zijlstra75980e92013-02-22 16:34:32 -08001426{
Mel Gorman09940a42016-05-19 17:13:53 -07001427 page->flags |= LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT;
Peter Zijlstra75980e92013-02-22 16:34:32 -08001428}
Peter Zijlstra90572892013-10-07 11:29:20 +01001429#endif /* LAST_CPUPID_NOT_IN_PAGE_FLAGS */
1430#else /* !CONFIG_NUMA_BALANCING */
1431static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
Mel Gorman57e0a032012-11-12 09:06:20 +00001432{
Peter Zijlstra90572892013-10-07 11:29:20 +01001433 return page_to_nid(page); /* XXX */
Mel Gorman57e0a032012-11-12 09:06:20 +00001434}
1435
Peter Zijlstra90572892013-10-07 11:29:20 +01001436static inline int page_cpupid_last(struct page *page)
Mel Gorman57e0a032012-11-12 09:06:20 +00001437{
Peter Zijlstra90572892013-10-07 11:29:20 +01001438 return page_to_nid(page); /* XXX */
Mel Gorman57e0a032012-11-12 09:06:20 +00001439}
1440
Peter Zijlstra90572892013-10-07 11:29:20 +01001441static inline int cpupid_to_nid(int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001442{
1443 return -1;
1444}
1445
Peter Zijlstra90572892013-10-07 11:29:20 +01001446static inline int cpupid_to_pid(int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001447{
1448 return -1;
1449}
1450
Peter Zijlstra90572892013-10-07 11:29:20 +01001451static inline int cpupid_to_cpu(int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001452{
1453 return -1;
1454}
1455
Peter Zijlstra90572892013-10-07 11:29:20 +01001456static inline int cpu_pid_to_cpupid(int nid, int pid)
1457{
1458 return -1;
1459}
1460
1461static inline bool cpupid_pid_unset(int cpupid)
Mel Gormanb7958542013-10-07 11:29:07 +01001462{
Jason Yan2b787442020-06-04 16:49:49 -07001463 return true;
Mel Gormanb7958542013-10-07 11:29:07 +01001464}
1465
Peter Zijlstra90572892013-10-07 11:29:20 +01001466static inline void page_cpupid_reset_last(struct page *page)
Mel Gorman57e0a032012-11-12 09:06:20 +00001467{
1468}
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001469
1470static inline bool cpupid_match_pid(struct task_struct *task, int cpupid)
1471{
1472 return false;
1473}
Peter Zijlstra90572892013-10-07 11:29:20 +01001474#endif /* CONFIG_NUMA_BALANCING */
Mel Gorman57e0a032012-11-12 09:06:20 +00001475
Andrey Konovalov11167162020-12-22 12:02:10 -08001476#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
Andrey Konovalov4e35a812020-12-22 12:03:10 -08001477
Andrey Konovalovea1ffe72021-03-18 17:01:20 +11001478/*
1479 * KASAN per-page tags are stored xor'ed with 0xff. This allows to avoid
1480 * setting tags for all pages to native kernel tag value 0xff, as the default
1481 * value 0x00 maps to 0xff.
1482 */
1483
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001484static inline u8 page_kasan_tag(const struct page *page)
1485{
Andrey Konovalovea1ffe72021-03-18 17:01:20 +11001486 u8 tag = 0xff;
1487
1488 if (kasan_enabled()) {
1489 tag = (page->flags >> KASAN_TAG_PGSHIFT) & KASAN_TAG_MASK;
1490 tag ^= 0xff;
1491 }
1492
1493 return tag;
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001494}
1495
1496static inline void page_kasan_tag_set(struct page *page, u8 tag)
1497{
Andrey Konovalov4e35a812020-12-22 12:03:10 -08001498 if (kasan_enabled()) {
Andrey Konovalovea1ffe72021-03-18 17:01:20 +11001499 tag ^= 0xff;
Andrey Konovalov4e35a812020-12-22 12:03:10 -08001500 page->flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
1501 page->flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
1502 }
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001503}
1504
1505static inline void page_kasan_tag_reset(struct page *page)
1506{
Andrey Konovalov4e35a812020-12-22 12:03:10 -08001507 if (kasan_enabled())
1508 page_kasan_tag_set(page, 0xff);
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001509}
Andrey Konovalov4e35a812020-12-22 12:03:10 -08001510
1511#else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
1512
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001513static inline u8 page_kasan_tag(const struct page *page)
1514{
1515 return 0xff;
1516}
1517
1518static inline void page_kasan_tag_set(struct page *page, u8 tag) { }
1519static inline void page_kasan_tag_reset(struct page *page) { }
Andrey Konovalov4e35a812020-12-22 12:03:10 -08001520
1521#endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001522
Ian Campbell33dd4e02011-07-25 17:11:51 -07001523static inline struct zone *page_zone(const struct page *page)
Christoph Lameter89689ae2006-12-06 20:31:45 -08001524{
1525 return &NODE_DATA(page_to_nid(page))->node_zones[page_zonenum(page)];
1526}
1527
Mel Gorman75ef7182016-07-28 15:45:24 -07001528static inline pg_data_t *page_pgdat(const struct page *page)
1529{
1530 return NODE_DATA(page_to_nid(page));
1531}
1532
Cody P Schafer9127ab42013-02-22 16:35:21 -08001533#ifdef SECTION_IN_PAGE_FLAGS
Daniel Kiperbf4e8902011-05-24 17:12:32 -07001534static inline void set_page_section(struct page *page, unsigned long section)
1535{
1536 page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT);
1537 page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
1538}
1539
Ian Campbellaa462ab2011-08-17 17:40:33 +01001540static inline unsigned long page_to_section(const struct page *page)
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001541{
1542 return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
1543}
Christoph Lameter308c05e2008-04-28 02:12:43 -07001544#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001545
Christoph Lameter2f1b6242006-09-25 23:31:13 -07001546static inline void set_page_zone(struct page *page, enum zone_type zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Dave Hansen348f8b62005-06-23 00:07:40 -07001548 page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT);
1549 page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT;
1550}
Christoph Lameter2f1b6242006-09-25 23:31:13 -07001551
Dave Hansen348f8b62005-06-23 00:07:40 -07001552static inline void set_page_node(struct page *page, unsigned long node)
1553{
1554 page->flags &= ~(NODES_MASK << NODES_PGSHIFT);
1555 page->flags |= (node & NODES_MASK) << NODES_PGSHIFT;
1556}
Christoph Lameter89689ae2006-12-06 20:31:45 -08001557
Christoph Lameter2f1b6242006-09-25 23:31:13 -07001558static inline void set_page_links(struct page *page, enum zone_type zone,
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001559 unsigned long node, unsigned long pfn)
Dave Hansen348f8b62005-06-23 00:07:40 -07001560{
1561 set_page_zone(page, zone);
1562 set_page_node(page, node);
Cody P Schafer9127ab42013-02-22 16:35:21 -08001563#ifdef SECTION_IN_PAGE_FLAGS
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001564 set_page_section(page, pfn_to_section_nr(pfn));
Daniel Kiperbf4e8902011-05-24 17:12:32 -07001565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Greg Thelen0610c252015-10-01 15:37:02 -07001568#ifdef CONFIG_MEMCG
1569static inline struct mem_cgroup *page_memcg(struct page *page)
1570{
1571 return page->mem_cgroup;
1572}
Johannes Weiner55779ec2016-07-28 15:45:10 -07001573static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
1574{
1575 WARN_ON_ONCE(!rcu_read_lock_held());
1576 return READ_ONCE(page->mem_cgroup);
1577}
Greg Thelen0610c252015-10-01 15:37:02 -07001578#else
1579static inline struct mem_cgroup *page_memcg(struct page *page)
1580{
1581 return NULL;
1582}
Johannes Weiner55779ec2016-07-28 15:45:10 -07001583static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
1584{
1585 WARN_ON_ONCE(!rcu_read_lock_held());
1586 return NULL;
1587}
Greg Thelen0610c252015-10-01 15:37:02 -07001588#endif
1589
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001590/*
1591 * Some inline functions in vmstat.h depend on page_zone()
1592 */
1593#include <linux/vmstat.h>
1594
Ian Campbell33dd4e02011-07-25 17:11:51 -07001595static __always_inline void *lowmem_page_address(const struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Ard Biesheuvel1dff8082016-04-18 18:04:57 +02001597 return page_to_virt(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
1600#if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
1601#define HASHED_PAGE_VIRTUAL
1602#endif
1603
1604#if defined(WANT_PAGE_VIRTUAL)
Geert Uytterhoevenf92f4552014-01-21 15:48:47 -08001605static inline void *page_address(const struct page *page)
1606{
1607 return page->virtual;
1608}
1609static inline void set_page_address(struct page *page, void *address)
1610{
1611 page->virtual = address;
1612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613#define page_address_init() do { } while(0)
1614#endif
1615
1616#if defined(HASHED_PAGE_VIRTUAL)
Ian Campbellf9918792011-08-17 13:45:09 +01001617void *page_address(const struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618void set_page_address(struct page *page, void *virtual);
1619void page_address_init(void);
1620#endif
1621
1622#if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
1623#define page_address(page) lowmem_page_address(page)
1624#define set_page_address(page, address) do { } while(0)
1625#define page_address_init() do { } while(0)
1626#endif
1627
Kirill A. Shutemove39155e2015-04-15 16:14:53 -07001628extern void *page_rmapping(struct page *page);
1629extern struct anon_vma *page_anon_vma(struct page *page);
Shaohua Li98003392013-02-22 16:34:35 -08001630extern struct address_space *page_mapping(struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Mel Gormanf981c592012-07-31 16:44:47 -07001632extern struct address_space *__page_file_mapping(struct page *);
1633
1634static inline
1635struct address_space *page_file_mapping(struct page *page)
1636{
1637 if (unlikely(PageSwapCache(page)))
1638 return __page_file_mapping(page);
1639
1640 return page->mapping;
1641}
1642
Huang Yingf6ab1f72016-10-07 17:00:21 -07001643extern pgoff_t __page_file_index(struct page *page);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645/*
1646 * Return the pagecache index of the passed page. Regular pagecache pages
Huang Yingf6ab1f72016-10-07 17:00:21 -07001647 * use ->index whereas swapcache pages use swp_offset(->private)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 */
1649static inline pgoff_t page_index(struct page *page)
1650{
1651 if (unlikely(PageSwapCache(page)))
Huang Yingf6ab1f72016-10-07 17:00:21 -07001652 return __page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return page->index;
1654}
1655
Andrew Morton1aa8aea2016-05-19 17:12:00 -07001656bool page_mapped(struct page *page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001657struct address_space *page_mapping(struct page *page);
Huang Yingcb9f7532018-04-05 16:24:39 -07001658struct address_space *page_mapping_file(struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660/*
Michal Hocko2f064f32015-08-21 14:11:51 -07001661 * Return true only if the page has been allocated with
1662 * ALLOC_NO_WATERMARKS and the low watermark was not
1663 * met implying that the system is under some pressure.
1664 */
1665static inline bool page_is_pfmemalloc(struct page *page)
1666{
1667 /*
1668 * Page index cannot be this large so this must be
1669 * a pfmemalloc page.
1670 */
1671 return page->index == -1UL;
1672}
1673
1674/*
1675 * Only to be called by the page allocator on a freshly allocated
1676 * page.
1677 */
1678static inline void set_page_pfmemalloc(struct page *page)
1679{
1680 page->index = -1UL;
1681}
1682
1683static inline void clear_page_pfmemalloc(struct page *page)
1684{
1685 page->index = 0;
1686}
1687
1688/*
Nick Piggin1c0fe6e2009-01-06 14:38:59 -08001689 * Can be called by the pagefault handler when it gets a VM_FAULT_OOM.
1690 */
1691extern void pagefault_out_of_memory(void);
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
Matthew Wilcox (Oracle)ee6c4002020-08-14 17:30:43 -07001694#define offset_in_thp(page, p) ((unsigned long)(p) & (thp_size(page) - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
David Rientjesddd588b2011-03-22 16:30:46 -07001696/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07001697 * Flags passed to show_mem() and show_free_areas() to suppress output in
David Rientjesddd588b2011-03-22 16:30:46 -07001698 * various contexts.
1699 */
David Rientjes4b59e6c2013-04-29 15:06:11 -07001700#define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */
David Rientjesddd588b2011-03-22 16:30:46 -07001701
Michal Hocko9af744d2017-02-22 15:46:16 -08001702extern void show_free_areas(unsigned int flags, nodemask_t *nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Arnd Bergmann710ec382019-09-23 15:32:59 -07001704#ifdef CONFIG_MMU
Wang Xiaoqiang7f43add2016-01-15 16:57:22 -08001705extern bool can_do_mlock(void);
Arnd Bergmann710ec382019-09-23 15:32:59 -07001706#else
1707static inline bool can_do_mlock(void) { return false; }
1708#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709extern int user_shm_lock(size_t, struct user_struct *);
1710extern void user_shm_unlock(size_t, struct user_struct *);
1711
1712/*
1713 * Parameter block passed down to zap_pte_range in exceptional cases.
1714 */
1715struct zap_details {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct address_space *check_mapping; /* Check page->mapping if set */
1717 pgoff_t first_index; /* Lowest page->index to unmap */
1718 pgoff_t last_index; /* Highest page->index to unmap */
Hugh Dickins00102752021-06-15 18:24:03 -07001719 struct page *single_page; /* Locked page to be unmapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720};
1721
Laurent Dufour10a5eb62018-04-17 16:33:21 +02001722struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
1723 pte_t pte, unsigned long vma_flags);
1724static inline struct page *vm_normal_page(struct vm_area_struct *vma,
1725 unsigned long addr, pte_t pte)
1726{
1727 return _vm_normal_page(vma, addr, pte, vma->vm_flags);
1728}
1729
Gerald Schaefer28093f92016-04-28 16:18:35 -07001730struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
1731 pmd_t pmd);
Nick Piggin7e675132008-04-28 02:13:00 -07001732
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001733void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1734 unsigned long size);
Al Viro14f5ff52012-03-05 13:38:09 -05001735void zap_page_range(struct vm_area_struct *vma, unsigned long address,
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001736 unsigned long size);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001737void unmap_vmas(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
1738 unsigned long start, unsigned long end);
Matt Mackalle6473092008-02-04 22:29:01 -08001739
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001740struct mmu_notifier_range;
1741
Jan Beulich42b77722008-07-23 21:27:10 -07001742void free_pgd_range(struct mmu_gather *tlb, unsigned long addr,
Hugh Dickins3bf5ee92005-04-19 13:29:16 -07001743 unsigned long end, unsigned long floor, unsigned long ceiling);
Peter Xuc78f4632020-10-13 16:54:21 -07001744int
1745copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
Paolo Bonzinia42150f2021-02-05 05:07:11 -05001746int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,
1747 struct mmu_notifier_range *range, pte_t **ptepp,
1748 pmd_t **pmdpp, spinlock_t **ptlp);
Christoph Hellwig6d9c9ec2020-12-15 20:47:23 -08001749int follow_pte(struct mm_struct *mm, unsigned long address,
Paolo Bonzinia42150f2021-02-05 05:07:11 -05001750 pte_t **ptepp, spinlock_t **ptlp);
Johannes Weiner3b6748e2009-06-16 15:32:35 -07001751int follow_pfn(struct vm_area_struct *vma, unsigned long address,
1752 unsigned long *pfn);
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08001753int follow_phys(struct vm_area_struct *vma, unsigned long address,
1754 unsigned int flags, unsigned long *prot, resource_size_t *phys);
Rik van Riel28b2ee22008-07-23 21:27:05 -07001755int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
1756 void *buf, int len, int write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Peter Zijlstra2ce6b112018-04-17 16:33:14 +02001758#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
1759static inline void vm_write_begin(struct vm_area_struct *vma)
1760{
Vinayak Menonc9201632021-01-15 19:52:40 +05301761 /*
1762 * The reads never spins and preemption
1763 * disablement is not required.
1764 */
Peter Zijlstra2ce6b112018-04-17 16:33:14 +02001765 raw_write_seqcount_begin(&vma->vm_sequence);
1766}
Vinayak Menonc9201632021-01-15 19:52:40 +05301767static inline void vm_write_end(struct vm_area_struct *vma)
Peter Zijlstra2ce6b112018-04-17 16:33:14 +02001768{
1769 raw_write_seqcount_end(&vma->vm_sequence);
1770}
1771#else
1772static inline void vm_write_begin(struct vm_area_struct *vma)
1773{
1774}
Peter Zijlstra2ce6b112018-04-17 16:33:14 +02001775static inline void vm_write_end(struct vm_area_struct *vma)
1776{
1777}
Peter Zijlstra2ce6b112018-04-17 16:33:14 +02001778#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
1779
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001780extern void truncate_pagecache(struct inode *inode, loff_t new);
Christoph Hellwig2c27c652010-06-04 11:30:04 +02001781extern void truncate_setsize(struct inode *inode, loff_t newsize);
Jan Kara90a80202014-10-01 21:49:18 -04001782void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to);
Hugh Dickins623e3db2012-03-28 14:42:40 -07001783void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end);
Nick Piggin750b4982009-09-16 11:50:12 +02001784int truncate_inode_page(struct address_space *mapping, struct page *page);
Andi Kleen25718732009-09-16 11:50:13 +02001785int generic_error_remove_page(struct address_space *mapping, struct page *page);
Wu Fengguang83f78662009-09-16 11:50:13 +02001786int invalidate_inode_page(struct page *page);
1787
David Howells7ee1dd32006-01-06 00:11:44 -08001788#ifdef CONFIG_MMU
Souptick Joarder2b740302018-08-23 17:01:36 -07001789extern vm_fault_t handle_mm_fault(struct vm_area_struct *vma,
Peter Xubce617e2020-08-11 18:37:44 -07001790 unsigned long address, unsigned int flags,
1791 struct pt_regs *regs);
Peter Xu64019a22020-08-11 18:39:01 -07001792extern int fixup_user_fault(struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001793 unsigned long address, unsigned int fault_flags,
1794 bool *unlocked);
Peter Zijlstra1c537172018-04-17 16:33:24 +02001795
1796#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
Suren Baghdasaryan9e4d8422021-02-03 17:16:09 -08001797extern vm_fault_t __handle_speculative_fault(struct mm_struct *mm,
1798 unsigned long address,
1799 unsigned int flags,
Minchan Kim458e81e2021-05-03 10:52:58 -07001800 struct vm_area_struct **vma,
1801 struct pt_regs *regs);
Suren Baghdasaryan9e4d8422021-02-03 17:16:09 -08001802static inline vm_fault_t handle_speculative_fault(struct mm_struct *mm,
1803 unsigned long address,
1804 unsigned int flags,
Minchan Kim458e81e2021-05-03 10:52:58 -07001805 struct vm_area_struct **vma,
1806 struct pt_regs *regs)
Peter Zijlstra1c537172018-04-17 16:33:24 +02001807{
1808 /*
1809 * Try speculative page fault for multithreaded user space task only.
1810 */
Laurent Dufour99e15a02018-04-17 16:33:28 +02001811 if (!(flags & FAULT_FLAG_USER) || atomic_read(&mm->mm_users) == 1) {
1812 *vma = NULL;
Peter Zijlstra1c537172018-04-17 16:33:24 +02001813 return VM_FAULT_RETRY;
Laurent Dufour99e15a02018-04-17 16:33:28 +02001814 }
Minchan Kim458e81e2021-05-03 10:52:58 -07001815 return __handle_speculative_fault(mm, address, flags, vma, regs);
Peter Zijlstra1c537172018-04-17 16:33:24 +02001816}
Laurent Dufour99e15a02018-04-17 16:33:28 +02001817extern bool can_reuse_spf_vma(struct vm_area_struct *vma,
1818 unsigned long address);
Peter Zijlstra1c537172018-04-17 16:33:24 +02001819#else
Suren Baghdasaryan9e4d8422021-02-03 17:16:09 -08001820static inline vm_fault_t handle_speculative_fault(struct mm_struct *mm,
1821 unsigned long address,
1822 unsigned int flags,
Minchan Kim458e81e2021-05-03 10:52:58 -07001823 struct vm_area_struct **vma,
1824 struct pt_regs *regs)
Peter Zijlstra1c537172018-04-17 16:33:24 +02001825{
1826 return VM_FAULT_RETRY;
1827}
Laurent Dufour99e15a02018-04-17 16:33:28 +02001828static inline bool can_reuse_spf_vma(struct vm_area_struct *vma,
1829 unsigned long address)
1830{
1831 return false;
1832}
Peter Zijlstra1c537172018-04-17 16:33:24 +02001833#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
1834
Hugh Dickins00102752021-06-15 18:24:03 -07001835void unmap_mapping_page(struct page *page);
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08001836void unmap_mapping_pages(struct address_space *mapping,
1837 pgoff_t start, pgoff_t nr, bool even_cows);
1838void unmap_mapping_range(struct address_space *mapping,
1839 loff_t const holebegin, loff_t const holelen, int even_cows);
David Howells7ee1dd32006-01-06 00:11:44 -08001840#else
Souptick Joarder2b740302018-08-23 17:01:36 -07001841static inline vm_fault_t handle_mm_fault(struct vm_area_struct *vma,
Peter Xubce617e2020-08-11 18:37:44 -07001842 unsigned long address, unsigned int flags,
1843 struct pt_regs *regs)
David Howells7ee1dd32006-01-06 00:11:44 -08001844{
1845 /* should never happen if there's no MMU */
1846 BUG();
1847 return VM_FAULT_SIGBUS;
1848}
Peter Xu64019a22020-08-11 18:39:01 -07001849static inline int fixup_user_fault(struct mm_struct *mm, unsigned long address,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001850 unsigned int fault_flags, bool *unlocked)
Peter Zijlstra5c723ba2011-07-27 12:17:11 +02001851{
1852 /* should never happen if there's no MMU */
1853 BUG();
1854 return -EFAULT;
1855}
Hugh Dickins00102752021-06-15 18:24:03 -07001856static inline void unmap_mapping_page(struct page *page) { }
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08001857static inline void unmap_mapping_pages(struct address_space *mapping,
1858 pgoff_t start, pgoff_t nr, bool even_cows) { }
1859static inline void unmap_mapping_range(struct address_space *mapping,
1860 loff_t const holebegin, loff_t const holelen, int even_cows) { }
David Howells7ee1dd32006-01-06 00:11:44 -08001861#endif
Nick Pigginf33ea7f2005-08-03 20:24:01 +10001862
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08001863static inline void unmap_shared_mapping_range(struct address_space *mapping,
1864 loff_t const holebegin, loff_t const holelen)
1865{
1866 unmap_mapping_range(mapping, holebegin, holelen, 0);
1867}
1868
1869extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
1870 void *buf, int len, unsigned int gup_flags);
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04001871extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001872 void *buf, int len, unsigned int gup_flags);
Eric W. Biederman84d77d32016-11-22 12:06:50 -06001873extern int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1874 unsigned long addr, void *buf, int len, unsigned int gup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Peter Xu64019a22020-08-11 18:39:01 -07001876long get_user_pages_remote(struct mm_struct *mm,
Dave Hansen1e987792016-02-12 13:01:54 -08001877 unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001878 unsigned int gup_flags, struct page **pages,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001879 struct vm_area_struct **vmas, int *locked);
Peter Xu64019a22020-08-11 18:39:01 -07001880long pin_user_pages_remote(struct mm_struct *mm,
John Hubbardeddb1c22020-01-30 22:12:54 -08001881 unsigned long start, unsigned long nr_pages,
1882 unsigned int gup_flags, struct page **pages,
1883 struct vm_area_struct **vmas, int *locked);
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001884long get_user_pages(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +01001885 unsigned int gup_flags, struct page **pages,
Dave Hansencde70142016-02-12 13:01:55 -08001886 struct vm_area_struct **vmas);
John Hubbardeddb1c22020-01-30 22:12:54 -08001887long pin_user_pages(unsigned long start, unsigned long nr_pages,
1888 unsigned int gup_flags, struct page **pages,
1889 struct vm_area_struct **vmas);
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001890long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes3b913172016-10-13 01:20:14 +01001891 unsigned int gup_flags, struct page **pages, int *locked);
John Hubbard420c2092020-06-07 21:41:02 -07001892long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
1893 unsigned int gup_flags, struct page **pages, int *locked);
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001894long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakesc1641542016-10-13 01:20:13 +01001895 struct page **pages, unsigned int gup_flags);
John Hubbard91429022020-06-01 21:48:27 -07001896long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
1897 struct page **pages, unsigned int gup_flags);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001898
Ira Weiny73b01402019-05-13 17:17:11 -07001899int get_user_pages_fast(unsigned long start, int nr_pages,
1900 unsigned int gup_flags, struct page **pages);
John Hubbardeddb1c22020-01-30 22:12:54 -08001901int pin_user_pages_fast(unsigned long start, int nr_pages,
1902 unsigned int gup_flags, struct page **pages);
Jan Kara8025e5d2015-07-13 11:55:44 -03001903
Daniel Jordan79eb5972019-07-16 16:30:54 -07001904int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc);
1905int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc,
1906 struct task_struct *task, bool bypass_rlim);
1907
Jan Kara8025e5d2015-07-13 11:55:44 -03001908/* Container for pinned pfns / pages */
1909struct frame_vector {
1910 unsigned int nr_allocated; /* Number of frames we have space for */
1911 unsigned int nr_frames; /* Number of frames stored in ptrs array */
1912 bool got_ref; /* Did we pin pages by getting page ref? */
1913 bool is_pfns; /* Does array contain pages or pfns? */
chenqiwu57e86fa2020-06-04 16:48:55 -07001914 void *ptrs[]; /* Array of pinned pfns / pages. Use
Jan Kara8025e5d2015-07-13 11:55:44 -03001915 * pfns_vector_pages() or pfns_vector_pfns()
1916 * for access */
1917};
1918
1919struct frame_vector *frame_vector_create(unsigned int nr_frames);
1920void frame_vector_destroy(struct frame_vector *vec);
1921int get_vaddr_frames(unsigned long start, unsigned int nr_pfns,
Lorenzo Stoakes7f23b352016-10-13 01:20:15 +01001922 unsigned int gup_flags, struct frame_vector *vec);
Jan Kara8025e5d2015-07-13 11:55:44 -03001923void put_vaddr_frames(struct frame_vector *vec);
1924int frame_vector_to_pages(struct frame_vector *vec);
1925void frame_vector_to_pfns(struct frame_vector *vec);
1926
1927static inline unsigned int frame_vector_count(struct frame_vector *vec)
1928{
1929 return vec->nr_frames;
1930}
1931
1932static inline struct page **frame_vector_pages(struct frame_vector *vec)
1933{
1934 if (vec->is_pfns) {
1935 int err = frame_vector_to_pages(vec);
1936
1937 if (err)
1938 return ERR_PTR(err);
1939 }
1940 return (struct page **)(vec->ptrs);
1941}
1942
1943static inline unsigned long *frame_vector_pfns(struct frame_vector *vec)
1944{
1945 if (!vec->is_pfns)
1946 frame_vector_to_pfns(vec);
1947 return (unsigned long *)(vec->ptrs);
1948}
1949
Mel Gorman18022c52012-07-31 16:44:51 -07001950struct kvec;
1951int get_kernel_pages(const struct kvec *iov, int nr_pages, int write,
1952 struct page **pages);
1953int get_kernel_page(unsigned long start, int write, struct page **pages);
Hugh Dickinsf3e8fcc2009-09-21 17:03:25 -07001954struct page *get_dump_page(unsigned long addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
David Howellscf9a2ae2006-08-29 19:05:54 +01001956extern int try_to_release_page(struct page * page, gfp_t gfp_mask);
Lukas Czernerd47992f2013-05-21 23:17:23 -04001957extern void do_invalidatepage(struct page *page, unsigned int offset,
1958 unsigned int length);
David Howellscf9a2ae2006-08-29 19:05:54 +01001959
Matthew Wilcoxf82b3762018-04-10 16:36:44 -07001960void __set_page_dirty(struct page *, struct address_space *, int warn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961int __set_page_dirty_nobuffers(struct page *page);
Ken Chen76719322007-02-10 01:43:15 -08001962int __set_page_dirty_no_writeback(struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963int redirty_page_for_writepage(struct writeback_control *wbc,
1964 struct page *page);
Johannes Weiner62cccb82016-03-15 14:57:22 -07001965void account_page_dirtied(struct page *page, struct address_space *mapping);
Greg Thelenc4843a72015-05-22 17:13:16 -04001966void account_page_cleaned(struct page *page, struct address_space *mapping,
Johannes Weiner62cccb82016-03-15 14:57:22 -07001967 struct bdi_writeback *wb);
Harvey Harrisonb3c97522008-02-13 15:03:15 -08001968int set_page_dirty(struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969int set_page_dirty_lock(struct page *page);
Jan Kara736304f2017-11-15 17:37:11 -08001970void __cancel_dirty_page(struct page *page);
1971static inline void cancel_dirty_page(struct page *page)
1972{
1973 /* Avoid atomic ops, locking, etc. when not actually needed. */
1974 if (PageDirty(page))
1975 __cancel_dirty_page(page);
1976}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977int clear_page_dirty_for_io(struct page *page);
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -07001978
William Robertsa9090252014-02-11 10:11:59 -08001979int get_cmdline(struct task_struct *task, char *buffer, int buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001981extern unsigned long move_page_tables(struct vm_area_struct *vma,
1982 unsigned long old_addr, struct vm_area_struct *new_vma,
Michel Lespinasse38a76012012-10-08 16:31:50 -07001983 unsigned long new_addr, unsigned long len,
1984 bool need_rmap_locks);
Peter Xu58705442020-04-06 20:05:45 -07001985
1986/*
1987 * Flags used by change_protection(). For now we make it a bitmap so
1988 * that we can pass in multiple flags just like parameters. However
1989 * for now all the callers are only use one of the flags at the same
1990 * time.
1991 */
1992/* Whether we should allow dirty bit accounting */
1993#define MM_CP_DIRTY_ACCT (1UL << 0)
1994/* Whether this protection change is for NUMA hints */
1995#define MM_CP_PROT_NUMA (1UL << 1)
Peter Xu292924b2020-04-06 20:05:49 -07001996/* Whether this change is for write protecting */
1997#define MM_CP_UFFD_WP (1UL << 2) /* do wp */
1998#define MM_CP_UFFD_WP_RESOLVE (1UL << 3) /* Resolve wp */
1999#define MM_CP_UFFD_WP_ALL (MM_CP_UFFD_WP | \
2000 MM_CP_UFFD_WP_RESOLVE)
Peter Xu58705442020-04-06 20:05:45 -07002001
Peter Zijlstra7da4d642012-11-19 03:14:23 +01002002extern unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
2003 unsigned long end, pgprot_t newprot,
Peter Xu58705442020-04-06 20:05:45 -07002004 unsigned long cp_flags);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002005extern int mprotect_fixup(struct vm_area_struct *vma,
2006 struct vm_area_struct **pprev, unsigned long start,
2007 unsigned long end, unsigned long newflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Nick Piggin21cc1992008-07-25 19:45:22 -07002009/*
Peter Zijlstra465a4542009-06-15 12:31:37 +02002010 * doesn't attempt to fault and will return short.
2011 */
Souptick Joarderdadbb612020-06-07 21:40:55 -07002012int get_user_pages_fast_only(unsigned long start, int nr_pages,
2013 unsigned int gup_flags, struct page **pages);
John Hubbard104acc32020-06-03 15:56:34 -07002014int pin_user_pages_fast_only(unsigned long start, int nr_pages,
2015 unsigned int gup_flags, struct page **pages);
Souptick Joarderdadbb612020-06-07 21:40:55 -07002016
2017static inline bool get_user_page_fast_only(unsigned long addr,
2018 unsigned int gup_flags, struct page **pagep)
2019{
2020 return get_user_pages_fast_only(addr, 1, gup_flags, pagep) == 1;
2021}
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002022/*
2023 * per-process(per-mm_struct) statistics.
2024 */
Matt Fleming172703b2011-05-24 17:12:36 -07002025static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
2026{
Konstantin Khlebnikov69c97822012-03-21 16:33:49 -07002027 long val = atomic_long_read(&mm->rss_stat.count[member]);
2028
2029#ifdef SPLIT_RSS_COUNTING
2030 /*
2031 * counter is updated in asynchronous manner and may go to minus.
2032 * But it's never be expected number for users.
2033 */
2034 if (val < 0)
2035 val = 0;
Matt Fleming172703b2011-05-24 17:12:36 -07002036#endif
Konstantin Khlebnikov69c97822012-03-21 16:33:49 -07002037 return (unsigned long)val;
2038}
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002039
Joel Fernandes77dfeaa2019-12-10 10:45:34 -05002040void mm_trace_rss_stat(struct mm_struct *mm, int member, long count,
2041 long value);
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -08002042
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002043static inline void add_mm_counter(struct mm_struct *mm, int member, long value)
2044{
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -08002045 long count = atomic_long_add_return(value, &mm->rss_stat.count[member]);
2046
Joel Fernandes77dfeaa2019-12-10 10:45:34 -05002047 mm_trace_rss_stat(mm, member, count, value);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002048}
2049
2050static inline void inc_mm_counter(struct mm_struct *mm, int member)
2051{
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -08002052 long count = atomic_long_inc_return(&mm->rss_stat.count[member]);
2053
Joel Fernandes77dfeaa2019-12-10 10:45:34 -05002054 mm_trace_rss_stat(mm, member, count, 1);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002055}
2056
2057static inline void dec_mm_counter(struct mm_struct *mm, int member)
2058{
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -08002059 long count = atomic_long_dec_return(&mm->rss_stat.count[member]);
2060
Joel Fernandes77dfeaa2019-12-10 10:45:34 -05002061 mm_trace_rss_stat(mm, member, count, -1);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002062}
2063
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002064/* Optimized variant when page is already known not to be PageAnon */
2065static inline int mm_counter_file(struct page *page)
2066{
2067 if (PageSwapBacked(page))
2068 return MM_SHMEMPAGES;
2069 return MM_FILEPAGES;
2070}
2071
2072static inline int mm_counter(struct page *page)
2073{
2074 if (PageAnon(page))
2075 return MM_ANONPAGES;
2076 return mm_counter_file(page);
2077}
2078
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002079static inline unsigned long get_mm_rss(struct mm_struct *mm)
2080{
2081 return get_mm_counter(mm, MM_FILEPAGES) +
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002082 get_mm_counter(mm, MM_ANONPAGES) +
2083 get_mm_counter(mm, MM_SHMEMPAGES);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002084}
2085
2086static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
2087{
2088 return max(mm->hiwater_rss, get_mm_rss(mm));
2089}
2090
2091static inline unsigned long get_mm_hiwater_vm(struct mm_struct *mm)
2092{
2093 return max(mm->hiwater_vm, mm->total_vm);
2094}
2095
2096static inline void update_hiwater_rss(struct mm_struct *mm)
2097{
2098 unsigned long _rss = get_mm_rss(mm);
2099
2100 if ((mm)->hiwater_rss < _rss)
2101 (mm)->hiwater_rss = _rss;
2102}
2103
2104static inline void update_hiwater_vm(struct mm_struct *mm)
2105{
2106 if (mm->hiwater_vm < mm->total_vm)
2107 mm->hiwater_vm = mm->total_vm;
2108}
2109
Petr Cermak695f0552015-02-12 15:01:00 -08002110static inline void reset_mm_hiwater_rss(struct mm_struct *mm)
2111{
2112 mm->hiwater_rss = get_mm_rss(mm);
2113}
2114
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08002115static inline void setmax_mm_hiwater_rss(unsigned long *maxrss,
2116 struct mm_struct *mm)
2117{
2118 unsigned long hiwater_rss = get_mm_hiwater_rss(mm);
2119
2120 if (*maxrss < hiwater_rss)
2121 *maxrss = hiwater_rss;
2122}
2123
KAMEZAWA Hiroyuki53bddb42010-03-10 15:20:38 -08002124#if defined(SPLIT_RSS_COUNTING)
David Rientjes05af2e12012-03-21 16:34:13 -07002125void sync_mm_rss(struct mm_struct *mm);
KAMEZAWA Hiroyuki53bddb42010-03-10 15:20:38 -08002126#else
David Rientjes05af2e12012-03-21 16:34:13 -07002127static inline void sync_mm_rss(struct mm_struct *mm)
KAMEZAWA Hiroyuki53bddb42010-03-10 15:20:38 -08002128{
2129}
2130#endif
Peter Zijlstra465a4542009-06-15 12:31:37 +02002131
Anshuman Khandual78e7c5a2020-04-10 14:33:13 -07002132#ifndef CONFIG_ARCH_HAS_PTE_SPECIAL
2133static inline int pte_special(pte_t pte)
2134{
2135 return 0;
2136}
2137
2138static inline pte_t pte_mkspecial(pte_t pte)
2139{
2140 return pte;
2141}
2142#endif
2143
Robin Murphy17596732019-07-16 16:30:47 -07002144#ifndef CONFIG_ARCH_HAS_PTE_DEVMAP
Dan Williams3565fce2016-01-15 16:56:55 -08002145static inline int pte_devmap(pte_t pte)
2146{
2147 return 0;
2148}
2149#endif
2150
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07002151int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot);
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002152
Namhyung Kim25ca1d62010-10-26 14:21:59 -07002153extern pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
2154 spinlock_t **ptl);
2155static inline pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,
2156 spinlock_t **ptl)
2157{
2158 pte_t *ptep;
2159 __cond_lock(*ptl, ptep = __get_locked_pte(mm, addr, ptl));
2160 return ptep;
2161}
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08002162
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002163#ifdef __PAGETABLE_P4D_FOLDED
2164static inline int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
Nick Piggin5f22df02007-05-06 14:49:02 -07002165 unsigned long address)
2166{
2167 return 0;
2168}
2169#else
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002170int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address);
2171#endif
2172
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08002173#if defined(__PAGETABLE_PUD_FOLDED) || !defined(CONFIG_MMU)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002174static inline int __pud_alloc(struct mm_struct *mm, p4d_t *p4d,
2175 unsigned long address)
2176{
2177 return 0;
2178}
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08002179static inline void mm_inc_nr_puds(struct mm_struct *mm) {}
2180static inline void mm_dec_nr_puds(struct mm_struct *mm) {}
2181
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002182#else
2183int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08002184
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08002185static inline void mm_inc_nr_puds(struct mm_struct *mm)
2186{
Martin Schwidefsky6d212db2018-10-15 10:30:23 +02002187 if (mm_pud_folded(mm))
2188 return;
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002189 atomic_long_add(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08002190}
2191
2192static inline void mm_dec_nr_puds(struct mm_struct *mm)
2193{
Martin Schwidefsky6d212db2018-10-15 10:30:23 +02002194 if (mm_pud_folded(mm))
2195 return;
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002196 atomic_long_sub(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08002197}
Nick Piggin5f22df02007-05-06 14:49:02 -07002198#endif
2199
Kirill A. Shutemov2d2f5112015-02-12 14:59:59 -08002200#if defined(__PAGETABLE_PMD_FOLDED) || !defined(CONFIG_MMU)
Nick Piggin5f22df02007-05-06 14:49:02 -07002201static inline int __pmd_alloc(struct mm_struct *mm, pud_t *pud,
2202 unsigned long address)
2203{
2204 return 0;
2205}
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08002206
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08002207static inline void mm_inc_nr_pmds(struct mm_struct *mm) {}
2208static inline void mm_dec_nr_pmds(struct mm_struct *mm) {}
2209
Nick Piggin5f22df02007-05-06 14:49:02 -07002210#else
Hugh Dickins1bb36302005-10-29 18:16:22 -07002211int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08002212
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08002213static inline void mm_inc_nr_pmds(struct mm_struct *mm)
2214{
Martin Schwidefsky6d212db2018-10-15 10:30:23 +02002215 if (mm_pmd_folded(mm))
2216 return;
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002217 atomic_long_add(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08002218}
2219
2220static inline void mm_dec_nr_pmds(struct mm_struct *mm)
2221{
Martin Schwidefsky6d212db2018-10-15 10:30:23 +02002222 if (mm_pmd_folded(mm))
2223 return;
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002224 atomic_long_sub(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08002225}
Nick Piggin5f22df02007-05-06 14:49:02 -07002226#endif
2227
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002228#ifdef CONFIG_MMU
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002229static inline void mm_pgtables_bytes_init(struct mm_struct *mm)
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002230{
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002231 atomic_long_set(&mm->pgtables_bytes, 0);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002232}
2233
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002234static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm)
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002235{
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002236 return atomic_long_read(&mm->pgtables_bytes);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002237}
2238
2239static inline void mm_inc_nr_ptes(struct mm_struct *mm)
2240{
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002241 atomic_long_add(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002242}
2243
2244static inline void mm_dec_nr_ptes(struct mm_struct *mm)
2245{
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002246 atomic_long_sub(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002247}
2248#else
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002249
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08002250static inline void mm_pgtables_bytes_init(struct mm_struct *mm) {}
2251static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm)
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08002252{
2253 return 0;
2254}
2255
2256static inline void mm_inc_nr_ptes(struct mm_struct *mm) {}
2257static inline void mm_dec_nr_ptes(struct mm_struct *mm) {}
2258#endif
2259
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002260int __pte_alloc(struct mm_struct *mm, pmd_t *pmd);
2261int __pte_alloc_kernel(pmd_t *pmd);
Hugh Dickins1bb36302005-10-29 18:16:22 -07002262
Mike Rapoportf9492862019-12-04 16:54:32 -08002263#if defined(CONFIG_MMU)
Kirill A. Shutemov505a60e2017-03-09 17:24:03 +03002264
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002265static inline p4d_t *p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
2266 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002268 return (unlikely(pgd_none(*pgd)) && __p4d_alloc(mm, pgd, address)) ?
2269 NULL : p4d_offset(pgd, address);
2270}
2271
2272static inline pud_t *pud_alloc(struct mm_struct *mm, p4d_t *p4d,
2273 unsigned long address)
2274{
2275 return (unlikely(p4d_none(*p4d)) && __pud_alloc(mm, p4d, address)) ?
2276 NULL : pud_offset(p4d, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
Joerg Roedeld8626132020-06-01 21:52:18 -07002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2280{
Hugh Dickins1bb36302005-10-29 18:16:22 -07002281 return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))?
2282 NULL: pmd_offset(pud, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
Mike Rapoportf9492862019-12-04 16:54:32 -08002284#endif /* CONFIG_MMU */
Hugh Dickins1bb36302005-10-29 18:16:22 -07002285
Kirill A. Shutemov57c1ffc2013-11-14 14:30:45 -08002286#if USE_SPLIT_PTE_PTLOCKS
Kirill A. Shutemov597d7952013-12-20 13:35:58 +02002287#if ALLOC_SPLIT_PTLOCKS
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08002288void __init ptlock_cache_init(void);
Peter Zijlstra539edb52013-11-14 14:31:52 -08002289extern bool ptlock_alloc(struct page *page);
2290extern void ptlock_free(struct page *page);
2291
2292static inline spinlock_t *ptlock_ptr(struct page *page)
2293{
2294 return page->ptl;
2295}
Kirill A. Shutemov597d7952013-12-20 13:35:58 +02002296#else /* ALLOC_SPLIT_PTLOCKS */
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08002297static inline void ptlock_cache_init(void)
2298{
2299}
2300
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002301static inline bool ptlock_alloc(struct page *page)
2302{
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002303 return true;
2304}
Peter Zijlstra539edb52013-11-14 14:31:52 -08002305
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002306static inline void ptlock_free(struct page *page)
2307{
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002308}
2309
2310static inline spinlock_t *ptlock_ptr(struct page *page)
2311{
Peter Zijlstra539edb52013-11-14 14:31:52 -08002312 return &page->ptl;
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002313}
Kirill A. Shutemov597d7952013-12-20 13:35:58 +02002314#endif /* ALLOC_SPLIT_PTLOCKS */
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002315
2316static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
2317{
2318 return ptlock_ptr(pmd_page(*pmd));
2319}
2320
2321static inline bool ptlock_init(struct page *page)
2322{
2323 /*
2324 * prep_new_page() initialize page->private (and therefore page->ptl)
2325 * with 0. Make sure nobody took it in use in between.
2326 *
2327 * It can happen if arch try to use slab for page table allocation:
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08002328 * slab code uses page->slab_cache, which share storage with page->ptl.
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002329 */
Sasha Levin309381fea2014-01-23 15:52:54 -08002330 VM_BUG_ON_PAGE(*(unsigned long *)&page->ptl, page);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002331 if (!ptlock_alloc(page))
2332 return false;
2333 spin_lock_init(ptlock_ptr(page));
2334 return true;
2335}
2336
Kirill A. Shutemov57c1ffc2013-11-14 14:30:45 -08002337#else /* !USE_SPLIT_PTE_PTLOCKS */
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002338/*
2339 * We use mm->page_table_lock to guard all pagetable pages of the mm.
2340 */
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002341static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
2342{
2343 return &mm->page_table_lock;
2344}
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08002345static inline void ptlock_cache_init(void) {}
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002346static inline bool ptlock_init(struct page *page) { return true; }
Yu Zhao9e247ba2018-12-28 00:36:58 -08002347static inline void ptlock_free(struct page *page) {}
Kirill A. Shutemov57c1ffc2013-11-14 14:30:45 -08002348#endif /* USE_SPLIT_PTE_PTLOCKS */
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002349
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08002350static inline void pgtable_init(void)
2351{
2352 ptlock_cache_init();
2353 pgtable_cache_init();
2354}
2355
Mark Rutlandb4ed71f2019-09-25 16:49:46 -07002356static inline bool pgtable_pte_page_ctor(struct page *page)
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002357{
Vladimir Davydov706874e2015-11-05 18:49:27 -08002358 if (!ptlock_init(page))
2359 return false;
Matthew Wilcox1d40a5e2018-06-07 17:08:23 -07002360 __SetPageTable(page);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002361 inc_zone_page_state(page, NR_PAGETABLE);
Vladimir Davydov706874e2015-11-05 18:49:27 -08002362 return true;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002363}
2364
Mark Rutlandb4ed71f2019-09-25 16:49:46 -07002365static inline void pgtable_pte_page_dtor(struct page *page)
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002366{
Yu Zhao9e247ba2018-12-28 00:36:58 -08002367 ptlock_free(page);
Matthew Wilcox1d40a5e2018-06-07 17:08:23 -07002368 __ClearPageTable(page);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002369 dec_zone_page_state(page, NR_PAGETABLE);
2370}
2371
Hugh Dickinsc74df322005-10-29 18:16:23 -07002372#define pte_offset_map_lock(mm, pmd, address, ptlp) \
2373({ \
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002374 spinlock_t *__ptl = pte_lockptr(mm, pmd); \
Hugh Dickinsc74df322005-10-29 18:16:23 -07002375 pte_t *__pte = pte_offset_map(pmd, address); \
2376 *(ptlp) = __ptl; \
2377 spin_lock(__ptl); \
2378 __pte; \
2379})
2380
2381#define pte_unmap_unlock(pte, ptl) do { \
2382 spin_unlock(ptl); \
2383 pte_unmap(pte); \
2384} while (0)
2385
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002386#define pte_alloc(mm, pmd) (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd))
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -07002387
2388#define pte_alloc_map(mm, pmd, address) \
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002389 (pte_alloc(mm, pmd) ? NULL : pte_offset_map(pmd, address))
Hugh Dickins1bb36302005-10-29 18:16:22 -07002390
Hugh Dickinsc74df322005-10-29 18:16:23 -07002391#define pte_alloc_map_lock(mm, pmd, address, ptlp) \
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002392 (pte_alloc(mm, pmd) ? \
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -07002393 NULL : pte_offset_map_lock(mm, pmd, address, ptlp))
Hugh Dickinsc74df322005-10-29 18:16:23 -07002394
Hugh Dickins1bb36302005-10-29 18:16:22 -07002395#define pte_alloc_kernel(pmd, address) \
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002396 ((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd))? \
Hugh Dickins1bb36302005-10-29 18:16:22 -07002397 NULL: pte_offset_kernel(pmd, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002399#if USE_SPLIT_PMD_PTLOCKS
2400
Martin Schwidefsky634391a2014-02-13 13:53:33 +01002401static struct page *pmd_to_page(pmd_t *pmd)
2402{
2403 unsigned long mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
2404 return virt_to_page((void *)((unsigned long) pmd & mask));
2405}
2406
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002407static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
2408{
Martin Schwidefsky634391a2014-02-13 13:53:33 +01002409 return ptlock_ptr(pmd_to_page(pmd));
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002410}
2411
Matthew Wilcoxb2b29d62020-10-13 16:53:22 -07002412static inline bool pmd_ptlock_init(struct page *page)
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002413{
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002414#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2415 page->pmd_huge_pte = NULL;
2416#endif
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002417 return ptlock_init(page);
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002418}
2419
Matthew Wilcoxb2b29d62020-10-13 16:53:22 -07002420static inline void pmd_ptlock_free(struct page *page)
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002421{
2422#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Sasha Levin309381fea2014-01-23 15:52:54 -08002423 VM_BUG_ON_PAGE(page->pmd_huge_pte, page);
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002424#endif
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08002425 ptlock_free(page);
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002426}
2427
Martin Schwidefsky634391a2014-02-13 13:53:33 +01002428#define pmd_huge_pte(mm, pmd) (pmd_to_page(pmd)->pmd_huge_pte)
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002429
2430#else
2431
Kirill A. Shutemov9a86cb72013-11-14 14:30:51 -08002432static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
2433{
2434 return &mm->page_table_lock;
2435}
2436
Matthew Wilcoxb2b29d62020-10-13 16:53:22 -07002437static inline bool pmd_ptlock_init(struct page *page) { return true; }
2438static inline void pmd_ptlock_free(struct page *page) {}
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002439
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08002440#define pmd_huge_pte(mm, pmd) ((mm)->pmd_huge_pte)
Kirill A. Shutemov9a86cb72013-11-14 14:30:51 -08002441
Kirill A. Shutemove009bb32013-11-14 14:31:07 -08002442#endif
2443
Kirill A. Shutemov9a86cb72013-11-14 14:30:51 -08002444static inline spinlock_t *pmd_lock(struct mm_struct *mm, pmd_t *pmd)
2445{
2446 spinlock_t *ptl = pmd_lockptr(mm, pmd);
2447 spin_lock(ptl);
2448 return ptl;
2449}
2450
Matthew Wilcoxb2b29d62020-10-13 16:53:22 -07002451static inline bool pgtable_pmd_page_ctor(struct page *page)
2452{
2453 if (!pmd_ptlock_init(page))
2454 return false;
2455 __SetPageTable(page);
2456 inc_zone_page_state(page, NR_PAGETABLE);
2457 return true;
2458}
2459
2460static inline void pgtable_pmd_page_dtor(struct page *page)
2461{
2462 pmd_ptlock_free(page);
2463 __ClearPageTable(page);
2464 dec_zone_page_state(page, NR_PAGETABLE);
2465}
2466
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002467/*
2468 * No scalability reason to split PUD locks yet, but follow the same pattern
2469 * as the PMD locks to make it easier if we decide to. The VM should not be
2470 * considered ready to switch to split PUD locks yet; there may be places
2471 * which need to be converted from page_table_lock.
2472 */
2473static inline spinlock_t *pud_lockptr(struct mm_struct *mm, pud_t *pud)
2474{
2475 return &mm->page_table_lock;
2476}
Nicholas Piggin62906022016-12-25 13:00:30 +10002477
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002478static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud)
2479{
2480 spinlock_t *ptl = pud_lockptr(mm, pud);
2481
2482 spin_lock(ptl);
2483 return ptl;
2484}
2485
2486extern void __init pagecache_init(void);
Mike Rapoportbc9331a2020-06-03 15:58:09 -07002487extern void __init free_area_init_memoryless_node(int nid);
David Howells49a7f042012-03-28 18:30:03 +01002488extern void free_initmem(void);
2489
Jiang Liu69afade2013-04-29 15:06:21 -07002490/*
2491 * Free reserved pages within range [PAGE_ALIGN(start), end & PAGE_MASK)
2492 * into the buddy system. The freed pages will be poisoned with pattern
Jiang Liudbe67df2013-07-03 15:02:51 -07002493 * "poison" if it's within range [0, UCHAR_MAX].
Jiang Liu69afade2013-04-29 15:06:21 -07002494 * Return pages freed into the buddy system.
2495 */
Jiang Liu11199692013-07-03 15:02:48 -07002496extern unsigned long free_reserved_area(void *start, void *end,
Alexey Dobriyane5cb1132018-12-28 00:36:03 -08002497 int poison, const char *s);
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07002498
Jiang Liucfa11e02013-04-29 15:07:00 -07002499#ifdef CONFIG_HIGHMEM
2500/*
2501 * Free a highmem page into the buddy system, adjusting totalhigh_pages
2502 * and totalram_pages.
2503 */
2504extern void free_highmem_page(struct page *page);
2505#endif
Jiang Liu69afade2013-04-29 15:06:21 -07002506
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07002507extern void adjust_managed_page_count(struct page *page, long count);
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07002508extern void mem_init_print_info(const char *str);
Jiang Liu69afade2013-04-29 15:06:21 -07002509
Stefan Bader4b50bcc2016-05-20 16:58:38 -07002510extern void reserve_bootmem_region(phys_addr_t start, phys_addr_t end);
Nathan Zimmer92923ca2015-06-30 14:56:48 -07002511
Jiang Liu69afade2013-04-29 15:06:21 -07002512/* Free the reserved page into the buddy system, so it gets managed. */
2513static inline void __free_reserved_page(struct page *page)
2514{
2515 ClearPageReserved(page);
2516 init_page_count(page);
2517 __free_page(page);
2518}
2519
2520static inline void free_reserved_page(struct page *page)
2521{
2522 __free_reserved_page(page);
2523 adjust_managed_page_count(page, 1);
2524}
2525
2526static inline void mark_page_reserved(struct page *page)
2527{
2528 SetPageReserved(page);
2529 adjust_managed_page_count(page, -1);
2530}
2531
2532/*
2533 * Default method to free all the __init memory into the buddy system.
Jiang Liudbe67df2013-07-03 15:02:51 -07002534 * The freed pages will be poisoned with pattern "poison" if it's within
2535 * range [0, UCHAR_MAX].
2536 * Return pages freed into the buddy system.
Jiang Liu69afade2013-04-29 15:06:21 -07002537 */
2538static inline unsigned long free_initmem_default(int poison)
2539{
2540 extern char __init_begin[], __init_end[];
2541
Jiang Liu11199692013-07-03 15:02:48 -07002542 return free_reserved_area(&__init_begin, &__init_end,
Jiang Liu69afade2013-04-29 15:06:21 -07002543 poison, "unused kernel");
2544}
2545
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07002546static inline unsigned long get_num_physpages(void)
2547{
2548 int nid;
2549 unsigned long phys_pages = 0;
2550
2551 for_each_online_node(nid)
2552 phys_pages += node_present_pages(nid);
2553
2554 return phys_pages;
2555}
2556
Mel Gormanc7132162006-09-27 01:49:43 -07002557/*
Mike Rapoport3f08a302020-06-03 15:57:02 -07002558 * Using memblock node mappings, an architecture may initialise its
Mike Rapoportbc9331a2020-06-03 15:58:09 -07002559 * zones, allocate the backing mem_map and account for memory holes in an
2560 * architecture independent manner.
Mel Gormanc7132162006-09-27 01:49:43 -07002561 *
2562 * An architecture is expected to register range of page frames backed by
Tejun Heo0ee332c2011-12-08 10:22:09 -08002563 * physical memory with memblock_add[_node]() before calling
Mike Rapoport9691a072020-06-03 15:57:10 -07002564 * free_area_init() passing in the PFN each zone ends at. At a basic
Mel Gormanc7132162006-09-27 01:49:43 -07002565 * usage, an architecture is expected to do something like
2566 *
2567 * unsigned long max_zone_pfns[MAX_NR_ZONES] = {max_dma, max_normal_pfn,
2568 * max_highmem_pfn};
2569 * for_each_valid_physical_page_range()
Tejun Heo0ee332c2011-12-08 10:22:09 -08002570 * memblock_add_node(base, size, nid)
Mike Rapoport9691a072020-06-03 15:57:10 -07002571 * free_area_init(max_zone_pfns);
Mel Gormanc7132162006-09-27 01:49:43 -07002572 */
Mike Rapoport9691a072020-06-03 15:57:10 -07002573void free_area_init(unsigned long *max_zone_pfn);
Tejun Heo1e019792011-07-12 09:45:34 +02002574unsigned long node_map_pfn_alignment(void);
Yinghai Lu32996252009-12-15 17:59:02 -08002575unsigned long __absent_pages_in_range(int nid, unsigned long start_pfn,
2576 unsigned long end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07002577extern unsigned long absent_pages_in_range(unsigned long start_pfn,
2578 unsigned long end_pfn);
2579extern void get_pfn_range_for_nid(unsigned int nid,
2580 unsigned long *start_pfn, unsigned long *end_pfn);
2581extern unsigned long find_min_pfn_with_active_regions(void);
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08002582
Mike Rapoport3f08a302020-06-03 15:57:02 -07002583#ifndef CONFIG_NEED_MULTIPLE_NODES
Mike Rapoport6f24fbd2020-06-03 15:56:57 -07002584static inline int early_pfn_to_nid(unsigned long pfn)
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08002585{
2586 return 0;
2587}
2588#else
2589/* please see mm/page_alloc.c */
2590extern int __meminit early_pfn_to_nid(unsigned long pfn);
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08002591/* there is a per-arch backend function. */
Mel Gorman8a942fd2015-06-30 14:56:55 -07002592extern int __meminit __early_pfn_to_nid(unsigned long pfn,
2593 struct mminit_pfnnid_cache *state);
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08002594#endif
2595
Mel Gorman0e0b8642006-09-27 01:49:56 -07002596extern void set_dma_reserve(unsigned long new_dma_reserve);
Baoquan He98b57682020-12-29 15:14:37 -08002597extern void memmap_init_zone(unsigned long, int, unsigned long,
2598 unsigned long, unsigned long, enum meminit_context,
2599 struct vmem_altmap *, int migratetype);
Minchan Kimbc75d332009-06-16 15:32:48 -07002600extern void setup_per_zone_wmarks(void);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07002601extern int __meminit init_per_zone_wmark_min(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602extern void mem_init(void);
David Howells8feae132009-01-08 12:04:47 +00002603extern void __init mmap_init(void);
Michal Hocko9af744d2017-02-22 15:46:16 -08002604extern void show_mem(unsigned int flags, nodemask_t *nodemask);
Igor Redkod02bd272016-03-17 14:19:05 -07002605extern long si_mem_available(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606extern void si_meminfo(struct sysinfo * val);
2607extern void si_meminfo_node(struct sysinfo *val, int nid);
Srikar Dronamrajuf6f34b42016-10-07 16:59:15 -07002608#ifdef __HAVE_ARCH_RESERVED_KERNEL_PAGES
2609extern unsigned long arch_reserved_kernel_pages(void);
2610#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Michal Hockoa8e99252017-02-22 15:46:10 -08002612extern __printf(3, 4)
2613void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...);
Dave Hansena238ab52011-05-24 17:12:16 -07002614
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002615extern void setup_per_cpu_pageset(void);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002616
Paul Szabo75f7ad82013-02-22 16:34:42 -08002617/* page_alloc.c */
2618extern int min_free_kbytes;
Mel Gorman1c308442018-12-28 00:35:52 -08002619extern int watermark_boost_factor;
Johannes Weiner795ae7a2016-03-17 14:19:14 -07002620extern int watermark_scale_factor;
Mike Rapoport51930df2020-06-03 15:58:03 -07002621extern bool arch_has_descending_max_zone_pfns(void);
Paul Szabo75f7ad82013-02-22 16:34:42 -08002622
David Howells8feae132009-01-08 12:04:47 +00002623/* nommu.c */
David Howells33e5d7692009-04-02 16:56:32 -07002624extern atomic_long_t mmap_pages_allocated;
David Howells7e660872010-01-15 17:01:39 -08002625extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t);
David Howells8feae132009-01-08 12:04:47 +00002626
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002627/* interval_tree.c */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002628void vma_interval_tree_insert(struct vm_area_struct *node,
Davidlohr Buesof808c132017-09-08 16:15:08 -07002629 struct rb_root_cached *root);
Michel Lespinasse9826a512012-10-08 16:31:35 -07002630void vma_interval_tree_insert_after(struct vm_area_struct *node,
2631 struct vm_area_struct *prev,
Davidlohr Buesof808c132017-09-08 16:15:08 -07002632 struct rb_root_cached *root);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002633void vma_interval_tree_remove(struct vm_area_struct *node,
Davidlohr Buesof808c132017-09-08 16:15:08 -07002634 struct rb_root_cached *root);
2635struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root,
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002636 unsigned long start, unsigned long last);
2637struct vm_area_struct *vma_interval_tree_iter_next(struct vm_area_struct *node,
2638 unsigned long start, unsigned long last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002640#define vma_interval_tree_foreach(vma, root, start, last) \
2641 for (vma = vma_interval_tree_iter_first(root, start, last); \
2642 vma; vma = vma_interval_tree_iter_next(vma, start, last))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Michel Lespinassebf181b92012-10-08 16:31:39 -07002644void anon_vma_interval_tree_insert(struct anon_vma_chain *node,
Davidlohr Buesof808c132017-09-08 16:15:08 -07002645 struct rb_root_cached *root);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002646void anon_vma_interval_tree_remove(struct anon_vma_chain *node,
Davidlohr Buesof808c132017-09-08 16:15:08 -07002647 struct rb_root_cached *root);
2648struct anon_vma_chain *
2649anon_vma_interval_tree_iter_first(struct rb_root_cached *root,
2650 unsigned long start, unsigned long last);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002651struct anon_vma_chain *anon_vma_interval_tree_iter_next(
2652 struct anon_vma_chain *node, unsigned long start, unsigned long last);
Michel Lespinasseed8ea812012-10-08 16:31:45 -07002653#ifdef CONFIG_DEBUG_VM_RB
2654void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
2655#endif
Michel Lespinassebf181b92012-10-08 16:31:39 -07002656
2657#define anon_vma_interval_tree_foreach(avc, root, start, last) \
2658 for (avc = anon_vma_interval_tree_iter_first(root, start, last); \
2659 avc; avc = anon_vma_interval_tree_iter_next(avc, start, last))
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661/* mmap.c */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002662extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07002663extern int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
2664 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
Laurent Dufour05257562018-04-17 16:33:16 +02002665 struct vm_area_struct *expand, bool keep_locked);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07002666static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start,
2667 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
2668{
Laurent Dufour05257562018-04-17 16:33:16 +02002669 return __vma_adjust(vma, start, end, pgoff, insert, NULL, false);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07002670}
Laurent Dufour05257562018-04-17 16:33:16 +02002671
2672extern struct vm_area_struct *__vma_merge(struct mm_struct *mm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct vm_area_struct *prev, unsigned long addr, unsigned long end,
Laurent Dufour05257562018-04-17 16:33:16 +02002674 unsigned long vm_flags, struct anon_vma *anon, struct file *file,
2675 pgoff_t pgoff, struct mempolicy *mpol, struct vm_userfaultfd_ctx uff,
2676 const char __user *user, bool keep_locked);
2677
2678static inline struct vm_area_struct *vma_merge(struct mm_struct *mm,
2679 struct vm_area_struct *prev, unsigned long addr, unsigned long end,
2680 unsigned long vm_flags, struct anon_vma *anon, struct file *file,
2681 pgoff_t off, struct mempolicy *pol, struct vm_userfaultfd_ctx uff,
2682 const char __user *user)
2683{
2684 return __vma_merge(mm, prev, addr, end, vm_flags, anon, file, off,
2685 pol, uff, user, false);
2686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
David Rientjesdef5efe2017-02-24 14:58:47 -08002689extern int __split_vma(struct mm_struct *, struct vm_area_struct *,
2690 unsigned long addr, int new_below);
2691extern int split_vma(struct mm_struct *, struct vm_area_struct *,
2692 unsigned long addr, int new_below);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
2694extern void __vma_link_rb(struct mm_struct *, struct vm_area_struct *,
2695 struct rb_node **, struct rb_node *);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002696extern void unlink_file_vma(struct vm_area_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
Michel Lespinasse38a76012012-10-08 16:31:50 -07002698 unsigned long addr, unsigned long len, pgoff_t pgoff,
2699 bool *need_rmap_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700extern void exit_mmap(struct mm_struct *);
Matt Helsley925d1c42008-04-29 01:01:36 -07002701
Cyrill Gorcunov9c599022014-10-09 15:27:29 -07002702static inline int check_data_rlimit(unsigned long rlim,
2703 unsigned long new,
2704 unsigned long start,
2705 unsigned long end_data,
2706 unsigned long start_data)
2707{
2708 if (rlim < RLIM_INFINITY) {
2709 if (((new - start) + (end_data - start_data)) > rlim)
2710 return -ENOSPC;
2711 }
2712
2713 return 0;
2714}
2715
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002716extern int mm_take_all_locks(struct mm_struct *mm);
2717extern void mm_drop_all_locks(struct mm_struct *mm);
2718
Jiri Slaby38646012011-05-26 16:25:46 -07002719extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
2720extern struct file *get_mm_exe_file(struct mm_struct *mm);
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02002721extern struct file *get_task_exe_file(struct task_struct *task);
Matt Helsley925d1c42008-04-29 01:01:36 -07002722
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002723extern bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long npages);
2724extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages);
2725
Dmitry Safonov2eefd872016-09-05 16:33:05 +03002726extern bool vma_is_special_mapping(const struct vm_area_struct *vma,
2727 const struct vm_special_mapping *sm);
Stefani Seibold3935ed62014-03-17 23:22:02 +01002728extern struct vm_area_struct *_install_special_mapping(struct mm_struct *mm,
2729 unsigned long addr, unsigned long len,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002730 unsigned long flags,
2731 const struct vm_special_mapping *spec);
2732/* This is an obsolete alternative to _install_special_mapping. */
Roland McGrathfa5dc222007-02-08 14:20:41 -08002733extern int install_special_mapping(struct mm_struct *mm,
2734 unsigned long addr, unsigned long len,
2735 unsigned long flags, struct page **pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Alexandre Ghiti649775b2019-09-23 15:38:37 -07002737unsigned long randomize_stack_top(unsigned long stack_top);
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
2740
Miklos Szeredi0165ab42007-07-15 23:38:26 -07002741extern unsigned long mmap_region(struct file *file, unsigned long addr,
Mike Rapoport897ab3e2017-02-24 14:58:22 -08002742 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
2743 struct list_head *uf);
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07002744extern unsigned long do_mmap(struct file *file, unsigned long addr,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08002745 unsigned long len, unsigned long prot, unsigned long flags,
Peter Collingbourne45e55302020-08-06 23:23:37 -07002746 unsigned long pgoff, unsigned long *populate, struct list_head *uf);
Yang Shi85a06832018-10-26 15:08:50 -07002747extern int __do_munmap(struct mm_struct *, unsigned long, size_t,
2748 struct list_head *uf, bool downgrade);
Mike Rapoport897ab3e2017-02-24 14:58:22 -08002749extern int do_munmap(struct mm_struct *, unsigned long, size_t,
2750 struct list_head *uf);
Minchan Kim0726b012020-10-17 16:14:50 -07002751extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08002753#ifdef CONFIG_MMU
2754extern int __mm_populate(unsigned long addr, unsigned long len,
2755 int ignore_errors);
2756static inline void mm_populate(unsigned long addr, unsigned long len)
2757{
2758 /* Ignore errors */
2759 (void) __mm_populate(addr, len, 1);
2760}
2761#else
2762static inline void mm_populate(unsigned long addr, unsigned long len) {}
2763#endif
2764
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002765/* These take the mm semaphore themselves */
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002766extern int __must_check vm_brk(unsigned long, unsigned long);
Denys Vlasenko16e72e92017-02-22 15:45:16 -08002767extern int __must_check vm_brk_flags(unsigned long, unsigned long, unsigned long);
Al Virobfce2812012-04-20 21:57:04 -04002768extern int vm_munmap(unsigned long, size_t);
Michal Hocko9fbeb5a2016-05-23 16:25:30 -07002769extern unsigned long __must_check vm_mmap(struct file *, unsigned long,
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07002770 unsigned long, unsigned long,
2771 unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002773struct vm_unmapped_area_info {
2774#define VM_UNMAPPED_AREA_TOPDOWN 1
2775 unsigned long flags;
2776 unsigned long length;
2777 unsigned long low_limit;
2778 unsigned long high_limit;
2779 unsigned long align_mask;
2780 unsigned long align_offset;
2781};
2782
Jaewon Kimbaceaf12020-04-01 21:09:10 -07002783extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002784
Hugh Dickins85821aa2011-07-25 17:12:23 -07002785/* truncate.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786extern void truncate_inode_pages(struct address_space *, loff_t);
Hans Reiserd7339072006-01-06 00:10:36 -08002787extern void truncate_inode_pages_range(struct address_space *,
2788 loff_t lstart, loff_t lend);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07002789extern void truncate_inode_pages_final(struct address_space *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791/* generic vm_area_ops exported for stackable file systems */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002792extern vm_fault_t filemap_fault(struct vm_fault *vmf);
Kirill A. Shutemov0aa300a2020-12-19 15:19:23 +03002793extern vm_fault_t filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002794 pgoff_t start_pgoff, pgoff_t end_pgoff);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002795extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf);
Vinayak Menon35eacb52021-03-18 15:20:17 +05302796#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
2797extern bool filemap_allow_speculation(void);
2798#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800/* mm/page-writeback.c */
Jeff Layton2b69c822017-07-05 15:26:48 -04002801int __must_check write_one_page(struct page *page);
Nick Piggin1cf6e7d2009-02-18 14:48:18 -08002802void task_dirty_inc(struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Hugh Dickins1be71072017-06-19 04:03:24 -07002804extern unsigned long stack_guard_gap;
Michal Hockod05f3162011-05-24 17:11:44 -07002805/* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002806extern int expand_stack(struct vm_area_struct *vma, unsigned long address);
Michal Hockod05f3162011-05-24 17:11:44 -07002807
Randy Dunlap111923372020-08-11 18:32:30 -07002808/* CONFIG_STACK_GROWSUP still needs to grow downwards at some places */
Michal Hockod05f3162011-05-24 17:11:44 -07002809extern int expand_downwards(struct vm_area_struct *vma,
2810 unsigned long address);
Luck, Tony8ca3eb082010-08-24 11:44:18 -07002811#if VM_GROWSUP
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002812extern int expand_upwards(struct vm_area_struct *vma, unsigned long address);
Luck, Tony8ca3eb082010-08-24 11:44:18 -07002813#else
Linus Torvaldsfee7e492015-01-06 13:00:05 -08002814 #define expand_upwards(vma, address) (0)
Matthew Wilcox9ab88512005-11-18 16:16:42 -05002815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
2818extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
2819extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
2820 struct vm_area_struct **pprev);
2821
2822/* Look up the first VMA which intersects the interval start_addr..end_addr-1,
2823 NULL if none. Assume start_addr < end_addr. */
2824static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
2825{
2826 struct vm_area_struct * vma = find_vma(mm,start_addr);
2827
2828 if (vma && end_addr <= vma->vm_start)
2829 vma = NULL;
2830 return vma;
2831}
2832
Hugh Dickins1be71072017-06-19 04:03:24 -07002833static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
2834{
2835 unsigned long vm_start = vma->vm_start;
2836
2837 if (vma->vm_flags & VM_GROWSDOWN) {
2838 vm_start -= stack_guard_gap;
2839 if (vm_start > vma->vm_start)
2840 vm_start = 0;
2841 }
2842 return vm_start;
2843}
2844
2845static inline unsigned long vm_end_gap(struct vm_area_struct *vma)
2846{
2847 unsigned long vm_end = vma->vm_end;
2848
2849 if (vma->vm_flags & VM_GROWSUP) {
2850 vm_end += stack_guard_gap;
2851 if (vm_end < vma->vm_end)
2852 vm_end = -PAGE_SIZE;
2853 }
2854 return vm_end;
2855}
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857static inline unsigned long vma_pages(struct vm_area_struct *vma)
2858{
2859 return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
2860}
2861
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002862/* Look up the first VMA which exactly match the interval vm_start ... vm_end */
2863static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,
2864 unsigned long vm_start, unsigned long vm_end)
2865{
2866 struct vm_area_struct *vma = find_vma(mm, vm_start);
2867
2868 if (vma && (vma->vm_start != vm_start || vma->vm_end != vm_end))
2869 vma = NULL;
2870
2871 return vma;
2872}
2873
Mike Kravetz017b1662018-10-05 15:51:29 -07002874static inline bool range_in_vma(struct vm_area_struct *vma,
2875 unsigned long start, unsigned long end)
2876{
2877 return (vma && vma->vm_start <= start && end <= vma->vm_end);
2878}
2879
David Howellsbad849b2010-08-26 16:00:34 +01002880#ifdef CONFIG_MMU
Hugh Dickins804af2c2006-07-26 21:39:49 +01002881pgprot_t vm_get_page_prot(unsigned long vm_flags);
Peter Feiner64e455072014-10-13 15:55:46 -07002882void vma_set_page_prot(struct vm_area_struct *vma);
David Howellsbad849b2010-08-26 16:00:34 +01002883#else
2884static inline pgprot_t vm_get_page_prot(unsigned long vm_flags)
2885{
2886 return __pgprot(0);
2887}
Peter Feiner64e455072014-10-13 15:55:46 -07002888static inline void vma_set_page_prot(struct vm_area_struct *vma)
2889{
2890 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
2891}
David Howellsbad849b2010-08-26 16:00:34 +01002892#endif
2893
Aneesh Kumar K.V58772312013-12-06 00:08:22 +05302894#ifdef CONFIG_NUMA_BALANCING
Mel Gorman4b10e7d2012-10-25 14:16:32 +02002895unsigned long change_prot_numa(struct vm_area_struct *vma,
Lee Schermerhornb24f53a2012-10-25 14:16:32 +02002896 unsigned long start, unsigned long end);
2897#endif
2898
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07002899struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07002900int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
2901 unsigned long pfn, unsigned long size, pgprot_t);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08002902int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
Arjun Roy8cd39842020-04-10 14:33:01 -07002903int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
2904 struct page **pages, unsigned long *num);
Souptick Joardera667d742019-05-13 17:21:56 -07002905int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
2906 unsigned long num);
2907int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
2908 unsigned long num);
Matthew Wilcoxae2b01f2018-10-26 15:04:29 -07002909vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Nick Piggine0dc0d82007-02-12 00:51:36 -08002910 unsigned long pfn);
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07002911vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
Andy Lutomirski1745cbc2015-12-29 20:12:20 -08002912 unsigned long pfn, pgprot_t pgprot);
Matthew Wilcox5d747632018-10-26 15:04:10 -07002913vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
Dan Williams01c8f1c2016-01-15 16:56:40 -08002914 pfn_t pfn);
Thomas Hellstrom574c5b32019-11-22 09:25:12 +01002915vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
2916 pfn_t pfn, pgprot_t pgprot);
Souptick Joarderab77dab2018-06-07 17:04:29 -07002917vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
2918 unsigned long addr, pfn_t pfn);
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07002919int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len);
2920
Souptick Joarder1c8f4222018-04-05 16:25:23 -07002921static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma,
2922 unsigned long addr, struct page *page)
2923{
2924 int err = vm_insert_page(vma, addr, page);
2925
2926 if (err == -ENOMEM)
2927 return VM_FAULT_OOM;
2928 if (err < 0 && err != -EBUSY)
2929 return VM_FAULT_SIGBUS;
2930
2931 return VM_FAULT_NOPAGE;
2932}
2933
Jason Gunthorpef8f6ae52020-11-01 17:08:00 -08002934#ifndef io_remap_pfn_range
2935static inline int io_remap_pfn_range(struct vm_area_struct *vma,
2936 unsigned long addr, unsigned long pfn,
2937 unsigned long size, pgprot_t prot)
2938{
2939 return remap_pfn_range(vma, addr, pfn, size, pgprot_decrypted(prot));
2940}
2941#endif
2942
Souptick Joarderd97baf92018-05-18 16:08:47 -07002943static inline vm_fault_t vmf_error(int err)
2944{
2945 if (err == -ENOMEM)
2946 return VM_FAULT_OOM;
2947 return VM_FAULT_SIGBUS;
2948}
2949
Keith Buschdf06b372018-10-26 15:10:28 -07002950struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
2951 unsigned int foll_flags);
Michel Lespinasse240aade2013-02-22 16:35:56 -08002952
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07002953#define FOLL_WRITE 0x01 /* check pte is writable */
2954#define FOLL_TOUCH 0x02 /* mark page accessed */
2955#define FOLL_GET 0x04 /* do get_page on page */
Hugh Dickins8e4b9a62009-09-21 17:03:26 -07002956#define FOLL_DUMP 0x08 /* give error on hole if it would be zero */
Hugh Dickins58fa8792009-09-21 17:03:31 -07002957#define FOLL_FORCE 0x10 /* get_user_pages read/write w/o permission */
Gleb Natapov318b2752011-03-22 16:30:51 -07002958#define FOLL_NOWAIT 0x20 /* if a disk transfer is needed, start the IO
2959 * and return without waiting upon it */
Kirill A. Shutemov84d33df2015-04-14 15:44:37 -07002960#define FOLL_POPULATE 0x40 /* fault in page */
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08002961#define FOLL_SPLIT 0x80 /* don't return transhuge pages, split them */
Huang Ying69ebb832011-01-30 11:15:48 +08002962#define FOLL_HWPOISON 0x100 /* check page is hwpoisoned */
Andrea Arcangeli0b9d7052012-10-05 21:36:27 +02002963#define FOLL_NUMA 0x200 /* force NUMA hinting page fault */
Hugh Dickins5117b3b2013-02-22 16:36:07 -08002964#define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -07002965#define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */
Eric B Munsonde60f5f2015-11-05 18:51:36 -08002966#define FOLL_MLOCK 0x1000 /* lock present pages */
Dave Hansen1e987792016-02-12 13:01:54 -08002967#define FOLL_REMOTE 0x2000 /* we are working on non-current tsk/mm */
Linus Torvalds19be0ea2016-10-13 13:07:36 -07002968#define FOLL_COW 0x4000 /* internal GUP flag */
Willy Tarreau7f7ccc22018-05-11 08:11:44 +02002969#define FOLL_ANON 0x8000 /* don't do file mappings */
Ira Weiny932f4a62019-05-13 17:17:03 -07002970#define FOLL_LONGTERM 0x10000 /* mapping lifetime is indefinite: see below */
Song Liubfe7b002019-09-23 15:38:25 -07002971#define FOLL_SPLIT_PMD 0x20000 /* split huge pmd before returning */
John Hubbardf1f6a7d2020-01-30 22:13:35 -08002972#define FOLL_PIN 0x40000 /* pages must be released via unpin_user_page */
John Hubbard376a34ef2020-06-03 15:56:30 -07002973#define FOLL_FAST_ONLY 0x80000 /* gup_fast: prevent fall-back to slow gup */
Ira Weiny932f4a62019-05-13 17:17:03 -07002974
2975/*
John Hubbardeddb1c22020-01-30 22:12:54 -08002976 * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
2977 * other. Here is what they mean, and how to use them:
Ira Weiny932f4a62019-05-13 17:17:03 -07002978 *
2979 * FOLL_LONGTERM indicates that the page will be held for an indefinite time
John Hubbardeddb1c22020-01-30 22:12:54 -08002980 * period _often_ under userspace control. This is in contrast to
2981 * iov_iter_get_pages(), whose usages are transient.
Ira Weiny932f4a62019-05-13 17:17:03 -07002982 *
2983 * FIXME: For pages which are part of a filesystem, mappings are subject to the
2984 * lifetime enforced by the filesystem and we need guarantees that longterm
2985 * users like RDMA and V4L2 only establish mappings which coordinate usage with
2986 * the filesystem. Ideas for this coordination include revoking the longterm
2987 * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was
2988 * added after the problem with filesystems was found FS DAX VMAs are
2989 * specifically failed. Filesystem pages are still subject to bugs and use of
2990 * FOLL_LONGTERM should be avoided on those pages.
2991 *
2992 * FIXME: Also NOTE that FOLL_LONGTERM is not supported in every GUP call.
2993 * Currently only get_user_pages() and get_user_pages_fast() support this flag
2994 * and calls to get_user_pages_[un]locked are specifically not allowed. This
2995 * is due to an incompatibility with the FS DAX check and
John Hubbardeddb1c22020-01-30 22:12:54 -08002996 * FAULT_FLAG_ALLOW_RETRY.
Ira Weiny932f4a62019-05-13 17:17:03 -07002997 *
John Hubbardeddb1c22020-01-30 22:12:54 -08002998 * In the CMA case: long term pins in a CMA region would unnecessarily fragment
2999 * that region. And so, CMA attempts to migrate the page before pinning, when
Ira Weiny932f4a62019-05-13 17:17:03 -07003000 * FOLL_LONGTERM is specified.
John Hubbardeddb1c22020-01-30 22:12:54 -08003001 *
3002 * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
3003 * but an additional pin counting system) will be invoked. This is intended for
3004 * anything that gets a page reference and then touches page data (for example,
3005 * Direct IO). This lets the filesystem know that some non-file-system entity is
3006 * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
3007 * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
John Hubbardf1f6a7d2020-01-30 22:13:35 -08003008 * a call to unpin_user_page().
John Hubbardeddb1c22020-01-30 22:12:54 -08003009 *
3010 * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
3011 * and separate refcounting mechanisms, however, and that means that each has
3012 * its own acquire and release mechanisms:
3013 *
3014 * FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
3015 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -08003016 * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
John Hubbardeddb1c22020-01-30 22:12:54 -08003017 *
3018 * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
3019 * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
3020 * calls applied to them, and that's perfectly OK. This is a constraint on the
3021 * callers, not on the pages.)
3022 *
3023 * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
3024 * directly by the caller. That's in order to help avoid mismatches when
3025 * releasing pages: get_user_pages*() pages must be released via put_page(),
John Hubbardf1f6a7d2020-01-30 22:13:35 -08003026 * while pin_user_pages*() pages must be released via unpin_user_page().
John Hubbardeddb1c22020-01-30 22:12:54 -08003027 *
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02003028 * Please see Documentation/core-api/pin_user_pages.rst for more information.
Ira Weiny932f4a62019-05-13 17:17:03 -07003029 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Souptick Joarder2b740302018-08-23 17:01:36 -07003031static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags)
James Morse9a291a72017-06-02 14:46:46 -07003032{
3033 if (vm_fault & VM_FAULT_OOM)
3034 return -ENOMEM;
3035 if (vm_fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
3036 return (foll_flags & FOLL_HWPOISON) ? -EHWPOISON : -EFAULT;
3037 if (vm_fault & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
3038 return -EFAULT;
3039 return 0;
3040}
3041
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -07003042typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07003043extern int apply_to_page_range(struct mm_struct *mm, unsigned long address,
3044 unsigned long size, pte_fn_t fn, void *data);
Daniel Axtensbe1db472019-12-17 20:51:41 -08003045extern int apply_to_existing_page_range(struct mm_struct *mm,
3046 unsigned long address, unsigned long size,
3047 pte_fn_t fn, void *data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07003048
Vlastimil Babka0879d442020-12-14 19:13:30 -08003049extern void init_mem_debugging_and_hardening(void);
Laura Abbott8823b1d2016-03-15 14:56:27 -07003050#ifdef CONFIG_PAGE_POISONING
Vlastimil Babkae871c7f2020-12-14 19:13:34 -08003051extern void __kernel_poison_pages(struct page *page, int numpages);
3052extern void __kernel_unpoison_pages(struct page *page, int numpages);
3053extern bool _page_poisoning_enabled_early;
3054DECLARE_STATIC_KEY_FALSE(_page_poisoning_enabled);
3055static inline bool page_poisoning_enabled(void)
3056{
3057 return _page_poisoning_enabled_early;
3058}
3059/*
3060 * For use in fast paths after init_mem_debugging() has run, or when a
3061 * false negative result is not harmful when called too early.
3062 */
3063static inline bool page_poisoning_enabled_static(void)
3064{
3065 return static_branch_unlikely(&_page_poisoning_enabled);
3066}
3067static inline void kernel_poison_pages(struct page *page, int numpages)
3068{
3069 if (page_poisoning_enabled_static())
3070 __kernel_poison_pages(page, numpages);
3071}
3072static inline void kernel_unpoison_pages(struct page *page, int numpages)
3073{
3074 if (page_poisoning_enabled_static())
3075 __kernel_unpoison_pages(page, numpages);
3076}
Laura Abbott8823b1d2016-03-15 14:56:27 -07003077#else
3078static inline bool page_poisoning_enabled(void) { return false; }
Vlastimil Babkae871c7f2020-12-14 19:13:34 -08003079static inline bool page_poisoning_enabled_static(void) { return false; }
Vlastimil Babkaa2bbfa42020-12-14 19:13:38 -08003080static inline void __kernel_poison_pages(struct page *page, int nunmpages) { }
Vlastimil Babkae871c7f2020-12-14 19:13:34 -08003081static inline void kernel_poison_pages(struct page *page, int numpages) { }
3082static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
Laura Abbott8823b1d2016-03-15 14:56:27 -07003083#endif
3084
Alexander Potapenko64713842019-07-11 20:59:19 -07003085DECLARE_STATIC_KEY_FALSE(init_on_alloc);
Alexander Potapenko64713842019-07-11 20:59:19 -07003086static inline bool want_init_on_alloc(gfp_t flags)
3087{
Vlastimil Babka0879d442020-12-14 19:13:30 -08003088 if (static_branch_unlikely(&init_on_alloc))
Alexander Potapenko64713842019-07-11 20:59:19 -07003089 return true;
3090 return flags & __GFP_ZERO;
3091}
3092
Alexander Potapenko64713842019-07-11 20:59:19 -07003093DECLARE_STATIC_KEY_FALSE(init_on_free);
Alexander Potapenko64713842019-07-11 20:59:19 -07003094static inline bool want_init_on_free(void)
3095{
Vlastimil Babka0879d442020-12-14 19:13:30 -08003096 return static_branch_unlikely(&init_on_free);
Alexander Potapenko64713842019-07-11 20:59:19 -07003097}
3098
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08003099extern bool _debug_pagealloc_enabled_early;
3100DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
Joonsoo Kim031bc572014-12-12 16:55:52 -08003101
3102static inline bool debug_pagealloc_enabled(void)
3103{
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08003104 return IS_ENABLED(CONFIG_DEBUG_PAGEALLOC) &&
3105 _debug_pagealloc_enabled_early;
3106}
3107
3108/*
3109 * For use in fast paths after init_debug_pagealloc() has run, or when a
3110 * false negative result is not harmful when called too early.
3111 */
3112static inline bool debug_pagealloc_enabled_static(void)
3113{
Vlastimil Babka96a2b032019-07-11 20:55:06 -07003114 if (!IS_ENABLED(CONFIG_DEBUG_PAGEALLOC))
3115 return false;
3116
3117 return static_branch_unlikely(&_debug_pagealloc_enabled);
Joonsoo Kim031bc572014-12-12 16:55:52 -08003118}
3119
Rick Edgecombed6332692019-04-25 17:11:35 -07003120#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_ARCH_HAS_SET_DIRECT_MAP)
3121extern void __kernel_map_pages(struct page *page, int numpages, int enable);
3122
Vlastimil Babkac87cbc12020-03-05 22:28:42 -08003123/*
3124 * When called in DEBUG_PAGEALLOC context, the call should most likely be
3125 * guarded by debug_pagealloc_enabled() or debug_pagealloc_enabled_static()
3126 */
Joonsoo Kim031bc572014-12-12 16:55:52 -08003127static inline void
3128kernel_map_pages(struct page *page, int numpages, int enable)
3129{
Joonsoo Kim031bc572014-12-12 16:55:52 -08003130 __kernel_map_pages(page, numpages, enable);
3131}
Mike Rapoport9538c5a2020-12-14 19:10:20 -08003132
3133static inline void debug_pagealloc_map_pages(struct page *page, int numpages)
3134{
3135 if (debug_pagealloc_enabled_static())
3136 __kernel_map_pages(page, numpages, 1);
3137}
3138
3139static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages)
3140{
3141 if (debug_pagealloc_enabled_static())
3142 __kernel_map_pages(page, numpages, 0);
3143}
3144
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01003145#ifdef CONFIG_HIBERNATION
3146extern bool kernel_page_present(struct page *page);
Joonsoo Kim40b44132016-03-15 14:54:21 -07003147#endif /* CONFIG_HIBERNATION */
Rick Edgecombed6332692019-04-25 17:11:35 -07003148#else /* CONFIG_DEBUG_PAGEALLOC || CONFIG_ARCH_HAS_SET_DIRECT_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149static inline void
Nick Piggin9858db52006-10-11 01:21:30 -07003150kernel_map_pages(struct page *page, int numpages, int enable) {}
Mike Rapoport9538c5a2020-12-14 19:10:20 -08003151static inline void debug_pagealloc_map_pages(struct page *page, int numpages) {}
3152static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages) {}
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01003153#ifdef CONFIG_HIBERNATION
3154static inline bool kernel_page_present(struct page *page) { return true; }
Joonsoo Kim40b44132016-03-15 14:54:21 -07003155#endif /* CONFIG_HIBERNATION */
Rick Edgecombed6332692019-04-25 17:11:35 -07003156#endif /* CONFIG_DEBUG_PAGEALLOC || CONFIG_ARCH_HAS_SET_DIRECT_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Andy Lutomirskia6c19df2014-08-08 14:23:40 -07003158#ifdef __HAVE_ARCH_GATE_AREA
Stephen Wilson31db58b2011-03-13 15:49:15 -04003159extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
Andy Lutomirskia6c19df2014-08-08 14:23:40 -07003160extern int in_gate_area_no_mm(unsigned long addr);
3161extern int in_gate_area(struct mm_struct *mm, unsigned long addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162#else
Andy Lutomirskia6c19df2014-08-08 14:23:40 -07003163static inline struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
3164{
3165 return NULL;
3166}
3167static inline int in_gate_area_no_mm(unsigned long addr) { return 0; }
3168static inline int in_gate_area(struct mm_struct *mm, unsigned long addr)
3169{
3170 return 0;
3171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172#endif /* __HAVE_ARCH_GATE_AREA */
3173
Michal Hocko44a70ade2016-07-28 15:44:43 -07003174extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm);
3175
Josh Triplett146732c2013-04-29 15:07:22 -07003176#ifdef CONFIG_SYSCTL
3177extern int sysctl_drop_caches;
Christoph Hellwig32927392020-04-24 08:43:38 +02003178int drop_caches_sysctl_handler(struct ctl_table *, int, void *, size_t *,
3179 loff_t *);
Josh Triplett146732c2013-04-29 15:07:22 -07003180#endif
3181
Vladimir Davydovcb731d62015-02-12 14:58:54 -08003182void drop_slab(void);
3183void drop_slab_node(int nid);
Andrew Morton9d0243b2006-01-08 01:00:39 -08003184
Luke Yang7a9166e2006-02-20 18:28:07 -08003185#ifndef CONFIG_MMU
3186#define randomize_va_space 0
3187#else
Andi Kleena62eaf12006-02-16 23:41:58 +01003188extern int randomize_va_space;
Luke Yang7a9166e2006-02-20 18:28:07 -08003189#endif
Andi Kleena62eaf12006-02-16 23:41:58 +01003190
Sam Ravnborg045e72a2007-07-26 10:41:13 -07003191const char * arch_vma_name(struct vm_area_struct *vma);
Christoph Hellwig89165b82019-07-16 16:26:30 -07003192#ifdef CONFIG_MMU
Andi Kleen03252912008-01-30 13:33:18 +01003193void print_vma_addr(char *prefix, unsigned long rip);
Christoph Hellwig89165b82019-07-16 16:26:30 -07003194#else
3195static inline void print_vma_addr(char *prefix, unsigned long rip)
3196{
3197}
3198#endif
Ingo Molnare6e54942006-06-27 02:53:50 -07003199
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -07003200void *sparse_buffer_alloc(unsigned long size);
Dan Williamse9c0a3f02019-07-18 15:58:11 -07003201struct page * __populate_section_memmap(unsigned long pfn,
3202 unsigned long nr_pages, int nid, struct vmem_altmap *altmap);
Andy Whitcroft29c71112007-10-16 01:24:14 -07003203pgd_t *vmemmap_pgd_populate(unsigned long addr, int node);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03003204p4d_t *vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node);
3205pud_t *vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node);
Andy Whitcroft29c71112007-10-16 01:24:14 -07003206pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node);
Anshuman Khandual1d9cfee2020-08-06 23:23:19 -07003207pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
3208 struct vmem_altmap *altmap);
Christoph Lameter8f6aac42007-10-16 01:24:13 -07003209void *vmemmap_alloc_block(unsigned long size, int node);
Dan Williams4b94ffd2016-01-15 16:56:22 -08003210struct vmem_altmap;
Anshuman Khandual56993b42020-08-06 23:23:24 -07003211void *vmemmap_alloc_block_buf(unsigned long size, int node,
3212 struct vmem_altmap *altmap);
Christoph Lameter8f6aac42007-10-16 01:24:13 -07003213void vmemmap_verify(pte_t *, int, unsigned long, unsigned long);
Johannes Weiner0aad8182013-04-29 15:07:50 -07003214int vmemmap_populate_basepages(unsigned long start, unsigned long end,
Anshuman Khandual1d9cfee2020-08-06 23:23:19 -07003215 int node, struct vmem_altmap *altmap);
Christoph Hellwig7b73d972017-12-29 08:53:54 +01003216int vmemmap_populate(unsigned long start, unsigned long end, int node,
3217 struct vmem_altmap *altmap);
Yinghai Luc2b91e22008-04-12 01:19:24 -07003218void vmemmap_populate_print_last(void);
Tang Chen01975182013-02-22 16:33:08 -08003219#ifdef CONFIG_MEMORY_HOTPLUG
Christoph Hellwig24b6d412017-12-29 08:53:56 +01003220void vmemmap_free(unsigned long start, unsigned long end,
3221 struct vmem_altmap *altmap);
Tang Chen01975182013-02-22 16:33:08 -08003222#endif
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08003223void register_page_bootmem_memmap(unsigned long section_nr, struct page *map,
Baoquan He15670bf2017-10-28 09:30:38 +08003224 unsigned long nr_pages);
Andi Kleen6a460792009-09-16 11:50:15 +02003225
Andi Kleen82ba0112009-12-16 12:19:57 +01003226enum mf_flags {
3227 MF_COUNT_INCREASED = 1 << 0,
Tony Luck7329bbe2011-12-13 09:27:58 -08003228 MF_ACTION_REQUIRED = 1 << 1,
Tony Luck6751ed62012-07-11 10:20:47 -07003229 MF_MUST_KILL = 1 << 2,
Naveen N. Raocf870c72013-07-10 14:57:01 +05303230 MF_SOFT_OFFLINE = 1 << 3,
Andi Kleen82ba0112009-12-16 12:19:57 +01003231};
Eric W. Biederman83b57532017-07-09 18:14:01 -05003232extern int memory_failure(unsigned long pfn, int flags);
3233extern void memory_failure_queue(unsigned long pfn, int flags);
James Morse06202232020-05-01 17:45:41 +01003234extern void memory_failure_queue_kick(int cpu);
Wu Fengguang847ce402009-12-16 12:19:58 +01003235extern int unpoison_memory(unsigned long pfn);
Andi Kleen6a460792009-09-16 11:50:15 +02003236extern int sysctl_memory_failure_early_kill;
3237extern int sysctl_memory_failure_recovery;
Andi Kleenfacb6012009-12-16 12:20:00 +01003238extern void shake_page(struct page *p, int access);
Guenter Roeck5844a482018-04-05 16:24:32 -07003239extern atomic_long_t num_poisoned_pages __read_mostly;
Naoya Horiguchifeec24a2019-11-30 17:53:38 -08003240extern int soft_offline_page(unsigned long pfn, int flags);
Andi Kleen6a460792009-09-16 11:50:15 +02003241
Xie XiuQicc637b12015-06-24 16:57:30 -07003242
3243/*
3244 * Error handlers for various types of pages.
3245 */
Xie XiuQicc3e2af2015-06-24 16:57:33 -07003246enum mf_result {
Xie XiuQicc637b12015-06-24 16:57:30 -07003247 MF_IGNORED, /* Error: cannot be handled */
3248 MF_FAILED, /* Error: handling failed */
3249 MF_DELAYED, /* Will be handled later */
3250 MF_RECOVERED, /* Successfully recovered */
3251};
3252
3253enum mf_action_page_type {
3254 MF_MSG_KERNEL,
3255 MF_MSG_KERNEL_HIGH_ORDER,
3256 MF_MSG_SLAB,
3257 MF_MSG_DIFFERENT_COMPOUND,
3258 MF_MSG_POISONED_HUGE,
3259 MF_MSG_HUGE,
3260 MF_MSG_FREE_HUGE,
Naoya Horiguchi31286a82018-04-05 16:23:05 -07003261 MF_MSG_NON_PMD_HUGE,
Xie XiuQicc637b12015-06-24 16:57:30 -07003262 MF_MSG_UNMAP_FAILED,
3263 MF_MSG_DIRTY_SWAPCACHE,
3264 MF_MSG_CLEAN_SWAPCACHE,
3265 MF_MSG_DIRTY_MLOCKED_LRU,
3266 MF_MSG_CLEAN_MLOCKED_LRU,
3267 MF_MSG_DIRTY_UNEVICTABLE_LRU,
3268 MF_MSG_CLEAN_UNEVICTABLE_LRU,
3269 MF_MSG_DIRTY_LRU,
3270 MF_MSG_CLEAN_LRU,
3271 MF_MSG_TRUNCATED_LRU,
3272 MF_MSG_BUDDY,
3273 MF_MSG_BUDDY_2ND,
Dan Williams6100e342018-07-13 21:50:21 -07003274 MF_MSG_DAX,
Naoya Horiguchi5d1fd5d2020-10-15 20:07:21 -07003275 MF_MSG_UNSPLIT_THP,
Xie XiuQicc637b12015-06-24 16:57:30 -07003276 MF_MSG_UNKNOWN,
3277};
3278
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003279#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
3280extern void clear_huge_page(struct page *page,
Huang Yingc79b57e2017-09-06 16:25:04 -07003281 unsigned long addr_hint,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003282 unsigned int pages_per_huge_page);
3283extern void copy_user_huge_page(struct page *dst, struct page *src,
Huang Yingc9f4cd72018-08-17 15:45:49 -07003284 unsigned long addr_hint,
3285 struct vm_area_struct *vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003286 unsigned int pages_per_huge_page);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08003287extern long copy_huge_page_from_user(struct page *dst_page,
3288 const void __user *usr_src,
Mike Kravetz810a56b2017-02-22 15:42:58 -08003289 unsigned int pages_per_huge_page,
3290 bool allow_pagefault);
Thomas Hellstrom (VMware)2484ca92020-03-24 18:47:17 +01003291
3292/**
3293 * vma_is_special_huge - Are transhuge page-table entries considered special?
3294 * @vma: Pointer to the struct vm_area_struct to consider
3295 *
3296 * Whether transhuge page-table entries are considered "special" following
3297 * the definition in vm_normal_page().
3298 *
3299 * Return: true if transhuge page-table entries should be considered special,
3300 * false otherwise.
3301 */
3302static inline bool vma_is_special_huge(const struct vm_area_struct *vma)
3303{
3304 return vma_is_dax(vma) || (vma->vm_file &&
3305 (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)));
3306}
3307
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003308#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
3309
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08003310#ifdef CONFIG_DEBUG_PAGEALLOC
3311extern unsigned int _debug_guardpage_minorder;
Vlastimil Babka96a2b032019-07-11 20:55:06 -07003312DECLARE_STATIC_KEY_FALSE(_debug_guardpage_enabled);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08003313
3314static inline unsigned int debug_guardpage_minorder(void)
3315{
3316 return _debug_guardpage_minorder;
3317}
3318
Joonsoo Kime30825f2014-12-12 16:55:49 -08003319static inline bool debug_guardpage_enabled(void)
3320{
Vlastimil Babka96a2b032019-07-11 20:55:06 -07003321 return static_branch_unlikely(&_debug_guardpage_enabled);
Joonsoo Kime30825f2014-12-12 16:55:49 -08003322}
3323
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08003324static inline bool page_is_guard(struct page *page)
3325{
Joonsoo Kime30825f2014-12-12 16:55:49 -08003326 if (!debug_guardpage_enabled())
3327 return false;
3328
Vlastimil Babka3972f6b2019-07-11 20:55:13 -07003329 return PageGuard(page);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08003330}
3331#else
3332static inline unsigned int debug_guardpage_minorder(void) { return 0; }
Joonsoo Kime30825f2014-12-12 16:55:49 -08003333static inline bool debug_guardpage_enabled(void) { return false; }
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08003334static inline bool page_is_guard(struct page *page) { return false; }
3335#endif /* CONFIG_DEBUG_PAGEALLOC */
3336
Cody P Schaferf9872ca2013-04-29 15:08:01 -07003337#if MAX_NUMNODES > 1
3338void __init setup_nr_node_ids(void);
3339#else
3340static inline void setup_nr_node_ids(void) {}
3341#endif
3342
Song Liu010c1642019-09-23 15:38:19 -07003343extern int memcmp_pages(struct page *page1, struct page *page2);
3344
3345static inline int pages_identical(struct page *page1, struct page *page2)
3346{
3347 return !memcmp_pages(page1, page2);
3348}
3349
Thomas Hellstromc5acad82019-03-19 13:12:30 +01003350#ifdef CONFIG_MAPPING_DIRTY_HELPERS
3351unsigned long clean_record_shared_mapping_range(struct address_space *mapping,
3352 pgoff_t first_index, pgoff_t nr,
3353 pgoff_t bitmap_pgoff,
3354 unsigned long *bitmap,
3355 pgoff_t *start,
3356 pgoff_t *end);
3357
3358unsigned long wp_shared_mapping_range(struct address_space *mapping,
3359 pgoff_t first_index, pgoff_t nr);
3360#endif
3361
Christoph Hellwig2374c092020-04-24 08:43:36 +02003362extern int sysctl_nr_trim_pages;
Vinayak Menon35eacb52021-03-18 15:20:17 +05303363extern bool pte_map_lock_addr(struct vm_fault *vmf, unsigned long addr);
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05303364extern int reclaim_shmem_address_space(struct address_space *mapping);
3365extern int reclaim_pages_from_list(struct list_head *page_list);
Christoph Hellwig2374c092020-04-24 08:43:36 +02003366
Peter Xu01486862021-05-14 17:27:04 -07003367/**
3368 * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it
3369 * @seals: the seals to check
3370 * @vma: the vma to operate on
3371 *
3372 * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on
3373 * the vma flags. Return 0 if check pass, or <0 for errors.
3374 */
3375static inline int seal_check_future_write(int seals, struct vm_area_struct *vma)
3376{
3377 if (seals & F_SEAL_FUTURE_WRITE) {
3378 /*
3379 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
3380 * "future write" seal active.
3381 */
3382 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
3383 return -EPERM;
3384
3385 /*
3386 * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as
3387 * MAP_SHARED and read-only, take care to not allow mprotect to
3388 * revert protections on such mappings. Do this only for shared
3389 * mappings. For private mappings, don't need to mask
3390 * VM_MAYWRITE as we still want them to be COW-writable.
3391 */
3392 if (vma->vm_flags & VM_SHARED)
3393 vma->vm_flags &= ~(VM_MAYWRITE);
3394 }
3395
3396 return 0;
3397}
3398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399#endif /* __KERNEL__ */
3400#endif /* _LINUX_MM_H */