blob: 183694b80bcc85f1ae7ec050135648046133c4c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/mmap.c
3 *
4 * Written by obz.
5 *
Alan Cox046c6882009-01-05 14:06:29 +00006 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Cyril Hrubise8420a82013-04-29 15:08:33 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070013#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070015#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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 Dickinsc01d5b32016-07-26 15:26:15 -070028#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040030#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/mount.h>
32#include <linux/mempolicy.h>
33#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070034#include <linux/mmu_notifier.h>
Konstantin Khlebnikov82f71ae2014-08-06 16:06:36 -070035#include <linux/mmdebug.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020036#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040037#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080038#include <linux/khugepaged.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053039#include <linux/uprobes.h>
Michel Lespinassed3737182012-12-11 16:01:38 -080040#include <linux/rbtree_augmented.h>
Andrew Shewmaker16408792013-04-29 15:08:12 -070041#include <linux/notifier.h>
42#include <linux/memory.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070043#include <linux/printk.h>
Andrea Arcangeli19a809a2015-09-04 15:46:24 -070044#include <linux/userfaultfd_k.h>
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -080045#include <linux/moduleparam.h>
Dave Hansen62b5f7d2016-02-12 13:02:40 -080046#include <linux/pkeys.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/uaccess.h>
49#include <asm/cacheflush.h>
50#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020051#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jan Beulich42b77722008-07-23 21:27:10 -070053#include "internal.h"
54
Kirill Korotaev3a459752006-09-07 14:17:04 +040055#ifndef arch_mmap_check
56#define arch_mmap_check(addr, len, flags) (0)
57#endif
58
Daniel Cashmand07e2252016-01-14 15:19:53 -080059#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
60const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
61const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
62int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
63#endif
64#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
65const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
66const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
67int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
68#endif
69
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -070070static bool ignore_rlimit_data;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -080071core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
Daniel Cashmand07e2252016-01-14 15:19:53 -080072
Hugh Dickinse0da3822005-04-19 13:29:15 -070073static 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 Torvalds1da177e2005-04-16 15:20:36 -070077/* 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.pscc71aba2014-10-09 15:26:29 -070086 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * 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 *
Catalin Marinascab15ce2016-08-11 18:44:50 +010091 * On arm64, PROT_EXEC has the following behaviour for both MAP_SHARED and
92 * MAP_PRIVATE:
93 * r: (no) no
94 * w: (no) no
95 * x: (yes) yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97pgprot_t protection_map[16] = {
98 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
99 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
100};
101
Hugh Dickins804af2c2006-07-26 21:39:49 +0100102pgprot_t vm_get_page_prot(unsigned long vm_flags)
103{
Dave Kleikampb845f312008-07-08 00:28:51 +1000104 return __pgprot(pgprot_val(protection_map[vm_flags &
105 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
106 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +0100107}
108EXPORT_SYMBOL(vm_get_page_prot);
109
Peter Feiner64e45502014-10-13 15:55:46 -0700110static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags)
111{
112 return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
113}
114
115/* Update vma->vm_page_prot to reflect vma->vm_flags. */
116void vma_set_page_prot(struct vm_area_struct *vma)
117{
118 unsigned long vm_flags = vma->vm_flags;
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700119 pgprot_t vm_page_prot;
Peter Feiner64e45502014-10-13 15:55:46 -0700120
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700121 vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
122 if (vma_wants_writenotify(vma, vm_page_prot)) {
Peter Feiner64e45502014-10-13 15:55:46 -0700123 vm_flags &= ~VM_SHARED;
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700124 vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags);
Peter Feiner64e45502014-10-13 15:55:46 -0700125 }
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700126 /* remove_protection_ptes reads vma->vm_page_prot without mmap_sem */
127 WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
Peter Feiner64e45502014-10-13 15:55:46 -0700128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800131 * Requires inode->i_mapping->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
133static void __remove_shared_vm_struct(struct vm_area_struct *vma,
134 struct file *file, struct address_space *mapping)
135{
136 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500137 atomic_inc(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700139 mapping_unmap_writable(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800142 vma_interval_tree_remove(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 flush_dcache_mmap_unlock(mapping);
144}
145
146/*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700147 * Unlink a file-based vm structure from its interval tree, to hide
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700148 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700150void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct file *file = vma->vm_file;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (file) {
155 struct address_space *mapping = file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800156 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 __remove_shared_vm_struct(vma, file, mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800158 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700160}
161
162/*
163 * Close a vm structure and free it, returning the next.
164 */
165static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
166{
167 struct vm_area_struct *next = vma->vm_next;
168
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700169 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (vma->vm_ops && vma->vm_ops->close)
171 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700172 if (vma->vm_file)
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700173 fput(vma->vm_file);
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700174 mpol_put(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700176 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700179static int do_brk(unsigned long addr, unsigned long len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700180
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100181SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700183 unsigned long retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned long newbrk, oldbrk;
185 struct mm_struct *mm = current->mm;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700186 unsigned long min_brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800187 bool populate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700189 if (down_write_killable(&mm->mmap_sem))
190 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700192#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800193 /*
194 * CONFIG_COMPAT_BRK can still be overridden by setting
195 * randomize_va_space to 2, which will still cause mm->start_brk
196 * to be arbitrarily shifted
197 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700198 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800199 min_brk = mm->start_brk;
200 else
201 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700202#else
203 min_brk = mm->start_brk;
204#endif
205 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700207
208 /*
209 * Check against rlimit here. If this check is done later after the test
210 * of oldbrk with newbrk then it can escape the test and let the data
211 * segment grow beyond its set limit the in case where the limit is
212 * not page aligned -Ram Gupta
213 */
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700214 if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
215 mm->end_data, mm->start_data))
Ram Gupta1e624192006-04-10 22:52:57 -0700216 goto out;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 newbrk = PAGE_ALIGN(brk);
219 oldbrk = PAGE_ALIGN(mm->brk);
220 if (oldbrk == newbrk)
221 goto set_brk;
222
223 /* Always allow shrinking brk. */
224 if (brk <= mm->brk) {
225 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
226 goto set_brk;
227 goto out;
228 }
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Check against existing mmap mappings. */
231 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
232 goto out;
233
234 /* Ok, looks good - let it rip. */
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700235 if (do_brk(oldbrk, newbrk-oldbrk) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto out;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238set_brk:
239 mm->brk = brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800240 populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0;
241 up_write(&mm->mmap_sem);
242 if (populate)
243 mm_populate(oldbrk, newbrk - oldbrk);
244 return brk;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246out:
247 retval = mm->brk;
248 up_write(&mm->mmap_sem);
249 return retval;
250}
251
Michel Lespinassed3737182012-12-11 16:01:38 -0800252static long vma_compute_subtree_gap(struct vm_area_struct *vma)
253{
254 unsigned long max, subtree_gap;
255 max = vma->vm_start;
256 if (vma->vm_prev)
257 max -= vma->vm_prev->vm_end;
258 if (vma->vm_rb.rb_left) {
259 subtree_gap = rb_entry(vma->vm_rb.rb_left,
260 struct vm_area_struct, vm_rb)->rb_subtree_gap;
261 if (subtree_gap > max)
262 max = subtree_gap;
263 }
264 if (vma->vm_rb.rb_right) {
265 subtree_gap = rb_entry(vma->vm_rb.rb_right,
266 struct vm_area_struct, vm_rb)->rb_subtree_gap;
267 if (subtree_gap > max)
268 max = subtree_gap;
269 }
270 return max;
271}
272
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700273#ifdef CONFIG_DEBUG_VM_RB
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800274static int browse_rb(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800276 struct rb_root *root = &mm->mm_rb;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800277 int i = 0, j, bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct rb_node *nd, *pn = NULL;
279 unsigned long prev = 0, pend = 0;
280
281 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
282 struct vm_area_struct *vma;
283 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800284 if (vma->vm_start < prev) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700285 pr_emerg("vm_start %lx < prev %lx\n",
286 vma->vm_start, prev);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800287 bug = 1;
288 }
289 if (vma->vm_start < pend) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700290 pr_emerg("vm_start %lx < pend %lx\n",
291 vma->vm_start, pend);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800292 bug = 1;
293 }
294 if (vma->vm_start > vma->vm_end) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700295 pr_emerg("vm_start %lx > vm_end %lx\n",
296 vma->vm_start, vma->vm_end);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800297 bug = 1;
298 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800299 spin_lock(&mm->page_table_lock);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800300 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700301 pr_emerg("free gap %lx, correct %lx\n",
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800302 vma->rb_subtree_gap,
303 vma_compute_subtree_gap(vma));
304 bug = 1;
305 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800306 spin_unlock(&mm->page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 i++;
308 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800309 prev = vma->vm_start;
310 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 j = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800313 for (nd = pn; nd; nd = rb_prev(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 j++;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800315 if (i != j) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700316 pr_emerg("backwards %d, forwards %d\n", j, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800317 bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800319 return bug ? -1 : i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Michel Lespinassed3737182012-12-11 16:01:38 -0800322static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
323{
324 struct rb_node *nd;
325
326 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
327 struct vm_area_struct *vma;
328 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Sasha Levin96dad672014-10-09 15:28:39 -0700329 VM_BUG_ON_VMA(vma != ignore &&
330 vma->rb_subtree_gap != vma_compute_subtree_gap(vma),
331 vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Rashika Kheriaeafd4dc2014-04-03 14:48:03 -0700335static void validate_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 int bug = 0;
338 int i = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800339 unsigned long highest_address = 0;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700340 struct vm_area_struct *vma = mm->mmap;
Andrew Mortonff26f702014-10-09 15:28:19 -0700341
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700342 while (vma) {
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800343 struct anon_vma *anon_vma = vma->anon_vma;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700344 struct anon_vma_chain *avc;
Andrew Mortonff26f702014-10-09 15:28:19 -0700345
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800346 if (anon_vma) {
347 anon_vma_lock_read(anon_vma);
348 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
349 anon_vma_interval_tree_verify(avc);
350 anon_vma_unlock_read(anon_vma);
351 }
352
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800353 highest_address = vma->vm_end;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700354 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 i++;
356 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800357 if (i != mm->map_count) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700358 pr_emerg("map_count %d vm_next %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800359 bug = 1;
360 }
361 if (highest_address != mm->highest_vm_end) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700362 pr_emerg("mm->highest_vm_end %lx, found %lx\n",
Andrew Mortonff26f702014-10-09 15:28:19 -0700363 mm->highest_vm_end, highest_address);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800364 bug = 1;
365 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800366 i = browse_rb(mm);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800367 if (i != mm->map_count) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700368 if (i != -1)
369 pr_emerg("map_count %d rb %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800370 bug = 1;
371 }
Sasha Levin96dad672014-10-09 15:28:39 -0700372 VM_BUG_ON_MM(bug, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374#else
Michel Lespinassed3737182012-12-11 16:01:38 -0800375#define validate_mm_rb(root, ignore) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#define validate_mm(mm) do { } while (0)
377#endif
378
Michel Lespinassed3737182012-12-11 16:01:38 -0800379RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
380 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
381
382/*
383 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
384 * vma->vm_prev->vm_end values changed, without modifying the vma's position
385 * in the rbtree.
386 */
387static void vma_gap_update(struct vm_area_struct *vma)
388{
389 /*
390 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
391 * function that does exacltly what we want.
392 */
393 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
394}
395
396static inline void vma_rb_insert(struct vm_area_struct *vma,
397 struct rb_root *root)
398{
399 /* All rb_subtree_gap values must be consistent prior to insertion */
400 validate_mm_rb(root, NULL);
401
402 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
403}
404
405static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
406{
407 /*
408 * All rb_subtree_gap values must be consistent prior to erase,
409 * with the possible exception of the vma being erased.
410 */
411 validate_mm_rb(root, vma);
412
413 /*
414 * Note rb_erase_augmented is a fairly large inline function,
415 * so make sure we instantiate it only once with our desired
416 * augmented rbtree callbacks.
417 */
418 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
419}
420
Michel Lespinassebf181b92012-10-08 16:31:39 -0700421/*
422 * vma has some anon_vma assigned, and is already inserted on that
423 * anon_vma's interval trees.
424 *
425 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
426 * vma must be removed from the anon_vma's interval trees using
427 * anon_vma_interval_tree_pre_update_vma().
428 *
429 * After the update, the vma will be reinserted using
430 * anon_vma_interval_tree_post_update_vma().
431 *
432 * The entire update must be protected by exclusive mmap_sem and by
433 * the root anon_vma's mutex.
434 */
435static inline void
436anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
437{
438 struct anon_vma_chain *avc;
439
440 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
441 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
442}
443
444static inline void
445anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
446{
447 struct anon_vma_chain *avc;
448
449 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
450 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
451}
452
Hugh Dickins6597d782012-10-08 16:29:07 -0700453static int find_vma_links(struct mm_struct *mm, unsigned long addr,
454 unsigned long end, struct vm_area_struct **pprev,
455 struct rb_node ***rb_link, struct rb_node **rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Hugh Dickins6597d782012-10-08 16:29:07 -0700457 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 __rb_link = &mm->mm_rb.rb_node;
460 rb_prev = __rb_parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 while (*__rb_link) {
463 struct vm_area_struct *vma_tmp;
464
465 __rb_parent = *__rb_link;
466 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
467
468 if (vma_tmp->vm_end > addr) {
Hugh Dickins6597d782012-10-08 16:29:07 -0700469 /* Fail if an existing vma overlaps the area */
470 if (vma_tmp->vm_start < end)
471 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 __rb_link = &__rb_parent->rb_left;
473 } else {
474 rb_prev = __rb_parent;
475 __rb_link = &__rb_parent->rb_right;
476 }
477 }
478
479 *pprev = NULL;
480 if (rb_prev)
481 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
482 *rb_link = __rb_link;
483 *rb_parent = __rb_parent;
Hugh Dickins6597d782012-10-08 16:29:07 -0700484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Cyril Hrubise8420a82013-04-29 15:08:33 -0700487static unsigned long count_vma_pages_range(struct mm_struct *mm,
488 unsigned long addr, unsigned long end)
489{
490 unsigned long nr_pages = 0;
491 struct vm_area_struct *vma;
492
493 /* Find first overlaping mapping */
494 vma = find_vma_intersection(mm, addr, end);
495 if (!vma)
496 return 0;
497
498 nr_pages = (min(end, vma->vm_end) -
499 max(addr, vma->vm_start)) >> PAGE_SHIFT;
500
501 /* Iterate over the rest of the overlaps */
502 for (vma = vma->vm_next; vma; vma = vma->vm_next) {
503 unsigned long overlap_len;
504
505 if (vma->vm_start > end)
506 break;
507
508 overlap_len = min(end, vma->vm_end) - vma->vm_start;
509 nr_pages += overlap_len >> PAGE_SHIFT;
510 }
511
512 return nr_pages;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
516 struct rb_node **rb_link, struct rb_node *rb_parent)
517{
Michel Lespinassed3737182012-12-11 16:01:38 -0800518 /* Update tracking information for the gap following the new vma. */
519 if (vma->vm_next)
520 vma_gap_update(vma->vm_next);
521 else
522 mm->highest_vm_end = vma->vm_end;
523
524 /*
525 * vma->vm_prev wasn't known when we followed the rbtree to find the
526 * correct insertion point for that vma. As a result, we could not
527 * update the vma vm_rb parents rb_subtree_gap values on the way down.
528 * So, we first insert the vma with a zero rb_subtree_gap value
529 * (to be consistent with what we did on the way down), and then
530 * immediately update the gap to the correct value. Finally we
531 * rebalance the rbtree after all augmented values have been set.
532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
Michel Lespinassed3737182012-12-11 16:01:38 -0800534 vma->rb_subtree_gap = 0;
535 vma_gap_update(vma);
536 vma_rb_insert(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700539static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
ZhenwenXu48aae422009-01-06 14:40:21 -0800541 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 file = vma->vm_file;
544 if (file) {
545 struct address_space *mapping = file->f_mapping;
546
547 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500548 atomic_dec(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700550 atomic_inc(&mapping->i_mmap_writable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800553 vma_interval_tree_insert(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 flush_dcache_mmap_unlock(mapping);
555 }
556}
557
558static void
559__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
560 struct vm_area_struct *prev, struct rb_node **rb_link,
561 struct rb_node *rb_parent)
562{
563 __vma_link_list(mm, vma, prev, rb_parent);
564 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
568 struct vm_area_struct *prev, struct rb_node **rb_link,
569 struct rb_node *rb_parent)
570{
571 struct address_space *mapping = NULL;
572
Huang Shijie64ac4942014-06-04 16:07:33 -0700573 if (vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 mapping = vma->vm_file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800575 i_mmap_lock_write(mapping);
Huang Shijie64ac4942014-06-04 16:07:33 -0700576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 __vma_link(mm, vma, prev, rb_link, rb_parent);
579 __vma_link_file(vma);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800582 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 mm->map_count++;
585 validate_mm(mm);
586}
587
588/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700589 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700590 * mm's list and rbtree. It has already been inserted into the interval tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800592static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Hugh Dickins6597d782012-10-08 16:29:07 -0700594 struct vm_area_struct *prev;
ZhenwenXu48aae422009-01-06 14:40:21 -0800595 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Hugh Dickins6597d782012-10-08 16:29:07 -0700597 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
598 &prev, &rb_link, &rb_parent))
599 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 __vma_link(mm, vma, prev, rb_link, rb_parent);
601 mm->map_count++;
602}
603
604static inline void
605__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
606 struct vm_area_struct *prev)
607{
Michel Lespinassed3737182012-12-11 16:01:38 -0800608 struct vm_area_struct *next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700609
Michel Lespinassed3737182012-12-11 16:01:38 -0800610 vma_rb_erase(vma, &mm->mm_rb);
611 prev->vm_next = next = vma->vm_next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700612 if (next)
613 next->vm_prev = prev;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700614
615 /* Kill the cache */
616 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/*
620 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
621 * is already present in an i_mmap tree without adjusting the tree.
622 * The following helper function should be used when such adjustments
623 * are necessary. The "insert" vma (if any) is to be inserted
624 * before we drop the necessary locks.
625 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800626int vma_adjust(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
628{
629 struct mm_struct *mm = vma->vm_mm;
630 struct vm_area_struct *next = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct address_space *mapping = NULL;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700632 struct rb_root *root = NULL;
Rik van Riel012f18002010-08-09 17:18:40 -0700633 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct file *file = vma->vm_file;
Michel Lespinassed3737182012-12-11 16:01:38 -0800635 bool start_changed = false, end_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 long adjust_next = 0;
637 int remove_next = 0;
638
639 if (next && !insert) {
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700640 struct vm_area_struct *exporter = NULL, *importer = NULL;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (end >= next->vm_end) {
643 /*
644 * vma expands, overlapping all the next, and
645 * perhaps the one after too (mprotect case 6).
646 */
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700647 remove_next = 1 + (end > next->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 end = next->vm_end;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700649 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 importer = vma;
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700651
652 /*
653 * If next doesn't have anon_vma, import from vma after
654 * next, if the vma overlaps with it.
655 */
656 if (remove_next == 2 && next && !next->anon_vma)
657 exporter = next->vm_next;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else if (end > next->vm_start) {
660 /*
661 * vma expands, overlapping part of the next:
662 * mprotect case 5 shifting the boundary up.
663 */
664 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700665 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 importer = vma;
667 } else if (end < vma->vm_end) {
668 /*
669 * vma shrinks, and !insert tells it's not
670 * split_vma inserting another: so it must be
671 * mprotect case 4 shifting the boundary down.
672 */
vishnu.pscc71aba2014-10-09 15:26:29 -0700673 adjust_next = -((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700674 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 importer = next;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Rik van Riel5beb4932010-03-05 13:42:07 -0800678 /*
679 * Easily overlooked: when mprotect shifts the boundary,
680 * make sure the expanding vma has anon_vma set if the
681 * shrinking vma had, to cover any anon pages imported.
682 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700683 if (exporter && exporter->anon_vma && !importer->anon_vma) {
Daniel Forrestc4ea95d2014-12-02 15:59:42 -0800684 int error;
685
Linus Torvalds287d97a2010-04-10 15:22:30 -0700686 importer->anon_vma = exporter->anon_vma;
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300687 error = anon_vma_clone(importer, exporter);
Leon Yu3fe89b32015-03-25 15:55:11 -0700688 if (error)
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300689 return error;
Rik van Riel5beb4932010-03-05 13:42:07 -0800690 }
691 }
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700692again:
Kirill A. Shutemov37f9f552016-07-26 15:25:48 -0700693 vma_adjust_trans_huge(vma, start, end, adjust_next);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (file) {
696 mapping = file->f_mapping;
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800697 root = &mapping->i_mmap;
698 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530699
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800700 if (adjust_next)
701 uprobe_munmap(next, next->vm_start, next->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530702
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800703 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700706 * Put into interval tree now, so instantiated pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * are visible to arm/parisc __flush_dcache_page
708 * throughout; but we cannot insert into address
709 * space until vma start or end is updated.
710 */
711 __vma_link_file(insert);
712 }
713 }
714
Michel Lespinassebf181b92012-10-08 16:31:39 -0700715 anon_vma = vma->anon_vma;
716 if (!anon_vma && adjust_next)
717 anon_vma = next->anon_vma;
718 if (anon_vma) {
Sasha Levin81d1b092014-10-09 15:28:10 -0700719 VM_BUG_ON_VMA(adjust_next && next->anon_vma &&
720 anon_vma != next->anon_vma, next);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000721 anon_vma_lock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700722 anon_vma_interval_tree_pre_update_vma(vma);
723 if (adjust_next)
724 anon_vma_interval_tree_pre_update_vma(next);
725 }
Rik van Riel012f18002010-08-09 17:18:40 -0700726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (root) {
728 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700729 vma_interval_tree_remove(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700731 vma_interval_tree_remove(next, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
Michel Lespinassed3737182012-12-11 16:01:38 -0800734 if (start != vma->vm_start) {
735 vma->vm_start = start;
736 start_changed = true;
737 }
738 if (end != vma->vm_end) {
739 vma->vm_end = end;
740 end_changed = true;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 vma->vm_pgoff = pgoff;
743 if (adjust_next) {
744 next->vm_start += adjust_next << PAGE_SHIFT;
745 next->vm_pgoff += adjust_next;
746 }
747
748 if (root) {
749 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700750 vma_interval_tree_insert(next, root);
751 vma_interval_tree_insert(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 flush_dcache_mmap_unlock(mapping);
753 }
754
755 if (remove_next) {
756 /*
757 * vma_merge has merged next into vma, and needs
758 * us to remove next before dropping the locks.
759 */
760 __vma_unlink(mm, next, vma);
761 if (file)
762 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 } else if (insert) {
764 /*
765 * split_vma has split insert from vma, and needs
766 * us to insert it before dropping the locks
767 * (it may either follow vma or precede it).
768 */
769 __insert_vm_struct(mm, insert);
Michel Lespinassed3737182012-12-11 16:01:38 -0800770 } else {
771 if (start_changed)
772 vma_gap_update(vma);
773 if (end_changed) {
774 if (!next)
775 mm->highest_vm_end = end;
776 else if (!adjust_next)
777 vma_gap_update(next);
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Michel Lespinassebf181b92012-10-08 16:31:39 -0700781 if (anon_vma) {
782 anon_vma_interval_tree_post_update_vma(vma);
783 if (adjust_next)
784 anon_vma_interval_tree_post_update_vma(next);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800785 anon_vma_unlock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800788 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530790 if (root) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100791 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530792
793 if (adjust_next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100794 uprobe_mmap(next);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530795 }
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700798 if (file) {
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530799 uprobe_munmap(next, next->vm_start, next->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700801 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800802 if (next->anon_vma)
803 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 mm->map_count--;
Oleg Nesterov3964acd2013-07-31 13:53:28 -0700805 mpol_put(vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 kmem_cache_free(vm_area_cachep, next);
807 /*
808 * In mprotect's case 6 (see comments on vma_merge),
809 * we must remove another next too. It would clutter
810 * up the code too much to do both in one go.
811 */
Michel Lespinassed3737182012-12-11 16:01:38 -0800812 next = vma->vm_next;
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700813 if (remove_next == 2) {
814 remove_next = 1;
815 end = next->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto again;
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700817 }
Michel Lespinassed3737182012-12-11 16:01:38 -0800818 else if (next)
819 vma_gap_update(next);
Andrea Arcangelifb8c41e2016-10-07 17:01:25 -0700820 else {
821 /*
822 * If remove_next == 2 we obviously can't
823 * reach this path.
824 *
825 * If remove_next == 3 we can't reach this
826 * path because pre-swap() next is always not
827 * NULL. pre-swap() "next" is not being
828 * removed and its next->vm_end is not altered
829 * (and furthermore "end" already matches
830 * next->vm_end in remove_next == 3).
831 *
832 * We reach this only in the remove_next == 1
833 * case if the "next" vma that was removed was
834 * the highest vma of the mm. However in such
835 * case next->vm_end == "end" and the extended
836 * "vma" has vma->vm_end == next->vm_end so
837 * mm->highest_vm_end doesn't need any update
838 * in remove_next == 1 case.
839 */
840 VM_WARN_ON(mm->highest_vm_end != end);
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530843 if (insert && file)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100844 uprobe_mmap(insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800847
848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
852 * If the vma has a ->close operation then the driver probably needs to release
853 * per-vma resources, so we don't attempt to merge those.
854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static inline int is_mergeable_vma(struct vm_area_struct *vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700856 struct file *file, unsigned long vm_flags,
857 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Cyrill Gorcunov34228d42014-01-23 15:53:42 -0800859 /*
860 * VM_SOFTDIRTY should not prevent from VMA merging, if we
861 * match the flags but dirty bit -- the caller should mark
862 * merged VMA as dirty. If dirty bit won't be excluded from
863 * comparison, we increase pressue on the memory system forcing
864 * the kernel to generate new VMAs when old one could be
865 * extended instead.
866 */
867 if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869 if (vma->vm_file != file)
870 return 0;
871 if (vma->vm_ops && vma->vm_ops->close)
872 return 0;
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700873 if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx))
874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 1;
876}
877
878static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700879 struct anon_vma *anon_vma2,
880 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Shaohua Li965f55d2011-05-24 17:11:20 -0700882 /*
883 * The list_is_singular() test is to avoid merging VMA cloned from
884 * parents. This can improve scalability caused by anon_vma lock.
885 */
886 if ((!anon_vma1 || !anon_vma2) && (!vma ||
887 list_is_singular(&vma->anon_vma_chain)))
888 return 1;
889 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892/*
893 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
894 * in front of (at a lower virtual address and file offset than) the vma.
895 *
896 * We cannot merge two vmas if they have differently assigned (non-NULL)
897 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
898 *
899 * We don't check here for the merged mmap wrapping around the end of pagecache
900 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
901 * wrap, nor mmaps which cover the final page at index -1UL.
902 */
903static int
904can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700905 struct anon_vma *anon_vma, struct file *file,
906 pgoff_t vm_pgoff,
907 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700909 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700910 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (vma->vm_pgoff == vm_pgoff)
912 return 1;
913 }
914 return 0;
915}
916
917/*
918 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
919 * beyond (at a higher virtual address and file offset than) the vma.
920 *
921 * We cannot merge two vmas if they have differently assigned (non-NULL)
922 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
923 */
924static int
925can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700926 struct anon_vma *anon_vma, struct file *file,
927 pgoff_t vm_pgoff,
928 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700930 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700931 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 pgoff_t vm_pglen;
Libind6e93212013-07-03 15:01:26 -0700933 vm_pglen = vma_pages(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
935 return 1;
936 }
937 return 0;
938}
939
940/*
941 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
942 * whether that can be merged with its predecessor or its successor.
943 * Or both (it neatly fills a hole).
944 *
945 * In most cases - when called for mmap, brk or mremap - [addr,end) is
946 * certain not to be mapped by the time vma_merge is called; but when
947 * called for mprotect, it is certain to be already mapped (either at
948 * an offset within prev, or at the start of next), and the flags of
949 * this area are about to be changed to vm_flags - and the no-change
950 * case has already been eliminated.
951 *
952 * The following mprotect cases have to be considered, where AAAA is
953 * the area passed down from mprotect_fixup, never extending beyond one
954 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
955 *
956 * AAAA AAAA AAAA AAAA
957 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
958 * cannot merge might become might become might become
959 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
960 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
961 * mremap move: PPPPNNNNNNNN 8
962 * AAAA
963 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
964 * might become case 1 below case 2 below case 3 below
965 *
966 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
967 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
968 */
969struct vm_area_struct *vma_merge(struct mm_struct *mm,
970 struct vm_area_struct *prev, unsigned long addr,
971 unsigned long end, unsigned long vm_flags,
vishnu.pscc71aba2014-10-09 15:26:29 -0700972 struct anon_vma *anon_vma, struct file *file,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700973 pgoff_t pgoff, struct mempolicy *policy,
974 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
977 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800978 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /*
981 * We later require that vma->vm_flags == vm_flags,
982 * so this tests vma->vm_flags & VM_SPECIAL, too.
983 */
984 if (vm_flags & VM_SPECIAL)
985 return NULL;
986
987 if (prev)
988 next = prev->vm_next;
989 else
990 next = mm->mmap;
991 area = next;
992 if (next && next->vm_end == end) /* cases 6, 7, 8 */
993 next = next->vm_next;
994
995 /*
996 * Can it merge with the predecessor?
997 */
998 if (prev && prev->vm_end == addr &&
vishnu.pscc71aba2014-10-09 15:26:29 -0700999 mpol_equal(vma_policy(prev), policy) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 can_vma_merge_after(prev, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001001 anon_vma, file, pgoff,
1002 vm_userfaultfd_ctx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * OK, it can. Can we now merge in the successor as well?
1005 */
1006 if (next && end == next->vm_start &&
1007 mpol_equal(policy, vma_policy(next)) &&
1008 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001009 anon_vma, file,
1010 pgoff+pglen,
1011 vm_userfaultfd_ctx) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -07001013 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* cases 1, 6 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001015 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 next->vm_end, prev->vm_pgoff, NULL);
1017 } else /* cases 2, 5, 7 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001018 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 end, prev->vm_pgoff, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001020 if (err)
1021 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001022 khugepaged_enter_vma_merge(prev, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return prev;
1024 }
1025
1026 /*
1027 * Can this new request be merged in front of next?
1028 */
1029 if (next && end == next->vm_start &&
vishnu.pscc71aba2014-10-09 15:26:29 -07001030 mpol_equal(policy, vma_policy(next)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001032 anon_vma, file, pgoff+pglen,
1033 vm_userfaultfd_ctx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (prev && addr < prev->vm_end) /* case 4 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001035 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 addr, prev->vm_pgoff, NULL);
1037 else /* cases 3, 8 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001038 err = vma_adjust(area, addr, next->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 next->vm_pgoff - pglen, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001040 if (err)
1041 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001042 khugepaged_enter_vma_merge(area, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return area;
1044 }
1045
1046 return NULL;
1047}
1048
1049/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001050 * Rough compatbility check to quickly see if it's even worth looking
1051 * at sharing an anon_vma.
1052 *
1053 * They need to have the same vm_file, and the flags can only differ
1054 * in things that mprotect may change.
1055 *
1056 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1057 * we can merge the two vma's. For example, we refuse to merge a vma if
1058 * there is a vm_ops->close() function, because that indicates that the
1059 * driver is doing some kind of reference counting. But that doesn't
1060 * really matter for the anon_vma sharing case.
1061 */
1062static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1063{
1064 return a->vm_end == b->vm_start &&
1065 mpol_equal(vma_policy(a), vma_policy(b)) &&
1066 a->vm_file == b->vm_file &&
Cyrill Gorcunov34228d42014-01-23 15:53:42 -08001067 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001068 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1069}
1070
1071/*
1072 * Do some basic sanity checking to see if we can re-use the anon_vma
1073 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1074 * the same as 'old', the other will be the new one that is trying
1075 * to share the anon_vma.
1076 *
1077 * NOTE! This runs with mm_sem held for reading, so it is possible that
1078 * the anon_vma of 'old' is concurrently in the process of being set up
1079 * by another page fault trying to merge _that_. But that's ok: if it
1080 * is being set up, that automatically means that it will be a singleton
1081 * acceptable for merging, so we can do all of this optimistically. But
Jason Low4db0c3c2015-04-15 16:14:08 -07001082 * we do that READ_ONCE() to make sure that we never re-load the pointer.
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001083 *
1084 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1085 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1086 * is to return an anon_vma that is "complex" due to having gone through
1087 * a fork).
1088 *
1089 * We also make sure that the two vma's are compatible (adjacent,
1090 * and with the same memory policies). That's all stable, even with just
1091 * a read lock on the mm_sem.
1092 */
1093static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1094{
1095 if (anon_vma_compatible(a, b)) {
Jason Low4db0c3c2015-04-15 16:14:08 -07001096 struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001097
1098 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1099 return anon_vma;
1100 }
1101 return NULL;
1102}
1103
1104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1106 * neighbouring vmas for a suitable anon_vma, before it goes off
1107 * to allocate a new anon_vma. It checks because a repetitive
1108 * sequence of mprotects and faults may otherwise lead to distinct
1109 * anon_vmas being allocated, preventing vma merge in subsequent
1110 * mprotect.
1111 */
1112struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1113{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001114 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 near = vma->vm_next;
1118 if (!near)
1119 goto try_prev;
1120
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001121 anon_vma = reusable_anon_vma(near, vma, near);
1122 if (anon_vma)
1123 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -07001125 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!near)
1127 goto none;
1128
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001129 anon_vma = reusable_anon_vma(near, near, vma);
1130 if (anon_vma)
1131 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132none:
1133 /*
1134 * There's no absolute need to look only at touching neighbours:
1135 * we could search further afield for "compatible" anon_vmas.
1136 * But it would probably just be a waste of time searching,
1137 * or lead to too many vmas hanging off the same anon_vma.
1138 * We're trying to allow mprotect remerging later on,
1139 * not trying to minimize memory used for anon_vmas.
1140 */
1141 return NULL;
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/*
Al Viro40401532012-02-13 03:58:52 +00001145 * If a hint addr is less than mmap_min_addr change hint to be as
1146 * low as possible but still greater than mmap_min_addr
1147 */
1148static inline unsigned long round_hint_to_min(unsigned long hint)
1149{
1150 hint &= PAGE_MASK;
1151 if (((void *)hint != NULL) &&
1152 (hint < mmap_min_addr))
1153 return PAGE_ALIGN(mmap_min_addr);
1154 return hint;
1155}
1156
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001157static inline int mlock_future_check(struct mm_struct *mm,
1158 unsigned long flags,
1159 unsigned long len)
1160{
1161 unsigned long locked, lock_limit;
1162
1163 /* mlock MCL_FUTURE? */
1164 if (flags & VM_LOCKED) {
1165 locked = len >> PAGE_SHIFT;
1166 locked += mm->locked_vm;
1167 lock_limit = rlimit(RLIMIT_MEMLOCK);
1168 lock_limit >>= PAGE_SHIFT;
1169 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1170 return -EAGAIN;
1171 }
1172 return 0;
1173}
1174
Al Viro40401532012-02-13 03:58:52 +00001175/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001176 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001178unsigned long do_mmap(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 unsigned long len, unsigned long prot,
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001180 unsigned long flags, vm_flags_t vm_flags,
1181 unsigned long pgoff, unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
vishnu.pscc71aba2014-10-09 15:26:29 -07001183 struct mm_struct *mm = current->mm;
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001184 int pkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Michel Lespinasse41badc12013-02-22 16:32:47 -08001186 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001187
Piotr Kwapulinskie37609b2015-06-24 16:58:39 -07001188 if (!len)
1189 return -EINVAL;
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * Does the application expect PROT_READ to imply PROT_EXEC?
1193 *
1194 * (the exception is when the underlying filesystem is noexec
1195 * mounted, in which case we dont add PROT_EXEC.)
1196 */
1197 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Eric W. Biederman90f85722015-06-29 14:42:03 -05001198 if (!(file && path_noexec(&file->f_path)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 prot |= PROT_EXEC;
1200
Eric Paris7cd94142007-11-26 18:47:40 -05001201 if (!(flags & MAP_FIXED))
1202 addr = round_hint_to_min(addr);
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Careful about overflows.. */
1205 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001206 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -ENOMEM;
1208
1209 /* offset overflow? */
1210 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
vishnu.pscc71aba2014-10-09 15:26:29 -07001211 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* Too many mappings? */
1214 if (mm->map_count > sysctl_max_map_count)
1215 return -ENOMEM;
1216
1217 /* Obtain the address to map to. we verify (or select) it and ensure
1218 * that it represents a valid section of the address space.
1219 */
1220 addr = get_unmapped_area(file, addr, len, pgoff, flags);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001221 if (offset_in_page(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return addr;
1223
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001224 if (prot == PROT_EXEC) {
1225 pkey = execute_only_pkey(mm);
1226 if (pkey < 0)
1227 pkey = 0;
1228 }
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* Do simple checking here so the lower-level routines won't have
1231 * to. we assume access permissions have been handled by the open
1232 * of the memory object, so we don't do any here.
1233 */
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001234 vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1236
Huang Shijiecdf7b342009-09-21 17:03:36 -07001237 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!can_do_mlock())
1239 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001240
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001241 if (mlock_future_check(mm, vm_flags, len))
1242 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (file) {
Oleg Nesterov077bf222013-09-11 14:20:19 -07001245 struct inode *inode = file_inode(file);
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 switch (flags & MAP_TYPE) {
1248 case MAP_SHARED:
1249 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1250 return -EACCES;
1251
1252 /*
1253 * Make sure we don't allow writing to an append-only
1254 * file..
1255 */
1256 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1257 return -EACCES;
1258
1259 /*
1260 * Make sure there are no mandatory locks on the file.
1261 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001262 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EAGAIN;
1264
1265 vm_flags |= VM_SHARED | VM_MAYSHARE;
1266 if (!(file->f_mode & FMODE_WRITE))
1267 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1268
1269 /* fall through */
1270 case MAP_PRIVATE:
1271 if (!(file->f_mode & FMODE_READ))
1272 return -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001273 if (path_noexec(&file->f_path)) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001274 if (vm_flags & VM_EXEC)
1275 return -EPERM;
1276 vm_flags &= ~VM_MAYEXEC;
1277 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001278
Al Viro72c2d532013-09-22 16:27:52 -04001279 if (!file->f_op->mmap)
Linus Torvalds80c56062006-10-15 14:09:55 -07001280 return -ENODEV;
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001281 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1282 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284
1285 default:
1286 return -EINVAL;
1287 }
1288 } else {
1289 switch (flags & MAP_TYPE) {
1290 case MAP_SHARED:
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001291 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1292 return -EINVAL;
Tejun Heoce363942008-09-03 16:09:47 +02001293 /*
1294 * Ignore pgoff.
1295 */
1296 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 vm_flags |= VM_SHARED | VM_MAYSHARE;
1298 break;
1299 case MAP_PRIVATE:
1300 /*
1301 * Set pgoff according to addr for anon_vma.
1302 */
1303 pgoff = addr >> PAGE_SHIFT;
1304 break;
1305 default:
1306 return -EINVAL;
1307 }
1308 }
1309
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001310 /*
1311 * Set 'VM_NORESERVE' if we should not account for the
1312 * memory use of this mapping.
1313 */
1314 if (flags & MAP_NORESERVE) {
1315 /* We honor MAP_NORESERVE if allowed to overcommit */
1316 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1317 vm_flags |= VM_NORESERVE;
1318
1319 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1320 if (file && is_file_hugepages(file))
1321 vm_flags |= VM_NORESERVE;
1322 }
1323
1324 addr = mmap_region(file, addr, len, vm_flags, pgoff);
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -07001325 if (!IS_ERR_VALUE(addr) &&
1326 ((vm_flags & VM_LOCKED) ||
1327 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
Michel Lespinasse41badc12013-02-22 16:32:47 -08001328 *populate = len;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001329 return addr;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001330}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001331
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001332SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1333 unsigned long, prot, unsigned long, flags,
1334 unsigned long, fd, unsigned long, pgoff)
1335{
1336 struct file *file = NULL;
Chen Gang1e3ee142015-11-05 18:48:35 -08001337 unsigned long retval;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001338
1339 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001340 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001341 file = fget(fd);
1342 if (!file)
Chen Gang1e3ee142015-11-05 18:48:35 -08001343 return -EBADF;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001344 if (is_file_hugepages(file))
1345 len = ALIGN(len, huge_page_size(hstate_file(file)));
Jörn Engel493af572013-07-08 16:00:26 -07001346 retval = -EINVAL;
1347 if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
1348 goto out_fput;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001349 } else if (flags & MAP_HUGETLB) {
1350 struct user_struct *user = NULL;
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001351 struct hstate *hs;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001352
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001353 hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK);
Li Zefan091d0d52013-05-09 15:08:15 +08001354 if (!hs)
1355 return -EINVAL;
1356
1357 len = ALIGN(len, huge_page_size(hs));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001358 /*
1359 * VM_NORESERVE is used because the reservations will be
1360 * taken when vm_ops->mmap() is called
1361 * A dummy user value is used because we are not locking
1362 * memory so no accounting is necessary
1363 */
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001364 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001365 VM_NORESERVE,
1366 &user, HUGETLB_ANONHUGE_INODE,
1367 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001368 if (IS_ERR(file))
1369 return PTR_ERR(file);
1370 }
1371
1372 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1373
Michal Hocko9fbeb5a2016-05-23 16:25:30 -07001374 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Jörn Engel493af572013-07-08 16:00:26 -07001375out_fput:
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001376 if (file)
1377 fput(file);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001378 return retval;
1379}
1380
Christoph Hellwiga4679372010-03-10 15:21:15 -08001381#ifdef __ARCH_WANT_SYS_OLD_MMAP
1382struct mmap_arg_struct {
1383 unsigned long addr;
1384 unsigned long len;
1385 unsigned long prot;
1386 unsigned long flags;
1387 unsigned long fd;
1388 unsigned long offset;
1389};
1390
1391SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1392{
1393 struct mmap_arg_struct a;
1394
1395 if (copy_from_user(&a, arg, sizeof(a)))
1396 return -EFAULT;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001397 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001398 return -EINVAL;
1399
1400 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1401 a.offset >> PAGE_SHIFT);
1402}
1403#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1404
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001405/*
1406 * Some shared mappigns will want the pages marked read-only
1407 * to track write events. If so, we'll downgrade vm_page_prot
1408 * to the private version (using protection_map[] without the
1409 * VM_SHARED bit).
1410 */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001411int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001412{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001413 vm_flags_t vm_flags = vma->vm_flags;
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001414 const struct vm_operations_struct *vm_ops = vma->vm_ops;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001415
1416 /* If it was private or non-writable, the write bit is already clear */
1417 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1418 return 0;
1419
1420 /* The backer wishes to know when pages are first written to? */
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001421 if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001422 return 1;
1423
Peter Feiner64e45502014-10-13 15:55:46 -07001424 /* The open routine did something to the protections that pgprot_modify
1425 * won't preserve? */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001426 if (pgprot_val(vm_page_prot) !=
1427 pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001428 return 0;
1429
Peter Feiner64e45502014-10-13 15:55:46 -07001430 /* Do we need to track softdirty? */
1431 if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
1432 return 1;
1433
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001434 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001435 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001436 return 0;
1437
1438 /* Can the mapping track the dirty pages? */
1439 return vma->vm_file && vma->vm_file->f_mapping &&
1440 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1441}
1442
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001443/*
1444 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001445 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001446 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001447static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001448{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001449 /*
1450 * hugetlb has its own accounting separate from the core VM
1451 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1452 */
1453 if (file && is_file_hugepages(file))
1454 return 0;
1455
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001456 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1457}
1458
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001459unsigned long mmap_region(struct file *file, unsigned long addr,
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001460 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001461{
1462 struct mm_struct *mm = current->mm;
1463 struct vm_area_struct *vma, *prev;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001464 int error;
1465 struct rb_node **rb_link, *rb_parent;
1466 unsigned long charged = 0;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001467
Cyril Hrubise8420a82013-04-29 15:08:33 -07001468 /* Check against address space limit. */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001469 if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) {
Cyril Hrubise8420a82013-04-29 15:08:33 -07001470 unsigned long nr_pages;
1471
1472 /*
1473 * MAP_FIXED may remove pages of mappings that intersects with
1474 * requested mapping. Account for the pages it would unmap.
1475 */
Cyril Hrubise8420a82013-04-29 15:08:33 -07001476 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1477
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001478 if (!may_expand_vm(mm, vm_flags,
1479 (len >> PAGE_SHIFT) - nr_pages))
Cyril Hrubise8420a82013-04-29 15:08:33 -07001480 return -ENOMEM;
1481 }
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* Clear old maps */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07001484 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
1485 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (do_munmap(mm, addr, len))
1487 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001490 /*
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001491 * Private writable mapping: check memory availability
1492 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001493 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001494 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001495 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001496 return -ENOMEM;
1497 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
1500 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001501 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001503 vma = vma_merge(mm, prev, addr, addr + len, vm_flags,
1504 NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX);
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001505 if (vma)
1506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 /*
1509 * Determine the object being mapped and call the appropriate
1510 * specific mapper. the address has already been validated, but
1511 * not unmapped, but the maps are removed from the list.
1512 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001513 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (!vma) {
1515 error = -ENOMEM;
1516 goto unacct_error;
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 vma->vm_mm = mm;
1520 vma->vm_start = addr;
1521 vma->vm_end = addr + len;
1522 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001523 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001525 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (vm_flags & VM_DENYWRITE) {
1529 error = deny_write_access(file);
1530 if (error)
1531 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001533 if (vm_flags & VM_SHARED) {
1534 error = mapping_map_writable(file->f_mapping);
1535 if (error)
1536 goto allow_write_and_free_vma;
1537 }
1538
1539 /* ->mmap() can change vma->vm_file, but must guarantee that
1540 * vma_link() below can deny write-access if VM_DENYWRITE is set
1541 * and map writably if VM_SHARED is set. This usually means the
1542 * new file must not have been exposed to user-space, yet.
1543 */
Al Virocb0942b2012-08-27 14:48:26 -04001544 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 error = file->f_op->mmap(file, vma);
1546 if (error)
1547 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001548
1549 /* Can addr have changed??
1550 *
1551 * Answer: Yes, several device drivers can do it in their
1552 * f_op->mmap method. -DaveM
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001553 * Bug: If addr is changed, prev, rb_link, rb_parent should
1554 * be updated for vma_link()
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001555 */
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001556 WARN_ON_ONCE(addr != vma->vm_start);
1557
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001558 addr = vma->vm_start;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001559 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 } else if (vm_flags & VM_SHARED) {
1561 error = shmem_zero_setup(vma);
1562 if (error)
1563 goto free_vma;
1564 }
1565
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001566 vma_link(mm, vma, prev, rb_link, rb_parent);
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001567 /* Once vma denies write, undo our temporary denial count */
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001568 if (file) {
1569 if (vm_flags & VM_SHARED)
1570 mapping_unmap_writable(file->f_mapping);
1571 if (vm_flags & VM_DENYWRITE)
1572 allow_write_access(file);
1573 }
Oleg Nesterove8686772013-09-11 14:20:20 -07001574 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001575out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001577
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001578 vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (vm_flags & VM_LOCKED) {
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001580 if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
1581 vma == get_gate_vma(current->mm)))
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001582 mm->locked_vm += (len >> PAGE_SHIFT);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001583 else
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001584 vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001585 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301586
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001587 if (file)
1588 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301589
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001590 /*
1591 * New (or expanded) vma always get soft dirty status.
1592 * Otherwise user-space soft-dirty page tracker won't
1593 * be able to distinguish situation when vma area unmapped,
1594 * then new mapped in-place (which must be aimed as
1595 * a completely new data area).
1596 */
1597 vma->vm_flags |= VM_SOFTDIRTY;
1598
Peter Feiner64e45502014-10-13 15:55:46 -07001599 vma_set_page_prot(vma);
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return addr;
1602
1603unmap_and_free_vma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 vma->vm_file = NULL;
1605 fput(file);
1606
1607 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001608 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1609 charged = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001610 if (vm_flags & VM_SHARED)
1611 mapping_unmap_writable(file->f_mapping);
1612allow_write_and_free_vma:
1613 if (vm_flags & VM_DENYWRITE)
1614 allow_write_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615free_vma:
1616 kmem_cache_free(vm_area_cachep, vma);
1617unacct_error:
1618 if (charged)
1619 vm_unacct_memory(charged);
1620 return error;
1621}
1622
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001623unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1624{
1625 /*
1626 * We implement the search by looking for an rbtree node that
1627 * immediately follows a suitable gap. That is,
1628 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1629 * - gap_end = vma->vm_start >= info->low_limit + length;
1630 * - gap_end - gap_start >= length
1631 */
1632
1633 struct mm_struct *mm = current->mm;
1634 struct vm_area_struct *vma;
1635 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1636
1637 /* Adjust search length to account for worst case alignment overhead */
1638 length = info->length + info->align_mask;
1639 if (length < info->length)
1640 return -ENOMEM;
1641
1642 /* Adjust search limits by the desired length */
1643 if (info->high_limit < length)
1644 return -ENOMEM;
1645 high_limit = info->high_limit - length;
1646
1647 if (info->low_limit > high_limit)
1648 return -ENOMEM;
1649 low_limit = info->low_limit + length;
1650
1651 /* Check if rbtree root looks promising */
1652 if (RB_EMPTY_ROOT(&mm->mm_rb))
1653 goto check_highest;
1654 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1655 if (vma->rb_subtree_gap < length)
1656 goto check_highest;
1657
1658 while (true) {
1659 /* Visit left subtree if it looks promising */
1660 gap_end = vma->vm_start;
1661 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1662 struct vm_area_struct *left =
1663 rb_entry(vma->vm_rb.rb_left,
1664 struct vm_area_struct, vm_rb);
1665 if (left->rb_subtree_gap >= length) {
1666 vma = left;
1667 continue;
1668 }
1669 }
1670
1671 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1672check_current:
1673 /* Check if current node has a suitable gap */
1674 if (gap_start > high_limit)
1675 return -ENOMEM;
1676 if (gap_end >= low_limit && gap_end - gap_start >= length)
1677 goto found;
1678
1679 /* Visit right subtree if it looks promising */
1680 if (vma->vm_rb.rb_right) {
1681 struct vm_area_struct *right =
1682 rb_entry(vma->vm_rb.rb_right,
1683 struct vm_area_struct, vm_rb);
1684 if (right->rb_subtree_gap >= length) {
1685 vma = right;
1686 continue;
1687 }
1688 }
1689
1690 /* Go back up the rbtree to find next candidate node */
1691 while (true) {
1692 struct rb_node *prev = &vma->vm_rb;
1693 if (!rb_parent(prev))
1694 goto check_highest;
1695 vma = rb_entry(rb_parent(prev),
1696 struct vm_area_struct, vm_rb);
1697 if (prev == vma->vm_rb.rb_left) {
1698 gap_start = vma->vm_prev->vm_end;
1699 gap_end = vma->vm_start;
1700 goto check_current;
1701 }
1702 }
1703 }
1704
1705check_highest:
1706 /* Check highest gap, which does not precede any rbtree node */
1707 gap_start = mm->highest_vm_end;
1708 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1709 if (gap_start > high_limit)
1710 return -ENOMEM;
1711
1712found:
1713 /* We found a suitable gap. Clip it with the original low_limit. */
1714 if (gap_start < info->low_limit)
1715 gap_start = info->low_limit;
1716
1717 /* Adjust gap address to the desired alignment */
1718 gap_start += (info->align_offset - gap_start) & info->align_mask;
1719
1720 VM_BUG_ON(gap_start + info->length > info->high_limit);
1721 VM_BUG_ON(gap_start + info->length > gap_end);
1722 return gap_start;
1723}
1724
1725unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1726{
1727 struct mm_struct *mm = current->mm;
1728 struct vm_area_struct *vma;
1729 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1730
1731 /* Adjust search length to account for worst case alignment overhead */
1732 length = info->length + info->align_mask;
1733 if (length < info->length)
1734 return -ENOMEM;
1735
1736 /*
1737 * Adjust search limits by the desired length.
1738 * See implementation comment at top of unmapped_area().
1739 */
1740 gap_end = info->high_limit;
1741 if (gap_end < length)
1742 return -ENOMEM;
1743 high_limit = gap_end - length;
1744
1745 if (info->low_limit > high_limit)
1746 return -ENOMEM;
1747 low_limit = info->low_limit + length;
1748
1749 /* Check highest gap, which does not precede any rbtree node */
1750 gap_start = mm->highest_vm_end;
1751 if (gap_start <= high_limit)
1752 goto found_highest;
1753
1754 /* Check if rbtree root looks promising */
1755 if (RB_EMPTY_ROOT(&mm->mm_rb))
1756 return -ENOMEM;
1757 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1758 if (vma->rb_subtree_gap < length)
1759 return -ENOMEM;
1760
1761 while (true) {
1762 /* Visit right subtree if it looks promising */
1763 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1764 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1765 struct vm_area_struct *right =
1766 rb_entry(vma->vm_rb.rb_right,
1767 struct vm_area_struct, vm_rb);
1768 if (right->rb_subtree_gap >= length) {
1769 vma = right;
1770 continue;
1771 }
1772 }
1773
1774check_current:
1775 /* Check if current node has a suitable gap */
1776 gap_end = vma->vm_start;
1777 if (gap_end < low_limit)
1778 return -ENOMEM;
1779 if (gap_start <= high_limit && gap_end - gap_start >= length)
1780 goto found;
1781
1782 /* Visit left subtree if it looks promising */
1783 if (vma->vm_rb.rb_left) {
1784 struct vm_area_struct *left =
1785 rb_entry(vma->vm_rb.rb_left,
1786 struct vm_area_struct, vm_rb);
1787 if (left->rb_subtree_gap >= length) {
1788 vma = left;
1789 continue;
1790 }
1791 }
1792
1793 /* Go back up the rbtree to find next candidate node */
1794 while (true) {
1795 struct rb_node *prev = &vma->vm_rb;
1796 if (!rb_parent(prev))
1797 return -ENOMEM;
1798 vma = rb_entry(rb_parent(prev),
1799 struct vm_area_struct, vm_rb);
1800 if (prev == vma->vm_rb.rb_right) {
1801 gap_start = vma->vm_prev ?
1802 vma->vm_prev->vm_end : 0;
1803 goto check_current;
1804 }
1805 }
1806 }
1807
1808found:
1809 /* We found a suitable gap. Clip it with the original high_limit. */
1810 if (gap_end > info->high_limit)
1811 gap_end = info->high_limit;
1812
1813found_highest:
1814 /* Compute highest gap address at the desired alignment */
1815 gap_end -= info->length;
1816 gap_end -= (gap_end - info->align_offset) & info->align_mask;
1817
1818 VM_BUG_ON(gap_end < info->low_limit);
1819 VM_BUG_ON(gap_end < gap_start);
1820 return gap_end;
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/* Get an address range which is currently unmapped.
1824 * For shmat() with addr=0.
1825 *
1826 * Ugly calling convention alert:
1827 * Return value with the low bits set means error value,
1828 * ie
1829 * if (ret & ~PAGE_MASK)
1830 * error = ret;
1831 *
1832 * This function "knows" that -ENOMEM has the bits set.
1833 */
1834#ifndef HAVE_ARCH_UNMAPPED_AREA
1835unsigned long
1836arch_get_unmapped_area(struct file *filp, unsigned long addr,
1837 unsigned long len, unsigned long pgoff, unsigned long flags)
1838{
1839 struct mm_struct *mm = current->mm;
1840 struct vm_area_struct *vma;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001841 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001843 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return -ENOMEM;
1845
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001846 if (flags & MAP_FIXED)
1847 return addr;
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (addr) {
1850 addr = PAGE_ALIGN(addr);
1851 vma = find_vma(mm, addr);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001852 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 (!vma || addr + len <= vma->vm_start))
1854 return addr;
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001857 info.flags = 0;
1858 info.length = len;
Heiko Carstens4e99b022013-11-12 15:07:54 -08001859 info.low_limit = mm->mmap_base;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001860 info.high_limit = TASK_SIZE;
1861 info.align_mask = 0;
1862 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
vishnu.pscc71aba2014-10-09 15:26:29 -07001864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866/*
1867 * This mmap-allocator allocates new areas top-down from below the
1868 * stack's low limit (the base):
1869 */
1870#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1871unsigned long
1872arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1873 const unsigned long len, const unsigned long pgoff,
1874 const unsigned long flags)
1875{
1876 struct vm_area_struct *vma;
1877 struct mm_struct *mm = current->mm;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001878 unsigned long addr = addr0;
1879 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 /* requested length too big for entire address space */
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001882 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return -ENOMEM;
1884
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001885 if (flags & MAP_FIXED)
1886 return addr;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* requesting a specific address */
1889 if (addr) {
1890 addr = PAGE_ALIGN(addr);
1891 vma = find_vma(mm, addr);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001892 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 (!vma || addr + len <= vma->vm_start))
1894 return addr;
1895 }
1896
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001897 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
1898 info.length = len;
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001899 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001900 info.high_limit = mm->mmap_base;
1901 info.align_mask = 0;
1902 addr = vm_unmapped_area(&info);
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /*
1905 * A failed mmap() very likely causes application failure,
1906 * so fall back to the bottom-up function here. This scenario
1907 * can happen with large stack limits and large mmap()
1908 * allocations.
1909 */
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001910 if (offset_in_page(addr)) {
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001911 VM_BUG_ON(addr != -ENOMEM);
1912 info.flags = 0;
1913 info.low_limit = TASK_UNMAPPED_BASE;
1914 info.high_limit = TASK_SIZE;
1915 addr = vm_unmapped_area(&info);
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 return addr;
1919}
1920#endif
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922unsigned long
1923get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1924 unsigned long pgoff, unsigned long flags)
1925{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001926 unsigned long (*get_area)(struct file *, unsigned long,
1927 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Al Viro9206de92009-12-03 15:23:11 -05001929 unsigned long error = arch_mmap_check(addr, len, flags);
1930 if (error)
1931 return error;
1932
1933 /* Careful about overflows.. */
1934 if (len > TASK_SIZE)
1935 return -ENOMEM;
1936
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001937 get_area = current->mm->get_unmapped_area;
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07001938 if (file) {
1939 if (file->f_op->get_unmapped_area)
1940 get_area = file->f_op->get_unmapped_area;
1941 } else if (flags & MAP_SHARED) {
1942 /*
1943 * mmap_region() will call shmem_zero_setup() to create a file,
1944 * so use shmem's get_unmapped_area in case it can be huge.
1945 * do_mmap_pgoff() will clear pgoff, so match alignment.
1946 */
1947 pgoff = 0;
1948 get_area = shmem_get_unmapped_area;
1949 }
1950
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001951 addr = get_area(file, addr, len, pgoff, flags);
1952 if (IS_ERR_VALUE(addr))
1953 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001955 if (addr > TASK_SIZE - len)
1956 return -ENOMEM;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001957 if (offset_in_page(addr))
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001958 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001959
Al Viro9ac4ed42012-05-30 17:13:15 -04001960 error = security_mmap_addr(addr);
1961 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
1964EXPORT_SYMBOL(get_unmapped_area);
1965
1966/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08001967struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001969 struct rb_node *rb_node;
1970 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001972 /* Check the cache first. */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001973 vma = vmacache_find(mm, addr);
1974 if (likely(vma))
1975 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001977 rb_node = mm->mm_rb.rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001979 while (rb_node) {
1980 struct vm_area_struct *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001982 tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001983
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001984 if (tmp->vm_end > addr) {
1985 vma = tmp;
1986 if (tmp->vm_start <= addr)
1987 break;
1988 rb_node = rb_node->rb_left;
1989 } else
1990 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001992
1993 if (vma)
1994 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return vma;
1996}
1997
1998EXPORT_SYMBOL(find_vma);
1999
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002000/*
2001 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003struct vm_area_struct *
2004find_vma_prev(struct mm_struct *mm, unsigned long addr,
2005 struct vm_area_struct **pprev)
2006{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002007 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002009 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05002010 if (vma) {
2011 *pprev = vma->vm_prev;
2012 } else {
2013 struct rb_node *rb_node = mm->mm_rb.rb_node;
2014 *pprev = NULL;
2015 while (rb_node) {
2016 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
2017 rb_node = rb_node->rb_right;
2018 }
2019 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002020 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023/*
2024 * Verify that the stack growth is acceptable and
2025 * update accounting. This is shared with both the
2026 * grow-up and grow-down cases.
2027 */
ZhenwenXu48aae422009-01-06 14:40:21 -08002028static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct mm_struct *mm = vma->vm_mm;
2031 struct rlimit *rlim = current->signal->rlim;
Linus Torvalds690eac52015-01-11 11:33:57 -08002032 unsigned long new_start, actual_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 /* address space limit tests */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002035 if (!may_expand_vm(mm, vma->vm_flags, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return -ENOMEM;
2037
2038 /* Stack limit test */
Linus Torvalds690eac52015-01-11 11:33:57 -08002039 actual_size = size;
2040 if (size && (vma->vm_flags & (VM_GROWSUP | VM_GROWSDOWN)))
2041 actual_size -= PAGE_SIZE;
Jason Low4db0c3c2015-04-15 16:14:08 -07002042 if (actual_size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return -ENOMEM;
2044
2045 /* mlock limit tests */
2046 if (vma->vm_flags & VM_LOCKED) {
2047 unsigned long locked;
2048 unsigned long limit;
2049 locked = mm->locked_vm + grow;
Jason Low4db0c3c2015-04-15 16:14:08 -07002050 limit = READ_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
Jiri Slaby59e99e52010-03-05 13:41:44 -08002051 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (locked > limit && !capable(CAP_IPC_LOCK))
2053 return -ENOMEM;
2054 }
2055
Adam Litke0d59a012007-01-30 14:35:39 -08002056 /* Check to ensure the stack will not grow into a hugetlb-only region */
2057 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
2058 vma->vm_end - size;
2059 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
2060 return -EFAULT;
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
2063 * Overcommit.. This must be the final test, as it will
2064 * update security statistics.
2065 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01002066 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return -ENOMEM;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return 0;
2070}
2071
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002072#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002074 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2075 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002077int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Oleg Nesterov09357812015-11-05 18:48:17 -08002079 struct mm_struct *mm = vma->vm_mm;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002080 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 if (!(vma->vm_flags & VM_GROWSUP))
2083 return -EFAULT;
2084
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002085 /* Guard against wrapping around to address 0. */
2086 if (address < PAGE_ALIGN(address+4))
2087 address = PAGE_ALIGN(address+4);
2088 else
2089 return -ENOMEM;
2090
2091 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (unlikely(anon_vma_prepare(vma)))
2093 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 /*
2096 * vma->vm_start/vm_end cannot change under us because the caller
2097 * is required to hold the mmap_sem in read mode. We need the
2098 * anon_vma lock to serialize against concurrent expand_stacks.
2099 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002100 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /* Somebody else might have raced and expanded it already */
2103 if (address > vma->vm_end) {
2104 unsigned long size, grow;
2105
2106 size = address - vma->vm_start;
2107 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2108
Hugh Dickins42c36f62011-05-09 17:44:42 -07002109 error = -ENOMEM;
2110 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2111 error = acct_stack_growth(vma, size, grow);
2112 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002113 /*
2114 * vma_gap_update() doesn't support concurrent
2115 * updates, but we only hold a shared mmap_sem
2116 * lock here, so we need to protect against
2117 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002118 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002119 * we don't guarantee that all growable vmas
2120 * in a mm share the same root anon vma.
2121 * So, we reuse mm->page_table_lock to guard
2122 * against concurrent vma expansions.
2123 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002124 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002125 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002126 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002127 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002128 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07002129 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002130 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002131 if (vma->vm_next)
2132 vma_gap_update(vma->vm_next);
2133 else
Oleg Nesterov09357812015-11-05 18:48:17 -08002134 mm->highest_vm_end = address;
2135 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002136
Hugh Dickins42c36f62011-05-09 17:44:42 -07002137 perf_event_mmap(vma);
2138 }
Eric B Munson3af9e852010-05-18 15:30:49 +01002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002141 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002142 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002143 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return error;
2145}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002146#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/*
2149 * vma is the first one with address < vma->vm_start. Have to extend vma.
2150 */
Michal Hockod05f3162011-05-24 17:11:44 -07002151int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002152 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Oleg Nesterov09357812015-11-05 18:48:17 -08002154 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int error;
2156
Eric Paris88694772007-11-26 18:47:26 -05002157 address &= PAGE_MASK;
Al Viroe5467852012-05-30 13:30:51 -04002158 error = security_mmap_addr(address);
Eric Paris88694772007-11-26 18:47:26 -05002159 if (error)
2160 return error;
2161
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002162 /* We must make sure the anon_vma is allocated. */
2163 if (unlikely(anon_vma_prepare(vma)))
2164 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 /*
2167 * vma->vm_start/vm_end cannot change under us because the caller
2168 * is required to hold the mmap_sem in read mode. We need the
2169 * anon_vma lock to serialize against concurrent expand_stacks.
2170 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002171 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 /* Somebody else might have raced and expanded it already */
2174 if (address < vma->vm_start) {
2175 unsigned long size, grow;
2176
2177 size = vma->vm_end - address;
2178 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2179
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002180 error = -ENOMEM;
2181 if (grow <= vma->vm_pgoff) {
2182 error = acct_stack_growth(vma, size, grow);
2183 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002184 /*
2185 * vma_gap_update() doesn't support concurrent
2186 * updates, but we only hold a shared mmap_sem
2187 * lock here, so we need to protect against
2188 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002189 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002190 * we don't guarantee that all growable vmas
2191 * in a mm share the same root anon vma.
2192 * So, we reuse mm->page_table_lock to guard
2193 * against concurrent vma expansions.
2194 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002195 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002196 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002197 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002198 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002199 anon_vma_interval_tree_pre_update_vma(vma);
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002200 vma->vm_start = address;
2201 vma->vm_pgoff -= grow;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002202 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002203 vma_gap_update(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002204 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002205
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002206 perf_event_mmap(vma);
2207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002210 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002211 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002212 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return error;
2214}
2215
Linus Torvalds09884962013-02-27 08:36:04 -08002216/*
2217 * Note how expand_stack() refuses to expand the stack all the way to
2218 * abut the next virtual mapping, *unless* that mapping itself is also
2219 * a stack mapping. We want to leave room for a guard page, after all
2220 * (the guard page itself is not added here, that is done by the
2221 * actual page faulting logic)
2222 *
2223 * This matches the behavior of the guard page logic (see mm/memory.c:
2224 * check_stack_guard_page()), which only allows the guard page to be
2225 * removed under these circumstances.
2226 */
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002227#ifdef CONFIG_STACK_GROWSUP
2228int expand_stack(struct vm_area_struct *vma, unsigned long address)
2229{
Linus Torvalds09884962013-02-27 08:36:04 -08002230 struct vm_area_struct *next;
2231
2232 address &= PAGE_MASK;
2233 next = vma->vm_next;
2234 if (next && next->vm_start == address + PAGE_SIZE) {
2235 if (!(next->vm_flags & VM_GROWSUP))
2236 return -ENOMEM;
2237 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002238 return expand_upwards(vma, address);
2239}
2240
2241struct vm_area_struct *
2242find_extend_vma(struct mm_struct *mm, unsigned long addr)
2243{
2244 struct vm_area_struct *vma, *prev;
2245
2246 addr &= PAGE_MASK;
2247 vma = find_vma_prev(mm, addr, &prev);
2248 if (vma && (vma->vm_start <= addr))
2249 return vma;
Denys Vlasenko1c127182008-11-12 01:24:41 +01002250 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002251 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002252 if (prev->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002253 populate_vma_page_range(prev, addr, prev->vm_end, NULL);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002254 return prev;
2255}
2256#else
2257int expand_stack(struct vm_area_struct *vma, unsigned long address)
2258{
Linus Torvalds09884962013-02-27 08:36:04 -08002259 struct vm_area_struct *prev;
2260
2261 address &= PAGE_MASK;
2262 prev = vma->vm_prev;
2263 if (prev && prev->vm_end == address) {
2264 if (!(prev->vm_flags & VM_GROWSDOWN))
2265 return -ENOMEM;
2266 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002267 return expand_downwards(vma, address);
2268}
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270struct vm_area_struct *
vishnu.pscc71aba2014-10-09 15:26:29 -07002271find_extend_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
vishnu.pscc71aba2014-10-09 15:26:29 -07002273 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 unsigned long start;
2275
2276 addr &= PAGE_MASK;
vishnu.pscc71aba2014-10-09 15:26:29 -07002277 vma = find_vma(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (!vma)
2279 return NULL;
2280 if (vma->vm_start <= addr)
2281 return vma;
2282 if (!(vma->vm_flags & VM_GROWSDOWN))
2283 return NULL;
2284 start = vma->vm_start;
2285 if (expand_stack(vma, addr))
2286 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002287 if (vma->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002288 populate_vma_page_range(vma, addr, start, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return vma;
2290}
2291#endif
2292
Jesse Barnese1d6d012014-12-12 16:55:27 -08002293EXPORT_SYMBOL_GPL(find_extend_vma);
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002296 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002298 *
2299 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002301static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002303 unsigned long nr_accounted = 0;
2304
Hugh Dickins365e9c872005-10-29 18:16:18 -07002305 /* Update high watermark before we lower total_vm */
2306 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002308 long nrpages = vma_pages(vma);
2309
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002310 if (vma->vm_flags & VM_ACCOUNT)
2311 nr_accounted += nrpages;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002312 vm_stat_account(mm, vma->vm_flags, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002313 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002314 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002315 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 validate_mm(mm);
2317}
2318
2319/*
2320 * Get rid of page table information in the indicated region.
2321 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002322 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 */
2324static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002325 struct vm_area_struct *vma, struct vm_area_struct *prev,
2326 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
vishnu.pscc71aba2014-10-09 15:26:29 -07002328 struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002329 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07002332 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002333 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002334 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002335 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins6ee86302013-04-29 15:07:44 -07002336 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002337 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
2340/*
2341 * Create a list of vma's touched by the unmap, removing them from the mm's
2342 * vma list as we go..
2343 */
2344static void
2345detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2346 struct vm_area_struct *prev, unsigned long end)
2347{
2348 struct vm_area_struct **insertion_point;
2349 struct vm_area_struct *tail_vma = NULL;
2350
2351 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002352 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 do {
Michel Lespinassed3737182012-12-11 16:01:38 -08002354 vma_rb_erase(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 mm->map_count--;
2356 tail_vma = vma;
2357 vma = vma->vm_next;
2358 } while (vma && vma->vm_start < end);
2359 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002360 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002361 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002362 vma_gap_update(vma);
2363 } else
2364 mm->highest_vm_end = prev ? prev->vm_end : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 tail_vma->vm_next = NULL;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002366
2367 /* Kill the cache */
2368 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
2371/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002372 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2373 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 */
vishnu.pscc71aba2014-10-09 15:26:29 -07002375static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 unsigned long addr, int new_below)
2377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct vm_area_struct *new;
Chen Gange3975892015-09-08 15:03:38 -07002379 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Andi Kleena5516432008-07-23 21:27:41 -07002381 if (is_vm_hugetlb_page(vma) && (addr &
2382 ~(huge_page_mask(hstate_vma(vma)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return -EINVAL;
2384
Christoph Lametere94b1762006-12-06 20:33:17 -08002385 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (!new)
Chen Gange3975892015-09-08 15:03:38 -07002387 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /* most fields are the same, copy all, and then fixup */
2390 *new = *vma;
2391
Rik van Riel5beb4932010-03-05 13:42:07 -08002392 INIT_LIST_HEAD(&new->anon_vma_chain);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (new_below)
2395 new->vm_end = addr;
2396 else {
2397 new->vm_start = addr;
2398 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2399 }
2400
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002401 err = vma_dup_policy(vma, new);
2402 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002403 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Daniel Forrestc4ea95d2014-12-02 15:59:42 -08002405 err = anon_vma_clone(new, vma);
2406 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002407 goto out_free_mpol;
2408
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002409 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 get_file(new->vm_file);
2411
2412 if (new->vm_ops && new->vm_ops->open)
2413 new->vm_ops->open(new);
2414
2415 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002416 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2418 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002419 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Rik van Riel5beb4932010-03-05 13:42:07 -08002421 /* Success. */
2422 if (!err)
2423 return 0;
2424
2425 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002426 if (new->vm_ops && new->vm_ops->close)
2427 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002428 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002429 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002430 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002431 out_free_mpol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002432 mpol_put(vma_policy(new));
Rik van Riel5beb4932010-03-05 13:42:07 -08002433 out_free_vma:
2434 kmem_cache_free(vm_area_cachep, new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002438/*
2439 * Split a vma into two pieces at address 'addr', a new vma is allocated
2440 * either for the first part or the tail.
2441 */
2442int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2443 unsigned long addr, int new_below)
2444{
2445 if (mm->map_count >= sysctl_max_map_count)
2446 return -ENOMEM;
2447
2448 return __split_vma(mm, vma, addr, new_below);
2449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/* Munmap is split into 2 main parts -- this part which finds
2452 * what needs doing, and the areas themselves, which do the
2453 * work. This now handles partial unmappings.
2454 * Jeremy Fitzhardinge <jeremy@goop.org>
2455 */
2456int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2457{
2458 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002459 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002461 if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return -EINVAL;
2463
vishnu.pscc71aba2014-10-09 15:26:29 -07002464 len = PAGE_ALIGN(len);
2465 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return -EINVAL;
2467
2468 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002469 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002470 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002472 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002473 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 /* if it doesn't overlap, we have nothing.. */
2476 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002477 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return 0;
2479
2480 /*
2481 * If we need to split any vma, do it now to save pain later.
2482 *
2483 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2484 * unmapped vm_area_struct will remain in use: so lower split_vma
2485 * places tmp vma above, and higher split_vma places tmp vma below.
2486 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002487 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002488 int error;
2489
2490 /*
2491 * Make sure that map_count on return from munmap() will
2492 * not exceed its limit; but let map_count go just above
2493 * its limit temporarily, to help free resources as expected.
2494 */
2495 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2496 return -ENOMEM;
2497
2498 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (error)
2500 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002501 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
2503
2504 /* Does it split the last one? */
2505 last = find_vma(mm, end);
2506 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002507 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (error)
2509 return error;
2510 }
vishnu.pscc71aba2014-10-09 15:26:29 -07002511 vma = prev ? prev->vm_next : mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002514 * unlock any mlock()ed ranges before detaching vmas
2515 */
2516 if (mm->locked_vm) {
2517 struct vm_area_struct *tmp = vma;
2518 while (tmp && tmp->vm_start < end) {
2519 if (tmp->vm_flags & VM_LOCKED) {
2520 mm->locked_vm -= vma_pages(tmp);
2521 munlock_vma_pages_all(tmp);
2522 }
2523 tmp = tmp->vm_next;
2524 }
2525 }
2526
2527 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 * Remove the vma's, and unmap the actual pages
2529 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002530 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2531 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Dave Hansen1de4fa12014-11-14 07:18:31 -08002533 arch_unmap(mm, vma, start, end);
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002536 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 return 0;
2539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Al Virobfce2812012-04-20 21:57:04 -04002541int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002542{
2543 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002544 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002545
Michal Hockoae798782016-05-23 16:25:33 -07002546 if (down_write_killable(&mm->mmap_sem))
2547 return -EINTR;
2548
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002549 ret = do_munmap(mm, start, len);
2550 up_write(&mm->mmap_sem);
2551 return ret;
2552}
2553EXPORT_SYMBOL(vm_munmap);
2554
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002555SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002557 int ret;
2558 struct mm_struct *mm = current->mm;
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 profile_munmap(addr);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002561 if (down_write_killable(&mm->mmap_sem))
2562 return -EINTR;
2563 ret = do_munmap(mm, addr, len);
2564 up_write(&mm->mmap_sem);
2565 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002568
2569/*
2570 * Emulation of deprecated remap_file_pages() syscall.
2571 */
2572SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
2573 unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
2574{
2575
2576 struct mm_struct *mm = current->mm;
2577 struct vm_area_struct *vma;
2578 unsigned long populate = 0;
2579 unsigned long ret = -EINVAL;
2580 struct file *file;
2581
Joe Perches756a025f02016-03-17 14:19:47 -07002582 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
2583 current->comm, current->pid);
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002584
2585 if (prot)
2586 return ret;
2587 start = start & PAGE_MASK;
2588 size = size & PAGE_MASK;
2589
2590 if (start + size <= start)
2591 return ret;
2592
2593 /* Does pgoff wrap? */
2594 if (pgoff + (size >> PAGE_SHIFT) < pgoff)
2595 return ret;
2596
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002597 if (down_write_killable(&mm->mmap_sem))
2598 return -EINTR;
2599
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002600 vma = find_vma(mm, start);
2601
2602 if (!vma || !(vma->vm_flags & VM_SHARED))
2603 goto out;
2604
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002605 if (start < vma->vm_start)
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002606 goto out;
2607
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002608 if (start + size > vma->vm_end) {
2609 struct vm_area_struct *next;
2610
2611 for (next = vma->vm_next; next; next = next->vm_next) {
2612 /* hole between vmas ? */
2613 if (next->vm_start != next->vm_prev->vm_end)
2614 goto out;
2615
2616 if (next->vm_file != vma->vm_file)
2617 goto out;
2618
2619 if (next->vm_flags != vma->vm_flags)
2620 goto out;
2621
2622 if (start + size <= next->vm_end)
2623 break;
2624 }
2625
2626 if (!next)
2627 goto out;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002628 }
2629
2630 prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
2631 prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
2632 prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
2633
2634 flags &= MAP_NONBLOCK;
2635 flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
2636 if (vma->vm_flags & VM_LOCKED) {
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002637 struct vm_area_struct *tmp;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002638 flags |= MAP_LOCKED;
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002639
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002640 /* drop PG_Mlocked flag for over-mapped range */
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002641 for (tmp = vma; tmp->vm_start >= start + size;
2642 tmp = tmp->vm_next) {
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07002643 /*
2644 * Split pmd and munlock page on the border
2645 * of the range.
2646 */
2647 vma_adjust_trans_huge(tmp, start, start + size, 0);
2648
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002649 munlock_vma_pages_range(tmp,
2650 max(tmp->vm_start, start),
2651 min(tmp->vm_end, start + size));
2652 }
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002653 }
2654
2655 file = get_file(vma->vm_file);
2656 ret = do_mmap_pgoff(vma->vm_file, start, size,
2657 prot, flags, pgoff, &populate);
2658 fput(file);
2659out:
2660 up_write(&mm->mmap_sem);
2661 if (populate)
2662 mm_populate(ret, populate);
2663 if (!IS_ERR_VALUE(ret))
2664 ret = 0;
2665 return ret;
2666}
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668static inline void verify_mm_writelocked(struct mm_struct *mm)
2669{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002670#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2672 WARN_ON(1);
2673 up_read(&mm->mmap_sem);
2674 }
2675#endif
2676}
2677
2678/*
2679 * this is really a simplified "do_mmap". it only handles
2680 * anonymous maps. eventually we may be able to do some
2681 * brk-specific accounting here.
2682 */
Kees Cookba093a62016-08-02 14:04:54 -07002683static int do_brk(unsigned long addr, unsigned long request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
vishnu.pscc71aba2014-10-09 15:26:29 -07002685 struct mm_struct *mm = current->mm;
2686 struct vm_area_struct *vma, *prev;
Kees Cookba093a62016-08-02 14:04:54 -07002687 unsigned long flags, len;
vishnu.pscc71aba2014-10-09 15:26:29 -07002688 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002690 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Kees Cookba093a62016-08-02 14:04:54 -07002692 len = PAGE_ALIGN(request);
2693 if (len < request)
2694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (!len)
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Kirill Korotaev3a459752006-09-07 14:17:04 +04002698 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2699
Al Viro2c6a1012009-12-03 19:40:46 -05002700 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002701 if (offset_in_page(error))
Kirill Korotaev3a459752006-09-07 14:17:04 +04002702 return error;
2703
Davidlohr Bueso363ee172014-01-21 15:49:15 -08002704 error = mlock_future_check(mm, mm->def_flags, len);
2705 if (error)
2706 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 /*
2709 * mm->mmap_sem is required to protect against another thread
2710 * changing the mappings in case we sleep.
2711 */
2712 verify_mm_writelocked(mm);
2713
2714 /*
2715 * Clear old maps. this also does some error checking for us
2716 */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07002717 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
2718 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (do_munmap(mm, addr, len))
2720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722
2723 /* Check against address space limits *after* clearing old maps... */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002724 if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return -ENOMEM;
2726
2727 if (mm->map_count > sysctl_max_map_count)
2728 return -ENOMEM;
2729
Al Viro191c5422012-02-13 03:58:52 +00002730 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 return -ENOMEM;
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002734 vma = vma_merge(mm, prev, addr, addr + len, flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07002735 NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX);
Rik van Rielba470de2008-10-18 20:26:50 -07002736 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 goto out;
2738
2739 /*
2740 * create a vma struct for an anonymous mapping
2741 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002742 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (!vma) {
2744 vm_unacct_memory(len >> PAGE_SHIFT);
2745 return -ENOMEM;
2746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Rik van Riel5beb4932010-03-05 13:42:07 -08002748 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 vma->vm_mm = mm;
2750 vma->vm_start = addr;
2751 vma->vm_end = addr + len;
2752 vma->vm_pgoff = pgoff;
2753 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002754 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 vma_link(mm, vma, prev, rb_link, rb_parent);
2756out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002757 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 mm->total_vm += len >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002759 mm->data_vm += len >> PAGE_SHIFT;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002760 if (flags & VM_LOCKED)
2761 mm->locked_vm += (len >> PAGE_SHIFT);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07002762 vma->vm_flags |= VM_SOFTDIRTY;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002766int vm_brk(unsigned long addr, unsigned long len)
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002767{
2768 struct mm_struct *mm = current->mm;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002769 int ret;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002770 bool populate;
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002771
Michal Hocko2d6c9282016-05-23 16:25:42 -07002772 if (down_write_killable(&mm->mmap_sem))
2773 return -EINTR;
2774
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002775 ret = do_brk(addr, len);
Michel Lespinasse128557f2013-02-22 16:32:40 -08002776 populate = ((mm->def_flags & VM_LOCKED) != 0);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002777 up_write(&mm->mmap_sem);
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002778 if (populate && !ret)
Michel Lespinasse128557f2013-02-22 16:32:40 -08002779 mm_populate(addr, len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002780 return ret;
2781}
2782EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784/* Release all mmaps. */
2785void exit_mmap(struct mm_struct *mm)
2786{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002787 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002788 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 unsigned long nr_accounted = 0;
2790
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002791 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002792 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002793
Rik van Rielba470de2008-10-18 20:26:50 -07002794 if (mm->locked_vm) {
2795 vma = mm->mmap;
2796 while (vma) {
2797 if (vma->vm_flags & VM_LOCKED)
2798 munlock_vma_pages_all(vma);
2799 vma = vma->vm_next;
2800 }
2801 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002802
2803 arch_exit_mmap(mm);
2804
Rik van Rielba470de2008-10-18 20:26:50 -07002805 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002806 if (!vma) /* Can happen if dup_mmap() received an OOM */
2807 return;
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 flush_cache_mm(mm);
Linus Torvalds2b047252013-08-15 11:42:25 -07002811 tlb_gather_mmu(&tlb, mm, 0, -1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08002812 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07002813 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002814 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07002815
Hugh Dickins6ee86302013-04-29 15:07:44 -07002816 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05002817 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07002820 * Walk the list again, actually closing and freeing it,
2821 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002823 while (vma) {
2824 if (vma->vm_flags & VM_ACCOUNT)
2825 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002826 vma = remove_vma(vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002827 }
2828 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831/* Insert vm structure into process list sorted by address
2832 * and into the inode's i_mmap tree. If vm_file is non-NULL
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08002833 * then i_mmap_rwsem is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 */
Hugh Dickins6597d782012-10-08 16:29:07 -07002835int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Hugh Dickins6597d782012-10-08 16:29:07 -07002837 struct vm_area_struct *prev;
2838 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Chen Gangc9d13f52015-09-08 15:04:08 -07002840 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
2841 &prev, &rb_link, &rb_parent))
2842 return -ENOMEM;
2843 if ((vma->vm_flags & VM_ACCOUNT) &&
2844 security_vm_enough_memory_mm(mm, vma_pages(vma)))
2845 return -ENOMEM;
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 /*
2848 * The vm_pgoff of a purely anonymous vma should be irrelevant
2849 * until its first write fault, when page's anon_vma and index
2850 * are set. But now set the vm_pgoff it will almost certainly
2851 * end up with (unless mremap moves it elsewhere before that
2852 * first wfault), so /proc/pid/maps tells a consistent story.
2853 *
2854 * By setting it to reflect the virtual start address of the
2855 * vma, merges and splits can happen in a seamless way, just
2856 * using the existing file pgoff checks and manipulations.
2857 * Similarly in do_mmap_pgoff and in do_brk.
2858 */
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07002859 if (vma_is_anonymous(vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 BUG_ON(vma->anon_vma);
2861 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2862 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 vma_link(mm, vma, prev, rb_link, rb_parent);
2865 return 0;
2866}
2867
2868/*
2869 * Copy the vma structure to a new location in the same mm,
2870 * prior to moving page table entries, to effect an mremap move.
2871 */
2872struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07002873 unsigned long addr, unsigned long len, pgoff_t pgoff,
2874 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
2876 struct vm_area_struct *vma = *vmap;
2877 unsigned long vma_start = vma->vm_start;
2878 struct mm_struct *mm = vma->vm_mm;
2879 struct vm_area_struct *new_vma, *prev;
2880 struct rb_node **rb_link, *rb_parent;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002881 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 /*
2884 * If anonymous vma has not yet been faulted, update new pgoff
2885 * to match new location, to increase its chance of merging.
2886 */
Oleg Nesterovce757992015-09-08 14:58:34 -07002887 if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002889 faulted_in_anon_vma = false;
2890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Hugh Dickins6597d782012-10-08 16:29:07 -07002892 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
2893 return NULL; /* should never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07002895 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
2896 vma->vm_userfaultfd_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (new_vma) {
2898 /*
2899 * Source vma may have been merged into new_vma
2900 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002901 if (unlikely(vma_start >= new_vma->vm_start &&
2902 vma_start < new_vma->vm_end)) {
2903 /*
2904 * The only way we can get a vma_merge with
2905 * self during an mremap is if the vma hasn't
2906 * been faulted in yet and we were allowed to
2907 * reset the dst vma->vm_pgoff to the
2908 * destination address of the mremap to allow
2909 * the merge to happen. mremap must change the
2910 * vm_pgoff linearity between src and dst vmas
2911 * (in turn preventing a vma_merge) to be
2912 * safe. It is only safe to keep the vm_pgoff
2913 * linear if there are no pages mapped yet.
2914 */
Sasha Levin81d1b092014-10-09 15:28:10 -07002915 VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002916 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07002917 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07002918 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08002920 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Chen Gange3975892015-09-08 15:03:38 -07002921 if (!new_vma)
2922 goto out;
2923 *new_vma = *vma;
2924 new_vma->vm_start = addr;
2925 new_vma->vm_end = addr + len;
2926 new_vma->vm_pgoff = pgoff;
2927 if (vma_dup_policy(vma, new_vma))
2928 goto out_free_vma;
2929 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2930 if (anon_vma_clone(new_vma, vma))
2931 goto out_free_mempol;
2932 if (new_vma->vm_file)
2933 get_file(new_vma->vm_file);
2934 if (new_vma->vm_ops && new_vma->vm_ops->open)
2935 new_vma->vm_ops->open(new_vma);
2936 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2937 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
2939 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002940
Chen Gange3975892015-09-08 15:03:38 -07002941out_free_mempol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002942 mpol_put(vma_policy(new_vma));
Chen Gange3975892015-09-08 15:03:38 -07002943out_free_vma:
Rik van Riel5beb4932010-03-05 13:42:07 -08002944 kmem_cache_free(vm_area_cachep, new_vma);
Chen Gange3975892015-09-08 15:03:38 -07002945out:
Rik van Riel5beb4932010-03-05 13:42:07 -08002946 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002948
2949/*
2950 * Return true if the calling process may expand its vm space by the passed
2951 * number of pages
2952 */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002953bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
akpm@osdl.org119f6572005-05-01 08:58:35 -07002954{
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002955 if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
2956 return false;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002957
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002958 if (is_data_mapping(flags) &&
2959 mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07002960 /* Workaround for Valgrind */
2961 if (rlimit(RLIMIT_DATA) == 0 &&
2962 mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
2963 return true;
2964 if (!ignore_rlimit_data) {
2965 pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits or use boot option ignore_rlimit_data.\n",
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002966 current->comm, current->pid,
2967 (mm->data_vm + npages) << PAGE_SHIFT,
2968 rlimit(RLIMIT_DATA));
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002969 return false;
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07002970 }
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002971 }
akpm@osdl.org119f6572005-05-01 08:58:35 -07002972
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002973 return true;
2974}
2975
2976void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
2977{
2978 mm->total_vm += npages;
2979
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002980 if (is_exec_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002981 mm->exec_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002982 else if (is_stack_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002983 mm->stack_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002984 else if (is_data_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002985 mm->data_vm += npages;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002986}
Roland McGrathfa5dc222007-02-08 14:20:41 -08002987
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002988static int special_mapping_fault(struct vm_area_struct *vma,
2989 struct vm_fault *vmf);
2990
2991/*
2992 * Having a close hook prevents vma merging regardless of flags.
2993 */
2994static void special_mapping_close(struct vm_area_struct *vma)
2995{
2996}
2997
2998static const char *special_mapping_name(struct vm_area_struct *vma)
2999{
3000 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
3001}
3002
Dmitry Safonovb059a452016-06-28 14:35:38 +03003003static int special_mapping_mremap(struct vm_area_struct *new_vma)
3004{
3005 struct vm_special_mapping *sm = new_vma->vm_private_data;
3006
3007 if (sm->mremap)
3008 return sm->mremap(sm, new_vma);
3009 return 0;
3010}
3011
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003012static const struct vm_operations_struct special_mapping_vmops = {
3013 .close = special_mapping_close,
3014 .fault = special_mapping_fault,
Dmitry Safonovb059a452016-06-28 14:35:38 +03003015 .mremap = special_mapping_mremap,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003016 .name = special_mapping_name,
3017};
3018
3019static const struct vm_operations_struct legacy_special_mapping_vmops = {
3020 .close = special_mapping_close,
3021 .fault = special_mapping_fault,
3022};
Roland McGrathfa5dc222007-02-08 14:20:41 -08003023
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003024static int special_mapping_fault(struct vm_area_struct *vma,
3025 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003026{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003027 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003028 struct page **pages;
3029
Andy Lutomirskif872f542015-12-29 20:12:19 -08003030 if (vma->vm_ops == &legacy_special_mapping_vmops) {
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003031 pages = vma->vm_private_data;
Andy Lutomirskif872f542015-12-29 20:12:19 -08003032 } else {
3033 struct vm_special_mapping *sm = vma->vm_private_data;
3034
3035 if (sm->fault)
3036 return sm->fault(sm, vma, vmf);
3037
3038 pages = sm->pages;
3039 }
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003040
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003041 for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003042 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003043
3044 if (*pages) {
3045 struct page *page = *pages;
3046 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003047 vmf->page = page;
3048 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003049 }
3050
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003051 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003052}
3053
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003054static struct vm_area_struct *__install_special_mapping(
3055 struct mm_struct *mm,
3056 unsigned long addr, unsigned long len,
Chen Gang27f28b92015-11-05 18:48:41 -08003057 unsigned long vm_flags, void *priv,
3058 const struct vm_operations_struct *ops)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003059{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003060 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003061 struct vm_area_struct *vma;
3062
3063 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
3064 if (unlikely(vma == NULL))
Stefani Seibold3935ed62014-03-17 23:22:02 +01003065 return ERR_PTR(-ENOMEM);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003066
Rik van Riel5beb4932010-03-05 13:42:07 -08003067 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003068 vma->vm_mm = mm;
3069 vma->vm_start = addr;
3070 vma->vm_end = addr + len;
3071
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003072 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
Coly Li3ed75eb2007-10-18 23:39:15 -07003073 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003074
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003075 vma->vm_ops = ops;
3076 vma->vm_private_data = priv;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003077
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003078 ret = insert_vm_struct(mm, vma);
3079 if (ret)
3080 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003081
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003082 vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003083
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02003085
Stefani Seibold3935ed62014-03-17 23:22:02 +01003086 return vma;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003087
3088out:
3089 kmem_cache_free(vm_area_cachep, vma);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003090 return ERR_PTR(ret);
3091}
3092
Dmitry Safonov2eefd872016-09-05 16:33:05 +03003093bool vma_is_special_mapping(const struct vm_area_struct *vma,
3094 const struct vm_special_mapping *sm)
3095{
3096 return vma->vm_private_data == sm &&
3097 (vma->vm_ops == &special_mapping_vmops ||
3098 vma->vm_ops == &legacy_special_mapping_vmops);
3099}
3100
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003101/*
3102 * Called with mm->mmap_sem held for writing.
3103 * Insert a new vma covering the given region, with the given flags.
3104 * Its pages are supplied by the given array of struct page *.
3105 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3106 * The region past the last page supplied will always produce SIGBUS.
3107 * The array pointer and the pages it points to are assumed to stay alive
3108 * for as long as this mapping might exist.
3109 */
3110struct vm_area_struct *_install_special_mapping(
3111 struct mm_struct *mm,
3112 unsigned long addr, unsigned long len,
3113 unsigned long vm_flags, const struct vm_special_mapping *spec)
3114{
Chen Gang27f28b92015-11-05 18:48:41 -08003115 return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
3116 &special_mapping_vmops);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003117}
3118
Stefani Seibold3935ed62014-03-17 23:22:02 +01003119int install_special_mapping(struct mm_struct *mm,
3120 unsigned long addr, unsigned long len,
3121 unsigned long vm_flags, struct page **pages)
3122{
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003123 struct vm_area_struct *vma = __install_special_mapping(
Chen Gang27f28b92015-11-05 18:48:41 -08003124 mm, addr, len, vm_flags, (void *)pages,
3125 &legacy_special_mapping_vmops);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003126
Duan Jiong14bd5b42014-06-04 16:07:05 -07003127 return PTR_ERR_OR_ZERO(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003128}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003129
3130static DEFINE_MUTEX(mm_all_locks_mutex);
3131
Peter Zijlstra454ed842008-08-11 09:30:25 +02003132static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003133{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003134 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003135 /*
3136 * The LSB of head.next can't change from under us
3137 * because we hold the mm_all_locks_mutex.
3138 */
Jiri Kosina572043c2013-01-11 14:31:59 -08003139 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003140 /*
3141 * We can safely modify head.next after taking the
Ingo Molnar5a505082012-12-02 19:56:46 +00003142 * anon_vma->root->rwsem. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003143 * the same anon_vma we won't take it again.
3144 *
3145 * No need of atomic instructions here, head.next
3146 * can't change from under us thanks to the
Ingo Molnar5a505082012-12-02 19:56:46 +00003147 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003148 */
3149 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003150 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003151 BUG();
3152 }
3153}
3154
Peter Zijlstra454ed842008-08-11 09:30:25 +02003155static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003156{
3157 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3158 /*
3159 * AS_MM_ALL_LOCKS can't change from under us because
3160 * we hold the mm_all_locks_mutex.
3161 *
3162 * Operations on ->flags have to be atomic because
3163 * even if AS_MM_ALL_LOCKS is stable thanks to the
3164 * mm_all_locks_mutex, there may be other cpus
3165 * changing other bitflags in parallel to us.
3166 */
3167 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
3168 BUG();
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003169 down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003170 }
3171}
3172
3173/*
3174 * This operation locks against the VM for all pte/vma/mm related
3175 * operations that could ever happen on a certain mm. This includes
3176 * vmtruncate, try_to_unmap, and all page faults.
3177 *
3178 * The caller must take the mmap_sem in write mode before calling
3179 * mm_take_all_locks(). The caller isn't allowed to release the
3180 * mmap_sem until mm_drop_all_locks() returns.
3181 *
3182 * mmap_sem in write mode is required in order to block all operations
3183 * that could modify pagetables and free pages without need of
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -08003184 * altering the vma layout. It's also needed in write mode to avoid new
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003185 * anon_vmas to be associated with existing vmas.
3186 *
3187 * A single task can't take more than one mm_take_all_locks() in a row
3188 * or it would deadlock.
3189 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07003190 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003191 * mapping->flags avoid to take the same lock twice, if more than one
3192 * vma in this mm is backed by the same anon_vma or address_space.
3193 *
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003194 * We take locks in following order, accordingly to comment at beginning
3195 * of mm/rmap.c:
3196 * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
3197 * hugetlb mapping);
3198 * - all i_mmap_rwsem locks;
3199 * - all anon_vma->rwseml
3200 *
3201 * We can take all locks within these types randomly because the VM code
3202 * doesn't nest them and we protected from parallel mm_take_all_locks() by
3203 * mm_all_locks_mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003204 *
3205 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3206 * that may have to take thousand of locks.
3207 *
3208 * mm_take_all_locks() can fail if it's interrupted by signals.
3209 */
3210int mm_take_all_locks(struct mm_struct *mm)
3211{
3212 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003213 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003214
3215 BUG_ON(down_read_trylock(&mm->mmap_sem));
3216
3217 mutex_lock(&mm_all_locks_mutex);
3218
3219 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3220 if (signal_pending(current))
3221 goto out_unlock;
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003222 if (vma->vm_file && vma->vm_file->f_mapping &&
3223 is_vm_hugetlb_page(vma))
3224 vm_lock_mapping(mm, vma->vm_file->f_mapping);
3225 }
3226
3227 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3228 if (signal_pending(current))
3229 goto out_unlock;
3230 if (vma->vm_file && vma->vm_file->f_mapping &&
3231 !is_vm_hugetlb_page(vma))
Peter Zijlstra454ed842008-08-11 09:30:25 +02003232 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003233 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003234
3235 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3236 if (signal_pending(current))
3237 goto out_unlock;
3238 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003239 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3240 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003241 }
3242
Kautuk Consul584cff52011-10-31 17:08:59 -07003243 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003244
3245out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07003246 mm_drop_all_locks(mm);
3247 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003248}
3249
3250static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3251{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003252 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003253 /*
3254 * The LSB of head.next can't change to 0 from under
3255 * us because we hold the mm_all_locks_mutex.
3256 *
3257 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07003258 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003259 * never see our bitflag.
3260 *
3261 * No need of atomic instructions here, head.next
3262 * can't change from under us until we release the
Ingo Molnar5a505082012-12-02 19:56:46 +00003263 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003264 */
3265 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003266 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003267 BUG();
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08003268 anon_vma_unlock_write(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003269 }
3270}
3271
3272static void vm_unlock_mapping(struct address_space *mapping)
3273{
3274 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3275 /*
3276 * AS_MM_ALL_LOCKS can't change to 0 from under us
3277 * because we hold the mm_all_locks_mutex.
3278 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003279 i_mmap_unlock_write(mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003280 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3281 &mapping->flags))
3282 BUG();
3283 }
3284}
3285
3286/*
3287 * The mmap_sem cannot be released by the caller until
3288 * mm_drop_all_locks() returns.
3289 */
3290void mm_drop_all_locks(struct mm_struct *mm)
3291{
3292 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003293 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003294
3295 BUG_ON(down_read_trylock(&mm->mmap_sem));
3296 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3297
3298 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3299 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003300 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3301 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003302 if (vma->vm_file && vma->vm_file->f_mapping)
3303 vm_unlock_mapping(vma->vm_file->f_mapping);
3304 }
3305
3306 mutex_unlock(&mm_all_locks_mutex);
3307}
David Howells8feae132009-01-08 12:04:47 +00003308
3309/*
3310 * initialise the VMA slab
3311 */
3312void __init mmap_init(void)
3313{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003314 int ret;
3315
Tejun Heo908c7f12014-09-08 09:51:29 +09003316 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003317 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00003318}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003319
3320/*
3321 * Initialise sysctl_user_reserve_kbytes.
3322 *
3323 * This is intended to prevent a user from starting a single memory hogging
3324 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3325 * mode.
3326 *
3327 * The default value is min(3% of free memory, 128MB)
3328 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3329 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003330static int init_user_reserve(void)
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003331{
3332 unsigned long free_kbytes;
3333
3334 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3335
3336 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3337 return 0;
3338}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003339subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003340
3341/*
3342 * Initialise sysctl_admin_reserve_kbytes.
3343 *
3344 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3345 * to log in and kill a memory hogging process.
3346 *
3347 * Systems with more than 256MB will reserve 8MB, enough to recover
3348 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3349 * only reserve 3% of free pages by default.
3350 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003351static int init_admin_reserve(void)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003352{
3353 unsigned long free_kbytes;
3354
3355 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3356
3357 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3358 return 0;
3359}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003360subsys_initcall(init_admin_reserve);
Andrew Shewmaker16408792013-04-29 15:08:12 -07003361
3362/*
3363 * Reinititalise user and admin reserves if memory is added or removed.
3364 *
3365 * The default user reserve max is 128MB, and the default max for the
3366 * admin reserve is 8MB. These are usually, but not always, enough to
3367 * enable recovery from a memory hogging process using login/sshd, a shell,
3368 * and tools like top. It may make sense to increase or even disable the
3369 * reserve depending on the existence of swap or variations in the recovery
3370 * tools. So, the admin may have changed them.
3371 *
3372 * If memory is added and the reserves have been eliminated or increased above
3373 * the default max, then we'll trust the admin.
3374 *
3375 * If memory is removed and there isn't enough free memory, then we
3376 * need to reset the reserves.
3377 *
3378 * Otherwise keep the reserve set by the admin.
3379 */
3380static int reserve_mem_notifier(struct notifier_block *nb,
3381 unsigned long action, void *data)
3382{
3383 unsigned long tmp, free_kbytes;
3384
3385 switch (action) {
3386 case MEM_ONLINE:
3387 /* Default max is 128MB. Leave alone if modified by operator. */
3388 tmp = sysctl_user_reserve_kbytes;
3389 if (0 < tmp && tmp < (1UL << 17))
3390 init_user_reserve();
3391
3392 /* Default max is 8MB. Leave alone if modified by operator. */
3393 tmp = sysctl_admin_reserve_kbytes;
3394 if (0 < tmp && tmp < (1UL << 13))
3395 init_admin_reserve();
3396
3397 break;
3398 case MEM_OFFLINE:
3399 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3400
3401 if (sysctl_user_reserve_kbytes > free_kbytes) {
3402 init_user_reserve();
3403 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3404 sysctl_user_reserve_kbytes);
3405 }
3406
3407 if (sysctl_admin_reserve_kbytes > free_kbytes) {
3408 init_admin_reserve();
3409 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3410 sysctl_admin_reserve_kbytes);
3411 }
3412 break;
3413 default:
3414 break;
3415 }
3416 return NOTIFY_OK;
3417}
3418
3419static struct notifier_block reserve_mem_nb = {
3420 .notifier_call = reserve_mem_notifier,
3421};
3422
3423static int __meminit init_reserve_notifier(void)
3424{
3425 if (register_hotmemory_notifier(&reserve_mem_nb))
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07003426 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
Andrew Shewmaker16408792013-04-29 15:08:12 -07003427
3428 return 0;
3429}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003430subsys_initcall(init_reserve_notifier);