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