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