blob: cbb3c4ff8718f2d7896fce37f363eb3cc3e4d38b [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
Cyril Hrubis3e55f212013-04-29 15:08:33 -07009#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070011#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/shm.h>
14#include <linux/mman.h>
15#include <linux/pagemap.h>
16#include <linux/swap.h>
17#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/file.h>
21#include <linux/fs.h>
22#include <linux/personality.h>
23#include <linux/security.h>
24#include <linux/hugetlb.h>
25#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mount.h>
28#include <linux/mempolicy.h>
29#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070030#include <linux/mmu_notifier.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040032#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080033#include <linux/khugepaged.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/uaccess.h>
36#include <asm/cacheflush.h>
37#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020038#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jan Beulich42b77722008-07-23 21:27:10 -070040#include "internal.h"
41
Kirill Korotaev3a459752006-09-07 14:17:04 +040042#ifndef arch_mmap_check
43#define arch_mmap_check(addr, len, flags) (0)
44#endif
45
Martin Schwidefsky08e7d9b2008-02-04 22:29:16 -080046#ifndef arch_rebalance_pgtables
47#define arch_rebalance_pgtables(addr, len) (addr)
48#endif
49
dcashmanaa5b5c52015-12-29 14:24:39 -080050#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
51const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
52const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
53int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
54#endif
55#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
56const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
57const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
58int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
59#endif
60
61
Hugh Dickinse0da3822005-04-19 13:29:15 -070062static void unmap_region(struct mm_struct *mm,
63 struct vm_area_struct *vma, struct vm_area_struct *prev,
64 unsigned long start, unsigned long end);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * WARNING: the debugging will use recursive algorithms so never enable this
68 * unless you know what you are doing.
69 */
70#undef DEBUG_MM_RB
71
72/* description of effects of mapping type and prot in current implementation.
73 * this is due to the limited x86 page protection hardware. The expected
74 * behavior is in parens:
75 *
76 * map_type prot
77 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
78 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
79 * w: (no) no w: (no) no w: (yes) yes w: (no) no
80 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
81 *
82 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
83 * w: (no) no w: (no) no w: (copy) copy w: (no) no
84 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
85 *
86 */
87pgprot_t protection_map[16] = {
88 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
89 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
90};
91
Hugh Dickins804af2c2006-07-26 21:39:49 +010092pgprot_t vm_get_page_prot(unsigned long vm_flags)
93{
Dave Kleikampb845f312008-07-08 00:28:51 +100094 return __pgprot(pgprot_val(protection_map[vm_flags &
95 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
96 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +010097}
98EXPORT_SYMBOL(vm_get_page_prot);
99
Shaohua Li34679d72011-05-24 17:11:18 -0700100int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */
101int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */
Christoph Lameterc3d8c142005-09-06 15:16:33 -0700102int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
Shaohua Li34679d72011-05-24 17:11:18 -0700103/*
104 * Make sure vm_committed_as in one cacheline and not cacheline shared with
105 * other variables. It can be updated by several CPUs frequently.
106 */
107struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
110 * Check that a process has enough memory to allocate a new virtual
111 * mapping. 0 means there is enough memory for the allocation to
112 * succeed and -ENOMEM implies there is not.
113 *
114 * We currently support three overcommit policies, which are set via the
115 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
116 *
117 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
118 * Additional code 2002 Jul 20 by Robert Love.
119 *
120 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
121 *
122 * Note this is a helper function intended to be used by LSMs which
123 * wish to use this logic.
124 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700125int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 unsigned long free, allowed;
128
129 vm_acct_memory(pages);
130
131 /*
132 * Sometimes we want to use more memory than we have
133 */
134 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
135 return 0;
136
137 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -0700138 free = global_page_state(NR_FREE_PAGES);
139 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Dmitry Finkc15bef32011-07-25 17:12:19 -0700141 /*
142 * shmem pages shouldn't be counted as free in this
143 * case, they can't be purged, only swapped out, and
144 * that won't affect the overall amount of available
145 * memory in the system.
146 */
147 free -= global_page_state(NR_SHMEM);
148
Shaohua Li1abbca72013-02-22 16:34:38 -0800149 free += get_nr_swap_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /*
152 * Any slabs which are created with the
153 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
154 * which are reclaimable, under pressure. The dentry
155 * cache and most inode caches should fall into this
156 */
Christoph Lameter972d1a72006-09-25 23:31:51 -0700157 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -0700160 * Leave reserved pages. The pages are not for anonymous pages.
161 */
162 if (free <= totalreserve_pages)
163 goto error;
164 else
165 free -= totalreserve_pages;
166
167 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * Leave the last 3% for root
169 */
170 if (!cap_sys_admin)
171 free -= free / 32;
172
173 if (free > pages)
174 return 0;
175
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700176 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178
179 allowed = (totalram_pages - hugetlb_total_pages())
180 * sysctl_overcommit_ratio / 100;
181 /*
182 * Leave the last 3% for root
183 */
184 if (!cap_sys_admin)
185 allowed -= allowed / 32;
186 allowed += total_swap_pages;
187
188 /* Don't let a single process grow too big:
189 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -0700190 if (mm)
191 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700193 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 0;
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700195error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 vm_unacct_memory(pages);
197
198 return -ENOMEM;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700202 * Requires inode->i_mapping->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204static void __remove_shared_vm_struct(struct vm_area_struct *vma,
205 struct file *file, struct address_space *mapping)
206{
207 if (vma->vm_flags & VM_DENYWRITE)
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800208 atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (vma->vm_flags & VM_SHARED)
210 mapping->i_mmap_writable--;
211
212 flush_dcache_mmap_lock(mapping);
213 if (unlikely(vma->vm_flags & VM_NONLINEAR))
214 list_del_init(&vma->shared.vm_set.list);
215 else
216 vma_prio_tree_remove(vma, &mapping->i_mmap);
217 flush_dcache_mmap_unlock(mapping);
218}
219
220/*
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700221 * Unlink a file-based vm structure from its prio_tree, to hide
222 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700224void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct file *file = vma->vm_file;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (file) {
229 struct address_space *mapping = file->f_mapping;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700230 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 __remove_shared_vm_struct(vma, file, mapping);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700232 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700234}
235
236/*
237 * Close a vm structure and free it, returning the next.
238 */
239static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
240{
241 struct vm_area_struct *next = vma->vm_next;
242
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700243 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (vma->vm_ops && vma->vm_ops->close)
245 vma->vm_ops->close(vma);
Matt Helsley925d1c42008-04-29 01:01:36 -0700246 if (vma->vm_file) {
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700247 fput(vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700248 if (vma->vm_flags & VM_EXECUTABLE)
249 removed_exe_file_vma(vma->vm_mm);
250 }
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700251 mpol_put(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700253 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700256static unsigned long do_brk(unsigned long addr, unsigned long len);
257
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100258SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 unsigned long rlim, retval;
261 unsigned long newbrk, oldbrk;
262 struct mm_struct *mm = current->mm;
Hugh Dickinse8a93762017-06-19 04:03:24 -0700263 struct vm_area_struct *next;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700264 unsigned long min_brk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 down_write(&mm->mmap_sem);
267
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700268#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800269 /*
270 * CONFIG_COMPAT_BRK can still be overridden by setting
271 * randomize_va_space to 2, which will still cause mm->start_brk
272 * to be arbitrarily shifted
273 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700274 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800275 min_brk = mm->start_brk;
276 else
277 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700278#else
279 min_brk = mm->start_brk;
280#endif
281 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700283
284 /*
285 * Check against rlimit here. If this check is done later after the test
286 * of oldbrk with newbrk then it can escape the test and let the data
287 * segment grow beyond its set limit the in case where the limit is
288 * not page aligned -Ram Gupta
289 */
Jiri Slaby59e99e52010-03-05 13:41:44 -0800290 rlim = rlimit(RLIMIT_DATA);
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100291 if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
292 (mm->end_data - mm->start_data) > rlim)
Ram Gupta1e624192006-04-10 22:52:57 -0700293 goto out;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 newbrk = PAGE_ALIGN(brk);
296 oldbrk = PAGE_ALIGN(mm->brk);
297 if (oldbrk == newbrk)
298 goto set_brk;
299
300 /* Always allow shrinking brk. */
301 if (brk <= mm->brk) {
302 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
303 goto set_brk;
304 goto out;
305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* Check against existing mmap mappings. */
Hugh Dickinse8a93762017-06-19 04:03:24 -0700308 next = find_vma(mm, oldbrk);
309 if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto out;
311
312 /* Ok, looks good - let it rip. */
313 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
314 goto out;
315set_brk:
316 mm->brk = brk;
317out:
318 retval = mm->brk;
319 up_write(&mm->mmap_sem);
320 return retval;
321}
322
323#ifdef DEBUG_MM_RB
324static int browse_rb(struct rb_root *root)
325{
326 int i = 0, j;
327 struct rb_node *nd, *pn = NULL;
328 unsigned long prev = 0, pend = 0;
329
330 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
331 struct vm_area_struct *vma;
332 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
333 if (vma->vm_start < prev)
334 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
335 if (vma->vm_start < pend)
336 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
337 if (vma->vm_start > vma->vm_end)
338 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
339 i++;
340 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800341 prev = vma->vm_start;
342 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 j = 0;
345 for (nd = pn; nd; nd = rb_prev(nd)) {
346 j++;
347 }
348 if (i != j)
349 printk("backwards %d, forwards %d\n", j, i), i = 0;
350 return i;
351}
352
353void validate_mm(struct mm_struct *mm)
354{
355 int bug = 0;
356 int i = 0;
357 struct vm_area_struct *tmp = mm->mmap;
358 while (tmp) {
359 tmp = tmp->vm_next;
360 i++;
361 }
362 if (i != mm->map_count)
363 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
364 i = browse_rb(&mm->mm_rb);
365 if (i != mm->map_count)
366 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200367 BUG_ON(bug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369#else
370#define validate_mm(mm) do { } while (0)
371#endif
372
373static struct vm_area_struct *
374find_vma_prepare(struct mm_struct *mm, unsigned long addr,
375 struct vm_area_struct **pprev, struct rb_node ***rb_link,
376 struct rb_node ** rb_parent)
377{
378 struct vm_area_struct * vma;
379 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
380
381 __rb_link = &mm->mm_rb.rb_node;
382 rb_prev = __rb_parent = NULL;
383 vma = NULL;
384
385 while (*__rb_link) {
386 struct vm_area_struct *vma_tmp;
387
388 __rb_parent = *__rb_link;
389 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
390
391 if (vma_tmp->vm_end > addr) {
392 vma = vma_tmp;
393 if (vma_tmp->vm_start <= addr)
Benny Halevydfe195f2008-08-05 13:01:41 -0700394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 __rb_link = &__rb_parent->rb_left;
396 } else {
397 rb_prev = __rb_parent;
398 __rb_link = &__rb_parent->rb_right;
399 }
400 }
401
402 *pprev = NULL;
403 if (rb_prev)
404 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
405 *rb_link = __rb_link;
406 *rb_parent = __rb_parent;
407 return vma;
408}
409
Cyril Hrubis3e55f212013-04-29 15:08:33 -0700410static unsigned long count_vma_pages_range(struct mm_struct *mm,
411 unsigned long addr, unsigned long end)
412{
413 unsigned long nr_pages = 0;
414 struct vm_area_struct *vma;
415
416 /* Find first overlaping mapping */
417 vma = find_vma_intersection(mm, addr, end);
418 if (!vma)
419 return 0;
420
421 nr_pages = (min(end, vma->vm_end) -
422 max(addr, vma->vm_start)) >> PAGE_SHIFT;
423
424 /* Iterate over the rest of the overlaps */
425 for (vma = vma->vm_next; vma; vma = vma->vm_next) {
426 unsigned long overlap_len;
427
428 if (vma->vm_start > end)
429 break;
430
431 overlap_len = min(end, vma->vm_end) - vma->vm_start;
432 nr_pages += overlap_len >> PAGE_SHIFT;
433 }
434
435 return nr_pages;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
439 struct rb_node **rb_link, struct rb_node *rb_parent)
440{
441 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
442 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
443}
444
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700445static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
ZhenwenXu48aae422009-01-06 14:40:21 -0800447 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 file = vma->vm_file;
450 if (file) {
451 struct address_space *mapping = file->f_mapping;
452
453 if (vma->vm_flags & VM_DENYWRITE)
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800454 atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (vma->vm_flags & VM_SHARED)
456 mapping->i_mmap_writable++;
457
458 flush_dcache_mmap_lock(mapping);
459 if (unlikely(vma->vm_flags & VM_NONLINEAR))
460 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
461 else
462 vma_prio_tree_insert(vma, &mapping->i_mmap);
463 flush_dcache_mmap_unlock(mapping);
464 }
465}
466
467static void
468__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
469 struct vm_area_struct *prev, struct rb_node **rb_link,
470 struct rb_node *rb_parent)
471{
472 __vma_link_list(mm, vma, prev, rb_parent);
473 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
477 struct vm_area_struct *prev, struct rb_node **rb_link,
478 struct rb_node *rb_parent)
479{
480 struct address_space *mapping = NULL;
481
482 if (vma->vm_file)
483 mapping = vma->vm_file->f_mapping;
484
Peter Zijlstra97a89412011-05-24 17:12:04 -0700485 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700486 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 __vma_link(mm, vma, prev, rb_link, rb_parent);
489 __vma_link_file(vma);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700492 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 mm->map_count++;
495 validate_mm(mm);
496}
497
498/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700499 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
500 * mm's list and rbtree. It has already been inserted into the prio_tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800502static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
ZhenwenXu48aae422009-01-06 14:40:21 -0800504 struct vm_area_struct *__vma, *prev;
505 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200508 BUG_ON(__vma && __vma->vm_start < vma->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 __vma_link(mm, vma, prev, rb_link, rb_parent);
510 mm->map_count++;
511}
512
513static inline void
514__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
515 struct vm_area_struct *prev)
516{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700517 struct vm_area_struct *next = vma->vm_next;
518
519 prev->vm_next = next;
520 if (next)
521 next->vm_prev = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rb_erase(&vma->vm_rb, &mm->mm_rb);
523 if (mm->mmap_cache == vma)
524 mm->mmap_cache = prev;
525}
526
527/*
528 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
529 * is already present in an i_mmap tree without adjusting the tree.
530 * The following helper function should be used when such adjustments
531 * are necessary. The "insert" vma (if any) is to be inserted
532 * before we drop the necessary locks.
533 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800534int vma_adjust(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
536{
537 struct mm_struct *mm = vma->vm_mm;
538 struct vm_area_struct *next = vma->vm_next;
539 struct vm_area_struct *importer = NULL;
540 struct address_space *mapping = NULL;
541 struct prio_tree_root *root = NULL;
Rik van Riel012f1802010-08-09 17:18:40 -0700542 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 long adjust_next = 0;
545 int remove_next = 0;
546
547 if (next && !insert) {
Linus Torvalds287d97a2010-04-10 15:22:30 -0700548 struct vm_area_struct *exporter = NULL;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (end >= next->vm_end) {
551 /*
552 * vma expands, overlapping all the next, and
553 * perhaps the one after too (mprotect case 6).
554 */
555again: remove_next = 1 + (end > next->vm_end);
556 end = next->vm_end;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700557 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 importer = vma;
559 } else if (end > next->vm_start) {
560 /*
561 * vma expands, overlapping part of the next:
562 * mprotect case 5 shifting the boundary up.
563 */
564 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700565 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 importer = vma;
567 } else if (end < vma->vm_end) {
568 /*
569 * vma shrinks, and !insert tells it's not
570 * split_vma inserting another: so it must be
571 * mprotect case 4 shifting the boundary down.
572 */
573 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700574 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 importer = next;
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Rik van Riel5beb4932010-03-05 13:42:07 -0800578 /*
579 * Easily overlooked: when mprotect shifts the boundary,
580 * make sure the expanding vma has anon_vma set if the
581 * shrinking vma had, to cover any anon pages imported.
582 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700583 if (exporter && exporter->anon_vma && !importer->anon_vma) {
584 if (anon_vma_clone(importer, exporter))
Rik van Riel5beb4932010-03-05 13:42:07 -0800585 return -ENOMEM;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700586 importer->anon_vma = exporter->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -0800587 }
588 }
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (file) {
591 mapping = file->f_mapping;
592 if (!(vma->vm_flags & VM_NONLINEAR))
593 root = &mapping->i_mmap;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700594 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Put into prio_tree now, so instantiated pages
598 * are visible to arm/parisc __flush_dcache_page
599 * throughout; but we cannot insert into address
600 * space until vma start or end is updated.
601 */
602 __vma_link_file(insert);
603 }
604 }
605
Andrea Arcangeli94fcc582011-01-13 15:47:08 -0800606 vma_adjust_trans_huge(vma, start, end, adjust_next);
607
Rik van Riel012f1802010-08-09 17:18:40 -0700608 /*
609 * When changing only vma->vm_end, we don't really need anon_vma
610 * lock. This is a fairly rare case by itself, but the anon_vma
611 * lock may be shared between many sibling processes. Skipping
612 * the lock for brk adjustments makes a difference sometimes.
613 */
Shaohua Li5f70b962011-05-24 17:11:19 -0700614 if (vma->anon_vma && (importer || start != vma->vm_start)) {
Rik van Riel012f1802010-08-09 17:18:40 -0700615 anon_vma = vma->anon_vma;
616 anon_vma_lock(anon_vma);
617 }
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (root) {
620 flush_dcache_mmap_lock(mapping);
621 vma_prio_tree_remove(vma, root);
622 if (adjust_next)
623 vma_prio_tree_remove(next, root);
624 }
625
626 vma->vm_start = start;
627 vma->vm_end = end;
628 vma->vm_pgoff = pgoff;
629 if (adjust_next) {
630 next->vm_start += adjust_next << PAGE_SHIFT;
631 next->vm_pgoff += adjust_next;
632 }
633
634 if (root) {
635 if (adjust_next)
636 vma_prio_tree_insert(next, root);
637 vma_prio_tree_insert(vma, root);
638 flush_dcache_mmap_unlock(mapping);
639 }
640
641 if (remove_next) {
642 /*
643 * vma_merge has merged next into vma, and needs
644 * us to remove next before dropping the locks.
645 */
646 __vma_unlink(mm, next, vma);
647 if (file)
648 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 } else if (insert) {
650 /*
651 * split_vma has split insert from vma, and needs
652 * us to insert it before dropping the locks
653 * (it may either follow vma or precede it).
654 */
655 __insert_vm_struct(mm, insert);
656 }
657
Rik van Riel012f1802010-08-09 17:18:40 -0700658 if (anon_vma)
659 anon_vma_unlock(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700661 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700664 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700666 if (next->vm_flags & VM_EXECUTABLE)
667 removed_exe_file_vma(mm);
668 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800669 if (next->anon_vma)
670 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 mm->map_count--;
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700672 mpol_put(vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 kmem_cache_free(vm_area_cachep, next);
674 /*
675 * In mprotect's case 6 (see comments on vma_merge),
676 * we must remove another next too. It would clutter
677 * up the code too much to do both in one go.
678 */
679 if (remove_next == 2) {
680 next = vma->vm_next;
681 goto again;
682 }
683 }
684
685 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800686
687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
691 * If the vma has a ->close operation then the driver probably needs to release
692 * per-vma resources, so we don't attempt to merge those.
693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static inline int is_mergeable_vma(struct vm_area_struct *vma,
Colin Crossa9e6b182013-06-26 17:26:01 -0700695 struct file *file, unsigned long vm_flags,
696 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Hugh Dickins8314c4f2009-09-21 17:02:25 -0700698 /* VM_CAN_NONLINEAR may get set later by f_op->mmap() */
699 if ((vma->vm_flags ^ vm_flags) & ~VM_CAN_NONLINEAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701 if (vma->vm_file != file)
702 return 0;
703 if (vma->vm_ops && vma->vm_ops->close)
704 return 0;
Colin Crossa9e6b182013-06-26 17:26:01 -0700705 if (vma_get_anon_name(vma) != anon_name)
706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 1;
708}
709
710static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700711 struct anon_vma *anon_vma2,
712 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Shaohua Li965f55d2011-05-24 17:11:20 -0700714 /*
715 * The list_is_singular() test is to avoid merging VMA cloned from
716 * parents. This can improve scalability caused by anon_vma lock.
717 */
718 if ((!anon_vma1 || !anon_vma2) && (!vma ||
719 list_is_singular(&vma->anon_vma_chain)))
720 return 1;
721 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724/*
725 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
726 * in front of (at a lower virtual address and file offset than) the vma.
727 *
728 * We cannot merge two vmas if they have differently assigned (non-NULL)
729 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
730 *
731 * We don't check here for the merged mmap wrapping around the end of pagecache
732 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
733 * wrap, nor mmaps which cover the final page at index -1UL.
734 */
735static int
736can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
Colin Crossa9e6b182013-06-26 17:26:01 -0700737 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff,
738 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Colin Crossa9e6b182013-06-26 17:26:01 -0700740 if (is_mergeable_vma(vma, file, vm_flags, anon_name) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700741 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (vma->vm_pgoff == vm_pgoff)
743 return 1;
744 }
745 return 0;
746}
747
748/*
749 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
750 * beyond (at a higher virtual address and file offset than) the vma.
751 *
752 * We cannot merge two vmas if they have differently assigned (non-NULL)
753 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
754 */
755static int
756can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
Colin Crossa9e6b182013-06-26 17:26:01 -0700757 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff,
758 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Colin Crossa9e6b182013-06-26 17:26:01 -0700760 if (is_mergeable_vma(vma, file, vm_flags, anon_name) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700761 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 pgoff_t vm_pglen;
763 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
764 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
765 return 1;
766 }
767 return 0;
768}
769
770/*
Colin Crossa9e6b182013-06-26 17:26:01 -0700771 * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name),
772 * figure out whether that can be merged with its predecessor or its
773 * successor. Or both (it neatly fills a hole).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
775 * In most cases - when called for mmap, brk or mremap - [addr,end) is
776 * certain not to be mapped by the time vma_merge is called; but when
777 * called for mprotect, it is certain to be already mapped (either at
778 * an offset within prev, or at the start of next), and the flags of
779 * this area are about to be changed to vm_flags - and the no-change
780 * case has already been eliminated.
781 *
782 * The following mprotect cases have to be considered, where AAAA is
783 * the area passed down from mprotect_fixup, never extending beyond one
784 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
785 *
786 * AAAA AAAA AAAA AAAA
787 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
788 * cannot merge might become might become might become
789 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
790 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
791 * mremap move: PPPPNNNNNNNN 8
792 * AAAA
793 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
794 * might become case 1 below case 2 below case 3 below
795 *
796 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
797 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
798 */
799struct vm_area_struct *vma_merge(struct mm_struct *mm,
800 struct vm_area_struct *prev, unsigned long addr,
801 unsigned long end, unsigned long vm_flags,
802 struct anon_vma *anon_vma, struct file *file,
Colin Crossa9e6b182013-06-26 17:26:01 -0700803 pgoff_t pgoff, struct mempolicy *policy,
804 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
807 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800808 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * We later require that vma->vm_flags == vm_flags,
812 * so this tests vma->vm_flags & VM_SPECIAL, too.
813 */
814 if (vm_flags & VM_SPECIAL)
815 return NULL;
816
817 if (prev)
818 next = prev->vm_next;
819 else
820 next = mm->mmap;
821 area = next;
822 if (next && next->vm_end == end) /* cases 6, 7, 8 */
823 next = next->vm_next;
824
825 /*
826 * Can it merge with the predecessor?
827 */
828 if (prev && prev->vm_end == addr &&
829 mpol_equal(vma_policy(prev), policy) &&
Colin Crossa9e6b182013-06-26 17:26:01 -0700830 can_vma_merge_after(prev, vm_flags, anon_vma,
831 file, pgoff, anon_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * OK, it can. Can we now merge in the successor as well?
834 */
835 if (next && end == next->vm_start &&
836 mpol_equal(policy, vma_policy(next)) &&
Colin Crossa9e6b182013-06-26 17:26:01 -0700837 can_vma_merge_before(next, vm_flags, anon_vma,
838 file, pgoff+pglen, anon_name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -0700840 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* cases 1, 6 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800842 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 next->vm_end, prev->vm_pgoff, NULL);
844 } else /* cases 2, 5, 7 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800845 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 end, prev->vm_pgoff, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -0800847 if (err)
848 return NULL;
Andrea Arcangelib15d00b2011-01-13 15:46:59 -0800849 khugepaged_enter_vma_merge(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return prev;
851 }
852
853 /*
854 * Can this new request be merged in front of next?
855 */
856 if (next && end == next->vm_start &&
857 mpol_equal(policy, vma_policy(next)) &&
Colin Crossa9e6b182013-06-26 17:26:01 -0700858 can_vma_merge_before(next, vm_flags, anon_vma,
859 file, pgoff+pglen, anon_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (prev && addr < prev->vm_end) /* case 4 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800861 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 addr, prev->vm_pgoff, NULL);
863 else /* cases 3, 8 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800864 err = vma_adjust(area, addr, next->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 next->vm_pgoff - pglen, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -0800866 if (err)
867 return NULL;
Andrea Arcangelib15d00b2011-01-13 15:46:59 -0800868 khugepaged_enter_vma_merge(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return area;
870 }
871
872 return NULL;
873}
874
875/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700876 * Rough compatbility check to quickly see if it's even worth looking
877 * at sharing an anon_vma.
878 *
879 * They need to have the same vm_file, and the flags can only differ
880 * in things that mprotect may change.
881 *
882 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
883 * we can merge the two vma's. For example, we refuse to merge a vma if
884 * there is a vm_ops->close() function, because that indicates that the
885 * driver is doing some kind of reference counting. But that doesn't
886 * really matter for the anon_vma sharing case.
887 */
888static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
889{
890 return a->vm_end == b->vm_start &&
891 mpol_equal(vma_policy(a), vma_policy(b)) &&
892 a->vm_file == b->vm_file &&
893 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
894 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
895}
896
897/*
898 * Do some basic sanity checking to see if we can re-use the anon_vma
899 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
900 * the same as 'old', the other will be the new one that is trying
901 * to share the anon_vma.
902 *
903 * NOTE! This runs with mm_sem held for reading, so it is possible that
904 * the anon_vma of 'old' is concurrently in the process of being set up
905 * by another page fault trying to merge _that_. But that's ok: if it
906 * is being set up, that automatically means that it will be a singleton
907 * acceptable for merging, so we can do all of this optimistically. But
908 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
909 *
910 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
911 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
912 * is to return an anon_vma that is "complex" due to having gone through
913 * a fork).
914 *
915 * We also make sure that the two vma's are compatible (adjacent,
916 * and with the same memory policies). That's all stable, even with just
917 * a read lock on the mm_sem.
918 */
919static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
920{
921 if (anon_vma_compatible(a, b)) {
922 struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
923
924 if (anon_vma && list_is_singular(&old->anon_vma_chain))
925 return anon_vma;
926 }
927 return NULL;
928}
929
930/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
932 * neighbouring vmas for a suitable anon_vma, before it goes off
933 * to allocate a new anon_vma. It checks because a repetitive
934 * sequence of mprotects and faults may otherwise lead to distinct
935 * anon_vmas being allocated, preventing vma merge in subsequent
936 * mprotect.
937 */
938struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
939{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700940 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 near = vma->vm_next;
944 if (!near)
945 goto try_prev;
946
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700947 anon_vma = reusable_anon_vma(near, vma, near);
948 if (anon_vma)
949 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -0700951 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!near)
953 goto none;
954
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700955 anon_vma = reusable_anon_vma(near, near, vma);
956 if (anon_vma)
957 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958none:
959 /*
960 * There's no absolute need to look only at touching neighbours:
961 * we could search further afield for "compatible" anon_vmas.
962 * But it would probably just be a waste of time searching,
963 * or lead to too many vmas hanging off the same anon_vma.
964 * We're trying to allow mprotect remerging later on,
965 * not trying to minimize memory used for anon_vmas.
966 */
967 return NULL;
968}
969
970#ifdef CONFIG_PROC_FS
Hugh Dickinsab50b8e2005-10-29 18:15:56 -0700971void vm_stat_account(struct mm_struct *mm, unsigned long flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct file *file, long pages)
973{
974 const unsigned long stack_flags
975 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (file) {
978 mm->shared_vm += pages;
979 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
980 mm->exec_vm += pages;
981 } else if (flags & stack_flags)
982 mm->stack_vm += pages;
983 if (flags & (VM_RESERVED|VM_IO))
984 mm->reserved_vm += pages;
985}
986#endif /* CONFIG_PROC_FS */
987
988/*
Al Viro40401532012-02-13 03:58:52 +0000989 * If a hint addr is less than mmap_min_addr change hint to be as
990 * low as possible but still greater than mmap_min_addr
991 */
992static inline unsigned long round_hint_to_min(unsigned long hint)
993{
994 hint &= PAGE_MASK;
995 if (((void *)hint != NULL) &&
996 (hint < mmap_min_addr))
997 return PAGE_ALIGN(mmap_min_addr);
998 return hint;
999}
1000
1001/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001002 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
1004
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001005static unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unsigned long len, unsigned long prot,
1007 unsigned long flags, unsigned long pgoff)
1008{
1009 struct mm_struct * mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct inode *inode;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001011 vm_flags_t vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int error;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001013 unsigned long reqprot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * Does the application expect PROT_READ to imply PROT_EXEC?
1017 *
1018 * (the exception is when the underlying filesystem is noexec
1019 * mounted, in which case we dont add PROT_EXEC.)
1020 */
1021 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001022 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 prot |= PROT_EXEC;
1024
1025 if (!len)
1026 return -EINVAL;
1027
Eric Paris7cd94142007-11-26 18:47:40 -05001028 if (!(flags & MAP_FIXED))
1029 addr = round_hint_to_min(addr);
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* Careful about overflows.. */
1032 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001033 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -ENOMEM;
1035
1036 /* offset overflow? */
1037 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
1038 return -EOVERFLOW;
1039
1040 /* Too many mappings? */
1041 if (mm->map_count > sysctl_max_map_count)
1042 return -ENOMEM;
1043
1044 /* Obtain the address to map to. we verify (or select) it and ensure
1045 * that it represents a valid section of the address space.
1046 */
1047 addr = get_unmapped_area(file, addr, len, pgoff, flags);
1048 if (addr & ~PAGE_MASK)
1049 return addr;
1050
1051 /* Do simple checking here so the lower-level routines won't have
1052 * to. we assume access permissions have been handled by the open
1053 * of the memory object, so we don't do any here.
1054 */
1055 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
1056 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1057
Huang Shijiecdf7b342009-09-21 17:03:36 -07001058 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!can_do_mlock())
1060 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* mlock MCL_FUTURE? */
1063 if (vm_flags & VM_LOCKED) {
1064 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07001065 locked = len >> PAGE_SHIFT;
1066 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08001067 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07001068 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1070 return -EAGAIN;
1071 }
1072
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001073 inode = file ? file->f_path.dentry->d_inode : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (file) {
1076 switch (flags & MAP_TYPE) {
1077 case MAP_SHARED:
1078 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1079 return -EACCES;
1080
1081 /*
1082 * Make sure we don't allow writing to an append-only
1083 * file..
1084 */
1085 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1086 return -EACCES;
1087
1088 /*
1089 * Make sure there are no mandatory locks on the file.
1090 */
1091 if (locks_verify_locked(inode))
1092 return -EAGAIN;
1093
1094 vm_flags |= VM_SHARED | VM_MAYSHARE;
1095 if (!(file->f_mode & FMODE_WRITE))
1096 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1097
1098 /* fall through */
1099 case MAP_PRIVATE:
1100 if (!(file->f_mode & FMODE_READ))
1101 return -EACCES;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001102 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001103 if (vm_flags & VM_EXEC)
1104 return -EPERM;
1105 vm_flags &= ~VM_MAYEXEC;
1106 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001107
1108 if (!file->f_op || !file->f_op->mmap)
1109 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111
1112 default:
1113 return -EINVAL;
1114 }
1115 } else {
1116 switch (flags & MAP_TYPE) {
1117 case MAP_SHARED:
Tejun Heoce363942008-09-03 16:09:47 +02001118 /*
1119 * Ignore pgoff.
1120 */
1121 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 vm_flags |= VM_SHARED | VM_MAYSHARE;
1123 break;
1124 case MAP_PRIVATE:
1125 /*
1126 * Set pgoff according to addr for anon_vma.
1127 */
1128 pgoff = addr >> PAGE_SHIFT;
1129 break;
1130 default:
1131 return -EINVAL;
1132 }
1133 }
1134
Eric Parised032182007-06-28 15:55:21 -04001135 error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (error)
1137 return error;
Eric Parised032182007-06-28 15:55:21 -04001138
Mel Gorman5a6fe122009-02-10 14:02:27 +00001139 return mmap_region(file, addr, len, flags, vm_flags, pgoff);
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001140}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001141
1142unsigned long do_mmap(struct file *file, unsigned long addr,
1143 unsigned long len, unsigned long prot,
1144 unsigned long flag, unsigned long offset)
1145{
1146 if (unlikely(offset + PAGE_ALIGN(len) < offset))
1147 return -EINVAL;
1148 if (unlikely(offset & ~PAGE_MASK))
1149 return -EINVAL;
1150 return do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
1151}
1152EXPORT_SYMBOL(do_mmap);
1153
1154unsigned long vm_mmap(struct file *file, unsigned long addr,
1155 unsigned long len, unsigned long prot,
1156 unsigned long flag, unsigned long offset)
1157{
1158 unsigned long ret;
1159 struct mm_struct *mm = current->mm;
1160
1161 down_write(&mm->mmap_sem);
1162 ret = do_mmap(file, addr, len, prot, flag, offset);
1163 up_write(&mm->mmap_sem);
1164 return ret;
1165}
1166EXPORT_SYMBOL(vm_mmap);
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001167
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001168SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1169 unsigned long, prot, unsigned long, flags,
1170 unsigned long, fd, unsigned long, pgoff)
1171{
1172 struct file *file = NULL;
1173 unsigned long retval = -EBADF;
1174
1175 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001176 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001177 if (unlikely(flags & MAP_HUGETLB))
1178 return -EINVAL;
1179 file = fget(fd);
1180 if (!file)
1181 goto out;
Naoya Horiguchif8597ae2013-05-07 16:18:13 -07001182 if (is_file_hugepages(file))
1183 len = ALIGN(len, huge_page_size(hstate_file(file)));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001184 } else if (flags & MAP_HUGETLB) {
1185 struct user_struct *user = NULL;
Naoya Horiguchif8597ae2013-05-07 16:18:13 -07001186
1187 len = ALIGN(len, huge_page_size(&default_hstate));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001188 /*
1189 * VM_NORESERVE is used because the reservations will be
1190 * taken when vm_ops->mmap() is called
1191 * A dummy user value is used because we are not locking
1192 * memory so no accounting is necessary
1193 */
Naoya Horiguchif8597ae2013-05-07 16:18:13 -07001194 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Steven Truelove40716e22012-03-21 16:34:14 -07001195 VM_NORESERVE, &user,
1196 HUGETLB_ANONHUGE_INODE);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001197 if (IS_ERR(file))
1198 return PTR_ERR(file);
1199 }
1200
1201 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1202
1203 down_write(&current->mm->mmap_sem);
1204 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1205 up_write(&current->mm->mmap_sem);
1206
1207 if (file)
1208 fput(file);
1209out:
1210 return retval;
1211}
1212
Christoph Hellwiga4679372010-03-10 15:21:15 -08001213#ifdef __ARCH_WANT_SYS_OLD_MMAP
1214struct mmap_arg_struct {
1215 unsigned long addr;
1216 unsigned long len;
1217 unsigned long prot;
1218 unsigned long flags;
1219 unsigned long fd;
1220 unsigned long offset;
1221};
1222
1223SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1224{
1225 struct mmap_arg_struct a;
1226
1227 if (copy_from_user(&a, arg, sizeof(a)))
1228 return -EFAULT;
1229 if (a.offset & ~PAGE_MASK)
1230 return -EINVAL;
1231
1232 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1233 a.offset >> PAGE_SHIFT);
1234}
1235#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1236
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001237/*
1238 * Some shared mappigns will want the pages marked read-only
1239 * to track write events. If so, we'll downgrade vm_page_prot
1240 * to the private version (using protection_map[] without the
1241 * VM_SHARED bit).
1242 */
1243int vma_wants_writenotify(struct vm_area_struct *vma)
1244{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001245 vm_flags_t vm_flags = vma->vm_flags;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001246
1247 /* If it was private or non-writable, the write bit is already clear */
1248 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1249 return 0;
1250
1251 /* The backer wishes to know when pages are first written to? */
1252 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1253 return 1;
1254
1255 /* The open routine did something to the protections already? */
1256 if (pgprot_val(vma->vm_page_prot) !=
Coly Li3ed75eb2007-10-18 23:39:15 -07001257 pgprot_val(vm_get_page_prot(vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001258 return 0;
1259
1260 /* Specialty mapping? */
1261 if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
1262 return 0;
1263
1264 /* Can the mapping track the dirty pages? */
1265 return vma->vm_file && vma->vm_file->f_mapping &&
1266 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1267}
1268
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001269/*
1270 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001271 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001272 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001273static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001274{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001275 /*
1276 * hugetlb has its own accounting separate from the core VM
1277 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1278 */
1279 if (file && is_file_hugepages(file))
1280 return 0;
1281
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001282 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1283}
1284
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001285unsigned long mmap_region(struct file *file, unsigned long addr,
1286 unsigned long len, unsigned long flags,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001287 vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001288{
1289 struct mm_struct *mm = current->mm;
1290 struct vm_area_struct *vma, *prev;
1291 int correct_wcount = 0;
1292 int error;
1293 struct rb_node **rb_link, *rb_parent;
1294 unsigned long charged = 0;
1295 struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
1296
Cyril Hrubis3e55f212013-04-29 15:08:33 -07001297 /* Check against address space limit. */
1298 if (!may_expand_vm(mm, len >> PAGE_SHIFT)) {
1299 unsigned long nr_pages;
1300
1301 /*
1302 * MAP_FIXED may remove pages of mappings that intersects with
1303 * requested mapping. Account for the pages it would unmap.
1304 */
1305 if (!(vm_flags & MAP_FIXED))
1306 return -ENOMEM;
1307
1308 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1309
1310 if (!may_expand_vm(mm, (len >> PAGE_SHIFT) - nr_pages))
1311 return -ENOMEM;
1312 }
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* Clear old maps */
1315 error = -ENOMEM;
1316munmap_back:
1317 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1318 if (vma && vma->vm_start < addr + len) {
1319 if (do_munmap(mm, addr, len))
1320 return -ENOMEM;
1321 goto munmap_back;
1322 }
1323
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001324 /*
1325 * Set 'VM_NORESERVE' if we should not account for the
Mel Gorman5a6fe122009-02-10 14:02:27 +00001326 * memory use of this mapping.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001327 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001328 if ((flags & MAP_NORESERVE)) {
1329 /* We honor MAP_NORESERVE if allowed to overcommit */
1330 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1331 vm_flags |= VM_NORESERVE;
1332
1333 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1334 if (file && is_file_hugepages(file))
1335 vm_flags |= VM_NORESERVE;
1336 }
Andy Whitcroftcdfd4322008-07-23 21:27:28 -07001337
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001338 /*
1339 * Private writable mapping: check memory availability
1340 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001341 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001342 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001343 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001344 return -ENOMEM;
1345 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
1348 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001349 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
Colin Crossa9e6b182013-06-26 17:26:01 -07001351 vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff,
1352 NULL, NULL);
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001353 if (vma)
1354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /*
1357 * Determine the object being mapped and call the appropriate
1358 * specific mapper. the address has already been validated, but
1359 * not unmapped, but the maps are removed from the list.
1360 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001361 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!vma) {
1363 error = -ENOMEM;
1364 goto unacct_error;
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 vma->vm_mm = mm;
1368 vma->vm_start = addr;
1369 vma->vm_end = addr + len;
1370 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001371 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001373 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Hugh Dickinsce8fea72012-03-06 12:28:52 -08001375 error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1379 goto free_vma;
1380 if (vm_flags & VM_DENYWRITE) {
1381 error = deny_write_access(file);
1382 if (error)
1383 goto free_vma;
1384 correct_wcount = 1;
1385 }
1386 vma->vm_file = file;
1387 get_file(file);
1388 error = file->f_op->mmap(file, vma);
1389 if (error)
1390 goto unmap_and_free_vma;
Matt Helsley925d1c42008-04-29 01:01:36 -07001391 if (vm_flags & VM_EXECUTABLE)
1392 added_exe_file_vma(mm);
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001393
1394 /* Can addr have changed??
1395 *
1396 * Answer: Yes, several device drivers can do it in their
1397 * f_op->mmap method. -DaveM
1398 */
1399 addr = vma->vm_start;
1400 pgoff = vma->vm_pgoff;
1401 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 } else if (vm_flags & VM_SHARED) {
Al Viro835ee792012-03-05 06:39:47 +00001403 if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
1404 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 error = shmem_zero_setup(vma);
1406 if (error)
1407 goto free_vma;
1408 }
1409
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001410 if (vma_wants_writenotify(vma)) {
1411 pgprot_t pprot = vma->vm_page_prot;
1412
1413 /* Can vma->vm_page_prot have changed??
1414 *
1415 * Answer: Yes, drivers may have changed it in their
1416 * f_op->mmap method.
1417 *
1418 * Ensures that vmas marked as uncached stay that way.
1419 */
Hugh Dickins1ddd4392007-10-22 20:45:12 -07001420 vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001421 if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1422 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1423 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -07001424
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001425 vma_link(mm, vma, prev, rb_link, rb_parent);
1426 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001427
1428 /* Once vma denies write, undo our temporary denial count */
1429 if (correct_wcount)
1430 atomic_inc(&inode->i_writecount);
1431out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001432 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 mm->total_vm += len >> PAGE_SHIFT;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001435 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (vm_flags & VM_LOCKED) {
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001437 if (!mlock_vma_pages_range(vma, addr, addr + len))
1438 mm->locked_vm += (len >> PAGE_SHIFT);
Rik van Rielba470de2008-10-18 20:26:50 -07001439 } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
Nick Piggin54cb8822007-07-19 01:46:59 -07001440 make_pages_present(addr, addr + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return addr;
1442
1443unmap_and_free_vma:
1444 if (correct_wcount)
1445 atomic_inc(&inode->i_writecount);
1446 vma->vm_file = NULL;
1447 fput(file);
1448
1449 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001450 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1451 charged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452free_vma:
1453 kmem_cache_free(vm_area_cachep, vma);
1454unacct_error:
1455 if (charged)
1456 vm_unacct_memory(charged);
1457 return error;
1458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/* Get an address range which is currently unmapped.
1461 * For shmat() with addr=0.
1462 *
1463 * Ugly calling convention alert:
1464 * Return value with the low bits set means error value,
1465 * ie
1466 * if (ret & ~PAGE_MASK)
1467 * error = ret;
1468 *
1469 * This function "knows" that -ENOMEM has the bits set.
1470 */
1471#ifndef HAVE_ARCH_UNMAPPED_AREA
1472unsigned long
1473arch_get_unmapped_area(struct file *filp, unsigned long addr,
1474 unsigned long len, unsigned long pgoff, unsigned long flags)
1475{
1476 struct mm_struct *mm = current->mm;
Hugh Dickinse8a93762017-06-19 04:03:24 -07001477 struct vm_area_struct *vma, *prev;
1478 unsigned long start_addr, vm_start, prev_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (len > TASK_SIZE)
1481 return -ENOMEM;
1482
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001483 if (flags & MAP_FIXED)
1484 return addr;
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (addr) {
1487 addr = PAGE_ALIGN(addr);
Hugh Dickinse8a93762017-06-19 04:03:24 -07001488 vma = find_vma_prev(mm, addr, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (TASK_SIZE - len >= addr &&
Hugh Dickinse8a93762017-06-19 04:03:24 -07001490 (!vma || addr + len <= vm_start_gap(vma)) &&
1491 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return addr;
1493 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001494 if (len > mm->cached_hole_size) {
1495 start_addr = addr = mm->free_area_cache;
1496 } else {
1497 start_addr = addr = TASK_UNMAPPED_BASE;
1498 mm->cached_hole_size = 0;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501full_search:
Hugh Dickinse8a93762017-06-19 04:03:24 -07001502 for (vma = find_vma_prev(mm, addr, &prev); ; prev = vma,
1503 vma = vma->vm_next) {
1504 if (prev) {
1505 prev_end = vm_end_gap(prev);
1506 if (addr < prev_end) {
1507 addr = prev_end;
1508 /* If vma already violates gap, forget it */
1509 if (vma && addr > vma->vm_start)
1510 addr = vma->vm_start;
1511 }
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* At this point: (!vma || addr < vma->vm_end). */
1514 if (TASK_SIZE - len < addr) {
1515 /*
1516 * Start a new search - just in case we missed
1517 * some holes.
1518 */
1519 if (start_addr != TASK_UNMAPPED_BASE) {
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001520 addr = TASK_UNMAPPED_BASE;
1521 start_addr = addr;
1522 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto full_search;
1524 }
1525 return -ENOMEM;
1526 }
Hugh Dickinse8a93762017-06-19 04:03:24 -07001527 vm_start = vma ? vm_start_gap(vma) : TASK_SIZE;
1528 if (addr + len <= vm_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /*
1530 * Remember the place where we stopped the search:
1531 */
1532 mm->free_area_cache = addr + len;
1533 return addr;
1534 }
Hugh Dickinse8a93762017-06-19 04:03:24 -07001535 if (addr + mm->cached_hole_size < vm_start)
1536 mm->cached_hole_size = vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538}
1539#endif
1540
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001541void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 /*
1544 * Is this a new hole at the lowest possible address?
1545 */
Xiao Guangrongf44d2192012-03-21 16:33:56 -07001546 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001547 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550/*
1551 * This mmap-allocator allocates new areas top-down from below the
1552 * stack's low limit (the base):
1553 */
1554#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1555unsigned long
1556arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1557 const unsigned long len, const unsigned long pgoff,
1558 const unsigned long flags)
1559{
Hugh Dickinse8a93762017-06-19 04:03:24 -07001560 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 struct mm_struct *mm = current->mm;
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001562 unsigned long addr = addr0, start_addr;
Hugh Dickinse8a93762017-06-19 04:03:24 -07001563 unsigned long vm_start, prev_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /* requested length too big for entire address space */
1566 if (len > TASK_SIZE)
1567 return -ENOMEM;
1568
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001569 if (flags & MAP_FIXED)
1570 return addr;
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* requesting a specific address */
1573 if (addr) {
1574 addr = PAGE_ALIGN(addr);
Hugh Dickinse8a93762017-06-19 04:03:24 -07001575 vma = find_vma_prev(mm, addr, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (TASK_SIZE - len >= addr &&
Hugh Dickinse8a93762017-06-19 04:03:24 -07001577 (!vma || addr + len <= vm_start_gap(vma)) &&
1578 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return addr;
1580 }
1581
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001582 /* check if free_area_cache is useful for us */
1583 if (len <= mm->cached_hole_size) {
1584 mm->cached_hole_size = 0;
1585 mm->free_area_cache = mm->mmap_base;
1586 }
1587
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001588try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* either no address requested or can't fit in requested address hole */
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001590 start_addr = addr = mm->free_area_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001592 if (addr < len)
1593 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001595 addr -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 do {
1597 /*
1598 * Lookup failure means no vma is above this address,
1599 * else if new region fits below vma->vm_start,
1600 * return with success:
1601 */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001602 vma = find_vma_prev(mm, addr, &prev);
1603 vm_start = vma ? vm_start_gap(vma) : mm->mmap_base;
1604 prev_end = vm_end_gap(prev);
1605
1606 if (addr + len <= vm_start && addr >= prev_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* remember the address as a hint for next time */
1608 return (mm->free_area_cache = addr);
1609
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001610 /* remember the largest hole we saw so far */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001611 if (addr + mm->cached_hole_size < vm_start)
1612 mm->cached_hole_size = vm_start - addr;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* try just below the current vma->vm_start */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001615 addr = vm_start - len;
1616 } while (len < vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001618fail:
1619 /*
1620 * if hint left us with no space for the requested
1621 * mapping then try again:
1622 *
1623 * Note: this is different with the case of bottomup
1624 * which does the fully line-search, but we use find_vma
1625 * here that causes some holes skipped.
1626 */
1627 if (start_addr != mm->mmap_base) {
1628 mm->free_area_cache = mm->mmap_base;
1629 mm->cached_hole_size = 0;
1630 goto try_again;
1631 }
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /*
1634 * A failed mmap() very likely causes application failure,
1635 * so fall back to the bottom-up function here. This scenario
1636 * can happen with large stack limits and large mmap()
1637 * allocations.
1638 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001639 mm->cached_hole_size = ~0UL;
1640 mm->free_area_cache = TASK_UNMAPPED_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1642 /*
1643 * Restore the topdown base:
1644 */
1645 mm->free_area_cache = mm->mmap_base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001646 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 return addr;
1649}
1650#endif
1651
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001652void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 /*
1655 * Is this a new hole at the highest possible address?
1656 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001657 if (addr > mm->free_area_cache)
1658 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /* dont allow allocations above current base */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001661 if (mm->free_area_cache > mm->mmap_base)
1662 mm->free_area_cache = mm->mmap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665unsigned long
1666get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1667 unsigned long pgoff, unsigned long flags)
1668{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001669 unsigned long (*get_area)(struct file *, unsigned long,
1670 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Al Viro9206de92009-12-03 15:23:11 -05001672 unsigned long error = arch_mmap_check(addr, len, flags);
1673 if (error)
1674 return error;
1675
1676 /* Careful about overflows.. */
1677 if (len > TASK_SIZE)
1678 return -ENOMEM;
1679
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001680 get_area = current->mm->get_unmapped_area;
1681 if (file && file->f_op && file->f_op->get_unmapped_area)
1682 get_area = file->f_op->get_unmapped_area;
1683 addr = get_area(file, addr, len, pgoff, flags);
1684 if (IS_ERR_VALUE(addr))
1685 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001687 if (addr > TASK_SIZE - len)
1688 return -ENOMEM;
1689 if (addr & ~PAGE_MASK)
1690 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001691
Martin Schwidefsky08e7d9b2008-02-04 22:29:16 -08001692 return arch_rebalance_pgtables(addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695EXPORT_SYMBOL(get_unmapped_area);
1696
1697/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08001698struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 struct vm_area_struct *vma = NULL;
1701
1702 if (mm) {
1703 /* Check the cache first. */
1704 /* (Cache hit rate is typically around 35%.) */
Jan Stancek9ee73982013-04-08 13:00:02 -07001705 vma = ACCESS_ONCE(mm->mmap_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1707 struct rb_node * rb_node;
1708
1709 rb_node = mm->mm_rb.rb_node;
1710 vma = NULL;
1711
1712 while (rb_node) {
1713 struct vm_area_struct * vma_tmp;
1714
1715 vma_tmp = rb_entry(rb_node,
1716 struct vm_area_struct, vm_rb);
1717
1718 if (vma_tmp->vm_end > addr) {
1719 vma = vma_tmp;
1720 if (vma_tmp->vm_start <= addr)
1721 break;
1722 rb_node = rb_node->rb_left;
1723 } else
1724 rb_node = rb_node->rb_right;
1725 }
1726 if (vma)
1727 mm->mmap_cache = vma;
1728 }
1729 }
1730 return vma;
1731}
1732
1733EXPORT_SYMBOL(find_vma);
1734
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001735/*
1736 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738struct vm_area_struct *
1739find_vma_prev(struct mm_struct *mm, unsigned long addr,
1740 struct vm_area_struct **pprev)
1741{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001742 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001744 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05001745 if (vma) {
1746 *pprev = vma->vm_prev;
1747 } else {
1748 struct rb_node *rb_node = mm->mm_rb.rb_node;
1749 *pprev = NULL;
1750 while (rb_node) {
1751 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1752 rb_node = rb_node->rb_right;
1753 }
1754 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001755 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758/*
1759 * Verify that the stack growth is acceptable and
1760 * update accounting. This is shared with both the
1761 * grow-up and grow-down cases.
1762 */
ZhenwenXu48aae422009-01-06 14:40:21 -08001763static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 struct mm_struct *mm = vma->vm_mm;
1766 struct rlimit *rlim = current->signal->rlim;
Adam Litke0d59a012007-01-30 14:35:39 -08001767 unsigned long new_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /* address space limit tests */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001770 if (!may_expand_vm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return -ENOMEM;
1772
1773 /* Stack limit test */
Jiri Slaby59e99e52010-03-05 13:41:44 -08001774 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return -ENOMEM;
1776
1777 /* mlock limit tests */
1778 if (vma->vm_flags & VM_LOCKED) {
1779 unsigned long locked;
1780 unsigned long limit;
1781 locked = mm->locked_vm + grow;
Jiri Slaby59e99e52010-03-05 13:41:44 -08001782 limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
1783 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (locked > limit && !capable(CAP_IPC_LOCK))
1785 return -ENOMEM;
1786 }
1787
Adam Litke0d59a012007-01-30 14:35:39 -08001788 /* Check to ensure the stack will not grow into a hugetlb-only region */
1789 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
1790 vma->vm_end - size;
1791 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
1792 return -EFAULT;
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /*
1795 * Overcommit.. This must be the final test, as it will
1796 * update security statistics.
1797 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01001798 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return -ENOMEM;
1800
1801 /* Ok, everything looks good - let it rip */
1802 mm->total_vm += grow;
1803 if (vma->vm_flags & VM_LOCKED)
1804 mm->locked_vm += grow;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001805 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return 0;
1807}
1808
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001809#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001811 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1812 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001814int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Hugh Dickinse8a93762017-06-19 04:03:24 -07001816 struct vm_area_struct *next;
1817 unsigned long gap_addr;
1818 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 if (!(vma->vm_flags & VM_GROWSUP))
1821 return -EFAULT;
1822
1823 /*
1824 * We must make sure the anon_vma is allocated
1825 * so that the anon_vma locking is not a noop.
1826 */
Helge Deller036d4232017-06-19 17:34:05 +02001827 /* Guard against exceeding limits of the address space. */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001828 address &= PAGE_MASK;
Helge Deller036d4232017-06-19 17:34:05 +02001829 if (address >= TASK_SIZE)
Hugh Dickinse8a93762017-06-19 04:03:24 -07001830 return -ENOMEM;
Helge Deller036d4232017-06-19 17:34:05 +02001831 address += PAGE_SIZE;
Hugh Dickinse8a93762017-06-19 04:03:24 -07001832
1833 /* Enforce stack_guard_gap */
1834 gap_addr = address + stack_guard_gap;
Helge Deller036d4232017-06-19 17:34:05 +02001835
1836 /* Guard against overflow */
1837 if (gap_addr < address || gap_addr > TASK_SIZE)
1838 gap_addr = TASK_SIZE;
1839
Hugh Dickinse8a93762017-06-19 04:03:24 -07001840 next = vma->vm_next;
1841 if (next && next->vm_start < gap_addr) {
1842 if (!(next->vm_flags & VM_GROWSUP))
1843 return -ENOMEM;
1844 /* Check that both stack segments have the same anon_vma? */
1845 }
1846
1847 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (unlikely(anon_vma_prepare(vma)))
1849 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /*
1852 * vma->vm_start/vm_end cannot change under us because the caller
1853 * is required to hold the mmap_sem in read mode. We need the
1854 * anon_vma lock to serialize against concurrent expand_stacks.
1855 */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001856 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* Somebody else might have raced and expanded it already */
1859 if (address > vma->vm_end) {
1860 unsigned long size, grow;
1861
1862 size = address - vma->vm_start;
1863 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1864
Hugh Dickins42c36f62011-05-09 17:44:42 -07001865 error = -ENOMEM;
1866 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
1867 error = acct_stack_growth(vma, size, grow);
1868 if (!error) {
1869 vma->vm_end = address;
1870 perf_event_mmap(vma);
1871 }
Eric B Munson3af9e852010-05-18 15:30:49 +01001872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07001874 vma_unlock_anon_vma(vma);
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08001875 khugepaged_enter_vma_merge(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return error;
1877}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001878#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/*
1881 * vma is the first one with address < vma->vm_start. Have to extend vma.
1882 */
Michal Hockod05f3162011-05-24 17:11:44 -07001883int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001884 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Hugh Dickinse8a93762017-06-19 04:03:24 -07001886 struct vm_area_struct *prev;
1887 unsigned long gap_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int error;
1889
Eric Paris88694772007-11-26 18:47:26 -05001890 address &= PAGE_MASK;
Richard Knutsson88c3f7a2007-12-08 12:02:48 +01001891 error = security_file_mmap(NULL, 0, 0, 0, address, 1);
Eric Paris88694772007-11-26 18:47:26 -05001892 if (error)
1893 return error;
1894
Hugh Dickinse8a93762017-06-19 04:03:24 -07001895 /* Enforce stack_guard_gap */
1896 gap_addr = address - stack_guard_gap;
1897 if (gap_addr > address)
1898 return -ENOMEM;
1899 prev = vma->vm_prev;
1900 if (prev && prev->vm_end > gap_addr) {
1901 if (!(prev->vm_flags & VM_GROWSDOWN))
1902 return -ENOMEM;
1903 /* Check that both stack segments have the same anon_vma? */
1904 }
1905
1906 /* We must make sure the anon_vma is allocated. */
1907 if (unlikely(anon_vma_prepare(vma)))
1908 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /*
1911 * vma->vm_start/vm_end cannot change under us because the caller
1912 * is required to hold the mmap_sem in read mode. We need the
1913 * anon_vma lock to serialize against concurrent expand_stacks.
1914 */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001915 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /* Somebody else might have raced and expanded it already */
1918 if (address < vma->vm_start) {
1919 unsigned long size, grow;
1920
1921 size = vma->vm_end - address;
1922 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1923
Linus Torvaldsa626ca62011-04-13 08:07:28 -07001924 error = -ENOMEM;
1925 if (grow <= vma->vm_pgoff) {
1926 error = acct_stack_growth(vma, size, grow);
1927 if (!error) {
1928 vma->vm_start = address;
1929 vma->vm_pgoff -= grow;
1930 perf_event_mmap(vma);
1931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07001934 vma_unlock_anon_vma(vma);
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08001935 khugepaged_enter_vma_merge(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return error;
1937}
1938
Hugh Dickinse8a93762017-06-19 04:03:24 -07001939/* enforced gap between the expanding stack and other mappings. */
1940unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
1941
1942static int __init cmdline_parse_stack_guard_gap(char *p)
1943{
1944 unsigned long val;
1945 char *endptr;
1946
1947 val = simple_strtoul(p, &endptr, 10);
1948 if (!*endptr)
1949 stack_guard_gap = val << PAGE_SHIFT;
1950
1951 return 0;
1952}
1953__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
1954
Linus Torvaldsd21452e2013-02-27 08:36:04 -08001955/*
1956 * Note how expand_stack() refuses to expand the stack all the way to
1957 * abut the next virtual mapping, *unless* that mapping itself is also
1958 * a stack mapping. We want to leave room for a guard page, after all
1959 * (the guard page itself is not added here, that is done by the
1960 * actual page faulting logic)
1961 *
1962 * This matches the behavior of the guard page logic (see mm/memory.c:
1963 * check_stack_guard_page()), which only allows the guard page to be
1964 * removed under these circumstances.
1965 */
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001966#ifdef CONFIG_STACK_GROWSUP
1967int expand_stack(struct vm_area_struct *vma, unsigned long address)
1968{
Linus Torvaldsd21452e2013-02-27 08:36:04 -08001969 struct vm_area_struct *next;
1970
1971 address &= PAGE_MASK;
1972 next = vma->vm_next;
1973 if (next && next->vm_start == address + PAGE_SIZE) {
1974 if (!(next->vm_flags & VM_GROWSUP))
1975 return -ENOMEM;
1976 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001977 return expand_upwards(vma, address);
1978}
1979
1980struct vm_area_struct *
1981find_extend_vma(struct mm_struct *mm, unsigned long addr)
1982{
1983 struct vm_area_struct *vma, *prev;
1984
1985 addr &= PAGE_MASK;
1986 vma = find_vma_prev(mm, addr, &prev);
1987 if (vma && (vma->vm_start <= addr))
1988 return vma;
Denys Vlasenko1c127182008-11-12 01:24:41 +01001989 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001990 return NULL;
Rik van Rielba470de2008-10-18 20:26:50 -07001991 if (prev->vm_flags & VM_LOCKED) {
KOSAKI Motohiroc58267c2010-03-05 13:41:43 -08001992 mlock_vma_pages_range(prev, addr, prev->vm_end);
Rik van Rielba470de2008-10-18 20:26:50 -07001993 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001994 return prev;
1995}
1996#else
1997int expand_stack(struct vm_area_struct *vma, unsigned long address)
1998{
Linus Torvaldsd21452e2013-02-27 08:36:04 -08001999 struct vm_area_struct *prev;
2000
2001 address &= PAGE_MASK;
2002 prev = vma->vm_prev;
2003 if (prev && prev->vm_end == address) {
2004 if (!(prev->vm_flags & VM_GROWSDOWN))
2005 return -ENOMEM;
2006 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002007 return expand_downwards(vma, address);
2008}
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010struct vm_area_struct *
2011find_extend_vma(struct mm_struct * mm, unsigned long addr)
2012{
2013 struct vm_area_struct * vma;
2014 unsigned long start;
2015
2016 addr &= PAGE_MASK;
2017 vma = find_vma(mm,addr);
2018 if (!vma)
2019 return NULL;
2020 if (vma->vm_start <= addr)
2021 return vma;
2022 if (!(vma->vm_flags & VM_GROWSDOWN))
2023 return NULL;
2024 start = vma->vm_start;
2025 if (expand_stack(vma, addr))
2026 return NULL;
Rik van Rielba470de2008-10-18 20:26:50 -07002027 if (vma->vm_flags & VM_LOCKED) {
KOSAKI Motohiroc58267c2010-03-05 13:41:43 -08002028 mlock_vma_pages_range(vma, addr, start);
Rik van Rielba470de2008-10-18 20:26:50 -07002029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return vma;
2031}
2032#endif
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002035 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002037 *
2038 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002040static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Hugh Dickins365e9c872005-10-29 18:16:18 -07002042 /* Update high watermark before we lower total_vm */
2043 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002045 long nrpages = vma_pages(vma);
2046
2047 mm->total_vm -= nrpages;
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002048 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002049 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002050 } while (vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 validate_mm(mm);
2052}
2053
2054/*
2055 * Get rid of page table information in the indicated region.
2056 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002057 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
2059static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002060 struct vm_area_struct *vma, struct vm_area_struct *prev,
2061 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Hugh Dickinse0da3822005-04-19 13:29:15 -07002063 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002064 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 unsigned long nr_accounted = 0;
2066
2067 lru_add_drain();
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002068 tlb_gather_mmu(&tlb, mm, 0);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002069 update_hiwater_rss(mm);
Hugh Dickins508034a2005-10-29 18:16:30 -07002070 unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 vm_unacct_memory(nr_accounted);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002072 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins188b13f2013-04-29 15:07:44 -07002073 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002074 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
2077/*
2078 * Create a list of vma's touched by the unmap, removing them from the mm's
2079 * vma list as we go..
2080 */
2081static void
2082detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2083 struct vm_area_struct *prev, unsigned long end)
2084{
2085 struct vm_area_struct **insertion_point;
2086 struct vm_area_struct *tail_vma = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002087 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002090 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 do {
2092 rb_erase(&vma->vm_rb, &mm->mm_rb);
2093 mm->map_count--;
2094 tail_vma = vma;
2095 vma = vma->vm_next;
2096 } while (vma && vma->vm_start < end);
2097 *insertion_point = vma;
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002098 if (vma)
2099 vma->vm_prev = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 tail_vma->vm_next = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002101 if (mm->unmap_area == arch_unmap_area)
2102 addr = prev ? prev->vm_end : mm->mmap_base;
2103 else
2104 addr = vma ? vma->vm_start : mm->mmap_base;
2105 mm->unmap_area(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 mm->mmap_cache = NULL; /* Kill the cache. */
2107}
2108
2109/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002110 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2111 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 */
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002113static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 unsigned long addr, int new_below)
2115{
2116 struct mempolicy *pol;
2117 struct vm_area_struct *new;
Rik van Riel5beb4932010-03-05 13:42:07 -08002118 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Andi Kleena5516432008-07-23 21:27:41 -07002120 if (is_vm_hugetlb_page(vma) && (addr &
2121 ~(huge_page_mask(hstate_vma(vma)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return -EINVAL;
2123
Christoph Lametere94b1762006-12-06 20:33:17 -08002124 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (!new)
Rik van Riel5beb4932010-03-05 13:42:07 -08002126 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* most fields are the same, copy all, and then fixup */
2129 *new = *vma;
2130
Rik van Riel5beb4932010-03-05 13:42:07 -08002131 INIT_LIST_HEAD(&new->anon_vma_chain);
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (new_below)
2134 new->vm_end = addr;
2135 else {
2136 new->vm_start = addr;
2137 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2138 }
2139
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07002140 pol = mpol_dup(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (IS_ERR(pol)) {
Rik van Riel5beb4932010-03-05 13:42:07 -08002142 err = PTR_ERR(pol);
2143 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 vma_set_policy(new, pol);
2146
Rik van Riel5beb4932010-03-05 13:42:07 -08002147 if (anon_vma_clone(new, vma))
2148 goto out_free_mpol;
2149
Matt Helsley925d1c42008-04-29 01:01:36 -07002150 if (new->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 get_file(new->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07002152 if (vma->vm_flags & VM_EXECUTABLE)
2153 added_exe_file_vma(mm);
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (new->vm_ops && new->vm_ops->open)
2157 new->vm_ops->open(new);
2158
2159 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002160 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2162 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002163 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Rik van Riel5beb4932010-03-05 13:42:07 -08002165 /* Success. */
2166 if (!err)
2167 return 0;
2168
2169 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002170 if (new->vm_ops && new->vm_ops->close)
2171 new->vm_ops->close(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002172 if (new->vm_file) {
2173 if (vma->vm_flags & VM_EXECUTABLE)
2174 removed_exe_file_vma(mm);
2175 fput(new->vm_file);
2176 }
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002177 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002178 out_free_mpol:
2179 mpol_put(pol);
2180 out_free_vma:
2181 kmem_cache_free(vm_area_cachep, new);
2182 out_err:
2183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002186/*
2187 * Split a vma into two pieces at address 'addr', a new vma is allocated
2188 * either for the first part or the tail.
2189 */
2190int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2191 unsigned long addr, int new_below)
2192{
2193 if (mm->map_count >= sysctl_max_map_count)
2194 return -ENOMEM;
2195
2196 return __split_vma(mm, vma, addr, new_below);
2197}
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199/* Munmap is split into 2 main parts -- this part which finds
2200 * what needs doing, and the areas themselves, which do the
2201 * work. This now handles partial unmappings.
2202 * Jeremy Fitzhardinge <jeremy@goop.org>
2203 */
2204int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2205{
2206 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002207 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
2210 return -EINVAL;
2211
2212 if ((len = PAGE_ALIGN(len)) == 0)
2213 return -EINVAL;
2214
2215 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002216 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002217 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002219 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002220 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 /* if it doesn't overlap, we have nothing.. */
2223 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002224 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 0;
2226
2227 /*
2228 * If we need to split any vma, do it now to save pain later.
2229 *
2230 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2231 * unmapped vm_area_struct will remain in use: so lower split_vma
2232 * places tmp vma above, and higher split_vma places tmp vma below.
2233 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002234 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002235 int error;
2236
2237 /*
2238 * Make sure that map_count on return from munmap() will
2239 * not exceed its limit; but let map_count go just above
2240 * its limit temporarily, to help free resources as expected.
2241 */
2242 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2243 return -ENOMEM;
2244
2245 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (error)
2247 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002248 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250
2251 /* Does it split the last one? */
2252 last = find_vma(mm, end);
2253 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002254 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (error)
2256 return error;
2257 }
Hugh Dickins146425a2005-04-19 13:29:18 -07002258 vma = prev? prev->vm_next: mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002261 * unlock any mlock()ed ranges before detaching vmas
2262 */
2263 if (mm->locked_vm) {
2264 struct vm_area_struct *tmp = vma;
2265 while (tmp && tmp->vm_start < end) {
2266 if (tmp->vm_flags & VM_LOCKED) {
2267 mm->locked_vm -= vma_pages(tmp);
2268 munlock_vma_pages_all(tmp);
2269 }
2270 tmp = tmp->vm_next;
2271 }
2272 }
2273
2274 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * Remove the vma's, and unmap the actual pages
2276 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002277 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2278 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002281 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 return 0;
2284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285EXPORT_SYMBOL(do_munmap);
2286
Al Virobfce2812012-04-20 21:57:04 -04002287int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002288{
2289 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002290 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002291
2292 down_write(&mm->mmap_sem);
2293 ret = do_munmap(mm, start, len);
2294 up_write(&mm->mmap_sem);
2295 return ret;
2296}
2297EXPORT_SYMBOL(vm_munmap);
2298
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002299SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 profile_munmap(addr);
Al Virobfce2812012-04-20 21:57:04 -04002302 return vm_munmap(addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
2305static inline void verify_mm_writelocked(struct mm_struct *mm)
2306{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002307#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2309 WARN_ON(1);
2310 up_read(&mm->mmap_sem);
2311 }
2312#endif
2313}
2314
2315/*
2316 * this is really a simplified "do_mmap". it only handles
2317 * anonymous maps. eventually we may be able to do some
2318 * brk-specific accounting here.
2319 */
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002320static unsigned long do_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 struct mm_struct * mm = current->mm;
2323 struct vm_area_struct * vma, * prev;
2324 unsigned long flags;
2325 struct rb_node ** rb_link, * rb_parent;
2326 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002327 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 len = PAGE_ALIGN(len);
2330 if (!len)
2331 return addr;
2332
Richard Knutsson88c3f7a2007-12-08 12:02:48 +01002333 error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
Eric Paris5a211a52007-12-04 11:06:55 -05002334 if (error)
2335 return error;
2336
Kirill Korotaev3a459752006-09-07 14:17:04 +04002337 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2338
Al Viro2c6a10162009-12-03 19:40:46 -05002339 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
2340 if (error & ~PAGE_MASK)
Kirill Korotaev3a459752006-09-07 14:17:04 +04002341 return error;
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /*
2344 * mlock MCL_FUTURE?
2345 */
2346 if (mm->def_flags & VM_LOCKED) {
2347 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07002348 locked = len >> PAGE_SHIFT;
2349 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002350 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07002351 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
2353 return -EAGAIN;
2354 }
2355
2356 /*
2357 * mm->mmap_sem is required to protect against another thread
2358 * changing the mappings in case we sleep.
2359 */
2360 verify_mm_writelocked(mm);
2361
2362 /*
2363 * Clear old maps. this also does some error checking for us
2364 */
2365 munmap_back:
2366 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2367 if (vma && vma->vm_start < addr + len) {
2368 if (do_munmap(mm, addr, len))
2369 return -ENOMEM;
2370 goto munmap_back;
2371 }
2372
2373 /* Check against address space limits *after* clearing old maps... */
akpm@osdl.org119f6572005-05-01 08:58:35 -07002374 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return -ENOMEM;
2376
2377 if (mm->map_count > sysctl_max_map_count)
2378 return -ENOMEM;
2379
Al Viro191c5422012-02-13 03:58:52 +00002380 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return -ENOMEM;
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002384 vma = vma_merge(mm, prev, addr, addr + len, flags,
Colin Crossa9e6b182013-06-26 17:26:01 -07002385 NULL, NULL, pgoff, NULL, NULL);
Rik van Rielba470de2008-10-18 20:26:50 -07002386 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto out;
2388
2389 /*
2390 * create a vma struct for an anonymous mapping
2391 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002392 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (!vma) {
2394 vm_unacct_memory(len >> PAGE_SHIFT);
2395 return -ENOMEM;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Rik van Riel5beb4932010-03-05 13:42:07 -08002398 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 vma->vm_mm = mm;
2400 vma->vm_start = addr;
2401 vma->vm_end = addr + len;
2402 vma->vm_pgoff = pgoff;
2403 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002404 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 vma_link(mm, vma, prev, rb_link, rb_parent);
2406out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002407 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 mm->total_vm += len >> PAGE_SHIFT;
2409 if (flags & VM_LOCKED) {
Rik van Rielba470de2008-10-18 20:26:50 -07002410 if (!mlock_vma_pages_range(vma, addr, addr + len))
2411 mm->locked_vm += (len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413 return addr;
2414}
2415
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002416unsigned long vm_brk(unsigned long addr, unsigned long len)
2417{
2418 struct mm_struct *mm = current->mm;
2419 unsigned long ret;
2420
2421 down_write(&mm->mmap_sem);
2422 ret = do_brk(addr, len);
2423 up_write(&mm->mmap_sem);
2424 return ret;
2425}
2426EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428/* Release all mmaps. */
2429void exit_mmap(struct mm_struct *mm)
2430{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002431 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002432 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 unsigned long nr_accounted = 0;
2434
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002435 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002436 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002437
Rik van Rielba470de2008-10-18 20:26:50 -07002438 if (mm->locked_vm) {
2439 vma = mm->mmap;
2440 while (vma) {
2441 if (vma->vm_flags & VM_LOCKED)
2442 munlock_vma_pages_all(vma);
2443 vma = vma->vm_next;
2444 }
2445 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002446
2447 arch_exit_mmap(mm);
2448
Rik van Rielba470de2008-10-18 20:26:50 -07002449 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002450 if (!vma) /* Can happen if dup_mmap() received an OOM */
2451 return;
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 flush_cache_mm(mm);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002455 tlb_gather_mmu(&tlb, mm, 1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08002456 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07002457 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Al Viro6e8bb012012-03-05 13:41:15 -05002458 unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 vm_unacct_memory(nr_accounted);
Hugh Dickins9ba69292009-09-21 17:02:20 -07002460
Hugh Dickins188b13f2013-04-29 15:07:44 -07002461 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05002462 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07002465 * Walk the list again, actually closing and freeing it,
2466 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002468 while (vma)
2469 vma = remove_vma(vma);
Hugh Dickinse0da3822005-04-19 13:29:15 -07002470
Hugh Dickinse2cdef82005-04-19 13:29:19 -07002471 BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474/* Insert vm structure into process list sorted by address
2475 * and into the inode's i_mmap tree. If vm_file is non-NULL
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002476 * then i_mmap_mutex is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 */
2478int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
2479{
2480 struct vm_area_struct * __vma, * prev;
2481 struct rb_node ** rb_link, * rb_parent;
2482
2483 /*
2484 * The vm_pgoff of a purely anonymous vma should be irrelevant
2485 * until its first write fault, when page's anon_vma and index
2486 * are set. But now set the vm_pgoff it will almost certainly
2487 * end up with (unless mremap moves it elsewhere before that
2488 * first wfault), so /proc/pid/maps tells a consistent story.
2489 *
2490 * By setting it to reflect the virtual start address of the
2491 * vma, merges and splits can happen in a seamless way, just
2492 * using the existing file pgoff checks and manipulations.
2493 * Similarly in do_mmap_pgoff and in do_brk.
2494 */
2495 if (!vma->vm_file) {
2496 BUG_ON(vma->anon_vma);
2497 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2498 }
2499 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2500 if (__vma && __vma->vm_start < vma->vm_end)
2501 return -ENOMEM;
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002502 if ((vma->vm_flags & VM_ACCOUNT) &&
Alan Cox34b4e4a2007-08-22 14:01:28 -07002503 security_vm_enough_memory_mm(mm, vma_pages(vma)))
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002504 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 vma_link(mm, vma, prev, rb_link, rb_parent);
2506 return 0;
2507}
2508
2509/*
2510 * Copy the vma structure to a new location in the same mm,
2511 * prior to moving page table entries, to effect an mremap move.
2512 */
2513struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2514 unsigned long addr, unsigned long len, pgoff_t pgoff)
2515{
2516 struct vm_area_struct *vma = *vmap;
2517 unsigned long vma_start = vma->vm_start;
2518 struct mm_struct *mm = vma->vm_mm;
2519 struct vm_area_struct *new_vma, *prev;
2520 struct rb_node **rb_link, *rb_parent;
2521 struct mempolicy *pol;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002522 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 /*
2525 * If anonymous vma has not yet been faulted, update new pgoff
2526 * to match new location, to increase its chance of merging.
2527 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002528 if (unlikely(!vma->vm_file && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002530 faulted_in_anon_vma = false;
2531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2534 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
Colin Crossa9e6b182013-06-26 17:26:01 -07002535 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
2536 vma_get_anon_name(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (new_vma) {
2538 /*
2539 * Source vma may have been merged into new_vma
2540 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002541 if (unlikely(vma_start >= new_vma->vm_start &&
2542 vma_start < new_vma->vm_end)) {
2543 /*
2544 * The only way we can get a vma_merge with
2545 * self during an mremap is if the vma hasn't
2546 * been faulted in yet and we were allowed to
2547 * reset the dst vma->vm_pgoff to the
2548 * destination address of the mremap to allow
2549 * the merge to happen. mremap must change the
2550 * vm_pgoff linearity between src and dst vmas
2551 * (in turn preventing a vma_merge) to be
2552 * safe. It is only safe to keep the vm_pgoff
2553 * linear if there are no pages mapped yet.
2554 */
2555 VM_BUG_ON(faulted_in_anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 *vmap = new_vma;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002557 } else
2558 anon_vma_moveto_tail(new_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08002560 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (new_vma) {
2562 *new_vma = *vma;
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07002563 pol = mpol_dup(vma_policy(vma));
Rik van Riel5beb4932010-03-05 13:42:07 -08002564 if (IS_ERR(pol))
2565 goto out_free_vma;
2566 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2567 if (anon_vma_clone(new_vma, vma))
2568 goto out_free_mempol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 vma_set_policy(new_vma, pol);
2570 new_vma->vm_start = addr;
2571 new_vma->vm_end = addr + len;
2572 new_vma->vm_pgoff = pgoff;
Matt Helsley925d1c42008-04-29 01:01:36 -07002573 if (new_vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 get_file(new_vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07002575 if (vma->vm_flags & VM_EXECUTABLE)
2576 added_exe_file_vma(mm);
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (new_vma->vm_ops && new_vma->vm_ops->open)
2579 new_vma->vm_ops->open(new_vma);
2580 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2581 }
2582 }
2583 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002584
2585 out_free_mempol:
2586 mpol_put(pol);
2587 out_free_vma:
2588 kmem_cache_free(vm_area_cachep, new_vma);
2589 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002591
2592/*
2593 * Return true if the calling process may expand its vm space by the passed
2594 * number of pages
2595 */
2596int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2597{
2598 unsigned long cur = mm->total_vm; /* pages */
2599 unsigned long lim;
2600
Jiri Slaby59e99e52010-03-05 13:41:44 -08002601 lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002602
2603 if (cur + npages > lim)
2604 return 0;
2605 return 1;
2606}
Roland McGrathfa5dc222007-02-08 14:20:41 -08002607
2608
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002609static int special_mapping_fault(struct vm_area_struct *vma,
2610 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08002611{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002612 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002613 struct page **pages;
2614
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002615 /*
2616 * special mappings have no vm_file, and in that case, the mm
2617 * uses vm_pgoff internally. So we have to subtract it from here.
2618 * We are allowed to do this because we are the mm; do not copy
2619 * this code into drivers!
2620 */
2621 pgoff = vmf->pgoff - vma->vm_pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002622
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002623 for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2624 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002625
2626 if (*pages) {
2627 struct page *page = *pages;
2628 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002629 vmf->page = page;
2630 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002631 }
2632
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002633 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002634}
2635
2636/*
2637 * Having a close hook prevents vma merging regardless of flags.
2638 */
2639static void special_mapping_close(struct vm_area_struct *vma)
2640{
2641}
2642
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002643static const struct vm_operations_struct special_mapping_vmops = {
Roland McGrathfa5dc222007-02-08 14:20:41 -08002644 .close = special_mapping_close,
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002645 .fault = special_mapping_fault,
Roland McGrathfa5dc222007-02-08 14:20:41 -08002646};
2647
2648/*
2649 * Called with mm->mmap_sem held for writing.
2650 * Insert a new vma covering the given region, with the given flags.
2651 * Its pages are supplied by the given array of struct page *.
2652 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2653 * The region past the last page supplied will always produce SIGBUS.
2654 * The array pointer and the pages it points to are assumed to stay alive
2655 * for as long as this mapping might exist.
2656 */
2657int install_special_mapping(struct mm_struct *mm,
2658 unsigned long addr, unsigned long len,
2659 unsigned long vm_flags, struct page **pages)
2660{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002661 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002662 struct vm_area_struct *vma;
2663
2664 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2665 if (unlikely(vma == NULL))
2666 return -ENOMEM;
2667
Rik van Riel5beb4932010-03-05 13:42:07 -08002668 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002669 vma->vm_mm = mm;
2670 vma->vm_start = addr;
2671 vma->vm_end = addr + len;
2672
Nick Piggin2f987352008-02-02 03:08:53 +01002673 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
Coly Li3ed75eb2007-10-18 23:39:15 -07002674 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002675
2676 vma->vm_ops = &special_mapping_vmops;
2677 vma->vm_private_data = pages;
2678
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002679 ret = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1);
2680 if (ret)
2681 goto out;
2682
2683 ret = insert_vm_struct(mm, vma);
2684 if (ret)
2685 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002686
2687 mm->total_vm += len >> PAGE_SHIFT;
2688
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002689 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02002690
Roland McGrathfa5dc222007-02-08 14:20:41 -08002691 return 0;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002692
2693out:
2694 kmem_cache_free(vm_area_cachep, vma);
2695 return ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002696}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002697
2698static DEFINE_MUTEX(mm_all_locks_mutex);
2699
Peter Zijlstra454ed842008-08-11 09:30:25 +02002700static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002701{
Rik van Riel012f1802010-08-09 17:18:40 -07002702 if (!test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002703 /*
2704 * The LSB of head.next can't change from under us
2705 * because we hold the mm_all_locks_mutex.
2706 */
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002707 mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002708 /*
2709 * We can safely modify head.next after taking the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002710 * anon_vma->root->mutex. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002711 * the same anon_vma we won't take it again.
2712 *
2713 * No need of atomic instructions here, head.next
2714 * can't change from under us thanks to the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002715 * anon_vma->root->mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002716 */
2717 if (__test_and_set_bit(0, (unsigned long *)
Rik van Riel012f1802010-08-09 17:18:40 -07002718 &anon_vma->root->head.next))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002719 BUG();
2720 }
2721}
2722
Peter Zijlstra454ed842008-08-11 09:30:25 +02002723static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002724{
2725 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2726 /*
2727 * AS_MM_ALL_LOCKS can't change from under us because
2728 * we hold the mm_all_locks_mutex.
2729 *
2730 * Operations on ->flags have to be atomic because
2731 * even if AS_MM_ALL_LOCKS is stable thanks to the
2732 * mm_all_locks_mutex, there may be other cpus
2733 * changing other bitflags in parallel to us.
2734 */
2735 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2736 BUG();
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002737 mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002738 }
2739}
2740
2741/*
2742 * This operation locks against the VM for all pte/vma/mm related
2743 * operations that could ever happen on a certain mm. This includes
2744 * vmtruncate, try_to_unmap, and all page faults.
2745 *
2746 * The caller must take the mmap_sem in write mode before calling
2747 * mm_take_all_locks(). The caller isn't allowed to release the
2748 * mmap_sem until mm_drop_all_locks() returns.
2749 *
2750 * mmap_sem in write mode is required in order to block all operations
2751 * that could modify pagetables and free pages without need of
2752 * altering the vma layout (for example populate_range() with
2753 * nonlinear vmas). It's also needed in write mode to avoid new
2754 * anon_vmas to be associated with existing vmas.
2755 *
2756 * A single task can't take more than one mm_take_all_locks() in a row
2757 * or it would deadlock.
2758 *
2759 * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
2760 * mapping->flags avoid to take the same lock twice, if more than one
2761 * vma in this mm is backed by the same anon_vma or address_space.
2762 *
2763 * We can take all the locks in random order because the VM code
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002764 * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002765 * takes more than one of them in a row. Secondly we're protected
2766 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2767 *
2768 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2769 * that may have to take thousand of locks.
2770 *
2771 * mm_take_all_locks() can fail if it's interrupted by signals.
2772 */
2773int mm_take_all_locks(struct mm_struct *mm)
2774{
2775 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002776 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002777
2778 BUG_ON(down_read_trylock(&mm->mmap_sem));
2779
2780 mutex_lock(&mm_all_locks_mutex);
2781
2782 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2783 if (signal_pending(current))
2784 goto out_unlock;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002785 if (vma->vm_file && vma->vm_file->f_mapping)
Peter Zijlstra454ed842008-08-11 09:30:25 +02002786 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002787 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02002788
2789 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2790 if (signal_pending(current))
2791 goto out_unlock;
2792 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08002793 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2794 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02002795 }
2796
Kautuk Consul584cff52011-10-31 17:08:59 -07002797 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002798
2799out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07002800 mm_drop_all_locks(mm);
2801 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002802}
2803
2804static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
2805{
Rik van Riel012f1802010-08-09 17:18:40 -07002806 if (test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002807 /*
2808 * The LSB of head.next can't change to 0 from under
2809 * us because we hold the mm_all_locks_mutex.
2810 *
2811 * We must however clear the bitflag before unlocking
2812 * the vma so the users using the anon_vma->head will
2813 * never see our bitflag.
2814 *
2815 * No need of atomic instructions here, head.next
2816 * can't change from under us until we release the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002817 * anon_vma->root->mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002818 */
2819 if (!__test_and_clear_bit(0, (unsigned long *)
Rik van Riel012f1802010-08-09 17:18:40 -07002820 &anon_vma->root->head.next))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002821 BUG();
Rik van Rielcba48b92010-08-09 17:18:38 -07002822 anon_vma_unlock(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002823 }
2824}
2825
2826static void vm_unlock_mapping(struct address_space *mapping)
2827{
2828 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2829 /*
2830 * AS_MM_ALL_LOCKS can't change to 0 from under us
2831 * because we hold the mm_all_locks_mutex.
2832 */
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002833 mutex_unlock(&mapping->i_mmap_mutex);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002834 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
2835 &mapping->flags))
2836 BUG();
2837 }
2838}
2839
2840/*
2841 * The mmap_sem cannot be released by the caller until
2842 * mm_drop_all_locks() returns.
2843 */
2844void mm_drop_all_locks(struct mm_struct *mm)
2845{
2846 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002847 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002848
2849 BUG_ON(down_read_trylock(&mm->mmap_sem));
2850 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
2851
2852 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2853 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08002854 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2855 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002856 if (vma->vm_file && vma->vm_file->f_mapping)
2857 vm_unlock_mapping(vma->vm_file->f_mapping);
2858 }
2859
2860 mutex_unlock(&mm_all_locks_mutex);
2861}
David Howells8feae132009-01-08 12:04:47 +00002862
2863/*
2864 * initialise the VMA slab
2865 */
2866void __init mmap_init(void)
2867{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07002868 int ret;
2869
2870 ret = percpu_counter_init(&vm_committed_as, 0);
2871 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00002872}