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