Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mm/mmap.c |
| 3 | * |
| 4 | * Written by obz. |
| 5 | * |
Alan Cox | 046c688 | 2009-01-05 14:06:29 +0000 | [diff] [blame] | 6 | * Address space accounting code <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Mitchel Humpherys | b1de0d1 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Cyril Hrubis | e8420a8 | 2013-04-29 15:08:33 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> |
Alexey Dobriyan | 4af3c9c | 2007-10-16 23:29:23 -0700 | [diff] [blame] | 13 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/mm.h> |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 15 | #include <linux/vmacache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/shm.h> |
| 17 | #include <linux/mman.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/swap.h> |
| 20 | #include <linux/syscalls.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 21 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/file.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/personality.h> |
| 26 | #include <linux/security.h> |
| 27 | #include <linux/hugetlb.h> |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 28 | #include <linux/shmem_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/profile.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 30 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/mount.h> |
| 32 | #include <linux/mempolicy.h> |
| 33 | #include <linux/rmap.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 34 | #include <linux/mmu_notifier.h> |
Konstantin Khlebnikov | 82f71ae | 2014-08-06 16:06:36 -0700 | [diff] [blame] | 35 | #include <linux/mmdebug.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 36 | #include <linux/perf_event.h> |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 37 | #include <linux/audit.h> |
Andrea Arcangeli | b15d00b | 2011-01-13 15:46:59 -0800 | [diff] [blame] | 38 | #include <linux/khugepaged.h> |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 39 | #include <linux/uprobes.h> |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 40 | #include <linux/rbtree_augmented.h> |
Andrew Shewmaker | 1640879 | 2013-04-29 15:08:12 -0700 | [diff] [blame] | 41 | #include <linux/notifier.h> |
| 42 | #include <linux/memory.h> |
Mitchel Humpherys | b1de0d1 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 43 | #include <linux/printk.h> |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 44 | #include <linux/userfaultfd_k.h> |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 45 | #include <linux/moduleparam.h> |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 46 | #include <linux/pkeys.h> |
Andrea Arcangeli | 07b039e9 | 2017-09-06 16:25:00 -0700 | [diff] [blame] | 47 | #include <linux/oom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #include <asm/uaccess.h> |
| 50 | #include <asm/cacheflush.h> |
| 51 | #include <asm/tlb.h> |
Jeremy Fitzhardinge | d6dd61c | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 52 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 54 | #include "internal.h" |
| 55 | |
Kirill Korotaev | 3a45975 | 2006-09-07 14:17:04 +0400 | [diff] [blame] | 56 | #ifndef arch_mmap_check |
| 57 | #define arch_mmap_check(addr, len, flags) (0) |
| 58 | #endif |
| 59 | |
Daniel Cashman | d07e225 | 2016-01-14 15:19:53 -0800 | [diff] [blame] | 60 | #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS |
| 61 | const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN; |
| 62 | const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX; |
| 63 | int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS; |
| 64 | #endif |
| 65 | #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS |
| 66 | const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN; |
| 67 | const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX; |
| 68 | int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS; |
| 69 | #endif |
| 70 | |
Konstantin Khlebnikov | f4fcd55 | 2016-05-20 16:57:45 -0700 | [diff] [blame] | 71 | static bool ignore_rlimit_data; |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 72 | core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); |
Daniel Cashman | d07e225 | 2016-01-14 15:19:53 -0800 | [diff] [blame] | 73 | |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 74 | static void unmap_region(struct mm_struct *mm, |
| 75 | struct vm_area_struct *vma, struct vm_area_struct *prev, |
| 76 | unsigned long start, unsigned long end); |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* description of effects of mapping type and prot in current implementation. |
| 79 | * this is due to the limited x86 page protection hardware. The expected |
| 80 | * behavior is in parens: |
| 81 | * |
| 82 | * map_type prot |
| 83 | * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC |
| 84 | * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes |
| 85 | * w: (no) no w: (no) no w: (yes) yes w: (no) no |
| 86 | * x: (no) no x: (no) yes x: (no) yes x: (yes) yes |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 87 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes |
| 89 | * w: (no) no w: (no) no w: (copy) copy w: (no) no |
| 90 | * x: (no) no x: (no) yes x: (no) yes x: (yes) yes |
| 91 | * |
Catalin Marinas | cab15ce | 2016-08-11 18:44:50 +0100 | [diff] [blame] | 92 | * On arm64, PROT_EXEC has the following behaviour for both MAP_SHARED and |
| 93 | * MAP_PRIVATE: |
| 94 | * r: (no) no |
| 95 | * w: (no) no |
| 96 | * x: (yes) yes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | */ |
| 98 | pgprot_t protection_map[16] = { |
| 99 | __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111, |
| 100 | __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111 |
| 101 | }; |
| 102 | |
Hugh Dickins | 804af2c | 2006-07-26 21:39:49 +0100 | [diff] [blame] | 103 | pgprot_t vm_get_page_prot(unsigned long vm_flags) |
| 104 | { |
Dave Kleikamp | b845f31 | 2008-07-08 00:28:51 +1000 | [diff] [blame] | 105 | return __pgprot(pgprot_val(protection_map[vm_flags & |
| 106 | (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) | |
| 107 | pgprot_val(arch_vm_get_page_prot(vm_flags))); |
Hugh Dickins | 804af2c | 2006-07-26 21:39:49 +0100 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL(vm_get_page_prot); |
| 110 | |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 111 | static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) |
| 112 | { |
| 113 | return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); |
| 114 | } |
| 115 | |
| 116 | /* Update vma->vm_page_prot to reflect vma->vm_flags. */ |
| 117 | void vma_set_page_prot(struct vm_area_struct *vma) |
| 118 | { |
| 119 | unsigned long vm_flags = vma->vm_flags; |
Andrea Arcangeli | 6d2329f | 2016-10-07 17:01:22 -0700 | [diff] [blame] | 120 | pgprot_t vm_page_prot; |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 121 | |
Andrea Arcangeli | 6d2329f | 2016-10-07 17:01:22 -0700 | [diff] [blame] | 122 | vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags); |
| 123 | if (vma_wants_writenotify(vma, vm_page_prot)) { |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 124 | vm_flags &= ~VM_SHARED; |
Andrea Arcangeli | 6d2329f | 2016-10-07 17:01:22 -0700 | [diff] [blame] | 125 | vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags); |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 126 | } |
Andrea Arcangeli | 6d2329f | 2016-10-07 17:01:22 -0700 | [diff] [blame] | 127 | /* remove_protection_ptes reads vma->vm_page_prot without mmap_sem */ |
| 128 | WRITE_ONCE(vma->vm_page_prot, vm_page_prot); |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 132 | * Requires inode->i_mapping->i_mmap_rwsem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | */ |
| 134 | static void __remove_shared_vm_struct(struct vm_area_struct *vma, |
| 135 | struct file *file, struct address_space *mapping) |
| 136 | { |
| 137 | if (vma->vm_flags & VM_DENYWRITE) |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 138 | atomic_inc(&file_inode(file)->i_writecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (vma->vm_flags & VM_SHARED) |
David Herrmann | 4bb5f5d | 2014-08-08 14:25:25 -0700 | [diff] [blame] | 140 | mapping_unmap_writable(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | flush_dcache_mmap_lock(mapping); |
Kirill A. Shutemov | 27ba064 | 2015-02-10 14:09:59 -0800 | [diff] [blame] | 143 | vma_interval_tree_remove(vma, &mapping->i_mmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | flush_dcache_mmap_unlock(mapping); |
| 145 | } |
| 146 | |
| 147 | /* |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 148 | * Unlink a file-based vm structure from its interval tree, to hide |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 149 | * vma from rmap and vmtruncate before freeing its page tables. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | */ |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 151 | void unlink_file_vma(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
| 153 | struct file *file = vma->vm_file; |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (file) { |
| 156 | struct address_space *mapping = file->f_mapping; |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 157 | i_mmap_lock_write(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | __remove_shared_vm_struct(vma, file, mapping); |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 159 | i_mmap_unlock_write(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 163 | static void __free_vma(struct vm_area_struct *vma) |
| 164 | { |
| 165 | if (vma->vm_file) |
| 166 | fput(vma->vm_file); |
| 167 | mpol_put(vma_policy(vma)); |
| 168 | kmem_cache_free(vm_area_cachep, vma); |
| 169 | } |
| 170 | |
| 171 | #ifdef CONFIG_SPECULATIVE_PAGE_FAULT |
| 172 | void put_vma(struct vm_area_struct *vma) |
| 173 | { |
| 174 | if (atomic_dec_and_test(&vma->vm_ref_count)) |
| 175 | __free_vma(vma); |
| 176 | } |
| 177 | #else |
| 178 | static inline void put_vma(struct vm_area_struct *vma) |
| 179 | { |
| 180 | __free_vma(vma); |
| 181 | } |
| 182 | #endif |
| 183 | |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 184 | /* |
| 185 | * Close a vm structure and free it, returning the next. |
| 186 | */ |
| 187 | static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) |
| 188 | { |
| 189 | struct vm_area_struct *next = vma->vm_next; |
| 190 | |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 191 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (vma->vm_ops && vma->vm_ops->close) |
| 193 | vma->vm_ops->close(vma); |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 194 | put_vma(vma); |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 195 | return next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 198 | static int do_brk(unsigned long addr, unsigned long len); |
Linus Torvalds | e4eb1ff | 2012-04-20 15:35:40 -0700 | [diff] [blame] | 199 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 200 | SYSCALL_DEFINE1(brk, unsigned long, brk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Cyrill Gorcunov | 8764b33 | 2014-10-09 15:27:32 -0700 | [diff] [blame] | 202 | unsigned long retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | unsigned long newbrk, oldbrk; |
| 204 | struct mm_struct *mm = current->mm; |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 205 | struct vm_area_struct *next; |
Jiri Kosina | a5b4592 | 2008-06-05 22:46:05 -0700 | [diff] [blame] | 206 | unsigned long min_brk; |
Michel Lespinasse | 128557f | 2013-02-22 16:32:40 -0800 | [diff] [blame] | 207 | bool populate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Michal Hocko | dc0ef0d | 2016-05-23 16:25:27 -0700 | [diff] [blame] | 209 | if (down_write_killable(&mm->mmap_sem)) |
| 210 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Jiri Kosina | a5b4592 | 2008-06-05 22:46:05 -0700 | [diff] [blame] | 212 | #ifdef CONFIG_COMPAT_BRK |
Jiri Kosina | 5520e89 | 2011-01-13 15:47:23 -0800 | [diff] [blame] | 213 | /* |
| 214 | * CONFIG_COMPAT_BRK can still be overridden by setting |
| 215 | * randomize_va_space to 2, which will still cause mm->start_brk |
| 216 | * to be arbitrarily shifted |
| 217 | */ |
Jiri Kosina | 4471a67 | 2011-04-14 15:22:09 -0700 | [diff] [blame] | 218 | if (current->brk_randomized) |
Jiri Kosina | 5520e89 | 2011-01-13 15:47:23 -0800 | [diff] [blame] | 219 | min_brk = mm->start_brk; |
| 220 | else |
| 221 | min_brk = mm->end_data; |
Jiri Kosina | a5b4592 | 2008-06-05 22:46:05 -0700 | [diff] [blame] | 222 | #else |
| 223 | min_brk = mm->start_brk; |
| 224 | #endif |
| 225 | if (brk < min_brk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | goto out; |
Ram Gupta | 1e62419 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * Check against rlimit here. If this check is done later after the test |
| 230 | * of oldbrk with newbrk then it can escape the test and let the data |
| 231 | * segment grow beyond its set limit the in case where the limit is |
| 232 | * not page aligned -Ram Gupta |
| 233 | */ |
Cyrill Gorcunov | 8764b33 | 2014-10-09 15:27:32 -0700 | [diff] [blame] | 234 | if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, |
| 235 | mm->end_data, mm->start_data)) |
Ram Gupta | 1e62419 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 236 | goto out; |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | newbrk = PAGE_ALIGN(brk); |
| 239 | oldbrk = PAGE_ALIGN(mm->brk); |
| 240 | if (oldbrk == newbrk) |
| 241 | goto set_brk; |
| 242 | |
| 243 | /* Always allow shrinking brk. */ |
| 244 | if (brk <= mm->brk) { |
| 245 | if (!do_munmap(mm, newbrk, oldbrk-newbrk)) |
| 246 | goto set_brk; |
| 247 | goto out; |
| 248 | } |
| 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | /* Check against existing mmap mappings. */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 251 | next = find_vma(mm, oldbrk); |
| 252 | if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | goto out; |
| 254 | |
| 255 | /* Ok, looks good - let it rip. */ |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 256 | if (do_brk(oldbrk, newbrk-oldbrk) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto out; |
Michel Lespinasse | 128557f | 2013-02-22 16:32:40 -0800 | [diff] [blame] | 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | set_brk: |
| 260 | mm->brk = brk; |
Michel Lespinasse | 128557f | 2013-02-22 16:32:40 -0800 | [diff] [blame] | 261 | populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; |
| 262 | up_write(&mm->mmap_sem); |
| 263 | if (populate) |
| 264 | mm_populate(oldbrk, newbrk - oldbrk); |
| 265 | return brk; |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | out: |
| 268 | retval = mm->brk; |
| 269 | up_write(&mm->mmap_sem); |
| 270 | return retval; |
| 271 | } |
| 272 | |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 273 | static long vma_compute_subtree_gap(struct vm_area_struct *vma) |
| 274 | { |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 275 | unsigned long max, prev_end, subtree_gap; |
| 276 | |
| 277 | /* |
| 278 | * Note: in the rare case of a VM_GROWSDOWN above a VM_GROWSUP, we |
| 279 | * allow two stack_guard_gaps between them here, and when choosing |
| 280 | * an unmapped area; whereas when expanding we only require one. |
| 281 | * That's a little inconsistent, but keeps the code here simpler. |
| 282 | */ |
| 283 | max = vm_start_gap(vma); |
| 284 | if (vma->vm_prev) { |
| 285 | prev_end = vm_end_gap(vma->vm_prev); |
| 286 | if (max > prev_end) |
| 287 | max -= prev_end; |
| 288 | else |
| 289 | max = 0; |
| 290 | } |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 291 | if (vma->vm_rb.rb_left) { |
| 292 | subtree_gap = rb_entry(vma->vm_rb.rb_left, |
| 293 | struct vm_area_struct, vm_rb)->rb_subtree_gap; |
| 294 | if (subtree_gap > max) |
| 295 | max = subtree_gap; |
| 296 | } |
| 297 | if (vma->vm_rb.rb_right) { |
| 298 | subtree_gap = rb_entry(vma->vm_rb.rb_right, |
| 299 | struct vm_area_struct, vm_rb)->rb_subtree_gap; |
| 300 | if (subtree_gap > max) |
| 301 | max = subtree_gap; |
| 302 | } |
| 303 | return max; |
| 304 | } |
| 305 | |
Michel Lespinasse | ed8ea81 | 2012-10-08 16:31:45 -0700 | [diff] [blame] | 306 | #ifdef CONFIG_DEBUG_VM_RB |
Andrea Arcangeli | acf128d | 2016-02-05 15:36:13 -0800 | [diff] [blame] | 307 | static int browse_rb(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Andrea Arcangeli | acf128d | 2016-02-05 15:36:13 -0800 | [diff] [blame] | 309 | struct rb_root *root = &mm->mm_rb; |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 310 | int i = 0, j, bug = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | struct rb_node *nd, *pn = NULL; |
| 312 | unsigned long prev = 0, pend = 0; |
| 313 | |
| 314 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 315 | struct vm_area_struct *vma; |
| 316 | vma = rb_entry(nd, struct vm_area_struct, vm_rb); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 317 | if (vma->vm_start < prev) { |
Andrew Morton | ff26f70 | 2014-10-09 15:28:19 -0700 | [diff] [blame] | 318 | pr_emerg("vm_start %lx < prev %lx\n", |
| 319 | vma->vm_start, prev); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 320 | bug = 1; |
| 321 | } |
| 322 | if (vma->vm_start < pend) { |
Andrew Morton | ff26f70 | 2014-10-09 15:28:19 -0700 | [diff] [blame] | 323 | pr_emerg("vm_start %lx < pend %lx\n", |
| 324 | vma->vm_start, pend); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 325 | bug = 1; |
| 326 | } |
| 327 | if (vma->vm_start > vma->vm_end) { |
Andrew Morton | ff26f70 | 2014-10-09 15:28:19 -0700 | [diff] [blame] | 328 | pr_emerg("vm_start %lx > vm_end %lx\n", |
| 329 | vma->vm_start, vma->vm_end); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 330 | bug = 1; |
| 331 | } |
Andrea Arcangeli | acf128d | 2016-02-05 15:36:13 -0800 | [diff] [blame] | 332 | spin_lock(&mm->page_table_lock); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 333 | if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) { |
Sasha Levin | 8542bdf | 2014-09-09 14:50:59 -0700 | [diff] [blame] | 334 | pr_emerg("free gap %lx, correct %lx\n", |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 335 | vma->rb_subtree_gap, |
| 336 | vma_compute_subtree_gap(vma)); |
| 337 | bug = 1; |
| 338 | } |
Andrea Arcangeli | acf128d | 2016-02-05 15:36:13 -0800 | [diff] [blame] | 339 | spin_unlock(&mm->page_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | i++; |
| 341 | pn = nd; |
David Miller | d1af65d | 2007-02-28 20:13:13 -0800 | [diff] [blame] | 342 | prev = vma->vm_start; |
| 343 | pend = vma->vm_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | j = 0; |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 346 | for (nd = pn; nd; nd = rb_prev(nd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | j++; |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 348 | if (i != j) { |
Sasha Levin | 8542bdf | 2014-09-09 14:50:59 -0700 | [diff] [blame] | 349 | pr_emerg("backwards %d, forwards %d\n", j, i); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 350 | bug = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 352 | return bug ? -1 : i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 355 | static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore) |
| 356 | { |
| 357 | struct rb_node *nd; |
| 358 | |
| 359 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 360 | struct vm_area_struct *vma; |
| 361 | vma = rb_entry(nd, struct vm_area_struct, vm_rb); |
Sasha Levin | 96dad67 | 2014-10-09 15:28:39 -0700 | [diff] [blame] | 362 | VM_BUG_ON_VMA(vma != ignore && |
| 363 | vma->rb_subtree_gap != vma_compute_subtree_gap(vma), |
| 364 | vma); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Rashika Kheria | eafd4dc | 2014-04-03 14:48:03 -0700 | [diff] [blame] | 368 | static void validate_mm(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
| 370 | int bug = 0; |
| 371 | int i = 0; |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 372 | unsigned long highest_address = 0; |
Michel Lespinasse | ed8ea81 | 2012-10-08 16:31:45 -0700 | [diff] [blame] | 373 | struct vm_area_struct *vma = mm->mmap; |
Andrew Morton | ff26f70 | 2014-10-09 15:28:19 -0700 | [diff] [blame] | 374 | |
Michel Lespinasse | ed8ea81 | 2012-10-08 16:31:45 -0700 | [diff] [blame] | 375 | while (vma) { |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 376 | struct anon_vma *anon_vma = vma->anon_vma; |
Michel Lespinasse | ed8ea81 | 2012-10-08 16:31:45 -0700 | [diff] [blame] | 377 | struct anon_vma_chain *avc; |
Andrew Morton | ff26f70 | 2014-10-09 15:28:19 -0700 | [diff] [blame] | 378 | |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 379 | if (anon_vma) { |
| 380 | anon_vma_lock_read(anon_vma); |
| 381 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
| 382 | anon_vma_interval_tree_verify(avc); |
| 383 | anon_vma_unlock_read(anon_vma); |
| 384 | } |
| 385 | |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 386 | highest_address = vm_end_gap(vma); |
Michel Lespinasse | ed8ea81 | 2012-10-08 16:31:45 -0700 | [diff] [blame] | 387 | vma = vma->vm_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | i++; |
| 389 | } |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 390 | if (i != mm->map_count) { |
Sasha Levin | 8542bdf | 2014-09-09 14:50:59 -0700 | [diff] [blame] | 391 | pr_emerg("map_count %d vm_next %d\n", mm->map_count, i); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 392 | bug = 1; |
| 393 | } |
| 394 | if (highest_address != mm->highest_vm_end) { |
Sasha Levin | 8542bdf | 2014-09-09 14:50:59 -0700 | [diff] [blame] | 395 | pr_emerg("mm->highest_vm_end %lx, found %lx\n", |
Andrew Morton | ff26f70 | 2014-10-09 15:28:19 -0700 | [diff] [blame] | 396 | mm->highest_vm_end, highest_address); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 397 | bug = 1; |
| 398 | } |
Andrea Arcangeli | acf128d | 2016-02-05 15:36:13 -0800 | [diff] [blame] | 399 | i = browse_rb(mm); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 400 | if (i != mm->map_count) { |
Andrew Morton | ff26f70 | 2014-10-09 15:28:19 -0700 | [diff] [blame] | 401 | if (i != -1) |
| 402 | pr_emerg("map_count %d rb %d\n", mm->map_count, i); |
Michel Lespinasse | 5a0768f | 2012-12-11 16:01:42 -0800 | [diff] [blame] | 403 | bug = 1; |
| 404 | } |
Sasha Levin | 96dad67 | 2014-10-09 15:28:39 -0700 | [diff] [blame] | 405 | VM_BUG_ON_MM(bug, mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | #else |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 408 | #define validate_mm_rb(root, ignore) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | #define validate_mm(mm) do { } while (0) |
| 410 | #endif |
| 411 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 412 | #ifdef CONFIG_SPECULATIVE_PAGE_FAULT |
| 413 | #define mm_rb_write_lock(mm) write_lock(&(mm)->mm_rb_lock) |
| 414 | #define mm_rb_write_unlock(mm) write_unlock(&(mm)->mm_rb_lock) |
| 415 | #else |
| 416 | #define mm_rb_write_lock(mm) do { } while (0) |
| 417 | #define mm_rb_write_unlock(mm) do { } while (0) |
| 418 | #endif /* CONFIG_SPECULATIVE_PAGE_FAULT */ |
| 419 | |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 420 | RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb, |
| 421 | unsigned long, rb_subtree_gap, vma_compute_subtree_gap) |
| 422 | |
| 423 | /* |
| 424 | * Update augmented rbtree rb_subtree_gap values after vma->vm_start or |
| 425 | * vma->vm_prev->vm_end values changed, without modifying the vma's position |
| 426 | * in the rbtree. |
| 427 | */ |
| 428 | static void vma_gap_update(struct vm_area_struct *vma) |
| 429 | { |
| 430 | /* |
| 431 | * As it turns out, RB_DECLARE_CALLBACKS() already created a callback |
| 432 | * function that does exacltly what we want. |
| 433 | */ |
| 434 | vma_gap_callbacks_propagate(&vma->vm_rb, NULL); |
| 435 | } |
| 436 | |
| 437 | static inline void vma_rb_insert(struct vm_area_struct *vma, |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 438 | struct mm_struct *mm) |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 439 | { |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 440 | struct rb_root *root = &mm->mm_rb; |
| 441 | |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 442 | /* All rb_subtree_gap values must be consistent prior to insertion */ |
| 443 | validate_mm_rb(root, NULL); |
| 444 | |
| 445 | rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks); |
| 446 | } |
| 447 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 448 | static void __vma_rb_erase(struct vm_area_struct *vma, struct mm_struct *mm) |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 449 | { |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 450 | struct rb_root *root = &mm->mm_rb; |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 451 | /* |
| 452 | * Note rb_erase_augmented is a fairly large inline function, |
| 453 | * so make sure we instantiate it only once with our desired |
| 454 | * augmented rbtree callbacks. |
| 455 | */ |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 456 | mm_rb_write_lock(mm); |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 457 | rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks); |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 458 | mm_rb_write_unlock(mm); /* wmb */ |
| 459 | |
| 460 | /* |
| 461 | * Ensure the removal is complete before clearing the node. |
| 462 | * Matched by vma_has_changed()/handle_speculative_fault(). |
| 463 | */ |
| 464 | RB_CLEAR_NODE(&vma->vm_rb); |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static __always_inline void vma_rb_erase_ignore(struct vm_area_struct *vma, |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 468 | struct mm_struct *mm, |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 469 | struct vm_area_struct *ignore) |
| 470 | { |
| 471 | /* |
| 472 | * All rb_subtree_gap values must be consistent prior to erase, |
| 473 | * with the possible exception of the "next" vma being erased if |
| 474 | * next->vm_start was reduced. |
| 475 | */ |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 476 | validate_mm_rb(&mm->mm_rb, ignore); |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 477 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 478 | __vma_rb_erase(vma, mm); |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static __always_inline void vma_rb_erase(struct vm_area_struct *vma, |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 482 | struct mm_struct *mm) |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 483 | { |
| 484 | /* |
| 485 | * All rb_subtree_gap values must be consistent prior to erase, |
| 486 | * with the possible exception of the vma being erased. |
| 487 | */ |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 488 | validate_mm_rb(&mm->mm_rb, vma); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 489 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 490 | __vma_rb_erase(vma, mm); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 493 | /* |
| 494 | * vma has some anon_vma assigned, and is already inserted on that |
| 495 | * anon_vma's interval trees. |
| 496 | * |
| 497 | * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the |
| 498 | * vma must be removed from the anon_vma's interval trees using |
| 499 | * anon_vma_interval_tree_pre_update_vma(). |
| 500 | * |
| 501 | * After the update, the vma will be reinserted using |
| 502 | * anon_vma_interval_tree_post_update_vma(). |
| 503 | * |
| 504 | * The entire update must be protected by exclusive mmap_sem and by |
| 505 | * the root anon_vma's mutex. |
| 506 | */ |
| 507 | static inline void |
| 508 | anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) |
| 509 | { |
| 510 | struct anon_vma_chain *avc; |
| 511 | |
| 512 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
| 513 | anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); |
| 514 | } |
| 515 | |
| 516 | static inline void |
| 517 | anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) |
| 518 | { |
| 519 | struct anon_vma_chain *avc; |
| 520 | |
| 521 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
| 522 | anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); |
| 523 | } |
| 524 | |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 525 | static int find_vma_links(struct mm_struct *mm, unsigned long addr, |
| 526 | unsigned long end, struct vm_area_struct **pprev, |
| 527 | struct rb_node ***rb_link, struct rb_node **rb_parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | { |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 529 | struct rb_node **__rb_link, *__rb_parent, *rb_prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
| 531 | __rb_link = &mm->mm_rb.rb_node; |
| 532 | rb_prev = __rb_parent = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | while (*__rb_link) { |
| 535 | struct vm_area_struct *vma_tmp; |
| 536 | |
| 537 | __rb_parent = *__rb_link; |
| 538 | vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb); |
| 539 | |
| 540 | if (vma_tmp->vm_end > addr) { |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 541 | /* Fail if an existing vma overlaps the area */ |
| 542 | if (vma_tmp->vm_start < end) |
| 543 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | __rb_link = &__rb_parent->rb_left; |
| 545 | } else { |
| 546 | rb_prev = __rb_parent; |
| 547 | __rb_link = &__rb_parent->rb_right; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | *pprev = NULL; |
| 552 | if (rb_prev) |
| 553 | *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); |
| 554 | *rb_link = __rb_link; |
| 555 | *rb_parent = __rb_parent; |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 556 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Cyril Hrubis | e8420a8 | 2013-04-29 15:08:33 -0700 | [diff] [blame] | 559 | static unsigned long count_vma_pages_range(struct mm_struct *mm, |
| 560 | unsigned long addr, unsigned long end) |
| 561 | { |
| 562 | unsigned long nr_pages = 0; |
| 563 | struct vm_area_struct *vma; |
| 564 | |
| 565 | /* Find first overlaping mapping */ |
| 566 | vma = find_vma_intersection(mm, addr, end); |
| 567 | if (!vma) |
| 568 | return 0; |
| 569 | |
| 570 | nr_pages = (min(end, vma->vm_end) - |
| 571 | max(addr, vma->vm_start)) >> PAGE_SHIFT; |
| 572 | |
| 573 | /* Iterate over the rest of the overlaps */ |
| 574 | for (vma = vma->vm_next; vma; vma = vma->vm_next) { |
| 575 | unsigned long overlap_len; |
| 576 | |
| 577 | if (vma->vm_start > end) |
| 578 | break; |
| 579 | |
| 580 | overlap_len = min(end, vma->vm_end) - vma->vm_start; |
| 581 | nr_pages += overlap_len >> PAGE_SHIFT; |
| 582 | } |
| 583 | |
| 584 | return nr_pages; |
| 585 | } |
| 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma, |
| 588 | struct rb_node **rb_link, struct rb_node *rb_parent) |
| 589 | { |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 590 | /* Update tracking information for the gap following the new vma. */ |
| 591 | if (vma->vm_next) |
| 592 | vma_gap_update(vma->vm_next); |
| 593 | else |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 594 | mm->highest_vm_end = vm_end_gap(vma); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | * vma->vm_prev wasn't known when we followed the rbtree to find the |
| 598 | * correct insertion point for that vma. As a result, we could not |
| 599 | * update the vma vm_rb parents rb_subtree_gap values on the way down. |
| 600 | * So, we first insert the vma with a zero rb_subtree_gap value |
| 601 | * (to be consistent with what we did on the way down), and then |
| 602 | * immediately update the gap to the correct value. Finally we |
| 603 | * rebalance the rbtree after all augmented values have been set. |
| 604 | */ |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 605 | mm_rb_write_lock(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | rb_link_node(&vma->vm_rb, rb_parent, rb_link); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 607 | vma->rb_subtree_gap = 0; |
| 608 | vma_gap_update(vma); |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 609 | vma_rb_insert(vma, mm); |
| 610 | mm_rb_write_unlock(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Denys Vlasenko | cb8f488 | 2008-10-18 20:27:01 -0700 | [diff] [blame] | 613 | static void __vma_link_file(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
ZhenwenXu | 48aae42 | 2009-01-06 14:40:21 -0800 | [diff] [blame] | 615 | struct file *file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | file = vma->vm_file; |
| 618 | if (file) { |
| 619 | struct address_space *mapping = file->f_mapping; |
| 620 | |
| 621 | if (vma->vm_flags & VM_DENYWRITE) |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 622 | atomic_dec(&file_inode(file)->i_writecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | if (vma->vm_flags & VM_SHARED) |
David Herrmann | 4bb5f5d | 2014-08-08 14:25:25 -0700 | [diff] [blame] | 624 | atomic_inc(&mapping->i_mmap_writable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | flush_dcache_mmap_lock(mapping); |
Kirill A. Shutemov | 27ba064 | 2015-02-10 14:09:59 -0800 | [diff] [blame] | 627 | vma_interval_tree_insert(vma, &mapping->i_mmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | flush_dcache_mmap_unlock(mapping); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static void |
| 633 | __vma_link(struct mm_struct *mm, struct vm_area_struct *vma, |
| 634 | struct vm_area_struct *prev, struct rb_node **rb_link, |
| 635 | struct rb_node *rb_parent) |
| 636 | { |
| 637 | __vma_link_list(mm, vma, prev, rb_parent); |
| 638 | __vma_link_rb(mm, vma, rb_link, rb_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma, |
| 642 | struct vm_area_struct *prev, struct rb_node **rb_link, |
| 643 | struct rb_node *rb_parent) |
| 644 | { |
| 645 | struct address_space *mapping = NULL; |
| 646 | |
Huang Shijie | 64ac494 | 2014-06-04 16:07:33 -0700 | [diff] [blame] | 647 | if (vma->vm_file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | mapping = vma->vm_file->f_mapping; |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 649 | i_mmap_lock_write(mapping); |
Huang Shijie | 64ac494 | 2014-06-04 16:07:33 -0700 | [diff] [blame] | 650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | __vma_link(mm, vma, prev, rb_link, rb_parent); |
| 653 | __vma_link_file(vma); |
| 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | if (mapping) |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 656 | i_mmap_unlock_write(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | mm->map_count++; |
| 659 | validate_mm(mm); |
| 660 | } |
| 661 | |
| 662 | /* |
Kautuk Consul | 88f6b4c | 2012-03-21 16:34:16 -0700 | [diff] [blame] | 663 | * Helper for vma_adjust() in the split_vma insert case: insert a vma into the |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 664 | * mm's list and rbtree. It has already been inserted into the interval tree. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | */ |
ZhenwenXu | 48aae42 | 2009-01-06 14:40:21 -0800 | [diff] [blame] | 666 | static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 668 | struct vm_area_struct *prev; |
ZhenwenXu | 48aae42 | 2009-01-06 14:40:21 -0800 | [diff] [blame] | 669 | struct rb_node **rb_link, *rb_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 671 | if (find_vma_links(mm, vma->vm_start, vma->vm_end, |
| 672 | &prev, &rb_link, &rb_parent)) |
| 673 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | __vma_link(mm, vma, prev, rb_link, rb_parent); |
| 675 | mm->map_count++; |
| 676 | } |
| 677 | |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 678 | static __always_inline void __vma_unlink_common(struct mm_struct *mm, |
| 679 | struct vm_area_struct *vma, |
| 680 | struct vm_area_struct *prev, |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 681 | bool has_prev, |
| 682 | struct vm_area_struct *ignore) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | { |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 684 | struct vm_area_struct *next; |
Linus Torvalds | 297c5ee | 2010-08-20 16:24:55 -0700 | [diff] [blame] | 685 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 686 | vma_rb_erase_ignore(vma, mm, ignore); |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 687 | next = vma->vm_next; |
| 688 | if (has_prev) |
| 689 | prev->vm_next = next; |
| 690 | else { |
| 691 | prev = vma->vm_prev; |
| 692 | if (prev) |
| 693 | prev->vm_next = next; |
| 694 | else |
| 695 | mm->mmap = next; |
| 696 | } |
Linus Torvalds | 297c5ee | 2010-08-20 16:24:55 -0700 | [diff] [blame] | 697 | if (next) |
| 698 | next->vm_prev = prev; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 699 | |
| 700 | /* Kill the cache */ |
| 701 | vmacache_invalidate(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 704 | static inline void __vma_unlink_prev(struct mm_struct *mm, |
| 705 | struct vm_area_struct *vma, |
| 706 | struct vm_area_struct *prev) |
| 707 | { |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 708 | __vma_unlink_common(mm, vma, prev, true, vma); |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* |
| 712 | * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that |
| 713 | * is already present in an i_mmap tree without adjusting the tree. |
| 714 | * The following helper function should be used when such adjustments |
| 715 | * are necessary. The "insert" vma (if any) is to be inserted |
| 716 | * before we drop the necessary locks. |
| 717 | */ |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 718 | int __vma_adjust(struct vm_area_struct *vma, unsigned long start, |
| 719 | unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert, |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 720 | struct vm_area_struct *expand, bool keep_locked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
| 722 | struct mm_struct *mm = vma->vm_mm; |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 723 | struct vm_area_struct *next = vma->vm_next, *orig_vma = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | struct address_space *mapping = NULL; |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 725 | struct rb_root *root = NULL; |
Rik van Riel | 012f1800 | 2010-08-09 17:18:40 -0700 | [diff] [blame] | 726 | struct anon_vma *anon_vma = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | struct file *file = vma->vm_file; |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 728 | bool start_changed = false, end_changed = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | long adjust_next = 0; |
| 730 | int remove_next = 0; |
| 731 | |
Peter Zijlstra | 365031a | 2018-04-17 16:33:14 +0200 | [diff] [blame] | 732 | /* |
| 733 | * Why using vm_raw_write*() functions here to avoid lockdep's warning ? |
| 734 | * |
| 735 | * Locked is complaining about a theoretical lock dependency, involving |
| 736 | * 3 locks: |
| 737 | * mapping->i_mmap_rwsem --> vma->vm_sequence --> fs_reclaim |
| 738 | * |
| 739 | * Here are the major path leading to this dependency : |
| 740 | * 1. __vma_adjust() mmap_sem -> vm_sequence -> i_mmap_rwsem |
| 741 | * 2. move_vmap() mmap_sem -> vm_sequence -> fs_reclaim |
| 742 | * 3. __alloc_pages_nodemask() fs_reclaim -> i_mmap_rwsem |
| 743 | * 4. unmap_mapping_range() i_mmap_rwsem -> vm_sequence |
| 744 | * |
| 745 | * So there is no way to solve this easily, especially because in |
| 746 | * unmap_mapping_range() the i_mmap_rwsem is grab while the impacted |
| 747 | * VMAs are not yet known. |
| 748 | * However, the way the vm_seq is used is guarantying that we will |
| 749 | * never block on it since we just check for its value and never wait |
| 750 | * for it to move, see vma_has_changed() and handle_speculative_fault(). |
| 751 | */ |
| 752 | vm_raw_write_begin(vma); |
| 753 | if (next) |
| 754 | vm_raw_write_begin(next); |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | if (next && !insert) { |
Kirill A. Shutemov | 734537c | 2016-07-28 15:49:01 -0700 | [diff] [blame] | 757 | struct vm_area_struct *exporter = NULL, *importer = NULL; |
Linus Torvalds | 287d97a | 2010-04-10 15:22:30 -0700 | [diff] [blame] | 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | if (end >= next->vm_end) { |
| 760 | /* |
| 761 | * vma expands, overlapping all the next, and |
| 762 | * perhaps the one after too (mprotect case 6). |
Andrea Arcangeli | 86d12e4 | 2016-10-07 17:01:34 -0700 | [diff] [blame] | 763 | * The only other cases that gets here are |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 764 | * case 1, case 7 and case 8. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | */ |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 766 | if (next == expand) { |
| 767 | /* |
| 768 | * The only case where we don't expand "vma" |
| 769 | * and we expand "next" instead is case 8. |
| 770 | */ |
| 771 | VM_WARN_ON(end != next->vm_end); |
| 772 | /* |
| 773 | * remove_next == 3 means we're |
| 774 | * removing "vma" and that to do so we |
| 775 | * swapped "vma" and "next". |
| 776 | */ |
| 777 | remove_next = 3; |
| 778 | VM_WARN_ON(file != next->vm_file); |
| 779 | swap(vma, next); |
| 780 | } else { |
| 781 | VM_WARN_ON(expand != vma); |
| 782 | /* |
| 783 | * case 1, 6, 7, remove_next == 2 is case 6, |
| 784 | * remove_next == 1 is case 1 or 7. |
| 785 | */ |
| 786 | remove_next = 1 + (end > next->vm_end); |
| 787 | VM_WARN_ON(remove_next == 2 && |
| 788 | end != next->vm_next->vm_end); |
| 789 | VM_WARN_ON(remove_next == 1 && |
| 790 | end != next->vm_end); |
| 791 | /* trim end to next, for case 6 first pass */ |
| 792 | end = next->vm_end; |
| 793 | } |
| 794 | |
Linus Torvalds | 287d97a | 2010-04-10 15:22:30 -0700 | [diff] [blame] | 795 | exporter = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | importer = vma; |
Kirill A. Shutemov | 734537c | 2016-07-28 15:49:01 -0700 | [diff] [blame] | 797 | |
| 798 | /* |
| 799 | * If next doesn't have anon_vma, import from vma after |
| 800 | * next, if the vma overlaps with it. |
| 801 | */ |
Andrea Arcangeli | 97a42cd | 2016-10-07 17:01:31 -0700 | [diff] [blame] | 802 | if (remove_next == 2 && !next->anon_vma) |
Kirill A. Shutemov | 734537c | 2016-07-28 15:49:01 -0700 | [diff] [blame] | 803 | exporter = next->vm_next; |
| 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } else if (end > next->vm_start) { |
| 806 | /* |
| 807 | * vma expands, overlapping part of the next: |
| 808 | * mprotect case 5 shifting the boundary up. |
| 809 | */ |
| 810 | adjust_next = (end - next->vm_start) >> PAGE_SHIFT; |
Linus Torvalds | 287d97a | 2010-04-10 15:22:30 -0700 | [diff] [blame] | 811 | exporter = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | importer = vma; |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 813 | VM_WARN_ON(expand != importer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } else if (end < vma->vm_end) { |
| 815 | /* |
| 816 | * vma shrinks, and !insert tells it's not |
| 817 | * split_vma inserting another: so it must be |
| 818 | * mprotect case 4 shifting the boundary down. |
| 819 | */ |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 820 | adjust_next = -((vma->vm_end - end) >> PAGE_SHIFT); |
Linus Torvalds | 287d97a | 2010-04-10 15:22:30 -0700 | [diff] [blame] | 821 | exporter = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | importer = next; |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 823 | VM_WARN_ON(expand != importer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 826 | /* |
| 827 | * Easily overlooked: when mprotect shifts the boundary, |
| 828 | * make sure the expanding vma has anon_vma set if the |
| 829 | * shrinking vma had, to cover any anon pages imported. |
| 830 | */ |
Linus Torvalds | 287d97a | 2010-04-10 15:22:30 -0700 | [diff] [blame] | 831 | if (exporter && exporter->anon_vma && !importer->anon_vma) { |
Daniel Forrest | c4ea95d | 2014-12-02 15:59:42 -0800 | [diff] [blame] | 832 | int error; |
| 833 | |
Linus Torvalds | 287d97a | 2010-04-10 15:22:30 -0700 | [diff] [blame] | 834 | importer->anon_vma = exporter->anon_vma; |
Konstantin Khlebnikov | b800c91 | 2015-01-11 16:54:06 +0300 | [diff] [blame] | 835 | error = anon_vma_clone(importer, exporter); |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 836 | if (error) { |
| 837 | if (next && next != vma) |
| 838 | vm_raw_write_end(next); |
| 839 | vm_raw_write_end(vma); |
Konstantin Khlebnikov | b800c91 | 2015-01-11 16:54:06 +0300 | [diff] [blame] | 840 | return error; |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 841 | } |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 842 | } |
| 843 | } |
Kirill A. Shutemov | 734537c | 2016-07-28 15:49:01 -0700 | [diff] [blame] | 844 | again: |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 845 | vma_adjust_trans_huge(orig_vma, start, end, adjust_next); |
Kirill A. Shutemov | 37f9f55 | 2016-07-26 15:25:48 -0700 | [diff] [blame] | 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | if (file) { |
| 848 | mapping = file->f_mapping; |
Kirill A. Shutemov | 27ba064 | 2015-02-10 14:09:59 -0800 | [diff] [blame] | 849 | root = &mapping->i_mmap; |
| 850 | uprobe_munmap(vma, vma->vm_start, vma->vm_end); |
Srikar Dronamraju | 682968e | 2012-03-30 23:56:46 +0530 | [diff] [blame] | 851 | |
Kirill A. Shutemov | 27ba064 | 2015-02-10 14:09:59 -0800 | [diff] [blame] | 852 | if (adjust_next) |
| 853 | uprobe_munmap(next, next->vm_start, next->vm_end); |
Srikar Dronamraju | 682968e | 2012-03-30 23:56:46 +0530 | [diff] [blame] | 854 | |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 855 | i_mmap_lock_write(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (insert) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | /* |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 858 | * Put into interval tree now, so instantiated pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | * are visible to arm/parisc __flush_dcache_page |
| 860 | * throughout; but we cannot insert into address |
| 861 | * space until vma start or end is updated. |
| 862 | */ |
| 863 | __vma_link_file(insert); |
| 864 | } |
| 865 | } |
| 866 | |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 867 | anon_vma = vma->anon_vma; |
| 868 | if (!anon_vma && adjust_next) |
| 869 | anon_vma = next->anon_vma; |
| 870 | if (anon_vma) { |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 871 | VM_WARN_ON(adjust_next && next->anon_vma && |
| 872 | anon_vma != next->anon_vma); |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 873 | anon_vma_lock_write(anon_vma); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 874 | anon_vma_interval_tree_pre_update_vma(vma); |
| 875 | if (adjust_next) |
| 876 | anon_vma_interval_tree_pre_update_vma(next); |
| 877 | } |
Rik van Riel | 012f1800 | 2010-08-09 17:18:40 -0700 | [diff] [blame] | 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | if (root) { |
| 880 | flush_dcache_mmap_lock(mapping); |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 881 | vma_interval_tree_remove(vma, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | if (adjust_next) |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 883 | vma_interval_tree_remove(next, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 886 | if (start != vma->vm_start) { |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 887 | WRITE_ONCE(vma->vm_start, start); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 888 | start_changed = true; |
| 889 | } |
| 890 | if (end != vma->vm_end) { |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 891 | WRITE_ONCE(vma->vm_end, end); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 892 | end_changed = true; |
| 893 | } |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 894 | WRITE_ONCE(vma->vm_pgoff, pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | if (adjust_next) { |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 896 | WRITE_ONCE(next->vm_start, |
| 897 | next->vm_start + (adjust_next << PAGE_SHIFT)); |
| 898 | WRITE_ONCE(next->vm_pgoff, next->vm_pgoff + adjust_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (root) { |
| 902 | if (adjust_next) |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 903 | vma_interval_tree_insert(next, root); |
| 904 | vma_interval_tree_insert(vma, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | flush_dcache_mmap_unlock(mapping); |
| 906 | } |
| 907 | |
| 908 | if (remove_next) { |
| 909 | /* |
| 910 | * vma_merge has merged next into vma, and needs |
| 911 | * us to remove next before dropping the locks. |
| 912 | */ |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 913 | if (remove_next != 3) |
| 914 | __vma_unlink_prev(mm, next, vma); |
| 915 | else |
Andrea Arcangeli | 8f26e0b | 2016-10-07 17:01:37 -0700 | [diff] [blame] | 916 | /* |
| 917 | * vma is not before next if they've been |
| 918 | * swapped. |
| 919 | * |
| 920 | * pre-swap() next->vm_start was reduced so |
| 921 | * tell validate_mm_rb to ignore pre-swap() |
| 922 | * "next" (which is stored in post-swap() |
| 923 | * "vma"). |
| 924 | */ |
| 925 | __vma_unlink_common(mm, next, NULL, false, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | if (file) |
| 927 | __remove_shared_vm_struct(next, file, mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } else if (insert) { |
| 929 | /* |
| 930 | * split_vma has split insert from vma, and needs |
| 931 | * us to insert it before dropping the locks |
| 932 | * (it may either follow vma or precede it). |
| 933 | */ |
| 934 | __insert_vm_struct(mm, insert); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 935 | } else { |
| 936 | if (start_changed) |
| 937 | vma_gap_update(vma); |
| 938 | if (end_changed) { |
| 939 | if (!next) |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 940 | mm->highest_vm_end = vm_end_gap(vma); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 941 | else if (!adjust_next) |
| 942 | vma_gap_update(next); |
| 943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 946 | if (anon_vma) { |
| 947 | anon_vma_interval_tree_post_update_vma(vma); |
| 948 | if (adjust_next) |
| 949 | anon_vma_interval_tree_post_update_vma(next); |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 950 | anon_vma_unlock_write(anon_vma); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | if (mapping) |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 953 | i_mmap_unlock_write(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 955 | if (root) { |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 956 | uprobe_mmap(vma); |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 957 | |
| 958 | if (adjust_next) |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 959 | uprobe_mmap(next); |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 960 | } |
| 961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | if (remove_next) { |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 963 | if (file) |
Srikar Dronamraju | cbc91f7 | 2012-04-11 16:05:27 +0530 | [diff] [blame] | 964 | uprobe_munmap(next, next->vm_start, next->vm_end); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 965 | if (next->anon_vma) |
| 966 | anon_vma_merge(vma, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | mm->map_count--; |
Peter Zijlstra | 365031a | 2018-04-17 16:33:14 +0200 | [diff] [blame] | 968 | vm_raw_write_end(next); |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 969 | put_vma(next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | /* |
| 971 | * In mprotect's case 6 (see comments on vma_merge), |
| 972 | * we must remove another next too. It would clutter |
| 973 | * up the code too much to do both in one go. |
| 974 | */ |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 975 | if (remove_next != 3) { |
| 976 | /* |
| 977 | * If "next" was removed and vma->vm_end was |
| 978 | * expanded (up) over it, in turn |
| 979 | * "next->vm_prev->vm_end" changed and the |
| 980 | * "vma->vm_next" gap must be updated. |
| 981 | */ |
| 982 | next = vma->vm_next; |
Peter Zijlstra | 365031a | 2018-04-17 16:33:14 +0200 | [diff] [blame] | 983 | if (next) |
| 984 | vm_raw_write_begin(next); |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 985 | } else { |
| 986 | /* |
| 987 | * For the scope of the comment "next" and |
| 988 | * "vma" considered pre-swap(): if "vma" was |
| 989 | * removed, next->vm_start was expanded (down) |
| 990 | * over it and the "next" gap must be updated. |
| 991 | * Because of the swap() the post-swap() "vma" |
| 992 | * actually points to pre-swap() "next" |
| 993 | * (post-swap() "next" as opposed is now a |
| 994 | * dangling pointer). |
| 995 | */ |
| 996 | next = vma; |
| 997 | } |
Kirill A. Shutemov | 734537c | 2016-07-28 15:49:01 -0700 | [diff] [blame] | 998 | if (remove_next == 2) { |
| 999 | remove_next = 1; |
| 1000 | end = next->vm_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | goto again; |
Kirill A. Shutemov | 734537c | 2016-07-28 15:49:01 -0700 | [diff] [blame] | 1002 | } |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 1003 | else if (next) |
| 1004 | vma_gap_update(next); |
Andrea Arcangeli | fb8c41e | 2016-10-07 17:01:25 -0700 | [diff] [blame] | 1005 | else { |
| 1006 | /* |
| 1007 | * If remove_next == 2 we obviously can't |
| 1008 | * reach this path. |
| 1009 | * |
| 1010 | * If remove_next == 3 we can't reach this |
| 1011 | * path because pre-swap() next is always not |
| 1012 | * NULL. pre-swap() "next" is not being |
| 1013 | * removed and its next->vm_end is not altered |
| 1014 | * (and furthermore "end" already matches |
| 1015 | * next->vm_end in remove_next == 3). |
| 1016 | * |
| 1017 | * We reach this only in the remove_next == 1 |
| 1018 | * case if the "next" vma that was removed was |
| 1019 | * the highest vma of the mm. However in such |
| 1020 | * case next->vm_end == "end" and the extended |
| 1021 | * "vma" has vma->vm_end == next->vm_end so |
| 1022 | * mm->highest_vm_end doesn't need any update |
| 1023 | * in remove_next == 1 case. |
| 1024 | */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 1025 | VM_WARN_ON(mm->highest_vm_end != vm_end_gap(vma)); |
Andrea Arcangeli | fb8c41e | 2016-10-07 17:01:25 -0700 | [diff] [blame] | 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | } |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 1028 | if (insert && file) |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 1029 | uprobe_mmap(insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
Peter Zijlstra | 365031a | 2018-04-17 16:33:14 +0200 | [diff] [blame] | 1031 | if (next && next != vma) |
| 1032 | vm_raw_write_end(next); |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 1033 | if (!keep_locked) |
| 1034 | vm_raw_write_end(vma); |
Peter Zijlstra | 365031a | 2018-04-17 16:33:14 +0200 | [diff] [blame] | 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | validate_mm(mm); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1037 | |
| 1038 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * If the vma has a ->close operation then the driver probably needs to release |
| 1043 | * per-vma resources, so we don't attempt to merge those. |
| 1044 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | static inline int is_mergeable_vma(struct vm_area_struct *vma, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1046 | struct file *file, unsigned long vm_flags, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1047 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
| 1048 | const char __user *anon_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | { |
Cyrill Gorcunov | 34228d4 | 2014-01-23 15:53:42 -0800 | [diff] [blame] | 1050 | /* |
| 1051 | * VM_SOFTDIRTY should not prevent from VMA merging, if we |
| 1052 | * match the flags but dirty bit -- the caller should mark |
| 1053 | * merged VMA as dirty. If dirty bit won't be excluded from |
| 1054 | * comparison, we increase pressue on the memory system forcing |
| 1055 | * the kernel to generate new VMAs when old one could be |
| 1056 | * extended instead. |
| 1057 | */ |
| 1058 | if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | return 0; |
| 1060 | if (vma->vm_file != file) |
| 1061 | return 0; |
| 1062 | if (vma->vm_ops && vma->vm_ops->close) |
| 1063 | return 0; |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1064 | if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) |
| 1065 | return 0; |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1066 | if (vma_get_anon_name(vma) != anon_name) |
| 1067 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return 1; |
| 1069 | } |
| 1070 | |
| 1071 | static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, |
Shaohua Li | 965f55d | 2011-05-24 17:11:20 -0700 | [diff] [blame] | 1072 | struct anon_vma *anon_vma2, |
| 1073 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | { |
Shaohua Li | 965f55d | 2011-05-24 17:11:20 -0700 | [diff] [blame] | 1075 | /* |
| 1076 | * The list_is_singular() test is to avoid merging VMA cloned from |
| 1077 | * parents. This can improve scalability caused by anon_vma lock. |
| 1078 | */ |
| 1079 | if ((!anon_vma1 || !anon_vma2) && (!vma || |
| 1080 | list_is_singular(&vma->anon_vma_chain))) |
| 1081 | return 1; |
| 1082 | return anon_vma1 == anon_vma2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) |
| 1087 | * in front of (at a lower virtual address and file offset than) the vma. |
| 1088 | * |
| 1089 | * We cannot merge two vmas if they have differently assigned (non-NULL) |
| 1090 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. |
| 1091 | * |
| 1092 | * We don't check here for the merged mmap wrapping around the end of pagecache |
| 1093 | * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which |
| 1094 | * wrap, nor mmaps which cover the final page at index -1UL. |
| 1095 | */ |
| 1096 | static int |
| 1097 | can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1098 | struct anon_vma *anon_vma, struct file *file, |
| 1099 | pgoff_t vm_pgoff, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1100 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
| 1101 | const char __user *anon_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | { |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1103 | if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && |
Shaohua Li | 965f55d | 2011-05-24 17:11:20 -0700 | [diff] [blame] | 1104 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | if (vma->vm_pgoff == vm_pgoff) |
| 1106 | return 1; |
| 1107 | } |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) |
| 1113 | * beyond (at a higher virtual address and file offset than) the vma. |
| 1114 | * |
| 1115 | * We cannot merge two vmas if they have differently assigned (non-NULL) |
| 1116 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. |
| 1117 | */ |
| 1118 | static int |
| 1119 | can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1120 | struct anon_vma *anon_vma, struct file *file, |
| 1121 | pgoff_t vm_pgoff, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1122 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
| 1123 | const char __user *anon_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | { |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1125 | if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && |
Shaohua Li | 965f55d | 2011-05-24 17:11:20 -0700 | [diff] [blame] | 1126 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | pgoff_t vm_pglen; |
Libin | d6e9321 | 2013-07-03 15:01:26 -0700 | [diff] [blame] | 1128 | vm_pglen = vma_pages(vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | if (vma->vm_pgoff + vm_pglen == vm_pgoff) |
| 1130 | return 1; |
| 1131 | } |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1136 | * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), |
| 1137 | * figure out whether that can be merged with its predecessor or its |
| 1138 | * successor. Or both (it neatly fills a hole). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | * |
| 1140 | * In most cases - when called for mmap, brk or mremap - [addr,end) is |
| 1141 | * certain not to be mapped by the time vma_merge is called; but when |
| 1142 | * called for mprotect, it is certain to be already mapped (either at |
| 1143 | * an offset within prev, or at the start of next), and the flags of |
| 1144 | * this area are about to be changed to vm_flags - and the no-change |
| 1145 | * case has already been eliminated. |
| 1146 | * |
| 1147 | * The following mprotect cases have to be considered, where AAAA is |
| 1148 | * the area passed down from mprotect_fixup, never extending beyond one |
| 1149 | * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: |
| 1150 | * |
| 1151 | * AAAA AAAA AAAA AAAA |
| 1152 | * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX |
| 1153 | * cannot merge might become might become might become |
| 1154 | * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or |
| 1155 | * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1156 | * mremap move: PPPPXXXXXXXX 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | * AAAA |
| 1158 | * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN |
| 1159 | * might become case 1 below case 2 below case 3 below |
| 1160 | * |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1161 | * It is important for case 8 that the the vma NNNN overlapping the |
| 1162 | * region AAAA is never going to extended over XXXX. Instead XXXX must |
| 1163 | * be extended in region AAAA and NNNN must be removed. This way in |
| 1164 | * all cases where vma_merge succeeds, the moment vma_adjust drops the |
| 1165 | * rmap_locks, the properties of the merged vma will be already |
| 1166 | * correct for the whole merged range. Some of those properties like |
| 1167 | * vm_page_prot/vm_flags may be accessed by rmap_walks and they must |
| 1168 | * be correct for the whole merged range immediately after the |
| 1169 | * rmap_locks are released. Otherwise if XXXX would be removed and |
| 1170 | * NNNN would be extended over the XXXX range, remove_migration_ptes |
| 1171 | * or other rmap walkers (if working on addresses beyond the "end" |
| 1172 | * parameter) may establish ptes with the wrong permissions of NNNN |
| 1173 | * instead of the right permissions of XXXX. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | */ |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 1175 | struct vm_area_struct *__vma_merge(struct mm_struct *mm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | struct vm_area_struct *prev, unsigned long addr, |
| 1177 | unsigned long end, unsigned long vm_flags, |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 1178 | struct anon_vma *anon_vma, struct file *file, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1179 | pgoff_t pgoff, struct mempolicy *policy, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1180 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 1181 | const char __user *anon_name, bool keep_locked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | { |
| 1183 | pgoff_t pglen = (end - addr) >> PAGE_SHIFT; |
| 1184 | struct vm_area_struct *area, *next; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1185 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
| 1187 | /* |
| 1188 | * We later require that vma->vm_flags == vm_flags, |
| 1189 | * so this tests vma->vm_flags & VM_SPECIAL, too. |
| 1190 | */ |
| 1191 | if (vm_flags & VM_SPECIAL) |
| 1192 | return NULL; |
| 1193 | |
| 1194 | if (prev) |
| 1195 | next = prev->vm_next; |
| 1196 | else |
| 1197 | next = mm->mmap; |
| 1198 | area = next; |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1199 | if (area && area->vm_end == end) /* cases 6, 7, 8 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | next = next->vm_next; |
| 1201 | |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1202 | /* verify some invariant that must be enforced by the caller */ |
| 1203 | VM_WARN_ON(prev && addr <= prev->vm_start); |
| 1204 | VM_WARN_ON(area && end > area->vm_end); |
| 1205 | VM_WARN_ON(addr >= end); |
| 1206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | /* |
| 1208 | * Can it merge with the predecessor? |
| 1209 | */ |
| 1210 | if (prev && prev->vm_end == addr && |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 1211 | mpol_equal(vma_policy(prev), policy) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | can_vma_merge_after(prev, vm_flags, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1213 | anon_vma, file, pgoff, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1214 | vm_userfaultfd_ctx, |
| 1215 | anon_name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | /* |
| 1217 | * OK, it can. Can we now merge in the successor as well? |
| 1218 | */ |
| 1219 | if (next && end == next->vm_start && |
| 1220 | mpol_equal(policy, vma_policy(next)) && |
| 1221 | can_vma_merge_before(next, vm_flags, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1222 | anon_vma, file, |
| 1223 | pgoff+pglen, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1224 | vm_userfaultfd_ctx, |
| 1225 | anon_name) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | is_mergeable_anon_vma(prev->anon_vma, |
Shaohua Li | 965f55d | 2011-05-24 17:11:20 -0700 | [diff] [blame] | 1227 | next->anon_vma, NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | /* cases 1, 6 */ |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1229 | err = __vma_adjust(prev, prev->vm_start, |
| 1230 | next->vm_end, prev->vm_pgoff, NULL, |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 1231 | prev, keep_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | } else /* cases 2, 5, 7 */ |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1233 | err = __vma_adjust(prev, prev->vm_start, |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 1234 | end, prev->vm_pgoff, NULL, prev, |
| 1235 | keep_locked); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1236 | if (err) |
| 1237 | return NULL; |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 1238 | khugepaged_enter_vma_merge(prev, vm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | return prev; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * Can this new request be merged in front of next? |
| 1244 | */ |
| 1245 | if (next && end == next->vm_start && |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 1246 | mpol_equal(policy, vma_policy(next)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | can_vma_merge_before(next, vm_flags, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1248 | anon_vma, file, pgoff+pglen, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1249 | vm_userfaultfd_ctx, |
| 1250 | anon_name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | if (prev && addr < prev->vm_end) /* case 4 */ |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1252 | err = __vma_adjust(prev, prev->vm_start, |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 1253 | addr, prev->vm_pgoff, NULL, next, |
| 1254 | keep_locked); |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1255 | else { /* cases 3, 8 */ |
| 1256 | err = __vma_adjust(area, addr, next->vm_end, |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 1257 | next->vm_pgoff - pglen, NULL, next, |
| 1258 | keep_locked); |
Andrea Arcangeli | e86f15e | 2016-10-07 17:01:28 -0700 | [diff] [blame] | 1259 | /* |
| 1260 | * In case 3 area is already equal to next and |
| 1261 | * this is a noop, but in case 8 "area" has |
| 1262 | * been removed and next was expanded over it. |
| 1263 | */ |
| 1264 | area = next; |
| 1265 | } |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1266 | if (err) |
| 1267 | return NULL; |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 1268 | khugepaged_enter_vma_merge(area, vm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | return area; |
| 1270 | } |
| 1271 | |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Linus Torvalds | d0e9fe1 | 2010-04-10 10:36:19 -0700 | [diff] [blame] | 1276 | * Rough compatbility check to quickly see if it's even worth looking |
| 1277 | * at sharing an anon_vma. |
| 1278 | * |
| 1279 | * They need to have the same vm_file, and the flags can only differ |
| 1280 | * in things that mprotect may change. |
| 1281 | * |
| 1282 | * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that |
| 1283 | * we can merge the two vma's. For example, we refuse to merge a vma if |
| 1284 | * there is a vm_ops->close() function, because that indicates that the |
| 1285 | * driver is doing some kind of reference counting. But that doesn't |
| 1286 | * really matter for the anon_vma sharing case. |
| 1287 | */ |
| 1288 | static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) |
| 1289 | { |
| 1290 | return a->vm_end == b->vm_start && |
| 1291 | mpol_equal(vma_policy(a), vma_policy(b)) && |
| 1292 | a->vm_file == b->vm_file && |
Cyrill Gorcunov | 34228d4 | 2014-01-23 15:53:42 -0800 | [diff] [blame] | 1293 | !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) && |
Linus Torvalds | d0e9fe1 | 2010-04-10 10:36:19 -0700 | [diff] [blame] | 1294 | b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Do some basic sanity checking to see if we can re-use the anon_vma |
| 1299 | * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be |
| 1300 | * the same as 'old', the other will be the new one that is trying |
| 1301 | * to share the anon_vma. |
| 1302 | * |
| 1303 | * NOTE! This runs with mm_sem held for reading, so it is possible that |
| 1304 | * the anon_vma of 'old' is concurrently in the process of being set up |
| 1305 | * by another page fault trying to merge _that_. But that's ok: if it |
| 1306 | * is being set up, that automatically means that it will be a singleton |
| 1307 | * acceptable for merging, so we can do all of this optimistically. But |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 1308 | * we do that READ_ONCE() to make sure that we never re-load the pointer. |
Linus Torvalds | d0e9fe1 | 2010-04-10 10:36:19 -0700 | [diff] [blame] | 1309 | * |
| 1310 | * IOW: that the "list_is_singular()" test on the anon_vma_chain only |
| 1311 | * matters for the 'stable anon_vma' case (ie the thing we want to avoid |
| 1312 | * is to return an anon_vma that is "complex" due to having gone through |
| 1313 | * a fork). |
| 1314 | * |
| 1315 | * We also make sure that the two vma's are compatible (adjacent, |
| 1316 | * and with the same memory policies). That's all stable, even with just |
| 1317 | * a read lock on the mm_sem. |
| 1318 | */ |
| 1319 | static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) |
| 1320 | { |
| 1321 | if (anon_vma_compatible(a, b)) { |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 1322 | struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); |
Linus Torvalds | d0e9fe1 | 2010-04-10 10:36:19 -0700 | [diff] [blame] | 1323 | |
| 1324 | if (anon_vma && list_is_singular(&old->anon_vma_chain)) |
| 1325 | return anon_vma; |
| 1326 | } |
| 1327 | return NULL; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | * find_mergeable_anon_vma is used by anon_vma_prepare, to check |
| 1332 | * neighbouring vmas for a suitable anon_vma, before it goes off |
| 1333 | * to allocate a new anon_vma. It checks because a repetitive |
| 1334 | * sequence of mprotects and faults may otherwise lead to distinct |
| 1335 | * anon_vmas being allocated, preventing vma merge in subsequent |
| 1336 | * mprotect. |
| 1337 | */ |
| 1338 | struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) |
| 1339 | { |
Linus Torvalds | d0e9fe1 | 2010-04-10 10:36:19 -0700 | [diff] [blame] | 1340 | struct anon_vma *anon_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | struct vm_area_struct *near; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
| 1343 | near = vma->vm_next; |
| 1344 | if (!near) |
| 1345 | goto try_prev; |
| 1346 | |
Linus Torvalds | d0e9fe1 | 2010-04-10 10:36:19 -0700 | [diff] [blame] | 1347 | anon_vma = reusable_anon_vma(near, vma, near); |
| 1348 | if (anon_vma) |
| 1349 | return anon_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | try_prev: |
Linus Torvalds | 9be34c9 | 2011-06-16 00:35:09 -0700 | [diff] [blame] | 1351 | near = vma->vm_prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | if (!near) |
| 1353 | goto none; |
| 1354 | |
Linus Torvalds | d0e9fe1 | 2010-04-10 10:36:19 -0700 | [diff] [blame] | 1355 | anon_vma = reusable_anon_vma(near, near, vma); |
| 1356 | if (anon_vma) |
| 1357 | return anon_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | none: |
| 1359 | /* |
| 1360 | * There's no absolute need to look only at touching neighbours: |
| 1361 | * we could search further afield for "compatible" anon_vmas. |
| 1362 | * But it would probably just be a waste of time searching, |
| 1363 | * or lead to too many vmas hanging off the same anon_vma. |
| 1364 | * We're trying to allow mprotect remerging later on, |
| 1365 | * not trying to minimize memory used for anon_vmas. |
| 1366 | */ |
| 1367 | return NULL; |
| 1368 | } |
| 1369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | /* |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 1371 | * If a hint addr is less than mmap_min_addr change hint to be as |
| 1372 | * low as possible but still greater than mmap_min_addr |
| 1373 | */ |
| 1374 | static inline unsigned long round_hint_to_min(unsigned long hint) |
| 1375 | { |
| 1376 | hint &= PAGE_MASK; |
| 1377 | if (((void *)hint != NULL) && |
| 1378 | (hint < mmap_min_addr)) |
| 1379 | return PAGE_ALIGN(mmap_min_addr); |
| 1380 | return hint; |
| 1381 | } |
| 1382 | |
Davidlohr Bueso | 363ee17 | 2014-01-21 15:49:15 -0800 | [diff] [blame] | 1383 | static inline int mlock_future_check(struct mm_struct *mm, |
| 1384 | unsigned long flags, |
| 1385 | unsigned long len) |
| 1386 | { |
| 1387 | unsigned long locked, lock_limit; |
| 1388 | |
| 1389 | /* mlock MCL_FUTURE? */ |
| 1390 | if (flags & VM_LOCKED) { |
| 1391 | locked = len >> PAGE_SHIFT; |
| 1392 | locked += mm->locked_vm; |
| 1393 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
| 1394 | lock_limit >>= PAGE_SHIFT; |
| 1395 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) |
| 1396 | return -EAGAIN; |
| 1397 | } |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Linus Torvalds | 7a40374 | 2018-05-11 09:52:01 -0700 | [diff] [blame] | 1401 | static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) |
| 1402 | { |
| 1403 | if (S_ISREG(inode->i_mode)) |
Linus Torvalds | 4be6529 | 2018-05-19 09:29:11 -0700 | [diff] [blame] | 1404 | return MAX_LFS_FILESIZE; |
Linus Torvalds | 7a40374 | 2018-05-11 09:52:01 -0700 | [diff] [blame] | 1405 | |
| 1406 | if (S_ISBLK(inode->i_mode)) |
| 1407 | return MAX_LFS_FILESIZE; |
| 1408 | |
| 1409 | /* Special "we do even unsigned file positions" case */ |
| 1410 | if (file->f_mode & FMODE_UNSIGNED_OFFSET) |
| 1411 | return 0; |
| 1412 | |
| 1413 | /* Yes, random drivers might want more. But I'm tired of buggy drivers */ |
| 1414 | return ULONG_MAX; |
| 1415 | } |
| 1416 | |
| 1417 | static inline bool file_mmap_ok(struct file *file, struct inode *inode, |
| 1418 | unsigned long pgoff, unsigned long len) |
| 1419 | { |
| 1420 | u64 maxsize = file_mmap_size_max(file, inode); |
| 1421 | |
| 1422 | if (maxsize && len > maxsize) |
| 1423 | return false; |
| 1424 | maxsize -= len; |
| 1425 | if (pgoff > maxsize >> PAGE_SHIFT) |
| 1426 | return false; |
| 1427 | return true; |
| 1428 | } |
| 1429 | |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 1430 | /* |
Jianjun Kong | 27f5de7 | 2009-09-17 19:26:26 -0700 | [diff] [blame] | 1431 | * The caller must hold down_write(¤t->mm->mmap_sem). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | */ |
Oleg Nesterov | 1fcfd8d | 2015-09-09 15:39:29 -0700 | [diff] [blame] | 1433 | unsigned long do_mmap(struct file *file, unsigned long addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | unsigned long len, unsigned long prot, |
Oleg Nesterov | 1fcfd8d | 2015-09-09 15:39:29 -0700 | [diff] [blame] | 1435 | unsigned long flags, vm_flags_t vm_flags, |
| 1436 | unsigned long pgoff, unsigned long *populate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | { |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 1438 | struct mm_struct *mm = current->mm; |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 1439 | int pkey = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | |
Michel Lespinasse | 41badc1 | 2013-02-22 16:32:47 -0800 | [diff] [blame] | 1441 | *populate = 0; |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 1442 | |
Piotr Kwapulinski | e37609b | 2015-06-24 16:58:39 -0700 | [diff] [blame] | 1443 | if (!len) |
| 1444 | return -EINVAL; |
| 1445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | /* |
| 1447 | * Does the application expect PROT_READ to imply PROT_EXEC? |
| 1448 | * |
| 1449 | * (the exception is when the underlying filesystem is noexec |
| 1450 | * mounted, in which case we dont add PROT_EXEC.) |
| 1451 | */ |
| 1452 | if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) |
Eric W. Biederman | 90f8572 | 2015-06-29 14:42:03 -0500 | [diff] [blame] | 1453 | if (!(file && path_noexec(&file->f_path))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | prot |= PROT_EXEC; |
| 1455 | |
Eric Paris | 7cd9414 | 2007-11-26 18:47:40 -0500 | [diff] [blame] | 1456 | if (!(flags & MAP_FIXED)) |
| 1457 | addr = round_hint_to_min(addr); |
| 1458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | /* Careful about overflows.. */ |
| 1460 | len = PAGE_ALIGN(len); |
Al Viro | 9206de9 | 2009-12-03 15:23:11 -0500 | [diff] [blame] | 1461 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | return -ENOMEM; |
| 1463 | |
| 1464 | /* offset overflow? */ |
| 1465 | if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 1466 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | |
| 1468 | /* Too many mappings? */ |
| 1469 | if (mm->map_count > sysctl_max_map_count) |
| 1470 | return -ENOMEM; |
| 1471 | |
| 1472 | /* Obtain the address to map to. we verify (or select) it and ensure |
| 1473 | * that it represents a valid section of the address space. |
| 1474 | */ |
| 1475 | addr = get_unmapped_area(file, addr, len, pgoff, flags); |
Alexander Kuleshov | de1741a | 2015-11-05 18:46:54 -0800 | [diff] [blame] | 1476 | if (offset_in_page(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | return addr; |
| 1478 | |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 1479 | if (prot == PROT_EXEC) { |
| 1480 | pkey = execute_only_pkey(mm); |
| 1481 | if (pkey < 0) |
| 1482 | pkey = 0; |
| 1483 | } |
| 1484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | /* Do simple checking here so the lower-level routines won't have |
| 1486 | * to. we assume access permissions have been handled by the open |
| 1487 | * of the memory object, so we don't do any here. |
| 1488 | */ |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 1489 | vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
| 1491 | |
Huang Shijie | cdf7b34 | 2009-09-21 17:03:36 -0700 | [diff] [blame] | 1492 | if (flags & MAP_LOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | if (!can_do_mlock()) |
| 1494 | return -EPERM; |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 1495 | |
Davidlohr Bueso | 363ee17 | 2014-01-21 15:49:15 -0800 | [diff] [blame] | 1496 | if (mlock_future_check(mm, vm_flags, len)) |
| 1497 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (file) { |
Oleg Nesterov | 077bf22 | 2013-09-11 14:20:19 -0700 | [diff] [blame] | 1500 | struct inode *inode = file_inode(file); |
| 1501 | |
Linus Torvalds | 7a40374 | 2018-05-11 09:52:01 -0700 | [diff] [blame] | 1502 | if (!file_mmap_ok(file, inode, pgoff, len)) |
| 1503 | return -EOVERFLOW; |
| 1504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | switch (flags & MAP_TYPE) { |
| 1506 | case MAP_SHARED: |
| 1507 | if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) |
| 1508 | return -EACCES; |
| 1509 | |
| 1510 | /* |
| 1511 | * Make sure we don't allow writing to an append-only |
| 1512 | * file.. |
| 1513 | */ |
| 1514 | if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) |
| 1515 | return -EACCES; |
| 1516 | |
| 1517 | /* |
| 1518 | * Make sure there are no mandatory locks on the file. |
| 1519 | */ |
Jeff Layton | d7a0698 | 2014-03-10 09:54:15 -0400 | [diff] [blame] | 1520 | if (locks_verify_locked(file)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | return -EAGAIN; |
| 1522 | |
| 1523 | vm_flags |= VM_SHARED | VM_MAYSHARE; |
| 1524 | if (!(file->f_mode & FMODE_WRITE)) |
| 1525 | vm_flags &= ~(VM_MAYWRITE | VM_SHARED); |
| 1526 | |
| 1527 | /* fall through */ |
| 1528 | case MAP_PRIVATE: |
| 1529 | if (!(file->f_mode & FMODE_READ)) |
| 1530 | return -EACCES; |
Eric W. Biederman | 90f8572 | 2015-06-29 14:42:03 -0500 | [diff] [blame] | 1531 | if (path_noexec(&file->f_path)) { |
Linus Torvalds | 80c5606 | 2006-10-15 14:09:55 -0700 | [diff] [blame] | 1532 | if (vm_flags & VM_EXEC) |
| 1533 | return -EPERM; |
| 1534 | vm_flags &= ~VM_MAYEXEC; |
| 1535 | } |
Linus Torvalds | 80c5606 | 2006-10-15 14:09:55 -0700 | [diff] [blame] | 1536 | |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 1537 | if (!file->f_op->mmap) |
Linus Torvalds | 80c5606 | 2006-10-15 14:09:55 -0700 | [diff] [blame] | 1538 | return -ENODEV; |
Oleg Nesterov | b2c56e4 | 2013-09-11 14:20:18 -0700 | [diff] [blame] | 1539 | if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) |
| 1540 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | break; |
| 1542 | |
| 1543 | default: |
| 1544 | return -EINVAL; |
| 1545 | } |
| 1546 | } else { |
| 1547 | switch (flags & MAP_TYPE) { |
| 1548 | case MAP_SHARED: |
Oleg Nesterov | b2c56e4 | 2013-09-11 14:20:18 -0700 | [diff] [blame] | 1549 | if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) |
| 1550 | return -EINVAL; |
Tejun Heo | ce36394 | 2008-09-03 16:09:47 +0200 | [diff] [blame] | 1551 | /* |
| 1552 | * Ignore pgoff. |
| 1553 | */ |
| 1554 | pgoff = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | vm_flags |= VM_SHARED | VM_MAYSHARE; |
| 1556 | break; |
| 1557 | case MAP_PRIVATE: |
| 1558 | /* |
| 1559 | * Set pgoff according to addr for anon_vma. |
| 1560 | */ |
| 1561 | pgoff = addr >> PAGE_SHIFT; |
| 1562 | break; |
| 1563 | default: |
| 1564 | return -EINVAL; |
| 1565 | } |
| 1566 | } |
| 1567 | |
Michel Lespinasse | c22c0d6 | 2013-02-22 16:32:43 -0800 | [diff] [blame] | 1568 | /* |
| 1569 | * Set 'VM_NORESERVE' if we should not account for the |
| 1570 | * memory use of this mapping. |
| 1571 | */ |
| 1572 | if (flags & MAP_NORESERVE) { |
| 1573 | /* We honor MAP_NORESERVE if allowed to overcommit */ |
| 1574 | if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) |
| 1575 | vm_flags |= VM_NORESERVE; |
| 1576 | |
| 1577 | /* hugetlb applies strict overcommit unless MAP_NORESERVE */ |
| 1578 | if (file && is_file_hugepages(file)) |
| 1579 | vm_flags |= VM_NORESERVE; |
| 1580 | } |
| 1581 | |
| 1582 | addr = mmap_region(file, addr, len, vm_flags, pgoff); |
Michel Lespinasse | 09a9f1d | 2013-03-28 16:26:23 -0700 | [diff] [blame] | 1583 | if (!IS_ERR_VALUE(addr) && |
| 1584 | ((vm_flags & VM_LOCKED) || |
| 1585 | (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) |
Michel Lespinasse | 41badc1 | 2013-02-22 16:32:47 -0800 | [diff] [blame] | 1586 | *populate = len; |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 1587 | return addr; |
Miklos Szeredi | 0165ab4 | 2007-07-15 23:38:26 -0700 | [diff] [blame] | 1588 | } |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1589 | |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1590 | SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, |
| 1591 | unsigned long, prot, unsigned long, flags, |
| 1592 | unsigned long, fd, unsigned long, pgoff) |
| 1593 | { |
| 1594 | struct file *file = NULL; |
Chen Gang | 1e3ee14 | 2015-11-05 18:48:35 -0800 | [diff] [blame] | 1595 | unsigned long retval; |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1596 | |
| 1597 | if (!(flags & MAP_ANONYMOUS)) { |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 1598 | audit_mmap_fd(fd, flags); |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1599 | file = fget(fd); |
| 1600 | if (!file) |
Chen Gang | 1e3ee14 | 2015-11-05 18:48:35 -0800 | [diff] [blame] | 1601 | return -EBADF; |
Naoya Horiguchi | af73e4d | 2013-05-07 16:18:13 -0700 | [diff] [blame] | 1602 | if (is_file_hugepages(file)) |
| 1603 | len = ALIGN(len, huge_page_size(hstate_file(file))); |
Jörn Engel | 493af57 | 2013-07-08 16:00:26 -0700 | [diff] [blame] | 1604 | retval = -EINVAL; |
| 1605 | if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file))) |
| 1606 | goto out_fput; |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1607 | } else if (flags & MAP_HUGETLB) { |
| 1608 | struct user_struct *user = NULL; |
Andrew Morton | c103a4d | 2013-07-08 16:01:08 -0700 | [diff] [blame] | 1609 | struct hstate *hs; |
Naoya Horiguchi | af73e4d | 2013-05-07 16:18:13 -0700 | [diff] [blame] | 1610 | |
Andrew Morton | c103a4d | 2013-07-08 16:01:08 -0700 | [diff] [blame] | 1611 | hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK); |
Li Zefan | 091d0d5 | 2013-05-09 15:08:15 +0800 | [diff] [blame] | 1612 | if (!hs) |
| 1613 | return -EINVAL; |
| 1614 | |
| 1615 | len = ALIGN(len, huge_page_size(hs)); |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1616 | /* |
| 1617 | * VM_NORESERVE is used because the reservations will be |
| 1618 | * taken when vm_ops->mmap() is called |
| 1619 | * A dummy user value is used because we are not locking |
| 1620 | * memory so no accounting is necessary |
| 1621 | */ |
Naoya Horiguchi | af73e4d | 2013-05-07 16:18:13 -0700 | [diff] [blame] | 1622 | file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, |
Andi Kleen | 42d7395 | 2012-12-11 16:01:34 -0800 | [diff] [blame] | 1623 | VM_NORESERVE, |
| 1624 | &user, HUGETLB_ANONHUGE_INODE, |
| 1625 | (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1626 | if (IS_ERR(file)) |
| 1627 | return PTR_ERR(file); |
| 1628 | } |
| 1629 | |
| 1630 | flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); |
| 1631 | |
Michal Hocko | 9fbeb5a | 2016-05-23 16:25:30 -0700 | [diff] [blame] | 1632 | retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
Jörn Engel | 493af57 | 2013-07-08 16:00:26 -0700 | [diff] [blame] | 1633 | out_fput: |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1634 | if (file) |
| 1635 | fput(file); |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1636 | return retval; |
| 1637 | } |
| 1638 | |
Christoph Hellwig | a467937 | 2010-03-10 15:21:15 -0800 | [diff] [blame] | 1639 | #ifdef __ARCH_WANT_SYS_OLD_MMAP |
| 1640 | struct mmap_arg_struct { |
| 1641 | unsigned long addr; |
| 1642 | unsigned long len; |
| 1643 | unsigned long prot; |
| 1644 | unsigned long flags; |
| 1645 | unsigned long fd; |
| 1646 | unsigned long offset; |
| 1647 | }; |
| 1648 | |
| 1649 | SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) |
| 1650 | { |
| 1651 | struct mmap_arg_struct a; |
| 1652 | |
| 1653 | if (copy_from_user(&a, arg, sizeof(a))) |
| 1654 | return -EFAULT; |
Alexander Kuleshov | de1741a | 2015-11-05 18:46:54 -0800 | [diff] [blame] | 1655 | if (offset_in_page(a.offset)) |
Christoph Hellwig | a467937 | 2010-03-10 15:21:15 -0800 | [diff] [blame] | 1656 | return -EINVAL; |
| 1657 | |
| 1658 | return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, |
| 1659 | a.offset >> PAGE_SHIFT); |
| 1660 | } |
| 1661 | #endif /* __ARCH_WANT_SYS_OLD_MMAP */ |
| 1662 | |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 1663 | /* |
| 1664 | * Some shared mappigns will want the pages marked read-only |
| 1665 | * to track write events. If so, we'll downgrade vm_page_prot |
| 1666 | * to the private version (using protection_map[] without the |
| 1667 | * VM_SHARED bit). |
| 1668 | */ |
Andrea Arcangeli | 6d2329f | 2016-10-07 17:01:22 -0700 | [diff] [blame] | 1669 | int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 1670 | { |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 1671 | vm_flags_t vm_flags = vma->vm_flags; |
Kirill A. Shutemov | 8a04446 | 2015-09-22 14:59:12 -0700 | [diff] [blame] | 1672 | const struct vm_operations_struct *vm_ops = vma->vm_ops; |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 1673 | |
| 1674 | /* If it was private or non-writable, the write bit is already clear */ |
| 1675 | if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) |
| 1676 | return 0; |
| 1677 | |
| 1678 | /* The backer wishes to know when pages are first written to? */ |
Kirill A. Shutemov | 8a04446 | 2015-09-22 14:59:12 -0700 | [diff] [blame] | 1679 | if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 1680 | return 1; |
| 1681 | |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 1682 | /* The open routine did something to the protections that pgprot_modify |
| 1683 | * won't preserve? */ |
Andrea Arcangeli | 6d2329f | 2016-10-07 17:01:22 -0700 | [diff] [blame] | 1684 | if (pgprot_val(vm_page_prot) != |
| 1685 | pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 1686 | return 0; |
| 1687 | |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 1688 | /* Do we need to track softdirty? */ |
| 1689 | if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY)) |
| 1690 | return 1; |
| 1691 | |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 1692 | /* Specialty mapping? */ |
Konstantin Khlebnikov | 4b6e1e3 | 2012-10-08 16:28:40 -0700 | [diff] [blame] | 1693 | if (vm_flags & VM_PFNMAP) |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 1694 | return 0; |
| 1695 | |
| 1696 | /* Can the mapping track the dirty pages? */ |
| 1697 | return vma->vm_file && vma->vm_file->f_mapping && |
| 1698 | mapping_cap_account_dirty(vma->vm_file->f_mapping); |
| 1699 | } |
| 1700 | |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1701 | /* |
| 1702 | * We account for memory if it's a private writeable mapping, |
Mel Gorman | 5a6fe12 | 2009-02-10 14:02:27 +0000 | [diff] [blame] | 1703 | * not hugepages and VM_NORESERVE wasn't set. |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1704 | */ |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 1705 | static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1706 | { |
Mel Gorman | 5a6fe12 | 2009-02-10 14:02:27 +0000 | [diff] [blame] | 1707 | /* |
| 1708 | * hugetlb has its own accounting separate from the core VM |
| 1709 | * VM_HUGETLB may not be set yet so we cannot check for that flag. |
| 1710 | */ |
| 1711 | if (file && is_file_hugepages(file)) |
| 1712 | return 0; |
| 1713 | |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1714 | return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; |
| 1715 | } |
| 1716 | |
Miklos Szeredi | 0165ab4 | 2007-07-15 23:38:26 -0700 | [diff] [blame] | 1717 | unsigned long mmap_region(struct file *file, unsigned long addr, |
Michel Lespinasse | c22c0d6 | 2013-02-22 16:32:43 -0800 | [diff] [blame] | 1718 | unsigned long len, vm_flags_t vm_flags, unsigned long pgoff) |
Miklos Szeredi | 0165ab4 | 2007-07-15 23:38:26 -0700 | [diff] [blame] | 1719 | { |
| 1720 | struct mm_struct *mm = current->mm; |
| 1721 | struct vm_area_struct *vma, *prev; |
Miklos Szeredi | 0165ab4 | 2007-07-15 23:38:26 -0700 | [diff] [blame] | 1722 | int error; |
| 1723 | struct rb_node **rb_link, *rb_parent; |
| 1724 | unsigned long charged = 0; |
Miklos Szeredi | 0165ab4 | 2007-07-15 23:38:26 -0700 | [diff] [blame] | 1725 | |
Cyril Hrubis | e8420a8 | 2013-04-29 15:08:33 -0700 | [diff] [blame] | 1726 | /* Check against address space limit. */ |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 1727 | if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { |
Cyril Hrubis | e8420a8 | 2013-04-29 15:08:33 -0700 | [diff] [blame] | 1728 | unsigned long nr_pages; |
| 1729 | |
| 1730 | /* |
| 1731 | * MAP_FIXED may remove pages of mappings that intersects with |
| 1732 | * requested mapping. Account for the pages it would unmap. |
| 1733 | */ |
Cyril Hrubis | e8420a8 | 2013-04-29 15:08:33 -0700 | [diff] [blame] | 1734 | nr_pages = count_vma_pages_range(mm, addr, addr + len); |
| 1735 | |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 1736 | if (!may_expand_vm(mm, vm_flags, |
| 1737 | (len >> PAGE_SHIFT) - nr_pages)) |
Cyril Hrubis | e8420a8 | 2013-04-29 15:08:33 -0700 | [diff] [blame] | 1738 | return -ENOMEM; |
| 1739 | } |
| 1740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | /* Clear old maps */ |
Rasmus Villemoes | 9fcd145 | 2015-04-15 16:14:32 -0700 | [diff] [blame] | 1742 | while (find_vma_links(mm, addr, addr + len, &prev, &rb_link, |
| 1743 | &rb_parent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | if (do_munmap(mm, addr, len)) |
| 1745 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1748 | /* |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1749 | * Private writable mapping: check memory availability |
| 1750 | */ |
Mel Gorman | 5a6fe12 | 2009-02-10 14:02:27 +0000 | [diff] [blame] | 1751 | if (accountable_mapping(file, vm_flags)) { |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1752 | charged = len >> PAGE_SHIFT; |
Al Viro | 191c542 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 1753 | if (security_vm_enough_memory_mm(mm, charged)) |
Linus Torvalds | fc8744a | 2009-01-31 15:08:56 -0800 | [diff] [blame] | 1754 | return -ENOMEM; |
| 1755 | vm_flags |= VM_ACCOUNT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /* |
Linus Torvalds | de33c8d | 2009-01-29 17:46:42 -0800 | [diff] [blame] | 1759 | * Can we just expand an old mapping? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | */ |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 1761 | vma = vma_merge(mm, prev, addr, addr + len, vm_flags, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 1762 | NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX, NULL); |
Linus Torvalds | de33c8d | 2009-01-29 17:46:42 -0800 | [diff] [blame] | 1763 | if (vma) |
| 1764 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
| 1766 | /* |
| 1767 | * Determine the object being mapped and call the appropriate |
| 1768 | * specific mapper. the address has already been validated, but |
| 1769 | * not unmapped, but the maps are removed from the list. |
| 1770 | */ |
Pekka Enberg | c5e3b83 | 2006-03-25 03:06:43 -0800 | [diff] [blame] | 1771 | vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | if (!vma) { |
| 1773 | error = -ENOMEM; |
| 1774 | goto unacct_error; |
| 1775 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | |
| 1777 | vma->vm_mm = mm; |
| 1778 | vma->vm_start = addr; |
| 1779 | vma->vm_end = addr + len; |
| 1780 | vma->vm_flags = vm_flags; |
Coly Li | 3ed75eb | 2007-10-18 23:39:15 -0700 | [diff] [blame] | 1781 | vma->vm_page_prot = vm_get_page_prot(vm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | vma->vm_pgoff = pgoff; |
Laurent Dufour | 6315ccc | 2018-04-17 16:33:13 +0200 | [diff] [blame] | 1783 | INIT_VMA(vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | |
| 1785 | if (file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | if (vm_flags & VM_DENYWRITE) { |
| 1787 | error = deny_write_access(file); |
| 1788 | if (error) |
| 1789 | goto free_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | } |
David Herrmann | 4bb5f5d | 2014-08-08 14:25:25 -0700 | [diff] [blame] | 1791 | if (vm_flags & VM_SHARED) { |
| 1792 | error = mapping_map_writable(file->f_mapping); |
| 1793 | if (error) |
| 1794 | goto allow_write_and_free_vma; |
| 1795 | } |
| 1796 | |
| 1797 | /* ->mmap() can change vma->vm_file, but must guarantee that |
| 1798 | * vma_link() below can deny write-access if VM_DENYWRITE is set |
| 1799 | * and map writably if VM_SHARED is set. This usually means the |
| 1800 | * new file must not have been exposed to user-space, yet. |
| 1801 | */ |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 1802 | vma->vm_file = get_file(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | error = file->f_op->mmap(file, vma); |
| 1804 | if (error) |
| 1805 | goto unmap_and_free_vma; |
Huang Shijie | f8dbf0a7 | 2009-09-21 17:03:41 -0700 | [diff] [blame] | 1806 | |
| 1807 | /* Can addr have changed?? |
| 1808 | * |
| 1809 | * Answer: Yes, several device drivers can do it in their |
| 1810 | * f_op->mmap method. -DaveM |
Joonsoo Kim | 2897b4d | 2012-12-12 13:51:53 -0800 | [diff] [blame] | 1811 | * Bug: If addr is changed, prev, rb_link, rb_parent should |
| 1812 | * be updated for vma_link() |
Huang Shijie | f8dbf0a7 | 2009-09-21 17:03:41 -0700 | [diff] [blame] | 1813 | */ |
Joonsoo Kim | 2897b4d | 2012-12-12 13:51:53 -0800 | [diff] [blame] | 1814 | WARN_ON_ONCE(addr != vma->vm_start); |
| 1815 | |
Huang Shijie | f8dbf0a7 | 2009-09-21 17:03:41 -0700 | [diff] [blame] | 1816 | addr = vma->vm_start; |
Huang Shijie | f8dbf0a7 | 2009-09-21 17:03:41 -0700 | [diff] [blame] | 1817 | vm_flags = vma->vm_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | } else if (vm_flags & VM_SHARED) { |
| 1819 | error = shmem_zero_setup(vma); |
| 1820 | if (error) |
| 1821 | goto free_vma; |
| 1822 | } |
| 1823 | |
Linus Torvalds | de33c8d | 2009-01-29 17:46:42 -0800 | [diff] [blame] | 1824 | vma_link(mm, vma, prev, rb_link, rb_parent); |
Oleg Nesterov | 4d3d5b4 | 2008-04-28 02:12:10 -0700 | [diff] [blame] | 1825 | /* Once vma denies write, undo our temporary denial count */ |
David Herrmann | 4bb5f5d | 2014-08-08 14:25:25 -0700 | [diff] [blame] | 1826 | if (file) { |
| 1827 | if (vm_flags & VM_SHARED) |
| 1828 | mapping_unmap_writable(file->f_mapping); |
| 1829 | if (vm_flags & VM_DENYWRITE) |
| 1830 | allow_write_access(file); |
| 1831 | } |
Oleg Nesterov | e868677 | 2013-09-11 14:20:20 -0700 | [diff] [blame] | 1832 | file = vma->vm_file; |
Oleg Nesterov | 4d3d5b4 | 2008-04-28 02:12:10 -0700 | [diff] [blame] | 1833 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1834 | perf_event_mmap(vma); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 1835 | |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 1836 | vm_write_begin(vma); |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 1837 | vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | if (vm_flags & VM_LOCKED) { |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 1839 | if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) || |
| 1840 | vma == get_gate_vma(current->mm))) |
KOSAKI Motohiro | 06f9d8c | 2010-03-05 13:41:43 -0800 | [diff] [blame] | 1841 | mm->locked_vm += (len >> PAGE_SHIFT); |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 1842 | else |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 1843 | WRITE_ONCE(vma->vm_flags, |
| 1844 | vma->vm_flags & VM_LOCKED_CLEAR_MASK); |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 1845 | } |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 1846 | |
Oleg Nesterov | c7a3a88 | 2012-08-19 19:10:42 +0200 | [diff] [blame] | 1847 | if (file) |
| 1848 | uprobe_mmap(vma); |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 1849 | |
Cyrill Gorcunov | d9104d1 | 2013-09-11 14:22:24 -0700 | [diff] [blame] | 1850 | /* |
| 1851 | * New (or expanded) vma always get soft dirty status. |
| 1852 | * Otherwise user-space soft-dirty page tracker won't |
| 1853 | * be able to distinguish situation when vma area unmapped, |
| 1854 | * then new mapped in-place (which must be aimed as |
| 1855 | * a completely new data area). |
| 1856 | */ |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 1857 | WRITE_ONCE(vma->vm_flags, vma->vm_flags | VM_SOFTDIRTY); |
Cyrill Gorcunov | d9104d1 | 2013-09-11 14:22:24 -0700 | [diff] [blame] | 1858 | |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 1859 | vma_set_page_prot(vma); |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 1860 | vm_write_end(vma); |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 1861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | return addr; |
| 1863 | |
| 1864 | unmap_and_free_vma: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | vma->vm_file = NULL; |
| 1866 | fput(file); |
| 1867 | |
| 1868 | /* Undo any partial mapping done by a device driver. */ |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 1869 | unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); |
| 1870 | charged = 0; |
David Herrmann | 4bb5f5d | 2014-08-08 14:25:25 -0700 | [diff] [blame] | 1871 | if (vm_flags & VM_SHARED) |
| 1872 | mapping_unmap_writable(file->f_mapping); |
| 1873 | allow_write_and_free_vma: |
| 1874 | if (vm_flags & VM_DENYWRITE) |
| 1875 | allow_write_access(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | free_vma: |
| 1877 | kmem_cache_free(vm_area_cachep, vma); |
| 1878 | unacct_error: |
| 1879 | if (charged) |
| 1880 | vm_unacct_memory(charged); |
| 1881 | return error; |
| 1882 | } |
| 1883 | |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 1884 | unsigned long unmapped_area(struct vm_unmapped_area_info *info) |
| 1885 | { |
| 1886 | /* |
| 1887 | * We implement the search by looking for an rbtree node that |
| 1888 | * immediately follows a suitable gap. That is, |
| 1889 | * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length; |
| 1890 | * - gap_end = vma->vm_start >= info->low_limit + length; |
| 1891 | * - gap_end - gap_start >= length |
| 1892 | */ |
| 1893 | |
| 1894 | struct mm_struct *mm = current->mm; |
| 1895 | struct vm_area_struct *vma; |
| 1896 | unsigned long length, low_limit, high_limit, gap_start, gap_end; |
| 1897 | |
| 1898 | /* Adjust search length to account for worst case alignment overhead */ |
| 1899 | length = info->length + info->align_mask; |
| 1900 | if (length < info->length) |
| 1901 | return -ENOMEM; |
| 1902 | |
| 1903 | /* Adjust search limits by the desired length */ |
| 1904 | if (info->high_limit < length) |
| 1905 | return -ENOMEM; |
| 1906 | high_limit = info->high_limit - length; |
| 1907 | |
| 1908 | if (info->low_limit > high_limit) |
| 1909 | return -ENOMEM; |
| 1910 | low_limit = info->low_limit + length; |
| 1911 | |
| 1912 | /* Check if rbtree root looks promising */ |
| 1913 | if (RB_EMPTY_ROOT(&mm->mm_rb)) |
| 1914 | goto check_highest; |
| 1915 | vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); |
| 1916 | if (vma->rb_subtree_gap < length) |
| 1917 | goto check_highest; |
| 1918 | |
| 1919 | while (true) { |
| 1920 | /* Visit left subtree if it looks promising */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 1921 | gap_end = vm_start_gap(vma); |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 1922 | if (gap_end >= low_limit && vma->vm_rb.rb_left) { |
| 1923 | struct vm_area_struct *left = |
| 1924 | rb_entry(vma->vm_rb.rb_left, |
| 1925 | struct vm_area_struct, vm_rb); |
| 1926 | if (left->rb_subtree_gap >= length) { |
| 1927 | vma = left; |
| 1928 | continue; |
| 1929 | } |
| 1930 | } |
| 1931 | |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 1932 | gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0; |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 1933 | check_current: |
| 1934 | /* Check if current node has a suitable gap */ |
| 1935 | if (gap_start > high_limit) |
| 1936 | return -ENOMEM; |
Hugh Dickins | ce7fe85 | 2017-06-20 02:10:44 -0700 | [diff] [blame] | 1937 | if (gap_end >= low_limit && |
| 1938 | gap_end > gap_start && gap_end - gap_start >= length) |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 1939 | goto found; |
| 1940 | |
| 1941 | /* Visit right subtree if it looks promising */ |
| 1942 | if (vma->vm_rb.rb_right) { |
| 1943 | struct vm_area_struct *right = |
| 1944 | rb_entry(vma->vm_rb.rb_right, |
| 1945 | struct vm_area_struct, vm_rb); |
| 1946 | if (right->rb_subtree_gap >= length) { |
| 1947 | vma = right; |
| 1948 | continue; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | /* Go back up the rbtree to find next candidate node */ |
| 1953 | while (true) { |
| 1954 | struct rb_node *prev = &vma->vm_rb; |
| 1955 | if (!rb_parent(prev)) |
| 1956 | goto check_highest; |
| 1957 | vma = rb_entry(rb_parent(prev), |
| 1958 | struct vm_area_struct, vm_rb); |
| 1959 | if (prev == vma->vm_rb.rb_left) { |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 1960 | gap_start = vm_end_gap(vma->vm_prev); |
| 1961 | gap_end = vm_start_gap(vma); |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 1962 | goto check_current; |
| 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | check_highest: |
| 1968 | /* Check highest gap, which does not precede any rbtree node */ |
| 1969 | gap_start = mm->highest_vm_end; |
| 1970 | gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */ |
| 1971 | if (gap_start > high_limit) |
| 1972 | return -ENOMEM; |
| 1973 | |
| 1974 | found: |
| 1975 | /* We found a suitable gap. Clip it with the original low_limit. */ |
| 1976 | if (gap_start < info->low_limit) |
| 1977 | gap_start = info->low_limit; |
| 1978 | |
| 1979 | /* Adjust gap address to the desired alignment */ |
| 1980 | gap_start += (info->align_offset - gap_start) & info->align_mask; |
| 1981 | |
| 1982 | VM_BUG_ON(gap_start + info->length > info->high_limit); |
| 1983 | VM_BUG_ON(gap_start + info->length > gap_end); |
| 1984 | return gap_start; |
| 1985 | } |
| 1986 | |
| 1987 | unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) |
| 1988 | { |
| 1989 | struct mm_struct *mm = current->mm; |
| 1990 | struct vm_area_struct *vma; |
| 1991 | unsigned long length, low_limit, high_limit, gap_start, gap_end; |
| 1992 | |
| 1993 | /* Adjust search length to account for worst case alignment overhead */ |
| 1994 | length = info->length + info->align_mask; |
| 1995 | if (length < info->length) |
| 1996 | return -ENOMEM; |
| 1997 | |
| 1998 | /* |
| 1999 | * Adjust search limits by the desired length. |
| 2000 | * See implementation comment at top of unmapped_area(). |
| 2001 | */ |
| 2002 | gap_end = info->high_limit; |
| 2003 | if (gap_end < length) |
| 2004 | return -ENOMEM; |
| 2005 | high_limit = gap_end - length; |
| 2006 | |
| 2007 | if (info->low_limit > high_limit) |
| 2008 | return -ENOMEM; |
| 2009 | low_limit = info->low_limit + length; |
| 2010 | |
| 2011 | /* Check highest gap, which does not precede any rbtree node */ |
| 2012 | gap_start = mm->highest_vm_end; |
| 2013 | if (gap_start <= high_limit) |
| 2014 | goto found_highest; |
| 2015 | |
| 2016 | /* Check if rbtree root looks promising */ |
| 2017 | if (RB_EMPTY_ROOT(&mm->mm_rb)) |
| 2018 | return -ENOMEM; |
| 2019 | vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); |
| 2020 | if (vma->rb_subtree_gap < length) |
| 2021 | return -ENOMEM; |
| 2022 | |
| 2023 | while (true) { |
| 2024 | /* Visit right subtree if it looks promising */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2025 | gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0; |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2026 | if (gap_start <= high_limit && vma->vm_rb.rb_right) { |
| 2027 | struct vm_area_struct *right = |
| 2028 | rb_entry(vma->vm_rb.rb_right, |
| 2029 | struct vm_area_struct, vm_rb); |
| 2030 | if (right->rb_subtree_gap >= length) { |
| 2031 | vma = right; |
| 2032 | continue; |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | check_current: |
| 2037 | /* Check if current node has a suitable gap */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2038 | gap_end = vm_start_gap(vma); |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2039 | if (gap_end < low_limit) |
| 2040 | return -ENOMEM; |
Hugh Dickins | ce7fe85 | 2017-06-20 02:10:44 -0700 | [diff] [blame] | 2041 | if (gap_start <= high_limit && |
| 2042 | gap_end > gap_start && gap_end - gap_start >= length) |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2043 | goto found; |
| 2044 | |
| 2045 | /* Visit left subtree if it looks promising */ |
| 2046 | if (vma->vm_rb.rb_left) { |
| 2047 | struct vm_area_struct *left = |
| 2048 | rb_entry(vma->vm_rb.rb_left, |
| 2049 | struct vm_area_struct, vm_rb); |
| 2050 | if (left->rb_subtree_gap >= length) { |
| 2051 | vma = left; |
| 2052 | continue; |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | /* Go back up the rbtree to find next candidate node */ |
| 2057 | while (true) { |
| 2058 | struct rb_node *prev = &vma->vm_rb; |
| 2059 | if (!rb_parent(prev)) |
| 2060 | return -ENOMEM; |
| 2061 | vma = rb_entry(rb_parent(prev), |
| 2062 | struct vm_area_struct, vm_rb); |
| 2063 | if (prev == vma->vm_rb.rb_right) { |
| 2064 | gap_start = vma->vm_prev ? |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2065 | vm_end_gap(vma->vm_prev) : 0; |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2066 | goto check_current; |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | found: |
| 2072 | /* We found a suitable gap. Clip it with the original high_limit. */ |
| 2073 | if (gap_end > info->high_limit) |
| 2074 | gap_end = info->high_limit; |
| 2075 | |
| 2076 | found_highest: |
| 2077 | /* Compute highest gap address at the desired alignment */ |
| 2078 | gap_end -= info->length; |
| 2079 | gap_end -= (gap_end - info->align_offset) & info->align_mask; |
| 2080 | |
| 2081 | VM_BUG_ON(gap_end < info->low_limit); |
| 2082 | VM_BUG_ON(gap_end < gap_start); |
| 2083 | return gap_end; |
| 2084 | } |
| 2085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | /* Get an address range which is currently unmapped. |
| 2087 | * For shmat() with addr=0. |
| 2088 | * |
| 2089 | * Ugly calling convention alert: |
| 2090 | * Return value with the low bits set means error value, |
| 2091 | * ie |
| 2092 | * if (ret & ~PAGE_MASK) |
| 2093 | * error = ret; |
| 2094 | * |
| 2095 | * This function "knows" that -ENOMEM has the bits set. |
| 2096 | */ |
| 2097 | #ifndef HAVE_ARCH_UNMAPPED_AREA |
| 2098 | unsigned long |
| 2099 | arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 2100 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 2101 | { |
| 2102 | struct mm_struct *mm = current->mm; |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2103 | struct vm_area_struct *vma, *prev; |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2104 | struct vm_unmapped_area_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | |
Akira Takeuchi | 2afc745 | 2013-11-12 15:08:21 -0800 | [diff] [blame] | 2106 | if (len > TASK_SIZE - mmap_min_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | return -ENOMEM; |
| 2108 | |
Benjamin Herrenschmidt | 06abdfb | 2007-05-06 14:50:13 -0700 | [diff] [blame] | 2109 | if (flags & MAP_FIXED) |
| 2110 | return addr; |
| 2111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | if (addr) { |
| 2113 | addr = PAGE_ALIGN(addr); |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2114 | vma = find_vma_prev(mm, addr, &prev); |
Akira Takeuchi | 2afc745 | 2013-11-12 15:08:21 -0800 | [diff] [blame] | 2115 | if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2116 | (!vma || addr + len <= vm_start_gap(vma)) && |
| 2117 | (!prev || addr >= vm_end_gap(prev))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | return addr; |
| 2119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2121 | info.flags = 0; |
| 2122 | info.length = len; |
Heiko Carstens | 4e99b02 | 2013-11-12 15:07:54 -0800 | [diff] [blame] | 2123 | info.low_limit = mm->mmap_base; |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2124 | info.high_limit = TASK_SIZE; |
| 2125 | info.align_mask = 0; |
| 2126 | return vm_unmapped_area(&info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2128 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | /* |
| 2131 | * This mmap-allocator allocates new areas top-down from below the |
| 2132 | * stack's low limit (the base): |
| 2133 | */ |
| 2134 | #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN |
| 2135 | unsigned long |
| 2136 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 2137 | const unsigned long len, const unsigned long pgoff, |
| 2138 | const unsigned long flags) |
| 2139 | { |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2140 | struct vm_area_struct *vma, *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | struct mm_struct *mm = current->mm; |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2142 | unsigned long addr = addr0; |
| 2143 | struct vm_unmapped_area_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | |
| 2145 | /* requested length too big for entire address space */ |
Akira Takeuchi | 2afc745 | 2013-11-12 15:08:21 -0800 | [diff] [blame] | 2146 | if (len > TASK_SIZE - mmap_min_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | return -ENOMEM; |
| 2148 | |
Benjamin Herrenschmidt | 06abdfb | 2007-05-06 14:50:13 -0700 | [diff] [blame] | 2149 | if (flags & MAP_FIXED) |
| 2150 | return addr; |
| 2151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | /* requesting a specific address */ |
| 2153 | if (addr) { |
| 2154 | addr = PAGE_ALIGN(addr); |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2155 | vma = find_vma_prev(mm, addr, &prev); |
Akira Takeuchi | 2afc745 | 2013-11-12 15:08:21 -0800 | [diff] [blame] | 2156 | if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2157 | (!vma || addr + len <= vm_start_gap(vma)) && |
| 2158 | (!prev || addr >= vm_end_gap(prev))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | return addr; |
| 2160 | } |
| 2161 | |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2162 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 2163 | info.length = len; |
Akira Takeuchi | 2afc745 | 2013-11-12 15:08:21 -0800 | [diff] [blame] | 2164 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2165 | info.high_limit = mm->mmap_base; |
| 2166 | info.align_mask = 0; |
| 2167 | addr = vm_unmapped_area(&info); |
Xiao Guangrong | b716ad9 | 2012-03-21 16:33:56 -0700 | [diff] [blame] | 2168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | /* |
| 2170 | * A failed mmap() very likely causes application failure, |
| 2171 | * so fall back to the bottom-up function here. This scenario |
| 2172 | * can happen with large stack limits and large mmap() |
| 2173 | * allocations. |
| 2174 | */ |
Alexander Kuleshov | de1741a | 2015-11-05 18:46:54 -0800 | [diff] [blame] | 2175 | if (offset_in_page(addr)) { |
Michel Lespinasse | db4fbfb | 2012-12-11 16:01:49 -0800 | [diff] [blame] | 2176 | VM_BUG_ON(addr != -ENOMEM); |
| 2177 | info.flags = 0; |
| 2178 | info.low_limit = TASK_UNMAPPED_BASE; |
| 2179 | info.high_limit = TASK_SIZE; |
| 2180 | addr = vm_unmapped_area(&info); |
| 2181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | |
| 2183 | return addr; |
| 2184 | } |
| 2185 | #endif |
| 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | unsigned long |
| 2188 | get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, |
| 2189 | unsigned long pgoff, unsigned long flags) |
| 2190 | { |
Benjamin Herrenschmidt | 06abdfb | 2007-05-06 14:50:13 -0700 | [diff] [blame] | 2191 | unsigned long (*get_area)(struct file *, unsigned long, |
| 2192 | unsigned long, unsigned long, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | |
Al Viro | 9206de9 | 2009-12-03 15:23:11 -0500 | [diff] [blame] | 2194 | unsigned long error = arch_mmap_check(addr, len, flags); |
| 2195 | if (error) |
| 2196 | return error; |
| 2197 | |
| 2198 | /* Careful about overflows.. */ |
| 2199 | if (len > TASK_SIZE) |
| 2200 | return -ENOMEM; |
| 2201 | |
Benjamin Herrenschmidt | 06abdfb | 2007-05-06 14:50:13 -0700 | [diff] [blame] | 2202 | get_area = current->mm->get_unmapped_area; |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2203 | if (file) { |
| 2204 | if (file->f_op->get_unmapped_area) |
| 2205 | get_area = file->f_op->get_unmapped_area; |
| 2206 | } else if (flags & MAP_SHARED) { |
| 2207 | /* |
| 2208 | * mmap_region() will call shmem_zero_setup() to create a file, |
| 2209 | * so use shmem's get_unmapped_area in case it can be huge. |
| 2210 | * do_mmap_pgoff() will clear pgoff, so match alignment. |
| 2211 | */ |
| 2212 | pgoff = 0; |
| 2213 | get_area = shmem_get_unmapped_area; |
| 2214 | } |
| 2215 | |
Benjamin Herrenschmidt | 06abdfb | 2007-05-06 14:50:13 -0700 | [diff] [blame] | 2216 | addr = get_area(file, addr, len, pgoff, flags); |
| 2217 | if (IS_ERR_VALUE(addr)) |
| 2218 | return addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | |
Linus Torvalds | 07ab67c | 2005-05-19 22:43:37 -0700 | [diff] [blame] | 2220 | if (addr > TASK_SIZE - len) |
| 2221 | return -ENOMEM; |
Alexander Kuleshov | de1741a | 2015-11-05 18:46:54 -0800 | [diff] [blame] | 2222 | if (offset_in_page(addr)) |
Linus Torvalds | 07ab67c | 2005-05-19 22:43:37 -0700 | [diff] [blame] | 2223 | return -EINVAL; |
Benjamin Herrenschmidt | 06abdfb | 2007-05-06 14:50:13 -0700 | [diff] [blame] | 2224 | |
Al Viro | 9ac4ed4 | 2012-05-30 17:13:15 -0400 | [diff] [blame] | 2225 | error = security_mmap_addr(addr); |
| 2226 | return error ? error : addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | EXPORT_SYMBOL(get_unmapped_area); |
| 2230 | |
| 2231 | /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 2232 | static struct vm_area_struct *__find_vma(struct mm_struct *mm, |
| 2233 | unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | { |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2235 | struct rb_node *rb_node; |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 2236 | struct vm_area_struct *vma = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2238 | rb_node = mm->mm_rb.rb_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2240 | while (rb_node) { |
| 2241 | struct vm_area_struct *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2243 | tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb); |
Rajman Mekaco | 841e31e | 2012-05-29 15:06:21 -0700 | [diff] [blame] | 2244 | |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2245 | if (tmp->vm_end > addr) { |
| 2246 | vma = tmp; |
| 2247 | if (tmp->vm_start <= addr) |
| 2248 | break; |
| 2249 | rb_node = rb_node->rb_left; |
| 2250 | } else |
| 2251 | rb_node = rb_node->rb_right; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | } |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2253 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 2254 | return vma; |
| 2255 | } |
| 2256 | |
| 2257 | struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) |
| 2258 | { |
| 2259 | struct vm_area_struct *vma; |
| 2260 | |
| 2261 | /* Check the cache first. */ |
| 2262 | vma = vmacache_find(mm, addr); |
| 2263 | if (likely(vma)) |
| 2264 | return vma; |
| 2265 | |
| 2266 | vma = __find_vma(mm, addr); |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2267 | if (vma) |
| 2268 | vmacache_update(addr, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | return vma; |
| 2270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | EXPORT_SYMBOL(find_vma); |
| 2272 | |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 2273 | #ifdef CONFIG_SPECULATIVE_PAGE_FAULT |
| 2274 | struct vm_area_struct *get_vma(struct mm_struct *mm, unsigned long addr) |
| 2275 | { |
| 2276 | struct vm_area_struct *vma = NULL; |
| 2277 | |
| 2278 | read_lock(&mm->mm_rb_lock); |
| 2279 | vma = __find_vma(mm, addr); |
| 2280 | if (vma) |
| 2281 | atomic_inc(&vma->vm_ref_count); |
| 2282 | read_unlock(&mm->mm_rb_lock); |
| 2283 | |
| 2284 | return vma; |
| 2285 | } |
| 2286 | #endif |
| 2287 | |
KOSAKI Motohiro | 6bd4837 | 2012-01-10 15:08:07 -0800 | [diff] [blame] | 2288 | /* |
| 2289 | * Same as find_vma, but also return a pointer to the previous VMA in *pprev. |
KOSAKI Motohiro | 6bd4837 | 2012-01-10 15:08:07 -0800 | [diff] [blame] | 2290 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | struct vm_area_struct * |
| 2292 | find_vma_prev(struct mm_struct *mm, unsigned long addr, |
| 2293 | struct vm_area_struct **pprev) |
| 2294 | { |
KOSAKI Motohiro | 6bd4837 | 2012-01-10 15:08:07 -0800 | [diff] [blame] | 2295 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | |
KOSAKI Motohiro | 6bd4837 | 2012-01-10 15:08:07 -0800 | [diff] [blame] | 2297 | vma = find_vma(mm, addr); |
Mikulas Patocka | 83cd904 | 2012-03-04 19:52:03 -0500 | [diff] [blame] | 2298 | if (vma) { |
| 2299 | *pprev = vma->vm_prev; |
| 2300 | } else { |
| 2301 | struct rb_node *rb_node = mm->mm_rb.rb_node; |
| 2302 | *pprev = NULL; |
| 2303 | while (rb_node) { |
| 2304 | *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb); |
| 2305 | rb_node = rb_node->rb_right; |
| 2306 | } |
| 2307 | } |
KOSAKI Motohiro | 6bd4837 | 2012-01-10 15:08:07 -0800 | [diff] [blame] | 2308 | return vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | /* |
| 2312 | * Verify that the stack growth is acceptable and |
| 2313 | * update accounting. This is shared with both the |
| 2314 | * grow-up and grow-down cases. |
| 2315 | */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2316 | static int acct_stack_growth(struct vm_area_struct *vma, |
| 2317 | unsigned long size, unsigned long grow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | { |
| 2319 | struct mm_struct *mm = vma->vm_mm; |
| 2320 | struct rlimit *rlim = current->signal->rlim; |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2321 | unsigned long new_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | |
| 2323 | /* address space limit tests */ |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 2324 | if (!may_expand_vm(mm, vma->vm_flags, grow)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | return -ENOMEM; |
| 2326 | |
| 2327 | /* Stack limit test */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2328 | if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | return -ENOMEM; |
| 2330 | |
| 2331 | /* mlock limit tests */ |
| 2332 | if (vma->vm_flags & VM_LOCKED) { |
| 2333 | unsigned long locked; |
| 2334 | unsigned long limit; |
| 2335 | locked = mm->locked_vm + grow; |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 2336 | limit = READ_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur); |
Jiri Slaby | 59e99e5 | 2010-03-05 13:41:44 -0800 | [diff] [blame] | 2337 | limit >>= PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | if (locked > limit && !capable(CAP_IPC_LOCK)) |
| 2339 | return -ENOMEM; |
| 2340 | } |
| 2341 | |
Adam Litke | 0d59a01 | 2007-01-30 14:35:39 -0800 | [diff] [blame] | 2342 | /* Check to ensure the stack will not grow into a hugetlb-only region */ |
| 2343 | new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : |
| 2344 | vma->vm_end - size; |
| 2345 | if (is_hugepage_only_range(vma->vm_mm, new_start, size)) |
| 2346 | return -EFAULT; |
| 2347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | /* |
| 2349 | * Overcommit.. This must be the final test, as it will |
| 2350 | * update security statistics. |
| 2351 | */ |
Hugh Dickins | 05fa199 | 2009-04-16 21:58:12 +0100 | [diff] [blame] | 2352 | if (security_vm_enough_memory_mm(mm, grow)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | return -ENOMEM; |
| 2354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | return 0; |
| 2356 | } |
| 2357 | |
Hugh Dickins | 46dea3d | 2005-10-29 18:16:20 -0700 | [diff] [blame] | 2358 | #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | /* |
Hugh Dickins | 46dea3d | 2005-10-29 18:16:20 -0700 | [diff] [blame] | 2360 | * PA-RISC uses this for its stack; IA64 for its Register Backing Store. |
| 2361 | * vma is the last one with address > vma->vm_end. Have to extend vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | */ |
Hugh Dickins | 46dea3d | 2005-10-29 18:16:20 -0700 | [diff] [blame] | 2363 | int expand_upwards(struct vm_area_struct *vma, unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | { |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2365 | struct mm_struct *mm = vma->vm_mm; |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2366 | struct vm_area_struct *next; |
| 2367 | unsigned long gap_addr; |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2368 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | |
| 2370 | if (!(vma->vm_flags & VM_GROWSUP)) |
| 2371 | return -EFAULT; |
| 2372 | |
Helge Deller | 5d10ad62 | 2017-06-19 17:34:05 +0200 | [diff] [blame] | 2373 | /* Guard against exceeding limits of the address space. */ |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2374 | address &= PAGE_MASK; |
Helge Deller | 38dfd2e | 2017-07-14 14:49:38 -0700 | [diff] [blame] | 2375 | if (address >= (TASK_SIZE & PAGE_MASK)) |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2376 | return -ENOMEM; |
Helge Deller | 5d10ad62 | 2017-06-19 17:34:05 +0200 | [diff] [blame] | 2377 | address += PAGE_SIZE; |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2378 | |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2379 | /* Enforce stack_guard_gap */ |
| 2380 | gap_addr = address + stack_guard_gap; |
Helge Deller | 5d10ad62 | 2017-06-19 17:34:05 +0200 | [diff] [blame] | 2381 | |
| 2382 | /* Guard against overflow */ |
| 2383 | if (gap_addr < address || gap_addr > TASK_SIZE) |
| 2384 | gap_addr = TASK_SIZE; |
| 2385 | |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2386 | next = vma->vm_next; |
Michal Hocko | c57664b | 2017-07-10 15:49:51 -0700 | [diff] [blame] | 2387 | if (next && next->vm_start < gap_addr && |
| 2388 | (next->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) { |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2389 | if (!(next->vm_flags & VM_GROWSUP)) |
| 2390 | return -ENOMEM; |
| 2391 | /* Check that both stack segments have the same anon_vma? */ |
| 2392 | } |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2393 | |
| 2394 | /* We must make sure the anon_vma is allocated. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | if (unlikely(anon_vma_prepare(vma))) |
| 2396 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
| 2398 | /* |
| 2399 | * vma->vm_start/vm_end cannot change under us because the caller |
| 2400 | * is required to hold the mmap_sem in read mode. We need the |
| 2401 | * anon_vma lock to serialize against concurrent expand_stacks. |
| 2402 | */ |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2403 | anon_vma_lock_write(vma->anon_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | |
| 2405 | /* Somebody else might have raced and expanded it already */ |
| 2406 | if (address > vma->vm_end) { |
| 2407 | unsigned long size, grow; |
| 2408 | |
| 2409 | size = address - vma->vm_start; |
| 2410 | grow = (address - vma->vm_end) >> PAGE_SHIFT; |
| 2411 | |
Hugh Dickins | 42c36f6 | 2011-05-09 17:44:42 -0700 | [diff] [blame] | 2412 | error = -ENOMEM; |
| 2413 | if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { |
| 2414 | error = acct_stack_growth(vma, size, grow); |
| 2415 | if (!error) { |
Michel Lespinasse | 4128997 | 2012-12-12 13:52:25 -0800 | [diff] [blame] | 2416 | /* |
| 2417 | * vma_gap_update() doesn't support concurrent |
| 2418 | * updates, but we only hold a shared mmap_sem |
| 2419 | * lock here, so we need to protect against |
| 2420 | * concurrent vma expansions. |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2421 | * anon_vma_lock_write() doesn't help here, as |
Michel Lespinasse | 4128997 | 2012-12-12 13:52:25 -0800 | [diff] [blame] | 2422 | * we don't guarantee that all growable vmas |
| 2423 | * in a mm share the same root anon vma. |
| 2424 | * So, we reuse mm->page_table_lock to guard |
| 2425 | * against concurrent vma expansions. |
| 2426 | */ |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2427 | spin_lock(&mm->page_table_lock); |
Oleg Nesterov | 87e8827 | 2015-11-05 18:48:14 -0800 | [diff] [blame] | 2428 | if (vma->vm_flags & VM_LOCKED) |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2429 | mm->locked_vm += grow; |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 2430 | vm_stat_account(mm, vma->vm_flags, grow); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 2431 | anon_vma_interval_tree_pre_update_vma(vma); |
Hugh Dickins | 42c36f6 | 2011-05-09 17:44:42 -0700 | [diff] [blame] | 2432 | vma->vm_end = address; |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 2433 | anon_vma_interval_tree_post_update_vma(vma); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 2434 | if (vma->vm_next) |
| 2435 | vma_gap_update(vma->vm_next); |
| 2436 | else |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2437 | mm->highest_vm_end = vm_end_gap(vma); |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2438 | spin_unlock(&mm->page_table_lock); |
Michel Lespinasse | 4128997 | 2012-12-12 13:52:25 -0800 | [diff] [blame] | 2439 | |
Hugh Dickins | 42c36f6 | 2011-05-09 17:44:42 -0700 | [diff] [blame] | 2440 | perf_event_mmap(vma); |
| 2441 | } |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 2442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | } |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2444 | anon_vma_unlock_write(vma->anon_vma); |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 2445 | khugepaged_enter_vma_merge(vma, vma->vm_flags); |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2446 | validate_mm(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | return error; |
| 2448 | } |
Hugh Dickins | 46dea3d | 2005-10-29 18:16:20 -0700 | [diff] [blame] | 2449 | #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ |
| 2450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | /* |
| 2452 | * vma is the first one with address < vma->vm_start. Have to extend vma. |
| 2453 | */ |
Michal Hocko | d05f316 | 2011-05-24 17:11:44 -0700 | [diff] [blame] | 2454 | int expand_downwards(struct vm_area_struct *vma, |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 2455 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | { |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2457 | struct mm_struct *mm = vma->vm_mm; |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2458 | struct vm_area_struct *prev; |
| 2459 | unsigned long gap_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | int error; |
| 2461 | |
Eric Paris | 8869477 | 2007-11-26 18:47:26 -0500 | [diff] [blame] | 2462 | address &= PAGE_MASK; |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 2463 | error = security_mmap_addr(address); |
Eric Paris | 8869477 | 2007-11-26 18:47:26 -0500 | [diff] [blame] | 2464 | if (error) |
| 2465 | return error; |
| 2466 | |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2467 | /* Enforce stack_guard_gap */ |
| 2468 | gap_addr = address - stack_guard_gap; |
| 2469 | if (gap_addr > address) |
| 2470 | return -ENOMEM; |
| 2471 | prev = vma->vm_prev; |
Michal Hocko | c57664b | 2017-07-10 15:49:51 -0700 | [diff] [blame] | 2472 | if (prev && prev->vm_end > gap_addr && |
| 2473 | (prev->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) { |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2474 | if (!(prev->vm_flags & VM_GROWSDOWN)) |
| 2475 | return -ENOMEM; |
| 2476 | /* Check that both stack segments have the same anon_vma? */ |
| 2477 | } |
| 2478 | |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2479 | /* We must make sure the anon_vma is allocated. */ |
| 2480 | if (unlikely(anon_vma_prepare(vma))) |
| 2481 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | |
| 2483 | /* |
| 2484 | * vma->vm_start/vm_end cannot change under us because the caller |
| 2485 | * is required to hold the mmap_sem in read mode. We need the |
| 2486 | * anon_vma lock to serialize against concurrent expand_stacks. |
| 2487 | */ |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2488 | anon_vma_lock_write(vma->anon_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
| 2490 | /* Somebody else might have raced and expanded it already */ |
| 2491 | if (address < vma->vm_start) { |
| 2492 | unsigned long size, grow; |
| 2493 | |
| 2494 | size = vma->vm_end - address; |
| 2495 | grow = (vma->vm_start - address) >> PAGE_SHIFT; |
| 2496 | |
Linus Torvalds | a626ca6 | 2011-04-13 08:07:28 -0700 | [diff] [blame] | 2497 | error = -ENOMEM; |
| 2498 | if (grow <= vma->vm_pgoff) { |
| 2499 | error = acct_stack_growth(vma, size, grow); |
| 2500 | if (!error) { |
Michel Lespinasse | 4128997 | 2012-12-12 13:52:25 -0800 | [diff] [blame] | 2501 | /* |
| 2502 | * vma_gap_update() doesn't support concurrent |
| 2503 | * updates, but we only hold a shared mmap_sem |
| 2504 | * lock here, so we need to protect against |
| 2505 | * concurrent vma expansions. |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2506 | * anon_vma_lock_write() doesn't help here, as |
Michel Lespinasse | 4128997 | 2012-12-12 13:52:25 -0800 | [diff] [blame] | 2507 | * we don't guarantee that all growable vmas |
| 2508 | * in a mm share the same root anon vma. |
| 2509 | * So, we reuse mm->page_table_lock to guard |
| 2510 | * against concurrent vma expansions. |
| 2511 | */ |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2512 | spin_lock(&mm->page_table_lock); |
Oleg Nesterov | 87e8827 | 2015-11-05 18:48:14 -0800 | [diff] [blame] | 2513 | if (vma->vm_flags & VM_LOCKED) |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2514 | mm->locked_vm += grow; |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 2515 | vm_stat_account(mm, vma->vm_flags, grow); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 2516 | anon_vma_interval_tree_pre_update_vma(vma); |
Laurent Dufour | dd2b465 | 2018-04-17 16:33:15 +0200 | [diff] [blame] | 2517 | WRITE_ONCE(vma->vm_start, address); |
| 2518 | WRITE_ONCE(vma->vm_pgoff, vma->vm_pgoff - grow); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 2519 | anon_vma_interval_tree_post_update_vma(vma); |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 2520 | vma_gap_update(vma); |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2521 | spin_unlock(&mm->page_table_lock); |
Michel Lespinasse | 4128997 | 2012-12-12 13:52:25 -0800 | [diff] [blame] | 2522 | |
Linus Torvalds | a626ca6 | 2011-04-13 08:07:28 -0700 | [diff] [blame] | 2523 | perf_event_mmap(vma); |
| 2524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | } |
| 2526 | } |
Konstantin Khlebnikov | 12352d3 | 2016-02-05 15:36:50 -0800 | [diff] [blame] | 2527 | anon_vma_unlock_write(vma->anon_vma); |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 2528 | khugepaged_enter_vma_merge(vma, vma->vm_flags); |
Oleg Nesterov | 0935781 | 2015-11-05 18:48:17 -0800 | [diff] [blame] | 2529 | validate_mm(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | return error; |
| 2531 | } |
| 2532 | |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2533 | /* enforced gap between the expanding stack and other mappings. */ |
| 2534 | unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; |
| 2535 | |
| 2536 | static int __init cmdline_parse_stack_guard_gap(char *p) |
| 2537 | { |
| 2538 | unsigned long val; |
| 2539 | char *endptr; |
| 2540 | |
| 2541 | val = simple_strtoul(p, &endptr, 10); |
| 2542 | if (!*endptr) |
| 2543 | stack_guard_gap = val << PAGE_SHIFT; |
| 2544 | |
| 2545 | return 0; |
| 2546 | } |
| 2547 | __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); |
| 2548 | |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 2549 | #ifdef CONFIG_STACK_GROWSUP |
| 2550 | int expand_stack(struct vm_area_struct *vma, unsigned long address) |
| 2551 | { |
| 2552 | return expand_upwards(vma, address); |
| 2553 | } |
| 2554 | |
| 2555 | struct vm_area_struct * |
| 2556 | find_extend_vma(struct mm_struct *mm, unsigned long addr) |
| 2557 | { |
| 2558 | struct vm_area_struct *vma, *prev; |
| 2559 | |
| 2560 | addr &= PAGE_MASK; |
| 2561 | vma = find_vma_prev(mm, addr, &prev); |
| 2562 | if (vma && (vma->vm_start <= addr)) |
| 2563 | return vma; |
Denys Vlasenko | 1c12718 | 2008-11-12 01:24:41 +0100 | [diff] [blame] | 2564 | if (!prev || expand_stack(prev, addr)) |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 2565 | return NULL; |
Michel Lespinasse | cea10a1 | 2013-02-22 16:32:44 -0800 | [diff] [blame] | 2566 | if (prev->vm_flags & VM_LOCKED) |
Kirill A. Shutemov | fc05f56 | 2015-04-14 15:44:39 -0700 | [diff] [blame] | 2567 | populate_vma_page_range(prev, addr, prev->vm_end, NULL); |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 2568 | return prev; |
| 2569 | } |
| 2570 | #else |
| 2571 | int expand_stack(struct vm_area_struct *vma, unsigned long address) |
| 2572 | { |
| 2573 | return expand_downwards(vma, address); |
| 2574 | } |
| 2575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | struct vm_area_struct * |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2577 | find_extend_vma(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | { |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2579 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | unsigned long start; |
| 2581 | |
| 2582 | addr &= PAGE_MASK; |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2583 | vma = find_vma(mm, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | if (!vma) |
| 2585 | return NULL; |
| 2586 | if (vma->vm_start <= addr) |
| 2587 | return vma; |
| 2588 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 2589 | return NULL; |
| 2590 | start = vma->vm_start; |
| 2591 | if (expand_stack(vma, addr)) |
| 2592 | return NULL; |
Michel Lespinasse | cea10a1 | 2013-02-22 16:32:44 -0800 | [diff] [blame] | 2593 | if (vma->vm_flags & VM_LOCKED) |
Kirill A. Shutemov | fc05f56 | 2015-04-14 15:44:39 -0700 | [diff] [blame] | 2594 | populate_vma_page_range(vma, addr, start, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | return vma; |
| 2596 | } |
| 2597 | #endif |
| 2598 | |
Jesse Barnes | e1d6d01 | 2014-12-12 16:55:27 -0800 | [diff] [blame] | 2599 | EXPORT_SYMBOL_GPL(find_extend_vma); |
| 2600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | /* |
Hugh Dickins | 2c0b381 | 2005-10-29 18:15:56 -0700 | [diff] [blame] | 2602 | * Ok - we have the memory areas we should free on the vma list, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | * so release them, and do the vma updates. |
Hugh Dickins | 2c0b381 | 2005-10-29 18:15:56 -0700 | [diff] [blame] | 2604 | * |
| 2605 | * Called with the mm semaphore held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | */ |
Hugh Dickins | 2c0b381 | 2005-10-29 18:15:56 -0700 | [diff] [blame] | 2607 | static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | { |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 2609 | unsigned long nr_accounted = 0; |
| 2610 | |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 2611 | /* Update high watermark before we lower total_vm */ |
| 2612 | update_hiwater_vm(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | do { |
Hugh Dickins | 2c0b381 | 2005-10-29 18:15:56 -0700 | [diff] [blame] | 2614 | long nrpages = vma_pages(vma); |
| 2615 | |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 2616 | if (vma->vm_flags & VM_ACCOUNT) |
| 2617 | nr_accounted += nrpages; |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 2618 | vm_stat_account(mm, vma->vm_flags, -nrpages); |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 2619 | vma = remove_vma(vma); |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2620 | } while (vma); |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 2621 | vm_unacct_memory(nr_accounted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | validate_mm(mm); |
| 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | * Get rid of page table information in the indicated region. |
| 2627 | * |
Paolo 'Blaisorblade' Giarrusso | f10df68 | 2005-09-21 09:55:37 -0700 | [diff] [blame] | 2628 | * Called with the mm semaphore held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | */ |
| 2630 | static void unmap_region(struct mm_struct *mm, |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 2631 | struct vm_area_struct *vma, struct vm_area_struct *prev, |
| 2632 | unsigned long start, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | { |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2634 | struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap; |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 2635 | struct mmu_gather tlb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | |
| 2637 | lru_add_drain(); |
Linus Torvalds | 2b04725 | 2013-08-15 11:42:25 -0700 | [diff] [blame] | 2638 | tlb_gather_mmu(&tlb, mm, start, end); |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 2639 | update_hiwater_rss(mm); |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 2640 | unmap_vmas(&tlb, vma, start, end); |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 2641 | free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, |
Hugh Dickins | 6ee8630 | 2013-04-29 15:07:44 -0700 | [diff] [blame] | 2642 | next ? next->vm_start : USER_PGTABLES_CEILING); |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 2643 | tlb_finish_mmu(&tlb, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | /* |
| 2647 | * Create a list of vma's touched by the unmap, removing them from the mm's |
| 2648 | * vma list as we go.. |
| 2649 | */ |
| 2650 | static void |
| 2651 | detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, |
| 2652 | struct vm_area_struct *prev, unsigned long end) |
| 2653 | { |
| 2654 | struct vm_area_struct **insertion_point; |
| 2655 | struct vm_area_struct *tail_vma = NULL; |
| 2656 | |
| 2657 | insertion_point = (prev ? &prev->vm_next : &mm->mmap); |
Linus Torvalds | 297c5ee | 2010-08-20 16:24:55 -0700 | [diff] [blame] | 2658 | vma->vm_prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | do { |
Laurent Dufour | d817384 | 2018-04-17 16:33:23 +0200 | [diff] [blame] | 2660 | vma_rb_erase(vma, mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | mm->map_count--; |
| 2662 | tail_vma = vma; |
| 2663 | vma = vma->vm_next; |
| 2664 | } while (vma && vma->vm_start < end); |
| 2665 | *insertion_point = vma; |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 2666 | if (vma) { |
Linus Torvalds | 297c5ee | 2010-08-20 16:24:55 -0700 | [diff] [blame] | 2667 | vma->vm_prev = prev; |
Michel Lespinasse | d373718 | 2012-12-11 16:01:38 -0800 | [diff] [blame] | 2668 | vma_gap_update(vma); |
| 2669 | } else |
Hugh Dickins | cfc0eb40 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 2670 | mm->highest_vm_end = prev ? vm_end_gap(prev) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | tail_vma->vm_next = NULL; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2672 | |
| 2673 | /* Kill the cache */ |
| 2674 | vmacache_invalidate(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | /* |
KOSAKI Motohiro | 659ace5 | 2009-12-14 17:57:56 -0800 | [diff] [blame] | 2678 | * __split_vma() bypasses sysctl_max_map_count checking. We use this on the |
| 2679 | * munmap path where it doesn't make sense to fail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | */ |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2681 | static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | unsigned long addr, int new_below) |
| 2683 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | struct vm_area_struct *new; |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 2685 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | |
Dan Williams | cebe139 | 2017-11-29 16:10:28 -0800 | [diff] [blame] | 2687 | if (vma->vm_ops && vma->vm_ops->split) { |
| 2688 | err = vma->vm_ops->split(vma, addr); |
| 2689 | if (err) |
| 2690 | return err; |
| 2691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 2693 | new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | if (!new) |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 2695 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | |
| 2697 | /* most fields are the same, copy all, and then fixup */ |
| 2698 | *new = *vma; |
| 2699 | |
Laurent Dufour | 6315ccc | 2018-04-17 16:33:13 +0200 | [diff] [blame] | 2700 | INIT_VMA(new); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | if (new_below) |
| 2703 | new->vm_end = addr; |
| 2704 | else { |
| 2705 | new->vm_start = addr; |
| 2706 | new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); |
| 2707 | } |
| 2708 | |
Oleg Nesterov | ef0855d | 2013-09-11 14:20:14 -0700 | [diff] [blame] | 2709 | err = vma_dup_policy(vma, new); |
| 2710 | if (err) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2711 | goto out_free_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | |
Daniel Forrest | c4ea95d | 2014-12-02 15:59:42 -0800 | [diff] [blame] | 2713 | err = anon_vma_clone(new, vma); |
| 2714 | if (err) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2715 | goto out_free_mpol; |
| 2716 | |
Konstantin Khlebnikov | e9714ac | 2012-10-08 16:28:54 -0700 | [diff] [blame] | 2717 | if (new->vm_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | get_file(new->vm_file); |
| 2719 | |
| 2720 | if (new->vm_ops && new->vm_ops->open) |
| 2721 | new->vm_ops->open(new); |
| 2722 | |
| 2723 | if (new_below) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2724 | err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | ((addr - new->vm_start) >> PAGE_SHIFT), new); |
| 2726 | else |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2727 | err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2729 | /* Success. */ |
| 2730 | if (!err) |
| 2731 | return 0; |
| 2732 | |
| 2733 | /* Clean everything up if vma_adjust failed. */ |
Rik van Riel | 5892753 | 2010-04-26 12:33:03 -0400 | [diff] [blame] | 2734 | if (new->vm_ops && new->vm_ops->close) |
| 2735 | new->vm_ops->close(new); |
Konstantin Khlebnikov | e9714ac | 2012-10-08 16:28:54 -0700 | [diff] [blame] | 2736 | if (new->vm_file) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2737 | fput(new->vm_file); |
Andrea Arcangeli | 2aeadc3 | 2010-09-22 13:05:12 -0700 | [diff] [blame] | 2738 | unlink_anon_vmas(new); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2739 | out_free_mpol: |
Oleg Nesterov | ef0855d | 2013-09-11 14:20:14 -0700 | [diff] [blame] | 2740 | mpol_put(vma_policy(new)); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2741 | out_free_vma: |
| 2742 | kmem_cache_free(vm_area_cachep, new); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2743 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
KOSAKI Motohiro | 659ace5 | 2009-12-14 17:57:56 -0800 | [diff] [blame] | 2746 | /* |
| 2747 | * Split a vma into two pieces at address 'addr', a new vma is allocated |
| 2748 | * either for the first part or the tail. |
| 2749 | */ |
| 2750 | int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, |
| 2751 | unsigned long addr, int new_below) |
| 2752 | { |
| 2753 | if (mm->map_count >= sysctl_max_map_count) |
| 2754 | return -ENOMEM; |
| 2755 | |
| 2756 | return __split_vma(mm, vma, addr, new_below); |
| 2757 | } |
| 2758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | /* Munmap is split into 2 main parts -- this part which finds |
| 2760 | * what needs doing, and the areas themselves, which do the |
| 2761 | * work. This now handles partial unmappings. |
| 2762 | * Jeremy Fitzhardinge <jeremy@goop.org> |
| 2763 | */ |
| 2764 | int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) |
| 2765 | { |
| 2766 | unsigned long end; |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2767 | struct vm_area_struct *vma, *prev, *last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
Alexander Kuleshov | de1741a | 2015-11-05 18:46:54 -0800 | [diff] [blame] | 2769 | if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | return -EINVAL; |
| 2771 | |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2772 | len = PAGE_ALIGN(len); |
| 2773 | if (len == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | return -EINVAL; |
| 2775 | |
| 2776 | /* Find the first overlapping VMA */ |
Linus Torvalds | 9be34c9 | 2011-06-16 00:35:09 -0700 | [diff] [blame] | 2777 | vma = find_vma(mm, start); |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2778 | if (!vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | return 0; |
Linus Torvalds | 9be34c9 | 2011-06-16 00:35:09 -0700 | [diff] [blame] | 2780 | prev = vma->vm_prev; |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2781 | /* we have start < vma->vm_end */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | |
| 2783 | /* if it doesn't overlap, we have nothing.. */ |
| 2784 | end = start + len; |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2785 | if (vma->vm_start >= end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | return 0; |
| 2787 | |
| 2788 | /* |
| 2789 | * If we need to split any vma, do it now to save pain later. |
| 2790 | * |
| 2791 | * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially |
| 2792 | * unmapped vm_area_struct will remain in use: so lower split_vma |
| 2793 | * places tmp vma above, and higher split_vma places tmp vma below. |
| 2794 | */ |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2795 | if (start > vma->vm_start) { |
KOSAKI Motohiro | 659ace5 | 2009-12-14 17:57:56 -0800 | [diff] [blame] | 2796 | int error; |
| 2797 | |
| 2798 | /* |
| 2799 | * Make sure that map_count on return from munmap() will |
| 2800 | * not exceed its limit; but let map_count go just above |
| 2801 | * its limit temporarily, to help free resources as expected. |
| 2802 | */ |
| 2803 | if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) |
| 2804 | return -ENOMEM; |
| 2805 | |
| 2806 | error = __split_vma(mm, vma, start, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | if (error) |
| 2808 | return error; |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2809 | prev = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | /* Does it split the last one? */ |
| 2813 | last = find_vma(mm, end); |
| 2814 | if (last && end > last->vm_start) { |
KOSAKI Motohiro | 659ace5 | 2009-12-14 17:57:56 -0800 | [diff] [blame] | 2815 | int error = __split_vma(mm, last, end, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | if (error) |
| 2817 | return error; |
| 2818 | } |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2819 | vma = prev ? prev->vm_next : mm->mmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | |
| 2821 | /* |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 2822 | * unlock any mlock()ed ranges before detaching vmas |
| 2823 | */ |
| 2824 | if (mm->locked_vm) { |
| 2825 | struct vm_area_struct *tmp = vma; |
| 2826 | while (tmp && tmp->vm_start < end) { |
| 2827 | if (tmp->vm_flags & VM_LOCKED) { |
| 2828 | mm->locked_vm -= vma_pages(tmp); |
| 2829 | munlock_vma_pages_all(tmp); |
| 2830 | } |
| 2831 | tmp = tmp->vm_next; |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | * Remove the vma's, and unmap the actual pages |
| 2837 | */ |
Hugh Dickins | 146425a | 2005-04-19 13:29:18 -0700 | [diff] [blame] | 2838 | detach_vmas_to_be_unmapped(mm, vma, prev, end); |
| 2839 | unmap_region(mm, vma, prev, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | |
Dave Hansen | 1de4fa1 | 2014-11-14 07:18:31 -0800 | [diff] [blame] | 2841 | arch_unmap(mm, vma, start, end); |
| 2842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | /* Fix up all other VM information */ |
Hugh Dickins | 2c0b381 | 2005-10-29 18:15:56 -0700 | [diff] [blame] | 2844 | remove_vma_list(mm, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | |
| 2846 | return 0; |
| 2847 | } |
Guenter Roeck | abfb486 | 2016-03-24 10:39:14 -0700 | [diff] [blame] | 2848 | EXPORT_SYMBOL(do_munmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | |
Al Viro | bfce281 | 2012-04-20 21:57:04 -0400 | [diff] [blame] | 2850 | int vm_munmap(unsigned long start, size_t len) |
Linus Torvalds | a46ef99 | 2012-04-20 16:20:01 -0700 | [diff] [blame] | 2851 | { |
| 2852 | int ret; |
Al Viro | bfce281 | 2012-04-20 21:57:04 -0400 | [diff] [blame] | 2853 | struct mm_struct *mm = current->mm; |
Linus Torvalds | a46ef99 | 2012-04-20 16:20:01 -0700 | [diff] [blame] | 2854 | |
Michal Hocko | ae79878 | 2016-05-23 16:25:33 -0700 | [diff] [blame] | 2855 | if (down_write_killable(&mm->mmap_sem)) |
| 2856 | return -EINTR; |
| 2857 | |
Linus Torvalds | a46ef99 | 2012-04-20 16:20:01 -0700 | [diff] [blame] | 2858 | ret = do_munmap(mm, start, len); |
| 2859 | up_write(&mm->mmap_sem); |
| 2860 | return ret; |
| 2861 | } |
| 2862 | EXPORT_SYMBOL(vm_munmap); |
| 2863 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 2864 | SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | { |
Michal Hocko | dc0ef0d | 2016-05-23 16:25:27 -0700 | [diff] [blame] | 2866 | int ret; |
| 2867 | struct mm_struct *mm = current->mm; |
| 2868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | profile_munmap(addr); |
Michal Hocko | dc0ef0d | 2016-05-23 16:25:27 -0700 | [diff] [blame] | 2870 | if (down_write_killable(&mm->mmap_sem)) |
| 2871 | return -EINTR; |
| 2872 | ret = do_munmap(mm, addr, len); |
| 2873 | up_write(&mm->mmap_sem); |
| 2874 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2877 | |
| 2878 | /* |
| 2879 | * Emulation of deprecated remap_file_pages() syscall. |
| 2880 | */ |
| 2881 | SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, |
| 2882 | unsigned long, prot, unsigned long, pgoff, unsigned long, flags) |
| 2883 | { |
| 2884 | |
| 2885 | struct mm_struct *mm = current->mm; |
| 2886 | struct vm_area_struct *vma; |
| 2887 | unsigned long populate = 0; |
| 2888 | unsigned long ret = -EINVAL; |
| 2889 | struct file *file; |
| 2890 | |
Joe Perches | 756a025f0 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 2891 | pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n", |
| 2892 | current->comm, current->pid); |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2893 | |
| 2894 | if (prot) |
| 2895 | return ret; |
| 2896 | start = start & PAGE_MASK; |
| 2897 | size = size & PAGE_MASK; |
| 2898 | |
| 2899 | if (start + size <= start) |
| 2900 | return ret; |
| 2901 | |
| 2902 | /* Does pgoff wrap? */ |
| 2903 | if (pgoff + (size >> PAGE_SHIFT) < pgoff) |
| 2904 | return ret; |
| 2905 | |
Michal Hocko | dc0ef0d | 2016-05-23 16:25:27 -0700 | [diff] [blame] | 2906 | if (down_write_killable(&mm->mmap_sem)) |
| 2907 | return -EINTR; |
| 2908 | |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2909 | vma = find_vma(mm, start); |
| 2910 | |
| 2911 | if (!vma || !(vma->vm_flags & VM_SHARED)) |
| 2912 | goto out; |
| 2913 | |
Kirill A. Shutemov | 48f7df3 | 2016-02-17 13:11:15 -0800 | [diff] [blame] | 2914 | if (start < vma->vm_start) |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2915 | goto out; |
| 2916 | |
Kirill A. Shutemov | 48f7df3 | 2016-02-17 13:11:15 -0800 | [diff] [blame] | 2917 | if (start + size > vma->vm_end) { |
| 2918 | struct vm_area_struct *next; |
| 2919 | |
| 2920 | for (next = vma->vm_next; next; next = next->vm_next) { |
| 2921 | /* hole between vmas ? */ |
| 2922 | if (next->vm_start != next->vm_prev->vm_end) |
| 2923 | goto out; |
| 2924 | |
| 2925 | if (next->vm_file != vma->vm_file) |
| 2926 | goto out; |
| 2927 | |
| 2928 | if (next->vm_flags != vma->vm_flags) |
| 2929 | goto out; |
| 2930 | |
| 2931 | if (start + size <= next->vm_end) |
| 2932 | break; |
| 2933 | } |
| 2934 | |
| 2935 | if (!next) |
| 2936 | goto out; |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; |
| 2940 | prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; |
| 2941 | prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; |
| 2942 | |
| 2943 | flags &= MAP_NONBLOCK; |
| 2944 | flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; |
| 2945 | if (vma->vm_flags & VM_LOCKED) { |
Kirill A. Shutemov | 48f7df3 | 2016-02-17 13:11:15 -0800 | [diff] [blame] | 2946 | struct vm_area_struct *tmp; |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2947 | flags |= MAP_LOCKED; |
Kirill A. Shutemov | 48f7df3 | 2016-02-17 13:11:15 -0800 | [diff] [blame] | 2948 | |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2949 | /* drop PG_Mlocked flag for over-mapped range */ |
Kirill A. Shutemov | 48f7df3 | 2016-02-17 13:11:15 -0800 | [diff] [blame] | 2950 | for (tmp = vma; tmp->vm_start >= start + size; |
| 2951 | tmp = tmp->vm_next) { |
Kirill A. Shutemov | 9a73f61 | 2016-07-26 15:25:53 -0700 | [diff] [blame] | 2952 | /* |
| 2953 | * Split pmd and munlock page on the border |
| 2954 | * of the range. |
| 2955 | */ |
| 2956 | vma_adjust_trans_huge(tmp, start, start + size, 0); |
| 2957 | |
Kirill A. Shutemov | 48f7df3 | 2016-02-17 13:11:15 -0800 | [diff] [blame] | 2958 | munlock_vma_pages_range(tmp, |
| 2959 | max(tmp->vm_start, start), |
| 2960 | min(tmp->vm_end, start + size)); |
| 2961 | } |
Kirill A. Shutemov | c8d78c1 | 2015-02-10 14:09:46 -0800 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | file = get_file(vma->vm_file); |
| 2965 | ret = do_mmap_pgoff(vma->vm_file, start, size, |
| 2966 | prot, flags, pgoff, &populate); |
| 2967 | fput(file); |
| 2968 | out: |
| 2969 | up_write(&mm->mmap_sem); |
| 2970 | if (populate) |
| 2971 | mm_populate(ret, populate); |
| 2972 | if (!IS_ERR_VALUE(ret)) |
| 2973 | ret = 0; |
| 2974 | return ret; |
| 2975 | } |
| 2976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | static inline void verify_mm_writelocked(struct mm_struct *mm) |
| 2978 | { |
Paul E. McKenney | a241ec6 | 2005-10-30 15:03:12 -0800 | [diff] [blame] | 2979 | #ifdef CONFIG_DEBUG_VM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | if (unlikely(down_read_trylock(&mm->mmap_sem))) { |
| 2981 | WARN_ON(1); |
| 2982 | up_read(&mm->mmap_sem); |
| 2983 | } |
| 2984 | #endif |
| 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | * this is really a simplified "do_mmap". it only handles |
| 2989 | * anonymous maps. eventually we may be able to do some |
| 2990 | * brk-specific accounting here. |
| 2991 | */ |
Kees Cook | ba093a6 | 2016-08-02 14:04:54 -0700 | [diff] [blame] | 2992 | static int do_brk(unsigned long addr, unsigned long request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | { |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2994 | struct mm_struct *mm = current->mm; |
| 2995 | struct vm_area_struct *vma, *prev; |
Kees Cook | ba093a6 | 2016-08-02 14:04:54 -0700 | [diff] [blame] | 2996 | unsigned long flags, len; |
vishnu.ps | cc71aba | 2014-10-09 15:26:29 -0700 | [diff] [blame] | 2997 | struct rb_node **rb_link, *rb_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | pgoff_t pgoff = addr >> PAGE_SHIFT; |
Kirill Korotaev | 3a45975 | 2006-09-07 14:17:04 +0400 | [diff] [blame] | 2999 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | |
Kees Cook | ba093a6 | 2016-08-02 14:04:54 -0700 | [diff] [blame] | 3001 | len = PAGE_ALIGN(request); |
| 3002 | if (len < request) |
| 3003 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | if (!len) |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 3005 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | |
Kirill Korotaev | 3a45975 | 2006-09-07 14:17:04 +0400 | [diff] [blame] | 3007 | flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; |
| 3008 | |
Al Viro | 2c6a101 | 2009-12-03 19:40:46 -0500 | [diff] [blame] | 3009 | error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); |
Alexander Kuleshov | de1741a | 2015-11-05 18:46:54 -0800 | [diff] [blame] | 3010 | if (offset_in_page(error)) |
Kirill Korotaev | 3a45975 | 2006-09-07 14:17:04 +0400 | [diff] [blame] | 3011 | return error; |
| 3012 | |
Davidlohr Bueso | 363ee17 | 2014-01-21 15:49:15 -0800 | [diff] [blame] | 3013 | error = mlock_future_check(mm, mm->def_flags, len); |
| 3014 | if (error) |
| 3015 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | |
| 3017 | /* |
| 3018 | * mm->mmap_sem is required to protect against another thread |
| 3019 | * changing the mappings in case we sleep. |
| 3020 | */ |
| 3021 | verify_mm_writelocked(mm); |
| 3022 | |
| 3023 | /* |
| 3024 | * Clear old maps. this also does some error checking for us |
| 3025 | */ |
Rasmus Villemoes | 9fcd145 | 2015-04-15 16:14:32 -0700 | [diff] [blame] | 3026 | while (find_vma_links(mm, addr, addr + len, &prev, &rb_link, |
| 3027 | &rb_parent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | if (do_munmap(mm, addr, len)) |
| 3029 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | /* Check against address space limits *after* clearing old maps... */ |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3033 | if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | return -ENOMEM; |
| 3035 | |
| 3036 | if (mm->map_count > sysctl_max_map_count) |
| 3037 | return -ENOMEM; |
| 3038 | |
Al Viro | 191c542 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 3039 | if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | return -ENOMEM; |
| 3041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | /* Can we just expand an old private anonymous mapping? */ |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 3043 | vma = vma_merge(mm, prev, addr, addr + len, flags, |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 3044 | NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX, NULL); |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 3045 | if (vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | goto out; |
| 3047 | |
| 3048 | /* |
| 3049 | * create a vma struct for an anonymous mapping |
| 3050 | */ |
Pekka Enberg | c5e3b83 | 2006-03-25 03:06:43 -0800 | [diff] [blame] | 3051 | vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | if (!vma) { |
| 3053 | vm_unacct_memory(len >> PAGE_SHIFT); |
| 3054 | return -ENOMEM; |
| 3055 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | |
Laurent Dufour | 6315ccc | 2018-04-17 16:33:13 +0200 | [diff] [blame] | 3057 | INIT_VMA(vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | vma->vm_mm = mm; |
| 3059 | vma->vm_start = addr; |
| 3060 | vma->vm_end = addr + len; |
| 3061 | vma->vm_pgoff = pgoff; |
| 3062 | vma->vm_flags = flags; |
Coly Li | 3ed75eb | 2007-10-18 23:39:15 -0700 | [diff] [blame] | 3063 | vma->vm_page_prot = vm_get_page_prot(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | vma_link(mm, vma, prev, rb_link, rb_parent); |
| 3065 | out: |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3066 | perf_event_mmap(vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | mm->total_vm += len >> PAGE_SHIFT; |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3068 | mm->data_vm += len >> PAGE_SHIFT; |
Michel Lespinasse | 128557f | 2013-02-22 16:32:40 -0800 | [diff] [blame] | 3069 | if (flags & VM_LOCKED) |
| 3070 | mm->locked_vm += (len >> PAGE_SHIFT); |
Cyrill Gorcunov | d9104d1 | 2013-09-11 14:22:24 -0700 | [diff] [blame] | 3071 | vma->vm_flags |= VM_SOFTDIRTY; |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 3072 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | } |
| 3074 | |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 3075 | int vm_brk(unsigned long addr, unsigned long len) |
Linus Torvalds | e4eb1ff | 2012-04-20 15:35:40 -0700 | [diff] [blame] | 3076 | { |
| 3077 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 3078 | int ret; |
Michel Lespinasse | 128557f | 2013-02-22 16:32:40 -0800 | [diff] [blame] | 3079 | bool populate; |
Linus Torvalds | e4eb1ff | 2012-04-20 15:35:40 -0700 | [diff] [blame] | 3080 | |
Michal Hocko | 2d6c928 | 2016-05-23 16:25:42 -0700 | [diff] [blame] | 3081 | if (down_write_killable(&mm->mmap_sem)) |
| 3082 | return -EINTR; |
| 3083 | |
Linus Torvalds | e4eb1ff | 2012-04-20 15:35:40 -0700 | [diff] [blame] | 3084 | ret = do_brk(addr, len); |
Michel Lespinasse | 128557f | 2013-02-22 16:32:40 -0800 | [diff] [blame] | 3085 | populate = ((mm->def_flags & VM_LOCKED) != 0); |
Linus Torvalds | e4eb1ff | 2012-04-20 15:35:40 -0700 | [diff] [blame] | 3086 | up_write(&mm->mmap_sem); |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 3087 | if (populate && !ret) |
Michel Lespinasse | 128557f | 2013-02-22 16:32:40 -0800 | [diff] [blame] | 3088 | mm_populate(addr, len); |
Linus Torvalds | e4eb1ff | 2012-04-20 15:35:40 -0700 | [diff] [blame] | 3089 | return ret; |
| 3090 | } |
| 3091 | EXPORT_SYMBOL(vm_brk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | |
| 3093 | /* Release all mmaps. */ |
| 3094 | void exit_mmap(struct mm_struct *mm) |
| 3095 | { |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 3096 | struct mmu_gather tlb; |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 3097 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | unsigned long nr_accounted = 0; |
| 3099 | |
Jeremy Fitzhardinge | d6dd61c | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 3100 | /* mm's last user has gone, and its about to be pulled down */ |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 3101 | mmu_notifier_release(mm); |
Jeremy Fitzhardinge | d6dd61c | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 3102 | |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 3103 | if (mm->locked_vm) { |
| 3104 | vma = mm->mmap; |
| 3105 | while (vma) { |
| 3106 | if (vma->vm_flags & VM_LOCKED) |
| 3107 | munlock_vma_pages_all(vma); |
| 3108 | vma = vma->vm_next; |
| 3109 | } |
| 3110 | } |
Jeremy Fitzhardinge | 9480c53 | 2009-02-11 13:04:41 -0800 | [diff] [blame] | 3111 | |
| 3112 | arch_exit_mmap(mm); |
| 3113 | |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 3114 | vma = mm->mmap; |
Jeremy Fitzhardinge | 9480c53 | 2009-02-11 13:04:41 -0800 | [diff] [blame] | 3115 | if (!vma) /* Can happen if dup_mmap() received an OOM */ |
| 3116 | return; |
| 3117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | lru_add_drain(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | flush_cache_mm(mm); |
Linus Torvalds | 2b04725 | 2013-08-15 11:42:25 -0700 | [diff] [blame] | 3120 | tlb_gather_mmu(&tlb, mm, 0, -1); |
Oleg Nesterov | 901608d | 2009-01-06 14:40:29 -0800 | [diff] [blame] | 3121 | /* update_hiwater_rss(mm) here? but nobody should be looking */ |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 3122 | /* Use -1 here to ensure all VMAs in the mm are unmapped */ |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 3123 | unmap_vmas(&tlb, vma, 0, -1); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 3124 | |
Michal Hocko | 065e76d | 2017-12-14 15:33:15 -0800 | [diff] [blame] | 3125 | if (unlikely(mm_is_oom_victim(mm))) { |
Andrea Arcangeli | 07b039e9 | 2017-09-06 16:25:00 -0700 | [diff] [blame] | 3126 | /* |
| 3127 | * Wait for oom_reap_task() to stop working on this |
| 3128 | * mm. Because MMF_OOM_SKIP is already set before |
| 3129 | * calling down_read(), oom_reap_task() will not run |
| 3130 | * on this "mm" post up_write(). |
| 3131 | * |
Michal Hocko | 065e76d | 2017-12-14 15:33:15 -0800 | [diff] [blame] | 3132 | * mm_is_oom_victim() cannot be set from under us |
| 3133 | * either because victim->mm is already set to NULL |
Andrea Arcangeli | 07b039e9 | 2017-09-06 16:25:00 -0700 | [diff] [blame] | 3134 | * under task_lock before calling mmput and oom_mm is |
Michal Hocko | 065e76d | 2017-12-14 15:33:15 -0800 | [diff] [blame] | 3135 | * set not NULL by the OOM killer only if victim->mm |
Andrea Arcangeli | 07b039e9 | 2017-09-06 16:25:00 -0700 | [diff] [blame] | 3136 | * is found not NULL while holding the task_lock. |
| 3137 | */ |
Michal Hocko | 065e76d | 2017-12-14 15:33:15 -0800 | [diff] [blame] | 3138 | set_bit(MMF_OOM_SKIP, &mm->flags); |
Andrea Arcangeli | 07b039e9 | 2017-09-06 16:25:00 -0700 | [diff] [blame] | 3139 | down_write(&mm->mmap_sem); |
| 3140 | up_write(&mm->mmap_sem); |
| 3141 | } |
Hugh Dickins | 6ee8630 | 2013-04-29 15:07:44 -0700 | [diff] [blame] | 3142 | free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); |
Al Viro | 853f5e2 | 2012-03-05 14:03:47 -0500 | [diff] [blame] | 3143 | tlb_finish_mmu(&tlb, 0, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | /* |
Hugh Dickins | 8f4f8c1 | 2005-10-29 18:16:29 -0700 | [diff] [blame] | 3146 | * Walk the list again, actually closing and freeing it, |
| 3147 | * with preemption enabled, without holding any MM locks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | */ |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 3149 | while (vma) { |
| 3150 | if (vma->vm_flags & VM_ACCOUNT) |
| 3151 | nr_accounted += vma_pages(vma); |
Hugh Dickins | a8fb561 | 2005-10-29 18:15:57 -0700 | [diff] [blame] | 3152 | vma = remove_vma(vma); |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 3153 | } |
| 3154 | vm_unacct_memory(nr_accounted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | /* Insert vm structure into process list sorted by address |
| 3158 | * and into the inode's i_mmap tree. If vm_file is non-NULL |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 3159 | * then i_mmap_rwsem is taken here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | */ |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 3161 | int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | { |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 3163 | struct vm_area_struct *prev; |
| 3164 | struct rb_node **rb_link, *rb_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | |
Chen Gang | c9d13f5 | 2015-09-08 15:04:08 -0700 | [diff] [blame] | 3166 | if (find_vma_links(mm, vma->vm_start, vma->vm_end, |
| 3167 | &prev, &rb_link, &rb_parent)) |
| 3168 | return -ENOMEM; |
| 3169 | if ((vma->vm_flags & VM_ACCOUNT) && |
| 3170 | security_vm_enough_memory_mm(mm, vma_pages(vma))) |
| 3171 | return -ENOMEM; |
| 3172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | /* |
| 3174 | * The vm_pgoff of a purely anonymous vma should be irrelevant |
| 3175 | * until its first write fault, when page's anon_vma and index |
| 3176 | * are set. But now set the vm_pgoff it will almost certainly |
| 3177 | * end up with (unless mremap moves it elsewhere before that |
| 3178 | * first wfault), so /proc/pid/maps tells a consistent story. |
| 3179 | * |
| 3180 | * By setting it to reflect the virtual start address of the |
| 3181 | * vma, merges and splits can happen in a seamless way, just |
| 3182 | * using the existing file pgoff checks and manipulations. |
| 3183 | * Similarly in do_mmap_pgoff and in do_brk. |
| 3184 | */ |
Oleg Nesterov | 8a9cc3b | 2015-09-08 14:58:31 -0700 | [diff] [blame] | 3185 | if (vma_is_anonymous(vma)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | BUG_ON(vma->anon_vma); |
| 3187 | vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; |
| 3188 | } |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 3189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | vma_link(mm, vma, prev, rb_link, rb_parent); |
| 3191 | return 0; |
| 3192 | } |
| 3193 | |
| 3194 | /* |
| 3195 | * Copy the vma structure to a new location in the same mm, |
| 3196 | * prior to moving page table entries, to effect an mremap move. |
| 3197 | */ |
| 3198 | struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, |
Michel Lespinasse | 38a7601 | 2012-10-08 16:31:50 -0700 | [diff] [blame] | 3199 | unsigned long addr, unsigned long len, pgoff_t pgoff, |
| 3200 | bool *need_rmap_locks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | { |
| 3202 | struct vm_area_struct *vma = *vmap; |
| 3203 | unsigned long vma_start = vma->vm_start; |
| 3204 | struct mm_struct *mm = vma->vm_mm; |
| 3205 | struct vm_area_struct *new_vma, *prev; |
| 3206 | struct rb_node **rb_link, *rb_parent; |
Andrea Arcangeli | 948f017 | 2012-01-10 15:08:05 -0800 | [diff] [blame] | 3207 | bool faulted_in_anon_vma = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | |
| 3209 | /* |
| 3210 | * If anonymous vma has not yet been faulted, update new pgoff |
| 3211 | * to match new location, to increase its chance of merging. |
| 3212 | */ |
Oleg Nesterov | ce75799 | 2015-09-08 14:58:34 -0700 | [diff] [blame] | 3213 | if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | pgoff = addr >> PAGE_SHIFT; |
Andrea Arcangeli | 948f017 | 2012-01-10 15:08:05 -0800 | [diff] [blame] | 3215 | faulted_in_anon_vma = false; |
| 3216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | |
Hugh Dickins | 6597d78 | 2012-10-08 16:29:07 -0700 | [diff] [blame] | 3218 | if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) |
| 3219 | return NULL; /* should never get here */ |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 3220 | |
| 3221 | /* There is 3 cases to manage here in |
| 3222 | * AAAA AAAA AAAA AAAA |
| 3223 | * PPPP.... PPPP......NNNN PPPP....NNNN PP........NN |
| 3224 | * PPPPPPPP(A) PPPP..NNNNNNNN(B) PPPPPPPPPPPP(1) NULL |
| 3225 | * PPPPPPPPNNNN(2) |
| 3226 | * PPPPNNNNNNNN(3) |
| 3227 | * |
| 3228 | * new_vma == prev in case A,1,2 |
| 3229 | * new_vma == next in case B,3 |
| 3230 | */ |
| 3231 | new_vma = __vma_merge(mm, prev, addr, addr + len, vma->vm_flags, |
| 3232 | vma->anon_vma, vma->vm_file, pgoff, |
| 3233 | vma_policy(vma), vma->vm_userfaultfd_ctx, |
| 3234 | vma_get_anon_name(vma), true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | if (new_vma) { |
| 3236 | /* |
| 3237 | * Source vma may have been merged into new_vma |
| 3238 | */ |
Andrea Arcangeli | 948f017 | 2012-01-10 15:08:05 -0800 | [diff] [blame] | 3239 | if (unlikely(vma_start >= new_vma->vm_start && |
| 3240 | vma_start < new_vma->vm_end)) { |
| 3241 | /* |
| 3242 | * The only way we can get a vma_merge with |
| 3243 | * self during an mremap is if the vma hasn't |
| 3244 | * been faulted in yet and we were allowed to |
| 3245 | * reset the dst vma->vm_pgoff to the |
| 3246 | * destination address of the mremap to allow |
| 3247 | * the merge to happen. mremap must change the |
| 3248 | * vm_pgoff linearity between src and dst vmas |
| 3249 | * (in turn preventing a vma_merge) to be |
| 3250 | * safe. It is only safe to keep the vm_pgoff |
| 3251 | * linear if there are no pages mapped yet. |
| 3252 | */ |
Sasha Levin | 81d1b09 | 2014-10-09 15:28:10 -0700 | [diff] [blame] | 3253 | VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); |
Michel Lespinasse | 38a7601 | 2012-10-08 16:31:50 -0700 | [diff] [blame] | 3254 | *vmap = vma = new_vma; |
Michel Lespinasse | 108d664 | 2012-10-08 16:31:36 -0700 | [diff] [blame] | 3255 | } |
Michel Lespinasse | 38a7601 | 2012-10-08 16:31:50 -0700 | [diff] [blame] | 3256 | *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | } else { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 3258 | new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 3259 | if (!new_vma) |
| 3260 | goto out; |
| 3261 | *new_vma = *vma; |
| 3262 | new_vma->vm_start = addr; |
| 3263 | new_vma->vm_end = addr + len; |
| 3264 | new_vma->vm_pgoff = pgoff; |
| 3265 | if (vma_dup_policy(vma, new_vma)) |
| 3266 | goto out_free_vma; |
Laurent Dufour | 6315ccc | 2018-04-17 16:33:13 +0200 | [diff] [blame] | 3267 | INIT_VMA(new_vma); |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 3268 | if (anon_vma_clone(new_vma, vma)) |
| 3269 | goto out_free_mempol; |
| 3270 | if (new_vma->vm_file) |
| 3271 | get_file(new_vma->vm_file); |
| 3272 | if (new_vma->vm_ops && new_vma->vm_ops->open) |
| 3273 | new_vma->vm_ops->open(new_vma); |
Laurent Dufour | 3545b7e | 2018-04-17 16:33:16 +0200 | [diff] [blame] | 3274 | /* |
| 3275 | * As the VMA is linked right now, it may be hit by the |
| 3276 | * speculative page fault handler. But we don't want it to |
| 3277 | * to start mapping page in this area until the caller has |
| 3278 | * potentially move the pte from the moved VMA. To prevent |
| 3279 | * that we protect it right now, and let the caller unprotect |
| 3280 | * it once the move is done. |
| 3281 | */ |
| 3282 | vm_raw_write_begin(new_vma); |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 3283 | vma_link(mm, new_vma, prev, rb_link, rb_parent); |
| 3284 | *need_rmap_locks = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | } |
| 3286 | return new_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 3287 | |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 3288 | out_free_mempol: |
Oleg Nesterov | ef0855d | 2013-09-11 14:20:14 -0700 | [diff] [blame] | 3289 | mpol_put(vma_policy(new_vma)); |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 3290 | out_free_vma: |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 3291 | kmem_cache_free(vm_area_cachep, new_vma); |
Chen Gang | e397589 | 2015-09-08 15:03:38 -0700 | [diff] [blame] | 3292 | out: |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 3293 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | } |
akpm@osdl.org | 119f657 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 3295 | |
| 3296 | /* |
| 3297 | * Return true if the calling process may expand its vm space by the passed |
| 3298 | * number of pages |
| 3299 | */ |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3300 | bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) |
akpm@osdl.org | 119f657 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 3301 | { |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3302 | if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) |
| 3303 | return false; |
akpm@osdl.org | 119f657 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 3304 | |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 3305 | if (is_data_mapping(flags) && |
| 3306 | mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { |
Konstantin Khlebnikov | f4fcd55 | 2016-05-20 16:57:45 -0700 | [diff] [blame] | 3307 | /* Workaround for Valgrind */ |
| 3308 | if (rlimit(RLIMIT_DATA) == 0 && |
| 3309 | mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) |
| 3310 | return true; |
| 3311 | if (!ignore_rlimit_data) { |
| 3312 | pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits or use boot option ignore_rlimit_data.\n", |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 3313 | current->comm, current->pid, |
| 3314 | (mm->data_vm + npages) << PAGE_SHIFT, |
| 3315 | rlimit(RLIMIT_DATA)); |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 3316 | return false; |
Konstantin Khlebnikov | f4fcd55 | 2016-05-20 16:57:45 -0700 | [diff] [blame] | 3317 | } |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 3318 | } |
akpm@osdl.org | 119f657 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 3319 | |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3320 | return true; |
| 3321 | } |
| 3322 | |
| 3323 | void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) |
| 3324 | { |
| 3325 | mm->total_vm += npages; |
| 3326 | |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 3327 | if (is_exec_mapping(flags)) |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3328 | mm->exec_vm += npages; |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 3329 | else if (is_stack_mapping(flags)) |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3330 | mm->stack_vm += npages; |
Konstantin Khlebnikov | d977d56 | 2016-02-02 16:57:43 -0800 | [diff] [blame] | 3331 | else if (is_data_mapping(flags)) |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3332 | mm->data_vm += npages; |
akpm@osdl.org | 119f657 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 3333 | } |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3334 | |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3335 | static int special_mapping_fault(struct vm_area_struct *vma, |
| 3336 | struct vm_fault *vmf); |
| 3337 | |
| 3338 | /* |
| 3339 | * Having a close hook prevents vma merging regardless of flags. |
| 3340 | */ |
| 3341 | static void special_mapping_close(struct vm_area_struct *vma) |
| 3342 | { |
| 3343 | } |
| 3344 | |
| 3345 | static const char *special_mapping_name(struct vm_area_struct *vma) |
| 3346 | { |
| 3347 | return ((struct vm_special_mapping *)vma->vm_private_data)->name; |
| 3348 | } |
| 3349 | |
Dmitry Safonov | b059a45 | 2016-06-28 14:35:38 +0300 | [diff] [blame] | 3350 | static int special_mapping_mremap(struct vm_area_struct *new_vma) |
| 3351 | { |
| 3352 | struct vm_special_mapping *sm = new_vma->vm_private_data; |
| 3353 | |
| 3354 | if (sm->mremap) |
| 3355 | return sm->mremap(sm, new_vma); |
| 3356 | return 0; |
| 3357 | } |
| 3358 | |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3359 | static const struct vm_operations_struct special_mapping_vmops = { |
| 3360 | .close = special_mapping_close, |
| 3361 | .fault = special_mapping_fault, |
Dmitry Safonov | b059a45 | 2016-06-28 14:35:38 +0300 | [diff] [blame] | 3362 | .mremap = special_mapping_mremap, |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3363 | .name = special_mapping_name, |
| 3364 | }; |
| 3365 | |
| 3366 | static const struct vm_operations_struct legacy_special_mapping_vmops = { |
| 3367 | .close = special_mapping_close, |
| 3368 | .fault = special_mapping_fault, |
| 3369 | }; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3370 | |
Nick Piggin | b1d0e4f | 2008-02-09 01:15:19 +0100 | [diff] [blame] | 3371 | static int special_mapping_fault(struct vm_area_struct *vma, |
| 3372 | struct vm_fault *vmf) |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3373 | { |
Nick Piggin | b1d0e4f | 2008-02-09 01:15:19 +0100 | [diff] [blame] | 3374 | pgoff_t pgoff; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3375 | struct page **pages; |
| 3376 | |
Andy Lutomirski | f872f54 | 2015-12-29 20:12:19 -0800 | [diff] [blame] | 3377 | if (vma->vm_ops == &legacy_special_mapping_vmops) { |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3378 | pages = vma->vm_private_data; |
Andy Lutomirski | f872f54 | 2015-12-29 20:12:19 -0800 | [diff] [blame] | 3379 | } else { |
| 3380 | struct vm_special_mapping *sm = vma->vm_private_data; |
| 3381 | |
| 3382 | if (sm->fault) |
| 3383 | return sm->fault(sm, vma, vmf); |
| 3384 | |
| 3385 | pages = sm->pages; |
| 3386 | } |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3387 | |
Oleg Nesterov | 8a9cc3b | 2015-09-08 14:58:31 -0700 | [diff] [blame] | 3388 | for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) |
Nick Piggin | b1d0e4f | 2008-02-09 01:15:19 +0100 | [diff] [blame] | 3389 | pgoff--; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3390 | |
| 3391 | if (*pages) { |
| 3392 | struct page *page = *pages; |
| 3393 | get_page(page); |
Nick Piggin | b1d0e4f | 2008-02-09 01:15:19 +0100 | [diff] [blame] | 3394 | vmf->page = page; |
| 3395 | return 0; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3396 | } |
| 3397 | |
Nick Piggin | b1d0e4f | 2008-02-09 01:15:19 +0100 | [diff] [blame] | 3398 | return VM_FAULT_SIGBUS; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3399 | } |
| 3400 | |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3401 | static struct vm_area_struct *__install_special_mapping( |
| 3402 | struct mm_struct *mm, |
| 3403 | unsigned long addr, unsigned long len, |
Chen Gang | 27f28b9 | 2015-11-05 18:48:41 -0800 | [diff] [blame] | 3404 | unsigned long vm_flags, void *priv, |
| 3405 | const struct vm_operations_struct *ops) |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3406 | { |
Tavis Ormandy | 462e635e | 2010-12-09 15:29:42 +0100 | [diff] [blame] | 3407 | int ret; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3408 | struct vm_area_struct *vma; |
| 3409 | |
| 3410 | vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); |
| 3411 | if (unlikely(vma == NULL)) |
Stefani Seibold | 3935ed6 | 2014-03-17 23:22:02 +0100 | [diff] [blame] | 3412 | return ERR_PTR(-ENOMEM); |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3413 | |
Laurent Dufour | 6315ccc | 2018-04-17 16:33:13 +0200 | [diff] [blame] | 3414 | INIT_VMA(vma); |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3415 | vma->vm_mm = mm; |
| 3416 | vma->vm_start = addr; |
| 3417 | vma->vm_end = addr + len; |
| 3418 | |
Cyrill Gorcunov | d9104d1 | 2013-09-11 14:22:24 -0700 | [diff] [blame] | 3419 | vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY; |
Coly Li | 3ed75eb | 2007-10-18 23:39:15 -0700 | [diff] [blame] | 3420 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3421 | |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3422 | vma->vm_ops = ops; |
| 3423 | vma->vm_private_data = priv; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3424 | |
Tavis Ormandy | 462e635e | 2010-12-09 15:29:42 +0100 | [diff] [blame] | 3425 | ret = insert_vm_struct(mm, vma); |
| 3426 | if (ret) |
| 3427 | goto out; |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3428 | |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 3429 | vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3430 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3431 | perf_event_mmap(vma); |
Peter Zijlstra | 089dd79 | 2009-06-05 14:04:55 +0200 | [diff] [blame] | 3432 | |
Stefani Seibold | 3935ed6 | 2014-03-17 23:22:02 +0100 | [diff] [blame] | 3433 | return vma; |
Tavis Ormandy | 462e635e | 2010-12-09 15:29:42 +0100 | [diff] [blame] | 3434 | |
| 3435 | out: |
| 3436 | kmem_cache_free(vm_area_cachep, vma); |
Stefani Seibold | 3935ed6 | 2014-03-17 23:22:02 +0100 | [diff] [blame] | 3437 | return ERR_PTR(ret); |
| 3438 | } |
| 3439 | |
Dmitry Safonov | 2eefd87 | 2016-09-05 16:33:05 +0300 | [diff] [blame] | 3440 | bool vma_is_special_mapping(const struct vm_area_struct *vma, |
| 3441 | const struct vm_special_mapping *sm) |
| 3442 | { |
| 3443 | return vma->vm_private_data == sm && |
| 3444 | (vma->vm_ops == &special_mapping_vmops || |
| 3445 | vma->vm_ops == &legacy_special_mapping_vmops); |
| 3446 | } |
| 3447 | |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3448 | /* |
| 3449 | * Called with mm->mmap_sem held for writing. |
| 3450 | * Insert a new vma covering the given region, with the given flags. |
| 3451 | * Its pages are supplied by the given array of struct page *. |
| 3452 | * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. |
| 3453 | * The region past the last page supplied will always produce SIGBUS. |
| 3454 | * The array pointer and the pages it points to are assumed to stay alive |
| 3455 | * for as long as this mapping might exist. |
| 3456 | */ |
| 3457 | struct vm_area_struct *_install_special_mapping( |
| 3458 | struct mm_struct *mm, |
| 3459 | unsigned long addr, unsigned long len, |
| 3460 | unsigned long vm_flags, const struct vm_special_mapping *spec) |
| 3461 | { |
Chen Gang | 27f28b9 | 2015-11-05 18:48:41 -0800 | [diff] [blame] | 3462 | return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, |
| 3463 | &special_mapping_vmops); |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3464 | } |
| 3465 | |
Stefani Seibold | 3935ed6 | 2014-03-17 23:22:02 +0100 | [diff] [blame] | 3466 | int install_special_mapping(struct mm_struct *mm, |
| 3467 | unsigned long addr, unsigned long len, |
| 3468 | unsigned long vm_flags, struct page **pages) |
| 3469 | { |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 3470 | struct vm_area_struct *vma = __install_special_mapping( |
Chen Gang | 27f28b9 | 2015-11-05 18:48:41 -0800 | [diff] [blame] | 3471 | mm, addr, len, vm_flags, (void *)pages, |
| 3472 | &legacy_special_mapping_vmops); |
Stefani Seibold | 3935ed6 | 2014-03-17 23:22:02 +0100 | [diff] [blame] | 3473 | |
Duan Jiong | 14bd5b4 | 2014-06-04 16:07:05 -0700 | [diff] [blame] | 3474 | return PTR_ERR_OR_ZERO(vma); |
Roland McGrath | fa5dc22 | 2007-02-08 14:20:41 -0800 | [diff] [blame] | 3475 | } |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3476 | |
| 3477 | static DEFINE_MUTEX(mm_all_locks_mutex); |
| 3478 | |
Peter Zijlstra | 454ed84 | 2008-08-11 09:30:25 +0200 | [diff] [blame] | 3479 | static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3480 | { |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 3481 | if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) { |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3482 | /* |
| 3483 | * The LSB of head.next can't change from under us |
| 3484 | * because we hold the mm_all_locks_mutex. |
| 3485 | */ |
Jiri Kosina | 572043c | 2013-01-11 14:31:59 -0800 | [diff] [blame] | 3486 | down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem); |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3487 | /* |
| 3488 | * We can safely modify head.next after taking the |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 3489 | * anon_vma->root->rwsem. If some other vma in this mm shares |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3490 | * the same anon_vma we won't take it again. |
| 3491 | * |
| 3492 | * No need of atomic instructions here, head.next |
| 3493 | * can't change from under us thanks to the |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 3494 | * anon_vma->root->rwsem. |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3495 | */ |
| 3496 | if (__test_and_set_bit(0, (unsigned long *) |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 3497 | &anon_vma->root->rb_root.rb_node)) |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3498 | BUG(); |
| 3499 | } |
| 3500 | } |
| 3501 | |
Peter Zijlstra | 454ed84 | 2008-08-11 09:30:25 +0200 | [diff] [blame] | 3502 | static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3503 | { |
| 3504 | if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { |
| 3505 | /* |
| 3506 | * AS_MM_ALL_LOCKS can't change from under us because |
| 3507 | * we hold the mm_all_locks_mutex. |
| 3508 | * |
| 3509 | * Operations on ->flags have to be atomic because |
| 3510 | * even if AS_MM_ALL_LOCKS is stable thanks to the |
| 3511 | * mm_all_locks_mutex, there may be other cpus |
| 3512 | * changing other bitflags in parallel to us. |
| 3513 | */ |
| 3514 | if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) |
| 3515 | BUG(); |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 3516 | down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem); |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | /* |
| 3521 | * This operation locks against the VM for all pte/vma/mm related |
| 3522 | * operations that could ever happen on a certain mm. This includes |
| 3523 | * vmtruncate, try_to_unmap, and all page faults. |
| 3524 | * |
| 3525 | * The caller must take the mmap_sem in write mode before calling |
| 3526 | * mm_take_all_locks(). The caller isn't allowed to release the |
| 3527 | * mmap_sem until mm_drop_all_locks() returns. |
| 3528 | * |
| 3529 | * mmap_sem in write mode is required in order to block all operations |
| 3530 | * that could modify pagetables and free pages without need of |
Kirill A. Shutemov | 27ba064 | 2015-02-10 14:09:59 -0800 | [diff] [blame] | 3531 | * altering the vma layout. It's also needed in write mode to avoid new |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3532 | * anon_vmas to be associated with existing vmas. |
| 3533 | * |
| 3534 | * A single task can't take more than one mm_take_all_locks() in a row |
| 3535 | * or it would deadlock. |
| 3536 | * |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 3537 | * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3538 | * mapping->flags avoid to take the same lock twice, if more than one |
| 3539 | * vma in this mm is backed by the same anon_vma or address_space. |
| 3540 | * |
Kirill A. Shutemov | 88f306b | 2016-01-15 16:57:31 -0800 | [diff] [blame] | 3541 | * We take locks in following order, accordingly to comment at beginning |
| 3542 | * of mm/rmap.c: |
| 3543 | * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for |
| 3544 | * hugetlb mapping); |
| 3545 | * - all i_mmap_rwsem locks; |
| 3546 | * - all anon_vma->rwseml |
| 3547 | * |
| 3548 | * We can take all locks within these types randomly because the VM code |
| 3549 | * doesn't nest them and we protected from parallel mm_take_all_locks() by |
| 3550 | * mm_all_locks_mutex. |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3551 | * |
| 3552 | * mm_take_all_locks() and mm_drop_all_locks are expensive operations |
| 3553 | * that may have to take thousand of locks. |
| 3554 | * |
| 3555 | * mm_take_all_locks() can fail if it's interrupted by signals. |
| 3556 | */ |
| 3557 | int mm_take_all_locks(struct mm_struct *mm) |
| 3558 | { |
| 3559 | struct vm_area_struct *vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 3560 | struct anon_vma_chain *avc; |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3561 | |
| 3562 | BUG_ON(down_read_trylock(&mm->mmap_sem)); |
| 3563 | |
| 3564 | mutex_lock(&mm_all_locks_mutex); |
| 3565 | |
| 3566 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 3567 | if (signal_pending(current)) |
| 3568 | goto out_unlock; |
Kirill A. Shutemov | 88f306b | 2016-01-15 16:57:31 -0800 | [diff] [blame] | 3569 | if (vma->vm_file && vma->vm_file->f_mapping && |
| 3570 | is_vm_hugetlb_page(vma)) |
| 3571 | vm_lock_mapping(mm, vma->vm_file->f_mapping); |
| 3572 | } |
| 3573 | |
| 3574 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 3575 | if (signal_pending(current)) |
| 3576 | goto out_unlock; |
| 3577 | if (vma->vm_file && vma->vm_file->f_mapping && |
| 3578 | !is_vm_hugetlb_page(vma)) |
Peter Zijlstra | 454ed84 | 2008-08-11 09:30:25 +0200 | [diff] [blame] | 3579 | vm_lock_mapping(mm, vma->vm_file->f_mapping); |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3580 | } |
Peter Zijlstra | 7cd5a02 | 2008-08-11 09:30:25 +0200 | [diff] [blame] | 3581 | |
| 3582 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 3583 | if (signal_pending(current)) |
| 3584 | goto out_unlock; |
| 3585 | if (vma->anon_vma) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 3586 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
| 3587 | vm_lock_anon_vma(mm, avc->anon_vma); |
Peter Zijlstra | 7cd5a02 | 2008-08-11 09:30:25 +0200 | [diff] [blame] | 3588 | } |
| 3589 | |
Kautuk Consul | 584cff5 | 2011-10-31 17:08:59 -0700 | [diff] [blame] | 3590 | return 0; |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3591 | |
| 3592 | out_unlock: |
Kautuk Consul | 584cff5 | 2011-10-31 17:08:59 -0700 | [diff] [blame] | 3593 | mm_drop_all_locks(mm); |
| 3594 | return -EINTR; |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | static void vm_unlock_anon_vma(struct anon_vma *anon_vma) |
| 3598 | { |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 3599 | if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) { |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3600 | /* |
| 3601 | * The LSB of head.next can't change to 0 from under |
| 3602 | * us because we hold the mm_all_locks_mutex. |
| 3603 | * |
| 3604 | * We must however clear the bitflag before unlocking |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 3605 | * the vma so the users using the anon_vma->rb_root will |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3606 | * never see our bitflag. |
| 3607 | * |
| 3608 | * No need of atomic instructions here, head.next |
| 3609 | * can't change from under us until we release the |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 3610 | * anon_vma->root->rwsem. |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3611 | */ |
| 3612 | if (!__test_and_clear_bit(0, (unsigned long *) |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 3613 | &anon_vma->root->rb_root.rb_node)) |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3614 | BUG(); |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 3615 | anon_vma_unlock_write(anon_vma); |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | static void vm_unlock_mapping(struct address_space *mapping) |
| 3620 | { |
| 3621 | if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { |
| 3622 | /* |
| 3623 | * AS_MM_ALL_LOCKS can't change to 0 from under us |
| 3624 | * because we hold the mm_all_locks_mutex. |
| 3625 | */ |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 3626 | i_mmap_unlock_write(mapping); |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3627 | if (!test_and_clear_bit(AS_MM_ALL_LOCKS, |
| 3628 | &mapping->flags)) |
| 3629 | BUG(); |
| 3630 | } |
| 3631 | } |
| 3632 | |
| 3633 | /* |
| 3634 | * The mmap_sem cannot be released by the caller until |
| 3635 | * mm_drop_all_locks() returns. |
| 3636 | */ |
| 3637 | void mm_drop_all_locks(struct mm_struct *mm) |
| 3638 | { |
| 3639 | struct vm_area_struct *vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 3640 | struct anon_vma_chain *avc; |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3641 | |
| 3642 | BUG_ON(down_read_trylock(&mm->mmap_sem)); |
| 3643 | BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); |
| 3644 | |
| 3645 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 3646 | if (vma->anon_vma) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 3647 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
| 3648 | vm_unlock_anon_vma(avc->anon_vma); |
Andrea Arcangeli | 7906d00 | 2008-07-28 15:46:26 -0700 | [diff] [blame] | 3649 | if (vma->vm_file && vma->vm_file->f_mapping) |
| 3650 | vm_unlock_mapping(vma->vm_file->f_mapping); |
| 3651 | } |
| 3652 | |
| 3653 | mutex_unlock(&mm_all_locks_mutex); |
| 3654 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 3655 | |
| 3656 | /* |
| 3657 | * initialise the VMA slab |
| 3658 | */ |
| 3659 | void __init mmap_init(void) |
| 3660 | { |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 3661 | int ret; |
| 3662 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 3663 | ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 3664 | VM_BUG_ON(ret); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 3665 | } |
Andrew Shewmaker | c9b1d09 | 2013-04-29 15:08:10 -0700 | [diff] [blame] | 3666 | |
| 3667 | /* |
| 3668 | * Initialise sysctl_user_reserve_kbytes. |
| 3669 | * |
| 3670 | * This is intended to prevent a user from starting a single memory hogging |
| 3671 | * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER |
| 3672 | * mode. |
| 3673 | * |
| 3674 | * The default value is min(3% of free memory, 128MB) |
| 3675 | * 128MB is enough to recover with sshd/login, bash, and top/kill. |
| 3676 | */ |
Andrew Shewmaker | 1640879 | 2013-04-29 15:08:12 -0700 | [diff] [blame] | 3677 | static int init_user_reserve(void) |
Andrew Shewmaker | c9b1d09 | 2013-04-29 15:08:10 -0700 | [diff] [blame] | 3678 | { |
| 3679 | unsigned long free_kbytes; |
| 3680 | |
| 3681 | free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
| 3682 | |
| 3683 | sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); |
| 3684 | return 0; |
| 3685 | } |
Paul Gortmaker | a64fb3c | 2014-01-23 15:53:30 -0800 | [diff] [blame] | 3686 | subsys_initcall(init_user_reserve); |
Andrew Shewmaker | 4eeab4f | 2013-04-29 15:08:11 -0700 | [diff] [blame] | 3687 | |
| 3688 | /* |
| 3689 | * Initialise sysctl_admin_reserve_kbytes. |
| 3690 | * |
| 3691 | * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin |
| 3692 | * to log in and kill a memory hogging process. |
| 3693 | * |
| 3694 | * Systems with more than 256MB will reserve 8MB, enough to recover |
| 3695 | * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will |
| 3696 | * only reserve 3% of free pages by default. |
| 3697 | */ |
Andrew Shewmaker | 1640879 | 2013-04-29 15:08:12 -0700 | [diff] [blame] | 3698 | static int init_admin_reserve(void) |
Andrew Shewmaker | 4eeab4f | 2013-04-29 15:08:11 -0700 | [diff] [blame] | 3699 | { |
| 3700 | unsigned long free_kbytes; |
| 3701 | |
| 3702 | free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
| 3703 | |
| 3704 | sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); |
| 3705 | return 0; |
| 3706 | } |
Paul Gortmaker | a64fb3c | 2014-01-23 15:53:30 -0800 | [diff] [blame] | 3707 | subsys_initcall(init_admin_reserve); |
Andrew Shewmaker | 1640879 | 2013-04-29 15:08:12 -0700 | [diff] [blame] | 3708 | |
| 3709 | /* |
| 3710 | * Reinititalise user and admin reserves if memory is added or removed. |
| 3711 | * |
| 3712 | * The default user reserve max is 128MB, and the default max for the |
| 3713 | * admin reserve is 8MB. These are usually, but not always, enough to |
| 3714 | * enable recovery from a memory hogging process using login/sshd, a shell, |
| 3715 | * and tools like top. It may make sense to increase or even disable the |
| 3716 | * reserve depending on the existence of swap or variations in the recovery |
| 3717 | * tools. So, the admin may have changed them. |
| 3718 | * |
| 3719 | * If memory is added and the reserves have been eliminated or increased above |
| 3720 | * the default max, then we'll trust the admin. |
| 3721 | * |
| 3722 | * If memory is removed and there isn't enough free memory, then we |
| 3723 | * need to reset the reserves. |
| 3724 | * |
| 3725 | * Otherwise keep the reserve set by the admin. |
| 3726 | */ |
| 3727 | static int reserve_mem_notifier(struct notifier_block *nb, |
| 3728 | unsigned long action, void *data) |
| 3729 | { |
| 3730 | unsigned long tmp, free_kbytes; |
| 3731 | |
| 3732 | switch (action) { |
| 3733 | case MEM_ONLINE: |
| 3734 | /* Default max is 128MB. Leave alone if modified by operator. */ |
| 3735 | tmp = sysctl_user_reserve_kbytes; |
| 3736 | if (0 < tmp && tmp < (1UL << 17)) |
| 3737 | init_user_reserve(); |
| 3738 | |
| 3739 | /* Default max is 8MB. Leave alone if modified by operator. */ |
| 3740 | tmp = sysctl_admin_reserve_kbytes; |
| 3741 | if (0 < tmp && tmp < (1UL << 13)) |
| 3742 | init_admin_reserve(); |
| 3743 | |
| 3744 | break; |
| 3745 | case MEM_OFFLINE: |
| 3746 | free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
| 3747 | |
| 3748 | if (sysctl_user_reserve_kbytes > free_kbytes) { |
| 3749 | init_user_reserve(); |
| 3750 | pr_info("vm.user_reserve_kbytes reset to %lu\n", |
| 3751 | sysctl_user_reserve_kbytes); |
| 3752 | } |
| 3753 | |
| 3754 | if (sysctl_admin_reserve_kbytes > free_kbytes) { |
| 3755 | init_admin_reserve(); |
| 3756 | pr_info("vm.admin_reserve_kbytes reset to %lu\n", |
| 3757 | sysctl_admin_reserve_kbytes); |
| 3758 | } |
| 3759 | break; |
| 3760 | default: |
| 3761 | break; |
| 3762 | } |
| 3763 | return NOTIFY_OK; |
| 3764 | } |
| 3765 | |
| 3766 | static struct notifier_block reserve_mem_nb = { |
| 3767 | .notifier_call = reserve_mem_notifier, |
| 3768 | }; |
| 3769 | |
| 3770 | static int __meminit init_reserve_notifier(void) |
| 3771 | { |
| 3772 | if (register_hotmemory_notifier(&reserve_mem_nb)) |
Mitchel Humpherys | b1de0d1 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 3773 | pr_err("Failed registering memory add/remove notifier for admin reserve\n"); |
Andrew Shewmaker | 1640879 | 2013-04-29 15:08:12 -0700 | [diff] [blame] | 3774 | |
| 3775 | return 0; |
| 3776 | } |
Paul Gortmaker | a64fb3c | 2014-01-23 15:53:30 -0800 | [diff] [blame] | 3777 | subsys_initcall(init_reserve_notifier); |